Kernel32 should create named objects with OBJ_OPENIF flag set.
[wine/multimedia.git] / dlls / kernel / sync.c
blob809ab525bc66b9d8371945efc763014590eb48bf
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 #ifdef HAVE_SYS_IOCTL_H
30 #include <sys/ioctl.h>
31 #endif
32 #ifdef HAVE_POLL_H
33 #include <poll.h>
34 #endif
35 #ifdef HAVE_SYS_POLL_H
36 #include <sys/poll.h>
37 #endif
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
40 #endif
41 #include <stdarg.h>
42 #include <stdio.h>
44 #define NONAMELESSUNION
45 #define NONAMELESSSTRUCT
47 #include "ntstatus.h"
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winerror.h"
51 #include "winnls.h"
52 #include "winternl.h"
53 #include "winioctl.h"
55 #include "wine/server.h"
56 #include "wine/unicode.h"
57 #include "wine/winbase16.h"
58 #include "kernel_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(sync);
64 /* check if current version is NT or Win95 */
65 inline static int is_version_nt(void)
67 return !(GetVersion() & 0x80000000);
71 /***********************************************************************
72 * Sleep (KERNEL32.@)
74 VOID WINAPI Sleep( DWORD timeout )
76 SleepEx( timeout, FALSE );
79 /******************************************************************************
80 * SleepEx (KERNEL32.@)
82 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
84 NTSTATUS status;
86 if (timeout == INFINITE) status = NtDelayExecution( alertable, NULL );
87 else
89 LARGE_INTEGER time;
91 time.QuadPart = timeout * (ULONGLONG)10000;
92 time.QuadPart = -time.QuadPart;
93 status = NtDelayExecution( alertable, &time );
95 if (status != STATUS_USER_APC) status = STATUS_SUCCESS;
96 return status;
100 /***********************************************************************
101 * SwitchToThread (KERNEL32.@)
103 BOOL WINAPI SwitchToThread(void)
105 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
109 /***********************************************************************
110 * WaitForSingleObject (KERNEL32.@)
112 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
114 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
118 /***********************************************************************
119 * WaitForSingleObjectEx (KERNEL32.@)
121 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
122 BOOL alertable )
124 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
128 /***********************************************************************
129 * WaitForMultipleObjects (KERNEL32.@)
131 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
132 BOOL wait_all, DWORD timeout )
134 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
138 /***********************************************************************
139 * WaitForMultipleObjectsEx (KERNEL32.@)
141 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
142 BOOL wait_all, DWORD timeout,
143 BOOL alertable )
145 NTSTATUS status;
146 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
147 unsigned int i;
149 if (count >= MAXIMUM_WAIT_OBJECTS)
151 SetLastError(ERROR_INVALID_PARAMETER);
152 return WAIT_FAILED;
154 for (i = 0; i < count; i++)
156 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
157 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
158 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
159 hloc[i] = GetStdHandle( (DWORD)handles[i] );
160 else
161 hloc[i] = handles[i];
163 /* yes, even screen buffer console handles are waitable, and are
164 * handled as a handle to the console itself !!
166 if (is_console_handle(hloc[i]))
168 if (!VerifyConsoleIoHandle(hloc[i]))
170 return FALSE;
172 hloc[i] = GetConsoleInputWaitHandle();
176 if (timeout == INFINITE)
178 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, NULL );
180 else
182 LARGE_INTEGER time;
184 time.QuadPart = timeout * (ULONGLONG)10000;
185 time.QuadPart = -time.QuadPart;
186 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, &time );
189 if (HIWORD(status)) /* is it an error code? */
191 SetLastError( RtlNtStatusToDosError(status) );
192 status = WAIT_FAILED;
194 return status;
198 /***********************************************************************
199 * WaitForSingleObject (KERNEL.460)
201 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
203 DWORD retval, mutex_count;
205 ReleaseThunkLock( &mutex_count );
206 retval = WaitForSingleObject( handle, timeout );
207 RestoreThunkLock( mutex_count );
208 return retval;
211 /***********************************************************************
212 * WaitForMultipleObjects (KERNEL.461)
214 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
215 BOOL wait_all, DWORD timeout )
217 DWORD retval, mutex_count;
219 ReleaseThunkLock( &mutex_count );
220 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
221 RestoreThunkLock( mutex_count );
222 return retval;
225 /***********************************************************************
226 * WaitForMultipleObjectsEx (KERNEL.495)
228 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
229 BOOL wait_all, DWORD timeout, BOOL alertable )
231 DWORD retval, mutex_count;
233 ReleaseThunkLock( &mutex_count );
234 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
235 RestoreThunkLock( mutex_count );
236 return retval;
239 /***********************************************************************
240 * RegisterWaitForSingleObject (KERNEL32.@)
242 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
243 WAITORTIMERCALLBACK Callback, PVOID Context,
244 ULONG dwMilliseconds, ULONG dwFlags)
246 FIXME("%p %p %p %p %ld %ld\n",
247 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
248 return FALSE;
251 /***********************************************************************
252 * RegisterWaitForSingleObjectEx (KERNEL32.@)
254 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
255 WAITORTIMERCALLBACK Callback, PVOID Context,
256 ULONG dwMilliseconds, ULONG dwFlags )
258 FIXME("%p %p %p %ld %ld\n",
259 hObject,Callback,Context,dwMilliseconds,dwFlags);
260 return 0;
263 /***********************************************************************
264 * UnregisterWait (KERNEL32.@)
266 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
268 FIXME("%p\n",WaitHandle);
269 return FALSE;
272 /***********************************************************************
273 * UnregisterWaitEx (KERNEL32.@)
275 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
277 FIXME("%p %p\n",WaitHandle, CompletionEvent);
278 return FALSE;
281 /***********************************************************************
282 * SignalObjectAndWait (KERNEL32.@)
284 * Allows to atomically signal any of the synchro objects (semaphore,
285 * mutex, event) and wait on another.
287 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
288 DWORD dwMilliseconds, BOOL bAlertable )
290 NTSTATUS status;
291 LARGE_INTEGER timeout, *ptimeout = NULL;
293 TRACE("%p %p %ld %d\n", hObjectToSignal,
294 hObjectToWaitOn, dwMilliseconds, bAlertable);
296 if (dwMilliseconds != INFINITE)
298 timeout.QuadPart = dwMilliseconds * (ULONGLONG)10000;
299 timeout.QuadPart = -timeout.QuadPart;
300 ptimeout = &timeout;
303 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn,
304 bAlertable, ptimeout );
305 if (HIWORD(status))
307 SetLastError( RtlNtStatusToDosError(status) );
308 status = WAIT_FAILED;
310 return status;
313 /***********************************************************************
314 * InitializeCriticalSection (KERNEL32.@)
316 * Initialise a critical section before use.
318 * PARAMS
319 * crit [O] Critical section to initialise.
321 * RETURNS
322 * Nothing. If the function fails an exception is raised.
324 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
326 NTSTATUS ret = RtlInitializeCriticalSection( crit );
327 if (ret) RtlRaiseStatus( ret );
330 /***********************************************************************
331 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
333 * Initialise a critical section with a spin count.
335 * PARAMS
336 * crit [O] Critical section to initialise.
337 * spincount [I] Number of times to spin upon contention.
339 * RETURNS
340 * Success: TRUE.
341 * Failure: Nothing. If the function fails an exception is raised.
343 * NOTES
344 * spincount is ignored on uni-processor systems.
346 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
348 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
349 if (ret) RtlRaiseStatus( ret );
350 return !ret;
353 /***********************************************************************
354 * MakeCriticalSectionGlobal (KERNEL32.@)
356 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
358 /* let's assume that only one thread at a time will try to do this */
359 HANDLE sem = crit->LockSemaphore;
360 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
361 crit->LockSemaphore = ConvertToGlobalHandle( sem );
362 if (crit->DebugInfo)
364 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
365 crit->DebugInfo = NULL;
370 /***********************************************************************
371 * ReinitializeCriticalSection (KERNEL32.@)
373 * Initialise an already used critical section.
375 * PARAMS
376 * crit [O] Critical section to initialise.
378 * RETURNS
379 * Nothing.
381 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
383 if ( !crit->LockSemaphore )
384 RtlInitializeCriticalSection( crit );
388 /***********************************************************************
389 * UninitializeCriticalSection (KERNEL32.@)
391 * UnInitialise a critical section after use.
393 * PARAMS
394 * crit [O] Critical section to uninitialise (destroy).
396 * RETURNS
397 * Nothing.
399 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
401 RtlDeleteCriticalSection( crit );
405 /***********************************************************************
406 * CreateEventA (KERNEL32.@)
408 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
409 BOOL initial_state, LPCSTR name )
411 WCHAR buffer[MAX_PATH];
413 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
415 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
417 SetLastError( ERROR_FILENAME_EXCED_RANGE );
418 return 0;
420 return CreateEventW( sa, manual_reset, initial_state, buffer );
424 /***********************************************************************
425 * CreateEventW (KERNEL32.@)
427 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
428 BOOL initial_state, LPCWSTR name )
430 HANDLE ret;
431 UNICODE_STRING nameW;
432 OBJECT_ATTRIBUTES attr;
433 NTSTATUS status;
435 /* one buggy program needs this
436 * ("Van Dale Groot woordenboek der Nederlandse taal")
438 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
440 ERR("Bad security attributes pointer %p\n",sa);
441 SetLastError( ERROR_INVALID_PARAMETER);
442 return 0;
445 attr.Length = sizeof(attr);
446 attr.RootDirectory = 0;
447 attr.ObjectName = NULL;
448 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
449 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
450 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
451 attr.SecurityQualityOfService = NULL;
452 if (name)
454 RtlInitUnicodeString( &nameW, name );
455 attr.ObjectName = &nameW;
458 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
459 if (status == STATUS_OBJECT_NAME_EXISTS)
460 SetLastError( ERROR_ALREADY_EXISTS );
461 else
462 SetLastError( RtlNtStatusToDosError(status) );
463 return ret;
467 /***********************************************************************
468 * CreateW32Event (KERNEL.457)
470 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
472 return CreateEventW( NULL, manual_reset, initial_state, NULL );
476 /***********************************************************************
477 * OpenEventA (KERNEL32.@)
479 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
481 WCHAR buffer[MAX_PATH];
483 if (!name) return OpenEventW( access, inherit, NULL );
485 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
487 SetLastError( ERROR_FILENAME_EXCED_RANGE );
488 return 0;
490 return OpenEventW( access, inherit, buffer );
494 /***********************************************************************
495 * OpenEventW (KERNEL32.@)
497 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
499 HANDLE ret;
500 UNICODE_STRING nameW;
501 OBJECT_ATTRIBUTES attr;
502 NTSTATUS status;
504 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
506 attr.Length = sizeof(attr);
507 attr.RootDirectory = 0;
508 attr.ObjectName = NULL;
509 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
510 attr.SecurityDescriptor = NULL;
511 attr.SecurityQualityOfService = NULL;
512 if (name)
514 RtlInitUnicodeString( &nameW, name );
515 attr.ObjectName = &nameW;
518 status = NtOpenEvent( &ret, access, &attr );
519 if (status != STATUS_SUCCESS)
521 SetLastError( RtlNtStatusToDosError(status) );
522 return 0;
524 return ret;
527 /***********************************************************************
528 * PulseEvent (KERNEL32.@)
530 BOOL WINAPI PulseEvent( HANDLE handle )
532 NTSTATUS status;
534 if ((status = NtPulseEvent( handle, NULL )))
535 SetLastError( RtlNtStatusToDosError(status) );
536 return !status;
540 /***********************************************************************
541 * SetW32Event (KERNEL.458)
542 * SetEvent (KERNEL32.@)
544 BOOL WINAPI SetEvent( HANDLE handle )
546 NTSTATUS status;
548 if ((status = NtSetEvent( handle, NULL )))
549 SetLastError( RtlNtStatusToDosError(status) );
550 return !status;
554 /***********************************************************************
555 * ResetW32Event (KERNEL.459)
556 * ResetEvent (KERNEL32.@)
558 BOOL WINAPI ResetEvent( HANDLE handle )
560 NTSTATUS status;
562 if ((status = NtResetEvent( handle, NULL )))
563 SetLastError( RtlNtStatusToDosError(status) );
564 return !status;
568 /***********************************************************************
569 * NOTE: The Win95 VWin32_Event routines given below are really low-level
570 * routines implemented directly by VWin32. The user-mode libraries
571 * implement Win32 synchronisation routines on top of these low-level
572 * primitives. We do it the other way around here :-)
575 /***********************************************************************
576 * VWin32_EventCreate (KERNEL.442)
578 HANDLE WINAPI VWin32_EventCreate(VOID)
580 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
581 return ConvertToGlobalHandle( hEvent );
584 /***********************************************************************
585 * VWin32_EventDestroy (KERNEL.443)
587 VOID WINAPI VWin32_EventDestroy(HANDLE event)
589 CloseHandle( event );
592 /***********************************************************************
593 * VWin32_EventWait (KERNEL.450)
595 VOID WINAPI VWin32_EventWait(HANDLE event)
597 DWORD mutex_count;
599 ReleaseThunkLock( &mutex_count );
600 WaitForSingleObject( event, INFINITE );
601 RestoreThunkLock( mutex_count );
604 /***********************************************************************
605 * VWin32_EventSet (KERNEL.451)
606 * KERNEL_479 (KERNEL.479)
608 VOID WINAPI VWin32_EventSet(HANDLE event)
610 SetEvent( event );
615 /***********************************************************************
616 * CreateMutexA (KERNEL32.@)
618 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
620 WCHAR buffer[MAX_PATH];
622 if (!name) return CreateMutexW( sa, owner, NULL );
624 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
626 SetLastError( ERROR_FILENAME_EXCED_RANGE );
627 return 0;
629 return CreateMutexW( sa, owner, buffer );
633 /***********************************************************************
634 * CreateMutexW (KERNEL32.@)
636 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
638 HANDLE ret;
639 UNICODE_STRING nameW;
640 OBJECT_ATTRIBUTES attr;
641 NTSTATUS status;
643 attr.Length = sizeof(attr);
644 attr.RootDirectory = 0;
645 attr.ObjectName = NULL;
646 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
647 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
648 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
649 attr.SecurityQualityOfService = NULL;
650 if (name)
652 RtlInitUnicodeString( &nameW, name );
653 attr.ObjectName = &nameW;
656 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
657 if (status == STATUS_OBJECT_NAME_EXISTS)
658 SetLastError( ERROR_ALREADY_EXISTS );
659 else
660 SetLastError( RtlNtStatusToDosError(status) );
661 return ret;
665 /***********************************************************************
666 * OpenMutexA (KERNEL32.@)
668 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
670 WCHAR buffer[MAX_PATH];
672 if (!name) return OpenMutexW( access, inherit, NULL );
674 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
676 SetLastError( ERROR_FILENAME_EXCED_RANGE );
677 return 0;
679 return OpenMutexW( access, inherit, buffer );
683 /***********************************************************************
684 * OpenMutexW (KERNEL32.@)
686 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
688 HANDLE ret;
689 UNICODE_STRING nameW;
690 OBJECT_ATTRIBUTES attr;
691 NTSTATUS status;
693 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
695 attr.Length = sizeof(attr);
696 attr.RootDirectory = 0;
697 attr.ObjectName = NULL;
698 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
699 attr.SecurityDescriptor = NULL;
700 attr.SecurityQualityOfService = NULL;
701 if (name)
703 RtlInitUnicodeString( &nameW, name );
704 attr.ObjectName = &nameW;
707 status = NtOpenMutant( &ret, access, &attr );
708 if (status != STATUS_SUCCESS)
710 SetLastError( RtlNtStatusToDosError(status) );
711 return 0;
713 return ret;
717 /***********************************************************************
718 * ReleaseMutex (KERNEL32.@)
720 BOOL WINAPI ReleaseMutex( HANDLE handle )
722 NTSTATUS status;
724 status = NtReleaseMutant(handle, NULL);
725 if (status != STATUS_SUCCESS)
727 SetLastError( RtlNtStatusToDosError(status) );
728 return FALSE;
730 return TRUE;
735 * Semaphores
739 /***********************************************************************
740 * CreateSemaphoreA (KERNEL32.@)
742 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
744 WCHAR buffer[MAX_PATH];
746 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
748 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
750 SetLastError( ERROR_FILENAME_EXCED_RANGE );
751 return 0;
753 return CreateSemaphoreW( sa, initial, max, buffer );
757 /***********************************************************************
758 * CreateSemaphoreW (KERNEL32.@)
760 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
761 LONG max, LPCWSTR name )
763 HANDLE ret;
764 UNICODE_STRING nameW;
765 OBJECT_ATTRIBUTES attr;
766 NTSTATUS status;
768 attr.Length = sizeof(attr);
769 attr.RootDirectory = 0;
770 attr.ObjectName = NULL;
771 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
772 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
773 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
774 attr.SecurityQualityOfService = NULL;
775 if (name)
777 RtlInitUnicodeString( &nameW, name );
778 attr.ObjectName = &nameW;
781 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
782 if (status == STATUS_OBJECT_NAME_EXISTS)
783 SetLastError( ERROR_ALREADY_EXISTS );
784 else
785 SetLastError( RtlNtStatusToDosError(status) );
786 return ret;
790 /***********************************************************************
791 * OpenSemaphoreA (KERNEL32.@)
793 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
795 WCHAR buffer[MAX_PATH];
797 if (!name) return OpenSemaphoreW( access, inherit, NULL );
799 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
801 SetLastError( ERROR_FILENAME_EXCED_RANGE );
802 return 0;
804 return OpenSemaphoreW( access, inherit, buffer );
808 /***********************************************************************
809 * OpenSemaphoreW (KERNEL32.@)
811 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
813 HANDLE ret;
814 UNICODE_STRING nameW;
815 OBJECT_ATTRIBUTES attr;
816 NTSTATUS status;
818 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
820 attr.Length = sizeof(attr);
821 attr.RootDirectory = 0;
822 attr.ObjectName = NULL;
823 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
824 attr.SecurityDescriptor = NULL;
825 attr.SecurityQualityOfService = NULL;
826 if (name)
828 RtlInitUnicodeString( &nameW, name );
829 attr.ObjectName = &nameW;
832 status = NtOpenSemaphore( &ret, access, &attr );
833 if (status != STATUS_SUCCESS)
835 SetLastError( RtlNtStatusToDosError(status) );
836 return 0;
838 return ret;
842 /***********************************************************************
843 * ReleaseSemaphore (KERNEL32.@)
845 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
847 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
848 if (status) SetLastError( RtlNtStatusToDosError(status) );
849 return !status;
854 * Timers
858 /***********************************************************************
859 * CreateWaitableTimerA (KERNEL32.@)
861 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
863 WCHAR buffer[MAX_PATH];
865 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
867 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
869 SetLastError( ERROR_FILENAME_EXCED_RANGE );
870 return 0;
872 return CreateWaitableTimerW( sa, manual, buffer );
876 /***********************************************************************
877 * CreateWaitableTimerW (KERNEL32.@)
879 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
881 HANDLE handle;
882 NTSTATUS status;
883 UNICODE_STRING nameW;
884 OBJECT_ATTRIBUTES attr;
886 attr.Length = sizeof(attr);
887 attr.RootDirectory = 0;
888 attr.ObjectName = NULL;
889 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
890 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
891 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
892 attr.SecurityQualityOfService = NULL;
893 if (name)
895 RtlInitUnicodeString( &nameW, name );
896 attr.ObjectName = &nameW;
899 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
900 manual ? NotificationTimer : SynchronizationTimer);
901 if (status == STATUS_OBJECT_NAME_EXISTS)
902 SetLastError( ERROR_ALREADY_EXISTS );
903 else
904 SetLastError( RtlNtStatusToDosError(status) );
905 return handle;
909 /***********************************************************************
910 * OpenWaitableTimerA (KERNEL32.@)
912 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
914 WCHAR buffer[MAX_PATH];
916 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
918 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
920 SetLastError( ERROR_FILENAME_EXCED_RANGE );
921 return 0;
923 return OpenWaitableTimerW( access, inherit, buffer );
927 /***********************************************************************
928 * OpenWaitableTimerW (KERNEL32.@)
930 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
932 HANDLE handle;
933 UNICODE_STRING nameW;
934 OBJECT_ATTRIBUTES attr;
935 NTSTATUS status;
937 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
939 attr.Length = sizeof(attr);
940 attr.RootDirectory = 0;
941 attr.ObjectName = NULL;
942 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
943 attr.SecurityDescriptor = NULL;
944 attr.SecurityQualityOfService = NULL;
945 if (name)
947 RtlInitUnicodeString( &nameW, name );
948 attr.ObjectName = &nameW;
951 status = NtOpenTimer(&handle, access, &attr);
952 if (status != STATUS_SUCCESS)
954 SetLastError( RtlNtStatusToDosError(status) );
955 return 0;
957 return handle;
961 /***********************************************************************
962 * SetWaitableTimer (KERNEL32.@)
964 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
965 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
967 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
968 arg, resume, period, NULL);
970 if (status != STATUS_SUCCESS)
972 SetLastError( RtlNtStatusToDosError(status) );
973 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
975 return TRUE;
979 /***********************************************************************
980 * CancelWaitableTimer (KERNEL32.@)
982 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
984 NTSTATUS status;
986 status = NtCancelTimer(handle, NULL);
987 if (status != STATUS_SUCCESS)
989 SetLastError( RtlNtStatusToDosError(status) );
990 return FALSE;
992 return TRUE;
996 /***********************************************************************
997 * CreateTimerQueue (KERNEL32.@)
999 HANDLE WINAPI CreateTimerQueue(void)
1001 FIXME("stub\n");
1002 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1003 return NULL;
1007 /***********************************************************************
1008 * DeleteTimerQueueEx (KERNEL32.@)
1010 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1012 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
1013 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1014 return 0;
1017 /***********************************************************************
1018 * CreateTimerQueueTimer (KERNEL32.@)
1020 * Creates a timer-queue timer. This timer expires at the specified due
1021 * time (in ms), then after every specified period (in ms). When the timer
1022 * expires, the callback function is called.
1024 * RETURNS
1025 * nonzero on success or zero on faillure
1027 * BUGS
1028 * Unimplemented
1030 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1031 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1032 DWORD DueTime, DWORD Period, ULONG Flags )
1034 FIXME("stub\n");
1035 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1036 return TRUE;
1039 /***********************************************************************
1040 * DeleteTimerQueueTimer (KERNEL32.@)
1042 * Cancels a timer-queue timer.
1044 * RETURNS
1045 * nonzero on success or zero on faillure
1047 * BUGS
1048 * Unimplemented
1050 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1051 HANDLE CompletionEvent )
1053 FIXME("stub\n");
1054 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1055 return TRUE;
1060 * Pipes
1064 /***********************************************************************
1065 * CreateNamedPipeA (KERNEL32.@)
1067 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1068 DWORD dwPipeMode, DWORD nMaxInstances,
1069 DWORD nOutBufferSize, DWORD nInBufferSize,
1070 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1072 WCHAR buffer[MAX_PATH];
1074 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1075 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1077 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1079 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1080 return INVALID_HANDLE_VALUE;
1082 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1083 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1087 /***********************************************************************
1088 * CreateNamedPipeW (KERNEL32.@)
1090 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1091 DWORD dwPipeMode, DWORD nMaxInstances,
1092 DWORD nOutBufferSize, DWORD nInBufferSize,
1093 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1095 HANDLE handle;
1096 UNICODE_STRING nt_name;
1097 OBJECT_ATTRIBUTES attr;
1098 DWORD options;
1099 BOOLEAN pipe_type, read_mode, non_block;
1100 NTSTATUS status;
1101 IO_STATUS_BLOCK iosb;
1102 LARGE_INTEGER timeout;
1104 TRACE("(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n",
1105 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1106 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1108 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1110 SetLastError( ERROR_PATH_NOT_FOUND );
1111 return INVALID_HANDLE_VALUE;
1113 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1115 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1116 RtlFreeUnicodeString( &nt_name );
1117 return INVALID_HANDLE_VALUE;
1120 attr.Length = sizeof(attr);
1121 attr.RootDirectory = 0;
1122 attr.ObjectName = &nt_name;
1123 attr.Attributes = OBJ_CASE_INSENSITIVE |
1124 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1125 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1126 attr.SecurityQualityOfService = NULL;
1128 options = 0;
1129 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1130 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1131 if ((dwOpenMode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
1132 options |= FILE_PIPE_FULL_DUPLEX;
1133 else if (dwOpenMode & PIPE_ACCESS_INBOUND) options |= FILE_PIPE_INBOUND;
1134 else if (dwOpenMode & PIPE_ACCESS_OUTBOUND) options |= FILE_PIPE_OUTBOUND;
1135 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1136 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1137 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1138 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0UL;
1140 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1142 SetLastError(0);
1144 status = NtCreateNamedPipeFile(&handle, GENERIC_READ|GENERIC_WRITE, &attr, &iosb,
1145 0, FILE_OVERWRITE_IF, options, pipe_type,
1146 read_mode, non_block, nMaxInstances,
1147 nInBufferSize, nOutBufferSize, &timeout);
1149 RtlFreeUnicodeString( &nt_name );
1150 if (status)
1152 handle = INVALID_HANDLE_VALUE;
1153 SetLastError( RtlNtStatusToDosError(status) );
1155 return handle;
1159 /***********************************************************************
1160 * PeekNamedPipe (KERNEL32.@)
1162 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1163 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1165 #ifdef FIONREAD
1166 int avail=0, fd, ret, flags;
1168 TRACE("(%p,%p,%lu,%p,%p,%p)\n", hPipe, lpvBuffer, cbBuffer, lpcbRead, lpcbAvail, lpcbMessage);
1170 ret = wine_server_handle_to_fd( hPipe, GENERIC_READ, &fd, &flags );
1171 if (ret)
1173 SetLastError( RtlNtStatusToDosError(ret) );
1174 return FALSE;
1176 if (flags & FD_FLAG_RECV_SHUTDOWN)
1178 wine_server_release_fd( hPipe, fd );
1179 SetLastError ( ERROR_PIPE_NOT_CONNECTED );
1180 return FALSE;
1183 if (ioctl(fd,FIONREAD, &avail ) != 0)
1185 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1186 wine_server_release_fd( hPipe, fd );
1187 return FALSE;
1189 if (!avail) /* check for closed pipe */
1191 struct pollfd pollfd;
1192 pollfd.fd = fd;
1193 pollfd.events = POLLIN;
1194 pollfd.revents = 0;
1195 switch (poll( &pollfd, 1, 0 ))
1197 case 0:
1198 break;
1199 case 1: /* got something */
1200 if (!(pollfd.revents & (POLLHUP | POLLERR))) break;
1201 TRACE("POLLHUP | POLLERR\n");
1202 /* fall through */
1203 case -1:
1204 wine_server_release_fd( hPipe, fd );
1205 SetLastError(ERROR_BROKEN_PIPE);
1206 return FALSE;
1209 TRACE(" 0x%08x bytes available\n", avail );
1210 ret = TRUE;
1211 if (lpcbAvail)
1212 *lpcbAvail = avail;
1213 if (lpcbRead)
1214 *lpcbRead = 0;
1215 if (avail && lpvBuffer && cbBuffer)
1217 int readbytes = (avail < cbBuffer) ? avail : cbBuffer;
1218 readbytes = recv(fd, lpvBuffer, readbytes, MSG_PEEK);
1219 if (readbytes < 0)
1221 WARN("failed to peek socket (%d)\n", errno);
1222 ret = FALSE;
1224 else if (lpcbRead)
1225 *lpcbRead = readbytes;
1227 wine_server_release_fd( hPipe, fd );
1228 return ret;
1229 #endif /* defined(FIONREAD) */
1231 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1232 FIXME("function not implemented\n");
1233 return FALSE;
1236 /***********************************************************************
1237 * PIPE_CompletionWait (Internal)
1239 static void CALLBACK PIPE_CompletionWait(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
1241 LPOVERLAPPED ovlp = (LPOVERLAPPED)user;
1243 TRACE("for %p/%p, status=%08lx\n", ovlp, iosb, status);
1245 if (ovlp)
1247 ovlp->Internal = status;
1248 SetEvent(ovlp->hEvent);
1250 TRACE("done\n");
1253 /***********************************************************************
1254 * WaitNamedPipeA (KERNEL32.@)
1256 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1258 WCHAR buffer[MAX_PATH];
1260 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1262 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1264 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1265 return 0;
1267 return WaitNamedPipeW( buffer, nTimeOut );
1271 /***********************************************************************
1272 * WaitNamedPipeW (KERNEL32.@)
1274 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1276 BOOL ret;
1277 OVERLAPPED ov;
1278 UNICODE_STRING nt_name;
1279 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1281 TRACE("%s 0x%08lx\n",debugstr_w(name),nTimeOut);
1283 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1284 return FALSE;
1286 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1288 RtlFreeUnicodeString( &nt_name );
1289 return FALSE;
1291 if (nt_name.Length < sizeof(leadin) ||
1292 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(leadin[0])))
1294 RtlFreeUnicodeString( &nt_name );
1295 return FALSE;
1298 memset(&ov,0,sizeof(ov));
1299 ov.hEvent = CreateEventW( NULL, 0, 0, NULL );
1300 if (!ov.hEvent)
1301 return FALSE;
1303 SERVER_START_REQ( wait_named_pipe )
1305 req->timeout = nTimeOut;
1306 req->overlapped = &ov;
1307 req->func = PIPE_CompletionWait;
1308 wine_server_add_data( req, nt_name.Buffer + 4, nt_name.Length - 4*sizeof(WCHAR) );
1309 ret = !wine_server_call_err( req );
1311 SERVER_END_REQ;
1313 RtlFreeUnicodeString( &nt_name );
1315 if(ret)
1317 if (WAIT_OBJECT_0==WaitForSingleObject(ov.hEvent,INFINITE))
1319 SetLastError(RtlNtStatusToDosError(ov.Internal));
1320 ret = (ov.Internal==STATUS_SUCCESS);
1323 CloseHandle(ov.hEvent);
1324 return ret;
1328 /***********************************************************************
1329 * ConnectNamedPipe (KERNEL32.@)
1331 * Connects to a named pipe
1333 * Parameters
1334 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1335 * overlapped: Optional OVERLAPPED struct
1337 * Return values
1338 * TRUE: Success
1339 * FALSE: Failure, GetLastError can be called for further details
1341 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1343 NTSTATUS status;
1344 IO_STATUS_BLOCK status_block;
1346 TRACE("(%p,%p)\n", hPipe, overlapped);
1348 if(overlapped)
1349 overlapped->Internal = STATUS_PENDING;
1351 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, NULL,
1352 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1353 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1355 if (status == STATUS_SUCCESS) return TRUE;
1356 SetLastError( RtlNtStatusToDosError(status) );
1357 return FALSE;
1360 /***********************************************************************
1361 * DisconnectNamedPipe (KERNEL32.@)
1363 * Disconnects from a named pipe
1365 * Parameters
1366 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1368 * Return values
1369 * TRUE: Success
1370 * FALSE: Failure, GetLastError can be called for further details
1372 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1374 NTSTATUS status;
1375 IO_STATUS_BLOCK io_block;
1377 TRACE("(%p)\n",hPipe);
1379 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1380 NULL, 0, NULL, 0);
1381 if (status == STATUS_SUCCESS) return TRUE;
1382 SetLastError( RtlNtStatusToDosError(status) );
1383 return FALSE;
1386 /***********************************************************************
1387 * TransactNamedPipe (KERNEL32.@)
1389 * BUGS
1390 * should be done as a single operation in the wineserver or kernel
1392 BOOL WINAPI TransactNamedPipe(
1393 HANDLE handle, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1394 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1396 BOOL r;
1397 DWORD count;
1399 TRACE("%p %p %ld %p %ld %p %p\n",
1400 handle, lpInput, dwInputSize, lpOutput,
1401 dwOutputSize, lpBytesRead, lpOverlapped);
1403 if (lpOverlapped)
1405 FIXME("Doesn't support overlapped operation as yet\n");
1406 return FALSE;
1409 r = WriteFile(handle, lpOutput, dwOutputSize, &count, NULL);
1410 if (r)
1411 r = ReadFile(handle, lpInput, dwInputSize, lpBytesRead, NULL);
1413 return r;
1416 /***********************************************************************
1417 * GetNamedPipeInfo (KERNEL32.@)
1419 BOOL WINAPI GetNamedPipeInfo(
1420 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1421 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1423 BOOL ret;
1425 TRACE("%p %p %p %p %p\n", hNamedPipe, lpFlags,
1426 lpOutputBufferSize, lpInputBufferSize, lpMaxInstances);
1428 SERVER_START_REQ( get_named_pipe_info )
1430 req->handle = hNamedPipe;
1431 ret = !wine_server_call_err( req );
1432 if (lpFlags)
1434 *lpFlags = 0;
1435 if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)
1436 *lpFlags |= PIPE_TYPE_MESSAGE;
1437 if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
1438 *lpFlags |= PIPE_READMODE_MESSAGE;
1439 if (reply->flags & NAMED_PIPE_NONBLOCKING_MODE)
1440 *lpFlags |= PIPE_NOWAIT;
1442 if (lpOutputBufferSize) *lpOutputBufferSize = reply->outsize;
1443 if (lpInputBufferSize) *lpInputBufferSize = reply->outsize;
1444 if (lpMaxInstances) *lpMaxInstances = reply->maxinstances;
1446 SERVER_END_REQ;
1448 return ret;
1451 /***********************************************************************
1452 * GetNamedPipeHandleStateA (KERNEL32.@)
1454 BOOL WINAPI GetNamedPipeHandleStateA(
1455 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1456 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1457 LPSTR lpUsername, DWORD nUsernameMaxSize)
1459 FIXME("%p %p %p %p %p %p %ld\n",
1460 hNamedPipe, lpState, lpCurInstances,
1461 lpMaxCollectionCount, lpCollectDataTimeout,
1462 lpUsername, nUsernameMaxSize);
1464 return FALSE;
1467 /***********************************************************************
1468 * GetNamedPipeHandleStateW (KERNEL32.@)
1470 BOOL WINAPI GetNamedPipeHandleStateW(
1471 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1472 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1473 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1475 FIXME("%p %p %p %p %p %p %ld\n",
1476 hNamedPipe, lpState, lpCurInstances,
1477 lpMaxCollectionCount, lpCollectDataTimeout,
1478 lpUsername, nUsernameMaxSize);
1480 return FALSE;
1483 /***********************************************************************
1484 * SetNamedPipeHandleState (KERNEL32.@)
1486 BOOL WINAPI SetNamedPipeHandleState(
1487 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1488 LPDWORD lpCollectDataTimeout)
1490 /* should be a fixme, but this function is called a lot by the RPC
1491 * runtime, and it slows down InstallShield a fair bit. */
1492 WARN("stub: %p %p/%ld %p %p\n",
1493 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1494 return FALSE;
1497 /***********************************************************************
1498 * CallNamedPipeA (KERNEL32.@)
1500 BOOL WINAPI CallNamedPipeA(
1501 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1502 LPVOID lpOutput, DWORD dwOutputSize,
1503 LPDWORD lpBytesRead, DWORD nTimeout)
1505 DWORD len;
1506 LPWSTR str = NULL;
1507 BOOL ret;
1509 TRACE("%s %p %ld %p %ld %p %ld\n",
1510 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1511 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1513 if( lpNamedPipeName )
1515 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1516 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1517 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1519 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1520 dwOutputSize, lpBytesRead, nTimeout );
1521 if( lpNamedPipeName )
1522 HeapFree( GetProcessHeap(), 0, str );
1524 return ret;
1527 /***********************************************************************
1528 * CallNamedPipeW (KERNEL32.@)
1530 BOOL WINAPI CallNamedPipeW(
1531 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1532 LPVOID lpOutput, DWORD lpOutputSize,
1533 LPDWORD lpBytesRead, DWORD nTimeout)
1535 FIXME("%s %p %ld %p %ld %p %ld\n",
1536 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1537 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1538 return FALSE;
1541 /******************************************************************
1542 * CreatePipe (KERNEL32.@)
1545 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1546 LPSECURITY_ATTRIBUTES sa, DWORD size )
1548 static unsigned index /* = 0 */;
1549 WCHAR name[64];
1550 HANDLE hr, hw;
1551 unsigned in_index = index;
1552 UNICODE_STRING nt_name;
1553 OBJECT_ATTRIBUTES attr;
1554 NTSTATUS status;
1555 IO_STATUS_BLOCK iosb;
1556 LARGE_INTEGER timeout;
1558 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1560 attr.Length = sizeof(attr);
1561 attr.RootDirectory = 0;
1562 attr.ObjectName = &nt_name;
1563 attr.Attributes = OBJ_CASE_INSENSITIVE |
1564 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1565 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1566 attr.SecurityQualityOfService = NULL;
1568 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1569 /* generate a unique pipe name (system wide) */
1572 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1573 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1574 'u','.','%','0','8','u','\0' };
1576 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1577 GetCurrentProcessId(), ++index);
1578 RtlInitUnicodeString(&nt_name, name);
1579 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1580 0, FILE_OVERWRITE_IF,
1581 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1582 FALSE, FALSE, FALSE,
1583 1, size, size, &timeout);
1584 if (status)
1586 SetLastError( RtlNtStatusToDosError(status) );
1587 hr = INVALID_HANDLE_VALUE;
1589 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1590 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1591 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1593 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1594 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1596 if (status)
1598 SetLastError( RtlNtStatusToDosError(status) );
1599 NtClose(hr);
1600 return FALSE;
1603 *hReadPipe = hr;
1604 *hWritePipe = hw;
1605 return TRUE;
1609 /******************************************************************************
1610 * CreateMailslotA [KERNEL32.@]
1612 * See CreatMailslotW.
1614 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1615 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1617 DWORD len;
1618 HANDLE handle;
1619 LPWSTR name = NULL;
1621 TRACE("%s %ld %ld %p\n", debugstr_a(lpName),
1622 nMaxMessageSize, lReadTimeout, sa);
1624 if( lpName )
1626 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1627 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1628 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1631 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1633 HeapFree( GetProcessHeap(), 0, name );
1635 return handle;
1639 /******************************************************************************
1640 * CreateMailslotW [KERNEL32.@]
1642 * Create a mailslot with specified name.
1644 * PARAMS
1645 * lpName [I] Pointer to string for mailslot name
1646 * nMaxMessageSize [I] Maximum message size
1647 * lReadTimeout [I] Milliseconds before read time-out
1648 * sa [I] Pointer to security structure
1650 * RETURNS
1651 * Success: Handle to mailslot
1652 * Failure: INVALID_HANDLE_VALUE
1654 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1655 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1657 HANDLE handle = INVALID_HANDLE_VALUE;
1658 OBJECT_ATTRIBUTES attr;
1659 UNICODE_STRING nameW;
1660 LARGE_INTEGER timeout;
1661 IO_STATUS_BLOCK iosb;
1662 NTSTATUS status;
1664 TRACE("%s %ld %ld %p\n", debugstr_w(lpName),
1665 nMaxMessageSize, lReadTimeout, sa);
1667 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1669 SetLastError( ERROR_PATH_NOT_FOUND );
1670 return INVALID_HANDLE_VALUE;
1673 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1675 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1676 RtlFreeUnicodeString( &nameW );
1677 return INVALID_HANDLE_VALUE;
1680 attr.Length = sizeof(attr);
1681 attr.RootDirectory = 0;
1682 attr.Attributes = OBJ_CASE_INSENSITIVE;
1683 attr.ObjectName = &nameW;
1684 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1685 attr.SecurityQualityOfService = NULL;
1687 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1688 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1689 else
1690 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1692 status = NtCreateMailslotFile( &handle, GENERIC_READ | GENERIC_WRITE, &attr,
1693 &iosb, 0, 0, nMaxMessageSize, &timeout );
1694 if (status)
1696 SetLastError( RtlNtStatusToDosError(status) );
1697 handle = INVALID_HANDLE_VALUE;
1700 RtlFreeUnicodeString( &nameW );
1701 return handle;
1705 /******************************************************************************
1706 * GetMailslotInfo [KERNEL32.@]
1708 * Retrieve information about a mailslot.
1710 * PARAMS
1711 * hMailslot [I] Mailslot handle
1712 * lpMaxMessageSize [O] Address of maximum message size
1713 * lpNextSize [O] Address of size of next message
1714 * lpMessageCount [O] Address of number of messages
1715 * lpReadTimeout [O] Address of read time-out
1717 * RETURNS
1718 * Success: TRUE
1719 * Failure: FALSE
1721 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1722 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1723 LPDWORD lpReadTimeout )
1725 FILE_MAILSLOT_QUERY_INFORMATION info;
1726 IO_STATUS_BLOCK iosb;
1727 NTSTATUS status;
1729 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1730 lpNextSize, lpMessageCount, lpReadTimeout);
1732 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1733 FileMailslotQueryInformation );
1735 if( status != STATUS_SUCCESS )
1737 SetLastError( RtlNtStatusToDosError(status) );
1738 return FALSE;
1741 if( lpMaxMessageSize )
1742 *lpMaxMessageSize = info.MaximumMessageSize;
1743 if( lpNextSize )
1744 *lpNextSize = info.NextMessageSize;
1745 if( lpMessageCount )
1746 *lpMessageCount = info.MessagesAvailable;
1747 if( lpReadTimeout )
1748 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1750 return TRUE;
1754 /******************************************************************************
1755 * SetMailslotInfo [KERNEL32.@]
1757 * Set the read timeout of a mailslot.
1759 * PARAMS
1760 * hMailslot [I] Mailslot handle
1761 * dwReadTimeout [I] Timeout in milliseconds.
1763 * RETURNS
1764 * Success: TRUE
1765 * Failure: FALSE
1767 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1769 FILE_MAILSLOT_SET_INFORMATION info;
1770 IO_STATUS_BLOCK iosb;
1771 NTSTATUS status;
1773 TRACE("%p %ld\n", hMailslot, dwReadTimeout);
1775 info.ReadTimeout.QuadPart = dwReadTimeout * -10000;
1776 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1777 FileMailslotSetInformation );
1778 if( status != STATUS_SUCCESS )
1780 SetLastError( RtlNtStatusToDosError(status) );
1781 return FALSE;
1783 return TRUE;
1787 /******************************************************************************
1788 * CreateIoCompletionPort (KERNEL32.@)
1790 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1791 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1793 FIXME("(%p, %p, %08lx, %08lx): stub.\n",
1794 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1795 return NULL;
1799 /******************************************************************************
1800 * GetQueuedCompletionStatus (KERNEL32.@)
1802 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1803 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1804 DWORD dwMilliseconds )
1806 FIXME("(%p,%p,%p,%p,%ld), stub!\n",
1807 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1808 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1809 return FALSE;
1812 /******************************************************************************
1813 * CreateJobObjectW (KERNEL32.@)
1815 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1817 FIXME("%p %s\n", attr, debugstr_w(name) );
1818 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1819 return 0;
1822 /******************************************************************************
1823 * CreateJobObjectA (KERNEL32.@)
1825 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1827 LPWSTR str = NULL;
1828 UINT len;
1829 HANDLE r;
1831 TRACE("%p %s\n", attr, debugstr_a(name) );
1833 if( name )
1835 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1836 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1837 if( !str )
1839 SetLastError( ERROR_OUTOFMEMORY );
1840 return 0;
1842 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1845 r = CreateJobObjectW( attr, str );
1847 HeapFree( GetProcessHeap(), 0, str );
1849 return r;
1852 /******************************************************************************
1853 * AssignProcessToJobObject (KERNEL32.@)
1855 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1857 FIXME("%p %p\n", hJob, hProcess);
1858 return TRUE;
1861 #ifdef __i386__
1863 /***********************************************************************
1864 * InterlockedCompareExchange (KERNEL32.@)
1866 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1867 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1868 "movl 12(%esp),%eax\n\t"
1869 "movl 8(%esp),%ecx\n\t"
1870 "movl 4(%esp),%edx\n\t"
1871 "lock; cmpxchgl %ecx,(%edx)\n\t"
1872 "ret $12")
1874 /***********************************************************************
1875 * InterlockedExchange (KERNEL32.@)
1877 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1878 __ASM_GLOBAL_FUNC(InterlockedExchange,
1879 "movl 8(%esp),%eax\n\t"
1880 "movl 4(%esp),%edx\n\t"
1881 "lock; xchgl %eax,(%edx)\n\t"
1882 "ret $8")
1884 /***********************************************************************
1885 * InterlockedExchangeAdd (KERNEL32.@)
1887 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1888 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1889 "movl 8(%esp),%eax\n\t"
1890 "movl 4(%esp),%edx\n\t"
1891 "lock; xaddl %eax,(%edx)\n\t"
1892 "ret $8")
1894 /***********************************************************************
1895 * InterlockedIncrement (KERNEL32.@)
1897 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1898 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1899 "movl 4(%esp),%edx\n\t"
1900 "movl $1,%eax\n\t"
1901 "lock; xaddl %eax,(%edx)\n\t"
1902 "incl %eax\n\t"
1903 "ret $4")
1905 /***********************************************************************
1906 * InterlockedDecrement (KERNEL32.@)
1908 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1909 "movl 4(%esp),%edx\n\t"
1910 "movl $-1,%eax\n\t"
1911 "lock; xaddl %eax,(%edx)\n\t"
1912 "decl %eax\n\t"
1913 "ret $4")
1915 #else /* __i386__ */
1917 /***********************************************************************
1918 * InterlockedCompareExchange (KERNEL32.@)
1920 * Atomically swap one value with another.
1922 * PARAMS
1923 * dest [I/O] The value to replace
1924 * xchq [I] The value to be swapped
1925 * compare [I] The value to compare to dest
1927 * RETURNS
1928 * The resulting value of dest.
1930 * NOTES
1931 * dest is updated only if it is equal to compare, otherwise no swap is done.
1933 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
1935 return interlocked_cmpxchg( (int *)dest, xchg, compare );
1938 /***********************************************************************
1939 * InterlockedExchange (KERNEL32.@)
1941 * Atomically swap one value with another.
1943 * PARAMS
1944 * dest [I/O] The value to replace
1945 * val [I] The value to be swapped
1947 * RETURNS
1948 * The resulting value of dest.
1950 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
1952 return interlocked_xchg( (int *)dest, val );
1955 /***********************************************************************
1956 * InterlockedExchangeAdd (KERNEL32.@)
1958 * Atomically add one value to another.
1960 * PARAMS
1961 * dest [I/O] The value to add to
1962 * incr [I] The value to be added
1964 * RETURNS
1965 * The resulting value of dest.
1967 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
1969 return interlocked_xchg_add( (int *)dest, incr );
1972 /***********************************************************************
1973 * InterlockedIncrement (KERNEL32.@)
1975 * Atomically increment a value.
1977 * PARAMS
1978 * dest [I/O] The value to increment
1980 * RETURNS
1981 * The resulting value of dest.
1983 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
1985 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
1988 /***********************************************************************
1989 * InterlockedDecrement (KERNEL32.@)
1991 * Atomically decrement a value.
1993 * PARAMS
1994 * dest [I/O] The value to decrement
1996 * RETURNS
1997 * The resulting value of dest.
1999 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
2001 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
2004 #endif /* __i386__ */