Fixed InterLocked* function definitions for non-x86 platforms in
[wine/hacks.git] / dlls / kernel / sync.c
blobfe785eca44344108a525cdffa389f15a71aefe87
1 /*
2 * Kernel synchronization objects
4 * Copyright 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <errno.h>
29 #ifdef HAVE_SYS_IOCTL_H
30 #include <sys/ioctl.h>
31 #endif
32 #ifdef HAVE_POLL_H
33 #include <poll.h>
34 #endif
35 #ifdef HAVE_SYS_POLL_H
36 #include <sys/poll.h>
37 #endif
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
40 #endif
41 #include <stdarg.h>
42 #include <stdio.h>
44 #define NONAMELESSUNION
45 #define NONAMELESSSTRUCT
47 #include "ntstatus.h"
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winerror.h"
51 #include "winnls.h"
52 #include "winternl.h"
53 #include "winioctl.h"
55 #include "wine/server.h"
56 #include "wine/unicode.h"
57 #include "wine/winbase16.h"
58 #include "kernel_private.h"
60 #include "wine/debug.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(sync);
64 /* check if current version is NT or Win95 */
65 inline static int is_version_nt(void)
67 return !(GetVersion() & 0x80000000);
71 /***********************************************************************
72 * Sleep (KERNEL32.@)
74 VOID WINAPI Sleep( DWORD timeout )
76 SleepEx( timeout, FALSE );
79 /******************************************************************************
80 * SleepEx (KERNEL32.@)
82 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
84 NTSTATUS status;
86 if (timeout == INFINITE) status = NtDelayExecution( alertable, NULL );
87 else
89 LARGE_INTEGER time;
91 time.QuadPart = timeout * (ULONGLONG)10000;
92 time.QuadPart = -time.QuadPart;
93 status = NtDelayExecution( alertable, &time );
95 if (status != STATUS_USER_APC) status = STATUS_SUCCESS;
96 return status;
100 /***********************************************************************
101 * SwitchToThread (KERNEL32.@)
103 BOOL WINAPI SwitchToThread(void)
105 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED);
109 /***********************************************************************
110 * WaitForSingleObject (KERNEL32.@)
112 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
114 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
118 /***********************************************************************
119 * WaitForSingleObjectEx (KERNEL32.@)
121 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
122 BOOL alertable )
124 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
128 /***********************************************************************
129 * WaitForMultipleObjects (KERNEL32.@)
131 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
132 BOOL wait_all, DWORD timeout )
134 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
138 /***********************************************************************
139 * WaitForMultipleObjectsEx (KERNEL32.@)
141 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
142 BOOL wait_all, DWORD timeout,
143 BOOL alertable )
145 NTSTATUS status;
146 HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
147 unsigned int i;
149 if (count >= MAXIMUM_WAIT_OBJECTS)
151 SetLastError(ERROR_INVALID_PARAMETER);
152 return WAIT_FAILED;
154 for (i = 0; i < count; i++)
156 if ((handles[i] == (HANDLE)STD_INPUT_HANDLE) ||
157 (handles[i] == (HANDLE)STD_OUTPUT_HANDLE) ||
158 (handles[i] == (HANDLE)STD_ERROR_HANDLE))
159 hloc[i] = GetStdHandle( (DWORD)handles[i] );
160 else
161 hloc[i] = handles[i];
163 /* yes, even screen buffer console handles are waitable, and are
164 * handled as a handle to the console itself !!
166 if (is_console_handle(hloc[i]))
168 if (!VerifyConsoleIoHandle(hloc[i]))
170 return FALSE;
172 hloc[i] = GetConsoleInputWaitHandle();
176 if (timeout == INFINITE)
178 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, NULL );
180 else
182 LARGE_INTEGER time;
184 time.QuadPart = timeout * (ULONGLONG)10000;
185 time.QuadPart = -time.QuadPart;
186 status = NtWaitForMultipleObjects( count, hloc, wait_all, alertable, &time );
189 if (HIWORD(status)) /* is it an error code? */
191 SetLastError( RtlNtStatusToDosError(status) );
192 status = WAIT_FAILED;
194 return status;
198 /***********************************************************************
199 * WaitForSingleObject (KERNEL.460)
201 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
203 DWORD retval, mutex_count;
205 ReleaseThunkLock( &mutex_count );
206 retval = WaitForSingleObject( handle, timeout );
207 RestoreThunkLock( mutex_count );
208 return retval;
211 /***********************************************************************
212 * WaitForMultipleObjects (KERNEL.461)
214 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
215 BOOL wait_all, DWORD timeout )
217 DWORD retval, mutex_count;
219 ReleaseThunkLock( &mutex_count );
220 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
221 RestoreThunkLock( mutex_count );
222 return retval;
225 /***********************************************************************
226 * WaitForMultipleObjectsEx (KERNEL.495)
228 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
229 BOOL wait_all, DWORD timeout, BOOL alertable )
231 DWORD retval, mutex_count;
233 ReleaseThunkLock( &mutex_count );
234 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
235 RestoreThunkLock( mutex_count );
236 return retval;
239 /***********************************************************************
240 * RegisterWaitForSingleObject (KERNEL32.@)
242 BOOL WINAPI RegisterWaitForSingleObject(PHANDLE phNewWaitObject, HANDLE hObject,
243 WAITORTIMERCALLBACK Callback, PVOID Context,
244 ULONG dwMilliseconds, ULONG dwFlags)
246 FIXME("%p %p %p %p %ld %ld\n",
247 phNewWaitObject,hObject,Callback,Context,dwMilliseconds,dwFlags);
248 return FALSE;
251 /***********************************************************************
252 * RegisterWaitForSingleObjectEx (KERNEL32.@)
254 HANDLE WINAPI RegisterWaitForSingleObjectEx( HANDLE hObject,
255 WAITORTIMERCALLBACK Callback, PVOID Context,
256 ULONG dwMilliseconds, ULONG dwFlags )
258 FIXME("%p %p %p %ld %ld\n",
259 hObject,Callback,Context,dwMilliseconds,dwFlags);
260 return 0;
263 /***********************************************************************
264 * UnregisterWait (KERNEL32.@)
266 BOOL WINAPI UnregisterWait( HANDLE WaitHandle )
268 FIXME("%p\n",WaitHandle);
269 return FALSE;
272 /***********************************************************************
273 * UnregisterWaitEx (KERNEL32.@)
275 BOOL WINAPI UnregisterWaitEx( HANDLE WaitHandle, HANDLE CompletionEvent )
277 FIXME("%p %p\n",WaitHandle, CompletionEvent);
278 return FALSE;
281 /***********************************************************************
282 * SignalObjectAndWait (KERNEL32.@)
284 * Allows to atomically signal any of the synchro objects (semaphore,
285 * mutex, event) and wait on another.
287 DWORD WINAPI SignalObjectAndWait( HANDLE hObjectToSignal, HANDLE hObjectToWaitOn,
288 DWORD dwMilliseconds, BOOL bAlertable )
290 NTSTATUS status;
291 LARGE_INTEGER timeout, *ptimeout = NULL;
293 TRACE("%p %p %ld %d\n", hObjectToSignal,
294 hObjectToWaitOn, dwMilliseconds, bAlertable);
296 if (dwMilliseconds != INFINITE)
298 timeout.QuadPart = dwMilliseconds * (ULONGLONG)10000;
299 timeout.QuadPart = -timeout.QuadPart;
300 ptimeout = &timeout;
303 status = NtSignalAndWaitForSingleObject( hObjectToSignal, hObjectToWaitOn,
304 bAlertable, ptimeout );
305 if (HIWORD(status))
307 SetLastError( RtlNtStatusToDosError(status) );
308 status = WAIT_FAILED;
310 return status;
313 /***********************************************************************
314 * InitializeCriticalSection (KERNEL32.@)
316 * Initialise a critical section before use.
318 * PARAMS
319 * crit [O] Critical section to initialise.
321 * RETURNS
322 * Nothing. If the function fails an exception is raised.
324 void WINAPI InitializeCriticalSection( CRITICAL_SECTION *crit )
326 NTSTATUS ret = RtlInitializeCriticalSection( crit );
327 if (ret) RtlRaiseStatus( ret );
330 /***********************************************************************
331 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
333 * Initialise a critical section with a spin count.
335 * PARAMS
336 * crit [O] Critical section to initialise.
337 * spincount [I] Number of times to spin upon contention.
339 * RETURNS
340 * Success: TRUE.
341 * Failure: Nothing. If the function fails an exception is raised.
343 * NOTES
344 * spincount is ignored on uni-processor systems.
346 BOOL WINAPI InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD spincount )
348 NTSTATUS ret = RtlInitializeCriticalSectionAndSpinCount( crit, spincount );
349 if (ret) RtlRaiseStatus( ret );
350 return !ret;
353 /***********************************************************************
354 * MakeCriticalSectionGlobal (KERNEL32.@)
356 void WINAPI MakeCriticalSectionGlobal( CRITICAL_SECTION *crit )
358 /* let's assume that only one thread at a time will try to do this */
359 HANDLE sem = crit->LockSemaphore;
360 if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
361 crit->LockSemaphore = ConvertToGlobalHandle( sem );
362 if (crit->DebugInfo)
364 RtlFreeHeap( GetProcessHeap(), 0, crit->DebugInfo );
365 crit->DebugInfo = NULL;
370 /***********************************************************************
371 * ReinitializeCriticalSection (KERNEL32.@)
373 * Initialise an already used critical section.
375 * PARAMS
376 * crit [O] Critical section to initialise.
378 * RETURNS
379 * Nothing.
381 void WINAPI ReinitializeCriticalSection( CRITICAL_SECTION *crit )
383 if ( !crit->LockSemaphore )
384 RtlInitializeCriticalSection( crit );
388 /***********************************************************************
389 * UninitializeCriticalSection (KERNEL32.@)
391 * UnInitialise a critical section after use.
393 * PARAMS
394 * crit [O] Critical section to uninitialise (destroy).
396 * RETURNS
397 * Nothing.
399 void WINAPI UninitializeCriticalSection( CRITICAL_SECTION *crit )
401 RtlDeleteCriticalSection( crit );
405 /***********************************************************************
406 * CreateEventA (KERNEL32.@)
408 HANDLE WINAPI CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
409 BOOL initial_state, LPCSTR name )
411 WCHAR buffer[MAX_PATH];
413 if (!name) return CreateEventW( sa, manual_reset, initial_state, NULL );
415 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
417 SetLastError( ERROR_FILENAME_EXCED_RANGE );
418 return 0;
420 return CreateEventW( sa, manual_reset, initial_state, buffer );
424 /***********************************************************************
425 * CreateEventW (KERNEL32.@)
427 HANDLE WINAPI CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
428 BOOL initial_state, LPCWSTR name )
430 HANDLE ret;
431 UNICODE_STRING nameW;
432 OBJECT_ATTRIBUTES attr;
433 NTSTATUS status;
435 /* one buggy program needs this
436 * ("Van Dale Groot woordenboek der Nederlandse taal")
438 if (sa && IsBadReadPtr(sa,sizeof(SECURITY_ATTRIBUTES)))
440 ERR("Bad security attributes pointer %p\n",sa);
441 SetLastError( ERROR_INVALID_PARAMETER);
442 return 0;
445 attr.Length = sizeof(attr);
446 attr.RootDirectory = 0;
447 attr.ObjectName = NULL;
448 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
449 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
450 attr.SecurityQualityOfService = NULL;
451 if (name)
453 RtlInitUnicodeString( &nameW, name );
454 attr.ObjectName = &nameW;
457 status = NtCreateEvent( &ret, EVENT_ALL_ACCESS, &attr, manual_reset, initial_state );
458 SetLastError( RtlNtStatusToDosError(status) );
459 return ret;
463 /***********************************************************************
464 * CreateW32Event (KERNEL.457)
466 HANDLE WINAPI WIN16_CreateEvent( BOOL manual_reset, BOOL initial_state )
468 return CreateEventW( NULL, manual_reset, initial_state, NULL );
472 /***********************************************************************
473 * OpenEventA (KERNEL32.@)
475 HANDLE WINAPI OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
477 WCHAR buffer[MAX_PATH];
479 if (!name) return OpenEventW( access, inherit, NULL );
481 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
483 SetLastError( ERROR_FILENAME_EXCED_RANGE );
484 return 0;
486 return OpenEventW( access, inherit, buffer );
490 /***********************************************************************
491 * OpenEventW (KERNEL32.@)
493 HANDLE WINAPI OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
495 HANDLE ret;
496 UNICODE_STRING nameW;
497 OBJECT_ATTRIBUTES attr;
498 NTSTATUS status;
500 if (!is_version_nt()) access = EVENT_ALL_ACCESS;
502 attr.Length = sizeof(attr);
503 attr.RootDirectory = 0;
504 attr.ObjectName = NULL;
505 attr.Attributes = inherit ? OBJ_INHERIT : 0;
506 attr.SecurityDescriptor = NULL;
507 attr.SecurityQualityOfService = NULL;
508 if (name)
510 RtlInitUnicodeString( &nameW, name );
511 attr.ObjectName = &nameW;
514 status = NtOpenEvent( &ret, access, &attr );
515 if (status != STATUS_SUCCESS)
517 SetLastError( RtlNtStatusToDosError(status) );
518 return 0;
520 return ret;
523 /***********************************************************************
524 * PulseEvent (KERNEL32.@)
526 BOOL WINAPI PulseEvent( HANDLE handle )
528 NTSTATUS status;
530 if ((status = NtPulseEvent( handle, NULL )))
531 SetLastError( RtlNtStatusToDosError(status) );
532 return !status;
536 /***********************************************************************
537 * SetW32Event (KERNEL.458)
538 * SetEvent (KERNEL32.@)
540 BOOL WINAPI SetEvent( HANDLE handle )
542 NTSTATUS status;
544 if ((status = NtSetEvent( handle, NULL )))
545 SetLastError( RtlNtStatusToDosError(status) );
546 return !status;
550 /***********************************************************************
551 * ResetW32Event (KERNEL.459)
552 * ResetEvent (KERNEL32.@)
554 BOOL WINAPI ResetEvent( HANDLE handle )
556 NTSTATUS status;
558 if ((status = NtResetEvent( handle, NULL )))
559 SetLastError( RtlNtStatusToDosError(status) );
560 return !status;
564 /***********************************************************************
565 * NOTE: The Win95 VWin32_Event routines given below are really low-level
566 * routines implemented directly by VWin32. The user-mode libraries
567 * implement Win32 synchronisation routines on top of these low-level
568 * primitives. We do it the other way around here :-)
571 /***********************************************************************
572 * VWin32_EventCreate (KERNEL.442)
574 HANDLE WINAPI VWin32_EventCreate(VOID)
576 HANDLE hEvent = CreateEventW( NULL, FALSE, 0, NULL );
577 return ConvertToGlobalHandle( hEvent );
580 /***********************************************************************
581 * VWin32_EventDestroy (KERNEL.443)
583 VOID WINAPI VWin32_EventDestroy(HANDLE event)
585 CloseHandle( event );
588 /***********************************************************************
589 * VWin32_EventWait (KERNEL.450)
591 VOID WINAPI VWin32_EventWait(HANDLE event)
593 DWORD mutex_count;
595 ReleaseThunkLock( &mutex_count );
596 WaitForSingleObject( event, INFINITE );
597 RestoreThunkLock( mutex_count );
600 /***********************************************************************
601 * VWin32_EventSet (KERNEL.451)
602 * KERNEL_479 (KERNEL.479)
604 VOID WINAPI VWin32_EventSet(HANDLE event)
606 SetEvent( event );
611 /***********************************************************************
612 * CreateMutexA (KERNEL32.@)
614 HANDLE WINAPI CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
616 WCHAR buffer[MAX_PATH];
618 if (!name) return CreateMutexW( sa, owner, NULL );
620 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
622 SetLastError( ERROR_FILENAME_EXCED_RANGE );
623 return 0;
625 return CreateMutexW( sa, owner, buffer );
629 /***********************************************************************
630 * CreateMutexW (KERNEL32.@)
632 HANDLE WINAPI CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
634 HANDLE ret;
635 UNICODE_STRING nameW;
636 OBJECT_ATTRIBUTES attr;
637 NTSTATUS status;
639 attr.Length = sizeof(attr);
640 attr.RootDirectory = 0;
641 attr.ObjectName = NULL;
642 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
643 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
644 attr.SecurityQualityOfService = NULL;
645 if (name)
647 RtlInitUnicodeString( &nameW, name );
648 attr.ObjectName = &nameW;
651 status = NtCreateMutant( &ret, MUTEX_ALL_ACCESS, &attr, owner );
652 SetLastError( RtlNtStatusToDosError(status) );
653 return ret;
657 /***********************************************************************
658 * OpenMutexA (KERNEL32.@)
660 HANDLE WINAPI OpenMutexA( DWORD access, BOOL inherit, LPCSTR name )
662 WCHAR buffer[MAX_PATH];
664 if (!name) return OpenMutexW( access, inherit, NULL );
666 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
668 SetLastError( ERROR_FILENAME_EXCED_RANGE );
669 return 0;
671 return OpenMutexW( access, inherit, buffer );
675 /***********************************************************************
676 * OpenMutexW (KERNEL32.@)
678 HANDLE WINAPI OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
680 HANDLE ret;
681 UNICODE_STRING nameW;
682 OBJECT_ATTRIBUTES attr;
683 NTSTATUS status;
685 if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
687 attr.Length = sizeof(attr);
688 attr.RootDirectory = 0;
689 attr.ObjectName = NULL;
690 attr.Attributes = inherit ? OBJ_INHERIT : 0;
691 attr.SecurityDescriptor = NULL;
692 attr.SecurityQualityOfService = NULL;
693 if (name)
695 RtlInitUnicodeString( &nameW, name );
696 attr.ObjectName = &nameW;
699 status = NtOpenMutant( &ret, access, &attr );
700 if (status != STATUS_SUCCESS)
702 SetLastError( RtlNtStatusToDosError(status) );
703 return 0;
705 return ret;
709 /***********************************************************************
710 * ReleaseMutex (KERNEL32.@)
712 BOOL WINAPI ReleaseMutex( HANDLE handle )
714 NTSTATUS status;
716 status = NtReleaseMutant(handle, NULL);
717 if (status != STATUS_SUCCESS)
719 SetLastError( RtlNtStatusToDosError(status) );
720 return FALSE;
722 return TRUE;
727 * Semaphores
731 /***********************************************************************
732 * CreateSemaphoreA (KERNEL32.@)
734 HANDLE WINAPI CreateSemaphoreA( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max, LPCSTR name )
736 WCHAR buffer[MAX_PATH];
738 if (!name) return CreateSemaphoreW( sa, initial, max, NULL );
740 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
742 SetLastError( ERROR_FILENAME_EXCED_RANGE );
743 return 0;
745 return CreateSemaphoreW( sa, initial, max, buffer );
749 /***********************************************************************
750 * CreateSemaphoreW (KERNEL32.@)
752 HANDLE WINAPI CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
753 LONG max, LPCWSTR name )
755 HANDLE ret;
756 UNICODE_STRING nameW;
757 OBJECT_ATTRIBUTES attr;
758 NTSTATUS status;
760 attr.Length = sizeof(attr);
761 attr.RootDirectory = 0;
762 attr.ObjectName = NULL;
763 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
764 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
765 attr.SecurityQualityOfService = NULL;
766 if (name)
768 RtlInitUnicodeString( &nameW, name );
769 attr.ObjectName = &nameW;
772 status = NtCreateSemaphore( &ret, SEMAPHORE_ALL_ACCESS, &attr, initial, max );
773 SetLastError( RtlNtStatusToDosError(status) );
774 return ret;
778 /***********************************************************************
779 * OpenSemaphoreA (KERNEL32.@)
781 HANDLE WINAPI OpenSemaphoreA( DWORD access, BOOL inherit, LPCSTR name )
783 WCHAR buffer[MAX_PATH];
785 if (!name) return OpenSemaphoreW( access, inherit, NULL );
787 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
789 SetLastError( ERROR_FILENAME_EXCED_RANGE );
790 return 0;
792 return OpenSemaphoreW( access, inherit, buffer );
796 /***********************************************************************
797 * OpenSemaphoreW (KERNEL32.@)
799 HANDLE WINAPI OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
801 HANDLE ret;
802 UNICODE_STRING nameW;
803 OBJECT_ATTRIBUTES attr;
804 NTSTATUS status;
806 if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
808 attr.Length = sizeof(attr);
809 attr.RootDirectory = 0;
810 attr.ObjectName = NULL;
811 attr.Attributes = inherit ? OBJ_INHERIT : 0;
812 attr.SecurityDescriptor = NULL;
813 attr.SecurityQualityOfService = NULL;
814 if (name)
816 RtlInitUnicodeString( &nameW, name );
817 attr.ObjectName = &nameW;
820 status = NtOpenSemaphore( &ret, access, &attr );
821 if (status != STATUS_SUCCESS)
823 SetLastError( RtlNtStatusToDosError(status) );
824 return 0;
826 return ret;
830 /***********************************************************************
831 * ReleaseSemaphore (KERNEL32.@)
833 BOOL WINAPI ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
835 NTSTATUS status = NtReleaseSemaphore( handle, count, (PULONG)previous );
836 if (status) SetLastError( RtlNtStatusToDosError(status) );
837 return !status;
842 * Timers
846 /***********************************************************************
847 * CreateWaitableTimerA (KERNEL32.@)
849 HANDLE WINAPI CreateWaitableTimerA( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCSTR name )
851 WCHAR buffer[MAX_PATH];
853 if (!name) return CreateWaitableTimerW( sa, manual, NULL );
855 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
857 SetLastError( ERROR_FILENAME_EXCED_RANGE );
858 return 0;
860 return CreateWaitableTimerW( sa, manual, buffer );
864 /***********************************************************************
865 * CreateWaitableTimerW (KERNEL32.@)
867 HANDLE WINAPI CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
869 HANDLE handle;
870 NTSTATUS status;
871 UNICODE_STRING nameW;
872 OBJECT_ATTRIBUTES attr;
874 attr.Length = sizeof(attr);
875 attr.RootDirectory = 0;
876 attr.ObjectName = NULL;
877 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
878 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
879 attr.SecurityQualityOfService = NULL;
880 if (name)
882 RtlInitUnicodeString( &nameW, name );
883 attr.ObjectName = &nameW;
886 status = NtCreateTimer(&handle, TIMER_ALL_ACCESS, &attr,
887 manual ? NotificationTimer : SynchronizationTimer);
888 SetLastError( RtlNtStatusToDosError(status) );
889 return handle;
893 /***********************************************************************
894 * OpenWaitableTimerA (KERNEL32.@)
896 HANDLE WINAPI OpenWaitableTimerA( DWORD access, BOOL inherit, LPCSTR name )
898 WCHAR buffer[MAX_PATH];
900 if (!name) return OpenWaitableTimerW( access, inherit, NULL );
902 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
904 SetLastError( ERROR_FILENAME_EXCED_RANGE );
905 return 0;
907 return OpenWaitableTimerW( access, inherit, buffer );
911 /***********************************************************************
912 * OpenWaitableTimerW (KERNEL32.@)
914 HANDLE WINAPI OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
916 HANDLE handle;
917 UNICODE_STRING nameW;
918 OBJECT_ATTRIBUTES attr;
919 NTSTATUS status;
921 if (!is_version_nt()) access = TIMER_ALL_ACCESS;
923 attr.Length = sizeof(attr);
924 attr.RootDirectory = 0;
925 attr.ObjectName = NULL;
926 attr.Attributes = inherit ? OBJ_INHERIT : 0;
927 attr.SecurityDescriptor = NULL;
928 attr.SecurityQualityOfService = NULL;
929 if (name)
931 RtlInitUnicodeString( &nameW, name );
932 attr.ObjectName = &nameW;
935 status = NtOpenTimer(&handle, access, &attr);
936 if (status != STATUS_SUCCESS)
938 SetLastError( RtlNtStatusToDosError(status) );
939 return 0;
941 return handle;
945 /***********************************************************************
946 * SetWaitableTimer (KERNEL32.@)
948 BOOL WINAPI SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
949 PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
951 NTSTATUS status = NtSetTimer(handle, when, (PTIMER_APC_ROUTINE)callback,
952 arg, resume, period, NULL);
954 if (status != STATUS_SUCCESS)
956 SetLastError( RtlNtStatusToDosError(status) );
957 if (status != STATUS_TIMER_RESUME_IGNORED) return FALSE;
959 return TRUE;
963 /***********************************************************************
964 * CancelWaitableTimer (KERNEL32.@)
966 BOOL WINAPI CancelWaitableTimer( HANDLE handle )
968 NTSTATUS status;
970 status = NtCancelTimer(handle, NULL);
971 if (status != STATUS_SUCCESS)
973 SetLastError( RtlNtStatusToDosError(status) );
974 return FALSE;
976 return TRUE;
980 /***********************************************************************
981 * CreateTimerQueue (KERNEL32.@)
983 HANDLE WINAPI CreateTimerQueue(void)
985 FIXME("stub\n");
986 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
987 return NULL;
991 /***********************************************************************
992 * DeleteTimerQueueEx (KERNEL32.@)
994 BOOL WINAPI DeleteTimerQueueEx(HANDLE TimerQueue, HANDLE CompletionEvent)
996 FIXME("(%p, %p): stub\n", TimerQueue, CompletionEvent);
997 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
998 return 0;
1001 /***********************************************************************
1002 * CreateTimerQueueTimer (KERNEL32.@)
1004 * Creates a timer-queue timer. This timer expires at the specified due
1005 * time (in ms), then after every specified period (in ms). When the timer
1006 * expires, the callback function is called.
1008 * RETURNS
1009 * nonzero on success or zero on faillure
1011 * BUGS
1012 * Unimplemented
1014 BOOL WINAPI CreateTimerQueueTimer( PHANDLE phNewTimer, HANDLE TimerQueue,
1015 WAITORTIMERCALLBACK Callback, PVOID Parameter,
1016 DWORD DueTime, DWORD Period, ULONG Flags )
1018 FIXME("stub\n");
1019 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1020 return TRUE;
1023 /***********************************************************************
1024 * DeleteTimerQueueTimer (KERNEL32.@)
1026 * Cancels a timer-queue timer.
1028 * RETURNS
1029 * nonzero on success or zero on faillure
1031 * BUGS
1032 * Unimplemented
1034 BOOL WINAPI DeleteTimerQueueTimer( HANDLE TimerQueue, HANDLE Timer,
1035 HANDLE CompletionEvent )
1037 FIXME("stub\n");
1038 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1039 return TRUE;
1044 * Pipes
1048 /***********************************************************************
1049 * CreateNamedPipeA (KERNEL32.@)
1051 HANDLE WINAPI CreateNamedPipeA( LPCSTR name, DWORD dwOpenMode,
1052 DWORD dwPipeMode, DWORD nMaxInstances,
1053 DWORD nOutBufferSize, DWORD nInBufferSize,
1054 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES attr )
1056 WCHAR buffer[MAX_PATH];
1058 if (!name) return CreateNamedPipeW( NULL, dwOpenMode, dwPipeMode, nMaxInstances,
1059 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1061 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1063 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1064 return INVALID_HANDLE_VALUE;
1066 return CreateNamedPipeW( buffer, dwOpenMode, dwPipeMode, nMaxInstances,
1067 nOutBufferSize, nInBufferSize, nDefaultTimeOut, attr );
1071 /***********************************************************************
1072 * CreateNamedPipeW (KERNEL32.@)
1074 HANDLE WINAPI CreateNamedPipeW( LPCWSTR name, DWORD dwOpenMode,
1075 DWORD dwPipeMode, DWORD nMaxInstances,
1076 DWORD nOutBufferSize, DWORD nInBufferSize,
1077 DWORD nDefaultTimeOut, LPSECURITY_ATTRIBUTES sa )
1079 HANDLE handle;
1080 UNICODE_STRING nt_name;
1081 OBJECT_ATTRIBUTES attr;
1082 DWORD options;
1083 BOOLEAN pipe_type, read_mode, non_block;
1084 NTSTATUS status;
1085 IO_STATUS_BLOCK iosb;
1086 LARGE_INTEGER timeout;
1088 TRACE("(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n",
1089 debugstr_w(name), dwOpenMode, dwPipeMode, nMaxInstances,
1090 nOutBufferSize, nInBufferSize, nDefaultTimeOut, sa );
1092 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1094 SetLastError( ERROR_PATH_NOT_FOUND );
1095 return INVALID_HANDLE_VALUE;
1097 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1099 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1100 RtlFreeUnicodeString( &nt_name );
1101 return INVALID_HANDLE_VALUE;
1104 attr.Length = sizeof(attr);
1105 attr.RootDirectory = 0;
1106 attr.ObjectName = &nt_name;
1107 attr.Attributes = (sa && sa->bInheritHandle) ? OBJ_INHERIT : 0;
1108 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1109 attr.SecurityQualityOfService = NULL;
1111 options = 0;
1112 if (dwOpenMode & FILE_FLAG_WRITE_THROUGH) options |= FILE_WRITE_THROUGH;
1113 if (!(dwOpenMode & FILE_FLAG_OVERLAPPED)) options |= FILE_SYNCHRONOUS_IO_ALERT;
1114 if ((dwOpenMode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
1115 options |= FILE_PIPE_FULL_DUPLEX;
1116 else if (dwOpenMode & PIPE_ACCESS_INBOUND) options |= FILE_PIPE_INBOUND;
1117 else if (dwOpenMode & PIPE_ACCESS_OUTBOUND) options |= FILE_PIPE_OUTBOUND;
1118 pipe_type = (dwPipeMode & PIPE_TYPE_MESSAGE) ? TRUE : FALSE;
1119 read_mode = (dwPipeMode & PIPE_READMODE_MESSAGE) ? TRUE : FALSE;
1120 non_block = (dwPipeMode & PIPE_NOWAIT) ? TRUE : FALSE;
1121 if (nMaxInstances >= PIPE_UNLIMITED_INSTANCES) nMaxInstances = ~0UL;
1123 timeout.QuadPart = (ULONGLONG)nDefaultTimeOut * -10000;
1125 SetLastError(0);
1127 status = NtCreateNamedPipeFile(&handle, 0, &attr, &iosb, 0, FILE_OVERWRITE_IF,
1128 options, pipe_type, read_mode, non_block,
1129 nMaxInstances, nInBufferSize, nOutBufferSize,
1130 &timeout);
1132 RtlFreeUnicodeString( &nt_name );
1133 if (status)
1135 handle = INVALID_HANDLE_VALUE;
1136 SetLastError( RtlNtStatusToDosError(status) );
1138 return handle;
1142 /***********************************************************************
1143 * PeekNamedPipe (KERNEL32.@)
1145 BOOL WINAPI PeekNamedPipe( HANDLE hPipe, LPVOID lpvBuffer, DWORD cbBuffer,
1146 LPDWORD lpcbRead, LPDWORD lpcbAvail, LPDWORD lpcbMessage )
1148 #ifdef FIONREAD
1149 int avail=0, fd, ret, flags;
1151 TRACE("(%p,%p,%lu,%p,%p,%p)\n", hPipe, lpvBuffer, cbBuffer, lpcbRead, lpcbAvail, lpcbMessage);
1153 ret = wine_server_handle_to_fd( hPipe, GENERIC_READ, &fd, &flags );
1154 if (ret)
1156 SetLastError( RtlNtStatusToDosError(ret) );
1157 return FALSE;
1159 if (flags & FD_FLAG_RECV_SHUTDOWN)
1161 wine_server_release_fd( hPipe, fd );
1162 SetLastError ( ERROR_PIPE_NOT_CONNECTED );
1163 return FALSE;
1166 if (ioctl(fd,FIONREAD, &avail ) != 0)
1168 TRACE("FIONREAD failed reason: %s\n",strerror(errno));
1169 wine_server_release_fd( hPipe, fd );
1170 return FALSE;
1172 if (!avail) /* check for closed pipe */
1174 struct pollfd pollfd;
1175 pollfd.fd = fd;
1176 pollfd.events = POLLIN;
1177 pollfd.revents = 0;
1178 switch (poll( &pollfd, 1, 0 ))
1180 case 0:
1181 break;
1182 case 1: /* got something */
1183 if (!(pollfd.revents & (POLLHUP | POLLERR))) break;
1184 TRACE("POLLHUP | POLLERR\n");
1185 /* fall through */
1186 case -1:
1187 wine_server_release_fd( hPipe, fd );
1188 SetLastError(ERROR_BROKEN_PIPE);
1189 return FALSE;
1192 TRACE(" 0x%08x bytes available\n", avail );
1193 ret = TRUE;
1194 if (lpcbAvail)
1195 *lpcbAvail = avail;
1196 if (lpcbRead)
1197 *lpcbRead = 0;
1198 if (avail && lpvBuffer && cbBuffer)
1200 int readbytes = (avail < cbBuffer) ? avail : cbBuffer;
1201 readbytes = recv(fd, lpvBuffer, readbytes, MSG_PEEK);
1202 if (readbytes < 0)
1204 WARN("failed to peek socket (%d)\n", errno);
1205 ret = FALSE;
1207 else if (lpcbRead)
1208 *lpcbRead = readbytes;
1210 wine_server_release_fd( hPipe, fd );
1211 return ret;
1212 #endif /* defined(FIONREAD) */
1214 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1215 FIXME("function not implemented\n");
1216 return FALSE;
1219 /***********************************************************************
1220 * PIPE_CompletionWait (Internal)
1222 static void CALLBACK PIPE_CompletionWait(void *user, PIO_STATUS_BLOCK iosb, ULONG status)
1224 LPOVERLAPPED ovlp = (LPOVERLAPPED)user;
1226 TRACE("for %p/%p, status=%08lx\n", ovlp, iosb, status);
1228 if (ovlp)
1230 ovlp->Internal = status;
1231 SetEvent(ovlp->hEvent);
1233 TRACE("done\n");
1236 /***********************************************************************
1237 * WaitNamedPipeA (KERNEL32.@)
1239 BOOL WINAPI WaitNamedPipeA (LPCSTR name, DWORD nTimeOut)
1241 WCHAR buffer[MAX_PATH];
1243 if (!name) return WaitNamedPipeW( NULL, nTimeOut );
1245 if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
1247 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1248 return 0;
1250 return WaitNamedPipeW( buffer, nTimeOut );
1254 /***********************************************************************
1255 * WaitNamedPipeW (KERNEL32.@)
1257 BOOL WINAPI WaitNamedPipeW (LPCWSTR name, DWORD nTimeOut)
1259 BOOL ret;
1260 OVERLAPPED ov;
1261 UNICODE_STRING nt_name;
1262 static const WCHAR leadin[] = {'\\','?','?','\\','P','I','P','E','\\'};
1264 TRACE("%s 0x%08lx\n",debugstr_w(name),nTimeOut);
1266 if (!RtlDosPathNameToNtPathName_U( name, &nt_name, NULL, NULL ))
1267 return FALSE;
1269 if (nt_name.Length >= MAX_PATH * sizeof(WCHAR) )
1271 RtlFreeUnicodeString( &nt_name );
1272 return FALSE;
1274 if (nt_name.Length < sizeof(leadin) ||
1275 strncmpiW( nt_name.Buffer, leadin, sizeof(leadin)/sizeof(leadin[0])))
1277 RtlFreeUnicodeString( &nt_name );
1278 return FALSE;
1281 memset(&ov,0,sizeof(ov));
1282 ov.hEvent = CreateEventW( NULL, 0, 0, NULL );
1283 if (!ov.hEvent)
1284 return FALSE;
1286 SERVER_START_REQ( wait_named_pipe )
1288 req->timeout = nTimeOut;
1289 req->overlapped = &ov;
1290 req->func = PIPE_CompletionWait;
1291 wine_server_add_data( req, nt_name.Buffer + 4, nt_name.Length - 4*sizeof(WCHAR) );
1292 ret = !wine_server_call_err( req );
1294 SERVER_END_REQ;
1296 RtlFreeUnicodeString( &nt_name );
1298 if(ret)
1300 if (WAIT_OBJECT_0==WaitForSingleObject(ov.hEvent,INFINITE))
1302 SetLastError(RtlNtStatusToDosError(ov.Internal));
1303 ret = (ov.Internal==STATUS_SUCCESS);
1306 CloseHandle(ov.hEvent);
1307 return ret;
1311 /***********************************************************************
1312 * ConnectNamedPipe (KERNEL32.@)
1314 BOOL WINAPI ConnectNamedPipe(HANDLE hPipe, LPOVERLAPPED overlapped)
1316 BOOL ret;
1317 LPOVERLAPPED pov;
1318 OVERLAPPED ov;
1320 TRACE("(%p,%p)\n", hPipe, overlapped);
1322 if (!overlapped)
1324 memset(&ov, 0, sizeof(ov));
1325 ov.hEvent = CreateEventW(NULL, 0, 0, NULL);
1326 if (!ov.hEvent) return FALSE;
1327 pov = &ov;
1329 else pov = overlapped;
1331 pov->Internal = STATUS_PENDING;
1333 SERVER_START_REQ( connect_named_pipe )
1335 req->handle = hPipe;
1336 req->overlapped = pov;
1337 req->func = PIPE_CompletionWait;
1338 ret = !wine_server_call_err( req );
1340 SERVER_END_REQ;
1342 if (ret)
1344 if (overlapped)
1346 SetLastError( ERROR_IO_PENDING );
1347 ret = FALSE;
1349 else
1351 ret = GetOverlappedResult(hPipe, &ov, NULL, TRUE);
1352 CloseHandle(ov.hEvent);
1356 return ret;
1359 /***********************************************************************
1360 * DisconnectNamedPipe (KERNEL32.@)
1362 * Disconnects from a named pipe
1364 * Parameters
1365 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1367 * Return values
1368 * TRUE: Success
1369 * FALSE: Failure, GetLastError can be called for further details
1371 BOOL WINAPI DisconnectNamedPipe(HANDLE hPipe)
1373 NTSTATUS status;
1374 IO_STATUS_BLOCK io_block;
1376 TRACE("(%p)\n",hPipe);
1378 status = NtFsControlFile(hPipe, 0, NULL, NULL, &io_block, FSCTL_PIPE_DISCONNECT,
1379 NULL, 0, NULL, 0);
1380 if (status == STATUS_SUCCESS) return TRUE;
1381 SetLastError( RtlNtStatusToDosError(status) );
1382 return FALSE;
1385 /***********************************************************************
1386 * TransactNamedPipe (KERNEL32.@)
1388 * BUGS
1389 * should be done as a single operation in the wineserver or kernel
1391 BOOL WINAPI TransactNamedPipe(
1392 HANDLE handle, LPVOID lpInput, DWORD dwInputSize, LPVOID lpOutput,
1393 DWORD dwOutputSize, LPDWORD lpBytesRead, LPOVERLAPPED lpOverlapped)
1395 BOOL r;
1396 DWORD count;
1398 TRACE("%p %p %ld %p %ld %p %p\n",
1399 handle, lpInput, dwInputSize, lpOutput,
1400 dwOutputSize, lpBytesRead, lpOverlapped);
1402 if (lpOverlapped)
1404 FIXME("Doesn't support overlapped operation as yet\n");
1405 return FALSE;
1408 r = WriteFile(handle, lpOutput, dwOutputSize, &count, NULL);
1409 if (r)
1410 r = ReadFile(handle, lpInput, dwInputSize, lpBytesRead, NULL);
1412 return r;
1415 /***********************************************************************
1416 * GetNamedPipeInfo (KERNEL32.@)
1418 BOOL WINAPI GetNamedPipeInfo(
1419 HANDLE hNamedPipe, LPDWORD lpFlags, LPDWORD lpOutputBufferSize,
1420 LPDWORD lpInputBufferSize, LPDWORD lpMaxInstances)
1422 BOOL ret;
1424 TRACE("%p %p %p %p %p\n", hNamedPipe, lpFlags,
1425 lpOutputBufferSize, lpInputBufferSize, lpMaxInstances);
1427 SERVER_START_REQ( get_named_pipe_info )
1429 req->handle = hNamedPipe;
1430 ret = !wine_server_call_err( req );
1431 if (lpFlags)
1433 *lpFlags = 0;
1434 if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)
1435 *lpFlags |= PIPE_TYPE_MESSAGE;
1436 if (reply->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
1437 *lpFlags |= PIPE_READMODE_MESSAGE;
1438 if (reply->flags & NAMED_PIPE_NONBLOCKING_MODE)
1439 *lpFlags |= PIPE_NOWAIT;
1441 if (lpOutputBufferSize) *lpOutputBufferSize = reply->outsize;
1442 if (lpInputBufferSize) *lpInputBufferSize = reply->outsize;
1443 if (lpMaxInstances) *lpMaxInstances = reply->maxinstances;
1445 SERVER_END_REQ;
1447 return ret;
1450 /***********************************************************************
1451 * GetNamedPipeHandleStateA (KERNEL32.@)
1453 BOOL WINAPI GetNamedPipeHandleStateA(
1454 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1455 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1456 LPSTR lpUsername, DWORD nUsernameMaxSize)
1458 FIXME("%p %p %p %p %p %p %ld\n",
1459 hNamedPipe, lpState, lpCurInstances,
1460 lpMaxCollectionCount, lpCollectDataTimeout,
1461 lpUsername, nUsernameMaxSize);
1463 return FALSE;
1466 /***********************************************************************
1467 * GetNamedPipeHandleStateW (KERNEL32.@)
1469 BOOL WINAPI GetNamedPipeHandleStateW(
1470 HANDLE hNamedPipe, LPDWORD lpState, LPDWORD lpCurInstances,
1471 LPDWORD lpMaxCollectionCount, LPDWORD lpCollectDataTimeout,
1472 LPWSTR lpUsername, DWORD nUsernameMaxSize)
1474 FIXME("%p %p %p %p %p %p %ld\n",
1475 hNamedPipe, lpState, lpCurInstances,
1476 lpMaxCollectionCount, lpCollectDataTimeout,
1477 lpUsername, nUsernameMaxSize);
1479 return FALSE;
1482 /***********************************************************************
1483 * SetNamedPipeHandleState (KERNEL32.@)
1485 BOOL WINAPI SetNamedPipeHandleState(
1486 HANDLE hNamedPipe, LPDWORD lpMode, LPDWORD lpMaxCollectionCount,
1487 LPDWORD lpCollectDataTimeout)
1489 /* should be a fixme, but this function is called a lot by the RPC
1490 * runtime, and it slows down InstallShield a fair bit. */
1491 WARN("stub: %p %p/%ld %p %p\n",
1492 hNamedPipe, lpMode, lpMode ? *lpMode : 0, lpMaxCollectionCount, lpCollectDataTimeout);
1493 return FALSE;
1496 /***********************************************************************
1497 * CallNamedPipeA (KERNEL32.@)
1499 BOOL WINAPI CallNamedPipeA(
1500 LPCSTR lpNamedPipeName, LPVOID lpInput, DWORD dwInputSize,
1501 LPVOID lpOutput, DWORD dwOutputSize,
1502 LPDWORD lpBytesRead, DWORD nTimeout)
1504 DWORD len;
1505 LPWSTR str = NULL;
1506 BOOL ret;
1508 TRACE("%s %p %ld %p %ld %p %ld\n",
1509 debugstr_a(lpNamedPipeName), lpInput, dwInputSize,
1510 lpOutput, dwOutputSize, lpBytesRead, nTimeout);
1512 if( lpNamedPipeName )
1514 len = MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, NULL, 0 );
1515 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1516 MultiByteToWideChar( CP_ACP, 0, lpNamedPipeName, -1, str, len );
1518 ret = CallNamedPipeW( str, lpInput, dwInputSize, lpOutput,
1519 dwOutputSize, lpBytesRead, nTimeout );
1520 if( lpNamedPipeName )
1521 HeapFree( GetProcessHeap(), 0, str );
1523 return ret;
1526 /***********************************************************************
1527 * CallNamedPipeW (KERNEL32.@)
1529 BOOL WINAPI CallNamedPipeW(
1530 LPCWSTR lpNamedPipeName, LPVOID lpInput, DWORD lpInputSize,
1531 LPVOID lpOutput, DWORD lpOutputSize,
1532 LPDWORD lpBytesRead, DWORD nTimeout)
1534 FIXME("%s %p %ld %p %ld %p %ld\n",
1535 debugstr_w(lpNamedPipeName), lpInput, lpInputSize,
1536 lpOutput, lpOutputSize, lpBytesRead, nTimeout);
1537 return FALSE;
1540 /******************************************************************
1541 * CreatePipe (KERNEL32.@)
1544 BOOL WINAPI CreatePipe( PHANDLE hReadPipe, PHANDLE hWritePipe,
1545 LPSECURITY_ATTRIBUTES sa, DWORD size )
1547 static unsigned index = 0;
1548 WCHAR name[64];
1549 HANDLE hr, hw;
1550 unsigned in_index = index;
1552 *hReadPipe = *hWritePipe = INVALID_HANDLE_VALUE;
1553 /* generate a unique pipe name (system wide) */
1556 static const WCHAR nameFmt[] = { '\\','\\','.','\\','p','i','p','e',
1557 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1558 'u','.','%','0','8','u','\0' };
1559 snprintfW(name, sizeof(name) / sizeof(name[0]), nameFmt,
1560 GetCurrentProcessId(), ++index);
1561 hr = CreateNamedPipeW(name, PIPE_ACCESS_INBOUND,
1562 PIPE_TYPE_BYTE | PIPE_WAIT, 1, size, size,
1563 NMPWAIT_USE_DEFAULT_WAIT, sa);
1564 } while (hr == INVALID_HANDLE_VALUE && index != in_index);
1565 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1566 if (hr == INVALID_HANDLE_VALUE) return FALSE;
1568 hw = CreateFileW(name, GENERIC_WRITE, 0, sa, OPEN_EXISTING, 0, 0);
1569 if (hw == INVALID_HANDLE_VALUE)
1571 CloseHandle(hr);
1572 return FALSE;
1575 *hReadPipe = hr;
1576 *hWritePipe = hw;
1577 return TRUE;
1581 /******************************************************************************
1582 * CreateMailslotA [KERNEL32.@]
1584 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
1585 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1587 DWORD len;
1588 HANDLE handle;
1589 LPWSTR name = NULL;
1591 TRACE("%s %ld %ld %p\n", debugstr_a(lpName),
1592 nMaxMessageSize, lReadTimeout, sa);
1594 if( lpName )
1596 len = MultiByteToWideChar( CP_ACP, 0, lpName, -1, NULL, 0 );
1597 name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1598 MultiByteToWideChar( CP_ACP, 0, lpName, -1, name, len );
1601 handle = CreateMailslotW( name, nMaxMessageSize, lReadTimeout, sa );
1603 HeapFree( GetProcessHeap(), 0, name );
1605 return handle;
1609 /******************************************************************************
1610 * CreateMailslotW [KERNEL32.@]
1612 * Create a mailslot with specified name.
1614 * PARAMS
1615 * lpName [I] Pointer to string for mailslot name
1616 * nMaxMessageSize [I] Maximum message size
1617 * lReadTimeout [I] Milliseconds before read time-out
1618 * sa [I] Pointer to security structure
1620 * RETURNS
1621 * Success: Handle to mailslot
1622 * Failure: INVALID_HANDLE_VALUE
1624 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
1625 DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
1627 HANDLE handle = INVALID_HANDLE_VALUE;
1628 OBJECT_ATTRIBUTES attr;
1629 UNICODE_STRING nameW;
1630 LARGE_INTEGER timeout;
1631 IO_STATUS_BLOCK iosb;
1632 NTSTATUS status;
1634 TRACE("%s %ld %ld %p\n", debugstr_w(lpName),
1635 nMaxMessageSize, lReadTimeout, sa);
1637 if (!RtlDosPathNameToNtPathName_U( lpName, &nameW, NULL, NULL ))
1639 SetLastError( ERROR_PATH_NOT_FOUND );
1640 return INVALID_HANDLE_VALUE;
1643 if (nameW.Length >= MAX_PATH * sizeof(WCHAR) )
1645 SetLastError( ERROR_FILENAME_EXCED_RANGE );
1646 RtlFreeUnicodeString( &nameW );
1647 return INVALID_HANDLE_VALUE;
1650 attr.Length = sizeof(attr);
1651 attr.RootDirectory = 0;
1652 attr.Attributes = OBJ_CASE_INSENSITIVE;
1653 attr.ObjectName = &nameW;
1654 attr.SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
1655 attr.SecurityQualityOfService = NULL;
1657 timeout.QuadPart = (ULONGLONG) lReadTimeout * -10000;
1659 status = NtCreateMailslotFile( &handle, GENERIC_READ | GENERIC_WRITE, &attr,
1660 &iosb, 0, 0, nMaxMessageSize, &timeout );
1661 if (status)
1663 SetLastError( RtlNtStatusToDosError(status) );
1664 handle = INVALID_HANDLE_VALUE;
1667 RtlFreeUnicodeString( &nameW );
1668 return handle;
1672 /******************************************************************************
1673 * GetMailslotInfo [KERNEL32.@]
1675 * Retrieve information about a mailslot.
1677 * PARAMS
1678 * hMailslot [I] Mailslot handle
1679 * lpMaxMessageSize [O] Address of maximum message size
1680 * lpNextSize [O] Address of size of next message
1681 * lpMessageCount [O] Address of number of messages
1682 * lpReadTimeout [O] Address of read time-out
1684 * RETURNS
1685 * Success: TRUE
1686 * Failure: FALSE
1688 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
1689 LPDWORD lpNextSize, LPDWORD lpMessageCount,
1690 LPDWORD lpReadTimeout )
1692 FILE_MAILSLOT_QUERY_INFORMATION info;
1693 IO_STATUS_BLOCK iosb;
1694 NTSTATUS status;
1696 TRACE("%p %p %p %p %p\n",hMailslot, lpMaxMessageSize,
1697 lpNextSize, lpMessageCount, lpReadTimeout);
1699 status = NtQueryInformationFile( hMailslot, &iosb, &info, sizeof info,
1700 FileMailslotQueryInformation );
1702 if( status != STATUS_SUCCESS )
1704 SetLastError( RtlNtStatusToDosError(status) );
1705 return FALSE;
1708 if( lpMaxMessageSize )
1709 *lpMaxMessageSize = info.MaximumMessageSize;
1710 if( lpNextSize )
1711 *lpNextSize = info.NextMessageSize;
1712 if( lpMessageCount )
1713 *lpMessageCount = info.MessagesAvailable;
1714 if( lpReadTimeout )
1715 *lpReadTimeout = info.ReadTimeout.QuadPart / -10000;
1717 return TRUE;
1721 /******************************************************************************
1722 * SetMailslotInfo [KERNEL32.@]
1724 * Set the read timeout of a mailslot.
1726 * PARAMS
1727 * hMailslot [I] Mailslot handle
1728 * dwReadTimeout [I] Timeout in milliseconds.
1730 * RETURNS
1731 * Success: TRUE
1732 * Failure: FALSE
1734 BOOL WINAPI SetMailslotInfo( HANDLE hMailslot, DWORD dwReadTimeout)
1736 FILE_MAILSLOT_SET_INFORMATION info;
1737 IO_STATUS_BLOCK iosb;
1738 NTSTATUS status;
1740 TRACE("%p %ld\n", hMailslot, dwReadTimeout);
1742 info.ReadTimeout.QuadPart = dwReadTimeout * -10000;
1743 status = NtSetInformationFile( hMailslot, &iosb, &info, sizeof info,
1744 FileMailslotSetInformation );
1745 if( status != STATUS_SUCCESS )
1747 SetLastError( RtlNtStatusToDosError(status) );
1748 return FALSE;
1750 return TRUE;
1754 /******************************************************************************
1755 * CreateIoCompletionPort (KERNEL32.@)
1757 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle, HANDLE hExistingCompletionPort,
1758 ULONG_PTR CompletionKey, DWORD dwNumberOfConcurrentThreads)
1760 FIXME("(%p, %p, %08lx, %08lx): stub.\n",
1761 hFileHandle, hExistingCompletionPort, CompletionKey, dwNumberOfConcurrentThreads);
1762 return NULL;
1766 /******************************************************************************
1767 * GetQueuedCompletionStatus (KERNEL32.@)
1769 BOOL WINAPI GetQueuedCompletionStatus( HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
1770 PULONG_PTR pCompletionKey, LPOVERLAPPED *lpOverlapped,
1771 DWORD dwMilliseconds )
1773 FIXME("(%p,%p,%p,%p,%ld), stub!\n",
1774 CompletionPort,lpNumberOfBytesTransferred,pCompletionKey,lpOverlapped,dwMilliseconds);
1775 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1776 return FALSE;
1779 /******************************************************************************
1780 * CreateJobObjectW (KERNEL32.@)
1782 HANDLE WINAPI CreateJobObjectW( LPSECURITY_ATTRIBUTES attr, LPCWSTR name )
1784 FIXME("%p %s\n", attr, debugstr_w(name) );
1785 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
1786 return 0;
1789 /******************************************************************************
1790 * CreateJobObjectA (KERNEL32.@)
1792 HANDLE WINAPI CreateJobObjectA( LPSECURITY_ATTRIBUTES attr, LPCSTR name )
1794 LPWSTR str = NULL;
1795 UINT len;
1796 HANDLE r;
1798 TRACE("%p %s\n", attr, debugstr_a(name) );
1800 if( name )
1802 len = MultiByteToWideChar( CP_ACP, 0, name, -1, NULL, 0 );
1803 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1804 if( !str )
1806 SetLastError( ERROR_OUTOFMEMORY );
1807 return 0;
1809 len = MultiByteToWideChar( CP_ACP, 0, name, -1, str, len );
1812 r = CreateJobObjectW( attr, str );
1814 HeapFree( GetProcessHeap(), 0, str );
1816 return r;
1819 /******************************************************************************
1820 * AssignProcessToJobObject (KERNEL32.@)
1822 BOOL WINAPI AssignProcessToJobObject( HANDLE hJob, HANDLE hProcess )
1824 FIXME("%p %p\n", hJob, hProcess);
1825 return TRUE;
1828 #ifdef __i386__
1830 /***********************************************************************
1831 * InterlockedCompareExchange (KERNEL32.@)
1833 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1834 __ASM_GLOBAL_FUNC(InterlockedCompareExchange,
1835 "movl 12(%esp),%eax\n\t"
1836 "movl 8(%esp),%ecx\n\t"
1837 "movl 4(%esp),%edx\n\t"
1838 "lock; cmpxchgl %ecx,(%edx)\n\t"
1839 "ret $12")
1841 /***********************************************************************
1842 * InterlockedExchange (KERNEL32.@)
1844 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1845 __ASM_GLOBAL_FUNC(InterlockedExchange,
1846 "movl 8(%esp),%eax\n\t"
1847 "movl 4(%esp),%edx\n\t"
1848 "lock; xchgl %eax,(%edx)\n\t"
1849 "ret $8")
1851 /***********************************************************************
1852 * InterlockedExchangeAdd (KERNEL32.@)
1854 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1855 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd,
1856 "movl 8(%esp),%eax\n\t"
1857 "movl 4(%esp),%edx\n\t"
1858 "lock; xaddl %eax,(%edx)\n\t"
1859 "ret $8")
1861 /***********************************************************************
1862 * InterlockedIncrement (KERNEL32.@)
1864 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1865 __ASM_GLOBAL_FUNC(InterlockedIncrement,
1866 "movl 4(%esp),%edx\n\t"
1867 "movl $1,%eax\n\t"
1868 "lock; xaddl %eax,(%edx)\n\t"
1869 "incl %eax\n\t"
1870 "ret $4")
1872 /***********************************************************************
1873 * InterlockedDecrement (KERNEL32.@)
1875 __ASM_GLOBAL_FUNC(InterlockedDecrement,
1876 "movl 4(%esp),%edx\n\t"
1877 "movl $-1,%eax\n\t"
1878 "lock; xaddl %eax,(%edx)\n\t"
1879 "decl %eax\n\t"
1880 "ret $4")
1882 #else /* __i386__ */
1884 /***********************************************************************
1885 * InterlockedCompareExchange (KERNEL32.@)
1887 * Atomically swap one value with another.
1889 * PARAMS
1890 * dest [I/O] The value to replace
1891 * xchq [I] The value to be swapped
1892 * compare [I] The value to compare to dest
1894 * RETURNS
1895 * The resulting value of dest.
1897 * NOTES
1898 * dest is updated only if it is equal to compare, otherwise no swap is done.
1900 LONG WINAPI InterlockedCompareExchange( LONG volatile *dest, LONG xchg, LONG compare )
1902 return interlocked_cmpxchg( dest, xchg, compare );
1905 /***********************************************************************
1906 * InterlockedExchange (KERNEL32.@)
1908 * Atomically swap one value with another.
1910 * PARAMS
1911 * dest [I/O] The value to replace
1912 * val [I] The value to be swapped
1914 * RETURNS
1915 * The resulting value of dest.
1917 LONG WINAPI InterlockedExchange( LONG volatile *dest, LONG val )
1919 return interlocked_xchg( dest, val );
1922 /***********************************************************************
1923 * InterlockedExchangeAdd (KERNEL32.@)
1925 * Atomically add one value to another.
1927 * PARAMS
1928 * dest [I/O] The value to add to
1929 * incr [I] The value to be added
1931 * RETURNS
1932 * The resulting value of dest.
1934 LONG WINAPI InterlockedExchangeAdd( LONG volatile *dest, LONG incr )
1936 return interlocked_xchg_add( dest, incr );
1939 /***********************************************************************
1940 * InterlockedIncrement (KERNEL32.@)
1942 * Atomically increment a value.
1944 * PARAMS
1945 * dest [I/O] The value to increment
1947 * RETURNS
1948 * The resulting value of dest.
1950 LONG WINAPI InterlockedIncrement( LONG volatile *dest )
1952 return interlocked_xchg_add( dest, 1 ) + 1;
1955 /***********************************************************************
1956 * InterlockedDecrement (KERNEL32.@)
1958 * Atomically decrement a value.
1960 * PARAMS
1961 * dest [I/O] The value to decrement
1963 * RETURNS
1964 * The resulting value of dest.
1966 LONG WINAPI InterlockedDecrement( LONG volatile *dest )
1968 return interlocked_xchg_add( dest, -1 ) - 1;
1971 #endif /* __i386__ */