push 9eb9af089d68d39110a91889d3a673043db63c4b
[wine/hacks.git] / dlls / kernel32 / sync.c
blob2318c91827373834c772382d6a075c5789e6b0ce
1 /*
2 * Kernel synchronization objects
4 * Copyright 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <errno.h>
29 #include <stdarg.h>
30 #include <stdio.h>
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "winnls.h"
41 #include "winternl.h"
42 #include "winioctl.h"
43 #include "ddk/wdm.h"
45 #include "wine/unicode.h"
46 #include "wine/winbase16.h"
47 #include "kernel_private.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(sync);
53 /* check if current version is NT or Win95 */
54 static inline int is_version_nt(void)
56 return !(GetVersion() & 0x80000000);
59 /* returns directory handle to \\BaseNamedObjects */
60 HANDLE get_BaseNamedObjects_handle(void)
62 static HANDLE handle = NULL;
63 static const WCHAR basenameW[] =
64 {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
65 UNICODE_STRING str;
66 OBJECT_ATTRIBUTES attr;
68 if (!handle)
70 HANDLE dir;
72 RtlInitUnicodeString(&str, basenameW);
73 InitializeObjectAttributes(&attr, &str, 0, 0, NULL);
74 NtOpenDirectoryObject(&dir, DIRECTORY_CREATE_OBJECT|DIRECTORY_TRAVERSE,
75 &attr);
76 if (InterlockedCompareExchangePointer( (PVOID)&handle, dir, 0 ) != 0)
78 /* someone beat us here... */
79 CloseHandle( dir );
82 return handle;
85 /* helper for kernel32->ntdll timeout format conversion */
86 static inline PLARGE_INTEGER get_nt_timeout( PLARGE_INTEGER pTime, DWORD timeout )
88 if (timeout == INFINITE) return NULL;
89 pTime->QuadPart = (ULONGLONG)timeout * -10000;
90 return pTime;
93 /***********************************************************************
94 * Sleep (KERNEL32.@)
96 VOID WINAPI Sleep( DWORD timeout )
98 SleepEx( timeout, FALSE );
101 /******************************************************************************
102 * SleepEx (KERNEL32.@)
104 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
106 NTSTATUS status;
107 LARGE_INTEGER time;
109 status = NtDelayExecution( alertable, get_nt_timeout( &time, timeout ) );
110 if (status == STATUS_USER_APC) return WAIT_IO_COMPLETION;
111 return 0;
115 /***********************************************************************
116 * SwitchToThread (KERNEL32.@)
118 BOOL WINAPI SwitchToThread(void)
120 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
124 /***********************************************************************
125 * WaitForSingleObject (KERNEL32.@)
127 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
129 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
133 /***********************************************************************
134 * WaitForSingleObjectEx (KERNEL32.@)
136 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
137 BOOL alertable )
139 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
143 /***********************************************************************
144 * WaitForMultipleObjects (KERNEL32.@)
146 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
147 BOOL wait_all, DWORD timeout )
149 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
153 /***********************************************************************
154 * WaitForMultipleObjectsEx (KERNEL32.@)
156 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
157 BOOL wait_all, DWORD timeout,
158 BOOL alertable )
160 NTSTATUS status;
161 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
162 LARGE_INTEGER time;
163 unsigned int i;
165 if (count > MAXIMUM_WAIT_OBJECTS)
167 SetLastError(ERROR_INVALID_PARAMETER);
168 return WAIT_FAILED;
170 for (i = 0; i < count; i++)
172 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
173 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
174 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
175 hloc[i] = GetStdHandle( HandleToULong(handles[i]) );
176 else
177 hloc[i] = handles[i];
179 /* yes, even screen buffer console handles are waitable, and are
180 * handled as a handle to the console itself !!
182 if (is_console_handle(hloc[i]))
184 if (!VerifyConsoleIoHandle(hloc[i]))
186 return FALSE;
188 hloc[i] = GetConsoleInputWaitHandle();
192 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable,
193 get_nt_timeout( &time, timeout ) );
195 if (HIWORD(status)) /* is it an error code? */
197 SetLastError( RtlNtStatusToDosError(status) );
198 status = WAIT_FAILED;
200 return status;
204 /***********************************************************************
205 * WaitForSingleObject (KERNEL.460)
207 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
209 DWORD retval, mutex_count;
211 ReleaseThunkLock( &mutex_count );
212 retval = WaitForSingleObject( handle, timeout );
213 RestoreThunkLock( mutex_count );
214 return retval;
217 /***********************************************************************
218 * WaitForMultipleObjects (KERNEL.461)
220 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
221 BOOL wait_all, DWORD timeout )
223 DWORD retval, mutex_count;
225 ReleaseThunkLock( &mutex_count );
226 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
227 RestoreThunkLock( mutex_count );
228 return retval;
231 /***********************************************************************
232 * WaitForMultipleObjectsEx (KERNEL.495)
234 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
235 BOOL wait_all, DWORD timeout, BOOL alertable )
237 DWORD retval, mutex_count;
239 ReleaseThunkLock( &mutex_count );
240 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
241 RestoreThunkLock( mutex_count );
242 return retval;
245 /***********************************************************************
246 * RegisterWaitForSingleObject (KERNEL32.@)
248 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
249 WAITORTIMERCALLBACK Callback, PVOID Context,
250 ULONG dwMilliseconds, ULONG dwFlags)
252 NTSTATUS status;
254 TRACE("%p %p %p %p %d %d\n",
255 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
257 status = RtlRegisterWait( phNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
258 if (status != STATUS_SUCCESS)
260 SetLastError( RtlNtStatusToDosError(status) );
261 return FALSE;
263 return TRUE;
266 /***********************************************************************
267 * RegisterWaitForSingleObjectEx (KERNEL32.@)
269 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
270 WAITORTIMERCALLBACK Callback, PVOID Context,
271 ULONG dwMilliseconds, ULONG dwFlags )
273 NTSTATUS status;
274 HANDLE hNewWaitObject;
276 TRACE("%p %p %p %d %d\n",
277 hObject,Callback,Context,dwMilliseconds,dwFlags);
279 status = RtlRegisterWait( &hNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
280 if (status != STATUS_SUCCESS)
282 SetLastError( RtlNtStatusToDosError(status) );
283 return NULL;
285 return hNewWaitObject;
288 /***********************************************************************
289 * UnregisterWait (KERNEL32.@)
291 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
293 NTSTATUS status;
295 TRACE("%p\n",WaitHandle);
297 status = RtlDeregisterWait( WaitHandle );
298 if (status != STATUS_SUCCESS)
300 SetLastError( RtlNtStatusToDosError(status) );
301 return FALSE;
303 return TRUE;
306 /***********************************************************************
307 * UnregisterWaitEx (KERNEL32.@)
309 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
311 NTSTATUS status;
313 TRACE("%p %p\n",WaitHandle, CompletionEvent);
315 status = RtlDeregisterWaitEx( WaitHandle, CompletionEvent );
316 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
317 return !status;
320 /***********************************************************************
321 * SignalObjectAndWait (KERNEL32.@)
323 * Allows to atomically signal any of the synchro objects (semaphore,
324 * mutex, event) and wait on another.
326 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
327 DWORD dwMilliseconds, BOOL bAlertable )
329 NTSTATUS status;
330 LARGE_INTEGER timeout;
332 TRACE("%p %p %d %d\n", hObjectToSignal,
333 hObjectToWaitOn, dwMilliseconds, bAlertable);
335 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn, bAlertable,
336 get_nt_timeout( &timeout, dwMilliseconds ) );
337 if (HIWORD(status))
339 SetLastError( RtlNtStatusToDosError(status) );
340 status = WAIT_FAILED;
342 return status;
345 /***********************************************************************
346 * InitializeCriticalSection (KERNEL32.@)
348 * Initialise a critical section before use.
350 * PARAMS
351 * crit [O] Critical section to initialise.
353 * RETURNS
354 * Nothing. If the function fails an exception is raised.
356 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
358 InitializeCriticalSectionEx( crit, 0, 0 );
361 /***********************************************************************
362 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
364 * Initialise a critical section with a spin count.
366 * PARAMS
367 * crit [O] Critical section to initialise.
368 * spincount [I] Number of times to spin upon contention.
370 * RETURNS
371 * Success: TRUE.
372 * Failure: Nothing. If the function fails an exception is raised.
374 * NOTES
375 * spincount is ignored on uni-processor systems.
377 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
379 return InitializeCriticalSectionEx( crit, spincount, 0 );
382 /***********************************************************************
383 * InitializeCriticalSectionEx (KERNEL32.@)
385 * Initialise a critical section with a spin count and flags.
387 * PARAMS
388 * crit [O] Critical section to initialise.
389 * spincount [I] Number of times to spin upon contention.
390 * flags [I] CRITICAL_SECTION_ flags from winbase.h.
392 * RETURNS
393 * Success: TRUE.
394 * Failure: Nothing. If the function fails an exception is raised.
396 * NOTES
397 * spincount is ignored on uni-processor systems.
399 BOOL WINAPI InitializeCriticalSectionEx( CRITICAL_SECTION *crit, DWORD spincount, DWORD flags )
401 NTSTATUS ret = RtlInitializeCriticalSectionEx( crit, spincount, flags );
402 if (ret) RtlRaiseStatus( ret );
403 return !ret;
406 /***********************************************************************
407 * MakeCriticalSectionGlobal (KERNEL32.@)
409 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
411 /* let's assume that only one thread at a time will try to do this */
412 HANDLE sem = crit->LockSemaphore;
413 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
414 crit->LockSemaphore = ConvertToGlobalHandle( sem );
415 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
416 crit->DebugInfo = NULL;
420 /***********************************************************************
421 * ReinitializeCriticalSection (KERNEL32.@)
423 * Initialise an already used critical section.
425 * PARAMS
426 * crit [O] Critical section to initialise.
428 * RETURNS
429 * Nothing.
431 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
433 if ( !crit->LockSemaphore )
434 RtlInitializeCriticalSection( crit );
438 /***********************************************************************
439 * UninitializeCriticalSection (KERNEL32.@)
441 * UnInitialise a critical section after use.
443 * PARAMS
444 * crit [O] Critical section to uninitialise (destroy).
446 * RETURNS
447 * Nothing.
449 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
451 RtlDeleteCriticalSection( crit );
455 /***********************************************************************
456 * CreateEventA (KERNEL32.@)
458 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
459 BOOL initial_state, LPCSTR name )
461 DWORD flags = 0;
463 if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET;
464 if (initial_state) flags |= CREATE_EVENT_INITIAL_SET;
465 return CreateEventExA( sa, name, flags, EVENT_ALL_ACCESS );
469 /***********************************************************************
470 * CreateEventW (KERNEL32.@)
472 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
473 BOOL initial_state, LPCWSTR name )
475 DWORD flags = 0;
477 if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET;
478 if (initial_state) flags |= CREATE_EVENT_INITIAL_SET;
479 return CreateEventExW( sa, name, flags, EVENT_ALL_ACCESS );
483 /***********************************************************************
484 * CreateEventExA (KERNEL32.@)
486 HANDLE WINAPI CreateEventExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
488 WCHAR buffer[MAX_PATH];
490 if (!name) return CreateEventExW( sa, NULL, flags, access );
492 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
494 SetLastError( ERROR_FILENAME_EXCED_RANGE );
495 return 0;
497 return CreateEventExW( sa, buffer, flags, access );
501 /***********************************************************************
502 * CreateEventExW (KERNEL32.@)
504 HANDLE WINAPI CreateEventExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
506 HANDLE ret;
507 UNICODE_STRING nameW;
508 OBJECT_ATTRIBUTES attr;
509 NTSTATUS status;
511 /* one buggy program needs this
512 * ("Van Dale Groot woordenboek der Nederlandse taal")
514 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
516 ERR("Bad security attributes pointer %p\n",sa);
517 SetLastError( ERROR_INVALID_PARAMETER);
518 return 0;
521 attr.Length = sizeof(attr);
522 attr.RootDirectory = 0;
523 attr.ObjectName = NULL;
524 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
525 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
526 attr.SecurityQualityOfService = NULL;
527 if (name)
529 RtlInitUnicodeString( &nameW, name );
530 attr.ObjectName = &nameW;
531 attr.RootDirectory = get_BaseNamedObjects_handle();
534 status = NtCreateEvent( &ret, access, &attr, (flags & CREATE_EVENT_MANUAL_RESET) != 0,
535 (flags & CREATE_EVENT_INITIAL_SET) != 0 );
536 if (status == STATUS_OBJECT_NAME_EXISTS)
537 SetLastError( ERROR_ALREADY_EXISTS );
538 else
539 SetLastError( RtlNtStatusToDosError(status) );
540 return ret;
544 /***********************************************************************
545 * CreateW32Event (KERNEL.457)
547 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
549 return CreateEventW( NULL, manual_reset, initial_state, NULL );
553 /***********************************************************************
554 * OpenEventA (KERNEL32.@)
556 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
558 WCHAR buffer[MAX_PATH];
560 if (!name) return OpenEventW( access, inherit, NULL );
562 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
564 SetLastError( ERROR_FILENAME_EXCED_RANGE );
565 return 0;
567 return OpenEventW( access, inherit, buffer );
571 /***********************************************************************
572 * OpenEventW (KERNEL32.@)
574 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
576 HANDLE ret;
577 UNICODE_STRING nameW;
578 OBJECT_ATTRIBUTES attr;
579 NTSTATUS status;
581 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
583 attr.Length = sizeof(attr);
584 attr.RootDirectory = 0;
585 attr.ObjectName = NULL;
586 attr.Attributes = inherit ? OBJ_INHERIT : 0;
587 attr.SecurityDescriptor = NULL;
588 attr.SecurityQualityOfService = NULL;
589 if (name)
591 RtlInitUnicodeString( &nameW, name );
592 attr.ObjectName = &nameW;
593 attr.RootDirectory = get_BaseNamedObjects_handle();
596 status = NtOpenEvent( &ret, access, &attr );
597 if (status != STATUS_SUCCESS)
599 SetLastError( RtlNtStatusToDosError(status) );
600 return 0;
602 return ret;
605 /***********************************************************************
606 * PulseEvent (KERNEL32.@)
608 BOOL WINAPI PulseEvent( HANDLE handle )
610 NTSTATUS status;
612 if ((status = NtPulseEvent( handle, NULL )))
613 SetLastError( RtlNtStatusToDosError(status) );
614 return !status;
618 /***********************************************************************
619 * SetW32Event (KERNEL.458)
620 * SetEvent (KERNEL32.@)
622 BOOL WINAPI SetEvent( HANDLE handle )
624 NTSTATUS status;
626 if ((status = NtSetEvent( handle, NULL )))
627 SetLastError( RtlNtStatusToDosError(status) );
628 return !status;
632 /***********************************************************************
633 * ResetW32Event (KERNEL.459)
634 * ResetEvent (KERNEL32.@)
636 BOOL WINAPI ResetEvent( HANDLE handle )
638 NTSTATUS status;
640 if ((status = NtResetEvent( handle, NULL )))
641 SetLastError( RtlNtStatusToDosError(status) );
642 return !status;
646 /***********************************************************************
647 * NOTE: The Win95 VWin32_Event routines given below are really low-level
648 * routines implemented directly by VWin32. The user-mode libraries
649 * implement Win32 synchronisation routines on top of these low-level
650 * primitives. We do it the other way around here :-)
653 /***********************************************************************
654 * VWin32_EventCreate (KERNEL.442)
656 HANDLE WINAPI VWin32_EventCreate(VOID)
658 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
659 return ConvertToGlobalHandle( hEvent );
662 /***********************************************************************
663 * VWin32_EventDestroy (KERNEL.443)
665 VOID WINAPI VWin32_EventDestroy(HANDLE event)
667 CloseHandle( event );
670 /***********************************************************************
671 * VWin32_EventWait (KERNEL.450)
673 VOID WINAPI VWin32_EventWait(HANDLE event)
675 DWORD mutex_count;
677 ReleaseThunkLock( &mutex_count );
678 WaitForSingleObject( event, INFINITE );
679 RestoreThunkLock( mutex_count );
682 /***********************************************************************
683 * VWin32_EventSet (KERNEL.451)
684 * KERNEL_479 (KERNEL.479)
686 VOID WINAPI VWin32_EventSet(HANDLE event)
688 SetEvent( event );
693 /***********************************************************************
694 * CreateMutexA (KERNEL32.@)
696 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
698 return CreateMutexExA( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS );
702 /***********************************************************************
703 * CreateMutexW (KERNEL32.@)
705 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
707 return CreateMutexExW( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS );
711 /***********************************************************************
712 * CreateMutexExA (KERNEL32.@)
714 HANDLE WINAPI CreateMutexExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
716 WCHAR buffer[MAX_PATH];
718 if (!name) return CreateMutexExW( sa, NULL, flags, access );
720 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
722 SetLastError( ERROR_FILENAME_EXCED_RANGE );
723 return 0;
725 return CreateMutexExW( sa, buffer, flags, access );
729 /***********************************************************************
730 * CreateMutexExW (KERNEL32.@)
732 HANDLE WINAPI CreateMutexExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
734 HANDLE ret;
735 UNICODE_STRING nameW;
736 OBJECT_ATTRIBUTES attr;
737 NTSTATUS status;
739 attr.Length = sizeof(attr);
740 attr.RootDirectory = 0;
741 attr.ObjectName = NULL;
742 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
743 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
744 attr.SecurityQualityOfService = NULL;
745 if (name)
747 RtlInitUnicodeString( &nameW, name );
748 attr.ObjectName = &nameW;
749 attr.RootDirectory = get_BaseNamedObjects_handle();
752 status = NtCreateMutant( &ret, access, &attr, (flags & CREATE_MUTEX_INITIAL_OWNER) != 0 );
753 if (status == STATUS_OBJECT_NAME_EXISTS)
754 SetLastError( ERROR_ALREADY_EXISTS );
755 else
756 SetLastError( RtlNtStatusToDosError(status) );
757 return ret;
761 /***********************************************************************
762 * OpenMutexA (KERNEL32.@)
764 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
766 WCHAR buffer[MAX_PATH];
768 if (!name) return OpenMutexW( access, inherit, NULL );
770 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
772 SetLastError( ERROR_FILENAME_EXCED_RANGE );
773 return 0;
775 return OpenMutexW( access, inherit, buffer );
779 /***********************************************************************
780 * OpenMutexW (KERNEL32.@)
782 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
784 HANDLE ret;
785 UNICODE_STRING nameW;
786 OBJECT_ATTRIBUTES attr;
787 NTSTATUS status;
789 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
791 attr.Length = sizeof(attr);
792 attr.RootDirectory = 0;
793 attr.ObjectName = NULL;
794 attr.Attributes = inherit ? OBJ_INHERIT : 0;
795 attr.SecurityDescriptor = NULL;
796 attr.SecurityQualityOfService = NULL;
797 if (name)
799 RtlInitUnicodeString( &nameW, name );
800 attr.ObjectName = &nameW;
801 attr.RootDirectory = get_BaseNamedObjects_handle();
804 status = NtOpenMutant( &ret, access, &attr );
805 if (status != STATUS_SUCCESS)
807 SetLastError( RtlNtStatusToDosError(status) );
808 return 0;
810 return ret;
814 /***********************************************************************
815 * ReleaseMutex (KERNEL32.@)
817 BOOL WINAPI ReleaseMutex( HANDLE handle )
819 NTSTATUS status;
821 status = NtReleaseMutant(handle, NULL);
822 if (status != STATUS_SUCCESS)
824 SetLastError( RtlNtStatusToDosError(status) );
825 return FALSE;
827 return TRUE;
832 * Semaphores
836 /***********************************************************************
837 * CreateSemaphoreA (KERNEL32.@)
839 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
841 return CreateSemaphoreExA( sa, initial, max, name, 0, SEMAPHORE_ALL_ACCESS );
845 /***********************************************************************
846 * CreateSemaphoreW (KERNEL32.@)
848 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
849 LONG max, LPCWSTR name )
851 return CreateSemaphoreExW( sa, initial, max, name, 0, SEMAPHORE_ALL_ACCESS );
855 /***********************************************************************
856 * CreateSemaphoreExA (KERNEL32.@)
858 HANDLE WINAPI CreateSemaphoreExA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name,
859 DWORD flags, DWORD access )
861 WCHAR buffer[MAX_PATH];
863 if (!name) return CreateSemaphoreExW( sa, initial, max, NULL, flags, access );
865 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
867 SetLastError( ERROR_FILENAME_EXCED_RANGE );
868 return 0;
870 return CreateSemaphoreExW( sa, initial, max, buffer, flags, access );
874 /***********************************************************************
875 * CreateSemaphoreExW (KERNEL32.@)
877 HANDLE WINAPI CreateSemaphoreExW( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCWSTR name,
878 DWORD flags, DWORD access )
880 HANDLE ret;
881 UNICODE_STRING nameW;
882 OBJECT_ATTRIBUTES attr;
883 NTSTATUS status;
885 attr.Length = sizeof(attr);
886 attr.RootDirectory = 0;
887 attr.ObjectName = NULL;
888 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
889 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
890 attr.SecurityQualityOfService = NULL;
891 if (name)
893 RtlInitUnicodeString( &nameW, name );
894 attr.ObjectName = &nameW;
895 attr.RootDirectory = get_BaseNamedObjects_handle();
898 status = NtCreateSemaphore( &ret, access, &attr, initial, max );
899 if (status == STATUS_OBJECT_NAME_EXISTS)
900 SetLastError( ERROR_ALREADY_EXISTS );
901 else
902 SetLastError( RtlNtStatusToDosError(status) );
903 return ret;
907 /***********************************************************************
908 * OpenSemaphoreA (KERNEL32.@)
910 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
912 WCHAR buffer[MAX_PATH];
914 if (!name) return OpenSemaphoreW( access, inherit, NULL );
916 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
918 SetLastError( ERROR_FILENAME_EXCED_RANGE );
919 return 0;
921 return OpenSemaphoreW( access, inherit, buffer );
925 /***********************************************************************
926 * OpenSemaphoreW (KERNEL32.@)
928 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
930 HANDLE ret;
931 UNICODE_STRING nameW;
932 OBJECT_ATTRIBUTES attr;
933 NTSTATUS status;
935 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
937 attr.Length = sizeof(attr);
938 attr.RootDirectory = 0;
939 attr.ObjectName = NULL;
940 attr.Attributes = inherit ? OBJ_INHERIT : 0;
941 attr.SecurityDescriptor = NULL;
942 attr.SecurityQualityOfService = NULL;
943 if (name)
945 RtlInitUnicodeString( &nameW, name );
946 attr.ObjectName = &nameW;
947 attr.RootDirectory = get_BaseNamedObjects_handle();
950 status = NtOpenSemaphore( &ret, access, &attr );
951 if (status != STATUS_SUCCESS)
953 SetLastError( RtlNtStatusToDosError(status) );
954 return 0;
956 return ret;
960 /***********************************************************************
961 * ReleaseSemaphore (KERNEL32.@)
963 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
965 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
966 if (status) SetLastError( RtlNtStatusToDosError(status) );
967 return !status;
972 * Jobs
975 /******************************************************************************
976 * CreateJobObjectW (KERNEL32.@)
978 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES sa, LPCWSTR name )
980 HANDLE ret = 0;
981 UNICODE_STRING nameW;
982 OBJECT_ATTRIBUTES attr;
983 NTSTATUS status;
985 attr.Length = sizeof(attr);
986 attr.RootDirectory = 0;
987 attr.ObjectName = NULL;
988 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
989 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
990 attr.SecurityQualityOfService = NULL;
991 if (name)
993 RtlInitUnicodeString( &nameW, name );
994 attr.ObjectName = &nameW;
995 attr.RootDirectory = get_BaseNamedObjects_handle();
998 status = NtCreateJobObject( &ret, JOB_OBJECT_ALL_ACCESS, &attr );
999 if (status == STATUS_OBJECT_NAME_EXISTS)
1000 SetLastError( ERROR_ALREADY_EXISTS );
1001 else
1002 SetLastError( RtlNtStatusToDosError(status) );
1003 return ret;
1006 /******************************************************************************
1007 * CreateJobObjectA (KERNEL32.@)
1009 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1011 WCHAR buffer[MAX_PATH];
1013 if (!name) return CreateJobObjectW( attr, NULL );
1015 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1017 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1018 return 0;
1020 return CreateJobObjectW( attr, buffer );
1023 /******************************************************************************
1024 * OpenJobObjectW (KERNEL32.@)
1026 HANDLE WINAPI OpenJobObjectW( DWORD access, BOOL inherit, LPCWSTR name )
1028 HANDLE ret;
1029 UNICODE_STRING nameW;
1030 OBJECT_ATTRIBUTES attr;
1031 NTSTATUS status;
1033 attr.Length = sizeof(attr);
1034 attr.RootDirectory = 0;
1035 attr.ObjectName = NULL;
1036 attr.Attributes = inherit ? OBJ_INHERIT : 0;
1037 attr.SecurityDescriptor = NULL;
1038 attr.SecurityQualityOfService = NULL;
1039 if (name)
1041 RtlInitUnicodeString( &nameW, name );
1042 attr.ObjectName = &nameW;
1043 attr.RootDirectory = get_BaseNamedObjects_handle();
1046 status = NtOpenJobObject( &ret, access, &attr );
1047 if (status != STATUS_SUCCESS)
1049 SetLastError( RtlNtStatusToDosError(status) );
1050 return 0;
1052 return ret;
1055 /******************************************************************************
1056 * OpenJobObjectA (KERNEL32.@)
1058 HANDLE WINAPI OpenJobObjectA( DWORD access, BOOL inherit, LPCSTR name )
1060 WCHAR buffer[MAX_PATH];
1062 if (!name) return OpenJobObjectW( access, inherit, NULL );
1064 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1066 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1067 return 0;
1069 return OpenJobObjectW( access, inherit, buffer );
1072 /******************************************************************************
1073 * TerminateJobObject (KERNEL32.@)
1075 BOOL WINAPI TerminateJobObject( HANDLE job, UINT exit_code )
1077 NTSTATUS status = NtTerminateJobObject( job, exit_code );
1078 if (status) SetLastError( RtlNtStatusToDosError(status) );
1079 return !status;
1082 /******************************************************************************
1083 * QueryInformationJobObject (KERNEL32.@)
1085 BOOL WINAPI QueryInformationJobObject( HANDLE job, JOBOBJECTINFOCLASS class, LPVOID info,
1086 DWORD len, DWORD *ret_len )
1088 NTSTATUS status = NtQueryInformationJobObject( job, class, info, len, ret_len );
1089 if (status) SetLastError( RtlNtStatusToDosError(status) );
1090 return !status;
1093 /******************************************************************************
1094 * SetInformationJobObject (KERNEL32.@)
1096 BOOL WINAPI SetInformationJobObject( HANDLE job, JOBOBJECTINFOCLASS class, LPVOID info, DWORD len )
1098 NTSTATUS status = NtSetInformationJobObject( job, class, info, len );
1099 if (status) SetLastError( RtlNtStatusToDosError(status) );
1100 return !status;
1103 /******************************************************************************
1104 * AssignProcessToJobObject (KERNEL32.@)
1106 BOOL WINAPI AssignProcessToJobObject( HANDLE job, HANDLE process )
1108 NTSTATUS status = NtAssignProcessToJobObject( job, process );
1109 if (status) SetLastError( RtlNtStatusToDosError(status) );
1110 return !status;
1113 /******************************************************************************
1114 * IsProcessInJob (KERNEL32.@)
1116 BOOL WINAPI IsProcessInJob( HANDLE process, HANDLE job, PBOOL result )
1118 NTSTATUS status = NtIsProcessInJob( job, process );
1119 switch(status)
1121 case STATUS_PROCESS_IN_JOB:
1122 *result = TRUE;
1123 return TRUE;
1124 case STATUS_PROCESS_NOT_IN_JOB:
1125 *result = FALSE;
1126 return TRUE;
1127 default:
1128 SetLastError( RtlNtStatusToDosError(status) );
1129 return FALSE;
1135 * Timers
1139 /***********************************************************************
1140 * CreateWaitableTimerA (KERNEL32.@)
1142 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
1144 return CreateWaitableTimerExA( sa, name, manual ? CREATE_WAITABLE_TIMER_MANUAL_RESET : 0,
1145 TIMER_ALL_ACCESS );
1149 /***********************************************************************
1150 * CreateWaitableTimerW (KERNEL32.@)
1152 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
1154 return CreateWaitableTimerExW( sa, name, manual ? CREATE_WAITABLE_TIMER_MANUAL_RESET : 0,
1155 TIMER_ALL_ACCESS );
1159 /***********************************************************************
1160 * CreateWaitableTimerExA (KERNEL32.@)
1162 HANDLE WINAPI CreateWaitableTimerExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
1164 WCHAR buffer[MAX_PATH];
1166 if (!name) return CreateWaitableTimerExW( sa, NULL, flags, access );
1168 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1170 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1171 return 0;
1173 return CreateWaitableTimerExW( sa, buffer, flags, access );
1177 /***********************************************************************
1178 * CreateWaitableTimerExW (KERNEL32.@)
1180 HANDLE WINAPI CreateWaitableTimerExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
1182 HANDLE handle;
1183 NTSTATUS status;
1184 UNICODE_STRING nameW;
1185 OBJECT_ATTRIBUTES attr;
1187 attr.Length = sizeof(attr);
1188 attr.RootDirectory = 0;
1189 attr.ObjectName = NULL;
1190 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1191 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1192 attr.SecurityQualityOfService = NULL;
1193 if (name)
1195 RtlInitUnicodeString( &nameW, name );
1196 attr.ObjectName = &nameW;
1197 attr.RootDirectory = get_BaseNamedObjects_handle();
1200 status = NtCreateTimer( &handle, access, &attr,
1201 (flags & CREATE_WAITABLE_TIMER_MANUAL_RESET) ? NotificationTimer : SynchronizationTimer );
1202 if (status == STATUS_OBJECT_NAME_EXISTS)
1203 SetLastError( ERROR_ALREADY_EXISTS );
1204 else
1205 SetLastError( RtlNtStatusToDosError(status) );
1206 return handle;
1210 /***********************************************************************
1211 * OpenWaitableTimerA (KERNEL32.@)
1213 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
1215 WCHAR buffer[MAX_PATH];
1217 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
1219 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1221 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1222 return 0;
1224 return OpenWaitableTimerW( access, inherit, buffer );
1228 /***********************************************************************
1229 * OpenWaitableTimerW (KERNEL32.@)
1231 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
1233 HANDLE handle;
1234 UNICODE_STRING nameW;
1235 OBJECT_ATTRIBUTES attr;
1236 NTSTATUS status;
1238 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
1240 attr.Length = sizeof(attr);
1241 attr.RootDirectory = 0;
1242 attr.ObjectName = NULL;
1243 attr.Attributes = inherit ? OBJ_INHERIT : 0;
1244 attr.SecurityDescriptor = NULL;
1245 attr.SecurityQualityOfService = NULL;
1246 if (name)
1248 RtlInitUnicodeString( &nameW, name );
1249 attr.ObjectName = &nameW;
1250 attr.RootDirectory = get_BaseNamedObjects_handle();
1253 status = NtOpenTimer(&handle, access, &attr);
1254 if (status != STATUS_SUCCESS)
1256 SetLastError( RtlNtStatusToDosError(status) );
1257 return 0;
1259 return handle;
1263 /***********************************************************************
1264 * SetWaitableTimer (KERNEL32.@)
1266 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
1267 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
1269 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
1270 arg, resume, period, NULL);
1272 if (status != STATUS_SUCCESS)
1274 SetLastError( RtlNtStatusToDosError(status) );
1275 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
1277 return TRUE;
1281 /***********************************************************************
1282 * CancelWaitableTimer (KERNEL32.@)
1284 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1286 NTSTATUS status;
1288 status = NtCancelTimer(handle, NULL);
1289 if (status != STATUS_SUCCESS)
1291 SetLastError( RtlNtStatusToDosError(status) );
1292 return FALSE;
1294 return TRUE;
1298 /***********************************************************************
1299 * CreateTimerQueue (KERNEL32.@)
1301 HANDLE WINAPI CreateTimerQueue(void)
1303 HANDLE q;
1304 NTSTATUS status = RtlCreateTimerQueue(&q);
1306 if (status != STATUS_SUCCESS)
1308 SetLastError( RtlNtStatusToDosError(status) );
1309 return NULL;
1312 return q;
1316 /***********************************************************************
1317 * DeleteTimerQueueEx (KERNEL32.@)
1319 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1321 NTSTATUS status = RtlDeleteTimerQueueEx(TimerQueue, CompletionEvent);
1323 if (status != STATUS_SUCCESS)
1325 SetLastError( RtlNtStatusToDosError(status) );
1326 return FALSE;
1329 return TRUE;
1332 /***********************************************************************
1333 * CreateTimerQueueTimer (KERNEL32.@)
1335 * Creates a timer-queue timer. This timer expires at the specified due
1336 * time (in ms), then after every specified period (in ms). When the timer
1337 * expires, the callback function is called.
1339 * RETURNS
1340 * nonzero on success or zero on failure
1342 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1343 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1344 DWORD DueTime, DWORD Period, ULONG Flags )
1346 NTSTATUS status = RtlCreateTimer(phNewTimer, TimerQueue, Callback,
1347 Parameter, DueTime, Period, Flags);
1349 if (status != STATUS_SUCCESS)
1351 SetLastError( RtlNtStatusToDosError(status) );
1352 return FALSE;
1355 return TRUE;
1358 /***********************************************************************
1359 * ChangeTimerQueueTimer (KERNEL32.@)
1361 * Changes the times at which the timer expires.
1363 * RETURNS
1364 * nonzero on success or zero on failure
1366 BOOL WINAPI ChangeTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1367 ULONG DueTime, ULONG Period )
1369 NTSTATUS status = RtlUpdateTimer(TimerQueue, Timer, DueTime, Period);
1371 if (status != STATUS_SUCCESS)
1373 SetLastError( RtlNtStatusToDosError(status) );
1374 return FALSE;
1377 return TRUE;
1380 /***********************************************************************
1381 * DeleteTimerQueueTimer (KERNEL32.@)
1383 * Cancels a timer-queue timer.
1385 * RETURNS
1386 * nonzero on success or zero on failure
1388 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1389 HANDLE CompletionEvent )
1391 NTSTATUS status = RtlDeleteTimer(TimerQueue, Timer, CompletionEvent);
1392 if (status != STATUS_SUCCESS)
1394 SetLastError( RtlNtStatusToDosError(status) );
1395 return FALSE;
1397 return TRUE;
1402 * Pipes
1406 /***********************************************************************
1407 * CreateNamedPipeA (KERNEL32.@)
1409 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1410 DWORD dwPipeMode, DWORD nMaxInstances,
1411 DWORD nOutBufferSize, DWORD nInBufferSize,
1412 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1414 WCHAR buffer[MAX_PATH];
1416 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1417 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1419 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1421 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1422 return INVALID_HANDLE_VALUE;
1424 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1425 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1429 /***********************************************************************
1430 * CreateNamedPipeW (KERNEL32.@)
1432 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1433 DWORD dwPipeMode, DWORD nMaxInstances,
1434 DWORD nOutBufferSize, DWORD nInBufferSize,
1435 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1437 HANDLE handle;
1438 UNICODE_STRING nt_name;
1439 OBJECT_ATTRIBUTES attr;
1440 DWORD access, options;
1441 BOOLEAN pipe_type, read_mode, non_block;
1442 NTSTATUS status;
1443 IO_STATUS_BLOCK iosb;
1444 LARGE_INTEGER timeout;
1446 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1447 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1448 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1450 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1452 SetLastError( ERROR_PATH_NOT_FOUND );
1453 return INVALID_HANDLE_VALUE;
1455 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1457 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1458 RtlFreeUnicodeString( &nt_name );
1459 return INVALID_HANDLE_VALUE;
1462 attr.Length = sizeof(attr);
1463 attr.RootDirectory = 0;
1464 attr.ObjectName = &nt_name;
1465 attr.Attributes = OBJ_CASE_INSENSITIVE |
1466 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1467 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1468 attr.SecurityQualityOfService = NULL;
1470 switch(dwOpenMode & 3)
1472 case PIPE_ACCESS_INBOUND:
1473 options = FILE_PIPE_INBOUND;
1474 access = GENERIC_READ;
1475 break;
1476 case PIPE_ACCESS_OUTBOUND:
1477 options = FILE_PIPE_OUTBOUND;
1478 access = GENERIC_WRITE;
1479 break;
1480 case PIPE_ACCESS_DUPLEX:
1481 options = FILE_PIPE_FULL_DUPLEX;
1482 access = GENERIC_READ | GENERIC_WRITE;
1483 break;
1484 default:
1485 SetLastError( ERROR_INVALID_PARAMETER );
1486 return INVALID_HANDLE_VALUE;
1488 access |= SYNCHRONIZE;
1489 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1490 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1491 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1492 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1493 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1494 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0U;
1496 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1498 SetLastError(0);
1500 status = NtCreateNamedPipeFile(&handle, access, &attr, &iosb, 0,
1501 FILE_OVERWRITE_IF, options, pipe_type,
1502 read_mode, non_block, nMaxInstances,
1503 nInBufferSize, nOutBufferSize, &timeout);
1505 RtlFreeUnicodeString( &nt_name );
1506 if (status)
1508 handle = INVALID_HANDLE_VALUE;
1509 SetLastError( RtlNtStatusToDosError(status) );
1511 return handle;
1515 /***********************************************************************
1516 * PeekNamedPipe (KERNEL32.@)
1518 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1519 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1521 FILE_PIPE_PEEK_BUFFER local_buffer;
1522 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1523 IO_STATUS_BLOCK io;
1524 NTSTATUS status;
1526 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1527 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1529 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1530 return FALSE;
1533 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1534 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1535 if (!status)
1537 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1538 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1539 if (lpcbRead) *lpcbRead = read_size;
1540 if (lpcbMessage) *lpcbMessage = 0; /* FIXME */
1541 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1543 else SetLastError( RtlNtStatusToDosError(status) );
1545 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1546 return !status;
1549 /***********************************************************************
1550 * WaitNamedPipeA (KERNEL32.@)
1552 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1554 WCHAR buffer[MAX_PATH];
1556 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1558 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1560 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1561 return 0;
1563 return WaitNamedPipeW( buffer, nTimeOut );
1567 /***********************************************************************
1568 * WaitNamedPipeW (KERNEL32.@)
1570 * Waits for a named pipe instance to become available
1572 * PARAMS
1573 * name [I] Pointer to a named pipe name to wait for
1574 * nTimeOut [I] How long to wait in ms
1576 * RETURNS
1577 * TRUE: Success, named pipe can be opened with CreateFile
1578 * FALSE: Failure, GetLastError can be called for further details
1580 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1582 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1583 NTSTATUS status;
1584 UNICODE_STRING nt_name, pipe_dev_name;
1585 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1586 IO_STATUS_BLOCK iosb;
1587 OBJECT_ATTRIBUTES attr;
1588 ULONG sz_pipe_wait;
1589 HANDLE pipe_dev;
1591 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1593 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1594 return FALSE;
1596 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1597 nt_name.Length < sizeof(leadin) ||
1598 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR)) != 0)
1600 RtlFreeUnicodeString( &nt_name );
1601 SetLastError( ERROR_PATH_NOT_FOUND );
1602 return FALSE;
1605 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1606 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1608 RtlFreeUnicodeString( &nt_name );
1609 SetLastError( ERROR_OUTOFMEMORY );
1610 return FALSE;
1613 pipe_dev_name.Buffer = nt_name.Buffer;
1614 pipe_dev_name.Length = sizeof(leadin);
1615 pipe_dev_name.MaximumLength = sizeof(leadin);
1616 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1617 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1618 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1619 FILE_SYNCHRONOUS_IO_NONALERT);
1620 if (status != ERROR_SUCCESS)
1622 SetLastError( ERROR_PATH_NOT_FOUND );
1623 return FALSE;
1626 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1627 if (nTimeOut == NMPWAIT_WAIT_FOREVER)
1628 pipe_wait->Timeout.QuadPart = ((ULONGLONG)0x7fffffff << 32) | 0xffffffff;
1629 else
1630 pipe_wait->Timeout.QuadPart = (ULONGLONG)nTimeOut * -10000;
1631 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1632 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1633 pipe_wait->NameLength);
1634 RtlFreeUnicodeString( &nt_name );
1636 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1637 pipe_wait, sz_pipe_wait, NULL, 0 );
1639 HeapFree( GetProcessHeap(), 0, pipe_wait );
1640 NtClose( pipe_dev );
1642 if(status != STATUS_SUCCESS)
1644 SetLastError(RtlNtStatusToDosError(status));
1645 return FALSE;
1647 else
1648 return TRUE;
1652 /***********************************************************************
1653 * ConnectNamedPipe (KERNEL32.@)
1655 * Connects to a named pipe
1657 * Parameters
1658 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1659 * overlapped: Optional OVERLAPPED struct
1661 * Return values
1662 * TRUE: Success
1663 * FALSE: Failure, GetLastError can be called for further details
1665 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1667 NTSTATUS status;
1668 IO_STATUS_BLOCK status_block;
1669 LPVOID cvalue = NULL;
1671 TRACE("(%p,%p)\n", hPipe, overlapped);
1673 if(overlapped)
1675 overlapped->Internal = STATUS_PENDING;
1676 overlapped->InternalHigh = 0;
1677 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1680 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, cvalue,
1681 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1682 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1684 if (status == STATUS_SUCCESS) return TRUE;
1685 SetLastError( RtlNtStatusToDosError(status) );
1686 return FALSE;
1689 /***********************************************************************
1690 * DisconnectNamedPipe (KERNEL32.@)
1692 * Disconnects from a named pipe
1694 * Parameters
1695 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1697 * Return values
1698 * TRUE: Success
1699 * FALSE: Failure, GetLastError can be called for further details
1701 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1703 NTSTATUS status;
1704 IO_STATUS_BLOCK io_block;
1706 TRACE("(%p)\n",hPipe);
1708 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1709 NULL, 0, NULL, 0);
1710 if (status == STATUS_SUCCESS) return TRUE;
1711 SetLastError( RtlNtStatusToDosError(status) );
1712 return FALSE;
1715 /***********************************************************************
1716 * TransactNamedPipe (KERNEL32.@)
1718 * BUGS
1719 * should be done as a single operation in the wineserver or kernel
1721 BOOL WINAPI TransactNamedPipe(
1722 HANDLE handle, LPVOID write_buf, DWORD write_size, LPVOID read_buf,
1723 DWORD read_size, LPDWORD bytes_read, LPOVERLAPPED overlapped)
1725 BOOL r;
1726 DWORD count;
1728 TRACE("%p %p %d %p %d %p %p\n",
1729 handle, write_buf, write_size, read_buf,
1730 read_size, bytes_read, overlapped);
1732 if (overlapped)
1734 FIXME("Doesn't support overlapped operation as yet\n");
1735 return FALSE;
1738 r = WriteFile(handle, write_buf, write_size, &count, NULL);
1739 if (r)
1740 r = ReadFile(handle, read_buf, read_size, bytes_read, NULL);
1742 return r;
1745 /***********************************************************************
1746 * GetNamedPipeInfo (KERNEL32.@)
1748 BOOL WINAPI GetNamedPipeInfo(
1749 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1750 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1752 FILE_PIPE_LOCAL_INFORMATION fpli;
1753 IO_STATUS_BLOCK iosb;
1754 NTSTATUS status;
1756 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1757 FilePipeLocalInformation);
1758 if (status)
1760 SetLastError( RtlNtStatusToDosError(status) );
1761 return FALSE;
1764 if (lpFlags)
1766 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1767 PIPE_SERVER_END : PIPE_CLIENT_END;
1768 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1769 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1772 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1773 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1774 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1776 return TRUE;
1779 /***********************************************************************
1780 * GetNamedPipeHandleStateA (KERNEL32.@)
1782 BOOL WINAPI GetNamedPipeHandleStateA(
1783 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1784 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1785 LPSTR lpUsername, DWORD nUsernameMaxSize)
1787 FIXME("%p %p %p %p %p %p %d\n",
1788 hNamedPipe, lpState, lpCurInstances,
1789 lpMaxCollectionCount, lpCollectDataTimeout,
1790 lpUsername, nUsernameMaxSize);
1792 return FALSE;
1795 /***********************************************************************
1796 * GetNamedPipeHandleStateW (KERNEL32.@)
1798 BOOL WINAPI GetNamedPipeHandleStateW(
1799 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1800 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1801 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1803 FIXME("%p %p %p %p %p %p %d\n",
1804 hNamedPipe, lpState, lpCurInstances,
1805 lpMaxCollectionCount, lpCollectDataTimeout,
1806 lpUsername, nUsernameMaxSize);
1808 return FALSE;
1811 /***********************************************************************
1812 * SetNamedPipeHandleState (KERNEL32.@)
1814 BOOL WINAPI SetNamedPipeHandleState(
1815 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1816 LPDWORD lpCollectDataTimeout)
1818 /* should be a fixme, but this function is called a lot by the RPC
1819 * runtime, and it slows down InstallShield a fair bit. */
1820 WARN("stub: %p %p/%d %p %p\n",
1821 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1822 return FALSE;
1825 /***********************************************************************
1826 * CallNamedPipeA (KERNEL32.@)
1828 BOOL WINAPI CallNamedPipeA(
1829 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1830 LPVOID lpOutput, DWORD dwOutputSize,
1831 LPDWORD lpBytesRead, DWORD nTimeout)
1833 DWORD len;
1834 LPWSTR str = NULL;
1835 BOOL ret;
1837 TRACE("%s %p %d %p %d %p %d\n",
1838 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1839 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1841 if( lpNamedPipeName )
1843 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1844 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1845 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1847 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1848 dwOutputSize, lpBytesRead, nTimeout );
1849 if( lpNamedPipeName )
1850 HeapFree( GetProcessHeap(), 0, str );
1852 return ret;
1855 /***********************************************************************
1856 * CallNamedPipeW (KERNEL32.@)
1858 BOOL WINAPI CallNamedPipeW(
1859 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1860 LPVOID lpOutput, DWORD lpOutputSize,
1861 LPDWORD lpBytesRead, DWORD nTimeout)
1863 HANDLE pipe;
1864 BOOL ret;
1865 DWORD mode;
1867 TRACE("%s %p %d %p %d %p %d\n",
1868 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1869 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1871 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1872 if (pipe == INVALID_HANDLE_VALUE)
1874 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1875 if (!ret)
1876 return FALSE;
1877 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1878 if (pipe == INVALID_HANDLE_VALUE)
1879 return FALSE;
1882 mode = PIPE_READMODE_MESSAGE;
1883 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1885 /* Currently SetNamedPipeHandleState() is a stub returning FALSE */
1886 if (ret) FIXME("Now that SetNamedPipeHandleState() is more than a stub, please update CallNamedPipeW\n");
1888 if (!ret)
1890 CloseHandle(pipe);
1891 return FALSE;
1894 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1895 CloseHandle(pipe);
1896 if (!ret)
1897 return FALSE;
1899 return TRUE;
1902 /******************************************************************
1903 * CreatePipe (KERNEL32.@)
1906 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1907 LPSECURITY_ATTRIBUTES sa, DWORD size )
1909 static unsigned index /* = 0 */;
1910 WCHAR name[64];
1911 HANDLE hr, hw;
1912 unsigned in_index = index;
1913 UNICODE_STRING nt_name;
1914 OBJECT_ATTRIBUTES attr;
1915 NTSTATUS status;
1916 IO_STATUS_BLOCK iosb;
1917 LARGE_INTEGER timeout;
1919 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1921 attr.Length = sizeof(attr);
1922 attr.RootDirectory = 0;
1923 attr.ObjectName = &nt_name;
1924 attr.Attributes = OBJ_CASE_INSENSITIVE |
1925 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1926 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1927 attr.SecurityQualityOfService = NULL;
1929 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1930 /* generate a unique pipe name (system wide) */
1933 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1934 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1935 'u','.','%','0','8','u','\0' };
1937 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1938 GetCurrentProcessId(), ++index);
1939 RtlInitUnicodeString(&nt_name, name);
1940 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1941 0, FILE_OVERWRITE_IF,
1942 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1943 FALSE, FALSE, FALSE,
1944 1, size, size, &timeout);
1945 if (status)
1947 SetLastError( RtlNtStatusToDosError(status) );
1948 hr = INVALID_HANDLE_VALUE;
1950 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1951 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1952 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1954 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1955 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1957 if (status)
1959 SetLastError( RtlNtStatusToDosError(status) );
1960 NtClose(hr);
1961 return FALSE;
1964 *hReadPipe = hr;
1965 *hWritePipe = hw;
1966 return TRUE;
1970 /******************************************************************************
1971 * CreateMailslotA [KERNEL32.@]
1973 * See CreateMailslotW.
1975 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1976 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1978 DWORD len;
1979 HANDLE handle;
1980 LPWSTR name = NULL;
1982 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1983 nMaxMessageSize, lReadTimeout, sa);
1985 if( lpName )
1987 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1988 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1989 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1992 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1994 HeapFree( GetProcessHeap(), 0, name );
1996 return handle;
2000 /******************************************************************************
2001 * CreateMailslotW [KERNEL32.@]
2003 * Create a mailslot with specified name.
2005 * PARAMS
2006 * lpName [I] Pointer to string for mailslot name
2007 * nMaxMessageSize [I] Maximum message size
2008 * lReadTimeout [I] Milliseconds before read time-out
2009 * sa [I] Pointer to security structure
2011 * RETURNS
2012 * Success: Handle to mailslot
2013 * Failure: INVALID_HANDLE_VALUE
2015 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
2016 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
2018 HANDLE handle = INVALID_HANDLE_VALUE;
2019 OBJECT_ATTRIBUTES attr;
2020 UNICODE_STRING nameW;
2021 LARGE_INTEGER timeout;
2022 IO_STATUS_BLOCK iosb;
2023 NTSTATUS status;
2025 TRACE("%s %d %d %p\n", debugstr_w(lpName),
2026 nMaxMessageSize, lReadTimeout, sa);
2028 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
2030 SetLastError( ERROR_PATH_NOT_FOUND );
2031 return INVALID_HANDLE_VALUE;
2034 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
2036 SetLastError( ERROR_FILENAME_EXCED_RANGE );
2037 RtlFreeUnicodeString( &nameW );
2038 return INVALID_HANDLE_VALUE;
2041 attr.Length = sizeof(attr);
2042 attr.RootDirectory = 0;
2043 attr.Attributes = OBJ_CASE_INSENSITIVE;
2044 attr.ObjectName = &nameW;
2045 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
2046 attr.SecurityQualityOfService = NULL;
2048 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
2049 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
2050 else
2051 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
2053 status = NtCreateMailslotFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr,
2054 &iosb, 0, 0, nMaxMessageSize, &timeout );
2055 if (status)
2057 SetLastError( RtlNtStatusToDosError(status) );
2058 handle = INVALID_HANDLE_VALUE;
2061 RtlFreeUnicodeString( &nameW );
2062 return handle;
2066 /******************************************************************************
2067 * GetMailslotInfo [KERNEL32.@]
2069 * Retrieve information about a mailslot.
2071 * PARAMS
2072 * hMailslot [I] Mailslot handle
2073 * lpMaxMessageSize [O] Address of maximum message size
2074 * lpNextSize [O] Address of size of next message
2075 * lpMessageCount [O] Address of number of messages
2076 * lpReadTimeout [O] Address of read time-out
2078 * RETURNS
2079 * Success: TRUE
2080 * Failure: FALSE
2082 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
2083 LPDWORD lpNextSize, LPDWORD lpMessageCount,
2084 LPDWORD lpReadTimeout )
2086 FILE_MAILSLOT_QUERY_INFORMATION info;
2087 IO_STATUS_BLOCK iosb;
2088 NTSTATUS status;
2090 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
2091 lpNextSize, lpMessageCount, lpReadTimeout);
2093 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
2094 FileMailslotQueryInformation );
2096 if( status != STATUS_SUCCESS )
2098 SetLastError( RtlNtStatusToDosError(status) );
2099 return FALSE;
2102 if( lpMaxMessageSize )
2103 *lpMaxMessageSize = info.MaximumMessageSize;
2104 if( lpNextSize )
2105 *lpNextSize = info.NextMessageSize;
2106 if( lpMessageCount )
2107 *lpMessageCount = info.MessagesAvailable;
2108 if( lpReadTimeout )
2110 if (info.ReadTimeout.QuadPart == (((LONGLONG)0x7fffffff << 32) | 0xffffffff))
2111 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
2112 else
2113 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
2115 return TRUE;
2119 /******************************************************************************
2120 * SetMailslotInfo [KERNEL32.@]
2122 * Set the read timeout of a mailslot.
2124 * PARAMS
2125 * hMailslot [I] Mailslot handle
2126 * dwReadTimeout [I] Timeout in milliseconds.
2128 * RETURNS
2129 * Success: TRUE
2130 * Failure: FALSE
2132 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
2134 FILE_MAILSLOT_SET_INFORMATION info;
2135 IO_STATUS_BLOCK iosb;
2136 NTSTATUS status;
2138 TRACE("%p %d\n", hMailslot, dwReadTimeout);
2140 if (dwReadTimeout != MAILSLOT_WAIT_FOREVER)
2141 info.ReadTimeout.QuadPart = (ULONGLONG)dwReadTimeout * -10000;
2142 else
2143 info.ReadTimeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
2144 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
2145 FileMailslotSetInformation );
2146 if( status != STATUS_SUCCESS )
2148 SetLastError( RtlNtStatusToDosError(status) );
2149 return FALSE;
2151 return TRUE;
2155 /******************************************************************************
2156 * CreateIoCompletionPort (KERNEL32.@)
2158 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
2159 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
2161 NTSTATUS status;
2162 HANDLE ret = 0;
2164 TRACE("(%p, %p, %08lx, %08x)\n",
2165 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
2167 if (hExistingCompletionPort && hFileHandle == INVALID_HANDLE_VALUE)
2169 SetLastError( ERROR_INVALID_PARAMETER);
2170 return NULL;
2173 if (hExistingCompletionPort)
2174 ret = hExistingCompletionPort;
2175 else
2177 status = NtCreateIoCompletion( &ret, IO_COMPLETION_ALL_ACCESS, NULL, dwNumberOfConcurrentThreads );
2178 if (status != STATUS_SUCCESS) goto fail;
2181 if (hFileHandle != INVALID_HANDLE_VALUE)
2183 FILE_COMPLETION_INFORMATION info;
2184 IO_STATUS_BLOCK iosb;
2186 info.CompletionPort = ret;
2187 info.CompletionKey = CompletionKey;
2188 status = NtSetInformationFile( hFileHandle, &iosb, &info, sizeof(info), FileCompletionInformation );
2189 if (status != STATUS_SUCCESS) goto fail;
2192 return ret;
2194 fail:
2195 if (ret && !hExistingCompletionPort)
2196 CloseHandle( ret );
2197 SetLastError( RtlNtStatusToDosError(status) );
2198 return 0;
2201 /******************************************************************************
2202 * GetQueuedCompletionStatus (KERNEL32.@)
2204 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
2205 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
2206 DWORD dwMilliseconds )
2208 NTSTATUS status;
2209 IO_STATUS_BLOCK iosb;
2210 LARGE_INTEGER wait_time;
2212 TRACE("(%p,%p,%p,%p,%d)\n",
2213 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
2215 *lpOverlapped = NULL;
2217 status = NtRemoveIoCompletion( CompletionPort, pCompletionKey, (PULONG_PTR)lpOverlapped,
2218 &iosb, get_nt_timeout( &wait_time, dwMilliseconds ) );
2219 if (status == STATUS_SUCCESS)
2221 *lpNumberOfBytesTransferred = iosb.Information;
2222 return TRUE;
2225 SetLastError( RtlNtStatusToDosError(status) );
2226 return FALSE;
2230 /******************************************************************************
2231 * PostQueuedCompletionStatus (KERNEL32.@)
2233 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
2234 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
2236 NTSTATUS status;
2238 TRACE("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
2240 status = NtSetIoCompletion( CompletionPort, dwCompletionKey, (ULONG_PTR)lpOverlapped,
2241 STATUS_SUCCESS, dwNumberOfBytes );
2243 if (status == STATUS_SUCCESS) return TRUE;
2244 SetLastError( RtlNtStatusToDosError(status) );
2245 return FALSE;
2248 /******************************************************************************
2249 * BindIoCompletionCallback (KERNEL32.@)
2251 BOOL WINAPI BindIoCompletionCallback( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags)
2253 NTSTATUS status;
2255 TRACE("(%p, %p, %d)\n", FileHandle, Function, Flags);
2257 status = RtlSetIoCompletionCallback( FileHandle, (PRTL_OVERLAPPED_COMPLETION_ROUTINE)Function, Flags );
2258 if (status == STATUS_SUCCESS) return TRUE;
2259 SetLastError( RtlNtStatusToDosError(status) );
2260 return FALSE;
2263 #ifdef __i386__
2265 /***********************************************************************
2266 * InterlockedCompareExchange (KERNEL32.@)
2268 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
2269 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
2270 "movl 12(%esp),%eax\n\t"
2271 "movl 8(%esp),%ecx\n\t"
2272 "movl 4(%esp),%edx\n\t"
2273 "lock; cmpxchgl %ecx,(%edx)\n\t"
2274 "ret $12")
2276 /***********************************************************************
2277 * InterlockedExchange (KERNEL32.@)
2279 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
2280 __ASM_GLOBAL_FUNC(InterlockedExchange,
2281 "movl 8(%esp),%eax\n\t"
2282 "movl 4(%esp),%edx\n\t"
2283 "lock; xchgl %eax,(%edx)\n\t"
2284 "ret $8")
2286 /***********************************************************************
2287 * InterlockedExchangeAdd (KERNEL32.@)
2289 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
2290 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
2291 "movl 8(%esp),%eax\n\t"
2292 "movl 4(%esp),%edx\n\t"
2293 "lock; xaddl %eax,(%edx)\n\t"
2294 "ret $8")
2296 /***********************************************************************
2297 * InterlockedIncrement (KERNEL32.@)
2299 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2300 __ASM_GLOBAL_FUNC(InterlockedIncrement,
2301 "movl 4(%esp),%edx\n\t"
2302 "movl $1,%eax\n\t"
2303 "lock; xaddl %eax,(%edx)\n\t"
2304 "incl %eax\n\t"
2305 "ret $4")
2307 /***********************************************************************
2308 * InterlockedDecrement (KERNEL32.@)
2310 __ASM_GLOBAL_FUNC(InterlockedDecrement,
2311 "movl 4(%esp),%edx\n\t"
2312 "movl $-1,%eax\n\t"
2313 "lock; xaddl %eax,(%edx)\n\t"
2314 "decl %eax\n\t"
2315 "ret $4")
2317 #else /* __i386__ */
2319 /***********************************************************************
2320 * InterlockedCompareExchange (KERNEL32.@)
2322 * Atomically swap one value with another.
2324 * PARAMS
2325 * dest [I/O] The value to replace
2326 * xchq [I] The value to be swapped
2327 * compare [I] The value to compare to dest
2329 * RETURNS
2330 * The resulting value of dest.
2332 * NOTES
2333 * dest is updated only if it is equal to compare, otherwise no swap is done.
2335 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
2337 return interlocked_cmpxchg( (int *)dest, xchg, compare );
2340 /***********************************************************************
2341 * InterlockedExchange (KERNEL32.@)
2343 * Atomically swap one value with another.
2345 * PARAMS
2346 * dest [I/O] The value to replace
2347 * val [I] The value to be swapped
2349 * RETURNS
2350 * The resulting value of dest.
2352 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
2354 return interlocked_xchg( (int *)dest, val );
2357 /***********************************************************************
2358 * InterlockedExchangeAdd (KERNEL32.@)
2360 * Atomically add one value to another.
2362 * PARAMS
2363 * dest [I/O] The value to add to
2364 * incr [I] The value to be added
2366 * RETURNS
2367 * The resulting value of dest.
2369 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
2371 return interlocked_xchg_add( (int *)dest, incr );
2374 /***********************************************************************
2375 * InterlockedIncrement (KERNEL32.@)
2377 * Atomically increment a value.
2379 * PARAMS
2380 * dest [I/O] The value to increment
2382 * RETURNS
2383 * The resulting value of dest.
2385 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
2387 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
2390 /***********************************************************************
2391 * InterlockedDecrement (KERNEL32.@)
2393 * Atomically decrement a value.
2395 * PARAMS
2396 * dest [I/O] The value to decrement
2398 * RETURNS
2399 * The resulting value of dest.
2401 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
2403 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
2406 #endif /* __i386__ */