user: Fix return type and argument type for GetDlgItemText{A,W}.
[wine/multimedia.git] / dlls / kernel / sync.c
blob5a2535e7b641ccc540e9b850c62a8903fe9ce2b4
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 #define WIN32_NO_STATUS
49 #include "windef.h"
50 #include "winbase.h"
51 #include "winerror.h"
52 #include "winnls.h"
53 #include "winternl.h"
54 #include "winioctl.h"
55 #include "ddk/wdm.h"
57 #include "wine/server.h"
58 #include "wine/unicode.h"
59 #include "wine/winbase16.h"
60 #include "kernel_private.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(sync);
66 /* check if current version is NT or Win95 */
67 inline static int is_version_nt(void)
69 return !(GetVersion() & 0x80000000);
72 /* returns directory handle to \\BaseNamedObjects */
73 HANDLE get_BaseNamedObjects_handle(void)
75 static HANDLE handle = NULL;
76 static const WCHAR basenameW[] =
77 {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
78 UNICODE_STRING str;
79 OBJECT_ATTRIBUTES attr;
81 if (!handle)
83 HANDLE dir;
85 RtlInitUnicodeString(&str, basenameW);
86 InitializeObjectAttributes(&attr, &str, 0, 0, NULL);
87 NtOpenDirectoryObject(&dir, DIRECTORY_CREATE_OBJECT|DIRECTORY_TRAVERSE,
88 &attr);
89 if (InterlockedCompareExchangePointer( (PVOID)&handle, dir, 0 ) != 0)
91 /* someone beat us here... */
92 CloseHandle( dir );
95 return handle;
98 /***********************************************************************
99 * Sleep (KERNEL32.@)
101 VOID WINAPI Sleep( DWORD timeout )
103 SleepEx( timeout, FALSE );
106 /******************************************************************************
107 * SleepEx (KERNEL32.@)
109 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
111 NTSTATUS status;
113 if (timeout == INFINITE) status = NtDelayExecution( alertable, NULL );
114 else
116 LARGE_INTEGER time;
118 time.QuadPart = timeout * (ULONGLONG)10000;
119 time.QuadPart = -time.QuadPart;
120 status = NtDelayExecution( alertable, &time );
122 if (status != STATUS_USER_APC) status = STATUS_SUCCESS;
123 return status;
127 /***********************************************************************
128 * SwitchToThread (KERNEL32.@)
130 BOOL WINAPI SwitchToThread(void)
132 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
136 /***********************************************************************
137 * WaitForSingleObject (KERNEL32.@)
139 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
141 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
145 /***********************************************************************
146 * WaitForSingleObjectEx (KERNEL32.@)
148 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
149 BOOL alertable )
151 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
155 /***********************************************************************
156 * WaitForMultipleObjects (KERNEL32.@)
158 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
159 BOOL wait_all, DWORD timeout )
161 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
165 /***********************************************************************
166 * WaitForMultipleObjectsEx (KERNEL32.@)
168 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
169 BOOL wait_all, DWORD timeout,
170 BOOL alertable )
172 NTSTATUS status;
173 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
174 unsigned int i;
176 if (count >= MAXIMUM_WAIT_OBJECTS)
178 SetLastError(ERROR_INVALID_PARAMETER);
179 return WAIT_FAILED;
181 for (i = 0; i < count; i++)
183 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
184 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
185 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
186 hloc[i] = GetStdHandle( (DWORD)handles[i] );
187 else
188 hloc[i] = handles[i];
190 /* yes, even screen buffer console handles are waitable, and are
191 * handled as a handle to the console itself !!
193 if (is_console_handle(hloc[i]))
195 if (!VerifyConsoleIoHandle(hloc[i]))
197 return FALSE;
199 hloc[i] = GetConsoleInputWaitHandle();
203 if (timeout == INFINITE)
205 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, NULL );
207 else
209 LARGE_INTEGER time;
211 time.QuadPart = timeout * (ULONGLONG)10000;
212 time.QuadPart = -time.QuadPart;
213 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, &time );
216 if (HIWORD(status)) /* is it an error code? */
218 SetLastError( RtlNtStatusToDosError(status) );
219 status = WAIT_FAILED;
221 return status;
225 /***********************************************************************
226 * WaitForSingleObject (KERNEL.460)
228 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
230 DWORD retval, mutex_count;
232 ReleaseThunkLock( &mutex_count );
233 retval = WaitForSingleObject( handle, timeout );
234 RestoreThunkLock( mutex_count );
235 return retval;
238 /***********************************************************************
239 * WaitForMultipleObjects (KERNEL.461)
241 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
242 BOOL wait_all, DWORD timeout )
244 DWORD retval, mutex_count;
246 ReleaseThunkLock( &mutex_count );
247 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
248 RestoreThunkLock( mutex_count );
249 return retval;
252 /***********************************************************************
253 * WaitForMultipleObjectsEx (KERNEL.495)
255 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
256 BOOL wait_all, DWORD timeout, BOOL alertable )
258 DWORD retval, mutex_count;
260 ReleaseThunkLock( &mutex_count );
261 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
262 RestoreThunkLock( mutex_count );
263 return retval;
266 /***********************************************************************
267 * RegisterWaitForSingleObject (KERNEL32.@)
269 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
270 WAITORTIMERCALLBACK Callback, PVOID Context,
271 ULONG dwMilliseconds, ULONG dwFlags)
273 FIXME("%p %p %p %p %ld %ld\n",
274 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
275 return FALSE;
278 /***********************************************************************
279 * RegisterWaitForSingleObjectEx (KERNEL32.@)
281 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
282 WAITORTIMERCALLBACK Callback, PVOID Context,
283 ULONG dwMilliseconds, ULONG dwFlags )
285 FIXME("%p %p %p %ld %ld\n",
286 hObject,Callback,Context,dwMilliseconds,dwFlags);
287 return 0;
290 /***********************************************************************
291 * UnregisterWait (KERNEL32.@)
293 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
295 FIXME("%p\n",WaitHandle);
296 return FALSE;
299 /***********************************************************************
300 * UnregisterWaitEx (KERNEL32.@)
302 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
304 FIXME("%p %p\n",WaitHandle, CompletionEvent);
305 return FALSE;
308 /***********************************************************************
309 * SignalObjectAndWait (KERNEL32.@)
311 * Allows to atomically signal any of the synchro objects (semaphore,
312 * mutex, event) and wait on another.
314 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
315 DWORD dwMilliseconds, BOOL bAlertable )
317 NTSTATUS status;
318 LARGE_INTEGER timeout, *ptimeout = NULL;
320 TRACE("%p %p %ld %d\n", hObjectToSignal,
321 hObjectToWaitOn, dwMilliseconds, bAlertable);
323 if (dwMilliseconds != INFINITE)
325 timeout.QuadPart = dwMilliseconds * (ULONGLONG)10000;
326 timeout.QuadPart = -timeout.QuadPart;
327 ptimeout = &timeout;
330 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn,
331 bAlertable, ptimeout );
332 if (HIWORD(status))
334 SetLastError( RtlNtStatusToDosError(status) );
335 status = WAIT_FAILED;
337 return status;
340 /***********************************************************************
341 * InitializeCriticalSection (KERNEL32.@)
343 * Initialise a critical section before use.
345 * PARAMS
346 * crit [O] Critical section to initialise.
348 * RETURNS
349 * Nothing. If the function fails an exception is raised.
351 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
353 NTSTATUS ret = RtlInitializeCriticalSection( crit );
354 if (ret) RtlRaiseStatus( ret );
357 /***********************************************************************
358 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
360 * Initialise a critical section with a spin count.
362 * PARAMS
363 * crit [O] Critical section to initialise.
364 * spincount [I] Number of times to spin upon contention.
366 * RETURNS
367 * Success: TRUE.
368 * Failure: Nothing. If the function fails an exception is raised.
370 * NOTES
371 * spincount is ignored on uni-processor systems.
373 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
375 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
376 if (ret) RtlRaiseStatus( ret );
377 return !ret;
380 /***********************************************************************
381 * MakeCriticalSectionGlobal (KERNEL32.@)
383 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
385 /* let's assume that only one thread at a time will try to do this */
386 HANDLE sem = crit->LockSemaphore;
387 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
388 crit->LockSemaphore = ConvertToGlobalHandle( sem );
389 if (crit->DebugInfo)
391 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
392 crit->DebugInfo = NULL;
397 /***********************************************************************
398 * ReinitializeCriticalSection (KERNEL32.@)
400 * Initialise an already used critical section.
402 * PARAMS
403 * crit [O] Critical section to initialise.
405 * RETURNS
406 * Nothing.
408 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
410 if ( !crit->LockSemaphore )
411 RtlInitializeCriticalSection( crit );
415 /***********************************************************************
416 * UninitializeCriticalSection (KERNEL32.@)
418 * UnInitialise a critical section after use.
420 * PARAMS
421 * crit [O] Critical section to uninitialise (destroy).
423 * RETURNS
424 * Nothing.
426 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
428 RtlDeleteCriticalSection( crit );
432 /***********************************************************************
433 * CreateEventA (KERNEL32.@)
435 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
436 BOOL initial_state, LPCSTR name )
438 WCHAR buffer[MAX_PATH];
440 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
442 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
444 SetLastError( ERROR_FILENAME_EXCED_RANGE );
445 return 0;
447 return CreateEventW( sa, manual_reset, initial_state, buffer );
451 /***********************************************************************
452 * CreateEventW (KERNEL32.@)
454 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
455 BOOL initial_state, LPCWSTR name )
457 HANDLE ret;
458 UNICODE_STRING nameW;
459 OBJECT_ATTRIBUTES attr;
460 NTSTATUS status;
462 /* one buggy program needs this
463 * ("Van Dale Groot woordenboek der Nederlandse taal")
465 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
467 ERR("Bad security attributes pointer %p\n",sa);
468 SetLastError( ERROR_INVALID_PARAMETER);
469 return 0;
472 attr.Length = sizeof(attr);
473 attr.RootDirectory = 0;
474 attr.ObjectName = NULL;
475 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
476 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
477 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
478 attr.SecurityQualityOfService = NULL;
479 if (name)
481 RtlInitUnicodeString( &nameW, name );
482 attr.ObjectName = &nameW;
483 attr.RootDirectory = get_BaseNamedObjects_handle();
486 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
487 if (status == STATUS_OBJECT_NAME_EXISTS)
488 SetLastError( ERROR_ALREADY_EXISTS );
489 else
490 SetLastError( RtlNtStatusToDosError(status) );
491 return ret;
495 /***********************************************************************
496 * CreateW32Event (KERNEL.457)
498 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
500 return CreateEventW( NULL, manual_reset, initial_state, NULL );
504 /***********************************************************************
505 * OpenEventA (KERNEL32.@)
507 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
509 WCHAR buffer[MAX_PATH];
511 if (!name) return OpenEventW( access, inherit, NULL );
513 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
515 SetLastError( ERROR_FILENAME_EXCED_RANGE );
516 return 0;
518 return OpenEventW( access, inherit, buffer );
522 /***********************************************************************
523 * OpenEventW (KERNEL32.@)
525 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
527 HANDLE ret;
528 UNICODE_STRING nameW;
529 OBJECT_ATTRIBUTES attr;
530 NTSTATUS status;
532 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
534 attr.Length = sizeof(attr);
535 attr.RootDirectory = 0;
536 attr.ObjectName = NULL;
537 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
538 attr.SecurityDescriptor = NULL;
539 attr.SecurityQualityOfService = NULL;
540 if (name)
542 RtlInitUnicodeString( &nameW, name );
543 attr.ObjectName = &nameW;
544 attr.RootDirectory = get_BaseNamedObjects_handle();
547 status = NtOpenEvent( &ret, access, &attr );
548 if (status != STATUS_SUCCESS)
550 SetLastError( RtlNtStatusToDosError(status) );
551 return 0;
553 return ret;
556 /***********************************************************************
557 * PulseEvent (KERNEL32.@)
559 BOOL WINAPI PulseEvent( HANDLE handle )
561 NTSTATUS status;
563 if ((status = NtPulseEvent( handle, NULL )))
564 SetLastError( RtlNtStatusToDosError(status) );
565 return !status;
569 /***********************************************************************
570 * SetW32Event (KERNEL.458)
571 * SetEvent (KERNEL32.@)
573 BOOL WINAPI SetEvent( HANDLE handle )
575 NTSTATUS status;
577 if ((status = NtSetEvent( handle, NULL )))
578 SetLastError( RtlNtStatusToDosError(status) );
579 return !status;
583 /***********************************************************************
584 * ResetW32Event (KERNEL.459)
585 * ResetEvent (KERNEL32.@)
587 BOOL WINAPI ResetEvent( HANDLE handle )
589 NTSTATUS status;
591 if ((status = NtResetEvent( handle, NULL )))
592 SetLastError( RtlNtStatusToDosError(status) );
593 return !status;
597 /***********************************************************************
598 * NOTE: The Win95 VWin32_Event routines given below are really low-level
599 * routines implemented directly by VWin32. The user-mode libraries
600 * implement Win32 synchronisation routines on top of these low-level
601 * primitives. We do it the other way around here :-)
604 /***********************************************************************
605 * VWin32_EventCreate (KERNEL.442)
607 HANDLE WINAPI VWin32_EventCreate(VOID)
609 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
610 return ConvertToGlobalHandle( hEvent );
613 /***********************************************************************
614 * VWin32_EventDestroy (KERNEL.443)
616 VOID WINAPI VWin32_EventDestroy(HANDLE event)
618 CloseHandle( event );
621 /***********************************************************************
622 * VWin32_EventWait (KERNEL.450)
624 VOID WINAPI VWin32_EventWait(HANDLE event)
626 DWORD mutex_count;
628 ReleaseThunkLock( &mutex_count );
629 WaitForSingleObject( event, INFINITE );
630 RestoreThunkLock( mutex_count );
633 /***********************************************************************
634 * VWin32_EventSet (KERNEL.451)
635 * KERNEL_479 (KERNEL.479)
637 VOID WINAPI VWin32_EventSet(HANDLE event)
639 SetEvent( event );
644 /***********************************************************************
645 * CreateMutexA (KERNEL32.@)
647 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
649 WCHAR buffer[MAX_PATH];
651 if (!name) return CreateMutexW( sa, owner, NULL );
653 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
655 SetLastError( ERROR_FILENAME_EXCED_RANGE );
656 return 0;
658 return CreateMutexW( sa, owner, buffer );
662 /***********************************************************************
663 * CreateMutexW (KERNEL32.@)
665 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
667 HANDLE ret;
668 UNICODE_STRING nameW;
669 OBJECT_ATTRIBUTES attr;
670 NTSTATUS status;
672 attr.Length = sizeof(attr);
673 attr.RootDirectory = 0;
674 attr.ObjectName = NULL;
675 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
676 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
677 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
678 attr.SecurityQualityOfService = NULL;
679 if (name)
681 RtlInitUnicodeString( &nameW, name );
682 attr.ObjectName = &nameW;
683 attr.RootDirectory = get_BaseNamedObjects_handle();
686 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
687 if (status == STATUS_OBJECT_NAME_EXISTS)
688 SetLastError( ERROR_ALREADY_EXISTS );
689 else
690 SetLastError( RtlNtStatusToDosError(status) );
691 return ret;
695 /***********************************************************************
696 * OpenMutexA (KERNEL32.@)
698 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
700 WCHAR buffer[MAX_PATH];
702 if (!name) return OpenMutexW( access, inherit, NULL );
704 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
706 SetLastError( ERROR_FILENAME_EXCED_RANGE );
707 return 0;
709 return OpenMutexW( access, inherit, buffer );
713 /***********************************************************************
714 * OpenMutexW (KERNEL32.@)
716 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
718 HANDLE ret;
719 UNICODE_STRING nameW;
720 OBJECT_ATTRIBUTES attr;
721 NTSTATUS status;
723 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
725 attr.Length = sizeof(attr);
726 attr.RootDirectory = 0;
727 attr.ObjectName = NULL;
728 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
729 attr.SecurityDescriptor = NULL;
730 attr.SecurityQualityOfService = NULL;
731 if (name)
733 RtlInitUnicodeString( &nameW, name );
734 attr.ObjectName = &nameW;
735 attr.RootDirectory = get_BaseNamedObjects_handle();
738 status = NtOpenMutant( &ret, access, &attr );
739 if (status != STATUS_SUCCESS)
741 SetLastError( RtlNtStatusToDosError(status) );
742 return 0;
744 return ret;
748 /***********************************************************************
749 * ReleaseMutex (KERNEL32.@)
751 BOOL WINAPI ReleaseMutex( HANDLE handle )
753 NTSTATUS status;
755 status = NtReleaseMutant(handle, NULL);
756 if (status != STATUS_SUCCESS)
758 SetLastError( RtlNtStatusToDosError(status) );
759 return FALSE;
761 return TRUE;
766 * Semaphores
770 /***********************************************************************
771 * CreateSemaphoreA (KERNEL32.@)
773 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
775 WCHAR buffer[MAX_PATH];
777 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
779 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
781 SetLastError( ERROR_FILENAME_EXCED_RANGE );
782 return 0;
784 return CreateSemaphoreW( sa, initial, max, buffer );
788 /***********************************************************************
789 * CreateSemaphoreW (KERNEL32.@)
791 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
792 LONG max, LPCWSTR name )
794 HANDLE ret;
795 UNICODE_STRING nameW;
796 OBJECT_ATTRIBUTES attr;
797 NTSTATUS status;
799 attr.Length = sizeof(attr);
800 attr.RootDirectory = 0;
801 attr.ObjectName = NULL;
802 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
803 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
804 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
805 attr.SecurityQualityOfService = NULL;
806 if (name)
808 RtlInitUnicodeString( &nameW, name );
809 attr.ObjectName = &nameW;
810 attr.RootDirectory = get_BaseNamedObjects_handle();
813 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
814 if (status == STATUS_OBJECT_NAME_EXISTS)
815 SetLastError( ERROR_ALREADY_EXISTS );
816 else
817 SetLastError( RtlNtStatusToDosError(status) );
818 return ret;
822 /***********************************************************************
823 * OpenSemaphoreA (KERNEL32.@)
825 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
827 WCHAR buffer[MAX_PATH];
829 if (!name) return OpenSemaphoreW( access, inherit, NULL );
831 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
833 SetLastError( ERROR_FILENAME_EXCED_RANGE );
834 return 0;
836 return OpenSemaphoreW( access, inherit, buffer );
840 /***********************************************************************
841 * OpenSemaphoreW (KERNEL32.@)
843 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
845 HANDLE ret;
846 UNICODE_STRING nameW;
847 OBJECT_ATTRIBUTES attr;
848 NTSTATUS status;
850 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
852 attr.Length = sizeof(attr);
853 attr.RootDirectory = 0;
854 attr.ObjectName = NULL;
855 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
856 attr.SecurityDescriptor = NULL;
857 attr.SecurityQualityOfService = NULL;
858 if (name)
860 RtlInitUnicodeString( &nameW, name );
861 attr.ObjectName = &nameW;
862 attr.RootDirectory = get_BaseNamedObjects_handle();
865 status = NtOpenSemaphore( &ret, access, &attr );
866 if (status != STATUS_SUCCESS)
868 SetLastError( RtlNtStatusToDosError(status) );
869 return 0;
871 return ret;
875 /***********************************************************************
876 * ReleaseSemaphore (KERNEL32.@)
878 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
880 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
881 if (status) SetLastError( RtlNtStatusToDosError(status) );
882 return !status;
887 * Timers
891 /***********************************************************************
892 * CreateWaitableTimerA (KERNEL32.@)
894 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
896 WCHAR buffer[MAX_PATH];
898 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
900 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
902 SetLastError( ERROR_FILENAME_EXCED_RANGE );
903 return 0;
905 return CreateWaitableTimerW( sa, manual, buffer );
909 /***********************************************************************
910 * CreateWaitableTimerW (KERNEL32.@)
912 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
914 HANDLE handle;
915 NTSTATUS status;
916 UNICODE_STRING nameW;
917 OBJECT_ATTRIBUTES attr;
919 attr.Length = sizeof(attr);
920 attr.RootDirectory = 0;
921 attr.ObjectName = NULL;
922 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
923 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
924 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
925 attr.SecurityQualityOfService = NULL;
926 if (name)
928 RtlInitUnicodeString( &nameW, name );
929 attr.ObjectName = &nameW;
930 attr.RootDirectory = get_BaseNamedObjects_handle();
933 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
934 manual ? NotificationTimer : SynchronizationTimer);
935 if (status == STATUS_OBJECT_NAME_EXISTS)
936 SetLastError( ERROR_ALREADY_EXISTS );
937 else
938 SetLastError( RtlNtStatusToDosError(status) );
939 return handle;
943 /***********************************************************************
944 * OpenWaitableTimerA (KERNEL32.@)
946 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
948 WCHAR buffer[MAX_PATH];
950 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
952 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
954 SetLastError( ERROR_FILENAME_EXCED_RANGE );
955 return 0;
957 return OpenWaitableTimerW( access, inherit, buffer );
961 /***********************************************************************
962 * OpenWaitableTimerW (KERNEL32.@)
964 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
966 HANDLE handle;
967 UNICODE_STRING nameW;
968 OBJECT_ATTRIBUTES attr;
969 NTSTATUS status;
971 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
973 attr.Length = sizeof(attr);
974 attr.RootDirectory = 0;
975 attr.ObjectName = NULL;
976 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
977 attr.SecurityDescriptor = NULL;
978 attr.SecurityQualityOfService = NULL;
979 if (name)
981 RtlInitUnicodeString( &nameW, name );
982 attr.ObjectName = &nameW;
983 attr.RootDirectory = get_BaseNamedObjects_handle();
986 status = NtOpenTimer(&handle, access, &attr);
987 if (status != STATUS_SUCCESS)
989 SetLastError( RtlNtStatusToDosError(status) );
990 return 0;
992 return handle;
996 /***********************************************************************
997 * SetWaitableTimer (KERNEL32.@)
999 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
1000 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
1002 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
1003 arg, resume, period, NULL);
1005 if (status != STATUS_SUCCESS)
1007 SetLastError( RtlNtStatusToDosError(status) );
1008 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
1010 return TRUE;
1014 /***********************************************************************
1015 * CancelWaitableTimer (KERNEL32.@)
1017 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1019 NTSTATUS status;
1021 status = NtCancelTimer(handle, NULL);
1022 if (status != STATUS_SUCCESS)
1024 SetLastError( RtlNtStatusToDosError(status) );
1025 return FALSE;
1027 return TRUE;
1031 /***********************************************************************
1032 * CreateTimerQueue (KERNEL32.@)
1034 HANDLE WINAPI CreateTimerQueue(void)
1036 FIXME("stub\n");
1037 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1038 return NULL;
1042 /***********************************************************************
1043 * DeleteTimerQueueEx (KERNEL32.@)
1045 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1047 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
1048 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1049 return 0;
1052 /***********************************************************************
1053 * CreateTimerQueueTimer (KERNEL32.@)
1055 * Creates a timer-queue timer. This timer expires at the specified due
1056 * time (in ms), then after every specified period (in ms). When the timer
1057 * expires, the callback function is called.
1059 * RETURNS
1060 * nonzero on success or zero on faillure
1062 * BUGS
1063 * Unimplemented
1065 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1066 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1067 DWORD DueTime, DWORD Period, ULONG Flags )
1069 FIXME("stub\n");
1070 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1071 return TRUE;
1074 /***********************************************************************
1075 * DeleteTimerQueueTimer (KERNEL32.@)
1077 * Cancels a timer-queue timer.
1079 * RETURNS
1080 * nonzero on success or zero on faillure
1082 * BUGS
1083 * Unimplemented
1085 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1086 HANDLE CompletionEvent )
1088 FIXME("stub\n");
1089 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1090 return TRUE;
1095 * Pipes
1099 /***********************************************************************
1100 * CreateNamedPipeA (KERNEL32.@)
1102 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1103 DWORD dwPipeMode, DWORD nMaxInstances,
1104 DWORD nOutBufferSize, DWORD nInBufferSize,
1105 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1107 WCHAR buffer[MAX_PATH];
1109 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1110 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1112 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1114 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1115 return INVALID_HANDLE_VALUE;
1117 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1118 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1122 /***********************************************************************
1123 * CreateNamedPipeW (KERNEL32.@)
1125 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1126 DWORD dwPipeMode, DWORD nMaxInstances,
1127 DWORD nOutBufferSize, DWORD nInBufferSize,
1128 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1130 HANDLE handle;
1131 UNICODE_STRING nt_name;
1132 OBJECT_ATTRIBUTES attr;
1133 DWORD options;
1134 BOOLEAN pipe_type, read_mode, non_block;
1135 NTSTATUS status;
1136 IO_STATUS_BLOCK iosb;
1137 LARGE_INTEGER timeout;
1139 TRACE("(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n",
1140 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1141 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1143 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1145 SetLastError( ERROR_PATH_NOT_FOUND );
1146 return INVALID_HANDLE_VALUE;
1148 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1150 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1151 RtlFreeUnicodeString( &nt_name );
1152 return INVALID_HANDLE_VALUE;
1155 attr.Length = sizeof(attr);
1156 attr.RootDirectory = 0;
1157 attr.ObjectName = &nt_name;
1158 attr.Attributes = OBJ_CASE_INSENSITIVE |
1159 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1160 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1161 attr.SecurityQualityOfService = NULL;
1163 options = 0;
1164 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1165 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1166 if ((dwOpenMode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
1167 options |= FILE_PIPE_FULL_DUPLEX;
1168 else if (dwOpenMode & PIPE_ACCESS_INBOUND) options |= FILE_PIPE_INBOUND;
1169 else if (dwOpenMode & PIPE_ACCESS_OUTBOUND) options |= FILE_PIPE_OUTBOUND;
1170 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1171 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1172 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1173 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0UL;
1175 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1177 SetLastError(0);
1179 status = NtCreateNamedPipeFile(&handle, GENERIC_READ|GENERIC_WRITE, &attr, &iosb,
1180 0, FILE_OVERWRITE_IF, options, pipe_type,
1181 read_mode, non_block, nMaxInstances,
1182 nInBufferSize, nOutBufferSize, &timeout);
1184 RtlFreeUnicodeString( &nt_name );
1185 if (status)
1187 handle = INVALID_HANDLE_VALUE;
1188 SetLastError( RtlNtStatusToDosError(status) );
1190 return handle;
1194 /***********************************************************************
1195 * PeekNamedPipe (KERNEL32.@)
1197 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1198 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1200 #ifdef FIONREAD
1201 int avail=0, fd, ret, flags;
1203 TRACE("(%p,%p,%lu,%p,%p,%p)\n", hPipe, lpvBuffer, cbBuffer, lpcbRead, lpcbAvail, lpcbMessage);
1205 ret = wine_server_handle_to_fd( hPipe, FILE_READ_DATA, &fd, &flags );
1206 if (ret)
1208 SetLastError( RtlNtStatusToDosError(ret) );
1209 return FALSE;
1211 if (flags & FD_FLAG_RECV_SHUTDOWN)
1213 wine_server_release_fd( hPipe, fd );
1214 SetLastError ( ERROR_PIPE_NOT_CONNECTED );
1215 return FALSE;
1218 if (ioctl(fd,FIONREAD, &avail ) != 0)
1220 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1221 wine_server_release_fd( hPipe, fd );
1222 return FALSE;
1224 if (!avail) /* check for closed pipe */
1226 struct pollfd pollfd;
1227 pollfd.fd = fd;
1228 pollfd.events = POLLIN;
1229 pollfd.revents = 0;
1230 switch (poll( &pollfd, 1, 0 ))
1232 case 0:
1233 break;
1234 case 1: /* got something */
1235 if (!(pollfd.revents & (POLLHUP | POLLERR))) break;
1236 TRACE("POLLHUP | POLLERR\n");
1237 /* fall through */
1238 case -1:
1239 wine_server_release_fd( hPipe, fd );
1240 SetLastError(ERROR_BROKEN_PIPE);
1241 return FALSE;
1244 TRACE(" 0x%08x bytes available\n", avail );
1245 ret = TRUE;
1246 if (lpcbAvail)
1247 *lpcbAvail = avail;
1248 if (lpcbRead)
1249 *lpcbRead = 0;
1250 if (avail && lpvBuffer && cbBuffer)
1252 int readbytes = (avail < cbBuffer) ? avail : cbBuffer;
1253 readbytes = recv(fd, lpvBuffer, readbytes, MSG_PEEK);
1254 if (readbytes < 0)
1256 WARN("failed to peek socket (%d)\n", errno);
1257 ret = FALSE;
1259 else if (lpcbRead)
1260 *lpcbRead = readbytes;
1262 wine_server_release_fd( hPipe, fd );
1263 return ret;
1264 #endif /* defined(FIONREAD) */
1266 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1267 FIXME("function not implemented\n");
1268 return FALSE;
1271 /***********************************************************************
1272 * WaitNamedPipeA (KERNEL32.@)
1274 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1276 WCHAR buffer[MAX_PATH];
1278 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1280 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1282 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1283 return 0;
1285 return WaitNamedPipeW( buffer, nTimeOut );
1289 /***********************************************************************
1290 * WaitNamedPipeW (KERNEL32.@)
1292 * Waits for a named pipe instance to become available
1294 * PARAMS
1295 * name [I] Pointer to a named pipe name to wait for
1296 * nTimeOut [I] How long to wait in ms
1298 * RETURNS
1299 * TRUE: Success, named pipe can be opened with CreteFile
1300 * FALSE: Failure, GetLastError can be called for further details
1302 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1304 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1305 NTSTATUS status;
1306 UNICODE_STRING nt_name, pipe_dev_name;
1307 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1308 IO_STATUS_BLOCK iosb;
1309 OBJECT_ATTRIBUTES attr;
1310 ULONG sz_pipe_wait;
1311 HANDLE pipe_dev;
1313 TRACE("%s 0x%08lx\n",debugstr_w(name),nTimeOut);
1315 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1316 return FALSE;
1318 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1319 nt_name.Length < sizeof(leadin) ||
1320 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR) != 0))
1322 RtlFreeUnicodeString( &nt_name );
1323 SetLastError( ERROR_PATH_NOT_FOUND );
1324 return FALSE;
1327 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1328 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1330 RtlFreeUnicodeString( &nt_name );
1331 SetLastError( ERROR_OUTOFMEMORY );
1332 return FALSE;
1335 pipe_dev_name.Buffer = nt_name.Buffer;
1336 pipe_dev_name.Length = sizeof(leadin);
1337 pipe_dev_name.MaximumLength = sizeof(leadin);
1338 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1339 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1340 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1341 FILE_SYNCHRONOUS_IO_NONALERT);
1342 if (status != ERROR_SUCCESS)
1344 SetLastError( ERROR_PATH_NOT_FOUND );
1345 return FALSE;
1348 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1349 pipe_wait->Timeout.QuadPart = nTimeOut * -10000L;
1350 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1351 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1352 pipe_wait->NameLength);
1353 RtlFreeUnicodeString( &nt_name );
1355 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1356 pipe_wait, sz_pipe_wait, NULL, 0 );
1358 HeapFree( GetProcessHeap(), 0, pipe_wait );
1359 NtClose( pipe_dev );
1361 if(status != STATUS_SUCCESS)
1363 SetLastError(RtlNtStatusToDosError(status));
1364 return FALSE;
1366 else
1367 return TRUE;
1371 /***********************************************************************
1372 * ConnectNamedPipe (KERNEL32.@)
1374 * Connects to a named pipe
1376 * Parameters
1377 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1378 * overlapped: Optional OVERLAPPED struct
1380 * Return values
1381 * TRUE: Success
1382 * FALSE: Failure, GetLastError can be called for further details
1384 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1386 NTSTATUS status;
1387 IO_STATUS_BLOCK status_block;
1389 TRACE("(%p,%p)\n", hPipe, overlapped);
1391 if(overlapped)
1392 overlapped->Internal = STATUS_PENDING;
1394 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, NULL,
1395 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1396 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1398 if (status == STATUS_SUCCESS) return TRUE;
1399 SetLastError( RtlNtStatusToDosError(status) );
1400 return FALSE;
1403 /***********************************************************************
1404 * DisconnectNamedPipe (KERNEL32.@)
1406 * Disconnects from a named pipe
1408 * Parameters
1409 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1411 * Return values
1412 * TRUE: Success
1413 * FALSE: Failure, GetLastError can be called for further details
1415 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1417 NTSTATUS status;
1418 IO_STATUS_BLOCK io_block;
1420 TRACE("(%p)\n",hPipe);
1422 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1423 NULL, 0, NULL, 0);
1424 if (status == STATUS_SUCCESS) return TRUE;
1425 SetLastError( RtlNtStatusToDosError(status) );
1426 return FALSE;
1429 /***********************************************************************
1430 * TransactNamedPipe (KERNEL32.@)
1432 * BUGS
1433 * should be done as a single operation in the wineserver or kernel
1435 BOOL WINAPI TransactNamedPipe(
1436 HANDLE handle, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1437 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1439 BOOL r;
1440 DWORD count;
1442 TRACE("%p %p %ld %p %ld %p %p\n",
1443 handle, lpInput, dwInputSize, lpOutput,
1444 dwOutputSize, lpBytesRead, lpOverlapped);
1446 if (lpOverlapped)
1448 FIXME("Doesn't support overlapped operation as yet\n");
1449 return FALSE;
1452 r = WriteFile(handle, lpOutput, dwOutputSize, &count, NULL);
1453 if (r)
1454 r = ReadFile(handle, lpInput, dwInputSize, lpBytesRead, NULL);
1456 return r;
1459 /***********************************************************************
1460 * GetNamedPipeInfo (KERNEL32.@)
1462 BOOL WINAPI GetNamedPipeInfo(
1463 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1464 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1466 BOOL ret;
1468 TRACE("%p %p %p %p %p\n", hNamedPipe, lpFlags,
1469 lpOutputBufferSize, lpInputBufferSize, lpMaxInstances);
1471 SERVER_START_REQ( get_named_pipe_info )
1473 req->handle = hNamedPipe;
1474 ret = !wine_server_call_err( req );
1475 if (lpFlags)
1477 *lpFlags = 0;
1478 if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)
1479 *lpFlags |= PIPE_TYPE_MESSAGE;
1480 if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
1481 *lpFlags |= PIPE_READMODE_MESSAGE;
1482 if (reply->flags & NAMED_PIPE_NONBLOCKING_MODE)
1483 *lpFlags |= PIPE_NOWAIT;
1485 if (lpOutputBufferSize) *lpOutputBufferSize = reply->outsize;
1486 if (lpInputBufferSize) *lpInputBufferSize = reply->outsize;
1487 if (lpMaxInstances) *lpMaxInstances = reply->maxinstances;
1489 SERVER_END_REQ;
1491 return ret;
1494 /***********************************************************************
1495 * GetNamedPipeHandleStateA (KERNEL32.@)
1497 BOOL WINAPI GetNamedPipeHandleStateA(
1498 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1499 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1500 LPSTR lpUsername, DWORD nUsernameMaxSize)
1502 FIXME("%p %p %p %p %p %p %ld\n",
1503 hNamedPipe, lpState, lpCurInstances,
1504 lpMaxCollectionCount, lpCollectDataTimeout,
1505 lpUsername, nUsernameMaxSize);
1507 return FALSE;
1510 /***********************************************************************
1511 * GetNamedPipeHandleStateW (KERNEL32.@)
1513 BOOL WINAPI GetNamedPipeHandleStateW(
1514 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1515 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1516 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1518 FIXME("%p %p %p %p %p %p %ld\n",
1519 hNamedPipe, lpState, lpCurInstances,
1520 lpMaxCollectionCount, lpCollectDataTimeout,
1521 lpUsername, nUsernameMaxSize);
1523 return FALSE;
1526 /***********************************************************************
1527 * SetNamedPipeHandleState (KERNEL32.@)
1529 BOOL WINAPI SetNamedPipeHandleState(
1530 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1531 LPDWORD lpCollectDataTimeout)
1533 /* should be a fixme, but this function is called a lot by the RPC
1534 * runtime, and it slows down InstallShield a fair bit. */
1535 WARN("stub: %p %p/%ld %p %p\n",
1536 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1537 return FALSE;
1540 /***********************************************************************
1541 * CallNamedPipeA (KERNEL32.@)
1543 BOOL WINAPI CallNamedPipeA(
1544 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1545 LPVOID lpOutput, DWORD dwOutputSize,
1546 LPDWORD lpBytesRead, DWORD nTimeout)
1548 DWORD len;
1549 LPWSTR str = NULL;
1550 BOOL ret;
1552 TRACE("%s %p %ld %p %ld %p %ld\n",
1553 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1554 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1556 if( lpNamedPipeName )
1558 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1559 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1560 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1562 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1563 dwOutputSize, lpBytesRead, nTimeout );
1564 if( lpNamedPipeName )
1565 HeapFree( GetProcessHeap(), 0, str );
1567 return ret;
1570 /***********************************************************************
1571 * CallNamedPipeW (KERNEL32.@)
1573 BOOL WINAPI CallNamedPipeW(
1574 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1575 LPVOID lpOutput, DWORD lpOutputSize,
1576 LPDWORD lpBytesRead, DWORD nTimeout)
1578 FIXME("%s %p %ld %p %ld %p %ld\n",
1579 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1580 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1581 return FALSE;
1584 /******************************************************************
1585 * CreatePipe (KERNEL32.@)
1588 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1589 LPSECURITY_ATTRIBUTES sa, DWORD size )
1591 static unsigned index /* = 0 */;
1592 WCHAR name[64];
1593 HANDLE hr, hw;
1594 unsigned in_index = index;
1595 UNICODE_STRING nt_name;
1596 OBJECT_ATTRIBUTES attr;
1597 NTSTATUS status;
1598 IO_STATUS_BLOCK iosb;
1599 LARGE_INTEGER timeout;
1601 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1603 attr.Length = sizeof(attr);
1604 attr.RootDirectory = 0;
1605 attr.ObjectName = &nt_name;
1606 attr.Attributes = OBJ_CASE_INSENSITIVE |
1607 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1608 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1609 attr.SecurityQualityOfService = NULL;
1611 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1612 /* generate a unique pipe name (system wide) */
1615 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1616 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1617 'u','.','%','0','8','u','\0' };
1619 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1620 GetCurrentProcessId(), ++index);
1621 RtlInitUnicodeString(&nt_name, name);
1622 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1623 0, FILE_OVERWRITE_IF,
1624 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1625 FALSE, FALSE, FALSE,
1626 1, size, size, &timeout);
1627 if (status)
1629 SetLastError( RtlNtStatusToDosError(status) );
1630 hr = INVALID_HANDLE_VALUE;
1632 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1633 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1634 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1636 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1637 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1639 if (status)
1641 SetLastError( RtlNtStatusToDosError(status) );
1642 NtClose(hr);
1643 return FALSE;
1646 *hReadPipe = hr;
1647 *hWritePipe = hw;
1648 return TRUE;
1652 /******************************************************************************
1653 * CreateMailslotA [KERNEL32.@]
1655 * See CreatMailslotW.
1657 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1658 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1660 DWORD len;
1661 HANDLE handle;
1662 LPWSTR name = NULL;
1664 TRACE("%s %ld %ld %p\n", debugstr_a(lpName),
1665 nMaxMessageSize, lReadTimeout, sa);
1667 if( lpName )
1669 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1670 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1671 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1674 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1676 HeapFree( GetProcessHeap(), 0, name );
1678 return handle;
1682 /******************************************************************************
1683 * CreateMailslotW [KERNEL32.@]
1685 * Create a mailslot with specified name.
1687 * PARAMS
1688 * lpName [I] Pointer to string for mailslot name
1689 * nMaxMessageSize [I] Maximum message size
1690 * lReadTimeout [I] Milliseconds before read time-out
1691 * sa [I] Pointer to security structure
1693 * RETURNS
1694 * Success: Handle to mailslot
1695 * Failure: INVALID_HANDLE_VALUE
1697 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1698 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1700 HANDLE handle = INVALID_HANDLE_VALUE;
1701 OBJECT_ATTRIBUTES attr;
1702 UNICODE_STRING nameW;
1703 LARGE_INTEGER timeout;
1704 IO_STATUS_BLOCK iosb;
1705 NTSTATUS status;
1707 TRACE("%s %ld %ld %p\n", debugstr_w(lpName),
1708 nMaxMessageSize, lReadTimeout, sa);
1710 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1712 SetLastError( ERROR_PATH_NOT_FOUND );
1713 return INVALID_HANDLE_VALUE;
1716 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1718 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1719 RtlFreeUnicodeString( &nameW );
1720 return INVALID_HANDLE_VALUE;
1723 attr.Length = sizeof(attr);
1724 attr.RootDirectory = 0;
1725 attr.Attributes = OBJ_CASE_INSENSITIVE;
1726 attr.ObjectName = &nameW;
1727 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1728 attr.SecurityQualityOfService = NULL;
1730 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1731 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1732 else
1733 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1735 status = NtCreateMailslotFile( &handle, GENERIC_READ | GENERIC_WRITE, &attr,
1736 &iosb, 0, 0, nMaxMessageSize, &timeout );
1737 if (status)
1739 SetLastError( RtlNtStatusToDosError(status) );
1740 handle = INVALID_HANDLE_VALUE;
1743 RtlFreeUnicodeString( &nameW );
1744 return handle;
1748 /******************************************************************************
1749 * GetMailslotInfo [KERNEL32.@]
1751 * Retrieve information about a mailslot.
1753 * PARAMS
1754 * hMailslot [I] Mailslot handle
1755 * lpMaxMessageSize [O] Address of maximum message size
1756 * lpNextSize [O] Address of size of next message
1757 * lpMessageCount [O] Address of number of messages
1758 * lpReadTimeout [O] Address of read time-out
1760 * RETURNS
1761 * Success: TRUE
1762 * Failure: FALSE
1764 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1765 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1766 LPDWORD lpReadTimeout )
1768 FILE_MAILSLOT_QUERY_INFORMATION info;
1769 IO_STATUS_BLOCK iosb;
1770 NTSTATUS status;
1772 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1773 lpNextSize, lpMessageCount, lpReadTimeout);
1775 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1776 FileMailslotQueryInformation );
1778 if( status != STATUS_SUCCESS )
1780 SetLastError( RtlNtStatusToDosError(status) );
1781 return FALSE;
1784 if( lpMaxMessageSize )
1785 *lpMaxMessageSize = info.MaximumMessageSize;
1786 if( lpNextSize )
1787 *lpNextSize = info.NextMessageSize;
1788 if( lpMessageCount )
1789 *lpMessageCount = info.MessagesAvailable;
1790 if( lpReadTimeout )
1791 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1793 return TRUE;
1797 /******************************************************************************
1798 * SetMailslotInfo [KERNEL32.@]
1800 * Set the read timeout of a mailslot.
1802 * PARAMS
1803 * hMailslot [I] Mailslot handle
1804 * dwReadTimeout [I] Timeout in milliseconds.
1806 * RETURNS
1807 * Success: TRUE
1808 * Failure: FALSE
1810 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1812 FILE_MAILSLOT_SET_INFORMATION info;
1813 IO_STATUS_BLOCK iosb;
1814 NTSTATUS status;
1816 TRACE("%p %ld\n", hMailslot, dwReadTimeout);
1818 info.ReadTimeout.QuadPart = dwReadTimeout * -10000;
1819 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1820 FileMailslotSetInformation );
1821 if( status != STATUS_SUCCESS )
1823 SetLastError( RtlNtStatusToDosError(status) );
1824 return FALSE;
1826 return TRUE;
1830 /******************************************************************************
1831 * CreateIoCompletionPort (KERNEL32.@)
1833 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1834 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1836 FIXME("(%p, %p, %08lx, %08lx): stub.\n",
1837 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1838 return NULL;
1842 /******************************************************************************
1843 * GetQueuedCompletionStatus (KERNEL32.@)
1845 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1846 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1847 DWORD dwMilliseconds )
1849 FIXME("(%p,%p,%p,%p,%ld), stub!\n",
1850 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1851 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1852 return FALSE;
1855 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
1856 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
1858 FIXME("%p %ld %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
1859 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1860 return FALSE;
1863 /******************************************************************************
1864 * CreateJobObjectW (KERNEL32.@)
1866 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1868 FIXME("%p %s\n", attr, debugstr_w(name) );
1869 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1870 return 0;
1873 /******************************************************************************
1874 * CreateJobObjectA (KERNEL32.@)
1876 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1878 LPWSTR str = NULL;
1879 UINT len;
1880 HANDLE r;
1882 TRACE("%p %s\n", attr, debugstr_a(name) );
1884 if( name )
1886 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1887 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1888 if( !str )
1890 SetLastError( ERROR_OUTOFMEMORY );
1891 return 0;
1893 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1896 r = CreateJobObjectW( attr, str );
1898 HeapFree( GetProcessHeap(), 0, str );
1900 return r;
1903 /******************************************************************************
1904 * AssignProcessToJobObject (KERNEL32.@)
1906 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1908 FIXME("%p %p\n", hJob, hProcess);
1909 return TRUE;
1912 #ifdef __i386__
1914 /***********************************************************************
1915 * InterlockedCompareExchange (KERNEL32.@)
1917 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1918 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1919 "movl 12(%esp),%eax\n\t"
1920 "movl 8(%esp),%ecx\n\t"
1921 "movl 4(%esp),%edx\n\t"
1922 "lock; cmpxchgl %ecx,(%edx)\n\t"
1923 "ret $12")
1925 /***********************************************************************
1926 * InterlockedExchange (KERNEL32.@)
1928 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1929 __ASM_GLOBAL_FUNC(InterlockedExchange,
1930 "movl 8(%esp),%eax\n\t"
1931 "movl 4(%esp),%edx\n\t"
1932 "lock; xchgl %eax,(%edx)\n\t"
1933 "ret $8")
1935 /***********************************************************************
1936 * InterlockedExchangeAdd (KERNEL32.@)
1938 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1939 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1940 "movl 8(%esp),%eax\n\t"
1941 "movl 4(%esp),%edx\n\t"
1942 "lock; xaddl %eax,(%edx)\n\t"
1943 "ret $8")
1945 /***********************************************************************
1946 * InterlockedIncrement (KERNEL32.@)
1948 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1949 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1950 "movl 4(%esp),%edx\n\t"
1951 "movl $1,%eax\n\t"
1952 "lock; xaddl %eax,(%edx)\n\t"
1953 "incl %eax\n\t"
1954 "ret $4")
1956 /***********************************************************************
1957 * InterlockedDecrement (KERNEL32.@)
1959 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1960 "movl 4(%esp),%edx\n\t"
1961 "movl $-1,%eax\n\t"
1962 "lock; xaddl %eax,(%edx)\n\t"
1963 "decl %eax\n\t"
1964 "ret $4")
1966 #else /* __i386__ */
1968 /***********************************************************************
1969 * InterlockedCompareExchange (KERNEL32.@)
1971 * Atomically swap one value with another.
1973 * PARAMS
1974 * dest [I/O] The value to replace
1975 * xchq [I] The value to be swapped
1976 * compare [I] The value to compare to dest
1978 * RETURNS
1979 * The resulting value of dest.
1981 * NOTES
1982 * dest is updated only if it is equal to compare, otherwise no swap is done.
1984 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
1986 return interlocked_cmpxchg( (int *)dest, xchg, compare );
1989 /***********************************************************************
1990 * InterlockedExchange (KERNEL32.@)
1992 * Atomically swap one value with another.
1994 * PARAMS
1995 * dest [I/O] The value to replace
1996 * val [I] The value to be swapped
1998 * RETURNS
1999 * The resulting value of dest.
2001 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
2003 return interlocked_xchg( (int *)dest, val );
2006 /***********************************************************************
2007 * InterlockedExchangeAdd (KERNEL32.@)
2009 * Atomically add one value to another.
2011 * PARAMS
2012 * dest [I/O] The value to add to
2013 * incr [I] The value to be added
2015 * RETURNS
2016 * The resulting value of dest.
2018 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
2020 return interlocked_xchg_add( (int *)dest, incr );
2023 /***********************************************************************
2024 * InterlockedIncrement (KERNEL32.@)
2026 * Atomically increment a value.
2028 * PARAMS
2029 * dest [I/O] The value to increment
2031 * RETURNS
2032 * The resulting value of dest.
2034 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
2036 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
2039 /***********************************************************************
2040 * InterlockedDecrement (KERNEL32.@)
2042 * Atomically decrement a value.
2044 * PARAMS
2045 * dest [I/O] The value to decrement
2047 * RETURNS
2048 * The resulting value of dest.
2050 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
2052 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
2055 #endif /* __i386__ */