d3d9/tests: Add a test for cube texture mipmap autogeneration.
[wine.git] / dlls / kernel32 / sync.c
blobd3d46b7dc8925938bf154c8fe3d68232a788197e
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[] = {'\\','S','e','s','s','i','o','n','s','\\','%','u',
61 '\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
62 WCHAR buffer[64];
63 UNICODE_STRING str;
64 OBJECT_ATTRIBUTES attr;
66 if (!handle)
68 HANDLE dir;
70 sprintfW( buffer, basenameW, NtCurrentTeb()->Peb->SessionId );
71 RtlInitUnicodeString( &str, buffer );
72 InitializeObjectAttributes(&attr, &str, 0, 0, NULL);
73 NtOpenDirectoryObject(&dir, DIRECTORY_CREATE_OBJECT|DIRECTORY_TRAVERSE,
74 &attr);
75 if (InterlockedCompareExchangePointer( &handle, dir, 0 ) != 0)
77 /* someone beat us here... */
78 CloseHandle( dir );
81 return handle;
84 /* helper for kernel32->ntdll timeout format conversion */
85 static inline PLARGE_INTEGER get_nt_timeout( PLARGE_INTEGER pTime, DWORD timeout )
87 if (timeout == INFINITE) return NULL;
88 pTime->QuadPart = (ULONGLONG)timeout * -10000;
89 return pTime;
92 /***********************************************************************
93 * Sleep (KERNEL32.@)
95 VOID WINAPI DECLSPEC_HOTPATCH Sleep( DWORD timeout )
97 SleepEx( timeout, FALSE );
100 /******************************************************************************
101 * SleepEx (KERNEL32.@)
103 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
105 NTSTATUS status;
106 LARGE_INTEGER time;
108 status = NtDelayExecution( alertable, get_nt_timeout( &time, timeout ) );
109 if (status == STATUS_USER_APC) return WAIT_IO_COMPLETION;
110 return 0;
114 /***********************************************************************
115 * SwitchToThread (KERNEL32.@)
117 BOOL WINAPI SwitchToThread(void)
119 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
123 /***********************************************************************
124 * WaitForSingleObject (KERNEL32.@)
126 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
128 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
132 /***********************************************************************
133 * WaitForSingleObjectEx (KERNEL32.@)
135 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
136 BOOL alertable )
138 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
142 /***********************************************************************
143 * WaitForMultipleObjects (KERNEL32.@)
145 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
146 BOOL wait_all, DWORD timeout )
148 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
151 static HANDLE normalize_handle_if_console(HANDLE handle)
153 if ((handle == (HANDLE)STD_INPUT_HANDLE) ||
154 (handle == (HANDLE)STD_OUTPUT_HANDLE) ||
155 (handle == (HANDLE)STD_ERROR_HANDLE))
156 handle = GetStdHandle( HandleToULong(handle) );
158 /* yes, even screen buffer console handles are waitable, and are
159 * handled as a handle to the console itself !!
161 if (is_console_handle(handle))
163 if (VerifyConsoleIoHandle(handle))
164 handle = GetConsoleInputWaitHandle();
166 return handle;
169 /***********************************************************************
170 * WaitForMultipleObjectsEx (KERNEL32.@)
172 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
173 BOOL wait_all, DWORD timeout,
174 BOOL alertable )
176 NTSTATUS status;
177 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
178 LARGE_INTEGER time;
179 unsigned int i;
181 if (count > MAXIMUM_WAIT_OBJECTS)
183 SetLastError(ERROR_INVALID_PARAMETER);
184 return WAIT_FAILED;
186 for (i = 0; i < count; i++)
187 hloc[i] = normalize_handle_if_console(handles[i]);
189 status = NtWaitForMultipleObjects( count, hloc, !wait_all, alertable,
190 get_nt_timeout( &time, timeout ) );
192 if (HIWORD(status)) /* is it an error code? */
194 SetLastError( RtlNtStatusToDosError(status) );
195 status = WAIT_FAILED;
197 return status;
201 /***********************************************************************
202 * RegisterWaitForSingleObject (KERNEL32.@)
204 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
205 WAITORTIMERCALLBACK Callback, PVOID Context,
206 ULONG dwMilliseconds, ULONG dwFlags)
208 NTSTATUS status;
210 TRACE("%p %p %p %p %d %d\n",
211 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
213 hObject = normalize_handle_if_console(hObject);
214 status = RtlRegisterWait( phNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
215 if (status != STATUS_SUCCESS)
217 SetLastError( RtlNtStatusToDosError(status) );
218 return FALSE;
220 return TRUE;
223 /***********************************************************************
224 * RegisterWaitForSingleObjectEx (KERNEL32.@)
226 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
227 WAITORTIMERCALLBACK Callback, PVOID Context,
228 ULONG dwMilliseconds, ULONG dwFlags )
230 NTSTATUS status;
231 HANDLE hNewWaitObject;
233 TRACE("%p %p %p %d %d\n",
234 hObject,Callback,Context,dwMilliseconds,dwFlags);
236 hObject = normalize_handle_if_console(hObject);
237 status = RtlRegisterWait( &hNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
238 if (status != STATUS_SUCCESS)
240 SetLastError( RtlNtStatusToDosError(status) );
241 return NULL;
243 return hNewWaitObject;
246 /***********************************************************************
247 * UnregisterWait (KERNEL32.@)
249 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
251 NTSTATUS status;
253 TRACE("%p\n",WaitHandle);
255 status = RtlDeregisterWait( WaitHandle );
256 if (status != STATUS_SUCCESS)
258 SetLastError( RtlNtStatusToDosError(status) );
259 return FALSE;
261 return TRUE;
264 /***********************************************************************
265 * UnregisterWaitEx (KERNEL32.@)
267 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
269 NTSTATUS status;
271 TRACE("%p %p\n",WaitHandle, CompletionEvent);
273 status = RtlDeregisterWaitEx( WaitHandle, CompletionEvent );
274 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
275 return !status;
278 /***********************************************************************
279 * SignalObjectAndWait (KERNEL32.@)
281 * Makes it possible to atomically signal any of the synchronization
282 * objects (semaphore, mutex, event) and wait on another.
284 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
285 DWORD dwMilliseconds, BOOL bAlertable )
287 NTSTATUS status;
288 LARGE_INTEGER timeout;
290 TRACE("%p %p %d %d\n", hObjectToSignal,
291 hObjectToWaitOn, dwMilliseconds, bAlertable);
293 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn, bAlertable,
294 get_nt_timeout( &timeout, dwMilliseconds ) );
295 if (HIWORD(status))
297 SetLastError( RtlNtStatusToDosError(status) );
298 status = WAIT_FAILED;
300 return status;
303 /***********************************************************************
304 * InitializeCriticalSection (KERNEL32.@)
306 * Initialise a critical section before use.
308 * PARAMS
309 * crit [O] Critical section to initialise.
311 * RETURNS
312 * Nothing. If the function fails an exception is raised.
314 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
316 InitializeCriticalSectionEx( crit, 0, 0 );
319 /***********************************************************************
320 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
322 * Initialise a critical section with a spin count.
324 * PARAMS
325 * crit [O] Critical section to initialise.
326 * spincount [I] Number of times to spin upon contention.
328 * RETURNS
329 * Success: TRUE.
330 * Failure: Nothing. If the function fails an exception is raised.
332 * NOTES
333 * spincount is ignored on uni-processor systems.
335 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
337 return InitializeCriticalSectionEx( crit, spincount, 0 );
340 /***********************************************************************
341 * InitializeCriticalSectionEx (KERNEL32.@)
343 * Initialise a critical section with a spin count and flags.
345 * PARAMS
346 * crit [O] Critical section to initialise.
347 * spincount [I] Number of times to spin upon contention.
348 * flags [I] CRITICAL_SECTION_ flags from winbase.h.
350 * RETURNS
351 * Success: TRUE.
352 * Failure: Nothing. If the function fails an exception is raised.
354 * NOTES
355 * spincount is ignored on uni-processor systems.
357 BOOL WINAPI InitializeCriticalSectionEx( CRITICAL_SECTION *crit, DWORD spincount, DWORD flags )
359 NTSTATUS ret = RtlInitializeCriticalSectionEx( crit, spincount, flags );
360 if (ret) RtlRaiseStatus( ret );
361 return !ret;
364 /***********************************************************************
365 * MakeCriticalSectionGlobal (KERNEL32.@)
367 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
369 /* let's assume that only one thread at a time will try to do this */
370 HANDLE sem = crit->LockSemaphore;
371 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
372 crit->LockSemaphore = ConvertToGlobalHandle( sem );
373 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
374 crit->DebugInfo = NULL;
378 /***********************************************************************
379 * ReinitializeCriticalSection (KERNEL32.@)
381 * Initialise an already used critical section.
383 * PARAMS
384 * crit [O] Critical section to initialise.
386 * RETURNS
387 * Nothing.
389 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
391 if ( !crit->LockSemaphore )
392 RtlInitializeCriticalSection( crit );
396 /***********************************************************************
397 * UninitializeCriticalSection (KERNEL32.@)
399 * UnInitialise a critical section after use.
401 * PARAMS
402 * crit [O] Critical section to uninitialise (destroy).
404 * RETURNS
405 * Nothing.
407 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
409 RtlDeleteCriticalSection( crit );
413 /***********************************************************************
414 * CreateEventA (KERNEL32.@)
416 HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
417 BOOL initial_state, LPCSTR name )
419 DWORD flags = 0;
421 if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET;
422 if (initial_state) flags |= CREATE_EVENT_INITIAL_SET;
423 return CreateEventExA( sa, name, flags, EVENT_ALL_ACCESS );
427 /***********************************************************************
428 * CreateEventW (KERNEL32.@)
430 HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
431 BOOL initial_state, LPCWSTR name )
433 DWORD flags = 0;
435 if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET;
436 if (initial_state) flags |= CREATE_EVENT_INITIAL_SET;
437 return CreateEventExW( sa, name, flags, EVENT_ALL_ACCESS );
441 /***********************************************************************
442 * CreateEventExA (KERNEL32.@)
444 HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
446 WCHAR buffer[MAX_PATH];
448 if (!name) return CreateEventExW( sa, NULL, flags, access );
450 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
452 SetLastError( ERROR_FILENAME_EXCED_RANGE );
453 return 0;
455 return CreateEventExW( sa, buffer, flags, access );
459 /***********************************************************************
460 * CreateEventExW (KERNEL32.@)
462 HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
464 HANDLE ret = 0;
465 UNICODE_STRING nameW;
466 OBJECT_ATTRIBUTES attr;
467 NTSTATUS status;
469 /* one buggy program needs this
470 * ("Van Dale Groot woordenboek der Nederlandse taal")
472 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
474 ERR("Bad security attributes pointer %p\n",sa);
475 SetLastError( ERROR_INVALID_PARAMETER);
476 return 0;
479 attr.Length = sizeof(attr);
480 attr.RootDirectory = 0;
481 attr.ObjectName = NULL;
482 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
483 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
484 attr.SecurityQualityOfService = NULL;
485 if (name)
487 RtlInitUnicodeString( &nameW, name );
488 attr.ObjectName = &nameW;
489 attr.RootDirectory = get_BaseNamedObjects_handle();
492 status = NtCreateEvent( &ret, access, &attr,
493 (flags & CREATE_EVENT_MANUAL_RESET) ? NotificationEvent : SynchronizationEvent,
494 (flags & CREATE_EVENT_INITIAL_SET) != 0 );
495 if (status == STATUS_OBJECT_NAME_EXISTS)
496 SetLastError( ERROR_ALREADY_EXISTS );
497 else
498 SetLastError( RtlNtStatusToDosError(status) );
499 return ret;
503 /***********************************************************************
504 * OpenEventA (KERNEL32.@)
506 HANDLE WINAPI DECLSPEC_HOTPATCH OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
508 WCHAR buffer[MAX_PATH];
510 if (!name) return OpenEventW( access, inherit, NULL );
512 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
514 SetLastError( ERROR_FILENAME_EXCED_RANGE );
515 return 0;
517 return OpenEventW( access, inherit, buffer );
521 /***********************************************************************
522 * OpenEventW (KERNEL32.@)
524 HANDLE WINAPI DECLSPEC_HOTPATCH OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
526 HANDLE ret;
527 UNICODE_STRING nameW;
528 OBJECT_ATTRIBUTES attr;
529 NTSTATUS status;
531 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
533 attr.Length = sizeof(attr);
534 attr.RootDirectory = 0;
535 attr.ObjectName = NULL;
536 attr.Attributes = inherit ? OBJ_INHERIT : 0;
537 attr.SecurityDescriptor = NULL;
538 attr.SecurityQualityOfService = NULL;
539 if (name)
541 RtlInitUnicodeString( &nameW, name );
542 attr.ObjectName = &nameW;
543 attr.RootDirectory = get_BaseNamedObjects_handle();
546 status = NtOpenEvent( &ret, access, &attr );
547 if (status != STATUS_SUCCESS)
549 SetLastError( RtlNtStatusToDosError(status) );
550 return 0;
552 return ret;
555 /***********************************************************************
556 * PulseEvent (KERNEL32.@)
558 BOOL WINAPI DECLSPEC_HOTPATCH PulseEvent( HANDLE handle )
560 NTSTATUS status;
562 if ((status = NtPulseEvent( handle, NULL )))
563 SetLastError( RtlNtStatusToDosError(status) );
564 return !status;
568 /***********************************************************************
569 * SetEvent (KERNEL32.@)
571 BOOL WINAPI DECLSPEC_HOTPATCH SetEvent( HANDLE handle )
573 NTSTATUS status;
575 if ((status = NtSetEvent( handle, NULL )))
576 SetLastError( RtlNtStatusToDosError(status) );
577 return !status;
581 /***********************************************************************
582 * ResetEvent (KERNEL32.@)
584 BOOL WINAPI DECLSPEC_HOTPATCH ResetEvent( HANDLE handle )
586 NTSTATUS status;
588 if ((status = NtResetEvent( handle, NULL )))
589 SetLastError( RtlNtStatusToDosError(status) );
590 return !status;
594 /***********************************************************************
595 * CreateMutexA (KERNEL32.@)
597 HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
599 return CreateMutexExA( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS );
603 /***********************************************************************
604 * CreateMutexW (KERNEL32.@)
606 HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
608 return CreateMutexExW( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS );
612 /***********************************************************************
613 * CreateMutexExA (KERNEL32.@)
615 HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
617 ANSI_STRING nameA;
618 NTSTATUS status;
620 if (!name) return CreateMutexExW( sa, NULL, flags, access );
622 RtlInitAnsiString( &nameA, name );
623 status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString, &nameA, FALSE );
624 if (status != STATUS_SUCCESS)
626 SetLastError( ERROR_FILENAME_EXCED_RANGE );
627 return 0;
629 return CreateMutexExW( sa, NtCurrentTeb()->StaticUnicodeString.Buffer, flags, access );
633 /***********************************************************************
634 * CreateMutexExW (KERNEL32.@)
636 HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
638 HANDLE ret = 0;
639 UNICODE_STRING nameW;
640 OBJECT_ATTRIBUTES attr;
641 NTSTATUS status;
643 attr.Length = sizeof(attr);
644 attr.RootDirectory = 0;
645 attr.ObjectName = NULL;
646 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
647 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
648 attr.SecurityQualityOfService = NULL;
649 if (name)
651 RtlInitUnicodeString( &nameW, name );
652 attr.ObjectName = &nameW;
653 attr.RootDirectory = get_BaseNamedObjects_handle();
656 status = NtCreateMutant( &ret, access, &attr, (flags & CREATE_MUTEX_INITIAL_OWNER) != 0 );
657 if (status == STATUS_OBJECT_NAME_EXISTS)
658 SetLastError( ERROR_ALREADY_EXISTS );
659 else
660 SetLastError( RtlNtStatusToDosError(status) );
661 return ret;
665 /***********************************************************************
666 * OpenMutexA (KERNEL32.@)
668 HANDLE WINAPI DECLSPEC_HOTPATCH OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
670 WCHAR buffer[MAX_PATH];
672 if (!name) return OpenMutexW( access, inherit, NULL );
674 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
676 SetLastError( ERROR_FILENAME_EXCED_RANGE );
677 return 0;
679 return OpenMutexW( access, inherit, buffer );
683 /***********************************************************************
684 * OpenMutexW (KERNEL32.@)
686 HANDLE WINAPI DECLSPEC_HOTPATCH OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
688 HANDLE ret;
689 UNICODE_STRING nameW;
690 OBJECT_ATTRIBUTES attr;
691 NTSTATUS status;
693 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
695 attr.Length = sizeof(attr);
696 attr.RootDirectory = 0;
697 attr.ObjectName = NULL;
698 attr.Attributes = inherit ? OBJ_INHERIT : 0;
699 attr.SecurityDescriptor = NULL;
700 attr.SecurityQualityOfService = NULL;
701 if (name)
703 RtlInitUnicodeString( &nameW, name );
704 attr.ObjectName = &nameW;
705 attr.RootDirectory = get_BaseNamedObjects_handle();
708 status = NtOpenMutant( &ret, access, &attr );
709 if (status != STATUS_SUCCESS)
711 SetLastError( RtlNtStatusToDosError(status) );
712 return 0;
714 return ret;
718 /***********************************************************************
719 * ReleaseMutex (KERNEL32.@)
721 BOOL WINAPI DECLSPEC_HOTPATCH ReleaseMutex( HANDLE handle )
723 NTSTATUS status;
725 status = NtReleaseMutant(handle, NULL);
726 if (status != STATUS_SUCCESS)
728 SetLastError( RtlNtStatusToDosError(status) );
729 return FALSE;
731 return TRUE;
736 * Semaphores
740 /***********************************************************************
741 * CreateSemaphoreA (KERNEL32.@)
743 HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
745 return CreateSemaphoreExA( sa, initial, max, name, 0, SEMAPHORE_ALL_ACCESS );
749 /***********************************************************************
750 * CreateSemaphoreW (KERNEL32.@)
752 HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCWSTR name )
754 return CreateSemaphoreExW( sa, initial, max, name, 0, SEMAPHORE_ALL_ACCESS );
758 /***********************************************************************
759 * CreateSemaphoreExA (KERNEL32.@)
761 HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreExA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max,
762 LPCSTR name, DWORD flags, DWORD access )
764 WCHAR buffer[MAX_PATH];
766 if (!name) return CreateSemaphoreExW( sa, initial, max, NULL, flags, access );
768 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
770 SetLastError( ERROR_FILENAME_EXCED_RANGE );
771 return 0;
773 return CreateSemaphoreExW( sa, initial, max, buffer, flags, access );
777 /***********************************************************************
778 * CreateSemaphoreExW (KERNEL32.@)
780 HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreExW( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max,
781 LPCWSTR name, DWORD flags, DWORD access )
783 HANDLE ret = 0;
784 UNICODE_STRING nameW;
785 OBJECT_ATTRIBUTES attr;
786 NTSTATUS status;
788 attr.Length = sizeof(attr);
789 attr.RootDirectory = 0;
790 attr.ObjectName = NULL;
791 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
792 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
793 attr.SecurityQualityOfService = NULL;
794 if (name)
796 RtlInitUnicodeString( &nameW, name );
797 attr.ObjectName = &nameW;
798 attr.RootDirectory = get_BaseNamedObjects_handle();
801 status = NtCreateSemaphore( &ret, access, &attr, initial, max );
802 if (status == STATUS_OBJECT_NAME_EXISTS)
803 SetLastError( ERROR_ALREADY_EXISTS );
804 else
805 SetLastError( RtlNtStatusToDosError(status) );
806 return ret;
810 /***********************************************************************
811 * OpenSemaphoreA (KERNEL32.@)
813 HANDLE WINAPI DECLSPEC_HOTPATCH OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
815 WCHAR buffer[MAX_PATH];
817 if (!name) return OpenSemaphoreW( access, inherit, NULL );
819 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
821 SetLastError( ERROR_FILENAME_EXCED_RANGE );
822 return 0;
824 return OpenSemaphoreW( access, inherit, buffer );
828 /***********************************************************************
829 * OpenSemaphoreW (KERNEL32.@)
831 HANDLE WINAPI DECLSPEC_HOTPATCH OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
833 HANDLE ret;
834 UNICODE_STRING nameW;
835 OBJECT_ATTRIBUTES attr;
836 NTSTATUS status;
838 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
840 attr.Length = sizeof(attr);
841 attr.RootDirectory = 0;
842 attr.ObjectName = NULL;
843 attr.Attributes = inherit ? OBJ_INHERIT : 0;
844 attr.SecurityDescriptor = NULL;
845 attr.SecurityQualityOfService = NULL;
846 if (name)
848 RtlInitUnicodeString( &nameW, name );
849 attr.ObjectName = &nameW;
850 attr.RootDirectory = get_BaseNamedObjects_handle();
853 status = NtOpenSemaphore( &ret, access, &attr );
854 if (status != STATUS_SUCCESS)
856 SetLastError( RtlNtStatusToDosError(status) );
857 return 0;
859 return ret;
863 /***********************************************************************
864 * ReleaseSemaphore (KERNEL32.@)
866 BOOL WINAPI DECLSPEC_HOTPATCH ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
868 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
869 if (status) SetLastError( RtlNtStatusToDosError(status) );
870 return !status;
875 * Jobs
878 /******************************************************************************
879 * CreateJobObjectW (KERNEL32.@)
881 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES sa, LPCWSTR name )
883 HANDLE ret = 0;
884 UNICODE_STRING nameW;
885 OBJECT_ATTRIBUTES attr;
886 NTSTATUS status;
888 attr.Length = sizeof(attr);
889 attr.RootDirectory = 0;
890 attr.ObjectName = NULL;
891 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
892 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
893 attr.SecurityQualityOfService = NULL;
894 if (name)
896 RtlInitUnicodeString( &nameW, name );
897 attr.ObjectName = &nameW;
898 attr.RootDirectory = get_BaseNamedObjects_handle();
901 status = NtCreateJobObject( &ret, JOB_OBJECT_ALL_ACCESS, &attr );
902 if (status == STATUS_OBJECT_NAME_EXISTS)
903 SetLastError( ERROR_ALREADY_EXISTS );
904 else
905 SetLastError( RtlNtStatusToDosError(status) );
906 return ret;
909 /******************************************************************************
910 * CreateJobObjectA (KERNEL32.@)
912 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
914 WCHAR buffer[MAX_PATH];
916 if (!name) return CreateJobObjectW( attr, NULL );
918 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
920 SetLastError( ERROR_FILENAME_EXCED_RANGE );
921 return 0;
923 return CreateJobObjectW( attr, buffer );
926 /******************************************************************************
927 * OpenJobObjectW (KERNEL32.@)
929 HANDLE WINAPI OpenJobObjectW( DWORD access, BOOL inherit, LPCWSTR name )
931 HANDLE ret;
932 UNICODE_STRING nameW;
933 OBJECT_ATTRIBUTES attr;
934 NTSTATUS status;
936 attr.Length = sizeof(attr);
937 attr.RootDirectory = 0;
938 attr.ObjectName = NULL;
939 attr.Attributes = inherit ? OBJ_INHERIT : 0;
940 attr.SecurityDescriptor = NULL;
941 attr.SecurityQualityOfService = NULL;
942 if (name)
944 RtlInitUnicodeString( &nameW, name );
945 attr.ObjectName = &nameW;
946 attr.RootDirectory = get_BaseNamedObjects_handle();
949 status = NtOpenJobObject( &ret, access, &attr );
950 if (status != STATUS_SUCCESS)
952 SetLastError( RtlNtStatusToDosError(status) );
953 return 0;
955 return ret;
958 /******************************************************************************
959 * OpenJobObjectA (KERNEL32.@)
961 HANDLE WINAPI OpenJobObjectA( DWORD access, BOOL inherit, LPCSTR name )
963 WCHAR buffer[MAX_PATH];
965 if (!name) return OpenJobObjectW( access, inherit, NULL );
967 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
969 SetLastError( ERROR_FILENAME_EXCED_RANGE );
970 return 0;
972 return OpenJobObjectW( access, inherit, buffer );
975 /******************************************************************************
976 * TerminateJobObject (KERNEL32.@)
978 BOOL WINAPI TerminateJobObject( HANDLE job, UINT exit_code )
980 NTSTATUS status = NtTerminateJobObject( job, exit_code );
981 if (status) SetLastError( RtlNtStatusToDosError(status) );
982 return !status;
985 /******************************************************************************
986 * QueryInformationJobObject (KERNEL32.@)
988 BOOL WINAPI QueryInformationJobObject( HANDLE job, JOBOBJECTINFOCLASS class, LPVOID info,
989 DWORD len, DWORD *ret_len )
991 NTSTATUS status = NtQueryInformationJobObject( job, class, info, len, ret_len );
992 if (status) SetLastError( RtlNtStatusToDosError(status) );
993 return !status;
996 /******************************************************************************
997 * SetInformationJobObject (KERNEL32.@)
999 BOOL WINAPI SetInformationJobObject( HANDLE job, JOBOBJECTINFOCLASS class, LPVOID info, DWORD len )
1001 NTSTATUS status = NtSetInformationJobObject( job, class, info, len );
1002 if (status) SetLastError( RtlNtStatusToDosError(status) );
1003 return !status;
1006 /******************************************************************************
1007 * AssignProcessToJobObject (KERNEL32.@)
1009 BOOL WINAPI AssignProcessToJobObject( HANDLE job, HANDLE process )
1011 NTSTATUS status = NtAssignProcessToJobObject( job, process );
1012 if (status) SetLastError( RtlNtStatusToDosError(status) );
1013 return !status;
1016 /******************************************************************************
1017 * IsProcessInJob (KERNEL32.@)
1019 BOOL WINAPI IsProcessInJob( HANDLE process, HANDLE job, PBOOL result )
1021 NTSTATUS status = NtIsProcessInJob( process, job );
1022 switch(status)
1024 case STATUS_PROCESS_IN_JOB:
1025 *result = TRUE;
1026 return TRUE;
1027 case STATUS_PROCESS_NOT_IN_JOB:
1028 *result = FALSE;
1029 return TRUE;
1030 default:
1031 SetLastError( RtlNtStatusToDosError(status) );
1032 return FALSE;
1038 * Timers
1042 /***********************************************************************
1043 * CreateWaitableTimerA (KERNEL32.@)
1045 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
1047 return CreateWaitableTimerExA( sa, name, manual ? CREATE_WAITABLE_TIMER_MANUAL_RESET : 0,
1048 TIMER_ALL_ACCESS );
1052 /***********************************************************************
1053 * CreateWaitableTimerW (KERNEL32.@)
1055 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
1057 return CreateWaitableTimerExW( sa, name, manual ? CREATE_WAITABLE_TIMER_MANUAL_RESET : 0,
1058 TIMER_ALL_ACCESS );
1062 /***********************************************************************
1063 * CreateWaitableTimerExA (KERNEL32.@)
1065 HANDLE WINAPI CreateWaitableTimerExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
1067 WCHAR buffer[MAX_PATH];
1069 if (!name) return CreateWaitableTimerExW( sa, NULL, flags, access );
1071 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1073 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1074 return 0;
1076 return CreateWaitableTimerExW( sa, buffer, flags, access );
1080 /***********************************************************************
1081 * CreateWaitableTimerExW (KERNEL32.@)
1083 HANDLE WINAPI CreateWaitableTimerExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
1085 HANDLE handle;
1086 NTSTATUS status;
1087 UNICODE_STRING nameW;
1088 OBJECT_ATTRIBUTES attr;
1090 attr.Length = sizeof(attr);
1091 attr.RootDirectory = 0;
1092 attr.ObjectName = NULL;
1093 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1094 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1095 attr.SecurityQualityOfService = NULL;
1096 if (name)
1098 RtlInitUnicodeString( &nameW, name );
1099 attr.ObjectName = &nameW;
1100 attr.RootDirectory = get_BaseNamedObjects_handle();
1103 status = NtCreateTimer( &handle, access, &attr,
1104 (flags & CREATE_WAITABLE_TIMER_MANUAL_RESET) ? NotificationTimer : SynchronizationTimer );
1105 if (status == STATUS_OBJECT_NAME_EXISTS)
1106 SetLastError( ERROR_ALREADY_EXISTS );
1107 else
1108 SetLastError( RtlNtStatusToDosError(status) );
1109 return handle;
1113 /***********************************************************************
1114 * OpenWaitableTimerA (KERNEL32.@)
1116 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
1118 WCHAR buffer[MAX_PATH];
1120 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
1122 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1124 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1125 return 0;
1127 return OpenWaitableTimerW( access, inherit, buffer );
1131 /***********************************************************************
1132 * OpenWaitableTimerW (KERNEL32.@)
1134 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
1136 HANDLE handle;
1137 UNICODE_STRING nameW;
1138 OBJECT_ATTRIBUTES attr;
1139 NTSTATUS status;
1141 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
1143 attr.Length = sizeof(attr);
1144 attr.RootDirectory = 0;
1145 attr.ObjectName = NULL;
1146 attr.Attributes = inherit ? OBJ_INHERIT : 0;
1147 attr.SecurityDescriptor = NULL;
1148 attr.SecurityQualityOfService = NULL;
1149 if (name)
1151 RtlInitUnicodeString( &nameW, name );
1152 attr.ObjectName = &nameW;
1153 attr.RootDirectory = get_BaseNamedObjects_handle();
1156 status = NtOpenTimer(&handle, access, &attr);
1157 if (status != STATUS_SUCCESS)
1159 SetLastError( RtlNtStatusToDosError(status) );
1160 return 0;
1162 return handle;
1166 /***********************************************************************
1167 * SetWaitableTimer (KERNEL32.@)
1169 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
1170 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
1172 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
1173 arg, resume, period, NULL);
1175 if (status != STATUS_SUCCESS)
1177 SetLastError( RtlNtStatusToDosError(status) );
1178 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
1180 return TRUE;
1183 /***********************************************************************
1184 * SetWaitableTimerEx (KERNEL32.@)
1186 BOOL WINAPI SetWaitableTimerEx( HANDLE handle, const LARGE_INTEGER *when, LONG period,
1187 PTIMERAPCROUTINE callback, LPVOID arg, REASON_CONTEXT *context, ULONG tolerabledelay )
1189 static int once;
1190 if (!once++)
1192 FIXME("(%p, %p, %d, %p, %p, %p, %d) semi-stub\n",
1193 handle, when, period, callback, arg, context, tolerabledelay);
1195 return SetWaitableTimer(handle, when, period, callback, arg, FALSE);
1198 /***********************************************************************
1199 * CancelWaitableTimer (KERNEL32.@)
1201 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1203 NTSTATUS status;
1205 status = NtCancelTimer(handle, NULL);
1206 if (status != STATUS_SUCCESS)
1208 SetLastError( RtlNtStatusToDosError(status) );
1209 return FALSE;
1211 return TRUE;
1215 /***********************************************************************
1216 * CreateTimerQueue (KERNEL32.@)
1218 HANDLE WINAPI CreateTimerQueue(void)
1220 HANDLE q;
1221 NTSTATUS status = RtlCreateTimerQueue(&q);
1223 if (status != STATUS_SUCCESS)
1225 SetLastError( RtlNtStatusToDosError(status) );
1226 return NULL;
1229 return q;
1233 /***********************************************************************
1234 * DeleteTimerQueueEx (KERNEL32.@)
1236 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1238 NTSTATUS status = RtlDeleteTimerQueueEx(TimerQueue, CompletionEvent);
1240 if (status != STATUS_SUCCESS)
1242 SetLastError( RtlNtStatusToDosError(status) );
1243 return FALSE;
1246 return TRUE;
1249 /***********************************************************************
1250 * DeleteTimerQueue (KERNEL32.@)
1252 BOOL WINAPI DeleteTimerQueue(HANDLE TimerQueue)
1254 return DeleteTimerQueueEx(TimerQueue, NULL);
1257 /***********************************************************************
1258 * CreateTimerQueueTimer (KERNEL32.@)
1260 * Creates a timer-queue timer. This timer expires at the specified due
1261 * time (in ms), then after every specified period (in ms). When the timer
1262 * expires, the callback function is called.
1264 * RETURNS
1265 * nonzero on success or zero on failure
1267 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1268 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1269 DWORD DueTime, DWORD Period, ULONG Flags )
1271 NTSTATUS status = RtlCreateTimer(phNewTimer, TimerQueue, Callback,
1272 Parameter, DueTime, Period, Flags);
1274 if (status != STATUS_SUCCESS)
1276 SetLastError( RtlNtStatusToDosError(status) );
1277 return FALSE;
1280 return TRUE;
1283 /***********************************************************************
1284 * ChangeTimerQueueTimer (KERNEL32.@)
1286 * Changes the times at which the timer expires.
1288 * RETURNS
1289 * nonzero on success or zero on failure
1291 BOOL WINAPI ChangeTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1292 ULONG DueTime, ULONG Period )
1294 NTSTATUS status = RtlUpdateTimer(TimerQueue, Timer, DueTime, Period);
1296 if (status != STATUS_SUCCESS)
1298 SetLastError( RtlNtStatusToDosError(status) );
1299 return FALSE;
1302 return TRUE;
1305 /***********************************************************************
1306 * CancelTimerQueueTimer (KERNEL32.@)
1308 BOOL WINAPI CancelTimerQueueTimer(HANDLE queue, HANDLE timer)
1310 FIXME("stub: %p %p\n", queue, timer);
1311 return FALSE;
1314 /***********************************************************************
1315 * DeleteTimerQueueTimer (KERNEL32.@)
1317 * Cancels a timer-queue timer.
1319 * RETURNS
1320 * nonzero on success or zero on failure
1322 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1323 HANDLE CompletionEvent )
1325 NTSTATUS status = RtlDeleteTimer(TimerQueue, Timer, CompletionEvent);
1326 if (status != STATUS_SUCCESS)
1328 SetLastError( RtlNtStatusToDosError(status) );
1329 return FALSE;
1331 return TRUE;
1336 * Pipes
1340 /***********************************************************************
1341 * CreateNamedPipeA (KERNEL32.@)
1343 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1344 DWORD dwPipeMode, DWORD nMaxInstances,
1345 DWORD nOutBufferSize, DWORD nInBufferSize,
1346 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1348 WCHAR buffer[MAX_PATH];
1350 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1351 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1353 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1355 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1356 return INVALID_HANDLE_VALUE;
1358 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1359 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1363 /***********************************************************************
1364 * CreateNamedPipeW (KERNEL32.@)
1366 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1367 DWORD dwPipeMode, DWORD nMaxInstances,
1368 DWORD nOutBufferSize, DWORD nInBufferSize,
1369 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1371 HANDLE handle;
1372 UNICODE_STRING nt_name;
1373 OBJECT_ATTRIBUTES attr;
1374 DWORD access, options, sharing;
1375 BOOLEAN pipe_type, read_mode, non_block;
1376 NTSTATUS status;
1377 IO_STATUS_BLOCK iosb;
1378 LARGE_INTEGER timeout;
1380 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1381 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1382 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1384 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1386 SetLastError( ERROR_PATH_NOT_FOUND );
1387 return INVALID_HANDLE_VALUE;
1389 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1391 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1392 RtlFreeUnicodeString( &nt_name );
1393 return INVALID_HANDLE_VALUE;
1396 attr.Length = sizeof(attr);
1397 attr.RootDirectory = 0;
1398 attr.ObjectName = &nt_name;
1399 attr.Attributes = OBJ_CASE_INSENSITIVE |
1400 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1401 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1402 attr.SecurityQualityOfService = NULL;
1404 switch(dwOpenMode & 3)
1406 case PIPE_ACCESS_INBOUND:
1407 sharing = FILE_SHARE_WRITE;
1408 access = GENERIC_READ;
1409 break;
1410 case PIPE_ACCESS_OUTBOUND:
1411 sharing = FILE_SHARE_READ;
1412 access = GENERIC_WRITE;
1413 break;
1414 case PIPE_ACCESS_DUPLEX:
1415 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1416 access = GENERIC_READ | GENERIC_WRITE;
1417 break;
1418 default:
1419 SetLastError( ERROR_INVALID_PARAMETER );
1420 return INVALID_HANDLE_VALUE;
1422 access |= SYNCHRONIZE;
1423 options = 0;
1424 if (dwOpenMode & WRITE_DAC) access |= WRITE_DAC;
1425 if (dwOpenMode & WRITE_OWNER) access |= WRITE_OWNER;
1426 if (dwOpenMode & ACCESS_SYSTEM_SECURITY) access |= ACCESS_SYSTEM_SECURITY;
1427 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1428 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_NONALERT;
1429 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) != 0;
1430 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) != 0;
1431 non_block = (dwPipeMode & PIPE_NOWAIT) != 0;
1432 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0U;
1434 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1436 SetLastError(0);
1438 status = NtCreateNamedPipeFile(&handle, access, &attr, &iosb, sharing,
1439 FILE_OVERWRITE_IF, options, pipe_type,
1440 read_mode, non_block, nMaxInstances,
1441 nInBufferSize, nOutBufferSize, &timeout);
1443 RtlFreeUnicodeString( &nt_name );
1444 if (status)
1446 handle = INVALID_HANDLE_VALUE;
1447 SetLastError( RtlNtStatusToDosError(status) );
1449 return handle;
1453 /***********************************************************************
1454 * PeekNamedPipe (KERNEL32.@)
1456 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1457 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1459 FILE_PIPE_PEEK_BUFFER local_buffer;
1460 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1461 IO_STATUS_BLOCK io;
1462 NTSTATUS status;
1464 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1465 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1467 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1468 return FALSE;
1471 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1472 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1473 if (!status)
1475 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1476 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1477 if (lpcbRead) *lpcbRead = read_size;
1478 if (lpcbMessage) *lpcbMessage = buffer->MessageLength - read_size;
1479 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1481 else SetLastError( RtlNtStatusToDosError(status) );
1483 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1484 return !status;
1487 /***********************************************************************
1488 * WaitNamedPipeA (KERNEL32.@)
1490 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1492 WCHAR buffer[MAX_PATH];
1494 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1496 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1498 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1499 return FALSE;
1501 return WaitNamedPipeW( buffer, nTimeOut );
1505 /***********************************************************************
1506 * WaitNamedPipeW (KERNEL32.@)
1508 * Waits for a named pipe instance to become available
1510 * PARAMS
1511 * name [I] Pointer to a named pipe name to wait for
1512 * nTimeOut [I] How long to wait in ms
1514 * RETURNS
1515 * TRUE: Success, named pipe can be opened with CreateFile
1516 * FALSE: Failure, GetLastError can be called for further details
1518 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1520 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1521 NTSTATUS status;
1522 UNICODE_STRING nt_name, pipe_dev_name;
1523 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1524 IO_STATUS_BLOCK iosb;
1525 OBJECT_ATTRIBUTES attr;
1526 ULONG sz_pipe_wait;
1527 HANDLE pipe_dev;
1529 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1531 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1532 return FALSE;
1534 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1535 nt_name.Length < sizeof(leadin) ||
1536 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR)) != 0)
1538 RtlFreeUnicodeString( &nt_name );
1539 SetLastError( ERROR_PATH_NOT_FOUND );
1540 return FALSE;
1543 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1544 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1546 RtlFreeUnicodeString( &nt_name );
1547 SetLastError( ERROR_OUTOFMEMORY );
1548 return FALSE;
1551 pipe_dev_name.Buffer = nt_name.Buffer;
1552 pipe_dev_name.Length = sizeof(leadin);
1553 pipe_dev_name.MaximumLength = sizeof(leadin);
1554 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1555 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES | SYNCHRONIZE, &attr,
1556 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1557 FILE_SYNCHRONOUS_IO_NONALERT);
1558 if (status != STATUS_SUCCESS)
1560 HeapFree( GetProcessHeap(), 0, pipe_wait);
1561 RtlFreeUnicodeString( &nt_name );
1562 SetLastError( ERROR_PATH_NOT_FOUND );
1563 return FALSE;
1566 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1567 if (nTimeOut == NMPWAIT_WAIT_FOREVER)
1568 pipe_wait->Timeout.QuadPart = ((ULONGLONG)0x7fffffff << 32) | 0xffffffff;
1569 else
1570 pipe_wait->Timeout.QuadPart = (ULONGLONG)nTimeOut * -10000;
1571 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1572 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1573 pipe_wait->NameLength);
1574 RtlFreeUnicodeString( &nt_name );
1576 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1577 pipe_wait, sz_pipe_wait, NULL, 0 );
1579 HeapFree( GetProcessHeap(), 0, pipe_wait );
1580 NtClose( pipe_dev );
1582 if(status != STATUS_SUCCESS)
1584 SetLastError(RtlNtStatusToDosError(status));
1585 return FALSE;
1587 else
1588 return TRUE;
1592 /***********************************************************************
1593 * ConnectNamedPipe (KERNEL32.@)
1595 * Connects to a named pipe
1597 * Parameters
1598 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1599 * overlapped: Optional OVERLAPPED struct
1601 * Return values
1602 * TRUE: Success
1603 * FALSE: Failure, GetLastError can be called for further details
1605 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1607 NTSTATUS status;
1608 IO_STATUS_BLOCK status_block;
1609 LPVOID cvalue = NULL;
1611 TRACE("(%p,%p)\n", hPipe, overlapped);
1613 if(overlapped)
1615 overlapped->Internal = STATUS_PENDING;
1616 overlapped->InternalHigh = 0;
1617 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1620 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, cvalue,
1621 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1622 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1624 if (status == STATUS_SUCCESS) return TRUE;
1625 SetLastError( RtlNtStatusToDosError(status) );
1626 return FALSE;
1629 /***********************************************************************
1630 * DisconnectNamedPipe (KERNEL32.@)
1632 * Disconnects from a named pipe
1634 * Parameters
1635 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1637 * Return values
1638 * TRUE: Success
1639 * FALSE: Failure, GetLastError can be called for further details
1641 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1643 NTSTATUS status;
1644 IO_STATUS_BLOCK io_block;
1646 TRACE("(%p)\n",hPipe);
1648 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1649 NULL, 0, NULL, 0);
1650 if (status == STATUS_SUCCESS) return TRUE;
1651 SetLastError( RtlNtStatusToDosError(status) );
1652 return FALSE;
1655 /***********************************************************************
1656 * TransactNamedPipe (KERNEL32.@)
1658 * BUGS
1659 * should be done as a single operation in the wineserver or kernel
1661 BOOL WINAPI TransactNamedPipe(
1662 HANDLE handle, LPVOID write_buf, DWORD write_size, LPVOID read_buf,
1663 DWORD read_size, LPDWORD bytes_read, LPOVERLAPPED overlapped)
1665 BOOL r;
1666 DWORD count;
1668 TRACE("%p %p %d %p %d %p %p\n",
1669 handle, write_buf, write_size, read_buf,
1670 read_size, bytes_read, overlapped);
1672 if (overlapped)
1674 FIXME("Doesn't support overlapped operation as yet\n");
1675 return FALSE;
1678 r = WriteFile(handle, write_buf, write_size, &count, NULL);
1679 if (r)
1680 r = ReadFile(handle, read_buf, read_size, bytes_read, NULL);
1682 return r;
1685 /***********************************************************************
1686 * GetNamedPipeInfo (KERNEL32.@)
1688 BOOL WINAPI GetNamedPipeInfo(
1689 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1690 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1692 FILE_PIPE_LOCAL_INFORMATION fpli;
1693 IO_STATUS_BLOCK iosb;
1694 NTSTATUS status;
1696 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1697 FilePipeLocalInformation);
1698 if (status)
1700 SetLastError( RtlNtStatusToDosError(status) );
1701 return FALSE;
1704 if (lpFlags)
1706 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1707 PIPE_SERVER_END : PIPE_CLIENT_END;
1708 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1709 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1712 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1713 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1714 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1716 return TRUE;
1719 /***********************************************************************
1720 * GetNamedPipeHandleStateA (KERNEL32.@)
1722 BOOL WINAPI GetNamedPipeHandleStateA(
1723 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1724 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1725 LPSTR lpUsername, DWORD nUsernameMaxSize)
1727 WARN("%p %p %p %p %p %p %d: semi-stub\n",
1728 hNamedPipe, lpState, lpCurInstances,
1729 lpMaxCollectionCount, lpCollectDataTimeout,
1730 lpUsername, nUsernameMaxSize);
1732 if (lpUsername && nUsernameMaxSize)
1733 *lpUsername = 0;
1735 return GetNamedPipeHandleStateW(hNamedPipe, lpState, lpCurInstances,
1736 lpMaxCollectionCount, lpCollectDataTimeout, NULL, 0);
1739 /***********************************************************************
1740 * GetNamedPipeHandleStateW (KERNEL32.@)
1742 BOOL WINAPI GetNamedPipeHandleStateW(
1743 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1744 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1745 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1747 IO_STATUS_BLOCK iosb;
1748 NTSTATUS status;
1750 FIXME("%p %p %p %p %p %p %d: semi-stub\n",
1751 hNamedPipe, lpState, lpCurInstances,
1752 lpMaxCollectionCount, lpCollectDataTimeout,
1753 lpUsername, nUsernameMaxSize);
1755 if (lpMaxCollectionCount)
1756 *lpMaxCollectionCount = 0;
1758 if (lpCollectDataTimeout)
1759 *lpCollectDataTimeout = 0;
1761 if (lpUsername && nUsernameMaxSize)
1762 *lpUsername = 0;
1764 if (lpState)
1766 FILE_PIPE_INFORMATION fpi;
1767 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpi, sizeof(fpi),
1768 FilePipeInformation);
1769 if (status)
1771 SetLastError( RtlNtStatusToDosError(status) );
1772 return FALSE;
1775 *lpState = (fpi.ReadMode ? PIPE_READMODE_MESSAGE : PIPE_READMODE_BYTE) |
1776 (fpi.CompletionMode ? PIPE_NOWAIT : PIPE_WAIT);
1779 if (lpCurInstances)
1781 FILE_PIPE_LOCAL_INFORMATION fpli;
1782 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1783 FilePipeLocalInformation);
1784 if (status)
1786 SetLastError( RtlNtStatusToDosError(status) );
1787 return FALSE;
1790 *lpCurInstances = fpli.CurrentInstances;
1793 return TRUE;
1796 /***********************************************************************
1797 * SetNamedPipeHandleState (KERNEL32.@)
1799 BOOL WINAPI SetNamedPipeHandleState(
1800 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1801 LPDWORD lpCollectDataTimeout)
1803 /* should be a fixme, but this function is called a lot by the RPC
1804 * runtime, and it slows down InstallShield a fair bit. */
1805 WARN("semi-stub: %p %p/%d %p %p\n",
1806 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1808 if (lpMode)
1810 FILE_PIPE_INFORMATION fpi;
1811 IO_STATUS_BLOCK iosb;
1812 NTSTATUS status;
1814 if (*lpMode & ~(PIPE_READMODE_MESSAGE | PIPE_NOWAIT))
1815 status = STATUS_INVALID_PARAMETER;
1816 else
1818 fpi.CompletionMode = (*lpMode & PIPE_NOWAIT) ?
1819 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
1820 fpi.ReadMode = (*lpMode & PIPE_READMODE_MESSAGE) ?
1821 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
1822 status = NtSetInformationFile(hNamedPipe, &iosb, &fpi, sizeof(fpi), FilePipeInformation);
1825 if (status)
1827 SetLastError( RtlNtStatusToDosError(status) );
1828 return FALSE;
1832 return TRUE;
1835 /***********************************************************************
1836 * CallNamedPipeA (KERNEL32.@)
1838 BOOL WINAPI CallNamedPipeA(
1839 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1840 LPVOID lpOutput, DWORD dwOutputSize,
1841 LPDWORD lpBytesRead, DWORD nTimeout)
1843 DWORD len;
1844 LPWSTR str = NULL;
1845 BOOL ret;
1847 TRACE("%s %p %d %p %d %p %d\n",
1848 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1849 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1851 if( lpNamedPipeName )
1853 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1854 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1855 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1857 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1858 dwOutputSize, lpBytesRead, nTimeout );
1859 if( lpNamedPipeName )
1860 HeapFree( GetProcessHeap(), 0, str );
1862 return ret;
1865 /***********************************************************************
1866 * CallNamedPipeW (KERNEL32.@)
1868 BOOL WINAPI CallNamedPipeW(
1869 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1870 LPVOID lpOutput, DWORD lpOutputSize,
1871 LPDWORD lpBytesRead, DWORD nTimeout)
1873 HANDLE pipe;
1874 BOOL ret;
1875 DWORD mode;
1877 TRACE("%s %p %d %p %d %p %d\n",
1878 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1879 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1881 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1882 if (pipe == INVALID_HANDLE_VALUE)
1884 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1885 if (!ret)
1886 return FALSE;
1887 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1888 if (pipe == INVALID_HANDLE_VALUE)
1889 return FALSE;
1892 mode = PIPE_READMODE_MESSAGE;
1893 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1894 if (!ret)
1896 CloseHandle(pipe);
1897 return FALSE;
1900 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1901 CloseHandle(pipe);
1902 if (!ret)
1903 return FALSE;
1905 return TRUE;
1908 /******************************************************************
1909 * CreatePipe (KERNEL32.@)
1912 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1913 LPSECURITY_ATTRIBUTES sa, DWORD size )
1915 static unsigned index /* = 0 */;
1916 WCHAR name[64];
1917 HANDLE hr, hw;
1918 unsigned in_index = index;
1919 UNICODE_STRING nt_name;
1920 OBJECT_ATTRIBUTES attr;
1921 NTSTATUS status;
1922 IO_STATUS_BLOCK iosb;
1923 LARGE_INTEGER timeout;
1925 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1927 attr.Length = sizeof(attr);
1928 attr.RootDirectory = 0;
1929 attr.ObjectName = &nt_name;
1930 attr.Attributes = OBJ_CASE_INSENSITIVE |
1931 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1932 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1933 attr.SecurityQualityOfService = NULL;
1935 if (!size) size = 4096;
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 FILE_SHARE_WRITE, FILE_OVERWRITE_IF,
1950 FILE_SYNCHRONOUS_IO_NONALERT,
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_NONALERT | 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 if (iosb.u.Status >= 0) return TRUE;
2231 SetLastError( RtlNtStatusToDosError(iosb.u.Status) );
2232 return FALSE;
2235 if (status == STATUS_TIMEOUT) SetLastError( WAIT_TIMEOUT );
2236 else SetLastError( RtlNtStatusToDosError(status) );
2237 return FALSE;
2241 /******************************************************************************
2242 * PostQueuedCompletionStatus (KERNEL32.@)
2244 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
2245 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
2247 NTSTATUS status;
2249 TRACE("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
2251 status = NtSetIoCompletion( CompletionPort, dwCompletionKey, (ULONG_PTR)lpOverlapped,
2252 STATUS_SUCCESS, dwNumberOfBytes );
2254 if (status == STATUS_SUCCESS) return TRUE;
2255 SetLastError( RtlNtStatusToDosError(status) );
2256 return FALSE;
2259 /******************************************************************************
2260 * BindIoCompletionCallback (KERNEL32.@)
2262 BOOL WINAPI BindIoCompletionCallback( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags)
2264 NTSTATUS status;
2266 TRACE("(%p, %p, %d)\n", FileHandle, Function, Flags);
2268 status = RtlSetIoCompletionCallback( FileHandle, (PRTL_OVERLAPPED_COMPLETION_ROUTINE)Function, Flags );
2269 if (status == STATUS_SUCCESS) return TRUE;
2270 SetLastError( RtlNtStatusToDosError(status) );
2271 return FALSE;
2275 /***********************************************************************
2276 * CreateMemoryResourceNotification (KERNEL32.@)
2278 HANDLE WINAPI CreateMemoryResourceNotification(MEMORY_RESOURCE_NOTIFICATION_TYPE type)
2280 static const WCHAR lowmemW[] =
2281 {'\\','K','e','r','n','e','l','O','b','j','e','c','t','s',
2282 '\\','L','o','w','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n',0};
2283 static const WCHAR highmemW[] =
2284 {'\\','K','e','r','n','e','l','O','b','j','e','c','t','s',
2285 '\\','H','i','g','h','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n',0};
2286 HANDLE ret;
2287 UNICODE_STRING nameW;
2288 OBJECT_ATTRIBUTES attr;
2289 NTSTATUS status;
2291 switch (type)
2293 case LowMemoryResourceNotification:
2294 RtlInitUnicodeString( &nameW, lowmemW );
2295 break;
2296 case HighMemoryResourceNotification:
2297 RtlInitUnicodeString( &nameW, highmemW );
2298 break;
2299 default:
2300 SetLastError( ERROR_INVALID_PARAMETER );
2301 return 0;
2304 attr.Length = sizeof(attr);
2305 attr.RootDirectory = 0;
2306 attr.ObjectName = &nameW;
2307 attr.Attributes = 0;
2308 attr.SecurityDescriptor = NULL;
2309 attr.SecurityQualityOfService = NULL;
2310 status = NtOpenEvent( &ret, EVENT_ALL_ACCESS, &attr );
2311 if (status != STATUS_SUCCESS)
2313 SetLastError( RtlNtStatusToDosError(status) );
2314 return 0;
2316 return ret;
2319 /***********************************************************************
2320 * QueryMemoryResourceNotification (KERNEL32.@)
2322 BOOL WINAPI QueryMemoryResourceNotification(HANDLE handle, PBOOL state)
2324 switch (WaitForSingleObject( handle, 0 ))
2326 case WAIT_OBJECT_0:
2327 *state = TRUE;
2328 return TRUE;
2329 case WAIT_TIMEOUT:
2330 *state = FALSE;
2331 return TRUE;
2333 SetLastError( ERROR_INVALID_PARAMETER );
2334 return FALSE;
2337 /***********************************************************************
2338 * InitOnceBeginInitialize (KERNEL32.@)
2340 BOOL WINAPI InitOnceBeginInitialize( INIT_ONCE *once, DWORD flags, BOOL *pending, void **context )
2342 NTSTATUS status = RtlRunOnceBeginInitialize( once, flags, context );
2343 if (status >= 0) *pending = (status == STATUS_PENDING);
2344 else SetLastError( RtlNtStatusToDosError(status) );
2345 return status >= 0;
2348 /***********************************************************************
2349 * InitOnceComplete (KERNEL32.@)
2351 BOOL WINAPI InitOnceComplete( INIT_ONCE *once, DWORD flags, void *context )
2353 NTSTATUS status = RtlRunOnceComplete( once, flags, context );
2354 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
2355 return !status;
2358 /***********************************************************************
2359 * InitOnceExecuteOnce (KERNEL32.@)
2361 BOOL WINAPI InitOnceExecuteOnce( INIT_ONCE *once, PINIT_ONCE_FN func, void *param, void **context )
2363 return !RtlRunOnceExecuteOnce( once, (PRTL_RUN_ONCE_INIT_FN)func, param, context );
2366 #ifdef __i386__
2368 /***********************************************************************
2369 * InterlockedCompareExchange (KERNEL32.@)
2371 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
2372 __ASM_STDCALL_FUNC(InterlockedCompareExchange, 12,
2373 "movl 12(%esp),%eax\n\t"
2374 "movl 8(%esp),%ecx\n\t"
2375 "movl 4(%esp),%edx\n\t"
2376 "lock; cmpxchgl %ecx,(%edx)\n\t"
2377 "ret $12")
2379 /***********************************************************************
2380 * InterlockedExchange (KERNEL32.@)
2382 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
2383 __ASM_STDCALL_FUNC(InterlockedExchange, 8,
2384 "movl 8(%esp),%eax\n\t"
2385 "movl 4(%esp),%edx\n\t"
2386 "lock; xchgl %eax,(%edx)\n\t"
2387 "ret $8")
2389 /***********************************************************************
2390 * InterlockedExchangeAdd (KERNEL32.@)
2392 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
2393 __ASM_STDCALL_FUNC(InterlockedExchangeAdd, 8,
2394 "movl 8(%esp),%eax\n\t"
2395 "movl 4(%esp),%edx\n\t"
2396 "lock; xaddl %eax,(%edx)\n\t"
2397 "ret $8")
2399 /***********************************************************************
2400 * InterlockedIncrement (KERNEL32.@)
2402 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2403 __ASM_STDCALL_FUNC(InterlockedIncrement, 4,
2404 "movl 4(%esp),%edx\n\t"
2405 "movl $1,%eax\n\t"
2406 "lock; xaddl %eax,(%edx)\n\t"
2407 "incl %eax\n\t"
2408 "ret $4")
2410 /***********************************************************************
2411 * InterlockedDecrement (KERNEL32.@)
2413 __ASM_STDCALL_FUNC(InterlockedDecrement, 4,
2414 "movl 4(%esp),%edx\n\t"
2415 "movl $-1,%eax\n\t"
2416 "lock; xaddl %eax,(%edx)\n\t"
2417 "decl %eax\n\t"
2418 "ret $4")
2420 #endif /* __i386__ */
2422 /***********************************************************************
2423 * SleepConditionVariableCS (KERNEL32.@)
2425 BOOL WINAPI SleepConditionVariableCS( CONDITION_VARIABLE *variable, CRITICAL_SECTION *crit, DWORD timeout )
2427 NTSTATUS status;
2428 LARGE_INTEGER time;
2430 status = RtlSleepConditionVariableCS( variable, crit, get_nt_timeout( &time, timeout ) );
2432 if (status != STATUS_SUCCESS)
2434 SetLastError( RtlNtStatusToDosError(status) );
2435 return FALSE;
2437 return TRUE;
2440 /***********************************************************************
2441 * SleepConditionVariableSRW (KERNEL32.@)
2443 BOOL WINAPI SleepConditionVariableSRW( RTL_CONDITION_VARIABLE *variable, RTL_SRWLOCK *lock, DWORD timeout, ULONG flags )
2445 NTSTATUS status;
2446 LARGE_INTEGER time;
2448 status = RtlSleepConditionVariableSRW( variable, lock, get_nt_timeout( &time, timeout ), flags );
2450 if (status != STATUS_SUCCESS)
2452 SetLastError( RtlNtStatusToDosError(status) );
2453 return FALSE;
2455 return TRUE;