wined3d: Pass a texture and sub-resource index to wined3d_volume_download_data().
[wine.git] / dlls / kernel32 / sync.c
blobc10fd01d63763f619244c18da5b058b3481b2d43
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 );
152 /***********************************************************************
153 * WaitForMultipleObjectsEx (KERNEL32.@)
155 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
156 BOOL wait_all, DWORD timeout,
157 BOOL alertable )
159 NTSTATUS status;
160 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
161 LARGE_INTEGER time;
162 unsigned int i;
164 if (count > MAXIMUM_WAIT_OBJECTS)
166 SetLastError(ERROR_INVALID_PARAMETER);
167 return WAIT_FAILED;
169 for (i = 0; i < count; i++)
171 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
172 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
173 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
174 hloc[i] = GetStdHandle( HandleToULong(handles[i]) );
175 else
176 hloc[i] = handles[i];
178 /* yes, even screen buffer console handles are waitable, and are
179 * handled as a handle to the console itself !!
181 if (is_console_handle(hloc[i]))
183 if (VerifyConsoleIoHandle(hloc[i]))
184 hloc[i] = GetConsoleInputWaitHandle();
188 status = NtWaitForMultipleObjects( count, hloc, !wait_all, alertable,
189 get_nt_timeout( &time, timeout ) );
191 if (HIWORD(status)) /* is it an error code? */
193 SetLastError( RtlNtStatusToDosError(status) );
194 status = WAIT_FAILED;
196 return status;
200 /***********************************************************************
201 * RegisterWaitForSingleObject (KERNEL32.@)
203 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
204 WAITORTIMERCALLBACK Callback, PVOID Context,
205 ULONG dwMilliseconds, ULONG dwFlags)
207 NTSTATUS status;
209 TRACE("%p %p %p %p %d %d\n",
210 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
212 status = RtlRegisterWait( phNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
213 if (status != STATUS_SUCCESS)
215 SetLastError( RtlNtStatusToDosError(status) );
216 return FALSE;
218 return TRUE;
221 /***********************************************************************
222 * RegisterWaitForSingleObjectEx (KERNEL32.@)
224 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
225 WAITORTIMERCALLBACK Callback, PVOID Context,
226 ULONG dwMilliseconds, ULONG dwFlags )
228 NTSTATUS status;
229 HANDLE hNewWaitObject;
231 TRACE("%p %p %p %d %d\n",
232 hObject,Callback,Context,dwMilliseconds,dwFlags);
234 status = RtlRegisterWait( &hNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
235 if (status != STATUS_SUCCESS)
237 SetLastError( RtlNtStatusToDosError(status) );
238 return NULL;
240 return hNewWaitObject;
243 /***********************************************************************
244 * UnregisterWait (KERNEL32.@)
246 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
248 NTSTATUS status;
250 TRACE("%p\n",WaitHandle);
252 status = RtlDeregisterWait( WaitHandle );
253 if (status != STATUS_SUCCESS)
255 SetLastError( RtlNtStatusToDosError(status) );
256 return FALSE;
258 return TRUE;
261 /***********************************************************************
262 * UnregisterWaitEx (KERNEL32.@)
264 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
266 NTSTATUS status;
268 TRACE("%p %p\n",WaitHandle, CompletionEvent);
270 status = RtlDeregisterWaitEx( WaitHandle, CompletionEvent );
271 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
272 return !status;
275 /***********************************************************************
276 * SignalObjectAndWait (KERNEL32.@)
278 * Makes it possible to atomically signal any of the synchronization
279 * objects (semaphore, mutex, event) and wait on another.
281 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
282 DWORD dwMilliseconds, BOOL bAlertable )
284 NTSTATUS status;
285 LARGE_INTEGER timeout;
287 TRACE("%p %p %d %d\n", hObjectToSignal,
288 hObjectToWaitOn, dwMilliseconds, bAlertable);
290 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn, bAlertable,
291 get_nt_timeout( &timeout, dwMilliseconds ) );
292 if (HIWORD(status))
294 SetLastError( RtlNtStatusToDosError(status) );
295 status = WAIT_FAILED;
297 return status;
300 /***********************************************************************
301 * InitializeCriticalSection (KERNEL32.@)
303 * Initialise a critical section before use.
305 * PARAMS
306 * crit [O] Critical section to initialise.
308 * RETURNS
309 * Nothing. If the function fails an exception is raised.
311 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
313 InitializeCriticalSectionEx( crit, 0, 0 );
316 /***********************************************************************
317 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
319 * Initialise a critical section with a spin count.
321 * PARAMS
322 * crit [O] Critical section to initialise.
323 * spincount [I] Number of times to spin upon contention.
325 * RETURNS
326 * Success: TRUE.
327 * Failure: Nothing. If the function fails an exception is raised.
329 * NOTES
330 * spincount is ignored on uni-processor systems.
332 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
334 return InitializeCriticalSectionEx( crit, spincount, 0 );
337 /***********************************************************************
338 * InitializeCriticalSectionEx (KERNEL32.@)
340 * Initialise a critical section with a spin count and flags.
342 * PARAMS
343 * crit [O] Critical section to initialise.
344 * spincount [I] Number of times to spin upon contention.
345 * flags [I] CRITICAL_SECTION_ flags from winbase.h.
347 * RETURNS
348 * Success: TRUE.
349 * Failure: Nothing. If the function fails an exception is raised.
351 * NOTES
352 * spincount is ignored on uni-processor systems.
354 BOOL WINAPI InitializeCriticalSectionEx( CRITICAL_SECTION *crit, DWORD spincount, DWORD flags )
356 NTSTATUS ret = RtlInitializeCriticalSectionEx( crit, spincount, flags );
357 if (ret) RtlRaiseStatus( ret );
358 return !ret;
361 /***********************************************************************
362 * MakeCriticalSectionGlobal (KERNEL32.@)
364 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
366 /* let's assume that only one thread at a time will try to do this */
367 HANDLE sem = crit->LockSemaphore;
368 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
369 crit->LockSemaphore = ConvertToGlobalHandle( sem );
370 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
371 crit->DebugInfo = NULL;
375 /***********************************************************************
376 * ReinitializeCriticalSection (KERNEL32.@)
378 * Initialise an already used critical section.
380 * PARAMS
381 * crit [O] Critical section to initialise.
383 * RETURNS
384 * Nothing.
386 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
388 if ( !crit->LockSemaphore )
389 RtlInitializeCriticalSection( crit );
393 /***********************************************************************
394 * UninitializeCriticalSection (KERNEL32.@)
396 * UnInitialise a critical section after use.
398 * PARAMS
399 * crit [O] Critical section to uninitialise (destroy).
401 * RETURNS
402 * Nothing.
404 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
406 RtlDeleteCriticalSection( crit );
410 /***********************************************************************
411 * CreateEventA (KERNEL32.@)
413 HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
414 BOOL initial_state, LPCSTR name )
416 DWORD flags = 0;
418 if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET;
419 if (initial_state) flags |= CREATE_EVENT_INITIAL_SET;
420 return CreateEventExA( sa, name, flags, EVENT_ALL_ACCESS );
424 /***********************************************************************
425 * CreateEventW (KERNEL32.@)
427 HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
428 BOOL initial_state, LPCWSTR name )
430 DWORD flags = 0;
432 if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET;
433 if (initial_state) flags |= CREATE_EVENT_INITIAL_SET;
434 return CreateEventExW( sa, name, flags, EVENT_ALL_ACCESS );
438 /***********************************************************************
439 * CreateEventExA (KERNEL32.@)
441 HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
443 WCHAR buffer[MAX_PATH];
445 if (!name) return CreateEventExW( sa, NULL, flags, access );
447 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
449 SetLastError( ERROR_FILENAME_EXCED_RANGE );
450 return 0;
452 return CreateEventExW( sa, buffer, flags, access );
456 /***********************************************************************
457 * CreateEventExW (KERNEL32.@)
459 HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
461 HANDLE ret = 0;
462 UNICODE_STRING nameW;
463 OBJECT_ATTRIBUTES attr;
464 NTSTATUS status;
466 /* one buggy program needs this
467 * ("Van Dale Groot woordenboek der Nederlandse taal")
469 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
471 ERR("Bad security attributes pointer %p\n",sa);
472 SetLastError( ERROR_INVALID_PARAMETER);
473 return 0;
476 attr.Length = sizeof(attr);
477 attr.RootDirectory = 0;
478 attr.ObjectName = NULL;
479 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
480 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
481 attr.SecurityQualityOfService = NULL;
482 if (name)
484 RtlInitUnicodeString( &nameW, name );
485 attr.ObjectName = &nameW;
486 attr.RootDirectory = get_BaseNamedObjects_handle();
489 status = NtCreateEvent( &ret, access, &attr,
490 (flags & CREATE_EVENT_MANUAL_RESET) ? NotificationEvent : SynchronizationEvent,
491 (flags & CREATE_EVENT_INITIAL_SET) != 0 );
492 if (status == STATUS_OBJECT_NAME_EXISTS)
493 SetLastError( ERROR_ALREADY_EXISTS );
494 else
495 SetLastError( RtlNtStatusToDosError(status) );
496 return ret;
500 /***********************************************************************
501 * OpenEventA (KERNEL32.@)
503 HANDLE WINAPI DECLSPEC_HOTPATCH OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
505 WCHAR buffer[MAX_PATH];
507 if (!name) return OpenEventW( access, inherit, NULL );
509 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
511 SetLastError( ERROR_FILENAME_EXCED_RANGE );
512 return 0;
514 return OpenEventW( access, inherit, buffer );
518 /***********************************************************************
519 * OpenEventW (KERNEL32.@)
521 HANDLE WINAPI DECLSPEC_HOTPATCH OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
523 HANDLE ret;
524 UNICODE_STRING nameW;
525 OBJECT_ATTRIBUTES attr;
526 NTSTATUS status;
528 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
530 attr.Length = sizeof(attr);
531 attr.RootDirectory = 0;
532 attr.ObjectName = NULL;
533 attr.Attributes = inherit ? OBJ_INHERIT : 0;
534 attr.SecurityDescriptor = NULL;
535 attr.SecurityQualityOfService = NULL;
536 if (name)
538 RtlInitUnicodeString( &nameW, name );
539 attr.ObjectName = &nameW;
540 attr.RootDirectory = get_BaseNamedObjects_handle();
543 status = NtOpenEvent( &ret, access, &attr );
544 if (status != STATUS_SUCCESS)
546 SetLastError( RtlNtStatusToDosError(status) );
547 return 0;
549 return ret;
552 /***********************************************************************
553 * PulseEvent (KERNEL32.@)
555 BOOL WINAPI DECLSPEC_HOTPATCH PulseEvent( HANDLE handle )
557 NTSTATUS status;
559 if ((status = NtPulseEvent( handle, NULL )))
560 SetLastError( RtlNtStatusToDosError(status) );
561 return !status;
565 /***********************************************************************
566 * SetEvent (KERNEL32.@)
568 BOOL WINAPI DECLSPEC_HOTPATCH SetEvent( HANDLE handle )
570 NTSTATUS status;
572 if ((status = NtSetEvent( handle, NULL )))
573 SetLastError( RtlNtStatusToDosError(status) );
574 return !status;
578 /***********************************************************************
579 * ResetEvent (KERNEL32.@)
581 BOOL WINAPI DECLSPEC_HOTPATCH ResetEvent( HANDLE handle )
583 NTSTATUS status;
585 if ((status = NtResetEvent( handle, NULL )))
586 SetLastError( RtlNtStatusToDosError(status) );
587 return !status;
591 /***********************************************************************
592 * CreateMutexA (KERNEL32.@)
594 HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
596 return CreateMutexExA( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS );
600 /***********************************************************************
601 * CreateMutexW (KERNEL32.@)
603 HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
605 return CreateMutexExW( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS );
609 /***********************************************************************
610 * CreateMutexExA (KERNEL32.@)
612 HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
614 ANSI_STRING nameA;
615 NTSTATUS status;
617 if (!name) return CreateMutexExW( sa, NULL, flags, access );
619 RtlInitAnsiString( &nameA, name );
620 status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString, &nameA, FALSE );
621 if (status != STATUS_SUCCESS)
623 SetLastError( ERROR_FILENAME_EXCED_RANGE );
624 return 0;
626 return CreateMutexExW( sa, NtCurrentTeb()->StaticUnicodeString.Buffer, flags, access );
630 /***********************************************************************
631 * CreateMutexExW (KERNEL32.@)
633 HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
635 HANDLE ret = 0;
636 UNICODE_STRING nameW;
637 OBJECT_ATTRIBUTES attr;
638 NTSTATUS status;
640 attr.Length = sizeof(attr);
641 attr.RootDirectory = 0;
642 attr.ObjectName = NULL;
643 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
644 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
645 attr.SecurityQualityOfService = NULL;
646 if (name)
648 RtlInitUnicodeString( &nameW, name );
649 attr.ObjectName = &nameW;
650 attr.RootDirectory = get_BaseNamedObjects_handle();
653 status = NtCreateMutant( &ret, access, &attr, (flags & CREATE_MUTEX_INITIAL_OWNER) != 0 );
654 if (status == STATUS_OBJECT_NAME_EXISTS)
655 SetLastError( ERROR_ALREADY_EXISTS );
656 else
657 SetLastError( RtlNtStatusToDosError(status) );
658 return ret;
662 /***********************************************************************
663 * OpenMutexA (KERNEL32.@)
665 HANDLE WINAPI DECLSPEC_HOTPATCH OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
667 WCHAR buffer[MAX_PATH];
669 if (!name) return OpenMutexW( access, inherit, NULL );
671 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
673 SetLastError( ERROR_FILENAME_EXCED_RANGE );
674 return 0;
676 return OpenMutexW( access, inherit, buffer );
680 /***********************************************************************
681 * OpenMutexW (KERNEL32.@)
683 HANDLE WINAPI DECLSPEC_HOTPATCH OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
685 HANDLE ret;
686 UNICODE_STRING nameW;
687 OBJECT_ATTRIBUTES attr;
688 NTSTATUS status;
690 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
692 attr.Length = sizeof(attr);
693 attr.RootDirectory = 0;
694 attr.ObjectName = NULL;
695 attr.Attributes = inherit ? OBJ_INHERIT : 0;
696 attr.SecurityDescriptor = NULL;
697 attr.SecurityQualityOfService = NULL;
698 if (name)
700 RtlInitUnicodeString( &nameW, name );
701 attr.ObjectName = &nameW;
702 attr.RootDirectory = get_BaseNamedObjects_handle();
705 status = NtOpenMutant( &ret, access, &attr );
706 if (status != STATUS_SUCCESS)
708 SetLastError( RtlNtStatusToDosError(status) );
709 return 0;
711 return ret;
715 /***********************************************************************
716 * ReleaseMutex (KERNEL32.@)
718 BOOL WINAPI DECLSPEC_HOTPATCH ReleaseMutex( HANDLE handle )
720 NTSTATUS status;
722 status = NtReleaseMutant(handle, NULL);
723 if (status != STATUS_SUCCESS)
725 SetLastError( RtlNtStatusToDosError(status) );
726 return FALSE;
728 return TRUE;
733 * Semaphores
737 /***********************************************************************
738 * CreateSemaphoreA (KERNEL32.@)
740 HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
742 return CreateSemaphoreExA( sa, initial, max, name, 0, SEMAPHORE_ALL_ACCESS );
746 /***********************************************************************
747 * CreateSemaphoreW (KERNEL32.@)
749 HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCWSTR name )
751 return CreateSemaphoreExW( sa, initial, max, name, 0, SEMAPHORE_ALL_ACCESS );
755 /***********************************************************************
756 * CreateSemaphoreExA (KERNEL32.@)
758 HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreExA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max,
759 LPCSTR name, DWORD flags, DWORD access )
761 WCHAR buffer[MAX_PATH];
763 if (!name) return CreateSemaphoreExW( sa, initial, max, NULL, flags, access );
765 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
767 SetLastError( ERROR_FILENAME_EXCED_RANGE );
768 return 0;
770 return CreateSemaphoreExW( sa, initial, max, buffer, flags, access );
774 /***********************************************************************
775 * CreateSemaphoreExW (KERNEL32.@)
777 HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreExW( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max,
778 LPCWSTR name, DWORD flags, DWORD access )
780 HANDLE ret = 0;
781 UNICODE_STRING nameW;
782 OBJECT_ATTRIBUTES attr;
783 NTSTATUS status;
785 attr.Length = sizeof(attr);
786 attr.RootDirectory = 0;
787 attr.ObjectName = NULL;
788 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
789 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
790 attr.SecurityQualityOfService = NULL;
791 if (name)
793 RtlInitUnicodeString( &nameW, name );
794 attr.ObjectName = &nameW;
795 attr.RootDirectory = get_BaseNamedObjects_handle();
798 status = NtCreateSemaphore( &ret, access, &attr, initial, max );
799 if (status == STATUS_OBJECT_NAME_EXISTS)
800 SetLastError( ERROR_ALREADY_EXISTS );
801 else
802 SetLastError( RtlNtStatusToDosError(status) );
803 return ret;
807 /***********************************************************************
808 * OpenSemaphoreA (KERNEL32.@)
810 HANDLE WINAPI DECLSPEC_HOTPATCH OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
812 WCHAR buffer[MAX_PATH];
814 if (!name) return OpenSemaphoreW( access, inherit, NULL );
816 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
818 SetLastError( ERROR_FILENAME_EXCED_RANGE );
819 return 0;
821 return OpenSemaphoreW( access, inherit, buffer );
825 /***********************************************************************
826 * OpenSemaphoreW (KERNEL32.@)
828 HANDLE WINAPI DECLSPEC_HOTPATCH OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
830 HANDLE ret;
831 UNICODE_STRING nameW;
832 OBJECT_ATTRIBUTES attr;
833 NTSTATUS status;
835 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
837 attr.Length = sizeof(attr);
838 attr.RootDirectory = 0;
839 attr.ObjectName = NULL;
840 attr.Attributes = inherit ? OBJ_INHERIT : 0;
841 attr.SecurityDescriptor = NULL;
842 attr.SecurityQualityOfService = NULL;
843 if (name)
845 RtlInitUnicodeString( &nameW, name );
846 attr.ObjectName = &nameW;
847 attr.RootDirectory = get_BaseNamedObjects_handle();
850 status = NtOpenSemaphore( &ret, access, &attr );
851 if (status != STATUS_SUCCESS)
853 SetLastError( RtlNtStatusToDosError(status) );
854 return 0;
856 return ret;
860 /***********************************************************************
861 * ReleaseSemaphore (KERNEL32.@)
863 BOOL WINAPI DECLSPEC_HOTPATCH ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
865 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
866 if (status) SetLastError( RtlNtStatusToDosError(status) );
867 return !status;
872 * Jobs
875 /******************************************************************************
876 * CreateJobObjectW (KERNEL32.@)
878 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES sa, LPCWSTR name )
880 HANDLE ret = 0;
881 UNICODE_STRING nameW;
882 OBJECT_ATTRIBUTES attr;
883 NTSTATUS status;
885 attr.Length = sizeof(attr);
886 attr.RootDirectory = 0;
887 attr.ObjectName = NULL;
888 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
889 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
890 attr.SecurityQualityOfService = NULL;
891 if (name)
893 RtlInitUnicodeString( &nameW, name );
894 attr.ObjectName = &nameW;
895 attr.RootDirectory = get_BaseNamedObjects_handle();
898 status = NtCreateJobObject( &ret, JOB_OBJECT_ALL_ACCESS, &attr );
899 if (status == STATUS_OBJECT_NAME_EXISTS)
900 SetLastError( ERROR_ALREADY_EXISTS );
901 else
902 SetLastError( RtlNtStatusToDosError(status) );
903 return ret;
906 /******************************************************************************
907 * CreateJobObjectA (KERNEL32.@)
909 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
911 WCHAR buffer[MAX_PATH];
913 if (!name) return CreateJobObjectW( attr, NULL );
915 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
917 SetLastError( ERROR_FILENAME_EXCED_RANGE );
918 return 0;
920 return CreateJobObjectW( attr, buffer );
923 /******************************************************************************
924 * OpenJobObjectW (KERNEL32.@)
926 HANDLE WINAPI OpenJobObjectW( DWORD access, BOOL inherit, LPCWSTR name )
928 HANDLE ret;
929 UNICODE_STRING nameW;
930 OBJECT_ATTRIBUTES attr;
931 NTSTATUS status;
933 attr.Length = sizeof(attr);
934 attr.RootDirectory = 0;
935 attr.ObjectName = NULL;
936 attr.Attributes = inherit ? OBJ_INHERIT : 0;
937 attr.SecurityDescriptor = NULL;
938 attr.SecurityQualityOfService = NULL;
939 if (name)
941 RtlInitUnicodeString( &nameW, name );
942 attr.ObjectName = &nameW;
943 attr.RootDirectory = get_BaseNamedObjects_handle();
946 status = NtOpenJobObject( &ret, access, &attr );
947 if (status != STATUS_SUCCESS)
949 SetLastError( RtlNtStatusToDosError(status) );
950 return 0;
952 return ret;
955 /******************************************************************************
956 * OpenJobObjectA (KERNEL32.@)
958 HANDLE WINAPI OpenJobObjectA( DWORD access, BOOL inherit, LPCSTR name )
960 WCHAR buffer[MAX_PATH];
962 if (!name) return OpenJobObjectW( access, inherit, NULL );
964 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
966 SetLastError( ERROR_FILENAME_EXCED_RANGE );
967 return 0;
969 return OpenJobObjectW( access, inherit, buffer );
972 /******************************************************************************
973 * TerminateJobObject (KERNEL32.@)
975 BOOL WINAPI TerminateJobObject( HANDLE job, UINT exit_code )
977 NTSTATUS status = NtTerminateJobObject( job, exit_code );
978 if (status) SetLastError( RtlNtStatusToDosError(status) );
979 return !status;
982 /******************************************************************************
983 * QueryInformationJobObject (KERNEL32.@)
985 BOOL WINAPI QueryInformationJobObject( HANDLE job, JOBOBJECTINFOCLASS class, LPVOID info,
986 DWORD len, DWORD *ret_len )
988 NTSTATUS status = NtQueryInformationJobObject( job, class, info, len, ret_len );
989 if (status) SetLastError( RtlNtStatusToDosError(status) );
990 return !status;
993 /******************************************************************************
994 * SetInformationJobObject (KERNEL32.@)
996 BOOL WINAPI SetInformationJobObject( HANDLE job, JOBOBJECTINFOCLASS class, LPVOID info, DWORD len )
998 NTSTATUS status = NtSetInformationJobObject( job, class, info, len );
999 if (status) SetLastError( RtlNtStatusToDosError(status) );
1000 return !status;
1003 /******************************************************************************
1004 * AssignProcessToJobObject (KERNEL32.@)
1006 BOOL WINAPI AssignProcessToJobObject( HANDLE job, HANDLE process )
1008 NTSTATUS status = NtAssignProcessToJobObject( job, process );
1009 if (status) SetLastError( RtlNtStatusToDosError(status) );
1010 return !status;
1013 /******************************************************************************
1014 * IsProcessInJob (KERNEL32.@)
1016 BOOL WINAPI IsProcessInJob( HANDLE process, HANDLE job, PBOOL result )
1018 NTSTATUS status = NtIsProcessInJob( process, job );
1019 switch(status)
1021 case STATUS_PROCESS_IN_JOB:
1022 *result = TRUE;
1023 return TRUE;
1024 case STATUS_PROCESS_NOT_IN_JOB:
1025 *result = FALSE;
1026 return TRUE;
1027 default:
1028 SetLastError( RtlNtStatusToDosError(status) );
1029 return FALSE;
1035 * Timers
1039 /***********************************************************************
1040 * CreateWaitableTimerA (KERNEL32.@)
1042 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
1044 return CreateWaitableTimerExA( sa, name, manual ? CREATE_WAITABLE_TIMER_MANUAL_RESET : 0,
1045 TIMER_ALL_ACCESS );
1049 /***********************************************************************
1050 * CreateWaitableTimerW (KERNEL32.@)
1052 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
1054 return CreateWaitableTimerExW( sa, name, manual ? CREATE_WAITABLE_TIMER_MANUAL_RESET : 0,
1055 TIMER_ALL_ACCESS );
1059 /***********************************************************************
1060 * CreateWaitableTimerExA (KERNEL32.@)
1062 HANDLE WINAPI CreateWaitableTimerExA( SECURITY_ATTRIBUTES *sa, LPCSTR name, DWORD flags, DWORD access )
1064 WCHAR buffer[MAX_PATH];
1066 if (!name) return CreateWaitableTimerExW( sa, NULL, flags, access );
1068 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1070 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1071 return 0;
1073 return CreateWaitableTimerExW( sa, buffer, flags, access );
1077 /***********************************************************************
1078 * CreateWaitableTimerExW (KERNEL32.@)
1080 HANDLE WINAPI CreateWaitableTimerExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name, DWORD flags, DWORD access )
1082 HANDLE handle;
1083 NTSTATUS status;
1084 UNICODE_STRING nameW;
1085 OBJECT_ATTRIBUTES attr;
1087 attr.Length = sizeof(attr);
1088 attr.RootDirectory = 0;
1089 attr.ObjectName = NULL;
1090 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1091 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1092 attr.SecurityQualityOfService = NULL;
1093 if (name)
1095 RtlInitUnicodeString( &nameW, name );
1096 attr.ObjectName = &nameW;
1097 attr.RootDirectory = get_BaseNamedObjects_handle();
1100 status = NtCreateTimer( &handle, access, &attr,
1101 (flags & CREATE_WAITABLE_TIMER_MANUAL_RESET) ? NotificationTimer : SynchronizationTimer );
1102 if (status == STATUS_OBJECT_NAME_EXISTS)
1103 SetLastError( ERROR_ALREADY_EXISTS );
1104 else
1105 SetLastError( RtlNtStatusToDosError(status) );
1106 return handle;
1110 /***********************************************************************
1111 * OpenWaitableTimerA (KERNEL32.@)
1113 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
1115 WCHAR buffer[MAX_PATH];
1117 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
1119 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1121 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1122 return 0;
1124 return OpenWaitableTimerW( access, inherit, buffer );
1128 /***********************************************************************
1129 * OpenWaitableTimerW (KERNEL32.@)
1131 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
1133 HANDLE handle;
1134 UNICODE_STRING nameW;
1135 OBJECT_ATTRIBUTES attr;
1136 NTSTATUS status;
1138 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
1140 attr.Length = sizeof(attr);
1141 attr.RootDirectory = 0;
1142 attr.ObjectName = NULL;
1143 attr.Attributes = inherit ? OBJ_INHERIT : 0;
1144 attr.SecurityDescriptor = NULL;
1145 attr.SecurityQualityOfService = NULL;
1146 if (name)
1148 RtlInitUnicodeString( &nameW, name );
1149 attr.ObjectName = &nameW;
1150 attr.RootDirectory = get_BaseNamedObjects_handle();
1153 status = NtOpenTimer(&handle, access, &attr);
1154 if (status != STATUS_SUCCESS)
1156 SetLastError( RtlNtStatusToDosError(status) );
1157 return 0;
1159 return handle;
1163 /***********************************************************************
1164 * SetWaitableTimer (KERNEL32.@)
1166 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
1167 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
1169 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
1170 arg, resume, period, NULL);
1172 if (status != STATUS_SUCCESS)
1174 SetLastError( RtlNtStatusToDosError(status) );
1175 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
1177 return TRUE;
1180 /***********************************************************************
1181 * SetWaitableTimerEx (KERNEL32.@)
1183 BOOL WINAPI SetWaitableTimerEx( HANDLE handle, const LARGE_INTEGER *when, LONG period,
1184 PTIMERAPCROUTINE callback, LPVOID arg, REASON_CONTEXT *context, ULONG tolerabledelay )
1186 static int once;
1187 if (!once++)
1189 FIXME("(%p, %p, %d, %p, %p, %p, %d) semi-stub\n",
1190 handle, when, period, callback, arg, context, tolerabledelay);
1192 return SetWaitableTimer(handle, when, period, callback, arg, FALSE);
1195 /***********************************************************************
1196 * CancelWaitableTimer (KERNEL32.@)
1198 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1200 NTSTATUS status;
1202 status = NtCancelTimer(handle, NULL);
1203 if (status != STATUS_SUCCESS)
1205 SetLastError( RtlNtStatusToDosError(status) );
1206 return FALSE;
1208 return TRUE;
1212 /***********************************************************************
1213 * CreateTimerQueue (KERNEL32.@)
1215 HANDLE WINAPI CreateTimerQueue(void)
1217 HANDLE q;
1218 NTSTATUS status = RtlCreateTimerQueue(&q);
1220 if (status != STATUS_SUCCESS)
1222 SetLastError( RtlNtStatusToDosError(status) );
1223 return NULL;
1226 return q;
1230 /***********************************************************************
1231 * DeleteTimerQueueEx (KERNEL32.@)
1233 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1235 NTSTATUS status = RtlDeleteTimerQueueEx(TimerQueue, CompletionEvent);
1237 if (status != STATUS_SUCCESS)
1239 SetLastError( RtlNtStatusToDosError(status) );
1240 return FALSE;
1243 return TRUE;
1246 /***********************************************************************
1247 * DeleteTimerQueue (KERNEL32.@)
1249 BOOL WINAPI DeleteTimerQueue(HANDLE TimerQueue)
1251 return DeleteTimerQueueEx(TimerQueue, NULL);
1254 /***********************************************************************
1255 * CreateTimerQueueTimer (KERNEL32.@)
1257 * Creates a timer-queue timer. This timer expires at the specified due
1258 * time (in ms), then after every specified period (in ms). When the timer
1259 * expires, the callback function is called.
1261 * RETURNS
1262 * nonzero on success or zero on failure
1264 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1265 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1266 DWORD DueTime, DWORD Period, ULONG Flags )
1268 NTSTATUS status = RtlCreateTimer(phNewTimer, TimerQueue, Callback,
1269 Parameter, DueTime, Period, Flags);
1271 if (status != STATUS_SUCCESS)
1273 SetLastError( RtlNtStatusToDosError(status) );
1274 return FALSE;
1277 return TRUE;
1280 /***********************************************************************
1281 * ChangeTimerQueueTimer (KERNEL32.@)
1283 * Changes the times at which the timer expires.
1285 * RETURNS
1286 * nonzero on success or zero on failure
1288 BOOL WINAPI ChangeTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1289 ULONG DueTime, ULONG Period )
1291 NTSTATUS status = RtlUpdateTimer(TimerQueue, Timer, DueTime, Period);
1293 if (status != STATUS_SUCCESS)
1295 SetLastError( RtlNtStatusToDosError(status) );
1296 return FALSE;
1299 return TRUE;
1302 /***********************************************************************
1303 * CancelTimerQueueTimer (KERNEL32.@)
1305 BOOL WINAPI CancelTimerQueueTimer(HANDLE queue, HANDLE timer)
1307 FIXME("stub: %p %p\n", queue, timer);
1308 return FALSE;
1311 /***********************************************************************
1312 * DeleteTimerQueueTimer (KERNEL32.@)
1314 * Cancels a timer-queue timer.
1316 * RETURNS
1317 * nonzero on success or zero on failure
1319 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1320 HANDLE CompletionEvent )
1322 NTSTATUS status = RtlDeleteTimer(TimerQueue, Timer, CompletionEvent);
1323 if (status != STATUS_SUCCESS)
1325 SetLastError( RtlNtStatusToDosError(status) );
1326 return FALSE;
1328 return TRUE;
1333 * Pipes
1337 /***********************************************************************
1338 * CreateNamedPipeA (KERNEL32.@)
1340 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1341 DWORD dwPipeMode, DWORD nMaxInstances,
1342 DWORD nOutBufferSize, DWORD nInBufferSize,
1343 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1345 WCHAR buffer[MAX_PATH];
1347 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1348 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1350 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1352 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1353 return INVALID_HANDLE_VALUE;
1355 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1356 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1360 /***********************************************************************
1361 * CreateNamedPipeW (KERNEL32.@)
1363 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1364 DWORD dwPipeMode, DWORD nMaxInstances,
1365 DWORD nOutBufferSize, DWORD nInBufferSize,
1366 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1368 HANDLE handle;
1369 UNICODE_STRING nt_name;
1370 OBJECT_ATTRIBUTES attr;
1371 DWORD access, options, sharing;
1372 BOOLEAN pipe_type, read_mode, non_block;
1373 NTSTATUS status;
1374 IO_STATUS_BLOCK iosb;
1375 LARGE_INTEGER timeout;
1377 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1378 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1379 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1381 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1383 SetLastError( ERROR_PATH_NOT_FOUND );
1384 return INVALID_HANDLE_VALUE;
1386 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1388 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1389 RtlFreeUnicodeString( &nt_name );
1390 return INVALID_HANDLE_VALUE;
1393 attr.Length = sizeof(attr);
1394 attr.RootDirectory = 0;
1395 attr.ObjectName = &nt_name;
1396 attr.Attributes = OBJ_CASE_INSENSITIVE |
1397 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1398 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1399 attr.SecurityQualityOfService = NULL;
1401 switch(dwOpenMode & 3)
1403 case PIPE_ACCESS_INBOUND:
1404 sharing = FILE_SHARE_WRITE;
1405 access = GENERIC_READ;
1406 break;
1407 case PIPE_ACCESS_OUTBOUND:
1408 sharing = FILE_SHARE_READ;
1409 access = GENERIC_WRITE;
1410 break;
1411 case PIPE_ACCESS_DUPLEX:
1412 sharing = FILE_SHARE_READ | FILE_SHARE_WRITE;
1413 access = GENERIC_READ | GENERIC_WRITE;
1414 break;
1415 default:
1416 SetLastError( ERROR_INVALID_PARAMETER );
1417 return INVALID_HANDLE_VALUE;
1419 access |= SYNCHRONIZE;
1420 options = 0;
1421 if (dwOpenMode & WRITE_DAC) access |= WRITE_DAC;
1422 if (dwOpenMode & WRITE_OWNER) access |= WRITE_OWNER;
1423 if (dwOpenMode & ACCESS_SYSTEM_SECURITY) access |= ACCESS_SYSTEM_SECURITY;
1424 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1425 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_NONALERT;
1426 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) != 0;
1427 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) != 0;
1428 non_block = (dwPipeMode & PIPE_NOWAIT) != 0;
1429 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0U;
1431 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1433 SetLastError(0);
1435 status = NtCreateNamedPipeFile(&handle, access, &attr, &iosb, sharing,
1436 FILE_OVERWRITE_IF, options, pipe_type,
1437 read_mode, non_block, nMaxInstances,
1438 nInBufferSize, nOutBufferSize, &timeout);
1440 RtlFreeUnicodeString( &nt_name );
1441 if (status)
1443 handle = INVALID_HANDLE_VALUE;
1444 SetLastError( RtlNtStatusToDosError(status) );
1446 return handle;
1450 /***********************************************************************
1451 * PeekNamedPipe (KERNEL32.@)
1453 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1454 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1456 FILE_PIPE_PEEK_BUFFER local_buffer;
1457 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1458 IO_STATUS_BLOCK io;
1459 NTSTATUS status;
1461 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1462 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1464 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1465 return FALSE;
1468 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1469 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1470 if (!status)
1472 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1473 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1474 if (lpcbRead) *lpcbRead = read_size;
1475 if (lpcbMessage) *lpcbMessage = 0; /* FIXME */
1476 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1478 else SetLastError( RtlNtStatusToDosError(status) );
1480 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1481 return !status;
1484 /***********************************************************************
1485 * WaitNamedPipeA (KERNEL32.@)
1487 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1489 WCHAR buffer[MAX_PATH];
1491 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1493 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1495 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1496 return FALSE;
1498 return WaitNamedPipeW( buffer, nTimeOut );
1502 /***********************************************************************
1503 * WaitNamedPipeW (KERNEL32.@)
1505 * Waits for a named pipe instance to become available
1507 * PARAMS
1508 * name [I] Pointer to a named pipe name to wait for
1509 * nTimeOut [I] How long to wait in ms
1511 * RETURNS
1512 * TRUE: Success, named pipe can be opened with CreateFile
1513 * FALSE: Failure, GetLastError can be called for further details
1515 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1517 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1518 NTSTATUS status;
1519 UNICODE_STRING nt_name, pipe_dev_name;
1520 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1521 IO_STATUS_BLOCK iosb;
1522 OBJECT_ATTRIBUTES attr;
1523 ULONG sz_pipe_wait;
1524 HANDLE pipe_dev;
1526 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1528 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1529 return FALSE;
1531 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1532 nt_name.Length < sizeof(leadin) ||
1533 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR)) != 0)
1535 RtlFreeUnicodeString( &nt_name );
1536 SetLastError( ERROR_PATH_NOT_FOUND );
1537 return FALSE;
1540 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1541 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1543 RtlFreeUnicodeString( &nt_name );
1544 SetLastError( ERROR_OUTOFMEMORY );
1545 return FALSE;
1548 pipe_dev_name.Buffer = nt_name.Buffer;
1549 pipe_dev_name.Length = sizeof(leadin);
1550 pipe_dev_name.MaximumLength = sizeof(leadin);
1551 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1552 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES | SYNCHRONIZE, &attr,
1553 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1554 FILE_SYNCHRONOUS_IO_NONALERT);
1555 if (status != ERROR_SUCCESS)
1557 HeapFree( GetProcessHeap(), 0, pipe_wait);
1558 RtlFreeUnicodeString( &nt_name );
1559 SetLastError( ERROR_PATH_NOT_FOUND );
1560 return FALSE;
1563 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1564 if (nTimeOut == NMPWAIT_WAIT_FOREVER)
1565 pipe_wait->Timeout.QuadPart = ((ULONGLONG)0x7fffffff << 32) | 0xffffffff;
1566 else
1567 pipe_wait->Timeout.QuadPart = (ULONGLONG)nTimeOut * -10000;
1568 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1569 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1570 pipe_wait->NameLength);
1571 RtlFreeUnicodeString( &nt_name );
1573 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1574 pipe_wait, sz_pipe_wait, NULL, 0 );
1576 HeapFree( GetProcessHeap(), 0, pipe_wait );
1577 NtClose( pipe_dev );
1579 if(status != STATUS_SUCCESS)
1581 SetLastError(RtlNtStatusToDosError(status));
1582 return FALSE;
1584 else
1585 return TRUE;
1589 /***********************************************************************
1590 * ConnectNamedPipe (KERNEL32.@)
1592 * Connects to a named pipe
1594 * Parameters
1595 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1596 * overlapped: Optional OVERLAPPED struct
1598 * Return values
1599 * TRUE: Success
1600 * FALSE: Failure, GetLastError can be called for further details
1602 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1604 NTSTATUS status;
1605 IO_STATUS_BLOCK status_block;
1606 LPVOID cvalue = NULL;
1608 TRACE("(%p,%p)\n", hPipe, overlapped);
1610 if(overlapped)
1612 overlapped->Internal = STATUS_PENDING;
1613 overlapped->InternalHigh = 0;
1614 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1617 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, cvalue,
1618 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1619 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1621 if (status == STATUS_SUCCESS) return TRUE;
1622 SetLastError( RtlNtStatusToDosError(status) );
1623 return FALSE;
1626 /***********************************************************************
1627 * DisconnectNamedPipe (KERNEL32.@)
1629 * Disconnects from a named pipe
1631 * Parameters
1632 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1634 * Return values
1635 * TRUE: Success
1636 * FALSE: Failure, GetLastError can be called for further details
1638 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1640 NTSTATUS status;
1641 IO_STATUS_BLOCK io_block;
1643 TRACE("(%p)\n",hPipe);
1645 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1646 NULL, 0, NULL, 0);
1647 if (status == STATUS_SUCCESS) return TRUE;
1648 SetLastError( RtlNtStatusToDosError(status) );
1649 return FALSE;
1652 /***********************************************************************
1653 * TransactNamedPipe (KERNEL32.@)
1655 * BUGS
1656 * should be done as a single operation in the wineserver or kernel
1658 BOOL WINAPI TransactNamedPipe(
1659 HANDLE handle, LPVOID write_buf, DWORD write_size, LPVOID read_buf,
1660 DWORD read_size, LPDWORD bytes_read, LPOVERLAPPED overlapped)
1662 BOOL r;
1663 DWORD count;
1665 TRACE("%p %p %d %p %d %p %p\n",
1666 handle, write_buf, write_size, read_buf,
1667 read_size, bytes_read, overlapped);
1669 if (overlapped)
1671 FIXME("Doesn't support overlapped operation as yet\n");
1672 return FALSE;
1675 r = WriteFile(handle, write_buf, write_size, &count, NULL);
1676 if (r)
1677 r = ReadFile(handle, read_buf, read_size, bytes_read, NULL);
1679 return r;
1682 /***********************************************************************
1683 * GetNamedPipeInfo (KERNEL32.@)
1685 BOOL WINAPI GetNamedPipeInfo(
1686 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1687 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1689 FILE_PIPE_LOCAL_INFORMATION fpli;
1690 IO_STATUS_BLOCK iosb;
1691 NTSTATUS status;
1693 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1694 FilePipeLocalInformation);
1695 if (status)
1697 SetLastError( RtlNtStatusToDosError(status) );
1698 return FALSE;
1701 if (lpFlags)
1703 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1704 PIPE_SERVER_END : PIPE_CLIENT_END;
1705 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1706 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1709 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1710 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1711 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1713 return TRUE;
1716 /***********************************************************************
1717 * GetNamedPipeHandleStateA (KERNEL32.@)
1719 BOOL WINAPI GetNamedPipeHandleStateA(
1720 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1721 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1722 LPSTR lpUsername, DWORD nUsernameMaxSize)
1724 WARN("%p %p %p %p %p %p %d: semi-stub\n",
1725 hNamedPipe, lpState, lpCurInstances,
1726 lpMaxCollectionCount, lpCollectDataTimeout,
1727 lpUsername, nUsernameMaxSize);
1729 if (lpUsername && nUsernameMaxSize)
1730 *lpUsername = 0;
1732 return GetNamedPipeHandleStateW(hNamedPipe, lpState, lpCurInstances,
1733 lpMaxCollectionCount, lpCollectDataTimeout, NULL, 0);
1736 /***********************************************************************
1737 * GetNamedPipeHandleStateW (KERNEL32.@)
1739 BOOL WINAPI GetNamedPipeHandleStateW(
1740 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1741 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1742 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1744 IO_STATUS_BLOCK iosb;
1745 NTSTATUS status;
1747 FIXME("%p %p %p %p %p %p %d: semi-stub\n",
1748 hNamedPipe, lpState, lpCurInstances,
1749 lpMaxCollectionCount, lpCollectDataTimeout,
1750 lpUsername, nUsernameMaxSize);
1752 if (lpMaxCollectionCount)
1753 *lpMaxCollectionCount = 0;
1755 if (lpCollectDataTimeout)
1756 *lpCollectDataTimeout = 0;
1758 if (lpUsername && nUsernameMaxSize)
1759 *lpUsername = 0;
1761 if (lpState)
1763 FILE_PIPE_INFORMATION fpi;
1764 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpi, sizeof(fpi),
1765 FilePipeInformation);
1766 if (status)
1768 SetLastError( RtlNtStatusToDosError(status) );
1769 return FALSE;
1772 *lpState = (fpi.ReadMode ? PIPE_READMODE_MESSAGE : PIPE_READMODE_BYTE) |
1773 (fpi.CompletionMode ? PIPE_NOWAIT : PIPE_WAIT);
1776 if (lpCurInstances)
1778 FILE_PIPE_LOCAL_INFORMATION fpli;
1779 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1780 FilePipeLocalInformation);
1781 if (status)
1783 SetLastError( RtlNtStatusToDosError(status) );
1784 return FALSE;
1787 *lpCurInstances = fpli.CurrentInstances;
1790 return TRUE;
1793 /***********************************************************************
1794 * SetNamedPipeHandleState (KERNEL32.@)
1796 BOOL WINAPI SetNamedPipeHandleState(
1797 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1798 LPDWORD lpCollectDataTimeout)
1800 /* should be a fixme, but this function is called a lot by the RPC
1801 * runtime, and it slows down InstallShield a fair bit. */
1802 WARN("semi-stub: %p %p/%d %p %p\n",
1803 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1805 if (lpMode)
1807 FILE_PIPE_INFORMATION fpi;
1808 IO_STATUS_BLOCK iosb;
1809 NTSTATUS status;
1811 if (*lpMode & ~(PIPE_READMODE_MESSAGE | PIPE_NOWAIT))
1812 status = STATUS_INVALID_PARAMETER;
1813 else
1815 fpi.CompletionMode = (*lpMode & PIPE_NOWAIT) ?
1816 FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
1817 fpi.ReadMode = (*lpMode & PIPE_READMODE_MESSAGE) ?
1818 FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
1819 status = NtSetInformationFile(hNamedPipe, &iosb, &fpi, sizeof(fpi), FilePipeInformation);
1822 if (status)
1824 SetLastError( RtlNtStatusToDosError(status) );
1825 return FALSE;
1829 return TRUE;
1832 /***********************************************************************
1833 * CallNamedPipeA (KERNEL32.@)
1835 BOOL WINAPI CallNamedPipeA(
1836 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1837 LPVOID lpOutput, DWORD dwOutputSize,
1838 LPDWORD lpBytesRead, DWORD nTimeout)
1840 DWORD len;
1841 LPWSTR str = NULL;
1842 BOOL ret;
1844 TRACE("%s %p %d %p %d %p %d\n",
1845 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1846 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1848 if( lpNamedPipeName )
1850 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1851 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1852 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1854 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1855 dwOutputSize, lpBytesRead, nTimeout );
1856 if( lpNamedPipeName )
1857 HeapFree( GetProcessHeap(), 0, str );
1859 return ret;
1862 /***********************************************************************
1863 * CallNamedPipeW (KERNEL32.@)
1865 BOOL WINAPI CallNamedPipeW(
1866 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1867 LPVOID lpOutput, DWORD lpOutputSize,
1868 LPDWORD lpBytesRead, DWORD nTimeout)
1870 HANDLE pipe;
1871 BOOL ret;
1872 DWORD mode;
1874 TRACE("%s %p %d %p %d %p %d\n",
1875 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1876 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1878 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1879 if (pipe == INVALID_HANDLE_VALUE)
1881 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1882 if (!ret)
1883 return FALSE;
1884 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1885 if (pipe == INVALID_HANDLE_VALUE)
1886 return FALSE;
1889 mode = PIPE_READMODE_MESSAGE;
1890 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1891 if (!ret)
1893 CloseHandle(pipe);
1894 return FALSE;
1897 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1898 CloseHandle(pipe);
1899 if (!ret)
1900 return FALSE;
1902 return TRUE;
1905 /******************************************************************
1906 * CreatePipe (KERNEL32.@)
1909 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1910 LPSECURITY_ATTRIBUTES sa, DWORD size )
1912 static unsigned index /* = 0 */;
1913 WCHAR name[64];
1914 HANDLE hr, hw;
1915 unsigned in_index = index;
1916 UNICODE_STRING nt_name;
1917 OBJECT_ATTRIBUTES attr;
1918 NTSTATUS status;
1919 IO_STATUS_BLOCK iosb;
1920 LARGE_INTEGER timeout;
1922 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1924 attr.Length = sizeof(attr);
1925 attr.RootDirectory = 0;
1926 attr.ObjectName = &nt_name;
1927 attr.Attributes = OBJ_CASE_INSENSITIVE |
1928 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1929 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1930 attr.SecurityQualityOfService = NULL;
1932 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1933 /* generate a unique pipe name (system wide) */
1936 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1937 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1938 'u','.','%','0','8','u','\0' };
1940 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1941 GetCurrentProcessId(), ++index);
1942 RtlInitUnicodeString(&nt_name, name);
1943 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1944 FILE_SHARE_WRITE, FILE_OVERWRITE_IF,
1945 FILE_SYNCHRONOUS_IO_NONALERT,
1946 FALSE, FALSE, FALSE,
1947 1, size, size, &timeout);
1948 if (status)
1950 SetLastError( RtlNtStatusToDosError(status) );
1951 hr = INVALID_HANDLE_VALUE;
1953 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1954 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1955 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1957 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1958 FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE);
1960 if (status)
1962 SetLastError( RtlNtStatusToDosError(status) );
1963 NtClose(hr);
1964 return FALSE;
1967 *hReadPipe = hr;
1968 *hWritePipe = hw;
1969 return TRUE;
1973 /******************************************************************************
1974 * CreateMailslotA [KERNEL32.@]
1976 * See CreateMailslotW.
1978 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1979 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1981 DWORD len;
1982 HANDLE handle;
1983 LPWSTR name = NULL;
1985 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1986 nMaxMessageSize, lReadTimeout, sa);
1988 if( lpName )
1990 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1991 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1992 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1995 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1997 HeapFree( GetProcessHeap(), 0, name );
1999 return handle;
2003 /******************************************************************************
2004 * CreateMailslotW [KERNEL32.@]
2006 * Create a mailslot with specified name.
2008 * PARAMS
2009 * lpName [I] Pointer to string for mailslot name
2010 * nMaxMessageSize [I] Maximum message size
2011 * lReadTimeout [I] Milliseconds before read time-out
2012 * sa [I] Pointer to security structure
2014 * RETURNS
2015 * Success: Handle to mailslot
2016 * Failure: INVALID_HANDLE_VALUE
2018 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
2019 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
2021 HANDLE handle = INVALID_HANDLE_VALUE;
2022 OBJECT_ATTRIBUTES attr;
2023 UNICODE_STRING nameW;
2024 LARGE_INTEGER timeout;
2025 IO_STATUS_BLOCK iosb;
2026 NTSTATUS status;
2028 TRACE("%s %d %d %p\n", debugstr_w(lpName),
2029 nMaxMessageSize, lReadTimeout, sa);
2031 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
2033 SetLastError( ERROR_PATH_NOT_FOUND );
2034 return INVALID_HANDLE_VALUE;
2037 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
2039 SetLastError( ERROR_FILENAME_EXCED_RANGE );
2040 RtlFreeUnicodeString( &nameW );
2041 return INVALID_HANDLE_VALUE;
2044 attr.Length = sizeof(attr);
2045 attr.RootDirectory = 0;
2046 attr.Attributes = OBJ_CASE_INSENSITIVE;
2047 attr.ObjectName = &nameW;
2048 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
2049 attr.SecurityQualityOfService = NULL;
2051 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
2052 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
2053 else
2054 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
2056 status = NtCreateMailslotFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr,
2057 &iosb, 0, 0, nMaxMessageSize, &timeout );
2058 if (status)
2060 SetLastError( RtlNtStatusToDosError(status) );
2061 handle = INVALID_HANDLE_VALUE;
2064 RtlFreeUnicodeString( &nameW );
2065 return handle;
2069 /******************************************************************************
2070 * GetMailslotInfo [KERNEL32.@]
2072 * Retrieve information about a mailslot.
2074 * PARAMS
2075 * hMailslot [I] Mailslot handle
2076 * lpMaxMessageSize [O] Address of maximum message size
2077 * lpNextSize [O] Address of size of next message
2078 * lpMessageCount [O] Address of number of messages
2079 * lpReadTimeout [O] Address of read time-out
2081 * RETURNS
2082 * Success: TRUE
2083 * Failure: FALSE
2085 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
2086 LPDWORD lpNextSize, LPDWORD lpMessageCount,
2087 LPDWORD lpReadTimeout )
2089 FILE_MAILSLOT_QUERY_INFORMATION info;
2090 IO_STATUS_BLOCK iosb;
2091 NTSTATUS status;
2093 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
2094 lpNextSize, lpMessageCount, lpReadTimeout);
2096 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
2097 FileMailslotQueryInformation );
2099 if( status != STATUS_SUCCESS )
2101 SetLastError( RtlNtStatusToDosError(status) );
2102 return FALSE;
2105 if( lpMaxMessageSize )
2106 *lpMaxMessageSize = info.MaximumMessageSize;
2107 if( lpNextSize )
2108 *lpNextSize = info.NextMessageSize;
2109 if( lpMessageCount )
2110 *lpMessageCount = info.MessagesAvailable;
2111 if( lpReadTimeout )
2113 if (info.ReadTimeout.QuadPart == (((LONGLONG)0x7fffffff << 32) | 0xffffffff))
2114 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
2115 else
2116 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
2118 return TRUE;
2122 /******************************************************************************
2123 * SetMailslotInfo [KERNEL32.@]
2125 * Set the read timeout of a mailslot.
2127 * PARAMS
2128 * hMailslot [I] Mailslot handle
2129 * dwReadTimeout [I] Timeout in milliseconds.
2131 * RETURNS
2132 * Success: TRUE
2133 * Failure: FALSE
2135 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
2137 FILE_MAILSLOT_SET_INFORMATION info;
2138 IO_STATUS_BLOCK iosb;
2139 NTSTATUS status;
2141 TRACE("%p %d\n", hMailslot, dwReadTimeout);
2143 if (dwReadTimeout != MAILSLOT_WAIT_FOREVER)
2144 info.ReadTimeout.QuadPart = (ULONGLONG)dwReadTimeout * -10000;
2145 else
2146 info.ReadTimeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
2147 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
2148 FileMailslotSetInformation );
2149 if( status != STATUS_SUCCESS )
2151 SetLastError( RtlNtStatusToDosError(status) );
2152 return FALSE;
2154 return TRUE;
2158 /******************************************************************************
2159 * CreateIoCompletionPort (KERNEL32.@)
2161 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
2162 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
2164 NTSTATUS status;
2165 HANDLE ret = 0;
2167 TRACE("(%p, %p, %08lx, %08x)\n",
2168 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
2170 if (hExistingCompletionPort && hFileHandle == INVALID_HANDLE_VALUE)
2172 SetLastError( ERROR_INVALID_PARAMETER);
2173 return NULL;
2176 if (hExistingCompletionPort)
2177 ret = hExistingCompletionPort;
2178 else
2180 status = NtCreateIoCompletion( &ret, IO_COMPLETION_ALL_ACCESS, NULL, dwNumberOfConcurrentThreads );
2181 if (status != STATUS_SUCCESS) goto fail;
2184 if (hFileHandle != INVALID_HANDLE_VALUE)
2186 FILE_COMPLETION_INFORMATION info;
2187 IO_STATUS_BLOCK iosb;
2189 info.CompletionPort = ret;
2190 info.CompletionKey = CompletionKey;
2191 status = NtSetInformationFile( hFileHandle, &iosb, &info, sizeof(info), FileCompletionInformation );
2192 if (status != STATUS_SUCCESS) goto fail;
2195 return ret;
2197 fail:
2198 if (ret && !hExistingCompletionPort)
2199 CloseHandle( ret );
2200 SetLastError( RtlNtStatusToDosError(status) );
2201 return 0;
2204 /******************************************************************************
2205 * GetQueuedCompletionStatus (KERNEL32.@)
2207 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
2208 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
2209 DWORD dwMilliseconds )
2211 NTSTATUS status;
2212 IO_STATUS_BLOCK iosb;
2213 LARGE_INTEGER wait_time;
2215 TRACE("(%p,%p,%p,%p,%d)\n",
2216 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
2218 *lpOverlapped = NULL;
2220 status = NtRemoveIoCompletion( CompletionPort, pCompletionKey, (PULONG_PTR)lpOverlapped,
2221 &iosb, get_nt_timeout( &wait_time, dwMilliseconds ) );
2222 if (status == STATUS_SUCCESS)
2224 *lpNumberOfBytesTransferred = iosb.Information;
2225 if (iosb.u.Status >= 0) return TRUE;
2226 SetLastError( RtlNtStatusToDosError(iosb.u.Status) );
2227 return FALSE;
2230 if (status == STATUS_TIMEOUT) SetLastError( WAIT_TIMEOUT );
2231 else SetLastError( RtlNtStatusToDosError(status) );
2232 return FALSE;
2236 /******************************************************************************
2237 * PostQueuedCompletionStatus (KERNEL32.@)
2239 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
2240 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
2242 NTSTATUS status;
2244 TRACE("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
2246 status = NtSetIoCompletion( CompletionPort, dwCompletionKey, (ULONG_PTR)lpOverlapped,
2247 STATUS_SUCCESS, dwNumberOfBytes );
2249 if (status == STATUS_SUCCESS) return TRUE;
2250 SetLastError( RtlNtStatusToDosError(status) );
2251 return FALSE;
2254 /******************************************************************************
2255 * BindIoCompletionCallback (KERNEL32.@)
2257 BOOL WINAPI BindIoCompletionCallback( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags)
2259 NTSTATUS status;
2261 TRACE("(%p, %p, %d)\n", FileHandle, Function, Flags);
2263 status = RtlSetIoCompletionCallback( FileHandle, (PRTL_OVERLAPPED_COMPLETION_ROUTINE)Function, Flags );
2264 if (status == STATUS_SUCCESS) return TRUE;
2265 SetLastError( RtlNtStatusToDosError(status) );
2266 return FALSE;
2270 /***********************************************************************
2271 * CreateMemoryResourceNotification (KERNEL32.@)
2273 HANDLE WINAPI CreateMemoryResourceNotification(MEMORY_RESOURCE_NOTIFICATION_TYPE type)
2275 static const WCHAR lowmemW[] =
2276 {'\\','K','e','r','n','e','l','O','b','j','e','c','t','s',
2277 '\\','L','o','w','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n',0};
2278 static const WCHAR highmemW[] =
2279 {'\\','K','e','r','n','e','l','O','b','j','e','c','t','s',
2280 '\\','H','i','g','h','M','e','m','o','r','y','C','o','n','d','i','t','i','o','n',0};
2281 HANDLE ret;
2282 UNICODE_STRING nameW;
2283 OBJECT_ATTRIBUTES attr;
2284 NTSTATUS status;
2286 switch (type)
2288 case LowMemoryResourceNotification:
2289 RtlInitUnicodeString( &nameW, lowmemW );
2290 break;
2291 case HighMemoryResourceNotification:
2292 RtlInitUnicodeString( &nameW, highmemW );
2293 break;
2294 default:
2295 SetLastError( ERROR_INVALID_PARAMETER );
2296 return 0;
2299 attr.Length = sizeof(attr);
2300 attr.RootDirectory = 0;
2301 attr.ObjectName = &nameW;
2302 attr.Attributes = 0;
2303 attr.SecurityDescriptor = NULL;
2304 attr.SecurityQualityOfService = NULL;
2305 status = NtOpenEvent( &ret, EVENT_ALL_ACCESS, &attr );
2306 if (status != STATUS_SUCCESS)
2308 SetLastError( RtlNtStatusToDosError(status) );
2309 return 0;
2311 return ret;
2314 /***********************************************************************
2315 * QueryMemoryResourceNotification (KERNEL32.@)
2317 BOOL WINAPI QueryMemoryResourceNotification(HANDLE handle, PBOOL state)
2319 switch (WaitForSingleObject( handle, 0 ))
2321 case WAIT_OBJECT_0:
2322 *state = TRUE;
2323 return TRUE;
2324 case WAIT_TIMEOUT:
2325 *state = FALSE;
2326 return TRUE;
2328 SetLastError( ERROR_INVALID_PARAMETER );
2329 return FALSE;
2332 /***********************************************************************
2333 * InitOnceBeginInitialize (KERNEL32.@)
2335 BOOL WINAPI InitOnceBeginInitialize( INIT_ONCE *once, DWORD flags, BOOL *pending, void **context )
2337 NTSTATUS status = RtlRunOnceBeginInitialize( once, flags, context );
2338 if (status >= 0) *pending = (status == STATUS_PENDING);
2339 else SetLastError( RtlNtStatusToDosError(status) );
2340 return status >= 0;
2343 /***********************************************************************
2344 * InitOnceComplete (KERNEL32.@)
2346 BOOL WINAPI InitOnceComplete( INIT_ONCE *once, DWORD flags, void *context )
2348 NTSTATUS status = RtlRunOnceComplete( once, flags, context );
2349 if (status != STATUS_SUCCESS) SetLastError( RtlNtStatusToDosError(status) );
2350 return !status;
2353 /***********************************************************************
2354 * InitOnceExecuteOnce (KERNEL32.@)
2356 BOOL WINAPI InitOnceExecuteOnce( INIT_ONCE *once, PINIT_ONCE_FN func, void *param, void **context )
2358 return !RtlRunOnceExecuteOnce( once, (PRTL_RUN_ONCE_INIT_FN)func, param, context );
2361 #ifdef __i386__
2363 /***********************************************************************
2364 * InterlockedCompareExchange (KERNEL32.@)
2366 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
2367 __ASM_STDCALL_FUNC(InterlockedCompareExchange, 12,
2368 "movl 12(%esp),%eax\n\t"
2369 "movl 8(%esp),%ecx\n\t"
2370 "movl 4(%esp),%edx\n\t"
2371 "lock; cmpxchgl %ecx,(%edx)\n\t"
2372 "ret $12")
2374 /***********************************************************************
2375 * InterlockedExchange (KERNEL32.@)
2377 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
2378 __ASM_STDCALL_FUNC(InterlockedExchange, 8,
2379 "movl 8(%esp),%eax\n\t"
2380 "movl 4(%esp),%edx\n\t"
2381 "lock; xchgl %eax,(%edx)\n\t"
2382 "ret $8")
2384 /***********************************************************************
2385 * InterlockedExchangeAdd (KERNEL32.@)
2387 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
2388 __ASM_STDCALL_FUNC(InterlockedExchangeAdd, 8,
2389 "movl 8(%esp),%eax\n\t"
2390 "movl 4(%esp),%edx\n\t"
2391 "lock; xaddl %eax,(%edx)\n\t"
2392 "ret $8")
2394 /***********************************************************************
2395 * InterlockedIncrement (KERNEL32.@)
2397 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2398 __ASM_STDCALL_FUNC(InterlockedIncrement, 4,
2399 "movl 4(%esp),%edx\n\t"
2400 "movl $1,%eax\n\t"
2401 "lock; xaddl %eax,(%edx)\n\t"
2402 "incl %eax\n\t"
2403 "ret $4")
2405 /***********************************************************************
2406 * InterlockedDecrement (KERNEL32.@)
2408 __ASM_STDCALL_FUNC(InterlockedDecrement, 4,
2409 "movl 4(%esp),%edx\n\t"
2410 "movl $-1,%eax\n\t"
2411 "lock; xaddl %eax,(%edx)\n\t"
2412 "decl %eax\n\t"
2413 "ret $4")
2415 #endif /* __i386__ */
2417 /***********************************************************************
2418 * SleepConditionVariableCS (KERNEL32.@)
2420 BOOL WINAPI SleepConditionVariableCS( CONDITION_VARIABLE *variable, CRITICAL_SECTION *crit, DWORD timeout )
2422 NTSTATUS status;
2423 LARGE_INTEGER time;
2425 status = RtlSleepConditionVariableCS( variable, crit, get_nt_timeout( &time, timeout ) );
2427 if (status != STATUS_SUCCESS)
2429 SetLastError( RtlNtStatusToDosError(status) );
2430 return FALSE;
2432 return TRUE;
2435 /***********************************************************************
2436 * SleepConditionVariableSRW (KERNEL32.@)
2438 BOOL WINAPI SleepConditionVariableSRW( RTL_CONDITION_VARIABLE *variable, RTL_SRWLOCK *lock, DWORD timeout, ULONG flags )
2440 NTSTATUS status;
2441 LARGE_INTEGER time;
2443 status = RtlSleepConditionVariableSRW( variable, lock, get_nt_timeout( &time, timeout ), flags );
2445 if (status != STATUS_SUCCESS)
2447 SetLastError( RtlNtStatusToDosError(status) );
2448 return FALSE;
2450 return TRUE;