push eb25bf65c4616aa55a810ed5c29198b1a080b208
[wine/hacks.git] / dlls / kernel32 / sync.c
blob38002d7c584a3fd4760f924adab2f6c6fef51453
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 * DeleteTimerQueue (KERNEL32.@)
1335 BOOL WINAPI DeleteTimerQueue(HANDLE TimerQueue)
1337 return DeleteTimerQueueEx(TimerQueue, NULL);
1340 /***********************************************************************
1341 * CreateTimerQueueTimer (KERNEL32.@)
1343 * Creates a timer-queue timer. This timer expires at the specified due
1344 * time (in ms), then after every specified period (in ms). When the timer
1345 * expires, the callback function is called.
1347 * RETURNS
1348 * nonzero on success or zero on failure
1350 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1351 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1352 DWORD DueTime, DWORD Period, ULONG Flags )
1354 NTSTATUS status = RtlCreateTimer(phNewTimer, TimerQueue, Callback,
1355 Parameter, DueTime, Period, Flags);
1357 if (status != STATUS_SUCCESS)
1359 SetLastError( RtlNtStatusToDosError(status) );
1360 return FALSE;
1363 return TRUE;
1366 /***********************************************************************
1367 * ChangeTimerQueueTimer (KERNEL32.@)
1369 * Changes the times at which the timer expires.
1371 * RETURNS
1372 * nonzero on success or zero on failure
1374 BOOL WINAPI ChangeTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1375 ULONG DueTime, ULONG Period )
1377 NTSTATUS status = RtlUpdateTimer(TimerQueue, Timer, DueTime, Period);
1379 if (status != STATUS_SUCCESS)
1381 SetLastError( RtlNtStatusToDosError(status) );
1382 return FALSE;
1385 return TRUE;
1388 /***********************************************************************
1389 * DeleteTimerQueueTimer (KERNEL32.@)
1391 * Cancels a timer-queue timer.
1393 * RETURNS
1394 * nonzero on success or zero on failure
1396 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1397 HANDLE CompletionEvent )
1399 NTSTATUS status = RtlDeleteTimer(TimerQueue, Timer, CompletionEvent);
1400 if (status != STATUS_SUCCESS)
1402 SetLastError( RtlNtStatusToDosError(status) );
1403 return FALSE;
1405 return TRUE;
1410 * Pipes
1414 /***********************************************************************
1415 * CreateNamedPipeA (KERNEL32.@)
1417 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1418 DWORD dwPipeMode, DWORD nMaxInstances,
1419 DWORD nOutBufferSize, DWORD nInBufferSize,
1420 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1422 WCHAR buffer[MAX_PATH];
1424 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1425 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1427 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1429 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1430 return INVALID_HANDLE_VALUE;
1432 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1433 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1437 /***********************************************************************
1438 * CreateNamedPipeW (KERNEL32.@)
1440 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1441 DWORD dwPipeMode, DWORD nMaxInstances,
1442 DWORD nOutBufferSize, DWORD nInBufferSize,
1443 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1445 HANDLE handle;
1446 UNICODE_STRING nt_name;
1447 OBJECT_ATTRIBUTES attr;
1448 DWORD access, options;
1449 BOOLEAN pipe_type, read_mode, non_block;
1450 NTSTATUS status;
1451 IO_STATUS_BLOCK iosb;
1452 LARGE_INTEGER timeout;
1454 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1455 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1456 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1458 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1460 SetLastError( ERROR_PATH_NOT_FOUND );
1461 return INVALID_HANDLE_VALUE;
1463 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1465 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1466 RtlFreeUnicodeString( &nt_name );
1467 return INVALID_HANDLE_VALUE;
1470 attr.Length = sizeof(attr);
1471 attr.RootDirectory = 0;
1472 attr.ObjectName = &nt_name;
1473 attr.Attributes = OBJ_CASE_INSENSITIVE |
1474 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1475 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1476 attr.SecurityQualityOfService = NULL;
1478 switch(dwOpenMode & 3)
1480 case PIPE_ACCESS_INBOUND:
1481 options = FILE_PIPE_INBOUND;
1482 access = GENERIC_READ;
1483 break;
1484 case PIPE_ACCESS_OUTBOUND:
1485 options = FILE_PIPE_OUTBOUND;
1486 access = GENERIC_WRITE;
1487 break;
1488 case PIPE_ACCESS_DUPLEX:
1489 options = FILE_PIPE_FULL_DUPLEX;
1490 access = GENERIC_READ | GENERIC_WRITE;
1491 break;
1492 default:
1493 SetLastError( ERROR_INVALID_PARAMETER );
1494 return INVALID_HANDLE_VALUE;
1496 access |= SYNCHRONIZE;
1497 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1498 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1499 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1500 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1501 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1502 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0U;
1504 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1506 SetLastError(0);
1508 status = NtCreateNamedPipeFile(&handle, access, &attr, &iosb, 0,
1509 FILE_OVERWRITE_IF, options, pipe_type,
1510 read_mode, non_block, nMaxInstances,
1511 nInBufferSize, nOutBufferSize, &timeout);
1513 RtlFreeUnicodeString( &nt_name );
1514 if (status)
1516 handle = INVALID_HANDLE_VALUE;
1517 SetLastError( RtlNtStatusToDosError(status) );
1519 return handle;
1523 /***********************************************************************
1524 * PeekNamedPipe (KERNEL32.@)
1526 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1527 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1529 FILE_PIPE_PEEK_BUFFER local_buffer;
1530 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1531 IO_STATUS_BLOCK io;
1532 NTSTATUS status;
1534 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1535 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1537 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1538 return FALSE;
1541 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1542 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1543 if (!status)
1545 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1546 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1547 if (lpcbRead) *lpcbRead = read_size;
1548 if (lpcbMessage) *lpcbMessage = 0; /* FIXME */
1549 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1551 else SetLastError( RtlNtStatusToDosError(status) );
1553 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1554 return !status;
1557 /***********************************************************************
1558 * WaitNamedPipeA (KERNEL32.@)
1560 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1562 WCHAR buffer[MAX_PATH];
1564 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1566 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1568 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1569 return 0;
1571 return WaitNamedPipeW( buffer, nTimeOut );
1575 /***********************************************************************
1576 * WaitNamedPipeW (KERNEL32.@)
1578 * Waits for a named pipe instance to become available
1580 * PARAMS
1581 * name [I] Pointer to a named pipe name to wait for
1582 * nTimeOut [I] How long to wait in ms
1584 * RETURNS
1585 * TRUE: Success, named pipe can be opened with CreateFile
1586 * FALSE: Failure, GetLastError can be called for further details
1588 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1590 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1591 NTSTATUS status;
1592 UNICODE_STRING nt_name, pipe_dev_name;
1593 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1594 IO_STATUS_BLOCK iosb;
1595 OBJECT_ATTRIBUTES attr;
1596 ULONG sz_pipe_wait;
1597 HANDLE pipe_dev;
1599 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1601 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1602 return FALSE;
1604 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1605 nt_name.Length < sizeof(leadin) ||
1606 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR)) != 0)
1608 RtlFreeUnicodeString( &nt_name );
1609 SetLastError( ERROR_PATH_NOT_FOUND );
1610 return FALSE;
1613 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1614 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1616 RtlFreeUnicodeString( &nt_name );
1617 SetLastError( ERROR_OUTOFMEMORY );
1618 return FALSE;
1621 pipe_dev_name.Buffer = nt_name.Buffer;
1622 pipe_dev_name.Length = sizeof(leadin);
1623 pipe_dev_name.MaximumLength = sizeof(leadin);
1624 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1625 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1626 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1627 FILE_SYNCHRONOUS_IO_NONALERT);
1628 if (status != ERROR_SUCCESS)
1630 SetLastError( ERROR_PATH_NOT_FOUND );
1631 return FALSE;
1634 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1635 if (nTimeOut == NMPWAIT_WAIT_FOREVER)
1636 pipe_wait->Timeout.QuadPart = ((ULONGLONG)0x7fffffff << 32) | 0xffffffff;
1637 else
1638 pipe_wait->Timeout.QuadPart = (ULONGLONG)nTimeOut * -10000;
1639 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1640 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1641 pipe_wait->NameLength);
1642 RtlFreeUnicodeString( &nt_name );
1644 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1645 pipe_wait, sz_pipe_wait, NULL, 0 );
1647 HeapFree( GetProcessHeap(), 0, pipe_wait );
1648 NtClose( pipe_dev );
1650 if(status != STATUS_SUCCESS)
1652 SetLastError(RtlNtStatusToDosError(status));
1653 return FALSE;
1655 else
1656 return TRUE;
1660 /***********************************************************************
1661 * ConnectNamedPipe (KERNEL32.@)
1663 * Connects to a named pipe
1665 * Parameters
1666 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1667 * overlapped: Optional OVERLAPPED struct
1669 * Return values
1670 * TRUE: Success
1671 * FALSE: Failure, GetLastError can be called for further details
1673 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1675 NTSTATUS status;
1676 IO_STATUS_BLOCK status_block;
1677 LPVOID cvalue = NULL;
1679 TRACE("(%p,%p)\n", hPipe, overlapped);
1681 if(overlapped)
1683 overlapped->Internal = STATUS_PENDING;
1684 overlapped->InternalHigh = 0;
1685 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1688 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, cvalue,
1689 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1690 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1692 if (status == STATUS_SUCCESS) return TRUE;
1693 SetLastError( RtlNtStatusToDosError(status) );
1694 return FALSE;
1697 /***********************************************************************
1698 * DisconnectNamedPipe (KERNEL32.@)
1700 * Disconnects from a named pipe
1702 * Parameters
1703 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1705 * Return values
1706 * TRUE: Success
1707 * FALSE: Failure, GetLastError can be called for further details
1709 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1711 NTSTATUS status;
1712 IO_STATUS_BLOCK io_block;
1714 TRACE("(%p)\n",hPipe);
1716 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1717 NULL, 0, NULL, 0);
1718 if (status == STATUS_SUCCESS) return TRUE;
1719 SetLastError( RtlNtStatusToDosError(status) );
1720 return FALSE;
1723 /***********************************************************************
1724 * TransactNamedPipe (KERNEL32.@)
1726 * BUGS
1727 * should be done as a single operation in the wineserver or kernel
1729 BOOL WINAPI TransactNamedPipe(
1730 HANDLE handle, LPVOID write_buf, DWORD write_size, LPVOID read_buf,
1731 DWORD read_size, LPDWORD bytes_read, LPOVERLAPPED overlapped)
1733 BOOL r;
1734 DWORD count;
1736 TRACE("%p %p %d %p %d %p %p\n",
1737 handle, write_buf, write_size, read_buf,
1738 read_size, bytes_read, overlapped);
1740 if (overlapped)
1742 FIXME("Doesn't support overlapped operation as yet\n");
1743 return FALSE;
1746 r = WriteFile(handle, write_buf, write_size, &count, NULL);
1747 if (r)
1748 r = ReadFile(handle, read_buf, read_size, bytes_read, NULL);
1750 return r;
1753 /***********************************************************************
1754 * GetNamedPipeInfo (KERNEL32.@)
1756 BOOL WINAPI GetNamedPipeInfo(
1757 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1758 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1760 FILE_PIPE_LOCAL_INFORMATION fpli;
1761 IO_STATUS_BLOCK iosb;
1762 NTSTATUS status;
1764 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1765 FilePipeLocalInformation);
1766 if (status)
1768 SetLastError( RtlNtStatusToDosError(status) );
1769 return FALSE;
1772 if (lpFlags)
1774 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1775 PIPE_SERVER_END : PIPE_CLIENT_END;
1776 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1777 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1780 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1781 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1782 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1784 return TRUE;
1787 /***********************************************************************
1788 * GetNamedPipeHandleStateA (KERNEL32.@)
1790 BOOL WINAPI GetNamedPipeHandleStateA(
1791 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1792 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1793 LPSTR lpUsername, DWORD nUsernameMaxSize)
1795 FIXME("%p %p %p %p %p %p %d\n",
1796 hNamedPipe, lpState, lpCurInstances,
1797 lpMaxCollectionCount, lpCollectDataTimeout,
1798 lpUsername, nUsernameMaxSize);
1800 return FALSE;
1803 /***********************************************************************
1804 * GetNamedPipeHandleStateW (KERNEL32.@)
1806 BOOL WINAPI GetNamedPipeHandleStateW(
1807 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1808 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1809 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1811 FIXME("%p %p %p %p %p %p %d\n",
1812 hNamedPipe, lpState, lpCurInstances,
1813 lpMaxCollectionCount, lpCollectDataTimeout,
1814 lpUsername, nUsernameMaxSize);
1816 return FALSE;
1819 /***********************************************************************
1820 * SetNamedPipeHandleState (KERNEL32.@)
1822 BOOL WINAPI SetNamedPipeHandleState(
1823 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1824 LPDWORD lpCollectDataTimeout)
1826 /* should be a fixme, but this function is called a lot by the RPC
1827 * runtime, and it slows down InstallShield a fair bit. */
1828 WARN("stub: %p %p/%d %p %p\n",
1829 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1830 return FALSE;
1833 /***********************************************************************
1834 * CallNamedPipeA (KERNEL32.@)
1836 BOOL WINAPI CallNamedPipeA(
1837 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1838 LPVOID lpOutput, DWORD dwOutputSize,
1839 LPDWORD lpBytesRead, DWORD nTimeout)
1841 DWORD len;
1842 LPWSTR str = NULL;
1843 BOOL ret;
1845 TRACE("%s %p %d %p %d %p %d\n",
1846 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1847 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1849 if( lpNamedPipeName )
1851 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1852 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1853 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1855 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1856 dwOutputSize, lpBytesRead, nTimeout );
1857 if( lpNamedPipeName )
1858 HeapFree( GetProcessHeap(), 0, str );
1860 return ret;
1863 /***********************************************************************
1864 * CallNamedPipeW (KERNEL32.@)
1866 BOOL WINAPI CallNamedPipeW(
1867 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1868 LPVOID lpOutput, DWORD lpOutputSize,
1869 LPDWORD lpBytesRead, DWORD nTimeout)
1871 HANDLE pipe;
1872 BOOL ret;
1873 DWORD mode;
1875 TRACE("%s %p %d %p %d %p %d\n",
1876 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1877 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1879 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1880 if (pipe == INVALID_HANDLE_VALUE)
1882 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1883 if (!ret)
1884 return FALSE;
1885 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1886 if (pipe == INVALID_HANDLE_VALUE)
1887 return FALSE;
1890 mode = PIPE_READMODE_MESSAGE;
1891 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1893 /* Currently SetNamedPipeHandleState() is a stub returning FALSE */
1894 if (ret) FIXME("Now that SetNamedPipeHandleState() is more than a stub, please update CallNamedPipeW\n");
1896 if (!ret)
1898 CloseHandle(pipe);
1899 return FALSE;
1902 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1903 CloseHandle(pipe);
1904 if (!ret)
1905 return FALSE;
1907 return TRUE;
1910 /******************************************************************
1911 * CreatePipe (KERNEL32.@)
1914 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1915 LPSECURITY_ATTRIBUTES sa, DWORD size )
1917 static unsigned index /* = 0 */;
1918 WCHAR name[64];
1919 HANDLE hr, hw;
1920 unsigned in_index = index;
1921 UNICODE_STRING nt_name;
1922 OBJECT_ATTRIBUTES attr;
1923 NTSTATUS status;
1924 IO_STATUS_BLOCK iosb;
1925 LARGE_INTEGER timeout;
1927 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1929 attr.Length = sizeof(attr);
1930 attr.RootDirectory = 0;
1931 attr.ObjectName = &nt_name;
1932 attr.Attributes = OBJ_CASE_INSENSITIVE |
1933 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1934 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1935 attr.SecurityQualityOfService = NULL;
1937 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1938 /* generate a unique pipe name (system wide) */
1941 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1942 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1943 'u','.','%','0','8','u','\0' };
1945 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1946 GetCurrentProcessId(), ++index);
1947 RtlInitUnicodeString(&nt_name, name);
1948 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1949 0, FILE_OVERWRITE_IF,
1950 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1951 FALSE, FALSE, FALSE,
1952 1, size, size, &timeout);
1953 if (status)
1955 SetLastError( RtlNtStatusToDosError(status) );
1956 hr = INVALID_HANDLE_VALUE;
1958 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1959 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1960 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1962 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1963 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1965 if (status)
1967 SetLastError( RtlNtStatusToDosError(status) );
1968 NtClose(hr);
1969 return FALSE;
1972 *hReadPipe = hr;
1973 *hWritePipe = hw;
1974 return TRUE;
1978 /******************************************************************************
1979 * CreateMailslotA [KERNEL32.@]
1981 * See CreateMailslotW.
1983 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1984 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1986 DWORD len;
1987 HANDLE handle;
1988 LPWSTR name = NULL;
1990 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1991 nMaxMessageSize, lReadTimeout, sa);
1993 if( lpName )
1995 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1996 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1997 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
2000 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
2002 HeapFree( GetProcessHeap(), 0, name );
2004 return handle;
2008 /******************************************************************************
2009 * CreateMailslotW [KERNEL32.@]
2011 * Create a mailslot with specified name.
2013 * PARAMS
2014 * lpName [I] Pointer to string for mailslot name
2015 * nMaxMessageSize [I] Maximum message size
2016 * lReadTimeout [I] Milliseconds before read time-out
2017 * sa [I] Pointer to security structure
2019 * RETURNS
2020 * Success: Handle to mailslot
2021 * Failure: INVALID_HANDLE_VALUE
2023 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
2024 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
2026 HANDLE handle = INVALID_HANDLE_VALUE;
2027 OBJECT_ATTRIBUTES attr;
2028 UNICODE_STRING nameW;
2029 LARGE_INTEGER timeout;
2030 IO_STATUS_BLOCK iosb;
2031 NTSTATUS status;
2033 TRACE("%s %d %d %p\n", debugstr_w(lpName),
2034 nMaxMessageSize, lReadTimeout, sa);
2036 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
2038 SetLastError( ERROR_PATH_NOT_FOUND );
2039 return INVALID_HANDLE_VALUE;
2042 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
2044 SetLastError( ERROR_FILENAME_EXCED_RANGE );
2045 RtlFreeUnicodeString( &nameW );
2046 return INVALID_HANDLE_VALUE;
2049 attr.Length = sizeof(attr);
2050 attr.RootDirectory = 0;
2051 attr.Attributes = OBJ_CASE_INSENSITIVE;
2052 attr.ObjectName = &nameW;
2053 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
2054 attr.SecurityQualityOfService = NULL;
2056 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
2057 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
2058 else
2059 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
2061 status = NtCreateMailslotFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr,
2062 &iosb, 0, 0, nMaxMessageSize, &timeout );
2063 if (status)
2065 SetLastError( RtlNtStatusToDosError(status) );
2066 handle = INVALID_HANDLE_VALUE;
2069 RtlFreeUnicodeString( &nameW );
2070 return handle;
2074 /******************************************************************************
2075 * GetMailslotInfo [KERNEL32.@]
2077 * Retrieve information about a mailslot.
2079 * PARAMS
2080 * hMailslot [I] Mailslot handle
2081 * lpMaxMessageSize [O] Address of maximum message size
2082 * lpNextSize [O] Address of size of next message
2083 * lpMessageCount [O] Address of number of messages
2084 * lpReadTimeout [O] Address of read time-out
2086 * RETURNS
2087 * Success: TRUE
2088 * Failure: FALSE
2090 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
2091 LPDWORD lpNextSize, LPDWORD lpMessageCount,
2092 LPDWORD lpReadTimeout )
2094 FILE_MAILSLOT_QUERY_INFORMATION info;
2095 IO_STATUS_BLOCK iosb;
2096 NTSTATUS status;
2098 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
2099 lpNextSize, lpMessageCount, lpReadTimeout);
2101 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
2102 FileMailslotQueryInformation );
2104 if( status != STATUS_SUCCESS )
2106 SetLastError( RtlNtStatusToDosError(status) );
2107 return FALSE;
2110 if( lpMaxMessageSize )
2111 *lpMaxMessageSize = info.MaximumMessageSize;
2112 if( lpNextSize )
2113 *lpNextSize = info.NextMessageSize;
2114 if( lpMessageCount )
2115 *lpMessageCount = info.MessagesAvailable;
2116 if( lpReadTimeout )
2118 if (info.ReadTimeout.QuadPart == (((LONGLONG)0x7fffffff << 32) | 0xffffffff))
2119 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
2120 else
2121 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
2123 return TRUE;
2127 /******************************************************************************
2128 * SetMailslotInfo [KERNEL32.@]
2130 * Set the read timeout of a mailslot.
2132 * PARAMS
2133 * hMailslot [I] Mailslot handle
2134 * dwReadTimeout [I] Timeout in milliseconds.
2136 * RETURNS
2137 * Success: TRUE
2138 * Failure: FALSE
2140 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
2142 FILE_MAILSLOT_SET_INFORMATION info;
2143 IO_STATUS_BLOCK iosb;
2144 NTSTATUS status;
2146 TRACE("%p %d\n", hMailslot, dwReadTimeout);
2148 if (dwReadTimeout != MAILSLOT_WAIT_FOREVER)
2149 info.ReadTimeout.QuadPart = (ULONGLONG)dwReadTimeout * -10000;
2150 else
2151 info.ReadTimeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
2152 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
2153 FileMailslotSetInformation );
2154 if( status != STATUS_SUCCESS )
2156 SetLastError( RtlNtStatusToDosError(status) );
2157 return FALSE;
2159 return TRUE;
2163 /******************************************************************************
2164 * CreateIoCompletionPort (KERNEL32.@)
2166 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
2167 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
2169 NTSTATUS status;
2170 HANDLE ret = 0;
2172 TRACE("(%p, %p, %08lx, %08x)\n",
2173 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
2175 if (hExistingCompletionPort && hFileHandle == INVALID_HANDLE_VALUE)
2177 SetLastError( ERROR_INVALID_PARAMETER);
2178 return NULL;
2181 if (hExistingCompletionPort)
2182 ret = hExistingCompletionPort;
2183 else
2185 status = NtCreateIoCompletion( &ret, IO_COMPLETION_ALL_ACCESS, NULL, dwNumberOfConcurrentThreads );
2186 if (status != STATUS_SUCCESS) goto fail;
2189 if (hFileHandle != INVALID_HANDLE_VALUE)
2191 FILE_COMPLETION_INFORMATION info;
2192 IO_STATUS_BLOCK iosb;
2194 info.CompletionPort = ret;
2195 info.CompletionKey = CompletionKey;
2196 status = NtSetInformationFile( hFileHandle, &iosb, &info, sizeof(info), FileCompletionInformation );
2197 if (status != STATUS_SUCCESS) goto fail;
2200 return ret;
2202 fail:
2203 if (ret && !hExistingCompletionPort)
2204 CloseHandle( ret );
2205 SetLastError( RtlNtStatusToDosError(status) );
2206 return 0;
2209 /******************************************************************************
2210 * GetQueuedCompletionStatus (KERNEL32.@)
2212 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
2213 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
2214 DWORD dwMilliseconds )
2216 NTSTATUS status;
2217 IO_STATUS_BLOCK iosb;
2218 LARGE_INTEGER wait_time;
2220 TRACE("(%p,%p,%p,%p,%d)\n",
2221 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
2223 *lpOverlapped = NULL;
2225 status = NtRemoveIoCompletion( CompletionPort, pCompletionKey, (PULONG_PTR)lpOverlapped,
2226 &iosb, get_nt_timeout( &wait_time, dwMilliseconds ) );
2227 if (status == STATUS_SUCCESS)
2229 *lpNumberOfBytesTransferred = iosb.Information;
2230 return TRUE;
2233 if (status == STATUS_TIMEOUT) SetLastError( WAIT_TIMEOUT );
2234 else SetLastError( RtlNtStatusToDosError(status) );
2235 return FALSE;
2239 /******************************************************************************
2240 * PostQueuedCompletionStatus (KERNEL32.@)
2242 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
2243 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
2245 NTSTATUS status;
2247 TRACE("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
2249 status = NtSetIoCompletion( CompletionPort, dwCompletionKey, (ULONG_PTR)lpOverlapped,
2250 STATUS_SUCCESS, dwNumberOfBytes );
2252 if (status == STATUS_SUCCESS) return TRUE;
2253 SetLastError( RtlNtStatusToDosError(status) );
2254 return FALSE;
2257 /******************************************************************************
2258 * BindIoCompletionCallback (KERNEL32.@)
2260 BOOL WINAPI BindIoCompletionCallback( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags)
2262 NTSTATUS status;
2264 TRACE("(%p, %p, %d)\n", FileHandle, Function, Flags);
2266 status = RtlSetIoCompletionCallback( FileHandle, (PRTL_OVERLAPPED_COMPLETION_ROUTINE)Function, Flags );
2267 if (status == STATUS_SUCCESS) return TRUE;
2268 SetLastError( RtlNtStatusToDosError(status) );
2269 return FALSE;
2272 #ifdef __i386__
2274 /***********************************************************************
2275 * InterlockedCompareExchange (KERNEL32.@)
2277 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
2278 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
2279 "movl 12(%esp),%eax\n\t"
2280 "movl 8(%esp),%ecx\n\t"
2281 "movl 4(%esp),%edx\n\t"
2282 "lock; cmpxchgl %ecx,(%edx)\n\t"
2283 "ret $12")
2285 /***********************************************************************
2286 * InterlockedExchange (KERNEL32.@)
2288 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
2289 __ASM_GLOBAL_FUNC(InterlockedExchange,
2290 "movl 8(%esp),%eax\n\t"
2291 "movl 4(%esp),%edx\n\t"
2292 "lock; xchgl %eax,(%edx)\n\t"
2293 "ret $8")
2295 /***********************************************************************
2296 * InterlockedExchangeAdd (KERNEL32.@)
2298 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
2299 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
2300 "movl 8(%esp),%eax\n\t"
2301 "movl 4(%esp),%edx\n\t"
2302 "lock; xaddl %eax,(%edx)\n\t"
2303 "ret $8")
2305 /***********************************************************************
2306 * InterlockedIncrement (KERNEL32.@)
2308 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2309 __ASM_GLOBAL_FUNC(InterlockedIncrement,
2310 "movl 4(%esp),%edx\n\t"
2311 "movl $1,%eax\n\t"
2312 "lock; xaddl %eax,(%edx)\n\t"
2313 "incl %eax\n\t"
2314 "ret $4")
2316 /***********************************************************************
2317 * InterlockedDecrement (KERNEL32.@)
2319 __ASM_GLOBAL_FUNC(InterlockedDecrement,
2320 "movl 4(%esp),%edx\n\t"
2321 "movl $-1,%eax\n\t"
2322 "lock; xaddl %eax,(%edx)\n\t"
2323 "decl %eax\n\t"
2324 "ret $4")
2326 #endif /* __i386__ */