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
54 #include "wine/server.h"
55 #include "wine/unicode.h"
56 #include "wine/winbase16.h"
57 #include "kernel_private.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(sync);
63 /* check if current version is NT or Win95 */
64 inline static int is_version_nt(void)
66 return !(GetVersion() & 0x80000000);
70 /***********************************************************************
73 VOID WINAPI Sleep( DWORD timeout )
75 SleepEx( timeout, FALSE );
78 /******************************************************************************
79 * SleepEx (KERNEL32.@)
81 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
85 if (timeout == INFINITE) status = NtDelayExecution( alertable, NULL );
90 time.QuadPart = timeout * (ULONGLONG)10000;
91 time.QuadPart = -time.QuadPart;
92 status = NtDelayExecution( alertable, &time );
94 if (status != STATUS_USER_APC) status = STATUS_SUCCESS;
99 /***********************************************************************
100 * SwitchToThread (KERNEL32.@)
102 BOOL WINAPI SwitchToThread(void)
104 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
108 /***********************************************************************
109 * WaitForSingleObject (KERNEL32.@)
111 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
113 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
117 /***********************************************************************
118 * WaitForSingleObjectEx (KERNEL32.@)
120 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
123 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
127 /***********************************************************************
128 * WaitForMultipleObjects (KERNEL32.@)
130 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
131 BOOL wait_all, DWORD timeout )
133 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
137 /***********************************************************************
138 * WaitForMultipleObjectsEx (KERNEL32.@)
140 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
141 BOOL wait_all, DWORD timeout,
145 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
148 if (count >= MAXIMUM_WAIT_OBJECTS)
150 SetLastError(ERROR_INVALID_PARAMETER);
153 for (i = 0; i < count; i++)
155 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
156 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
157 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
158 hloc[i] = GetStdHandle( (DWORD)handles[i] );
160 hloc[i] = handles[i];
162 /* yes, even screen buffer console handles are waitable, and are
163 * handled as a handle to the console itself !!
165 if (is_console_handle(hloc[i]))
167 if (!VerifyConsoleIoHandle(hloc[i]))
171 hloc[i] = GetConsoleInputWaitHandle();
175 if (timeout == INFINITE)
177 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, NULL );
183 time.QuadPart = timeout * (ULONGLONG)10000;
184 time.QuadPart = -time.QuadPart;
185 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, &time );
188 if (HIWORD(status)) /* is it an error code? */
190 SetLastError( RtlNtStatusToDosError(status) );
191 status = WAIT_FAILED;
197 /***********************************************************************
198 * WaitForSingleObject (KERNEL.460)
200 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
202 DWORD retval, mutex_count;
204 ReleaseThunkLock( &mutex_count );
205 retval = WaitForSingleObject( handle, timeout );
206 RestoreThunkLock( mutex_count );
210 /***********************************************************************
211 * WaitForMultipleObjects (KERNEL.461)
213 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
214 BOOL wait_all, DWORD timeout )
216 DWORD retval, mutex_count;
218 ReleaseThunkLock( &mutex_count );
219 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
220 RestoreThunkLock( mutex_count );
224 /***********************************************************************
225 * WaitForMultipleObjectsEx (KERNEL.495)
227 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
228 BOOL wait_all, DWORD timeout, BOOL alertable )
230 DWORD retval, mutex_count;
232 ReleaseThunkLock( &mutex_count );
233 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
234 RestoreThunkLock( mutex_count );
238 /***********************************************************************
239 * RegisterWaitForSingleObject (KERNEL32.@)
241 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
242 WAITORTIMERCALLBACK Callback, PVOID Context,
243 ULONG dwMilliseconds, ULONG dwFlags)
245 FIXME("%p %p %p %p %ld %ld\n",
246 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
250 /***********************************************************************
251 * RegisterWaitForSingleObjectEx (KERNEL32.@)
253 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
254 WAITORTIMERCALLBACK Callback, PVOID Context,
255 ULONG dwMilliseconds, ULONG dwFlags )
257 FIXME("%p %p %p %ld %ld\n",
258 hObject,Callback,Context,dwMilliseconds,dwFlags);
262 /***********************************************************************
263 * UnregisterWait (KERNEL32.@)
265 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
267 FIXME("%p\n",WaitHandle);
271 /***********************************************************************
272 * UnregisterWaitEx (KERNEL32.@)
274 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
276 FIXME("%p %p\n",WaitHandle, CompletionEvent);
280 /***********************************************************************
281 * SignalObjectAndWait (KERNEL32.@)
283 * Allows to atomically signal any of the synchro objects (semaphore,
284 * mutex, event) and wait on another.
286 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
287 DWORD dwMilliseconds, BOOL bAlertable )
290 LARGE_INTEGER timeout, *ptimeout = NULL;
292 TRACE("%p %p %ld %d\n", hObjectToSignal,
293 hObjectToWaitOn, dwMilliseconds, bAlertable);
295 if (dwMilliseconds != INFINITE)
297 timeout.QuadPart = dwMilliseconds * (ULONGLONG)10000;
298 timeout.QuadPart = -timeout.QuadPart;
302 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn,
303 bAlertable, ptimeout );
306 SetLastError( RtlNtStatusToDosError(status) );
307 status = WAIT_FAILED;
312 /***********************************************************************
313 * InitializeCriticalSection (KERNEL32.@)
315 * Initialise a critical section before use.
318 * crit [O] Critical section to initialise.
321 * Nothing. If the function fails an exception is raised.
323 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
325 NTSTATUS ret = RtlInitializeCriticalSection( crit );
326 if (ret) RtlRaiseStatus( ret );
329 /***********************************************************************
330 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
332 * Initialise a critical section with a spin count.
335 * crit [O] Critical section to initialise.
336 * spincount [I] Number of times to spin upon contention.
340 * Failure: Nothing. If the function fails an exception is raised.
343 * spincount is ignored on uni-processor systems.
345 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
347 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
348 if (ret) RtlRaiseStatus( ret );
352 /***********************************************************************
353 * MakeCriticalSectionGlobal (KERNEL32.@)
355 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
357 /* let's assume that only one thread at a time will try to do this */
358 HANDLE sem = crit->LockSemaphore;
359 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
360 crit->LockSemaphore = ConvertToGlobalHandle( sem );
363 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
364 crit->DebugInfo = NULL;
369 /***********************************************************************
370 * ReinitializeCriticalSection (KERNEL32.@)
372 * Initialise an already used critical section.
375 * crit [O] Critical section to initialise.
380 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
382 if ( !crit->LockSemaphore )
383 RtlInitializeCriticalSection( crit );
387 /***********************************************************************
388 * UninitializeCriticalSection (KERNEL32.@)
390 * UnInitialise a critical section after use.
393 * crit [O] Critical section to uninitialise (destroy).
398 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
400 RtlDeleteCriticalSection( crit );
404 /***********************************************************************
405 * CreateEventA (KERNEL32.@)
407 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
408 BOOL initial_state, LPCSTR name )
410 WCHAR buffer[MAX_PATH];
412 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
414 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
416 SetLastError( ERROR_FILENAME_EXCED_RANGE );
419 return CreateEventW( sa, manual_reset, initial_state, buffer );
423 /***********************************************************************
424 * CreateEventW (KERNEL32.@)
426 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
427 BOOL initial_state, LPCWSTR name )
430 UNICODE_STRING nameW;
431 OBJECT_ATTRIBUTES attr;
434 /* one buggy program needs this
435 * ("Van Dale Groot woordenboek der Nederlandse taal")
437 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
439 ERR("Bad security attributes pointer %p\n",sa);
440 SetLastError( ERROR_INVALID_PARAMETER);
444 attr.Length = sizeof(attr);
445 attr.RootDirectory = 0;
446 attr.ObjectName = NULL;
447 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
448 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
449 attr.SecurityQualityOfService = NULL;
452 RtlInitUnicodeString( &nameW, name );
453 attr.ObjectName = &nameW;
456 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
457 SetLastError( RtlNtStatusToDosError(status) );
462 /***********************************************************************
463 * CreateW32Event (KERNEL.457)
465 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
467 return CreateEventW( NULL, manual_reset, initial_state, NULL );
471 /***********************************************************************
472 * OpenEventA (KERNEL32.@)
474 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
476 WCHAR buffer[MAX_PATH];
478 if (!name) return OpenEventW( access, inherit, NULL );
480 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
482 SetLastError( ERROR_FILENAME_EXCED_RANGE );
485 return OpenEventW( access, inherit, buffer );
489 /***********************************************************************
490 * OpenEventW (KERNEL32.@)
492 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
495 UNICODE_STRING nameW;
496 OBJECT_ATTRIBUTES attr;
499 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
501 attr.Length = sizeof(attr);
502 attr.RootDirectory = 0;
503 attr.ObjectName = NULL;
504 attr.Attributes = inherit ? OBJ_INHERIT : 0;
505 attr.SecurityDescriptor = NULL;
506 attr.SecurityQualityOfService = NULL;
509 RtlInitUnicodeString( &nameW, name );
510 attr.ObjectName = &nameW;
513 status = NtOpenEvent( &ret, access, &attr );
514 if (status != STATUS_SUCCESS)
516 SetLastError( RtlNtStatusToDosError(status) );
522 /***********************************************************************
523 * PulseEvent (KERNEL32.@)
525 BOOL WINAPI PulseEvent( HANDLE handle )
529 if ((status = NtPulseEvent( handle, NULL )))
530 SetLastError( RtlNtStatusToDosError(status) );
535 /***********************************************************************
536 * SetW32Event (KERNEL.458)
537 * SetEvent (KERNEL32.@)
539 BOOL WINAPI SetEvent( HANDLE handle )
543 if ((status = NtSetEvent( handle, NULL )))
544 SetLastError( RtlNtStatusToDosError(status) );
549 /***********************************************************************
550 * ResetW32Event (KERNEL.459)
551 * ResetEvent (KERNEL32.@)
553 BOOL WINAPI ResetEvent( HANDLE handle )
557 if ((status = NtResetEvent( handle, NULL )))
558 SetLastError( RtlNtStatusToDosError(status) );
563 /***********************************************************************
564 * NOTE: The Win95 VWin32_Event routines given below are really low-level
565 * routines implemented directly by VWin32. The user-mode libraries
566 * implement Win32 synchronisation routines on top of these low-level
567 * primitives. We do it the other way around here :-)
570 /***********************************************************************
571 * VWin32_EventCreate (KERNEL.442)
573 HANDLE WINAPI VWin32_EventCreate(VOID)
575 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
576 return ConvertToGlobalHandle( hEvent );
579 /***********************************************************************
580 * VWin32_EventDestroy (KERNEL.443)
582 VOID WINAPI VWin32_EventDestroy(HANDLE event)
584 CloseHandle( event );
587 /***********************************************************************
588 * VWin32_EventWait (KERNEL.450)
590 VOID WINAPI VWin32_EventWait(HANDLE event)
594 ReleaseThunkLock( &mutex_count );
595 WaitForSingleObject( event, INFINITE );
596 RestoreThunkLock( mutex_count );
599 /***********************************************************************
600 * VWin32_EventSet (KERNEL.451)
601 * KERNEL_479 (KERNEL.479)
603 VOID WINAPI VWin32_EventSet(HANDLE event)
610 /***********************************************************************
611 * CreateMutexA (KERNEL32.@)
613 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
615 WCHAR buffer[MAX_PATH];
617 if (!name) return CreateMutexW( sa, owner, NULL );
619 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
621 SetLastError( ERROR_FILENAME_EXCED_RANGE );
624 return CreateMutexW( sa, owner, buffer );
628 /***********************************************************************
629 * CreateMutexW (KERNEL32.@)
631 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
634 UNICODE_STRING nameW;
635 OBJECT_ATTRIBUTES attr;
638 attr.Length = sizeof(attr);
639 attr.RootDirectory = 0;
640 attr.ObjectName = NULL;
641 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
642 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
643 attr.SecurityQualityOfService = NULL;
646 RtlInitUnicodeString( &nameW, name );
647 attr.ObjectName = &nameW;
650 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
651 SetLastError( RtlNtStatusToDosError(status) );
656 /***********************************************************************
657 * OpenMutexA (KERNEL32.@)
659 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
661 WCHAR buffer[MAX_PATH];
663 if (!name) return OpenMutexW( access, inherit, NULL );
665 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
667 SetLastError( ERROR_FILENAME_EXCED_RANGE );
670 return OpenMutexW( access, inherit, buffer );
674 /***********************************************************************
675 * OpenMutexW (KERNEL32.@)
677 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
680 UNICODE_STRING nameW;
681 OBJECT_ATTRIBUTES attr;
684 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
686 attr.Length = sizeof(attr);
687 attr.RootDirectory = 0;
688 attr.ObjectName = NULL;
689 attr.Attributes = inherit ? OBJ_INHERIT : 0;
690 attr.SecurityDescriptor = NULL;
691 attr.SecurityQualityOfService = NULL;
694 RtlInitUnicodeString( &nameW, name );
695 attr.ObjectName = &nameW;
698 status = NtOpenMutant( &ret, access, &attr );
699 if (status != STATUS_SUCCESS)
701 SetLastError( RtlNtStatusToDosError(status) );
708 /***********************************************************************
709 * ReleaseMutex (KERNEL32.@)
711 BOOL WINAPI ReleaseMutex( HANDLE handle )
715 status = NtReleaseMutant(handle, NULL);
716 if (status != STATUS_SUCCESS)
718 SetLastError( RtlNtStatusToDosError(status) );
730 /***********************************************************************
731 * CreateSemaphoreA (KERNEL32.@)
733 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
735 WCHAR buffer[MAX_PATH];
737 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
739 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
741 SetLastError( ERROR_FILENAME_EXCED_RANGE );
744 return CreateSemaphoreW( sa, initial, max, buffer );
748 /***********************************************************************
749 * CreateSemaphoreW (KERNEL32.@)
751 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
752 LONG max, LPCWSTR name )
755 UNICODE_STRING nameW;
756 OBJECT_ATTRIBUTES attr;
759 attr.Length = sizeof(attr);
760 attr.RootDirectory = 0;
761 attr.ObjectName = NULL;
762 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
763 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
764 attr.SecurityQualityOfService = NULL;
767 RtlInitUnicodeString( &nameW, name );
768 attr.ObjectName = &nameW;
771 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
772 SetLastError( RtlNtStatusToDosError(status) );
777 /***********************************************************************
778 * OpenSemaphoreA (KERNEL32.@)
780 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
782 WCHAR buffer[MAX_PATH];
784 if (!name) return OpenSemaphoreW( access, inherit, NULL );
786 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
788 SetLastError( ERROR_FILENAME_EXCED_RANGE );
791 return OpenSemaphoreW( access, inherit, buffer );
795 /***********************************************************************
796 * OpenSemaphoreW (KERNEL32.@)
798 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
801 UNICODE_STRING nameW;
802 OBJECT_ATTRIBUTES attr;
805 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
807 attr.Length = sizeof(attr);
808 attr.RootDirectory = 0;
809 attr.ObjectName = NULL;
810 attr.Attributes = inherit ? OBJ_INHERIT : 0;
811 attr.SecurityDescriptor = NULL;
812 attr.SecurityQualityOfService = NULL;
815 RtlInitUnicodeString( &nameW, name );
816 attr.ObjectName = &nameW;
819 status = NtOpenSemaphore( &ret, access, &attr );
820 if (status != STATUS_SUCCESS)
822 SetLastError( RtlNtStatusToDosError(status) );
829 /***********************************************************************
830 * ReleaseSemaphore (KERNEL32.@)
832 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
834 NTSTATUS status = NtReleaseSemaphore( handle, count, previous );
835 if (status) SetLastError( RtlNtStatusToDosError(status) );
845 /***********************************************************************
846 * CreateWaitableTimerA (KERNEL32.@)
848 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
850 WCHAR buffer[MAX_PATH];
852 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
854 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
856 SetLastError( ERROR_FILENAME_EXCED_RANGE );
859 return CreateWaitableTimerW( sa, manual, buffer );
863 /***********************************************************************
864 * CreateWaitableTimerW (KERNEL32.@)
866 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
870 UNICODE_STRING nameW;
871 OBJECT_ATTRIBUTES attr;
873 attr.Length = sizeof(attr);
874 attr.RootDirectory = 0;
875 attr.ObjectName = NULL;
876 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
877 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
878 attr.SecurityQualityOfService = NULL;
881 RtlInitUnicodeString( &nameW, name );
882 attr.ObjectName = &nameW;
885 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
886 manual ? NotificationTimer : SynchronizationTimer);
887 SetLastError( RtlNtStatusToDosError(status) );
892 /***********************************************************************
893 * OpenWaitableTimerA (KERNEL32.@)
895 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
897 WCHAR buffer[MAX_PATH];
899 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
901 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
903 SetLastError( ERROR_FILENAME_EXCED_RANGE );
906 return OpenWaitableTimerW( access, inherit, buffer );
910 /***********************************************************************
911 * OpenWaitableTimerW (KERNEL32.@)
913 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
916 UNICODE_STRING nameW;
917 OBJECT_ATTRIBUTES attr;
920 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
922 attr.Length = sizeof(attr);
923 attr.RootDirectory = 0;
924 attr.ObjectName = NULL;
925 attr.Attributes = inherit ? OBJ_INHERIT : 0;
926 attr.SecurityDescriptor = NULL;
927 attr.SecurityQualityOfService = NULL;
930 RtlInitUnicodeString( &nameW, name );
931 attr.ObjectName = &nameW;
934 status = NtOpenTimer(&handle, access, &attr);
935 if (status != STATUS_SUCCESS)
937 SetLastError( RtlNtStatusToDosError(status) );
944 /***********************************************************************
945 * SetWaitableTimer (KERNEL32.@)
947 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
948 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
950 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
951 arg, resume, period, NULL);
953 if (status != STATUS_SUCCESS)
955 SetLastError( RtlNtStatusToDosError(status) );
956 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
962 /***********************************************************************
963 * CancelWaitableTimer (KERNEL32.@)
965 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
969 status = NtCancelTimer(handle, NULL);
970 if (status != STATUS_SUCCESS)
972 SetLastError( RtlNtStatusToDosError(status) );
979 /***********************************************************************
980 * CreateTimerQueue (KERNEL32.@)
982 HANDLE WINAPI CreateTimerQueue(void)
985 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
990 /***********************************************************************
991 * DeleteTimerQueueEx (KERNEL32.@)
993 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
995 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
996 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1000 /***********************************************************************
1001 * CreateTimerQueueTimer (KERNEL32.@)
1003 * Creates a timer-queue timer. This timer expires at the specified due
1004 * time (in ms), then after every specified period (in ms). When the timer
1005 * expires, the callback function is called.
1008 * nonzero on success or zero on faillure
1013 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1014 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1015 DWORD DueTime, DWORD Period, ULONG Flags )
1018 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1022 /***********************************************************************
1023 * DeleteTimerQueueTimer (KERNEL32.@)
1025 * Cancels a timer-queue timer.
1028 * nonzero on success or zero on faillure
1033 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1034 HANDLE CompletionEvent )
1037 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1047 /***********************************************************************
1048 * CreateNamedPipeA (KERNEL32.@)
1050 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1051 DWORD dwPipeMode, DWORD nMaxInstances,
1052 DWORD nOutBufferSize, DWORD nInBufferSize,
1053 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1055 WCHAR buffer[MAX_PATH];
1057 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1058 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1060 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1062 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1063 return INVALID_HANDLE_VALUE;
1065 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1066 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1070 /***********************************************************************
1071 * CreateNamedPipeW (KERNEL32.@)
1073 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1074 DWORD dwPipeMode, DWORD nMaxInstances,
1075 DWORD nOutBufferSize, DWORD nInBufferSize,
1076 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1079 UNICODE_STRING nt_name;
1080 OBJECT_ATTRIBUTES attr;
1082 BOOLEAN pipe_type, read_mode, non_block;
1084 IO_STATUS_BLOCK iosb;
1085 LARGE_INTEGER timeout;
1087 TRACE("(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n",
1088 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1089 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1091 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1093 SetLastError( ERROR_PATH_NOT_FOUND );
1094 return INVALID_HANDLE_VALUE;
1096 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1098 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1099 RtlFreeUnicodeString( &nt_name );
1100 return INVALID_HANDLE_VALUE;
1103 attr.Length = sizeof(attr);
1104 attr.RootDirectory = 0;
1105 attr.ObjectName = &nt_name;
1106 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
1107 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1108 attr.SecurityQualityOfService = NULL;
1111 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1112 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1113 if ((dwOpenMode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
1114 options |= FILE_PIPE_FULL_DUPLEX;
1115 else if (dwOpenMode & PIPE_ACCESS_INBOUND) options |= FILE_PIPE_INBOUND;
1116 else if (dwOpenMode & PIPE_ACCESS_OUTBOUND) options |= FILE_PIPE_OUTBOUND;
1117 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1118 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1119 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1120 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0UL;
1122 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1126 status = NtCreateNamedPipeFile(&handle, 0, &attr, &iosb, 0, FILE_OVERWRITE_IF,
1127 options, pipe_type, read_mode, non_block,
1128 nMaxInstances, nInBufferSize, nOutBufferSize,
1131 RtlFreeUnicodeString( &nt_name );
1134 handle = INVALID_HANDLE_VALUE;
1135 SetLastError( RtlNtStatusToDosError(status) );
1141 /***********************************************************************
1142 * PeekNamedPipe (KERNEL32.@)
1144 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1145 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1148 int avail=0, fd, ret, flags;
1150 TRACE("(%p,%p,%lu,%p,%p,%p)\n", hPipe, lpvBuffer, cbBuffer, lpcbRead, lpcbAvail, lpcbMessage);
1152 ret = wine_server_handle_to_fd( hPipe, GENERIC_READ, &fd, &flags );
1155 SetLastError( RtlNtStatusToDosError(ret) );
1158 if (flags & FD_FLAG_RECV_SHUTDOWN)
1160 wine_server_release_fd( hPipe, fd );
1161 SetLastError ( ERROR_PIPE_NOT_CONNECTED );
1165 if (ioctl(fd,FIONREAD, &avail ) != 0)
1167 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1168 wine_server_release_fd( hPipe, fd );
1171 if (!avail) /* check for closed pipe */
1173 struct pollfd pollfd;
1175 pollfd.events = POLLIN;
1177 switch (poll( &pollfd, 1, 0 ))
1181 case 1: /* got something */
1182 if (!(pollfd.revents & (POLLHUP | POLLERR))) break;
1183 TRACE("POLLHUP | POLLERR\n");
1186 wine_server_release_fd( hPipe, fd );
1187 SetLastError(ERROR_BROKEN_PIPE);
1191 TRACE(" 0x%08x bytes available\n", avail );
1197 if (avail && lpvBuffer && cbBuffer)
1199 int readbytes = (avail < cbBuffer) ? avail : cbBuffer;
1200 readbytes = recv(fd, lpvBuffer, readbytes, MSG_PEEK);
1203 WARN("failed to peek socket (%d)\n", errno);
1207 *lpcbRead = readbytes;
1209 wine_server_release_fd( hPipe, fd );
1211 #endif /* defined(FIONREAD) */
1213 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1214 FIXME("function not implemented\n");
1218 /***********************************************************************
1219 * PIPE_CompletionWait (Internal)
1221 static void CALLBACK PIPE_CompletionWait(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
1223 LPOVERLAPPED ovlp = (LPOVERLAPPED)user;
1225 TRACE("for %p/%p, status=%08lx\n", ovlp, iosb, status);
1229 ovlp->Internal = status;
1230 SetEvent(ovlp->hEvent);
1235 /***********************************************************************
1236 * WaitNamedPipeA (KERNEL32.@)
1238 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1240 WCHAR buffer[MAX_PATH];
1242 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1244 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1246 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1249 return WaitNamedPipeW( buffer, nTimeOut );
1253 /***********************************************************************
1254 * WaitNamedPipeW (KERNEL32.@)
1256 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1260 UNICODE_STRING nt_name;
1261 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1263 TRACE("%s 0x%08lx\n",debugstr_w(name),nTimeOut);
1265 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1268 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1270 RtlFreeUnicodeString( &nt_name );
1273 if (nt_name.Length < sizeof(leadin) ||
1274 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(leadin[0])))
1276 RtlFreeUnicodeString( &nt_name );
1280 memset(&ov,0,sizeof(ov));
1281 ov.hEvent = CreateEventW( NULL, 0, 0, NULL );
1285 SERVER_START_REQ( wait_named_pipe )
1287 req->timeout = nTimeOut;
1288 req->overlapped = &ov;
1289 req->func = PIPE_CompletionWait;
1290 wine_server_add_data( req, nt_name.Buffer + 4, nt_name.Length - 4*sizeof(WCHAR) );
1291 ret = !wine_server_call_err( req );
1295 RtlFreeUnicodeString( &nt_name );
1299 if (WAIT_OBJECT_0==WaitForSingleObject(ov.hEvent,INFINITE))
1301 SetLastError(RtlNtStatusToDosError(ov.Internal));
1302 ret = (ov.Internal==STATUS_SUCCESS);
1305 CloseHandle(ov.hEvent);
1310 /***********************************************************************
1311 * ConnectNamedPipe (KERNEL32.@)
1313 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1319 TRACE("(%p,%p)\n", hPipe, overlapped);
1323 memset(&ov, 0, sizeof(ov));
1324 ov.hEvent = CreateEventW(NULL, 0, 0, NULL);
1325 if (!ov.hEvent) return FALSE;
1328 else pov = overlapped;
1330 pov->Internal = STATUS_PENDING;
1332 SERVER_START_REQ( connect_named_pipe )
1334 req->handle = hPipe;
1335 req->overlapped = pov;
1336 req->func = PIPE_CompletionWait;
1337 ret = !wine_server_call_err( req );
1345 SetLastError( ERROR_IO_PENDING );
1350 ret = GetOverlappedResult(hPipe, &ov, NULL, TRUE);
1351 CloseHandle(ov.hEvent);
1358 /***********************************************************************
1359 * DisconnectNamedPipe (KERNEL32.@)
1361 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1365 TRACE("(%p)\n",hPipe);
1367 SERVER_START_REQ( disconnect_named_pipe )
1369 req->handle = hPipe;
1370 ret = !wine_server_call_err( req );
1371 if (ret && reply->fd != -1) close( reply->fd );
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;
1545 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1546 /* generate a unique pipe name (system wide) */
1549 static const WCHAR nameFmt[] = { '\\','\\','.','\\','p','i','p','e',
1550 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1551 'u','.','%','0','8','u','\0' };
1552 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1553 GetCurrentProcessId(), ++index);
1554 hr = CreateNamedPipeW(name, PIPE_ACCESS_INBOUND,
1555 PIPE_TYPE_BYTE | PIPE_WAIT, 1, size, size,
1556 NMPWAIT_USE_DEFAULT_WAIT, sa);
1557 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1558 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1559 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1561 hw = CreateFileW(name, GENERIC_WRITE, 0, sa, OPEN_EXISTING, 0, 0);
1562 if (hw == INVALID_HANDLE_VALUE)
1574 /******************************************************************************
1575 * CreateMailslotA [KERNEL32.@]
1577 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1578 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1584 TRACE("%s %ld %ld %p\n", debugstr_a(lpName),
1585 nMaxMessageSize, lReadTimeout, sa);
1589 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1590 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1591 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1594 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1596 HeapFree( GetProcessHeap(), 0, name );
1602 /******************************************************************************
1603 * CreateMailslotW [KERNEL32.@]
1605 * Create a mailslot with specified name.
1608 * lpName [I] Pointer to string for mailslot name
1609 * nMaxMessageSize [I] Maximum message size
1610 * lReadTimeout [I] Milliseconds before read time-out
1611 * sa [I] Pointer to security structure
1614 * Success: Handle to mailslot
1615 * Failure: INVALID_HANDLE_VALUE
1617 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1618 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1620 HANDLE handle = INVALID_HANDLE_VALUE;
1621 OBJECT_ATTRIBUTES attr;
1622 UNICODE_STRING nameW;
1623 LARGE_INTEGER timeout;
1624 IO_STATUS_BLOCK iosb;
1627 TRACE("%s %ld %ld %p\n", debugstr_w(lpName),
1628 nMaxMessageSize, lReadTimeout, sa);
1630 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1632 SetLastError( ERROR_PATH_NOT_FOUND );
1633 return INVALID_HANDLE_VALUE;
1636 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1638 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1639 RtlFreeUnicodeString( &nameW );
1640 return INVALID_HANDLE_VALUE;
1643 attr.Length = sizeof(attr);
1644 attr.RootDirectory = 0;
1645 attr.Attributes = OBJ_CASE_INSENSITIVE;
1646 attr.ObjectName = &nameW;
1647 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1648 attr.SecurityQualityOfService = NULL;
1650 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1652 status = NtCreateMailslotFile( &handle, GENERIC_READ | GENERIC_WRITE, &attr,
1653 &iosb, 0, 0, nMaxMessageSize, &timeout );
1656 SetLastError( RtlNtStatusToDosError(status) );
1657 handle = INVALID_HANDLE_VALUE;
1660 RtlFreeUnicodeString( &nameW );
1665 /******************************************************************************
1666 * GetMailslotInfo [KERNEL32.@]
1668 * Retrieve information about a mailslot.
1671 * hMailslot [I] Mailslot handle
1672 * lpMaxMessageSize [O] Address of maximum message size
1673 * lpNextSize [O] Address of size of next message
1674 * lpMessageCount [O] Address of number of messages
1675 * lpReadTimeout [O] Address of read time-out
1681 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1682 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1683 LPDWORD lpReadTimeout )
1685 FILE_MAILSLOT_QUERY_INFORMATION info;
1686 IO_STATUS_BLOCK iosb;
1689 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1690 lpNextSize, lpMessageCount, lpReadTimeout);
1692 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1693 FileMailslotQueryInformation );
1695 if( status != STATUS_SUCCESS )
1697 SetLastError( RtlNtStatusToDosError(status) );
1701 if( lpMaxMessageSize )
1702 *lpMaxMessageSize = info.MaximumMessageSize;
1704 *lpNextSize = info.NextMessageSize;
1705 if( lpMessageCount )
1706 *lpMessageCount = info.MessagesAvailable;
1708 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1714 /******************************************************************************
1715 * SetMailslotInfo [KERNEL32.@]
1717 * Set the read timeout of a mailslot.
1720 * hMailslot [I] Mailslot handle
1721 * dwReadTimeout [I] Timeout in milliseconds.
1727 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1729 FILE_MAILSLOT_SET_INFORMATION info;
1730 IO_STATUS_BLOCK iosb;
1733 TRACE("%p %ld\n", hMailslot, dwReadTimeout);
1735 info.ReadTimeout.QuadPart = dwReadTimeout * -10000;
1736 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1737 FileMailslotSetInformation );
1738 if( status != STATUS_SUCCESS )
1740 SetLastError( RtlNtStatusToDosError(status) );
1747 /******************************************************************************
1748 * CreateIoCompletionPort (KERNEL32.@)
1750 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1751 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1753 FIXME("(%p, %p, %08lx, %08lx): stub.\n",
1754 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1759 /******************************************************************************
1760 * GetQueuedCompletionStatus (KERNEL32.@)
1762 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1763 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1764 DWORD dwMilliseconds )
1766 FIXME("(%p,%p,%p,%p,%ld), stub!\n",
1767 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1768 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1772 /******************************************************************************
1773 * CreateJobObjectW (KERNEL32.@)
1775 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1777 FIXME("%p %s\n", attr, debugstr_w(name) );
1778 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1782 /******************************************************************************
1783 * CreateJobObjectA (KERNEL32.@)
1785 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1791 TRACE("%p %s\n", attr, debugstr_a(name) );
1795 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1796 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1799 SetLastError( ERROR_OUTOFMEMORY );
1802 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1805 r = CreateJobObjectW( attr, str );
1807 HeapFree( GetProcessHeap(), 0, str );
1812 /******************************************************************************
1813 * AssignProcessToJobObject (KERNEL32.@)
1815 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1817 FIXME("%p %p\n", hJob, hProcess);
1823 /***********************************************************************
1824 * InterlockedCompareExchange (KERNEL32.@)
1826 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1827 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1828 "movl 12(%esp),%eax\n\t"
1829 "movl 8(%esp),%ecx\n\t"
1830 "movl 4(%esp),%edx\n\t"
1831 "lock; cmpxchgl %ecx,(%edx)\n\t"
1834 /***********************************************************************
1835 * InterlockedExchange (KERNEL32.@)
1837 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1838 __ASM_GLOBAL_FUNC(InterlockedExchange,
1839 "movl 8(%esp),%eax\n\t"
1840 "movl 4(%esp),%edx\n\t"
1841 "lock; xchgl %eax,(%edx)\n\t"
1844 /***********************************************************************
1845 * InterlockedExchangeAdd (KERNEL32.@)
1847 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1848 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1849 "movl 8(%esp),%eax\n\t"
1850 "movl 4(%esp),%edx\n\t"
1851 "lock; xaddl %eax,(%edx)\n\t"
1854 /***********************************************************************
1855 * InterlockedIncrement (KERNEL32.@)
1857 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1858 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1859 "movl 4(%esp),%edx\n\t"
1861 "lock; xaddl %eax,(%edx)\n\t"
1865 /***********************************************************************
1866 * InterlockedDecrement (KERNEL32.@)
1868 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1869 "movl 4(%esp),%edx\n\t"
1871 "lock; xaddl %eax,(%edx)\n\t"
1875 #else /* __i386__ */
1877 /***********************************************************************
1878 * InterlockedCompareExchange (KERNEL32.@)
1880 * Atomically swap one value with another.
1883 * dest [I/O] The value to replace
1884 * xchq [I] The value to be swapped
1885 * compare [I] The value to compare to dest
1888 * The resulting value of dest.
1891 * dest is updated only if it is equal to compare, otherwise no swap is done.
1893 LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare )
1895 return interlocked_cmpxchg( dest, xchg, compare );
1898 /***********************************************************************
1899 * InterlockedExchange (KERNEL32.@)
1901 * Atomically swap one value with another.
1904 * dest [I/O] The value to replace
1905 * val [I] The value to be swapped
1908 * The resulting value of dest.
1910 LONG WINAPI InterlockedExchange( PLONG dest, LONG val )
1912 return interlocked_xchg( dest, val );
1915 /***********************************************************************
1916 * InterlockedExchangeAdd (KERNEL32.@)
1918 * Atomically add one value to another.
1921 * dest [I/O] The value to add to
1922 * incr [I] The value to be added
1925 * The resulting value of dest.
1927 LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr )
1929 return interlocked_xchg_add( dest, incr );
1932 /***********************************************************************
1933 * InterlockedIncrement (KERNEL32.@)
1935 * Atomically increment a value.
1938 * dest [I/O] The value to increment
1941 * The resulting value of dest.
1943 LONG WINAPI InterlockedIncrement( PLONG dest )
1945 return interlocked_xchg_add( dest, 1 ) + 1;
1948 /***********************************************************************
1949 * InterlockedDecrement (KERNEL32.@)
1951 * Atomically decrement a value.
1954 * dest [I/O] The value to decrement
1957 * The resulting value of dest.
1959 LONG WINAPI InterlockedDecrement( PLONG dest )
1961 return interlocked_xchg_add( dest, -1 ) - 1;
1964 #endif /* __i386__ */