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)
252 FIXME("%p %p %p %p %d %d\n",
253 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
257 /***********************************************************************
258 * RegisterWaitForSingleObjectEx (KERNEL32.@)
260 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
261 WAITORTIMERCALLBACK Callback, PVOID Context,
262 ULONG dwMilliseconds, ULONG dwFlags )
264 FIXME("%p %p %p %d %d\n",
265 hObject,Callback,Context,dwMilliseconds,dwFlags);
269 /***********************************************************************
270 * UnregisterWait (KERNEL32.@)
272 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
274 FIXME("%p\n",WaitHandle);
278 /***********************************************************************
279 * UnregisterWaitEx (KERNEL32.@)
281 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
283 FIXME("%p %p\n",WaitHandle, CompletionEvent);
287 /***********************************************************************
288 * SignalObjectAndWait (KERNEL32.@)
290 * Allows to atomically signal any of the synchro objects (semaphore,
291 * mutex, event) and wait on another.
293 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
294 DWORD dwMilliseconds, BOOL bAlertable )
297 LARGE_INTEGER timeout;
299 TRACE("%p %p %d %d\n", hObjectToSignal,
300 hObjectToWaitOn, dwMilliseconds, bAlertable);
302 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn, bAlertable,
303 get_nt_timeout( &timeout, dwMilliseconds ) );
306 SetLastError( RtlNtStatusToDosError(status) );
307 status = WAIT_FAILED;
312 /***********************************************************************
313 * InitializeCriticalSection (KERNEL32.@)
315 * Initialise a critical section before use.
318 * crit [O] Critical section to initialise.
321 * Nothing. If the function fails an exception is raised.
323 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
325 NTSTATUS ret = RtlInitializeCriticalSection( crit );
326 if (ret) RtlRaiseStatus( ret );
329 /***********************************************************************
330 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
332 * Initialise a critical section with a spin count.
335 * crit [O] Critical section to initialise.
336 * spincount [I] Number of times to spin upon contention.
340 * Failure: Nothing. If the function fails an exception is raised.
343 * spincount is ignored on uni-processor systems.
345 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
347 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
348 if (ret) RtlRaiseStatus( ret );
352 /***********************************************************************
353 * MakeCriticalSectionGlobal (KERNEL32.@)
355 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
357 /* let's assume that only one thread at a time will try to do this */
358 HANDLE sem = crit->LockSemaphore;
359 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
360 crit->LockSemaphore = ConvertToGlobalHandle( sem );
361 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
362 crit->DebugInfo = NULL;
366 /***********************************************************************
367 * ReinitializeCriticalSection (KERNEL32.@)
369 * Initialise an already used critical section.
372 * crit [O] Critical section to initialise.
377 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
379 if ( !crit->LockSemaphore )
380 RtlInitializeCriticalSection( crit );
384 /***********************************************************************
385 * UninitializeCriticalSection (KERNEL32.@)
387 * UnInitialise a critical section after use.
390 * crit [O] Critical section to uninitialise (destroy).
395 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
397 RtlDeleteCriticalSection( crit );
401 /***********************************************************************
402 * CreateEventA (KERNEL32.@)
404 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
405 BOOL initial_state, LPCSTR name )
407 WCHAR buffer[MAX_PATH];
409 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
411 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
413 SetLastError( ERROR_FILENAME_EXCED_RANGE );
416 return CreateEventW( sa, manual_reset, initial_state, buffer );
420 /***********************************************************************
421 * CreateEventW (KERNEL32.@)
423 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
424 BOOL initial_state, LPCWSTR name )
427 UNICODE_STRING nameW;
428 OBJECT_ATTRIBUTES attr;
431 /* one buggy program needs this
432 * ("Van Dale Groot woordenboek der Nederlandse taal")
434 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
436 ERR("Bad security attributes pointer %p\n",sa);
437 SetLastError( ERROR_INVALID_PARAMETER);
441 attr.Length = sizeof(attr);
442 attr.RootDirectory = 0;
443 attr.ObjectName = NULL;
444 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
445 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
446 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
447 attr.SecurityQualityOfService = NULL;
450 RtlInitUnicodeString( &nameW, name );
451 attr.ObjectName = &nameW;
452 attr.RootDirectory = get_BaseNamedObjects_handle();
455 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
456 if (status == STATUS_OBJECT_NAME_EXISTS)
457 SetLastError( ERROR_ALREADY_EXISTS );
459 SetLastError( RtlNtStatusToDosError(status) );
464 /***********************************************************************
465 * CreateW32Event (KERNEL.457)
467 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
469 return CreateEventW( NULL, manual_reset, initial_state, NULL );
473 /***********************************************************************
474 * OpenEventA (KERNEL32.@)
476 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
478 WCHAR buffer[MAX_PATH];
480 if (!name) return OpenEventW( access, inherit, NULL );
482 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
484 SetLastError( ERROR_FILENAME_EXCED_RANGE );
487 return OpenEventW( access, inherit, buffer );
491 /***********************************************************************
492 * OpenEventW (KERNEL32.@)
494 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
497 UNICODE_STRING nameW;
498 OBJECT_ATTRIBUTES attr;
501 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
503 attr.Length = sizeof(attr);
504 attr.RootDirectory = 0;
505 attr.ObjectName = NULL;
506 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
507 attr.SecurityDescriptor = NULL;
508 attr.SecurityQualityOfService = NULL;
511 RtlInitUnicodeString( &nameW, name );
512 attr.ObjectName = &nameW;
513 attr.RootDirectory = get_BaseNamedObjects_handle();
516 status = NtOpenEvent( &ret, access, &attr );
517 if (status != STATUS_SUCCESS)
519 SetLastError( RtlNtStatusToDosError(status) );
525 /***********************************************************************
526 * PulseEvent (KERNEL32.@)
528 BOOL WINAPI PulseEvent( HANDLE handle )
532 if ((status = NtPulseEvent( handle, NULL )))
533 SetLastError( RtlNtStatusToDosError(status) );
538 /***********************************************************************
539 * SetW32Event (KERNEL.458)
540 * SetEvent (KERNEL32.@)
542 BOOL WINAPI SetEvent( HANDLE handle )
546 if ((status = NtSetEvent( handle, NULL )))
547 SetLastError( RtlNtStatusToDosError(status) );
552 /***********************************************************************
553 * ResetW32Event (KERNEL.459)
554 * ResetEvent (KERNEL32.@)
556 BOOL WINAPI ResetEvent( HANDLE handle )
560 if ((status = NtResetEvent( handle, NULL )))
561 SetLastError( RtlNtStatusToDosError(status) );
566 /***********************************************************************
567 * NOTE: The Win95 VWin32_Event routines given below are really low-level
568 * routines implemented directly by VWin32. The user-mode libraries
569 * implement Win32 synchronisation routines on top of these low-level
570 * primitives. We do it the other way around here :-)
573 /***********************************************************************
574 * VWin32_EventCreate (KERNEL.442)
576 HANDLE WINAPI VWin32_EventCreate(VOID)
578 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
579 return ConvertToGlobalHandle( hEvent );
582 /***********************************************************************
583 * VWin32_EventDestroy (KERNEL.443)
585 VOID WINAPI VWin32_EventDestroy(HANDLE event)
587 CloseHandle( event );
590 /***********************************************************************
591 * VWin32_EventWait (KERNEL.450)
593 VOID WINAPI VWin32_EventWait(HANDLE event)
597 ReleaseThunkLock( &mutex_count );
598 WaitForSingleObject( event, INFINITE );
599 RestoreThunkLock( mutex_count );
602 /***********************************************************************
603 * VWin32_EventSet (KERNEL.451)
604 * KERNEL_479 (KERNEL.479)
606 VOID WINAPI VWin32_EventSet(HANDLE event)
613 /***********************************************************************
614 * CreateMutexA (KERNEL32.@)
616 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
618 WCHAR buffer[MAX_PATH];
620 if (!name) return CreateMutexW( sa, owner, NULL );
622 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
624 SetLastError( ERROR_FILENAME_EXCED_RANGE );
627 return CreateMutexW( sa, owner, buffer );
631 /***********************************************************************
632 * CreateMutexW (KERNEL32.@)
634 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
637 UNICODE_STRING nameW;
638 OBJECT_ATTRIBUTES attr;
641 attr.Length = sizeof(attr);
642 attr.RootDirectory = 0;
643 attr.ObjectName = NULL;
644 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
645 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
646 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
647 attr.SecurityQualityOfService = NULL;
650 RtlInitUnicodeString( &nameW, name );
651 attr.ObjectName = &nameW;
652 attr.RootDirectory = get_BaseNamedObjects_handle();
655 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
656 if (status == STATUS_OBJECT_NAME_EXISTS)
657 SetLastError( ERROR_ALREADY_EXISTS );
659 SetLastError( RtlNtStatusToDosError(status) );
664 /***********************************************************************
665 * OpenMutexA (KERNEL32.@)
667 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
669 WCHAR buffer[MAX_PATH];
671 if (!name) return OpenMutexW( access, inherit, NULL );
673 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
675 SetLastError( ERROR_FILENAME_EXCED_RANGE );
678 return OpenMutexW( access, inherit, buffer );
682 /***********************************************************************
683 * OpenMutexW (KERNEL32.@)
685 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
688 UNICODE_STRING nameW;
689 OBJECT_ATTRIBUTES attr;
692 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
694 attr.Length = sizeof(attr);
695 attr.RootDirectory = 0;
696 attr.ObjectName = NULL;
697 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
698 attr.SecurityDescriptor = NULL;
699 attr.SecurityQualityOfService = NULL;
702 RtlInitUnicodeString( &nameW, name );
703 attr.ObjectName = &nameW;
704 attr.RootDirectory = get_BaseNamedObjects_handle();
707 status = NtOpenMutant( &ret, access, &attr );
708 if (status != STATUS_SUCCESS)
710 SetLastError( RtlNtStatusToDosError(status) );
717 /***********************************************************************
718 * ReleaseMutex (KERNEL32.@)
720 BOOL WINAPI ReleaseMutex( HANDLE handle )
724 status = NtReleaseMutant(handle, NULL);
725 if (status != STATUS_SUCCESS)
727 SetLastError( RtlNtStatusToDosError(status) );
739 /***********************************************************************
740 * CreateSemaphoreA (KERNEL32.@)
742 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
744 WCHAR buffer[MAX_PATH];
746 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
748 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
750 SetLastError( ERROR_FILENAME_EXCED_RANGE );
753 return CreateSemaphoreW( sa, initial, max, buffer );
757 /***********************************************************************
758 * CreateSemaphoreW (KERNEL32.@)
760 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
761 LONG max, LPCWSTR name )
764 UNICODE_STRING nameW;
765 OBJECT_ATTRIBUTES attr;
768 attr.Length = sizeof(attr);
769 attr.RootDirectory = 0;
770 attr.ObjectName = NULL;
771 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
772 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
773 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
774 attr.SecurityQualityOfService = NULL;
777 RtlInitUnicodeString( &nameW, name );
778 attr.ObjectName = &nameW;
779 attr.RootDirectory = get_BaseNamedObjects_handle();
782 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
783 if (status == STATUS_OBJECT_NAME_EXISTS)
784 SetLastError( ERROR_ALREADY_EXISTS );
786 SetLastError( RtlNtStatusToDosError(status) );
791 /***********************************************************************
792 * OpenSemaphoreA (KERNEL32.@)
794 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
796 WCHAR buffer[MAX_PATH];
798 if (!name) return OpenSemaphoreW( access, inherit, NULL );
800 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
802 SetLastError( ERROR_FILENAME_EXCED_RANGE );
805 return OpenSemaphoreW( access, inherit, buffer );
809 /***********************************************************************
810 * OpenSemaphoreW (KERNEL32.@)
812 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
815 UNICODE_STRING nameW;
816 OBJECT_ATTRIBUTES attr;
819 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
821 attr.Length = sizeof(attr);
822 attr.RootDirectory = 0;
823 attr.ObjectName = NULL;
824 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
825 attr.SecurityDescriptor = NULL;
826 attr.SecurityQualityOfService = NULL;
829 RtlInitUnicodeString( &nameW, name );
830 attr.ObjectName = &nameW;
831 attr.RootDirectory = get_BaseNamedObjects_handle();
834 status = NtOpenSemaphore( &ret, access, &attr );
835 if (status != STATUS_SUCCESS)
837 SetLastError( RtlNtStatusToDosError(status) );
844 /***********************************************************************
845 * ReleaseSemaphore (KERNEL32.@)
847 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
849 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
850 if (status) SetLastError( RtlNtStatusToDosError(status) );
860 /***********************************************************************
861 * CreateWaitableTimerA (KERNEL32.@)
863 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
865 WCHAR buffer[MAX_PATH];
867 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
869 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
871 SetLastError( ERROR_FILENAME_EXCED_RANGE );
874 return CreateWaitableTimerW( sa, manual, buffer );
878 /***********************************************************************
879 * CreateWaitableTimerW (KERNEL32.@)
881 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
885 UNICODE_STRING nameW;
886 OBJECT_ATTRIBUTES attr;
888 attr.Length = sizeof(attr);
889 attr.RootDirectory = 0;
890 attr.ObjectName = NULL;
891 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
892 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
893 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
894 attr.SecurityQualityOfService = NULL;
897 RtlInitUnicodeString( &nameW, name );
898 attr.ObjectName = &nameW;
899 attr.RootDirectory = get_BaseNamedObjects_handle();
902 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
903 manual ? NotificationTimer : SynchronizationTimer);
904 if (status == STATUS_OBJECT_NAME_EXISTS)
905 SetLastError( ERROR_ALREADY_EXISTS );
907 SetLastError( RtlNtStatusToDosError(status) );
912 /***********************************************************************
913 * OpenWaitableTimerA (KERNEL32.@)
915 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
917 WCHAR buffer[MAX_PATH];
919 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
921 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
923 SetLastError( ERROR_FILENAME_EXCED_RANGE );
926 return OpenWaitableTimerW( access, inherit, buffer );
930 /***********************************************************************
931 * OpenWaitableTimerW (KERNEL32.@)
933 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
936 UNICODE_STRING nameW;
937 OBJECT_ATTRIBUTES attr;
940 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
942 attr.Length = sizeof(attr);
943 attr.RootDirectory = 0;
944 attr.ObjectName = NULL;
945 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
946 attr.SecurityDescriptor = NULL;
947 attr.SecurityQualityOfService = NULL;
950 RtlInitUnicodeString( &nameW, name );
951 attr.ObjectName = &nameW;
952 attr.RootDirectory = get_BaseNamedObjects_handle();
955 status = NtOpenTimer(&handle, access, &attr);
956 if (status != STATUS_SUCCESS)
958 SetLastError( RtlNtStatusToDosError(status) );
965 /***********************************************************************
966 * SetWaitableTimer (KERNEL32.@)
968 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
969 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
971 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
972 arg, resume, period, NULL);
974 if (status != STATUS_SUCCESS)
976 SetLastError( RtlNtStatusToDosError(status) );
977 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
983 /***********************************************************************
984 * CancelWaitableTimer (KERNEL32.@)
986 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
990 status = NtCancelTimer(handle, NULL);
991 if (status != STATUS_SUCCESS)
993 SetLastError( RtlNtStatusToDosError(status) );
1000 /***********************************************************************
1001 * CreateTimerQueue (KERNEL32.@)
1003 HANDLE WINAPI CreateTimerQueue(void)
1006 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1011 /***********************************************************************
1012 * DeleteTimerQueueEx (KERNEL32.@)
1014 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1016 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
1017 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1021 /***********************************************************************
1022 * CreateTimerQueueTimer (KERNEL32.@)
1024 * Creates a timer-queue timer. This timer expires at the specified due
1025 * time (in ms), then after every specified period (in ms). When the timer
1026 * expires, the callback function is called.
1029 * nonzero on success or zero on faillure
1034 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1035 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1036 DWORD DueTime, DWORD Period, ULONG Flags )
1039 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1043 /***********************************************************************
1044 * DeleteTimerQueueTimer (KERNEL32.@)
1046 * Cancels a timer-queue timer.
1049 * nonzero on success or zero on faillure
1054 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1055 HANDLE CompletionEvent )
1058 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1068 /***********************************************************************
1069 * CreateNamedPipeA (KERNEL32.@)
1071 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1072 DWORD dwPipeMode, DWORD nMaxInstances,
1073 DWORD nOutBufferSize, DWORD nInBufferSize,
1074 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1076 WCHAR buffer[MAX_PATH];
1078 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1079 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1081 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1083 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1084 return INVALID_HANDLE_VALUE;
1086 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1087 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1091 /***********************************************************************
1092 * CreateNamedPipeW (KERNEL32.@)
1094 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1095 DWORD dwPipeMode, DWORD nMaxInstances,
1096 DWORD nOutBufferSize, DWORD nInBufferSize,
1097 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1100 UNICODE_STRING nt_name;
1101 OBJECT_ATTRIBUTES attr;
1102 DWORD access, options;
1103 BOOLEAN pipe_type, read_mode, non_block;
1105 IO_STATUS_BLOCK iosb;
1106 LARGE_INTEGER timeout;
1108 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1109 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1110 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1112 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1114 SetLastError( ERROR_PATH_NOT_FOUND );
1115 return INVALID_HANDLE_VALUE;
1117 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1119 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1120 RtlFreeUnicodeString( &nt_name );
1121 return INVALID_HANDLE_VALUE;
1124 attr.Length = sizeof(attr);
1125 attr.RootDirectory = 0;
1126 attr.ObjectName = &nt_name;
1127 attr.Attributes = OBJ_CASE_INSENSITIVE |
1128 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1129 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1130 attr.SecurityQualityOfService = NULL;
1132 switch(dwOpenMode & 3)
1134 case PIPE_ACCESS_INBOUND:
1135 options = FILE_PIPE_INBOUND;
1136 access = GENERIC_READ;
1138 case PIPE_ACCESS_OUTBOUND:
1139 options = FILE_PIPE_OUTBOUND;
1140 access = GENERIC_WRITE;
1142 case PIPE_ACCESS_DUPLEX:
1143 options = FILE_PIPE_FULL_DUPLEX;
1144 access = GENERIC_READ | GENERIC_WRITE;
1147 SetLastError( ERROR_INVALID_PARAMETER );
1148 return INVALID_HANDLE_VALUE;
1150 access |= SYNCHRONIZE;
1151 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1152 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1153 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1154 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1155 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1156 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0U;
1158 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1162 status = NtCreateNamedPipeFile(&handle, access, &attr, &iosb, 0,
1163 FILE_OVERWRITE_IF, options, pipe_type,
1164 read_mode, non_block, nMaxInstances,
1165 nInBufferSize, nOutBufferSize, &timeout);
1167 RtlFreeUnicodeString( &nt_name );
1170 handle = INVALID_HANDLE_VALUE;
1171 SetLastError( RtlNtStatusToDosError(status) );
1177 /***********************************************************************
1178 * PeekNamedPipe (KERNEL32.@)
1180 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1181 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1183 FILE_PIPE_PEEK_BUFFER local_buffer;
1184 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1188 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1189 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1191 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1195 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1196 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1199 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1200 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1201 if (lpcbRead) *lpcbRead = read_size;
1202 if (lpcbMessage) *lpcbMessage = 0; /* FIXME */
1203 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1205 else SetLastError( RtlNtStatusToDosError(status) );
1207 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1211 /***********************************************************************
1212 * WaitNamedPipeA (KERNEL32.@)
1214 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1216 WCHAR buffer[MAX_PATH];
1218 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1220 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1222 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1225 return WaitNamedPipeW( buffer, nTimeOut );
1229 /***********************************************************************
1230 * WaitNamedPipeW (KERNEL32.@)
1232 * Waits for a named pipe instance to become available
1235 * name [I] Pointer to a named pipe name to wait for
1236 * nTimeOut [I] How long to wait in ms
1239 * TRUE: Success, named pipe can be opened with CreateFile
1240 * FALSE: Failure, GetLastError can be called for further details
1242 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1244 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1246 UNICODE_STRING nt_name, pipe_dev_name;
1247 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1248 IO_STATUS_BLOCK iosb;
1249 OBJECT_ATTRIBUTES attr;
1253 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1255 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1258 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1259 nt_name.Length < sizeof(leadin) ||
1260 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR) != 0))
1262 RtlFreeUnicodeString( &nt_name );
1263 SetLastError( ERROR_PATH_NOT_FOUND );
1267 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1268 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1270 RtlFreeUnicodeString( &nt_name );
1271 SetLastError( ERROR_OUTOFMEMORY );
1275 pipe_dev_name.Buffer = nt_name.Buffer;
1276 pipe_dev_name.Length = sizeof(leadin);
1277 pipe_dev_name.MaximumLength = sizeof(leadin);
1278 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1279 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1280 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1281 FILE_SYNCHRONOUS_IO_NONALERT);
1282 if (status != ERROR_SUCCESS)
1284 SetLastError( ERROR_PATH_NOT_FOUND );
1288 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1289 if (nTimeOut == NMPWAIT_WAIT_FOREVER)
1290 pipe_wait->Timeout.QuadPart = ((ULONGLONG)0x7fffffff << 32) | 0xffffffff;
1292 pipe_wait->Timeout.QuadPart = (ULONGLONG)nTimeOut * -10000;
1293 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1294 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1295 pipe_wait->NameLength);
1296 RtlFreeUnicodeString( &nt_name );
1298 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1299 pipe_wait, sz_pipe_wait, NULL, 0 );
1301 HeapFree( GetProcessHeap(), 0, pipe_wait );
1302 NtClose( pipe_dev );
1304 if(status != STATUS_SUCCESS)
1306 SetLastError(RtlNtStatusToDosError(status));
1314 /***********************************************************************
1315 * ConnectNamedPipe (KERNEL32.@)
1317 * Connects to a named pipe
1320 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1321 * overlapped: Optional OVERLAPPED struct
1325 * FALSE: Failure, GetLastError can be called for further details
1327 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1330 IO_STATUS_BLOCK status_block;
1331 LPVOID cvalue = NULL;
1333 TRACE("(%p,%p)\n", hPipe, overlapped);
1337 overlapped->Internal = STATUS_PENDING;
1338 overlapped->InternalHigh = 0;
1339 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1342 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, cvalue,
1343 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1344 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1346 if (status == STATUS_SUCCESS) return TRUE;
1347 SetLastError( RtlNtStatusToDosError(status) );
1351 /***********************************************************************
1352 * DisconnectNamedPipe (KERNEL32.@)
1354 * Disconnects from a named pipe
1357 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1361 * FALSE: Failure, GetLastError can be called for further details
1363 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1366 IO_STATUS_BLOCK io_block;
1368 TRACE("(%p)\n",hPipe);
1370 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1372 if (status == STATUS_SUCCESS) return TRUE;
1373 SetLastError( RtlNtStatusToDosError(status) );
1377 /***********************************************************************
1378 * TransactNamedPipe (KERNEL32.@)
1381 * should be done as a single operation in the wineserver or kernel
1383 BOOL WINAPI TransactNamedPipe(
1384 HANDLE handle, LPVOID write_buf, DWORD write_size, LPVOID read_buf,
1385 DWORD read_size, LPDWORD bytes_read, LPOVERLAPPED overlapped)
1390 TRACE("%p %p %d %p %d %p %p\n",
1391 handle, write_buf, write_size, read_buf,
1392 read_size, bytes_read, overlapped);
1396 FIXME("Doesn't support overlapped operation as yet\n");
1400 r = WriteFile(handle, write_buf, write_size, &count, NULL);
1402 r = ReadFile(handle, read_buf, read_size, bytes_read, NULL);
1407 /***********************************************************************
1408 * GetNamedPipeInfo (KERNEL32.@)
1410 BOOL WINAPI GetNamedPipeInfo(
1411 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1412 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1414 FILE_PIPE_LOCAL_INFORMATION fpli;
1415 IO_STATUS_BLOCK iosb;
1418 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1419 FilePipeLocalInformation);
1422 SetLastError( RtlNtStatusToDosError(status) );
1428 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1429 PIPE_SERVER_END : PIPE_CLIENT_END;
1430 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1431 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1434 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1435 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1436 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1441 /***********************************************************************
1442 * GetNamedPipeHandleStateA (KERNEL32.@)
1444 BOOL WINAPI GetNamedPipeHandleStateA(
1445 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1446 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1447 LPSTR lpUsername, DWORD nUsernameMaxSize)
1449 FIXME("%p %p %p %p %p %p %d\n",
1450 hNamedPipe, lpState, lpCurInstances,
1451 lpMaxCollectionCount, lpCollectDataTimeout,
1452 lpUsername, nUsernameMaxSize);
1457 /***********************************************************************
1458 * GetNamedPipeHandleStateW (KERNEL32.@)
1460 BOOL WINAPI GetNamedPipeHandleStateW(
1461 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1462 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1463 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1465 FIXME("%p %p %p %p %p %p %d\n",
1466 hNamedPipe, lpState, lpCurInstances,
1467 lpMaxCollectionCount, lpCollectDataTimeout,
1468 lpUsername, nUsernameMaxSize);
1473 /***********************************************************************
1474 * SetNamedPipeHandleState (KERNEL32.@)
1476 BOOL WINAPI SetNamedPipeHandleState(
1477 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1478 LPDWORD lpCollectDataTimeout)
1480 /* should be a fixme, but this function is called a lot by the RPC
1481 * runtime, and it slows down InstallShield a fair bit. */
1482 WARN("stub: %p %p/%d %p %p\n",
1483 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1487 /***********************************************************************
1488 * CallNamedPipeA (KERNEL32.@)
1490 BOOL WINAPI CallNamedPipeA(
1491 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1492 LPVOID lpOutput, DWORD dwOutputSize,
1493 LPDWORD lpBytesRead, DWORD nTimeout)
1499 TRACE("%s %p %d %p %d %p %d\n",
1500 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1501 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1503 if( lpNamedPipeName )
1505 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1506 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1507 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1509 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1510 dwOutputSize, lpBytesRead, nTimeout );
1511 if( lpNamedPipeName )
1512 HeapFree( GetProcessHeap(), 0, str );
1517 /***********************************************************************
1518 * CallNamedPipeW (KERNEL32.@)
1520 BOOL WINAPI CallNamedPipeW(
1521 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1522 LPVOID lpOutput, DWORD lpOutputSize,
1523 LPDWORD lpBytesRead, DWORD nTimeout)
1529 TRACE("%s %p %d %p %d %p %d\n",
1530 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1531 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1533 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1534 if (pipe == INVALID_HANDLE_VALUE)
1536 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1539 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1540 if (pipe == INVALID_HANDLE_VALUE)
1544 mode = PIPE_READMODE_MESSAGE;
1545 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1547 /* Currently SetNamedPipeHandleState() is a stub returning FALSE */
1548 if (ret) FIXME("Now that SetNamedPipeHandleState() is more than a stub, please update CallNamedPipeW\n");
1556 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1564 /******************************************************************
1565 * CreatePipe (KERNEL32.@)
1568 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1569 LPSECURITY_ATTRIBUTES sa, DWORD size )
1571 static unsigned index /* = 0 */;
1574 unsigned in_index = index;
1575 UNICODE_STRING nt_name;
1576 OBJECT_ATTRIBUTES attr;
1578 IO_STATUS_BLOCK iosb;
1579 LARGE_INTEGER timeout;
1581 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1583 attr.Length = sizeof(attr);
1584 attr.RootDirectory = 0;
1585 attr.ObjectName = &nt_name;
1586 attr.Attributes = OBJ_CASE_INSENSITIVE |
1587 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1588 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1589 attr.SecurityQualityOfService = NULL;
1591 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1592 /* generate a unique pipe name (system wide) */
1595 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1596 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1597 'u','.','%','0','8','u','\0' };
1599 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1600 GetCurrentProcessId(), ++index);
1601 RtlInitUnicodeString(&nt_name, name);
1602 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1603 0, FILE_OVERWRITE_IF,
1604 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1605 FALSE, FALSE, FALSE,
1606 1, size, size, &timeout);
1609 SetLastError( RtlNtStatusToDosError(status) );
1610 hr = INVALID_HANDLE_VALUE;
1612 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1613 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1614 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1616 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1617 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1621 SetLastError( RtlNtStatusToDosError(status) );
1632 /******************************************************************************
1633 * CreateMailslotA [KERNEL32.@]
1635 * See CreateMailslotW.
1637 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1638 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1644 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1645 nMaxMessageSize, lReadTimeout, sa);
1649 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1650 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1651 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1654 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1656 HeapFree( GetProcessHeap(), 0, name );
1662 /******************************************************************************
1663 * CreateMailslotW [KERNEL32.@]
1665 * Create a mailslot with specified name.
1668 * lpName [I] Pointer to string for mailslot name
1669 * nMaxMessageSize [I] Maximum message size
1670 * lReadTimeout [I] Milliseconds before read time-out
1671 * sa [I] Pointer to security structure
1674 * Success: Handle to mailslot
1675 * Failure: INVALID_HANDLE_VALUE
1677 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1678 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1680 HANDLE handle = INVALID_HANDLE_VALUE;
1681 OBJECT_ATTRIBUTES attr;
1682 UNICODE_STRING nameW;
1683 LARGE_INTEGER timeout;
1684 IO_STATUS_BLOCK iosb;
1687 TRACE("%s %d %d %p\n", debugstr_w(lpName),
1688 nMaxMessageSize, lReadTimeout, sa);
1690 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1692 SetLastError( ERROR_PATH_NOT_FOUND );
1693 return INVALID_HANDLE_VALUE;
1696 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1698 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1699 RtlFreeUnicodeString( &nameW );
1700 return INVALID_HANDLE_VALUE;
1703 attr.Length = sizeof(attr);
1704 attr.RootDirectory = 0;
1705 attr.Attributes = OBJ_CASE_INSENSITIVE;
1706 attr.ObjectName = &nameW;
1707 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1708 attr.SecurityQualityOfService = NULL;
1710 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1711 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1713 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1715 status = NtCreateMailslotFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr,
1716 &iosb, 0, 0, nMaxMessageSize, &timeout );
1719 SetLastError( RtlNtStatusToDosError(status) );
1720 handle = INVALID_HANDLE_VALUE;
1723 RtlFreeUnicodeString( &nameW );
1728 /******************************************************************************
1729 * GetMailslotInfo [KERNEL32.@]
1731 * Retrieve information about a mailslot.
1734 * hMailslot [I] Mailslot handle
1735 * lpMaxMessageSize [O] Address of maximum message size
1736 * lpNextSize [O] Address of size of next message
1737 * lpMessageCount [O] Address of number of messages
1738 * lpReadTimeout [O] Address of read time-out
1744 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1745 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1746 LPDWORD lpReadTimeout )
1748 FILE_MAILSLOT_QUERY_INFORMATION info;
1749 IO_STATUS_BLOCK iosb;
1752 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1753 lpNextSize, lpMessageCount, lpReadTimeout);
1755 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1756 FileMailslotQueryInformation );
1758 if( status != STATUS_SUCCESS )
1760 SetLastError( RtlNtStatusToDosError(status) );
1764 if( lpMaxMessageSize )
1765 *lpMaxMessageSize = info.MaximumMessageSize;
1767 *lpNextSize = info.NextMessageSize;
1768 if( lpMessageCount )
1769 *lpMessageCount = info.MessagesAvailable;
1772 if (info.ReadTimeout.QuadPart == (((LONGLONG)0x7fffffff << 32) | 0xffffffff))
1773 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
1775 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1781 /******************************************************************************
1782 * SetMailslotInfo [KERNEL32.@]
1784 * Set the read timeout of a mailslot.
1787 * hMailslot [I] Mailslot handle
1788 * dwReadTimeout [I] Timeout in milliseconds.
1794 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1796 FILE_MAILSLOT_SET_INFORMATION info;
1797 IO_STATUS_BLOCK iosb;
1800 TRACE("%p %d\n", hMailslot, dwReadTimeout);
1802 if (dwReadTimeout != MAILSLOT_WAIT_FOREVER)
1803 info.ReadTimeout.QuadPart = (ULONGLONG)dwReadTimeout * -10000;
1805 info.ReadTimeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1806 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1807 FileMailslotSetInformation );
1808 if( status != STATUS_SUCCESS )
1810 SetLastError( RtlNtStatusToDosError(status) );
1817 /******************************************************************************
1818 * CreateIoCompletionPort (KERNEL32.@)
1820 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1821 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1826 TRACE("(%p, %p, %08lx, %08x)\n",
1827 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1829 if (hExistingCompletionPort && hFileHandle == INVALID_HANDLE_VALUE)
1831 SetLastError( ERROR_INVALID_PARAMETER);
1835 if (hExistingCompletionPort)
1836 ret = hExistingCompletionPort;
1839 status = NtCreateIoCompletion( &ret, IO_COMPLETION_ALL_ACCESS, NULL, dwNumberOfConcurrentThreads );
1840 if (status != STATUS_SUCCESS) goto fail;
1843 if (hFileHandle != INVALID_HANDLE_VALUE)
1845 FILE_COMPLETION_INFORMATION info;
1846 IO_STATUS_BLOCK iosb;
1848 info.CompletionPort = ret;
1849 info.CompletionKey = CompletionKey;
1850 status = NtSetInformationFile( hFileHandle, &iosb, &info, sizeof(info), FileCompletionInformation );
1851 if (status != STATUS_SUCCESS) goto fail;
1857 if (ret && !hExistingCompletionPort)
1859 SetLastError( RtlNtStatusToDosError(status) );
1863 /******************************************************************************
1864 * GetQueuedCompletionStatus (KERNEL32.@)
1866 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1867 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1868 DWORD dwMilliseconds )
1871 IO_STATUS_BLOCK iosb;
1872 LARGE_INTEGER wait_time;
1874 TRACE("(%p,%p,%p,%p,%d)\n",
1875 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1877 *lpOverlapped = NULL;
1879 status = NtRemoveIoCompletion( CompletionPort, pCompletionKey, (PULONG_PTR)lpOverlapped,
1880 &iosb, get_nt_timeout( &wait_time, dwMilliseconds ) );
1881 if (status == STATUS_SUCCESS)
1883 *lpNumberOfBytesTransferred = iosb.Information;
1887 SetLastError( RtlNtStatusToDosError(status) );
1892 /******************************************************************************
1893 * PostQueuedCompletionStatus (KERNEL32.@)
1895 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
1896 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
1900 TRACE("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
1902 status = NtSetIoCompletion( CompletionPort, dwCompletionKey, (ULONG_PTR)lpOverlapped,
1903 STATUS_SUCCESS, dwNumberOfBytes );
1905 if (status == STATUS_SUCCESS) return TRUE;
1906 SetLastError( RtlNtStatusToDosError(status) );
1910 /******************************************************************************
1911 * BindIoCompletionCallback (KERNEL32.@)
1913 BOOL WINAPI BindIoCompletionCallback( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags)
1917 TRACE("(%p, %p, %d)\n", FileHandle, Function, Flags);
1919 status = RtlSetIoCompletionCallback( FileHandle, (PRTL_OVERLAPPED_COMPLETION_ROUTINE)Function, Flags );
1920 if (status == STATUS_SUCCESS) return TRUE;
1921 SetLastError( RtlNtStatusToDosError(status) );
1925 /******************************************************************************
1926 * CreateJobObjectW (KERNEL32.@)
1928 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1930 FIXME("%p %s\n", attr, debugstr_w(name) );
1931 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1935 /******************************************************************************
1936 * CreateJobObjectA (KERNEL32.@)
1938 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1944 TRACE("%p %s\n", attr, debugstr_a(name) );
1948 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1949 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1952 SetLastError( ERROR_OUTOFMEMORY );
1955 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1958 r = CreateJobObjectW( attr, str );
1960 HeapFree( GetProcessHeap(), 0, str );
1965 /******************************************************************************
1966 * AssignProcessToJobObject (KERNEL32.@)
1968 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1970 FIXME("%p %p\n", hJob, hProcess);
1976 /***********************************************************************
1977 * InterlockedCompareExchange (KERNEL32.@)
1979 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1980 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1981 "movl 12(%esp),%eax\n\t"
1982 "movl 8(%esp),%ecx\n\t"
1983 "movl 4(%esp),%edx\n\t"
1984 "lock; cmpxchgl %ecx,(%edx)\n\t"
1987 /***********************************************************************
1988 * InterlockedExchange (KERNEL32.@)
1990 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1991 __ASM_GLOBAL_FUNC(InterlockedExchange,
1992 "movl 8(%esp),%eax\n\t"
1993 "movl 4(%esp),%edx\n\t"
1994 "lock; xchgl %eax,(%edx)\n\t"
1997 /***********************************************************************
1998 * InterlockedExchangeAdd (KERNEL32.@)
2000 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
2001 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
2002 "movl 8(%esp),%eax\n\t"
2003 "movl 4(%esp),%edx\n\t"
2004 "lock; xaddl %eax,(%edx)\n\t"
2007 /***********************************************************************
2008 * InterlockedIncrement (KERNEL32.@)
2010 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2011 __ASM_GLOBAL_FUNC(InterlockedIncrement,
2012 "movl 4(%esp),%edx\n\t"
2014 "lock; xaddl %eax,(%edx)\n\t"
2018 /***********************************************************************
2019 * InterlockedDecrement (KERNEL32.@)
2021 __ASM_GLOBAL_FUNC(InterlockedDecrement,
2022 "movl 4(%esp),%edx\n\t"
2024 "lock; xaddl %eax,(%edx)\n\t"
2028 #else /* __i386__ */
2030 /***********************************************************************
2031 * InterlockedCompareExchange (KERNEL32.@)
2033 * Atomically swap one value with another.
2036 * dest [I/O] The value to replace
2037 * xchq [I] The value to be swapped
2038 * compare [I] The value to compare to dest
2041 * The resulting value of dest.
2044 * dest is updated only if it is equal to compare, otherwise no swap is done.
2046 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
2048 return interlocked_cmpxchg( (int *)dest, xchg, compare );
2051 /***********************************************************************
2052 * InterlockedExchange (KERNEL32.@)
2054 * Atomically swap one value with another.
2057 * dest [I/O] The value to replace
2058 * val [I] The value to be swapped
2061 * The resulting value of dest.
2063 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
2065 return interlocked_xchg( (int *)dest, val );
2068 /***********************************************************************
2069 * InterlockedExchangeAdd (KERNEL32.@)
2071 * Atomically add one value to another.
2074 * dest [I/O] The value to add to
2075 * incr [I] The value to be added
2078 * The resulting value of dest.
2080 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
2082 return interlocked_xchg_add( (int *)dest, incr );
2085 /***********************************************************************
2086 * InterlockedIncrement (KERNEL32.@)
2088 * Atomically increment a value.
2091 * dest [I/O] The value to increment
2094 * The resulting value of dest.
2096 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
2098 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
2101 /***********************************************************************
2102 * InterlockedDecrement (KERNEL32.@)
2104 * Atomically decrement a value.
2107 * dest [I/O] The value to decrement
2110 * The resulting value of dest.
2112 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
2114 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
2117 #endif /* __i386__ */