Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / kernel / sync.c
blobce2c4a1cd7323a62220f998308240f2fcd27d58a
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 "winreg.h"
53 #include "winternl.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 = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
449 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
450 attr.SecurityQualityOfService = NULL;
451 if (name)
453 RtlInitUnicodeString( &nameW, name );
454 attr.ObjectName = &nameW;
457 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
458 SetLastError( RtlNtStatusToDosError(status) );
459 return ret;
463 /***********************************************************************
464 * CreateW32Event (KERNEL.457)
466 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
468 return CreateEventW( NULL, manual_reset, initial_state, NULL );
472 /***********************************************************************
473 * OpenEventA (KERNEL32.@)
475 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
477 WCHAR buffer[MAX_PATH];
479 if (!name) return OpenEventW( access, inherit, NULL );
481 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
483 SetLastError( ERROR_FILENAME_EXCED_RANGE );
484 return 0;
486 return OpenEventW( access, inherit, buffer );
490 /***********************************************************************
491 * OpenEventW (KERNEL32.@)
493 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
495 HANDLE ret;
496 UNICODE_STRING nameW;
497 OBJECT_ATTRIBUTES attr;
498 NTSTATUS status;
500 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
502 attr.Length = sizeof(attr);
503 attr.RootDirectory = 0;
504 attr.ObjectName = NULL;
505 attr.Attributes = inherit ? OBJ_INHERIT : 0;
506 attr.SecurityDescriptor = NULL;
507 attr.SecurityQualityOfService = NULL;
508 if (name)
510 RtlInitUnicodeString( &nameW, name );
511 attr.ObjectName = &nameW;
514 status = NtOpenEvent( &ret, access, &attr );
515 if (status != STATUS_SUCCESS)
517 SetLastError( RtlNtStatusToDosError(status) );
518 return 0;
520 return ret;
523 /***********************************************************************
524 * PulseEvent (KERNEL32.@)
526 BOOL WINAPI PulseEvent( HANDLE handle )
528 NTSTATUS status;
530 if ((status = NtPulseEvent( handle, NULL )))
531 SetLastError( RtlNtStatusToDosError(status) );
532 return !status;
536 /***********************************************************************
537 * SetW32Event (KERNEL.458)
538 * SetEvent (KERNEL32.@)
540 BOOL WINAPI SetEvent( HANDLE handle )
542 NTSTATUS status;
544 if ((status = NtSetEvent( handle, NULL )))
545 SetLastError( RtlNtStatusToDosError(status) );
546 return !status;
550 /***********************************************************************
551 * ResetW32Event (KERNEL.459)
552 * ResetEvent (KERNEL32.@)
554 BOOL WINAPI ResetEvent( HANDLE handle )
556 NTSTATUS status;
558 if ((status = NtResetEvent( handle, NULL )))
559 SetLastError( RtlNtStatusToDosError(status) );
560 return !status;
564 /***********************************************************************
565 * NOTE: The Win95 VWin32_Event routines given below are really low-level
566 * routines implemented directly by VWin32. The user-mode libraries
567 * implement Win32 synchronisation routines on top of these low-level
568 * primitives. We do it the other way around here :-)
571 /***********************************************************************
572 * VWin32_EventCreate (KERNEL.442)
574 HANDLE WINAPI VWin32_EventCreate(VOID)
576 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
577 return ConvertToGlobalHandle( hEvent );
580 /***********************************************************************
581 * VWin32_EventDestroy (KERNEL.443)
583 VOID WINAPI VWin32_EventDestroy(HANDLE event)
585 CloseHandle( event );
588 /***********************************************************************
589 * VWin32_EventWait (KERNEL.450)
591 VOID WINAPI VWin32_EventWait(HANDLE event)
593 DWORD mutex_count;
595 ReleaseThunkLock( &mutex_count );
596 WaitForSingleObject( event, INFINITE );
597 RestoreThunkLock( mutex_count );
600 /***********************************************************************
601 * VWin32_EventSet (KERNEL.451)
602 * KERNEL_479 (KERNEL.479)
604 VOID WINAPI VWin32_EventSet(HANDLE event)
606 SetEvent( event );
611 /***********************************************************************
612 * CreateMutexA (KERNEL32.@)
614 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
616 WCHAR buffer[MAX_PATH];
618 if (!name) return CreateMutexW( sa, owner, NULL );
620 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
622 SetLastError( ERROR_FILENAME_EXCED_RANGE );
623 return 0;
625 return CreateMutexW( sa, owner, buffer );
629 /***********************************************************************
630 * CreateMutexW (KERNEL32.@)
632 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
634 HANDLE ret;
635 UNICODE_STRING nameW;
636 OBJECT_ATTRIBUTES attr;
637 NTSTATUS status;
639 attr.Length = sizeof(attr);
640 attr.RootDirectory = 0;
641 attr.ObjectName = NULL;
642 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
643 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
644 attr.SecurityQualityOfService = NULL;
645 if (name)
647 RtlInitUnicodeString( &nameW, name );
648 attr.ObjectName = &nameW;
651 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
652 SetLastError( RtlNtStatusToDosError(status) );
653 return ret;
657 /***********************************************************************
658 * OpenMutexA (KERNEL32.@)
660 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
662 WCHAR buffer[MAX_PATH];
664 if (!name) return OpenMutexW( access, inherit, NULL );
666 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
668 SetLastError( ERROR_FILENAME_EXCED_RANGE );
669 return 0;
671 return OpenMutexW( access, inherit, buffer );
675 /***********************************************************************
676 * OpenMutexW (KERNEL32.@)
678 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
680 HANDLE ret;
681 UNICODE_STRING nameW;
682 OBJECT_ATTRIBUTES attr;
683 NTSTATUS status;
685 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
687 attr.Length = sizeof(attr);
688 attr.RootDirectory = 0;
689 attr.ObjectName = NULL;
690 attr.Attributes = inherit ? OBJ_INHERIT : 0;
691 attr.SecurityDescriptor = NULL;
692 attr.SecurityQualityOfService = NULL;
693 if (name)
695 RtlInitUnicodeString( &nameW, name );
696 attr.ObjectName = &nameW;
699 status = NtOpenMutant( &ret, access, &attr );
700 if (status != STATUS_SUCCESS)
702 SetLastError( RtlNtStatusToDosError(status) );
703 return 0;
705 return ret;
709 /***********************************************************************
710 * ReleaseMutex (KERNEL32.@)
712 BOOL WINAPI ReleaseMutex( HANDLE handle )
714 NTSTATUS status;
716 status = NtReleaseMutant(handle, NULL);
717 if (status != STATUS_SUCCESS)
719 SetLastError( RtlNtStatusToDosError(status) );
720 return FALSE;
722 return TRUE;
727 * Semaphores
731 /***********************************************************************
732 * CreateSemaphoreA (KERNEL32.@)
734 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
736 WCHAR buffer[MAX_PATH];
738 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
740 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
742 SetLastError( ERROR_FILENAME_EXCED_RANGE );
743 return 0;
745 return CreateSemaphoreW( sa, initial, max, buffer );
749 /***********************************************************************
750 * CreateSemaphoreW (KERNEL32.@)
752 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
753 LONG max, LPCWSTR name )
755 HANDLE ret;
756 UNICODE_STRING nameW;
757 OBJECT_ATTRIBUTES attr;
758 NTSTATUS status;
760 attr.Length = sizeof(attr);
761 attr.RootDirectory = 0;
762 attr.ObjectName = NULL;
763 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
764 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
765 attr.SecurityQualityOfService = NULL;
766 if (name)
768 RtlInitUnicodeString( &nameW, name );
769 attr.ObjectName = &nameW;
772 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
773 SetLastError( RtlNtStatusToDosError(status) );
774 return ret;
778 /***********************************************************************
779 * OpenSemaphoreA (KERNEL32.@)
781 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
783 WCHAR buffer[MAX_PATH];
785 if (!name) return OpenSemaphoreW( access, inherit, NULL );
787 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
789 SetLastError( ERROR_FILENAME_EXCED_RANGE );
790 return 0;
792 return OpenSemaphoreW( access, inherit, buffer );
796 /***********************************************************************
797 * OpenSemaphoreW (KERNEL32.@)
799 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
801 HANDLE ret;
802 UNICODE_STRING nameW;
803 OBJECT_ATTRIBUTES attr;
804 NTSTATUS status;
806 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
808 attr.Length = sizeof(attr);
809 attr.RootDirectory = 0;
810 attr.ObjectName = NULL;
811 attr.Attributes = inherit ? OBJ_INHERIT : 0;
812 attr.SecurityDescriptor = NULL;
813 attr.SecurityQualityOfService = NULL;
814 if (name)
816 RtlInitUnicodeString( &nameW, name );
817 attr.ObjectName = &nameW;
820 status = NtOpenSemaphore( &ret, access, &attr );
821 if (status != STATUS_SUCCESS)
823 SetLastError( RtlNtStatusToDosError(status) );
824 return 0;
826 return ret;
830 /***********************************************************************
831 * ReleaseSemaphore (KERNEL32.@)
833 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
835 NTSTATUS status = NtReleaseSemaphore( handle, count, previous );
836 if (status) SetLastError( RtlNtStatusToDosError(status) );
837 return !status;
842 * Timers
846 /***********************************************************************
847 * CreateWaitableTimerA (KERNEL32.@)
849 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
851 WCHAR buffer[MAX_PATH];
853 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
855 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
857 SetLastError( ERROR_FILENAME_EXCED_RANGE );
858 return 0;
860 return CreateWaitableTimerW( sa, manual, buffer );
864 /***********************************************************************
865 * CreateWaitableTimerW (KERNEL32.@)
867 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
869 HANDLE handle;
870 NTSTATUS status;
871 UNICODE_STRING nameW;
872 OBJECT_ATTRIBUTES attr;
874 attr.Length = sizeof(attr);
875 attr.RootDirectory = 0;
876 attr.ObjectName = NULL;
877 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
878 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
879 attr.SecurityQualityOfService = NULL;
880 if (name)
882 RtlInitUnicodeString( &nameW, name );
883 attr.ObjectName = &nameW;
886 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
887 manual ? NotificationTimer : SynchronizationTimer);
888 SetLastError( RtlNtStatusToDosError(status) );
889 return handle;
893 /***********************************************************************
894 * OpenWaitableTimerA (KERNEL32.@)
896 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
898 WCHAR buffer[MAX_PATH];
900 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
902 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
904 SetLastError( ERROR_FILENAME_EXCED_RANGE );
905 return 0;
907 return OpenWaitableTimerW( access, inherit, buffer );
911 /***********************************************************************
912 * OpenWaitableTimerW (KERNEL32.@)
914 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
916 HANDLE handle;
917 UNICODE_STRING nameW;
918 OBJECT_ATTRIBUTES attr;
919 NTSTATUS status;
921 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
923 attr.Length = sizeof(attr);
924 attr.RootDirectory = 0;
925 attr.ObjectName = NULL;
926 attr.Attributes = inherit ? OBJ_INHERIT : 0;
927 attr.SecurityDescriptor = NULL;
928 attr.SecurityQualityOfService = NULL;
929 if (name)
931 RtlInitUnicodeString( &nameW, name );
932 attr.ObjectName = &nameW;
935 status = NtOpenTimer(&handle, access, &attr);
936 if (status != STATUS_SUCCESS)
938 SetLastError( RtlNtStatusToDosError(status) );
939 return 0;
941 return handle;
945 /***********************************************************************
946 * SetWaitableTimer (KERNEL32.@)
948 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
949 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
951 NTSTATUS status = NtSetTimer(handle, when, callback, arg, resume, period, NULL);
953 if (status != STATUS_SUCCESS)
955 SetLastError( RtlNtStatusToDosError(status) );
956 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
958 return TRUE;
962 /***********************************************************************
963 * CancelWaitableTimer (KERNEL32.@)
965 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
967 NTSTATUS status;
969 status = NtCancelTimer(handle, NULL);
970 if (status != STATUS_SUCCESS)
972 SetLastError( RtlNtStatusToDosError(status) );
973 return FALSE;
975 return TRUE;
979 /***********************************************************************
980 * CreateTimerQueue (KERNEL32.@)
982 HANDLE WINAPI CreateTimerQueue(void)
984 FIXME("stub\n");
985 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
986 return NULL;
990 /***********************************************************************
991 * DeleteTimerQueueEx (KERNEL32.@)
993 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
995 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
996 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
997 return 0;
1000 /***********************************************************************
1001 * CreateTimerQueueTimer (KERNEL32.@)
1003 * Creates a timer-queue timer. This timer expires at the specified due
1004 * time (in ms), then after every specified period (in ms). When the timer
1005 * expires, the callback function is called.
1007 * RETURNS
1008 * nonzero on success or zero on faillure
1010 * BUGS
1011 * Unimplemented
1013 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1014 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1015 DWORD DueTime, DWORD Period, ULONG Flags )
1017 FIXME("stub\n");
1018 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1019 return TRUE;
1022 /***********************************************************************
1023 * DeleteTimerQueueTimer (KERNEL32.@)
1025 * Cancels a timer-queue timer.
1027 * RETURNS
1028 * nonzero on success or zero on faillure
1030 * BUGS
1031 * Unimplemented
1033 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1034 HANDLE CompletionEvent )
1036 FIXME("stub\n");
1037 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1038 return TRUE;
1043 * Pipes
1047 /***********************************************************************
1048 * CreateNamedPipeA (KERNEL32.@)
1050 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1051 DWORD dwPipeMode, DWORD nMaxInstances,
1052 DWORD nOutBufferSize, DWORD nInBufferSize,
1053 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1055 WCHAR buffer[MAX_PATH];
1057 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1058 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1060 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1062 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1063 return INVALID_HANDLE_VALUE;
1065 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1066 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1070 /***********************************************************************
1071 * CreateNamedPipeW (KERNEL32.@)
1073 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1074 DWORD dwPipeMode, DWORD nMaxInstances,
1075 DWORD nOutBufferSize, DWORD nInBufferSize,
1076 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1078 HANDLE handle;
1079 UNICODE_STRING nt_name;
1080 OBJECT_ATTRIBUTES attr;
1081 DWORD options;
1082 BOOLEAN pipe_type, read_mode, non_block;
1083 NTSTATUS status;
1084 IO_STATUS_BLOCK iosb;
1085 LARGE_INTEGER timeout;
1087 TRACE("(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n",
1088 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1089 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1091 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1093 SetLastError( ERROR_PATH_NOT_FOUND );
1094 return INVALID_HANDLE_VALUE;
1096 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1098 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1099 RtlFreeUnicodeString( &nt_name );
1100 return INVALID_HANDLE_VALUE;
1103 attr.Length = sizeof(attr);
1104 attr.RootDirectory = 0;
1105 attr.ObjectName = &nt_name;
1106 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
1107 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1108 attr.SecurityQualityOfService = NULL;
1110 options = 0;
1111 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1112 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1113 if ((dwOpenMode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
1114 options |= FILE_PIPE_FULL_DUPLEX;
1115 else if (dwOpenMode & PIPE_ACCESS_INBOUND) options |= FILE_PIPE_INBOUND;
1116 else if (dwOpenMode & PIPE_ACCESS_OUTBOUND) options |= FILE_PIPE_OUTBOUND;
1117 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1118 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1119 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1120 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0UL;
1122 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1124 SetLastError(0);
1126 status = NtCreateNamedPipeFile(&handle, 0, &attr, &iosb, 0, FILE_OVERWRITE_IF,
1127 options, pipe_type, read_mode, non_block,
1128 nMaxInstances, nInBufferSize, nOutBufferSize,
1129 &timeout);
1131 RtlFreeUnicodeString( &nt_name );
1132 if (status)
1134 handle = INVALID_HANDLE_VALUE;
1135 SetLastError( RtlNtStatusToDosError(status) );
1137 return handle;
1141 /***********************************************************************
1142 * PeekNamedPipe (KERNEL32.@)
1144 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1145 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1147 #ifdef FIONREAD
1148 int avail=0, fd, ret, flags;
1150 TRACE("(%p,%p,%lu,%p,%p,%p)\n", hPipe, lpvBuffer, cbBuffer, lpcbRead, lpcbAvail, lpcbMessage);
1152 ret = wine_server_handle_to_fd( hPipe, GENERIC_READ, &fd, &flags );
1153 if (ret)
1155 SetLastError( RtlNtStatusToDosError(ret) );
1156 return FALSE;
1158 if (flags & FD_FLAG_RECV_SHUTDOWN)
1160 wine_server_release_fd( hPipe, fd );
1161 SetLastError ( ERROR_PIPE_NOT_CONNECTED );
1162 return FALSE;
1165 if (ioctl(fd,FIONREAD, &avail ) != 0)
1167 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1168 wine_server_release_fd( hPipe, fd );
1169 return FALSE;
1171 if (!avail) /* check for closed pipe */
1173 struct pollfd pollfd;
1174 pollfd.fd = fd;
1175 pollfd.events = POLLIN;
1176 pollfd.revents = 0;
1177 switch (poll( &pollfd, 1, 0 ))
1179 case 0:
1180 break;
1181 case 1: /* got something */
1182 if (!(pollfd.revents & (POLLHUP | POLLERR))) break;
1183 TRACE("POLLHUP | POLLERR\n");
1184 /* fall through */
1185 case -1:
1186 wine_server_release_fd( hPipe, fd );
1187 SetLastError(ERROR_BROKEN_PIPE);
1188 return FALSE;
1191 TRACE(" 0x%08x bytes available\n", avail );
1192 ret = TRUE;
1193 if (lpcbAvail)
1194 *lpcbAvail = avail;
1195 if (lpcbRead)
1196 *lpcbRead = 0;
1197 if (avail && lpvBuffer && cbBuffer)
1199 int readbytes = (avail < cbBuffer) ? avail : cbBuffer;
1200 readbytes = recv(fd, lpvBuffer, readbytes, MSG_PEEK);
1201 if (readbytes < 0)
1203 WARN("failed to peek socket (%d)\n", errno);
1204 ret = FALSE;
1206 else if (lpcbRead)
1207 *lpcbRead = readbytes;
1209 wine_server_release_fd( hPipe, fd );
1210 return ret;
1211 #endif /* defined(FIONREAD) */
1213 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1214 FIXME("function not implemented\n");
1215 return FALSE;
1218 /***********************************************************************
1219 * PIPE_CompletionWait (Internal)
1221 static void CALLBACK PIPE_CompletionWait(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
1223 LPOVERLAPPED ovlp = (LPOVERLAPPED)user;
1225 TRACE("for %p/%p, status=%08lx\n", ovlp, iosb, status);
1227 if (ovlp)
1229 ovlp->Internal = status;
1230 SetEvent(ovlp->hEvent);
1232 TRACE("done\n");
1235 /***********************************************************************
1236 * WaitNamedPipeA (KERNEL32.@)
1238 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1240 WCHAR buffer[MAX_PATH];
1242 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1244 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1246 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1247 return 0;
1249 return WaitNamedPipeW( buffer, nTimeOut );
1253 /***********************************************************************
1254 * WaitNamedPipeW (KERNEL32.@)
1256 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1258 BOOL ret;
1259 OVERLAPPED ov;
1260 UNICODE_STRING nt_name;
1261 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1263 TRACE("%s 0x%08lx\n",debugstr_w(name),nTimeOut);
1265 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1266 return FALSE;
1268 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1270 RtlFreeUnicodeString( &nt_name );
1271 return FALSE;
1273 if (nt_name.Length < sizeof(leadin) ||
1274 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(leadin[0])))
1276 RtlFreeUnicodeString( &nt_name );
1277 return FALSE;
1280 memset(&ov,0,sizeof(ov));
1281 ov.hEvent = CreateEventW( NULL, 0, 0, NULL );
1282 if (!ov.hEvent)
1283 return FALSE;
1285 SERVER_START_REQ( wait_named_pipe )
1287 req->timeout = nTimeOut;
1288 req->overlapped = &ov;
1289 req->func = PIPE_CompletionWait;
1290 wine_server_add_data( req, nt_name.Buffer + 4, nt_name.Length - 4*sizeof(WCHAR) );
1291 ret = !wine_server_call_err( req );
1293 SERVER_END_REQ;
1295 RtlFreeUnicodeString( &nt_name );
1297 if(ret)
1299 if (WAIT_OBJECT_0==WaitForSingleObject(ov.hEvent,INFINITE))
1301 SetLastError(RtlNtStatusToDosError(ov.Internal));
1302 ret = (ov.Internal==STATUS_SUCCESS);
1305 CloseHandle(ov.hEvent);
1306 return ret;
1310 /***********************************************************************
1311 * ConnectNamedPipe (KERNEL32.@)
1313 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1315 BOOL ret;
1316 LPOVERLAPPED pov;
1317 OVERLAPPED ov;
1319 TRACE("(%p,%p)\n", hPipe, overlapped);
1321 if (!overlapped)
1323 memset(&ov, 0, sizeof(ov));
1324 ov.hEvent = CreateEventW(NULL, 0, 0, NULL);
1325 if (!ov.hEvent) return FALSE;
1326 pov = &ov;
1328 else pov = overlapped;
1330 pov->Internal = STATUS_PENDING;
1332 SERVER_START_REQ( connect_named_pipe )
1334 req->handle = hPipe;
1335 req->overlapped = pov;
1336 req->func = PIPE_CompletionWait;
1337 ret = !wine_server_call_err( req );
1339 SERVER_END_REQ;
1341 if (ret)
1343 if (overlapped)
1345 SetLastError( ERROR_IO_PENDING );
1346 ret = FALSE;
1348 else
1350 ret = GetOverlappedResult(hPipe, &ov, NULL, TRUE);
1351 CloseHandle(ov.hEvent);
1355 return ret;
1358 /***********************************************************************
1359 * DisconnectNamedPipe (KERNEL32.@)
1361 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1363 BOOL ret;
1365 TRACE("(%p)\n",hPipe);
1367 SERVER_START_REQ( disconnect_named_pipe )
1369 req->handle = hPipe;
1370 ret = !wine_server_call_err( req );
1371 if (ret && reply->fd != -1) close( reply->fd );
1373 SERVER_END_REQ;
1375 return ret;
1378 /***********************************************************************
1379 * TransactNamedPipe (KERNEL32.@)
1381 * BUGS
1382 * should be done as a single operation in the wineserver or kernel
1384 BOOL WINAPI TransactNamedPipe(
1385 HANDLE handle, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1386 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1388 BOOL r;
1389 DWORD count;
1391 TRACE("%p %p %ld %p %ld %p %p\n",
1392 handle, lpInput, dwInputSize, lpOutput,
1393 dwOutputSize, lpBytesRead, lpOverlapped);
1395 if (lpOverlapped)
1397 FIXME("Doesn't support overlapped operation as yet\n");
1398 return FALSE;
1401 r = WriteFile(handle, lpOutput, dwOutputSize, &count, NULL);
1402 if (r)
1403 r = ReadFile(handle, lpInput, dwInputSize, lpBytesRead, NULL);
1405 return r;
1408 /***********************************************************************
1409 * GetNamedPipeInfo (KERNEL32.@)
1411 BOOL WINAPI GetNamedPipeInfo(
1412 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1413 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1415 BOOL ret;
1417 TRACE("%p %p %p %p %p\n", hNamedPipe, lpFlags,
1418 lpOutputBufferSize, lpInputBufferSize, lpMaxInstances);
1420 SERVER_START_REQ( get_named_pipe_info )
1422 req->handle = hNamedPipe;
1423 ret = !wine_server_call_err( req );
1424 if (lpFlags)
1426 *lpFlags = 0;
1427 if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)
1428 *lpFlags |= PIPE_TYPE_MESSAGE;
1429 if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
1430 *lpFlags |= PIPE_READMODE_MESSAGE;
1431 if (reply->flags & NAMED_PIPE_NONBLOCKING_MODE)
1432 *lpFlags |= PIPE_NOWAIT;
1434 if (lpOutputBufferSize) *lpOutputBufferSize = reply->outsize;
1435 if (lpInputBufferSize) *lpInputBufferSize = reply->outsize;
1436 if (lpMaxInstances) *lpMaxInstances = reply->maxinstances;
1438 SERVER_END_REQ;
1440 return ret;
1443 /***********************************************************************
1444 * GetNamedPipeHandleStateA (KERNEL32.@)
1446 BOOL WINAPI GetNamedPipeHandleStateA(
1447 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1448 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1449 LPSTR lpUsername, DWORD nUsernameMaxSize)
1451 FIXME("%p %p %p %p %p %p %ld\n",
1452 hNamedPipe, lpState, lpCurInstances,
1453 lpMaxCollectionCount, lpCollectDataTimeout,
1454 lpUsername, nUsernameMaxSize);
1456 return FALSE;
1459 /***********************************************************************
1460 * GetNamedPipeHandleStateW (KERNEL32.@)
1462 BOOL WINAPI GetNamedPipeHandleStateW(
1463 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1464 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1465 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1467 FIXME("%p %p %p %p %p %p %ld\n",
1468 hNamedPipe, lpState, lpCurInstances,
1469 lpMaxCollectionCount, lpCollectDataTimeout,
1470 lpUsername, nUsernameMaxSize);
1472 return FALSE;
1475 /***********************************************************************
1476 * SetNamedPipeHandleState (KERNEL32.@)
1478 BOOL WINAPI SetNamedPipeHandleState(
1479 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1480 LPDWORD lpCollectDataTimeout)
1482 FIXME("%p %p %p %p\n",
1483 hNamedPipe, lpMode, lpMaxCollectionCount, lpCollectDataTimeout);
1484 return FALSE;
1487 /***********************************************************************
1488 * CallNamedPipeA (KERNEL32.@)
1490 BOOL WINAPI CallNamedPipeA(
1491 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1492 LPVOID lpOutput, DWORD dwOutputSize,
1493 LPDWORD lpBytesRead, DWORD nTimeout)
1495 DWORD len;
1496 LPWSTR str = NULL;
1497 BOOL ret;
1499 TRACE("%s %p %ld %p %ld %p %ld\n",
1500 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1501 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1503 if( lpNamedPipeName )
1505 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1506 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1507 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1509 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1510 dwOutputSize, lpBytesRead, nTimeout );
1511 if( lpNamedPipeName )
1512 HeapFree( GetProcessHeap(), 0, str );
1514 return ret;
1517 /***********************************************************************
1518 * CallNamedPipeW (KERNEL32.@)
1520 BOOL WINAPI CallNamedPipeW(
1521 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1522 LPVOID lpOutput, DWORD lpOutputSize,
1523 LPDWORD lpBytesRead, DWORD nTimeout)
1525 FIXME("%s %p %ld %p %ld %p %ld\n",
1526 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1527 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1528 return FALSE;
1531 /******************************************************************
1532 * CreatePipe (KERNEL32.@)
1535 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1536 LPSECURITY_ATTRIBUTES sa, DWORD size )
1538 static unsigned index = 0;
1539 WCHAR name[64];
1540 HANDLE hr, hw;
1541 unsigned in_index = index;
1543 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1544 /* generate a unique pipe name (system wide) */
1547 static const WCHAR nameFmt[] = { '\\','\\','.','\\','p','i','p','e',
1548 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1549 'u','.','%','0','8','u','\0' };
1550 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1551 GetCurrentProcessId(), ++index);
1552 hr = CreateNamedPipeW(name, PIPE_ACCESS_INBOUND,
1553 PIPE_TYPE_BYTE | PIPE_WAIT, 1, size, size,
1554 NMPWAIT_USE_DEFAULT_WAIT, sa);
1555 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1556 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1557 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1559 hw = CreateFileW(name, GENERIC_WRITE, 0, sa, OPEN_EXISTING, 0, 0);
1560 if (hw == INVALID_HANDLE_VALUE)
1562 CloseHandle(hr);
1563 return FALSE;
1566 *hReadPipe = hr;
1567 *hWritePipe = hw;
1568 return TRUE;
1572 /******************************************************************************
1573 * CreateMailslotA [KERNEL32.@]
1575 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1576 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1578 DWORD len;
1579 HANDLE handle;
1580 LPWSTR name = NULL;
1582 TRACE("%s %ld %ld %p\n", debugstr_a(lpName),
1583 nMaxMessageSize, lReadTimeout, sa);
1585 if( lpName )
1587 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1588 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1589 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1592 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1594 HeapFree( GetProcessHeap(), 0, name );
1596 return handle;
1600 /******************************************************************************
1601 * CreateMailslotW [KERNEL32.@]
1603 * Create a mailslot with specified name.
1605 * PARAMS
1606 * lpName [I] Pointer to string for mailslot name
1607 * nMaxMessageSize [I] Maximum message size
1608 * lReadTimeout [I] Milliseconds before read time-out
1609 * sa [I] Pointer to security structure
1611 * RETURNS
1612 * Success: Handle to mailslot
1613 * Failure: INVALID_HANDLE_VALUE
1615 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1616 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1618 HANDLE handle = INVALID_HANDLE_VALUE;
1619 OBJECT_ATTRIBUTES attr;
1620 UNICODE_STRING nameW;
1621 LARGE_INTEGER timeout;
1622 IO_STATUS_BLOCK iosb;
1623 NTSTATUS status;
1625 TRACE("%s %ld %ld %p\n", debugstr_w(lpName),
1626 nMaxMessageSize, lReadTimeout, sa);
1628 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1630 SetLastError( ERROR_PATH_NOT_FOUND );
1631 return INVALID_HANDLE_VALUE;
1634 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1636 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1637 RtlFreeUnicodeString( &nameW );
1638 return INVALID_HANDLE_VALUE;
1641 attr.Length = sizeof(attr);
1642 attr.RootDirectory = 0;
1643 attr.Attributes = OBJ_CASE_INSENSITIVE;
1644 attr.ObjectName = &nameW;
1645 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1646 attr.SecurityQualityOfService = NULL;
1648 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1650 status = NtCreateMailslotFile( &handle, GENERIC_READ | GENERIC_WRITE, &attr,
1651 &iosb, 0, 0, nMaxMessageSize, &timeout );
1652 if (status)
1654 SetLastError( RtlNtStatusToDosError(status) );
1655 handle = INVALID_HANDLE_VALUE;
1658 RtlFreeUnicodeString( &nameW );
1659 return handle;
1663 /******************************************************************************
1664 * GetMailslotInfo [KERNEL32.@]
1666 * Retrieve information about a mailslot.
1668 * PARAMS
1669 * hMailslot [I] Mailslot handle
1670 * lpMaxMessageSize [O] Address of maximum message size
1671 * lpNextSize [O] Address of size of next message
1672 * lpMessageCount [O] Address of number of messages
1673 * lpReadTimeout [O] Address of read time-out
1675 * RETURNS
1676 * Success: TRUE
1677 * Failure: FALSE
1679 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1680 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1681 LPDWORD lpReadTimeout )
1683 FILE_MAILSLOT_QUERY_INFORMATION info;
1684 IO_STATUS_BLOCK iosb;
1685 NTSTATUS status;
1687 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1688 lpNextSize, lpMessageCount, lpReadTimeout);
1690 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1691 FileMailslotQueryInformation );
1693 if( status != STATUS_SUCCESS )
1695 SetLastError( RtlNtStatusToDosError(status) );
1696 return FALSE;
1699 if( lpMaxMessageSize )
1700 *lpMaxMessageSize = info.MaximumMessageSize;
1701 if( lpNextSize )
1702 *lpNextSize = info.NextMessageSize;
1703 if( lpMessageCount )
1704 *lpMessageCount = info.MessagesAvailable;
1705 if( lpReadTimeout )
1706 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1708 return TRUE;
1712 /******************************************************************************
1713 * SetMailslotInfo [KERNEL32.@]
1715 * Set the read timeout of a mailslot.
1717 * PARAMS
1718 * hMailslot [I] Mailslot handle
1719 * dwReadTimeout [I] Timeout in milliseconds.
1721 * RETURNS
1722 * Success: TRUE
1723 * Failure: FALSE
1725 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1727 FILE_MAILSLOT_SET_INFORMATION info;
1728 IO_STATUS_BLOCK iosb;
1729 NTSTATUS status;
1731 TRACE("%p %ld\n", hMailslot, dwReadTimeout);
1733 info.ReadTimeout.QuadPart = dwReadTimeout * -10000;
1734 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1735 FileMailslotSetInformation );
1736 if( status != STATUS_SUCCESS )
1738 SetLastError( RtlNtStatusToDosError(status) );
1739 return FALSE;
1741 return TRUE;
1745 /******************************************************************************
1746 * CreateIoCompletionPort (KERNEL32.@)
1748 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1749 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1751 FIXME("(%p, %p, %08lx, %08lx): stub.\n",
1752 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1753 return NULL;
1757 /******************************************************************************
1758 * GetQueuedCompletionStatus (KERNEL32.@)
1760 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1761 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1762 DWORD dwMilliseconds )
1764 FIXME("(%p,%p,%p,%p,%ld), stub!\n",
1765 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1766 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1767 return FALSE;
1770 /******************************************************************************
1771 * CreateJobObjectW (KERNEL32.@)
1773 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1775 FIXME("%p %s\n", attr, debugstr_w(name) );
1776 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1777 return 0;
1780 /******************************************************************************
1781 * CreateJobObjectA (KERNEL32.@)
1783 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1785 LPWSTR str = NULL;
1786 UINT len;
1787 HANDLE r;
1789 TRACE("%p %s\n", attr, debugstr_a(name) );
1791 if( name )
1793 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1794 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1795 if( !str )
1797 SetLastError( ERROR_OUTOFMEMORY );
1798 return 0;
1800 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1803 r = CreateJobObjectW( attr, str );
1805 HeapFree( GetProcessHeap(), 0, str );
1807 return r;
1810 /******************************************************************************
1811 * AssignProcessToJobObject (KERNEL32.@)
1813 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1815 FIXME("%p %p\n", hJob, hProcess);
1816 return TRUE;
1819 #ifdef __i386__
1821 /***********************************************************************
1822 * InterlockedCompareExchange (KERNEL32.@)
1824 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1825 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1826 "movl 12(%esp),%eax\n\t"
1827 "movl 8(%esp),%ecx\n\t"
1828 "movl 4(%esp),%edx\n\t"
1829 "lock; cmpxchgl %ecx,(%edx)\n\t"
1830 "ret $12")
1832 /***********************************************************************
1833 * InterlockedExchange (KERNEL32.@)
1835 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1836 __ASM_GLOBAL_FUNC(InterlockedExchange,
1837 "movl 8(%esp),%eax\n\t"
1838 "movl 4(%esp),%edx\n\t"
1839 "lock; xchgl %eax,(%edx)\n\t"
1840 "ret $8")
1842 /***********************************************************************
1843 * InterlockedExchangeAdd (KERNEL32.@)
1845 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1846 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1847 "movl 8(%esp),%eax\n\t"
1848 "movl 4(%esp),%edx\n\t"
1849 "lock; xaddl %eax,(%edx)\n\t"
1850 "ret $8")
1852 /***********************************************************************
1853 * InterlockedIncrement (KERNEL32.@)
1855 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1856 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1857 "movl 4(%esp),%edx\n\t"
1858 "movl $1,%eax\n\t"
1859 "lock; xaddl %eax,(%edx)\n\t"
1860 "incl %eax\n\t"
1861 "ret $4")
1863 /***********************************************************************
1864 * InterlockedDecrement (KERNEL32.@)
1866 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1867 "movl 4(%esp),%edx\n\t"
1868 "movl $-1,%eax\n\t"
1869 "lock; xaddl %eax,(%edx)\n\t"
1870 "decl %eax\n\t"
1871 "ret $4")
1873 #else /* __i386__ */
1875 /***********************************************************************
1876 * InterlockedCompareExchange (KERNEL32.@)
1878 * Atomically swap one value with another.
1880 * PARAMS
1881 * dest [I/O] The value to replace
1882 * xchq [I] The value to be swapped
1883 * compare [I] The value to compare to dest
1885 * RETURNS
1886 * The resulting value of dest.
1888 * NOTES
1889 * dest is updated only if it is equal to compare, otherwise no swap is done.
1891 LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare )
1893 return interlocked_cmpxchg( dest, xchg, compare );
1896 /***********************************************************************
1897 * InterlockedExchange (KERNEL32.@)
1899 * Atomically swap one value with another.
1901 * PARAMS
1902 * dest [I/O] The value to replace
1903 * val [I] The value to be swapped
1905 * RETURNS
1906 * The resulting value of dest.
1908 LONG WINAPI InterlockedExchange( PLONG dest, LONG val )
1910 return interlocked_xchg( dest, val );
1913 /***********************************************************************
1914 * InterlockedExchangeAdd (KERNEL32.@)
1916 * Atomically add one value to another.
1918 * PARAMS
1919 * dest [I/O] The value to add to
1920 * incr [I] The value to be added
1922 * RETURNS
1923 * The resulting value of dest.
1925 LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr )
1927 return interlocked_xchg_add( dest, incr );
1930 /***********************************************************************
1931 * InterlockedIncrement (KERNEL32.@)
1933 * Atomically increment a value.
1935 * PARAMS
1936 * dest [I/O] The value to increment
1938 * RETURNS
1939 * The resulting value of dest.
1941 LONG WINAPI InterlockedIncrement( PLONG dest )
1943 return interlocked_xchg_add( dest, 1 ) + 1;
1946 /***********************************************************************
1947 * InterlockedDecrement (KERNEL32.@)
1949 * Atomically decrement a value.
1951 * PARAMS
1952 * dest [I/O] The value to decrement
1954 * RETURNS
1955 * The resulting value of dest.
1957 LONG WINAPI InterlockedDecrement( PLONG dest )
1959 return interlocked_xchg_add( dest, -1 ) - 1;
1962 #endif /* __i386__ */