WTSFreeMemory stub.
[wine/hacks.git] / dlls / kernel / sync.c
blob8d3f43c1b91bbfb240d630a82af6954e1ad39354
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, DWORD dwMilliseconds, BOOL bAlertable )
289 FIXME("(%p %p %ld %d): stub\n", hObjectToSignal, hObjectToWaitOn, dwMilliseconds, bAlertable);
290 return WAIT_OBJECT_0;
294 /***********************************************************************
295 * InitializeCriticalSection (KERNEL32.@)
297 * Initialise a critical section before use.
299 * PARAMS
300 * crit [O] Critical section to initialise.
302 * RETURNS
303 * Nothing. If the function fails an exception is raised.
305 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
307 NTSTATUS ret = RtlInitializeCriticalSection( crit );
308 if (ret) RtlRaiseStatus( ret );
311 /***********************************************************************
312 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
314 * Initialise a critical section with a spin count.
316 * PARAMS
317 * crit [O] Critical section to initialise.
318 * spincount [I] Number of times to spin upon contention.
320 * RETURNS
321 * Success: TRUE.
322 * Failure: Nothing. If the function fails an exception is raised.
324 * NOTES
325 * spincount is ignored on uni-processor systems.
327 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
329 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
330 if (ret) RtlRaiseStatus( ret );
331 return !ret;
334 /***********************************************************************
335 * MakeCriticalSectionGlobal (KERNEL32.@)
337 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
339 /* let's assume that only one thread at a time will try to do this */
340 HANDLE sem = crit->LockSemaphore;
341 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
342 crit->LockSemaphore = ConvertToGlobalHandle( sem );
343 if (crit->DebugInfo)
345 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
346 crit->DebugInfo = NULL;
351 /***********************************************************************
352 * ReinitializeCriticalSection (KERNEL32.@)
354 * Initialise an already used critical section.
356 * PARAMS
357 * crit [O] Critical section to initialise.
359 * RETURNS
360 * Nothing.
362 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
364 if ( !crit->LockSemaphore )
365 RtlInitializeCriticalSection( crit );
369 /***********************************************************************
370 * UninitializeCriticalSection (KERNEL32.@)
372 * UnInitialise a critical section after use.
374 * PARAMS
375 * crit [O] Critical section to uninitialise (destroy).
377 * RETURNS
378 * Nothing.
380 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
382 RtlDeleteCriticalSection( crit );
386 /***********************************************************************
387 * CreateEventA (KERNEL32.@)
389 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
390 BOOL initial_state, LPCSTR name )
392 WCHAR buffer[MAX_PATH];
394 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
396 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
398 SetLastError( ERROR_FILENAME_EXCED_RANGE );
399 return 0;
401 return CreateEventW( sa, manual_reset, initial_state, buffer );
405 /***********************************************************************
406 * CreateEventW (KERNEL32.@)
408 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
409 BOOL initial_state, LPCWSTR name )
411 HANDLE ret;
412 UNICODE_STRING nameW;
413 OBJECT_ATTRIBUTES attr;
414 NTSTATUS status;
416 /* one buggy program needs this
417 * ("Van Dale Groot woordenboek der Nederlandse taal")
419 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
421 ERR("Bad security attributes pointer %p\n",sa);
422 SetLastError( ERROR_INVALID_PARAMETER);
423 return 0;
426 attr.Length = sizeof(attr);
427 attr.RootDirectory = 0;
428 attr.ObjectName = NULL;
429 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
430 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
431 attr.SecurityQualityOfService = NULL;
432 if (name)
434 RtlInitUnicodeString( &nameW, name );
435 attr.ObjectName = &nameW;
438 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
439 SetLastError( RtlNtStatusToDosError(status) );
440 return ret;
444 /***********************************************************************
445 * CreateW32Event (KERNEL.457)
447 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
449 return CreateEventW( NULL, manual_reset, initial_state, NULL );
453 /***********************************************************************
454 * OpenEventA (KERNEL32.@)
456 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
458 WCHAR buffer[MAX_PATH];
460 if (!name) return OpenEventW( access, inherit, NULL );
462 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
464 SetLastError( ERROR_FILENAME_EXCED_RANGE );
465 return 0;
467 return OpenEventW( access, inherit, buffer );
471 /***********************************************************************
472 * OpenEventW (KERNEL32.@)
474 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
476 HANDLE ret;
477 UNICODE_STRING nameW;
478 OBJECT_ATTRIBUTES attr;
479 NTSTATUS status;
481 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
483 attr.Length = sizeof(attr);
484 attr.RootDirectory = 0;
485 attr.ObjectName = NULL;
486 attr.Attributes = inherit ? OBJ_INHERIT : 0;
487 attr.SecurityDescriptor = NULL;
488 attr.SecurityQualityOfService = NULL;
489 if (name)
491 RtlInitUnicodeString( &nameW, name );
492 attr.ObjectName = &nameW;
495 status = NtOpenEvent( &ret, access, &attr );
496 if (status != STATUS_SUCCESS)
498 SetLastError( RtlNtStatusToDosError(status) );
499 return 0;
501 return ret;
504 /***********************************************************************
505 * PulseEvent (KERNEL32.@)
507 BOOL WINAPI PulseEvent( HANDLE handle )
509 NTSTATUS status;
511 if ((status = NtPulseEvent( handle, NULL )))
512 SetLastError( RtlNtStatusToDosError(status) );
513 return !status;
517 /***********************************************************************
518 * SetW32Event (KERNEL.458)
519 * SetEvent (KERNEL32.@)
521 BOOL WINAPI SetEvent( HANDLE handle )
523 NTSTATUS status;
525 if ((status = NtSetEvent( handle, NULL )))
526 SetLastError( RtlNtStatusToDosError(status) );
527 return !status;
531 /***********************************************************************
532 * ResetW32Event (KERNEL.459)
533 * ResetEvent (KERNEL32.@)
535 BOOL WINAPI ResetEvent( HANDLE handle )
537 NTSTATUS status;
539 if ((status = NtResetEvent( handle, NULL )))
540 SetLastError( RtlNtStatusToDosError(status) );
541 return !status;
545 /***********************************************************************
546 * NOTE: The Win95 VWin32_Event routines given below are really low-level
547 * routines implemented directly by VWin32. The user-mode libraries
548 * implement Win32 synchronisation routines on top of these low-level
549 * primitives. We do it the other way around here :-)
552 /***********************************************************************
553 * VWin32_EventCreate (KERNEL.442)
555 HANDLE WINAPI VWin32_EventCreate(VOID)
557 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
558 return ConvertToGlobalHandle( hEvent );
561 /***********************************************************************
562 * VWin32_EventDestroy (KERNEL.443)
564 VOID WINAPI VWin32_EventDestroy(HANDLE event)
566 CloseHandle( event );
569 /***********************************************************************
570 * VWin32_EventWait (KERNEL.450)
572 VOID WINAPI VWin32_EventWait(HANDLE event)
574 DWORD mutex_count;
576 ReleaseThunkLock( &mutex_count );
577 WaitForSingleObject( event, INFINITE );
578 RestoreThunkLock( mutex_count );
581 /***********************************************************************
582 * VWin32_EventSet (KERNEL.451)
583 * KERNEL_479 (KERNEL.479)
585 VOID WINAPI VWin32_EventSet(HANDLE event)
587 SetEvent( event );
592 /***********************************************************************
593 * CreateMutexA (KERNEL32.@)
595 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
597 WCHAR buffer[MAX_PATH];
599 if (!name) return CreateMutexW( sa, owner, NULL );
601 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
603 SetLastError( ERROR_FILENAME_EXCED_RANGE );
604 return 0;
606 return CreateMutexW( sa, owner, buffer );
610 /***********************************************************************
611 * CreateMutexW (KERNEL32.@)
613 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
615 HANDLE ret;
616 UNICODE_STRING nameW;
617 OBJECT_ATTRIBUTES attr;
618 NTSTATUS status;
620 attr.Length = sizeof(attr);
621 attr.RootDirectory = 0;
622 attr.ObjectName = NULL;
623 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
624 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
625 attr.SecurityQualityOfService = NULL;
626 if (name)
628 RtlInitUnicodeString( &nameW, name );
629 attr.ObjectName = &nameW;
632 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
633 SetLastError( RtlNtStatusToDosError(status) );
634 return ret;
638 /***********************************************************************
639 * OpenMutexA (KERNEL32.@)
641 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
643 WCHAR buffer[MAX_PATH];
645 if (!name) return OpenMutexW( access, inherit, NULL );
647 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
649 SetLastError( ERROR_FILENAME_EXCED_RANGE );
650 return 0;
652 return OpenMutexW( access, inherit, buffer );
656 /***********************************************************************
657 * OpenMutexW (KERNEL32.@)
659 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
661 HANDLE ret;
662 UNICODE_STRING nameW;
663 OBJECT_ATTRIBUTES attr;
664 NTSTATUS status;
666 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
668 attr.Length = sizeof(attr);
669 attr.RootDirectory = 0;
670 attr.ObjectName = NULL;
671 attr.Attributes = inherit ? OBJ_INHERIT : 0;
672 attr.SecurityDescriptor = NULL;
673 attr.SecurityQualityOfService = NULL;
674 if (name)
676 RtlInitUnicodeString( &nameW, name );
677 attr.ObjectName = &nameW;
680 status = NtOpenMutant( &ret, access, &attr );
681 if (status != STATUS_SUCCESS)
683 SetLastError( RtlNtStatusToDosError(status) );
684 return 0;
686 return ret;
690 /***********************************************************************
691 * ReleaseMutex (KERNEL32.@)
693 BOOL WINAPI ReleaseMutex( HANDLE handle )
695 NTSTATUS status;
697 status = NtReleaseMutant(handle, NULL);
698 if (status != STATUS_SUCCESS)
700 SetLastError( RtlNtStatusToDosError(status) );
701 return FALSE;
703 return TRUE;
708 * Semaphores
712 /***********************************************************************
713 * CreateSemaphoreA (KERNEL32.@)
715 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
717 WCHAR buffer[MAX_PATH];
719 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
721 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
723 SetLastError( ERROR_FILENAME_EXCED_RANGE );
724 return 0;
726 return CreateSemaphoreW( sa, initial, max, buffer );
730 /***********************************************************************
731 * CreateSemaphoreW (KERNEL32.@)
733 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
734 LONG max, LPCWSTR name )
736 HANDLE ret;
737 UNICODE_STRING nameW;
738 OBJECT_ATTRIBUTES attr;
739 NTSTATUS status;
741 attr.Length = sizeof(attr);
742 attr.RootDirectory = 0;
743 attr.ObjectName = NULL;
744 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
745 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
746 attr.SecurityQualityOfService = NULL;
747 if (name)
749 RtlInitUnicodeString( &nameW, name );
750 attr.ObjectName = &nameW;
753 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
754 SetLastError( RtlNtStatusToDosError(status) );
755 return ret;
759 /***********************************************************************
760 * OpenSemaphoreA (KERNEL32.@)
762 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
764 WCHAR buffer[MAX_PATH];
766 if (!name) return OpenSemaphoreW( access, inherit, NULL );
768 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
770 SetLastError( ERROR_FILENAME_EXCED_RANGE );
771 return 0;
773 return OpenSemaphoreW( access, inherit, buffer );
777 /***********************************************************************
778 * OpenSemaphoreW (KERNEL32.@)
780 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
782 HANDLE ret;
783 UNICODE_STRING nameW;
784 OBJECT_ATTRIBUTES attr;
785 NTSTATUS status;
787 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
789 attr.Length = sizeof(attr);
790 attr.RootDirectory = 0;
791 attr.ObjectName = NULL;
792 attr.Attributes = inherit ? OBJ_INHERIT : 0;
793 attr.SecurityDescriptor = NULL;
794 attr.SecurityQualityOfService = NULL;
795 if (name)
797 RtlInitUnicodeString( &nameW, name );
798 attr.ObjectName = &nameW;
801 status = NtOpenSemaphore( &ret, access, &attr );
802 if (status != STATUS_SUCCESS)
804 SetLastError( RtlNtStatusToDosError(status) );
805 return 0;
807 return ret;
811 /***********************************************************************
812 * ReleaseSemaphore (KERNEL32.@)
814 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
816 NTSTATUS status = NtReleaseSemaphore( handle, count, previous );
817 if (status) SetLastError( RtlNtStatusToDosError(status) );
818 return !status;
823 * Timers
827 /***********************************************************************
828 * CreateWaitableTimerA (KERNEL32.@)
830 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
832 WCHAR buffer[MAX_PATH];
834 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
836 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
838 SetLastError( ERROR_FILENAME_EXCED_RANGE );
839 return 0;
841 return CreateWaitableTimerW( sa, manual, buffer );
845 /***********************************************************************
846 * CreateWaitableTimerW (KERNEL32.@)
848 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
850 HANDLE handle;
851 NTSTATUS status;
852 UNICODE_STRING nameW;
853 OBJECT_ATTRIBUTES attr;
855 attr.Length = sizeof(attr);
856 attr.RootDirectory = 0;
857 attr.ObjectName = NULL;
858 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
859 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
860 attr.SecurityQualityOfService = NULL;
861 if (name)
863 RtlInitUnicodeString( &nameW, name );
864 attr.ObjectName = &nameW;
867 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
868 manual ? NotificationTimer : SynchronizationTimer);
869 SetLastError( RtlNtStatusToDosError(status) );
870 return handle;
874 /***********************************************************************
875 * OpenWaitableTimerA (KERNEL32.@)
877 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
879 WCHAR buffer[MAX_PATH];
881 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
883 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
885 SetLastError( ERROR_FILENAME_EXCED_RANGE );
886 return 0;
888 return OpenWaitableTimerW( access, inherit, buffer );
892 /***********************************************************************
893 * OpenWaitableTimerW (KERNEL32.@)
895 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
897 HANDLE handle;
898 UNICODE_STRING nameW;
899 OBJECT_ATTRIBUTES attr;
900 NTSTATUS status;
902 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
904 attr.Length = sizeof(attr);
905 attr.RootDirectory = 0;
906 attr.ObjectName = NULL;
907 attr.Attributes = inherit ? OBJ_INHERIT : 0;
908 attr.SecurityDescriptor = NULL;
909 attr.SecurityQualityOfService = NULL;
910 if (name)
912 RtlInitUnicodeString( &nameW, name );
913 attr.ObjectName = &nameW;
916 status = NtOpenTimer(&handle, access, &attr);
917 if (status != STATUS_SUCCESS)
919 SetLastError( RtlNtStatusToDosError(status) );
920 return 0;
922 return handle;
926 /***********************************************************************
927 * SetWaitableTimer (KERNEL32.@)
929 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
930 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
932 NTSTATUS status = NtSetTimer(handle, when, callback, arg, resume, period, NULL);
934 if (status != STATUS_SUCCESS)
936 SetLastError( RtlNtStatusToDosError(status) );
937 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
939 return TRUE;
943 /***********************************************************************
944 * CancelWaitableTimer (KERNEL32.@)
946 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
948 NTSTATUS status;
950 status = NtCancelTimer(handle, NULL);
951 if (status != STATUS_SUCCESS)
953 SetLastError( RtlNtStatusToDosError(status) );
954 return FALSE;
956 return TRUE;
960 /***********************************************************************
961 * CreateTimerQueue (KERNEL32.@)
963 HANDLE WINAPI CreateTimerQueue(void)
965 FIXME("stub\n");
966 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
967 return NULL;
971 /***********************************************************************
972 * DeleteTimerQueueEx (KERNEL32.@)
974 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
976 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
977 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
978 return 0;
981 /***********************************************************************
982 * CreateTimerQueueTimer (KERNEL32.@)
984 * Creates a timer-queue timer. This timer expires at the specified due
985 * time (in ms), then after every specified period (in ms). When the timer
986 * expires, the callback function is called.
988 * RETURNS
989 * nonzero on success or zero on faillure
991 * BUGS
992 * Unimplemented
994 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
995 WAITORTIMERCALLBACK Callback, PVOID Parameter,
996 DWORD DueTime, DWORD Period, ULONG Flags )
998 FIXME("stub\n");
999 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1000 return TRUE;
1003 /***********************************************************************
1004 * DeleteTimerQueueTimer (KERNEL32.@)
1006 * Cancels a timer-queue timer.
1008 * RETURNS
1009 * nonzero on success or zero on faillure
1011 * BUGS
1012 * Unimplemented
1014 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1015 HANDLE CompletionEvent )
1017 FIXME("stub\n");
1018 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1019 return TRUE;
1024 * Pipes
1028 /***********************************************************************
1029 * CreateNamedPipeA (KERNEL32.@)
1031 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1032 DWORD dwPipeMode, DWORD nMaxInstances,
1033 DWORD nOutBufferSize, DWORD nInBufferSize,
1034 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1036 WCHAR buffer[MAX_PATH];
1038 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1039 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1041 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1043 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1044 return INVALID_HANDLE_VALUE;
1046 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1047 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1051 /***********************************************************************
1052 * CreateNamedPipeW (KERNEL32.@)
1054 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1055 DWORD dwPipeMode, DWORD nMaxInstances,
1056 DWORD nOutBufferSize, DWORD nInBufferSize,
1057 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1059 HANDLE handle;
1060 UNICODE_STRING nt_name;
1061 OBJECT_ATTRIBUTES attr;
1062 DWORD options;
1063 BOOLEAN pipe_type, read_mode, non_block;
1064 NTSTATUS status;
1065 IO_STATUS_BLOCK iosb;
1066 LARGE_INTEGER timeout;
1068 TRACE("(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n",
1069 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1070 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1072 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1074 SetLastError( ERROR_PATH_NOT_FOUND );
1075 return INVALID_HANDLE_VALUE;
1077 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1079 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1080 RtlFreeUnicodeString( &nt_name );
1081 return INVALID_HANDLE_VALUE;
1084 attr.Length = sizeof(attr);
1085 attr.RootDirectory = 0;
1086 attr.ObjectName = &nt_name;
1087 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
1088 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1089 attr.SecurityQualityOfService = NULL;
1091 options = 0;
1092 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1093 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1094 if ((dwOpenMode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
1095 options |= FILE_PIPE_FULL_DUPLEX;
1096 else if (dwOpenMode & PIPE_ACCESS_INBOUND) options |= FILE_PIPE_INBOUND;
1097 else if (dwOpenMode & PIPE_ACCESS_OUTBOUND) options |= FILE_PIPE_OUTBOUND;
1098 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1099 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1100 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1101 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ULONG_MAX;
1103 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1105 SetLastError(0);
1107 status = NtCreateNamedPipeFile(&handle, 0, &attr, &iosb, 0, FILE_OVERWRITE_IF,
1108 options, pipe_type, read_mode, non_block,
1109 nMaxInstances, nInBufferSize, nOutBufferSize,
1110 &timeout);
1112 RtlFreeUnicodeString( &nt_name );
1113 if (status)
1115 handle = INVALID_HANDLE_VALUE;
1116 SetLastError( RtlNtStatusToDosError(status) );
1118 return handle;
1122 /***********************************************************************
1123 * PeekNamedPipe (KERNEL32.@)
1125 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1126 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1128 #ifdef FIONREAD
1129 int avail=0, fd, ret, flags;
1131 TRACE("(%p,%p,%lu,%p,%p,%p)\n", hPipe, lpvBuffer, cbBuffer, lpcbRead, lpcbAvail, lpcbMessage);
1133 ret = wine_server_handle_to_fd( hPipe, GENERIC_READ, &fd, &flags );
1134 if (ret)
1136 SetLastError( RtlNtStatusToDosError(ret) );
1137 return FALSE;
1139 if (flags & FD_FLAG_RECV_SHUTDOWN)
1141 wine_server_release_fd( hPipe, fd );
1142 SetLastError ( ERROR_PIPE_NOT_CONNECTED );
1143 return FALSE;
1146 if (ioctl(fd,FIONREAD, &avail ) != 0)
1148 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1149 wine_server_release_fd( hPipe, fd );
1150 return FALSE;
1152 if (!avail) /* check for closed pipe */
1154 struct pollfd pollfd;
1155 pollfd.fd = fd;
1156 pollfd.events = POLLIN;
1157 pollfd.revents = 0;
1158 switch (poll( &pollfd, 1, 0 ))
1160 case 0:
1161 break;
1162 case 1: /* got something */
1163 if (!(pollfd.revents & (POLLHUP | POLLERR))) break;
1164 TRACE("POLLHUP | POLLERR\n");
1165 /* fall through */
1166 case -1:
1167 wine_server_release_fd( hPipe, fd );
1168 SetLastError(ERROR_BROKEN_PIPE);
1169 return FALSE;
1172 TRACE(" 0x%08x bytes available\n", avail );
1173 ret = TRUE;
1174 if (lpcbAvail)
1175 *lpcbAvail = avail;
1176 if (lpcbRead)
1177 *lpcbRead = 0;
1178 if (avail && lpvBuffer && cbBuffer)
1180 int readbytes = (avail < cbBuffer) ? avail : cbBuffer;
1181 readbytes = recv(fd, lpvBuffer, readbytes, MSG_PEEK);
1182 if (readbytes < 0)
1184 WARN("failed to peek socket (%d)\n", errno);
1185 ret = FALSE;
1187 else if (lpcbRead)
1188 *lpcbRead = readbytes;
1190 wine_server_release_fd( hPipe, fd );
1191 return ret;
1192 #endif /* defined(FIONREAD) */
1194 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1195 FIXME("function not implemented\n");
1196 return FALSE;
1199 /***********************************************************************
1200 * PIPE_CompletionWait (Internal)
1202 static void CALLBACK PIPE_CompletionWait(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
1204 LPOVERLAPPED ovlp = (LPOVERLAPPED)user;
1206 TRACE("for %p/%p, status=%08lx\n", ovlp, iosb, status);
1208 if (ovlp)
1210 ovlp->Internal = status;
1211 SetEvent(ovlp->hEvent);
1213 TRACE("done\n");
1216 /***********************************************************************
1217 * WaitNamedPipeA (KERNEL32.@)
1219 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1221 WCHAR buffer[MAX_PATH];
1223 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1225 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1227 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1228 return 0;
1230 return WaitNamedPipeW( buffer, nTimeOut );
1234 /***********************************************************************
1235 * WaitNamedPipeW (KERNEL32.@)
1237 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1239 BOOL ret;
1240 OVERLAPPED ov;
1241 UNICODE_STRING nt_name;
1242 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1244 TRACE("%s 0x%08lx\n",debugstr_w(name),nTimeOut);
1246 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1247 return FALSE;
1249 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1251 RtlFreeUnicodeString( &nt_name );
1252 return FALSE;
1254 if (nt_name.Length < sizeof(leadin) ||
1255 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(leadin[0])))
1257 RtlFreeUnicodeString( &nt_name );
1258 return FALSE;
1261 memset(&ov,0,sizeof(ov));
1262 ov.hEvent = CreateEventW( NULL, 0, 0, NULL );
1263 if (!ov.hEvent)
1264 return FALSE;
1266 SERVER_START_REQ( wait_named_pipe )
1268 req->timeout = nTimeOut;
1269 req->overlapped = &ov;
1270 req->func = PIPE_CompletionWait;
1271 wine_server_add_data( req, nt_name.Buffer + 4, nt_name.Length - 4*sizeof(WCHAR) );
1272 ret = !wine_server_call_err( req );
1274 SERVER_END_REQ;
1276 RtlFreeUnicodeString( &nt_name );
1278 if(ret)
1280 if (WAIT_OBJECT_0==WaitForSingleObject(ov.hEvent,INFINITE))
1282 SetLastError(RtlNtStatusToDosError(ov.Internal));
1283 ret = (ov.Internal==STATUS_SUCCESS);
1286 CloseHandle(ov.hEvent);
1287 return ret;
1291 /***********************************************************************
1292 * ConnectNamedPipe (KERNEL32.@)
1294 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1296 BOOL ret;
1297 LPOVERLAPPED pov;
1298 OVERLAPPED ov;
1300 TRACE("(%p,%p)\n", hPipe, overlapped);
1302 if (!overlapped)
1304 memset(&ov, 0, sizeof(ov));
1305 ov.hEvent = CreateEventW(NULL, 0, 0, NULL);
1306 if (!ov.hEvent) return FALSE;
1307 pov = &ov;
1309 else pov = overlapped;
1311 pov->Internal = STATUS_PENDING;
1313 SERVER_START_REQ( connect_named_pipe )
1315 req->handle = hPipe;
1316 req->overlapped = pov;
1317 req->func = PIPE_CompletionWait;
1318 ret = !wine_server_call_err( req );
1320 SERVER_END_REQ;
1322 if (ret)
1324 if (overlapped)
1326 SetLastError( ERROR_IO_PENDING );
1327 ret = FALSE;
1329 else
1331 ret = GetOverlappedResult(hPipe, &ov, NULL, TRUE);
1332 CloseHandle(ov.hEvent);
1336 return ret;
1339 /***********************************************************************
1340 * DisconnectNamedPipe (KERNEL32.@)
1342 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1344 BOOL ret;
1346 TRACE("(%p)\n",hPipe);
1348 SERVER_START_REQ( disconnect_named_pipe )
1350 req->handle = hPipe;
1351 ret = !wine_server_call_err( req );
1352 if (ret && reply->fd != -1) close( reply->fd );
1354 SERVER_END_REQ;
1356 return ret;
1359 /***********************************************************************
1360 * TransactNamedPipe (KERNEL32.@)
1362 * BUGS
1363 * should be done as a single operation in the wineserver or kernel
1365 BOOL WINAPI TransactNamedPipe(
1366 HANDLE handle, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1367 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1369 BOOL r;
1370 DWORD count;
1372 TRACE("%p %p %ld %p %ld %p %p\n",
1373 handle, lpInput, dwInputSize, lpOutput,
1374 dwOutputSize, lpBytesRead, lpOverlapped);
1376 if (lpOverlapped)
1378 FIXME("Doesn't support overlapped operation as yet\n");
1379 return FALSE;
1382 r = WriteFile(handle, lpOutput, dwOutputSize, &count, NULL);
1383 if (r)
1384 r = ReadFile(handle, lpInput, dwInputSize, lpBytesRead, NULL);
1386 return r;
1389 /***********************************************************************
1390 * GetNamedPipeInfo (KERNEL32.@)
1392 BOOL WINAPI GetNamedPipeInfo(
1393 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1394 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1396 BOOL ret;
1398 TRACE("%p %p %p %p %p\n", hNamedPipe, lpFlags,
1399 lpOutputBufferSize, lpInputBufferSize, lpMaxInstances);
1401 SERVER_START_REQ( get_named_pipe_info )
1403 req->handle = hNamedPipe;
1404 ret = !wine_server_call_err( req );
1405 if (lpFlags)
1407 *lpFlags = 0;
1408 if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)
1409 *lpFlags |= PIPE_TYPE_MESSAGE;
1410 if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
1411 *lpFlags |= PIPE_READMODE_MESSAGE;
1412 if (reply->flags & NAMED_PIPE_NONBLOCKING_MODE)
1413 *lpFlags |= PIPE_NOWAIT;
1415 if (lpOutputBufferSize) *lpOutputBufferSize = reply->outsize;
1416 if (lpInputBufferSize) *lpInputBufferSize = reply->outsize;
1417 if (lpMaxInstances) *lpMaxInstances = reply->maxinstances;
1419 SERVER_END_REQ;
1421 return ret;
1424 /***********************************************************************
1425 * GetNamedPipeHandleStateA (KERNEL32.@)
1427 BOOL WINAPI GetNamedPipeHandleStateA(
1428 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1429 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1430 LPSTR lpUsername, DWORD nUsernameMaxSize)
1432 FIXME("%p %p %p %p %p %p %ld\n",
1433 hNamedPipe, lpState, lpCurInstances,
1434 lpMaxCollectionCount, lpCollectDataTimeout,
1435 lpUsername, nUsernameMaxSize);
1437 return FALSE;
1440 /***********************************************************************
1441 * GetNamedPipeHandleStateW (KERNEL32.@)
1443 BOOL WINAPI GetNamedPipeHandleStateW(
1444 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1445 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1446 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1448 FIXME("%p %p %p %p %p %p %ld\n",
1449 hNamedPipe, lpState, lpCurInstances,
1450 lpMaxCollectionCount, lpCollectDataTimeout,
1451 lpUsername, nUsernameMaxSize);
1453 return FALSE;
1456 /***********************************************************************
1457 * SetNamedPipeHandleState (KERNEL32.@)
1459 BOOL WINAPI SetNamedPipeHandleState(
1460 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1461 LPDWORD lpCollectDataTimeout)
1463 FIXME("%p %p %p %p\n",
1464 hNamedPipe, lpMode, lpMaxCollectionCount, lpCollectDataTimeout);
1465 return FALSE;
1468 /***********************************************************************
1469 * CallNamedPipeA (KERNEL32.@)
1471 BOOL WINAPI CallNamedPipeA(
1472 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1473 LPVOID lpOutput, DWORD dwOutputSize,
1474 LPDWORD lpBytesRead, DWORD nTimeout)
1476 DWORD len;
1477 LPWSTR str = NULL;
1478 BOOL ret;
1480 TRACE("%s %p %ld %p %ld %p %ld\n",
1481 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1482 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1484 if( lpNamedPipeName )
1486 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1487 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1488 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1490 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1491 dwOutputSize, lpBytesRead, nTimeout );
1492 if( lpNamedPipeName )
1493 HeapFree( GetProcessHeap(), 0, str );
1495 return ret;
1498 /***********************************************************************
1499 * CallNamedPipeW (KERNEL32.@)
1501 BOOL WINAPI CallNamedPipeW(
1502 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1503 LPVOID lpOutput, DWORD lpOutputSize,
1504 LPDWORD lpBytesRead, DWORD nTimeout)
1506 FIXME("%s %p %ld %p %ld %p %ld\n",
1507 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1508 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1509 return FALSE;
1512 /******************************************************************
1513 * CreatePipe (KERNEL32.@)
1516 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1517 LPSECURITY_ATTRIBUTES sa, DWORD size )
1519 static unsigned index = 0;
1520 WCHAR name[64];
1521 HANDLE hr, hw;
1522 unsigned in_index = index;
1524 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1525 /* generate a unique pipe name (system wide) */
1528 static const WCHAR nameFmt[] = { '\\','\\','.','\\','p','i','p','e',
1529 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1530 'u','.','%','0','8','u','\0' };
1531 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1532 GetCurrentProcessId(), ++index);
1533 hr = CreateNamedPipeW(name, PIPE_ACCESS_INBOUND,
1534 PIPE_TYPE_BYTE | PIPE_WAIT, 1, size, size,
1535 NMPWAIT_USE_DEFAULT_WAIT, sa);
1536 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1537 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1538 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1540 hw = CreateFileW(name, GENERIC_WRITE, 0, sa, OPEN_EXISTING, 0, 0);
1541 if (hw == INVALID_HANDLE_VALUE)
1543 CloseHandle(hr);
1544 return FALSE;
1547 *hReadPipe = hr;
1548 *hWritePipe = hw;
1549 return TRUE;
1553 /******************************************************************************
1554 * CreateMailslotA [KERNEL32.@]
1556 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1557 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1559 DWORD len;
1560 HANDLE handle;
1561 LPWSTR name = NULL;
1563 TRACE("%s %ld %ld %p\n", debugstr_a(lpName),
1564 nMaxMessageSize, lReadTimeout, sa);
1566 if( lpName )
1568 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1569 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1570 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1573 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1575 HeapFree( GetProcessHeap(), 0, name );
1577 return handle;
1581 /******************************************************************************
1582 * CreateMailslotW [KERNEL32.@]
1584 * Create a mailslot with specified name.
1586 * PARAMS
1587 * lpName [I] Pointer to string for mailslot name
1588 * nMaxMessageSize [I] Maximum message size
1589 * lReadTimeout [I] Milliseconds before read time-out
1590 * sa [I] Pointer to security structure
1592 * RETURNS
1593 * Success: Handle to mailslot
1594 * Failure: INVALID_HANDLE_VALUE
1596 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1597 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1599 HANDLE handle = INVALID_HANDLE_VALUE;
1600 OBJECT_ATTRIBUTES attr;
1601 UNICODE_STRING nameW;
1602 LARGE_INTEGER timeout;
1603 IO_STATUS_BLOCK iosb;
1604 NTSTATUS status;
1606 TRACE("%s %ld %ld %p\n", debugstr_w(lpName),
1607 nMaxMessageSize, lReadTimeout, sa);
1609 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1611 SetLastError( ERROR_PATH_NOT_FOUND );
1612 return INVALID_HANDLE_VALUE;
1615 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1617 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1618 RtlFreeUnicodeString( &nameW );
1619 return INVALID_HANDLE_VALUE;
1622 attr.Length = sizeof(attr);
1623 attr.RootDirectory = 0;
1624 attr.Attributes = OBJ_CASE_INSENSITIVE;
1625 attr.ObjectName = &nameW;
1626 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1627 attr.SecurityQualityOfService = NULL;
1629 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1631 status = NtCreateMailslotFile( &handle, GENERIC_READ | GENERIC_WRITE, &attr,
1632 &iosb, 0, 0, nMaxMessageSize, &timeout );
1633 if (status)
1635 SetLastError( RtlNtStatusToDosError(status) );
1636 handle = INVALID_HANDLE_VALUE;
1639 RtlFreeUnicodeString( &nameW );
1640 return handle;
1644 /******************************************************************************
1645 * GetMailslotInfo [KERNEL32.@]
1647 * Retrieve information about a mailslot.
1649 * PARAMS
1650 * hMailslot [I] Mailslot handle
1651 * lpMaxMessageSize [O] Address of maximum message size
1652 * lpNextSize [O] Address of size of next message
1653 * lpMessageCount [O] Address of number of messages
1654 * lpReadTimeout [O] Address of read time-out
1656 * RETURNS
1657 * Success: TRUE
1658 * Failure: FALSE
1660 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1661 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1662 LPDWORD lpReadTimeout )
1664 FILE_MAILSLOT_QUERY_INFORMATION info;
1665 IO_STATUS_BLOCK iosb;
1666 NTSTATUS status;
1668 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1669 lpNextSize, lpMessageCount, lpReadTimeout);
1671 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1672 FileMailslotQueryInformation );
1674 if( status != STATUS_SUCCESS )
1676 SetLastError( RtlNtStatusToDosError(status) );
1677 return FALSE;
1680 if( lpMaxMessageSize )
1681 *lpMaxMessageSize = info.MaximumMessageSize;
1682 if( lpNextSize )
1683 *lpNextSize = info.NextMessageSize;
1684 if( lpMessageCount )
1685 *lpMessageCount = info.MessagesAvailable;
1686 if( lpReadTimeout )
1687 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1689 return TRUE;
1693 /******************************************************************************
1694 * SetMailslotInfo [KERNEL32.@]
1696 * Set the read timeout of a mailslot.
1698 * PARAMS
1699 * hMailslot [I] Mailslot handle
1700 * dwReadTimeout [I] Timeout in milliseconds.
1702 * RETURNS
1703 * Success: TRUE
1704 * Failure: FALSE
1706 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1708 FILE_MAILSLOT_SET_INFORMATION info;
1709 IO_STATUS_BLOCK iosb;
1710 NTSTATUS status;
1712 TRACE("%p %ld\n", hMailslot, dwReadTimeout);
1714 info.ReadTimeout.QuadPart = dwReadTimeout * -10000;
1715 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1716 FileMailslotSetInformation );
1717 if( status != STATUS_SUCCESS )
1719 SetLastError( RtlNtStatusToDosError(status) );
1720 return FALSE;
1722 return TRUE;
1726 /******************************************************************************
1727 * CreateIoCompletionPort (KERNEL32.@)
1729 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1730 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1732 FIXME("(%p, %p, %08lx, %08lx): stub.\n",
1733 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1734 return NULL;
1738 /******************************************************************************
1739 * GetQueuedCompletionStatus (KERNEL32.@)
1741 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1742 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1743 DWORD dwMilliseconds )
1745 FIXME("(%p,%p,%p,%p,%ld), stub!\n",
1746 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1747 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1748 return FALSE;
1751 /******************************************************************************
1752 * CreateJobObjectW (KERNEL32.@)
1754 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1756 FIXME("%p %s\n", attr, debugstr_w(name) );
1757 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1758 return 0;
1761 /******************************************************************************
1762 * CreateJobObjectA (KERNEL32.@)
1764 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1766 LPWSTR str = NULL;
1767 UINT len;
1768 HANDLE r;
1770 TRACE("%p %s\n", attr, debugstr_a(name) );
1772 if( name )
1774 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1775 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1776 if( !str )
1778 SetLastError( ERROR_OUTOFMEMORY );
1779 return 0;
1781 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1784 r = CreateJobObjectW( attr, str );
1786 HeapFree( GetProcessHeap(), 0, str );
1788 return r;
1791 /******************************************************************************
1792 * AssignProcessToJobObject (KERNEL32.@)
1794 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1796 FIXME("%p %p\n", hJob, hProcess);
1797 return TRUE;
1800 #ifdef __i386__
1802 /***********************************************************************
1803 * InterlockedCompareExchange (KERNEL32.@)
1805 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1806 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1807 "movl 12(%esp),%eax\n\t"
1808 "movl 8(%esp),%ecx\n\t"
1809 "movl 4(%esp),%edx\n\t"
1810 "lock; cmpxchgl %ecx,(%edx)\n\t"
1811 "ret $12")
1813 /***********************************************************************
1814 * InterlockedExchange (KERNEL32.@)
1816 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1817 __ASM_GLOBAL_FUNC(InterlockedExchange,
1818 "movl 8(%esp),%eax\n\t"
1819 "movl 4(%esp),%edx\n\t"
1820 "lock; xchgl %eax,(%edx)\n\t"
1821 "ret $8")
1823 /***********************************************************************
1824 * InterlockedExchangeAdd (KERNEL32.@)
1826 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1827 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1828 "movl 8(%esp),%eax\n\t"
1829 "movl 4(%esp),%edx\n\t"
1830 "lock; xaddl %eax,(%edx)\n\t"
1831 "ret $8")
1833 /***********************************************************************
1834 * InterlockedIncrement (KERNEL32.@)
1836 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1837 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1838 "movl 4(%esp),%edx\n\t"
1839 "movl $1,%eax\n\t"
1840 "lock; xaddl %eax,(%edx)\n\t"
1841 "incl %eax\n\t"
1842 "ret $4")
1844 /***********************************************************************
1845 * InterlockedDecrement (KERNEL32.@)
1847 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1848 "movl 4(%esp),%edx\n\t"
1849 "movl $-1,%eax\n\t"
1850 "lock; xaddl %eax,(%edx)\n\t"
1851 "decl %eax\n\t"
1852 "ret $4")
1854 #else /* __i386__ */
1856 /***********************************************************************
1857 * InterlockedCompareExchange (KERNEL32.@)
1859 * Atomically swap one value with another.
1861 * PARAMS
1862 * dest [I/O] The value to replace
1863 * xchq [I] The value to be swapped
1864 * compare [I] The value to compare to dest
1866 * RETURNS
1867 * The resulting value of dest.
1869 * NOTES
1870 * dest is updated only if it is equal to compare, otherwise no swap is done.
1872 LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare )
1874 return interlocked_cmpxchg( dest, xchg, compare );
1877 /***********************************************************************
1878 * InterlockedExchange (KERNEL32.@)
1880 * Atomically swap one value with another.
1882 * PARAMS
1883 * dest [I/O] The value to replace
1884 * val [I] The value to be swapped
1886 * RETURNS
1887 * The resulting value of dest.
1889 LONG WINAPI InterlockedExchange( PLONG dest, LONG val )
1891 return interlocked_xchg( dest, val );
1894 /***********************************************************************
1895 * InterlockedExchangeAdd (KERNEL32.@)
1897 * Atomically add one value to another.
1899 * PARAMS
1900 * dest [I/O] The value to add to
1901 * incr [I] The value to be added
1903 * RETURNS
1904 * The resulting value of dest.
1906 LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr )
1908 return interlocked_xchg_add( dest, incr );
1911 /***********************************************************************
1912 * InterlockedIncrement (KERNEL32.@)
1914 * Atomically increment a value.
1916 * PARAMS
1917 * dest [I/O] The value to increment
1919 * RETURNS
1920 * The resulting value of dest.
1922 LONG WINAPI InterlockedIncrement( PLONG dest )
1924 return interlocked_xchg_add( dest, 1 ) + 1;
1927 /***********************************************************************
1928 * InterlockedDecrement (KERNEL32.@)
1930 * Atomically decrement a value.
1932 * PARAMS
1933 * dest [I/O] The value to decrement
1935 * RETURNS
1936 * The resulting value of dest.
1938 LONG WINAPI InterlockedDecrement( PLONG dest )
1940 return interlocked_xchg_add( dest, -1 ) - 1;
1943 #endif /* __i386__ */