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
22 #include "wine/port.h"
29 #ifdef HAVE_SYS_IOCTL_H
30 #include <sys/ioctl.h>
35 #ifdef HAVE_SYS_POLL_H
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
44 #define NONAMELESSUNION
45 #define NONAMELESSSTRUCT
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 /***********************************************************************
74 VOID WINAPI
Sleep( DWORD timeout
)
76 SleepEx( timeout
, FALSE
);
79 /******************************************************************************
80 * SleepEx (KERNEL32.@)
82 DWORD WINAPI
SleepEx( DWORD timeout
, BOOL alertable
)
86 if (timeout
== INFINITE
) status
= NtDelayExecution( alertable
, NULL
);
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
;
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
,
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
,
146 HANDLE hloc
[MAXIMUM_WAIT_OBJECTS
];
149 if (count
>= MAXIMUM_WAIT_OBJECTS
)
151 SetLastError(ERROR_INVALID_PARAMETER
);
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
] );
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
]))
172 hloc
[i
] = GetConsoleInputWaitHandle();
176 if (timeout
== INFINITE
)
178 status
= NtWaitForMultipleObjects( count
, hloc
, wait_all
, alertable
, NULL
);
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
;
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
);
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
);
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
);
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
);
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
);
263 /***********************************************************************
264 * UnregisterWait (KERNEL32.@)
266 BOOL WINAPI
UnregisterWait( HANDLE WaitHandle
)
268 FIXME("%p\n",WaitHandle
);
272 /***********************************************************************
273 * UnregisterWaitEx (KERNEL32.@)
275 BOOL WINAPI
UnregisterWaitEx( HANDLE WaitHandle
, HANDLE CompletionEvent
)
277 FIXME("%p %p\n",WaitHandle
, CompletionEvent
);
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
)
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
;
303 status
= NtSignalAndWaitForSingleObject( hObjectToSignal
, hObjectToWaitOn
,
304 bAlertable
, ptimeout
);
307 SetLastError( RtlNtStatusToDosError(status
) );
308 status
= WAIT_FAILED
;
313 /***********************************************************************
314 * InitializeCriticalSection (KERNEL32.@)
316 * Initialise a critical section before use.
319 * crit [O] Critical section to initialise.
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.
336 * crit [O] Critical section to initialise.
337 * spincount [I] Number of times to spin upon contention.
341 * Failure: Nothing. If the function fails an exception is raised.
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
);
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
);
364 RtlFreeHeap( GetProcessHeap(), 0, crit
->DebugInfo
);
365 crit
->DebugInfo
= NULL
;
370 /***********************************************************************
371 * ReinitializeCriticalSection (KERNEL32.@)
373 * Initialise an already used critical section.
376 * crit [O] Critical section to initialise.
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.
394 * crit [O] Critical section to uninitialise (destroy).
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
);
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
)
431 UNICODE_STRING nameW
;
432 OBJECT_ATTRIBUTES attr
;
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
);
445 attr
.Length
= sizeof(attr
);
446 attr
.RootDirectory
= 0;
447 attr
.ObjectName
= NULL
;
448 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
449 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
450 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
451 attr
.SecurityQualityOfService
= NULL
;
454 RtlInitUnicodeString( &nameW
, name
);
455 attr
.ObjectName
= &nameW
;
458 status
= NtCreateEvent( &ret
, EVENT_ALL_ACCESS
, &attr
, manual_reset
, initial_state
);
459 SetLastError( RtlNtStatusToDosError(status
) );
464 /***********************************************************************
465 * CreateW32Event (KERNEL.457)
467 HANDLE WINAPI
WIN16_CreateEvent( BOOL manual_reset
, BOOL initial_state
)
469 return CreateEventW( NULL
, manual_reset
, initial_state
, NULL
);
473 /***********************************************************************
474 * OpenEventA (KERNEL32.@)
476 HANDLE WINAPI
OpenEventA( DWORD access
, BOOL inherit
, LPCSTR name
)
478 WCHAR buffer
[MAX_PATH
];
480 if (!name
) return OpenEventW( access
, inherit
, NULL
);
482 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
484 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
487 return OpenEventW( access
, inherit
, buffer
);
491 /***********************************************************************
492 * OpenEventW (KERNEL32.@)
494 HANDLE WINAPI
OpenEventW( DWORD access
, BOOL inherit
, LPCWSTR name
)
497 UNICODE_STRING nameW
;
498 OBJECT_ATTRIBUTES attr
;
501 if (!is_version_nt()) access
= EVENT_ALL_ACCESS
;
503 attr
.Length
= sizeof(attr
);
504 attr
.RootDirectory
= 0;
505 attr
.ObjectName
= NULL
;
506 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| (inherit
? OBJ_INHERIT
: 0);
507 attr
.SecurityDescriptor
= NULL
;
508 attr
.SecurityQualityOfService
= NULL
;
511 RtlInitUnicodeString( &nameW
, name
);
512 attr
.ObjectName
= &nameW
;
515 status
= NtOpenEvent( &ret
, access
, &attr
);
516 if (status
!= STATUS_SUCCESS
)
518 SetLastError( RtlNtStatusToDosError(status
) );
524 /***********************************************************************
525 * PulseEvent (KERNEL32.@)
527 BOOL WINAPI
PulseEvent( HANDLE handle
)
531 if ((status
= NtPulseEvent( handle
, NULL
)))
532 SetLastError( RtlNtStatusToDosError(status
) );
537 /***********************************************************************
538 * SetW32Event (KERNEL.458)
539 * SetEvent (KERNEL32.@)
541 BOOL WINAPI
SetEvent( HANDLE handle
)
545 if ((status
= NtSetEvent( handle
, NULL
)))
546 SetLastError( RtlNtStatusToDosError(status
) );
551 /***********************************************************************
552 * ResetW32Event (KERNEL.459)
553 * ResetEvent (KERNEL32.@)
555 BOOL WINAPI
ResetEvent( HANDLE handle
)
559 if ((status
= NtResetEvent( handle
, NULL
)))
560 SetLastError( RtlNtStatusToDosError(status
) );
565 /***********************************************************************
566 * NOTE: The Win95 VWin32_Event routines given below are really low-level
567 * routines implemented directly by VWin32. The user-mode libraries
568 * implement Win32 synchronisation routines on top of these low-level
569 * primitives. We do it the other way around here :-)
572 /***********************************************************************
573 * VWin32_EventCreate (KERNEL.442)
575 HANDLE WINAPI
VWin32_EventCreate(VOID
)
577 HANDLE hEvent
= CreateEventW( NULL
, FALSE
, 0, NULL
);
578 return ConvertToGlobalHandle( hEvent
);
581 /***********************************************************************
582 * VWin32_EventDestroy (KERNEL.443)
584 VOID WINAPI
VWin32_EventDestroy(HANDLE event
)
586 CloseHandle( event
);
589 /***********************************************************************
590 * VWin32_EventWait (KERNEL.450)
592 VOID WINAPI
VWin32_EventWait(HANDLE event
)
596 ReleaseThunkLock( &mutex_count
);
597 WaitForSingleObject( event
, INFINITE
);
598 RestoreThunkLock( mutex_count
);
601 /***********************************************************************
602 * VWin32_EventSet (KERNEL.451)
603 * KERNEL_479 (KERNEL.479)
605 VOID WINAPI
VWin32_EventSet(HANDLE event
)
612 /***********************************************************************
613 * CreateMutexA (KERNEL32.@)
615 HANDLE WINAPI
CreateMutexA( SECURITY_ATTRIBUTES
*sa
, BOOL owner
, LPCSTR name
)
617 WCHAR buffer
[MAX_PATH
];
619 if (!name
) return CreateMutexW( sa
, owner
, NULL
);
621 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
623 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
626 return CreateMutexW( sa
, owner
, buffer
);
630 /***********************************************************************
631 * CreateMutexW (KERNEL32.@)
633 HANDLE WINAPI
CreateMutexW( SECURITY_ATTRIBUTES
*sa
, BOOL owner
, LPCWSTR name
)
636 UNICODE_STRING nameW
;
637 OBJECT_ATTRIBUTES attr
;
640 attr
.Length
= sizeof(attr
);
641 attr
.RootDirectory
= 0;
642 attr
.ObjectName
= NULL
;
643 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
644 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
645 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
646 attr
.SecurityQualityOfService
= NULL
;
649 RtlInitUnicodeString( &nameW
, name
);
650 attr
.ObjectName
= &nameW
;
653 status
= NtCreateMutant( &ret
, MUTEX_ALL_ACCESS
, &attr
, owner
);
654 SetLastError( RtlNtStatusToDosError(status
) );
659 /***********************************************************************
660 * OpenMutexA (KERNEL32.@)
662 HANDLE WINAPI
OpenMutexA( DWORD access
, BOOL inherit
, LPCSTR name
)
664 WCHAR buffer
[MAX_PATH
];
666 if (!name
) return OpenMutexW( access
, inherit
, NULL
);
668 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
670 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
673 return OpenMutexW( access
, inherit
, buffer
);
677 /***********************************************************************
678 * OpenMutexW (KERNEL32.@)
680 HANDLE WINAPI
OpenMutexW( DWORD access
, BOOL inherit
, LPCWSTR name
)
683 UNICODE_STRING nameW
;
684 OBJECT_ATTRIBUTES attr
;
687 if (!is_version_nt()) access
= MUTEX_ALL_ACCESS
;
689 attr
.Length
= sizeof(attr
);
690 attr
.RootDirectory
= 0;
691 attr
.ObjectName
= NULL
;
692 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| (inherit
? OBJ_INHERIT
: 0);
693 attr
.SecurityDescriptor
= NULL
;
694 attr
.SecurityQualityOfService
= NULL
;
697 RtlInitUnicodeString( &nameW
, name
);
698 attr
.ObjectName
= &nameW
;
701 status
= NtOpenMutant( &ret
, access
, &attr
);
702 if (status
!= STATUS_SUCCESS
)
704 SetLastError( RtlNtStatusToDosError(status
) );
711 /***********************************************************************
712 * ReleaseMutex (KERNEL32.@)
714 BOOL WINAPI
ReleaseMutex( HANDLE handle
)
718 status
= NtReleaseMutant(handle
, NULL
);
719 if (status
!= STATUS_SUCCESS
)
721 SetLastError( RtlNtStatusToDosError(status
) );
733 /***********************************************************************
734 * CreateSemaphoreA (KERNEL32.@)
736 HANDLE WINAPI
CreateSemaphoreA( SECURITY_ATTRIBUTES
*sa
, LONG initial
, LONG max
, LPCSTR name
)
738 WCHAR buffer
[MAX_PATH
];
740 if (!name
) return CreateSemaphoreW( sa
, initial
, max
, NULL
);
742 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
744 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
747 return CreateSemaphoreW( sa
, initial
, max
, buffer
);
751 /***********************************************************************
752 * CreateSemaphoreW (KERNEL32.@)
754 HANDLE WINAPI
CreateSemaphoreW( SECURITY_ATTRIBUTES
*sa
, LONG initial
,
755 LONG max
, LPCWSTR name
)
758 UNICODE_STRING nameW
;
759 OBJECT_ATTRIBUTES attr
;
762 attr
.Length
= sizeof(attr
);
763 attr
.RootDirectory
= 0;
764 attr
.ObjectName
= NULL
;
765 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
766 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
767 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
768 attr
.SecurityQualityOfService
= NULL
;
771 RtlInitUnicodeString( &nameW
, name
);
772 attr
.ObjectName
= &nameW
;
775 status
= NtCreateSemaphore( &ret
, SEMAPHORE_ALL_ACCESS
, &attr
, initial
, max
);
776 SetLastError( RtlNtStatusToDosError(status
) );
781 /***********************************************************************
782 * OpenSemaphoreA (KERNEL32.@)
784 HANDLE WINAPI
OpenSemaphoreA( DWORD access
, BOOL inherit
, LPCSTR name
)
786 WCHAR buffer
[MAX_PATH
];
788 if (!name
) return OpenSemaphoreW( access
, inherit
, NULL
);
790 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
792 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
795 return OpenSemaphoreW( access
, inherit
, buffer
);
799 /***********************************************************************
800 * OpenSemaphoreW (KERNEL32.@)
802 HANDLE WINAPI
OpenSemaphoreW( DWORD access
, BOOL inherit
, LPCWSTR name
)
805 UNICODE_STRING nameW
;
806 OBJECT_ATTRIBUTES attr
;
809 if (!is_version_nt()) access
= SEMAPHORE_ALL_ACCESS
;
811 attr
.Length
= sizeof(attr
);
812 attr
.RootDirectory
= 0;
813 attr
.ObjectName
= NULL
;
814 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| (inherit
? OBJ_INHERIT
: 0);
815 attr
.SecurityDescriptor
= NULL
;
816 attr
.SecurityQualityOfService
= NULL
;
819 RtlInitUnicodeString( &nameW
, name
);
820 attr
.ObjectName
= &nameW
;
823 status
= NtOpenSemaphore( &ret
, access
, &attr
);
824 if (status
!= STATUS_SUCCESS
)
826 SetLastError( RtlNtStatusToDosError(status
) );
833 /***********************************************************************
834 * ReleaseSemaphore (KERNEL32.@)
836 BOOL WINAPI
ReleaseSemaphore( HANDLE handle
, LONG count
, LONG
*previous
)
838 NTSTATUS status
= NtReleaseSemaphore( handle
, count
, (PULONG
)previous
);
839 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
849 /***********************************************************************
850 * CreateWaitableTimerA (KERNEL32.@)
852 HANDLE WINAPI
CreateWaitableTimerA( SECURITY_ATTRIBUTES
*sa
, BOOL manual
, LPCSTR name
)
854 WCHAR buffer
[MAX_PATH
];
856 if (!name
) return CreateWaitableTimerW( sa
, manual
, NULL
);
858 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
860 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
863 return CreateWaitableTimerW( sa
, manual
, buffer
);
867 /***********************************************************************
868 * CreateWaitableTimerW (KERNEL32.@)
870 HANDLE WINAPI
CreateWaitableTimerW( SECURITY_ATTRIBUTES
*sa
, BOOL manual
, LPCWSTR name
)
874 UNICODE_STRING nameW
;
875 OBJECT_ATTRIBUTES attr
;
877 attr
.Length
= sizeof(attr
);
878 attr
.RootDirectory
= 0;
879 attr
.ObjectName
= NULL
;
880 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
881 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
882 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
883 attr
.SecurityQualityOfService
= NULL
;
886 RtlInitUnicodeString( &nameW
, name
);
887 attr
.ObjectName
= &nameW
;
890 status
= NtCreateTimer(&handle
, TIMER_ALL_ACCESS
, &attr
,
891 manual
? NotificationTimer
: SynchronizationTimer
);
892 SetLastError( RtlNtStatusToDosError(status
) );
897 /***********************************************************************
898 * OpenWaitableTimerA (KERNEL32.@)
900 HANDLE WINAPI
OpenWaitableTimerA( DWORD access
, BOOL inherit
, LPCSTR name
)
902 WCHAR buffer
[MAX_PATH
];
904 if (!name
) return OpenWaitableTimerW( access
, inherit
, NULL
);
906 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
908 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
911 return OpenWaitableTimerW( access
, inherit
, buffer
);
915 /***********************************************************************
916 * OpenWaitableTimerW (KERNEL32.@)
918 HANDLE WINAPI
OpenWaitableTimerW( DWORD access
, BOOL inherit
, LPCWSTR name
)
921 UNICODE_STRING nameW
;
922 OBJECT_ATTRIBUTES attr
;
925 if (!is_version_nt()) access
= TIMER_ALL_ACCESS
;
927 attr
.Length
= sizeof(attr
);
928 attr
.RootDirectory
= 0;
929 attr
.ObjectName
= NULL
;
930 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| (inherit
? OBJ_INHERIT
: 0);
931 attr
.SecurityDescriptor
= NULL
;
932 attr
.SecurityQualityOfService
= NULL
;
935 RtlInitUnicodeString( &nameW
, name
);
936 attr
.ObjectName
= &nameW
;
939 status
= NtOpenTimer(&handle
, access
, &attr
);
940 if (status
!= STATUS_SUCCESS
)
942 SetLastError( RtlNtStatusToDosError(status
) );
949 /***********************************************************************
950 * SetWaitableTimer (KERNEL32.@)
952 BOOL WINAPI
SetWaitableTimer( HANDLE handle
, const LARGE_INTEGER
*when
, LONG period
,
953 PTIMERAPCROUTINE callback
, LPVOID arg
, BOOL resume
)
955 NTSTATUS status
= NtSetTimer(handle
, when
, (PTIMER_APC_ROUTINE
)callback
,
956 arg
, resume
, period
, NULL
);
958 if (status
!= STATUS_SUCCESS
)
960 SetLastError( RtlNtStatusToDosError(status
) );
961 if (status
!= STATUS_TIMER_RESUME_IGNORED
) return FALSE
;
967 /***********************************************************************
968 * CancelWaitableTimer (KERNEL32.@)
970 BOOL WINAPI
CancelWaitableTimer( HANDLE handle
)
974 status
= NtCancelTimer(handle
, NULL
);
975 if (status
!= STATUS_SUCCESS
)
977 SetLastError( RtlNtStatusToDosError(status
) );
984 /***********************************************************************
985 * CreateTimerQueue (KERNEL32.@)
987 HANDLE WINAPI
CreateTimerQueue(void)
990 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
995 /***********************************************************************
996 * DeleteTimerQueueEx (KERNEL32.@)
998 BOOL WINAPI
DeleteTimerQueueEx(HANDLE TimerQueue
, HANDLE CompletionEvent
)
1000 FIXME("(%p, %p): stub\n", TimerQueue
, CompletionEvent
);
1001 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1005 /***********************************************************************
1006 * CreateTimerQueueTimer (KERNEL32.@)
1008 * Creates a timer-queue timer. This timer expires at the specified due
1009 * time (in ms), then after every specified period (in ms). When the timer
1010 * expires, the callback function is called.
1013 * nonzero on success or zero on faillure
1018 BOOL WINAPI
CreateTimerQueueTimer( PHANDLE phNewTimer
, HANDLE TimerQueue
,
1019 WAITORTIMERCALLBACK Callback
, PVOID Parameter
,
1020 DWORD DueTime
, DWORD Period
, ULONG Flags
)
1023 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1027 /***********************************************************************
1028 * DeleteTimerQueueTimer (KERNEL32.@)
1030 * Cancels a timer-queue timer.
1033 * nonzero on success or zero on faillure
1038 BOOL WINAPI
DeleteTimerQueueTimer( HANDLE TimerQueue
, HANDLE Timer
,
1039 HANDLE CompletionEvent
)
1042 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1052 /***********************************************************************
1053 * CreateNamedPipeA (KERNEL32.@)
1055 HANDLE WINAPI
CreateNamedPipeA( LPCSTR name
, DWORD dwOpenMode
,
1056 DWORD dwPipeMode
, DWORD nMaxInstances
,
1057 DWORD nOutBufferSize
, DWORD nInBufferSize
,
1058 DWORD nDefaultTimeOut
, LPSECURITY_ATTRIBUTES attr
)
1060 WCHAR buffer
[MAX_PATH
];
1062 if (!name
) return CreateNamedPipeW( NULL
, dwOpenMode
, dwPipeMode
, nMaxInstances
,
1063 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, attr
);
1065 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1067 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1068 return INVALID_HANDLE_VALUE
;
1070 return CreateNamedPipeW( buffer
, dwOpenMode
, dwPipeMode
, nMaxInstances
,
1071 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, attr
);
1075 /***********************************************************************
1076 * CreateNamedPipeW (KERNEL32.@)
1078 HANDLE WINAPI
CreateNamedPipeW( LPCWSTR name
, DWORD dwOpenMode
,
1079 DWORD dwPipeMode
, DWORD nMaxInstances
,
1080 DWORD nOutBufferSize
, DWORD nInBufferSize
,
1081 DWORD nDefaultTimeOut
, LPSECURITY_ATTRIBUTES sa
)
1084 UNICODE_STRING nt_name
;
1085 OBJECT_ATTRIBUTES attr
;
1087 BOOLEAN pipe_type
, read_mode
, non_block
;
1089 IO_STATUS_BLOCK iosb
;
1090 LARGE_INTEGER timeout
;
1092 TRACE("(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n",
1093 debugstr_w(name
), dwOpenMode
, dwPipeMode
, nMaxInstances
,
1094 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, sa
);
1096 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1098 SetLastError( ERROR_PATH_NOT_FOUND
);
1099 return INVALID_HANDLE_VALUE
;
1101 if (nt_name
.Length
>= MAX_PATH
* sizeof(WCHAR
) )
1103 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1104 RtlFreeUnicodeString( &nt_name
);
1105 return INVALID_HANDLE_VALUE
;
1108 attr
.Length
= sizeof(attr
);
1109 attr
.RootDirectory
= 0;
1110 attr
.ObjectName
= &nt_name
;
1111 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
1112 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
1113 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1114 attr
.SecurityQualityOfService
= NULL
;
1117 if (dwOpenMode
& FILE_FLAG_WRITE_THROUGH
) options
|= FILE_WRITE_THROUGH
;
1118 if (!(dwOpenMode
& FILE_FLAG_OVERLAPPED
)) options
|= FILE_SYNCHRONOUS_IO_ALERT
;
1119 if ((dwOpenMode
& PIPE_ACCESS_DUPLEX
) == PIPE_ACCESS_DUPLEX
)
1120 options
|= FILE_PIPE_FULL_DUPLEX
;
1121 else if (dwOpenMode
& PIPE_ACCESS_INBOUND
) options
|= FILE_PIPE_INBOUND
;
1122 else if (dwOpenMode
& PIPE_ACCESS_OUTBOUND
) options
|= FILE_PIPE_OUTBOUND
;
1123 pipe_type
= (dwPipeMode
& PIPE_TYPE_MESSAGE
) ? TRUE
: FALSE
;
1124 read_mode
= (dwPipeMode
& PIPE_READMODE_MESSAGE
) ? TRUE
: FALSE
;
1125 non_block
= (dwPipeMode
& PIPE_NOWAIT
) ? TRUE
: FALSE
;
1126 if (nMaxInstances
>= PIPE_UNLIMITED_INSTANCES
) nMaxInstances
= ~0UL;
1128 timeout
.QuadPart
= (ULONGLONG
)nDefaultTimeOut
* -10000;
1132 status
= NtCreateNamedPipeFile(&handle
, GENERIC_READ
|GENERIC_WRITE
, &attr
, &iosb
,
1133 0, FILE_OVERWRITE_IF
, options
, pipe_type
,
1134 read_mode
, non_block
, nMaxInstances
,
1135 nInBufferSize
, nOutBufferSize
, &timeout
);
1137 RtlFreeUnicodeString( &nt_name
);
1140 handle
= INVALID_HANDLE_VALUE
;
1141 SetLastError( RtlNtStatusToDosError(status
) );
1147 /***********************************************************************
1148 * PeekNamedPipe (KERNEL32.@)
1150 BOOL WINAPI
PeekNamedPipe( HANDLE hPipe
, LPVOID lpvBuffer
, DWORD cbBuffer
,
1151 LPDWORD lpcbRead
, LPDWORD lpcbAvail
, LPDWORD lpcbMessage
)
1154 int avail
=0, fd
, ret
, flags
;
1156 TRACE("(%p,%p,%lu,%p,%p,%p)\n", hPipe
, lpvBuffer
, cbBuffer
, lpcbRead
, lpcbAvail
, lpcbMessage
);
1158 ret
= wine_server_handle_to_fd( hPipe
, GENERIC_READ
, &fd
, &flags
);
1161 SetLastError( RtlNtStatusToDosError(ret
) );
1164 if (flags
& FD_FLAG_RECV_SHUTDOWN
)
1166 wine_server_release_fd( hPipe
, fd
);
1167 SetLastError ( ERROR_PIPE_NOT_CONNECTED
);
1171 if (ioctl(fd
,FIONREAD
, &avail
) != 0)
1173 TRACE("FIONREAD failed reason: %s\n",strerror(errno
));
1174 wine_server_release_fd( hPipe
, fd
);
1177 if (!avail
) /* check for closed pipe */
1179 struct pollfd pollfd
;
1181 pollfd
.events
= POLLIN
;
1183 switch (poll( &pollfd
, 1, 0 ))
1187 case 1: /* got something */
1188 if (!(pollfd
.revents
& (POLLHUP
| POLLERR
))) break;
1189 TRACE("POLLHUP | POLLERR\n");
1192 wine_server_release_fd( hPipe
, fd
);
1193 SetLastError(ERROR_BROKEN_PIPE
);
1197 TRACE(" 0x%08x bytes available\n", avail
);
1203 if (avail
&& lpvBuffer
&& cbBuffer
)
1205 int readbytes
= (avail
< cbBuffer
) ? avail
: cbBuffer
;
1206 readbytes
= recv(fd
, lpvBuffer
, readbytes
, MSG_PEEK
);
1209 WARN("failed to peek socket (%d)\n", errno
);
1213 *lpcbRead
= readbytes
;
1215 wine_server_release_fd( hPipe
, fd
);
1217 #endif /* defined(FIONREAD) */
1219 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1220 FIXME("function not implemented\n");
1224 /***********************************************************************
1225 * PIPE_CompletionWait (Internal)
1227 static void CALLBACK
PIPE_CompletionWait(void *user
, PIO_STATUS_BLOCK iosb
, ULONG status
)
1229 LPOVERLAPPED ovlp
= (LPOVERLAPPED
)user
;
1231 TRACE("for %p/%p, status=%08lx\n", ovlp
, iosb
, status
);
1235 ovlp
->Internal
= status
;
1236 SetEvent(ovlp
->hEvent
);
1241 /***********************************************************************
1242 * WaitNamedPipeA (KERNEL32.@)
1244 BOOL WINAPI
WaitNamedPipeA (LPCSTR name
, DWORD nTimeOut
)
1246 WCHAR buffer
[MAX_PATH
];
1248 if (!name
) return WaitNamedPipeW( NULL
, nTimeOut
);
1250 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1252 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1255 return WaitNamedPipeW( buffer
, nTimeOut
);
1259 /***********************************************************************
1260 * WaitNamedPipeW (KERNEL32.@)
1262 BOOL WINAPI
WaitNamedPipeW (LPCWSTR name
, DWORD nTimeOut
)
1266 UNICODE_STRING nt_name
;
1267 static const WCHAR leadin
[] = {'\\','?','?','\\','P','I','P','E','\\'};
1269 TRACE("%s 0x%08lx\n",debugstr_w(name
),nTimeOut
);
1271 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1274 if (nt_name
.Length
>= MAX_PATH
* sizeof(WCHAR
) )
1276 RtlFreeUnicodeString( &nt_name
);
1279 if (nt_name
.Length
< sizeof(leadin
) ||
1280 strncmpiW( nt_name
.Buffer
, leadin
, sizeof(leadin
)/sizeof(leadin
[0])))
1282 RtlFreeUnicodeString( &nt_name
);
1286 memset(&ov
,0,sizeof(ov
));
1287 ov
.hEvent
= CreateEventW( NULL
, 0, 0, NULL
);
1291 SERVER_START_REQ( wait_named_pipe
)
1293 req
->timeout
= nTimeOut
;
1294 req
->overlapped
= &ov
;
1295 req
->func
= PIPE_CompletionWait
;
1296 wine_server_add_data( req
, nt_name
.Buffer
+ 4, nt_name
.Length
- 4*sizeof(WCHAR
) );
1297 ret
= !wine_server_call_err( req
);
1301 RtlFreeUnicodeString( &nt_name
);
1305 if (WAIT_OBJECT_0
==WaitForSingleObject(ov
.hEvent
,INFINITE
))
1307 SetLastError(RtlNtStatusToDosError(ov
.Internal
));
1308 ret
= (ov
.Internal
==STATUS_SUCCESS
);
1311 CloseHandle(ov
.hEvent
);
1316 /***********************************************************************
1317 * ConnectNamedPipe (KERNEL32.@)
1319 * Connects to a named pipe
1322 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1323 * overlapped: Optional OVERLAPPED struct
1327 * FALSE: Failure, GetLastError can be called for further details
1329 BOOL WINAPI
ConnectNamedPipe(HANDLE hPipe
, LPOVERLAPPED overlapped
)
1332 IO_STATUS_BLOCK status_block
;
1334 TRACE("(%p,%p)\n", hPipe
, overlapped
);
1337 overlapped
->Internal
= STATUS_PENDING
;
1339 status
= NtFsControlFile(hPipe
, overlapped
? overlapped
->hEvent
: NULL
, NULL
, NULL
,
1340 overlapped
? (IO_STATUS_BLOCK
*)overlapped
: &status_block
,
1341 FSCTL_PIPE_LISTEN
, NULL
, 0, NULL
, 0);
1343 if (status
== STATUS_SUCCESS
) return TRUE
;
1344 SetLastError( RtlNtStatusToDosError(status
) );
1348 /***********************************************************************
1349 * DisconnectNamedPipe (KERNEL32.@)
1351 * Disconnects from a named pipe
1354 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1358 * FALSE: Failure, GetLastError can be called for further details
1360 BOOL WINAPI
DisconnectNamedPipe(HANDLE hPipe
)
1363 IO_STATUS_BLOCK io_block
;
1365 TRACE("(%p)\n",hPipe
);
1367 status
= NtFsControlFile(hPipe
, 0, NULL
, NULL
, &io_block
, FSCTL_PIPE_DISCONNECT
,
1369 if (status
== STATUS_SUCCESS
) return TRUE
;
1370 SetLastError( RtlNtStatusToDosError(status
) );
1374 /***********************************************************************
1375 * TransactNamedPipe (KERNEL32.@)
1378 * should be done as a single operation in the wineserver or kernel
1380 BOOL WINAPI
TransactNamedPipe(
1381 HANDLE handle
, LPVOID lpInput
, DWORD dwInputSize
, LPVOID lpOutput
,
1382 DWORD dwOutputSize
, LPDWORD lpBytesRead
, LPOVERLAPPED lpOverlapped
)
1387 TRACE("%p %p %ld %p %ld %p %p\n",
1388 handle
, lpInput
, dwInputSize
, lpOutput
,
1389 dwOutputSize
, lpBytesRead
, lpOverlapped
);
1393 FIXME("Doesn't support overlapped operation as yet\n");
1397 r
= WriteFile(handle
, lpOutput
, dwOutputSize
, &count
, NULL
);
1399 r
= ReadFile(handle
, lpInput
, dwInputSize
, lpBytesRead
, NULL
);
1404 /***********************************************************************
1405 * GetNamedPipeInfo (KERNEL32.@)
1407 BOOL WINAPI
GetNamedPipeInfo(
1408 HANDLE hNamedPipe
, LPDWORD lpFlags
, LPDWORD lpOutputBufferSize
,
1409 LPDWORD lpInputBufferSize
, LPDWORD lpMaxInstances
)
1413 TRACE("%p %p %p %p %p\n", hNamedPipe
, lpFlags
,
1414 lpOutputBufferSize
, lpInputBufferSize
, lpMaxInstances
);
1416 SERVER_START_REQ( get_named_pipe_info
)
1418 req
->handle
= hNamedPipe
;
1419 ret
= !wine_server_call_err( req
);
1423 if (reply
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
)
1424 *lpFlags
|= PIPE_TYPE_MESSAGE
;
1425 if (reply
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
)
1426 *lpFlags
|= PIPE_READMODE_MESSAGE
;
1427 if (reply
->flags
& NAMED_PIPE_NONBLOCKING_MODE
)
1428 *lpFlags
|= PIPE_NOWAIT
;
1430 if (lpOutputBufferSize
) *lpOutputBufferSize
= reply
->outsize
;
1431 if (lpInputBufferSize
) *lpInputBufferSize
= reply
->outsize
;
1432 if (lpMaxInstances
) *lpMaxInstances
= reply
->maxinstances
;
1439 /***********************************************************************
1440 * GetNamedPipeHandleStateA (KERNEL32.@)
1442 BOOL WINAPI
GetNamedPipeHandleStateA(
1443 HANDLE hNamedPipe
, LPDWORD lpState
, LPDWORD lpCurInstances
,
1444 LPDWORD lpMaxCollectionCount
, LPDWORD lpCollectDataTimeout
,
1445 LPSTR lpUsername
, DWORD nUsernameMaxSize
)
1447 FIXME("%p %p %p %p %p %p %ld\n",
1448 hNamedPipe
, lpState
, lpCurInstances
,
1449 lpMaxCollectionCount
, lpCollectDataTimeout
,
1450 lpUsername
, nUsernameMaxSize
);
1455 /***********************************************************************
1456 * GetNamedPipeHandleStateW (KERNEL32.@)
1458 BOOL WINAPI
GetNamedPipeHandleStateW(
1459 HANDLE hNamedPipe
, LPDWORD lpState
, LPDWORD lpCurInstances
,
1460 LPDWORD lpMaxCollectionCount
, LPDWORD lpCollectDataTimeout
,
1461 LPWSTR lpUsername
, DWORD nUsernameMaxSize
)
1463 FIXME("%p %p %p %p %p %p %ld\n",
1464 hNamedPipe
, lpState
, lpCurInstances
,
1465 lpMaxCollectionCount
, lpCollectDataTimeout
,
1466 lpUsername
, nUsernameMaxSize
);
1471 /***********************************************************************
1472 * SetNamedPipeHandleState (KERNEL32.@)
1474 BOOL WINAPI
SetNamedPipeHandleState(
1475 HANDLE hNamedPipe
, LPDWORD lpMode
, LPDWORD lpMaxCollectionCount
,
1476 LPDWORD lpCollectDataTimeout
)
1478 /* should be a fixme, but this function is called a lot by the RPC
1479 * runtime, and it slows down InstallShield a fair bit. */
1480 WARN("stub: %p %p/%ld %p %p\n",
1481 hNamedPipe
, lpMode
, lpMode
? *lpMode
: 0, lpMaxCollectionCount
, lpCollectDataTimeout
);
1485 /***********************************************************************
1486 * CallNamedPipeA (KERNEL32.@)
1488 BOOL WINAPI
CallNamedPipeA(
1489 LPCSTR lpNamedPipeName
, LPVOID lpInput
, DWORD dwInputSize
,
1490 LPVOID lpOutput
, DWORD dwOutputSize
,
1491 LPDWORD lpBytesRead
, DWORD nTimeout
)
1497 TRACE("%s %p %ld %p %ld %p %ld\n",
1498 debugstr_a(lpNamedPipeName
), lpInput
, dwInputSize
,
1499 lpOutput
, dwOutputSize
, lpBytesRead
, nTimeout
);
1501 if( lpNamedPipeName
)
1503 len
= MultiByteToWideChar( CP_ACP
, 0, lpNamedPipeName
, -1, NULL
, 0 );
1504 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1505 MultiByteToWideChar( CP_ACP
, 0, lpNamedPipeName
, -1, str
, len
);
1507 ret
= CallNamedPipeW( str
, lpInput
, dwInputSize
, lpOutput
,
1508 dwOutputSize
, lpBytesRead
, nTimeout
);
1509 if( lpNamedPipeName
)
1510 HeapFree( GetProcessHeap(), 0, str
);
1515 /***********************************************************************
1516 * CallNamedPipeW (KERNEL32.@)
1518 BOOL WINAPI
CallNamedPipeW(
1519 LPCWSTR lpNamedPipeName
, LPVOID lpInput
, DWORD lpInputSize
,
1520 LPVOID lpOutput
, DWORD lpOutputSize
,
1521 LPDWORD lpBytesRead
, DWORD nTimeout
)
1523 FIXME("%s %p %ld %p %ld %p %ld\n",
1524 debugstr_w(lpNamedPipeName
), lpInput
, lpInputSize
,
1525 lpOutput
, lpOutputSize
, lpBytesRead
, nTimeout
);
1529 /******************************************************************
1530 * CreatePipe (KERNEL32.@)
1533 BOOL WINAPI
CreatePipe( PHANDLE hReadPipe
, PHANDLE hWritePipe
,
1534 LPSECURITY_ATTRIBUTES sa
, DWORD size
)
1536 static unsigned index
/* = 0 */;
1539 unsigned in_index
= index
;
1540 UNICODE_STRING nt_name
;
1541 OBJECT_ATTRIBUTES attr
;
1543 IO_STATUS_BLOCK iosb
;
1544 LARGE_INTEGER timeout
;
1546 *hReadPipe
= *hWritePipe
= INVALID_HANDLE_VALUE
;
1548 attr
.Length
= sizeof(attr
);
1549 attr
.RootDirectory
= 0;
1550 attr
.ObjectName
= &nt_name
;
1551 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
1552 (sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0;
1553 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1554 attr
.SecurityQualityOfService
= NULL
;
1556 timeout
.QuadPart
= (ULONGLONG
)NMPWAIT_USE_DEFAULT_WAIT
* -10000;
1557 /* generate a unique pipe name (system wide) */
1560 static const WCHAR nameFmt
[] = { '\\','?','?','\\','p','i','p','e',
1561 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1562 'u','.','%','0','8','u','\0' };
1564 snprintfW(name
, sizeof(name
) / sizeof(name
[0]), nameFmt
,
1565 GetCurrentProcessId(), ++index
);
1566 RtlInitUnicodeString(&nt_name
, name
);
1567 status
= NtCreateNamedPipeFile(&hr
, GENERIC_READ
| SYNCHRONIZE
, &attr
, &iosb
,
1568 0, FILE_OVERWRITE_IF
,
1569 FILE_SYNCHRONOUS_IO_ALERT
| FILE_PIPE_INBOUND
,
1570 FALSE
, FALSE
, FALSE
,
1571 1, size
, size
, &timeout
);
1574 SetLastError( RtlNtStatusToDosError(status
) );
1575 hr
= INVALID_HANDLE_VALUE
;
1577 } while (hr
== INVALID_HANDLE_VALUE
&& index
!= in_index
);
1578 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1579 if (hr
== INVALID_HANDLE_VALUE
) return FALSE
;
1581 status
= NtOpenFile(&hw
, GENERIC_WRITE
| SYNCHRONIZE
, &attr
, &iosb
, 0,
1582 FILE_SYNCHRONOUS_IO_ALERT
| FILE_NON_DIRECTORY_FILE
);
1586 SetLastError( RtlNtStatusToDosError(status
) );
1597 /******************************************************************************
1598 * CreateMailslotA [KERNEL32.@]
1600 HANDLE WINAPI
CreateMailslotA( LPCSTR lpName
, DWORD nMaxMessageSize
,
1601 DWORD lReadTimeout
, LPSECURITY_ATTRIBUTES sa
)
1607 TRACE("%s %ld %ld %p\n", debugstr_a(lpName
),
1608 nMaxMessageSize
, lReadTimeout
, sa
);
1612 len
= MultiByteToWideChar( CP_ACP
, 0, lpName
, -1, NULL
, 0 );
1613 name
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1614 MultiByteToWideChar( CP_ACP
, 0, lpName
, -1, name
, len
);
1617 handle
= CreateMailslotW( name
, nMaxMessageSize
, lReadTimeout
, sa
);
1619 HeapFree( GetProcessHeap(), 0, name
);
1625 /******************************************************************************
1626 * CreateMailslotW [KERNEL32.@]
1628 * Create a mailslot with specified name.
1631 * lpName [I] Pointer to string for mailslot name
1632 * nMaxMessageSize [I] Maximum message size
1633 * lReadTimeout [I] Milliseconds before read time-out
1634 * sa [I] Pointer to security structure
1637 * Success: Handle to mailslot
1638 * Failure: INVALID_HANDLE_VALUE
1640 HANDLE WINAPI
CreateMailslotW( LPCWSTR lpName
, DWORD nMaxMessageSize
,
1641 DWORD lReadTimeout
, LPSECURITY_ATTRIBUTES sa
)
1643 HANDLE handle
= INVALID_HANDLE_VALUE
;
1644 OBJECT_ATTRIBUTES attr
;
1645 UNICODE_STRING nameW
;
1646 LARGE_INTEGER timeout
;
1647 IO_STATUS_BLOCK iosb
;
1650 TRACE("%s %ld %ld %p\n", debugstr_w(lpName
),
1651 nMaxMessageSize
, lReadTimeout
, sa
);
1653 if (!RtlDosPathNameToNtPathName_U( lpName
, &nameW
, NULL
, NULL
))
1655 SetLastError( ERROR_PATH_NOT_FOUND
);
1656 return INVALID_HANDLE_VALUE
;
1659 if (nameW
.Length
>= MAX_PATH
* sizeof(WCHAR
) )
1661 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1662 RtlFreeUnicodeString( &nameW
);
1663 return INVALID_HANDLE_VALUE
;
1666 attr
.Length
= sizeof(attr
);
1667 attr
.RootDirectory
= 0;
1668 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1669 attr
.ObjectName
= &nameW
;
1670 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1671 attr
.SecurityQualityOfService
= NULL
;
1673 timeout
.QuadPart
= (ULONGLONG
) lReadTimeout
* -10000;
1675 status
= NtCreateMailslotFile( &handle
, GENERIC_READ
| GENERIC_WRITE
, &attr
,
1676 &iosb
, 0, 0, nMaxMessageSize
, &timeout
);
1679 SetLastError( RtlNtStatusToDosError(status
) );
1680 handle
= INVALID_HANDLE_VALUE
;
1683 RtlFreeUnicodeString( &nameW
);
1688 /******************************************************************************
1689 * GetMailslotInfo [KERNEL32.@]
1691 * Retrieve information about a mailslot.
1694 * hMailslot [I] Mailslot handle
1695 * lpMaxMessageSize [O] Address of maximum message size
1696 * lpNextSize [O] Address of size of next message
1697 * lpMessageCount [O] Address of number of messages
1698 * lpReadTimeout [O] Address of read time-out
1704 BOOL WINAPI
GetMailslotInfo( HANDLE hMailslot
, LPDWORD lpMaxMessageSize
,
1705 LPDWORD lpNextSize
, LPDWORD lpMessageCount
,
1706 LPDWORD lpReadTimeout
)
1708 FILE_MAILSLOT_QUERY_INFORMATION info
;
1709 IO_STATUS_BLOCK iosb
;
1712 TRACE("%p %p %p %p %p\n",hMailslot
, lpMaxMessageSize
,
1713 lpNextSize
, lpMessageCount
, lpReadTimeout
);
1715 status
= NtQueryInformationFile( hMailslot
, &iosb
, &info
, sizeof info
,
1716 FileMailslotQueryInformation
);
1718 if( status
!= STATUS_SUCCESS
)
1720 SetLastError( RtlNtStatusToDosError(status
) );
1724 if( lpMaxMessageSize
)
1725 *lpMaxMessageSize
= info
.MaximumMessageSize
;
1727 *lpNextSize
= info
.NextMessageSize
;
1728 if( lpMessageCount
)
1729 *lpMessageCount
= info
.MessagesAvailable
;
1731 *lpReadTimeout
= info
.ReadTimeout
.QuadPart
/ -10000;
1737 /******************************************************************************
1738 * SetMailslotInfo [KERNEL32.@]
1740 * Set the read timeout of a mailslot.
1743 * hMailslot [I] Mailslot handle
1744 * dwReadTimeout [I] Timeout in milliseconds.
1750 BOOL WINAPI
SetMailslotInfo( HANDLE hMailslot
, DWORD dwReadTimeout
)
1752 FILE_MAILSLOT_SET_INFORMATION info
;
1753 IO_STATUS_BLOCK iosb
;
1756 TRACE("%p %ld\n", hMailslot
, dwReadTimeout
);
1758 info
.ReadTimeout
.QuadPart
= dwReadTimeout
* -10000;
1759 status
= NtSetInformationFile( hMailslot
, &iosb
, &info
, sizeof info
,
1760 FileMailslotSetInformation
);
1761 if( status
!= STATUS_SUCCESS
)
1763 SetLastError( RtlNtStatusToDosError(status
) );
1770 /******************************************************************************
1771 * CreateIoCompletionPort (KERNEL32.@)
1773 HANDLE WINAPI
CreateIoCompletionPort(HANDLE hFileHandle
, HANDLE hExistingCompletionPort
,
1774 ULONG_PTR CompletionKey
, DWORD dwNumberOfConcurrentThreads
)
1776 FIXME("(%p, %p, %08lx, %08lx): stub.\n",
1777 hFileHandle
, hExistingCompletionPort
, CompletionKey
, dwNumberOfConcurrentThreads
);
1782 /******************************************************************************
1783 * GetQueuedCompletionStatus (KERNEL32.@)
1785 BOOL WINAPI
GetQueuedCompletionStatus( HANDLE CompletionPort
, LPDWORD lpNumberOfBytesTransferred
,
1786 PULONG_PTR pCompletionKey
, LPOVERLAPPED
*lpOverlapped
,
1787 DWORD dwMilliseconds
)
1789 FIXME("(%p,%p,%p,%p,%ld), stub!\n",
1790 CompletionPort
,lpNumberOfBytesTransferred
,pCompletionKey
,lpOverlapped
,dwMilliseconds
);
1791 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1795 /******************************************************************************
1796 * CreateJobObjectW (KERNEL32.@)
1798 HANDLE WINAPI
CreateJobObjectW( LPSECURITY_ATTRIBUTES attr
, LPCWSTR name
)
1800 FIXME("%p %s\n", attr
, debugstr_w(name
) );
1801 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1805 /******************************************************************************
1806 * CreateJobObjectA (KERNEL32.@)
1808 HANDLE WINAPI
CreateJobObjectA( LPSECURITY_ATTRIBUTES attr
, LPCSTR name
)
1814 TRACE("%p %s\n", attr
, debugstr_a(name
) );
1818 len
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
1819 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1822 SetLastError( ERROR_OUTOFMEMORY
);
1825 len
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, str
, len
);
1828 r
= CreateJobObjectW( attr
, str
);
1830 HeapFree( GetProcessHeap(), 0, str
);
1835 /******************************************************************************
1836 * AssignProcessToJobObject (KERNEL32.@)
1838 BOOL WINAPI
AssignProcessToJobObject( HANDLE hJob
, HANDLE hProcess
)
1840 FIXME("%p %p\n", hJob
, hProcess
);
1846 /***********************************************************************
1847 * InterlockedCompareExchange (KERNEL32.@)
1849 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1850 __ASM_GLOBAL_FUNC(InterlockedCompareExchange
,
1851 "movl 12(%esp),%eax\n\t"
1852 "movl 8(%esp),%ecx\n\t"
1853 "movl 4(%esp),%edx\n\t"
1854 "lock; cmpxchgl %ecx,(%edx)\n\t"
1857 /***********************************************************************
1858 * InterlockedExchange (KERNEL32.@)
1860 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1861 __ASM_GLOBAL_FUNC(InterlockedExchange
,
1862 "movl 8(%esp),%eax\n\t"
1863 "movl 4(%esp),%edx\n\t"
1864 "lock; xchgl %eax,(%edx)\n\t"
1867 /***********************************************************************
1868 * InterlockedExchangeAdd (KERNEL32.@)
1870 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1871 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd
,
1872 "movl 8(%esp),%eax\n\t"
1873 "movl 4(%esp),%edx\n\t"
1874 "lock; xaddl %eax,(%edx)\n\t"
1877 /***********************************************************************
1878 * InterlockedIncrement (KERNEL32.@)
1880 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1881 __ASM_GLOBAL_FUNC(InterlockedIncrement
,
1882 "movl 4(%esp),%edx\n\t"
1884 "lock; xaddl %eax,(%edx)\n\t"
1888 /***********************************************************************
1889 * InterlockedDecrement (KERNEL32.@)
1891 __ASM_GLOBAL_FUNC(InterlockedDecrement
,
1892 "movl 4(%esp),%edx\n\t"
1894 "lock; xaddl %eax,(%edx)\n\t"
1898 #else /* __i386__ */
1900 /***********************************************************************
1901 * InterlockedCompareExchange (KERNEL32.@)
1903 * Atomically swap one value with another.
1906 * dest [I/O] The value to replace
1907 * xchq [I] The value to be swapped
1908 * compare [I] The value to compare to dest
1911 * The resulting value of dest.
1914 * dest is updated only if it is equal to compare, otherwise no swap is done.
1916 LONG WINAPI
InterlockedCompareExchange( LONG
volatile *dest
, LONG xchg
, LONG compare
)
1918 return interlocked_cmpxchg( (int *)dest
, xchg
, compare
);
1921 /***********************************************************************
1922 * InterlockedExchange (KERNEL32.@)
1924 * Atomically swap one value with another.
1927 * dest [I/O] The value to replace
1928 * val [I] The value to be swapped
1931 * The resulting value of dest.
1933 LONG WINAPI
InterlockedExchange( LONG
volatile *dest
, LONG val
)
1935 return interlocked_xchg( (int *)dest
, val
);
1938 /***********************************************************************
1939 * InterlockedExchangeAdd (KERNEL32.@)
1941 * Atomically add one value to another.
1944 * dest [I/O] The value to add to
1945 * incr [I] The value to be added
1948 * The resulting value of dest.
1950 LONG WINAPI
InterlockedExchangeAdd( LONG
volatile *dest
, LONG incr
)
1952 return interlocked_xchg_add( (int *)dest
, incr
);
1955 /***********************************************************************
1956 * InterlockedIncrement (KERNEL32.@)
1958 * Atomically increment a value.
1961 * dest [I/O] The value to increment
1964 * The resulting value of dest.
1966 LONG WINAPI
InterlockedIncrement( LONG
volatile *dest
)
1968 return interlocked_xchg_add( (int *)dest
, 1 ) + 1;
1971 /***********************************************************************
1972 * InterlockedDecrement (KERNEL32.@)
1974 * Atomically decrement a value.
1977 * dest [I/O] The value to decrement
1980 * The resulting value of dest.
1982 LONG WINAPI
InterlockedDecrement( LONG
volatile *dest
)
1984 return interlocked_xchg_add( (int *)dest
, -1 ) - 1;
1987 #endif /* __i386__ */