dwrite: Fix recently added script properties.
[wine.git] / dlls / kernel32 / sync.c
blob13fb0f0a99a543755b63f28114dc384b64b81eb2
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 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #define NONAMELESSUNION
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "winnls.h"
39 #include "winternl.h"
40 #include "winioctl.h"
41 #include "ddk/wdm.h"
43 #include "wine/unicode.h"
44 #include "kernel_private.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(sync);
50 /* check if current version is NT or Win95 */
51 static inline BOOL is_version_nt(void)
53 return !(GetVersion() & 0x80000000);
56 /* returns directory handle to \\BaseNamedObjects */
57 HANDLE get_BaseNamedObjects_handle(void)
59 static HANDLE handle = NULL;
60 static const WCHAR basenameW[] =
61 {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
62 UNICODE_STRING str;
63 OBJECT_ATTRIBUTES attr;
65 if (!handle)
67 HANDLE dir;
69 RtlInitUnicodeString(&str, basenameW);
70 InitializeObjectAttributes(&attr, &str, 0, 0, NULL);
71 NtOpenDirectoryObject(&dir, DIRECTORY_CREATE_OBJECT|DIRECTORY_TRAVERSE,
72 &attr);
73 if (InterlockedCompareExchangePointer( &handle, dir, 0 ) != 0)
75 /* someone beat us here... */
76 CloseHandle( dir );
79 return handle;
82 /* helper for kernel32->ntdll timeout format conversion */
83 static inline PLARGE_INTEGER get_nt_timeout( PLARGE_INTEGER pTime, DWORD timeout )
85 if (timeout == INFINITE) return NULL;
86 pTime->QuadPart = (ULONGLONG)timeout * -10000;
87 return pTime;
90 /***********************************************************************
91 * Sleep (KERNEL32.@)
93 VOID WINAPI DECLSPEC_HOTPATCH Sleep( DWORD timeout )
95 SleepEx( timeout, FALSE );
98 /******************************************************************************
99 * SleepEx (KERNEL32.@)
101 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
103 NTSTATUS status;
104 LARGE_INTEGER time;
106 status = NtDelayExecution( alertable, get_nt_timeout( &time, timeout ) );
107 if (status == STATUS_USER_APC) return WAIT_IO_COMPLETION;
108 return 0;
112 /***********************************************************************
113 * SwitchToThread (KERNEL32.@)
115 BOOL WINAPI SwitchToThread(void)
117 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
121 /***********************************************************************
122 * WaitForSingleObject (KERNEL32.@)
124 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
126 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
130 /***********************************************************************
131 * WaitForSingleObjectEx (KERNEL32.@)
133 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
134 BOOL alertable )
136 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
140 /***********************************************************************
141 * WaitForMultipleObjects (KERNEL32.@)
143 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
144 BOOL wait_all, DWORD timeout )
146 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
150 /***********************************************************************
151 * WaitForMultipleObjectsEx (KERNEL32.@)
153 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
154 BOOL wait_all, DWORD timeout,
155 BOOL alertable )
157 NTSTATUS status;
158 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
159 LARGE_INTEGER time;
160 unsigned int i;
162 if (count > MAXIMUM_WAIT_OBJECTS)
164 SetLastError(ERROR_INVALID_PARAMETER);
165 return WAIT_FAILED;
167 for (i = 0; i < count; i++)
169 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
170 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
171 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
172 hloc[i] = GetStdHandle( HandleToULong(handles[i]) );
173 else
174 hloc[i] = handles[i];
176 /* yes, even screen buffer console handles are waitable, and are
177 * handled as a handle to the console itself !!
179 if (is_console_handle(hloc[i]))
181 if (VerifyConsoleIoHandle(hloc[i]))
182 hloc[i] = GetConsoleInputWaitHandle();
186 status = NtWaitForMultipleObjects( count, hloc, !wait_all, alertable,
187 get_nt_timeout( &time, timeout ) );
189 if (HIWORD(status)) /* is it an error code? */
191 SetLastError( RtlNtStatusToDosError(status) );
192 status = WAIT_FAILED;
194 return status;
198 /***********************************************************************
199 * RegisterWaitForSingleObject (KERNEL32.@)
201 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
202 WAITORTIMERCALLBACK Callback, PVOID Context,
203 ULONG dwMilliseconds, ULONG dwFlags)
205 NTSTATUS status;
207 TRACE("%p %p %p %p %d %d\n",
208 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
210 status = RtlRegisterWait( phNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
211 if (status != STATUS_SUCCESS)
213 SetLastError( RtlNtStatusToDosError(status) );
214 return FALSE;
216 return TRUE;
219 /***********************************************************************
220 * RegisterWaitForSingleObjectEx (KERNEL32.@)
222 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
223 WAITORTIMERCALLBACK Callback, PVOID Context,
224 ULONG dwMilliseconds, ULONG dwFlags )
226 NTSTATUS status;
227 HANDLE hNewWaitObject;
229 TRACE("%p %p %p %d %d\n",
230 hObject,Callback,Context,dwMilliseconds,dwFlags);
232 status = RtlRegisterWait( &hNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
233 if (status != STATUS_SUCCESS)
235 SetLastError( RtlNtStatusToDosError(status) );
236 return NULL;
238 return hNewWaitObject;
241 /***********************************************************************
242 * UnregisterWait (KERNEL32.@)
244 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
246 NTSTATUS status;
248 TRACE("%p\n",WaitHandle);
250 status = RtlDeregisterWait( WaitHandle );
251 if (status != STATUS_SUCCESS)
253 SetLastError( RtlNtStatusToDosError(status) );
254 return FALSE;
256 return TRUE;
259 /***********************************************************************
260 * UnregisterWaitEx (KERNEL32.@)
262 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
264 NTSTATUS status;
266 TRACE("%p %p\n",WaitHandle, CompletionEvent);
268 status = RtlDeregisterWaitEx( WaitHandle, CompletionEvent );
269 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
270 return !status;
273 /***********************************************************************
274 * SignalObjectAndWait (KERNEL32.@)
276 * Makes it possible to atomically signal any of the synchronization
277 * objects (semaphore, mutex, event) and wait on another.
279 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
280 DWORD dwMilliseconds, BOOL bAlertable )
282 NTSTATUS status;
283 LARGE_INTEGER timeout;
285 TRACE("%p %p %d %d\n", hObjectToSignal,
286 hObjectToWaitOn, dwMilliseconds, bAlertable);
288 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn, bAlertable,
289 get_nt_timeout( &timeout, dwMilliseconds ) );
290 if (HIWORD(status))
292 SetLastError( RtlNtStatusToDosError(status) );
293 status = WAIT_FAILED;
295 return status;
298 /***********************************************************************
299 * InitializeCriticalSection (KERNEL32.@)
301 * Initialise a critical section before use.
303 * PARAMS
304 * crit [O] Critical section to initialise.
306 * RETURNS
307 * Nothing. If the function fails an exception is raised.
309 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
311 InitializeCriticalSectionEx( crit, 0, 0 );
314 /***********************************************************************
315 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
317 * Initialise a critical section with a spin count.
319 * PARAMS
320 * crit [O] Critical section to initialise.
321 * spincount [I] Number of times to spin upon contention.
323 * RETURNS
324 * Success: TRUE.
325 * Failure: Nothing. If the function fails an exception is raised.
327 * NOTES
328 * spincount is ignored on uni-processor systems.
330 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
332 return InitializeCriticalSectionEx( crit, spincount, 0 );
335 /***********************************************************************
336 * InitializeCriticalSectionEx (KERNEL32.@)
338 * Initialise a critical section with a spin count and flags.
340 * PARAMS
341 * crit [O] Critical section to initialise.
342 * spincount [I] Number of times to spin upon contention.
343 * flags [I] CRITICAL_SECTION_ flags from winbase.h.
345 * RETURNS
346 * Success: TRUE.
347 * Failure: Nothing. If the function fails an exception is raised.
349 * NOTES
350 * spincount is ignored on uni-processor systems.
352 BOOL WINAPI InitializeCriticalSectionEx( CRITICAL_SECTION *crit, DWORD spincount, DWORD flags )
354 NTSTATUS ret = RtlInitializeCriticalSectionEx( crit, spincount, flags );
355 if (ret) RtlRaiseStatus( ret );
356 return !ret;
359 /***********************************************************************
360 * MakeCriticalSectionGlobal (KERNEL32.@)
362 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
364 /* let's assume that only one thread at a time will try to do this */
365 HANDLE sem = crit->LockSemaphore;
366 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
367 crit->LockSemaphore = ConvertToGlobalHandle( sem );
368 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
369 crit->DebugInfo = NULL;
373 /***********************************************************************
374 * ReinitializeCriticalSection (KERNEL32.@)
376 * Initialise an already used critical section.
378 * PARAMS
379 * crit [O] Critical section to initialise.
381 * RETURNS
382 * Nothing.
384 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
386 if ( !crit->LockSemaphore )
387 RtlInitializeCriticalSection( crit );
391 /***********************************************************************
392 * UninitializeCriticalSection (KERNEL32.@)
394 * UnInitialise a critical section after use.
396 * PARAMS
397 * crit [O] Critical section to uninitialise (destroy).
399 * RETURNS
400 * Nothing.
402 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
404 RtlDeleteCriticalSection( crit );
408 /***********************************************************************
409 * CreateEventA (KERNEL32.@)
411 HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
412 BOOL initial_state, LPCSTR name )
414 DWORD flags = 0;
416 if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET;
417 if (initial_state) flags |= CREATE_EVENT_INITIAL_SET;
418 return CreateEventExA( sa, name, flags, EVENT_ALL_ACCESS );
422 /***********************************************************************
423 * CreateEventW (KERNEL32.@)
425 HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
426 BOOL initial_state, LPCWSTR name )
428 DWORD flags = 0;
430 if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET;
431 if (initial_state) flags |= CREATE_EVENT_INITIAL_SET;
432 return CreateEventExW( sa, name, flags, EVENT_ALL_ACCESS );
436 /***********************************************************************
437 * CreateEventExA (KERNEL32.@)
439 HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
441 WCHAR buffer[MAX_PATH];
443 if (!name) return CreateEventExW( sa, NULL, flags, access );
445 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
447 SetLastError( ERROR_FILENAME_EXCED_RANGE );
448 return 0;
450 return CreateEventExW( sa, buffer, flags, access );
454 /***********************************************************************
455 * CreateEventExW (KERNEL32.@)
457 HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
459 HANDLE ret = 0;
460 UNICODE_STRING nameW;
461 OBJECT_ATTRIBUTES attr;
462 NTSTATUS status;
464 /* one buggy program needs this
465 * ("Van Dale Groot woordenboek der Nederlandse taal")
467 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
469 ERR("Bad security attributes pointer %p\n",sa);
470 SetLastError( ERROR_INVALID_PARAMETER);
471 return 0;
474 attr.Length = sizeof(attr);
475 attr.RootDirectory = 0;
476 attr.ObjectName = NULL;
477 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
478 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
479 attr.SecurityQualityOfService = NULL;
480 if (name)
482 RtlInitUnicodeString( &nameW, name );
483 attr.ObjectName = &nameW;
484 attr.RootDirectory = get_BaseNamedObjects_handle();
487 status = NtCreateEvent( &ret, access, &attr,
488 (flags & CREATE_EVENT_MANUAL_RESET) ? NotificationEvent : SynchronizationEvent,
489 (flags & CREATE_EVENT_INITIAL_SET) != 0 );
490 if (status == STATUS_OBJECT_NAME_EXISTS)
491 SetLastError( ERROR_ALREADY_EXISTS );
492 else
493 SetLastError( RtlNtStatusToDosError(status) );
494 return ret;
498 /***********************************************************************
499 * OpenEventA (KERNEL32.@)
501 HANDLE WINAPI DECLSPEC_HOTPATCH OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
503 WCHAR buffer[MAX_PATH];
505 if (!name) return OpenEventW( access, inherit, NULL );
507 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
509 SetLastError( ERROR_FILENAME_EXCED_RANGE );
510 return 0;
512 return OpenEventW( access, inherit, buffer );
516 /***********************************************************************
517 * OpenEventW (KERNEL32.@)
519 HANDLE WINAPI DECLSPEC_HOTPATCH OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
521 HANDLE ret;
522 UNICODE_STRING nameW;
523 OBJECT_ATTRIBUTES attr;
524 NTSTATUS status;
526 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
528 attr.Length = sizeof(attr);
529 attr.RootDirectory = 0;
530 attr.ObjectName = NULL;
531 attr.Attributes = inherit ? OBJ_INHERIT : 0;
532 attr.SecurityDescriptor = NULL;
533 attr.SecurityQualityOfService = NULL;
534 if (name)
536 RtlInitUnicodeString( &nameW, name );
537 attr.ObjectName = &nameW;
538 attr.RootDirectory = get_BaseNamedObjects_handle();
541 status = NtOpenEvent( &ret, access, &attr );
542 if (status != STATUS_SUCCESS)
544 SetLastError( RtlNtStatusToDosError(status) );
545 return 0;
547 return ret;
550 /***********************************************************************
551 * PulseEvent (KERNEL32.@)
553 BOOL WINAPI DECLSPEC_HOTPATCH PulseEvent( HANDLE handle )
555 NTSTATUS status;
557 if ((status = NtPulseEvent( handle, NULL )))
558 SetLastError( RtlNtStatusToDosError(status) );
559 return !status;
563 /***********************************************************************
564 * SetEvent (KERNEL32.@)
566 BOOL WINAPI DECLSPEC_HOTPATCH SetEvent( HANDLE handle )
568 NTSTATUS status;
570 if ((status = NtSetEvent( handle, NULL )))
571 SetLastError( RtlNtStatusToDosError(status) );
572 return !status;
576 /***********************************************************************
577 * ResetEvent (KERNEL32.@)
579 BOOL WINAPI DECLSPEC_HOTPATCH ResetEvent( HANDLE handle )
581 NTSTATUS status;
583 if ((status = NtResetEvent( handle, NULL )))
584 SetLastError( RtlNtStatusToDosError(status) );
585 return !status;
589 /***********************************************************************
590 * CreateMutexA (KERNEL32.@)
592 HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
594 return CreateMutexExA( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS );
598 /***********************************************************************
599 * CreateMutexW (KERNEL32.@)
601 HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
603 return CreateMutexExW( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS );
607 /***********************************************************************
608 * CreateMutexExA (KERNEL32.@)
610 HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
612 ANSI_STRING nameA;
613 NTSTATUS status;
615 if (!name) return CreateMutexExW( sa, NULL, flags, access );
617 RtlInitAnsiString( &nameA, name );
618 status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString, &nameA, FALSE );
619 if (status != STATUS_SUCCESS)
621 SetLastError( ERROR_FILENAME_EXCED_RANGE );
622 return 0;
624 return CreateMutexExW( sa, NtCurrentTeb()->StaticUnicodeString.Buffer, flags, access );
628 /***********************************************************************
629 * CreateMutexExW (KERNEL32.@)
631 HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
633 HANDLE ret = 0;
634 UNICODE_STRING nameW;
635 OBJECT_ATTRIBUTES attr;
636 NTSTATUS status;
638 attr.Length = sizeof(attr);
639 attr.RootDirectory = 0;
640 attr.ObjectName = NULL;
641 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
642 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
643 attr.SecurityQualityOfService = NULL;
644 if (name)
646 RtlInitUnicodeString( &nameW, name );
647 attr.ObjectName = &nameW;
648 attr.RootDirectory = get_BaseNamedObjects_handle();
651 status = NtCreateMutant( &ret, access, &attr, (flags & CREATE_MUTEX_INITIAL_OWNER) != 0 );
652 if (status == STATUS_OBJECT_NAME_EXISTS)
653 SetLastError( ERROR_ALREADY_EXISTS );
654 else
655 SetLastError( RtlNtStatusToDosError(status) );
656 return ret;
660 /***********************************************************************
661 * OpenMutexA (KERNEL32.@)
663 HANDLE WINAPI DECLSPEC_HOTPATCH OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
665 WCHAR buffer[MAX_PATH];
667 if (!name) return OpenMutexW( access, inherit, NULL );
669 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
671 SetLastError( ERROR_FILENAME_EXCED_RANGE );
672 return 0;
674 return OpenMutexW( access, inherit, buffer );
678 /***********************************************************************
679 * OpenMutexW (KERNEL32.@)
681 HANDLE WINAPI DECLSPEC_HOTPATCH OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
683 HANDLE ret;
684 UNICODE_STRING nameW;
685 OBJECT_ATTRIBUTES attr;
686 NTSTATUS status;
688 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
690 attr.Length = sizeof(attr);
691 attr.RootDirectory = 0;
692 attr.ObjectName = NULL;
693 attr.Attributes = inherit ? OBJ_INHERIT : 0;
694 attr.SecurityDescriptor = NULL;
695 attr.SecurityQualityOfService = NULL;
696 if (name)
698 RtlInitUnicodeString( &nameW, name );
699 attr.ObjectName = &nameW;
700 attr.RootDirectory = get_BaseNamedObjects_handle();
703 status = NtOpenMutant( &ret, access, &attr );
704 if (status != STATUS_SUCCESS)
706 SetLastError( RtlNtStatusToDosError(status) );
707 return 0;
709 return ret;
713 /***********************************************************************
714 * ReleaseMutex (KERNEL32.@)
716 BOOL WINAPI DECLSPEC_HOTPATCH ReleaseMutex( HANDLE handle )
718 NTSTATUS status;
720 status = NtReleaseMutant(handle, NULL);
721 if (status != STATUS_SUCCESS)
723 SetLastError( RtlNtStatusToDosError(status) );
724 return FALSE;
726 return TRUE;
731 * Semaphores
735 /***********************************************************************
736 * CreateSemaphoreA (KERNEL32.@)
738 HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
740 return CreateSemaphoreExA( sa, initial, max, name, 0, SEMAPHORE_ALL_ACCESS );
744 /***********************************************************************
745 * CreateSemaphoreW (KERNEL32.@)
747 HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCWSTR name )
749 return CreateSemaphoreExW( sa, initial, max, name, 0, SEMAPHORE_ALL_ACCESS );
753 /***********************************************************************
754 * CreateSemaphoreExA (KERNEL32.@)
756 HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreExA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max,
757 LPCSTR name, DWORD flags, DWORD access )
759 WCHAR buffer[MAX_PATH];
761 if (!name) return CreateSemaphoreExW( sa, initial, max, NULL, flags, access );
763 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
765 SetLastError( ERROR_FILENAME_EXCED_RANGE );
766 return 0;
768 return CreateSemaphoreExW( sa, initial, max, buffer, flags, access );
772 /***********************************************************************
773 * CreateSemaphoreExW (KERNEL32.@)
775 HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreExW( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max,
776 LPCWSTR name, DWORD flags, DWORD access )
778 HANDLE ret = 0;
779 UNICODE_STRING nameW;
780 OBJECT_ATTRIBUTES attr;
781 NTSTATUS status;
783 attr.Length = sizeof(attr);
784 attr.RootDirectory = 0;
785 attr.ObjectName = NULL;
786 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
787 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
788 attr.SecurityQualityOfService = NULL;
789 if (name)
791 RtlInitUnicodeString( &nameW, name );
792 attr.ObjectName = &nameW;
793 attr.RootDirectory = get_BaseNamedObjects_handle();
796 status = NtCreateSemaphore( &ret, access, &attr, initial, max );
797 if (status == STATUS_OBJECT_NAME_EXISTS)
798 SetLastError( ERROR_ALREADY_EXISTS );
799 else
800 SetLastError( RtlNtStatusToDosError(status) );
801 return ret;
805 /***********************************************************************
806 * OpenSemaphoreA (KERNEL32.@)
808 HANDLE WINAPI DECLSPEC_HOTPATCH OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
810 WCHAR buffer[MAX_PATH];
812 if (!name) return OpenSemaphoreW( access, inherit, NULL );
814 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
816 SetLastError( ERROR_FILENAME_EXCED_RANGE );
817 return 0;
819 return OpenSemaphoreW( access, inherit, buffer );
823 /***********************************************************************
824 * OpenSemaphoreW (KERNEL32.@)
826 HANDLE WINAPI DECLSPEC_HOTPATCH OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
828 HANDLE ret;
829 UNICODE_STRING nameW;
830 OBJECT_ATTRIBUTES attr;
831 NTSTATUS status;
833 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
835 attr.Length = sizeof(attr);
836 attr.RootDirectory = 0;
837 attr.ObjectName = NULL;
838 attr.Attributes = inherit ? OBJ_INHERIT : 0;
839 attr.SecurityDescriptor = NULL;
840 attr.SecurityQualityOfService = NULL;
841 if (name)
843 RtlInitUnicodeString( &nameW, name );
844 attr.ObjectName = &nameW;
845 attr.RootDirectory = get_BaseNamedObjects_handle();
848 status = NtOpenSemaphore( &ret, access, &attr );
849 if (status != STATUS_SUCCESS)
851 SetLastError( RtlNtStatusToDosError(status) );
852 return 0;
854 return ret;
858 /***********************************************************************
859 * ReleaseSemaphore (KERNEL32.@)
861 BOOL WINAPI DECLSPEC_HOTPATCH ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
863 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
864 if (status) SetLastError( RtlNtStatusToDosError(status) );
865 return !status;
870 * Jobs
873 /******************************************************************************
874 * CreateJobObjectW (KERNEL32.@)
876 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES sa, LPCWSTR name )
878 HANDLE ret = 0;
879 UNICODE_STRING nameW;
880 OBJECT_ATTRIBUTES attr;
881 NTSTATUS status;
883 attr.Length = sizeof(attr);
884 attr.RootDirectory = 0;
885 attr.ObjectName = NULL;
886 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
887 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
888 attr.SecurityQualityOfService = NULL;
889 if (name)
891 RtlInitUnicodeString( &nameW, name );
892 attr.ObjectName = &nameW;
893 attr.RootDirectory = get_BaseNamedObjects_handle();
896 status = NtCreateJobObject( &ret, JOB_OBJECT_ALL_ACCESS, &attr );
897 if (status == STATUS_OBJECT_NAME_EXISTS)
898 SetLastError( ERROR_ALREADY_EXISTS );
899 else
900 SetLastError( RtlNtStatusToDosError(status) );
901 return ret;
904 /******************************************************************************
905 * CreateJobObjectA (KERNEL32.@)
907 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
909 WCHAR buffer[MAX_PATH];
911 if (!name) return CreateJobObjectW( attr, NULL );
913 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
915 SetLastError( ERROR_FILENAME_EXCED_RANGE );
916 return 0;
918 return CreateJobObjectW( attr, buffer );
921 /******************************************************************************
922 * OpenJobObjectW (KERNEL32.@)
924 HANDLE WINAPI OpenJobObjectW( DWORD access, BOOL inherit, LPCWSTR name )
926 HANDLE ret;
927 UNICODE_STRING nameW;
928 OBJECT_ATTRIBUTES attr;
929 NTSTATUS status;
931 attr.Length = sizeof(attr);
932 attr.RootDirectory = 0;
933 attr.ObjectName = NULL;
934 attr.Attributes = inherit ? OBJ_INHERIT : 0;
935 attr.SecurityDescriptor = NULL;
936 attr.SecurityQualityOfService = NULL;
937 if (name)
939 RtlInitUnicodeString( &nameW, name );
940 attr.ObjectName = &nameW;
941 attr.RootDirectory = get_BaseNamedObjects_handle();
944 status = NtOpenJobObject( &ret, access, &attr );
945 if (status != STATUS_SUCCESS)
947 SetLastError( RtlNtStatusToDosError(status) );
948 return 0;
950 return ret;
953 /******************************************************************************
954 * OpenJobObjectA (KERNEL32.@)
956 HANDLE WINAPI OpenJobObjectA( DWORD access, BOOL inherit, LPCSTR name )
958 WCHAR buffer[MAX_PATH];
960 if (!name) return OpenJobObjectW( access, inherit, NULL );
962 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
964 SetLastError( ERROR_FILENAME_EXCED_RANGE );
965 return 0;
967 return OpenJobObjectW( access, inherit, buffer );
970 /******************************************************************************
971 * TerminateJobObject (KERNEL32.@)
973 BOOL WINAPI TerminateJobObject( HANDLE job, UINT exit_code )
975 NTSTATUS status = NtTerminateJobObject( job, exit_code );
976 if (status) SetLastError( RtlNtStatusToDosError(status) );
977 return !status;
980 /******************************************************************************
981 * QueryInformationJobObject (KERNEL32.@)
983 BOOL WINAPI QueryInformationJobObject( HANDLE job, JOBOBJECTINFOCLASS class, LPVOID info,
984 DWORD len, DWORD *ret_len )
986 NTSTATUS status = NtQueryInformationJobObject( job, class, info, len, ret_len );
987 if (status) SetLastError( RtlNtStatusToDosError(status) );
988 return !status;
991 /******************************************************************************
992 * SetInformationJobObject (KERNEL32.@)
994 BOOL WINAPI SetInformationJobObject( HANDLE job, JOBOBJECTINFOCLASS class, LPVOID info, DWORD len )
996 NTSTATUS status = NtSetInformationJobObject( job, class, info, len );
997 if (status) SetLastError( RtlNtStatusToDosError(status) );
998 return !status;
1001 /******************************************************************************
1002 * AssignProcessToJobObject (KERNEL32.@)
1004 BOOL WINAPI AssignProcessToJobObject( HANDLE job, HANDLE process )
1006 NTSTATUS status = NtAssignProcessToJobObject( job, process );
1007 if (status) SetLastError( RtlNtStatusToDosError(status) );
1008 return !status;
1011 /******************************************************************************
1012 * IsProcessInJob (KERNEL32.@)
1014 BOOL WINAPI IsProcessInJob( HANDLE process, HANDLE job, PBOOL result )
1016 NTSTATUS status = NtIsProcessInJob( process, job );
1017 switch(status)
1019 case STATUS_PROCESS_IN_JOB:
1020 *result = TRUE;
1021 return TRUE;
1022 case STATUS_PROCESS_NOT_IN_JOB:
1023 *result = FALSE;
1024 return TRUE;
1025 default:
1026 SetLastError( RtlNtStatusToDosError(status) );
1027 return FALSE;
1033 * Timers
1037 /***********************************************************************
1038 * CreateWaitableTimerA (KERNEL32.@)
1040 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
1042 return CreateWaitableTimerExA( sa, name, manual ? CREATE_WAITABLE_TIMER_MANUAL_RESET : 0,
1043 TIMER_ALL_ACCESS );
1047 /***********************************************************************
1048 * CreateWaitableTimerW (KERNEL32.@)
1050 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
1052 return CreateWaitableTimerExW( sa, name, manual ? CREATE_WAITABLE_TIMER_MANUAL_RESET : 0,
1053 TIMER_ALL_ACCESS );
1057 /***********************************************************************
1058 * CreateWaitableTimerExA (KERNEL32.@)
1060 HANDLE WINAPI CreateWaitableTimerExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
1062 WCHAR buffer[MAX_PATH];
1064 if (!name) return CreateWaitableTimerExW( sa, NULL, flags, access );
1066 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1068 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1069 return 0;
1071 return CreateWaitableTimerExW( sa, buffer, flags, access );
1075 /***********************************************************************
1076 * CreateWaitableTimerExW (KERNEL32.@)
1078 HANDLE WINAPI CreateWaitableTimerExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
1080 HANDLE handle;
1081 NTSTATUS status;
1082 UNICODE_STRING nameW;
1083 OBJECT_ATTRIBUTES attr;
1085 attr.Length = sizeof(attr);
1086 attr.RootDirectory = 0;
1087 attr.ObjectName = NULL;
1088 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1089 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1090 attr.SecurityQualityOfService = NULL;
1091 if (name)
1093 RtlInitUnicodeString( &nameW, name );
1094 attr.ObjectName = &nameW;
1095 attr.RootDirectory = get_BaseNamedObjects_handle();
1098 status = NtCreateTimer( &handle, access, &attr,
1099 (flags & CREATE_WAITABLE_TIMER_MANUAL_RESET) ? NotificationTimer : SynchronizationTimer );
1100 if (status == STATUS_OBJECT_NAME_EXISTS)
1101 SetLastError( ERROR_ALREADY_EXISTS );
1102 else
1103 SetLastError( RtlNtStatusToDosError(status) );
1104 return handle;
1108 /***********************************************************************
1109 * OpenWaitableTimerA (KERNEL32.@)
1111 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
1113 WCHAR buffer[MAX_PATH];
1115 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
1117 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1119 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1120 return 0;
1122 return OpenWaitableTimerW( access, inherit, buffer );
1126 /***********************************************************************
1127 * OpenWaitableTimerW (KERNEL32.@)
1129 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
1131 HANDLE handle;
1132 UNICODE_STRING nameW;
1133 OBJECT_ATTRIBUTES attr;
1134 NTSTATUS status;
1136 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
1138 attr.Length = sizeof(attr);
1139 attr.RootDirectory = 0;
1140 attr.ObjectName = NULL;
1141 attr.Attributes = inherit ? OBJ_INHERIT : 0;
1142 attr.SecurityDescriptor = NULL;
1143 attr.SecurityQualityOfService = NULL;
1144 if (name)
1146 RtlInitUnicodeString( &nameW, name );
1147 attr.ObjectName = &nameW;
1148 attr.RootDirectory = get_BaseNamedObjects_handle();
1151 status = NtOpenTimer(&handle, access, &attr);
1152 if (status != STATUS_SUCCESS)
1154 SetLastError( RtlNtStatusToDosError(status) );
1155 return 0;
1157 return handle;
1161 /***********************************************************************
1162 * SetWaitableTimer (KERNEL32.@)
1164 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
1165 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
1167 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
1168 arg, resume, period, NULL);
1170 if (status != STATUS_SUCCESS)
1172 SetLastError( RtlNtStatusToDosError(status) );
1173 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
1175 return TRUE;
1178 /***********************************************************************
1179 * SetWaitableTimerEx (KERNEL32.@)
1181 BOOL WINAPI SetWaitableTimerEx( HANDLE handle, const LARGE_INTEGER *when, LONG period,
1182 PTIMERAPCROUTINE callback, LPVOID arg, REASON_CONTEXT *context, ULONG tolerabledelay )
1184 static int once;
1185 if (!once++)
1187 FIXME("(%p, %p, %d, %p, %p, %p, %d) semi-stub\n",
1188 handle, when, period, callback, arg, context, tolerabledelay);
1190 return SetWaitableTimer(handle, when, period, callback, arg, FALSE);
1193 /***********************************************************************
1194 * CancelWaitableTimer (KERNEL32.@)
1196 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1198 NTSTATUS status;
1200 status = NtCancelTimer(handle, NULL);
1201 if (status != STATUS_SUCCESS)
1203 SetLastError( RtlNtStatusToDosError(status) );
1204 return FALSE;
1206 return TRUE;
1210 /***********************************************************************
1211 * CreateTimerQueue (KERNEL32.@)
1213 HANDLE WINAPI CreateTimerQueue(void)
1215 HANDLE q;
1216 NTSTATUS status = RtlCreateTimerQueue(&q);
1218 if (status != STATUS_SUCCESS)
1220 SetLastError( RtlNtStatusToDosError(status) );
1221 return NULL;
1224 return q;
1228 /***********************************************************************
1229 * DeleteTimerQueueEx (KERNEL32.@)
1231 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1233 NTSTATUS status = RtlDeleteTimerQueueEx(TimerQueue, CompletionEvent);
1235 if (status != STATUS_SUCCESS)
1237 SetLastError( RtlNtStatusToDosError(status) );
1238 return FALSE;
1241 return TRUE;
1244 /***********************************************************************
1245 * DeleteTimerQueue (KERNEL32.@)
1247 BOOL WINAPI DeleteTimerQueue(HANDLE TimerQueue)
1249 return DeleteTimerQueueEx(TimerQueue, NULL);
1252 /***********************************************************************
1253 * CreateTimerQueueTimer (KERNEL32.@)
1255 * Creates a timer-queue timer. This timer expires at the specified due
1256 * time (in ms), then after every specified period (in ms). When the timer
1257 * expires, the callback function is called.
1259 * RETURNS
1260 * nonzero on success or zero on failure
1262 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1263 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1264 DWORD DueTime, DWORD Period, ULONG Flags )
1266 NTSTATUS status = RtlCreateTimer(phNewTimer, TimerQueue, Callback,
1267 Parameter, DueTime, Period, Flags);
1269 if (status != STATUS_SUCCESS)
1271 SetLastError( RtlNtStatusToDosError(status) );
1272 return FALSE;
1275 return TRUE;
1278 /***********************************************************************
1279 * ChangeTimerQueueTimer (KERNEL32.@)
1281 * Changes the times at which the timer expires.
1283 * RETURNS
1284 * nonzero on success or zero on failure
1286 BOOL WINAPI ChangeTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1287 ULONG DueTime, ULONG Period )
1289 NTSTATUS status = RtlUpdateTimer(TimerQueue, Timer, DueTime, Period);
1291 if (status != STATUS_SUCCESS)
1293 SetLastError( RtlNtStatusToDosError(status) );
1294 return FALSE;
1297 return TRUE;
1300 /***********************************************************************
1301 * CancelTimerQueueTimer (KERNEL32.@)
1303 BOOL WINAPI CancelTimerQueueTimer(HANDLE queue, HANDLE timer)
1305 FIXME("stub: %p %p\n", queue, timer);
1306 return FALSE;
1309 /***********************************************************************
1310 * DeleteTimerQueueTimer (KERNEL32.@)
1312 * Cancels a timer-queue timer.
1314 * RETURNS
1315 * nonzero on success or zero on failure
1317 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1318 HANDLE CompletionEvent )
1320 NTSTATUS status = RtlDeleteTimer(TimerQueue, Timer, CompletionEvent);
1321 if (status != STATUS_SUCCESS)
1323 SetLastError( RtlNtStatusToDosError(status) );
1324 return FALSE;
1326 return TRUE;
1331 * Pipes
1335 /***********************************************************************
1336 * CreateNamedPipeA (KERNEL32.@)
1338 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1339 DWORD dwPipeMode, DWORD nMaxInstances,
1340 DWORD nOutBufferSize, DWORD nInBufferSize,
1341 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1343 WCHAR buffer[MAX_PATH];
1345 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1346 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1348 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1350 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1351 return INVALID_HANDLE_VALUE;
1353 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1354 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1358 /***********************************************************************
1359 * CreateNamedPipeW (KERNEL32.@)
1361 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1362 DWORD dwPipeMode, DWORD nMaxInstances,
1363 DWORD nOutBufferSize, DWORD nInBufferSize,
1364 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1366 HANDLE handle;
1367 UNICODE_STRING nt_name;
1368 OBJECT_ATTRIBUTES attr;
1369 DWORD access, options, sharing;
1370 BOOLEAN pipe_type, read_mode, non_block;
1371 NTSTATUS status;
1372 IO_STATUS_BLOCK iosb;
1373 LARGE_INTEGER timeout;
1375 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1376 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1377 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1379 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1381 SetLastError( ERROR_PATH_NOT_FOUND );
1382 return INVALID_HANDLE_VALUE;
1384 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1386 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1387 RtlFreeUnicodeString( &nt_name );
1388 return INVALID_HANDLE_VALUE;
1391 attr.Length = sizeof(attr);
1392 attr.RootDirectory = 0;
1393 attr.ObjectName = &nt_name;
1394 attr.Attributes = OBJ_CASE_INSENSITIVE |
1395 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1396 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1397 attr.SecurityQualityOfService = NULL;
1399 switch(dwOpenMode & 3)
1401 case PIPE_ACCESS_INBOUND:
1402 sharing = FILE_SHARE_WRITE;
1403 access = GENERIC_READ;
1404 break;
1405 case PIPE_ACCESS_OUTBOUND:
1406 sharing = FILE_SHARE_READ;
1407 access = GENERIC_WRITE;
1408 break;
1409 case PIPE_ACCESS_DUPLEX:
1410 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1411 access = GENERIC_READ | GENERIC_WRITE;
1412 break;
1413 default:
1414 SetLastError( ERROR_INVALID_PARAMETER );
1415 return INVALID_HANDLE_VALUE;
1417 access |= SYNCHRONIZE;
1418 options = 0;
1419 if (dwOpenMode & WRITE_DAC) access |= WRITE_DAC;
1420 if (dwOpenMode & WRITE_OWNER) access |= WRITE_OWNER;
1421 if (dwOpenMode & ACCESS_SYSTEM_SECURITY) access |= ACCESS_SYSTEM_SECURITY;
1422 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1423 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_NONALERT;
1424 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) != 0;
1425 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) != 0;
1426 non_block = (dwPipeMode & PIPE_NOWAIT) != 0;
1427 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0U;
1429 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1431 SetLastError(0);
1433 status = NtCreateNamedPipeFile(&handle, access, &attr, &iosb, sharing,
1434 FILE_OVERWRITE_IF, options, pipe_type,
1435 read_mode, non_block, nMaxInstances,
1436 nInBufferSize, nOutBufferSize, &timeout);
1438 RtlFreeUnicodeString( &nt_name );
1439 if (status)
1441 handle = INVALID_HANDLE_VALUE;
1442 SetLastError( RtlNtStatusToDosError(status) );
1444 return handle;
1448 /***********************************************************************
1449 * PeekNamedPipe (KERNEL32.@)
1451 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1452 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1454 FILE_PIPE_PEEK_BUFFER local_buffer;
1455 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1456 IO_STATUS_BLOCK io;
1457 NTSTATUS status;
1459 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1460 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1462 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1463 return FALSE;
1466 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1467 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1468 if (!status)
1470 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1471 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1472 if (lpcbRead) *lpcbRead = read_size;
1473 if (lpcbMessage) *lpcbMessage = 0; /* FIXME */
1474 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1476 else SetLastError( RtlNtStatusToDosError(status) );
1478 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1479 return !status;
1482 /***********************************************************************
1483 * WaitNamedPipeA (KERNEL32.@)
1485 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1487 WCHAR buffer[MAX_PATH];
1489 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1491 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1493 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1494 return FALSE;
1496 return WaitNamedPipeW( buffer, nTimeOut );
1500 /***********************************************************************
1501 * WaitNamedPipeW (KERNEL32.@)
1503 * Waits for a named pipe instance to become available
1505 * PARAMS
1506 * name [I] Pointer to a named pipe name to wait for
1507 * nTimeOut [I] How long to wait in ms
1509 * RETURNS
1510 * TRUE: Success, named pipe can be opened with CreateFile
1511 * FALSE: Failure, GetLastError can be called for further details
1513 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1515 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1516 NTSTATUS status;
1517 UNICODE_STRING nt_name, pipe_dev_name;
1518 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1519 IO_STATUS_BLOCK iosb;
1520 OBJECT_ATTRIBUTES attr;
1521 ULONG sz_pipe_wait;
1522 HANDLE pipe_dev;
1524 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1526 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1527 return FALSE;
1529 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1530 nt_name.Length < sizeof(leadin) ||
1531 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR)) != 0)
1533 RtlFreeUnicodeString( &nt_name );
1534 SetLastError( ERROR_PATH_NOT_FOUND );
1535 return FALSE;
1538 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1539 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1541 RtlFreeUnicodeString( &nt_name );
1542 SetLastError( ERROR_OUTOFMEMORY );
1543 return FALSE;
1546 pipe_dev_name.Buffer = nt_name.Buffer;
1547 pipe_dev_name.Length = sizeof(leadin);
1548 pipe_dev_name.MaximumLength = sizeof(leadin);
1549 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1550 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES | SYNCHRONIZE, &attr,
1551 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1552 FILE_SYNCHRONOUS_IO_NONALERT);
1553 if (status != ERROR_SUCCESS)
1555 HeapFree( GetProcessHeap(), 0, pipe_wait);
1556 RtlFreeUnicodeString( &nt_name );
1557 SetLastError( ERROR_PATH_NOT_FOUND );
1558 return FALSE;
1561 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1562 if (nTimeOut == NMPWAIT_WAIT_FOREVER)
1563 pipe_wait->Timeout.QuadPart = ((ULONGLONG)0x7fffffff << 32) | 0xffffffff;
1564 else
1565 pipe_wait->Timeout.QuadPart = (ULONGLONG)nTimeOut * -10000;
1566 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1567 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1568 pipe_wait->NameLength);
1569 RtlFreeUnicodeString( &nt_name );
1571 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1572 pipe_wait, sz_pipe_wait, NULL, 0 );
1574 HeapFree( GetProcessHeap(), 0, pipe_wait );
1575 NtClose( pipe_dev );
1577 if(status != STATUS_SUCCESS)
1579 SetLastError(RtlNtStatusToDosError(status));
1580 return FALSE;
1582 else
1583 return TRUE;
1587 /***********************************************************************
1588 * ConnectNamedPipe (KERNEL32.@)
1590 * Connects to a named pipe
1592 * Parameters
1593 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1594 * overlapped: Optional OVERLAPPED struct
1596 * Return values
1597 * TRUE: Success
1598 * FALSE: Failure, GetLastError can be called for further details
1600 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1602 NTSTATUS status;
1603 IO_STATUS_BLOCK status_block;
1604 LPVOID cvalue = NULL;
1606 TRACE("(%p,%p)\n", hPipe, overlapped);
1608 if(overlapped)
1610 overlapped->Internal = STATUS_PENDING;
1611 overlapped->InternalHigh = 0;
1612 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1615 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, cvalue,
1616 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1617 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1619 if (status == STATUS_SUCCESS) return TRUE;
1620 SetLastError( RtlNtStatusToDosError(status) );
1621 return FALSE;
1624 /***********************************************************************
1625 * DisconnectNamedPipe (KERNEL32.@)
1627 * Disconnects from a named pipe
1629 * Parameters
1630 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1632 * Return values
1633 * TRUE: Success
1634 * FALSE: Failure, GetLastError can be called for further details
1636 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1638 NTSTATUS status;
1639 IO_STATUS_BLOCK io_block;
1641 TRACE("(%p)\n",hPipe);
1643 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1644 NULL, 0, NULL, 0);
1645 if (status == STATUS_SUCCESS) return TRUE;
1646 SetLastError( RtlNtStatusToDosError(status) );
1647 return FALSE;
1650 /***********************************************************************
1651 * TransactNamedPipe (KERNEL32.@)
1653 * BUGS
1654 * should be done as a single operation in the wineserver or kernel
1656 BOOL WINAPI TransactNamedPipe(
1657 HANDLE handle, LPVOID write_buf, DWORD write_size, LPVOID read_buf,
1658 DWORD read_size, LPDWORD bytes_read, LPOVERLAPPED overlapped)
1660 BOOL r;
1661 DWORD count;
1663 TRACE("%p %p %d %p %d %p %p\n",
1664 handle, write_buf, write_size, read_buf,
1665 read_size, bytes_read, overlapped);
1667 if (overlapped)
1669 FIXME("Doesn't support overlapped operation as yet\n");
1670 return FALSE;
1673 r = WriteFile(handle, write_buf, write_size, &count, NULL);
1674 if (r)
1675 r = ReadFile(handle, read_buf, read_size, bytes_read, NULL);
1677 return r;
1680 /***********************************************************************
1681 * GetNamedPipeInfo (KERNEL32.@)
1683 BOOL WINAPI GetNamedPipeInfo(
1684 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1685 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1687 FILE_PIPE_LOCAL_INFORMATION fpli;
1688 IO_STATUS_BLOCK iosb;
1689 NTSTATUS status;
1691 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1692 FilePipeLocalInformation);
1693 if (status)
1695 SetLastError( RtlNtStatusToDosError(status) );
1696 return FALSE;
1699 if (lpFlags)
1701 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1702 PIPE_SERVER_END : PIPE_CLIENT_END;
1703 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1704 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1707 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1708 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1709 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1711 return TRUE;
1714 /***********************************************************************
1715 * GetNamedPipeHandleStateA (KERNEL32.@)
1717 BOOL WINAPI GetNamedPipeHandleStateA(
1718 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1719 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1720 LPSTR lpUsername, DWORD nUsernameMaxSize)
1722 WARN("%p %p %p %p %p %p %d: semi-stub\n",
1723 hNamedPipe, lpState, lpCurInstances,
1724 lpMaxCollectionCount, lpCollectDataTimeout,
1725 lpUsername, nUsernameMaxSize);
1727 if (lpUsername && nUsernameMaxSize)
1728 *lpUsername = 0;
1730 return GetNamedPipeHandleStateW(hNamedPipe, lpState, lpCurInstances,
1731 lpMaxCollectionCount, lpCollectDataTimeout, NULL, 0);
1734 /***********************************************************************
1735 * GetNamedPipeHandleStateW (KERNEL32.@)
1737 BOOL WINAPI GetNamedPipeHandleStateW(
1738 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1739 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1740 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1742 IO_STATUS_BLOCK iosb;
1743 NTSTATUS status;
1745 FIXME("%p %p %p %p %p %p %d: semi-stub\n",
1746 hNamedPipe, lpState, lpCurInstances,
1747 lpMaxCollectionCount, lpCollectDataTimeout,
1748 lpUsername, nUsernameMaxSize);
1750 if (lpMaxCollectionCount)
1751 *lpMaxCollectionCount = 0;
1753 if (lpCollectDataTimeout)
1754 *lpCollectDataTimeout = 0;
1756 if (lpUsername && nUsernameMaxSize)
1757 *lpUsername = 0;
1759 if (lpState)
1761 FILE_PIPE_INFORMATION fpi;
1762 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpi, sizeof(fpi),
1763 FilePipeInformation);
1764 if (status)
1766 SetLastError( RtlNtStatusToDosError(status) );
1767 return FALSE;
1770 *lpState = (fpi.ReadMode ? PIPE_READMODE_MESSAGE : PIPE_READMODE_BYTE) |
1771 (fpi.CompletionMode ? PIPE_NOWAIT : PIPE_WAIT);
1774 if (lpCurInstances)
1776 FILE_PIPE_LOCAL_INFORMATION fpli;
1777 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1778 FilePipeLocalInformation);
1779 if (status)
1781 SetLastError( RtlNtStatusToDosError(status) );
1782 return FALSE;
1785 *lpCurInstances = fpli.CurrentInstances;
1788 return TRUE;
1791 /***********************************************************************
1792 * SetNamedPipeHandleState (KERNEL32.@)
1794 BOOL WINAPI SetNamedPipeHandleState(
1795 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1796 LPDWORD lpCollectDataTimeout)
1798 /* should be a fixme, but this function is called a lot by the RPC
1799 * runtime, and it slows down InstallShield a fair bit. */
1800 WARN("semi-stub: %p %p/%d %p %p\n",
1801 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1803 if (lpMode)
1805 FILE_PIPE_INFORMATION fpi;
1806 IO_STATUS_BLOCK iosb;
1807 NTSTATUS status;
1809 if (*lpMode & ~(PIPE_READMODE_MESSAGE | PIPE_NOWAIT))
1810 status = STATUS_INVALID_PARAMETER;
1811 else
1813 fpi.CompletionMode = (*lpMode & PIPE_NOWAIT) ?
1814 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
1815 fpi.ReadMode = (*lpMode & PIPE_READMODE_MESSAGE) ?
1816 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
1817 status = NtSetInformationFile(hNamedPipe, &iosb, &fpi, sizeof(fpi), FilePipeInformation);
1820 if (status)
1822 SetLastError( RtlNtStatusToDosError(status) );
1823 return FALSE;
1827 return TRUE;
1830 /***********************************************************************
1831 * CallNamedPipeA (KERNEL32.@)
1833 BOOL WINAPI CallNamedPipeA(
1834 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1835 LPVOID lpOutput, DWORD dwOutputSize,
1836 LPDWORD lpBytesRead, DWORD nTimeout)
1838 DWORD len;
1839 LPWSTR str = NULL;
1840 BOOL ret;
1842 TRACE("%s %p %d %p %d %p %d\n",
1843 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1844 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1846 if( lpNamedPipeName )
1848 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1849 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1850 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1852 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1853 dwOutputSize, lpBytesRead, nTimeout );
1854 if( lpNamedPipeName )
1855 HeapFree( GetProcessHeap(), 0, str );
1857 return ret;
1860 /***********************************************************************
1861 * CallNamedPipeW (KERNEL32.@)
1863 BOOL WINAPI CallNamedPipeW(
1864 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1865 LPVOID lpOutput, DWORD lpOutputSize,
1866 LPDWORD lpBytesRead, DWORD nTimeout)
1868 HANDLE pipe;
1869 BOOL ret;
1870 DWORD mode;
1872 TRACE("%s %p %d %p %d %p %d\n",
1873 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1874 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1876 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1877 if (pipe == INVALID_HANDLE_VALUE)
1879 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1880 if (!ret)
1881 return FALSE;
1882 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1883 if (pipe == INVALID_HANDLE_VALUE)
1884 return FALSE;
1887 mode = PIPE_READMODE_MESSAGE;
1888 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1889 if (!ret)
1891 CloseHandle(pipe);
1892 return FALSE;
1895 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1896 CloseHandle(pipe);
1897 if (!ret)
1898 return FALSE;
1900 return TRUE;
1903 /******************************************************************
1904 * CreatePipe (KERNEL32.@)
1907 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1908 LPSECURITY_ATTRIBUTES sa, DWORD size )
1910 static unsigned index /* = 0 */;
1911 WCHAR name[64];
1912 HANDLE hr, hw;
1913 unsigned in_index = index;
1914 UNICODE_STRING nt_name;
1915 OBJECT_ATTRIBUTES attr;
1916 NTSTATUS status;
1917 IO_STATUS_BLOCK iosb;
1918 LARGE_INTEGER timeout;
1920 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1922 attr.Length = sizeof(attr);
1923 attr.RootDirectory = 0;
1924 attr.ObjectName = &nt_name;
1925 attr.Attributes = OBJ_CASE_INSENSITIVE |
1926 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1927 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1928 attr.SecurityQualityOfService = NULL;
1930 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1931 /* generate a unique pipe name (system wide) */
1934 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1935 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1936 'u','.','%','0','8','u','\0' };
1938 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1939 GetCurrentProcessId(), ++index);
1940 RtlInitUnicodeString(&nt_name, name);
1941 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1942 FILE_SHARE_WRITE, FILE_OVERWRITE_IF,
1943 FILE_SYNCHRONOUS_IO_NONALERT,
1944 FALSE, FALSE, FALSE,
1945 1, size, size, &timeout);
1946 if (status)
1948 SetLastError( RtlNtStatusToDosError(status) );
1949 hr = INVALID_HANDLE_VALUE;
1951 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1952 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1953 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1955 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1956 FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
1958 if (status)
1960 SetLastError( RtlNtStatusToDosError(status) );
1961 NtClose(hr);
1962 return FALSE;
1965 *hReadPipe = hr;
1966 *hWritePipe = hw;
1967 return TRUE;
1971 /******************************************************************************
1972 * CreateMailslotA [KERNEL32.@]
1974 * See CreateMailslotW.
1976 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1977 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1979 DWORD len;
1980 HANDLE handle;
1981 LPWSTR name = NULL;
1983 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1984 nMaxMessageSize, lReadTimeout, sa);
1986 if( lpName )
1988 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1989 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1990 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1993 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1995 HeapFree( GetProcessHeap(), 0, name );
1997 return handle;
2001 /******************************************************************************
2002 * CreateMailslotW [KERNEL32.@]
2004 * Create a mailslot with specified name.
2006 * PARAMS
2007 * lpName [I] Pointer to string for mailslot name
2008 * nMaxMessageSize [I] Maximum message size
2009 * lReadTimeout [I] Milliseconds before read time-out
2010 * sa [I] Pointer to security structure
2012 * RETURNS
2013 * Success: Handle to mailslot
2014 * Failure: INVALID_HANDLE_VALUE
2016 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
2017 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
2019 HANDLE handle = INVALID_HANDLE_VALUE;
2020 OBJECT_ATTRIBUTES attr;
2021 UNICODE_STRING nameW;
2022 LARGE_INTEGER timeout;
2023 IO_STATUS_BLOCK iosb;
2024 NTSTATUS status;
2026 TRACE("%s %d %d %p\n", debugstr_w(lpName),
2027 nMaxMessageSize, lReadTimeout, sa);
2029 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
2031 SetLastError( ERROR_PATH_NOT_FOUND );
2032 return INVALID_HANDLE_VALUE;
2035 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
2037 SetLastError( ERROR_FILENAME_EXCED_RANGE );
2038 RtlFreeUnicodeString( &nameW );
2039 return INVALID_HANDLE_VALUE;
2042 attr.Length = sizeof(attr);
2043 attr.RootDirectory = 0;
2044 attr.Attributes = OBJ_CASE_INSENSITIVE;
2045 attr.ObjectName = &nameW;
2046 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
2047 attr.SecurityQualityOfService = NULL;
2049 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
2050 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
2051 else
2052 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
2054 status = NtCreateMailslotFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr,
2055 &iosb, 0, 0, nMaxMessageSize, &timeout );
2056 if (status)
2058 SetLastError( RtlNtStatusToDosError(status) );
2059 handle = INVALID_HANDLE_VALUE;
2062 RtlFreeUnicodeString( &nameW );
2063 return handle;
2067 /******************************************************************************
2068 * GetMailslotInfo [KERNEL32.@]
2070 * Retrieve information about a mailslot.
2072 * PARAMS
2073 * hMailslot [I] Mailslot handle
2074 * lpMaxMessageSize [O] Address of maximum message size
2075 * lpNextSize [O] Address of size of next message
2076 * lpMessageCount [O] Address of number of messages
2077 * lpReadTimeout [O] Address of read time-out
2079 * RETURNS
2080 * Success: TRUE
2081 * Failure: FALSE
2083 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
2084 LPDWORD lpNextSize, LPDWORD lpMessageCount,
2085 LPDWORD lpReadTimeout )
2087 FILE_MAILSLOT_QUERY_INFORMATION info;
2088 IO_STATUS_BLOCK iosb;
2089 NTSTATUS status;
2091 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
2092 lpNextSize, lpMessageCount, lpReadTimeout);
2094 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
2095 FileMailslotQueryInformation );
2097 if( status != STATUS_SUCCESS )
2099 SetLastError( RtlNtStatusToDosError(status) );
2100 return FALSE;
2103 if( lpMaxMessageSize )
2104 *lpMaxMessageSize = info.MaximumMessageSize;
2105 if( lpNextSize )
2106 *lpNextSize = info.NextMessageSize;
2107 if( lpMessageCount )
2108 *lpMessageCount = info.MessagesAvailable;
2109 if( lpReadTimeout )
2111 if (info.ReadTimeout.QuadPart == (((LONGLONG)0x7fffffff << 32) | 0xffffffff))
2112 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
2113 else
2114 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
2116 return TRUE;
2120 /******************************************************************************
2121 * SetMailslotInfo [KERNEL32.@]
2123 * Set the read timeout of a mailslot.
2125 * PARAMS
2126 * hMailslot [I] Mailslot handle
2127 * dwReadTimeout [I] Timeout in milliseconds.
2129 * RETURNS
2130 * Success: TRUE
2131 * Failure: FALSE
2133 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
2135 FILE_MAILSLOT_SET_INFORMATION info;
2136 IO_STATUS_BLOCK iosb;
2137 NTSTATUS status;
2139 TRACE("%p %d\n", hMailslot, dwReadTimeout);
2141 if (dwReadTimeout != MAILSLOT_WAIT_FOREVER)
2142 info.ReadTimeout.QuadPart = (ULONGLONG)dwReadTimeout * -10000;
2143 else
2144 info.ReadTimeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
2145 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
2146 FileMailslotSetInformation );
2147 if( status != STATUS_SUCCESS )
2149 SetLastError( RtlNtStatusToDosError(status) );
2150 return FALSE;
2152 return TRUE;
2156 /******************************************************************************
2157 * CreateIoCompletionPort (KERNEL32.@)
2159 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
2160 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
2162 NTSTATUS status;
2163 HANDLE ret = 0;
2165 TRACE("(%p, %p, %08lx, %08x)\n",
2166 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
2168 if (hExistingCompletionPort && hFileHandle == INVALID_HANDLE_VALUE)
2170 SetLastError( ERROR_INVALID_PARAMETER);
2171 return NULL;
2174 if (hExistingCompletionPort)
2175 ret = hExistingCompletionPort;
2176 else
2178 status = NtCreateIoCompletion( &ret, IO_COMPLETION_ALL_ACCESS, NULL, dwNumberOfConcurrentThreads );
2179 if (status != STATUS_SUCCESS) goto fail;
2182 if (hFileHandle != INVALID_HANDLE_VALUE)
2184 FILE_COMPLETION_INFORMATION info;
2185 IO_STATUS_BLOCK iosb;
2187 info.CompletionPort = ret;
2188 info.CompletionKey = CompletionKey;
2189 status = NtSetInformationFile( hFileHandle, &iosb, &info, sizeof(info), FileCompletionInformation );
2190 if (status != STATUS_SUCCESS) goto fail;
2193 return ret;
2195 fail:
2196 if (ret && !hExistingCompletionPort)
2197 CloseHandle( ret );
2198 SetLastError( RtlNtStatusToDosError(status) );
2199 return 0;
2202 /******************************************************************************
2203 * GetQueuedCompletionStatus (KERNEL32.@)
2205 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
2206 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
2207 DWORD dwMilliseconds )
2209 NTSTATUS status;
2210 IO_STATUS_BLOCK iosb;
2211 LARGE_INTEGER wait_time;
2213 TRACE("(%p,%p,%p,%p,%d)\n",
2214 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
2216 *lpOverlapped = NULL;
2218 status = NtRemoveIoCompletion( CompletionPort, pCompletionKey, (PULONG_PTR)lpOverlapped,
2219 &iosb, get_nt_timeout( &wait_time, dwMilliseconds ) );
2220 if (status == STATUS_SUCCESS)
2222 *lpNumberOfBytesTransferred = iosb.Information;
2223 if (iosb.u.Status >= 0) return TRUE;
2224 SetLastError( RtlNtStatusToDosError(iosb.u.Status) );
2225 return FALSE;
2228 if (status == STATUS_TIMEOUT) SetLastError( WAIT_TIMEOUT );
2229 else SetLastError( RtlNtStatusToDosError(status) );
2230 return FALSE;
2234 /******************************************************************************
2235 * PostQueuedCompletionStatus (KERNEL32.@)
2237 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
2238 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
2240 NTSTATUS status;
2242 TRACE("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
2244 status = NtSetIoCompletion( CompletionPort, dwCompletionKey, (ULONG_PTR)lpOverlapped,
2245 STATUS_SUCCESS, dwNumberOfBytes );
2247 if (status == STATUS_SUCCESS) return TRUE;
2248 SetLastError( RtlNtStatusToDosError(status) );
2249 return FALSE;
2252 /******************************************************************************
2253 * BindIoCompletionCallback (KERNEL32.@)
2255 BOOL WINAPI BindIoCompletionCallback( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags)
2257 NTSTATUS status;
2259 TRACE("(%p, %p, %d)\n", FileHandle, Function, Flags);
2261 status = RtlSetIoCompletionCallback( FileHandle, (PRTL_OVERLAPPED_COMPLETION_ROUTINE)Function, Flags );
2262 if (status == STATUS_SUCCESS) return TRUE;
2263 SetLastError( RtlNtStatusToDosError(status) );
2264 return FALSE;
2268 /***********************************************************************
2269 * CreateMemoryResourceNotification (KERNEL32.@)
2271 HANDLE WINAPI CreateMemoryResourceNotification(MEMORY_RESOURCE_NOTIFICATION_TYPE type)
2273 static const WCHAR lowmemW[] =
2274 {'\\','K','e','r','n','e','l','O','b','j','e','c','t','s',
2275 '\\','L','o','w','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n',0};
2276 static const WCHAR highmemW[] =
2277 {'\\','K','e','r','n','e','l','O','b','j','e','c','t','s',
2278 '\\','H','i','g','h','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n',0};
2279 HANDLE ret;
2280 UNICODE_STRING nameW;
2281 OBJECT_ATTRIBUTES attr;
2282 NTSTATUS status;
2284 switch (type)
2286 case LowMemoryResourceNotification:
2287 RtlInitUnicodeString( &nameW, lowmemW );
2288 break;
2289 case HighMemoryResourceNotification:
2290 RtlInitUnicodeString( &nameW, highmemW );
2291 break;
2292 default:
2293 SetLastError( ERROR_INVALID_PARAMETER );
2294 return 0;
2297 attr.Length = sizeof(attr);
2298 attr.RootDirectory = 0;
2299 attr.ObjectName = &nameW;
2300 attr.Attributes = 0;
2301 attr.SecurityDescriptor = NULL;
2302 attr.SecurityQualityOfService = NULL;
2303 status = NtOpenEvent( &ret, EVENT_ALL_ACCESS, &attr );
2304 if (status != STATUS_SUCCESS)
2306 SetLastError( RtlNtStatusToDosError(status) );
2307 return 0;
2309 return ret;
2312 /***********************************************************************
2313 * QueryMemoryResourceNotification (KERNEL32.@)
2315 BOOL WINAPI QueryMemoryResourceNotification(HANDLE handle, PBOOL state)
2317 switch (WaitForSingleObject( handle, 0 ))
2319 case WAIT_OBJECT_0:
2320 *state = TRUE;
2321 return TRUE;
2322 case WAIT_TIMEOUT:
2323 *state = FALSE;
2324 return TRUE;
2326 SetLastError( ERROR_INVALID_PARAMETER );
2327 return FALSE;
2330 /***********************************************************************
2331 * InitOnceBeginInitialize (KERNEL32.@)
2333 BOOL WINAPI InitOnceBeginInitialize( INIT_ONCE *once, DWORD flags, BOOL *pending, void **context )
2335 NTSTATUS status = RtlRunOnceBeginInitialize( once, flags, context );
2336 if (status >= 0) *pending = (status == STATUS_PENDING);
2337 else SetLastError( RtlNtStatusToDosError(status) );
2338 return status >= 0;
2341 /***********************************************************************
2342 * InitOnceComplete (KERNEL32.@)
2344 BOOL WINAPI InitOnceComplete( INIT_ONCE *once, DWORD flags, void *context )
2346 NTSTATUS status = RtlRunOnceComplete( once, flags, context );
2347 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
2348 return !status;
2351 /***********************************************************************
2352 * InitOnceExecuteOnce (KERNEL32.@)
2354 BOOL WINAPI InitOnceExecuteOnce( INIT_ONCE *once, PINIT_ONCE_FN func, void *param, void **context )
2356 return !RtlRunOnceExecuteOnce( once, (PRTL_RUN_ONCE_INIT_FN)func, param, context );
2359 #ifdef __i386__
2361 /***********************************************************************
2362 * InterlockedCompareExchange (KERNEL32.@)
2364 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
2365 __ASM_STDCALL_FUNC(InterlockedCompareExchange, 12,
2366 "movl 12(%esp),%eax\n\t"
2367 "movl 8(%esp),%ecx\n\t"
2368 "movl 4(%esp),%edx\n\t"
2369 "lock; cmpxchgl %ecx,(%edx)\n\t"
2370 "ret $12")
2372 /***********************************************************************
2373 * InterlockedExchange (KERNEL32.@)
2375 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
2376 __ASM_STDCALL_FUNC(InterlockedExchange, 8,
2377 "movl 8(%esp),%eax\n\t"
2378 "movl 4(%esp),%edx\n\t"
2379 "lock; xchgl %eax,(%edx)\n\t"
2380 "ret $8")
2382 /***********************************************************************
2383 * InterlockedExchangeAdd (KERNEL32.@)
2385 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
2386 __ASM_STDCALL_FUNC(InterlockedExchangeAdd, 8,
2387 "movl 8(%esp),%eax\n\t"
2388 "movl 4(%esp),%edx\n\t"
2389 "lock; xaddl %eax,(%edx)\n\t"
2390 "ret $8")
2392 /***********************************************************************
2393 * InterlockedIncrement (KERNEL32.@)
2395 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2396 __ASM_STDCALL_FUNC(InterlockedIncrement, 4,
2397 "movl 4(%esp),%edx\n\t"
2398 "movl $1,%eax\n\t"
2399 "lock; xaddl %eax,(%edx)\n\t"
2400 "incl %eax\n\t"
2401 "ret $4")
2403 /***********************************************************************
2404 * InterlockedDecrement (KERNEL32.@)
2406 __ASM_STDCALL_FUNC(InterlockedDecrement, 4,
2407 "movl 4(%esp),%edx\n\t"
2408 "movl $-1,%eax\n\t"
2409 "lock; xaddl %eax,(%edx)\n\t"
2410 "decl %eax\n\t"
2411 "ret $4")
2413 #endif /* __i386__ */
2415 /***********************************************************************
2416 * SleepConditionVariableCS (KERNEL32.@)
2418 BOOL WINAPI SleepConditionVariableCS( CONDITION_VARIABLE *variable, CRITICAL_SECTION *crit, DWORD timeout )
2420 NTSTATUS status;
2421 LARGE_INTEGER time;
2423 status = RtlSleepConditionVariableCS( variable, crit, get_nt_timeout( &time, timeout ) );
2425 if (status != STATUS_SUCCESS)
2427 SetLastError( RtlNtStatusToDosError(status) );
2428 return FALSE;
2430 return TRUE;
2433 /***********************************************************************
2434 * SleepConditionVariableSRW (KERNEL32.@)
2436 BOOL WINAPI SleepConditionVariableSRW( RTL_CONDITION_VARIABLE *variable, RTL_SRWLOCK *lock, DWORD timeout, ULONG flags )
2438 NTSTATUS status;
2439 LARGE_INTEGER time;
2441 status = RtlSleepConditionVariableSRW( variable, lock, get_nt_timeout( &time, timeout ), flags );
2443 if (status != STATUS_SUCCESS)
2445 SetLastError( RtlNtStatusToDosError(status) );
2446 return FALSE;
2448 return TRUE;