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 /* helper for kernel32->ntdll timeout format conversion */
86 static inline PLARGE_INTEGER get_nt_timeout( PLARGE_INTEGER pTime, DWORD timeout )
88 if (timeout == INFINITE) return NULL;
89 pTime->QuadPart = (ULONGLONG)timeout * -10000;
93 /***********************************************************************
96 VOID WINAPI Sleep( DWORD timeout )
98 SleepEx( timeout, FALSE );
101 /******************************************************************************
102 * SleepEx (KERNEL32.@)
104 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
109 status = NtDelayExecution( alertable, get_nt_timeout( &time, timeout ) );
110 if (status == STATUS_USER_APC) return WAIT_IO_COMPLETION;
115 /***********************************************************************
116 * SwitchToThread (KERNEL32.@)
118 BOOL WINAPI SwitchToThread(void)
120 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
124 /***********************************************************************
125 * WaitForSingleObject (KERNEL32.@)
127 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
129 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
133 /***********************************************************************
134 * WaitForSingleObjectEx (KERNEL32.@)
136 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
139 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
143 /***********************************************************************
144 * WaitForMultipleObjects (KERNEL32.@)
146 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
147 BOOL wait_all, DWORD timeout )
149 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
153 /***********************************************************************
154 * WaitForMultipleObjectsEx (KERNEL32.@)
156 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
157 BOOL wait_all, DWORD timeout,
161 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
165 if (count > MAXIMUM_WAIT_OBJECTS)
167 SetLastError(ERROR_INVALID_PARAMETER);
170 for (i = 0; i < count; i++)
172 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
173 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
174 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
175 hloc[i] = GetStdHandle( HandleToULong(handles[i]) );
177 hloc[i] = handles[i];
179 /* yes, even screen buffer console handles are waitable, and are
180 * handled as a handle to the console itself !!
182 if (is_console_handle(hloc[i]))
184 if (!VerifyConsoleIoHandle(hloc[i]))
188 hloc[i] = GetConsoleInputWaitHandle();
192 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable,
193 get_nt_timeout( &time, timeout ) );
195 if (HIWORD(status)) /* is it an error code? */
197 SetLastError( RtlNtStatusToDosError(status) );
198 status = WAIT_FAILED;
204 /***********************************************************************
205 * WaitForSingleObject (KERNEL.460)
207 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
209 DWORD retval, mutex_count;
211 ReleaseThunkLock( &mutex_count );
212 retval = WaitForSingleObject( handle, timeout );
213 RestoreThunkLock( mutex_count );
217 /***********************************************************************
218 * WaitForMultipleObjects (KERNEL.461)
220 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
221 BOOL wait_all, DWORD timeout )
223 DWORD retval, mutex_count;
225 ReleaseThunkLock( &mutex_count );
226 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
227 RestoreThunkLock( mutex_count );
231 /***********************************************************************
232 * WaitForMultipleObjectsEx (KERNEL.495)
234 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
235 BOOL wait_all, DWORD timeout, BOOL alertable )
237 DWORD retval, mutex_count;
239 ReleaseThunkLock( &mutex_count );
240 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
241 RestoreThunkLock( mutex_count );
245 /***********************************************************************
246 * RegisterWaitForSingleObject (KERNEL32.@)
248 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
249 WAITORTIMERCALLBACK Callback, PVOID Context,
250 ULONG dwMilliseconds, ULONG dwFlags)
254 TRACE("%p %p %p %p %d %d\n",
255 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
257 status = RtlRegisterWait( phNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
258 if (status != STATUS_SUCCESS)
260 SetLastError( RtlNtStatusToDosError(status) );
266 /***********************************************************************
267 * RegisterWaitForSingleObjectEx (KERNEL32.@)
269 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
270 WAITORTIMERCALLBACK Callback, PVOID Context,
271 ULONG dwMilliseconds, ULONG dwFlags )
274 HANDLE hNewWaitObject;
276 TRACE("%p %p %p %d %d\n",
277 hObject,Callback,Context,dwMilliseconds,dwFlags);
279 status = RtlRegisterWait( &hNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
280 if (status != STATUS_SUCCESS)
282 SetLastError( RtlNtStatusToDosError(status) );
285 return hNewWaitObject;
288 /***********************************************************************
289 * UnregisterWait (KERNEL32.@)
291 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
295 TRACE("%p\n",WaitHandle);
297 status = RtlDeregisterWait( WaitHandle );
298 if (status != STATUS_SUCCESS)
300 SetLastError( RtlNtStatusToDosError(status) );
306 /***********************************************************************
307 * UnregisterWaitEx (KERNEL32.@)
309 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
311 FIXME("%p %p\n",WaitHandle, CompletionEvent);
315 /***********************************************************************
316 * SignalObjectAndWait (KERNEL32.@)
318 * Allows to atomically signal any of the synchro objects (semaphore,
319 * mutex, event) and wait on another.
321 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
322 DWORD dwMilliseconds, BOOL bAlertable )
325 LARGE_INTEGER timeout;
327 TRACE("%p %p %d %d\n", hObjectToSignal,
328 hObjectToWaitOn, dwMilliseconds, bAlertable);
330 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn, bAlertable,
331 get_nt_timeout( &timeout, dwMilliseconds ) );
334 SetLastError( RtlNtStatusToDosError(status) );
335 status = WAIT_FAILED;
340 /***********************************************************************
341 * InitializeCriticalSection (KERNEL32.@)
343 * Initialise a critical section before use.
346 * crit [O] Critical section to initialise.
349 * Nothing. If the function fails an exception is raised.
351 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
353 NTSTATUS ret = RtlInitializeCriticalSection( crit );
354 if (ret) RtlRaiseStatus( ret );
357 /***********************************************************************
358 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
360 * Initialise a critical section with a spin count.
363 * crit [O] Critical section to initialise.
364 * spincount [I] Number of times to spin upon contention.
368 * Failure: Nothing. If the function fails an exception is raised.
371 * spincount is ignored on uni-processor systems.
373 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
375 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
376 if (ret) RtlRaiseStatus( ret );
380 /***********************************************************************
381 * MakeCriticalSectionGlobal (KERNEL32.@)
383 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
385 /* let's assume that only one thread at a time will try to do this */
386 HANDLE sem = crit->LockSemaphore;
387 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
388 crit->LockSemaphore = ConvertToGlobalHandle( sem );
389 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
390 crit->DebugInfo = NULL;
394 /***********************************************************************
395 * ReinitializeCriticalSection (KERNEL32.@)
397 * Initialise an already used critical section.
400 * crit [O] Critical section to initialise.
405 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
407 if ( !crit->LockSemaphore )
408 RtlInitializeCriticalSection( crit );
412 /***********************************************************************
413 * UninitializeCriticalSection (KERNEL32.@)
415 * UnInitialise a critical section after use.
418 * crit [O] Critical section to uninitialise (destroy).
423 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
425 RtlDeleteCriticalSection( crit );
429 /***********************************************************************
430 * CreateEventA (KERNEL32.@)
432 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
433 BOOL initial_state, LPCSTR name )
435 WCHAR buffer[MAX_PATH];
437 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
439 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
441 SetLastError( ERROR_FILENAME_EXCED_RANGE );
444 return CreateEventW( sa, manual_reset, initial_state, buffer );
448 /***********************************************************************
449 * CreateEventW (KERNEL32.@)
451 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
452 BOOL initial_state, LPCWSTR name )
455 UNICODE_STRING nameW;
456 OBJECT_ATTRIBUTES attr;
459 /* one buggy program needs this
460 * ("Van Dale Groot woordenboek der Nederlandse taal")
462 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
464 ERR("Bad security attributes pointer %p\n",sa);
465 SetLastError( ERROR_INVALID_PARAMETER);
469 attr.Length = sizeof(attr);
470 attr.RootDirectory = 0;
471 attr.ObjectName = NULL;
472 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
473 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
474 attr.SecurityQualityOfService = NULL;
477 RtlInitUnicodeString( &nameW, name );
478 attr.ObjectName = &nameW;
479 attr.RootDirectory = get_BaseNamedObjects_handle();
482 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
483 if (status == STATUS_OBJECT_NAME_EXISTS)
484 SetLastError( ERROR_ALREADY_EXISTS );
486 SetLastError( RtlNtStatusToDosError(status) );
491 /***********************************************************************
492 * CreateW32Event (KERNEL.457)
494 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
496 return CreateEventW( NULL, manual_reset, initial_state, NULL );
500 /***********************************************************************
501 * OpenEventA (KERNEL32.@)
503 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
505 WCHAR buffer[MAX_PATH];
507 if (!name) return OpenEventW( access, inherit, NULL );
509 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
511 SetLastError( ERROR_FILENAME_EXCED_RANGE );
514 return OpenEventW( access, inherit, buffer );
518 /***********************************************************************
519 * OpenEventW (KERNEL32.@)
521 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
524 UNICODE_STRING nameW;
525 OBJECT_ATTRIBUTES attr;
528 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
530 attr.Length = sizeof(attr);
531 attr.RootDirectory = 0;
532 attr.ObjectName = NULL;
533 attr.Attributes = inherit ? OBJ_INHERIT : 0;
534 attr.SecurityDescriptor = NULL;
535 attr.SecurityQualityOfService = NULL;
538 RtlInitUnicodeString( &nameW, name );
539 attr.ObjectName = &nameW;
540 attr.RootDirectory = get_BaseNamedObjects_handle();
543 status = NtOpenEvent( &ret, access, &attr );
544 if (status != STATUS_SUCCESS)
546 SetLastError( RtlNtStatusToDosError(status) );
552 /***********************************************************************
553 * PulseEvent (KERNEL32.@)
555 BOOL WINAPI PulseEvent( HANDLE handle )
559 if ((status = NtPulseEvent( handle, NULL )))
560 SetLastError( RtlNtStatusToDosError(status) );
565 /***********************************************************************
566 * SetW32Event (KERNEL.458)
567 * SetEvent (KERNEL32.@)
569 BOOL WINAPI SetEvent( HANDLE handle )
573 if ((status = NtSetEvent( handle, NULL )))
574 SetLastError( RtlNtStatusToDosError(status) );
579 /***********************************************************************
580 * ResetW32Event (KERNEL.459)
581 * ResetEvent (KERNEL32.@)
583 BOOL WINAPI ResetEvent( HANDLE handle )
587 if ((status = NtResetEvent( handle, NULL )))
588 SetLastError( RtlNtStatusToDosError(status) );
593 /***********************************************************************
594 * NOTE: The Win95 VWin32_Event routines given below are really low-level
595 * routines implemented directly by VWin32. The user-mode libraries
596 * implement Win32 synchronisation routines on top of these low-level
597 * primitives. We do it the other way around here :-)
600 /***********************************************************************
601 * VWin32_EventCreate (KERNEL.442)
603 HANDLE WINAPI VWin32_EventCreate(VOID)
605 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
606 return ConvertToGlobalHandle( hEvent );
609 /***********************************************************************
610 * VWin32_EventDestroy (KERNEL.443)
612 VOID WINAPI VWin32_EventDestroy(HANDLE event)
614 CloseHandle( event );
617 /***********************************************************************
618 * VWin32_EventWait (KERNEL.450)
620 VOID WINAPI VWin32_EventWait(HANDLE event)
624 ReleaseThunkLock( &mutex_count );
625 WaitForSingleObject( event, INFINITE );
626 RestoreThunkLock( mutex_count );
629 /***********************************************************************
630 * VWin32_EventSet (KERNEL.451)
631 * KERNEL_479 (KERNEL.479)
633 VOID WINAPI VWin32_EventSet(HANDLE event)
640 /***********************************************************************
641 * CreateMutexA (KERNEL32.@)
643 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
645 WCHAR buffer[MAX_PATH];
647 if (!name) return CreateMutexW( sa, owner, NULL );
649 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
651 SetLastError( ERROR_FILENAME_EXCED_RANGE );
654 return CreateMutexW( sa, owner, buffer );
658 /***********************************************************************
659 * CreateMutexW (KERNEL32.@)
661 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
664 UNICODE_STRING nameW;
665 OBJECT_ATTRIBUTES attr;
668 attr.Length = sizeof(attr);
669 attr.RootDirectory = 0;
670 attr.ObjectName = NULL;
671 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
672 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
673 attr.SecurityQualityOfService = NULL;
676 RtlInitUnicodeString( &nameW, name );
677 attr.ObjectName = &nameW;
678 attr.RootDirectory = get_BaseNamedObjects_handle();
681 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
682 if (status == STATUS_OBJECT_NAME_EXISTS)
683 SetLastError( ERROR_ALREADY_EXISTS );
685 SetLastError( RtlNtStatusToDosError(status) );
690 /***********************************************************************
691 * OpenMutexA (KERNEL32.@)
693 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
695 WCHAR buffer[MAX_PATH];
697 if (!name) return OpenMutexW( access, inherit, NULL );
699 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
701 SetLastError( ERROR_FILENAME_EXCED_RANGE );
704 return OpenMutexW( access, inherit, buffer );
708 /***********************************************************************
709 * OpenMutexW (KERNEL32.@)
711 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
714 UNICODE_STRING nameW;
715 OBJECT_ATTRIBUTES attr;
718 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
720 attr.Length = sizeof(attr);
721 attr.RootDirectory = 0;
722 attr.ObjectName = NULL;
723 attr.Attributes = inherit ? OBJ_INHERIT : 0;
724 attr.SecurityDescriptor = NULL;
725 attr.SecurityQualityOfService = NULL;
728 RtlInitUnicodeString( &nameW, name );
729 attr.ObjectName = &nameW;
730 attr.RootDirectory = get_BaseNamedObjects_handle();
733 status = NtOpenMutant( &ret, access, &attr );
734 if (status != STATUS_SUCCESS)
736 SetLastError( RtlNtStatusToDosError(status) );
743 /***********************************************************************
744 * ReleaseMutex (KERNEL32.@)
746 BOOL WINAPI ReleaseMutex( HANDLE handle )
750 status = NtReleaseMutant(handle, NULL);
751 if (status != STATUS_SUCCESS)
753 SetLastError( RtlNtStatusToDosError(status) );
765 /***********************************************************************
766 * CreateSemaphoreA (KERNEL32.@)
768 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
770 WCHAR buffer[MAX_PATH];
772 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
774 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
776 SetLastError( ERROR_FILENAME_EXCED_RANGE );
779 return CreateSemaphoreW( sa, initial, max, buffer );
783 /***********************************************************************
784 * CreateSemaphoreW (KERNEL32.@)
786 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
787 LONG max, LPCWSTR name )
790 UNICODE_STRING nameW;
791 OBJECT_ATTRIBUTES attr;
794 attr.Length = sizeof(attr);
795 attr.RootDirectory = 0;
796 attr.ObjectName = NULL;
797 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
798 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
799 attr.SecurityQualityOfService = NULL;
802 RtlInitUnicodeString( &nameW, name );
803 attr.ObjectName = &nameW;
804 attr.RootDirectory = get_BaseNamedObjects_handle();
807 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
808 if (status == STATUS_OBJECT_NAME_EXISTS)
809 SetLastError( ERROR_ALREADY_EXISTS );
811 SetLastError( RtlNtStatusToDosError(status) );
816 /***********************************************************************
817 * OpenSemaphoreA (KERNEL32.@)
819 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
821 WCHAR buffer[MAX_PATH];
823 if (!name) return OpenSemaphoreW( access, inherit, NULL );
825 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
827 SetLastError( ERROR_FILENAME_EXCED_RANGE );
830 return OpenSemaphoreW( access, inherit, buffer );
834 /***********************************************************************
835 * OpenSemaphoreW (KERNEL32.@)
837 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
840 UNICODE_STRING nameW;
841 OBJECT_ATTRIBUTES attr;
844 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
846 attr.Length = sizeof(attr);
847 attr.RootDirectory = 0;
848 attr.ObjectName = NULL;
849 attr.Attributes = inherit ? OBJ_INHERIT : 0;
850 attr.SecurityDescriptor = NULL;
851 attr.SecurityQualityOfService = NULL;
854 RtlInitUnicodeString( &nameW, name );
855 attr.ObjectName = &nameW;
856 attr.RootDirectory = get_BaseNamedObjects_handle();
859 status = NtOpenSemaphore( &ret, access, &attr );
860 if (status != STATUS_SUCCESS)
862 SetLastError( RtlNtStatusToDosError(status) );
869 /***********************************************************************
870 * ReleaseSemaphore (KERNEL32.@)
872 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
874 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
875 if (status) SetLastError( RtlNtStatusToDosError(status) );
885 /***********************************************************************
886 * CreateWaitableTimerA (KERNEL32.@)
888 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
890 WCHAR buffer[MAX_PATH];
892 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
894 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
896 SetLastError( ERROR_FILENAME_EXCED_RANGE );
899 return CreateWaitableTimerW( sa, manual, buffer );
903 /***********************************************************************
904 * CreateWaitableTimerW (KERNEL32.@)
906 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
910 UNICODE_STRING nameW;
911 OBJECT_ATTRIBUTES attr;
913 attr.Length = sizeof(attr);
914 attr.RootDirectory = 0;
915 attr.ObjectName = NULL;
916 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
917 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
918 attr.SecurityQualityOfService = NULL;
921 RtlInitUnicodeString( &nameW, name );
922 attr.ObjectName = &nameW;
923 attr.RootDirectory = get_BaseNamedObjects_handle();
926 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
927 manual ? NotificationTimer : SynchronizationTimer);
928 if (status == STATUS_OBJECT_NAME_EXISTS)
929 SetLastError( ERROR_ALREADY_EXISTS );
931 SetLastError( RtlNtStatusToDosError(status) );
936 /***********************************************************************
937 * OpenWaitableTimerA (KERNEL32.@)
939 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
941 WCHAR buffer[MAX_PATH];
943 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
945 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
947 SetLastError( ERROR_FILENAME_EXCED_RANGE );
950 return OpenWaitableTimerW( access, inherit, buffer );
954 /***********************************************************************
955 * OpenWaitableTimerW (KERNEL32.@)
957 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
960 UNICODE_STRING nameW;
961 OBJECT_ATTRIBUTES attr;
964 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
966 attr.Length = sizeof(attr);
967 attr.RootDirectory = 0;
968 attr.ObjectName = NULL;
969 attr.Attributes = inherit ? OBJ_INHERIT : 0;
970 attr.SecurityDescriptor = NULL;
971 attr.SecurityQualityOfService = NULL;
974 RtlInitUnicodeString( &nameW, name );
975 attr.ObjectName = &nameW;
976 attr.RootDirectory = get_BaseNamedObjects_handle();
979 status = NtOpenTimer(&handle, access, &attr);
980 if (status != STATUS_SUCCESS)
982 SetLastError( RtlNtStatusToDosError(status) );
989 /***********************************************************************
990 * SetWaitableTimer (KERNEL32.@)
992 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
993 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
995 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
996 arg, resume, period, NULL);
998 if (status != STATUS_SUCCESS)
1000 SetLastError( RtlNtStatusToDosError(status) );
1001 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
1007 /***********************************************************************
1008 * CancelWaitableTimer (KERNEL32.@)
1010 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1014 status = NtCancelTimer(handle, NULL);
1015 if (status != STATUS_SUCCESS)
1017 SetLastError( RtlNtStatusToDosError(status) );
1024 /***********************************************************************
1025 * CreateTimerQueue (KERNEL32.@)
1027 HANDLE WINAPI CreateTimerQueue(void)
1030 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1035 /***********************************************************************
1036 * DeleteTimerQueueEx (KERNEL32.@)
1038 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1040 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
1041 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1045 /***********************************************************************
1046 * CreateTimerQueueTimer (KERNEL32.@)
1048 * Creates a timer-queue timer. This timer expires at the specified due
1049 * time (in ms), then after every specified period (in ms). When the timer
1050 * expires, the callback function is called.
1053 * nonzero on success or zero on failure
1058 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1059 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1060 DWORD DueTime, DWORD Period, ULONG Flags )
1063 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1067 /***********************************************************************
1068 * DeleteTimerQueueTimer (KERNEL32.@)
1070 * Cancels a timer-queue timer.
1073 * nonzero on success or zero on failure
1078 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1079 HANDLE CompletionEvent )
1082 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1092 /***********************************************************************
1093 * CreateNamedPipeA (KERNEL32.@)
1095 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1096 DWORD dwPipeMode, DWORD nMaxInstances,
1097 DWORD nOutBufferSize, DWORD nInBufferSize,
1098 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1100 WCHAR buffer[MAX_PATH];
1102 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1103 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1105 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1107 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1108 return INVALID_HANDLE_VALUE;
1110 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1111 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1115 /***********************************************************************
1116 * CreateNamedPipeW (KERNEL32.@)
1118 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1119 DWORD dwPipeMode, DWORD nMaxInstances,
1120 DWORD nOutBufferSize, DWORD nInBufferSize,
1121 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1124 UNICODE_STRING nt_name;
1125 OBJECT_ATTRIBUTES attr;
1126 DWORD access, options;
1127 BOOLEAN pipe_type, read_mode, non_block;
1129 IO_STATUS_BLOCK iosb;
1130 LARGE_INTEGER timeout;
1132 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1133 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1134 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1136 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1138 SetLastError( ERROR_PATH_NOT_FOUND );
1139 return INVALID_HANDLE_VALUE;
1141 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1143 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1144 RtlFreeUnicodeString( &nt_name );
1145 return INVALID_HANDLE_VALUE;
1148 attr.Length = sizeof(attr);
1149 attr.RootDirectory = 0;
1150 attr.ObjectName = &nt_name;
1151 attr.Attributes = OBJ_CASE_INSENSITIVE |
1152 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1153 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1154 attr.SecurityQualityOfService = NULL;
1156 switch(dwOpenMode & 3)
1158 case PIPE_ACCESS_INBOUND:
1159 options = FILE_PIPE_INBOUND;
1160 access = GENERIC_READ;
1162 case PIPE_ACCESS_OUTBOUND:
1163 options = FILE_PIPE_OUTBOUND;
1164 access = GENERIC_WRITE;
1166 case PIPE_ACCESS_DUPLEX:
1167 options = FILE_PIPE_FULL_DUPLEX;
1168 access = GENERIC_READ | GENERIC_WRITE;
1171 SetLastError( ERROR_INVALID_PARAMETER );
1172 return INVALID_HANDLE_VALUE;
1174 access |= SYNCHRONIZE;
1175 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1176 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1177 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1178 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1179 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1180 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0U;
1182 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1186 status = NtCreateNamedPipeFile(&handle, access, &attr, &iosb, 0,
1187 FILE_OVERWRITE_IF, options, pipe_type,
1188 read_mode, non_block, nMaxInstances,
1189 nInBufferSize, nOutBufferSize, &timeout);
1191 RtlFreeUnicodeString( &nt_name );
1194 handle = INVALID_HANDLE_VALUE;
1195 SetLastError( RtlNtStatusToDosError(status) );
1201 /***********************************************************************
1202 * PeekNamedPipe (KERNEL32.@)
1204 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1205 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1207 FILE_PIPE_PEEK_BUFFER local_buffer;
1208 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1212 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1213 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1215 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1219 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1220 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1223 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1224 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1225 if (lpcbRead) *lpcbRead = read_size;
1226 if (lpcbMessage) *lpcbMessage = 0; /* FIXME */
1227 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1229 else SetLastError( RtlNtStatusToDosError(status) );
1231 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1235 /***********************************************************************
1236 * WaitNamedPipeA (KERNEL32.@)
1238 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1240 WCHAR buffer[MAX_PATH];
1242 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1244 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1246 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1249 return WaitNamedPipeW( buffer, nTimeOut );
1253 /***********************************************************************
1254 * WaitNamedPipeW (KERNEL32.@)
1256 * Waits for a named pipe instance to become available
1259 * name [I] Pointer to a named pipe name to wait for
1260 * nTimeOut [I] How long to wait in ms
1263 * TRUE: Success, named pipe can be opened with CreateFile
1264 * FALSE: Failure, GetLastError can be called for further details
1266 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1268 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1270 UNICODE_STRING nt_name, pipe_dev_name;
1271 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1272 IO_STATUS_BLOCK iosb;
1273 OBJECT_ATTRIBUTES attr;
1277 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1279 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1282 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1283 nt_name.Length < sizeof(leadin) ||
1284 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR) != 0))
1286 RtlFreeUnicodeString( &nt_name );
1287 SetLastError( ERROR_PATH_NOT_FOUND );
1291 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1292 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1294 RtlFreeUnicodeString( &nt_name );
1295 SetLastError( ERROR_OUTOFMEMORY );
1299 pipe_dev_name.Buffer = nt_name.Buffer;
1300 pipe_dev_name.Length = sizeof(leadin);
1301 pipe_dev_name.MaximumLength = sizeof(leadin);
1302 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1303 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1304 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1305 FILE_SYNCHRONOUS_IO_NONALERT);
1306 if (status != ERROR_SUCCESS)
1308 SetLastError( ERROR_PATH_NOT_FOUND );
1312 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1313 if (nTimeOut == NMPWAIT_WAIT_FOREVER)
1314 pipe_wait->Timeout.QuadPart = ((ULONGLONG)0x7fffffff << 32) | 0xffffffff;
1316 pipe_wait->Timeout.QuadPart = (ULONGLONG)nTimeOut * -10000;
1317 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1318 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1319 pipe_wait->NameLength);
1320 RtlFreeUnicodeString( &nt_name );
1322 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1323 pipe_wait, sz_pipe_wait, NULL, 0 );
1325 HeapFree( GetProcessHeap(), 0, pipe_wait );
1326 NtClose( pipe_dev );
1328 if(status != STATUS_SUCCESS)
1330 SetLastError(RtlNtStatusToDosError(status));
1338 /***********************************************************************
1339 * ConnectNamedPipe (KERNEL32.@)
1341 * Connects to a named pipe
1344 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1345 * overlapped: Optional OVERLAPPED struct
1349 * FALSE: Failure, GetLastError can be called for further details
1351 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1354 IO_STATUS_BLOCK status_block;
1355 LPVOID cvalue = NULL;
1357 TRACE("(%p,%p)\n", hPipe, overlapped);
1361 overlapped->Internal = STATUS_PENDING;
1362 overlapped->InternalHigh = 0;
1363 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1366 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, cvalue,
1367 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1368 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1370 if (status == STATUS_SUCCESS) return TRUE;
1371 SetLastError( RtlNtStatusToDosError(status) );
1375 /***********************************************************************
1376 * DisconnectNamedPipe (KERNEL32.@)
1378 * Disconnects from a named pipe
1381 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1385 * FALSE: Failure, GetLastError can be called for further details
1387 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1390 IO_STATUS_BLOCK io_block;
1392 TRACE("(%p)\n",hPipe);
1394 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1396 if (status == STATUS_SUCCESS) return TRUE;
1397 SetLastError( RtlNtStatusToDosError(status) );
1401 /***********************************************************************
1402 * TransactNamedPipe (KERNEL32.@)
1405 * should be done as a single operation in the wineserver or kernel
1407 BOOL WINAPI TransactNamedPipe(
1408 HANDLE handle, LPVOID write_buf, DWORD write_size, LPVOID read_buf,
1409 DWORD read_size, LPDWORD bytes_read, LPOVERLAPPED overlapped)
1414 TRACE("%p %p %d %p %d %p %p\n",
1415 handle, write_buf, write_size, read_buf,
1416 read_size, bytes_read, overlapped);
1420 FIXME("Doesn't support overlapped operation as yet\n");
1424 r = WriteFile(handle, write_buf, write_size, &count, NULL);
1426 r = ReadFile(handle, read_buf, read_size, bytes_read, NULL);
1431 /***********************************************************************
1432 * GetNamedPipeInfo (KERNEL32.@)
1434 BOOL WINAPI GetNamedPipeInfo(
1435 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1436 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1438 FILE_PIPE_LOCAL_INFORMATION fpli;
1439 IO_STATUS_BLOCK iosb;
1442 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1443 FilePipeLocalInformation);
1446 SetLastError( RtlNtStatusToDosError(status) );
1452 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1453 PIPE_SERVER_END : PIPE_CLIENT_END;
1454 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1455 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1458 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1459 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1460 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1465 /***********************************************************************
1466 * GetNamedPipeHandleStateA (KERNEL32.@)
1468 BOOL WINAPI GetNamedPipeHandleStateA(
1469 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1470 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1471 LPSTR lpUsername, DWORD nUsernameMaxSize)
1473 FIXME("%p %p %p %p %p %p %d\n",
1474 hNamedPipe, lpState, lpCurInstances,
1475 lpMaxCollectionCount, lpCollectDataTimeout,
1476 lpUsername, nUsernameMaxSize);
1481 /***********************************************************************
1482 * GetNamedPipeHandleStateW (KERNEL32.@)
1484 BOOL WINAPI GetNamedPipeHandleStateW(
1485 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1486 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1487 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1489 FIXME("%p %p %p %p %p %p %d\n",
1490 hNamedPipe, lpState, lpCurInstances,
1491 lpMaxCollectionCount, lpCollectDataTimeout,
1492 lpUsername, nUsernameMaxSize);
1497 /***********************************************************************
1498 * SetNamedPipeHandleState (KERNEL32.@)
1500 BOOL WINAPI SetNamedPipeHandleState(
1501 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1502 LPDWORD lpCollectDataTimeout)
1504 /* should be a fixme, but this function is called a lot by the RPC
1505 * runtime, and it slows down InstallShield a fair bit. */
1506 WARN("stub: %p %p/%d %p %p\n",
1507 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1511 /***********************************************************************
1512 * CallNamedPipeA (KERNEL32.@)
1514 BOOL WINAPI CallNamedPipeA(
1515 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1516 LPVOID lpOutput, DWORD dwOutputSize,
1517 LPDWORD lpBytesRead, DWORD nTimeout)
1523 TRACE("%s %p %d %p %d %p %d\n",
1524 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1525 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1527 if( lpNamedPipeName )
1529 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1530 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1531 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1533 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1534 dwOutputSize, lpBytesRead, nTimeout );
1535 if( lpNamedPipeName )
1536 HeapFree( GetProcessHeap(), 0, str );
1541 /***********************************************************************
1542 * CallNamedPipeW (KERNEL32.@)
1544 BOOL WINAPI CallNamedPipeW(
1545 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1546 LPVOID lpOutput, DWORD lpOutputSize,
1547 LPDWORD lpBytesRead, DWORD nTimeout)
1553 TRACE("%s %p %d %p %d %p %d\n",
1554 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1555 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1557 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1558 if (pipe == INVALID_HANDLE_VALUE)
1560 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1563 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1564 if (pipe == INVALID_HANDLE_VALUE)
1568 mode = PIPE_READMODE_MESSAGE;
1569 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1571 /* Currently SetNamedPipeHandleState() is a stub returning FALSE */
1572 if (ret) FIXME("Now that SetNamedPipeHandleState() is more than a stub, please update CallNamedPipeW\n");
1580 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1588 /******************************************************************
1589 * CreatePipe (KERNEL32.@)
1592 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1593 LPSECURITY_ATTRIBUTES sa, DWORD size )
1595 static unsigned index /* = 0 */;
1598 unsigned in_index = index;
1599 UNICODE_STRING nt_name;
1600 OBJECT_ATTRIBUTES attr;
1602 IO_STATUS_BLOCK iosb;
1603 LARGE_INTEGER timeout;
1605 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1607 attr.Length = sizeof(attr);
1608 attr.RootDirectory = 0;
1609 attr.ObjectName = &nt_name;
1610 attr.Attributes = OBJ_CASE_INSENSITIVE |
1611 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1612 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1613 attr.SecurityQualityOfService = NULL;
1615 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1616 /* generate a unique pipe name (system wide) */
1619 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1620 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1621 'u','.','%','0','8','u','\0' };
1623 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1624 GetCurrentProcessId(), ++index);
1625 RtlInitUnicodeString(&nt_name, name);
1626 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1627 0, FILE_OVERWRITE_IF,
1628 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1629 FALSE, FALSE, FALSE,
1630 1, size, size, &timeout);
1633 SetLastError( RtlNtStatusToDosError(status) );
1634 hr = INVALID_HANDLE_VALUE;
1636 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1637 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1638 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1640 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1641 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1645 SetLastError( RtlNtStatusToDosError(status) );
1656 /******************************************************************************
1657 * CreateMailslotA [KERNEL32.@]
1659 * See CreateMailslotW.
1661 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1662 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1668 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1669 nMaxMessageSize, lReadTimeout, sa);
1673 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1674 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1675 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1678 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1680 HeapFree( GetProcessHeap(), 0, name );
1686 /******************************************************************************
1687 * CreateMailslotW [KERNEL32.@]
1689 * Create a mailslot with specified name.
1692 * lpName [I] Pointer to string for mailslot name
1693 * nMaxMessageSize [I] Maximum message size
1694 * lReadTimeout [I] Milliseconds before read time-out
1695 * sa [I] Pointer to security structure
1698 * Success: Handle to mailslot
1699 * Failure: INVALID_HANDLE_VALUE
1701 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1702 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1704 HANDLE handle = INVALID_HANDLE_VALUE;
1705 OBJECT_ATTRIBUTES attr;
1706 UNICODE_STRING nameW;
1707 LARGE_INTEGER timeout;
1708 IO_STATUS_BLOCK iosb;
1711 TRACE("%s %d %d %p\n", debugstr_w(lpName),
1712 nMaxMessageSize, lReadTimeout, sa);
1714 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1716 SetLastError( ERROR_PATH_NOT_FOUND );
1717 return INVALID_HANDLE_VALUE;
1720 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1722 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1723 RtlFreeUnicodeString( &nameW );
1724 return INVALID_HANDLE_VALUE;
1727 attr.Length = sizeof(attr);
1728 attr.RootDirectory = 0;
1729 attr.Attributes = OBJ_CASE_INSENSITIVE;
1730 attr.ObjectName = &nameW;
1731 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1732 attr.SecurityQualityOfService = NULL;
1734 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1735 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1737 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1739 status = NtCreateMailslotFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr,
1740 &iosb, 0, 0, nMaxMessageSize, &timeout );
1743 SetLastError( RtlNtStatusToDosError(status) );
1744 handle = INVALID_HANDLE_VALUE;
1747 RtlFreeUnicodeString( &nameW );
1752 /******************************************************************************
1753 * GetMailslotInfo [KERNEL32.@]
1755 * Retrieve information about a mailslot.
1758 * hMailslot [I] Mailslot handle
1759 * lpMaxMessageSize [O] Address of maximum message size
1760 * lpNextSize [O] Address of size of next message
1761 * lpMessageCount [O] Address of number of messages
1762 * lpReadTimeout [O] Address of read time-out
1768 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1769 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1770 LPDWORD lpReadTimeout )
1772 FILE_MAILSLOT_QUERY_INFORMATION info;
1773 IO_STATUS_BLOCK iosb;
1776 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1777 lpNextSize, lpMessageCount, lpReadTimeout);
1779 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1780 FileMailslotQueryInformation );
1782 if( status != STATUS_SUCCESS )
1784 SetLastError( RtlNtStatusToDosError(status) );
1788 if( lpMaxMessageSize )
1789 *lpMaxMessageSize = info.MaximumMessageSize;
1791 *lpNextSize = info.NextMessageSize;
1792 if( lpMessageCount )
1793 *lpMessageCount = info.MessagesAvailable;
1796 if (info.ReadTimeout.QuadPart == (((LONGLONG)0x7fffffff << 32) | 0xffffffff))
1797 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
1799 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1805 /******************************************************************************
1806 * SetMailslotInfo [KERNEL32.@]
1808 * Set the read timeout of a mailslot.
1811 * hMailslot [I] Mailslot handle
1812 * dwReadTimeout [I] Timeout in milliseconds.
1818 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1820 FILE_MAILSLOT_SET_INFORMATION info;
1821 IO_STATUS_BLOCK iosb;
1824 TRACE("%p %d\n", hMailslot, dwReadTimeout);
1826 if (dwReadTimeout != MAILSLOT_WAIT_FOREVER)
1827 info.ReadTimeout.QuadPart = (ULONGLONG)dwReadTimeout * -10000;
1829 info.ReadTimeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1830 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1831 FileMailslotSetInformation );
1832 if( status != STATUS_SUCCESS )
1834 SetLastError( RtlNtStatusToDosError(status) );
1841 /******************************************************************************
1842 * CreateIoCompletionPort (KERNEL32.@)
1844 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1845 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1850 TRACE("(%p, %p, %08lx, %08x)\n",
1851 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1853 if (hExistingCompletionPort && hFileHandle == INVALID_HANDLE_VALUE)
1855 SetLastError( ERROR_INVALID_PARAMETER);
1859 if (hExistingCompletionPort)
1860 ret = hExistingCompletionPort;
1863 status = NtCreateIoCompletion( &ret, IO_COMPLETION_ALL_ACCESS, NULL, dwNumberOfConcurrentThreads );
1864 if (status != STATUS_SUCCESS) goto fail;
1867 if (hFileHandle != INVALID_HANDLE_VALUE)
1869 FILE_COMPLETION_INFORMATION info;
1870 IO_STATUS_BLOCK iosb;
1872 info.CompletionPort = ret;
1873 info.CompletionKey = CompletionKey;
1874 status = NtSetInformationFile( hFileHandle, &iosb, &info, sizeof(info), FileCompletionInformation );
1875 if (status != STATUS_SUCCESS) goto fail;
1881 if (ret && !hExistingCompletionPort)
1883 SetLastError( RtlNtStatusToDosError(status) );
1887 /******************************************************************************
1888 * GetQueuedCompletionStatus (KERNEL32.@)
1890 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1891 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1892 DWORD dwMilliseconds )
1895 IO_STATUS_BLOCK iosb;
1896 LARGE_INTEGER wait_time;
1898 TRACE("(%p,%p,%p,%p,%d)\n",
1899 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1901 *lpOverlapped = NULL;
1903 status = NtRemoveIoCompletion( CompletionPort, pCompletionKey, (PULONG_PTR)lpOverlapped,
1904 &iosb, get_nt_timeout( &wait_time, dwMilliseconds ) );
1905 if (status == STATUS_SUCCESS)
1907 *lpNumberOfBytesTransferred = iosb.Information;
1911 SetLastError( RtlNtStatusToDosError(status) );
1916 /******************************************************************************
1917 * PostQueuedCompletionStatus (KERNEL32.@)
1919 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
1920 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
1924 TRACE("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
1926 status = NtSetIoCompletion( CompletionPort, dwCompletionKey, (ULONG_PTR)lpOverlapped,
1927 STATUS_SUCCESS, dwNumberOfBytes );
1929 if (status == STATUS_SUCCESS) return TRUE;
1930 SetLastError( RtlNtStatusToDosError(status) );
1934 /******************************************************************************
1935 * BindIoCompletionCallback (KERNEL32.@)
1937 BOOL WINAPI BindIoCompletionCallback( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags)
1941 TRACE("(%p, %p, %d)\n", FileHandle, Function, Flags);
1943 status = RtlSetIoCompletionCallback( FileHandle, (PRTL_OVERLAPPED_COMPLETION_ROUTINE)Function, Flags );
1944 if (status == STATUS_SUCCESS) return TRUE;
1945 SetLastError( RtlNtStatusToDosError(status) );
1949 /******************************************************************************
1950 * CreateJobObjectW (KERNEL32.@)
1952 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1954 FIXME("%p %s\n", attr, debugstr_w(name) );
1955 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1959 /******************************************************************************
1960 * CreateJobObjectA (KERNEL32.@)
1962 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1968 TRACE("%p %s\n", attr, debugstr_a(name) );
1972 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1973 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1976 SetLastError( ERROR_OUTOFMEMORY );
1979 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1982 r = CreateJobObjectW( attr, str );
1984 HeapFree( GetProcessHeap(), 0, str );
1989 /******************************************************************************
1990 * AssignProcessToJobObject (KERNEL32.@)
1992 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1994 FIXME("%p %p\n", hJob, hProcess);
2000 /***********************************************************************
2001 * InterlockedCompareExchange (KERNEL32.@)
2003 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
2004 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
2005 "movl 12(%esp),%eax\n\t"
2006 "movl 8(%esp),%ecx\n\t"
2007 "movl 4(%esp),%edx\n\t"
2008 "lock; cmpxchgl %ecx,(%edx)\n\t"
2011 /***********************************************************************
2012 * InterlockedExchange (KERNEL32.@)
2014 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
2015 __ASM_GLOBAL_FUNC(InterlockedExchange,
2016 "movl 8(%esp),%eax\n\t"
2017 "movl 4(%esp),%edx\n\t"
2018 "lock; xchgl %eax,(%edx)\n\t"
2021 /***********************************************************************
2022 * InterlockedExchangeAdd (KERNEL32.@)
2024 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
2025 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
2026 "movl 8(%esp),%eax\n\t"
2027 "movl 4(%esp),%edx\n\t"
2028 "lock; xaddl %eax,(%edx)\n\t"
2031 /***********************************************************************
2032 * InterlockedIncrement (KERNEL32.@)
2034 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2035 __ASM_GLOBAL_FUNC(InterlockedIncrement,
2036 "movl 4(%esp),%edx\n\t"
2038 "lock; xaddl %eax,(%edx)\n\t"
2042 /***********************************************************************
2043 * InterlockedDecrement (KERNEL32.@)
2045 __ASM_GLOBAL_FUNC(InterlockedDecrement,
2046 "movl 4(%esp),%edx\n\t"
2048 "lock; xaddl %eax,(%edx)\n\t"
2052 #else /* __i386__ */
2054 /***********************************************************************
2055 * InterlockedCompareExchange (KERNEL32.@)
2057 * Atomically swap one value with another.
2060 * dest [I/O] The value to replace
2061 * xchq [I] The value to be swapped
2062 * compare [I] The value to compare to dest
2065 * The resulting value of dest.
2068 * dest is updated only if it is equal to compare, otherwise no swap is done.
2070 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
2072 return interlocked_cmpxchg( (int *)dest, xchg, compare );
2075 /***********************************************************************
2076 * InterlockedExchange (KERNEL32.@)
2078 * Atomically swap one value with another.
2081 * dest [I/O] The value to replace
2082 * val [I] The value to be swapped
2085 * The resulting value of dest.
2087 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
2089 return interlocked_xchg( (int *)dest, val );
2092 /***********************************************************************
2093 * InterlockedExchangeAdd (KERNEL32.@)
2095 * Atomically add one value to another.
2098 * dest [I/O] The value to add to
2099 * incr [I] The value to be added
2102 * The resulting value of dest.
2104 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
2106 return interlocked_xchg_add( (int *)dest, incr );
2109 /***********************************************************************
2110 * InterlockedIncrement (KERNEL32.@)
2112 * Atomically increment a value.
2115 * dest [I/O] The value to increment
2118 * The resulting value of dest.
2120 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
2122 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
2125 /***********************************************************************
2126 * InterlockedDecrement (KERNEL32.@)
2128 * Atomically decrement a value.
2131 * dest [I/O] The value to decrement
2134 * The resulting value of dest.
2136 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
2138 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
2141 #endif /* __i386__ */