kernel32: Fix return values of GetConsoleCursorInfo.
[wine/multimedia.git] / dlls / kernel32 / sync.c
blobda558fd63d355fc6a94667190ffe1412fec7b18f
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 NTSTATUS status;
254 TRACE("%p %p %p %p %d %d\n",
255 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
257 status = RtlRegisterWait( phNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
258 if (status != STATUS_SUCCESS)
260 SetLastError( RtlNtStatusToDosError(status) );
261 return FALSE;
263 return TRUE;
266 /***********************************************************************
267 * RegisterWaitForSingleObjectEx (KERNEL32.@)
269 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
270 WAITORTIMERCALLBACK Callback, PVOID Context,
271 ULONG dwMilliseconds, ULONG dwFlags )
273 NTSTATUS status;
274 HANDLE hNewWaitObject;
276 TRACE("%p %p %p %d %d\n",
277 hObject,Callback,Context,dwMilliseconds,dwFlags);
279 status = RtlRegisterWait( &hNewWaitObject, hObject, Callback, Context, dwMilliseconds, dwFlags );
280 if (status != STATUS_SUCCESS)
282 SetLastError( RtlNtStatusToDosError(status) );
283 return NULL;
285 return hNewWaitObject;
288 /***********************************************************************
289 * UnregisterWait (KERNEL32.@)
291 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
293 NTSTATUS status;
295 TRACE("%p\n",WaitHandle);
297 status = RtlDeregisterWait( WaitHandle );
298 if (status != STATUS_SUCCESS)
300 SetLastError( RtlNtStatusToDosError(status) );
301 return FALSE;
303 return TRUE;
306 /***********************************************************************
307 * UnregisterWaitEx (KERNEL32.@)
309 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
311 FIXME("%p %p\n",WaitHandle, CompletionEvent);
312 return FALSE;
315 /***********************************************************************
316 * SignalObjectAndWait (KERNEL32.@)
318 * Allows to atomically signal any of the synchro objects (semaphore,
319 * mutex, event) and wait on another.
321 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
322 DWORD dwMilliseconds, BOOL bAlertable )
324 NTSTATUS status;
325 LARGE_INTEGER timeout;
327 TRACE("%p %p %d %d\n", hObjectToSignal,
328 hObjectToWaitOn, dwMilliseconds, bAlertable);
330 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn, bAlertable,
331 get_nt_timeout( &timeout, dwMilliseconds ) );
332 if (HIWORD(status))
334 SetLastError( RtlNtStatusToDosError(status) );
335 status = WAIT_FAILED;
337 return status;
340 /***********************************************************************
341 * InitializeCriticalSection (KERNEL32.@)
343 * Initialise a critical section before use.
345 * PARAMS
346 * crit [O] Critical section to initialise.
348 * RETURNS
349 * Nothing. If the function fails an exception is raised.
351 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
353 InitializeCriticalSectionEx( crit, 0, 0 );
356 /***********************************************************************
357 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
359 * Initialise a critical section with a spin count.
361 * PARAMS
362 * crit [O] Critical section to initialise.
363 * spincount [I] Number of times to spin upon contention.
365 * RETURNS
366 * Success: TRUE.
367 * Failure: Nothing. If the function fails an exception is raised.
369 * NOTES
370 * spincount is ignored on uni-processor systems.
372 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
374 return InitializeCriticalSectionEx( crit, spincount, 0 );
377 /***********************************************************************
378 * InitializeCriticalSectionEx (KERNEL32.@)
380 * Initialise a critical section with a spin count and flags.
382 * PARAMS
383 * crit [O] Critical section to initialise.
384 * spincount [I] Number of times to spin upon contention.
385 * flags [I] CRITICAL_SECTION_ flags from winbase.h.
387 * RETURNS
388 * Success: TRUE.
389 * Failure: Nothing. If the function fails an exception is raised.
391 * NOTES
392 * spincount is ignored on uni-processor systems.
394 BOOL WINAPI InitializeCriticalSectionEx( CRITICAL_SECTION *crit, DWORD spincount, DWORD flags )
396 NTSTATUS ret = RtlInitializeCriticalSectionEx( crit, spincount, flags );
397 if (ret) RtlRaiseStatus( ret );
398 return !ret;
401 /***********************************************************************
402 * MakeCriticalSectionGlobal (KERNEL32.@)
404 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
406 /* let's assume that only one thread at a time will try to do this */
407 HANDLE sem = crit->LockSemaphore;
408 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
409 crit->LockSemaphore = ConvertToGlobalHandle( sem );
410 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
411 crit->DebugInfo = NULL;
415 /***********************************************************************
416 * ReinitializeCriticalSection (KERNEL32.@)
418 * Initialise an already used critical section.
420 * PARAMS
421 * crit [O] Critical section to initialise.
423 * RETURNS
424 * Nothing.
426 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
428 if ( !crit->LockSemaphore )
429 RtlInitializeCriticalSection( crit );
433 /***********************************************************************
434 * UninitializeCriticalSection (KERNEL32.@)
436 * UnInitialise a critical section after use.
438 * PARAMS
439 * crit [O] Critical section to uninitialise (destroy).
441 * RETURNS
442 * Nothing.
444 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
446 RtlDeleteCriticalSection( crit );
450 /***********************************************************************
451 * CreateEventA (KERNEL32.@)
453 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
454 BOOL initial_state, LPCSTR name )
456 WCHAR buffer[MAX_PATH];
458 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
460 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
462 SetLastError( ERROR_FILENAME_EXCED_RANGE );
463 return 0;
465 return CreateEventW( sa, manual_reset, initial_state, buffer );
469 /***********************************************************************
470 * CreateEventW (KERNEL32.@)
472 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
473 BOOL initial_state, LPCWSTR name )
475 HANDLE ret;
476 UNICODE_STRING nameW;
477 OBJECT_ATTRIBUTES attr;
478 NTSTATUS status;
480 /* one buggy program needs this
481 * ("Van Dale Groot woordenboek der Nederlandse taal")
483 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
485 ERR("Bad security attributes pointer %p\n",sa);
486 SetLastError( ERROR_INVALID_PARAMETER);
487 return 0;
490 attr.Length = sizeof(attr);
491 attr.RootDirectory = 0;
492 attr.ObjectName = NULL;
493 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
494 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
495 attr.SecurityQualityOfService = NULL;
496 if (name)
498 RtlInitUnicodeString( &nameW, name );
499 attr.ObjectName = &nameW;
500 attr.RootDirectory = get_BaseNamedObjects_handle();
503 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
504 if (status == STATUS_OBJECT_NAME_EXISTS)
505 SetLastError( ERROR_ALREADY_EXISTS );
506 else
507 SetLastError( RtlNtStatusToDosError(status) );
508 return ret;
512 /***********************************************************************
513 * CreateW32Event (KERNEL.457)
515 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
517 return CreateEventW( NULL, manual_reset, initial_state, NULL );
521 /***********************************************************************
522 * OpenEventA (KERNEL32.@)
524 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
526 WCHAR buffer[MAX_PATH];
528 if (!name) return OpenEventW( access, inherit, NULL );
530 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
532 SetLastError( ERROR_FILENAME_EXCED_RANGE );
533 return 0;
535 return OpenEventW( access, inherit, buffer );
539 /***********************************************************************
540 * OpenEventW (KERNEL32.@)
542 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
544 HANDLE ret;
545 UNICODE_STRING nameW;
546 OBJECT_ATTRIBUTES attr;
547 NTSTATUS status;
549 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
551 attr.Length = sizeof(attr);
552 attr.RootDirectory = 0;
553 attr.ObjectName = NULL;
554 attr.Attributes = inherit ? OBJ_INHERIT : 0;
555 attr.SecurityDescriptor = NULL;
556 attr.SecurityQualityOfService = NULL;
557 if (name)
559 RtlInitUnicodeString( &nameW, name );
560 attr.ObjectName = &nameW;
561 attr.RootDirectory = get_BaseNamedObjects_handle();
564 status = NtOpenEvent( &ret, access, &attr );
565 if (status != STATUS_SUCCESS)
567 SetLastError( RtlNtStatusToDosError(status) );
568 return 0;
570 return ret;
573 /***********************************************************************
574 * PulseEvent (KERNEL32.@)
576 BOOL WINAPI PulseEvent( HANDLE handle )
578 NTSTATUS status;
580 if ((status = NtPulseEvent( handle, NULL )))
581 SetLastError( RtlNtStatusToDosError(status) );
582 return !status;
586 /***********************************************************************
587 * SetW32Event (KERNEL.458)
588 * SetEvent (KERNEL32.@)
590 BOOL WINAPI SetEvent( HANDLE handle )
592 NTSTATUS status;
594 if ((status = NtSetEvent( handle, NULL )))
595 SetLastError( RtlNtStatusToDosError(status) );
596 return !status;
600 /***********************************************************************
601 * ResetW32Event (KERNEL.459)
602 * ResetEvent (KERNEL32.@)
604 BOOL WINAPI ResetEvent( HANDLE handle )
606 NTSTATUS status;
608 if ((status = NtResetEvent( handle, NULL )))
609 SetLastError( RtlNtStatusToDosError(status) );
610 return !status;
614 /***********************************************************************
615 * NOTE: The Win95 VWin32_Event routines given below are really low-level
616 * routines implemented directly by VWin32. The user-mode libraries
617 * implement Win32 synchronisation routines on top of these low-level
618 * primitives. We do it the other way around here :-)
621 /***********************************************************************
622 * VWin32_EventCreate (KERNEL.442)
624 HANDLE WINAPI VWin32_EventCreate(VOID)
626 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
627 return ConvertToGlobalHandle( hEvent );
630 /***********************************************************************
631 * VWin32_EventDestroy (KERNEL.443)
633 VOID WINAPI VWin32_EventDestroy(HANDLE event)
635 CloseHandle( event );
638 /***********************************************************************
639 * VWin32_EventWait (KERNEL.450)
641 VOID WINAPI VWin32_EventWait(HANDLE event)
643 DWORD mutex_count;
645 ReleaseThunkLock( &mutex_count );
646 WaitForSingleObject( event, INFINITE );
647 RestoreThunkLock( mutex_count );
650 /***********************************************************************
651 * VWin32_EventSet (KERNEL.451)
652 * KERNEL_479 (KERNEL.479)
654 VOID WINAPI VWin32_EventSet(HANDLE event)
656 SetEvent( event );
661 /***********************************************************************
662 * CreateMutexA (KERNEL32.@)
664 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
666 WCHAR buffer[MAX_PATH];
668 if (!name) return CreateMutexW( sa, owner, NULL );
670 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
672 SetLastError( ERROR_FILENAME_EXCED_RANGE );
673 return 0;
675 return CreateMutexW( sa, owner, buffer );
679 /***********************************************************************
680 * CreateMutexW (KERNEL32.@)
682 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
684 HANDLE ret;
685 UNICODE_STRING nameW;
686 OBJECT_ATTRIBUTES attr;
687 NTSTATUS status;
689 attr.Length = sizeof(attr);
690 attr.RootDirectory = 0;
691 attr.ObjectName = NULL;
692 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
693 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
694 attr.SecurityQualityOfService = NULL;
695 if (name)
697 RtlInitUnicodeString( &nameW, name );
698 attr.ObjectName = &nameW;
699 attr.RootDirectory = get_BaseNamedObjects_handle();
702 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
703 if (status == STATUS_OBJECT_NAME_EXISTS)
704 SetLastError( ERROR_ALREADY_EXISTS );
705 else
706 SetLastError( RtlNtStatusToDosError(status) );
707 return ret;
711 /***********************************************************************
712 * OpenMutexA (KERNEL32.@)
714 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
716 WCHAR buffer[MAX_PATH];
718 if (!name) return OpenMutexW( access, inherit, NULL );
720 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
722 SetLastError( ERROR_FILENAME_EXCED_RANGE );
723 return 0;
725 return OpenMutexW( access, inherit, buffer );
729 /***********************************************************************
730 * OpenMutexW (KERNEL32.@)
732 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
734 HANDLE ret;
735 UNICODE_STRING nameW;
736 OBJECT_ATTRIBUTES attr;
737 NTSTATUS status;
739 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
741 attr.Length = sizeof(attr);
742 attr.RootDirectory = 0;
743 attr.ObjectName = NULL;
744 attr.Attributes = inherit ? OBJ_INHERIT : 0;
745 attr.SecurityDescriptor = NULL;
746 attr.SecurityQualityOfService = NULL;
747 if (name)
749 RtlInitUnicodeString( &nameW, name );
750 attr.ObjectName = &nameW;
751 attr.RootDirectory = get_BaseNamedObjects_handle();
754 status = NtOpenMutant( &ret, access, &attr );
755 if (status != STATUS_SUCCESS)
757 SetLastError( RtlNtStatusToDosError(status) );
758 return 0;
760 return ret;
764 /***********************************************************************
765 * ReleaseMutex (KERNEL32.@)
767 BOOL WINAPI ReleaseMutex( HANDLE handle )
769 NTSTATUS status;
771 status = NtReleaseMutant(handle, NULL);
772 if (status != STATUS_SUCCESS)
774 SetLastError( RtlNtStatusToDosError(status) );
775 return FALSE;
777 return TRUE;
782 * Semaphores
786 /***********************************************************************
787 * CreateSemaphoreA (KERNEL32.@)
789 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
791 WCHAR buffer[MAX_PATH];
793 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
795 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
797 SetLastError( ERROR_FILENAME_EXCED_RANGE );
798 return 0;
800 return CreateSemaphoreW( sa, initial, max, buffer );
804 /***********************************************************************
805 * CreateSemaphoreW (KERNEL32.@)
807 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
808 LONG max, LPCWSTR name )
810 HANDLE ret;
811 UNICODE_STRING nameW;
812 OBJECT_ATTRIBUTES attr;
813 NTSTATUS status;
815 attr.Length = sizeof(attr);
816 attr.RootDirectory = 0;
817 attr.ObjectName = NULL;
818 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
819 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
820 attr.SecurityQualityOfService = NULL;
821 if (name)
823 RtlInitUnicodeString( &nameW, name );
824 attr.ObjectName = &nameW;
825 attr.RootDirectory = get_BaseNamedObjects_handle();
828 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
829 if (status == STATUS_OBJECT_NAME_EXISTS)
830 SetLastError( ERROR_ALREADY_EXISTS );
831 else
832 SetLastError( RtlNtStatusToDosError(status) );
833 return ret;
837 /***********************************************************************
838 * OpenSemaphoreA (KERNEL32.@)
840 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
842 WCHAR buffer[MAX_PATH];
844 if (!name) return OpenSemaphoreW( access, inherit, NULL );
846 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
848 SetLastError( ERROR_FILENAME_EXCED_RANGE );
849 return 0;
851 return OpenSemaphoreW( access, inherit, buffer );
855 /***********************************************************************
856 * OpenSemaphoreW (KERNEL32.@)
858 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
860 HANDLE ret;
861 UNICODE_STRING nameW;
862 OBJECT_ATTRIBUTES attr;
863 NTSTATUS status;
865 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
867 attr.Length = sizeof(attr);
868 attr.RootDirectory = 0;
869 attr.ObjectName = NULL;
870 attr.Attributes = inherit ? OBJ_INHERIT : 0;
871 attr.SecurityDescriptor = NULL;
872 attr.SecurityQualityOfService = NULL;
873 if (name)
875 RtlInitUnicodeString( &nameW, name );
876 attr.ObjectName = &nameW;
877 attr.RootDirectory = get_BaseNamedObjects_handle();
880 status = NtOpenSemaphore( &ret, access, &attr );
881 if (status != STATUS_SUCCESS)
883 SetLastError( RtlNtStatusToDosError(status) );
884 return 0;
886 return ret;
890 /***********************************************************************
891 * ReleaseSemaphore (KERNEL32.@)
893 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
895 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
896 if (status) SetLastError( RtlNtStatusToDosError(status) );
897 return !status;
902 * Timers
906 /***********************************************************************
907 * CreateWaitableTimerA (KERNEL32.@)
909 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
911 WCHAR buffer[MAX_PATH];
913 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
915 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
917 SetLastError( ERROR_FILENAME_EXCED_RANGE );
918 return 0;
920 return CreateWaitableTimerW( sa, manual, buffer );
924 /***********************************************************************
925 * CreateWaitableTimerW (KERNEL32.@)
927 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
929 HANDLE handle;
930 NTSTATUS status;
931 UNICODE_STRING nameW;
932 OBJECT_ATTRIBUTES attr;
934 attr.Length = sizeof(attr);
935 attr.RootDirectory = 0;
936 attr.ObjectName = NULL;
937 attr.Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
938 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
939 attr.SecurityQualityOfService = NULL;
940 if (name)
942 RtlInitUnicodeString( &nameW, name );
943 attr.ObjectName = &nameW;
944 attr.RootDirectory = get_BaseNamedObjects_handle();
947 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
948 manual ? NotificationTimer : SynchronizationTimer);
949 if (status == STATUS_OBJECT_NAME_EXISTS)
950 SetLastError( ERROR_ALREADY_EXISTS );
951 else
952 SetLastError( RtlNtStatusToDosError(status) );
953 return handle;
957 /***********************************************************************
958 * OpenWaitableTimerA (KERNEL32.@)
960 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
962 WCHAR buffer[MAX_PATH];
964 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
966 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
968 SetLastError( ERROR_FILENAME_EXCED_RANGE );
969 return 0;
971 return OpenWaitableTimerW( access, inherit, buffer );
975 /***********************************************************************
976 * OpenWaitableTimerW (KERNEL32.@)
978 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
980 HANDLE handle;
981 UNICODE_STRING nameW;
982 OBJECT_ATTRIBUTES attr;
983 NTSTATUS status;
985 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
987 attr.Length = sizeof(attr);
988 attr.RootDirectory = 0;
989 attr.ObjectName = NULL;
990 attr.Attributes = inherit ? OBJ_INHERIT : 0;
991 attr.SecurityDescriptor = NULL;
992 attr.SecurityQualityOfService = NULL;
993 if (name)
995 RtlInitUnicodeString( &nameW, name );
996 attr.ObjectName = &nameW;
997 attr.RootDirectory = get_BaseNamedObjects_handle();
1000 status = NtOpenTimer(&handle, access, &attr);
1001 if (status != STATUS_SUCCESS)
1003 SetLastError( RtlNtStatusToDosError(status) );
1004 return 0;
1006 return handle;
1010 /***********************************************************************
1011 * SetWaitableTimer (KERNEL32.@)
1013 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
1014 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
1016 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
1017 arg, resume, period, NULL);
1019 if (status != STATUS_SUCCESS)
1021 SetLastError( RtlNtStatusToDosError(status) );
1022 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
1024 return TRUE;
1028 /***********************************************************************
1029 * CancelWaitableTimer (KERNEL32.@)
1031 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
1033 NTSTATUS status;
1035 status = NtCancelTimer(handle, NULL);
1036 if (status != STATUS_SUCCESS)
1038 SetLastError( RtlNtStatusToDosError(status) );
1039 return FALSE;
1041 return TRUE;
1045 /***********************************************************************
1046 * CreateTimerQueue (KERNEL32.@)
1048 HANDLE WINAPI CreateTimerQueue(void)
1050 FIXME("stub\n");
1051 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1052 return NULL;
1056 /***********************************************************************
1057 * DeleteTimerQueueEx (KERNEL32.@)
1059 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
1061 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
1062 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1063 return 0;
1066 /***********************************************************************
1067 * CreateTimerQueueTimer (KERNEL32.@)
1069 * Creates a timer-queue timer. This timer expires at the specified due
1070 * time (in ms), then after every specified period (in ms). When the timer
1071 * expires, the callback function is called.
1073 * RETURNS
1074 * nonzero on success or zero on failure
1076 * BUGS
1077 * Unimplemented
1079 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1080 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1081 DWORD DueTime, DWORD Period, ULONG Flags )
1083 FIXME("stub\n");
1084 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1085 return TRUE;
1088 /***********************************************************************
1089 * DeleteTimerQueueTimer (KERNEL32.@)
1091 * Cancels a timer-queue timer.
1093 * RETURNS
1094 * nonzero on success or zero on failure
1096 * BUGS
1097 * Unimplemented
1099 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1100 HANDLE CompletionEvent )
1102 FIXME("stub\n");
1103 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1104 return TRUE;
1109 * Pipes
1113 /***********************************************************************
1114 * CreateNamedPipeA (KERNEL32.@)
1116 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1117 DWORD dwPipeMode, DWORD nMaxInstances,
1118 DWORD nOutBufferSize, DWORD nInBufferSize,
1119 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1121 WCHAR buffer[MAX_PATH];
1123 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1124 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1126 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1128 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1129 return INVALID_HANDLE_VALUE;
1131 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1132 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1136 /***********************************************************************
1137 * CreateNamedPipeW (KERNEL32.@)
1139 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1140 DWORD dwPipeMode, DWORD nMaxInstances,
1141 DWORD nOutBufferSize, DWORD nInBufferSize,
1142 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1144 HANDLE handle;
1145 UNICODE_STRING nt_name;
1146 OBJECT_ATTRIBUTES attr;
1147 DWORD access, options;
1148 BOOLEAN pipe_type, read_mode, non_block;
1149 NTSTATUS status;
1150 IO_STATUS_BLOCK iosb;
1151 LARGE_INTEGER timeout;
1153 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1154 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1155 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1157 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1159 SetLastError( ERROR_PATH_NOT_FOUND );
1160 return INVALID_HANDLE_VALUE;
1162 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1164 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1165 RtlFreeUnicodeString( &nt_name );
1166 return INVALID_HANDLE_VALUE;
1169 attr.Length = sizeof(attr);
1170 attr.RootDirectory = 0;
1171 attr.ObjectName = &nt_name;
1172 attr.Attributes = OBJ_CASE_INSENSITIVE |
1173 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1174 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1175 attr.SecurityQualityOfService = NULL;
1177 switch(dwOpenMode & 3)
1179 case PIPE_ACCESS_INBOUND:
1180 options = FILE_PIPE_INBOUND;
1181 access = GENERIC_READ;
1182 break;
1183 case PIPE_ACCESS_OUTBOUND:
1184 options = FILE_PIPE_OUTBOUND;
1185 access = GENERIC_WRITE;
1186 break;
1187 case PIPE_ACCESS_DUPLEX:
1188 options = FILE_PIPE_FULL_DUPLEX;
1189 access = GENERIC_READ | GENERIC_WRITE;
1190 break;
1191 default:
1192 SetLastError( ERROR_INVALID_PARAMETER );
1193 return INVALID_HANDLE_VALUE;
1195 access |= SYNCHRONIZE;
1196 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1197 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1198 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1199 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1200 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1201 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0U;
1203 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1205 SetLastError(0);
1207 status = NtCreateNamedPipeFile(&handle, access, &attr, &iosb, 0,
1208 FILE_OVERWRITE_IF, options, pipe_type,
1209 read_mode, non_block, nMaxInstances,
1210 nInBufferSize, nOutBufferSize, &timeout);
1212 RtlFreeUnicodeString( &nt_name );
1213 if (status)
1215 handle = INVALID_HANDLE_VALUE;
1216 SetLastError( RtlNtStatusToDosError(status) );
1218 return handle;
1222 /***********************************************************************
1223 * PeekNamedPipe (KERNEL32.@)
1225 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1226 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1228 FILE_PIPE_PEEK_BUFFER local_buffer;
1229 FILE_PIPE_PEEK_BUFFER *buffer = &local_buffer;
1230 IO_STATUS_BLOCK io;
1231 NTSTATUS status;
1233 if (cbBuffer && !(buffer = HeapAlloc( GetProcessHeap(), 0,
1234 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ))))
1236 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
1237 return FALSE;
1240 status = NtFsControlFile( hPipe, 0, NULL, NULL, &io, FSCTL_PIPE_PEEK, NULL, 0,
1241 buffer, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data[cbBuffer] ) );
1242 if (!status)
1244 ULONG read_size = io.Information - FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER, Data );
1245 if (lpcbAvail) *lpcbAvail = buffer->ReadDataAvailable;
1246 if (lpcbRead) *lpcbRead = read_size;
1247 if (lpcbMessage) *lpcbMessage = 0; /* FIXME */
1248 if (lpvBuffer) memcpy( lpvBuffer, buffer->Data, read_size );
1250 else SetLastError( RtlNtStatusToDosError(status) );
1252 if (buffer != &local_buffer) HeapFree( GetProcessHeap(), 0, buffer );
1253 return !status;
1256 /***********************************************************************
1257 * WaitNamedPipeA (KERNEL32.@)
1259 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1261 WCHAR buffer[MAX_PATH];
1263 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1265 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1267 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1268 return 0;
1270 return WaitNamedPipeW( buffer, nTimeOut );
1274 /***********************************************************************
1275 * WaitNamedPipeW (KERNEL32.@)
1277 * Waits for a named pipe instance to become available
1279 * PARAMS
1280 * name [I] Pointer to a named pipe name to wait for
1281 * nTimeOut [I] How long to wait in ms
1283 * RETURNS
1284 * TRUE: Success, named pipe can be opened with CreateFile
1285 * FALSE: Failure, GetLastError can be called for further details
1287 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1289 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1290 NTSTATUS status;
1291 UNICODE_STRING nt_name, pipe_dev_name;
1292 FILE_PIPE_WAIT_FOR_BUFFER *pipe_wait;
1293 IO_STATUS_BLOCK iosb;
1294 OBJECT_ATTRIBUTES attr;
1295 ULONG sz_pipe_wait;
1296 HANDLE pipe_dev;
1298 TRACE("%s 0x%08x\n",debugstr_w(name),nTimeOut);
1300 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1301 return FALSE;
1303 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) ||
1304 nt_name.Length < sizeof(leadin) ||
1305 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(WCHAR) != 0))
1307 RtlFreeUnicodeString( &nt_name );
1308 SetLastError( ERROR_PATH_NOT_FOUND );
1309 return FALSE;
1312 sz_pipe_wait = sizeof(*pipe_wait) + nt_name.Length - sizeof(leadin) - sizeof(WCHAR);
1313 if (!(pipe_wait = HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait)))
1315 RtlFreeUnicodeString( &nt_name );
1316 SetLastError( ERROR_OUTOFMEMORY );
1317 return FALSE;
1320 pipe_dev_name.Buffer = nt_name.Buffer;
1321 pipe_dev_name.Length = sizeof(leadin);
1322 pipe_dev_name.MaximumLength = sizeof(leadin);
1323 InitializeObjectAttributes(&attr,&pipe_dev_name, OBJ_CASE_INSENSITIVE, NULL, NULL);
1324 status = NtOpenFile( &pipe_dev, FILE_READ_ATTRIBUTES, &attr,
1325 &iosb, FILE_SHARE_READ | FILE_SHARE_WRITE,
1326 FILE_SYNCHRONOUS_IO_NONALERT);
1327 if (status != ERROR_SUCCESS)
1329 SetLastError( ERROR_PATH_NOT_FOUND );
1330 return FALSE;
1333 pipe_wait->TimeoutSpecified = !(nTimeOut == NMPWAIT_USE_DEFAULT_WAIT);
1334 if (nTimeOut == NMPWAIT_WAIT_FOREVER)
1335 pipe_wait->Timeout.QuadPart = ((ULONGLONG)0x7fffffff << 32) | 0xffffffff;
1336 else
1337 pipe_wait->Timeout.QuadPart = (ULONGLONG)nTimeOut * -10000;
1338 pipe_wait->NameLength = nt_name.Length - sizeof(leadin);
1339 memcpy(pipe_wait->Name, nt_name.Buffer + sizeof(leadin)/sizeof(WCHAR),
1340 pipe_wait->NameLength);
1341 RtlFreeUnicodeString( &nt_name );
1343 status = NtFsControlFile( pipe_dev, NULL, NULL, NULL, &iosb, FSCTL_PIPE_WAIT,
1344 pipe_wait, sz_pipe_wait, NULL, 0 );
1346 HeapFree( GetProcessHeap(), 0, pipe_wait );
1347 NtClose( pipe_dev );
1349 if(status != STATUS_SUCCESS)
1351 SetLastError(RtlNtStatusToDosError(status));
1352 return FALSE;
1354 else
1355 return TRUE;
1359 /***********************************************************************
1360 * ConnectNamedPipe (KERNEL32.@)
1362 * Connects to a named pipe
1364 * Parameters
1365 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1366 * overlapped: Optional OVERLAPPED struct
1368 * Return values
1369 * TRUE: Success
1370 * FALSE: Failure, GetLastError can be called for further details
1372 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1374 NTSTATUS status;
1375 IO_STATUS_BLOCK status_block;
1376 LPVOID cvalue = NULL;
1378 TRACE("(%p,%p)\n", hPipe, overlapped);
1380 if(overlapped)
1382 overlapped->Internal = STATUS_PENDING;
1383 overlapped->InternalHigh = 0;
1384 if (((ULONG_PTR)overlapped->hEvent & 1) == 0) cvalue = overlapped;
1387 status = NtFsControlFile(hPipe, overlapped ? overlapped->hEvent : NULL, NULL, cvalue,
1388 overlapped ? (IO_STATUS_BLOCK *)overlapped : &status_block,
1389 FSCTL_PIPE_LISTEN, NULL, 0, NULL, 0);
1391 if (status == STATUS_SUCCESS) return TRUE;
1392 SetLastError( RtlNtStatusToDosError(status) );
1393 return FALSE;
1396 /***********************************************************************
1397 * DisconnectNamedPipe (KERNEL32.@)
1399 * Disconnects from a named pipe
1401 * Parameters
1402 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1404 * Return values
1405 * TRUE: Success
1406 * FALSE: Failure, GetLastError can be called for further details
1408 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1410 NTSTATUS status;
1411 IO_STATUS_BLOCK io_block;
1413 TRACE("(%p)\n",hPipe);
1415 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1416 NULL, 0, NULL, 0);
1417 if (status == STATUS_SUCCESS) return TRUE;
1418 SetLastError( RtlNtStatusToDosError(status) );
1419 return FALSE;
1422 /***********************************************************************
1423 * TransactNamedPipe (KERNEL32.@)
1425 * BUGS
1426 * should be done as a single operation in the wineserver or kernel
1428 BOOL WINAPI TransactNamedPipe(
1429 HANDLE handle, LPVOID write_buf, DWORD write_size, LPVOID read_buf,
1430 DWORD read_size, LPDWORD bytes_read, LPOVERLAPPED overlapped)
1432 BOOL r;
1433 DWORD count;
1435 TRACE("%p %p %d %p %d %p %p\n",
1436 handle, write_buf, write_size, read_buf,
1437 read_size, bytes_read, overlapped);
1439 if (overlapped)
1441 FIXME("Doesn't support overlapped operation as yet\n");
1442 return FALSE;
1445 r = WriteFile(handle, write_buf, write_size, &count, NULL);
1446 if (r)
1447 r = ReadFile(handle, read_buf, read_size, bytes_read, NULL);
1449 return r;
1452 /***********************************************************************
1453 * GetNamedPipeInfo (KERNEL32.@)
1455 BOOL WINAPI GetNamedPipeInfo(
1456 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1457 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1459 FILE_PIPE_LOCAL_INFORMATION fpli;
1460 IO_STATUS_BLOCK iosb;
1461 NTSTATUS status;
1463 status = NtQueryInformationFile(hNamedPipe, &iosb, &fpli, sizeof(fpli),
1464 FilePipeLocalInformation);
1465 if (status)
1467 SetLastError( RtlNtStatusToDosError(status) );
1468 return FALSE;
1471 if (lpFlags)
1473 *lpFlags = (fpli.NamedPipeEnd & FILE_PIPE_SERVER_END) ?
1474 PIPE_SERVER_END : PIPE_CLIENT_END;
1475 *lpFlags |= (fpli.NamedPipeType & FILE_PIPE_TYPE_MESSAGE) ?
1476 PIPE_TYPE_MESSAGE : PIPE_TYPE_BYTE;
1479 if (lpOutputBufferSize) *lpOutputBufferSize = fpli.OutboundQuota;
1480 if (lpInputBufferSize) *lpInputBufferSize = fpli.InboundQuota;
1481 if (lpMaxInstances) *lpMaxInstances = fpli.MaximumInstances;
1483 return TRUE;
1486 /***********************************************************************
1487 * GetNamedPipeHandleStateA (KERNEL32.@)
1489 BOOL WINAPI GetNamedPipeHandleStateA(
1490 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1491 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1492 LPSTR lpUsername, DWORD nUsernameMaxSize)
1494 FIXME("%p %p %p %p %p %p %d\n",
1495 hNamedPipe, lpState, lpCurInstances,
1496 lpMaxCollectionCount, lpCollectDataTimeout,
1497 lpUsername, nUsernameMaxSize);
1499 return FALSE;
1502 /***********************************************************************
1503 * GetNamedPipeHandleStateW (KERNEL32.@)
1505 BOOL WINAPI GetNamedPipeHandleStateW(
1506 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1507 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1508 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1510 FIXME("%p %p %p %p %p %p %d\n",
1511 hNamedPipe, lpState, lpCurInstances,
1512 lpMaxCollectionCount, lpCollectDataTimeout,
1513 lpUsername, nUsernameMaxSize);
1515 return FALSE;
1518 /***********************************************************************
1519 * SetNamedPipeHandleState (KERNEL32.@)
1521 BOOL WINAPI SetNamedPipeHandleState(
1522 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1523 LPDWORD lpCollectDataTimeout)
1525 /* should be a fixme, but this function is called a lot by the RPC
1526 * runtime, and it slows down InstallShield a fair bit. */
1527 WARN("stub: %p %p/%d %p %p\n",
1528 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1529 return FALSE;
1532 /***********************************************************************
1533 * CallNamedPipeA (KERNEL32.@)
1535 BOOL WINAPI CallNamedPipeA(
1536 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1537 LPVOID lpOutput, DWORD dwOutputSize,
1538 LPDWORD lpBytesRead, DWORD nTimeout)
1540 DWORD len;
1541 LPWSTR str = NULL;
1542 BOOL ret;
1544 TRACE("%s %p %d %p %d %p %d\n",
1545 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1546 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1548 if( lpNamedPipeName )
1550 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1551 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1552 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1554 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1555 dwOutputSize, lpBytesRead, nTimeout );
1556 if( lpNamedPipeName )
1557 HeapFree( GetProcessHeap(), 0, str );
1559 return ret;
1562 /***********************************************************************
1563 * CallNamedPipeW (KERNEL32.@)
1565 BOOL WINAPI CallNamedPipeW(
1566 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1567 LPVOID lpOutput, DWORD lpOutputSize,
1568 LPDWORD lpBytesRead, DWORD nTimeout)
1570 HANDLE pipe;
1571 BOOL ret;
1572 DWORD mode;
1574 TRACE("%s %p %d %p %d %p %d\n",
1575 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1576 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1578 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1579 if (pipe == INVALID_HANDLE_VALUE)
1581 ret = WaitNamedPipeW(lpNamedPipeName, nTimeout);
1582 if (!ret)
1583 return FALSE;
1584 pipe = CreateFileW(lpNamedPipeName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1585 if (pipe == INVALID_HANDLE_VALUE)
1586 return FALSE;
1589 mode = PIPE_READMODE_MESSAGE;
1590 ret = SetNamedPipeHandleState(pipe, &mode, NULL, NULL);
1592 /* Currently SetNamedPipeHandleState() is a stub returning FALSE */
1593 if (ret) FIXME("Now that SetNamedPipeHandleState() is more than a stub, please update CallNamedPipeW\n");
1595 if (!ret)
1597 CloseHandle(pipe);
1598 return FALSE;
1601 ret = TransactNamedPipe(pipe, lpInput, lpInputSize, lpOutput, lpOutputSize, lpBytesRead, NULL);
1602 CloseHandle(pipe);
1603 if (!ret)
1604 return FALSE;
1606 return TRUE;
1609 /******************************************************************
1610 * CreatePipe (KERNEL32.@)
1613 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1614 LPSECURITY_ATTRIBUTES sa, DWORD size )
1616 static unsigned index /* = 0 */;
1617 WCHAR name[64];
1618 HANDLE hr, hw;
1619 unsigned in_index = index;
1620 UNICODE_STRING nt_name;
1621 OBJECT_ATTRIBUTES attr;
1622 NTSTATUS status;
1623 IO_STATUS_BLOCK iosb;
1624 LARGE_INTEGER timeout;
1626 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1628 attr.Length = sizeof(attr);
1629 attr.RootDirectory = 0;
1630 attr.ObjectName = &nt_name;
1631 attr.Attributes = OBJ_CASE_INSENSITIVE |
1632 ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
1633 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1634 attr.SecurityQualityOfService = NULL;
1636 timeout.QuadPart = (ULONGLONG)NMPWAIT_USE_DEFAULT_WAIT * -10000;
1637 /* generate a unique pipe name (system wide) */
1640 static const WCHAR nameFmt[] = { '\\','?','?','\\','p','i','p','e',
1641 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1642 'u','.','%','0','8','u','\0' };
1644 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1645 GetCurrentProcessId(), ++index);
1646 RtlInitUnicodeString(&nt_name, name);
1647 status = NtCreateNamedPipeFile(&hr, GENERIC_READ | SYNCHRONIZE, &attr, &iosb,
1648 0, FILE_OVERWRITE_IF,
1649 FILE_SYNCHRONOUS_IO_ALERT | FILE_PIPE_INBOUND,
1650 FALSE, FALSE, FALSE,
1651 1, size, size, &timeout);
1652 if (status)
1654 SetLastError( RtlNtStatusToDosError(status) );
1655 hr = INVALID_HANDLE_VALUE;
1657 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1658 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1659 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1661 status = NtOpenFile(&hw, GENERIC_WRITE | SYNCHRONIZE, &attr, &iosb, 0,
1662 FILE_SYNCHRONOUS_IO_ALERT | FILE_NON_DIRECTORY_FILE);
1664 if (status)
1666 SetLastError( RtlNtStatusToDosError(status) );
1667 NtClose(hr);
1668 return FALSE;
1671 *hReadPipe = hr;
1672 *hWritePipe = hw;
1673 return TRUE;
1677 /******************************************************************************
1678 * CreateMailslotA [KERNEL32.@]
1680 * See CreateMailslotW.
1682 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1683 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1685 DWORD len;
1686 HANDLE handle;
1687 LPWSTR name = NULL;
1689 TRACE("%s %d %d %p\n", debugstr_a(lpName),
1690 nMaxMessageSize, lReadTimeout, sa);
1692 if( lpName )
1694 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1695 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1696 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1699 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1701 HeapFree( GetProcessHeap(), 0, name );
1703 return handle;
1707 /******************************************************************************
1708 * CreateMailslotW [KERNEL32.@]
1710 * Create a mailslot with specified name.
1712 * PARAMS
1713 * lpName [I] Pointer to string for mailslot name
1714 * nMaxMessageSize [I] Maximum message size
1715 * lReadTimeout [I] Milliseconds before read time-out
1716 * sa [I] Pointer to security structure
1718 * RETURNS
1719 * Success: Handle to mailslot
1720 * Failure: INVALID_HANDLE_VALUE
1722 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1723 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1725 HANDLE handle = INVALID_HANDLE_VALUE;
1726 OBJECT_ATTRIBUTES attr;
1727 UNICODE_STRING nameW;
1728 LARGE_INTEGER timeout;
1729 IO_STATUS_BLOCK iosb;
1730 NTSTATUS status;
1732 TRACE("%s %d %d %p\n", debugstr_w(lpName),
1733 nMaxMessageSize, lReadTimeout, sa);
1735 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1737 SetLastError( ERROR_PATH_NOT_FOUND );
1738 return INVALID_HANDLE_VALUE;
1741 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1743 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1744 RtlFreeUnicodeString( &nameW );
1745 return INVALID_HANDLE_VALUE;
1748 attr.Length = sizeof(attr);
1749 attr.RootDirectory = 0;
1750 attr.Attributes = OBJ_CASE_INSENSITIVE;
1751 attr.ObjectName = &nameW;
1752 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1753 attr.SecurityQualityOfService = NULL;
1755 if (lReadTimeout != MAILSLOT_WAIT_FOREVER)
1756 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1757 else
1758 timeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1760 status = NtCreateMailslotFile( &handle, GENERIC_READ | SYNCHRONIZE, &attr,
1761 &iosb, 0, 0, nMaxMessageSize, &timeout );
1762 if (status)
1764 SetLastError( RtlNtStatusToDosError(status) );
1765 handle = INVALID_HANDLE_VALUE;
1768 RtlFreeUnicodeString( &nameW );
1769 return handle;
1773 /******************************************************************************
1774 * GetMailslotInfo [KERNEL32.@]
1776 * Retrieve information about a mailslot.
1778 * PARAMS
1779 * hMailslot [I] Mailslot handle
1780 * lpMaxMessageSize [O] Address of maximum message size
1781 * lpNextSize [O] Address of size of next message
1782 * lpMessageCount [O] Address of number of messages
1783 * lpReadTimeout [O] Address of read time-out
1785 * RETURNS
1786 * Success: TRUE
1787 * Failure: FALSE
1789 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1790 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1791 LPDWORD lpReadTimeout )
1793 FILE_MAILSLOT_QUERY_INFORMATION info;
1794 IO_STATUS_BLOCK iosb;
1795 NTSTATUS status;
1797 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1798 lpNextSize, lpMessageCount, lpReadTimeout);
1800 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1801 FileMailslotQueryInformation );
1803 if( status != STATUS_SUCCESS )
1805 SetLastError( RtlNtStatusToDosError(status) );
1806 return FALSE;
1809 if( lpMaxMessageSize )
1810 *lpMaxMessageSize = info.MaximumMessageSize;
1811 if( lpNextSize )
1812 *lpNextSize = info.NextMessageSize;
1813 if( lpMessageCount )
1814 *lpMessageCount = info.MessagesAvailable;
1815 if( lpReadTimeout )
1817 if (info.ReadTimeout.QuadPart == (((LONGLONG)0x7fffffff << 32) | 0xffffffff))
1818 *lpReadTimeout = MAILSLOT_WAIT_FOREVER;
1819 else
1820 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1822 return TRUE;
1826 /******************************************************************************
1827 * SetMailslotInfo [KERNEL32.@]
1829 * Set the read timeout of a mailslot.
1831 * PARAMS
1832 * hMailslot [I] Mailslot handle
1833 * dwReadTimeout [I] Timeout in milliseconds.
1835 * RETURNS
1836 * Success: TRUE
1837 * Failure: FALSE
1839 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1841 FILE_MAILSLOT_SET_INFORMATION info;
1842 IO_STATUS_BLOCK iosb;
1843 NTSTATUS status;
1845 TRACE("%p %d\n", hMailslot, dwReadTimeout);
1847 if (dwReadTimeout != MAILSLOT_WAIT_FOREVER)
1848 info.ReadTimeout.QuadPart = (ULONGLONG)dwReadTimeout * -10000;
1849 else
1850 info.ReadTimeout.QuadPart = ((LONGLONG)0x7fffffff << 32) | 0xffffffff;
1851 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1852 FileMailslotSetInformation );
1853 if( status != STATUS_SUCCESS )
1855 SetLastError( RtlNtStatusToDosError(status) );
1856 return FALSE;
1858 return TRUE;
1862 /******************************************************************************
1863 * CreateIoCompletionPort (KERNEL32.@)
1865 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1866 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1868 NTSTATUS status;
1869 HANDLE ret = 0;
1871 TRACE("(%p, %p, %08lx, %08x)\n",
1872 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1874 if (hExistingCompletionPort && hFileHandle == INVALID_HANDLE_VALUE)
1876 SetLastError( ERROR_INVALID_PARAMETER);
1877 return NULL;
1880 if (hExistingCompletionPort)
1881 ret = hExistingCompletionPort;
1882 else
1884 status = NtCreateIoCompletion( &ret, IO_COMPLETION_ALL_ACCESS, NULL, dwNumberOfConcurrentThreads );
1885 if (status != STATUS_SUCCESS) goto fail;
1888 if (hFileHandle != INVALID_HANDLE_VALUE)
1890 FILE_COMPLETION_INFORMATION info;
1891 IO_STATUS_BLOCK iosb;
1893 info.CompletionPort = ret;
1894 info.CompletionKey = CompletionKey;
1895 status = NtSetInformationFile( hFileHandle, &iosb, &info, sizeof(info), FileCompletionInformation );
1896 if (status != STATUS_SUCCESS) goto fail;
1899 return ret;
1901 fail:
1902 if (ret && !hExistingCompletionPort)
1903 CloseHandle( ret );
1904 SetLastError( RtlNtStatusToDosError(status) );
1905 return 0;
1908 /******************************************************************************
1909 * GetQueuedCompletionStatus (KERNEL32.@)
1911 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1912 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1913 DWORD dwMilliseconds )
1915 NTSTATUS status;
1916 IO_STATUS_BLOCK iosb;
1917 LARGE_INTEGER wait_time;
1919 TRACE("(%p,%p,%p,%p,%d)\n",
1920 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1922 *lpOverlapped = NULL;
1924 status = NtRemoveIoCompletion( CompletionPort, pCompletionKey, (PULONG_PTR)lpOverlapped,
1925 &iosb, get_nt_timeout( &wait_time, dwMilliseconds ) );
1926 if (status == STATUS_SUCCESS)
1928 *lpNumberOfBytesTransferred = iosb.Information;
1929 return TRUE;
1932 SetLastError( RtlNtStatusToDosError(status) );
1933 return FALSE;
1937 /******************************************************************************
1938 * PostQueuedCompletionStatus (KERNEL32.@)
1940 BOOL WINAPI PostQueuedCompletionStatus( HANDLE CompletionPort, DWORD dwNumberOfBytes,
1941 ULONG_PTR dwCompletionKey, LPOVERLAPPED lpOverlapped)
1943 NTSTATUS status;
1945 TRACE("%p %d %08lx %p\n", CompletionPort, dwNumberOfBytes, dwCompletionKey, lpOverlapped );
1947 status = NtSetIoCompletion( CompletionPort, dwCompletionKey, (ULONG_PTR)lpOverlapped,
1948 STATUS_SUCCESS, dwNumberOfBytes );
1950 if (status == STATUS_SUCCESS) return TRUE;
1951 SetLastError( RtlNtStatusToDosError(status) );
1952 return FALSE;
1955 /******************************************************************************
1956 * BindIoCompletionCallback (KERNEL32.@)
1958 BOOL WINAPI BindIoCompletionCallback( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags)
1960 NTSTATUS status;
1962 TRACE("(%p, %p, %d)\n", FileHandle, Function, Flags);
1964 status = RtlSetIoCompletionCallback( FileHandle, (PRTL_OVERLAPPED_COMPLETION_ROUTINE)Function, Flags );
1965 if (status == STATUS_SUCCESS) return TRUE;
1966 SetLastError( RtlNtStatusToDosError(status) );
1967 return FALSE;
1970 /******************************************************************************
1971 * CreateJobObjectW (KERNEL32.@)
1973 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1975 FIXME("%p %s\n", attr, debugstr_w(name) );
1976 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1977 return 0;
1980 /******************************************************************************
1981 * CreateJobObjectA (KERNEL32.@)
1983 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1985 LPWSTR str = NULL;
1986 UINT len;
1987 HANDLE r;
1989 TRACE("%p %s\n", attr, debugstr_a(name) );
1991 if( name )
1993 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1994 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1995 if( !str )
1997 SetLastError( ERROR_OUTOFMEMORY );
1998 return 0;
2000 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
2003 r = CreateJobObjectW( attr, str );
2005 HeapFree( GetProcessHeap(), 0, str );
2007 return r;
2010 /******************************************************************************
2011 * AssignProcessToJobObject (KERNEL32.@)
2013 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
2015 FIXME("%p %p\n", hJob, hProcess);
2016 return TRUE;
2019 #ifdef __i386__
2021 /***********************************************************************
2022 * InterlockedCompareExchange (KERNEL32.@)
2024 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
2025 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
2026 "movl 12(%esp),%eax\n\t"
2027 "movl 8(%esp),%ecx\n\t"
2028 "movl 4(%esp),%edx\n\t"
2029 "lock; cmpxchgl %ecx,(%edx)\n\t"
2030 "ret $12")
2032 /***********************************************************************
2033 * InterlockedExchange (KERNEL32.@)
2035 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
2036 __ASM_GLOBAL_FUNC(InterlockedExchange,
2037 "movl 8(%esp),%eax\n\t"
2038 "movl 4(%esp),%edx\n\t"
2039 "lock; xchgl %eax,(%edx)\n\t"
2040 "ret $8")
2042 /***********************************************************************
2043 * InterlockedExchangeAdd (KERNEL32.@)
2045 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
2046 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
2047 "movl 8(%esp),%eax\n\t"
2048 "movl 4(%esp),%edx\n\t"
2049 "lock; xaddl %eax,(%edx)\n\t"
2050 "ret $8")
2052 /***********************************************************************
2053 * InterlockedIncrement (KERNEL32.@)
2055 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
2056 __ASM_GLOBAL_FUNC(InterlockedIncrement,
2057 "movl 4(%esp),%edx\n\t"
2058 "movl $1,%eax\n\t"
2059 "lock; xaddl %eax,(%edx)\n\t"
2060 "incl %eax\n\t"
2061 "ret $4")
2063 /***********************************************************************
2064 * InterlockedDecrement (KERNEL32.@)
2066 __ASM_GLOBAL_FUNC(InterlockedDecrement,
2067 "movl 4(%esp),%edx\n\t"
2068 "movl $-1,%eax\n\t"
2069 "lock; xaddl %eax,(%edx)\n\t"
2070 "decl %eax\n\t"
2071 "ret $4")
2073 #else /* __i386__ */
2075 /***********************************************************************
2076 * InterlockedCompareExchange (KERNEL32.@)
2078 * Atomically swap one value with another.
2080 * PARAMS
2081 * dest [I/O] The value to replace
2082 * xchq [I] The value to be swapped
2083 * compare [I] The value to compare to dest
2085 * RETURNS
2086 * The resulting value of dest.
2088 * NOTES
2089 * dest is updated only if it is equal to compare, otherwise no swap is done.
2091 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
2093 return interlocked_cmpxchg( (int *)dest, xchg, compare );
2096 /***********************************************************************
2097 * InterlockedExchange (KERNEL32.@)
2099 * Atomically swap one value with another.
2101 * PARAMS
2102 * dest [I/O] The value to replace
2103 * val [I] The value to be swapped
2105 * RETURNS
2106 * The resulting value of dest.
2108 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
2110 return interlocked_xchg( (int *)dest, val );
2113 /***********************************************************************
2114 * InterlockedExchangeAdd (KERNEL32.@)
2116 * Atomically add one value to another.
2118 * PARAMS
2119 * dest [I/O] The value to add to
2120 * incr [I] The value to be added
2122 * RETURNS
2123 * The resulting value of dest.
2125 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
2127 return interlocked_xchg_add( (int *)dest, incr );
2130 /***********************************************************************
2131 * InterlockedIncrement (KERNEL32.@)
2133 * Atomically increment a value.
2135 * PARAMS
2136 * dest [I/O] The value to increment
2138 * RETURNS
2139 * The resulting value of dest.
2141 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
2143 return interlocked_xchg_add( (int *)dest, 1 ) + 1;
2146 /***********************************************************************
2147 * InterlockedDecrement (KERNEL32.@)
2149 * Atomically decrement a value.
2151 * PARAMS
2152 * dest [I/O] The value to decrement
2154 * RETURNS
2155 * The resulting value of dest.
2157 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
2159 return interlocked_xchg_add( (int *)dest, -1 ) - 1;
2162 #endif /* __i386__ */