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 "kernel_private.h"
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(sync);
52 /* check if current version is NT or Win95 */
53 static inline int is_version_nt(void)
55 return !(GetVersion() & 0x80000000);
58 /* returns directory handle to \\BaseNamedObjects */
59 HANDLE get_BaseNamedObjects_handle(void)
61 static HANDLE handle = NULL;
62 static const WCHAR basenameW[] =
63 {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
65 OBJECT_ATTRIBUTES attr;
71 RtlInitUnicodeString(&str, basenameW);
72 InitializeObjectAttributes(&attr, &str, 0, 0, NULL);
73 NtOpenDirectoryObject(&dir, DIRECTORY_CREATE_OBJECT|DIRECTORY_TRAVERSE,
75 if (InterlockedCompareExchangePointer( &handle, dir, 0 ) != 0)
77 /* someone beat us here... */
84 /* helper for kernel32->ntdll timeout format conversion */
85 static inline PLARGE_INTEGER get_nt_timeout( PLARGE_INTEGER pTime, DWORD timeout )
87 if (timeout == INFINITE) return NULL;
88 pTime->QuadPart = (ULONGLONG)timeout * -10000;
92 /***********************************************************************
95 VOID WINAPI Sleep( DWORD timeout )
97 SleepEx( timeout, FALSE );
100 /******************************************************************************
101 * SleepEx (KERNEL32.@)
103 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
108 status = NtDelayExecution( alertable, get_nt_timeout( &time, timeout ) );
109 if (status == STATUS_USER_APC) return WAIT_IO_COMPLETION;
114 /***********************************************************************
115 * SwitchToThread (KERNEL32.@)
117 BOOL WINAPI SwitchToThread(void)
119 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
123 /***********************************************************************
124 * WaitForSingleObject (KERNEL32.@)
126 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
128 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
132 /***********************************************************************
133 * WaitForSingleObjectEx (KERNEL32.@)
135 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
138 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
142 /***********************************************************************
143 * WaitForMultipleObjects (KERNEL32.@)
145 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
146 BOOL wait_all, DWORD timeout )
148 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
152 /***********************************************************************
153 * WaitForMultipleObjectsEx (KERNEL32.@)
155 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
156 BOOL wait_all, DWORD timeout,
160 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
164 if (count > MAXIMUM_WAIT_OBJECTS)
166 SetLastError(ERROR_INVALID_PARAMETER);
169 for (i = 0; i < count; i++)
171 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
172 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
173 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
174 hloc[i] = GetStdHandle( HandleToULong(handles[i]) );
176 hloc[i] = handles[i];
178 /* yes, even screen buffer console handles are waitable, and are
179 * handled as a handle to the console itself !!
181 if (is_console_handle(hloc[i]))
183 if (!VerifyConsoleIoHandle(hloc[i]))
187 hloc[i] = GetConsoleInputWaitHandle();
191 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable,
192 get_nt_timeout( &time, timeout ) );
194 if (HIWORD(status)) /* is it an error code? */
196 SetLastError( RtlNtStatusToDosError(status) );
197 status = WAIT_FAILED;
203 /***********************************************************************
204 * RegisterWaitForSingleObject (KERNEL32.@)
206 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
207 WAITORTIMERCALLBACK Callback, PVOID Context,
208 ULONG dwMilliseconds, ULONG dwFlags)
212 TRACE("%p %p %p %p %d %d\n",
213 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
215 status = RtlRegisterWait( phNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
216 if (status != STATUS_SUCCESS)
218 SetLastError( RtlNtStatusToDosError(status) );
224 /***********************************************************************
225 * RegisterWaitForSingleObjectEx (KERNEL32.@)
227 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
228 WAITORTIMERCALLBACK Callback, PVOID Context,
229 ULONG dwMilliseconds, ULONG dwFlags )
232 HANDLE hNewWaitObject;
234 TRACE("%p %p %p %d %d\n",
235 hObject,Callback,Context,dwMilliseconds,dwFlags);
237 status = RtlRegisterWait( &hNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
238 if (status != STATUS_SUCCESS)
240 SetLastError( RtlNtStatusToDosError(status) );
243 return hNewWaitObject;
246 /***********************************************************************
247 * UnregisterWait (KERNEL32.@)
249 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
253 TRACE("%p\n",WaitHandle);
255 status = RtlDeregisterWait( WaitHandle );
256 if (status != STATUS_SUCCESS)
258 SetLastError( RtlNtStatusToDosError(status) );
264 /***********************************************************************
265 * UnregisterWaitEx (KERNEL32.@)
267 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
271 TRACE("%p %p\n",WaitHandle, CompletionEvent);
273 status = RtlDeregisterWaitEx( WaitHandle, CompletionEvent );
274 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
278 /***********************************************************************
279 * SignalObjectAndWait (KERNEL32.@)
281 * Allows to atomically signal any of the synchro objects (semaphore,
282 * mutex, event) and wait on another.
284 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
285 DWORD dwMilliseconds, BOOL bAlertable )
288 LARGE_INTEGER timeout;
290 TRACE("%p %p %d %d\n", hObjectToSignal,
291 hObjectToWaitOn, dwMilliseconds, bAlertable);
293 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn, bAlertable,
294 get_nt_timeout( &timeout, dwMilliseconds ) );
297 SetLastError( RtlNtStatusToDosError(status) );
298 status = WAIT_FAILED;
303 /***********************************************************************
304 * InitializeCriticalSection (KERNEL32.@)
306 * Initialise a critical section before use.
309 * crit [O] Critical section to initialise.
312 * Nothing. If the function fails an exception is raised.
314 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
316 InitializeCriticalSectionEx( crit, 0, 0 );
319 /***********************************************************************
320 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
322 * Initialise a critical section with a spin count.
325 * crit [O] Critical section to initialise.
326 * spincount [I] Number of times to spin upon contention.
330 * Failure: Nothing. If the function fails an exception is raised.
333 * spincount is ignored on uni-processor systems.
335 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
337 return InitializeCriticalSectionEx( crit, spincount, 0 );
340 /***********************************************************************
341 * InitializeCriticalSectionEx (KERNEL32.@)
343 * Initialise a critical section with a spin count and flags.
346 * crit [O] Critical section to initialise.
347 * spincount [I] Number of times to spin upon contention.
348 * flags [I] CRITICAL_SECTION_ flags from winbase.h.
352 * Failure: Nothing. If the function fails an exception is raised.
355 * spincount is ignored on uni-processor systems.
357 BOOL WINAPI InitializeCriticalSectionEx( CRITICAL_SECTION *crit, DWORD spincount, DWORD flags )
359 NTSTATUS ret = RtlInitializeCriticalSectionEx( crit, spincount, flags );
360 if (ret) RtlRaiseStatus( ret );
364 /***********************************************************************
365 * MakeCriticalSectionGlobal (KERNEL32.@)
367 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
369 /* let's assume that only one thread at a time will try to do this */
370 HANDLE sem = crit->LockSemaphore;
371 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
372 crit->LockSemaphore = ConvertToGlobalHandle( sem );
373 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
374 crit->DebugInfo = NULL;
378 /***********************************************************************
379 * ReinitializeCriticalSection (KERNEL32.@)
381 * Initialise an already used critical section.
384 * crit [O] Critical section to initialise.
389 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
391 if ( !crit->LockSemaphore )
392 RtlInitializeCriticalSection( crit );
396 /***********************************************************************
397 * UninitializeCriticalSection (KERNEL32.@)
399 * UnInitialise a critical section after use.
402 * crit [O] Critical section to uninitialise (destroy).
407 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
409 RtlDeleteCriticalSection( crit );
413 /***********************************************************************
414 * CreateEventA (KERNEL32.@)
416 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
417 BOOL initial_state, LPCSTR name )
421 if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET;
422 if (initial_state) flags |= CREATE_EVENT_INITIAL_SET;
423 return CreateEventExA( sa, name, flags, EVENT_ALL_ACCESS );
427 /***********************************************************************
428 * CreateEventW (KERNEL32.@)
430 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
431 BOOL initial_state, LPCWSTR name )
435 if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET;
436 if (initial_state) flags |= CREATE_EVENT_INITIAL_SET;
437 return CreateEventExW( sa, name, flags, EVENT_ALL_ACCESS );
441 /***********************************************************************
442 * CreateEventExA (KERNEL32.@)
444 HANDLE WINAPI CreateEventExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
446 WCHAR buffer[MAX_PATH];
448 if (!name) return CreateEventExW( sa, NULL, flags, access );
450 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
452 SetLastError( ERROR_FILENAME_EXCED_RANGE );
455 return CreateEventExW( sa, buffer, flags, access );
459 /***********************************************************************
460 * CreateEventExW (KERNEL32.@)
462 HANDLE WINAPI CreateEventExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
465 UNICODE_STRING nameW;
466 OBJECT_ATTRIBUTES attr;
469 /* one buggy program needs this
470 * ("Van Dale Groot woordenboek der Nederlandse taal")
472 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
474 ERR("Bad security attributes pointer %p\n",sa);
475 SetLastError( ERROR_INVALID_PARAMETER);
479 attr.Length = sizeof(attr);
480 attr.RootDirectory = 0;
481 attr.ObjectName = NULL;
482 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
483 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
484 attr.SecurityQualityOfService = NULL;
487 RtlInitUnicodeString( &nameW, name );
488 attr.ObjectName = &nameW;
489 attr.RootDirectory = get_BaseNamedObjects_handle();
492 status = NtCreateEvent( &ret, access, &attr,
493 (flags & CREATE_EVENT_MANUAL_RESET) ? NotificationEvent : SynchronizationEvent,
494 (flags & CREATE_EVENT_INITIAL_SET) != 0 );
495 if (status == STATUS_OBJECT_NAME_EXISTS)
496 SetLastError( ERROR_ALREADY_EXISTS );
498 SetLastError( RtlNtStatusToDosError(status) );
503 /***********************************************************************
504 * OpenEventA (KERNEL32.@)
506 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
508 WCHAR buffer[MAX_PATH];
510 if (!name) return OpenEventW( access, inherit, NULL );
512 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
514 SetLastError( ERROR_FILENAME_EXCED_RANGE );
517 return OpenEventW( access, inherit, buffer );
521 /***********************************************************************
522 * OpenEventW (KERNEL32.@)
524 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
527 UNICODE_STRING nameW;
528 OBJECT_ATTRIBUTES attr;
531 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
533 attr.Length = sizeof(attr);
534 attr.RootDirectory = 0;
535 attr.ObjectName = NULL;
536 attr.Attributes = inherit ? OBJ_INHERIT : 0;
537 attr.SecurityDescriptor = NULL;
538 attr.SecurityQualityOfService = NULL;
541 RtlInitUnicodeString( &nameW, name );
542 attr.ObjectName = &nameW;
543 attr.RootDirectory = get_BaseNamedObjects_handle();
546 status = NtOpenEvent( &ret, access, &attr );
547 if (status != STATUS_SUCCESS)
549 SetLastError( RtlNtStatusToDosError(status) );
555 /***********************************************************************
556 * PulseEvent (KERNEL32.@)
558 BOOL WINAPI PulseEvent( HANDLE handle )
562 if ((status = NtPulseEvent( handle, NULL )))
563 SetLastError( RtlNtStatusToDosError(status) );
568 /***********************************************************************
569 * SetEvent (KERNEL32.@)
571 BOOL WINAPI SetEvent( HANDLE handle )
575 if ((status = NtSetEvent( handle, NULL )))
576 SetLastError( RtlNtStatusToDosError(status) );
581 /***********************************************************************
582 * ResetEvent (KERNEL32.@)
584 BOOL WINAPI ResetEvent( HANDLE handle )
588 if ((status = NtResetEvent( handle, NULL )))
589 SetLastError( RtlNtStatusToDosError(status) );
594 /***********************************************************************
595 * CreateMutexA (KERNEL32.@)
597 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
599 return CreateMutexExA( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS );
603 /***********************************************************************
604 * CreateMutexW (KERNEL32.@)
606 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
608 return CreateMutexExW( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS );
612 /***********************************************************************
613 * CreateMutexExA (KERNEL32.@)
615 HANDLE WINAPI CreateMutexExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
617 WCHAR buffer[MAX_PATH];
619 if (!name) return CreateMutexExW( sa, NULL, flags, access );
621 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
623 SetLastError( ERROR_FILENAME_EXCED_RANGE );
626 return CreateMutexExW( sa, buffer, flags, access );
630 /***********************************************************************
631 * CreateMutexExW (KERNEL32.@)
633 HANDLE WINAPI CreateMutexExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
636 UNICODE_STRING nameW;
637 OBJECT_ATTRIBUTES attr;
640 attr.Length = sizeof(attr);
641 attr.RootDirectory = 0;
642 attr.ObjectName = NULL;
643 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
644 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
645 attr.SecurityQualityOfService = NULL;
648 RtlInitUnicodeString( &nameW, name );
649 attr.ObjectName = &nameW;
650 attr.RootDirectory = get_BaseNamedObjects_handle();
653 status = NtCreateMutant( &ret, access, &attr, (flags & CREATE_MUTEX_INITIAL_OWNER) != 0 );
654 if (status == STATUS_OBJECT_NAME_EXISTS)
655 SetLastError( ERROR_ALREADY_EXISTS );
657 SetLastError( RtlNtStatusToDosError(status) );
662 /***********************************************************************
663 * OpenMutexA (KERNEL32.@)
665 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
667 WCHAR buffer[MAX_PATH];
669 if (!name) return OpenMutexW( access, inherit, NULL );
671 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
673 SetLastError( ERROR_FILENAME_EXCED_RANGE );
676 return OpenMutexW( access, inherit, buffer );
680 /***********************************************************************
681 * OpenMutexW (KERNEL32.@)
683 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
686 UNICODE_STRING nameW;
687 OBJECT_ATTRIBUTES attr;
690 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
692 attr.Length = sizeof(attr);
693 attr.RootDirectory = 0;
694 attr.ObjectName = NULL;
695 attr.Attributes = inherit ? OBJ_INHERIT : 0;
696 attr.SecurityDescriptor = NULL;
697 attr.SecurityQualityOfService = NULL;
700 RtlInitUnicodeString( &nameW, name );
701 attr.ObjectName = &nameW;
702 attr.RootDirectory = get_BaseNamedObjects_handle();
705 status = NtOpenMutant( &ret, access, &attr );
706 if (status != STATUS_SUCCESS)
708 SetLastError( RtlNtStatusToDosError(status) );
715 /***********************************************************************
716 * ReleaseMutex (KERNEL32.@)
718 BOOL WINAPI ReleaseMutex( HANDLE handle )
722 status = NtReleaseMutant(handle, NULL);
723 if (status != STATUS_SUCCESS)
725 SetLastError( RtlNtStatusToDosError(status) );
737 /***********************************************************************
738 * CreateSemaphoreA (KERNEL32.@)
740 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
742 return CreateSemaphoreExA( sa, initial, max, name, 0, SEMAPHORE_ALL_ACCESS );
746 /***********************************************************************
747 * CreateSemaphoreW (KERNEL32.@)
749 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
750 LONG max, LPCWSTR name )
752 return CreateSemaphoreExW( sa, initial, max, name, 0, SEMAPHORE_ALL_ACCESS );
756 /***********************************************************************
757 * CreateSemaphoreExA (KERNEL32.@)
759 HANDLE WINAPI CreateSemaphoreExA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name,
760 DWORD flags, DWORD access )
762 WCHAR buffer[MAX_PATH];
764 if (!name) return CreateSemaphoreExW( sa, initial, max, NULL, flags, access );
766 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
768 SetLastError( ERROR_FILENAME_EXCED_RANGE );
771 return CreateSemaphoreExW( sa, initial, max, buffer, flags, access );
775 /***********************************************************************
776 * CreateSemaphoreExW (KERNEL32.@)
778 HANDLE WINAPI CreateSemaphoreExW( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCWSTR name,
779 DWORD flags, DWORD access )
782 UNICODE_STRING nameW;
783 OBJECT_ATTRIBUTES attr;
786 attr.Length = sizeof(attr);
787 attr.RootDirectory = 0;
788 attr.ObjectName = NULL;
789 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
790 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
791 attr.SecurityQualityOfService = NULL;
794 RtlInitUnicodeString( &nameW, name );
795 attr.ObjectName = &nameW;
796 attr.RootDirectory = get_BaseNamedObjects_handle();
799 status = NtCreateSemaphore( &ret, access, &attr, initial, max );
800 if (status == STATUS_OBJECT_NAME_EXISTS)
801 SetLastError( ERROR_ALREADY_EXISTS );
803 SetLastError( RtlNtStatusToDosError(status) );
808 /***********************************************************************
809 * OpenSemaphoreA (KERNEL32.@)
811 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
813 WCHAR buffer[MAX_PATH];
815 if (!name) return OpenSemaphoreW( access, inherit, NULL );
817 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
819 SetLastError( ERROR_FILENAME_EXCED_RANGE );
822 return OpenSemaphoreW( access, inherit, buffer );
826 /***********************************************************************
827 * OpenSemaphoreW (KERNEL32.@)
829 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
832 UNICODE_STRING nameW;
833 OBJECT_ATTRIBUTES attr;
836 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
838 attr.Length = sizeof(attr);
839 attr.RootDirectory = 0;
840 attr.ObjectName = NULL;
841 attr.Attributes = inherit ? OBJ_INHERIT : 0;
842 attr.SecurityDescriptor = NULL;
843 attr.SecurityQualityOfService = NULL;
846 RtlInitUnicodeString( &nameW, name );
847 attr.ObjectName = &nameW;
848 attr.RootDirectory = get_BaseNamedObjects_handle();
851 status = NtOpenSemaphore( &ret, access, &attr );
852 if (status != STATUS_SUCCESS)
854 SetLastError( RtlNtStatusToDosError(status) );
861 /***********************************************************************
862 * ReleaseSemaphore (KERNEL32.@)
864 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
866 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
867 if (status) SetLastError( RtlNtStatusToDosError(status) );
876 /******************************************************************************
877 * CreateJobObjectW (KERNEL32.@)
879 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES sa, LPCWSTR name )
882 UNICODE_STRING nameW;
883 OBJECT_ATTRIBUTES attr;
886 attr.Length = sizeof(attr);
887 attr.RootDirectory = 0;
888 attr.ObjectName = NULL;
889 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
890 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
891 attr.SecurityQualityOfService = NULL;
894 RtlInitUnicodeString( &nameW, name );
895 attr.ObjectName = &nameW;
896 attr.RootDirectory = get_BaseNamedObjects_handle();
899 status = NtCreateJobObject( &ret, JOB_OBJECT_ALL_ACCESS, &attr );
900 if (status == STATUS_OBJECT_NAME_EXISTS)
901 SetLastError( ERROR_ALREADY_EXISTS );
903 SetLastError( RtlNtStatusToDosError(status) );
907 /******************************************************************************
908 * CreateJobObjectA (KERNEL32.@)
910 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
912 WCHAR buffer[MAX_PATH];
914 if (!name) return CreateJobObjectW( attr, NULL );
916 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
918 SetLastError( ERROR_FILENAME_EXCED_RANGE );
921 return CreateJobObjectW( attr, buffer );
924 /******************************************************************************
925 * OpenJobObjectW (KERNEL32.@)
927 HANDLE WINAPI OpenJobObjectW( DWORD access, BOOL inherit, LPCWSTR name )
930 UNICODE_STRING nameW;
931 OBJECT_ATTRIBUTES attr;
934 attr.Length = sizeof(attr);
935 attr.RootDirectory = 0;
936 attr.ObjectName = NULL;
937 attr.Attributes = inherit ? OBJ_INHERIT : 0;
938 attr.SecurityDescriptor = NULL;
939 attr.SecurityQualityOfService = NULL;
942 RtlInitUnicodeString( &nameW, name );
943 attr.ObjectName = &nameW;
944 attr.RootDirectory = get_BaseNamedObjects_handle();
947 status = NtOpenJobObject( &ret, access, &attr );
948 if (status != STATUS_SUCCESS)
950 SetLastError( RtlNtStatusToDosError(status) );
956 /******************************************************************************
957 * OpenJobObjectA (KERNEL32.@)
959 HANDLE WINAPI OpenJobObjectA( DWORD access, BOOL inherit, LPCSTR name )
961 WCHAR buffer[MAX_PATH];
963 if (!name) return OpenJobObjectW( access, inherit, NULL );
965 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
967 SetLastError( ERROR_FILENAME_EXCED_RANGE );
970 return OpenJobObjectW( access, inherit, buffer );
973 /******************************************************************************
974 * TerminateJobObject (KERNEL32.@)
976 BOOL WINAPI TerminateJobObject( HANDLE job, UINT exit_code )
978 NTSTATUS status = NtTerminateJobObject( job, exit_code );
979 if (status) SetLastError( RtlNtStatusToDosError(status) );
983 /******************************************************************************
984 * QueryInformationJobObject (KERNEL32.@)
986 BOOL WINAPI QueryInformationJobObject( HANDLE job, JOBOBJECTINFOCLASS class, LPVOID info,
987 DWORD len, DWORD *ret_len )
989 NTSTATUS status = NtQueryInformationJobObject( job, class, info, len, ret_len );
990 if (status) SetLastError( RtlNtStatusToDosError(status) );
994 /******************************************************************************
995 * SetInformationJobObject (KERNEL32.@)
997 BOOL WINAPI SetInformationJobObject( HANDLE job, JOBOBJECTINFOCLASS class, LPVOID info, DWORD len )
999 NTSTATUS status = NtSetInformationJobObject( job, class, info, len );
1000 if (status) SetLastError( RtlNtStatusToDosError(status) );
1004 /******************************************************************************
1005 * AssignProcessToJobObject (KERNEL32.@)
1007 BOOL WINAPI AssignProcessToJobObject( HANDLE job, HANDLE process )
1009 NTSTATUS status = NtAssignProcessToJobObject( job, process );
1010 if (status) SetLastError( RtlNtStatusToDosError(status) );
1014 /******************************************************************************
1015 * IsProcessInJob (KERNEL32.@)
1017 BOOL WINAPI IsProcessInJob( HANDLE process, HANDLE job, PBOOL result )
1019 NTSTATUS status = NtIsProcessInJob( job, process );
1022 case STATUS_PROCESS_IN_JOB:
1025 case STATUS_PROCESS_NOT_IN_JOB:
1029 SetLastError( RtlNtStatusToDosError(status) );
1040 /***********************************************************************
1041 * CreateWaitableTimerA (KERNEL32.@)
1043 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
1045 return CreateWaitableTimerExA( sa, name, manual ? CREATE_WAITABLE_TIMER_MANUAL_RESET : 0,
1050 /***********************************************************************
1051 * CreateWaitableTimerW (KERNEL32.@)
1053 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
1055 return CreateWaitableTimerExW( sa, name, manual ? CREATE_WAITABLE_TIMER_MANUAL_RESET : 0,
1060 /***********************************************************************
1061 * CreateWaitableTimerExA (KERNEL32.@)
1063 HANDLE WINAPI CreateWaitableTimerExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
1065 WCHAR buffer[MAX_PATH];
1067 if (!name) return CreateWaitableTimerExW( sa, NULL, flags, access );
1069 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1071 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1074 return CreateWaitableTimerExW( sa, buffer, flags, access );
1078 /***********************************************************************
1079 * CreateWaitableTimerExW (KERNEL32.@)
1081 HANDLE WINAPI CreateWaitableTimerExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
1085 UNICODE_STRING nameW;
1086 OBJECT_ATTRIBUTES attr;
1088 attr.Length = sizeof(attr);
1089 attr.RootDirectory = 0;
1090 attr.ObjectName = NULL;
1091 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1092 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1093 attr.SecurityQualityOfService = NULL;
1096 RtlInitUnicodeString( &nameW, name );
1097 attr.ObjectName = &nameW;
1098 attr.RootDirectory = get_BaseNamedObjects_handle();
1101 status = NtCreateTimer( &handle, access, &attr,
1102 (flags & CREATE_WAITABLE_TIMER_MANUAL_RESET) ? NotificationTimer : SynchronizationTimer );
1103 if (status == STATUS_OBJECT_NAME_EXISTS)
1104 SetLastError( ERROR_ALREADY_EXISTS );
1106 SetLastError( RtlNtStatusToDosError(status) );
1111 /***********************************************************************
1112 * OpenWaitableTimerA (KERNEL32.@)
1114 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
1116 WCHAR buffer[MAX_PATH];
1118 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
1120 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1122 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1125 return OpenWaitableTimerW( access, inherit, buffer );
1129 /***********************************************************************
1130 * OpenWaitableTimerW (KERNEL32.@)
1132 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
1135 UNICODE_STRING nameW;
1136 OBJECT_ATTRIBUTES attr;
1139 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
1141 attr.Length = sizeof(attr);
1142 attr.RootDirectory = 0;
1143 attr.ObjectName = NULL;
1144 attr.Attributes = inherit ? OBJ_INHERIT : 0;
1145 attr.SecurityDescriptor = NULL;
1146 attr.SecurityQualityOfService = NULL;
1149 RtlInitUnicodeString( &nameW, name );
1150 attr.ObjectName = &nameW;
1151 attr.RootDirectory = get_BaseNamedObjects_handle();
1154 status = NtOpenTimer(&handle, access, &attr);
1155 if (status != STATUS_SUCCESS)
1157 SetLastError( RtlNtStatusToDosError(status) );
1164 /***********************************************************************
1165 * SetWaitableTimer (KERNEL32.@)
1167 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
1168 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
1170 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
1171 arg, resume, period, NULL);
1173 if (status != STATUS_SUCCESS)
1175 SetLastError( RtlNtStatusToDosError(status) );
1176 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
1182 /***********************************************************************
1183 * CancelWaitableTimer (KERNEL32.@)
1185 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1189 status = NtCancelTimer(handle, NULL);
1190 if (status != STATUS_SUCCESS)
1192 SetLastError( RtlNtStatusToDosError(status) );
1199 /***********************************************************************
1200 * CreateTimerQueue (KERNEL32.@)
1202 HANDLE WINAPI CreateTimerQueue(void)
1205 NTSTATUS status = RtlCreateTimerQueue(&q);
1207 if (status != STATUS_SUCCESS)
1209 SetLastError( RtlNtStatusToDosError(status) );
1217 /***********************************************************************
1218 * DeleteTimerQueueEx (KERNEL32.@)
1220 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1222 NTSTATUS status = RtlDeleteTimerQueueEx(TimerQueue, CompletionEvent);
1224 if (status != STATUS_SUCCESS)
1226 SetLastError( RtlNtStatusToDosError(status) );
1233 /***********************************************************************
1234 * DeleteTimerQueue (KERNEL32.@)
1236 BOOL WINAPI DeleteTimerQueue(HANDLE TimerQueue)
1238 return DeleteTimerQueueEx(TimerQueue, NULL);
1241 /***********************************************************************
1242 * CreateTimerQueueTimer (KERNEL32.@)
1244 * Creates a timer-queue timer. This timer expires at the specified due
1245 * time (in ms), then after every specified period (in ms). When the timer
1246 * expires, the callback function is called.
1249 * nonzero on success or zero on failure
1251 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1252 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1253 DWORD DueTime, DWORD Period, ULONG Flags )
1255 NTSTATUS status = RtlCreateTimer(phNewTimer, TimerQueue, Callback,
1256 Parameter, DueTime, Period, Flags);
1258 if (status != STATUS_SUCCESS)
1260 SetLastError( RtlNtStatusToDosError(status) );
1267 /***********************************************************************
1268 * ChangeTimerQueueTimer (KERNEL32.@)
1270 * Changes the times at which the timer expires.
1273 * nonzero on success or zero on failure
1275 BOOL WINAPI ChangeTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1276 ULONG DueTime, ULONG Period )
1278 NTSTATUS status = RtlUpdateTimer(TimerQueue, Timer, DueTime, Period);
1280 if (status != STATUS_SUCCESS)
1282 SetLastError( RtlNtStatusToDosError(status) );
1289 /***********************************************************************
1290 * DeleteTimerQueueTimer (KERNEL32.@)
1292 * Cancels a timer-queue timer.
1295 * nonzero on success or zero on failure
1297 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1298 HANDLE CompletionEvent )
1300 NTSTATUS status = RtlDeleteTimer(TimerQueue, Timer, CompletionEvent);
1301 if (status != STATUS_SUCCESS)
1303 SetLastError( RtlNtStatusToDosError(status) );
1315 /***********************************************************************
1316 * CreateNamedPipeA (KERNEL32.@)
1318 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1319 DWORD dwPipeMode, DWORD nMaxInstances,
1320 DWORD nOutBufferSize, DWORD nInBufferSize,
1321 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1323 WCHAR buffer[MAX_PATH];
1325 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1326 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1328 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1330 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1331 return INVALID_HANDLE_VALUE;
1333 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1334 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1338 /***********************************************************************
1339 * CreateNamedPipeW (KERNEL32.@)
1341 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1342 DWORD dwPipeMode, DWORD nMaxInstances,
1343 DWORD nOutBufferSize, DWORD nInBufferSize,
1344 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1347 UNICODE_STRING nt_name;
1348 OBJECT_ATTRIBUTES attr;
1349 DWORD access, options;
1350 BOOLEAN pipe_type, read_mode, non_block;
1352 IO_STATUS_BLOCK iosb;
1353 LARGE_INTEGER timeout;
1355 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1356 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1357 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1359 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1361 SetLastError( ERROR_PATH_NOT_FOUND );
1362 return INVALID_HANDLE_VALUE;
1364 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1366 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1367 RtlFreeUnicodeString( &nt_name );
1368 return INVALID_HANDLE_VALUE;
1371 attr.Length = sizeof(attr);
1372 attr.RootDirectory = 0;
1373 attr.ObjectName = &nt_name;
1374 attr.Attributes = OBJ_CASE_INSENSITIVE |
1375 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1376 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1377 attr.SecurityQualityOfService = NULL;
1379 switch(dwOpenMode & 3)
1381 case PIPE_ACCESS_INBOUND:
1382 options = FILE_PIPE_INBOUND;
1383 access = GENERIC_READ;
1385 case PIPE_ACCESS_OUTBOUND:
1386 options = FILE_PIPE_OUTBOUND;
1387 access = GENERIC_WRITE;
1389 case PIPE_ACCESS_DUPLEX:
1390 options = FILE_PIPE_FULL_DUPLEX;
1391 access = GENERIC_READ | GENERIC_WRITE;
1394 SetLastError( ERROR_INVALID_PARAMETER );
1395 return INVALID_HANDLE_VALUE;
1397 access |= SYNCHRONIZE;
1398 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1399 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1400 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1401 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1402 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1403 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0U;
1405 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1409 status = NtCreateNamedPipeFile(&handle, access, &attr, &iosb, 0,
1410 FILE_OVERWRITE_IF, options, pipe_type,
1411 read_mode, non_block, nMaxInstances,
1412 nInBufferSize, nOutBufferSize, &timeout);
1414 RtlFreeUnicodeString( &nt_name );
1417 handle = INVALID_HANDLE_VALUE;
1418 SetLastError( RtlNtStatusToDosError(status) );
1424 /***********************************************************************
1425 * PeekNamedPipe (KERNEL32.@)
1427 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1428 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1430 FILE_PIPE_PEEK_BUFFER local_buffer;
1431 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1435 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1436 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1438 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1442 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1443 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1446 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1447 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1448 if (lpcbRead) *lpcbRead = read_size;
1449 if (lpcbMessage) *lpcbMessage = 0; /* FIXME */
1450 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1452 else SetLastError( RtlNtStatusToDosError(status) );
1454 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1458 /***********************************************************************
1459 * WaitNamedPipeA (KERNEL32.@)
1461 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1463 WCHAR buffer[MAX_PATH];
1465 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1467 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1469 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1472 return WaitNamedPipeW( buffer, nTimeOut );
1476 /***********************************************************************
1477 * WaitNamedPipeW (KERNEL32.@)
1479 * Waits for a named pipe instance to become available
1482 * name [I] Pointer to a named pipe name to wait for
1483 * nTimeOut [I] How long to wait in ms
1486 * TRUE: Success, named pipe can be opened with CreateFile
1487 * FALSE: Failure, GetLastError can be called for further details
1489 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1491 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1493 UNICODE_STRING nt_name, pipe_dev_name;
1494 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1495 IO_STATUS_BLOCK iosb;
1496 OBJECT_ATTRIBUTES attr;
1500 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1502 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1505 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1506 nt_name.Length < sizeof(leadin) ||
1507 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR)) != 0)
1509 RtlFreeUnicodeString( &nt_name );
1510 SetLastError( ERROR_PATH_NOT_FOUND );
1514 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1515 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1517 RtlFreeUnicodeString( &nt_name );
1518 SetLastError( ERROR_OUTOFMEMORY );
1522 pipe_dev_name.Buffer = nt_name.Buffer;
1523 pipe_dev_name.Length = sizeof(leadin);
1524 pipe_dev_name.MaximumLength = sizeof(leadin);
1525 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1526 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1527 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1528 FILE_SYNCHRONOUS_IO_NONALERT);
1529 if (status != ERROR_SUCCESS)
1531 SetLastError( ERROR_PATH_NOT_FOUND );
1535 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1536 if (nTimeOut == NMPWAIT_WAIT_FOREVER)
1537 pipe_wait->Timeout.QuadPart = ((ULONGLONG)0x7fffffff << 32) | 0xffffffff;
1539 pipe_wait->Timeout.QuadPart = (ULONGLONG)nTimeOut * -10000;
1540 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1541 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1542 pipe_wait->NameLength);
1543 RtlFreeUnicodeString( &nt_name );
1545 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1546 pipe_wait, sz_pipe_wait, NULL, 0 );
1548 HeapFree( GetProcessHeap(), 0, pipe_wait );
1549 NtClose( pipe_dev );
1551 if(status != STATUS_SUCCESS)
1553 SetLastError(RtlNtStatusToDosError(status));
1561 /***********************************************************************
1562 * ConnectNamedPipe (KERNEL32.@)
1564 * Connects to a named pipe
1567 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1568 * overlapped: Optional OVERLAPPED struct
1572 * FALSE: Failure, GetLastError can be called for further details
1574 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1577 IO_STATUS_BLOCK status_block;
1578 LPVOID cvalue = NULL;
1580 TRACE("(%p,%p)\n", hPipe, overlapped);
1584 overlapped->Internal = STATUS_PENDING;
1585 overlapped->InternalHigh = 0;
1586 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1589 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, cvalue,
1590 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1591 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1593 if (status == STATUS_SUCCESS) return TRUE;
1594 SetLastError( RtlNtStatusToDosError(status) );
1598 /***********************************************************************
1599 * DisconnectNamedPipe (KERNEL32.@)
1601 * Disconnects from a named pipe
1604 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1608 * FALSE: Failure, GetLastError can be called for further details
1610 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1613 IO_STATUS_BLOCK io_block;
1615 TRACE("(%p)\n",hPipe);
1617 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1619 if (status == STATUS_SUCCESS) return TRUE;
1620 SetLastError( RtlNtStatusToDosError(status) );
1624 /***********************************************************************
1625 * TransactNamedPipe (KERNEL32.@)
1628 * should be done as a single operation in the wineserver or kernel
1630 BOOL WINAPI TransactNamedPipe(
1631 HANDLE handle, LPVOID write_buf, DWORD write_size, LPVOID read_buf,
1632 DWORD read_size, LPDWORD bytes_read, LPOVERLAPPED overlapped)
1637 TRACE("%p %p %d %p %d %p %p\n",
1638 handle, write_buf, write_size, read_buf,
1639 read_size, bytes_read, overlapped);
1643 FIXME("Doesn't support overlapped operation as yet\n");
1647 r = WriteFile(handle, write_buf, write_size, &count, NULL);
1649 r = ReadFile(handle, read_buf, read_size, bytes_read, NULL);
1654 /***********************************************************************
1655 * GetNamedPipeInfo (KERNEL32.@)
1657 BOOL WINAPI GetNamedPipeInfo(
1658 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1659 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1661 FILE_PIPE_LOCAL_INFORMATION fpli;
1662 IO_STATUS_BLOCK iosb;
1665 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1666 FilePipeLocalInformation);
1669 SetLastError( RtlNtStatusToDosError(status) );
1675 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1676 PIPE_SERVER_END : PIPE_CLIENT_END;
1677 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1678 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1681 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1682 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1683 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1688 /***********************************************************************
1689 * GetNamedPipeHandleStateA (KERNEL32.@)
1691 BOOL WINAPI GetNamedPipeHandleStateA(
1692 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1693 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1694 LPSTR lpUsername, DWORD nUsernameMaxSize)
1696 FIXME("%p %p %p %p %p %p %d\n",
1697 hNamedPipe, lpState, lpCurInstances,
1698 lpMaxCollectionCount, lpCollectDataTimeout,
1699 lpUsername, nUsernameMaxSize);
1704 /***********************************************************************
1705 * GetNamedPipeHandleStateW (KERNEL32.@)
1707 BOOL WINAPI GetNamedPipeHandleStateW(
1708 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1709 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1710 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1712 FIXME("%p %p %p %p %p %p %d\n",
1713 hNamedPipe, lpState, lpCurInstances,
1714 lpMaxCollectionCount, lpCollectDataTimeout,
1715 lpUsername, nUsernameMaxSize);
1720 /***********************************************************************
1721 * SetNamedPipeHandleState (KERNEL32.@)
1723 BOOL WINAPI SetNamedPipeHandleState(
1724 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1725 LPDWORD lpCollectDataTimeout)
1727 /* should be a fixme, but this function is called a lot by the RPC
1728 * runtime, and it slows down InstallShield a fair bit. */
1729 WARN("stub: %p %p/%d %p %p\n",
1730 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1734 /***********************************************************************
1735 * CallNamedPipeA (KERNEL32.@)
1737 BOOL WINAPI CallNamedPipeA(
1738 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1739 LPVOID lpOutput, DWORD dwOutputSize,
1740 LPDWORD lpBytesRead, DWORD nTimeout)
1746 TRACE("%s %p %d %p %d %p %d\n",
1747 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1748 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1750 if( lpNamedPipeName )
1752 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1753 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1754 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1756 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1757 dwOutputSize, lpBytesRead, nTimeout );
1758 if( lpNamedPipeName )
1759 HeapFree( GetProcessHeap(), 0, str );
1764 /***********************************************************************
1765 * CallNamedPipeW (KERNEL32.@)
1767 BOOL WINAPI CallNamedPipeW(
1768 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1769 LPVOID lpOutput, DWORD lpOutputSize,
1770 LPDWORD lpBytesRead, DWORD nTimeout)
1776 TRACE("%s %p %d %p %d %p %d\n",
1777 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1778 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1780 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1781 if (pipe == INVALID_HANDLE_VALUE)
1783 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1786 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1787 if (pipe == INVALID_HANDLE_VALUE)
1791 mode = PIPE_READMODE_MESSAGE;
1792 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1794 /* Currently SetNamedPipeHandleState() is a stub returning FALSE */
1795 if (ret) FIXME("Now that SetNamedPipeHandleState() is more than a stub, please update CallNamedPipeW\n");
1803 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1811 /******************************************************************
1812 * CreatePipe (KERNEL32.@)
1815 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1816 LPSECURITY_ATTRIBUTES sa, DWORD size )
1818 static unsigned index /* = 0 */;
1821 unsigned in_index = index;
1822 UNICODE_STRING nt_name;
1823 OBJECT_ATTRIBUTES attr;
1825 IO_STATUS_BLOCK iosb;
1826 LARGE_INTEGER timeout;
1828 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1830 attr.Length = sizeof(attr);
1831 attr.RootDirectory = 0;
1832 attr.ObjectName = &nt_name;
1833 attr.Attributes = OBJ_CASE_INSENSITIVE |
1834 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1835 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1836 attr.SecurityQualityOfService = NULL;
1838 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1839 /* generate a unique pipe name (system wide) */
1842 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1843 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1844 'u','.','%','0','8','u','\0' };
1846 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1847 GetCurrentProcessId(), ++index);
1848 RtlInitUnicodeString(&nt_name, name);
1849 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1850 0, FILE_OVERWRITE_IF,
1851 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1852 FALSE, FALSE, FALSE,
1853 1, size, size, &timeout);
1856 SetLastError( RtlNtStatusToDosError(status) );
1857 hr = INVALID_HANDLE_VALUE;
1859 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1860 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1861 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1863 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1864 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1868 SetLastError( RtlNtStatusToDosError(status) );
1879 /******************************************************************************
1880 * CreateMailslotA [KERNEL32.@]
1882 * See CreateMailslotW.
1884 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1885 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1891 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1892 nMaxMessageSize, lReadTimeout, sa);
1896 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1897 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1898 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1901 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1903 HeapFree( GetProcessHeap(), 0, name );
1909 /******************************************************************************
1910 * CreateMailslotW [KERNEL32.@]
1912 * Create a mailslot with specified name.
1915 * lpName [I] Pointer to string for mailslot name
1916 * nMaxMessageSize [I] Maximum message size
1917 * lReadTimeout [I] Milliseconds before read time-out
1918 * sa [I] Pointer to security structure
1921 * Success: Handle to mailslot
1922 * Failure: INVALID_HANDLE_VALUE
1924 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1925 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1927 HANDLE handle = INVALID_HANDLE_VALUE;
1928 OBJECT_ATTRIBUTES attr;
1929 UNICODE_STRING nameW;
1930 LARGE_INTEGER timeout;
1931 IO_STATUS_BLOCK iosb;
1934 TRACE("%s %d %d %p\n", debugstr_w(lpName),
1935 nMaxMessageSize, lReadTimeout, sa);
1937 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1939 SetLastError( ERROR_PATH_NOT_FOUND );
1940 return INVALID_HANDLE_VALUE;
1943 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1945 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1946 RtlFreeUnicodeString( &nameW );
1947 return INVALID_HANDLE_VALUE;
1950 attr.Length = sizeof(attr);
1951 attr.RootDirectory = 0;
1952 attr.Attributes = OBJ_CASE_INSENSITIVE;
1953 attr.ObjectName = &nameW;
1954 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1955 attr.SecurityQualityOfService = NULL;
1957 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1958 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1960 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1962 status = NtCreateMailslotFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr,
1963 &iosb, 0, 0, nMaxMessageSize, &timeout );
1966 SetLastError( RtlNtStatusToDosError(status) );
1967 handle = INVALID_HANDLE_VALUE;
1970 RtlFreeUnicodeString( &nameW );
1975 /******************************************************************************
1976 * GetMailslotInfo [KERNEL32.@]
1978 * Retrieve information about a mailslot.
1981 * hMailslot [I] Mailslot handle
1982 * lpMaxMessageSize [O] Address of maximum message size
1983 * lpNextSize [O] Address of size of next message
1984 * lpMessageCount [O] Address of number of messages
1985 * lpReadTimeout [O] Address of read time-out
1991 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1992 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1993 LPDWORD lpReadTimeout )
1995 FILE_MAILSLOT_QUERY_INFORMATION info;
1996 IO_STATUS_BLOCK iosb;
1999 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
2000 lpNextSize, lpMessageCount, lpReadTimeout);
2002 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
2003 FileMailslotQueryInformation );
2005 if( status != STATUS_SUCCESS )
2007 SetLastError( RtlNtStatusToDosError(status) );
2011 if( lpMaxMessageSize )
2012 *lpMaxMessageSize = info.MaximumMessageSize;
2014 *lpNextSize = info.NextMessageSize;
2015 if( lpMessageCount )
2016 *lpMessageCount = info.MessagesAvailable;
2019 if (info.ReadTimeout.QuadPart == (((LONGLONG)0x7fffffff << 32) | 0xffffffff))
2020 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
2022 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
2028 /******************************************************************************
2029 * SetMailslotInfo [KERNEL32.@]
2031 * Set the read timeout of a mailslot.
2034 * hMailslot [I] Mailslot handle
2035 * dwReadTimeout [I] Timeout in milliseconds.
2041 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
2043 FILE_MAILSLOT_SET_INFORMATION info;
2044 IO_STATUS_BLOCK iosb;
2047 TRACE("%p %d\n", hMailslot, dwReadTimeout);
2049 if (dwReadTimeout != MAILSLOT_WAIT_FOREVER)
2050 info.ReadTimeout.QuadPart = (ULONGLONG)dwReadTimeout * -10000;
2052 info.ReadTimeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
2053 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
2054 FileMailslotSetInformation );
2055 if( status != STATUS_SUCCESS )
2057 SetLastError( RtlNtStatusToDosError(status) );
2064 /******************************************************************************
2065 * CreateIoCompletionPort (KERNEL32.@)
2067 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
2068 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
2073 TRACE("(%p, %p, %08lx, %08x)\n",
2074 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
2076 if (hExistingCompletionPort && hFileHandle == INVALID_HANDLE_VALUE)
2078 SetLastError( ERROR_INVALID_PARAMETER);
2082 if (hExistingCompletionPort)
2083 ret = hExistingCompletionPort;
2086 status = NtCreateIoCompletion( &ret, IO_COMPLETION_ALL_ACCESS, NULL, dwNumberOfConcurrentThreads );
2087 if (status != STATUS_SUCCESS) goto fail;
2090 if (hFileHandle != INVALID_HANDLE_VALUE)
2092 FILE_COMPLETION_INFORMATION info;
2093 IO_STATUS_BLOCK iosb;
2095 info.CompletionPort = ret;
2096 info.CompletionKey = CompletionKey;
2097 status = NtSetInformationFile( hFileHandle, &iosb, &info, sizeof(info), FileCompletionInformation );
2098 if (status != STATUS_SUCCESS) goto fail;
2104 if (ret && !hExistingCompletionPort)
2106 SetLastError( RtlNtStatusToDosError(status) );
2110 /******************************************************************************
2111 * GetQueuedCompletionStatus (KERNEL32.@)
2113 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
2114 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
2115 DWORD dwMilliseconds )
2118 IO_STATUS_BLOCK iosb;
2119 LARGE_INTEGER wait_time;
2121 TRACE("(%p,%p,%p,%p,%d)\n",
2122 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
2124 *lpOverlapped = NULL;
2126 status = NtRemoveIoCompletion( CompletionPort, pCompletionKey, (PULONG_PTR)lpOverlapped,
2127 &iosb, get_nt_timeout( &wait_time, dwMilliseconds ) );
2128 if (status == STATUS_SUCCESS)
2130 *lpNumberOfBytesTransferred = iosb.Information;
2131 if (iosb.u.Status >= 0) return TRUE;
2132 SetLastError( RtlNtStatusToDosError(iosb.u.Status) );
2136 if (status == STATUS_TIMEOUT) SetLastError( WAIT_TIMEOUT );
2137 else SetLastError( RtlNtStatusToDosError(status) );
2142 /******************************************************************************
2143 * PostQueuedCompletionStatus (KERNEL32.@)
2145 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
2146 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
2150 TRACE("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
2152 status = NtSetIoCompletion( CompletionPort, dwCompletionKey, (ULONG_PTR)lpOverlapped,
2153 STATUS_SUCCESS, dwNumberOfBytes );
2155 if (status == STATUS_SUCCESS) return TRUE;
2156 SetLastError( RtlNtStatusToDosError(status) );
2160 /******************************************************************************
2161 * BindIoCompletionCallback (KERNEL32.@)
2163 BOOL WINAPI BindIoCompletionCallback( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags)
2167 TRACE("(%p, %p, %d)\n", FileHandle, Function, Flags);
2169 status = RtlSetIoCompletionCallback( FileHandle, (PRTL_OVERLAPPED_COMPLETION_ROUTINE)Function, Flags );
2170 if (status == STATUS_SUCCESS) return TRUE;
2171 SetLastError( RtlNtStatusToDosError(status) );
2176 /***********************************************************************
2177 * CreateMemoryResourceNotification (KERNEL32.@)
2179 HANDLE WINAPI CreateMemoryResourceNotification(MEMORY_RESOURCE_NOTIFICATION_TYPE nt)
2181 FIXME("(%d) stub\n", nt);
2182 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
2189 /***********************************************************************
2190 * InterlockedCompareExchange (KERNEL32.@)
2192 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
2193 __ASM_STDCALL_FUNC(InterlockedCompareExchange, 12,
2194 "movl 12(%esp),%eax\n\t"
2195 "movl 8(%esp),%ecx\n\t"
2196 "movl 4(%esp),%edx\n\t"
2197 "lock; cmpxchgl %ecx,(%edx)\n\t"
2200 /***********************************************************************
2201 * InterlockedExchange (KERNEL32.@)
2203 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
2204 __ASM_STDCALL_FUNC(InterlockedExchange, 8,
2205 "movl 8(%esp),%eax\n\t"
2206 "movl 4(%esp),%edx\n\t"
2207 "lock; xchgl %eax,(%edx)\n\t"
2210 /***********************************************************************
2211 * InterlockedExchangeAdd (KERNEL32.@)
2213 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
2214 __ASM_STDCALL_FUNC(InterlockedExchangeAdd, 8,
2215 "movl 8(%esp),%eax\n\t"
2216 "movl 4(%esp),%edx\n\t"
2217 "lock; xaddl %eax,(%edx)\n\t"
2220 /***********************************************************************
2221 * InterlockedIncrement (KERNEL32.@)
2223 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2224 __ASM_STDCALL_FUNC(InterlockedIncrement, 4,
2225 "movl 4(%esp),%edx\n\t"
2227 "lock; xaddl %eax,(%edx)\n\t"
2231 /***********************************************************************
2232 * InterlockedDecrement (KERNEL32.@)
2234 __ASM_STDCALL_FUNC(InterlockedDecrement, 4,
2235 "movl 4(%esp),%edx\n\t"
2237 "lock; xaddl %eax,(%edx)\n\t"
2241 #endif /* __i386__ */