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_CASE_INSENSITIVE | OBJ_OPENIF |
917 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
918 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
919 attr.SecurityQualityOfService = NULL;
922 RtlInitUnicodeString( &nameW, name );
923 attr.ObjectName = &nameW;
924 attr.RootDirectory = get_BaseNamedObjects_handle();
927 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
928 manual ? NotificationTimer : SynchronizationTimer);
929 if (status == STATUS_OBJECT_NAME_EXISTS)
930 SetLastError( ERROR_ALREADY_EXISTS );
932 SetLastError( RtlNtStatusToDosError(status) );
937 /***********************************************************************
938 * OpenWaitableTimerA (KERNEL32.@)
940 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
942 WCHAR buffer[MAX_PATH];
944 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
946 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
948 SetLastError( ERROR_FILENAME_EXCED_RANGE );
951 return OpenWaitableTimerW( access, inherit, buffer );
955 /***********************************************************************
956 * OpenWaitableTimerW (KERNEL32.@)
958 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
961 UNICODE_STRING nameW;
962 OBJECT_ATTRIBUTES attr;
965 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
967 attr.Length = sizeof(attr);
968 attr.RootDirectory = 0;
969 attr.ObjectName = NULL;
970 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
971 attr.SecurityDescriptor = NULL;
972 attr.SecurityQualityOfService = NULL;
975 RtlInitUnicodeString( &nameW, name );
976 attr.ObjectName = &nameW;
977 attr.RootDirectory = get_BaseNamedObjects_handle();
980 status = NtOpenTimer(&handle, access, &attr);
981 if (status != STATUS_SUCCESS)
983 SetLastError( RtlNtStatusToDosError(status) );
990 /***********************************************************************
991 * SetWaitableTimer (KERNEL32.@)
993 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
994 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
996 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
997 arg, resume, period, NULL);
999 if (status != STATUS_SUCCESS)
1001 SetLastError( RtlNtStatusToDosError(status) );
1002 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
1008 /***********************************************************************
1009 * CancelWaitableTimer (KERNEL32.@)
1011 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1015 status = NtCancelTimer(handle, NULL);
1016 if (status != STATUS_SUCCESS)
1018 SetLastError( RtlNtStatusToDosError(status) );
1025 /***********************************************************************
1026 * CreateTimerQueue (KERNEL32.@)
1028 HANDLE WINAPI CreateTimerQueue(void)
1031 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1036 /***********************************************************************
1037 * DeleteTimerQueueEx (KERNEL32.@)
1039 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1041 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
1042 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1046 /***********************************************************************
1047 * CreateTimerQueueTimer (KERNEL32.@)
1049 * Creates a timer-queue timer. This timer expires at the specified due
1050 * time (in ms), then after every specified period (in ms). When the timer
1051 * expires, the callback function is called.
1054 * nonzero on success or zero on failure
1059 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1060 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1061 DWORD DueTime, DWORD Period, ULONG Flags )
1064 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1068 /***********************************************************************
1069 * DeleteTimerQueueTimer (KERNEL32.@)
1071 * Cancels a timer-queue timer.
1074 * nonzero on success or zero on failure
1079 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1080 HANDLE CompletionEvent )
1083 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1093 /***********************************************************************
1094 * CreateNamedPipeA (KERNEL32.@)
1096 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1097 DWORD dwPipeMode, DWORD nMaxInstances,
1098 DWORD nOutBufferSize, DWORD nInBufferSize,
1099 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1101 WCHAR buffer[MAX_PATH];
1103 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1104 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1106 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1108 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1109 return INVALID_HANDLE_VALUE;
1111 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1112 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1116 /***********************************************************************
1117 * CreateNamedPipeW (KERNEL32.@)
1119 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1120 DWORD dwPipeMode, DWORD nMaxInstances,
1121 DWORD nOutBufferSize, DWORD nInBufferSize,
1122 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1125 UNICODE_STRING nt_name;
1126 OBJECT_ATTRIBUTES attr;
1127 DWORD access, options;
1128 BOOLEAN pipe_type, read_mode, non_block;
1130 IO_STATUS_BLOCK iosb;
1131 LARGE_INTEGER timeout;
1133 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1134 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1135 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1137 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1139 SetLastError( ERROR_PATH_NOT_FOUND );
1140 return INVALID_HANDLE_VALUE;
1142 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1144 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1145 RtlFreeUnicodeString( &nt_name );
1146 return INVALID_HANDLE_VALUE;
1149 attr.Length = sizeof(attr);
1150 attr.RootDirectory = 0;
1151 attr.ObjectName = &nt_name;
1152 attr.Attributes = OBJ_CASE_INSENSITIVE |
1153 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1154 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1155 attr.SecurityQualityOfService = NULL;
1157 switch(dwOpenMode & 3)
1159 case PIPE_ACCESS_INBOUND:
1160 options = FILE_PIPE_INBOUND;
1161 access = GENERIC_READ;
1163 case PIPE_ACCESS_OUTBOUND:
1164 options = FILE_PIPE_OUTBOUND;
1165 access = GENERIC_WRITE;
1167 case PIPE_ACCESS_DUPLEX:
1168 options = FILE_PIPE_FULL_DUPLEX;
1169 access = GENERIC_READ | GENERIC_WRITE;
1172 SetLastError( ERROR_INVALID_PARAMETER );
1173 return INVALID_HANDLE_VALUE;
1175 access |= SYNCHRONIZE;
1176 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1177 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1178 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1179 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1180 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1181 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0U;
1183 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1187 status = NtCreateNamedPipeFile(&handle, access, &attr, &iosb, 0,
1188 FILE_OVERWRITE_IF, options, pipe_type,
1189 read_mode, non_block, nMaxInstances,
1190 nInBufferSize, nOutBufferSize, &timeout);
1192 RtlFreeUnicodeString( &nt_name );
1195 handle = INVALID_HANDLE_VALUE;
1196 SetLastError( RtlNtStatusToDosError(status) );
1202 /***********************************************************************
1203 * PeekNamedPipe (KERNEL32.@)
1205 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1206 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1208 FILE_PIPE_PEEK_BUFFER local_buffer;
1209 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1213 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1214 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1216 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1220 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1221 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1224 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1225 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1226 if (lpcbRead) *lpcbRead = read_size;
1227 if (lpcbMessage) *lpcbMessage = 0; /* FIXME */
1228 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1230 else SetLastError( RtlNtStatusToDosError(status) );
1232 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1236 /***********************************************************************
1237 * WaitNamedPipeA (KERNEL32.@)
1239 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1241 WCHAR buffer[MAX_PATH];
1243 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1245 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1247 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1250 return WaitNamedPipeW( buffer, nTimeOut );
1254 /***********************************************************************
1255 * WaitNamedPipeW (KERNEL32.@)
1257 * Waits for a named pipe instance to become available
1260 * name [I] Pointer to a named pipe name to wait for
1261 * nTimeOut [I] How long to wait in ms
1264 * TRUE: Success, named pipe can be opened with CreateFile
1265 * FALSE: Failure, GetLastError can be called for further details
1267 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1269 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1271 UNICODE_STRING nt_name, pipe_dev_name;
1272 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1273 IO_STATUS_BLOCK iosb;
1274 OBJECT_ATTRIBUTES attr;
1278 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1280 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1283 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1284 nt_name.Length < sizeof(leadin) ||
1285 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR) != 0))
1287 RtlFreeUnicodeString( &nt_name );
1288 SetLastError( ERROR_PATH_NOT_FOUND );
1292 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1293 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1295 RtlFreeUnicodeString( &nt_name );
1296 SetLastError( ERROR_OUTOFMEMORY );
1300 pipe_dev_name.Buffer = nt_name.Buffer;
1301 pipe_dev_name.Length = sizeof(leadin);
1302 pipe_dev_name.MaximumLength = sizeof(leadin);
1303 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1304 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1305 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1306 FILE_SYNCHRONOUS_IO_NONALERT);
1307 if (status != ERROR_SUCCESS)
1309 SetLastError( ERROR_PATH_NOT_FOUND );
1313 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1314 if (nTimeOut == NMPWAIT_WAIT_FOREVER)
1315 pipe_wait->Timeout.QuadPart = ((ULONGLONG)0x7fffffff << 32) | 0xffffffff;
1317 pipe_wait->Timeout.QuadPart = (ULONGLONG)nTimeOut * -10000;
1318 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1319 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1320 pipe_wait->NameLength);
1321 RtlFreeUnicodeString( &nt_name );
1323 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1324 pipe_wait, sz_pipe_wait, NULL, 0 );
1326 HeapFree( GetProcessHeap(), 0, pipe_wait );
1327 NtClose( pipe_dev );
1329 if(status != STATUS_SUCCESS)
1331 SetLastError(RtlNtStatusToDosError(status));
1339 /***********************************************************************
1340 * ConnectNamedPipe (KERNEL32.@)
1342 * Connects to a named pipe
1345 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1346 * overlapped: Optional OVERLAPPED struct
1350 * FALSE: Failure, GetLastError can be called for further details
1352 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1355 IO_STATUS_BLOCK status_block;
1356 LPVOID cvalue = NULL;
1358 TRACE("(%p,%p)\n", hPipe, overlapped);
1362 overlapped->Internal = STATUS_PENDING;
1363 overlapped->InternalHigh = 0;
1364 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1367 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, cvalue,
1368 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1369 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1371 if (status == STATUS_SUCCESS) return TRUE;
1372 SetLastError( RtlNtStatusToDosError(status) );
1376 /***********************************************************************
1377 * DisconnectNamedPipe (KERNEL32.@)
1379 * Disconnects from a named pipe
1382 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1386 * FALSE: Failure, GetLastError can be called for further details
1388 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1391 IO_STATUS_BLOCK io_block;
1393 TRACE("(%p)\n",hPipe);
1395 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1397 if (status == STATUS_SUCCESS) return TRUE;
1398 SetLastError( RtlNtStatusToDosError(status) );
1402 /***********************************************************************
1403 * TransactNamedPipe (KERNEL32.@)
1406 * should be done as a single operation in the wineserver or kernel
1408 BOOL WINAPI TransactNamedPipe(
1409 HANDLE handle, LPVOID write_buf, DWORD write_size, LPVOID read_buf,
1410 DWORD read_size, LPDWORD bytes_read, LPOVERLAPPED overlapped)
1415 TRACE("%p %p %d %p %d %p %p\n",
1416 handle, write_buf, write_size, read_buf,
1417 read_size, bytes_read, overlapped);
1421 FIXME("Doesn't support overlapped operation as yet\n");
1425 r = WriteFile(handle, write_buf, write_size, &count, NULL);
1427 r = ReadFile(handle, read_buf, read_size, bytes_read, NULL);
1432 /***********************************************************************
1433 * GetNamedPipeInfo (KERNEL32.@)
1435 BOOL WINAPI GetNamedPipeInfo(
1436 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1437 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1439 FILE_PIPE_LOCAL_INFORMATION fpli;
1440 IO_STATUS_BLOCK iosb;
1443 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1444 FilePipeLocalInformation);
1447 SetLastError( RtlNtStatusToDosError(status) );
1453 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1454 PIPE_SERVER_END : PIPE_CLIENT_END;
1455 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1456 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1459 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1460 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1461 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1466 /***********************************************************************
1467 * GetNamedPipeHandleStateA (KERNEL32.@)
1469 BOOL WINAPI GetNamedPipeHandleStateA(
1470 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1471 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1472 LPSTR lpUsername, DWORD nUsernameMaxSize)
1474 FIXME("%p %p %p %p %p %p %d\n",
1475 hNamedPipe, lpState, lpCurInstances,
1476 lpMaxCollectionCount, lpCollectDataTimeout,
1477 lpUsername, nUsernameMaxSize);
1482 /***********************************************************************
1483 * GetNamedPipeHandleStateW (KERNEL32.@)
1485 BOOL WINAPI GetNamedPipeHandleStateW(
1486 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1487 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1488 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1490 FIXME("%p %p %p %p %p %p %d\n",
1491 hNamedPipe, lpState, lpCurInstances,
1492 lpMaxCollectionCount, lpCollectDataTimeout,
1493 lpUsername, nUsernameMaxSize);
1498 /***********************************************************************
1499 * SetNamedPipeHandleState (KERNEL32.@)
1501 BOOL WINAPI SetNamedPipeHandleState(
1502 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1503 LPDWORD lpCollectDataTimeout)
1505 /* should be a fixme, but this function is called a lot by the RPC
1506 * runtime, and it slows down InstallShield a fair bit. */
1507 WARN("stub: %p %p/%d %p %p\n",
1508 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1512 /***********************************************************************
1513 * CallNamedPipeA (KERNEL32.@)
1515 BOOL WINAPI CallNamedPipeA(
1516 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1517 LPVOID lpOutput, DWORD dwOutputSize,
1518 LPDWORD lpBytesRead, DWORD nTimeout)
1524 TRACE("%s %p %d %p %d %p %d\n",
1525 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1526 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1528 if( lpNamedPipeName )
1530 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1531 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1532 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1534 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1535 dwOutputSize, lpBytesRead, nTimeout );
1536 if( lpNamedPipeName )
1537 HeapFree( GetProcessHeap(), 0, str );
1542 /***********************************************************************
1543 * CallNamedPipeW (KERNEL32.@)
1545 BOOL WINAPI CallNamedPipeW(
1546 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1547 LPVOID lpOutput, DWORD lpOutputSize,
1548 LPDWORD lpBytesRead, DWORD nTimeout)
1554 TRACE("%s %p %d %p %d %p %d\n",
1555 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1556 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1558 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1559 if (pipe == INVALID_HANDLE_VALUE)
1561 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1564 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1565 if (pipe == INVALID_HANDLE_VALUE)
1569 mode = PIPE_READMODE_MESSAGE;
1570 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1572 /* Currently SetNamedPipeHandleState() is a stub returning FALSE */
1573 if (ret) FIXME("Now that SetNamedPipeHandleState() is more than a stub, please update CallNamedPipeW\n");
1581 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1589 /******************************************************************
1590 * CreatePipe (KERNEL32.@)
1593 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1594 LPSECURITY_ATTRIBUTES sa, DWORD size )
1596 static unsigned index /* = 0 */;
1599 unsigned in_index = index;
1600 UNICODE_STRING nt_name;
1601 OBJECT_ATTRIBUTES attr;
1603 IO_STATUS_BLOCK iosb;
1604 LARGE_INTEGER timeout;
1606 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1608 attr.Length = sizeof(attr);
1609 attr.RootDirectory = 0;
1610 attr.ObjectName = &nt_name;
1611 attr.Attributes = OBJ_CASE_INSENSITIVE |
1612 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1613 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1614 attr.SecurityQualityOfService = NULL;
1616 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1617 /* generate a unique pipe name (system wide) */
1620 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1621 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1622 'u','.','%','0','8','u','\0' };
1624 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1625 GetCurrentProcessId(), ++index);
1626 RtlInitUnicodeString(&nt_name, name);
1627 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1628 0, FILE_OVERWRITE_IF,
1629 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1630 FALSE, FALSE, FALSE,
1631 1, size, size, &timeout);
1634 SetLastError( RtlNtStatusToDosError(status) );
1635 hr = INVALID_HANDLE_VALUE;
1637 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1638 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1639 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1641 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1642 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1646 SetLastError( RtlNtStatusToDosError(status) );
1657 /******************************************************************************
1658 * CreateMailslotA [KERNEL32.@]
1660 * See CreateMailslotW.
1662 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1663 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1669 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1670 nMaxMessageSize, lReadTimeout, sa);
1674 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1675 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1676 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1679 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1681 HeapFree( GetProcessHeap(), 0, name );
1687 /******************************************************************************
1688 * CreateMailslotW [KERNEL32.@]
1690 * Create a mailslot with specified name.
1693 * lpName [I] Pointer to string for mailslot name
1694 * nMaxMessageSize [I] Maximum message size
1695 * lReadTimeout [I] Milliseconds before read time-out
1696 * sa [I] Pointer to security structure
1699 * Success: Handle to mailslot
1700 * Failure: INVALID_HANDLE_VALUE
1702 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1703 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1705 HANDLE handle = INVALID_HANDLE_VALUE;
1706 OBJECT_ATTRIBUTES attr;
1707 UNICODE_STRING nameW;
1708 LARGE_INTEGER timeout;
1709 IO_STATUS_BLOCK iosb;
1712 TRACE("%s %d %d %p\n", debugstr_w(lpName),
1713 nMaxMessageSize, lReadTimeout, sa);
1715 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1717 SetLastError( ERROR_PATH_NOT_FOUND );
1718 return INVALID_HANDLE_VALUE;
1721 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1723 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1724 RtlFreeUnicodeString( &nameW );
1725 return INVALID_HANDLE_VALUE;
1728 attr.Length = sizeof(attr);
1729 attr.RootDirectory = 0;
1730 attr.Attributes = OBJ_CASE_INSENSITIVE;
1731 attr.ObjectName = &nameW;
1732 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1733 attr.SecurityQualityOfService = NULL;
1735 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1736 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1738 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1740 status = NtCreateMailslotFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr,
1741 &iosb, 0, 0, nMaxMessageSize, &timeout );
1744 SetLastError( RtlNtStatusToDosError(status) );
1745 handle = INVALID_HANDLE_VALUE;
1748 RtlFreeUnicodeString( &nameW );
1753 /******************************************************************************
1754 * GetMailslotInfo [KERNEL32.@]
1756 * Retrieve information about a mailslot.
1759 * hMailslot [I] Mailslot handle
1760 * lpMaxMessageSize [O] Address of maximum message size
1761 * lpNextSize [O] Address of size of next message
1762 * lpMessageCount [O] Address of number of messages
1763 * lpReadTimeout [O] Address of read time-out
1769 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1770 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1771 LPDWORD lpReadTimeout )
1773 FILE_MAILSLOT_QUERY_INFORMATION info;
1774 IO_STATUS_BLOCK iosb;
1777 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1778 lpNextSize, lpMessageCount, lpReadTimeout);
1780 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1781 FileMailslotQueryInformation );
1783 if( status != STATUS_SUCCESS )
1785 SetLastError( RtlNtStatusToDosError(status) );
1789 if( lpMaxMessageSize )
1790 *lpMaxMessageSize = info.MaximumMessageSize;
1792 *lpNextSize = info.NextMessageSize;
1793 if( lpMessageCount )
1794 *lpMessageCount = info.MessagesAvailable;
1797 if (info.ReadTimeout.QuadPart == (((LONGLONG)0x7fffffff << 32) | 0xffffffff))
1798 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
1800 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1806 /******************************************************************************
1807 * SetMailslotInfo [KERNEL32.@]
1809 * Set the read timeout of a mailslot.
1812 * hMailslot [I] Mailslot handle
1813 * dwReadTimeout [I] Timeout in milliseconds.
1819 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1821 FILE_MAILSLOT_SET_INFORMATION info;
1822 IO_STATUS_BLOCK iosb;
1825 TRACE("%p %d\n", hMailslot, dwReadTimeout);
1827 if (dwReadTimeout != MAILSLOT_WAIT_FOREVER)
1828 info.ReadTimeout.QuadPart = (ULONGLONG)dwReadTimeout * -10000;
1830 info.ReadTimeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1831 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1832 FileMailslotSetInformation );
1833 if( status != STATUS_SUCCESS )
1835 SetLastError( RtlNtStatusToDosError(status) );
1842 /******************************************************************************
1843 * CreateIoCompletionPort (KERNEL32.@)
1845 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1846 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1851 TRACE("(%p, %p, %08lx, %08x)\n",
1852 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1854 if (hExistingCompletionPort && hFileHandle == INVALID_HANDLE_VALUE)
1856 SetLastError( ERROR_INVALID_PARAMETER);
1860 if (hExistingCompletionPort)
1861 ret = hExistingCompletionPort;
1864 status = NtCreateIoCompletion( &ret, IO_COMPLETION_ALL_ACCESS, NULL, dwNumberOfConcurrentThreads );
1865 if (status != STATUS_SUCCESS) goto fail;
1868 if (hFileHandle != INVALID_HANDLE_VALUE)
1870 FILE_COMPLETION_INFORMATION info;
1871 IO_STATUS_BLOCK iosb;
1873 info.CompletionPort = ret;
1874 info.CompletionKey = CompletionKey;
1875 status = NtSetInformationFile( hFileHandle, &iosb, &info, sizeof(info), FileCompletionInformation );
1876 if (status != STATUS_SUCCESS) goto fail;
1882 if (ret && !hExistingCompletionPort)
1884 SetLastError( RtlNtStatusToDosError(status) );
1888 /******************************************************************************
1889 * GetQueuedCompletionStatus (KERNEL32.@)
1891 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1892 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1893 DWORD dwMilliseconds )
1896 IO_STATUS_BLOCK iosb;
1897 LARGE_INTEGER wait_time;
1899 TRACE("(%p,%p,%p,%p,%d)\n",
1900 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1902 *lpOverlapped = NULL;
1904 status = NtRemoveIoCompletion( CompletionPort, pCompletionKey, (PULONG_PTR)lpOverlapped,
1905 &iosb, get_nt_timeout( &wait_time, dwMilliseconds ) );
1906 if (status == STATUS_SUCCESS)
1908 *lpNumberOfBytesTransferred = iosb.Information;
1912 SetLastError( RtlNtStatusToDosError(status) );
1917 /******************************************************************************
1918 * PostQueuedCompletionStatus (KERNEL32.@)
1920 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
1921 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
1925 TRACE("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
1927 status = NtSetIoCompletion( CompletionPort, dwCompletionKey, (ULONG_PTR)lpOverlapped,
1928 STATUS_SUCCESS, dwNumberOfBytes );
1930 if (status == STATUS_SUCCESS) return TRUE;
1931 SetLastError( RtlNtStatusToDosError(status) );
1935 /******************************************************************************
1936 * BindIoCompletionCallback (KERNEL32.@)
1938 BOOL WINAPI BindIoCompletionCallback( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags)
1942 TRACE("(%p, %p, %d)\n", FileHandle, Function, Flags);
1944 status = RtlSetIoCompletionCallback( FileHandle, (PRTL_OVERLAPPED_COMPLETION_ROUTINE)Function, Flags );
1945 if (status == STATUS_SUCCESS) return TRUE;
1946 SetLastError( RtlNtStatusToDosError(status) );
1950 /******************************************************************************
1951 * CreateJobObjectW (KERNEL32.@)
1953 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1955 FIXME("%p %s\n", attr, debugstr_w(name) );
1956 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1960 /******************************************************************************
1961 * CreateJobObjectA (KERNEL32.@)
1963 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1969 TRACE("%p %s\n", attr, debugstr_a(name) );
1973 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1974 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1977 SetLastError( ERROR_OUTOFMEMORY );
1980 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1983 r = CreateJobObjectW( attr, str );
1985 HeapFree( GetProcessHeap(), 0, str );
1990 /******************************************************************************
1991 * AssignProcessToJobObject (KERNEL32.@)
1993 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1995 FIXME("%p %p\n", hJob, hProcess);
2001 /***********************************************************************
2002 * InterlockedCompareExchange (KERNEL32.@)
2004 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
2005 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
2006 "movl 12(%esp),%eax\n\t"
2007 "movl 8(%esp),%ecx\n\t"
2008 "movl 4(%esp),%edx\n\t"
2009 "lock; cmpxchgl %ecx,(%edx)\n\t"
2012 /***********************************************************************
2013 * InterlockedExchange (KERNEL32.@)
2015 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
2016 __ASM_GLOBAL_FUNC(InterlockedExchange,
2017 "movl 8(%esp),%eax\n\t"
2018 "movl 4(%esp),%edx\n\t"
2019 "lock; xchgl %eax,(%edx)\n\t"
2022 /***********************************************************************
2023 * InterlockedExchangeAdd (KERNEL32.@)
2025 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
2026 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
2027 "movl 8(%esp),%eax\n\t"
2028 "movl 4(%esp),%edx\n\t"
2029 "lock; xaddl %eax,(%edx)\n\t"
2032 /***********************************************************************
2033 * InterlockedIncrement (KERNEL32.@)
2035 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2036 __ASM_GLOBAL_FUNC(InterlockedIncrement,
2037 "movl 4(%esp),%edx\n\t"
2039 "lock; xaddl %eax,(%edx)\n\t"
2043 /***********************************************************************
2044 * InterlockedDecrement (KERNEL32.@)
2046 __ASM_GLOBAL_FUNC(InterlockedDecrement,
2047 "movl 4(%esp),%edx\n\t"
2049 "lock; xaddl %eax,(%edx)\n\t"
2053 #else /* __i386__ */
2055 /***********************************************************************
2056 * InterlockedCompareExchange (KERNEL32.@)
2058 * Atomically swap one value with another.
2061 * dest [I/O] The value to replace
2062 * xchq [I] The value to be swapped
2063 * compare [I] The value to compare to dest
2066 * The resulting value of dest.
2069 * dest is updated only if it is equal to compare, otherwise no swap is done.
2071 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
2073 return interlocked_cmpxchg( (int *)dest, xchg, compare );
2076 /***********************************************************************
2077 * InterlockedExchange (KERNEL32.@)
2079 * Atomically swap one value with another.
2082 * dest [I/O] The value to replace
2083 * val [I] The value to be swapped
2086 * The resulting value of dest.
2088 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
2090 return interlocked_xchg( (int *)dest, val );
2093 /***********************************************************************
2094 * InterlockedExchangeAdd (KERNEL32.@)
2096 * Atomically add one value to another.
2099 * dest [I/O] The value to add to
2100 * incr [I] The value to be added
2103 * The resulting value of dest.
2105 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
2107 return interlocked_xchg_add( (int *)dest, incr );
2110 /***********************************************************************
2111 * InterlockedIncrement (KERNEL32.@)
2113 * Atomically increment a value.
2116 * dest [I/O] The value to increment
2119 * The resulting value of dest.
2121 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
2123 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
2126 /***********************************************************************
2127 * InterlockedDecrement (KERNEL32.@)
2129 * Atomically decrement a value.
2132 * dest [I/O] The value to decrement
2135 * The resulting value of dest.
2137 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
2139 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
2142 #endif /* __i386__ */