push 97f44e0adb27fff75ba63d8fb97c65db9edfbe82
[wine/hacks.git] / dlls / kernel32 / sync.c
blob08385f1a88a5efab2b1c72338acdfe0c32c7fba9
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;
1331 LPVOID cvalue = NULL;
1333 TRACE("(%p,%p)\n", hPipe, overlapped);
1335 if(overlapped)
1337 overlapped->Internal = STATUS_PENDING;
1338 overlapped->InternalHigh = 0;
1339 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1342 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, cvalue,
1343 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1344 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1346 if (status == STATUS_SUCCESS) return TRUE;
1347 SetLastError( RtlNtStatusToDosError(status) );
1348 return FALSE;
1351 /***********************************************************************
1352 * DisconnectNamedPipe (KERNEL32.@)
1354 * Disconnects from a named pipe
1356 * Parameters
1357 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1359 * Return values
1360 * TRUE: Success
1361 * FALSE: Failure, GetLastError can be called for further details
1363 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1365 NTSTATUS status;
1366 IO_STATUS_BLOCK io_block;
1368 TRACE("(%p)\n",hPipe);
1370 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1371 NULL, 0, NULL, 0);
1372 if (status == STATUS_SUCCESS) return TRUE;
1373 SetLastError( RtlNtStatusToDosError(status) );
1374 return FALSE;
1377 /***********************************************************************
1378 * TransactNamedPipe (KERNEL32.@)
1380 * BUGS
1381 * should be done as a single operation in the wineserver or kernel
1383 BOOL WINAPI TransactNamedPipe(
1384 HANDLE handle, LPVOID write_buf, DWORD write_size, LPVOID read_buf,
1385 DWORD read_size, LPDWORD bytes_read, LPOVERLAPPED overlapped)
1387 BOOL r;
1388 DWORD count;
1390 TRACE("%p %p %d %p %d %p %p\n",
1391 handle, write_buf, write_size, read_buf,
1392 read_size, bytes_read, overlapped);
1394 if (overlapped)
1396 FIXME("Doesn't support overlapped operation as yet\n");
1397 return FALSE;
1400 r = WriteFile(handle, write_buf, write_size, &count, NULL);
1401 if (r)
1402 r = ReadFile(handle, read_buf, read_size, bytes_read, NULL);
1404 return r;
1407 /***********************************************************************
1408 * GetNamedPipeInfo (KERNEL32.@)
1410 BOOL WINAPI GetNamedPipeInfo(
1411 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1412 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1414 FILE_PIPE_LOCAL_INFORMATION fpli;
1415 IO_STATUS_BLOCK iosb;
1416 NTSTATUS status;
1418 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1419 FilePipeLocalInformation);
1420 if (status)
1422 SetLastError( RtlNtStatusToDosError(status) );
1423 return FALSE;
1426 if (lpFlags)
1428 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1429 PIPE_SERVER_END : PIPE_CLIENT_END;
1430 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1431 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1434 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1435 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1436 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1438 return TRUE;
1441 /***********************************************************************
1442 * GetNamedPipeHandleStateA (KERNEL32.@)
1444 BOOL WINAPI GetNamedPipeHandleStateA(
1445 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1446 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1447 LPSTR lpUsername, DWORD nUsernameMaxSize)
1449 FIXME("%p %p %p %p %p %p %d\n",
1450 hNamedPipe, lpState, lpCurInstances,
1451 lpMaxCollectionCount, lpCollectDataTimeout,
1452 lpUsername, nUsernameMaxSize);
1454 return FALSE;
1457 /***********************************************************************
1458 * GetNamedPipeHandleStateW (KERNEL32.@)
1460 BOOL WINAPI GetNamedPipeHandleStateW(
1461 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1462 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1463 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1465 FIXME("%p %p %p %p %p %p %d\n",
1466 hNamedPipe, lpState, lpCurInstances,
1467 lpMaxCollectionCount, lpCollectDataTimeout,
1468 lpUsername, nUsernameMaxSize);
1470 return FALSE;
1473 /***********************************************************************
1474 * SetNamedPipeHandleState (KERNEL32.@)
1476 BOOL WINAPI SetNamedPipeHandleState(
1477 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1478 LPDWORD lpCollectDataTimeout)
1480 /* should be a fixme, but this function is called a lot by the RPC
1481 * runtime, and it slows down InstallShield a fair bit. */
1482 WARN("stub: %p %p/%d %p %p\n",
1483 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1484 return FALSE;
1487 /***********************************************************************
1488 * CallNamedPipeA (KERNEL32.@)
1490 BOOL WINAPI CallNamedPipeA(
1491 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1492 LPVOID lpOutput, DWORD dwOutputSize,
1493 LPDWORD lpBytesRead, DWORD nTimeout)
1495 DWORD len;
1496 LPWSTR str = NULL;
1497 BOOL ret;
1499 TRACE("%s %p %d %p %d %p %d\n",
1500 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1501 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1503 if( lpNamedPipeName )
1505 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1506 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1507 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1509 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1510 dwOutputSize, lpBytesRead, nTimeout );
1511 if( lpNamedPipeName )
1512 HeapFree( GetProcessHeap(), 0, str );
1514 return ret;
1517 /***********************************************************************
1518 * CallNamedPipeW (KERNEL32.@)
1520 BOOL WINAPI CallNamedPipeW(
1521 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1522 LPVOID lpOutput, DWORD lpOutputSize,
1523 LPDWORD lpBytesRead, DWORD nTimeout)
1525 HANDLE pipe;
1526 BOOL ret;
1527 DWORD mode;
1529 TRACE("%s %p %d %p %d %p %d\n",
1530 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1531 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1533 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1534 if (pipe == INVALID_HANDLE_VALUE)
1536 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1537 if (!ret)
1538 return FALSE;
1539 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1540 if (pipe == INVALID_HANDLE_VALUE)
1541 return FALSE;
1544 mode = PIPE_READMODE_MESSAGE;
1545 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1547 /* Currently SetNamedPipeHandleState() is a stub returning FALSE */
1548 if (ret) FIXME("Now that SetNamedPipeHandleState() is more than a stub, please update CallNamedPipeW\n");
1550 if (!ret)
1552 CloseHandle(pipe);
1553 return FALSE;
1556 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1557 CloseHandle(pipe);
1558 if (!ret)
1559 return FALSE;
1561 return TRUE;
1564 /******************************************************************
1565 * CreatePipe (KERNEL32.@)
1568 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1569 LPSECURITY_ATTRIBUTES sa, DWORD size )
1571 static unsigned index /* = 0 */;
1572 WCHAR name[64];
1573 HANDLE hr, hw;
1574 unsigned in_index = index;
1575 UNICODE_STRING nt_name;
1576 OBJECT_ATTRIBUTES attr;
1577 NTSTATUS status;
1578 IO_STATUS_BLOCK iosb;
1579 LARGE_INTEGER timeout;
1581 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1583 attr.Length = sizeof(attr);
1584 attr.RootDirectory = 0;
1585 attr.ObjectName = &nt_name;
1586 attr.Attributes = OBJ_CASE_INSENSITIVE |
1587 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1588 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1589 attr.SecurityQualityOfService = NULL;
1591 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1592 /* generate a unique pipe name (system wide) */
1595 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1596 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1597 'u','.','%','0','8','u','\0' };
1599 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1600 GetCurrentProcessId(), ++index);
1601 RtlInitUnicodeString(&nt_name, name);
1602 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1603 0, FILE_OVERWRITE_IF,
1604 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1605 FALSE, FALSE, FALSE,
1606 1, size, size, &timeout);
1607 if (status)
1609 SetLastError( RtlNtStatusToDosError(status) );
1610 hr = INVALID_HANDLE_VALUE;
1612 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1613 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1614 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1616 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1617 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1619 if (status)
1621 SetLastError( RtlNtStatusToDosError(status) );
1622 NtClose(hr);
1623 return FALSE;
1626 *hReadPipe = hr;
1627 *hWritePipe = hw;
1628 return TRUE;
1632 /******************************************************************************
1633 * CreateMailslotA [KERNEL32.@]
1635 * See CreatMailslotW.
1637 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1638 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1640 DWORD len;
1641 HANDLE handle;
1642 LPWSTR name = NULL;
1644 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1645 nMaxMessageSize, lReadTimeout, sa);
1647 if( lpName )
1649 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1650 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1651 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1654 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1656 HeapFree( GetProcessHeap(), 0, name );
1658 return handle;
1662 /******************************************************************************
1663 * CreateMailslotW [KERNEL32.@]
1665 * Create a mailslot with specified name.
1667 * PARAMS
1668 * lpName [I] Pointer to string for mailslot name
1669 * nMaxMessageSize [I] Maximum message size
1670 * lReadTimeout [I] Milliseconds before read time-out
1671 * sa [I] Pointer to security structure
1673 * RETURNS
1674 * Success: Handle to mailslot
1675 * Failure: INVALID_HANDLE_VALUE
1677 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1678 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1680 HANDLE handle = INVALID_HANDLE_VALUE;
1681 OBJECT_ATTRIBUTES attr;
1682 UNICODE_STRING nameW;
1683 LARGE_INTEGER timeout;
1684 IO_STATUS_BLOCK iosb;
1685 NTSTATUS status;
1687 TRACE("%s %d %d %p\n", debugstr_w(lpName),
1688 nMaxMessageSize, lReadTimeout, sa);
1690 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1692 SetLastError( ERROR_PATH_NOT_FOUND );
1693 return INVALID_HANDLE_VALUE;
1696 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1698 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1699 RtlFreeUnicodeString( &nameW );
1700 return INVALID_HANDLE_VALUE;
1703 attr.Length = sizeof(attr);
1704 attr.RootDirectory = 0;
1705 attr.Attributes = OBJ_CASE_INSENSITIVE;
1706 attr.ObjectName = &nameW;
1707 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1708 attr.SecurityQualityOfService = NULL;
1710 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1711 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1712 else
1713 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1715 status = NtCreateMailslotFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr,
1716 &iosb, 0, 0, nMaxMessageSize, &timeout );
1717 if (status)
1719 SetLastError( RtlNtStatusToDosError(status) );
1720 handle = INVALID_HANDLE_VALUE;
1723 RtlFreeUnicodeString( &nameW );
1724 return handle;
1728 /******************************************************************************
1729 * GetMailslotInfo [KERNEL32.@]
1731 * Retrieve information about a mailslot.
1733 * PARAMS
1734 * hMailslot [I] Mailslot handle
1735 * lpMaxMessageSize [O] Address of maximum message size
1736 * lpNextSize [O] Address of size of next message
1737 * lpMessageCount [O] Address of number of messages
1738 * lpReadTimeout [O] Address of read time-out
1740 * RETURNS
1741 * Success: TRUE
1742 * Failure: FALSE
1744 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1745 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1746 LPDWORD lpReadTimeout )
1748 FILE_MAILSLOT_QUERY_INFORMATION info;
1749 IO_STATUS_BLOCK iosb;
1750 NTSTATUS status;
1752 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1753 lpNextSize, lpMessageCount, lpReadTimeout);
1755 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1756 FileMailslotQueryInformation );
1758 if( status != STATUS_SUCCESS )
1760 SetLastError( RtlNtStatusToDosError(status) );
1761 return FALSE;
1764 if( lpMaxMessageSize )
1765 *lpMaxMessageSize = info.MaximumMessageSize;
1766 if( lpNextSize )
1767 *lpNextSize = info.NextMessageSize;
1768 if( lpMessageCount )
1769 *lpMessageCount = info.MessagesAvailable;
1770 if( lpReadTimeout )
1772 if (info.ReadTimeout.QuadPart == (((LONGLONG)0x7fffffff << 32) | 0xffffffff))
1773 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
1774 else
1775 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1777 return TRUE;
1781 /******************************************************************************
1782 * SetMailslotInfo [KERNEL32.@]
1784 * Set the read timeout of a mailslot.
1786 * PARAMS
1787 * hMailslot [I] Mailslot handle
1788 * dwReadTimeout [I] Timeout in milliseconds.
1790 * RETURNS
1791 * Success: TRUE
1792 * Failure: FALSE
1794 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1796 FILE_MAILSLOT_SET_INFORMATION info;
1797 IO_STATUS_BLOCK iosb;
1798 NTSTATUS status;
1800 TRACE("%p %d\n", hMailslot, dwReadTimeout);
1802 if (dwReadTimeout != MAILSLOT_WAIT_FOREVER)
1803 info.ReadTimeout.QuadPart = (ULONGLONG)dwReadTimeout * -10000;
1804 else
1805 info.ReadTimeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1806 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1807 FileMailslotSetInformation );
1808 if( status != STATUS_SUCCESS )
1810 SetLastError( RtlNtStatusToDosError(status) );
1811 return FALSE;
1813 return TRUE;
1817 /******************************************************************************
1818 * CreateIoCompletionPort (KERNEL32.@)
1820 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1821 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1823 NTSTATUS status;
1824 HANDLE ret = 0;
1826 TRACE("(%p, %p, %08lx, %08x)\n",
1827 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1829 if (hExistingCompletionPort && hFileHandle == INVALID_HANDLE_VALUE)
1831 SetLastError( ERROR_INVALID_PARAMETER);
1832 return NULL;
1835 if (hExistingCompletionPort)
1836 ret = hExistingCompletionPort;
1837 else
1839 status = NtCreateIoCompletion( &ret, IO_COMPLETION_ALL_ACCESS, NULL, dwNumberOfConcurrentThreads );
1840 if (status != STATUS_SUCCESS) goto fail;
1843 if (hFileHandle != INVALID_HANDLE_VALUE)
1845 FILE_COMPLETION_INFORMATION info;
1846 IO_STATUS_BLOCK iosb;
1848 info.CompletionPort = ret;
1849 info.CompletionKey = CompletionKey;
1850 status = NtSetInformationFile( hFileHandle, &iosb, &info, sizeof(info), FileCompletionInformation );
1851 if (status != STATUS_SUCCESS) goto fail;
1854 return ret;
1856 fail:
1857 if (ret && !hExistingCompletionPort)
1858 CloseHandle( ret );
1859 SetLastError( RtlNtStatusToDosError(status) );
1860 return 0;
1863 /******************************************************************************
1864 * GetQueuedCompletionStatus (KERNEL32.@)
1866 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1867 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1868 DWORD dwMilliseconds )
1870 NTSTATUS status;
1871 IO_STATUS_BLOCK iosb;
1872 LARGE_INTEGER wait_time;
1874 TRACE("(%p,%p,%p,%p,%d)\n",
1875 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1877 *lpOverlapped = NULL;
1879 status = NtRemoveIoCompletion( CompletionPort, pCompletionKey, (PULONG_PTR)lpOverlapped,
1880 &iosb, get_nt_timeout( &wait_time, dwMilliseconds ) );
1881 if (status == STATUS_SUCCESS)
1883 *lpNumberOfBytesTransferred = iosb.Information;
1884 return TRUE;
1887 SetLastError( RtlNtStatusToDosError(status) );
1888 return FALSE;
1892 /******************************************************************************
1893 * PostQueuedCompletionStatus (KERNEL32.@)
1895 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
1896 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
1898 NTSTATUS status;
1900 TRACE("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
1902 status = NtSetIoCompletion( CompletionPort, dwCompletionKey, (ULONG_PTR)lpOverlapped,
1903 STATUS_SUCCESS, dwNumberOfBytes );
1905 if (status == STATUS_SUCCESS) return TRUE;
1906 SetLastError( RtlNtStatusToDosError(status) );
1907 return FALSE;
1910 /******************************************************************************
1911 * BindIoCompletionCallback (KERNEL32.@)
1913 BOOL WINAPI BindIoCompletionCallback( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags)
1915 FIXME("%p, %p, %d, stub!\n", FileHandle, Function, Flags);
1916 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1917 return FALSE;
1920 /******************************************************************************
1921 * CreateJobObjectW (KERNEL32.@)
1923 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1925 FIXME("%p %s\n", attr, debugstr_w(name) );
1926 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1927 return 0;
1930 /******************************************************************************
1931 * CreateJobObjectA (KERNEL32.@)
1933 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1935 LPWSTR str = NULL;
1936 UINT len;
1937 HANDLE r;
1939 TRACE("%p %s\n", attr, debugstr_a(name) );
1941 if( name )
1943 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1944 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1945 if( !str )
1947 SetLastError( ERROR_OUTOFMEMORY );
1948 return 0;
1950 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1953 r = CreateJobObjectW( attr, str );
1955 HeapFree( GetProcessHeap(), 0, str );
1957 return r;
1960 /******************************************************************************
1961 * AssignProcessToJobObject (KERNEL32.@)
1963 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1965 FIXME("%p %p\n", hJob, hProcess);
1966 return TRUE;
1969 #ifdef __i386__
1971 /***********************************************************************
1972 * InterlockedCompareExchange (KERNEL32.@)
1974 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1975 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1976 "movl 12(%esp),%eax\n\t"
1977 "movl 8(%esp),%ecx\n\t"
1978 "movl 4(%esp),%edx\n\t"
1979 "lock; cmpxchgl %ecx,(%edx)\n\t"
1980 "ret $12")
1982 /***********************************************************************
1983 * InterlockedExchange (KERNEL32.@)
1985 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1986 __ASM_GLOBAL_FUNC(InterlockedExchange,
1987 "movl 8(%esp),%eax\n\t"
1988 "movl 4(%esp),%edx\n\t"
1989 "lock; xchgl %eax,(%edx)\n\t"
1990 "ret $8")
1992 /***********************************************************************
1993 * InterlockedExchangeAdd (KERNEL32.@)
1995 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1996 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1997 "movl 8(%esp),%eax\n\t"
1998 "movl 4(%esp),%edx\n\t"
1999 "lock; xaddl %eax,(%edx)\n\t"
2000 "ret $8")
2002 /***********************************************************************
2003 * InterlockedIncrement (KERNEL32.@)
2005 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2006 __ASM_GLOBAL_FUNC(InterlockedIncrement,
2007 "movl 4(%esp),%edx\n\t"
2008 "movl $1,%eax\n\t"
2009 "lock; xaddl %eax,(%edx)\n\t"
2010 "incl %eax\n\t"
2011 "ret $4")
2013 /***********************************************************************
2014 * InterlockedDecrement (KERNEL32.@)
2016 __ASM_GLOBAL_FUNC(InterlockedDecrement,
2017 "movl 4(%esp),%edx\n\t"
2018 "movl $-1,%eax\n\t"
2019 "lock; xaddl %eax,(%edx)\n\t"
2020 "decl %eax\n\t"
2021 "ret $4")
2023 #else /* __i386__ */
2025 /***********************************************************************
2026 * InterlockedCompareExchange (KERNEL32.@)
2028 * Atomically swap one value with another.
2030 * PARAMS
2031 * dest [I/O] The value to replace
2032 * xchq [I] The value to be swapped
2033 * compare [I] The value to compare to dest
2035 * RETURNS
2036 * The resulting value of dest.
2038 * NOTES
2039 * dest is updated only if it is equal to compare, otherwise no swap is done.
2041 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
2043 return interlocked_cmpxchg( (int *)dest, xchg, compare );
2046 /***********************************************************************
2047 * InterlockedExchange (KERNEL32.@)
2049 * Atomically swap one value with another.
2051 * PARAMS
2052 * dest [I/O] The value to replace
2053 * val [I] The value to be swapped
2055 * RETURNS
2056 * The resulting value of dest.
2058 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
2060 return interlocked_xchg( (int *)dest, val );
2063 /***********************************************************************
2064 * InterlockedExchangeAdd (KERNEL32.@)
2066 * Atomically add one value to another.
2068 * PARAMS
2069 * dest [I/O] The value to add to
2070 * incr [I] The value to be added
2072 * RETURNS
2073 * The resulting value of dest.
2075 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
2077 return interlocked_xchg_add( (int *)dest, incr );
2080 /***********************************************************************
2081 * InterlockedIncrement (KERNEL32.@)
2083 * Atomically increment a value.
2085 * PARAMS
2086 * dest [I/O] The value to increment
2088 * RETURNS
2089 * The resulting value of dest.
2091 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
2093 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
2096 /***********************************************************************
2097 * InterlockedDecrement (KERNEL32.@)
2099 * Atomically decrement a value.
2101 * PARAMS
2102 * dest [I/O] The value to decrement
2104 * RETURNS
2105 * The resulting value of dest.
2107 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
2109 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
2112 #endif /* __i386__ */