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"
29 #ifdef HAVE_SYS_IOCTL_H
30 #include <sys/ioctl.h>
35 #ifdef HAVE_SYS_POLL_H
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
44 #define NONAMELESSUNION
45 #define NONAMELESSSTRUCT
48 #define WIN32_NO_STATUS
57 #include "wine/server.h"
58 #include "wine/unicode.h"
59 #include "wine/winbase16.h"
60 #include "kernel_private.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(sync);
66 /* check if current version is NT or Win95 */
67 inline static int is_version_nt(void)
69 return !(GetVersion() & 0x80000000);
72 /* returns directory handle to \\BaseNamedObjects */
73 HANDLE get_BaseNamedObjects_handle(void)
75 static HANDLE handle = NULL;
76 static const WCHAR basenameW[] =
77 {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
79 OBJECT_ATTRIBUTES attr;
85 RtlInitUnicodeString(&str, basenameW);
86 InitializeObjectAttributes(&attr, &str, 0, 0, NULL);
87 NtOpenDirectoryObject(&dir, DIRECTORY_CREATE_OBJECT|DIRECTORY_TRAVERSE,
89 if (InterlockedCompareExchangePointer( (PVOID)&handle, dir, 0 ) != 0)
91 /* someone beat us here... */
98 /***********************************************************************
101 VOID WINAPI Sleep( DWORD timeout )
103 SleepEx( timeout, FALSE );
106 /******************************************************************************
107 * SleepEx (KERNEL32.@)
109 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
113 if (timeout == INFINITE) status = NtDelayExecution( alertable, NULL );
118 time.QuadPart = timeout * (ULONGLONG)10000;
119 time.QuadPart = -time.QuadPart;
120 status = NtDelayExecution( alertable, &time );
122 if (status != STATUS_USER_APC) status = STATUS_SUCCESS;
127 /***********************************************************************
128 * SwitchToThread (KERNEL32.@)
130 BOOL WINAPI SwitchToThread(void)
132 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
136 /***********************************************************************
137 * WaitForSingleObject (KERNEL32.@)
139 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
141 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
145 /***********************************************************************
146 * WaitForSingleObjectEx (KERNEL32.@)
148 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
151 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
155 /***********************************************************************
156 * WaitForMultipleObjects (KERNEL32.@)
158 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
159 BOOL wait_all, DWORD timeout )
161 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
165 /***********************************************************************
166 * WaitForMultipleObjectsEx (KERNEL32.@)
168 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
169 BOOL wait_all, DWORD timeout,
173 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
176 if (count > MAXIMUM_WAIT_OBJECTS)
178 SetLastError(ERROR_INVALID_PARAMETER);
181 for (i = 0; i < count; i++)
183 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
184 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
185 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
186 hloc[i] = GetStdHandle( (DWORD)handles[i] );
188 hloc[i] = handles[i];
190 /* yes, even screen buffer console handles are waitable, and are
191 * handled as a handle to the console itself !!
193 if (is_console_handle(hloc[i]))
195 if (!VerifyConsoleIoHandle(hloc[i]))
199 hloc[i] = GetConsoleInputWaitHandle();
203 if (timeout == INFINITE)
205 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, NULL );
211 time.QuadPart = timeout * (ULONGLONG)10000;
212 time.QuadPart = -time.QuadPart;
213 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, &time );
216 if (HIWORD(status)) /* is it an error code? */
218 SetLastError( RtlNtStatusToDosError(status) );
219 status = WAIT_FAILED;
225 /***********************************************************************
226 * WaitForSingleObject (KERNEL.460)
228 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
230 DWORD retval, mutex_count;
232 ReleaseThunkLock( &mutex_count );
233 retval = WaitForSingleObject( handle, timeout );
234 RestoreThunkLock( mutex_count );
238 /***********************************************************************
239 * WaitForMultipleObjects (KERNEL.461)
241 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
242 BOOL wait_all, DWORD timeout )
244 DWORD retval, mutex_count;
246 ReleaseThunkLock( &mutex_count );
247 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
248 RestoreThunkLock( mutex_count );
252 /***********************************************************************
253 * WaitForMultipleObjectsEx (KERNEL.495)
255 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
256 BOOL wait_all, DWORD timeout, BOOL alertable )
258 DWORD retval, mutex_count;
260 ReleaseThunkLock( &mutex_count );
261 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
262 RestoreThunkLock( mutex_count );
266 /***********************************************************************
267 * RegisterWaitForSingleObject (KERNEL32.@)
269 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
270 WAITORTIMERCALLBACK Callback, PVOID Context,
271 ULONG dwMilliseconds, ULONG dwFlags)
273 FIXME("%p %p %p %p %d %d\n",
274 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
278 /***********************************************************************
279 * RegisterWaitForSingleObjectEx (KERNEL32.@)
281 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
282 WAITORTIMERCALLBACK Callback, PVOID Context,
283 ULONG dwMilliseconds, ULONG dwFlags )
285 FIXME("%p %p %p %d %d\n",
286 hObject,Callback,Context,dwMilliseconds,dwFlags);
290 /***********************************************************************
291 * UnregisterWait (KERNEL32.@)
293 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
295 FIXME("%p\n",WaitHandle);
299 /***********************************************************************
300 * UnregisterWaitEx (KERNEL32.@)
302 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
304 FIXME("%p %p\n",WaitHandle, CompletionEvent);
308 /***********************************************************************
309 * SignalObjectAndWait (KERNEL32.@)
311 * Allows to atomically signal any of the synchro objects (semaphore,
312 * mutex, event) and wait on another.
314 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
315 DWORD dwMilliseconds, BOOL bAlertable )
318 LARGE_INTEGER timeout, *ptimeout = NULL;
320 TRACE("%p %p %d %d\n", hObjectToSignal,
321 hObjectToWaitOn, dwMilliseconds, bAlertable);
323 if (dwMilliseconds != INFINITE)
325 timeout.QuadPart = dwMilliseconds * (ULONGLONG)10000;
326 timeout.QuadPart = -timeout.QuadPart;
330 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn,
331 bAlertable, ptimeout );
334 SetLastError( RtlNtStatusToDosError(status) );
335 status = WAIT_FAILED;
340 /***********************************************************************
341 * InitializeCriticalSection (KERNEL32.@)
343 * Initialise a critical section before use.
346 * crit [O] Critical section to initialise.
349 * Nothing. If the function fails an exception is raised.
351 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
353 NTSTATUS ret = RtlInitializeCriticalSection( crit );
354 if (ret) RtlRaiseStatus( ret );
357 /***********************************************************************
358 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
360 * Initialise a critical section with a spin count.
363 * crit [O] Critical section to initialise.
364 * spincount [I] Number of times to spin upon contention.
368 * Failure: Nothing. If the function fails an exception is raised.
371 * spincount is ignored on uni-processor systems.
373 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
375 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
376 if (ret) RtlRaiseStatus( ret );
380 /***********************************************************************
381 * MakeCriticalSectionGlobal (KERNEL32.@)
383 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
385 /* let's assume that only one thread at a time will try to do this */
386 HANDLE sem = crit->LockSemaphore;
387 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
388 crit->LockSemaphore = ConvertToGlobalHandle( sem );
389 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
390 crit->DebugInfo = NULL;
394 /***********************************************************************
395 * ReinitializeCriticalSection (KERNEL32.@)
397 * Initialise an already used critical section.
400 * crit [O] Critical section to initialise.
405 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
407 if ( !crit->LockSemaphore )
408 RtlInitializeCriticalSection( crit );
412 /***********************************************************************
413 * UninitializeCriticalSection (KERNEL32.@)
415 * UnInitialise a critical section after use.
418 * crit [O] Critical section to uninitialise (destroy).
423 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
425 RtlDeleteCriticalSection( crit );
429 /***********************************************************************
430 * CreateEventA (KERNEL32.@)
432 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
433 BOOL initial_state, LPCSTR name )
435 WCHAR buffer[MAX_PATH];
437 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
439 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
441 SetLastError( ERROR_FILENAME_EXCED_RANGE );
444 return CreateEventW( sa, manual_reset, initial_state, buffer );
448 /***********************************************************************
449 * CreateEventW (KERNEL32.@)
451 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
452 BOOL initial_state, LPCWSTR name )
455 UNICODE_STRING nameW;
456 OBJECT_ATTRIBUTES attr;
459 /* one buggy program needs this
460 * ("Van Dale Groot woordenboek der Nederlandse taal")
462 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
464 ERR("Bad security attributes pointer %p\n",sa);
465 SetLastError( ERROR_INVALID_PARAMETER);
469 attr.Length = sizeof(attr);
470 attr.RootDirectory = 0;
471 attr.ObjectName = NULL;
472 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
473 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
474 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
475 attr.SecurityQualityOfService = NULL;
478 RtlInitUnicodeString( &nameW, name );
479 attr.ObjectName = &nameW;
480 attr.RootDirectory = get_BaseNamedObjects_handle();
483 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
484 if (status == STATUS_OBJECT_NAME_EXISTS)
485 SetLastError( ERROR_ALREADY_EXISTS );
487 SetLastError( RtlNtStatusToDosError(status) );
492 /***********************************************************************
493 * CreateW32Event (KERNEL.457)
495 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
497 return CreateEventW( NULL, manual_reset, initial_state, NULL );
501 /***********************************************************************
502 * OpenEventA (KERNEL32.@)
504 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
506 WCHAR buffer[MAX_PATH];
508 if (!name) return OpenEventW( access, inherit, NULL );
510 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
512 SetLastError( ERROR_FILENAME_EXCED_RANGE );
515 return OpenEventW( access, inherit, buffer );
519 /***********************************************************************
520 * OpenEventW (KERNEL32.@)
522 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
525 UNICODE_STRING nameW;
526 OBJECT_ATTRIBUTES attr;
529 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
531 attr.Length = sizeof(attr);
532 attr.RootDirectory = 0;
533 attr.ObjectName = NULL;
534 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
535 attr.SecurityDescriptor = NULL;
536 attr.SecurityQualityOfService = NULL;
539 RtlInitUnicodeString( &nameW, name );
540 attr.ObjectName = &nameW;
541 attr.RootDirectory = get_BaseNamedObjects_handle();
544 status = NtOpenEvent( &ret, access, &attr );
545 if (status != STATUS_SUCCESS)
547 SetLastError( RtlNtStatusToDosError(status) );
553 /***********************************************************************
554 * PulseEvent (KERNEL32.@)
556 BOOL WINAPI PulseEvent( HANDLE handle )
560 if ((status = NtPulseEvent( handle, NULL )))
561 SetLastError( RtlNtStatusToDosError(status) );
566 /***********************************************************************
567 * SetW32Event (KERNEL.458)
568 * SetEvent (KERNEL32.@)
570 BOOL WINAPI SetEvent( HANDLE handle )
574 if ((status = NtSetEvent( handle, NULL )))
575 SetLastError( RtlNtStatusToDosError(status) );
580 /***********************************************************************
581 * ResetW32Event (KERNEL.459)
582 * ResetEvent (KERNEL32.@)
584 BOOL WINAPI ResetEvent( HANDLE handle )
588 if ((status = NtResetEvent( handle, NULL )))
589 SetLastError( RtlNtStatusToDosError(status) );
594 /***********************************************************************
595 * NOTE: The Win95 VWin32_Event routines given below are really low-level
596 * routines implemented directly by VWin32. The user-mode libraries
597 * implement Win32 synchronisation routines on top of these low-level
598 * primitives. We do it the other way around here :-)
601 /***********************************************************************
602 * VWin32_EventCreate (KERNEL.442)
604 HANDLE WINAPI VWin32_EventCreate(VOID)
606 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
607 return ConvertToGlobalHandle( hEvent );
610 /***********************************************************************
611 * VWin32_EventDestroy (KERNEL.443)
613 VOID WINAPI VWin32_EventDestroy(HANDLE event)
615 CloseHandle( event );
618 /***********************************************************************
619 * VWin32_EventWait (KERNEL.450)
621 VOID WINAPI VWin32_EventWait(HANDLE event)
625 ReleaseThunkLock( &mutex_count );
626 WaitForSingleObject( event, INFINITE );
627 RestoreThunkLock( mutex_count );
630 /***********************************************************************
631 * VWin32_EventSet (KERNEL.451)
632 * KERNEL_479 (KERNEL.479)
634 VOID WINAPI VWin32_EventSet(HANDLE event)
641 /***********************************************************************
642 * CreateMutexA (KERNEL32.@)
644 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
646 WCHAR buffer[MAX_PATH];
648 if (!name) return CreateMutexW( sa, owner, NULL );
650 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
652 SetLastError( ERROR_FILENAME_EXCED_RANGE );
655 return CreateMutexW( sa, owner, buffer );
659 /***********************************************************************
660 * CreateMutexW (KERNEL32.@)
662 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
665 UNICODE_STRING nameW;
666 OBJECT_ATTRIBUTES attr;
669 attr.Length = sizeof(attr);
670 attr.RootDirectory = 0;
671 attr.ObjectName = NULL;
672 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
673 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
674 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
675 attr.SecurityQualityOfService = NULL;
678 RtlInitUnicodeString( &nameW, name );
679 attr.ObjectName = &nameW;
680 attr.RootDirectory = get_BaseNamedObjects_handle();
683 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
684 if (status == STATUS_OBJECT_NAME_EXISTS)
685 SetLastError( ERROR_ALREADY_EXISTS );
687 SetLastError( RtlNtStatusToDosError(status) );
692 /***********************************************************************
693 * OpenMutexA (KERNEL32.@)
695 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
697 WCHAR buffer[MAX_PATH];
699 if (!name) return OpenMutexW( access, inherit, NULL );
701 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
703 SetLastError( ERROR_FILENAME_EXCED_RANGE );
706 return OpenMutexW( access, inherit, buffer );
710 /***********************************************************************
711 * OpenMutexW (KERNEL32.@)
713 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
716 UNICODE_STRING nameW;
717 OBJECT_ATTRIBUTES attr;
720 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
722 attr.Length = sizeof(attr);
723 attr.RootDirectory = 0;
724 attr.ObjectName = NULL;
725 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
726 attr.SecurityDescriptor = NULL;
727 attr.SecurityQualityOfService = NULL;
730 RtlInitUnicodeString( &nameW, name );
731 attr.ObjectName = &nameW;
732 attr.RootDirectory = get_BaseNamedObjects_handle();
735 status = NtOpenMutant( &ret, access, &attr );
736 if (status != STATUS_SUCCESS)
738 SetLastError( RtlNtStatusToDosError(status) );
745 /***********************************************************************
746 * ReleaseMutex (KERNEL32.@)
748 BOOL WINAPI ReleaseMutex( HANDLE handle )
752 status = NtReleaseMutant(handle, NULL);
753 if (status != STATUS_SUCCESS)
755 SetLastError( RtlNtStatusToDosError(status) );
767 /***********************************************************************
768 * CreateSemaphoreA (KERNEL32.@)
770 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
772 WCHAR buffer[MAX_PATH];
774 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
776 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
778 SetLastError( ERROR_FILENAME_EXCED_RANGE );
781 return CreateSemaphoreW( sa, initial, max, buffer );
785 /***********************************************************************
786 * CreateSemaphoreW (KERNEL32.@)
788 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
789 LONG max, LPCWSTR name )
792 UNICODE_STRING nameW;
793 OBJECT_ATTRIBUTES attr;
796 attr.Length = sizeof(attr);
797 attr.RootDirectory = 0;
798 attr.ObjectName = NULL;
799 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
800 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
801 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
802 attr.SecurityQualityOfService = NULL;
805 RtlInitUnicodeString( &nameW, name );
806 attr.ObjectName = &nameW;
807 attr.RootDirectory = get_BaseNamedObjects_handle();
810 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
811 if (status == STATUS_OBJECT_NAME_EXISTS)
812 SetLastError( ERROR_ALREADY_EXISTS );
814 SetLastError( RtlNtStatusToDosError(status) );
819 /***********************************************************************
820 * OpenSemaphoreA (KERNEL32.@)
822 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
824 WCHAR buffer[MAX_PATH];
826 if (!name) return OpenSemaphoreW( access, inherit, NULL );
828 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
830 SetLastError( ERROR_FILENAME_EXCED_RANGE );
833 return OpenSemaphoreW( access, inherit, buffer );
837 /***********************************************************************
838 * OpenSemaphoreW (KERNEL32.@)
840 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
843 UNICODE_STRING nameW;
844 OBJECT_ATTRIBUTES attr;
847 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
849 attr.Length = sizeof(attr);
850 attr.RootDirectory = 0;
851 attr.ObjectName = NULL;
852 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
853 attr.SecurityDescriptor = NULL;
854 attr.SecurityQualityOfService = NULL;
857 RtlInitUnicodeString( &nameW, name );
858 attr.ObjectName = &nameW;
859 attr.RootDirectory = get_BaseNamedObjects_handle();
862 status = NtOpenSemaphore( &ret, access, &attr );
863 if (status != STATUS_SUCCESS)
865 SetLastError( RtlNtStatusToDosError(status) );
872 /***********************************************************************
873 * ReleaseSemaphore (KERNEL32.@)
875 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
877 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
878 if (status) SetLastError( RtlNtStatusToDosError(status) );
888 /***********************************************************************
889 * CreateWaitableTimerA (KERNEL32.@)
891 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
893 WCHAR buffer[MAX_PATH];
895 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
897 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
899 SetLastError( ERROR_FILENAME_EXCED_RANGE );
902 return CreateWaitableTimerW( sa, manual, buffer );
906 /***********************************************************************
907 * CreateWaitableTimerW (KERNEL32.@)
909 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
913 UNICODE_STRING nameW;
914 OBJECT_ATTRIBUTES attr;
916 attr.Length = sizeof(attr);
917 attr.RootDirectory = 0;
918 attr.ObjectName = NULL;
919 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
920 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
921 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
922 attr.SecurityQualityOfService = NULL;
925 RtlInitUnicodeString( &nameW, name );
926 attr.ObjectName = &nameW;
927 attr.RootDirectory = get_BaseNamedObjects_handle();
930 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
931 manual ? NotificationTimer : SynchronizationTimer);
932 if (status == STATUS_OBJECT_NAME_EXISTS)
933 SetLastError( ERROR_ALREADY_EXISTS );
935 SetLastError( RtlNtStatusToDosError(status) );
940 /***********************************************************************
941 * OpenWaitableTimerA (KERNEL32.@)
943 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
945 WCHAR buffer[MAX_PATH];
947 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
949 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
951 SetLastError( ERROR_FILENAME_EXCED_RANGE );
954 return OpenWaitableTimerW( access, inherit, buffer );
958 /***********************************************************************
959 * OpenWaitableTimerW (KERNEL32.@)
961 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
964 UNICODE_STRING nameW;
965 OBJECT_ATTRIBUTES attr;
968 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
970 attr.Length = sizeof(attr);
971 attr.RootDirectory = 0;
972 attr.ObjectName = NULL;
973 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
974 attr.SecurityDescriptor = NULL;
975 attr.SecurityQualityOfService = NULL;
978 RtlInitUnicodeString( &nameW, name );
979 attr.ObjectName = &nameW;
980 attr.RootDirectory = get_BaseNamedObjects_handle();
983 status = NtOpenTimer(&handle, access, &attr);
984 if (status != STATUS_SUCCESS)
986 SetLastError( RtlNtStatusToDosError(status) );
993 /***********************************************************************
994 * SetWaitableTimer (KERNEL32.@)
996 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
997 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
999 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
1000 arg, resume, period, NULL);
1002 if (status != STATUS_SUCCESS)
1004 SetLastError( RtlNtStatusToDosError(status) );
1005 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
1011 /***********************************************************************
1012 * CancelWaitableTimer (KERNEL32.@)
1014 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1018 status = NtCancelTimer(handle, NULL);
1019 if (status != STATUS_SUCCESS)
1021 SetLastError( RtlNtStatusToDosError(status) );
1028 /***********************************************************************
1029 * CreateTimerQueue (KERNEL32.@)
1031 HANDLE WINAPI CreateTimerQueue(void)
1034 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1039 /***********************************************************************
1040 * DeleteTimerQueueEx (KERNEL32.@)
1042 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1044 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
1045 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1049 /***********************************************************************
1050 * CreateTimerQueueTimer (KERNEL32.@)
1052 * Creates a timer-queue timer. This timer expires at the specified due
1053 * time (in ms), then after every specified period (in ms). When the timer
1054 * expires, the callback function is called.
1057 * nonzero on success or zero on faillure
1062 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1063 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1064 DWORD DueTime, DWORD Period, ULONG Flags )
1067 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1071 /***********************************************************************
1072 * DeleteTimerQueueTimer (KERNEL32.@)
1074 * Cancels a timer-queue timer.
1077 * nonzero on success or zero on faillure
1082 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1083 HANDLE CompletionEvent )
1086 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1096 /***********************************************************************
1097 * CreateNamedPipeA (KERNEL32.@)
1099 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1100 DWORD dwPipeMode, DWORD nMaxInstances,
1101 DWORD nOutBufferSize, DWORD nInBufferSize,
1102 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1104 WCHAR buffer[MAX_PATH];
1106 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1107 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1109 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1111 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1112 return INVALID_HANDLE_VALUE;
1114 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1115 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1119 /***********************************************************************
1120 * CreateNamedPipeW (KERNEL32.@)
1122 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1123 DWORD dwPipeMode, DWORD nMaxInstances,
1124 DWORD nOutBufferSize, DWORD nInBufferSize,
1125 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1128 UNICODE_STRING nt_name;
1129 OBJECT_ATTRIBUTES attr;
1131 BOOLEAN pipe_type, read_mode, non_block;
1133 IO_STATUS_BLOCK iosb;
1134 LARGE_INTEGER timeout;
1136 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1137 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1138 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1140 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1142 SetLastError( ERROR_PATH_NOT_FOUND );
1143 return INVALID_HANDLE_VALUE;
1145 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1147 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1148 RtlFreeUnicodeString( &nt_name );
1149 return INVALID_HANDLE_VALUE;
1152 attr.Length = sizeof(attr);
1153 attr.RootDirectory = 0;
1154 attr.ObjectName = &nt_name;
1155 attr.Attributes = OBJ_CASE_INSENSITIVE |
1156 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1157 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1158 attr.SecurityQualityOfService = NULL;
1161 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1162 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1163 if ((dwOpenMode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
1164 options |= FILE_PIPE_FULL_DUPLEX;
1165 else if (dwOpenMode & PIPE_ACCESS_INBOUND) options |= FILE_PIPE_INBOUND;
1166 else if (dwOpenMode & PIPE_ACCESS_OUTBOUND) options |= FILE_PIPE_OUTBOUND;
1167 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1168 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1169 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1170 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0UL;
1172 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1176 status = NtCreateNamedPipeFile(&handle, GENERIC_READ|GENERIC_WRITE, &attr, &iosb,
1177 0, FILE_OVERWRITE_IF, options, pipe_type,
1178 read_mode, non_block, nMaxInstances,
1179 nInBufferSize, nOutBufferSize, &timeout);
1181 RtlFreeUnicodeString( &nt_name );
1184 handle = INVALID_HANDLE_VALUE;
1185 SetLastError( RtlNtStatusToDosError(status) );
1191 /***********************************************************************
1192 * PeekNamedPipe (KERNEL32.@)
1194 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1195 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1198 int avail=0, fd, ret, flags;
1200 TRACE("(%p,%p,%u,%p,%p,%p)\n", hPipe, lpvBuffer, cbBuffer, lpcbRead, lpcbAvail, lpcbMessage);
1202 ret = wine_server_handle_to_fd( hPipe, FILE_READ_DATA, &fd, &flags );
1205 SetLastError( RtlNtStatusToDosError(ret) );
1208 if (flags & FD_FLAG_RECV_SHUTDOWN)
1210 wine_server_release_fd( hPipe, fd );
1211 SetLastError ( ERROR_PIPE_NOT_CONNECTED );
1215 if (ioctl(fd,FIONREAD, &avail ) != 0)
1217 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1218 wine_server_release_fd( hPipe, fd );
1221 if (!avail) /* check for closed pipe */
1223 struct pollfd pollfd;
1225 pollfd.events = POLLIN;
1227 switch (poll( &pollfd, 1, 0 ))
1231 case 1: /* got something */
1232 if (!(pollfd.revents & (POLLHUP | POLLERR))) break;
1233 TRACE("POLLHUP | POLLERR\n");
1236 wine_server_release_fd( hPipe, fd );
1237 SetLastError(ERROR_BROKEN_PIPE);
1241 TRACE(" 0x%08x bytes available\n", avail );
1247 if (avail && lpvBuffer && cbBuffer)
1249 int readbytes = (avail < cbBuffer) ? avail : cbBuffer;
1250 readbytes = recv(fd, lpvBuffer, readbytes, MSG_PEEK);
1253 WARN("failed to peek socket (%d)\n", errno);
1257 *lpcbRead = readbytes;
1259 wine_server_release_fd( hPipe, fd );
1261 #endif /* defined(FIONREAD) */
1263 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1264 FIXME("function not implemented\n");
1268 /***********************************************************************
1269 * WaitNamedPipeA (KERNEL32.@)
1271 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1273 WCHAR buffer[MAX_PATH];
1275 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1277 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1279 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1282 return WaitNamedPipeW( buffer, nTimeOut );
1286 /***********************************************************************
1287 * WaitNamedPipeW (KERNEL32.@)
1289 * Waits for a named pipe instance to become available
1292 * name [I] Pointer to a named pipe name to wait for
1293 * nTimeOut [I] How long to wait in ms
1296 * TRUE: Success, named pipe can be opened with CreateFile
1297 * FALSE: Failure, GetLastError can be called for further details
1299 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1301 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1303 UNICODE_STRING nt_name, pipe_dev_name;
1304 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1305 IO_STATUS_BLOCK iosb;
1306 OBJECT_ATTRIBUTES attr;
1310 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1312 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1315 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1316 nt_name.Length < sizeof(leadin) ||
1317 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR) != 0))
1319 RtlFreeUnicodeString( &nt_name );
1320 SetLastError( ERROR_PATH_NOT_FOUND );
1324 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1325 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1327 RtlFreeUnicodeString( &nt_name );
1328 SetLastError( ERROR_OUTOFMEMORY );
1332 pipe_dev_name.Buffer = nt_name.Buffer;
1333 pipe_dev_name.Length = sizeof(leadin);
1334 pipe_dev_name.MaximumLength = sizeof(leadin);
1335 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1336 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1337 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1338 FILE_SYNCHRONOUS_IO_NONALERT);
1339 if (status != ERROR_SUCCESS)
1341 SetLastError( ERROR_PATH_NOT_FOUND );
1345 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1346 pipe_wait->Timeout.QuadPart = nTimeOut * -10000L;
1347 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1348 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1349 pipe_wait->NameLength);
1350 RtlFreeUnicodeString( &nt_name );
1352 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1353 pipe_wait, sz_pipe_wait, NULL, 0 );
1355 HeapFree( GetProcessHeap(), 0, pipe_wait );
1356 NtClose( pipe_dev );
1358 if(status != STATUS_SUCCESS)
1360 SetLastError(RtlNtStatusToDosError(status));
1368 /***********************************************************************
1369 * ConnectNamedPipe (KERNEL32.@)
1371 * Connects to a named pipe
1374 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1375 * overlapped: Optional OVERLAPPED struct
1379 * FALSE: Failure, GetLastError can be called for further details
1381 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1384 IO_STATUS_BLOCK status_block;
1386 TRACE("(%p,%p)\n", hPipe, overlapped);
1389 overlapped->Internal = STATUS_PENDING;
1391 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, NULL,
1392 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1393 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1395 if (status == STATUS_SUCCESS) return TRUE;
1396 SetLastError( RtlNtStatusToDosError(status) );
1400 /***********************************************************************
1401 * DisconnectNamedPipe (KERNEL32.@)
1403 * Disconnects from a named pipe
1406 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1410 * FALSE: Failure, GetLastError can be called for further details
1412 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1415 IO_STATUS_BLOCK io_block;
1417 TRACE("(%p)\n",hPipe);
1419 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1421 if (status == STATUS_SUCCESS) return TRUE;
1422 SetLastError( RtlNtStatusToDosError(status) );
1426 /***********************************************************************
1427 * TransactNamedPipe (KERNEL32.@)
1430 * should be done as a single operation in the wineserver or kernel
1432 BOOL WINAPI TransactNamedPipe(
1433 HANDLE handle, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1434 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1439 TRACE("%p %p %d %p %d %p %p\n",
1440 handle, lpInput, dwInputSize, lpOutput,
1441 dwOutputSize, lpBytesRead, lpOverlapped);
1445 FIXME("Doesn't support overlapped operation as yet\n");
1449 r = WriteFile(handle, lpOutput, dwOutputSize, &count, NULL);
1451 r = ReadFile(handle, lpInput, dwInputSize, lpBytesRead, NULL);
1456 /***********************************************************************
1457 * GetNamedPipeInfo (KERNEL32.@)
1459 BOOL WINAPI GetNamedPipeInfo(
1460 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1461 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1463 FILE_PIPE_LOCAL_INFORMATION fpli;
1464 IO_STATUS_BLOCK iosb;
1467 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1468 FilePipeLocalInformation);
1471 SetLastError( RtlNtStatusToDosError(status) );
1477 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1478 PIPE_SERVER_END : PIPE_CLIENT_END;
1479 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1480 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1483 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1484 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1485 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1490 /***********************************************************************
1491 * GetNamedPipeHandleStateA (KERNEL32.@)
1493 BOOL WINAPI GetNamedPipeHandleStateA(
1494 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1495 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1496 LPSTR lpUsername, DWORD nUsernameMaxSize)
1498 FIXME("%p %p %p %p %p %p %d\n",
1499 hNamedPipe, lpState, lpCurInstances,
1500 lpMaxCollectionCount, lpCollectDataTimeout,
1501 lpUsername, nUsernameMaxSize);
1506 /***********************************************************************
1507 * GetNamedPipeHandleStateW (KERNEL32.@)
1509 BOOL WINAPI GetNamedPipeHandleStateW(
1510 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1511 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1512 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1514 FIXME("%p %p %p %p %p %p %d\n",
1515 hNamedPipe, lpState, lpCurInstances,
1516 lpMaxCollectionCount, lpCollectDataTimeout,
1517 lpUsername, nUsernameMaxSize);
1522 /***********************************************************************
1523 * SetNamedPipeHandleState (KERNEL32.@)
1525 BOOL WINAPI SetNamedPipeHandleState(
1526 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1527 LPDWORD lpCollectDataTimeout)
1529 /* should be a fixme, but this function is called a lot by the RPC
1530 * runtime, and it slows down InstallShield a fair bit. */
1531 WARN("stub: %p %p/%d %p %p\n",
1532 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1536 /***********************************************************************
1537 * CallNamedPipeA (KERNEL32.@)
1539 BOOL WINAPI CallNamedPipeA(
1540 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1541 LPVOID lpOutput, DWORD dwOutputSize,
1542 LPDWORD lpBytesRead, DWORD nTimeout)
1548 TRACE("%s %p %d %p %d %p %d\n",
1549 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1550 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1552 if( lpNamedPipeName )
1554 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1555 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1556 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1558 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1559 dwOutputSize, lpBytesRead, nTimeout );
1560 if( lpNamedPipeName )
1561 HeapFree( GetProcessHeap(), 0, str );
1566 /***********************************************************************
1567 * CallNamedPipeW (KERNEL32.@)
1569 BOOL WINAPI CallNamedPipeW(
1570 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1571 LPVOID lpOutput, DWORD lpOutputSize,
1572 LPDWORD lpBytesRead, DWORD nTimeout)
1574 FIXME("%s %p %d %p %d %p %d\n",
1575 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1576 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1580 /******************************************************************
1581 * CreatePipe (KERNEL32.@)
1584 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1585 LPSECURITY_ATTRIBUTES sa, DWORD size )
1587 static unsigned index /* = 0 */;
1590 unsigned in_index = index;
1591 UNICODE_STRING nt_name;
1592 OBJECT_ATTRIBUTES attr;
1594 IO_STATUS_BLOCK iosb;
1595 LARGE_INTEGER timeout;
1597 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1599 attr.Length = sizeof(attr);
1600 attr.RootDirectory = 0;
1601 attr.ObjectName = &nt_name;
1602 attr.Attributes = OBJ_CASE_INSENSITIVE |
1603 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1604 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1605 attr.SecurityQualityOfService = NULL;
1607 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1608 /* generate a unique pipe name (system wide) */
1611 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1612 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1613 'u','.','%','0','8','u','\0' };
1615 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1616 GetCurrentProcessId(), ++index);
1617 RtlInitUnicodeString(&nt_name, name);
1618 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1619 0, FILE_OVERWRITE_IF,
1620 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1621 FALSE, FALSE, FALSE,
1622 1, size, size, &timeout);
1625 SetLastError( RtlNtStatusToDosError(status) );
1626 hr = INVALID_HANDLE_VALUE;
1628 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1629 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1630 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1632 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1633 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1637 SetLastError( RtlNtStatusToDosError(status) );
1648 /******************************************************************************
1649 * CreateMailslotA [KERNEL32.@]
1651 * See CreatMailslotW.
1653 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1654 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1660 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1661 nMaxMessageSize, lReadTimeout, sa);
1665 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1666 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1667 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1670 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1672 HeapFree( GetProcessHeap(), 0, name );
1678 /******************************************************************************
1679 * CreateMailslotW [KERNEL32.@]
1681 * Create a mailslot with specified name.
1684 * lpName [I] Pointer to string for mailslot name
1685 * nMaxMessageSize [I] Maximum message size
1686 * lReadTimeout [I] Milliseconds before read time-out
1687 * sa [I] Pointer to security structure
1690 * Success: Handle to mailslot
1691 * Failure: INVALID_HANDLE_VALUE
1693 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1694 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1696 HANDLE handle = INVALID_HANDLE_VALUE;
1697 OBJECT_ATTRIBUTES attr;
1698 UNICODE_STRING nameW;
1699 LARGE_INTEGER timeout;
1700 IO_STATUS_BLOCK iosb;
1703 TRACE("%s %d %d %p\n", debugstr_w(lpName),
1704 nMaxMessageSize, lReadTimeout, sa);
1706 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1708 SetLastError( ERROR_PATH_NOT_FOUND );
1709 return INVALID_HANDLE_VALUE;
1712 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1714 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1715 RtlFreeUnicodeString( &nameW );
1716 return INVALID_HANDLE_VALUE;
1719 attr.Length = sizeof(attr);
1720 attr.RootDirectory = 0;
1721 attr.Attributes = OBJ_CASE_INSENSITIVE;
1722 attr.ObjectName = &nameW;
1723 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1724 attr.SecurityQualityOfService = NULL;
1726 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1727 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1729 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1731 status = NtCreateMailslotFile( &handle, GENERIC_READ | GENERIC_WRITE, &attr,
1732 &iosb, 0, 0, nMaxMessageSize, &timeout );
1735 SetLastError( RtlNtStatusToDosError(status) );
1736 handle = INVALID_HANDLE_VALUE;
1739 RtlFreeUnicodeString( &nameW );
1744 /******************************************************************************
1745 * GetMailslotInfo [KERNEL32.@]
1747 * Retrieve information about a mailslot.
1750 * hMailslot [I] Mailslot handle
1751 * lpMaxMessageSize [O] Address of maximum message size
1752 * lpNextSize [O] Address of size of next message
1753 * lpMessageCount [O] Address of number of messages
1754 * lpReadTimeout [O] Address of read time-out
1760 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1761 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1762 LPDWORD lpReadTimeout )
1764 FILE_MAILSLOT_QUERY_INFORMATION info;
1765 IO_STATUS_BLOCK iosb;
1768 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1769 lpNextSize, lpMessageCount, lpReadTimeout);
1771 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1772 FileMailslotQueryInformation );
1774 if( status != STATUS_SUCCESS )
1776 SetLastError( RtlNtStatusToDosError(status) );
1780 if( lpMaxMessageSize )
1781 *lpMaxMessageSize = info.MaximumMessageSize;
1783 *lpNextSize = info.NextMessageSize;
1784 if( lpMessageCount )
1785 *lpMessageCount = info.MessagesAvailable;
1787 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1793 /******************************************************************************
1794 * SetMailslotInfo [KERNEL32.@]
1796 * Set the read timeout of a mailslot.
1799 * hMailslot [I] Mailslot handle
1800 * dwReadTimeout [I] Timeout in milliseconds.
1806 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1808 FILE_MAILSLOT_SET_INFORMATION info;
1809 IO_STATUS_BLOCK iosb;
1812 TRACE("%p %d\n", hMailslot, dwReadTimeout);
1814 info.ReadTimeout.QuadPart = dwReadTimeout * -10000;
1815 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1816 FileMailslotSetInformation );
1817 if( status != STATUS_SUCCESS )
1819 SetLastError( RtlNtStatusToDosError(status) );
1826 /******************************************************************************
1827 * CreateIoCompletionPort (KERNEL32.@)
1829 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1830 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1832 FIXME("(%p, %p, %08lx, %08x): stub.\n",
1833 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1834 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1839 /******************************************************************************
1840 * GetQueuedCompletionStatus (KERNEL32.@)
1842 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1843 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1844 DWORD dwMilliseconds )
1846 FIXME("(%p,%p,%p,%p,%d), stub!\n",
1847 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1848 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1852 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
1853 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
1855 FIXME("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
1856 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1860 /******************************************************************************
1861 * CreateJobObjectW (KERNEL32.@)
1863 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1865 FIXME("%p %s\n", attr, debugstr_w(name) );
1866 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1870 /******************************************************************************
1871 * CreateJobObjectA (KERNEL32.@)
1873 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1879 TRACE("%p %s\n", attr, debugstr_a(name) );
1883 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1884 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1887 SetLastError( ERROR_OUTOFMEMORY );
1890 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1893 r = CreateJobObjectW( attr, str );
1895 HeapFree( GetProcessHeap(), 0, str );
1900 /******************************************************************************
1901 * AssignProcessToJobObject (KERNEL32.@)
1903 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1905 FIXME("%p %p\n", hJob, hProcess);
1911 /***********************************************************************
1912 * InterlockedCompareExchange (KERNEL32.@)
1914 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1915 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1916 "movl 12(%esp),%eax\n\t"
1917 "movl 8(%esp),%ecx\n\t"
1918 "movl 4(%esp),%edx\n\t"
1919 "lock; cmpxchgl %ecx,(%edx)\n\t"
1922 /***********************************************************************
1923 * InterlockedExchange (KERNEL32.@)
1925 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1926 __ASM_GLOBAL_FUNC(InterlockedExchange,
1927 "movl 8(%esp),%eax\n\t"
1928 "movl 4(%esp),%edx\n\t"
1929 "lock; xchgl %eax,(%edx)\n\t"
1932 /***********************************************************************
1933 * InterlockedExchangeAdd (KERNEL32.@)
1935 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1936 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1937 "movl 8(%esp),%eax\n\t"
1938 "movl 4(%esp),%edx\n\t"
1939 "lock; xaddl %eax,(%edx)\n\t"
1942 /***********************************************************************
1943 * InterlockedIncrement (KERNEL32.@)
1945 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1946 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1947 "movl 4(%esp),%edx\n\t"
1949 "lock; xaddl %eax,(%edx)\n\t"
1953 /***********************************************************************
1954 * InterlockedDecrement (KERNEL32.@)
1956 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1957 "movl 4(%esp),%edx\n\t"
1959 "lock; xaddl %eax,(%edx)\n\t"
1963 #else /* __i386__ */
1965 /***********************************************************************
1966 * InterlockedCompareExchange (KERNEL32.@)
1968 * Atomically swap one value with another.
1971 * dest [I/O] The value to replace
1972 * xchq [I] The value to be swapped
1973 * compare [I] The value to compare to dest
1976 * The resulting value of dest.
1979 * dest is updated only if it is equal to compare, otherwise no swap is done.
1981 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
1983 return interlocked_cmpxchg( (int *)dest, xchg, compare );
1986 /***********************************************************************
1987 * InterlockedExchange (KERNEL32.@)
1989 * Atomically swap one value with another.
1992 * dest [I/O] The value to replace
1993 * val [I] The value to be swapped
1996 * The resulting value of dest.
1998 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
2000 return interlocked_xchg( (int *)dest, val );
2003 /***********************************************************************
2004 * InterlockedExchangeAdd (KERNEL32.@)
2006 * Atomically add one value to another.
2009 * dest [I/O] The value to add to
2010 * incr [I] The value to be added
2013 * The resulting value of dest.
2015 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
2017 return interlocked_xchg_add( (int *)dest, incr );
2020 /***********************************************************************
2021 * InterlockedIncrement (KERNEL32.@)
2023 * Atomically increment a value.
2026 * dest [I/O] The value to increment
2029 * The resulting value of dest.
2031 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
2033 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
2036 /***********************************************************************
2037 * InterlockedDecrement (KERNEL32.@)
2039 * Atomically decrement a value.
2042 * dest [I/O] The value to decrement
2045 * The resulting value of dest.
2047 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
2049 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
2052 #endif /* __i386__ */