Return more sane mailslot error values.
[wine/multimedia.git] / dlls / kernel / sync.c
blob2e8158e8458da12202b3c45dc6f0bc5c3fb99376
1 /*
2 * Kernel synchronization objects
4 * Copyright 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <errno.h>
29 #ifdef HAVE_SYS_IOCTL_H
30 #include <sys/ioctl.h>
31 #endif
32 #ifdef HAVE_SYS_POLL_H
33 #include <sys/poll.h>
34 #endif
35 #include <stdarg.h>
36 #include <stdio.h>
38 #include "ntstatus.h"
39 #include "windef.h"
40 #include "winbase.h"
41 #include "winerror.h"
42 #include "winnls.h"
44 #include "wine/server.h"
45 #include "wine/unicode.h"
46 #include "kernel_private.h"
47 #include "file.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(win32);
53 /* check if current version is NT or Win95 */
54 inline static int is_version_nt(void)
56 return !(GetVersion() & 0x80000000);
60 /***********************************************************************
61 * Sleep (KERNEL32.@)
63 VOID WINAPI Sleep( DWORD timeout )
65 SleepEx( timeout, FALSE );
68 /******************************************************************************
69 * SleepEx (KERNEL32.@)
71 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
73 NTSTATUS status;
75 if (timeout == INFINITE) status = NtDelayExecution( alertable, NULL );
76 else
78 LARGE_INTEGER time;
80 time.QuadPart = timeout * (ULONGLONG)10000;
81 time.QuadPart = -time.QuadPart;
82 status = NtDelayExecution( alertable, &time );
84 if (status != STATUS_USER_APC) status = STATUS_SUCCESS;
85 return status;
89 /***********************************************************************
90 * WaitForSingleObject (KERNEL32.@)
92 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
94 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
98 /***********************************************************************
99 * WaitForSingleObjectEx (KERNEL32.@)
101 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
102 BOOL alertable )
104 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
108 /***********************************************************************
109 * WaitForMultipleObjects (KERNEL32.@)
111 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
112 BOOL wait_all, DWORD timeout )
114 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
118 /***********************************************************************
119 * WaitForMultipleObjectsEx (KERNEL32.@)
121 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
122 BOOL wait_all, DWORD timeout,
123 BOOL alertable )
125 NTSTATUS status;
126 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
127 int i;
129 if (count >= MAXIMUM_WAIT_OBJECTS)
131 SetLastError(ERROR_INVALID_PARAMETER);
132 return WAIT_FAILED;
134 for (i = 0; i < count; i++)
136 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
137 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
138 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
139 hloc[i] = GetStdHandle( (DWORD)handles[i] );
140 else
141 hloc[i] = handles[i];
143 /* yes, even screen buffer console handles are waitable, and are
144 * handled as a handle to the console itself !!
146 if (is_console_handle(hloc[i]))
148 if (!VerifyConsoleIoHandle(hloc[i]))
150 return FALSE;
152 hloc[i] = GetConsoleInputWaitHandle();
156 if (timeout == INFINITE)
158 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, NULL );
160 else
162 LARGE_INTEGER time;
164 time.QuadPart = timeout * (ULONGLONG)10000;
165 time.QuadPart = -time.QuadPart;
166 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, &time );
169 if (HIWORD(status)) /* is it an error code? */
171 SetLastError( RtlNtStatusToDosError(status) );
172 status = WAIT_FAILED;
174 return status;
178 /***********************************************************************
179 * WaitForSingleObject (KERNEL.460)
181 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
183 DWORD retval, mutex_count;
185 ReleaseThunkLock( &mutex_count );
186 retval = WaitForSingleObject( handle, timeout );
187 RestoreThunkLock( mutex_count );
188 return retval;
191 /***********************************************************************
192 * WaitForMultipleObjects (KERNEL.461)
194 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
195 BOOL wait_all, DWORD timeout )
197 DWORD retval, mutex_count;
199 ReleaseThunkLock( &mutex_count );
200 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
201 RestoreThunkLock( mutex_count );
202 return retval;
205 /***********************************************************************
206 * WaitForMultipleObjectsEx (KERNEL.495)
208 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
209 BOOL wait_all, DWORD timeout, BOOL alertable )
211 DWORD retval, mutex_count;
213 ReleaseThunkLock( &mutex_count );
214 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
215 RestoreThunkLock( mutex_count );
216 return retval;
219 /***********************************************************************
220 * RegisterWaitForSingleObject (KERNEL32.@)
222 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
223 WAITORTIMERCALLBACK Callback, PVOID Context,
224 ULONG dwMilliseconds, ULONG dwFlags)
226 FIXME("%p %p %p %p %ld %ld\n",
227 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
228 return FALSE;
231 /***********************************************************************
232 * RegisterWaitForSingleObjectEx (KERNEL32.@)
234 BOOL WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
235 WAITORTIMERCALLBACK Callback, PVOID Context,
236 ULONG dwMilliseconds, ULONG dwFlags )
238 FIXME("%p %p %p %ld %ld\n",
239 hObject,Callback,Context,dwMilliseconds,dwFlags);
240 return FALSE;
243 /***********************************************************************
244 * UnregisterWait (KERNEL32.@)
246 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
248 FIXME("%p\n",WaitHandle);
249 return FALSE;
252 /***********************************************************************
253 * UnregisterWaitEx (KERNEL32.@)
255 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
257 FIXME("%p %p\n",WaitHandle, CompletionEvent);
258 return FALSE;
261 /***********************************************************************
262 * InitializeCriticalSection (KERNEL32.@)
264 * Initialise a critical section before use.
266 * PARAMS
267 * crit [O] Critical section to initialise.
269 * RETURNS
270 * Nothing. If the function fails an exception is raised.
272 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
274 NTSTATUS ret = RtlInitializeCriticalSection( crit );
275 if (ret) RtlRaiseStatus( ret );
278 /***********************************************************************
279 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
281 * Initialise a critical section with a spin count.
283 * PARAMS
284 * crit [O] Critical section to initialise.
285 * spincount [I] Number of times to spin upon contention.
287 * RETURNS
288 * Success: TRUE.
289 * Failure: Nothing. If the function fails an exception is raised.
291 * NOTES
292 * spincount is ignored on uni-processor systems.
294 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
296 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
297 if (ret) RtlRaiseStatus( ret );
298 return !ret;
301 /***********************************************************************
302 * SetCriticalSectionSpinCount (KERNEL32.@)
304 * Set the spin count for a critical section.
306 * PARAMS
307 * crit [O] Critical section to set the spin count for.
308 * spincount [I] Number of times to spin upon contention.
310 * RETURNS
311 * The previous spin count value of crit.
313 * NOTES
314 * This function is available on NT4SP3 or later, but not Win98.
316 DWORD WINAPI SetCriticalSectionSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
318 ULONG_PTR oldspincount = crit->SpinCount;
319 if(spincount) FIXME("critsection=%p: spincount=%ld not supported\n", crit, spincount);
320 crit->SpinCount = spincount;
321 return oldspincount;
324 /***********************************************************************
325 * MakeCriticalSectionGlobal (KERNEL32.@)
327 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
329 /* let's assume that only one thread at a time will try to do this */
330 HANDLE sem = crit->LockSemaphore;
331 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
332 crit->LockSemaphore = ConvertToGlobalHandle( sem );
333 if (crit->DebugInfo)
335 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
336 crit->DebugInfo = NULL;
341 /***********************************************************************
342 * ReinitializeCriticalSection (KERNEL32.@)
344 * Initialise an already used critical section.
346 * PARAMS
347 * crit [O] Critical section to initialise.
349 * RETURNS
350 * Nothing.
352 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
354 if ( !crit->LockSemaphore )
355 RtlInitializeCriticalSection( crit );
359 /***********************************************************************
360 * UninitializeCriticalSection (KERNEL32.@)
362 * UnInitialise a critical section after use.
364 * PARAMS
365 * crit [O] Critical section to uninitialise (destroy).
367 * RETURNS
368 * Nothing.
370 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
372 RtlDeleteCriticalSection( crit );
376 /***********************************************************************
377 * CreateEventA (KERNEL32.@)
379 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
380 BOOL initial_state, LPCSTR name )
382 WCHAR buffer[MAX_PATH];
384 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
386 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
388 SetLastError( ERROR_FILENAME_EXCED_RANGE );
389 return 0;
391 return CreateEventW( sa, manual_reset, initial_state, buffer );
395 /***********************************************************************
396 * CreateEventW (KERNEL32.@)
398 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
399 BOOL initial_state, LPCWSTR name )
401 HANDLE ret;
402 DWORD len = name ? strlenW(name) : 0;
403 if (len >= MAX_PATH)
405 SetLastError( ERROR_FILENAME_EXCED_RANGE );
406 return 0;
408 /* one buggy program needs this
409 * ("Van Dale Groot woordenboek der Nederlandse taal")
411 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
413 ERR("Bad security attributes pointer %p\n",sa);
414 SetLastError( ERROR_INVALID_PARAMETER);
415 return 0;
417 SERVER_START_REQ( create_event )
419 req->manual_reset = manual_reset;
420 req->initial_state = initial_state;
421 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
422 wine_server_add_data( req, name, len * sizeof(WCHAR) );
423 SetLastError(0);
424 wine_server_call_err( req );
425 ret = reply->handle;
427 SERVER_END_REQ;
428 return ret;
432 /***********************************************************************
433 * CreateW32Event (KERNEL.457)
435 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
437 return CreateEventA( NULL, manual_reset, initial_state, NULL );
441 /***********************************************************************
442 * OpenEventA (KERNEL32.@)
444 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
446 WCHAR buffer[MAX_PATH];
448 if (!name) return OpenEventW( access, inherit, NULL );
450 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
452 SetLastError( ERROR_FILENAME_EXCED_RANGE );
453 return 0;
455 return OpenEventW( access, inherit, buffer );
459 /***********************************************************************
460 * OpenEventW (KERNEL32.@)
462 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
464 HANDLE ret;
465 DWORD len = name ? strlenW(name) : 0;
466 if (len >= MAX_PATH)
468 SetLastError( ERROR_FILENAME_EXCED_RANGE );
469 return 0;
471 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
473 SERVER_START_REQ( open_event )
475 req->access = access;
476 req->inherit = inherit;
477 wine_server_add_data( req, name, len * sizeof(WCHAR) );
478 wine_server_call_err( req );
479 ret = reply->handle;
481 SERVER_END_REQ;
482 return ret;
486 /***********************************************************************
487 * EVENT_Operation
489 * Execute an event operation (set,reset,pulse).
491 static BOOL EVENT_Operation( HANDLE handle, enum event_op op )
493 BOOL ret;
494 SERVER_START_REQ( event_op )
496 req->handle = handle;
497 req->op = op;
498 ret = !wine_server_call_err( req );
500 SERVER_END_REQ;
501 return ret;
505 /***********************************************************************
506 * PulseEvent (KERNEL32.@)
508 BOOL WINAPI PulseEvent( HANDLE handle )
510 return EVENT_Operation( handle, PULSE_EVENT );
514 /***********************************************************************
515 * SetW32Event (KERNEL.458)
516 * SetEvent (KERNEL32.@)
518 BOOL WINAPI SetEvent( HANDLE handle )
520 return EVENT_Operation( handle, SET_EVENT );
524 /***********************************************************************
525 * ResetW32Event (KERNEL.459)
526 * ResetEvent (KERNEL32.@)
528 BOOL WINAPI ResetEvent( HANDLE handle )
530 return EVENT_Operation( handle, RESET_EVENT );
534 /***********************************************************************
535 * NOTE: The Win95 VWin32_Event routines given below are really low-level
536 * routines implemented directly by VWin32. The user-mode libraries
537 * implement Win32 synchronisation routines on top of these low-level
538 * primitives. We do it the other way around here :-)
541 /***********************************************************************
542 * VWin32_EventCreate (KERNEL.442)
544 HANDLE WINAPI VWin32_EventCreate(VOID)
546 HANDLE hEvent = CreateEventA( NULL, FALSE, 0, NULL );
547 return ConvertToGlobalHandle( hEvent );
550 /***********************************************************************
551 * VWin32_EventDestroy (KERNEL.443)
553 VOID WINAPI VWin32_EventDestroy(HANDLE event)
555 CloseHandle( event );
558 /***********************************************************************
559 * VWin32_EventWait (KERNEL.450)
561 VOID WINAPI VWin32_EventWait(HANDLE event)
563 DWORD mutex_count;
565 ReleaseThunkLock( &mutex_count );
566 WaitForSingleObject( event, INFINITE );
567 RestoreThunkLock( mutex_count );
570 /***********************************************************************
571 * VWin32_EventSet (KERNEL.451)
572 * KERNEL_479 (KERNEL.479)
574 VOID WINAPI VWin32_EventSet(HANDLE event)
576 SetEvent( event );
581 /***********************************************************************
582 * CreateMutexA (KERNEL32.@)
584 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
586 WCHAR buffer[MAX_PATH];
588 if (!name) return CreateMutexW( sa, owner, NULL );
590 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
592 SetLastError( ERROR_FILENAME_EXCED_RANGE );
593 return 0;
595 return CreateMutexW( sa, owner, buffer );
599 /***********************************************************************
600 * CreateMutexW (KERNEL32.@)
602 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
604 HANDLE ret;
605 DWORD len = name ? strlenW(name) : 0;
606 if (len >= MAX_PATH)
608 SetLastError( ERROR_FILENAME_EXCED_RANGE );
609 return 0;
611 SERVER_START_REQ( create_mutex )
613 req->owned = owner;
614 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
615 wine_server_add_data( req, name, len * sizeof(WCHAR) );
616 SetLastError(0);
617 wine_server_call_err( req );
618 ret = reply->handle;
620 SERVER_END_REQ;
621 return ret;
625 /***********************************************************************
626 * OpenMutexA (KERNEL32.@)
628 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
630 WCHAR buffer[MAX_PATH];
632 if (!name) return OpenMutexW( access, inherit, NULL );
634 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
636 SetLastError( ERROR_FILENAME_EXCED_RANGE );
637 return 0;
639 return OpenMutexW( access, inherit, buffer );
643 /***********************************************************************
644 * OpenMutexW (KERNEL32.@)
646 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
648 HANDLE ret;
649 DWORD len = name ? strlenW(name) : 0;
650 if (len >= MAX_PATH)
652 SetLastError( ERROR_FILENAME_EXCED_RANGE );
653 return 0;
655 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
657 SERVER_START_REQ( open_mutex )
659 req->access = access;
660 req->inherit = inherit;
661 wine_server_add_data( req, name, len * sizeof(WCHAR) );
662 wine_server_call_err( req );
663 ret = reply->handle;
665 SERVER_END_REQ;
666 return ret;
670 /***********************************************************************
671 * ReleaseMutex (KERNEL32.@)
673 BOOL WINAPI ReleaseMutex( HANDLE handle )
675 BOOL ret;
676 SERVER_START_REQ( release_mutex )
678 req->handle = handle;
679 ret = !wine_server_call_err( req );
681 SERVER_END_REQ;
682 return ret;
687 * Semaphores
691 /***********************************************************************
692 * CreateSemaphoreA (KERNEL32.@)
694 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
696 WCHAR buffer[MAX_PATH];
698 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
700 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
702 SetLastError( ERROR_FILENAME_EXCED_RANGE );
703 return 0;
705 return CreateSemaphoreW( sa, initial, max, buffer );
709 /***********************************************************************
710 * CreateSemaphoreW (KERNEL32.@)
712 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
713 LONG max, LPCWSTR name )
715 HANDLE ret;
716 DWORD len = name ? strlenW(name) : 0;
718 /* Check parameters */
720 if ((max <= 0) || (initial < 0) || (initial > max))
722 SetLastError( ERROR_INVALID_PARAMETER );
723 return 0;
725 if (len >= MAX_PATH)
727 SetLastError( ERROR_FILENAME_EXCED_RANGE );
728 return 0;
731 SERVER_START_REQ( create_semaphore )
733 req->initial = (unsigned int)initial;
734 req->max = (unsigned int)max;
735 req->inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
736 wine_server_add_data( req, name, len * sizeof(WCHAR) );
737 SetLastError(0);
738 wine_server_call_err( req );
739 ret = reply->handle;
741 SERVER_END_REQ;
742 return ret;
746 /***********************************************************************
747 * OpenSemaphoreA (KERNEL32.@)
749 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
751 WCHAR buffer[MAX_PATH];
753 if (!name) return OpenSemaphoreW( access, inherit, NULL );
755 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
757 SetLastError( ERROR_FILENAME_EXCED_RANGE );
758 return 0;
760 return OpenSemaphoreW( access, inherit, buffer );
764 /***********************************************************************
765 * OpenSemaphoreW (KERNEL32.@)
767 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
769 HANDLE ret;
770 DWORD len = name ? strlenW(name) : 0;
771 if (len >= MAX_PATH)
773 SetLastError( ERROR_FILENAME_EXCED_RANGE );
774 return 0;
776 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
778 SERVER_START_REQ( open_semaphore )
780 req->access = access;
781 req->inherit = inherit;
782 wine_server_add_data( req, name, len * sizeof(WCHAR) );
783 wine_server_call_err( req );
784 ret = reply->handle;
786 SERVER_END_REQ;
787 return ret;
791 /***********************************************************************
792 * ReleaseSemaphore (KERNEL32.@)
794 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
796 NTSTATUS status = NtReleaseSemaphore( handle, count, previous );
797 if (status) SetLastError( RtlNtStatusToDosError(status) );
798 return !status;
803 * Timers
807 /***********************************************************************
808 * CreateWaitableTimerA (KERNEL32.@)
810 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
812 WCHAR buffer[MAX_PATH];
814 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
816 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
818 SetLastError( ERROR_FILENAME_EXCED_RANGE );
819 return 0;
821 return CreateWaitableTimerW( sa, manual, buffer );
825 /***********************************************************************
826 * CreateWaitableTimerW (KERNEL32.@)
828 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
830 HANDLE handle;
831 NTSTATUS status;
832 UNICODE_STRING us;
833 DWORD attr = 0;
834 OBJECT_ATTRIBUTES oa;
836 if (name) RtlInitUnicodeString(&us, name);
837 if (sa && (sa->nLength >= sizeof(*sa)) && sa->bInheritHandle)
838 attr |= OBJ_INHERIT;
839 InitializeObjectAttributes(&oa, name ? &us : NULL, attr,
840 NULL /* FIXME */, NULL /* FIXME */);
841 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &oa,
842 manual ? NotificationTimer : SynchronizationTimer);
844 if (status != STATUS_SUCCESS)
846 SetLastError( RtlNtStatusToDosError(status) );
847 return 0;
849 return handle;
853 /***********************************************************************
854 * OpenWaitableTimerA (KERNEL32.@)
856 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
858 WCHAR buffer[MAX_PATH];
860 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
862 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
864 SetLastError( ERROR_FILENAME_EXCED_RANGE );
865 return 0;
867 return OpenWaitableTimerW( access, inherit, buffer );
871 /***********************************************************************
872 * OpenWaitableTimerW (KERNEL32.@)
874 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
876 NTSTATUS status;
877 ULONG attr = 0;
878 UNICODE_STRING us;
879 HANDLE handle;
880 OBJECT_ATTRIBUTES oa;
882 if (inherit) attr |= OBJ_INHERIT;
884 if (name) RtlInitUnicodeString(&us, name);
885 InitializeObjectAttributes(&oa, name ? &us : NULL, attr, NULL /* FIXME */, NULL /* FIXME */);
886 status = NtOpenTimer(&handle, access, &oa);
887 if (status != STATUS_SUCCESS)
889 SetLastError( RtlNtStatusToDosError(status) );
890 return 0;
892 return handle;
896 /***********************************************************************
897 * SetWaitableTimer (KERNEL32.@)
899 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
900 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
902 NTSTATUS status = NtSetTimer(handle, when, callback, arg, resume, period, NULL);
904 if (status != STATUS_SUCCESS)
906 SetLastError( RtlNtStatusToDosError(status) );
907 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
909 return TRUE;
913 /***********************************************************************
914 * CancelWaitableTimer (KERNEL32.@)
916 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
918 NTSTATUS status;
920 status = NtCancelTimer(handle, NULL);
921 if (status != STATUS_SUCCESS)
923 SetLastError( RtlNtStatusToDosError(status) );
924 return FALSE;
926 return TRUE;
930 /***********************************************************************
931 * CreateTimerQueue (KERNEL32.@)
933 HANDLE WINAPI CreateTimerQueue(void)
935 FIXME("stub\n");
936 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
937 return NULL;
941 /***********************************************************************
942 * DeleteTimerQueueEx (KERNEL32.@)
944 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
946 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
947 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
948 return 0;
951 /***********************************************************************
952 * CreateTimerQueueTimer (KERNEL32.@)
954 * Creates a timer-queue timer. This timer expires at the specified due
955 * time (in ms), then after every specified period (in ms). When the timer
956 * expires, the callback function is called.
958 * RETURNS
959 * nonzero on success or zero on faillure
961 * BUGS
962 * Unimplemented
964 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
965 WAITORTIMERCALLBACK Callback, PVOID Parameter,
966 DWORD DueTime, DWORD Period, ULONG Flags )
968 FIXME("stub\n");
969 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
970 return TRUE;
973 /***********************************************************************
974 * DeleteTimerQueueTimer (KERNEL32.@)
976 * Cancels a timer-queue timer.
978 * RETURNS
979 * nonzero on success or zero on faillure
981 * BUGS
982 * Unimplemented
984 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
985 HANDLE CompletionEvent )
987 FIXME("stub\n");
988 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
989 return TRUE;
994 * Pipes
998 /***********************************************************************
999 * CreateNamedPipeA (KERNEL32.@)
1001 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1002 DWORD dwPipeMode, DWORD nMaxInstances,
1003 DWORD nOutBufferSize, DWORD nInBufferSize,
1004 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1006 WCHAR buffer[MAX_PATH];
1008 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1009 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1011 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1013 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1014 return INVALID_HANDLE_VALUE;
1016 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1017 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1021 /***********************************************************************
1022 * CreateNamedPipeW (KERNEL32.@)
1024 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1025 DWORD dwPipeMode, DWORD nMaxInstances,
1026 DWORD nOutBufferSize, DWORD nInBufferSize,
1027 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1029 HANDLE ret;
1030 DWORD len;
1031 static const WCHAR leadin[] = {'\\','\\','.','\\','P','I','P','E','\\'};
1033 TRACE("(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n",
1034 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1035 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1037 if (!name)
1039 SetLastError( ERROR_PATH_NOT_FOUND );
1040 return INVALID_HANDLE_VALUE;
1042 len = strlenW(name);
1043 if (len >= MAX_PATH)
1045 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1046 return INVALID_HANDLE_VALUE;
1048 if (strncmpiW(name, leadin, sizeof(leadin)/sizeof(leadin[0])))
1050 SetLastError( ERROR_INVALID_NAME );
1051 return INVALID_HANDLE_VALUE;
1053 SERVER_START_REQ( create_named_pipe )
1055 req->openmode = dwOpenMode;
1056 req->pipemode = dwPipeMode;
1057 req->maxinstances = nMaxInstances;
1058 req->outsize = nOutBufferSize;
1059 req->insize = nInBufferSize;
1060 req->timeout = nDefaultTimeOut;
1061 wine_server_add_data( req, name, len * sizeof(WCHAR) );
1062 SetLastError(0);
1063 if (!wine_server_call_err( req )) ret = reply->handle;
1064 else ret = INVALID_HANDLE_VALUE;
1066 SERVER_END_REQ;
1067 return ret;
1071 /***********************************************************************
1072 * PeekNamedPipe (KERNEL32.@)
1074 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1075 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1077 #ifdef FIONREAD
1078 int avail=0,fd;
1080 fd = FILE_GetUnixHandle(hPipe, GENERIC_READ);
1081 if (fd == -1) return FALSE;
1083 if (ioctl(fd,FIONREAD, &avail ) != 0)
1085 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1086 close(fd);
1087 return FALSE;
1089 if (!avail) /* check for closed pipe */
1091 struct pollfd pollfd;
1092 pollfd.fd = fd;
1093 pollfd.events = POLLIN;
1094 pollfd.revents = 0;
1095 switch (poll( &pollfd, 1, 0 ))
1097 case 0:
1098 break;
1099 case 1: /* got something */
1100 if (!(pollfd.revents & (POLLHUP | POLLERR))) break;
1101 TRACE("POLLHUP | POLLERR\n");
1102 /* fall through */
1103 case -1:
1104 close(fd);
1105 SetLastError(ERROR_BROKEN_PIPE);
1106 return FALSE;
1109 close(fd);
1110 TRACE(" 0x%08x bytes available\n", avail );
1111 if (!lpvBuffer && lpcbAvail)
1113 *lpcbAvail= avail;
1114 return TRUE;
1116 #endif /* defined(FIONREAD) */
1118 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1119 FIXME("function not implemented\n");
1120 return FALSE;
1123 /***********************************************************************
1124 * SYNC_CompletePipeOverlapped (Internal)
1126 static void SYNC_CompletePipeOverlapped (LPOVERLAPPED overlapped, DWORD result)
1128 TRACE("for %p result %08lx\n",overlapped,result);
1129 if(!overlapped)
1130 return;
1131 overlapped->Internal = result;
1132 SetEvent(overlapped->hEvent);
1136 /***********************************************************************
1137 * WaitNamedPipeA (KERNEL32.@)
1139 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1141 WCHAR buffer[MAX_PATH];
1143 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1145 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1147 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1148 return 0;
1150 return WaitNamedPipeW( buffer, nTimeOut );
1154 /***********************************************************************
1155 * WaitNamedPipeW (KERNEL32.@)
1157 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1159 DWORD len = name ? strlenW(name) : 0;
1160 BOOL ret;
1161 OVERLAPPED ov;
1163 if (len >= MAX_PATH)
1165 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1166 return FALSE;
1169 TRACE("%s 0x%08lx\n",debugstr_w(name),nTimeOut);
1171 memset(&ov,0,sizeof(ov));
1172 ov.hEvent = CreateEventA( NULL, 0, 0, NULL );
1173 if (!ov.hEvent)
1174 return FALSE;
1176 SERVER_START_REQ( wait_named_pipe )
1178 req->timeout = nTimeOut;
1179 req->overlapped = &ov;
1180 req->func = SYNC_CompletePipeOverlapped;
1181 wine_server_add_data( req, name, len * sizeof(WCHAR) );
1182 ret = !wine_server_call_err( req );
1184 SERVER_END_REQ;
1186 if(ret)
1188 if (WAIT_OBJECT_0==WaitForSingleObject(ov.hEvent,INFINITE))
1190 SetLastError(ov.Internal);
1191 ret = (ov.Internal==STATUS_SUCCESS);
1194 CloseHandle(ov.hEvent);
1195 return ret;
1199 /***********************************************************************
1200 * SYNC_ConnectNamedPipe (Internal)
1202 static BOOL SYNC_ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1204 BOOL ret;
1206 if(!overlapped)
1207 return FALSE;
1209 overlapped->Internal = STATUS_PENDING;
1211 SERVER_START_REQ( connect_named_pipe )
1213 req->handle = hPipe;
1214 req->overlapped = overlapped;
1215 req->func = SYNC_CompletePipeOverlapped;
1216 ret = !wine_server_call_err( req );
1218 SERVER_END_REQ;
1220 return ret;
1223 /***********************************************************************
1224 * ConnectNamedPipe (KERNEL32.@)
1226 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1228 OVERLAPPED ov;
1229 BOOL ret;
1231 TRACE("(%p,%p)\n",hPipe, overlapped);
1233 if(overlapped)
1235 if(SYNC_ConnectNamedPipe(hPipe,overlapped))
1236 SetLastError( ERROR_IO_PENDING );
1237 return FALSE;
1240 memset(&ov,0,sizeof(ov));
1241 ov.hEvent = CreateEventA(NULL,0,0,NULL);
1242 if (!ov.hEvent)
1243 return FALSE;
1245 ret=SYNC_ConnectNamedPipe(hPipe, &ov);
1246 if(ret)
1248 if (WAIT_OBJECT_0==WaitForSingleObject(ov.hEvent,INFINITE))
1250 SetLastError(ov.Internal);
1251 ret = (ov.Internal==STATUS_SUCCESS);
1255 CloseHandle(ov.hEvent);
1257 return ret;
1260 /***********************************************************************
1261 * DisconnectNamedPipe (KERNEL32.@)
1263 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1265 BOOL ret;
1267 TRACE("(%p)\n",hPipe);
1269 SERVER_START_REQ( disconnect_named_pipe )
1271 req->handle = hPipe;
1272 ret = !wine_server_call_err( req );
1273 if (ret && reply->fd != -1) close( reply->fd );
1275 SERVER_END_REQ;
1277 return ret;
1280 /***********************************************************************
1281 * TransactNamedPipe (KERNEL32.@)
1283 BOOL WINAPI TransactNamedPipe(
1284 HANDLE hPipe, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1285 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1287 FIXME("%p %p %ld %p %ld %p %p\n",
1288 hPipe, lpInput, dwInputSize, lpOutput,
1289 dwOutputSize, lpBytesRead, lpOverlapped);
1290 if(lpBytesRead)
1291 *lpBytesRead=0;
1292 return FALSE;
1295 /***********************************************************************
1296 * GetNamedPipeInfo (KERNEL32.@)
1298 BOOL WINAPI GetNamedPipeInfo(
1299 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1300 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1302 BOOL ret;
1304 TRACE("%p %p %p %p %p\n", hNamedPipe, lpFlags,
1305 lpOutputBufferSize, lpInputBufferSize, lpMaxInstances);
1307 SERVER_START_REQ( get_named_pipe_info )
1309 req->handle = hNamedPipe;
1310 ret = !wine_server_call_err( req );
1311 if(lpFlags) *lpFlags = reply->flags;
1312 if(lpOutputBufferSize) *lpOutputBufferSize = reply->outsize;
1313 if(lpInputBufferSize) *lpInputBufferSize = reply->outsize;
1314 if(lpMaxInstances) *lpMaxInstances = reply->maxinstances;
1316 SERVER_END_REQ;
1318 return ret;
1321 /***********************************************************************
1322 * GetNamedPipeHandleStateA (KERNEL32.@)
1324 BOOL WINAPI GetNamedPipeHandleStateA(
1325 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1326 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1327 LPSTR lpUsername, DWORD nUsernameMaxSize)
1329 FIXME("%p %p %p %p %p %p %ld\n",
1330 hNamedPipe, lpState, lpCurInstances,
1331 lpMaxCollectionCount, lpCollectDataTimeout,
1332 lpUsername, nUsernameMaxSize);
1334 return FALSE;
1337 /***********************************************************************
1338 * GetNamedPipeHandleStateW (KERNEL32.@)
1340 BOOL WINAPI GetNamedPipeHandleStateW(
1341 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1342 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1343 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1345 FIXME("%p %p %p %p %p %p %ld\n",
1346 hNamedPipe, lpState, lpCurInstances,
1347 lpMaxCollectionCount, lpCollectDataTimeout,
1348 lpUsername, nUsernameMaxSize);
1350 return FALSE;
1353 /***********************************************************************
1354 * SetNamedPipeHandleState (KERNEL32.@)
1356 BOOL WINAPI SetNamedPipeHandleState(
1357 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1358 LPDWORD lpCollectDataTimeout)
1360 FIXME("%p %p %p %p\n",
1361 hNamedPipe, lpMode, lpMaxCollectionCount, lpCollectDataTimeout);
1362 return FALSE;
1365 /***********************************************************************
1366 * CallNamedPipeA (KERNEL32.@)
1368 BOOL WINAPI CallNamedPipeA(
1369 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1370 LPVOID lpOutput, DWORD lpOutputSize,
1371 LPDWORD lpBytesRead, DWORD nTimeout)
1373 FIXME("%s %p %ld %p %ld %p %ld\n",
1374 debugstr_a(lpNamedPipeName), lpInput, lpInputSize,
1375 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1376 return FALSE;
1379 /***********************************************************************
1380 * CallNamedPipeW (KERNEL32.@)
1382 BOOL WINAPI CallNamedPipeW(
1383 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1384 LPVOID lpOutput, DWORD lpOutputSize,
1385 LPDWORD lpBytesRead, DWORD nTimeout)
1387 FIXME("%s %p %ld %p %ld %p %ld\n",
1388 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1389 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1390 return FALSE;
1393 /******************************************************************
1394 * CreatePipe (KERNEL32.@)
1397 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1398 LPSECURITY_ATTRIBUTES sa, DWORD size )
1400 static unsigned index = 0;
1401 char name[64];
1402 HANDLE hr, hw;
1403 unsigned in_index = index;
1405 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1406 /* generate a unique pipe name (system wide) */
1409 sprintf(name, "\\\\.\\pipe\\Win32.Pipes.%08lu.%08u", GetCurrentProcessId(), ++index);
1410 hr = CreateNamedPipeA(name, PIPE_ACCESS_INBOUND,
1411 PIPE_TYPE_BYTE | PIPE_WAIT, 1, size, size,
1412 NMPWAIT_USE_DEFAULT_WAIT, sa);
1413 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1414 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1415 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1417 hw = CreateFileA(name, GENERIC_WRITE, 0, sa, OPEN_EXISTING, 0, 0);
1418 if (hw == INVALID_HANDLE_VALUE)
1420 CloseHandle(hr);
1421 return FALSE;
1424 *hReadPipe = hr;
1425 *hWritePipe = hw;
1426 return TRUE;
1430 /******************************************************************************
1431 * CreateMailslotA [KERNEL32.@]
1433 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1434 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1436 DWORD len;
1437 HANDLE handle;
1438 LPWSTR name = NULL;
1440 TRACE("%s %ld %ld %p\n", debugstr_a(lpName),
1441 nMaxMessageSize, lReadTimeout, sa);
1443 if( lpName )
1445 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1446 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1447 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1450 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1452 if( name )
1453 HeapFree( GetProcessHeap(), 0, name );
1455 return handle;
1459 /******************************************************************************
1460 * CreateMailslotW [KERNEL32.@]
1462 * Create a mailslot with specified name.
1464 * PARAMS
1465 * lpName [I] Pointer to string for mailslot name
1466 * nMaxMessageSize [I] Maximum message size
1467 * lReadTimeout [I] Milliseconds before read time-out
1468 * sa [I] Pointer to security structure
1470 * RETURNS
1471 * Success: Handle to mailslot
1472 * Failure: INVALID_HANDLE_VALUE
1474 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1475 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1477 FIXME("(%s,%ld,%ld,%p): stub\n", debugstr_w(lpName),
1478 nMaxMessageSize, lReadTimeout, sa);
1479 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1480 return INVALID_HANDLE_VALUE;
1484 /******************************************************************************
1485 * GetMailslotInfo [KERNEL32.@]
1487 * Retrieve information about a mailslot.
1489 * PARAMS
1490 * hMailslot [I] Mailslot handle
1491 * lpMaxMessageSize [O] Address of maximum message size
1492 * lpNextSize [O] Address of size of next message
1493 * lpMessageCount [O] Address of number of messages
1494 * lpReadTimeout [O] Address of read time-out
1496 * RETURNS
1497 * Success: TRUE
1498 * Failure: FALSE
1500 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1501 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1502 LPDWORD lpReadTimeout )
1504 FIXME("(%p): stub\n",hMailslot);
1505 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1506 return FALSE;
1510 /******************************************************************************
1511 * SetMailslotInfo [KERNEL32.@]
1513 * Set the read timeout of a mailslot.
1515 * PARAMS
1516 * hMailslot [I] Mailslot handle
1517 * dwReadTimeout [I] Timeout in milliseconds.
1519 * RETURNS
1520 * Success: TRUE
1521 * Failure: FALSE
1523 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1525 FIXME("%p %ld: stub\n", hMailslot, dwReadTimeout);
1526 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1527 return FALSE;
1531 /******************************************************************************
1532 * CreateIoCompletionPort (KERNEL32.@)
1534 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1535 DWORD dwCompletionKey, DWORD dwNumberOfConcurrentThreads)
1537 FIXME("(%p, %p, %08lx, %08lx): stub.\n",
1538 hFileHandle, hExistingCompletionPort, dwCompletionKey, dwNumberOfConcurrentThreads);
1539 return NULL;
1543 /******************************************************************************
1544 * GetQueuedCompletionStatus (KERNEL32.@)
1546 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1547 LPDWORD lpCompletionKey, LPOVERLAPPED *lpOverlapped,
1548 DWORD dwMilliseconds )
1550 FIXME("(%p,%p,%p,%p,%ld), stub!\n",
1551 CompletionPort,lpNumberOfBytesTransferred,lpCompletionKey,lpOverlapped,dwMilliseconds);
1552 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1553 return FALSE;
1556 #ifdef __i386__
1558 /***********************************************************************
1559 * InterlockedCompareExchange (KERNEL32.@)
1561 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1562 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1563 "movl 12(%esp),%eax\n\t"
1564 "movl 8(%esp),%ecx\n\t"
1565 "movl 4(%esp),%edx\n\t"
1566 "lock; cmpxchgl %ecx,(%edx)\n\t"
1567 "ret $12");
1569 /***********************************************************************
1570 * InterlockedExchange (KERNEL32.@)
1572 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1573 __ASM_GLOBAL_FUNC(InterlockedExchange,
1574 "movl 8(%esp),%eax\n\t"
1575 "movl 4(%esp),%edx\n\t"
1576 "lock; xchgl %eax,(%edx)\n\t"
1577 "ret $8");
1579 /***********************************************************************
1580 * InterlockedExchangeAdd (KERNEL32.@)
1582 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1583 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1584 "movl 8(%esp),%eax\n\t"
1585 "movl 4(%esp),%edx\n\t"
1586 "lock; xaddl %eax,(%edx)\n\t"
1587 "ret $8");
1589 /***********************************************************************
1590 * InterlockedIncrement (KERNEL32.@)
1592 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1593 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1594 "movl 4(%esp),%edx\n\t"
1595 "movl $1,%eax\n\t"
1596 "lock; xaddl %eax,(%edx)\n\t"
1597 "incl %eax\n\t"
1598 "ret $4");
1600 /***********************************************************************
1601 * InterlockedDecrement (KERNEL32.@)
1603 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1604 "movl 4(%esp),%edx\n\t"
1605 "movl $-1,%eax\n\t"
1606 "lock; xaddl %eax,(%edx)\n\t"
1607 "decl %eax\n\t"
1608 "ret $4");
1610 #else /* __i386__ */
1612 /***********************************************************************
1613 * InterlockedCompareExchange (KERNEL32.@)
1615 * Atomically swap one value with another.
1617 * PARAMS
1618 * dest [I/O] The value to replace
1619 * xchq [I] The value to be swapped
1620 * compare [I] The value to compare to dest
1622 * RETURNS
1623 * The resulting value of dest.
1625 * NOTES
1626 * dest is updated only if it is equal to compare, otherwise no swap is done.
1628 LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare )
1630 return interlocked_cmpxchg( dest, xchg, compare );
1633 /***********************************************************************
1634 * InterlockedExchange (KERNEL32.@)
1636 * Atomically swap one value with another.
1638 * PARAMS
1639 * dest [I/O] The value to replace
1640 * val [I] The value to be swapped
1642 * RETURNS
1643 * The resulting value of dest.
1645 LONG WINAPI InterlockedExchange( PLONG dest, LONG val )
1647 return interlocked_xchg( dest, val );
1650 /***********************************************************************
1651 * InterlockedExchangeAdd (KERNEL32.@)
1653 * Atomically add one value to another.
1655 * PARAMS
1656 * dest [I/O] The value to add to
1657 * incr [I] The value to be added
1659 * RETURNS
1660 * The resulting value of dest.
1662 LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr )
1664 return interlocked_xchg_add( dest, incr );
1667 /***********************************************************************
1668 * InterlockedIncrement (KERNEL32.@)
1670 * Atomically increment a value.
1672 * PARAMS
1673 * dest [I/O] The value to increment
1675 * RETURNS
1676 * The resulting value of dest.
1678 LONG WINAPI InterlockedIncrement( PLONG dest )
1680 return interlocked_xchg_add( dest, 1 ) + 1;
1683 /***********************************************************************
1684 * InterlockedDecrement (KERNEL32.@)
1686 * Atomically decrement a value.
1688 * PARAMS
1689 * dest [I/O] The value to decrement
1691 * RETURNS
1692 * The resulting value of dest.
1694 LONG WINAPI InterlockedDecrement( PLONG dest )
1696 return interlocked_xchg_add( dest, -1 ) - 1;
1699 #endif /* __i386__ */