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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
55 #include "wine/server.h"
56 #include "wine/unicode.h"
57 #include "wine/winbase16.h"
58 #include "kernel_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(sync);
64 /* check if current version is NT or Win95 */
65 inline static int is_version_nt(void)
67 return !(GetVersion() & 0x80000000);
71 /***********************************************************************
74 VOID WINAPI Sleep( DWORD timeout )
76 SleepEx( timeout, FALSE );
79 /******************************************************************************
80 * SleepEx (KERNEL32.@)
82 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
86 if (timeout == INFINITE) status = NtDelayExecution( alertable, NULL );
91 time.QuadPart = timeout * (ULONGLONG)10000;
92 time.QuadPart = -time.QuadPart;
93 status = NtDelayExecution( alertable, &time );
95 if (status != STATUS_USER_APC) status = STATUS_SUCCESS;
100 /***********************************************************************
101 * SwitchToThread (KERNEL32.@)
103 BOOL WINAPI SwitchToThread(void)
105 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
109 /***********************************************************************
110 * WaitForSingleObject (KERNEL32.@)
112 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
114 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
118 /***********************************************************************
119 * WaitForSingleObjectEx (KERNEL32.@)
121 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
124 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
128 /***********************************************************************
129 * WaitForMultipleObjects (KERNEL32.@)
131 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
132 BOOL wait_all, DWORD timeout )
134 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
138 /***********************************************************************
139 * WaitForMultipleObjectsEx (KERNEL32.@)
141 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
142 BOOL wait_all, DWORD timeout,
146 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
149 if (count >= MAXIMUM_WAIT_OBJECTS)
151 SetLastError(ERROR_INVALID_PARAMETER);
154 for (i = 0; i < count; i++)
156 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
157 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
158 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
159 hloc[i] = GetStdHandle( (DWORD)handles[i] );
161 hloc[i] = handles[i];
163 /* yes, even screen buffer console handles are waitable, and are
164 * handled as a handle to the console itself !!
166 if (is_console_handle(hloc[i]))
168 if (!VerifyConsoleIoHandle(hloc[i]))
172 hloc[i] = GetConsoleInputWaitHandle();
176 if (timeout == INFINITE)
178 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, NULL );
184 time.QuadPart = timeout * (ULONGLONG)10000;
185 time.QuadPart = -time.QuadPart;
186 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, &time );
189 if (HIWORD(status)) /* is it an error code? */
191 SetLastError( RtlNtStatusToDosError(status) );
192 status = WAIT_FAILED;
198 /***********************************************************************
199 * WaitForSingleObject (KERNEL.460)
201 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
203 DWORD retval, mutex_count;
205 ReleaseThunkLock( &mutex_count );
206 retval = WaitForSingleObject( handle, timeout );
207 RestoreThunkLock( mutex_count );
211 /***********************************************************************
212 * WaitForMultipleObjects (KERNEL.461)
214 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
215 BOOL wait_all, DWORD timeout )
217 DWORD retval, mutex_count;
219 ReleaseThunkLock( &mutex_count );
220 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
221 RestoreThunkLock( mutex_count );
225 /***********************************************************************
226 * WaitForMultipleObjectsEx (KERNEL.495)
228 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
229 BOOL wait_all, DWORD timeout, BOOL alertable )
231 DWORD retval, mutex_count;
233 ReleaseThunkLock( &mutex_count );
234 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
235 RestoreThunkLock( mutex_count );
239 /***********************************************************************
240 * RegisterWaitForSingleObject (KERNEL32.@)
242 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
243 WAITORTIMERCALLBACK Callback, PVOID Context,
244 ULONG dwMilliseconds, ULONG dwFlags)
246 FIXME("%p %p %p %p %ld %ld\n",
247 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
251 /***********************************************************************
252 * RegisterWaitForSingleObjectEx (KERNEL32.@)
254 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
255 WAITORTIMERCALLBACK Callback, PVOID Context,
256 ULONG dwMilliseconds, ULONG dwFlags )
258 FIXME("%p %p %p %ld %ld\n",
259 hObject,Callback,Context,dwMilliseconds,dwFlags);
263 /***********************************************************************
264 * UnregisterWait (KERNEL32.@)
266 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
268 FIXME("%p\n",WaitHandle);
272 /***********************************************************************
273 * UnregisterWaitEx (KERNEL32.@)
275 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
277 FIXME("%p %p\n",WaitHandle, CompletionEvent);
281 /***********************************************************************
282 * SignalObjectAndWait (KERNEL32.@)
284 * Allows to atomically signal any of the synchro objects (semaphore,
285 * mutex, event) and wait on another.
287 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
288 DWORD dwMilliseconds, BOOL bAlertable )
291 LARGE_INTEGER timeout, *ptimeout = NULL;
293 TRACE("%p %p %ld %d\n", hObjectToSignal,
294 hObjectToWaitOn, dwMilliseconds, bAlertable);
296 if (dwMilliseconds != INFINITE)
298 timeout.QuadPart = dwMilliseconds * (ULONGLONG)10000;
299 timeout.QuadPart = -timeout.QuadPart;
303 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn,
304 bAlertable, ptimeout );
307 SetLastError( RtlNtStatusToDosError(status) );
308 status = WAIT_FAILED;
313 /***********************************************************************
314 * InitializeCriticalSection (KERNEL32.@)
316 * Initialise a critical section before use.
319 * crit [O] Critical section to initialise.
322 * Nothing. If the function fails an exception is raised.
324 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
326 NTSTATUS ret = RtlInitializeCriticalSection( crit );
327 if (ret) RtlRaiseStatus( ret );
330 /***********************************************************************
331 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
333 * Initialise a critical section with a spin count.
336 * crit [O] Critical section to initialise.
337 * spincount [I] Number of times to spin upon contention.
341 * Failure: Nothing. If the function fails an exception is raised.
344 * spincount is ignored on uni-processor systems.
346 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
348 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
349 if (ret) RtlRaiseStatus( ret );
353 /***********************************************************************
354 * MakeCriticalSectionGlobal (KERNEL32.@)
356 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
358 /* let's assume that only one thread at a time will try to do this */
359 HANDLE sem = crit->LockSemaphore;
360 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
361 crit->LockSemaphore = ConvertToGlobalHandle( sem );
364 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
365 crit->DebugInfo = NULL;
370 /***********************************************************************
371 * ReinitializeCriticalSection (KERNEL32.@)
373 * Initialise an already used critical section.
376 * crit [O] Critical section to initialise.
381 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
383 if ( !crit->LockSemaphore )
384 RtlInitializeCriticalSection( crit );
388 /***********************************************************************
389 * UninitializeCriticalSection (KERNEL32.@)
391 * UnInitialise a critical section after use.
394 * crit [O] Critical section to uninitialise (destroy).
399 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
401 RtlDeleteCriticalSection( crit );
405 /***********************************************************************
406 * CreateEventA (KERNEL32.@)
408 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
409 BOOL initial_state, LPCSTR name )
411 WCHAR buffer[MAX_PATH];
413 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
415 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
417 SetLastError( ERROR_FILENAME_EXCED_RANGE );
420 return CreateEventW( sa, manual_reset, initial_state, buffer );
424 /***********************************************************************
425 * CreateEventW (KERNEL32.@)
427 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
428 BOOL initial_state, LPCWSTR name )
431 UNICODE_STRING nameW;
432 OBJECT_ATTRIBUTES attr;
435 /* one buggy program needs this
436 * ("Van Dale Groot woordenboek der Nederlandse taal")
438 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
440 ERR("Bad security attributes pointer %p\n",sa);
441 SetLastError( ERROR_INVALID_PARAMETER);
445 attr.Length = sizeof(attr);
446 attr.RootDirectory = 0;
447 attr.ObjectName = NULL;
448 attr.Attributes = OBJ_CASE_INSENSITIVE |
449 (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
450 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
451 attr.SecurityQualityOfService = NULL;
454 RtlInitUnicodeString( &nameW, name );
455 attr.ObjectName = &nameW;
458 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
459 SetLastError( RtlNtStatusToDosError(status) );
464 /***********************************************************************
465 * CreateW32Event (KERNEL.457)
467 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
469 return CreateEventW( NULL, manual_reset, initial_state, NULL );
473 /***********************************************************************
474 * OpenEventA (KERNEL32.@)
476 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
478 WCHAR buffer[MAX_PATH];
480 if (!name) return OpenEventW( access, inherit, NULL );
482 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
484 SetLastError( ERROR_FILENAME_EXCED_RANGE );
487 return OpenEventW( access, inherit, buffer );
491 /***********************************************************************
492 * OpenEventW (KERNEL32.@)
494 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
497 UNICODE_STRING nameW;
498 OBJECT_ATTRIBUTES attr;
501 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
503 attr.Length = sizeof(attr);
504 attr.RootDirectory = 0;
505 attr.ObjectName = NULL;
506 attr.Attributes = OBJ_CASE_INSENSITIVE |
507 (inherit) ? OBJ_INHERIT : 0;
508 attr.SecurityDescriptor = NULL;
509 attr.SecurityQualityOfService = NULL;
512 RtlInitUnicodeString( &nameW, name );
513 attr.ObjectName = &nameW;
516 status = NtOpenEvent( &ret, access, &attr );
517 if (status != STATUS_SUCCESS)
519 SetLastError( RtlNtStatusToDosError(status) );
525 /***********************************************************************
526 * PulseEvent (KERNEL32.@)
528 BOOL WINAPI PulseEvent( HANDLE handle )
532 if ((status = NtPulseEvent( handle, NULL )))
533 SetLastError( RtlNtStatusToDosError(status) );
538 /***********************************************************************
539 * SetW32Event (KERNEL.458)
540 * SetEvent (KERNEL32.@)
542 BOOL WINAPI SetEvent( HANDLE handle )
546 if ((status = NtSetEvent( handle, NULL )))
547 SetLastError( RtlNtStatusToDosError(status) );
552 /***********************************************************************
553 * ResetW32Event (KERNEL.459)
554 * ResetEvent (KERNEL32.@)
556 BOOL WINAPI ResetEvent( HANDLE handle )
560 if ((status = NtResetEvent( handle, NULL )))
561 SetLastError( RtlNtStatusToDosError(status) );
566 /***********************************************************************
567 * NOTE: The Win95 VWin32_Event routines given below are really low-level
568 * routines implemented directly by VWin32. The user-mode libraries
569 * implement Win32 synchronisation routines on top of these low-level
570 * primitives. We do it the other way around here :-)
573 /***********************************************************************
574 * VWin32_EventCreate (KERNEL.442)
576 HANDLE WINAPI VWin32_EventCreate(VOID)
578 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
579 return ConvertToGlobalHandle( hEvent );
582 /***********************************************************************
583 * VWin32_EventDestroy (KERNEL.443)
585 VOID WINAPI VWin32_EventDestroy(HANDLE event)
587 CloseHandle( event );
590 /***********************************************************************
591 * VWin32_EventWait (KERNEL.450)
593 VOID WINAPI VWin32_EventWait(HANDLE event)
597 ReleaseThunkLock( &mutex_count );
598 WaitForSingleObject( event, INFINITE );
599 RestoreThunkLock( mutex_count );
602 /***********************************************************************
603 * VWin32_EventSet (KERNEL.451)
604 * KERNEL_479 (KERNEL.479)
606 VOID WINAPI VWin32_EventSet(HANDLE event)
613 /***********************************************************************
614 * CreateMutexA (KERNEL32.@)
616 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
618 WCHAR buffer[MAX_PATH];
620 if (!name) return CreateMutexW( sa, owner, NULL );
622 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
624 SetLastError( ERROR_FILENAME_EXCED_RANGE );
627 return CreateMutexW( sa, owner, buffer );
631 /***********************************************************************
632 * CreateMutexW (KERNEL32.@)
634 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
637 UNICODE_STRING nameW;
638 OBJECT_ATTRIBUTES attr;
641 attr.Length = sizeof(attr);
642 attr.RootDirectory = 0;
643 attr.ObjectName = NULL;
644 attr.Attributes = OBJ_CASE_INSENSITIVE |
645 (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
646 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
647 attr.SecurityQualityOfService = NULL;
650 RtlInitUnicodeString( &nameW, name );
651 attr.ObjectName = &nameW;
654 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
655 SetLastError( RtlNtStatusToDosError(status) );
660 /***********************************************************************
661 * OpenMutexA (KERNEL32.@)
663 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
665 WCHAR buffer[MAX_PATH];
667 if (!name) return OpenMutexW( access, inherit, NULL );
669 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
671 SetLastError( ERROR_FILENAME_EXCED_RANGE );
674 return OpenMutexW( access, inherit, buffer );
678 /***********************************************************************
679 * OpenMutexW (KERNEL32.@)
681 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
684 UNICODE_STRING nameW;
685 OBJECT_ATTRIBUTES attr;
688 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
690 attr.Length = sizeof(attr);
691 attr.RootDirectory = 0;
692 attr.ObjectName = NULL;
693 attr.Attributes = OBJ_CASE_INSENSITIVE |
694 (inherit) ? OBJ_INHERIT : 0;
695 attr.SecurityDescriptor = NULL;
696 attr.SecurityQualityOfService = NULL;
699 RtlInitUnicodeString( &nameW, name );
700 attr.ObjectName = &nameW;
703 status = NtOpenMutant( &ret, access, &attr );
704 if (status != STATUS_SUCCESS)
706 SetLastError( RtlNtStatusToDosError(status) );
713 /***********************************************************************
714 * ReleaseMutex (KERNEL32.@)
716 BOOL WINAPI ReleaseMutex( HANDLE handle )
720 status = NtReleaseMutant(handle, NULL);
721 if (status != STATUS_SUCCESS)
723 SetLastError( RtlNtStatusToDosError(status) );
735 /***********************************************************************
736 * CreateSemaphoreA (KERNEL32.@)
738 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
740 WCHAR buffer[MAX_PATH];
742 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
744 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
746 SetLastError( ERROR_FILENAME_EXCED_RANGE );
749 return CreateSemaphoreW( sa, initial, max, buffer );
753 /***********************************************************************
754 * CreateSemaphoreW (KERNEL32.@)
756 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
757 LONG max, LPCWSTR name )
760 UNICODE_STRING nameW;
761 OBJECT_ATTRIBUTES attr;
764 attr.Length = sizeof(attr);
765 attr.RootDirectory = 0;
766 attr.ObjectName = NULL;
767 attr.Attributes = OBJ_CASE_INSENSITIVE |
768 (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
769 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
770 attr.SecurityQualityOfService = NULL;
773 RtlInitUnicodeString( &nameW, name );
774 attr.ObjectName = &nameW;
777 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
778 SetLastError( RtlNtStatusToDosError(status) );
783 /***********************************************************************
784 * OpenSemaphoreA (KERNEL32.@)
786 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
788 WCHAR buffer[MAX_PATH];
790 if (!name) return OpenSemaphoreW( access, inherit, NULL );
792 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
794 SetLastError( ERROR_FILENAME_EXCED_RANGE );
797 return OpenSemaphoreW( access, inherit, buffer );
801 /***********************************************************************
802 * OpenSemaphoreW (KERNEL32.@)
804 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
807 UNICODE_STRING nameW;
808 OBJECT_ATTRIBUTES attr;
811 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
813 attr.Length = sizeof(attr);
814 attr.RootDirectory = 0;
815 attr.ObjectName = NULL;
816 attr.Attributes = OBJ_CASE_INSENSITIVE |
817 (inherit) ? OBJ_INHERIT : 0;
818 attr.SecurityDescriptor = NULL;
819 attr.SecurityQualityOfService = NULL;
822 RtlInitUnicodeString( &nameW, name );
823 attr.ObjectName = &nameW;
826 status = NtOpenSemaphore( &ret, access, &attr );
827 if (status != STATUS_SUCCESS)
829 SetLastError( RtlNtStatusToDosError(status) );
836 /***********************************************************************
837 * ReleaseSemaphore (KERNEL32.@)
839 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
841 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
842 if (status) SetLastError( RtlNtStatusToDosError(status) );
852 /***********************************************************************
853 * CreateWaitableTimerA (KERNEL32.@)
855 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
857 WCHAR buffer[MAX_PATH];
859 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
861 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
863 SetLastError( ERROR_FILENAME_EXCED_RANGE );
866 return CreateWaitableTimerW( sa, manual, buffer );
870 /***********************************************************************
871 * CreateWaitableTimerW (KERNEL32.@)
873 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
877 UNICODE_STRING nameW;
878 OBJECT_ATTRIBUTES attr;
880 attr.Length = sizeof(attr);
881 attr.RootDirectory = 0;
882 attr.ObjectName = NULL;
883 attr.Attributes = OBJ_CASE_INSENSITIVE |
884 (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
885 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
886 attr.SecurityQualityOfService = NULL;
889 RtlInitUnicodeString( &nameW, name );
890 attr.ObjectName = &nameW;
893 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
894 manual ? NotificationTimer : SynchronizationTimer);
895 SetLastError( RtlNtStatusToDosError(status) );
900 /***********************************************************************
901 * OpenWaitableTimerA (KERNEL32.@)
903 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
905 WCHAR buffer[MAX_PATH];
907 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
909 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
911 SetLastError( ERROR_FILENAME_EXCED_RANGE );
914 return OpenWaitableTimerW( access, inherit, buffer );
918 /***********************************************************************
919 * OpenWaitableTimerW (KERNEL32.@)
921 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
924 UNICODE_STRING nameW;
925 OBJECT_ATTRIBUTES attr;
928 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
930 attr.Length = sizeof(attr);
931 attr.RootDirectory = 0;
932 attr.ObjectName = NULL;
933 attr.Attributes = OBJ_CASE_INSENSITIVE |
934 (inherit) ? OBJ_INHERIT : 0;
935 attr.SecurityDescriptor = NULL;
936 attr.SecurityQualityOfService = NULL;
939 RtlInitUnicodeString( &nameW, name );
940 attr.ObjectName = &nameW;
943 status = NtOpenTimer(&handle, access, &attr);
944 if (status != STATUS_SUCCESS)
946 SetLastError( RtlNtStatusToDosError(status) );
953 /***********************************************************************
954 * SetWaitableTimer (KERNEL32.@)
956 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
957 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
959 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
960 arg, resume, period, NULL);
962 if (status != STATUS_SUCCESS)
964 SetLastError( RtlNtStatusToDosError(status) );
965 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
971 /***********************************************************************
972 * CancelWaitableTimer (KERNEL32.@)
974 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
978 status = NtCancelTimer(handle, NULL);
979 if (status != STATUS_SUCCESS)
981 SetLastError( RtlNtStatusToDosError(status) );
988 /***********************************************************************
989 * CreateTimerQueue (KERNEL32.@)
991 HANDLE WINAPI CreateTimerQueue(void)
994 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
999 /***********************************************************************
1000 * DeleteTimerQueueEx (KERNEL32.@)
1002 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1004 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
1005 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1009 /***********************************************************************
1010 * CreateTimerQueueTimer (KERNEL32.@)
1012 * Creates a timer-queue timer. This timer expires at the specified due
1013 * time (in ms), then after every specified period (in ms). When the timer
1014 * expires, the callback function is called.
1017 * nonzero on success or zero on faillure
1022 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1023 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1024 DWORD DueTime, DWORD Period, ULONG Flags )
1027 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1031 /***********************************************************************
1032 * DeleteTimerQueueTimer (KERNEL32.@)
1034 * Cancels a timer-queue timer.
1037 * nonzero on success or zero on faillure
1042 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1043 HANDLE CompletionEvent )
1046 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1056 /***********************************************************************
1057 * CreateNamedPipeA (KERNEL32.@)
1059 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1060 DWORD dwPipeMode, DWORD nMaxInstances,
1061 DWORD nOutBufferSize, DWORD nInBufferSize,
1062 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1064 WCHAR buffer[MAX_PATH];
1066 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1067 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1069 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1071 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1072 return INVALID_HANDLE_VALUE;
1074 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1075 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1079 /***********************************************************************
1080 * CreateNamedPipeW (KERNEL32.@)
1082 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1083 DWORD dwPipeMode, DWORD nMaxInstances,
1084 DWORD nOutBufferSize, DWORD nInBufferSize,
1085 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1088 UNICODE_STRING nt_name;
1089 OBJECT_ATTRIBUTES attr;
1091 BOOLEAN pipe_type, read_mode, non_block;
1093 IO_STATUS_BLOCK iosb;
1094 LARGE_INTEGER timeout;
1096 TRACE("(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n",
1097 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1098 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1100 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1102 SetLastError( ERROR_PATH_NOT_FOUND );
1103 return INVALID_HANDLE_VALUE;
1105 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1107 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1108 RtlFreeUnicodeString( &nt_name );
1109 return INVALID_HANDLE_VALUE;
1112 attr.Length = sizeof(attr);
1113 attr.RootDirectory = 0;
1114 attr.ObjectName = &nt_name;
1115 attr.Attributes = OBJ_CASE_INSENSITIVE |
1116 (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
1117 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1118 attr.SecurityQualityOfService = NULL;
1121 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1122 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1123 if ((dwOpenMode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
1124 options |= FILE_PIPE_FULL_DUPLEX;
1125 else if (dwOpenMode & PIPE_ACCESS_INBOUND) options |= FILE_PIPE_INBOUND;
1126 else if (dwOpenMode & PIPE_ACCESS_OUTBOUND) options |= FILE_PIPE_OUTBOUND;
1127 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1128 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1129 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1130 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0UL;
1132 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1136 status = NtCreateNamedPipeFile(&handle, GENERIC_READ|GENERIC_WRITE, &attr, &iosb,
1137 0, FILE_OVERWRITE_IF, options, pipe_type,
1138 read_mode, non_block, nMaxInstances,
1139 nInBufferSize, nOutBufferSize, &timeout);
1141 RtlFreeUnicodeString( &nt_name );
1144 handle = INVALID_HANDLE_VALUE;
1145 SetLastError( RtlNtStatusToDosError(status) );
1151 /***********************************************************************
1152 * PeekNamedPipe (KERNEL32.@)
1154 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1155 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1158 int avail=0, fd, ret, flags;
1160 TRACE("(%p,%p,%lu,%p,%p,%p)\n", hPipe, lpvBuffer, cbBuffer, lpcbRead, lpcbAvail, lpcbMessage);
1162 ret = wine_server_handle_to_fd( hPipe, GENERIC_READ, &fd, &flags );
1165 SetLastError( RtlNtStatusToDosError(ret) );
1168 if (flags & FD_FLAG_RECV_SHUTDOWN)
1170 wine_server_release_fd( hPipe, fd );
1171 SetLastError ( ERROR_PIPE_NOT_CONNECTED );
1175 if (ioctl(fd,FIONREAD, &avail ) != 0)
1177 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1178 wine_server_release_fd( hPipe, fd );
1181 if (!avail) /* check for closed pipe */
1183 struct pollfd pollfd;
1185 pollfd.events = POLLIN;
1187 switch (poll( &pollfd, 1, 0 ))
1191 case 1: /* got something */
1192 if (!(pollfd.revents & (POLLHUP | POLLERR))) break;
1193 TRACE("POLLHUP | POLLERR\n");
1196 wine_server_release_fd( hPipe, fd );
1197 SetLastError(ERROR_BROKEN_PIPE);
1201 TRACE(" 0x%08x bytes available\n", avail );
1207 if (avail && lpvBuffer && cbBuffer)
1209 int readbytes = (avail < cbBuffer) ? avail : cbBuffer;
1210 readbytes = recv(fd, lpvBuffer, readbytes, MSG_PEEK);
1213 WARN("failed to peek socket (%d)\n", errno);
1217 *lpcbRead = readbytes;
1219 wine_server_release_fd( hPipe, fd );
1221 #endif /* defined(FIONREAD) */
1223 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1224 FIXME("function not implemented\n");
1228 /***********************************************************************
1229 * PIPE_CompletionWait (Internal)
1231 static void CALLBACK PIPE_CompletionWait(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
1233 LPOVERLAPPED ovlp = (LPOVERLAPPED)user;
1235 TRACE("for %p/%p, status=%08lx\n", ovlp, iosb, status);
1239 ovlp->Internal = status;
1240 SetEvent(ovlp->hEvent);
1245 /***********************************************************************
1246 * WaitNamedPipeA (KERNEL32.@)
1248 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1250 WCHAR buffer[MAX_PATH];
1252 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1254 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1256 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1259 return WaitNamedPipeW( buffer, nTimeOut );
1263 /***********************************************************************
1264 * WaitNamedPipeW (KERNEL32.@)
1266 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1270 UNICODE_STRING nt_name;
1271 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1273 TRACE("%s 0x%08lx\n",debugstr_w(name),nTimeOut);
1275 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1278 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1280 RtlFreeUnicodeString( &nt_name );
1283 if (nt_name.Length < sizeof(leadin) ||
1284 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(leadin[0])))
1286 RtlFreeUnicodeString( &nt_name );
1290 memset(&ov,0,sizeof(ov));
1291 ov.hEvent = CreateEventW( NULL, 0, 0, NULL );
1295 SERVER_START_REQ( wait_named_pipe )
1297 req->timeout = nTimeOut;
1298 req->overlapped = &ov;
1299 req->func = PIPE_CompletionWait;
1300 wine_server_add_data( req, nt_name.Buffer + 4, nt_name.Length - 4*sizeof(WCHAR) );
1301 ret = !wine_server_call_err( req );
1305 RtlFreeUnicodeString( &nt_name );
1309 if (WAIT_OBJECT_0==WaitForSingleObject(ov.hEvent,INFINITE))
1311 SetLastError(RtlNtStatusToDosError(ov.Internal));
1312 ret = (ov.Internal==STATUS_SUCCESS);
1315 CloseHandle(ov.hEvent);
1320 /***********************************************************************
1321 * ConnectNamedPipe (KERNEL32.@)
1323 * Connects to a named pipe
1326 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1327 * overlapped: Optional OVERLAPPED struct
1331 * FALSE: Failure, GetLastError can be called for further details
1333 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1336 IO_STATUS_BLOCK status_block;
1338 TRACE("(%p,%p)\n", hPipe, overlapped);
1341 overlapped->Internal = STATUS_PENDING;
1343 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, NULL,
1344 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1345 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1347 if (status == STATUS_SUCCESS) return TRUE;
1348 SetLastError( RtlNtStatusToDosError(status) );
1352 /***********************************************************************
1353 * DisconnectNamedPipe (KERNEL32.@)
1355 * Disconnects from a named pipe
1358 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1362 * FALSE: Failure, GetLastError can be called for further details
1364 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1367 IO_STATUS_BLOCK io_block;
1369 TRACE("(%p)\n",hPipe);
1371 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1373 if (status == STATUS_SUCCESS) return TRUE;
1374 SetLastError( RtlNtStatusToDosError(status) );
1378 /***********************************************************************
1379 * TransactNamedPipe (KERNEL32.@)
1382 * should be done as a single operation in the wineserver or kernel
1384 BOOL WINAPI TransactNamedPipe(
1385 HANDLE handle, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1386 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1391 TRACE("%p %p %ld %p %ld %p %p\n",
1392 handle, lpInput, dwInputSize, lpOutput,
1393 dwOutputSize, lpBytesRead, lpOverlapped);
1397 FIXME("Doesn't support overlapped operation as yet\n");
1401 r = WriteFile(handle, lpOutput, dwOutputSize, &count, NULL);
1403 r = ReadFile(handle, lpInput, dwInputSize, lpBytesRead, NULL);
1408 /***********************************************************************
1409 * GetNamedPipeInfo (KERNEL32.@)
1411 BOOL WINAPI GetNamedPipeInfo(
1412 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1413 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1417 TRACE("%p %p %p %p %p\n", hNamedPipe, lpFlags,
1418 lpOutputBufferSize, lpInputBufferSize, lpMaxInstances);
1420 SERVER_START_REQ( get_named_pipe_info )
1422 req->handle = hNamedPipe;
1423 ret = !wine_server_call_err( req );
1427 if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)
1428 *lpFlags |= PIPE_TYPE_MESSAGE;
1429 if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
1430 *lpFlags |= PIPE_READMODE_MESSAGE;
1431 if (reply->flags & NAMED_PIPE_NONBLOCKING_MODE)
1432 *lpFlags |= PIPE_NOWAIT;
1434 if (lpOutputBufferSize) *lpOutputBufferSize = reply->outsize;
1435 if (lpInputBufferSize) *lpInputBufferSize = reply->outsize;
1436 if (lpMaxInstances) *lpMaxInstances = reply->maxinstances;
1443 /***********************************************************************
1444 * GetNamedPipeHandleStateA (KERNEL32.@)
1446 BOOL WINAPI GetNamedPipeHandleStateA(
1447 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1448 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1449 LPSTR lpUsername, DWORD nUsernameMaxSize)
1451 FIXME("%p %p %p %p %p %p %ld\n",
1452 hNamedPipe, lpState, lpCurInstances,
1453 lpMaxCollectionCount, lpCollectDataTimeout,
1454 lpUsername, nUsernameMaxSize);
1459 /***********************************************************************
1460 * GetNamedPipeHandleStateW (KERNEL32.@)
1462 BOOL WINAPI GetNamedPipeHandleStateW(
1463 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1464 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1465 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1467 FIXME("%p %p %p %p %p %p %ld\n",
1468 hNamedPipe, lpState, lpCurInstances,
1469 lpMaxCollectionCount, lpCollectDataTimeout,
1470 lpUsername, nUsernameMaxSize);
1475 /***********************************************************************
1476 * SetNamedPipeHandleState (KERNEL32.@)
1478 BOOL WINAPI SetNamedPipeHandleState(
1479 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1480 LPDWORD lpCollectDataTimeout)
1482 /* should be a fixme, but this function is called a lot by the RPC
1483 * runtime, and it slows down InstallShield a fair bit. */
1484 WARN("stub: %p %p/%ld %p %p\n",
1485 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1489 /***********************************************************************
1490 * CallNamedPipeA (KERNEL32.@)
1492 BOOL WINAPI CallNamedPipeA(
1493 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1494 LPVOID lpOutput, DWORD dwOutputSize,
1495 LPDWORD lpBytesRead, DWORD nTimeout)
1501 TRACE("%s %p %ld %p %ld %p %ld\n",
1502 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1503 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1505 if( lpNamedPipeName )
1507 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1508 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1509 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1511 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1512 dwOutputSize, lpBytesRead, nTimeout );
1513 if( lpNamedPipeName )
1514 HeapFree( GetProcessHeap(), 0, str );
1519 /***********************************************************************
1520 * CallNamedPipeW (KERNEL32.@)
1522 BOOL WINAPI CallNamedPipeW(
1523 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1524 LPVOID lpOutput, DWORD lpOutputSize,
1525 LPDWORD lpBytesRead, DWORD nTimeout)
1527 FIXME("%s %p %ld %p %ld %p %ld\n",
1528 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1529 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1533 /******************************************************************
1534 * CreatePipe (KERNEL32.@)
1537 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1538 LPSECURITY_ATTRIBUTES sa, DWORD size )
1540 static unsigned index /* = 0 */;
1543 unsigned in_index = index;
1544 UNICODE_STRING nt_name;
1545 OBJECT_ATTRIBUTES attr;
1547 IO_STATUS_BLOCK iosb;
1548 LARGE_INTEGER timeout;
1550 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1552 attr.Length = sizeof(attr);
1553 attr.RootDirectory = 0;
1554 attr.ObjectName = &nt_name;
1555 attr.Attributes = OBJ_CASE_INSENSITIVE |
1556 (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
1557 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1558 attr.SecurityQualityOfService = NULL;
1560 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1561 /* generate a unique pipe name (system wide) */
1564 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1565 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1566 'u','.','%','0','8','u','\0' };
1568 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1569 GetCurrentProcessId(), ++index);
1570 RtlInitUnicodeString(&nt_name, name);
1571 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1572 0, FILE_OVERWRITE_IF,
1573 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1574 FALSE, FALSE, FALSE,
1575 1, size, size, &timeout);
1578 SetLastError( RtlNtStatusToDosError(status) );
1579 hr = INVALID_HANDLE_VALUE;
1581 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1582 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1583 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1585 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1586 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1590 SetLastError( RtlNtStatusToDosError(status) );
1601 /******************************************************************************
1602 * CreateMailslotA [KERNEL32.@]
1604 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1605 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1611 TRACE("%s %ld %ld %p\n", debugstr_a(lpName),
1612 nMaxMessageSize, lReadTimeout, sa);
1616 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1617 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1618 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1621 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1623 HeapFree( GetProcessHeap(), 0, name );
1629 /******************************************************************************
1630 * CreateMailslotW [KERNEL32.@]
1632 * Create a mailslot with specified name.
1635 * lpName [I] Pointer to string for mailslot name
1636 * nMaxMessageSize [I] Maximum message size
1637 * lReadTimeout [I] Milliseconds before read time-out
1638 * sa [I] Pointer to security structure
1641 * Success: Handle to mailslot
1642 * Failure: INVALID_HANDLE_VALUE
1644 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1645 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1647 HANDLE handle = INVALID_HANDLE_VALUE;
1648 OBJECT_ATTRIBUTES attr;
1649 UNICODE_STRING nameW;
1650 LARGE_INTEGER timeout;
1651 IO_STATUS_BLOCK iosb;
1654 TRACE("%s %ld %ld %p\n", debugstr_w(lpName),
1655 nMaxMessageSize, lReadTimeout, sa);
1657 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1659 SetLastError( ERROR_PATH_NOT_FOUND );
1660 return INVALID_HANDLE_VALUE;
1663 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1665 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1666 RtlFreeUnicodeString( &nameW );
1667 return INVALID_HANDLE_VALUE;
1670 attr.Length = sizeof(attr);
1671 attr.RootDirectory = 0;
1672 attr.Attributes = OBJ_CASE_INSENSITIVE;
1673 attr.ObjectName = &nameW;
1674 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1675 attr.SecurityQualityOfService = NULL;
1677 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1679 status = NtCreateMailslotFile( &handle, GENERIC_READ | GENERIC_WRITE, &attr,
1680 &iosb, 0, 0, nMaxMessageSize, &timeout );
1683 SetLastError( RtlNtStatusToDosError(status) );
1684 handle = INVALID_HANDLE_VALUE;
1687 RtlFreeUnicodeString( &nameW );
1692 /******************************************************************************
1693 * GetMailslotInfo [KERNEL32.@]
1695 * Retrieve information about a mailslot.
1698 * hMailslot [I] Mailslot handle
1699 * lpMaxMessageSize [O] Address of maximum message size
1700 * lpNextSize [O] Address of size of next message
1701 * lpMessageCount [O] Address of number of messages
1702 * lpReadTimeout [O] Address of read time-out
1708 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1709 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1710 LPDWORD lpReadTimeout )
1712 FILE_MAILSLOT_QUERY_INFORMATION info;
1713 IO_STATUS_BLOCK iosb;
1716 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1717 lpNextSize, lpMessageCount, lpReadTimeout);
1719 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1720 FileMailslotQueryInformation );
1722 if( status != STATUS_SUCCESS )
1724 SetLastError( RtlNtStatusToDosError(status) );
1728 if( lpMaxMessageSize )
1729 *lpMaxMessageSize = info.MaximumMessageSize;
1731 *lpNextSize = info.NextMessageSize;
1732 if( lpMessageCount )
1733 *lpMessageCount = info.MessagesAvailable;
1735 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1741 /******************************************************************************
1742 * SetMailslotInfo [KERNEL32.@]
1744 * Set the read timeout of a mailslot.
1747 * hMailslot [I] Mailslot handle
1748 * dwReadTimeout [I] Timeout in milliseconds.
1754 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1756 FILE_MAILSLOT_SET_INFORMATION info;
1757 IO_STATUS_BLOCK iosb;
1760 TRACE("%p %ld\n", hMailslot, dwReadTimeout);
1762 info.ReadTimeout.QuadPart = dwReadTimeout * -10000;
1763 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1764 FileMailslotSetInformation );
1765 if( status != STATUS_SUCCESS )
1767 SetLastError( RtlNtStatusToDosError(status) );
1774 /******************************************************************************
1775 * CreateIoCompletionPort (KERNEL32.@)
1777 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1778 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1780 FIXME("(%p, %p, %08lx, %08lx): stub.\n",
1781 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1786 /******************************************************************************
1787 * GetQueuedCompletionStatus (KERNEL32.@)
1789 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1790 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1791 DWORD dwMilliseconds )
1793 FIXME("(%p,%p,%p,%p,%ld), stub!\n",
1794 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1795 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1799 /******************************************************************************
1800 * CreateJobObjectW (KERNEL32.@)
1802 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1804 FIXME("%p %s\n", attr, debugstr_w(name) );
1805 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1809 /******************************************************************************
1810 * CreateJobObjectA (KERNEL32.@)
1812 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1818 TRACE("%p %s\n", attr, debugstr_a(name) );
1822 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1823 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1826 SetLastError( ERROR_OUTOFMEMORY );
1829 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1832 r = CreateJobObjectW( attr, str );
1834 HeapFree( GetProcessHeap(), 0, str );
1839 /******************************************************************************
1840 * AssignProcessToJobObject (KERNEL32.@)
1842 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1844 FIXME("%p %p\n", hJob, hProcess);
1850 /***********************************************************************
1851 * InterlockedCompareExchange (KERNEL32.@)
1853 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1854 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1855 "movl 12(%esp),%eax\n\t"
1856 "movl 8(%esp),%ecx\n\t"
1857 "movl 4(%esp),%edx\n\t"
1858 "lock; cmpxchgl %ecx,(%edx)\n\t"
1861 /***********************************************************************
1862 * InterlockedExchange (KERNEL32.@)
1864 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1865 __ASM_GLOBAL_FUNC(InterlockedExchange,
1866 "movl 8(%esp),%eax\n\t"
1867 "movl 4(%esp),%edx\n\t"
1868 "lock; xchgl %eax,(%edx)\n\t"
1871 /***********************************************************************
1872 * InterlockedExchangeAdd (KERNEL32.@)
1874 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1875 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1876 "movl 8(%esp),%eax\n\t"
1877 "movl 4(%esp),%edx\n\t"
1878 "lock; xaddl %eax,(%edx)\n\t"
1881 /***********************************************************************
1882 * InterlockedIncrement (KERNEL32.@)
1884 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1885 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1886 "movl 4(%esp),%edx\n\t"
1888 "lock; xaddl %eax,(%edx)\n\t"
1892 /***********************************************************************
1893 * InterlockedDecrement (KERNEL32.@)
1895 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1896 "movl 4(%esp),%edx\n\t"
1898 "lock; xaddl %eax,(%edx)\n\t"
1902 #else /* __i386__ */
1904 /***********************************************************************
1905 * InterlockedCompareExchange (KERNEL32.@)
1907 * Atomically swap one value with another.
1910 * dest [I/O] The value to replace
1911 * xchq [I] The value to be swapped
1912 * compare [I] The value to compare to dest
1915 * The resulting value of dest.
1918 * dest is updated only if it is equal to compare, otherwise no swap is done.
1920 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
1922 return interlocked_cmpxchg( (int *)dest, xchg, compare );
1925 /***********************************************************************
1926 * InterlockedExchange (KERNEL32.@)
1928 * Atomically swap one value with another.
1931 * dest [I/O] The value to replace
1932 * val [I] The value to be swapped
1935 * The resulting value of dest.
1937 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
1939 return interlocked_xchg( (int *)dest, val );
1942 /***********************************************************************
1943 * InterlockedExchangeAdd (KERNEL32.@)
1945 * Atomically add one value to another.
1948 * dest [I/O] The value to add to
1949 * incr [I] The value to be added
1952 * The resulting value of dest.
1954 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
1956 return interlocked_xchg_add( (int *)dest, incr );
1959 /***********************************************************************
1960 * InterlockedIncrement (KERNEL32.@)
1962 * Atomically increment a value.
1965 * dest [I/O] The value to increment
1968 * The resulting value of dest.
1970 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
1972 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
1975 /***********************************************************************
1976 * InterlockedDecrement (KERNEL32.@)
1978 * Atomically decrement a value.
1981 * dest [I/O] The value to decrement
1984 * The resulting value of dest.
1986 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
1988 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
1991 #endif /* __i386__ */