wgl: Fix wglGetProcAddress bug.
[wine/hacks.git] / dlls / kernel32 / sync.c
blobeaa0f70dcd9a4065806c56208d931fc26306145c
1 /*
2 * Kernel synchronization objects
4 * Copyright 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <errno.h>
29 #include <stdarg.h>
30 #include <stdio.h>
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "winnls.h"
41 #include "winternl.h"
42 #include "winioctl.h"
43 #include "ddk/wdm.h"
45 #include "wine/unicode.h"
46 #include "wine/winbase16.h"
47 #include "kernel_private.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(sync);
53 /* check if current version is NT or Win95 */
54 inline static int is_version_nt(void)
56 return !(GetVersion() & 0x80000000);
59 /* returns directory handle to \\BaseNamedObjects */
60 HANDLE get_BaseNamedObjects_handle(void)
62 static HANDLE handle = NULL;
63 static const WCHAR basenameW[] =
64 {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
65 UNICODE_STRING str;
66 OBJECT_ATTRIBUTES attr;
68 if (!handle)
70 HANDLE dir;
72 RtlInitUnicodeString(&str, basenameW);
73 InitializeObjectAttributes(&attr, &str, 0, 0, NULL);
74 NtOpenDirectoryObject(&dir, DIRECTORY_CREATE_OBJECT|DIRECTORY_TRAVERSE,
75 &attr);
76 if (InterlockedCompareExchangePointer( (PVOID)&handle, dir, 0 ) != 0)
78 /* someone beat us here... */
79 CloseHandle( dir );
82 return handle;
85 /***********************************************************************
86 * Sleep (KERNEL32.@)
88 VOID WINAPI Sleep( DWORD timeout )
90 SleepEx( timeout, FALSE );
93 /******************************************************************************
94 * SleepEx (KERNEL32.@)
96 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
98 NTSTATUS status;
100 if (timeout == INFINITE) status = NtDelayExecution( alertable, NULL );
101 else
103 LARGE_INTEGER time;
105 time.QuadPart = timeout * (ULONGLONG)10000;
106 time.QuadPart = -time.QuadPart;
107 status = NtDelayExecution( alertable, &time );
109 if (status != STATUS_USER_APC) status = STATUS_SUCCESS;
110 return status;
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 unsigned int i;
163 if (count > MAXIMUM_WAIT_OBJECTS)
165 SetLastError(ERROR_INVALID_PARAMETER);
166 return WAIT_FAILED;
168 for (i = 0; i < count; i++)
170 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
171 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
172 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
173 hloc[i] = GetStdHandle( (DWORD)handles[i] );
174 else
175 hloc[i] = handles[i];
177 /* yes, even screen buffer console handles are waitable, and are
178 * handled as a handle to the console itself !!
180 if (is_console_handle(hloc[i]))
182 if (!VerifyConsoleIoHandle(hloc[i]))
184 return FALSE;
186 hloc[i] = GetConsoleInputWaitHandle();
190 if (timeout == INFINITE)
192 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, NULL );
194 else
196 LARGE_INTEGER time;
198 time.QuadPart = timeout * (ULONGLONG)10000;
199 time.QuadPart = -time.QuadPart;
200 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, &time );
203 if (HIWORD(status)) /* is it an error code? */
205 SetLastError( RtlNtStatusToDosError(status) );
206 status = WAIT_FAILED;
208 return status;
212 /***********************************************************************
213 * WaitForSingleObject (KERNEL.460)
215 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
217 DWORD retval, mutex_count;
219 ReleaseThunkLock( &mutex_count );
220 retval = WaitForSingleObject( handle, timeout );
221 RestoreThunkLock( mutex_count );
222 return retval;
225 /***********************************************************************
226 * WaitForMultipleObjects (KERNEL.461)
228 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
229 BOOL wait_all, DWORD timeout )
231 DWORD retval, mutex_count;
233 ReleaseThunkLock( &mutex_count );
234 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
235 RestoreThunkLock( mutex_count );
236 return retval;
239 /***********************************************************************
240 * WaitForMultipleObjectsEx (KERNEL.495)
242 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
243 BOOL wait_all, DWORD timeout, BOOL alertable )
245 DWORD retval, mutex_count;
247 ReleaseThunkLock( &mutex_count );
248 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
249 RestoreThunkLock( mutex_count );
250 return retval;
253 /***********************************************************************
254 * RegisterWaitForSingleObject (KERNEL32.@)
256 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
257 WAITORTIMERCALLBACK Callback, PVOID Context,
258 ULONG dwMilliseconds, ULONG dwFlags)
260 FIXME("%p %p %p %p %d %d\n",
261 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
262 return FALSE;
265 /***********************************************************************
266 * RegisterWaitForSingleObjectEx (KERNEL32.@)
268 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
269 WAITORTIMERCALLBACK Callback, PVOID Context,
270 ULONG dwMilliseconds, ULONG dwFlags )
272 FIXME("%p %p %p %d %d\n",
273 hObject,Callback,Context,dwMilliseconds,dwFlags);
274 return 0;
277 /***********************************************************************
278 * UnregisterWait (KERNEL32.@)
280 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
282 FIXME("%p\n",WaitHandle);
283 return FALSE;
286 /***********************************************************************
287 * UnregisterWaitEx (KERNEL32.@)
289 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
291 FIXME("%p %p\n",WaitHandle, CompletionEvent);
292 return FALSE;
295 /***********************************************************************
296 * SignalObjectAndWait (KERNEL32.@)
298 * Allows to atomically signal any of the synchro objects (semaphore,
299 * mutex, event) and wait on another.
301 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
302 DWORD dwMilliseconds, BOOL bAlertable )
304 NTSTATUS status;
305 LARGE_INTEGER timeout, *ptimeout = NULL;
307 TRACE("%p %p %d %d\n", hObjectToSignal,
308 hObjectToWaitOn, dwMilliseconds, bAlertable);
310 if (dwMilliseconds != INFINITE)
312 timeout.QuadPart = dwMilliseconds * (ULONGLONG)10000;
313 timeout.QuadPart = -timeout.QuadPart;
314 ptimeout = &timeout;
317 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn,
318 bAlertable, ptimeout );
319 if (HIWORD(status))
321 SetLastError( RtlNtStatusToDosError(status) );
322 status = WAIT_FAILED;
324 return status;
327 /***********************************************************************
328 * InitializeCriticalSection (KERNEL32.@)
330 * Initialise a critical section before use.
332 * PARAMS
333 * crit [O] Critical section to initialise.
335 * RETURNS
336 * Nothing. If the function fails an exception is raised.
338 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
340 NTSTATUS ret = RtlInitializeCriticalSection( crit );
341 if (ret) RtlRaiseStatus( ret );
344 /***********************************************************************
345 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
347 * Initialise a critical section with a spin count.
349 * PARAMS
350 * crit [O] Critical section to initialise.
351 * spincount [I] Number of times to spin upon contention.
353 * RETURNS
354 * Success: TRUE.
355 * Failure: Nothing. If the function fails an exception is raised.
357 * NOTES
358 * spincount is ignored on uni-processor systems.
360 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
362 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
363 if (ret) RtlRaiseStatus( ret );
364 return !ret;
367 /***********************************************************************
368 * MakeCriticalSectionGlobal (KERNEL32.@)
370 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
372 /* let's assume that only one thread at a time will try to do this */
373 HANDLE sem = crit->LockSemaphore;
374 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
375 crit->LockSemaphore = ConvertToGlobalHandle( sem );
376 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
377 crit->DebugInfo = NULL;
381 /***********************************************************************
382 * ReinitializeCriticalSection (KERNEL32.@)
384 * Initialise an already used critical section.
386 * PARAMS
387 * crit [O] Critical section to initialise.
389 * RETURNS
390 * Nothing.
392 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
394 if ( !crit->LockSemaphore )
395 RtlInitializeCriticalSection( crit );
399 /***********************************************************************
400 * UninitializeCriticalSection (KERNEL32.@)
402 * UnInitialise a critical section after use.
404 * PARAMS
405 * crit [O] Critical section to uninitialise (destroy).
407 * RETURNS
408 * Nothing.
410 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
412 RtlDeleteCriticalSection( crit );
416 /***********************************************************************
417 * CreateEventA (KERNEL32.@)
419 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
420 BOOL initial_state, LPCSTR name )
422 WCHAR buffer[MAX_PATH];
424 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
426 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
428 SetLastError( ERROR_FILENAME_EXCED_RANGE );
429 return 0;
431 return CreateEventW( sa, manual_reset, initial_state, buffer );
435 /***********************************************************************
436 * CreateEventW (KERNEL32.@)
438 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
439 BOOL initial_state, LPCWSTR name )
441 HANDLE ret;
442 UNICODE_STRING nameW;
443 OBJECT_ATTRIBUTES attr;
444 NTSTATUS status;
446 /* one buggy program needs this
447 * ("Van Dale Groot woordenboek der Nederlandse taal")
449 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
451 ERR("Bad security attributes pointer %p\n",sa);
452 SetLastError( ERROR_INVALID_PARAMETER);
453 return 0;
456 attr.Length = sizeof(attr);
457 attr.RootDirectory = 0;
458 attr.ObjectName = NULL;
459 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
460 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
461 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
462 attr.SecurityQualityOfService = NULL;
463 if (name)
465 RtlInitUnicodeString( &nameW, name );
466 attr.ObjectName = &nameW;
467 attr.RootDirectory = get_BaseNamedObjects_handle();
470 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
471 if (status == STATUS_OBJECT_NAME_EXISTS)
472 SetLastError( ERROR_ALREADY_EXISTS );
473 else
474 SetLastError( RtlNtStatusToDosError(status) );
475 return ret;
479 /***********************************************************************
480 * CreateW32Event (KERNEL.457)
482 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
484 return CreateEventW( NULL, manual_reset, initial_state, NULL );
488 /***********************************************************************
489 * OpenEventA (KERNEL32.@)
491 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
493 WCHAR buffer[MAX_PATH];
495 if (!name) return OpenEventW( access, inherit, NULL );
497 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
499 SetLastError( ERROR_FILENAME_EXCED_RANGE );
500 return 0;
502 return OpenEventW( access, inherit, buffer );
506 /***********************************************************************
507 * OpenEventW (KERNEL32.@)
509 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
511 HANDLE ret;
512 UNICODE_STRING nameW;
513 OBJECT_ATTRIBUTES attr;
514 NTSTATUS status;
516 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
518 attr.Length = sizeof(attr);
519 attr.RootDirectory = 0;
520 attr.ObjectName = NULL;
521 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
522 attr.SecurityDescriptor = NULL;
523 attr.SecurityQualityOfService = NULL;
524 if (name)
526 RtlInitUnicodeString( &nameW, name );
527 attr.ObjectName = &nameW;
528 attr.RootDirectory = get_BaseNamedObjects_handle();
531 status = NtOpenEvent( &ret, access, &attr );
532 if (status != STATUS_SUCCESS)
534 SetLastError( RtlNtStatusToDosError(status) );
535 return 0;
537 return ret;
540 /***********************************************************************
541 * PulseEvent (KERNEL32.@)
543 BOOL WINAPI PulseEvent( HANDLE handle )
545 NTSTATUS status;
547 if ((status = NtPulseEvent( handle, NULL )))
548 SetLastError( RtlNtStatusToDosError(status) );
549 return !status;
553 /***********************************************************************
554 * SetW32Event (KERNEL.458)
555 * SetEvent (KERNEL32.@)
557 BOOL WINAPI SetEvent( HANDLE handle )
559 NTSTATUS status;
561 if ((status = NtSetEvent( handle, NULL )))
562 SetLastError( RtlNtStatusToDosError(status) );
563 return !status;
567 /***********************************************************************
568 * ResetW32Event (KERNEL.459)
569 * ResetEvent (KERNEL32.@)
571 BOOL WINAPI ResetEvent( HANDLE handle )
573 NTSTATUS status;
575 if ((status = NtResetEvent( handle, NULL )))
576 SetLastError( RtlNtStatusToDosError(status) );
577 return !status;
581 /***********************************************************************
582 * NOTE: The Win95 VWin32_Event routines given below are really low-level
583 * routines implemented directly by VWin32. The user-mode libraries
584 * implement Win32 synchronisation routines on top of these low-level
585 * primitives. We do it the other way around here :-)
588 /***********************************************************************
589 * VWin32_EventCreate (KERNEL.442)
591 HANDLE WINAPI VWin32_EventCreate(VOID)
593 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
594 return ConvertToGlobalHandle( hEvent );
597 /***********************************************************************
598 * VWin32_EventDestroy (KERNEL.443)
600 VOID WINAPI VWin32_EventDestroy(HANDLE event)
602 CloseHandle( event );
605 /***********************************************************************
606 * VWin32_EventWait (KERNEL.450)
608 VOID WINAPI VWin32_EventWait(HANDLE event)
610 DWORD mutex_count;
612 ReleaseThunkLock( &mutex_count );
613 WaitForSingleObject( event, INFINITE );
614 RestoreThunkLock( mutex_count );
617 /***********************************************************************
618 * VWin32_EventSet (KERNEL.451)
619 * KERNEL_479 (KERNEL.479)
621 VOID WINAPI VWin32_EventSet(HANDLE event)
623 SetEvent( event );
628 /***********************************************************************
629 * CreateMutexA (KERNEL32.@)
631 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
633 WCHAR buffer[MAX_PATH];
635 if (!name) return CreateMutexW( sa, owner, NULL );
637 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
639 SetLastError( ERROR_FILENAME_EXCED_RANGE );
640 return 0;
642 return CreateMutexW( sa, owner, buffer );
646 /***********************************************************************
647 * CreateMutexW (KERNEL32.@)
649 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
651 HANDLE ret;
652 UNICODE_STRING nameW;
653 OBJECT_ATTRIBUTES attr;
654 NTSTATUS status;
656 attr.Length = sizeof(attr);
657 attr.RootDirectory = 0;
658 attr.ObjectName = NULL;
659 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
660 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
661 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
662 attr.SecurityQualityOfService = NULL;
663 if (name)
665 RtlInitUnicodeString( &nameW, name );
666 attr.ObjectName = &nameW;
667 attr.RootDirectory = get_BaseNamedObjects_handle();
670 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
671 if (status == STATUS_OBJECT_NAME_EXISTS)
672 SetLastError( ERROR_ALREADY_EXISTS );
673 else
674 SetLastError( RtlNtStatusToDosError(status) );
675 return ret;
679 /***********************************************************************
680 * OpenMutexA (KERNEL32.@)
682 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
684 WCHAR buffer[MAX_PATH];
686 if (!name) return OpenMutexW( access, inherit, NULL );
688 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
690 SetLastError( ERROR_FILENAME_EXCED_RANGE );
691 return 0;
693 return OpenMutexW( access, inherit, buffer );
697 /***********************************************************************
698 * OpenMutexW (KERNEL32.@)
700 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
702 HANDLE ret;
703 UNICODE_STRING nameW;
704 OBJECT_ATTRIBUTES attr;
705 NTSTATUS status;
707 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
709 attr.Length = sizeof(attr);
710 attr.RootDirectory = 0;
711 attr.ObjectName = NULL;
712 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
713 attr.SecurityDescriptor = NULL;
714 attr.SecurityQualityOfService = NULL;
715 if (name)
717 RtlInitUnicodeString( &nameW, name );
718 attr.ObjectName = &nameW;
719 attr.RootDirectory = get_BaseNamedObjects_handle();
722 status = NtOpenMutant( &ret, access, &attr );
723 if (status != STATUS_SUCCESS)
725 SetLastError( RtlNtStatusToDosError(status) );
726 return 0;
728 return ret;
732 /***********************************************************************
733 * ReleaseMutex (KERNEL32.@)
735 BOOL WINAPI ReleaseMutex( HANDLE handle )
737 NTSTATUS status;
739 status = NtReleaseMutant(handle, NULL);
740 if (status != STATUS_SUCCESS)
742 SetLastError( RtlNtStatusToDosError(status) );
743 return FALSE;
745 return TRUE;
750 * Semaphores
754 /***********************************************************************
755 * CreateSemaphoreA (KERNEL32.@)
757 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
759 WCHAR buffer[MAX_PATH];
761 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
763 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
765 SetLastError( ERROR_FILENAME_EXCED_RANGE );
766 return 0;
768 return CreateSemaphoreW( sa, initial, max, buffer );
772 /***********************************************************************
773 * CreateSemaphoreW (KERNEL32.@)
775 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
776 LONG max, LPCWSTR name )
778 HANDLE ret;
779 UNICODE_STRING nameW;
780 OBJECT_ATTRIBUTES attr;
781 NTSTATUS status;
783 attr.Length = sizeof(attr);
784 attr.RootDirectory = 0;
785 attr.ObjectName = NULL;
786 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
787 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
788 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
789 attr.SecurityQualityOfService = NULL;
790 if (name)
792 RtlInitUnicodeString( &nameW, name );
793 attr.ObjectName = &nameW;
794 attr.RootDirectory = get_BaseNamedObjects_handle();
797 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
798 if (status == STATUS_OBJECT_NAME_EXISTS)
799 SetLastError( ERROR_ALREADY_EXISTS );
800 else
801 SetLastError( RtlNtStatusToDosError(status) );
802 return ret;
806 /***********************************************************************
807 * OpenSemaphoreA (KERNEL32.@)
809 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
811 WCHAR buffer[MAX_PATH];
813 if (!name) return OpenSemaphoreW( access, inherit, NULL );
815 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
817 SetLastError( ERROR_FILENAME_EXCED_RANGE );
818 return 0;
820 return OpenSemaphoreW( access, inherit, buffer );
824 /***********************************************************************
825 * OpenSemaphoreW (KERNEL32.@)
827 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
829 HANDLE ret;
830 UNICODE_STRING nameW;
831 OBJECT_ATTRIBUTES attr;
832 NTSTATUS status;
834 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
836 attr.Length = sizeof(attr);
837 attr.RootDirectory = 0;
838 attr.ObjectName = NULL;
839 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
840 attr.SecurityDescriptor = NULL;
841 attr.SecurityQualityOfService = NULL;
842 if (name)
844 RtlInitUnicodeString( &nameW, name );
845 attr.ObjectName = &nameW;
846 attr.RootDirectory = get_BaseNamedObjects_handle();
849 status = NtOpenSemaphore( &ret, access, &attr );
850 if (status != STATUS_SUCCESS)
852 SetLastError( RtlNtStatusToDosError(status) );
853 return 0;
855 return ret;
859 /***********************************************************************
860 * ReleaseSemaphore (KERNEL32.@)
862 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
864 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
865 if (status) SetLastError( RtlNtStatusToDosError(status) );
866 return !status;
871 * Timers
875 /***********************************************************************
876 * CreateWaitableTimerA (KERNEL32.@)
878 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
880 WCHAR buffer[MAX_PATH];
882 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
884 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
886 SetLastError( ERROR_FILENAME_EXCED_RANGE );
887 return 0;
889 return CreateWaitableTimerW( sa, manual, buffer );
893 /***********************************************************************
894 * CreateWaitableTimerW (KERNEL32.@)
896 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
898 HANDLE handle;
899 NTSTATUS status;
900 UNICODE_STRING nameW;
901 OBJECT_ATTRIBUTES attr;
903 attr.Length = sizeof(attr);
904 attr.RootDirectory = 0;
905 attr.ObjectName = NULL;
906 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
907 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
908 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
909 attr.SecurityQualityOfService = NULL;
910 if (name)
912 RtlInitUnicodeString( &nameW, name );
913 attr.ObjectName = &nameW;
914 attr.RootDirectory = get_BaseNamedObjects_handle();
917 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
918 manual ? NotificationTimer : SynchronizationTimer);
919 if (status == STATUS_OBJECT_NAME_EXISTS)
920 SetLastError( ERROR_ALREADY_EXISTS );
921 else
922 SetLastError( RtlNtStatusToDosError(status) );
923 return handle;
927 /***********************************************************************
928 * OpenWaitableTimerA (KERNEL32.@)
930 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
932 WCHAR buffer[MAX_PATH];
934 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
936 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
938 SetLastError( ERROR_FILENAME_EXCED_RANGE );
939 return 0;
941 return OpenWaitableTimerW( access, inherit, buffer );
945 /***********************************************************************
946 * OpenWaitableTimerW (KERNEL32.@)
948 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
950 HANDLE handle;
951 UNICODE_STRING nameW;
952 OBJECT_ATTRIBUTES attr;
953 NTSTATUS status;
955 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
957 attr.Length = sizeof(attr);
958 attr.RootDirectory = 0;
959 attr.ObjectName = NULL;
960 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
961 attr.SecurityDescriptor = NULL;
962 attr.SecurityQualityOfService = NULL;
963 if (name)
965 RtlInitUnicodeString( &nameW, name );
966 attr.ObjectName = &nameW;
967 attr.RootDirectory = get_BaseNamedObjects_handle();
970 status = NtOpenTimer(&handle, access, &attr);
971 if (status != STATUS_SUCCESS)
973 SetLastError( RtlNtStatusToDosError(status) );
974 return 0;
976 return handle;
980 /***********************************************************************
981 * SetWaitableTimer (KERNEL32.@)
983 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
984 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
986 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
987 arg, resume, period, NULL);
989 if (status != STATUS_SUCCESS)
991 SetLastError( RtlNtStatusToDosError(status) );
992 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
994 return TRUE;
998 /***********************************************************************
999 * CancelWaitableTimer (KERNEL32.@)
1001 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1003 NTSTATUS status;
1005 status = NtCancelTimer(handle, NULL);
1006 if (status != STATUS_SUCCESS)
1008 SetLastError( RtlNtStatusToDosError(status) );
1009 return FALSE;
1011 return TRUE;
1015 /***********************************************************************
1016 * CreateTimerQueue (KERNEL32.@)
1018 HANDLE WINAPI CreateTimerQueue(void)
1020 FIXME("stub\n");
1021 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1022 return NULL;
1026 /***********************************************************************
1027 * DeleteTimerQueueEx (KERNEL32.@)
1029 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1031 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
1032 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1033 return 0;
1036 /***********************************************************************
1037 * CreateTimerQueueTimer (KERNEL32.@)
1039 * Creates a timer-queue timer. This timer expires at the specified due
1040 * time (in ms), then after every specified period (in ms). When the timer
1041 * expires, the callback function is called.
1043 * RETURNS
1044 * nonzero on success or zero on faillure
1046 * BUGS
1047 * Unimplemented
1049 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1050 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1051 DWORD DueTime, DWORD Period, ULONG Flags )
1053 FIXME("stub\n");
1054 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1055 return TRUE;
1058 /***********************************************************************
1059 * DeleteTimerQueueTimer (KERNEL32.@)
1061 * Cancels a timer-queue timer.
1063 * RETURNS
1064 * nonzero on success or zero on faillure
1066 * BUGS
1067 * Unimplemented
1069 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1070 HANDLE CompletionEvent )
1072 FIXME("stub\n");
1073 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1074 return TRUE;
1079 * Pipes
1083 /***********************************************************************
1084 * CreateNamedPipeA (KERNEL32.@)
1086 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1087 DWORD dwPipeMode, DWORD nMaxInstances,
1088 DWORD nOutBufferSize, DWORD nInBufferSize,
1089 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1091 WCHAR buffer[MAX_PATH];
1093 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1094 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1096 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1098 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1099 return INVALID_HANDLE_VALUE;
1101 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1102 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1106 /***********************************************************************
1107 * CreateNamedPipeW (KERNEL32.@)
1109 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1110 DWORD dwPipeMode, DWORD nMaxInstances,
1111 DWORD nOutBufferSize, DWORD nInBufferSize,
1112 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1114 HANDLE handle;
1115 UNICODE_STRING nt_name;
1116 OBJECT_ATTRIBUTES attr;
1117 DWORD options;
1118 BOOLEAN pipe_type, read_mode, non_block;
1119 NTSTATUS status;
1120 IO_STATUS_BLOCK iosb;
1121 LARGE_INTEGER timeout;
1123 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1124 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1125 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1127 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1129 SetLastError( ERROR_PATH_NOT_FOUND );
1130 return INVALID_HANDLE_VALUE;
1132 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1134 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1135 RtlFreeUnicodeString( &nt_name );
1136 return INVALID_HANDLE_VALUE;
1139 attr.Length = sizeof(attr);
1140 attr.RootDirectory = 0;
1141 attr.ObjectName = &nt_name;
1142 attr.Attributes = OBJ_CASE_INSENSITIVE |
1143 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1144 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1145 attr.SecurityQualityOfService = NULL;
1147 options = 0;
1148 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1149 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1150 if ((dwOpenMode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
1151 options |= FILE_PIPE_FULL_DUPLEX;
1152 else if (dwOpenMode & PIPE_ACCESS_INBOUND) options |= FILE_PIPE_INBOUND;
1153 else if (dwOpenMode & PIPE_ACCESS_OUTBOUND) options |= FILE_PIPE_OUTBOUND;
1154 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1155 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1156 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1157 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0UL;
1159 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1161 SetLastError(0);
1163 status = NtCreateNamedPipeFile(&handle, GENERIC_READ|GENERIC_WRITE, &attr, &iosb,
1164 0, FILE_OVERWRITE_IF, options, pipe_type,
1165 read_mode, non_block, nMaxInstances,
1166 nInBufferSize, nOutBufferSize, &timeout);
1168 RtlFreeUnicodeString( &nt_name );
1169 if (status)
1171 handle = INVALID_HANDLE_VALUE;
1172 SetLastError( RtlNtStatusToDosError(status) );
1174 return handle;
1178 /***********************************************************************
1179 * PeekNamedPipe (KERNEL32.@)
1181 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1182 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1184 FILE_PIPE_PEEK_BUFFER local_buffer;
1185 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1186 IO_STATUS_BLOCK io;
1187 NTSTATUS status;
1189 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1190 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1192 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1193 return FALSE;
1196 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1197 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1198 if (!status)
1200 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1201 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1202 if (lpcbRead) *lpcbRead = read_size;
1203 if (lpcbMessage) *lpcbMessage = 0; /* FIXME */
1204 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1206 else SetLastError( RtlNtStatusToDosError(status) );
1208 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1209 return !status;
1212 /***********************************************************************
1213 * WaitNamedPipeA (KERNEL32.@)
1215 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1217 WCHAR buffer[MAX_PATH];
1219 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1221 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1223 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1224 return 0;
1226 return WaitNamedPipeW( buffer, nTimeOut );
1230 /***********************************************************************
1231 * WaitNamedPipeW (KERNEL32.@)
1233 * Waits for a named pipe instance to become available
1235 * PARAMS
1236 * name [I] Pointer to a named pipe name to wait for
1237 * nTimeOut [I] How long to wait in ms
1239 * RETURNS
1240 * TRUE: Success, named pipe can be opened with CreateFile
1241 * FALSE: Failure, GetLastError can be called for further details
1243 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1245 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1246 NTSTATUS status;
1247 UNICODE_STRING nt_name, pipe_dev_name;
1248 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1249 IO_STATUS_BLOCK iosb;
1250 OBJECT_ATTRIBUTES attr;
1251 ULONG sz_pipe_wait;
1252 HANDLE pipe_dev;
1254 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1256 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1257 return FALSE;
1259 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1260 nt_name.Length < sizeof(leadin) ||
1261 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR) != 0))
1263 RtlFreeUnicodeString( &nt_name );
1264 SetLastError( ERROR_PATH_NOT_FOUND );
1265 return FALSE;
1268 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1269 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1271 RtlFreeUnicodeString( &nt_name );
1272 SetLastError( ERROR_OUTOFMEMORY );
1273 return FALSE;
1276 pipe_dev_name.Buffer = nt_name.Buffer;
1277 pipe_dev_name.Length = sizeof(leadin);
1278 pipe_dev_name.MaximumLength = sizeof(leadin);
1279 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1280 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1281 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1282 FILE_SYNCHRONOUS_IO_NONALERT);
1283 if (status != ERROR_SUCCESS)
1285 SetLastError( ERROR_PATH_NOT_FOUND );
1286 return FALSE;
1289 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1290 pipe_wait->Timeout.QuadPart = nTimeOut * -10000L;
1291 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1292 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1293 pipe_wait->NameLength);
1294 RtlFreeUnicodeString( &nt_name );
1296 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1297 pipe_wait, sz_pipe_wait, NULL, 0 );
1299 HeapFree( GetProcessHeap(), 0, pipe_wait );
1300 NtClose( pipe_dev );
1302 if(status != STATUS_SUCCESS)
1304 SetLastError(RtlNtStatusToDosError(status));
1305 return FALSE;
1307 else
1308 return TRUE;
1312 /***********************************************************************
1313 * ConnectNamedPipe (KERNEL32.@)
1315 * Connects to a named pipe
1317 * Parameters
1318 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1319 * overlapped: Optional OVERLAPPED struct
1321 * Return values
1322 * TRUE: Success
1323 * FALSE: Failure, GetLastError can be called for further details
1325 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1327 NTSTATUS status;
1328 IO_STATUS_BLOCK status_block;
1330 TRACE("(%p,%p)\n", hPipe, overlapped);
1332 if(overlapped)
1333 overlapped->Internal = STATUS_PENDING;
1335 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, NULL,
1336 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1337 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1339 if (status == STATUS_SUCCESS) return TRUE;
1340 SetLastError( RtlNtStatusToDosError(status) );
1341 return FALSE;
1344 /***********************************************************************
1345 * DisconnectNamedPipe (KERNEL32.@)
1347 * Disconnects from a named pipe
1349 * Parameters
1350 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1352 * Return values
1353 * TRUE: Success
1354 * FALSE: Failure, GetLastError can be called for further details
1356 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1358 NTSTATUS status;
1359 IO_STATUS_BLOCK io_block;
1361 TRACE("(%p)\n",hPipe);
1363 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1364 NULL, 0, NULL, 0);
1365 if (status == STATUS_SUCCESS) return TRUE;
1366 SetLastError( RtlNtStatusToDosError(status) );
1367 return FALSE;
1370 /***********************************************************************
1371 * TransactNamedPipe (KERNEL32.@)
1373 * BUGS
1374 * should be done as a single operation in the wineserver or kernel
1376 BOOL WINAPI TransactNamedPipe(
1377 HANDLE handle, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1378 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1380 BOOL r;
1381 DWORD count;
1383 TRACE("%p %p %d %p %d %p %p\n",
1384 handle, lpInput, dwInputSize, lpOutput,
1385 dwOutputSize, lpBytesRead, lpOverlapped);
1387 if (lpOverlapped)
1389 FIXME("Doesn't support overlapped operation as yet\n");
1390 return FALSE;
1393 r = WriteFile(handle, lpOutput, dwOutputSize, &count, NULL);
1394 if (r)
1395 r = ReadFile(handle, lpInput, dwInputSize, lpBytesRead, NULL);
1397 return r;
1400 /***********************************************************************
1401 * GetNamedPipeInfo (KERNEL32.@)
1403 BOOL WINAPI GetNamedPipeInfo(
1404 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1405 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1407 FILE_PIPE_LOCAL_INFORMATION fpli;
1408 IO_STATUS_BLOCK iosb;
1409 NTSTATUS status;
1411 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1412 FilePipeLocalInformation);
1413 if (status)
1415 SetLastError( RtlNtStatusToDosError(status) );
1416 return FALSE;
1419 if (lpFlags)
1421 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1422 PIPE_SERVER_END : PIPE_CLIENT_END;
1423 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1424 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1427 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1428 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1429 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1431 return TRUE;
1434 /***********************************************************************
1435 * GetNamedPipeHandleStateA (KERNEL32.@)
1437 BOOL WINAPI GetNamedPipeHandleStateA(
1438 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1439 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1440 LPSTR lpUsername, DWORD nUsernameMaxSize)
1442 FIXME("%p %p %p %p %p %p %d\n",
1443 hNamedPipe, lpState, lpCurInstances,
1444 lpMaxCollectionCount, lpCollectDataTimeout,
1445 lpUsername, nUsernameMaxSize);
1447 return FALSE;
1450 /***********************************************************************
1451 * GetNamedPipeHandleStateW (KERNEL32.@)
1453 BOOL WINAPI GetNamedPipeHandleStateW(
1454 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1455 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1456 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1458 FIXME("%p %p %p %p %p %p %d\n",
1459 hNamedPipe, lpState, lpCurInstances,
1460 lpMaxCollectionCount, lpCollectDataTimeout,
1461 lpUsername, nUsernameMaxSize);
1463 return FALSE;
1466 /***********************************************************************
1467 * SetNamedPipeHandleState (KERNEL32.@)
1469 BOOL WINAPI SetNamedPipeHandleState(
1470 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1471 LPDWORD lpCollectDataTimeout)
1473 /* should be a fixme, but this function is called a lot by the RPC
1474 * runtime, and it slows down InstallShield a fair bit. */
1475 WARN("stub: %p %p/%d %p %p\n",
1476 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1477 return FALSE;
1480 /***********************************************************************
1481 * CallNamedPipeA (KERNEL32.@)
1483 BOOL WINAPI CallNamedPipeA(
1484 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1485 LPVOID lpOutput, DWORD dwOutputSize,
1486 LPDWORD lpBytesRead, DWORD nTimeout)
1488 DWORD len;
1489 LPWSTR str = NULL;
1490 BOOL ret;
1492 TRACE("%s %p %d %p %d %p %d\n",
1493 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1494 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1496 if( lpNamedPipeName )
1498 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1499 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1500 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1502 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1503 dwOutputSize, lpBytesRead, nTimeout );
1504 if( lpNamedPipeName )
1505 HeapFree( GetProcessHeap(), 0, str );
1507 return ret;
1510 /***********************************************************************
1511 * CallNamedPipeW (KERNEL32.@)
1513 BOOL WINAPI CallNamedPipeW(
1514 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1515 LPVOID lpOutput, DWORD lpOutputSize,
1516 LPDWORD lpBytesRead, DWORD nTimeout)
1518 FIXME("%s %p %d %p %d %p %d\n",
1519 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1520 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1521 return FALSE;
1524 /******************************************************************
1525 * CreatePipe (KERNEL32.@)
1528 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1529 LPSECURITY_ATTRIBUTES sa, DWORD size )
1531 static unsigned index /* = 0 */;
1532 WCHAR name[64];
1533 HANDLE hr, hw;
1534 unsigned in_index = index;
1535 UNICODE_STRING nt_name;
1536 OBJECT_ATTRIBUTES attr;
1537 NTSTATUS status;
1538 IO_STATUS_BLOCK iosb;
1539 LARGE_INTEGER timeout;
1541 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1543 attr.Length = sizeof(attr);
1544 attr.RootDirectory = 0;
1545 attr.ObjectName = &nt_name;
1546 attr.Attributes = OBJ_CASE_INSENSITIVE |
1547 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1548 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1549 attr.SecurityQualityOfService = NULL;
1551 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1552 /* generate a unique pipe name (system wide) */
1555 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1556 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1557 'u','.','%','0','8','u','\0' };
1559 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1560 GetCurrentProcessId(), ++index);
1561 RtlInitUnicodeString(&nt_name, name);
1562 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1563 0, FILE_OVERWRITE_IF,
1564 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1565 FALSE, FALSE, FALSE,
1566 1, size, size, &timeout);
1567 if (status)
1569 SetLastError( RtlNtStatusToDosError(status) );
1570 hr = INVALID_HANDLE_VALUE;
1572 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1573 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1574 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1576 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1577 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1579 if (status)
1581 SetLastError( RtlNtStatusToDosError(status) );
1582 NtClose(hr);
1583 return FALSE;
1586 *hReadPipe = hr;
1587 *hWritePipe = hw;
1588 return TRUE;
1592 /******************************************************************************
1593 * CreateMailslotA [KERNEL32.@]
1595 * See CreatMailslotW.
1597 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1598 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1600 DWORD len;
1601 HANDLE handle;
1602 LPWSTR name = NULL;
1604 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1605 nMaxMessageSize, lReadTimeout, sa);
1607 if( lpName )
1609 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1610 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1611 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1614 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1616 HeapFree( GetProcessHeap(), 0, name );
1618 return handle;
1622 /******************************************************************************
1623 * CreateMailslotW [KERNEL32.@]
1625 * Create a mailslot with specified name.
1627 * PARAMS
1628 * lpName [I] Pointer to string for mailslot name
1629 * nMaxMessageSize [I] Maximum message size
1630 * lReadTimeout [I] Milliseconds before read time-out
1631 * sa [I] Pointer to security structure
1633 * RETURNS
1634 * Success: Handle to mailslot
1635 * Failure: INVALID_HANDLE_VALUE
1637 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1638 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1640 HANDLE handle = INVALID_HANDLE_VALUE;
1641 OBJECT_ATTRIBUTES attr;
1642 UNICODE_STRING nameW;
1643 LARGE_INTEGER timeout;
1644 IO_STATUS_BLOCK iosb;
1645 NTSTATUS status;
1647 TRACE("%s %d %d %p\n", debugstr_w(lpName),
1648 nMaxMessageSize, lReadTimeout, sa);
1650 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1652 SetLastError( ERROR_PATH_NOT_FOUND );
1653 return INVALID_HANDLE_VALUE;
1656 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1658 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1659 RtlFreeUnicodeString( &nameW );
1660 return INVALID_HANDLE_VALUE;
1663 attr.Length = sizeof(attr);
1664 attr.RootDirectory = 0;
1665 attr.Attributes = OBJ_CASE_INSENSITIVE;
1666 attr.ObjectName = &nameW;
1667 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1668 attr.SecurityQualityOfService = NULL;
1670 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1671 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1672 else
1673 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1675 status = NtCreateMailslotFile( &handle, GENERIC_READ | GENERIC_WRITE, &attr,
1676 &iosb, 0, 0, nMaxMessageSize, &timeout );
1677 if (status)
1679 SetLastError( RtlNtStatusToDosError(status) );
1680 handle = INVALID_HANDLE_VALUE;
1683 RtlFreeUnicodeString( &nameW );
1684 return handle;
1688 /******************************************************************************
1689 * GetMailslotInfo [KERNEL32.@]
1691 * Retrieve information about a mailslot.
1693 * PARAMS
1694 * hMailslot [I] Mailslot handle
1695 * lpMaxMessageSize [O] Address of maximum message size
1696 * lpNextSize [O] Address of size of next message
1697 * lpMessageCount [O] Address of number of messages
1698 * lpReadTimeout [O] Address of read time-out
1700 * RETURNS
1701 * Success: TRUE
1702 * Failure: FALSE
1704 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1705 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1706 LPDWORD lpReadTimeout )
1708 FILE_MAILSLOT_QUERY_INFORMATION info;
1709 IO_STATUS_BLOCK iosb;
1710 NTSTATUS status;
1712 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1713 lpNextSize, lpMessageCount, lpReadTimeout);
1715 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1716 FileMailslotQueryInformation );
1718 if( status != STATUS_SUCCESS )
1720 SetLastError( RtlNtStatusToDosError(status) );
1721 return FALSE;
1724 if( lpMaxMessageSize )
1725 *lpMaxMessageSize = info.MaximumMessageSize;
1726 if( lpNextSize )
1727 *lpNextSize = info.NextMessageSize;
1728 if( lpMessageCount )
1729 *lpMessageCount = info.MessagesAvailable;
1730 if( lpReadTimeout )
1731 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1733 return TRUE;
1737 /******************************************************************************
1738 * SetMailslotInfo [KERNEL32.@]
1740 * Set the read timeout of a mailslot.
1742 * PARAMS
1743 * hMailslot [I] Mailslot handle
1744 * dwReadTimeout [I] Timeout in milliseconds.
1746 * RETURNS
1747 * Success: TRUE
1748 * Failure: FALSE
1750 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1752 FILE_MAILSLOT_SET_INFORMATION info;
1753 IO_STATUS_BLOCK iosb;
1754 NTSTATUS status;
1756 TRACE("%p %d\n", hMailslot, dwReadTimeout);
1758 info.ReadTimeout.QuadPart = dwReadTimeout * -10000;
1759 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1760 FileMailslotSetInformation );
1761 if( status != STATUS_SUCCESS )
1763 SetLastError( RtlNtStatusToDosError(status) );
1764 return FALSE;
1766 return TRUE;
1770 /******************************************************************************
1771 * CreateIoCompletionPort (KERNEL32.@)
1773 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1774 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1776 FIXME("(%p, %p, %08lx, %08x): stub.\n",
1777 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1778 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1779 return NULL;
1783 /******************************************************************************
1784 * GetQueuedCompletionStatus (KERNEL32.@)
1786 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1787 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1788 DWORD dwMilliseconds )
1790 FIXME("(%p,%p,%p,%p,%d), stub!\n",
1791 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1792 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1793 return FALSE;
1796 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
1797 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
1799 FIXME("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
1800 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1801 return FALSE;
1804 /******************************************************************************
1805 * CreateJobObjectW (KERNEL32.@)
1807 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1809 FIXME("%p %s\n", attr, debugstr_w(name) );
1810 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1811 return 0;
1814 /******************************************************************************
1815 * CreateJobObjectA (KERNEL32.@)
1817 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1819 LPWSTR str = NULL;
1820 UINT len;
1821 HANDLE r;
1823 TRACE("%p %s\n", attr, debugstr_a(name) );
1825 if( name )
1827 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1828 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1829 if( !str )
1831 SetLastError( ERROR_OUTOFMEMORY );
1832 return 0;
1834 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1837 r = CreateJobObjectW( attr, str );
1839 HeapFree( GetProcessHeap(), 0, str );
1841 return r;
1844 /******************************************************************************
1845 * AssignProcessToJobObject (KERNEL32.@)
1847 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1849 FIXME("%p %p\n", hJob, hProcess);
1850 return TRUE;
1853 #ifdef __i386__
1855 /***********************************************************************
1856 * InterlockedCompareExchange (KERNEL32.@)
1858 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1859 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1860 "movl 12(%esp),%eax\n\t"
1861 "movl 8(%esp),%ecx\n\t"
1862 "movl 4(%esp),%edx\n\t"
1863 "lock; cmpxchgl %ecx,(%edx)\n\t"
1864 "ret $12")
1866 /***********************************************************************
1867 * InterlockedExchange (KERNEL32.@)
1869 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1870 __ASM_GLOBAL_FUNC(InterlockedExchange,
1871 "movl 8(%esp),%eax\n\t"
1872 "movl 4(%esp),%edx\n\t"
1873 "lock; xchgl %eax,(%edx)\n\t"
1874 "ret $8")
1876 /***********************************************************************
1877 * InterlockedExchangeAdd (KERNEL32.@)
1879 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1880 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1881 "movl 8(%esp),%eax\n\t"
1882 "movl 4(%esp),%edx\n\t"
1883 "lock; xaddl %eax,(%edx)\n\t"
1884 "ret $8")
1886 /***********************************************************************
1887 * InterlockedIncrement (KERNEL32.@)
1889 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1890 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1891 "movl 4(%esp),%edx\n\t"
1892 "movl $1,%eax\n\t"
1893 "lock; xaddl %eax,(%edx)\n\t"
1894 "incl %eax\n\t"
1895 "ret $4")
1897 /***********************************************************************
1898 * InterlockedDecrement (KERNEL32.@)
1900 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1901 "movl 4(%esp),%edx\n\t"
1902 "movl $-1,%eax\n\t"
1903 "lock; xaddl %eax,(%edx)\n\t"
1904 "decl %eax\n\t"
1905 "ret $4")
1907 #else /* __i386__ */
1909 /***********************************************************************
1910 * InterlockedCompareExchange (KERNEL32.@)
1912 * Atomically swap one value with another.
1914 * PARAMS
1915 * dest [I/O] The value to replace
1916 * xchq [I] The value to be swapped
1917 * compare [I] The value to compare to dest
1919 * RETURNS
1920 * The resulting value of dest.
1922 * NOTES
1923 * dest is updated only if it is equal to compare, otherwise no swap is done.
1925 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
1927 return interlocked_cmpxchg( (int *)dest, xchg, compare );
1930 /***********************************************************************
1931 * InterlockedExchange (KERNEL32.@)
1933 * Atomically swap one value with another.
1935 * PARAMS
1936 * dest [I/O] The value to replace
1937 * val [I] The value to be swapped
1939 * RETURNS
1940 * The resulting value of dest.
1942 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
1944 return interlocked_xchg( (int *)dest, val );
1947 /***********************************************************************
1948 * InterlockedExchangeAdd (KERNEL32.@)
1950 * Atomically add one value to another.
1952 * PARAMS
1953 * dest [I/O] The value to add to
1954 * incr [I] The value to be added
1956 * RETURNS
1957 * The resulting value of dest.
1959 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
1961 return interlocked_xchg_add( (int *)dest, incr );
1964 /***********************************************************************
1965 * InterlockedIncrement (KERNEL32.@)
1967 * Atomically increment a value.
1969 * PARAMS
1970 * dest [I/O] The value to increment
1972 * RETURNS
1973 * The resulting value of dest.
1975 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
1977 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
1980 /***********************************************************************
1981 * InterlockedDecrement (KERNEL32.@)
1983 * Atomically decrement a value.
1985 * PARAMS
1986 * dest [I/O] The value to decrement
1988 * RETURNS
1989 * The resulting value of dest.
1991 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
1993 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
1996 #endif /* __i386__ */