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
22 #include "wine/port.h"
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
36 #define WIN32_NO_STATUS
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};
66 OBJECT_ATTRIBUTES attr;
72 RtlInitUnicodeString(&str, basenameW);
73 InitializeObjectAttributes(&attr, &str, 0, 0, NULL);
74 NtOpenDirectoryObject(&dir, DIRECTORY_CREATE_OBJECT|DIRECTORY_TRAVERSE,
76 if (InterlockedCompareExchangePointer( (PVOID)&handle, dir, 0 ) != 0)
78 /* someone beat us here... */
85 /***********************************************************************
88 VOID WINAPI Sleep( DWORD timeout )
90 SleepEx( timeout, FALSE );
93 /******************************************************************************
94 * SleepEx (KERNEL32.@)
96 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
100 if (timeout == INFINITE) status = NtDelayExecution( alertable, NULL );
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;
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,
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,
160 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
163 if (count > MAXIMUM_WAIT_OBJECTS)
165 SetLastError(ERROR_INVALID_PARAMETER);
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( HandleToULong(handles[i]) );
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]))
186 hloc[i] = GetConsoleInputWaitHandle();
190 if (timeout == INFINITE)
192 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, NULL );
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;
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 );
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 );
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 );
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);
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);
277 /***********************************************************************
278 * UnregisterWait (KERNEL32.@)
280 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
282 FIXME("%p\n",WaitHandle);
286 /***********************************************************************
287 * UnregisterWaitEx (KERNEL32.@)
289 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
291 FIXME("%p %p\n",WaitHandle, CompletionEvent);
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 )
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;
317 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn,
318 bAlertable, ptimeout );
321 SetLastError( RtlNtStatusToDosError(status) );
322 status = WAIT_FAILED;
327 /***********************************************************************
328 * InitializeCriticalSection (KERNEL32.@)
330 * Initialise a critical section before use.
333 * crit [O] Critical section to initialise.
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.
350 * crit [O] Critical section to initialise.
351 * spincount [I] Number of times to spin upon contention.
355 * Failure: Nothing. If the function fails an exception is raised.
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 );
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.
387 * crit [O] Critical section to initialise.
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.
405 * crit [O] Critical section to uninitialise (destroy).
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 );
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 )
442 UNICODE_STRING nameW;
443 OBJECT_ATTRIBUTES attr;
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);
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;
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 );
474 SetLastError( RtlNtStatusToDosError(status) );
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 );
502 return OpenEventW( access, inherit, buffer );
506 /***********************************************************************
507 * OpenEventW (KERNEL32.@)
509 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
512 UNICODE_STRING nameW;
513 OBJECT_ATTRIBUTES attr;
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;
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) );
540 /***********************************************************************
541 * PulseEvent (KERNEL32.@)
543 BOOL WINAPI PulseEvent( HANDLE handle )
547 if ((status = NtPulseEvent( handle, NULL )))
548 SetLastError( RtlNtStatusToDosError(status) );
553 /***********************************************************************
554 * SetW32Event (KERNEL.458)
555 * SetEvent (KERNEL32.@)
557 BOOL WINAPI SetEvent( HANDLE handle )
561 if ((status = NtSetEvent( handle, NULL )))
562 SetLastError( RtlNtStatusToDosError(status) );
567 /***********************************************************************
568 * ResetW32Event (KERNEL.459)
569 * ResetEvent (KERNEL32.@)
571 BOOL WINAPI ResetEvent( HANDLE handle )
575 if ((status = NtResetEvent( handle, NULL )))
576 SetLastError( RtlNtStatusToDosError(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)
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)
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 );
642 return CreateMutexW( sa, owner, buffer );
646 /***********************************************************************
647 * CreateMutexW (KERNEL32.@)
649 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
652 UNICODE_STRING nameW;
653 OBJECT_ATTRIBUTES attr;
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;
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 );
674 SetLastError( RtlNtStatusToDosError(status) );
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 );
693 return OpenMutexW( access, inherit, buffer );
697 /***********************************************************************
698 * OpenMutexW (KERNEL32.@)
700 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
703 UNICODE_STRING nameW;
704 OBJECT_ATTRIBUTES attr;
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;
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) );
732 /***********************************************************************
733 * ReleaseMutex (KERNEL32.@)
735 BOOL WINAPI ReleaseMutex( HANDLE handle )
739 status = NtReleaseMutant(handle, NULL);
740 if (status != STATUS_SUCCESS)
742 SetLastError( RtlNtStatusToDosError(status) );
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 );
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 )
779 UNICODE_STRING nameW;
780 OBJECT_ATTRIBUTES attr;
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;
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 );
801 SetLastError( RtlNtStatusToDosError(status) );
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 );
820 return OpenSemaphoreW( access, inherit, buffer );
824 /***********************************************************************
825 * OpenSemaphoreW (KERNEL32.@)
827 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
830 UNICODE_STRING nameW;
831 OBJECT_ATTRIBUTES attr;
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;
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) );
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) );
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 );
889 return CreateWaitableTimerW( sa, manual, buffer );
893 /***********************************************************************
894 * CreateWaitableTimerW (KERNEL32.@)
896 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
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;
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 );
922 SetLastError( RtlNtStatusToDosError(status) );
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 );
941 return OpenWaitableTimerW( access, inherit, buffer );
945 /***********************************************************************
946 * OpenWaitableTimerW (KERNEL32.@)
948 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
951 UNICODE_STRING nameW;
952 OBJECT_ATTRIBUTES attr;
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;
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) );
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;
998 /***********************************************************************
999 * CancelWaitableTimer (KERNEL32.@)
1001 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1005 status = NtCancelTimer(handle, NULL);
1006 if (status != STATUS_SUCCESS)
1008 SetLastError( RtlNtStatusToDosError(status) );
1015 /***********************************************************************
1016 * CreateTimerQueue (KERNEL32.@)
1018 HANDLE WINAPI CreateTimerQueue(void)
1021 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
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);
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.
1044 * nonzero on success or zero on faillure
1049 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1050 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1051 DWORD DueTime, DWORD Period, ULONG Flags )
1054 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1058 /***********************************************************************
1059 * DeleteTimerQueueTimer (KERNEL32.@)
1061 * Cancels a timer-queue timer.
1064 * nonzero on success or zero on faillure
1069 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1070 HANDLE CompletionEvent )
1073 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
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 )
1115 UNICODE_STRING nt_name;
1116 OBJECT_ATTRIBUTES attr;
1117 DWORD access, options;
1118 BOOLEAN pipe_type, read_mode, non_block;
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 switch(dwOpenMode & 3)
1149 case PIPE_ACCESS_INBOUND:
1150 options = FILE_PIPE_INBOUND;
1151 access = GENERIC_READ;
1153 case PIPE_ACCESS_OUTBOUND:
1154 options = FILE_PIPE_OUTBOUND;
1155 access = GENERIC_WRITE;
1157 case PIPE_ACCESS_DUPLEX:
1158 options = FILE_PIPE_FULL_DUPLEX;
1159 access = GENERIC_READ | GENERIC_WRITE;
1162 SetLastError( ERROR_INVALID_PARAMETER );
1163 return INVALID_HANDLE_VALUE;
1165 access |= SYNCHRONIZE;
1166 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1167 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1168 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1169 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1170 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1171 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0U;
1173 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1177 status = NtCreateNamedPipeFile(&handle, access, &attr, &iosb, 0,
1178 FILE_OVERWRITE_IF, options, pipe_type,
1179 read_mode, non_block, nMaxInstances,
1180 nInBufferSize, nOutBufferSize, &timeout);
1182 RtlFreeUnicodeString( &nt_name );
1185 handle = INVALID_HANDLE_VALUE;
1186 SetLastError( RtlNtStatusToDosError(status) );
1192 /***********************************************************************
1193 * PeekNamedPipe (KERNEL32.@)
1195 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1196 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1198 FILE_PIPE_PEEK_BUFFER local_buffer;
1199 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1203 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1204 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1206 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1210 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1211 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1214 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1215 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1216 if (lpcbRead) *lpcbRead = read_size;
1217 if (lpcbMessage) *lpcbMessage = 0; /* FIXME */
1218 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1220 else SetLastError( RtlNtStatusToDosError(status) );
1222 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1226 /***********************************************************************
1227 * WaitNamedPipeA (KERNEL32.@)
1229 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1231 WCHAR buffer[MAX_PATH];
1233 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1235 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1237 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1240 return WaitNamedPipeW( buffer, nTimeOut );
1244 /***********************************************************************
1245 * WaitNamedPipeW (KERNEL32.@)
1247 * Waits for a named pipe instance to become available
1250 * name [I] Pointer to a named pipe name to wait for
1251 * nTimeOut [I] How long to wait in ms
1254 * TRUE: Success, named pipe can be opened with CreateFile
1255 * FALSE: Failure, GetLastError can be called for further details
1257 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1259 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1261 UNICODE_STRING nt_name, pipe_dev_name;
1262 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1263 IO_STATUS_BLOCK iosb;
1264 OBJECT_ATTRIBUTES attr;
1268 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1270 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1273 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1274 nt_name.Length < sizeof(leadin) ||
1275 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR) != 0))
1277 RtlFreeUnicodeString( &nt_name );
1278 SetLastError( ERROR_PATH_NOT_FOUND );
1282 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1283 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1285 RtlFreeUnicodeString( &nt_name );
1286 SetLastError( ERROR_OUTOFMEMORY );
1290 pipe_dev_name.Buffer = nt_name.Buffer;
1291 pipe_dev_name.Length = sizeof(leadin);
1292 pipe_dev_name.MaximumLength = sizeof(leadin);
1293 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1294 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1295 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1296 FILE_SYNCHRONOUS_IO_NONALERT);
1297 if (status != ERROR_SUCCESS)
1299 SetLastError( ERROR_PATH_NOT_FOUND );
1303 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1304 if (nTimeOut == NMPWAIT_WAIT_FOREVER)
1305 pipe_wait->Timeout.QuadPart = ((ULONGLONG)0x7fffffff << 32) | 0xffffffff;
1307 pipe_wait->Timeout.QuadPart = (ULONGLONG)nTimeOut * -10000;
1308 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1309 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1310 pipe_wait->NameLength);
1311 RtlFreeUnicodeString( &nt_name );
1313 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1314 pipe_wait, sz_pipe_wait, NULL, 0 );
1316 HeapFree( GetProcessHeap(), 0, pipe_wait );
1317 NtClose( pipe_dev );
1319 if(status != STATUS_SUCCESS)
1321 SetLastError(RtlNtStatusToDosError(status));
1329 /***********************************************************************
1330 * ConnectNamedPipe (KERNEL32.@)
1332 * Connects to a named pipe
1335 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1336 * overlapped: Optional OVERLAPPED struct
1340 * FALSE: Failure, GetLastError can be called for further details
1342 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1345 IO_STATUS_BLOCK status_block;
1347 TRACE("(%p,%p)\n", hPipe, overlapped);
1350 overlapped->Internal = STATUS_PENDING;
1352 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, NULL,
1353 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1354 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1356 if (status == STATUS_SUCCESS) return TRUE;
1357 SetLastError( RtlNtStatusToDosError(status) );
1361 /***********************************************************************
1362 * DisconnectNamedPipe (KERNEL32.@)
1364 * Disconnects from a named pipe
1367 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1371 * FALSE: Failure, GetLastError can be called for further details
1373 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1376 IO_STATUS_BLOCK io_block;
1378 TRACE("(%p)\n",hPipe);
1380 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1382 if (status == STATUS_SUCCESS) return TRUE;
1383 SetLastError( RtlNtStatusToDosError(status) );
1387 /***********************************************************************
1388 * TransactNamedPipe (KERNEL32.@)
1391 * should be done as a single operation in the wineserver or kernel
1393 BOOL WINAPI TransactNamedPipe(
1394 HANDLE handle, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1395 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1400 TRACE("%p %p %d %p %d %p %p\n",
1401 handle, lpInput, dwInputSize, lpOutput,
1402 dwOutputSize, lpBytesRead, lpOverlapped);
1406 FIXME("Doesn't support overlapped operation as yet\n");
1410 r = WriteFile(handle, lpOutput, dwOutputSize, &count, NULL);
1412 r = ReadFile(handle, lpInput, dwInputSize, lpBytesRead, NULL);
1417 /***********************************************************************
1418 * GetNamedPipeInfo (KERNEL32.@)
1420 BOOL WINAPI GetNamedPipeInfo(
1421 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1422 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1424 FILE_PIPE_LOCAL_INFORMATION fpli;
1425 IO_STATUS_BLOCK iosb;
1428 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1429 FilePipeLocalInformation);
1432 SetLastError( RtlNtStatusToDosError(status) );
1438 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1439 PIPE_SERVER_END : PIPE_CLIENT_END;
1440 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1441 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1444 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1445 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1446 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1451 /***********************************************************************
1452 * GetNamedPipeHandleStateA (KERNEL32.@)
1454 BOOL WINAPI GetNamedPipeHandleStateA(
1455 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1456 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1457 LPSTR lpUsername, DWORD nUsernameMaxSize)
1459 FIXME("%p %p %p %p %p %p %d\n",
1460 hNamedPipe, lpState, lpCurInstances,
1461 lpMaxCollectionCount, lpCollectDataTimeout,
1462 lpUsername, nUsernameMaxSize);
1467 /***********************************************************************
1468 * GetNamedPipeHandleStateW (KERNEL32.@)
1470 BOOL WINAPI GetNamedPipeHandleStateW(
1471 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1472 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1473 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1475 FIXME("%p %p %p %p %p %p %d\n",
1476 hNamedPipe, lpState, lpCurInstances,
1477 lpMaxCollectionCount, lpCollectDataTimeout,
1478 lpUsername, nUsernameMaxSize);
1483 /***********************************************************************
1484 * SetNamedPipeHandleState (KERNEL32.@)
1486 BOOL WINAPI SetNamedPipeHandleState(
1487 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1488 LPDWORD lpCollectDataTimeout)
1490 /* should be a fixme, but this function is called a lot by the RPC
1491 * runtime, and it slows down InstallShield a fair bit. */
1492 WARN("stub: %p %p/%d %p %p\n",
1493 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1497 /***********************************************************************
1498 * CallNamedPipeA (KERNEL32.@)
1500 BOOL WINAPI CallNamedPipeA(
1501 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1502 LPVOID lpOutput, DWORD dwOutputSize,
1503 LPDWORD lpBytesRead, DWORD nTimeout)
1509 TRACE("%s %p %d %p %d %p %d\n",
1510 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1511 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1513 if( lpNamedPipeName )
1515 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1516 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1517 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1519 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1520 dwOutputSize, lpBytesRead, nTimeout );
1521 if( lpNamedPipeName )
1522 HeapFree( GetProcessHeap(), 0, str );
1527 /***********************************************************************
1528 * CallNamedPipeW (KERNEL32.@)
1530 BOOL WINAPI CallNamedPipeW(
1531 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1532 LPVOID lpOutput, DWORD lpOutputSize,
1533 LPDWORD lpBytesRead, DWORD nTimeout)
1539 TRACE("%s %p %d %p %d %p %d\n",
1540 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1541 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1543 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1544 if (pipe == INVALID_HANDLE_VALUE)
1546 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1549 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1550 if (pipe == INVALID_HANDLE_VALUE)
1554 mode = PIPE_READMODE_MESSAGE;
1555 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1562 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1570 /******************************************************************
1571 * CreatePipe (KERNEL32.@)
1574 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1575 LPSECURITY_ATTRIBUTES sa, DWORD size )
1577 static unsigned index /* = 0 */;
1580 unsigned in_index = index;
1581 UNICODE_STRING nt_name;
1582 OBJECT_ATTRIBUTES attr;
1584 IO_STATUS_BLOCK iosb;
1585 LARGE_INTEGER timeout;
1587 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1589 attr.Length = sizeof(attr);
1590 attr.RootDirectory = 0;
1591 attr.ObjectName = &nt_name;
1592 attr.Attributes = OBJ_CASE_INSENSITIVE |
1593 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1594 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1595 attr.SecurityQualityOfService = NULL;
1597 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1598 /* generate a unique pipe name (system wide) */
1601 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1602 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1603 'u','.','%','0','8','u','\0' };
1605 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1606 GetCurrentProcessId(), ++index);
1607 RtlInitUnicodeString(&nt_name, name);
1608 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1609 0, FILE_OVERWRITE_IF,
1610 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1611 FALSE, FALSE, FALSE,
1612 1, size, size, &timeout);
1615 SetLastError( RtlNtStatusToDosError(status) );
1616 hr = INVALID_HANDLE_VALUE;
1618 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1619 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1620 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1622 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1623 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1627 SetLastError( RtlNtStatusToDosError(status) );
1638 /******************************************************************************
1639 * CreateMailslotA [KERNEL32.@]
1641 * See CreatMailslotW.
1643 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1644 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1650 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1651 nMaxMessageSize, lReadTimeout, sa);
1655 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1656 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1657 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1660 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1662 HeapFree( GetProcessHeap(), 0, name );
1668 /******************************************************************************
1669 * CreateMailslotW [KERNEL32.@]
1671 * Create a mailslot with specified name.
1674 * lpName [I] Pointer to string for mailslot name
1675 * nMaxMessageSize [I] Maximum message size
1676 * lReadTimeout [I] Milliseconds before read time-out
1677 * sa [I] Pointer to security structure
1680 * Success: Handle to mailslot
1681 * Failure: INVALID_HANDLE_VALUE
1683 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1684 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1686 HANDLE handle = INVALID_HANDLE_VALUE;
1687 OBJECT_ATTRIBUTES attr;
1688 UNICODE_STRING nameW;
1689 LARGE_INTEGER timeout;
1690 IO_STATUS_BLOCK iosb;
1693 TRACE("%s %d %d %p\n", debugstr_w(lpName),
1694 nMaxMessageSize, lReadTimeout, sa);
1696 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1698 SetLastError( ERROR_PATH_NOT_FOUND );
1699 return INVALID_HANDLE_VALUE;
1702 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1704 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1705 RtlFreeUnicodeString( &nameW );
1706 return INVALID_HANDLE_VALUE;
1709 attr.Length = sizeof(attr);
1710 attr.RootDirectory = 0;
1711 attr.Attributes = OBJ_CASE_INSENSITIVE;
1712 attr.ObjectName = &nameW;
1713 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1714 attr.SecurityQualityOfService = NULL;
1716 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1717 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1719 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1721 status = NtCreateMailslotFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr,
1722 &iosb, 0, 0, nMaxMessageSize, &timeout );
1725 SetLastError( RtlNtStatusToDosError(status) );
1726 handle = INVALID_HANDLE_VALUE;
1729 RtlFreeUnicodeString( &nameW );
1734 /******************************************************************************
1735 * GetMailslotInfo [KERNEL32.@]
1737 * Retrieve information about a mailslot.
1740 * hMailslot [I] Mailslot handle
1741 * lpMaxMessageSize [O] Address of maximum message size
1742 * lpNextSize [O] Address of size of next message
1743 * lpMessageCount [O] Address of number of messages
1744 * lpReadTimeout [O] Address of read time-out
1750 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1751 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1752 LPDWORD lpReadTimeout )
1754 FILE_MAILSLOT_QUERY_INFORMATION info;
1755 IO_STATUS_BLOCK iosb;
1758 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1759 lpNextSize, lpMessageCount, lpReadTimeout);
1761 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1762 FileMailslotQueryInformation );
1764 if( status != STATUS_SUCCESS )
1766 SetLastError( RtlNtStatusToDosError(status) );
1770 if( lpMaxMessageSize )
1771 *lpMaxMessageSize = info.MaximumMessageSize;
1773 *lpNextSize = info.NextMessageSize;
1774 if( lpMessageCount )
1775 *lpMessageCount = info.MessagesAvailable;
1778 if (info.ReadTimeout.QuadPart == (((LONGLONG)0x7fffffff << 32) | 0xffffffff))
1779 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
1781 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1787 /******************************************************************************
1788 * SetMailslotInfo [KERNEL32.@]
1790 * Set the read timeout of a mailslot.
1793 * hMailslot [I] Mailslot handle
1794 * dwReadTimeout [I] Timeout in milliseconds.
1800 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1802 FILE_MAILSLOT_SET_INFORMATION info;
1803 IO_STATUS_BLOCK iosb;
1806 TRACE("%p %d\n", hMailslot, dwReadTimeout);
1808 if (dwReadTimeout != MAILSLOT_WAIT_FOREVER)
1809 info.ReadTimeout.QuadPart = (ULONGLONG)dwReadTimeout * -10000;
1811 info.ReadTimeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1812 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1813 FileMailslotSetInformation );
1814 if( status != STATUS_SUCCESS )
1816 SetLastError( RtlNtStatusToDosError(status) );
1823 /******************************************************************************
1824 * CreateIoCompletionPort (KERNEL32.@)
1826 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1827 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1829 FIXME("(%p, %p, %08lx, %08x): stub.\n",
1830 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1831 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1836 /******************************************************************************
1837 * GetQueuedCompletionStatus (KERNEL32.@)
1839 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1840 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1841 DWORD dwMilliseconds )
1843 FIXME("(%p,%p,%p,%p,%d), stub!\n",
1844 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1845 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1849 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
1850 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
1852 FIXME("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
1853 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1857 /******************************************************************************
1858 * BindIoCompletionCallback (KERNEL32.@)
1860 BOOL WINAPI BindIoCompletionCallback( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags)
1862 FIXME("%p, %p, %d, stub!\n", FileHandle, Function, Flags);
1863 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1867 /******************************************************************************
1868 * CreateJobObjectW (KERNEL32.@)
1870 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1872 FIXME("%p %s\n", attr, debugstr_w(name) );
1873 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1877 /******************************************************************************
1878 * CreateJobObjectA (KERNEL32.@)
1880 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1886 TRACE("%p %s\n", attr, debugstr_a(name) );
1890 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1891 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1894 SetLastError( ERROR_OUTOFMEMORY );
1897 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1900 r = CreateJobObjectW( attr, str );
1902 HeapFree( GetProcessHeap(), 0, str );
1907 /******************************************************************************
1908 * AssignProcessToJobObject (KERNEL32.@)
1910 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1912 FIXME("%p %p\n", hJob, hProcess);
1918 /***********************************************************************
1919 * InterlockedCompareExchange (KERNEL32.@)
1921 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1922 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1923 "movl 12(%esp),%eax\n\t"
1924 "movl 8(%esp),%ecx\n\t"
1925 "movl 4(%esp),%edx\n\t"
1926 "lock; cmpxchgl %ecx,(%edx)\n\t"
1929 /***********************************************************************
1930 * InterlockedExchange (KERNEL32.@)
1932 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1933 __ASM_GLOBAL_FUNC(InterlockedExchange,
1934 "movl 8(%esp),%eax\n\t"
1935 "movl 4(%esp),%edx\n\t"
1936 "lock; xchgl %eax,(%edx)\n\t"
1939 /***********************************************************************
1940 * InterlockedExchangeAdd (KERNEL32.@)
1942 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1943 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1944 "movl 8(%esp),%eax\n\t"
1945 "movl 4(%esp),%edx\n\t"
1946 "lock; xaddl %eax,(%edx)\n\t"
1949 /***********************************************************************
1950 * InterlockedIncrement (KERNEL32.@)
1952 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1953 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1954 "movl 4(%esp),%edx\n\t"
1956 "lock; xaddl %eax,(%edx)\n\t"
1960 /***********************************************************************
1961 * InterlockedDecrement (KERNEL32.@)
1963 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1964 "movl 4(%esp),%edx\n\t"
1966 "lock; xaddl %eax,(%edx)\n\t"
1970 #else /* __i386__ */
1972 /***********************************************************************
1973 * InterlockedCompareExchange (KERNEL32.@)
1975 * Atomically swap one value with another.
1978 * dest [I/O] The value to replace
1979 * xchq [I] The value to be swapped
1980 * compare [I] The value to compare to dest
1983 * The resulting value of dest.
1986 * dest is updated only if it is equal to compare, otherwise no swap is done.
1988 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
1990 return interlocked_cmpxchg( (int *)dest, xchg, compare );
1993 /***********************************************************************
1994 * InterlockedExchange (KERNEL32.@)
1996 * Atomically swap one value with another.
1999 * dest [I/O] The value to replace
2000 * val [I] The value to be swapped
2003 * The resulting value of dest.
2005 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
2007 return interlocked_xchg( (int *)dest, val );
2010 /***********************************************************************
2011 * InterlockedExchangeAdd (KERNEL32.@)
2013 * Atomically add one value to another.
2016 * dest [I/O] The value to add to
2017 * incr [I] The value to be added
2020 * The resulting value of dest.
2022 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
2024 return interlocked_xchg_add( (int *)dest, incr );
2027 /***********************************************************************
2028 * InterlockedIncrement (KERNEL32.@)
2030 * Atomically increment a value.
2033 * dest [I/O] The value to increment
2036 * The resulting value of dest.
2038 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
2040 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
2043 /***********************************************************************
2044 * InterlockedDecrement (KERNEL32.@)
2046 * Atomically decrement a value.
2049 * dest [I/O] The value to decrement
2052 * The resulting value of dest.
2054 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
2056 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
2059 #endif /* __i386__ */