push a1eb882dd4a6a88419e75ca032b34641780365a0
[wine/hacks.git] / dlls / kernel32 / sync.c
blob7c15fd800dd3c159d57e17f965d816585270c0ab
1 /*
2 * Kernel synchronization objects
4 * Copyright 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <errno.h>
29 #include <stdarg.h>
30 #include <stdio.h>
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "winnls.h"
41 #include "winternl.h"
42 #include "winioctl.h"
43 #include "ddk/wdm.h"
45 #include "wine/unicode.h"
46 #include "wine/winbase16.h"
47 #include "kernel_private.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(sync);
53 /* check if current version is NT or Win95 */
54 static inline int is_version_nt(void)
56 return !(GetVersion() & 0x80000000);
59 /* returns directory handle to \\BaseNamedObjects */
60 HANDLE get_BaseNamedObjects_handle(void)
62 static HANDLE handle = NULL;
63 static const WCHAR basenameW[] =
64 {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
65 UNICODE_STRING str;
66 OBJECT_ATTRIBUTES attr;
68 if (!handle)
70 HANDLE dir;
72 RtlInitUnicodeString(&str, basenameW);
73 InitializeObjectAttributes(&attr, &str, 0, 0, NULL);
74 NtOpenDirectoryObject(&dir, DIRECTORY_CREATE_OBJECT|DIRECTORY_TRAVERSE,
75 &attr);
76 if (InterlockedCompareExchangePointer( (PVOID)&handle, dir, 0 ) != 0)
78 /* someone beat us here... */
79 CloseHandle( dir );
82 return handle;
85 /***********************************************************************
86 * Sleep (KERNEL32.@)
88 VOID WINAPI Sleep( DWORD timeout )
90 SleepEx( timeout, FALSE );
93 /******************************************************************************
94 * SleepEx (KERNEL32.@)
96 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
98 NTSTATUS status;
100 if (timeout == INFINITE) status = NtDelayExecution( alertable, NULL );
101 else
103 LARGE_INTEGER time;
105 time.QuadPart = timeout * (ULONGLONG)10000;
106 time.QuadPart = -time.QuadPart;
107 status = NtDelayExecution( alertable, &time );
109 if (status != STATUS_USER_APC) status = STATUS_SUCCESS;
110 return status;
114 /***********************************************************************
115 * SwitchToThread (KERNEL32.@)
117 BOOL WINAPI SwitchToThread(void)
119 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
123 /***********************************************************************
124 * WaitForSingleObject (KERNEL32.@)
126 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
128 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
132 /***********************************************************************
133 * WaitForSingleObjectEx (KERNEL32.@)
135 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
136 BOOL alertable )
138 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
142 /***********************************************************************
143 * WaitForMultipleObjects (KERNEL32.@)
145 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
146 BOOL wait_all, DWORD timeout )
148 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
152 /***********************************************************************
153 * WaitForMultipleObjectsEx (KERNEL32.@)
155 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
156 BOOL wait_all, DWORD timeout,
157 BOOL alertable )
159 NTSTATUS status;
160 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
161 unsigned int i;
163 if (count > MAXIMUM_WAIT_OBJECTS)
165 SetLastError(ERROR_INVALID_PARAMETER);
166 return WAIT_FAILED;
168 for (i = 0; i < count; i++)
170 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
171 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
172 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
173 hloc[i] = GetStdHandle( (DWORD)handles[i] );
174 else
175 hloc[i] = handles[i];
177 /* yes, even screen buffer console handles are waitable, and are
178 * handled as a handle to the console itself !!
180 if (is_console_handle(hloc[i]))
182 if (!VerifyConsoleIoHandle(hloc[i]))
184 return FALSE;
186 hloc[i] = GetConsoleInputWaitHandle();
190 if (timeout == INFINITE)
192 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, NULL );
194 else
196 LARGE_INTEGER time;
198 time.QuadPart = timeout * (ULONGLONG)10000;
199 time.QuadPart = -time.QuadPart;
200 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, &time );
203 if (HIWORD(status)) /* is it an error code? */
205 SetLastError( RtlNtStatusToDosError(status) );
206 status = WAIT_FAILED;
208 return status;
212 /***********************************************************************
213 * WaitForSingleObject (KERNEL.460)
215 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
217 DWORD retval, mutex_count;
219 ReleaseThunkLock( &mutex_count );
220 retval = WaitForSingleObject( handle, timeout );
221 RestoreThunkLock( mutex_count );
222 return retval;
225 /***********************************************************************
226 * WaitForMultipleObjects (KERNEL.461)
228 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
229 BOOL wait_all, DWORD timeout )
231 DWORD retval, mutex_count;
233 ReleaseThunkLock( &mutex_count );
234 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
235 RestoreThunkLock( mutex_count );
236 return retval;
239 /***********************************************************************
240 * WaitForMultipleObjectsEx (KERNEL.495)
242 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
243 BOOL wait_all, DWORD timeout, BOOL alertable )
245 DWORD retval, mutex_count;
247 ReleaseThunkLock( &mutex_count );
248 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
249 RestoreThunkLock( mutex_count );
250 return retval;
253 /***********************************************************************
254 * RegisterWaitForSingleObject (KERNEL32.@)
256 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
257 WAITORTIMERCALLBACK Callback, PVOID Context,
258 ULONG dwMilliseconds, ULONG dwFlags)
260 FIXME("%p %p %p %p %d %d\n",
261 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
262 return FALSE;
265 /***********************************************************************
266 * RegisterWaitForSingleObjectEx (KERNEL32.@)
268 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
269 WAITORTIMERCALLBACK Callback, PVOID Context,
270 ULONG dwMilliseconds, ULONG dwFlags )
272 FIXME("%p %p %p %d %d\n",
273 hObject,Callback,Context,dwMilliseconds,dwFlags);
274 return 0;
277 /***********************************************************************
278 * UnregisterWait (KERNEL32.@)
280 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
282 FIXME("%p\n",WaitHandle);
283 return FALSE;
286 /***********************************************************************
287 * UnregisterWaitEx (KERNEL32.@)
289 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
291 FIXME("%p %p\n",WaitHandle, CompletionEvent);
292 return FALSE;
295 /***********************************************************************
296 * SignalObjectAndWait (KERNEL32.@)
298 * Allows to atomically signal any of the synchro objects (semaphore,
299 * mutex, event) and wait on another.
301 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
302 DWORD dwMilliseconds, BOOL bAlertable )
304 NTSTATUS status;
305 LARGE_INTEGER timeout, *ptimeout = NULL;
307 TRACE("%p %p %d %d\n", hObjectToSignal,
308 hObjectToWaitOn, dwMilliseconds, bAlertable);
310 if (dwMilliseconds != INFINITE)
312 timeout.QuadPart = dwMilliseconds * (ULONGLONG)10000;
313 timeout.QuadPart = -timeout.QuadPart;
314 ptimeout = &timeout;
317 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn,
318 bAlertable, ptimeout );
319 if (HIWORD(status))
321 SetLastError( RtlNtStatusToDosError(status) );
322 status = WAIT_FAILED;
324 return status;
327 /***********************************************************************
328 * InitializeCriticalSection (KERNEL32.@)
330 * Initialise a critical section before use.
332 * PARAMS
333 * crit [O] Critical section to initialise.
335 * RETURNS
336 * Nothing. If the function fails an exception is raised.
338 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
340 NTSTATUS ret = RtlInitializeCriticalSection( crit );
341 if (ret) RtlRaiseStatus( ret );
344 /***********************************************************************
345 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
347 * Initialise a critical section with a spin count.
349 * PARAMS
350 * crit [O] Critical section to initialise.
351 * spincount [I] Number of times to spin upon contention.
353 * RETURNS
354 * Success: TRUE.
355 * Failure: Nothing. If the function fails an exception is raised.
357 * NOTES
358 * spincount is ignored on uni-processor systems.
360 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
362 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
363 if (ret) RtlRaiseStatus( ret );
364 return !ret;
367 /***********************************************************************
368 * MakeCriticalSectionGlobal (KERNEL32.@)
370 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
372 /* let's assume that only one thread at a time will try to do this */
373 HANDLE sem = crit->LockSemaphore;
374 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
375 crit->LockSemaphore = ConvertToGlobalHandle( sem );
376 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
377 crit->DebugInfo = NULL;
381 /***********************************************************************
382 * ReinitializeCriticalSection (KERNEL32.@)
384 * Initialise an already used critical section.
386 * PARAMS
387 * crit [O] Critical section to initialise.
389 * RETURNS
390 * Nothing.
392 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
394 if ( !crit->LockSemaphore )
395 RtlInitializeCriticalSection( crit );
399 /***********************************************************************
400 * UninitializeCriticalSection (KERNEL32.@)
402 * UnInitialise a critical section after use.
404 * PARAMS
405 * crit [O] Critical section to uninitialise (destroy).
407 * RETURNS
408 * Nothing.
410 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
412 RtlDeleteCriticalSection( crit );
416 /***********************************************************************
417 * CreateEventA (KERNEL32.@)
419 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
420 BOOL initial_state, LPCSTR name )
422 WCHAR buffer[MAX_PATH];
424 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
426 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
428 SetLastError( ERROR_FILENAME_EXCED_RANGE );
429 return 0;
431 return CreateEventW( sa, manual_reset, initial_state, buffer );
435 /***********************************************************************
436 * CreateEventW (KERNEL32.@)
438 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
439 BOOL initial_state, LPCWSTR name )
441 HANDLE ret;
442 UNICODE_STRING nameW;
443 OBJECT_ATTRIBUTES attr;
444 NTSTATUS status;
446 /* one buggy program needs this
447 * ("Van Dale Groot woordenboek der Nederlandse taal")
449 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
451 ERR("Bad security attributes pointer %p\n",sa);
452 SetLastError( ERROR_INVALID_PARAMETER);
453 return 0;
456 attr.Length = sizeof(attr);
457 attr.RootDirectory = 0;
458 attr.ObjectName = NULL;
459 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
460 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
461 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
462 attr.SecurityQualityOfService = NULL;
463 if (name)
465 RtlInitUnicodeString( &nameW, name );
466 attr.ObjectName = &nameW;
467 attr.RootDirectory = get_BaseNamedObjects_handle();
470 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
471 if (status == STATUS_OBJECT_NAME_EXISTS)
472 SetLastError( ERROR_ALREADY_EXISTS );
473 else
474 SetLastError( RtlNtStatusToDosError(status) );
475 return ret;
479 /***********************************************************************
480 * CreateW32Event (KERNEL.457)
482 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
484 return CreateEventW( NULL, manual_reset, initial_state, NULL );
488 /***********************************************************************
489 * OpenEventA (KERNEL32.@)
491 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
493 WCHAR buffer[MAX_PATH];
495 if (!name) return OpenEventW( access, inherit, NULL );
497 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
499 SetLastError( ERROR_FILENAME_EXCED_RANGE );
500 return 0;
502 return OpenEventW( access, inherit, buffer );
506 /***********************************************************************
507 * OpenEventW (KERNEL32.@)
509 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
511 HANDLE ret;
512 UNICODE_STRING nameW;
513 OBJECT_ATTRIBUTES attr;
514 NTSTATUS status;
516 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
518 attr.Length = sizeof(attr);
519 attr.RootDirectory = 0;
520 attr.ObjectName = NULL;
521 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
522 attr.SecurityDescriptor = NULL;
523 attr.SecurityQualityOfService = NULL;
524 if (name)
526 RtlInitUnicodeString( &nameW, name );
527 attr.ObjectName = &nameW;
528 attr.RootDirectory = get_BaseNamedObjects_handle();
531 status = NtOpenEvent( &ret, access, &attr );
532 if (status != STATUS_SUCCESS)
534 SetLastError( RtlNtStatusToDosError(status) );
535 return 0;
537 return ret;
540 /***********************************************************************
541 * PulseEvent (KERNEL32.@)
543 BOOL WINAPI PulseEvent( HANDLE handle )
545 NTSTATUS status;
547 if ((status = NtPulseEvent( handle, NULL )))
548 SetLastError( RtlNtStatusToDosError(status) );
549 return !status;
553 /***********************************************************************
554 * SetW32Event (KERNEL.458)
555 * SetEvent (KERNEL32.@)
557 BOOL WINAPI SetEvent( HANDLE handle )
559 NTSTATUS status;
561 if ((status = NtSetEvent( handle, NULL )))
562 SetLastError( RtlNtStatusToDosError(status) );
563 return !status;
567 /***********************************************************************
568 * ResetW32Event (KERNEL.459)
569 * ResetEvent (KERNEL32.@)
571 BOOL WINAPI ResetEvent( HANDLE handle )
573 NTSTATUS status;
575 if ((status = NtResetEvent( handle, NULL )))
576 SetLastError( RtlNtStatusToDosError(status) );
577 return !status;
581 /***********************************************************************
582 * NOTE: The Win95 VWin32_Event routines given below are really low-level
583 * routines implemented directly by VWin32. The user-mode libraries
584 * implement Win32 synchronisation routines on top of these low-level
585 * primitives. We do it the other way around here :-)
588 /***********************************************************************
589 * VWin32_EventCreate (KERNEL.442)
591 HANDLE WINAPI VWin32_EventCreate(VOID)
593 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
594 return ConvertToGlobalHandle( hEvent );
597 /***********************************************************************
598 * VWin32_EventDestroy (KERNEL.443)
600 VOID WINAPI VWin32_EventDestroy(HANDLE event)
602 CloseHandle( event );
605 /***********************************************************************
606 * VWin32_EventWait (KERNEL.450)
608 VOID WINAPI VWin32_EventWait(HANDLE event)
610 DWORD mutex_count;
612 ReleaseThunkLock( &mutex_count );
613 WaitForSingleObject( event, INFINITE );
614 RestoreThunkLock( mutex_count );
617 /***********************************************************************
618 * VWin32_EventSet (KERNEL.451)
619 * KERNEL_479 (KERNEL.479)
621 VOID WINAPI VWin32_EventSet(HANDLE event)
623 SetEvent( event );
628 /***********************************************************************
629 * CreateMutexA (KERNEL32.@)
631 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
633 WCHAR buffer[MAX_PATH];
635 if (!name) return CreateMutexW( sa, owner, NULL );
637 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
639 SetLastError( ERROR_FILENAME_EXCED_RANGE );
640 return 0;
642 return CreateMutexW( sa, owner, buffer );
646 /***********************************************************************
647 * CreateMutexW (KERNEL32.@)
649 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
651 HANDLE ret;
652 UNICODE_STRING nameW;
653 OBJECT_ATTRIBUTES attr;
654 NTSTATUS status;
656 attr.Length = sizeof(attr);
657 attr.RootDirectory = 0;
658 attr.ObjectName = NULL;
659 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
660 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
661 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
662 attr.SecurityQualityOfService = NULL;
663 if (name)
665 RtlInitUnicodeString( &nameW, name );
666 attr.ObjectName = &nameW;
667 attr.RootDirectory = get_BaseNamedObjects_handle();
670 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
671 if (status == STATUS_OBJECT_NAME_EXISTS)
672 SetLastError( ERROR_ALREADY_EXISTS );
673 else
674 SetLastError( RtlNtStatusToDosError(status) );
675 return ret;
679 /***********************************************************************
680 * OpenMutexA (KERNEL32.@)
682 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
684 WCHAR buffer[MAX_PATH];
686 if (!name) return OpenMutexW( access, inherit, NULL );
688 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
690 SetLastError( ERROR_FILENAME_EXCED_RANGE );
691 return 0;
693 return OpenMutexW( access, inherit, buffer );
697 /***********************************************************************
698 * OpenMutexW (KERNEL32.@)
700 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
702 HANDLE ret;
703 UNICODE_STRING nameW;
704 OBJECT_ATTRIBUTES attr;
705 NTSTATUS status;
707 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
709 attr.Length = sizeof(attr);
710 attr.RootDirectory = 0;
711 attr.ObjectName = NULL;
712 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
713 attr.SecurityDescriptor = NULL;
714 attr.SecurityQualityOfService = NULL;
715 if (name)
717 RtlInitUnicodeString( &nameW, name );
718 attr.ObjectName = &nameW;
719 attr.RootDirectory = get_BaseNamedObjects_handle();
722 status = NtOpenMutant( &ret, access, &attr );
723 if (status != STATUS_SUCCESS)
725 SetLastError( RtlNtStatusToDosError(status) );
726 return 0;
728 return ret;
732 /***********************************************************************
733 * ReleaseMutex (KERNEL32.@)
735 BOOL WINAPI ReleaseMutex( HANDLE handle )
737 NTSTATUS status;
739 status = NtReleaseMutant(handle, NULL);
740 if (status != STATUS_SUCCESS)
742 SetLastError( RtlNtStatusToDosError(status) );
743 return FALSE;
745 return TRUE;
750 * Semaphores
754 /***********************************************************************
755 * CreateSemaphoreA (KERNEL32.@)
757 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
759 WCHAR buffer[MAX_PATH];
761 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
763 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
765 SetLastError( ERROR_FILENAME_EXCED_RANGE );
766 return 0;
768 return CreateSemaphoreW( sa, initial, max, buffer );
772 /***********************************************************************
773 * CreateSemaphoreW (KERNEL32.@)
775 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
776 LONG max, LPCWSTR name )
778 HANDLE ret;
779 UNICODE_STRING nameW;
780 OBJECT_ATTRIBUTES attr;
781 NTSTATUS status;
783 attr.Length = sizeof(attr);
784 attr.RootDirectory = 0;
785 attr.ObjectName = NULL;
786 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
787 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
788 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
789 attr.SecurityQualityOfService = NULL;
790 if (name)
792 RtlInitUnicodeString( &nameW, name );
793 attr.ObjectName = &nameW;
794 attr.RootDirectory = get_BaseNamedObjects_handle();
797 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
798 if (status == STATUS_OBJECT_NAME_EXISTS)
799 SetLastError( ERROR_ALREADY_EXISTS );
800 else
801 SetLastError( RtlNtStatusToDosError(status) );
802 return ret;
806 /***********************************************************************
807 * OpenSemaphoreA (KERNEL32.@)
809 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
811 WCHAR buffer[MAX_PATH];
813 if (!name) return OpenSemaphoreW( access, inherit, NULL );
815 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
817 SetLastError( ERROR_FILENAME_EXCED_RANGE );
818 return 0;
820 return OpenSemaphoreW( access, inherit, buffer );
824 /***********************************************************************
825 * OpenSemaphoreW (KERNEL32.@)
827 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
829 HANDLE ret;
830 UNICODE_STRING nameW;
831 OBJECT_ATTRIBUTES attr;
832 NTSTATUS status;
834 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
836 attr.Length = sizeof(attr);
837 attr.RootDirectory = 0;
838 attr.ObjectName = NULL;
839 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
840 attr.SecurityDescriptor = NULL;
841 attr.SecurityQualityOfService = NULL;
842 if (name)
844 RtlInitUnicodeString( &nameW, name );
845 attr.ObjectName = &nameW;
846 attr.RootDirectory = get_BaseNamedObjects_handle();
849 status = NtOpenSemaphore( &ret, access, &attr );
850 if (status != STATUS_SUCCESS)
852 SetLastError( RtlNtStatusToDosError(status) );
853 return 0;
855 return ret;
859 /***********************************************************************
860 * ReleaseSemaphore (KERNEL32.@)
862 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
864 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
865 if (status) SetLastError( RtlNtStatusToDosError(status) );
866 return !status;
871 * Timers
875 /***********************************************************************
876 * CreateWaitableTimerA (KERNEL32.@)
878 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
880 WCHAR buffer[MAX_PATH];
882 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
884 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
886 SetLastError( ERROR_FILENAME_EXCED_RANGE );
887 return 0;
889 return CreateWaitableTimerW( sa, manual, buffer );
893 /***********************************************************************
894 * CreateWaitableTimerW (KERNEL32.@)
896 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
898 HANDLE handle;
899 NTSTATUS status;
900 UNICODE_STRING nameW;
901 OBJECT_ATTRIBUTES attr;
903 attr.Length = sizeof(attr);
904 attr.RootDirectory = 0;
905 attr.ObjectName = NULL;
906 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
907 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
908 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
909 attr.SecurityQualityOfService = NULL;
910 if (name)
912 RtlInitUnicodeString( &nameW, name );
913 attr.ObjectName = &nameW;
914 attr.RootDirectory = get_BaseNamedObjects_handle();
917 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
918 manual ? NotificationTimer : SynchronizationTimer);
919 if (status == STATUS_OBJECT_NAME_EXISTS)
920 SetLastError( ERROR_ALREADY_EXISTS );
921 else
922 SetLastError( RtlNtStatusToDosError(status) );
923 return handle;
927 /***********************************************************************
928 * OpenWaitableTimerA (KERNEL32.@)
930 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
932 WCHAR buffer[MAX_PATH];
934 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
936 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
938 SetLastError( ERROR_FILENAME_EXCED_RANGE );
939 return 0;
941 return OpenWaitableTimerW( access, inherit, buffer );
945 /***********************************************************************
946 * OpenWaitableTimerW (KERNEL32.@)
948 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
950 HANDLE handle;
951 UNICODE_STRING nameW;
952 OBJECT_ATTRIBUTES attr;
953 NTSTATUS status;
955 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
957 attr.Length = sizeof(attr);
958 attr.RootDirectory = 0;
959 attr.ObjectName = NULL;
960 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
961 attr.SecurityDescriptor = NULL;
962 attr.SecurityQualityOfService = NULL;
963 if (name)
965 RtlInitUnicodeString( &nameW, name );
966 attr.ObjectName = &nameW;
967 attr.RootDirectory = get_BaseNamedObjects_handle();
970 status = NtOpenTimer(&handle, access, &attr);
971 if (status != STATUS_SUCCESS)
973 SetLastError( RtlNtStatusToDosError(status) );
974 return 0;
976 return handle;
980 /***********************************************************************
981 * SetWaitableTimer (KERNEL32.@)
983 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
984 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
986 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
987 arg, resume, period, NULL);
989 if (status != STATUS_SUCCESS)
991 SetLastError( RtlNtStatusToDosError(status) );
992 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
994 return TRUE;
998 /***********************************************************************
999 * CancelWaitableTimer (KERNEL32.@)
1001 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1003 NTSTATUS status;
1005 status = NtCancelTimer(handle, NULL);
1006 if (status != STATUS_SUCCESS)
1008 SetLastError( RtlNtStatusToDosError(status) );
1009 return FALSE;
1011 return TRUE;
1015 /***********************************************************************
1016 * CreateTimerQueue (KERNEL32.@)
1018 HANDLE WINAPI CreateTimerQueue(void)
1020 FIXME("stub\n");
1021 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1022 return NULL;
1026 /***********************************************************************
1027 * DeleteTimerQueueEx (KERNEL32.@)
1029 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1031 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
1032 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1033 return 0;
1036 /***********************************************************************
1037 * CreateTimerQueueTimer (KERNEL32.@)
1039 * Creates a timer-queue timer. This timer expires at the specified due
1040 * time (in ms), then after every specified period (in ms). When the timer
1041 * expires, the callback function is called.
1043 * RETURNS
1044 * nonzero on success or zero on faillure
1046 * BUGS
1047 * Unimplemented
1049 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1050 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1051 DWORD DueTime, DWORD Period, ULONG Flags )
1053 FIXME("stub\n");
1054 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1055 return TRUE;
1058 /***********************************************************************
1059 * DeleteTimerQueueTimer (KERNEL32.@)
1061 * Cancels a timer-queue timer.
1063 * RETURNS
1064 * nonzero on success or zero on faillure
1066 * BUGS
1067 * Unimplemented
1069 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1070 HANDLE CompletionEvent )
1072 FIXME("stub\n");
1073 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1074 return TRUE;
1079 * Pipes
1083 /***********************************************************************
1084 * CreateNamedPipeA (KERNEL32.@)
1086 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1087 DWORD dwPipeMode, DWORD nMaxInstances,
1088 DWORD nOutBufferSize, DWORD nInBufferSize,
1089 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1091 WCHAR buffer[MAX_PATH];
1093 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1094 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1096 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1098 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1099 return INVALID_HANDLE_VALUE;
1101 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1102 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1106 /***********************************************************************
1107 * CreateNamedPipeW (KERNEL32.@)
1109 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1110 DWORD dwPipeMode, DWORD nMaxInstances,
1111 DWORD nOutBufferSize, DWORD nInBufferSize,
1112 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1114 HANDLE handle;
1115 UNICODE_STRING nt_name;
1116 OBJECT_ATTRIBUTES attr;
1117 DWORD options;
1118 BOOLEAN pipe_type, read_mode, non_block;
1119 NTSTATUS status;
1120 IO_STATUS_BLOCK iosb;
1121 LARGE_INTEGER timeout;
1123 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1124 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1125 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1127 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1129 SetLastError( ERROR_PATH_NOT_FOUND );
1130 return INVALID_HANDLE_VALUE;
1132 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1134 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1135 RtlFreeUnicodeString( &nt_name );
1136 return INVALID_HANDLE_VALUE;
1139 attr.Length = sizeof(attr);
1140 attr.RootDirectory = 0;
1141 attr.ObjectName = &nt_name;
1142 attr.Attributes = OBJ_CASE_INSENSITIVE |
1143 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1144 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1145 attr.SecurityQualityOfService = NULL;
1147 options = 0;
1148 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1149 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1150 if ((dwOpenMode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
1151 options |= FILE_PIPE_FULL_DUPLEX;
1152 else if (dwOpenMode & PIPE_ACCESS_INBOUND) options |= FILE_PIPE_INBOUND;
1153 else if (dwOpenMode & PIPE_ACCESS_OUTBOUND) options |= FILE_PIPE_OUTBOUND;
1154 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1155 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1156 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1157 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0UL;
1159 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1161 SetLastError(0);
1163 status = NtCreateNamedPipeFile(&handle, GENERIC_READ|GENERIC_WRITE, &attr, &iosb,
1164 0, FILE_OVERWRITE_IF, options, pipe_type,
1165 read_mode, non_block, nMaxInstances,
1166 nInBufferSize, nOutBufferSize, &timeout);
1168 RtlFreeUnicodeString( &nt_name );
1169 if (status)
1171 handle = INVALID_HANDLE_VALUE;
1172 SetLastError( RtlNtStatusToDosError(status) );
1174 return handle;
1178 /***********************************************************************
1179 * PeekNamedPipe (KERNEL32.@)
1181 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1182 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1184 FILE_PIPE_PEEK_BUFFER local_buffer;
1185 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1186 IO_STATUS_BLOCK io;
1187 NTSTATUS status;
1189 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1190 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1192 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1193 return FALSE;
1196 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1197 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1198 if (!status)
1200 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1201 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1202 if (lpcbRead) *lpcbRead = read_size;
1203 if (lpcbMessage) *lpcbMessage = 0; /* FIXME */
1204 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1206 else SetLastError( RtlNtStatusToDosError(status) );
1208 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1209 return !status;
1212 /***********************************************************************
1213 * WaitNamedPipeA (KERNEL32.@)
1215 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1217 WCHAR buffer[MAX_PATH];
1219 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1221 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1223 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1224 return 0;
1226 return WaitNamedPipeW( buffer, nTimeOut );
1230 /***********************************************************************
1231 * WaitNamedPipeW (KERNEL32.@)
1233 * Waits for a named pipe instance to become available
1235 * PARAMS
1236 * name [I] Pointer to a named pipe name to wait for
1237 * nTimeOut [I] How long to wait in ms
1239 * RETURNS
1240 * TRUE: Success, named pipe can be opened with CreateFile
1241 * FALSE: Failure, GetLastError can be called for further details
1243 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1245 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1246 NTSTATUS status;
1247 UNICODE_STRING nt_name, pipe_dev_name;
1248 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1249 IO_STATUS_BLOCK iosb;
1250 OBJECT_ATTRIBUTES attr;
1251 ULONG sz_pipe_wait;
1252 HANDLE pipe_dev;
1254 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1256 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1257 return FALSE;
1259 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1260 nt_name.Length < sizeof(leadin) ||
1261 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR) != 0))
1263 RtlFreeUnicodeString( &nt_name );
1264 SetLastError( ERROR_PATH_NOT_FOUND );
1265 return FALSE;
1268 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1269 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1271 RtlFreeUnicodeString( &nt_name );
1272 SetLastError( ERROR_OUTOFMEMORY );
1273 return FALSE;
1276 pipe_dev_name.Buffer = nt_name.Buffer;
1277 pipe_dev_name.Length = sizeof(leadin);
1278 pipe_dev_name.MaximumLength = sizeof(leadin);
1279 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1280 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1281 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1282 FILE_SYNCHRONOUS_IO_NONALERT);
1283 if (status != ERROR_SUCCESS)
1285 SetLastError( ERROR_PATH_NOT_FOUND );
1286 return FALSE;
1289 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1290 pipe_wait->Timeout.QuadPart = (ULONGLONG)nTimeOut * -10000;
1291 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1292 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1293 pipe_wait->NameLength);
1294 RtlFreeUnicodeString( &nt_name );
1296 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1297 pipe_wait, sz_pipe_wait, NULL, 0 );
1299 HeapFree( GetProcessHeap(), 0, pipe_wait );
1300 NtClose( pipe_dev );
1302 if(status != STATUS_SUCCESS)
1304 SetLastError(RtlNtStatusToDosError(status));
1305 return FALSE;
1307 else
1308 return TRUE;
1312 /***********************************************************************
1313 * ConnectNamedPipe (KERNEL32.@)
1315 * Connects to a named pipe
1317 * Parameters
1318 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1319 * overlapped: Optional OVERLAPPED struct
1321 * Return values
1322 * TRUE: Success
1323 * FALSE: Failure, GetLastError can be called for further details
1325 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1327 NTSTATUS status;
1328 IO_STATUS_BLOCK status_block;
1330 TRACE("(%p,%p)\n", hPipe, overlapped);
1332 if(overlapped)
1333 overlapped->Internal = STATUS_PENDING;
1335 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, NULL,
1336 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1337 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1339 if (status == STATUS_SUCCESS) return TRUE;
1340 SetLastError( RtlNtStatusToDosError(status) );
1341 return FALSE;
1344 /***********************************************************************
1345 * DisconnectNamedPipe (KERNEL32.@)
1347 * Disconnects from a named pipe
1349 * Parameters
1350 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1352 * Return values
1353 * TRUE: Success
1354 * FALSE: Failure, GetLastError can be called for further details
1356 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1358 NTSTATUS status;
1359 IO_STATUS_BLOCK io_block;
1361 TRACE("(%p)\n",hPipe);
1363 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1364 NULL, 0, NULL, 0);
1365 if (status == STATUS_SUCCESS) return TRUE;
1366 SetLastError( RtlNtStatusToDosError(status) );
1367 return FALSE;
1370 /***********************************************************************
1371 * TransactNamedPipe (KERNEL32.@)
1373 * BUGS
1374 * should be done as a single operation in the wineserver or kernel
1376 BOOL WINAPI TransactNamedPipe(
1377 HANDLE handle, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1378 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1380 BOOL r;
1381 DWORD count;
1383 TRACE("%p %p %d %p %d %p %p\n",
1384 handle, lpInput, dwInputSize, lpOutput,
1385 dwOutputSize, lpBytesRead, lpOverlapped);
1387 if (lpOverlapped)
1389 FIXME("Doesn't support overlapped operation as yet\n");
1390 return FALSE;
1393 r = WriteFile(handle, lpOutput, dwOutputSize, &count, NULL);
1394 if (r)
1395 r = ReadFile(handle, lpInput, dwInputSize, lpBytesRead, NULL);
1397 return r;
1400 /***********************************************************************
1401 * GetNamedPipeInfo (KERNEL32.@)
1403 BOOL WINAPI GetNamedPipeInfo(
1404 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1405 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1407 FILE_PIPE_LOCAL_INFORMATION fpli;
1408 IO_STATUS_BLOCK iosb;
1409 NTSTATUS status;
1411 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1412 FilePipeLocalInformation);
1413 if (status)
1415 SetLastError( RtlNtStatusToDosError(status) );
1416 return FALSE;
1419 if (lpFlags)
1421 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1422 PIPE_SERVER_END : PIPE_CLIENT_END;
1423 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1424 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1427 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1428 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1429 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1431 return TRUE;
1434 /***********************************************************************
1435 * GetNamedPipeHandleStateA (KERNEL32.@)
1437 BOOL WINAPI GetNamedPipeHandleStateA(
1438 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1439 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1440 LPSTR lpUsername, DWORD nUsernameMaxSize)
1442 FIXME("%p %p %p %p %p %p %d\n",
1443 hNamedPipe, lpState, lpCurInstances,
1444 lpMaxCollectionCount, lpCollectDataTimeout,
1445 lpUsername, nUsernameMaxSize);
1447 return FALSE;
1450 /***********************************************************************
1451 * GetNamedPipeHandleStateW (KERNEL32.@)
1453 BOOL WINAPI GetNamedPipeHandleStateW(
1454 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1455 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1456 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1458 FIXME("%p %p %p %p %p %p %d\n",
1459 hNamedPipe, lpState, lpCurInstances,
1460 lpMaxCollectionCount, lpCollectDataTimeout,
1461 lpUsername, nUsernameMaxSize);
1463 return FALSE;
1466 /***********************************************************************
1467 * SetNamedPipeHandleState (KERNEL32.@)
1469 BOOL WINAPI SetNamedPipeHandleState(
1470 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1471 LPDWORD lpCollectDataTimeout)
1473 /* should be a fixme, but this function is called a lot by the RPC
1474 * runtime, and it slows down InstallShield a fair bit. */
1475 WARN("stub: %p %p/%d %p %p\n",
1476 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1477 return FALSE;
1480 /***********************************************************************
1481 * CallNamedPipeA (KERNEL32.@)
1483 BOOL WINAPI CallNamedPipeA(
1484 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1485 LPVOID lpOutput, DWORD dwOutputSize,
1486 LPDWORD lpBytesRead, DWORD nTimeout)
1488 DWORD len;
1489 LPWSTR str = NULL;
1490 BOOL ret;
1492 TRACE("%s %p %d %p %d %p %d\n",
1493 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1494 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1496 if( lpNamedPipeName )
1498 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1499 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1500 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1502 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1503 dwOutputSize, lpBytesRead, nTimeout );
1504 if( lpNamedPipeName )
1505 HeapFree( GetProcessHeap(), 0, str );
1507 return ret;
1510 /***********************************************************************
1511 * CallNamedPipeW (KERNEL32.@)
1513 BOOL WINAPI CallNamedPipeW(
1514 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1515 LPVOID lpOutput, DWORD lpOutputSize,
1516 LPDWORD lpBytesRead, DWORD nTimeout)
1518 HANDLE pipe;
1519 BOOL ret;
1520 DWORD mode;
1522 TRACE("%s %p %d %p %d %p %d\n",
1523 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1524 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1526 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1527 if (pipe == INVALID_HANDLE_VALUE)
1529 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1530 if (!ret)
1531 return FALSE;
1532 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1533 if (pipe == INVALID_HANDLE_VALUE)
1534 return FALSE;
1537 mode = PIPE_READMODE_MESSAGE;
1538 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1539 if (!ret)
1541 CloseHandle(pipe);
1542 return FALSE;
1545 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1546 CloseHandle(pipe);
1547 if (!ret)
1548 return FALSE;
1550 return TRUE;
1553 /******************************************************************
1554 * CreatePipe (KERNEL32.@)
1557 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1558 LPSECURITY_ATTRIBUTES sa, DWORD size )
1560 static unsigned index /* = 0 */;
1561 WCHAR name[64];
1562 HANDLE hr, hw;
1563 unsigned in_index = index;
1564 UNICODE_STRING nt_name;
1565 OBJECT_ATTRIBUTES attr;
1566 NTSTATUS status;
1567 IO_STATUS_BLOCK iosb;
1568 LARGE_INTEGER timeout;
1570 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1572 attr.Length = sizeof(attr);
1573 attr.RootDirectory = 0;
1574 attr.ObjectName = &nt_name;
1575 attr.Attributes = OBJ_CASE_INSENSITIVE |
1576 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1577 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1578 attr.SecurityQualityOfService = NULL;
1580 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1581 /* generate a unique pipe name (system wide) */
1584 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1585 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1586 'u','.','%','0','8','u','\0' };
1588 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1589 GetCurrentProcessId(), ++index);
1590 RtlInitUnicodeString(&nt_name, name);
1591 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1592 0, FILE_OVERWRITE_IF,
1593 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1594 FALSE, FALSE, FALSE,
1595 1, size, size, &timeout);
1596 if (status)
1598 SetLastError( RtlNtStatusToDosError(status) );
1599 hr = INVALID_HANDLE_VALUE;
1601 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1602 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1603 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1605 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1606 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1608 if (status)
1610 SetLastError( RtlNtStatusToDosError(status) );
1611 NtClose(hr);
1612 return FALSE;
1615 *hReadPipe = hr;
1616 *hWritePipe = hw;
1617 return TRUE;
1621 /******************************************************************************
1622 * CreateMailslotA [KERNEL32.@]
1624 * See CreatMailslotW.
1626 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1627 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1629 DWORD len;
1630 HANDLE handle;
1631 LPWSTR name = NULL;
1633 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1634 nMaxMessageSize, lReadTimeout, sa);
1636 if( lpName )
1638 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1639 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1640 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1643 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1645 HeapFree( GetProcessHeap(), 0, name );
1647 return handle;
1651 /******************************************************************************
1652 * CreateMailslotW [KERNEL32.@]
1654 * Create a mailslot with specified name.
1656 * PARAMS
1657 * lpName [I] Pointer to string for mailslot name
1658 * nMaxMessageSize [I] Maximum message size
1659 * lReadTimeout [I] Milliseconds before read time-out
1660 * sa [I] Pointer to security structure
1662 * RETURNS
1663 * Success: Handle to mailslot
1664 * Failure: INVALID_HANDLE_VALUE
1666 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1667 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1669 HANDLE handle = INVALID_HANDLE_VALUE;
1670 OBJECT_ATTRIBUTES attr;
1671 UNICODE_STRING nameW;
1672 LARGE_INTEGER timeout;
1673 IO_STATUS_BLOCK iosb;
1674 NTSTATUS status;
1676 TRACE("%s %d %d %p\n", debugstr_w(lpName),
1677 nMaxMessageSize, lReadTimeout, sa);
1679 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1681 SetLastError( ERROR_PATH_NOT_FOUND );
1682 return INVALID_HANDLE_VALUE;
1685 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1687 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1688 RtlFreeUnicodeString( &nameW );
1689 return INVALID_HANDLE_VALUE;
1692 attr.Length = sizeof(attr);
1693 attr.RootDirectory = 0;
1694 attr.Attributes = OBJ_CASE_INSENSITIVE;
1695 attr.ObjectName = &nameW;
1696 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1697 attr.SecurityQualityOfService = NULL;
1699 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1700 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1701 else
1702 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1704 status = NtCreateMailslotFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr,
1705 &iosb, 0, 0, nMaxMessageSize, &timeout );
1706 if (status)
1708 SetLastError( RtlNtStatusToDosError(status) );
1709 handle = INVALID_HANDLE_VALUE;
1712 RtlFreeUnicodeString( &nameW );
1713 return handle;
1717 /******************************************************************************
1718 * GetMailslotInfo [KERNEL32.@]
1720 * Retrieve information about a mailslot.
1722 * PARAMS
1723 * hMailslot [I] Mailslot handle
1724 * lpMaxMessageSize [O] Address of maximum message size
1725 * lpNextSize [O] Address of size of next message
1726 * lpMessageCount [O] Address of number of messages
1727 * lpReadTimeout [O] Address of read time-out
1729 * RETURNS
1730 * Success: TRUE
1731 * Failure: FALSE
1733 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1734 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1735 LPDWORD lpReadTimeout )
1737 FILE_MAILSLOT_QUERY_INFORMATION info;
1738 IO_STATUS_BLOCK iosb;
1739 NTSTATUS status;
1741 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1742 lpNextSize, lpMessageCount, lpReadTimeout);
1744 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1745 FileMailslotQueryInformation );
1747 if( status != STATUS_SUCCESS )
1749 SetLastError( RtlNtStatusToDosError(status) );
1750 return FALSE;
1753 if( lpMaxMessageSize )
1754 *lpMaxMessageSize = info.MaximumMessageSize;
1755 if( lpNextSize )
1756 *lpNextSize = info.NextMessageSize;
1757 if( lpMessageCount )
1758 *lpMessageCount = info.MessagesAvailable;
1759 if( lpReadTimeout )
1760 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1762 return TRUE;
1766 /******************************************************************************
1767 * SetMailslotInfo [KERNEL32.@]
1769 * Set the read timeout of a mailslot.
1771 * PARAMS
1772 * hMailslot [I] Mailslot handle
1773 * dwReadTimeout [I] Timeout in milliseconds.
1775 * RETURNS
1776 * Success: TRUE
1777 * Failure: FALSE
1779 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1781 FILE_MAILSLOT_SET_INFORMATION info;
1782 IO_STATUS_BLOCK iosb;
1783 NTSTATUS status;
1785 TRACE("%p %d\n", hMailslot, dwReadTimeout);
1787 info.ReadTimeout.QuadPart = dwReadTimeout * -10000;
1788 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1789 FileMailslotSetInformation );
1790 if( status != STATUS_SUCCESS )
1792 SetLastError( RtlNtStatusToDosError(status) );
1793 return FALSE;
1795 return TRUE;
1799 /******************************************************************************
1800 * CreateIoCompletionPort (KERNEL32.@)
1802 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1803 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1805 FIXME("(%p, %p, %08lx, %08x): stub.\n",
1806 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1807 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1808 return NULL;
1812 /******************************************************************************
1813 * GetQueuedCompletionStatus (KERNEL32.@)
1815 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1816 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1817 DWORD dwMilliseconds )
1819 FIXME("(%p,%p,%p,%p,%d), stub!\n",
1820 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1821 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1822 return FALSE;
1825 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
1826 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
1828 FIXME("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
1829 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1830 return FALSE;
1833 /******************************************************************************
1834 * CreateJobObjectW (KERNEL32.@)
1836 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1838 FIXME("%p %s\n", attr, debugstr_w(name) );
1839 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1840 return 0;
1843 /******************************************************************************
1844 * CreateJobObjectA (KERNEL32.@)
1846 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1848 LPWSTR str = NULL;
1849 UINT len;
1850 HANDLE r;
1852 TRACE("%p %s\n", attr, debugstr_a(name) );
1854 if( name )
1856 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1857 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1858 if( !str )
1860 SetLastError( ERROR_OUTOFMEMORY );
1861 return 0;
1863 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1866 r = CreateJobObjectW( attr, str );
1868 HeapFree( GetProcessHeap(), 0, str );
1870 return r;
1873 /******************************************************************************
1874 * AssignProcessToJobObject (KERNEL32.@)
1876 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1878 FIXME("%p %p\n", hJob, hProcess);
1879 return TRUE;
1882 #ifdef __i386__
1884 /***********************************************************************
1885 * InterlockedCompareExchange (KERNEL32.@)
1887 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1888 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1889 "movl 12(%esp),%eax\n\t"
1890 "movl 8(%esp),%ecx\n\t"
1891 "movl 4(%esp),%edx\n\t"
1892 "lock; cmpxchgl %ecx,(%edx)\n\t"
1893 "ret $12")
1895 /***********************************************************************
1896 * InterlockedExchange (KERNEL32.@)
1898 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1899 __ASM_GLOBAL_FUNC(InterlockedExchange,
1900 "movl 8(%esp),%eax\n\t"
1901 "movl 4(%esp),%edx\n\t"
1902 "lock; xchgl %eax,(%edx)\n\t"
1903 "ret $8")
1905 /***********************************************************************
1906 * InterlockedExchangeAdd (KERNEL32.@)
1908 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1909 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1910 "movl 8(%esp),%eax\n\t"
1911 "movl 4(%esp),%edx\n\t"
1912 "lock; xaddl %eax,(%edx)\n\t"
1913 "ret $8")
1915 /***********************************************************************
1916 * InterlockedIncrement (KERNEL32.@)
1918 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1919 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1920 "movl 4(%esp),%edx\n\t"
1921 "movl $1,%eax\n\t"
1922 "lock; xaddl %eax,(%edx)\n\t"
1923 "incl %eax\n\t"
1924 "ret $4")
1926 /***********************************************************************
1927 * InterlockedDecrement (KERNEL32.@)
1929 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1930 "movl 4(%esp),%edx\n\t"
1931 "movl $-1,%eax\n\t"
1932 "lock; xaddl %eax,(%edx)\n\t"
1933 "decl %eax\n\t"
1934 "ret $4")
1936 #else /* __i386__ */
1938 /***********************************************************************
1939 * InterlockedCompareExchange (KERNEL32.@)
1941 * Atomically swap one value with another.
1943 * PARAMS
1944 * dest [I/O] The value to replace
1945 * xchq [I] The value to be swapped
1946 * compare [I] The value to compare to dest
1948 * RETURNS
1949 * The resulting value of dest.
1951 * NOTES
1952 * dest is updated only if it is equal to compare, otherwise no swap is done.
1954 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
1956 return interlocked_cmpxchg( (int *)dest, xchg, compare );
1959 /***********************************************************************
1960 * InterlockedExchange (KERNEL32.@)
1962 * Atomically swap one value with another.
1964 * PARAMS
1965 * dest [I/O] The value to replace
1966 * val [I] The value to be swapped
1968 * RETURNS
1969 * The resulting value of dest.
1971 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
1973 return interlocked_xchg( (int *)dest, val );
1976 /***********************************************************************
1977 * InterlockedExchangeAdd (KERNEL32.@)
1979 * Atomically add one value to another.
1981 * PARAMS
1982 * dest [I/O] The value to add to
1983 * incr [I] The value to be added
1985 * RETURNS
1986 * The resulting value of dest.
1988 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
1990 return interlocked_xchg_add( (int *)dest, incr );
1993 /***********************************************************************
1994 * InterlockedIncrement (KERNEL32.@)
1996 * Atomically increment a value.
1998 * PARAMS
1999 * dest [I/O] The value to increment
2001 * RETURNS
2002 * The resulting value of dest.
2004 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
2006 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
2009 /***********************************************************************
2010 * InterlockedDecrement (KERNEL32.@)
2012 * Atomically decrement a value.
2014 * PARAMS
2015 * dest [I/O] The value to decrement
2017 * RETURNS
2018 * The resulting value of dest.
2020 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
2022 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
2025 #endif /* __i386__ */