kernel32: Implement IO completion functions on top of the NT IoCompletion API.
[wine.git] / dlls / kernel32 / sync.c
blob07bd9b0c84ce5852dd59d702f708f1cb7b758a1a
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #include <stdarg.h>
30 #include <stdio.h>
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
35 #include "ntstatus.h"
36 #define WIN32_NO_STATUS
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winerror.h"
40 #include "winnls.h"
41 #include "winternl.h"
42 #include "winioctl.h"
43 #include "ddk/wdm.h"
45 #include "wine/unicode.h"
46 #include "wine/winbase16.h"
47 #include "kernel_private.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(sync);
53 /* check if current version is NT or Win95 */
54 static inline int is_version_nt(void)
56 return !(GetVersion() & 0x80000000);
59 /* returns directory handle to \\BaseNamedObjects */
60 HANDLE get_BaseNamedObjects_handle(void)
62 static HANDLE handle = NULL;
63 static const WCHAR basenameW[] =
64 {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
65 UNICODE_STRING str;
66 OBJECT_ATTRIBUTES attr;
68 if (!handle)
70 HANDLE dir;
72 RtlInitUnicodeString(&str, basenameW);
73 InitializeObjectAttributes(&attr, &str, 0, 0, NULL);
74 NtOpenDirectoryObject(&dir, DIRECTORY_CREATE_OBJECT|DIRECTORY_TRAVERSE,
75 &attr);
76 if (InterlockedCompareExchangePointer( (PVOID)&handle, dir, 0 ) != 0)
78 /* someone beat us here... */
79 CloseHandle( dir );
82 return handle;
85 /* helper for kernel32->ntdll timeout format conversion */
86 static inline PLARGE_INTEGER get_nt_timeout( PLARGE_INTEGER pTime, DWORD timeout )
88 if (timeout == INFINITE) return NULL;
89 pTime->QuadPart = (ULONGLONG)timeout * -10000;
90 return pTime;
93 /***********************************************************************
94 * Sleep (KERNEL32.@)
96 VOID WINAPI Sleep( DWORD timeout )
98 SleepEx( timeout, FALSE );
101 /******************************************************************************
102 * SleepEx (KERNEL32.@)
104 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
106 NTSTATUS status;
107 LARGE_INTEGER time;
109 status = NtDelayExecution( alertable, get_nt_timeout( &time, timeout ) );
110 if (status == STATUS_USER_APC) return WAIT_IO_COMPLETION;
111 return 0;
115 /***********************************************************************
116 * SwitchToThread (KERNEL32.@)
118 BOOL WINAPI SwitchToThread(void)
120 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
124 /***********************************************************************
125 * WaitForSingleObject (KERNEL32.@)
127 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
129 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
133 /***********************************************************************
134 * WaitForSingleObjectEx (KERNEL32.@)
136 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
137 BOOL alertable )
139 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
143 /***********************************************************************
144 * WaitForMultipleObjects (KERNEL32.@)
146 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
147 BOOL wait_all, DWORD timeout )
149 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
153 /***********************************************************************
154 * WaitForMultipleObjectsEx (KERNEL32.@)
156 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
157 BOOL wait_all, DWORD timeout,
158 BOOL alertable )
160 NTSTATUS status;
161 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
162 LARGE_INTEGER time;
163 unsigned int i;
165 if (count > MAXIMUM_WAIT_OBJECTS)
167 SetLastError(ERROR_INVALID_PARAMETER);
168 return WAIT_FAILED;
170 for (i = 0; i < count; i++)
172 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
173 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
174 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
175 hloc[i] = GetStdHandle( HandleToULong(handles[i]) );
176 else
177 hloc[i] = handles[i];
179 /* yes, even screen buffer console handles are waitable, and are
180 * handled as a handle to the console itself !!
182 if (is_console_handle(hloc[i]))
184 if (!VerifyConsoleIoHandle(hloc[i]))
186 return FALSE;
188 hloc[i] = GetConsoleInputWaitHandle();
192 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable,
193 get_nt_timeout( &time, timeout ) );
195 if (HIWORD(status)) /* is it an error code? */
197 SetLastError( RtlNtStatusToDosError(status) );
198 status = WAIT_FAILED;
200 return status;
204 /***********************************************************************
205 * WaitForSingleObject (KERNEL.460)
207 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
209 DWORD retval, mutex_count;
211 ReleaseThunkLock( &mutex_count );
212 retval = WaitForSingleObject( handle, timeout );
213 RestoreThunkLock( mutex_count );
214 return retval;
217 /***********************************************************************
218 * WaitForMultipleObjects (KERNEL.461)
220 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
221 BOOL wait_all, DWORD timeout )
223 DWORD retval, mutex_count;
225 ReleaseThunkLock( &mutex_count );
226 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
227 RestoreThunkLock( mutex_count );
228 return retval;
231 /***********************************************************************
232 * WaitForMultipleObjectsEx (KERNEL.495)
234 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
235 BOOL wait_all, DWORD timeout, BOOL alertable )
237 DWORD retval, mutex_count;
239 ReleaseThunkLock( &mutex_count );
240 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
241 RestoreThunkLock( mutex_count );
242 return retval;
245 /***********************************************************************
246 * RegisterWaitForSingleObject (KERNEL32.@)
248 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
249 WAITORTIMERCALLBACK Callback, PVOID Context,
250 ULONG dwMilliseconds, ULONG dwFlags)
252 FIXME("%p %p %p %p %d %d\n",
253 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
254 return FALSE;
257 /***********************************************************************
258 * RegisterWaitForSingleObjectEx (KERNEL32.@)
260 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
261 WAITORTIMERCALLBACK Callback, PVOID Context,
262 ULONG dwMilliseconds, ULONG dwFlags )
264 FIXME("%p %p %p %d %d\n",
265 hObject,Callback,Context,dwMilliseconds,dwFlags);
266 return 0;
269 /***********************************************************************
270 * UnregisterWait (KERNEL32.@)
272 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
274 FIXME("%p\n",WaitHandle);
275 return FALSE;
278 /***********************************************************************
279 * UnregisterWaitEx (KERNEL32.@)
281 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
283 FIXME("%p %p\n",WaitHandle, CompletionEvent);
284 return FALSE;
287 /***********************************************************************
288 * SignalObjectAndWait (KERNEL32.@)
290 * Allows to atomically signal any of the synchro objects (semaphore,
291 * mutex, event) and wait on another.
293 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
294 DWORD dwMilliseconds, BOOL bAlertable )
296 NTSTATUS status;
297 LARGE_INTEGER timeout;
299 TRACE("%p %p %d %d\n", hObjectToSignal,
300 hObjectToWaitOn, dwMilliseconds, bAlertable);
302 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn, bAlertable,
303 get_nt_timeout( &timeout, dwMilliseconds ) );
304 if (HIWORD(status))
306 SetLastError( RtlNtStatusToDosError(status) );
307 status = WAIT_FAILED;
309 return status;
312 /***********************************************************************
313 * InitializeCriticalSection (KERNEL32.@)
315 * Initialise a critical section before use.
317 * PARAMS
318 * crit [O] Critical section to initialise.
320 * RETURNS
321 * Nothing. If the function fails an exception is raised.
323 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
325 NTSTATUS ret = RtlInitializeCriticalSection( crit );
326 if (ret) RtlRaiseStatus( ret );
329 /***********************************************************************
330 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
332 * Initialise a critical section with a spin count.
334 * PARAMS
335 * crit [O] Critical section to initialise.
336 * spincount [I] Number of times to spin upon contention.
338 * RETURNS
339 * Success: TRUE.
340 * Failure: Nothing. If the function fails an exception is raised.
342 * NOTES
343 * spincount is ignored on uni-processor systems.
345 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
347 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
348 if (ret) RtlRaiseStatus( ret );
349 return !ret;
352 /***********************************************************************
353 * MakeCriticalSectionGlobal (KERNEL32.@)
355 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
357 /* let's assume that only one thread at a time will try to do this */
358 HANDLE sem = crit->LockSemaphore;
359 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
360 crit->LockSemaphore = ConvertToGlobalHandle( sem );
361 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
362 crit->DebugInfo = NULL;
366 /***********************************************************************
367 * ReinitializeCriticalSection (KERNEL32.@)
369 * Initialise an already used critical section.
371 * PARAMS
372 * crit [O] Critical section to initialise.
374 * RETURNS
375 * Nothing.
377 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
379 if ( !crit->LockSemaphore )
380 RtlInitializeCriticalSection( crit );
384 /***********************************************************************
385 * UninitializeCriticalSection (KERNEL32.@)
387 * UnInitialise a critical section after use.
389 * PARAMS
390 * crit [O] Critical section to uninitialise (destroy).
392 * RETURNS
393 * Nothing.
395 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
397 RtlDeleteCriticalSection( crit );
401 /***********************************************************************
402 * CreateEventA (KERNEL32.@)
404 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
405 BOOL initial_state, LPCSTR name )
407 WCHAR buffer[MAX_PATH];
409 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
411 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
413 SetLastError( ERROR_FILENAME_EXCED_RANGE );
414 return 0;
416 return CreateEventW( sa, manual_reset, initial_state, buffer );
420 /***********************************************************************
421 * CreateEventW (KERNEL32.@)
423 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
424 BOOL initial_state, LPCWSTR name )
426 HANDLE ret;
427 UNICODE_STRING nameW;
428 OBJECT_ATTRIBUTES attr;
429 NTSTATUS status;
431 /* one buggy program needs this
432 * ("Van Dale Groot woordenboek der Nederlandse taal")
434 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
436 ERR("Bad security attributes pointer %p\n",sa);
437 SetLastError( ERROR_INVALID_PARAMETER);
438 return 0;
441 attr.Length = sizeof(attr);
442 attr.RootDirectory = 0;
443 attr.ObjectName = NULL;
444 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
445 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
446 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
447 attr.SecurityQualityOfService = NULL;
448 if (name)
450 RtlInitUnicodeString( &nameW, name );
451 attr.ObjectName = &nameW;
452 attr.RootDirectory = get_BaseNamedObjects_handle();
455 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
456 if (status == STATUS_OBJECT_NAME_EXISTS)
457 SetLastError( ERROR_ALREADY_EXISTS );
458 else
459 SetLastError( RtlNtStatusToDosError(status) );
460 return ret;
464 /***********************************************************************
465 * CreateW32Event (KERNEL.457)
467 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
469 return CreateEventW( NULL, manual_reset, initial_state, NULL );
473 /***********************************************************************
474 * OpenEventA (KERNEL32.@)
476 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
478 WCHAR buffer[MAX_PATH];
480 if (!name) return OpenEventW( access, inherit, NULL );
482 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
484 SetLastError( ERROR_FILENAME_EXCED_RANGE );
485 return 0;
487 return OpenEventW( access, inherit, buffer );
491 /***********************************************************************
492 * OpenEventW (KERNEL32.@)
494 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
496 HANDLE ret;
497 UNICODE_STRING nameW;
498 OBJECT_ATTRIBUTES attr;
499 NTSTATUS status;
501 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
503 attr.Length = sizeof(attr);
504 attr.RootDirectory = 0;
505 attr.ObjectName = NULL;
506 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
507 attr.SecurityDescriptor = NULL;
508 attr.SecurityQualityOfService = NULL;
509 if (name)
511 RtlInitUnicodeString( &nameW, name );
512 attr.ObjectName = &nameW;
513 attr.RootDirectory = get_BaseNamedObjects_handle();
516 status = NtOpenEvent( &ret, access, &attr );
517 if (status != STATUS_SUCCESS)
519 SetLastError( RtlNtStatusToDosError(status) );
520 return 0;
522 return ret;
525 /***********************************************************************
526 * PulseEvent (KERNEL32.@)
528 BOOL WINAPI PulseEvent( HANDLE handle )
530 NTSTATUS status;
532 if ((status = NtPulseEvent( handle, NULL )))
533 SetLastError( RtlNtStatusToDosError(status) );
534 return !status;
538 /***********************************************************************
539 * SetW32Event (KERNEL.458)
540 * SetEvent (KERNEL32.@)
542 BOOL WINAPI SetEvent( HANDLE handle )
544 NTSTATUS status;
546 if ((status = NtSetEvent( handle, NULL )))
547 SetLastError( RtlNtStatusToDosError(status) );
548 return !status;
552 /***********************************************************************
553 * ResetW32Event (KERNEL.459)
554 * ResetEvent (KERNEL32.@)
556 BOOL WINAPI ResetEvent( HANDLE handle )
558 NTSTATUS status;
560 if ((status = NtResetEvent( handle, NULL )))
561 SetLastError( RtlNtStatusToDosError(status) );
562 return !status;
566 /***********************************************************************
567 * NOTE: The Win95 VWin32_Event routines given below are really low-level
568 * routines implemented directly by VWin32. The user-mode libraries
569 * implement Win32 synchronisation routines on top of these low-level
570 * primitives. We do it the other way around here :-)
573 /***********************************************************************
574 * VWin32_EventCreate (KERNEL.442)
576 HANDLE WINAPI VWin32_EventCreate(VOID)
578 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
579 return ConvertToGlobalHandle( hEvent );
582 /***********************************************************************
583 * VWin32_EventDestroy (KERNEL.443)
585 VOID WINAPI VWin32_EventDestroy(HANDLE event)
587 CloseHandle( event );
590 /***********************************************************************
591 * VWin32_EventWait (KERNEL.450)
593 VOID WINAPI VWin32_EventWait(HANDLE event)
595 DWORD mutex_count;
597 ReleaseThunkLock( &mutex_count );
598 WaitForSingleObject( event, INFINITE );
599 RestoreThunkLock( mutex_count );
602 /***********************************************************************
603 * VWin32_EventSet (KERNEL.451)
604 * KERNEL_479 (KERNEL.479)
606 VOID WINAPI VWin32_EventSet(HANDLE event)
608 SetEvent( event );
613 /***********************************************************************
614 * CreateMutexA (KERNEL32.@)
616 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
618 WCHAR buffer[MAX_PATH];
620 if (!name) return CreateMutexW( sa, owner, NULL );
622 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
624 SetLastError( ERROR_FILENAME_EXCED_RANGE );
625 return 0;
627 return CreateMutexW( sa, owner, buffer );
631 /***********************************************************************
632 * CreateMutexW (KERNEL32.@)
634 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
636 HANDLE ret;
637 UNICODE_STRING nameW;
638 OBJECT_ATTRIBUTES attr;
639 NTSTATUS status;
641 attr.Length = sizeof(attr);
642 attr.RootDirectory = 0;
643 attr.ObjectName = NULL;
644 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
645 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
646 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
647 attr.SecurityQualityOfService = NULL;
648 if (name)
650 RtlInitUnicodeString( &nameW, name );
651 attr.ObjectName = &nameW;
652 attr.RootDirectory = get_BaseNamedObjects_handle();
655 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
656 if (status == STATUS_OBJECT_NAME_EXISTS)
657 SetLastError( ERROR_ALREADY_EXISTS );
658 else
659 SetLastError( RtlNtStatusToDosError(status) );
660 return ret;
664 /***********************************************************************
665 * OpenMutexA (KERNEL32.@)
667 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
669 WCHAR buffer[MAX_PATH];
671 if (!name) return OpenMutexW( access, inherit, NULL );
673 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
675 SetLastError( ERROR_FILENAME_EXCED_RANGE );
676 return 0;
678 return OpenMutexW( access, inherit, buffer );
682 /***********************************************************************
683 * OpenMutexW (KERNEL32.@)
685 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
687 HANDLE ret;
688 UNICODE_STRING nameW;
689 OBJECT_ATTRIBUTES attr;
690 NTSTATUS status;
692 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
694 attr.Length = sizeof(attr);
695 attr.RootDirectory = 0;
696 attr.ObjectName = NULL;
697 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
698 attr.SecurityDescriptor = NULL;
699 attr.SecurityQualityOfService = NULL;
700 if (name)
702 RtlInitUnicodeString( &nameW, name );
703 attr.ObjectName = &nameW;
704 attr.RootDirectory = get_BaseNamedObjects_handle();
707 status = NtOpenMutant( &ret, access, &attr );
708 if (status != STATUS_SUCCESS)
710 SetLastError( RtlNtStatusToDosError(status) );
711 return 0;
713 return ret;
717 /***********************************************************************
718 * ReleaseMutex (KERNEL32.@)
720 BOOL WINAPI ReleaseMutex( HANDLE handle )
722 NTSTATUS status;
724 status = NtReleaseMutant(handle, NULL);
725 if (status != STATUS_SUCCESS)
727 SetLastError( RtlNtStatusToDosError(status) );
728 return FALSE;
730 return TRUE;
735 * Semaphores
739 /***********************************************************************
740 * CreateSemaphoreA (KERNEL32.@)
742 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
744 WCHAR buffer[MAX_PATH];
746 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
748 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
750 SetLastError( ERROR_FILENAME_EXCED_RANGE );
751 return 0;
753 return CreateSemaphoreW( sa, initial, max, buffer );
757 /***********************************************************************
758 * CreateSemaphoreW (KERNEL32.@)
760 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
761 LONG max, LPCWSTR name )
763 HANDLE ret;
764 UNICODE_STRING nameW;
765 OBJECT_ATTRIBUTES attr;
766 NTSTATUS status;
768 attr.Length = sizeof(attr);
769 attr.RootDirectory = 0;
770 attr.ObjectName = NULL;
771 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
772 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
773 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
774 attr.SecurityQualityOfService = NULL;
775 if (name)
777 RtlInitUnicodeString( &nameW, name );
778 attr.ObjectName = &nameW;
779 attr.RootDirectory = get_BaseNamedObjects_handle();
782 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
783 if (status == STATUS_OBJECT_NAME_EXISTS)
784 SetLastError( ERROR_ALREADY_EXISTS );
785 else
786 SetLastError( RtlNtStatusToDosError(status) );
787 return ret;
791 /***********************************************************************
792 * OpenSemaphoreA (KERNEL32.@)
794 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
796 WCHAR buffer[MAX_PATH];
798 if (!name) return OpenSemaphoreW( access, inherit, NULL );
800 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
802 SetLastError( ERROR_FILENAME_EXCED_RANGE );
803 return 0;
805 return OpenSemaphoreW( access, inherit, buffer );
809 /***********************************************************************
810 * OpenSemaphoreW (KERNEL32.@)
812 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
814 HANDLE ret;
815 UNICODE_STRING nameW;
816 OBJECT_ATTRIBUTES attr;
817 NTSTATUS status;
819 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
821 attr.Length = sizeof(attr);
822 attr.RootDirectory = 0;
823 attr.ObjectName = NULL;
824 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
825 attr.SecurityDescriptor = NULL;
826 attr.SecurityQualityOfService = NULL;
827 if (name)
829 RtlInitUnicodeString( &nameW, name );
830 attr.ObjectName = &nameW;
831 attr.RootDirectory = get_BaseNamedObjects_handle();
834 status = NtOpenSemaphore( &ret, access, &attr );
835 if (status != STATUS_SUCCESS)
837 SetLastError( RtlNtStatusToDosError(status) );
838 return 0;
840 return ret;
844 /***********************************************************************
845 * ReleaseSemaphore (KERNEL32.@)
847 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
849 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
850 if (status) SetLastError( RtlNtStatusToDosError(status) );
851 return !status;
856 * Timers
860 /***********************************************************************
861 * CreateWaitableTimerA (KERNEL32.@)
863 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
865 WCHAR buffer[MAX_PATH];
867 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
869 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
871 SetLastError( ERROR_FILENAME_EXCED_RANGE );
872 return 0;
874 return CreateWaitableTimerW( sa, manual, buffer );
878 /***********************************************************************
879 * CreateWaitableTimerW (KERNEL32.@)
881 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
883 HANDLE handle;
884 NTSTATUS status;
885 UNICODE_STRING nameW;
886 OBJECT_ATTRIBUTES attr;
888 attr.Length = sizeof(attr);
889 attr.RootDirectory = 0;
890 attr.ObjectName = NULL;
891 attr.Attributes = OBJ_CASE_INSENSITIVE | OBJ_OPENIF |
892 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
893 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
894 attr.SecurityQualityOfService = NULL;
895 if (name)
897 RtlInitUnicodeString( &nameW, name );
898 attr.ObjectName = &nameW;
899 attr.RootDirectory = get_BaseNamedObjects_handle();
902 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
903 manual ? NotificationTimer : SynchronizationTimer);
904 if (status == STATUS_OBJECT_NAME_EXISTS)
905 SetLastError( ERROR_ALREADY_EXISTS );
906 else
907 SetLastError( RtlNtStatusToDosError(status) );
908 return handle;
912 /***********************************************************************
913 * OpenWaitableTimerA (KERNEL32.@)
915 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
917 WCHAR buffer[MAX_PATH];
919 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
921 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
923 SetLastError( ERROR_FILENAME_EXCED_RANGE );
924 return 0;
926 return OpenWaitableTimerW( access, inherit, buffer );
930 /***********************************************************************
931 * OpenWaitableTimerW (KERNEL32.@)
933 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
935 HANDLE handle;
936 UNICODE_STRING nameW;
937 OBJECT_ATTRIBUTES attr;
938 NTSTATUS status;
940 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
942 attr.Length = sizeof(attr);
943 attr.RootDirectory = 0;
944 attr.ObjectName = NULL;
945 attr.Attributes = OBJ_CASE_INSENSITIVE | (inherit ? OBJ_INHERIT : 0);
946 attr.SecurityDescriptor = NULL;
947 attr.SecurityQualityOfService = NULL;
948 if (name)
950 RtlInitUnicodeString( &nameW, name );
951 attr.ObjectName = &nameW;
952 attr.RootDirectory = get_BaseNamedObjects_handle();
955 status = NtOpenTimer(&handle, access, &attr);
956 if (status != STATUS_SUCCESS)
958 SetLastError( RtlNtStatusToDosError(status) );
959 return 0;
961 return handle;
965 /***********************************************************************
966 * SetWaitableTimer (KERNEL32.@)
968 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
969 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
971 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
972 arg, resume, period, NULL);
974 if (status != STATUS_SUCCESS)
976 SetLastError( RtlNtStatusToDosError(status) );
977 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
979 return TRUE;
983 /***********************************************************************
984 * CancelWaitableTimer (KERNEL32.@)
986 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
988 NTSTATUS status;
990 status = NtCancelTimer(handle, NULL);
991 if (status != STATUS_SUCCESS)
993 SetLastError( RtlNtStatusToDosError(status) );
994 return FALSE;
996 return TRUE;
1000 /***********************************************************************
1001 * CreateTimerQueue (KERNEL32.@)
1003 HANDLE WINAPI CreateTimerQueue(void)
1005 FIXME("stub\n");
1006 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1007 return NULL;
1011 /***********************************************************************
1012 * DeleteTimerQueueEx (KERNEL32.@)
1014 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1016 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
1017 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1018 return 0;
1021 /***********************************************************************
1022 * CreateTimerQueueTimer (KERNEL32.@)
1024 * Creates a timer-queue timer. This timer expires at the specified due
1025 * time (in ms), then after every specified period (in ms). When the timer
1026 * expires, the callback function is called.
1028 * RETURNS
1029 * nonzero on success or zero on faillure
1031 * BUGS
1032 * Unimplemented
1034 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1035 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1036 DWORD DueTime, DWORD Period, ULONG Flags )
1038 FIXME("stub\n");
1039 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1040 return TRUE;
1043 /***********************************************************************
1044 * DeleteTimerQueueTimer (KERNEL32.@)
1046 * Cancels a timer-queue timer.
1048 * RETURNS
1049 * nonzero on success or zero on faillure
1051 * BUGS
1052 * Unimplemented
1054 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1055 HANDLE CompletionEvent )
1057 FIXME("stub\n");
1058 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1059 return TRUE;
1064 * Pipes
1068 /***********************************************************************
1069 * CreateNamedPipeA (KERNEL32.@)
1071 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1072 DWORD dwPipeMode, DWORD nMaxInstances,
1073 DWORD nOutBufferSize, DWORD nInBufferSize,
1074 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1076 WCHAR buffer[MAX_PATH];
1078 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1079 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1081 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1083 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1084 return INVALID_HANDLE_VALUE;
1086 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1087 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1091 /***********************************************************************
1092 * CreateNamedPipeW (KERNEL32.@)
1094 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1095 DWORD dwPipeMode, DWORD nMaxInstances,
1096 DWORD nOutBufferSize, DWORD nInBufferSize,
1097 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1099 HANDLE handle;
1100 UNICODE_STRING nt_name;
1101 OBJECT_ATTRIBUTES attr;
1102 DWORD access, options;
1103 BOOLEAN pipe_type, read_mode, non_block;
1104 NTSTATUS status;
1105 IO_STATUS_BLOCK iosb;
1106 LARGE_INTEGER timeout;
1108 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1109 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1110 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1112 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1114 SetLastError( ERROR_PATH_NOT_FOUND );
1115 return INVALID_HANDLE_VALUE;
1117 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1119 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1120 RtlFreeUnicodeString( &nt_name );
1121 return INVALID_HANDLE_VALUE;
1124 attr.Length = sizeof(attr);
1125 attr.RootDirectory = 0;
1126 attr.ObjectName = &nt_name;
1127 attr.Attributes = OBJ_CASE_INSENSITIVE |
1128 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1129 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1130 attr.SecurityQualityOfService = NULL;
1132 switch(dwOpenMode & 3)
1134 case PIPE_ACCESS_INBOUND:
1135 options = FILE_PIPE_INBOUND;
1136 access = GENERIC_READ;
1137 break;
1138 case PIPE_ACCESS_OUTBOUND:
1139 options = FILE_PIPE_OUTBOUND;
1140 access = GENERIC_WRITE;
1141 break;
1142 case PIPE_ACCESS_DUPLEX:
1143 options = FILE_PIPE_FULL_DUPLEX;
1144 access = GENERIC_READ | GENERIC_WRITE;
1145 break;
1146 default:
1147 SetLastError( ERROR_INVALID_PARAMETER );
1148 return INVALID_HANDLE_VALUE;
1150 access |= SYNCHRONIZE;
1151 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1152 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1153 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1154 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1155 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1156 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0U;
1158 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1160 SetLastError(0);
1162 status = NtCreateNamedPipeFile(&handle, access, &attr, &iosb, 0,
1163 FILE_OVERWRITE_IF, options, pipe_type,
1164 read_mode, non_block, nMaxInstances,
1165 nInBufferSize, nOutBufferSize, &timeout);
1167 RtlFreeUnicodeString( &nt_name );
1168 if (status)
1170 handle = INVALID_HANDLE_VALUE;
1171 SetLastError( RtlNtStatusToDosError(status) );
1173 return handle;
1177 /***********************************************************************
1178 * PeekNamedPipe (KERNEL32.@)
1180 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1181 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1183 FILE_PIPE_PEEK_BUFFER local_buffer;
1184 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1185 IO_STATUS_BLOCK io;
1186 NTSTATUS status;
1188 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1189 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1191 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1192 return FALSE;
1195 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1196 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1197 if (!status)
1199 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1200 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1201 if (lpcbRead) *lpcbRead = read_size;
1202 if (lpcbMessage) *lpcbMessage = 0; /* FIXME */
1203 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1205 else SetLastError( RtlNtStatusToDosError(status) );
1207 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1208 return !status;
1211 /***********************************************************************
1212 * WaitNamedPipeA (KERNEL32.@)
1214 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1216 WCHAR buffer[MAX_PATH];
1218 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1220 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1222 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1223 return 0;
1225 return WaitNamedPipeW( buffer, nTimeOut );
1229 /***********************************************************************
1230 * WaitNamedPipeW (KERNEL32.@)
1232 * Waits for a named pipe instance to become available
1234 * PARAMS
1235 * name [I] Pointer to a named pipe name to wait for
1236 * nTimeOut [I] How long to wait in ms
1238 * RETURNS
1239 * TRUE: Success, named pipe can be opened with CreateFile
1240 * FALSE: Failure, GetLastError can be called for further details
1242 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1244 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1245 NTSTATUS status;
1246 UNICODE_STRING nt_name, pipe_dev_name;
1247 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1248 IO_STATUS_BLOCK iosb;
1249 OBJECT_ATTRIBUTES attr;
1250 ULONG sz_pipe_wait;
1251 HANDLE pipe_dev;
1253 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1255 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1256 return FALSE;
1258 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1259 nt_name.Length < sizeof(leadin) ||
1260 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR) != 0))
1262 RtlFreeUnicodeString( &nt_name );
1263 SetLastError( ERROR_PATH_NOT_FOUND );
1264 return FALSE;
1267 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1268 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1270 RtlFreeUnicodeString( &nt_name );
1271 SetLastError( ERROR_OUTOFMEMORY );
1272 return FALSE;
1275 pipe_dev_name.Buffer = nt_name.Buffer;
1276 pipe_dev_name.Length = sizeof(leadin);
1277 pipe_dev_name.MaximumLength = sizeof(leadin);
1278 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1279 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1280 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1281 FILE_SYNCHRONOUS_IO_NONALERT);
1282 if (status != ERROR_SUCCESS)
1284 SetLastError( ERROR_PATH_NOT_FOUND );
1285 return FALSE;
1288 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1289 if (nTimeOut == NMPWAIT_WAIT_FOREVER)
1290 pipe_wait->Timeout.QuadPart = ((ULONGLONG)0x7fffffff << 32) | 0xffffffff;
1291 else
1292 pipe_wait->Timeout.QuadPart = (ULONGLONG)nTimeOut * -10000;
1293 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1294 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1295 pipe_wait->NameLength);
1296 RtlFreeUnicodeString( &nt_name );
1298 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1299 pipe_wait, sz_pipe_wait, NULL, 0 );
1301 HeapFree( GetProcessHeap(), 0, pipe_wait );
1302 NtClose( pipe_dev );
1304 if(status != STATUS_SUCCESS)
1306 SetLastError(RtlNtStatusToDosError(status));
1307 return FALSE;
1309 else
1310 return TRUE;
1314 /***********************************************************************
1315 * ConnectNamedPipe (KERNEL32.@)
1317 * Connects to a named pipe
1319 * Parameters
1320 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1321 * overlapped: Optional OVERLAPPED struct
1323 * Return values
1324 * TRUE: Success
1325 * FALSE: Failure, GetLastError can be called for further details
1327 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1329 NTSTATUS status;
1330 IO_STATUS_BLOCK status_block;
1332 TRACE("(%p,%p)\n", hPipe, overlapped);
1334 if(overlapped)
1335 overlapped->Internal = STATUS_PENDING;
1337 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, NULL,
1338 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1339 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1341 if (status == STATUS_SUCCESS) return TRUE;
1342 SetLastError( RtlNtStatusToDosError(status) );
1343 return FALSE;
1346 /***********************************************************************
1347 * DisconnectNamedPipe (KERNEL32.@)
1349 * Disconnects from a named pipe
1351 * Parameters
1352 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1354 * Return values
1355 * TRUE: Success
1356 * FALSE: Failure, GetLastError can be called for further details
1358 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1360 NTSTATUS status;
1361 IO_STATUS_BLOCK io_block;
1363 TRACE("(%p)\n",hPipe);
1365 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1366 NULL, 0, NULL, 0);
1367 if (status == STATUS_SUCCESS) return TRUE;
1368 SetLastError( RtlNtStatusToDosError(status) );
1369 return FALSE;
1372 /***********************************************************************
1373 * TransactNamedPipe (KERNEL32.@)
1375 * BUGS
1376 * should be done as a single operation in the wineserver or kernel
1378 BOOL WINAPI TransactNamedPipe(
1379 HANDLE handle, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1380 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1382 BOOL r;
1383 DWORD count;
1385 TRACE("%p %p %d %p %d %p %p\n",
1386 handle, lpInput, dwInputSize, lpOutput,
1387 dwOutputSize, lpBytesRead, lpOverlapped);
1389 if (lpOverlapped)
1391 FIXME("Doesn't support overlapped operation as yet\n");
1392 return FALSE;
1395 r = WriteFile(handle, lpOutput, dwOutputSize, &count, NULL);
1396 if (r)
1397 r = ReadFile(handle, lpInput, dwInputSize, lpBytesRead, NULL);
1399 return r;
1402 /***********************************************************************
1403 * GetNamedPipeInfo (KERNEL32.@)
1405 BOOL WINAPI GetNamedPipeInfo(
1406 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1407 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1409 FILE_PIPE_LOCAL_INFORMATION fpli;
1410 IO_STATUS_BLOCK iosb;
1411 NTSTATUS status;
1413 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1414 FilePipeLocalInformation);
1415 if (status)
1417 SetLastError( RtlNtStatusToDosError(status) );
1418 return FALSE;
1421 if (lpFlags)
1423 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1424 PIPE_SERVER_END : PIPE_CLIENT_END;
1425 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1426 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1429 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1430 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1431 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1433 return TRUE;
1436 /***********************************************************************
1437 * GetNamedPipeHandleStateA (KERNEL32.@)
1439 BOOL WINAPI GetNamedPipeHandleStateA(
1440 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1441 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1442 LPSTR lpUsername, DWORD nUsernameMaxSize)
1444 FIXME("%p %p %p %p %p %p %d\n",
1445 hNamedPipe, lpState, lpCurInstances,
1446 lpMaxCollectionCount, lpCollectDataTimeout,
1447 lpUsername, nUsernameMaxSize);
1449 return FALSE;
1452 /***********************************************************************
1453 * GetNamedPipeHandleStateW (KERNEL32.@)
1455 BOOL WINAPI GetNamedPipeHandleStateW(
1456 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1457 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1458 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1460 FIXME("%p %p %p %p %p %p %d\n",
1461 hNamedPipe, lpState, lpCurInstances,
1462 lpMaxCollectionCount, lpCollectDataTimeout,
1463 lpUsername, nUsernameMaxSize);
1465 return FALSE;
1468 /***********************************************************************
1469 * SetNamedPipeHandleState (KERNEL32.@)
1471 BOOL WINAPI SetNamedPipeHandleState(
1472 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1473 LPDWORD lpCollectDataTimeout)
1475 /* should be a fixme, but this function is called a lot by the RPC
1476 * runtime, and it slows down InstallShield a fair bit. */
1477 WARN("stub: %p %p/%d %p %p\n",
1478 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1479 return FALSE;
1482 /***********************************************************************
1483 * CallNamedPipeA (KERNEL32.@)
1485 BOOL WINAPI CallNamedPipeA(
1486 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1487 LPVOID lpOutput, DWORD dwOutputSize,
1488 LPDWORD lpBytesRead, DWORD nTimeout)
1490 DWORD len;
1491 LPWSTR str = NULL;
1492 BOOL ret;
1494 TRACE("%s %p %d %p %d %p %d\n",
1495 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1496 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1498 if( lpNamedPipeName )
1500 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1501 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1502 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1504 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1505 dwOutputSize, lpBytesRead, nTimeout );
1506 if( lpNamedPipeName )
1507 HeapFree( GetProcessHeap(), 0, str );
1509 return ret;
1512 /***********************************************************************
1513 * CallNamedPipeW (KERNEL32.@)
1515 BOOL WINAPI CallNamedPipeW(
1516 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1517 LPVOID lpOutput, DWORD lpOutputSize,
1518 LPDWORD lpBytesRead, DWORD nTimeout)
1520 HANDLE pipe;
1521 BOOL ret;
1522 DWORD mode;
1524 TRACE("%s %p %d %p %d %p %d\n",
1525 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1526 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1528 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1529 if (pipe == INVALID_HANDLE_VALUE)
1531 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1532 if (!ret)
1533 return FALSE;
1534 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1535 if (pipe == INVALID_HANDLE_VALUE)
1536 return FALSE;
1539 mode = PIPE_READMODE_MESSAGE;
1540 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1541 if (!ret)
1543 CloseHandle(pipe);
1544 return FALSE;
1547 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1548 CloseHandle(pipe);
1549 if (!ret)
1550 return FALSE;
1552 return TRUE;
1555 /******************************************************************
1556 * CreatePipe (KERNEL32.@)
1559 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1560 LPSECURITY_ATTRIBUTES sa, DWORD size )
1562 static unsigned index /* = 0 */;
1563 WCHAR name[64];
1564 HANDLE hr, hw;
1565 unsigned in_index = index;
1566 UNICODE_STRING nt_name;
1567 OBJECT_ATTRIBUTES attr;
1568 NTSTATUS status;
1569 IO_STATUS_BLOCK iosb;
1570 LARGE_INTEGER timeout;
1572 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1574 attr.Length = sizeof(attr);
1575 attr.RootDirectory = 0;
1576 attr.ObjectName = &nt_name;
1577 attr.Attributes = OBJ_CASE_INSENSITIVE |
1578 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1579 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1580 attr.SecurityQualityOfService = NULL;
1582 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1583 /* generate a unique pipe name (system wide) */
1586 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1587 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1588 'u','.','%','0','8','u','\0' };
1590 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1591 GetCurrentProcessId(), ++index);
1592 RtlInitUnicodeString(&nt_name, name);
1593 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1594 0, FILE_OVERWRITE_IF,
1595 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1596 FALSE, FALSE, FALSE,
1597 1, size, size, &timeout);
1598 if (status)
1600 SetLastError( RtlNtStatusToDosError(status) );
1601 hr = INVALID_HANDLE_VALUE;
1603 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1604 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1605 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1607 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1608 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1610 if (status)
1612 SetLastError( RtlNtStatusToDosError(status) );
1613 NtClose(hr);
1614 return FALSE;
1617 *hReadPipe = hr;
1618 *hWritePipe = hw;
1619 return TRUE;
1623 /******************************************************************************
1624 * CreateMailslotA [KERNEL32.@]
1626 * See CreatMailslotW.
1628 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1629 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1631 DWORD len;
1632 HANDLE handle;
1633 LPWSTR name = NULL;
1635 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1636 nMaxMessageSize, lReadTimeout, sa);
1638 if( lpName )
1640 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1641 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1642 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1645 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1647 HeapFree( GetProcessHeap(), 0, name );
1649 return handle;
1653 /******************************************************************************
1654 * CreateMailslotW [KERNEL32.@]
1656 * Create a mailslot with specified name.
1658 * PARAMS
1659 * lpName [I] Pointer to string for mailslot name
1660 * nMaxMessageSize [I] Maximum message size
1661 * lReadTimeout [I] Milliseconds before read time-out
1662 * sa [I] Pointer to security structure
1664 * RETURNS
1665 * Success: Handle to mailslot
1666 * Failure: INVALID_HANDLE_VALUE
1668 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1669 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1671 HANDLE handle = INVALID_HANDLE_VALUE;
1672 OBJECT_ATTRIBUTES attr;
1673 UNICODE_STRING nameW;
1674 LARGE_INTEGER timeout;
1675 IO_STATUS_BLOCK iosb;
1676 NTSTATUS status;
1678 TRACE("%s %d %d %p\n", debugstr_w(lpName),
1679 nMaxMessageSize, lReadTimeout, sa);
1681 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1683 SetLastError( ERROR_PATH_NOT_FOUND );
1684 return INVALID_HANDLE_VALUE;
1687 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1689 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1690 RtlFreeUnicodeString( &nameW );
1691 return INVALID_HANDLE_VALUE;
1694 attr.Length = sizeof(attr);
1695 attr.RootDirectory = 0;
1696 attr.Attributes = OBJ_CASE_INSENSITIVE;
1697 attr.ObjectName = &nameW;
1698 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1699 attr.SecurityQualityOfService = NULL;
1701 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1702 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1703 else
1704 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1706 status = NtCreateMailslotFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr,
1707 &iosb, 0, 0, nMaxMessageSize, &timeout );
1708 if (status)
1710 SetLastError( RtlNtStatusToDosError(status) );
1711 handle = INVALID_HANDLE_VALUE;
1714 RtlFreeUnicodeString( &nameW );
1715 return handle;
1719 /******************************************************************************
1720 * GetMailslotInfo [KERNEL32.@]
1722 * Retrieve information about a mailslot.
1724 * PARAMS
1725 * hMailslot [I] Mailslot handle
1726 * lpMaxMessageSize [O] Address of maximum message size
1727 * lpNextSize [O] Address of size of next message
1728 * lpMessageCount [O] Address of number of messages
1729 * lpReadTimeout [O] Address of read time-out
1731 * RETURNS
1732 * Success: TRUE
1733 * Failure: FALSE
1735 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1736 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1737 LPDWORD lpReadTimeout )
1739 FILE_MAILSLOT_QUERY_INFORMATION info;
1740 IO_STATUS_BLOCK iosb;
1741 NTSTATUS status;
1743 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1744 lpNextSize, lpMessageCount, lpReadTimeout);
1746 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1747 FileMailslotQueryInformation );
1749 if( status != STATUS_SUCCESS )
1751 SetLastError( RtlNtStatusToDosError(status) );
1752 return FALSE;
1755 if( lpMaxMessageSize )
1756 *lpMaxMessageSize = info.MaximumMessageSize;
1757 if( lpNextSize )
1758 *lpNextSize = info.NextMessageSize;
1759 if( lpMessageCount )
1760 *lpMessageCount = info.MessagesAvailable;
1761 if( lpReadTimeout )
1763 if (info.ReadTimeout.QuadPart == (((LONGLONG)0x7fffffff << 32) | 0xffffffff))
1764 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
1765 else
1766 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1768 return TRUE;
1772 /******************************************************************************
1773 * SetMailslotInfo [KERNEL32.@]
1775 * Set the read timeout of a mailslot.
1777 * PARAMS
1778 * hMailslot [I] Mailslot handle
1779 * dwReadTimeout [I] Timeout in milliseconds.
1781 * RETURNS
1782 * Success: TRUE
1783 * Failure: FALSE
1785 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1787 FILE_MAILSLOT_SET_INFORMATION info;
1788 IO_STATUS_BLOCK iosb;
1789 NTSTATUS status;
1791 TRACE("%p %d\n", hMailslot, dwReadTimeout);
1793 if (dwReadTimeout != MAILSLOT_WAIT_FOREVER)
1794 info.ReadTimeout.QuadPart = (ULONGLONG)dwReadTimeout * -10000;
1795 else
1796 info.ReadTimeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1797 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1798 FileMailslotSetInformation );
1799 if( status != STATUS_SUCCESS )
1801 SetLastError( RtlNtStatusToDosError(status) );
1802 return FALSE;
1804 return TRUE;
1808 /******************************************************************************
1809 * CreateIoCompletionPort (KERNEL32.@)
1811 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1812 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1814 NTSTATUS status;
1815 HANDLE ret = 0;
1817 TRACE("(%p, %p, %08lx, %08x)\n",
1818 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1820 if (hExistingCompletionPort && hFileHandle == INVALID_HANDLE_VALUE)
1822 SetLastError( ERROR_INVALID_PARAMETER);
1823 return NULL;
1826 if (hExistingCompletionPort)
1827 ret = hExistingCompletionPort;
1828 else
1830 status = NtCreateIoCompletion( &ret, IO_COMPLETION_ALL_ACCESS, NULL, dwNumberOfConcurrentThreads );
1831 if (status != STATUS_SUCCESS) goto fail;
1834 if (hFileHandle != INVALID_HANDLE_VALUE)
1836 FILE_COMPLETION_INFORMATION info;
1837 IO_STATUS_BLOCK iosb;
1839 info.CompletionPort = ret;
1840 info.CompletionKey = CompletionKey;
1841 status = NtSetInformationFile( hFileHandle, &iosb, &info, sizeof(info), FileCompletionInformation );
1842 if (status != STATUS_SUCCESS) goto fail;
1845 return ret;
1847 fail:
1848 if (ret && !hExistingCompletionPort)
1849 CloseHandle( ret );
1850 SetLastError( RtlNtStatusToDosError(status) );
1851 return 0;
1854 /******************************************************************************
1855 * GetQueuedCompletionStatus (KERNEL32.@)
1857 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1858 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1859 DWORD dwMilliseconds )
1861 NTSTATUS status;
1862 IO_STATUS_BLOCK iosb;
1863 LARGE_INTEGER wait_time;
1865 TRACE("(%p,%p,%p,%p,%d)\n",
1866 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1868 *lpOverlapped = NULL;
1870 status = NtRemoveIoCompletion( CompletionPort, pCompletionKey, (PULONG_PTR)lpOverlapped,
1871 &iosb, get_nt_timeout( &wait_time, dwMilliseconds ) );
1872 if (status == STATUS_SUCCESS)
1874 *lpNumberOfBytesTransferred = iosb.Information;
1875 return TRUE;
1878 SetLastError( RtlNtStatusToDosError(status) );
1879 return FALSE;
1883 /******************************************************************************
1884 * PostQueuedCompletionStatus (KERNEL32.@)
1886 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
1887 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
1889 NTSTATUS status;
1891 TRACE("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
1893 status = NtSetIoCompletion( CompletionPort, dwCompletionKey, (ULONG_PTR)lpOverlapped,
1894 STATUS_SUCCESS, dwNumberOfBytes );
1896 if (status == STATUS_SUCCESS) return TRUE;
1897 SetLastError( RtlNtStatusToDosError(status) );
1898 return FALSE;
1901 /******************************************************************************
1902 * BindIoCompletionCallback (KERNEL32.@)
1904 BOOL WINAPI BindIoCompletionCallback( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags)
1906 FIXME("%p, %p, %d, stub!\n", FileHandle, Function, Flags);
1907 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1908 return FALSE;
1911 /******************************************************************************
1912 * CreateJobObjectW (KERNEL32.@)
1914 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1916 FIXME("%p %s\n", attr, debugstr_w(name) );
1917 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1918 return 0;
1921 /******************************************************************************
1922 * CreateJobObjectA (KERNEL32.@)
1924 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1926 LPWSTR str = NULL;
1927 UINT len;
1928 HANDLE r;
1930 TRACE("%p %s\n", attr, debugstr_a(name) );
1932 if( name )
1934 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1935 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1936 if( !str )
1938 SetLastError( ERROR_OUTOFMEMORY );
1939 return 0;
1941 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1944 r = CreateJobObjectW( attr, str );
1946 HeapFree( GetProcessHeap(), 0, str );
1948 return r;
1951 /******************************************************************************
1952 * AssignProcessToJobObject (KERNEL32.@)
1954 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1956 FIXME("%p %p\n", hJob, hProcess);
1957 return TRUE;
1960 #ifdef __i386__
1962 /***********************************************************************
1963 * InterlockedCompareExchange (KERNEL32.@)
1965 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1966 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1967 "movl 12(%esp),%eax\n\t"
1968 "movl 8(%esp),%ecx\n\t"
1969 "movl 4(%esp),%edx\n\t"
1970 "lock; cmpxchgl %ecx,(%edx)\n\t"
1971 "ret $12")
1973 /***********************************************************************
1974 * InterlockedExchange (KERNEL32.@)
1976 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1977 __ASM_GLOBAL_FUNC(InterlockedExchange,
1978 "movl 8(%esp),%eax\n\t"
1979 "movl 4(%esp),%edx\n\t"
1980 "lock; xchgl %eax,(%edx)\n\t"
1981 "ret $8")
1983 /***********************************************************************
1984 * InterlockedExchangeAdd (KERNEL32.@)
1986 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1987 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1988 "movl 8(%esp),%eax\n\t"
1989 "movl 4(%esp),%edx\n\t"
1990 "lock; xaddl %eax,(%edx)\n\t"
1991 "ret $8")
1993 /***********************************************************************
1994 * InterlockedIncrement (KERNEL32.@)
1996 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1997 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1998 "movl 4(%esp),%edx\n\t"
1999 "movl $1,%eax\n\t"
2000 "lock; xaddl %eax,(%edx)\n\t"
2001 "incl %eax\n\t"
2002 "ret $4")
2004 /***********************************************************************
2005 * InterlockedDecrement (KERNEL32.@)
2007 __ASM_GLOBAL_FUNC(InterlockedDecrement,
2008 "movl 4(%esp),%edx\n\t"
2009 "movl $-1,%eax\n\t"
2010 "lock; xaddl %eax,(%edx)\n\t"
2011 "decl %eax\n\t"
2012 "ret $4")
2014 #else /* __i386__ */
2016 /***********************************************************************
2017 * InterlockedCompareExchange (KERNEL32.@)
2019 * Atomically swap one value with another.
2021 * PARAMS
2022 * dest [I/O] The value to replace
2023 * xchq [I] The value to be swapped
2024 * compare [I] The value to compare to dest
2026 * RETURNS
2027 * The resulting value of dest.
2029 * NOTES
2030 * dest is updated only if it is equal to compare, otherwise no swap is done.
2032 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
2034 return interlocked_cmpxchg( (int *)dest, xchg, compare );
2037 /***********************************************************************
2038 * InterlockedExchange (KERNEL32.@)
2040 * Atomically swap one value with another.
2042 * PARAMS
2043 * dest [I/O] The value to replace
2044 * val [I] The value to be swapped
2046 * RETURNS
2047 * The resulting value of dest.
2049 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
2051 return interlocked_xchg( (int *)dest, val );
2054 /***********************************************************************
2055 * InterlockedExchangeAdd (KERNEL32.@)
2057 * Atomically add one value to another.
2059 * PARAMS
2060 * dest [I/O] The value to add to
2061 * incr [I] The value to be added
2063 * RETURNS
2064 * The resulting value of dest.
2066 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
2068 return interlocked_xchg_add( (int *)dest, incr );
2071 /***********************************************************************
2072 * InterlockedIncrement (KERNEL32.@)
2074 * Atomically increment a value.
2076 * PARAMS
2077 * dest [I/O] The value to increment
2079 * RETURNS
2080 * The resulting value of dest.
2082 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
2084 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
2087 /***********************************************************************
2088 * InterlockedDecrement (KERNEL32.@)
2090 * Atomically decrement a value.
2092 * PARAMS
2093 * dest [I/O] The value to decrement
2095 * RETURNS
2096 * The resulting value of dest.
2098 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
2100 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
2103 #endif /* __i386__ */