kernel: Fix CreateToolhelp32Snapshot tests on win2k.
[wine.git] / dlls / kernel / sync.c
blob8c43e5347c355f5770894673396e7cca1af76be9
1 /*
2 * Kernel synchronization objects
4 * Copyright 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <errno.h>
29 #ifdef HAVE_SYS_IOCTL_H
30 #include <sys/ioctl.h>
31 #endif
32 #ifdef HAVE_POLL_H
33 #include <poll.h>
34 #endif
35 #ifdef HAVE_SYS_POLL_H
36 #include <sys/poll.h>
37 #endif
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
40 #endif
41 #include <stdarg.h>
42 #include <stdio.h>
44 #define NONAMELESSUNION
45 #define NONAMELESSSTRUCT
47 #include "ntstatus.h"
48 #define WIN32_NO_STATUS
49 #include "windef.h"
50 #include "winbase.h"
51 #include "winerror.h"
52 #include "winnls.h"
53 #include "winternl.h"
54 #include "winioctl.h"
55 #include "ddk/wdm.h"
57 #include "wine/server.h"
58 #include "wine/unicode.h"
59 #include "wine/winbase16.h"
60 #include "kernel_private.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(sync);
66 /* check if current version is NT or Win95 */
67 inline static int is_version_nt(void)
69 return !(GetVersion() & 0x80000000);
72 /* returns directory handle to \\BaseNamedObjects */
73 HANDLE get_BaseNamedObjects_handle(void)
75 static HANDLE handle = NULL;
76 static const WCHAR basenameW[] =
77 {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
78 UNICODE_STRING str;
79 OBJECT_ATTRIBUTES attr;
81 if (!handle)
83 HANDLE dir;
85 RtlInitUnicodeString(&str, basenameW);
86 InitializeObjectAttributes(&attr, &str, 0, 0, NULL);
87 NtOpenDirectoryObject(&dir, DIRECTORY_CREATE_OBJECT|DIRECTORY_TRAVERSE,
88 &attr);
89 if (InterlockedCompareExchangePointer( (PVOID)&handle, dir, 0 ) != 0)
91 /* someone beat us here... */
92 CloseHandle( dir );
95 return handle;
98 /***********************************************************************
99 * Sleep (KERNEL32.@)
101 VOID WINAPI Sleep( DWORD timeout )
103 SleepEx( timeout, FALSE );
106 /******************************************************************************
107 * SleepEx (KERNEL32.@)
109 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
111 NTSTATUS status;
113 if (timeout == INFINITE) status = NtDelayExecution( alertable, NULL );
114 else
116 LARGE_INTEGER time;
118 time.QuadPart = timeout * (ULONGLONG)10000;
119 time.QuadPart = -time.QuadPart;
120 status = NtDelayExecution( alertable, &time );
122 if (status != STATUS_USER_APC) status = STATUS_SUCCESS;
123 return status;
127 /***********************************************************************
128 * SwitchToThread (KERNEL32.@)
130 BOOL WINAPI SwitchToThread(void)
132 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
136 /***********************************************************************
137 * WaitForSingleObject (KERNEL32.@)
139 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
141 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
145 /***********************************************************************
146 * WaitForSingleObjectEx (KERNEL32.@)
148 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
149 BOOL alertable )
151 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
155 /***********************************************************************
156 * WaitForMultipleObjects (KERNEL32.@)
158 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
159 BOOL wait_all, DWORD timeout )
161 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
165 /***********************************************************************
166 * WaitForMultipleObjectsEx (KERNEL32.@)
168 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
169 BOOL wait_all, DWORD timeout,
170 BOOL alertable )
172 NTSTATUS status;
173 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
174 unsigned int i;
176 if (count > MAXIMUM_WAIT_OBJECTS)
178 SetLastError(ERROR_INVALID_PARAMETER);
179 return WAIT_FAILED;
181 for (i = 0; i < count; i++)
183 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
184 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
185 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
186 hloc[i] = GetStdHandle( (DWORD)handles[i] );
187 else
188 hloc[i] = handles[i];
190 /* yes, even screen buffer console handles are waitable, and are
191 * handled as a handle to the console itself !!
193 if (is_console_handle(hloc[i]))
195 if (!VerifyConsoleIoHandle(hloc[i]))
197 return FALSE;
199 hloc[i] = GetConsoleInputWaitHandle();
203 if (timeout == INFINITE)
205 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, NULL );
207 else
209 LARGE_INTEGER time;
211 time.QuadPart = timeout * (ULONGLONG)10000;
212 time.QuadPart = -time.QuadPart;
213 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, &time );
216 if (HIWORD(status)) /* is it an error code? */
218 SetLastError( RtlNtStatusToDosError(status) );
219 status = WAIT_FAILED;
221 return status;
225 /***********************************************************************
226 * WaitForSingleObject (KERNEL.460)
228 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
230 DWORD retval, mutex_count;
232 ReleaseThunkLock( &mutex_count );
233 retval = WaitForSingleObject( handle, timeout );
234 RestoreThunkLock( mutex_count );
235 return retval;
238 /***********************************************************************
239 * WaitForMultipleObjects (KERNEL.461)
241 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
242 BOOL wait_all, DWORD timeout )
244 DWORD retval, mutex_count;
246 ReleaseThunkLock( &mutex_count );
247 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
248 RestoreThunkLock( mutex_count );
249 return retval;
252 /***********************************************************************
253 * WaitForMultipleObjectsEx (KERNEL.495)
255 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
256 BOOL wait_all, DWORD timeout, BOOL alertable )
258 DWORD retval, mutex_count;
260 ReleaseThunkLock( &mutex_count );
261 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
262 RestoreThunkLock( mutex_count );
263 return retval;
266 /***********************************************************************
267 * RegisterWaitForSingleObject (KERNEL32.@)
269 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
270 WAITORTIMERCALLBACK Callback, PVOID Context,
271 ULONG dwMilliseconds, ULONG dwFlags)
273 FIXME("%p %p %p %p %ld %ld\n",
274 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
275 return FALSE;
278 /***********************************************************************
279 * RegisterWaitForSingleObjectEx (KERNEL32.@)
281 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
282 WAITORTIMERCALLBACK Callback, PVOID Context,
283 ULONG dwMilliseconds, ULONG dwFlags )
285 FIXME("%p %p %p %ld %ld\n",
286 hObject,Callback,Context,dwMilliseconds,dwFlags);
287 return 0;
290 /***********************************************************************
291 * UnregisterWait (KERNEL32.@)
293 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
295 FIXME("%p\n",WaitHandle);
296 return FALSE;
299 /***********************************************************************
300 * UnregisterWaitEx (KERNEL32.@)
302 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
304 FIXME("%p %p\n",WaitHandle, CompletionEvent);
305 return FALSE;
308 /***********************************************************************
309 * SignalObjectAndWait (KERNEL32.@)
311 * Allows to atomically signal any of the synchro objects (semaphore,
312 * mutex, event) and wait on another.
314 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
315 DWORD dwMilliseconds, BOOL bAlertable )
317 NTSTATUS status;
318 LARGE_INTEGER timeout, *ptimeout = NULL;
320 TRACE("%p %p %ld %d\n", hObjectToSignal,
321 hObjectToWaitOn, dwMilliseconds, bAlertable);
323 if (dwMilliseconds != INFINITE)
325 timeout.QuadPart = dwMilliseconds * (ULONGLONG)10000;
326 timeout.QuadPart = -timeout.QuadPart;
327 ptimeout = &timeout;
330 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn,
331 bAlertable, ptimeout );
332 if (HIWORD(status))
334 SetLastError( RtlNtStatusToDosError(status) );
335 status = WAIT_FAILED;
337 return status;
340 /***********************************************************************
341 * InitializeCriticalSection (KERNEL32.@)
343 * Initialise a critical section before use.
345 * PARAMS
346 * crit [O] Critical section to initialise.
348 * RETURNS
349 * Nothing. If the function fails an exception is raised.
351 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
353 NTSTATUS ret = RtlInitializeCriticalSection( crit );
354 if (ret) RtlRaiseStatus( ret );
357 /***********************************************************************
358 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
360 * Initialise a critical section with a spin count.
362 * PARAMS
363 * crit [O] Critical section to initialise.
364 * spincount [I] Number of times to spin upon contention.
366 * RETURNS
367 * Success: TRUE.
368 * Failure: Nothing. If the function fails an exception is raised.
370 * NOTES
371 * spincount is ignored on uni-processor systems.
373 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
375 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
376 if (ret) RtlRaiseStatus( ret );
377 return !ret;
380 /***********************************************************************
381 * MakeCriticalSectionGlobal (KERNEL32.@)
383 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
385 /* let's assume that only one thread at a time will try to do this */
386 HANDLE sem = crit->LockSemaphore;
387 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
388 crit->LockSemaphore = ConvertToGlobalHandle( sem );
389 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
390 crit->DebugInfo = NULL;
394 /***********************************************************************
395 * ReinitializeCriticalSection (KERNEL32.@)
397 * Initialise an already used critical section.
399 * PARAMS
400 * crit [O] Critical section to initialise.
402 * RETURNS
403 * Nothing.
405 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
407 if ( !crit->LockSemaphore )
408 RtlInitializeCriticalSection( crit );
412 /***********************************************************************
413 * UninitializeCriticalSection (KERNEL32.@)
415 * UnInitialise a critical section after use.
417 * PARAMS
418 * crit [O] Critical section to uninitialise (destroy).
420 * RETURNS
421 * Nothing.
423 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
425 RtlDeleteCriticalSection( crit );
429 /***********************************************************************
430 * CreateEventA (KERNEL32.@)
432 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
433 BOOL initial_state, LPCSTR name )
435 WCHAR buffer[MAX_PATH];
437 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
439 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
441 SetLastError( ERROR_FILENAME_EXCED_RANGE );
442 return 0;
444 return CreateEventW( sa, manual_reset, initial_state, buffer );
448 /***********************************************************************
449 * CreateEventW (KERNEL32.@)
451 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
452 BOOL initial_state, LPCWSTR name )
454 HANDLE ret;
455 UNICODE_STRING nameW;
456 OBJECT_ATTRIBUTES attr;
457 NTSTATUS status;
459 /* one buggy program needs this
460 * ("Van Dale Groot woordenboek der Nederlandse taal")
462 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
464 ERR("Bad security attributes pointer %p\n",sa);
465 SetLastError( ERROR_INVALID_PARAMETER);
466 return 0;
469 attr.Length = sizeof(attr);
470 attr.RootDirectory = 0;
471 attr.ObjectName = NULL;
472 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
473 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
474 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
475 attr.SecurityQualityOfService = NULL;
476 if (name)
478 RtlInitUnicodeString( &nameW, name );
479 attr.ObjectName = &nameW;
480 attr.RootDirectory = get_BaseNamedObjects_handle();
483 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
484 if (status == STATUS_OBJECT_NAME_EXISTS)
485 SetLastError( ERROR_ALREADY_EXISTS );
486 else
487 SetLastError( RtlNtStatusToDosError(status) );
488 return ret;
492 /***********************************************************************
493 * CreateW32Event (KERNEL.457)
495 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
497 return CreateEventW( NULL, manual_reset, initial_state, NULL );
501 /***********************************************************************
502 * OpenEventA (KERNEL32.@)
504 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
506 WCHAR buffer[MAX_PATH];
508 if (!name) return OpenEventW( access, inherit, NULL );
510 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
512 SetLastError( ERROR_FILENAME_EXCED_RANGE );
513 return 0;
515 return OpenEventW( access, inherit, buffer );
519 /***********************************************************************
520 * OpenEventW (KERNEL32.@)
522 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
524 HANDLE ret;
525 UNICODE_STRING nameW;
526 OBJECT_ATTRIBUTES attr;
527 NTSTATUS status;
529 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
531 attr.Length = sizeof(attr);
532 attr.RootDirectory = 0;
533 attr.ObjectName = NULL;
534 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
535 attr.SecurityDescriptor = NULL;
536 attr.SecurityQualityOfService = NULL;
537 if (name)
539 RtlInitUnicodeString( &nameW, name );
540 attr.ObjectName = &nameW;
541 attr.RootDirectory = get_BaseNamedObjects_handle();
544 status = NtOpenEvent( &ret, access, &attr );
545 if (status != STATUS_SUCCESS)
547 SetLastError( RtlNtStatusToDosError(status) );
548 return 0;
550 return ret;
553 /***********************************************************************
554 * PulseEvent (KERNEL32.@)
556 BOOL WINAPI PulseEvent( HANDLE handle )
558 NTSTATUS status;
560 if ((status = NtPulseEvent( handle, NULL )))
561 SetLastError( RtlNtStatusToDosError(status) );
562 return !status;
566 /***********************************************************************
567 * SetW32Event (KERNEL.458)
568 * SetEvent (KERNEL32.@)
570 BOOL WINAPI SetEvent( HANDLE handle )
572 NTSTATUS status;
574 if ((status = NtSetEvent( handle, NULL )))
575 SetLastError( RtlNtStatusToDosError(status) );
576 return !status;
580 /***********************************************************************
581 * ResetW32Event (KERNEL.459)
582 * ResetEvent (KERNEL32.@)
584 BOOL WINAPI ResetEvent( HANDLE handle )
586 NTSTATUS status;
588 if ((status = NtResetEvent( handle, NULL )))
589 SetLastError( RtlNtStatusToDosError(status) );
590 return !status;
594 /***********************************************************************
595 * NOTE: The Win95 VWin32_Event routines given below are really low-level
596 * routines implemented directly by VWin32. The user-mode libraries
597 * implement Win32 synchronisation routines on top of these low-level
598 * primitives. We do it the other way around here :-)
601 /***********************************************************************
602 * VWin32_EventCreate (KERNEL.442)
604 HANDLE WINAPI VWin32_EventCreate(VOID)
606 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
607 return ConvertToGlobalHandle( hEvent );
610 /***********************************************************************
611 * VWin32_EventDestroy (KERNEL.443)
613 VOID WINAPI VWin32_EventDestroy(HANDLE event)
615 CloseHandle( event );
618 /***********************************************************************
619 * VWin32_EventWait (KERNEL.450)
621 VOID WINAPI VWin32_EventWait(HANDLE event)
623 DWORD mutex_count;
625 ReleaseThunkLock( &mutex_count );
626 WaitForSingleObject( event, INFINITE );
627 RestoreThunkLock( mutex_count );
630 /***********************************************************************
631 * VWin32_EventSet (KERNEL.451)
632 * KERNEL_479 (KERNEL.479)
634 VOID WINAPI VWin32_EventSet(HANDLE event)
636 SetEvent( event );
641 /***********************************************************************
642 * CreateMutexA (KERNEL32.@)
644 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
646 WCHAR buffer[MAX_PATH];
648 if (!name) return CreateMutexW( sa, owner, NULL );
650 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
652 SetLastError( ERROR_FILENAME_EXCED_RANGE );
653 return 0;
655 return CreateMutexW( sa, owner, buffer );
659 /***********************************************************************
660 * CreateMutexW (KERNEL32.@)
662 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
664 HANDLE ret;
665 UNICODE_STRING nameW;
666 OBJECT_ATTRIBUTES attr;
667 NTSTATUS status;
669 attr.Length = sizeof(attr);
670 attr.RootDirectory = 0;
671 attr.ObjectName = NULL;
672 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
673 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
674 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
675 attr.SecurityQualityOfService = NULL;
676 if (name)
678 RtlInitUnicodeString( &nameW, name );
679 attr.ObjectName = &nameW;
680 attr.RootDirectory = get_BaseNamedObjects_handle();
683 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
684 if (status == STATUS_OBJECT_NAME_EXISTS)
685 SetLastError( ERROR_ALREADY_EXISTS );
686 else
687 SetLastError( RtlNtStatusToDosError(status) );
688 return ret;
692 /***********************************************************************
693 * OpenMutexA (KERNEL32.@)
695 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
697 WCHAR buffer[MAX_PATH];
699 if (!name) return OpenMutexW( access, inherit, NULL );
701 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
703 SetLastError( ERROR_FILENAME_EXCED_RANGE );
704 return 0;
706 return OpenMutexW( access, inherit, buffer );
710 /***********************************************************************
711 * OpenMutexW (KERNEL32.@)
713 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
715 HANDLE ret;
716 UNICODE_STRING nameW;
717 OBJECT_ATTRIBUTES attr;
718 NTSTATUS status;
720 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
722 attr.Length = sizeof(attr);
723 attr.RootDirectory = 0;
724 attr.ObjectName = NULL;
725 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
726 attr.SecurityDescriptor = NULL;
727 attr.SecurityQualityOfService = NULL;
728 if (name)
730 RtlInitUnicodeString( &nameW, name );
731 attr.ObjectName = &nameW;
732 attr.RootDirectory = get_BaseNamedObjects_handle();
735 status = NtOpenMutant( &ret, access, &attr );
736 if (status != STATUS_SUCCESS)
738 SetLastError( RtlNtStatusToDosError(status) );
739 return 0;
741 return ret;
745 /***********************************************************************
746 * ReleaseMutex (KERNEL32.@)
748 BOOL WINAPI ReleaseMutex( HANDLE handle )
750 NTSTATUS status;
752 status = NtReleaseMutant(handle, NULL);
753 if (status != STATUS_SUCCESS)
755 SetLastError( RtlNtStatusToDosError(status) );
756 return FALSE;
758 return TRUE;
763 * Semaphores
767 /***********************************************************************
768 * CreateSemaphoreA (KERNEL32.@)
770 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
772 WCHAR buffer[MAX_PATH];
774 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
776 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
778 SetLastError( ERROR_FILENAME_EXCED_RANGE );
779 return 0;
781 return CreateSemaphoreW( sa, initial, max, buffer );
785 /***********************************************************************
786 * CreateSemaphoreW (KERNEL32.@)
788 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
789 LONG max, LPCWSTR name )
791 HANDLE ret;
792 UNICODE_STRING nameW;
793 OBJECT_ATTRIBUTES attr;
794 NTSTATUS status;
796 attr.Length = sizeof(attr);
797 attr.RootDirectory = 0;
798 attr.ObjectName = NULL;
799 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
800 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
801 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
802 attr.SecurityQualityOfService = NULL;
803 if (name)
805 RtlInitUnicodeString( &nameW, name );
806 attr.ObjectName = &nameW;
807 attr.RootDirectory = get_BaseNamedObjects_handle();
810 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
811 if (status == STATUS_OBJECT_NAME_EXISTS)
812 SetLastError( ERROR_ALREADY_EXISTS );
813 else
814 SetLastError( RtlNtStatusToDosError(status) );
815 return ret;
819 /***********************************************************************
820 * OpenSemaphoreA (KERNEL32.@)
822 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
824 WCHAR buffer[MAX_PATH];
826 if (!name) return OpenSemaphoreW( access, inherit, NULL );
828 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
830 SetLastError( ERROR_FILENAME_EXCED_RANGE );
831 return 0;
833 return OpenSemaphoreW( access, inherit, buffer );
837 /***********************************************************************
838 * OpenSemaphoreW (KERNEL32.@)
840 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
842 HANDLE ret;
843 UNICODE_STRING nameW;
844 OBJECT_ATTRIBUTES attr;
845 NTSTATUS status;
847 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
849 attr.Length = sizeof(attr);
850 attr.RootDirectory = 0;
851 attr.ObjectName = NULL;
852 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
853 attr.SecurityDescriptor = NULL;
854 attr.SecurityQualityOfService = NULL;
855 if (name)
857 RtlInitUnicodeString( &nameW, name );
858 attr.ObjectName = &nameW;
859 attr.RootDirectory = get_BaseNamedObjects_handle();
862 status = NtOpenSemaphore( &ret, access, &attr );
863 if (status != STATUS_SUCCESS)
865 SetLastError( RtlNtStatusToDosError(status) );
866 return 0;
868 return ret;
872 /***********************************************************************
873 * ReleaseSemaphore (KERNEL32.@)
875 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
877 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
878 if (status) SetLastError( RtlNtStatusToDosError(status) );
879 return !status;
884 * Timers
888 /***********************************************************************
889 * CreateWaitableTimerA (KERNEL32.@)
891 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
893 WCHAR buffer[MAX_PATH];
895 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
897 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
899 SetLastError( ERROR_FILENAME_EXCED_RANGE );
900 return 0;
902 return CreateWaitableTimerW( sa, manual, buffer );
906 /***********************************************************************
907 * CreateWaitableTimerW (KERNEL32.@)
909 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
911 HANDLE handle;
912 NTSTATUS status;
913 UNICODE_STRING nameW;
914 OBJECT_ATTRIBUTES attr;
916 attr.Length = sizeof(attr);
917 attr.RootDirectory = 0;
918 attr.ObjectName = NULL;
919 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
920 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
921 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
922 attr.SecurityQualityOfService = NULL;
923 if (name)
925 RtlInitUnicodeString( &nameW, name );
926 attr.ObjectName = &nameW;
927 attr.RootDirectory = get_BaseNamedObjects_handle();
930 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
931 manual ? NotificationTimer : SynchronizationTimer);
932 if (status == STATUS_OBJECT_NAME_EXISTS)
933 SetLastError( ERROR_ALREADY_EXISTS );
934 else
935 SetLastError( RtlNtStatusToDosError(status) );
936 return handle;
940 /***********************************************************************
941 * OpenWaitableTimerA (KERNEL32.@)
943 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
945 WCHAR buffer[MAX_PATH];
947 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
949 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
951 SetLastError( ERROR_FILENAME_EXCED_RANGE );
952 return 0;
954 return OpenWaitableTimerW( access, inherit, buffer );
958 /***********************************************************************
959 * OpenWaitableTimerW (KERNEL32.@)
961 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
963 HANDLE handle;
964 UNICODE_STRING nameW;
965 OBJECT_ATTRIBUTES attr;
966 NTSTATUS status;
968 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
970 attr.Length = sizeof(attr);
971 attr.RootDirectory = 0;
972 attr.ObjectName = NULL;
973 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
974 attr.SecurityDescriptor = NULL;
975 attr.SecurityQualityOfService = NULL;
976 if (name)
978 RtlInitUnicodeString( &nameW, name );
979 attr.ObjectName = &nameW;
980 attr.RootDirectory = get_BaseNamedObjects_handle();
983 status = NtOpenTimer(&handle, access, &attr);
984 if (status != STATUS_SUCCESS)
986 SetLastError( RtlNtStatusToDosError(status) );
987 return 0;
989 return handle;
993 /***********************************************************************
994 * SetWaitableTimer (KERNEL32.@)
996 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
997 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
999 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
1000 arg, resume, period, NULL);
1002 if (status != STATUS_SUCCESS)
1004 SetLastError( RtlNtStatusToDosError(status) );
1005 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
1007 return TRUE;
1011 /***********************************************************************
1012 * CancelWaitableTimer (KERNEL32.@)
1014 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1016 NTSTATUS status;
1018 status = NtCancelTimer(handle, NULL);
1019 if (status != STATUS_SUCCESS)
1021 SetLastError( RtlNtStatusToDosError(status) );
1022 return FALSE;
1024 return TRUE;
1028 /***********************************************************************
1029 * CreateTimerQueue (KERNEL32.@)
1031 HANDLE WINAPI CreateTimerQueue(void)
1033 FIXME("stub\n");
1034 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1035 return NULL;
1039 /***********************************************************************
1040 * DeleteTimerQueueEx (KERNEL32.@)
1042 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1044 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
1045 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1046 return 0;
1049 /***********************************************************************
1050 * CreateTimerQueueTimer (KERNEL32.@)
1052 * Creates a timer-queue timer. This timer expires at the specified due
1053 * time (in ms), then after every specified period (in ms). When the timer
1054 * expires, the callback function is called.
1056 * RETURNS
1057 * nonzero on success or zero on faillure
1059 * BUGS
1060 * Unimplemented
1062 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1063 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1064 DWORD DueTime, DWORD Period, ULONG Flags )
1066 FIXME("stub\n");
1067 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1068 return TRUE;
1071 /***********************************************************************
1072 * DeleteTimerQueueTimer (KERNEL32.@)
1074 * Cancels a timer-queue timer.
1076 * RETURNS
1077 * nonzero on success or zero on faillure
1079 * BUGS
1080 * Unimplemented
1082 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1083 HANDLE CompletionEvent )
1085 FIXME("stub\n");
1086 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1087 return TRUE;
1092 * Pipes
1096 /***********************************************************************
1097 * CreateNamedPipeA (KERNEL32.@)
1099 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1100 DWORD dwPipeMode, DWORD nMaxInstances,
1101 DWORD nOutBufferSize, DWORD nInBufferSize,
1102 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1104 WCHAR buffer[MAX_PATH];
1106 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1107 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1109 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1111 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1112 return INVALID_HANDLE_VALUE;
1114 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1115 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1119 /***********************************************************************
1120 * CreateNamedPipeW (KERNEL32.@)
1122 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1123 DWORD dwPipeMode, DWORD nMaxInstances,
1124 DWORD nOutBufferSize, DWORD nInBufferSize,
1125 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1127 HANDLE handle;
1128 UNICODE_STRING nt_name;
1129 OBJECT_ATTRIBUTES attr;
1130 DWORD options;
1131 BOOLEAN pipe_type, read_mode, non_block;
1132 NTSTATUS status;
1133 IO_STATUS_BLOCK iosb;
1134 LARGE_INTEGER timeout;
1136 TRACE("(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n",
1137 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1138 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1140 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1142 SetLastError( ERROR_PATH_NOT_FOUND );
1143 return INVALID_HANDLE_VALUE;
1145 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1147 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1148 RtlFreeUnicodeString( &nt_name );
1149 return INVALID_HANDLE_VALUE;
1152 attr.Length = sizeof(attr);
1153 attr.RootDirectory = 0;
1154 attr.ObjectName = &nt_name;
1155 attr.Attributes = OBJ_CASE_INSENSITIVE |
1156 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1157 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1158 attr.SecurityQualityOfService = NULL;
1160 options = 0;
1161 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1162 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1163 if ((dwOpenMode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
1164 options |= FILE_PIPE_FULL_DUPLEX;
1165 else if (dwOpenMode & PIPE_ACCESS_INBOUND) options |= FILE_PIPE_INBOUND;
1166 else if (dwOpenMode & PIPE_ACCESS_OUTBOUND) options |= FILE_PIPE_OUTBOUND;
1167 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1168 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1169 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1170 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0UL;
1172 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1174 SetLastError(0);
1176 status = NtCreateNamedPipeFile(&handle, GENERIC_READ|GENERIC_WRITE, &attr, &iosb,
1177 0, FILE_OVERWRITE_IF, options, pipe_type,
1178 read_mode, non_block, nMaxInstances,
1179 nInBufferSize, nOutBufferSize, &timeout);
1181 RtlFreeUnicodeString( &nt_name );
1182 if (status)
1184 handle = INVALID_HANDLE_VALUE;
1185 SetLastError( RtlNtStatusToDosError(status) );
1187 return handle;
1191 /***********************************************************************
1192 * PeekNamedPipe (KERNEL32.@)
1194 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1195 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1197 #ifdef FIONREAD
1198 int avail=0, fd, ret, flags;
1200 TRACE("(%p,%p,%lu,%p,%p,%p)\n", hPipe, lpvBuffer, cbBuffer, lpcbRead, lpcbAvail, lpcbMessage);
1202 ret = wine_server_handle_to_fd( hPipe, FILE_READ_DATA, &fd, &flags );
1203 if (ret)
1205 SetLastError( RtlNtStatusToDosError(ret) );
1206 return FALSE;
1208 if (flags & FD_FLAG_RECV_SHUTDOWN)
1210 wine_server_release_fd( hPipe, fd );
1211 SetLastError ( ERROR_PIPE_NOT_CONNECTED );
1212 return FALSE;
1215 if (ioctl(fd,FIONREAD, &avail ) != 0)
1217 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1218 wine_server_release_fd( hPipe, fd );
1219 return FALSE;
1221 if (!avail) /* check for closed pipe */
1223 struct pollfd pollfd;
1224 pollfd.fd = fd;
1225 pollfd.events = POLLIN;
1226 pollfd.revents = 0;
1227 switch (poll( &pollfd, 1, 0 ))
1229 case 0:
1230 break;
1231 case 1: /* got something */
1232 if (!(pollfd.revents & (POLLHUP | POLLERR))) break;
1233 TRACE("POLLHUP | POLLERR\n");
1234 /* fall through */
1235 case -1:
1236 wine_server_release_fd( hPipe, fd );
1237 SetLastError(ERROR_BROKEN_PIPE);
1238 return FALSE;
1241 TRACE(" 0x%08x bytes available\n", avail );
1242 ret = TRUE;
1243 if (lpcbAvail)
1244 *lpcbAvail = avail;
1245 if (lpcbRead)
1246 *lpcbRead = 0;
1247 if (avail && lpvBuffer && cbBuffer)
1249 int readbytes = (avail < cbBuffer) ? avail : cbBuffer;
1250 readbytes = recv(fd, lpvBuffer, readbytes, MSG_PEEK);
1251 if (readbytes < 0)
1253 WARN("failed to peek socket (%d)\n", errno);
1254 ret = FALSE;
1256 else if (lpcbRead)
1257 *lpcbRead = readbytes;
1259 wine_server_release_fd( hPipe, fd );
1260 return ret;
1261 #endif /* defined(FIONREAD) */
1263 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1264 FIXME("function not implemented\n");
1265 return FALSE;
1268 /***********************************************************************
1269 * WaitNamedPipeA (KERNEL32.@)
1271 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1273 WCHAR buffer[MAX_PATH];
1275 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1277 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1279 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1280 return 0;
1282 return WaitNamedPipeW( buffer, nTimeOut );
1286 /***********************************************************************
1287 * WaitNamedPipeW (KERNEL32.@)
1289 * Waits for a named pipe instance to become available
1291 * PARAMS
1292 * name [I] Pointer to a named pipe name to wait for
1293 * nTimeOut [I] How long to wait in ms
1295 * RETURNS
1296 * TRUE: Success, named pipe can be opened with CreteFile
1297 * FALSE: Failure, GetLastError can be called for further details
1299 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1301 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1302 NTSTATUS status;
1303 UNICODE_STRING nt_name, pipe_dev_name;
1304 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1305 IO_STATUS_BLOCK iosb;
1306 OBJECT_ATTRIBUTES attr;
1307 ULONG sz_pipe_wait;
1308 HANDLE pipe_dev;
1310 TRACE("%s 0x%08lx\n",debugstr_w(name),nTimeOut);
1312 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1313 return FALSE;
1315 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1316 nt_name.Length < sizeof(leadin) ||
1317 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR) != 0))
1319 RtlFreeUnicodeString( &nt_name );
1320 SetLastError( ERROR_PATH_NOT_FOUND );
1321 return FALSE;
1324 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1325 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1327 RtlFreeUnicodeString( &nt_name );
1328 SetLastError( ERROR_OUTOFMEMORY );
1329 return FALSE;
1332 pipe_dev_name.Buffer = nt_name.Buffer;
1333 pipe_dev_name.Length = sizeof(leadin);
1334 pipe_dev_name.MaximumLength = sizeof(leadin);
1335 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1336 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1337 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1338 FILE_SYNCHRONOUS_IO_NONALERT);
1339 if (status != ERROR_SUCCESS)
1341 SetLastError( ERROR_PATH_NOT_FOUND );
1342 return FALSE;
1345 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1346 pipe_wait->Timeout.QuadPart = nTimeOut * -10000L;
1347 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1348 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1349 pipe_wait->NameLength);
1350 RtlFreeUnicodeString( &nt_name );
1352 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1353 pipe_wait, sz_pipe_wait, NULL, 0 );
1355 HeapFree( GetProcessHeap(), 0, pipe_wait );
1356 NtClose( pipe_dev );
1358 if(status != STATUS_SUCCESS)
1360 SetLastError(RtlNtStatusToDosError(status));
1361 return FALSE;
1363 else
1364 return TRUE;
1368 /***********************************************************************
1369 * ConnectNamedPipe (KERNEL32.@)
1371 * Connects to a named pipe
1373 * Parameters
1374 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1375 * overlapped: Optional OVERLAPPED struct
1377 * Return values
1378 * TRUE: Success
1379 * FALSE: Failure, GetLastError can be called for further details
1381 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1383 NTSTATUS status;
1384 IO_STATUS_BLOCK status_block;
1386 TRACE("(%p,%p)\n", hPipe, overlapped);
1388 if(overlapped)
1389 overlapped->Internal = STATUS_PENDING;
1391 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, NULL,
1392 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1393 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1395 if (status == STATUS_SUCCESS) return TRUE;
1396 SetLastError( RtlNtStatusToDosError(status) );
1397 return FALSE;
1400 /***********************************************************************
1401 * DisconnectNamedPipe (KERNEL32.@)
1403 * Disconnects from a named pipe
1405 * Parameters
1406 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1408 * Return values
1409 * TRUE: Success
1410 * FALSE: Failure, GetLastError can be called for further details
1412 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1414 NTSTATUS status;
1415 IO_STATUS_BLOCK io_block;
1417 TRACE("(%p)\n",hPipe);
1419 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1420 NULL, 0, NULL, 0);
1421 if (status == STATUS_SUCCESS) return TRUE;
1422 SetLastError( RtlNtStatusToDosError(status) );
1423 return FALSE;
1426 /***********************************************************************
1427 * TransactNamedPipe (KERNEL32.@)
1429 * BUGS
1430 * should be done as a single operation in the wineserver or kernel
1432 BOOL WINAPI TransactNamedPipe(
1433 HANDLE handle, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1434 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1436 BOOL r;
1437 DWORD count;
1439 TRACE("%p %p %ld %p %ld %p %p\n",
1440 handle, lpInput, dwInputSize, lpOutput,
1441 dwOutputSize, lpBytesRead, lpOverlapped);
1443 if (lpOverlapped)
1445 FIXME("Doesn't support overlapped operation as yet\n");
1446 return FALSE;
1449 r = WriteFile(handle, lpOutput, dwOutputSize, &count, NULL);
1450 if (r)
1451 r = ReadFile(handle, lpInput, dwInputSize, lpBytesRead, NULL);
1453 return r;
1456 /***********************************************************************
1457 * GetNamedPipeInfo (KERNEL32.@)
1459 BOOL WINAPI GetNamedPipeInfo(
1460 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1461 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1463 BOOL ret;
1465 TRACE("%p %p %p %p %p\n", hNamedPipe, lpFlags,
1466 lpOutputBufferSize, lpInputBufferSize, lpMaxInstances);
1468 SERVER_START_REQ( get_named_pipe_info )
1470 req->handle = hNamedPipe;
1471 ret = !wine_server_call_err( req );
1472 if (lpFlags)
1474 *lpFlags = 0;
1475 if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)
1476 *lpFlags |= PIPE_TYPE_MESSAGE;
1477 if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
1478 *lpFlags |= PIPE_READMODE_MESSAGE;
1479 if (reply->flags & NAMED_PIPE_NONBLOCKING_MODE)
1480 *lpFlags |= PIPE_NOWAIT;
1482 if (lpOutputBufferSize) *lpOutputBufferSize = reply->outsize;
1483 if (lpInputBufferSize) *lpInputBufferSize = reply->outsize;
1484 if (lpMaxInstances) *lpMaxInstances = reply->maxinstances;
1486 SERVER_END_REQ;
1488 return ret;
1491 /***********************************************************************
1492 * GetNamedPipeHandleStateA (KERNEL32.@)
1494 BOOL WINAPI GetNamedPipeHandleStateA(
1495 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1496 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1497 LPSTR lpUsername, DWORD nUsernameMaxSize)
1499 FIXME("%p %p %p %p %p %p %ld\n",
1500 hNamedPipe, lpState, lpCurInstances,
1501 lpMaxCollectionCount, lpCollectDataTimeout,
1502 lpUsername, nUsernameMaxSize);
1504 return FALSE;
1507 /***********************************************************************
1508 * GetNamedPipeHandleStateW (KERNEL32.@)
1510 BOOL WINAPI GetNamedPipeHandleStateW(
1511 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1512 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1513 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1515 FIXME("%p %p %p %p %p %p %ld\n",
1516 hNamedPipe, lpState, lpCurInstances,
1517 lpMaxCollectionCount, lpCollectDataTimeout,
1518 lpUsername, nUsernameMaxSize);
1520 return FALSE;
1523 /***********************************************************************
1524 * SetNamedPipeHandleState (KERNEL32.@)
1526 BOOL WINAPI SetNamedPipeHandleState(
1527 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1528 LPDWORD lpCollectDataTimeout)
1530 /* should be a fixme, but this function is called a lot by the RPC
1531 * runtime, and it slows down InstallShield a fair bit. */
1532 WARN("stub: %p %p/%ld %p %p\n",
1533 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1534 return FALSE;
1537 /***********************************************************************
1538 * CallNamedPipeA (KERNEL32.@)
1540 BOOL WINAPI CallNamedPipeA(
1541 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1542 LPVOID lpOutput, DWORD dwOutputSize,
1543 LPDWORD lpBytesRead, DWORD nTimeout)
1545 DWORD len;
1546 LPWSTR str = NULL;
1547 BOOL ret;
1549 TRACE("%s %p %ld %p %ld %p %ld\n",
1550 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1551 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1553 if( lpNamedPipeName )
1555 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1556 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1557 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1559 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1560 dwOutputSize, lpBytesRead, nTimeout );
1561 if( lpNamedPipeName )
1562 HeapFree( GetProcessHeap(), 0, str );
1564 return ret;
1567 /***********************************************************************
1568 * CallNamedPipeW (KERNEL32.@)
1570 BOOL WINAPI CallNamedPipeW(
1571 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1572 LPVOID lpOutput, DWORD lpOutputSize,
1573 LPDWORD lpBytesRead, DWORD nTimeout)
1575 FIXME("%s %p %ld %p %ld %p %ld\n",
1576 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1577 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1578 return FALSE;
1581 /******************************************************************
1582 * CreatePipe (KERNEL32.@)
1585 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1586 LPSECURITY_ATTRIBUTES sa, DWORD size )
1588 static unsigned index /* = 0 */;
1589 WCHAR name[64];
1590 HANDLE hr, hw;
1591 unsigned in_index = index;
1592 UNICODE_STRING nt_name;
1593 OBJECT_ATTRIBUTES attr;
1594 NTSTATUS status;
1595 IO_STATUS_BLOCK iosb;
1596 LARGE_INTEGER timeout;
1598 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1600 attr.Length = sizeof(attr);
1601 attr.RootDirectory = 0;
1602 attr.ObjectName = &nt_name;
1603 attr.Attributes = OBJ_CASE_INSENSITIVE |
1604 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1605 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1606 attr.SecurityQualityOfService = NULL;
1608 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1609 /* generate a unique pipe name (system wide) */
1612 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1613 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1614 'u','.','%','0','8','u','\0' };
1616 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1617 GetCurrentProcessId(), ++index);
1618 RtlInitUnicodeString(&nt_name, name);
1619 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1620 0, FILE_OVERWRITE_IF,
1621 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1622 FALSE, FALSE, FALSE,
1623 1, size, size, &timeout);
1624 if (status)
1626 SetLastError( RtlNtStatusToDosError(status) );
1627 hr = INVALID_HANDLE_VALUE;
1629 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1630 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1631 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1633 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1634 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1636 if (status)
1638 SetLastError( RtlNtStatusToDosError(status) );
1639 NtClose(hr);
1640 return FALSE;
1643 *hReadPipe = hr;
1644 *hWritePipe = hw;
1645 return TRUE;
1649 /******************************************************************************
1650 * CreateMailslotA [KERNEL32.@]
1652 * See CreatMailslotW.
1654 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1655 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1657 DWORD len;
1658 HANDLE handle;
1659 LPWSTR name = NULL;
1661 TRACE("%s %ld %ld %p\n", debugstr_a(lpName),
1662 nMaxMessageSize, lReadTimeout, sa);
1664 if( lpName )
1666 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1667 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1668 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1671 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1673 HeapFree( GetProcessHeap(), 0, name );
1675 return handle;
1679 /******************************************************************************
1680 * CreateMailslotW [KERNEL32.@]
1682 * Create a mailslot with specified name.
1684 * PARAMS
1685 * lpName [I] Pointer to string for mailslot name
1686 * nMaxMessageSize [I] Maximum message size
1687 * lReadTimeout [I] Milliseconds before read time-out
1688 * sa [I] Pointer to security structure
1690 * RETURNS
1691 * Success: Handle to mailslot
1692 * Failure: INVALID_HANDLE_VALUE
1694 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1695 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1697 HANDLE handle = INVALID_HANDLE_VALUE;
1698 OBJECT_ATTRIBUTES attr;
1699 UNICODE_STRING nameW;
1700 LARGE_INTEGER timeout;
1701 IO_STATUS_BLOCK iosb;
1702 NTSTATUS status;
1704 TRACE("%s %ld %ld %p\n", debugstr_w(lpName),
1705 nMaxMessageSize, lReadTimeout, sa);
1707 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1709 SetLastError( ERROR_PATH_NOT_FOUND );
1710 return INVALID_HANDLE_VALUE;
1713 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1715 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1716 RtlFreeUnicodeString( &nameW );
1717 return INVALID_HANDLE_VALUE;
1720 attr.Length = sizeof(attr);
1721 attr.RootDirectory = 0;
1722 attr.Attributes = OBJ_CASE_INSENSITIVE;
1723 attr.ObjectName = &nameW;
1724 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1725 attr.SecurityQualityOfService = NULL;
1727 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1728 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1729 else
1730 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1732 status = NtCreateMailslotFile( &handle, GENERIC_READ | GENERIC_WRITE, &attr,
1733 &iosb, 0, 0, nMaxMessageSize, &timeout );
1734 if (status)
1736 SetLastError( RtlNtStatusToDosError(status) );
1737 handle = INVALID_HANDLE_VALUE;
1740 RtlFreeUnicodeString( &nameW );
1741 return handle;
1745 /******************************************************************************
1746 * GetMailslotInfo [KERNEL32.@]
1748 * Retrieve information about a mailslot.
1750 * PARAMS
1751 * hMailslot [I] Mailslot handle
1752 * lpMaxMessageSize [O] Address of maximum message size
1753 * lpNextSize [O] Address of size of next message
1754 * lpMessageCount [O] Address of number of messages
1755 * lpReadTimeout [O] Address of read time-out
1757 * RETURNS
1758 * Success: TRUE
1759 * Failure: FALSE
1761 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1762 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1763 LPDWORD lpReadTimeout )
1765 FILE_MAILSLOT_QUERY_INFORMATION info;
1766 IO_STATUS_BLOCK iosb;
1767 NTSTATUS status;
1769 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1770 lpNextSize, lpMessageCount, lpReadTimeout);
1772 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1773 FileMailslotQueryInformation );
1775 if( status != STATUS_SUCCESS )
1777 SetLastError( RtlNtStatusToDosError(status) );
1778 return FALSE;
1781 if( lpMaxMessageSize )
1782 *lpMaxMessageSize = info.MaximumMessageSize;
1783 if( lpNextSize )
1784 *lpNextSize = info.NextMessageSize;
1785 if( lpMessageCount )
1786 *lpMessageCount = info.MessagesAvailable;
1787 if( lpReadTimeout )
1788 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1790 return TRUE;
1794 /******************************************************************************
1795 * SetMailslotInfo [KERNEL32.@]
1797 * Set the read timeout of a mailslot.
1799 * PARAMS
1800 * hMailslot [I] Mailslot handle
1801 * dwReadTimeout [I] Timeout in milliseconds.
1803 * RETURNS
1804 * Success: TRUE
1805 * Failure: FALSE
1807 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1809 FILE_MAILSLOT_SET_INFORMATION info;
1810 IO_STATUS_BLOCK iosb;
1811 NTSTATUS status;
1813 TRACE("%p %ld\n", hMailslot, dwReadTimeout);
1815 info.ReadTimeout.QuadPart = dwReadTimeout * -10000;
1816 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1817 FileMailslotSetInformation );
1818 if( status != STATUS_SUCCESS )
1820 SetLastError( RtlNtStatusToDosError(status) );
1821 return FALSE;
1823 return TRUE;
1827 /******************************************************************************
1828 * CreateIoCompletionPort (KERNEL32.@)
1830 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1831 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1833 FIXME("(%p, %p, %08lx, %08lx): stub.\n",
1834 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1835 return NULL;
1839 /******************************************************************************
1840 * GetQueuedCompletionStatus (KERNEL32.@)
1842 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1843 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1844 DWORD dwMilliseconds )
1846 FIXME("(%p,%p,%p,%p,%ld), stub!\n",
1847 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1848 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1849 return FALSE;
1852 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
1853 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
1855 FIXME("%p %ld %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
1856 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1857 return FALSE;
1860 /******************************************************************************
1861 * CreateJobObjectW (KERNEL32.@)
1863 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1865 FIXME("%p %s\n", attr, debugstr_w(name) );
1866 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1867 return 0;
1870 /******************************************************************************
1871 * CreateJobObjectA (KERNEL32.@)
1873 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1875 LPWSTR str = NULL;
1876 UINT len;
1877 HANDLE r;
1879 TRACE("%p %s\n", attr, debugstr_a(name) );
1881 if( name )
1883 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1884 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1885 if( !str )
1887 SetLastError( ERROR_OUTOFMEMORY );
1888 return 0;
1890 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1893 r = CreateJobObjectW( attr, str );
1895 HeapFree( GetProcessHeap(), 0, str );
1897 return r;
1900 /******************************************************************************
1901 * AssignProcessToJobObject (KERNEL32.@)
1903 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1905 FIXME("%p %p\n", hJob, hProcess);
1906 return TRUE;
1909 #ifdef __i386__
1911 /***********************************************************************
1912 * InterlockedCompareExchange (KERNEL32.@)
1914 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1915 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1916 "movl 12(%esp),%eax\n\t"
1917 "movl 8(%esp),%ecx\n\t"
1918 "movl 4(%esp),%edx\n\t"
1919 "lock; cmpxchgl %ecx,(%edx)\n\t"
1920 "ret $12")
1922 /***********************************************************************
1923 * InterlockedExchange (KERNEL32.@)
1925 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1926 __ASM_GLOBAL_FUNC(InterlockedExchange,
1927 "movl 8(%esp),%eax\n\t"
1928 "movl 4(%esp),%edx\n\t"
1929 "lock; xchgl %eax,(%edx)\n\t"
1930 "ret $8")
1932 /***********************************************************************
1933 * InterlockedExchangeAdd (KERNEL32.@)
1935 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1936 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1937 "movl 8(%esp),%eax\n\t"
1938 "movl 4(%esp),%edx\n\t"
1939 "lock; xaddl %eax,(%edx)\n\t"
1940 "ret $8")
1942 /***********************************************************************
1943 * InterlockedIncrement (KERNEL32.@)
1945 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1946 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1947 "movl 4(%esp),%edx\n\t"
1948 "movl $1,%eax\n\t"
1949 "lock; xaddl %eax,(%edx)\n\t"
1950 "incl %eax\n\t"
1951 "ret $4")
1953 /***********************************************************************
1954 * InterlockedDecrement (KERNEL32.@)
1956 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1957 "movl 4(%esp),%edx\n\t"
1958 "movl $-1,%eax\n\t"
1959 "lock; xaddl %eax,(%edx)\n\t"
1960 "decl %eax\n\t"
1961 "ret $4")
1963 #else /* __i386__ */
1965 /***********************************************************************
1966 * InterlockedCompareExchange (KERNEL32.@)
1968 * Atomically swap one value with another.
1970 * PARAMS
1971 * dest [I/O] The value to replace
1972 * xchq [I] The value to be swapped
1973 * compare [I] The value to compare to dest
1975 * RETURNS
1976 * The resulting value of dest.
1978 * NOTES
1979 * dest is updated only if it is equal to compare, otherwise no swap is done.
1981 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
1983 return interlocked_cmpxchg( (int *)dest, xchg, compare );
1986 /***********************************************************************
1987 * InterlockedExchange (KERNEL32.@)
1989 * Atomically swap one value with another.
1991 * PARAMS
1992 * dest [I/O] The value to replace
1993 * val [I] The value to be swapped
1995 * RETURNS
1996 * The resulting value of dest.
1998 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
2000 return interlocked_xchg( (int *)dest, val );
2003 /***********************************************************************
2004 * InterlockedExchangeAdd (KERNEL32.@)
2006 * Atomically add one value to another.
2008 * PARAMS
2009 * dest [I/O] The value to add to
2010 * incr [I] The value to be added
2012 * RETURNS
2013 * The resulting value of dest.
2015 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
2017 return interlocked_xchg_add( (int *)dest, incr );
2020 /***********************************************************************
2021 * InterlockedIncrement (KERNEL32.@)
2023 * Atomically increment a value.
2025 * PARAMS
2026 * dest [I/O] The value to increment
2028 * RETURNS
2029 * The resulting value of dest.
2031 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
2033 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
2036 /***********************************************************************
2037 * InterlockedDecrement (KERNEL32.@)
2039 * Atomically decrement a value.
2041 * PARAMS
2042 * dest [I/O] The value to decrement
2044 * RETURNS
2045 * The resulting value of dest.
2047 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
2049 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
2052 #endif /* __i386__ */