2 * Kernel synchronization objects
4 * Copyright 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
36 #define WIN32_NO_STATUS
45 #include "wine/unicode.h"
46 #include "wine/winbase16.h"
47 #include "kernel_private.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(sync
);
53 /* check if current version is NT or Win95 */
54 inline static int is_version_nt(void)
56 return !(GetVersion() & 0x80000000);
59 /* returns directory handle to \\BaseNamedObjects */
60 HANDLE
get_BaseNamedObjects_handle(void)
62 static HANDLE handle
= NULL
;
63 static const WCHAR basenameW
[] =
64 {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
66 OBJECT_ATTRIBUTES attr
;
72 RtlInitUnicodeString(&str
, basenameW
);
73 InitializeObjectAttributes(&attr
, &str
, 0, 0, NULL
);
74 NtOpenDirectoryObject(&dir
, DIRECTORY_CREATE_OBJECT
|DIRECTORY_TRAVERSE
,
76 if (InterlockedCompareExchangePointer( (PVOID
)&handle
, dir
, 0 ) != 0)
78 /* someone beat us here... */
85 /***********************************************************************
88 VOID WINAPI
Sleep( DWORD timeout
)
90 SleepEx( timeout
, FALSE
);
93 /******************************************************************************
94 * SleepEx (KERNEL32.@)
96 DWORD WINAPI
SleepEx( DWORD timeout
, BOOL alertable
)
100 if (timeout
== INFINITE
) status
= NtDelayExecution( alertable
, NULL
);
105 time
.QuadPart
= timeout
* (ULONGLONG
)10000;
106 time
.QuadPart
= -time
.QuadPart
;
107 status
= NtDelayExecution( alertable
, &time
);
109 if (status
!= STATUS_USER_APC
) status
= STATUS_SUCCESS
;
114 /***********************************************************************
115 * SwitchToThread (KERNEL32.@)
117 BOOL WINAPI
SwitchToThread(void)
119 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED
);
123 /***********************************************************************
124 * WaitForSingleObject (KERNEL32.@)
126 DWORD WINAPI
WaitForSingleObject( HANDLE handle
, DWORD timeout
)
128 return WaitForMultipleObjectsEx( 1, &handle
, FALSE
, timeout
, FALSE
);
132 /***********************************************************************
133 * WaitForSingleObjectEx (KERNEL32.@)
135 DWORD WINAPI
WaitForSingleObjectEx( HANDLE handle
, DWORD timeout
,
138 return WaitForMultipleObjectsEx( 1, &handle
, FALSE
, timeout
, alertable
);
142 /***********************************************************************
143 * WaitForMultipleObjects (KERNEL32.@)
145 DWORD WINAPI
WaitForMultipleObjects( DWORD count
, const HANDLE
*handles
,
146 BOOL wait_all
, DWORD timeout
)
148 return WaitForMultipleObjectsEx( count
, handles
, wait_all
, timeout
, FALSE
);
152 /***********************************************************************
153 * WaitForMultipleObjectsEx (KERNEL32.@)
155 DWORD WINAPI
WaitForMultipleObjectsEx( DWORD count
, const HANDLE
*handles
,
156 BOOL wait_all
, DWORD timeout
,
160 HANDLE hloc
[MAXIMUM_WAIT_OBJECTS
];
163 if (count
> MAXIMUM_WAIT_OBJECTS
)
165 SetLastError(ERROR_INVALID_PARAMETER
);
168 for (i
= 0; i
< count
; i
++)
170 if ((handles
[i
] == (HANDLE
)STD_INPUT_HANDLE
) ||
171 (handles
[i
] == (HANDLE
)STD_OUTPUT_HANDLE
) ||
172 (handles
[i
] == (HANDLE
)STD_ERROR_HANDLE
))
173 hloc
[i
] = GetStdHandle( (DWORD
)handles
[i
] );
175 hloc
[i
] = handles
[i
];
177 /* yes, even screen buffer console handles are waitable, and are
178 * handled as a handle to the console itself !!
180 if (is_console_handle(hloc
[i
]))
182 if (!VerifyConsoleIoHandle(hloc
[i
]))
186 hloc
[i
] = GetConsoleInputWaitHandle();
190 if (timeout
== INFINITE
)
192 status
= NtWaitForMultipleObjects( count
, hloc
, wait_all
, alertable
, NULL
);
198 time
.QuadPart
= timeout
* (ULONGLONG
)10000;
199 time
.QuadPart
= -time
.QuadPart
;
200 status
= NtWaitForMultipleObjects( count
, hloc
, wait_all
, alertable
, &time
);
203 if (HIWORD(status
)) /* is it an error code? */
205 SetLastError( RtlNtStatusToDosError(status
) );
206 status
= WAIT_FAILED
;
212 /***********************************************************************
213 * WaitForSingleObject (KERNEL.460)
215 DWORD WINAPI
WaitForSingleObject16( HANDLE handle
, DWORD timeout
)
217 DWORD retval
, mutex_count
;
219 ReleaseThunkLock( &mutex_count
);
220 retval
= WaitForSingleObject( handle
, timeout
);
221 RestoreThunkLock( mutex_count
);
225 /***********************************************************************
226 * WaitForMultipleObjects (KERNEL.461)
228 DWORD WINAPI
WaitForMultipleObjects16( DWORD count
, const HANDLE
*handles
,
229 BOOL wait_all
, DWORD timeout
)
231 DWORD retval
, mutex_count
;
233 ReleaseThunkLock( &mutex_count
);
234 retval
= WaitForMultipleObjectsEx( count
, handles
, wait_all
, timeout
, FALSE
);
235 RestoreThunkLock( mutex_count
);
239 /***********************************************************************
240 * WaitForMultipleObjectsEx (KERNEL.495)
242 DWORD WINAPI
WaitForMultipleObjectsEx16( DWORD count
, const HANDLE
*handles
,
243 BOOL wait_all
, DWORD timeout
, BOOL alertable
)
245 DWORD retval
, mutex_count
;
247 ReleaseThunkLock( &mutex_count
);
248 retval
= WaitForMultipleObjectsEx( count
, handles
, wait_all
, timeout
, alertable
);
249 RestoreThunkLock( mutex_count
);
253 /***********************************************************************
254 * RegisterWaitForSingleObject (KERNEL32.@)
256 BOOL WINAPI
RegisterWaitForSingleObject(PHANDLE phNewWaitObject
, HANDLE hObject
,
257 WAITORTIMERCALLBACK Callback
, PVOID Context
,
258 ULONG dwMilliseconds
, ULONG dwFlags
)
260 FIXME("%p %p %p %p %d %d\n",
261 phNewWaitObject
,hObject
,Callback
,Context
,dwMilliseconds
,dwFlags
);
265 /***********************************************************************
266 * RegisterWaitForSingleObjectEx (KERNEL32.@)
268 HANDLE WINAPI
RegisterWaitForSingleObjectEx( HANDLE hObject
,
269 WAITORTIMERCALLBACK Callback
, PVOID Context
,
270 ULONG dwMilliseconds
, ULONG dwFlags
)
272 FIXME("%p %p %p %d %d\n",
273 hObject
,Callback
,Context
,dwMilliseconds
,dwFlags
);
277 /***********************************************************************
278 * UnregisterWait (KERNEL32.@)
280 BOOL WINAPI
UnregisterWait( HANDLE WaitHandle
)
282 FIXME("%p\n",WaitHandle
);
286 /***********************************************************************
287 * UnregisterWaitEx (KERNEL32.@)
289 BOOL WINAPI
UnregisterWaitEx( HANDLE WaitHandle
, HANDLE CompletionEvent
)
291 FIXME("%p %p\n",WaitHandle
, CompletionEvent
);
295 /***********************************************************************
296 * SignalObjectAndWait (KERNEL32.@)
298 * Allows to atomically signal any of the synchro objects (semaphore,
299 * mutex, event) and wait on another.
301 DWORD WINAPI
SignalObjectAndWait( HANDLE hObjectToSignal
, HANDLE hObjectToWaitOn
,
302 DWORD dwMilliseconds
, BOOL bAlertable
)
305 LARGE_INTEGER timeout
, *ptimeout
= NULL
;
307 TRACE("%p %p %d %d\n", hObjectToSignal
,
308 hObjectToWaitOn
, dwMilliseconds
, bAlertable
);
310 if (dwMilliseconds
!= INFINITE
)
312 timeout
.QuadPart
= dwMilliseconds
* (ULONGLONG
)10000;
313 timeout
.QuadPart
= -timeout
.QuadPart
;
317 status
= NtSignalAndWaitForSingleObject( hObjectToSignal
, hObjectToWaitOn
,
318 bAlertable
, ptimeout
);
321 SetLastError( RtlNtStatusToDosError(status
) );
322 status
= WAIT_FAILED
;
327 /***********************************************************************
328 * InitializeCriticalSection (KERNEL32.@)
330 * Initialise a critical section before use.
333 * crit [O] Critical section to initialise.
336 * Nothing. If the function fails an exception is raised.
338 void WINAPI
InitializeCriticalSection( CRITICAL_SECTION
*crit
)
340 NTSTATUS ret
= RtlInitializeCriticalSection( crit
);
341 if (ret
) RtlRaiseStatus( ret
);
344 /***********************************************************************
345 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
347 * Initialise a critical section with a spin count.
350 * crit [O] Critical section to initialise.
351 * spincount [I] Number of times to spin upon contention.
355 * Failure: Nothing. If the function fails an exception is raised.
358 * spincount is ignored on uni-processor systems.
360 BOOL WINAPI
InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION
*crit
, DWORD spincount
)
362 NTSTATUS ret
= RtlInitializeCriticalSectionAndSpinCount( crit
, spincount
);
363 if (ret
) RtlRaiseStatus( ret
);
367 /***********************************************************************
368 * MakeCriticalSectionGlobal (KERNEL32.@)
370 void WINAPI
MakeCriticalSectionGlobal( CRITICAL_SECTION
*crit
)
372 /* let's assume that only one thread at a time will try to do this */
373 HANDLE sem
= crit
->LockSemaphore
;
374 if (!sem
) NtCreateSemaphore( &sem
, SEMAPHORE_ALL_ACCESS
, NULL
, 0, 1 );
375 crit
->LockSemaphore
= ConvertToGlobalHandle( sem
);
376 RtlFreeHeap( GetProcessHeap(), 0, crit
->DebugInfo
);
377 crit
->DebugInfo
= NULL
;
381 /***********************************************************************
382 * ReinitializeCriticalSection (KERNEL32.@)
384 * Initialise an already used critical section.
387 * crit [O] Critical section to initialise.
392 void WINAPI
ReinitializeCriticalSection( CRITICAL_SECTION
*crit
)
394 if ( !crit
->LockSemaphore
)
395 RtlInitializeCriticalSection( crit
);
399 /***********************************************************************
400 * UninitializeCriticalSection (KERNEL32.@)
402 * UnInitialise a critical section after use.
405 * crit [O] Critical section to uninitialise (destroy).
410 void WINAPI
UninitializeCriticalSection( CRITICAL_SECTION
*crit
)
412 RtlDeleteCriticalSection( crit
);
416 /***********************************************************************
417 * CreateEventA (KERNEL32.@)
419 HANDLE WINAPI
CreateEventA( SECURITY_ATTRIBUTES
*sa
, BOOL manual_reset
,
420 BOOL initial_state
, LPCSTR name
)
422 WCHAR buffer
[MAX_PATH
];
424 if (!name
) return CreateEventW( sa
, manual_reset
, initial_state
, NULL
);
426 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
428 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
431 return CreateEventW( sa
, manual_reset
, initial_state
, buffer
);
435 /***********************************************************************
436 * CreateEventW (KERNEL32.@)
438 HANDLE WINAPI
CreateEventW( SECURITY_ATTRIBUTES
*sa
, BOOL manual_reset
,
439 BOOL initial_state
, LPCWSTR name
)
442 UNICODE_STRING nameW
;
443 OBJECT_ATTRIBUTES attr
;
446 /* one buggy program needs this
447 * ("Van Dale Groot woordenboek der Nederlandse taal")
449 if (sa
&& IsBadReadPtr(sa
,sizeof(SECURITY_ATTRIBUTES
)))
451 ERR("Bad security attributes pointer %p\n",sa
);
452 SetLastError( ERROR_INVALID_PARAMETER
);
456 attr
.Length
= sizeof(attr
);
457 attr
.RootDirectory
= 0;
458 attr
.ObjectName
= NULL
;
459 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
|
460 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
461 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
462 attr
.SecurityQualityOfService
= NULL
;
465 RtlInitUnicodeString( &nameW
, name
);
466 attr
.ObjectName
= &nameW
;
467 attr
.RootDirectory
= get_BaseNamedObjects_handle();
470 status
= NtCreateEvent( &ret
, EVENT_ALL_ACCESS
, &attr
, manual_reset
, initial_state
);
471 if (status
== STATUS_OBJECT_NAME_EXISTS
)
472 SetLastError( ERROR_ALREADY_EXISTS
);
474 SetLastError( RtlNtStatusToDosError(status
) );
479 /***********************************************************************
480 * CreateW32Event (KERNEL.457)
482 HANDLE WINAPI
WIN16_CreateEvent( BOOL manual_reset
, BOOL initial_state
)
484 return CreateEventW( NULL
, manual_reset
, initial_state
, NULL
);
488 /***********************************************************************
489 * OpenEventA (KERNEL32.@)
491 HANDLE WINAPI
OpenEventA( DWORD access
, BOOL inherit
, LPCSTR name
)
493 WCHAR buffer
[MAX_PATH
];
495 if (!name
) return OpenEventW( access
, inherit
, NULL
);
497 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
499 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
502 return OpenEventW( access
, inherit
, buffer
);
506 /***********************************************************************
507 * OpenEventW (KERNEL32.@)
509 HANDLE WINAPI
OpenEventW( DWORD access
, BOOL inherit
, LPCWSTR name
)
512 UNICODE_STRING nameW
;
513 OBJECT_ATTRIBUTES attr
;
516 if (!is_version_nt()) access
= EVENT_ALL_ACCESS
;
518 attr
.Length
= sizeof(attr
);
519 attr
.RootDirectory
= 0;
520 attr
.ObjectName
= NULL
;
521 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| (inherit
? OBJ_INHERIT
: 0);
522 attr
.SecurityDescriptor
= NULL
;
523 attr
.SecurityQualityOfService
= NULL
;
526 RtlInitUnicodeString( &nameW
, name
);
527 attr
.ObjectName
= &nameW
;
528 attr
.RootDirectory
= get_BaseNamedObjects_handle();
531 status
= NtOpenEvent( &ret
, access
, &attr
);
532 if (status
!= STATUS_SUCCESS
)
534 SetLastError( RtlNtStatusToDosError(status
) );
540 /***********************************************************************
541 * PulseEvent (KERNEL32.@)
543 BOOL WINAPI
PulseEvent( HANDLE handle
)
547 if ((status
= NtPulseEvent( handle
, NULL
)))
548 SetLastError( RtlNtStatusToDosError(status
) );
553 /***********************************************************************
554 * SetW32Event (KERNEL.458)
555 * SetEvent (KERNEL32.@)
557 BOOL WINAPI
SetEvent( HANDLE handle
)
561 if ((status
= NtSetEvent( handle
, NULL
)))
562 SetLastError( RtlNtStatusToDosError(status
) );
567 /***********************************************************************
568 * ResetW32Event (KERNEL.459)
569 * ResetEvent (KERNEL32.@)
571 BOOL WINAPI
ResetEvent( HANDLE handle
)
575 if ((status
= NtResetEvent( handle
, NULL
)))
576 SetLastError( RtlNtStatusToDosError(status
) );
581 /***********************************************************************
582 * NOTE: The Win95 VWin32_Event routines given below are really low-level
583 * routines implemented directly by VWin32. The user-mode libraries
584 * implement Win32 synchronisation routines on top of these low-level
585 * primitives. We do it the other way around here :-)
588 /***********************************************************************
589 * VWin32_EventCreate (KERNEL.442)
591 HANDLE WINAPI
VWin32_EventCreate(VOID
)
593 HANDLE hEvent
= CreateEventW( NULL
, FALSE
, 0, NULL
);
594 return ConvertToGlobalHandle( hEvent
);
597 /***********************************************************************
598 * VWin32_EventDestroy (KERNEL.443)
600 VOID WINAPI
VWin32_EventDestroy(HANDLE event
)
602 CloseHandle( event
);
605 /***********************************************************************
606 * VWin32_EventWait (KERNEL.450)
608 VOID WINAPI
VWin32_EventWait(HANDLE event
)
612 ReleaseThunkLock( &mutex_count
);
613 WaitForSingleObject( event
, INFINITE
);
614 RestoreThunkLock( mutex_count
);
617 /***********************************************************************
618 * VWin32_EventSet (KERNEL.451)
619 * KERNEL_479 (KERNEL.479)
621 VOID WINAPI
VWin32_EventSet(HANDLE event
)
628 /***********************************************************************
629 * CreateMutexA (KERNEL32.@)
631 HANDLE WINAPI
CreateMutexA( SECURITY_ATTRIBUTES
*sa
, BOOL owner
, LPCSTR name
)
633 WCHAR buffer
[MAX_PATH
];
635 if (!name
) return CreateMutexW( sa
, owner
, NULL
);
637 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
639 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
642 return CreateMutexW( sa
, owner
, buffer
);
646 /***********************************************************************
647 * CreateMutexW (KERNEL32.@)
649 HANDLE WINAPI
CreateMutexW( SECURITY_ATTRIBUTES
*sa
, BOOL owner
, LPCWSTR name
)
652 UNICODE_STRING nameW
;
653 OBJECT_ATTRIBUTES attr
;
656 attr
.Length
= sizeof(attr
);
657 attr
.RootDirectory
= 0;
658 attr
.ObjectName
= NULL
;
659 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
|
660 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
661 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
662 attr
.SecurityQualityOfService
= NULL
;
665 RtlInitUnicodeString( &nameW
, name
);
666 attr
.ObjectName
= &nameW
;
667 attr
.RootDirectory
= get_BaseNamedObjects_handle();
670 status
= NtCreateMutant( &ret
, MUTEX_ALL_ACCESS
, &attr
, owner
);
671 if (status
== STATUS_OBJECT_NAME_EXISTS
)
672 SetLastError( ERROR_ALREADY_EXISTS
);
674 SetLastError( RtlNtStatusToDosError(status
) );
679 /***********************************************************************
680 * OpenMutexA (KERNEL32.@)
682 HANDLE WINAPI
OpenMutexA( DWORD access
, BOOL inherit
, LPCSTR name
)
684 WCHAR buffer
[MAX_PATH
];
686 if (!name
) return OpenMutexW( access
, inherit
, NULL
);
688 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
690 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
693 return OpenMutexW( access
, inherit
, buffer
);
697 /***********************************************************************
698 * OpenMutexW (KERNEL32.@)
700 HANDLE WINAPI
OpenMutexW( DWORD access
, BOOL inherit
, LPCWSTR name
)
703 UNICODE_STRING nameW
;
704 OBJECT_ATTRIBUTES attr
;
707 if (!is_version_nt()) access
= MUTEX_ALL_ACCESS
;
709 attr
.Length
= sizeof(attr
);
710 attr
.RootDirectory
= 0;
711 attr
.ObjectName
= NULL
;
712 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| (inherit
? OBJ_INHERIT
: 0);
713 attr
.SecurityDescriptor
= NULL
;
714 attr
.SecurityQualityOfService
= NULL
;
717 RtlInitUnicodeString( &nameW
, name
);
718 attr
.ObjectName
= &nameW
;
719 attr
.RootDirectory
= get_BaseNamedObjects_handle();
722 status
= NtOpenMutant( &ret
, access
, &attr
);
723 if (status
!= STATUS_SUCCESS
)
725 SetLastError( RtlNtStatusToDosError(status
) );
732 /***********************************************************************
733 * ReleaseMutex (KERNEL32.@)
735 BOOL WINAPI
ReleaseMutex( HANDLE handle
)
739 status
= NtReleaseMutant(handle
, NULL
);
740 if (status
!= STATUS_SUCCESS
)
742 SetLastError( RtlNtStatusToDosError(status
) );
754 /***********************************************************************
755 * CreateSemaphoreA (KERNEL32.@)
757 HANDLE WINAPI
CreateSemaphoreA( SECURITY_ATTRIBUTES
*sa
, LONG initial
, LONG max
, LPCSTR name
)
759 WCHAR buffer
[MAX_PATH
];
761 if (!name
) return CreateSemaphoreW( sa
, initial
, max
, NULL
);
763 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
765 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
768 return CreateSemaphoreW( sa
, initial
, max
, buffer
);
772 /***********************************************************************
773 * CreateSemaphoreW (KERNEL32.@)
775 HANDLE WINAPI
CreateSemaphoreW( SECURITY_ATTRIBUTES
*sa
, LONG initial
,
776 LONG max
, LPCWSTR name
)
779 UNICODE_STRING nameW
;
780 OBJECT_ATTRIBUTES attr
;
783 attr
.Length
= sizeof(attr
);
784 attr
.RootDirectory
= 0;
785 attr
.ObjectName
= NULL
;
786 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
|
787 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
788 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
789 attr
.SecurityQualityOfService
= NULL
;
792 RtlInitUnicodeString( &nameW
, name
);
793 attr
.ObjectName
= &nameW
;
794 attr
.RootDirectory
= get_BaseNamedObjects_handle();
797 status
= NtCreateSemaphore( &ret
, SEMAPHORE_ALL_ACCESS
, &attr
, initial
, max
);
798 if (status
== STATUS_OBJECT_NAME_EXISTS
)
799 SetLastError( ERROR_ALREADY_EXISTS
);
801 SetLastError( RtlNtStatusToDosError(status
) );
806 /***********************************************************************
807 * OpenSemaphoreA (KERNEL32.@)
809 HANDLE WINAPI
OpenSemaphoreA( DWORD access
, BOOL inherit
, LPCSTR name
)
811 WCHAR buffer
[MAX_PATH
];
813 if (!name
) return OpenSemaphoreW( access
, inherit
, NULL
);
815 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
817 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
820 return OpenSemaphoreW( access
, inherit
, buffer
);
824 /***********************************************************************
825 * OpenSemaphoreW (KERNEL32.@)
827 HANDLE WINAPI
OpenSemaphoreW( DWORD access
, BOOL inherit
, LPCWSTR name
)
830 UNICODE_STRING nameW
;
831 OBJECT_ATTRIBUTES attr
;
834 if (!is_version_nt()) access
= SEMAPHORE_ALL_ACCESS
;
836 attr
.Length
= sizeof(attr
);
837 attr
.RootDirectory
= 0;
838 attr
.ObjectName
= NULL
;
839 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| (inherit
? OBJ_INHERIT
: 0);
840 attr
.SecurityDescriptor
= NULL
;
841 attr
.SecurityQualityOfService
= NULL
;
844 RtlInitUnicodeString( &nameW
, name
);
845 attr
.ObjectName
= &nameW
;
846 attr
.RootDirectory
= get_BaseNamedObjects_handle();
849 status
= NtOpenSemaphore( &ret
, access
, &attr
);
850 if (status
!= STATUS_SUCCESS
)
852 SetLastError( RtlNtStatusToDosError(status
) );
859 /***********************************************************************
860 * ReleaseSemaphore (KERNEL32.@)
862 BOOL WINAPI
ReleaseSemaphore( HANDLE handle
, LONG count
, LONG
*previous
)
864 NTSTATUS status
= NtReleaseSemaphore( handle
, count
, (PULONG
)previous
);
865 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
875 /***********************************************************************
876 * CreateWaitableTimerA (KERNEL32.@)
878 HANDLE WINAPI
CreateWaitableTimerA( SECURITY_ATTRIBUTES
*sa
, BOOL manual
, LPCSTR name
)
880 WCHAR buffer
[MAX_PATH
];
882 if (!name
) return CreateWaitableTimerW( sa
, manual
, NULL
);
884 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
886 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
889 return CreateWaitableTimerW( sa
, manual
, buffer
);
893 /***********************************************************************
894 * CreateWaitableTimerW (KERNEL32.@)
896 HANDLE WINAPI
CreateWaitableTimerW( SECURITY_ATTRIBUTES
*sa
, BOOL manual
, LPCWSTR name
)
900 UNICODE_STRING nameW
;
901 OBJECT_ATTRIBUTES attr
;
903 attr
.Length
= sizeof(attr
);
904 attr
.RootDirectory
= 0;
905 attr
.ObjectName
= NULL
;
906 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
|
907 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
908 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
909 attr
.SecurityQualityOfService
= NULL
;
912 RtlInitUnicodeString( &nameW
, name
);
913 attr
.ObjectName
= &nameW
;
914 attr
.RootDirectory
= get_BaseNamedObjects_handle();
917 status
= NtCreateTimer(&handle
, TIMER_ALL_ACCESS
, &attr
,
918 manual
? NotificationTimer
: SynchronizationTimer
);
919 if (status
== STATUS_OBJECT_NAME_EXISTS
)
920 SetLastError( ERROR_ALREADY_EXISTS
);
922 SetLastError( RtlNtStatusToDosError(status
) );
927 /***********************************************************************
928 * OpenWaitableTimerA (KERNEL32.@)
930 HANDLE WINAPI
OpenWaitableTimerA( DWORD access
, BOOL inherit
, LPCSTR name
)
932 WCHAR buffer
[MAX_PATH
];
934 if (!name
) return OpenWaitableTimerW( access
, inherit
, NULL
);
936 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
938 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
941 return OpenWaitableTimerW( access
, inherit
, buffer
);
945 /***********************************************************************
946 * OpenWaitableTimerW (KERNEL32.@)
948 HANDLE WINAPI
OpenWaitableTimerW( DWORD access
, BOOL inherit
, LPCWSTR name
)
951 UNICODE_STRING nameW
;
952 OBJECT_ATTRIBUTES attr
;
955 if (!is_version_nt()) access
= TIMER_ALL_ACCESS
;
957 attr
.Length
= sizeof(attr
);
958 attr
.RootDirectory
= 0;
959 attr
.ObjectName
= NULL
;
960 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| (inherit
? OBJ_INHERIT
: 0);
961 attr
.SecurityDescriptor
= NULL
;
962 attr
.SecurityQualityOfService
= NULL
;
965 RtlInitUnicodeString( &nameW
, name
);
966 attr
.ObjectName
= &nameW
;
967 attr
.RootDirectory
= get_BaseNamedObjects_handle();
970 status
= NtOpenTimer(&handle
, access
, &attr
);
971 if (status
!= STATUS_SUCCESS
)
973 SetLastError( RtlNtStatusToDosError(status
) );
980 /***********************************************************************
981 * SetWaitableTimer (KERNEL32.@)
983 BOOL WINAPI
SetWaitableTimer( HANDLE handle
, const LARGE_INTEGER
*when
, LONG period
,
984 PTIMERAPCROUTINE callback
, LPVOID arg
, BOOL resume
)
986 NTSTATUS status
= NtSetTimer(handle
, when
, (PTIMER_APC_ROUTINE
)callback
,
987 arg
, resume
, period
, NULL
);
989 if (status
!= STATUS_SUCCESS
)
991 SetLastError( RtlNtStatusToDosError(status
) );
992 if (status
!= STATUS_TIMER_RESUME_IGNORED
) return FALSE
;
998 /***********************************************************************
999 * CancelWaitableTimer (KERNEL32.@)
1001 BOOL WINAPI
CancelWaitableTimer( HANDLE handle
)
1005 status
= NtCancelTimer(handle
, NULL
);
1006 if (status
!= STATUS_SUCCESS
)
1008 SetLastError( RtlNtStatusToDosError(status
) );
1015 /***********************************************************************
1016 * CreateTimerQueue (KERNEL32.@)
1018 HANDLE WINAPI
CreateTimerQueue(void)
1021 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1026 /***********************************************************************
1027 * DeleteTimerQueueEx (KERNEL32.@)
1029 BOOL WINAPI
DeleteTimerQueueEx(HANDLE TimerQueue
, HANDLE CompletionEvent
)
1031 FIXME("(%p, %p): stub\n", TimerQueue
, CompletionEvent
);
1032 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1036 /***********************************************************************
1037 * CreateTimerQueueTimer (KERNEL32.@)
1039 * Creates a timer-queue timer. This timer expires at the specified due
1040 * time (in ms), then after every specified period (in ms). When the timer
1041 * expires, the callback function is called.
1044 * nonzero on success or zero on faillure
1049 BOOL WINAPI
CreateTimerQueueTimer( PHANDLE phNewTimer
, HANDLE TimerQueue
,
1050 WAITORTIMERCALLBACK Callback
, PVOID Parameter
,
1051 DWORD DueTime
, DWORD Period
, ULONG Flags
)
1054 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1058 /***********************************************************************
1059 * DeleteTimerQueueTimer (KERNEL32.@)
1061 * Cancels a timer-queue timer.
1064 * nonzero on success or zero on faillure
1069 BOOL WINAPI
DeleteTimerQueueTimer( HANDLE TimerQueue
, HANDLE Timer
,
1070 HANDLE CompletionEvent
)
1073 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1083 /***********************************************************************
1084 * CreateNamedPipeA (KERNEL32.@)
1086 HANDLE WINAPI
CreateNamedPipeA( LPCSTR name
, DWORD dwOpenMode
,
1087 DWORD dwPipeMode
, DWORD nMaxInstances
,
1088 DWORD nOutBufferSize
, DWORD nInBufferSize
,
1089 DWORD nDefaultTimeOut
, LPSECURITY_ATTRIBUTES attr
)
1091 WCHAR buffer
[MAX_PATH
];
1093 if (!name
) return CreateNamedPipeW( NULL
, dwOpenMode
, dwPipeMode
, nMaxInstances
,
1094 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, attr
);
1096 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1098 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1099 return INVALID_HANDLE_VALUE
;
1101 return CreateNamedPipeW( buffer
, dwOpenMode
, dwPipeMode
, nMaxInstances
,
1102 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, attr
);
1106 /***********************************************************************
1107 * CreateNamedPipeW (KERNEL32.@)
1109 HANDLE WINAPI
CreateNamedPipeW( LPCWSTR name
, DWORD dwOpenMode
,
1110 DWORD dwPipeMode
, DWORD nMaxInstances
,
1111 DWORD nOutBufferSize
, DWORD nInBufferSize
,
1112 DWORD nDefaultTimeOut
, LPSECURITY_ATTRIBUTES sa
)
1115 UNICODE_STRING nt_name
;
1116 OBJECT_ATTRIBUTES attr
;
1118 BOOLEAN pipe_type
, read_mode
, non_block
;
1120 IO_STATUS_BLOCK iosb
;
1121 LARGE_INTEGER timeout
;
1123 TRACE("(%s, %#08x, %#08x, %d, %d, %d, %d, %p)\n",
1124 debugstr_w(name
), dwOpenMode
, dwPipeMode
, nMaxInstances
,
1125 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, sa
);
1127 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1129 SetLastError( ERROR_PATH_NOT_FOUND
);
1130 return INVALID_HANDLE_VALUE
;
1132 if (nt_name
.Length
>= MAX_PATH
* sizeof(WCHAR
) )
1134 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1135 RtlFreeUnicodeString( &nt_name
);
1136 return INVALID_HANDLE_VALUE
;
1139 attr
.Length
= sizeof(attr
);
1140 attr
.RootDirectory
= 0;
1141 attr
.ObjectName
= &nt_name
;
1142 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
1143 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
1144 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1145 attr
.SecurityQualityOfService
= NULL
;
1148 if (dwOpenMode
& FILE_FLAG_WRITE_THROUGH
) options
|= FILE_WRITE_THROUGH
;
1149 if (!(dwOpenMode
& FILE_FLAG_OVERLAPPED
)) options
|= FILE_SYNCHRONOUS_IO_ALERT
;
1150 if ((dwOpenMode
& PIPE_ACCESS_DUPLEX
) == PIPE_ACCESS_DUPLEX
)
1151 options
|= FILE_PIPE_FULL_DUPLEX
;
1152 else if (dwOpenMode
& PIPE_ACCESS_INBOUND
) options
|= FILE_PIPE_INBOUND
;
1153 else if (dwOpenMode
& PIPE_ACCESS_OUTBOUND
) options
|= FILE_PIPE_OUTBOUND
;
1154 pipe_type
= (dwPipeMode
& PIPE_TYPE_MESSAGE
) ? TRUE
: FALSE
;
1155 read_mode
= (dwPipeMode
& PIPE_READMODE_MESSAGE
) ? TRUE
: FALSE
;
1156 non_block
= (dwPipeMode
& PIPE_NOWAIT
) ? TRUE
: FALSE
;
1157 if (nMaxInstances
>= PIPE_UNLIMITED_INSTANCES
) nMaxInstances
= ~0UL;
1159 timeout
.QuadPart
= (ULONGLONG
)nDefaultTimeOut
* -10000;
1163 status
= NtCreateNamedPipeFile(&handle
, GENERIC_READ
|GENERIC_WRITE
, &attr
, &iosb
,
1164 0, FILE_OVERWRITE_IF
, options
, pipe_type
,
1165 read_mode
, non_block
, nMaxInstances
,
1166 nInBufferSize
, nOutBufferSize
, &timeout
);
1168 RtlFreeUnicodeString( &nt_name
);
1171 handle
= INVALID_HANDLE_VALUE
;
1172 SetLastError( RtlNtStatusToDosError(status
) );
1178 /***********************************************************************
1179 * PeekNamedPipe (KERNEL32.@)
1181 BOOL WINAPI
PeekNamedPipe( HANDLE hPipe
, LPVOID lpvBuffer
, DWORD cbBuffer
,
1182 LPDWORD lpcbRead
, LPDWORD lpcbAvail
, LPDWORD lpcbMessage
)
1184 FILE_PIPE_PEEK_BUFFER local_buffer
;
1185 FILE_PIPE_PEEK_BUFFER
*buffer
= &local_buffer
;
1189 if (cbBuffer
&& !(buffer
= HeapAlloc( GetProcessHeap(), 0,
1190 FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER
, Data
[cbBuffer
] ))))
1192 SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
1196 status
= NtFsControlFile( hPipe
, 0, NULL
, NULL
, &io
, FSCTL_PIPE_PEEK
, NULL
, 0,
1197 buffer
, FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER
, Data
[cbBuffer
] ) );
1200 ULONG read_size
= io
.Information
- FIELD_OFFSET( FILE_PIPE_PEEK_BUFFER
, Data
);
1201 if (lpcbAvail
) *lpcbAvail
= buffer
->ReadDataAvailable
;
1202 if (lpcbRead
) *lpcbRead
= read_size
;
1203 if (lpcbMessage
) *lpcbMessage
= 0; /* FIXME */
1204 if (lpvBuffer
) memcpy( lpvBuffer
, buffer
->Data
, read_size
);
1206 else SetLastError( RtlNtStatusToDosError(status
) );
1208 if (buffer
!= &local_buffer
) HeapFree( GetProcessHeap(), 0, buffer
);
1212 /***********************************************************************
1213 * WaitNamedPipeA (KERNEL32.@)
1215 BOOL WINAPI
WaitNamedPipeA (LPCSTR name
, DWORD nTimeOut
)
1217 WCHAR buffer
[MAX_PATH
];
1219 if (!name
) return WaitNamedPipeW( NULL
, nTimeOut
);
1221 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1223 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1226 return WaitNamedPipeW( buffer
, nTimeOut
);
1230 /***********************************************************************
1231 * WaitNamedPipeW (KERNEL32.@)
1233 * Waits for a named pipe instance to become available
1236 * name [I] Pointer to a named pipe name to wait for
1237 * nTimeOut [I] How long to wait in ms
1240 * TRUE: Success, named pipe can be opened with CreateFile
1241 * FALSE: Failure, GetLastError can be called for further details
1243 BOOL WINAPI
WaitNamedPipeW (LPCWSTR name
, DWORD nTimeOut
)
1245 static const WCHAR leadin
[] = {'\\','?','?','\\','P','I','P','E','\\'};
1247 UNICODE_STRING nt_name
, pipe_dev_name
;
1248 FILE_PIPE_WAIT_FOR_BUFFER
*pipe_wait
;
1249 IO_STATUS_BLOCK iosb
;
1250 OBJECT_ATTRIBUTES attr
;
1254 TRACE("%s 0x%08x\n",debugstr_w(name
),nTimeOut
);
1256 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1259 if (nt_name
.Length
>= MAX_PATH
* sizeof(WCHAR
) ||
1260 nt_name
.Length
< sizeof(leadin
) ||
1261 strncmpiW( nt_name
.Buffer
, leadin
, sizeof(leadin
)/sizeof(WCHAR
) != 0))
1263 RtlFreeUnicodeString( &nt_name
);
1264 SetLastError( ERROR_PATH_NOT_FOUND
);
1268 sz_pipe_wait
= sizeof(*pipe_wait
) + nt_name
.Length
- sizeof(leadin
) - sizeof(WCHAR
);
1269 if (!(pipe_wait
= HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait
)))
1271 RtlFreeUnicodeString( &nt_name
);
1272 SetLastError( ERROR_OUTOFMEMORY
);
1276 pipe_dev_name
.Buffer
= nt_name
.Buffer
;
1277 pipe_dev_name
.Length
= sizeof(leadin
);
1278 pipe_dev_name
.MaximumLength
= sizeof(leadin
);
1279 InitializeObjectAttributes(&attr
,&pipe_dev_name
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
1280 status
= NtOpenFile( &pipe_dev
, FILE_READ_ATTRIBUTES
, &attr
,
1281 &iosb
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1282 FILE_SYNCHRONOUS_IO_NONALERT
);
1283 if (status
!= ERROR_SUCCESS
)
1285 SetLastError( ERROR_PATH_NOT_FOUND
);
1289 pipe_wait
->TimeoutSpecified
= !(nTimeOut
== NMPWAIT_USE_DEFAULT_WAIT
);
1290 pipe_wait
->Timeout
.QuadPart
= nTimeOut
* -10000L;
1291 pipe_wait
->NameLength
= nt_name
.Length
- sizeof(leadin
);
1292 memcpy(pipe_wait
->Name
, nt_name
.Buffer
+ sizeof(leadin
)/sizeof(WCHAR
),
1293 pipe_wait
->NameLength
);
1294 RtlFreeUnicodeString( &nt_name
);
1296 status
= NtFsControlFile( pipe_dev
, NULL
, NULL
, NULL
, &iosb
, FSCTL_PIPE_WAIT
,
1297 pipe_wait
, sz_pipe_wait
, NULL
, 0 );
1299 HeapFree( GetProcessHeap(), 0, pipe_wait
);
1300 NtClose( pipe_dev
);
1302 if(status
!= STATUS_SUCCESS
)
1304 SetLastError(RtlNtStatusToDosError(status
));
1312 /***********************************************************************
1313 * ConnectNamedPipe (KERNEL32.@)
1315 * Connects to a named pipe
1318 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1319 * overlapped: Optional OVERLAPPED struct
1323 * FALSE: Failure, GetLastError can be called for further details
1325 BOOL WINAPI
ConnectNamedPipe(HANDLE hPipe
, LPOVERLAPPED overlapped
)
1328 IO_STATUS_BLOCK status_block
;
1330 TRACE("(%p,%p)\n", hPipe
, overlapped
);
1333 overlapped
->Internal
= STATUS_PENDING
;
1335 status
= NtFsControlFile(hPipe
, overlapped
? overlapped
->hEvent
: NULL
, NULL
, NULL
,
1336 overlapped
? (IO_STATUS_BLOCK
*)overlapped
: &status_block
,
1337 FSCTL_PIPE_LISTEN
, NULL
, 0, NULL
, 0);
1339 if (status
== STATUS_SUCCESS
) return TRUE
;
1340 SetLastError( RtlNtStatusToDosError(status
) );
1344 /***********************************************************************
1345 * DisconnectNamedPipe (KERNEL32.@)
1347 * Disconnects from a named pipe
1350 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1354 * FALSE: Failure, GetLastError can be called for further details
1356 BOOL WINAPI
DisconnectNamedPipe(HANDLE hPipe
)
1359 IO_STATUS_BLOCK io_block
;
1361 TRACE("(%p)\n",hPipe
);
1363 status
= NtFsControlFile(hPipe
, 0, NULL
, NULL
, &io_block
, FSCTL_PIPE_DISCONNECT
,
1365 if (status
== STATUS_SUCCESS
) return TRUE
;
1366 SetLastError( RtlNtStatusToDosError(status
) );
1370 /***********************************************************************
1371 * TransactNamedPipe (KERNEL32.@)
1374 * should be done as a single operation in the wineserver or kernel
1376 BOOL WINAPI
TransactNamedPipe(
1377 HANDLE handle
, LPVOID lpInput
, DWORD dwInputSize
, LPVOID lpOutput
,
1378 DWORD dwOutputSize
, LPDWORD lpBytesRead
, LPOVERLAPPED lpOverlapped
)
1383 TRACE("%p %p %d %p %d %p %p\n",
1384 handle
, lpInput
, dwInputSize
, lpOutput
,
1385 dwOutputSize
, lpBytesRead
, lpOverlapped
);
1389 FIXME("Doesn't support overlapped operation as yet\n");
1393 r
= WriteFile(handle
, lpOutput
, dwOutputSize
, &count
, NULL
);
1395 r
= ReadFile(handle
, lpInput
, dwInputSize
, lpBytesRead
, NULL
);
1400 /***********************************************************************
1401 * GetNamedPipeInfo (KERNEL32.@)
1403 BOOL WINAPI
GetNamedPipeInfo(
1404 HANDLE hNamedPipe
, LPDWORD lpFlags
, LPDWORD lpOutputBufferSize
,
1405 LPDWORD lpInputBufferSize
, LPDWORD lpMaxInstances
)
1407 FILE_PIPE_LOCAL_INFORMATION fpli
;
1408 IO_STATUS_BLOCK iosb
;
1411 status
= NtQueryInformationFile(hNamedPipe
, &iosb
, &fpli
, sizeof(fpli
),
1412 FilePipeLocalInformation
);
1415 SetLastError( RtlNtStatusToDosError(status
) );
1421 *lpFlags
= (fpli
.NamedPipeEnd
& FILE_PIPE_SERVER_END
) ?
1422 PIPE_SERVER_END
: PIPE_CLIENT_END
;
1423 *lpFlags
|= (fpli
.NamedPipeType
& FILE_PIPE_TYPE_MESSAGE
) ?
1424 PIPE_TYPE_MESSAGE
: PIPE_TYPE_BYTE
;
1427 if (lpOutputBufferSize
) *lpOutputBufferSize
= fpli
.OutboundQuota
;
1428 if (lpInputBufferSize
) *lpInputBufferSize
= fpli
.InboundQuota
;
1429 if (lpMaxInstances
) *lpMaxInstances
= fpli
.MaximumInstances
;
1434 /***********************************************************************
1435 * GetNamedPipeHandleStateA (KERNEL32.@)
1437 BOOL WINAPI
GetNamedPipeHandleStateA(
1438 HANDLE hNamedPipe
, LPDWORD lpState
, LPDWORD lpCurInstances
,
1439 LPDWORD lpMaxCollectionCount
, LPDWORD lpCollectDataTimeout
,
1440 LPSTR lpUsername
, DWORD nUsernameMaxSize
)
1442 FIXME("%p %p %p %p %p %p %d\n",
1443 hNamedPipe
, lpState
, lpCurInstances
,
1444 lpMaxCollectionCount
, lpCollectDataTimeout
,
1445 lpUsername
, nUsernameMaxSize
);
1450 /***********************************************************************
1451 * GetNamedPipeHandleStateW (KERNEL32.@)
1453 BOOL WINAPI
GetNamedPipeHandleStateW(
1454 HANDLE hNamedPipe
, LPDWORD lpState
, LPDWORD lpCurInstances
,
1455 LPDWORD lpMaxCollectionCount
, LPDWORD lpCollectDataTimeout
,
1456 LPWSTR lpUsername
, DWORD nUsernameMaxSize
)
1458 FIXME("%p %p %p %p %p %p %d\n",
1459 hNamedPipe
, lpState
, lpCurInstances
,
1460 lpMaxCollectionCount
, lpCollectDataTimeout
,
1461 lpUsername
, nUsernameMaxSize
);
1466 /***********************************************************************
1467 * SetNamedPipeHandleState (KERNEL32.@)
1469 BOOL WINAPI
SetNamedPipeHandleState(
1470 HANDLE hNamedPipe
, LPDWORD lpMode
, LPDWORD lpMaxCollectionCount
,
1471 LPDWORD lpCollectDataTimeout
)
1473 /* should be a fixme, but this function is called a lot by the RPC
1474 * runtime, and it slows down InstallShield a fair bit. */
1475 WARN("stub: %p %p/%d %p %p\n",
1476 hNamedPipe
, lpMode
, lpMode
? *lpMode
: 0, lpMaxCollectionCount
, lpCollectDataTimeout
);
1480 /***********************************************************************
1481 * CallNamedPipeA (KERNEL32.@)
1483 BOOL WINAPI
CallNamedPipeA(
1484 LPCSTR lpNamedPipeName
, LPVOID lpInput
, DWORD dwInputSize
,
1485 LPVOID lpOutput
, DWORD dwOutputSize
,
1486 LPDWORD lpBytesRead
, DWORD nTimeout
)
1492 TRACE("%s %p %d %p %d %p %d\n",
1493 debugstr_a(lpNamedPipeName
), lpInput
, dwInputSize
,
1494 lpOutput
, dwOutputSize
, lpBytesRead
, nTimeout
);
1496 if( lpNamedPipeName
)
1498 len
= MultiByteToWideChar( CP_ACP
, 0, lpNamedPipeName
, -1, NULL
, 0 );
1499 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1500 MultiByteToWideChar( CP_ACP
, 0, lpNamedPipeName
, -1, str
, len
);
1502 ret
= CallNamedPipeW( str
, lpInput
, dwInputSize
, lpOutput
,
1503 dwOutputSize
, lpBytesRead
, nTimeout
);
1504 if( lpNamedPipeName
)
1505 HeapFree( GetProcessHeap(), 0, str
);
1510 /***********************************************************************
1511 * CallNamedPipeW (KERNEL32.@)
1513 BOOL WINAPI
CallNamedPipeW(
1514 LPCWSTR lpNamedPipeName
, LPVOID lpInput
, DWORD lpInputSize
,
1515 LPVOID lpOutput
, DWORD lpOutputSize
,
1516 LPDWORD lpBytesRead
, DWORD nTimeout
)
1518 FIXME("%s %p %d %p %d %p %d\n",
1519 debugstr_w(lpNamedPipeName
), lpInput
, lpInputSize
,
1520 lpOutput
, lpOutputSize
, lpBytesRead
, nTimeout
);
1524 /******************************************************************
1525 * CreatePipe (KERNEL32.@)
1528 BOOL WINAPI
CreatePipe( PHANDLE hReadPipe
, PHANDLE hWritePipe
,
1529 LPSECURITY_ATTRIBUTES sa
, DWORD size
)
1531 static unsigned index
/* = 0 */;
1534 unsigned in_index
= index
;
1535 UNICODE_STRING nt_name
;
1536 OBJECT_ATTRIBUTES attr
;
1538 IO_STATUS_BLOCK iosb
;
1539 LARGE_INTEGER timeout
;
1541 *hReadPipe
= *hWritePipe
= INVALID_HANDLE_VALUE
;
1543 attr
.Length
= sizeof(attr
);
1544 attr
.RootDirectory
= 0;
1545 attr
.ObjectName
= &nt_name
;
1546 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
1547 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
1548 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1549 attr
.SecurityQualityOfService
= NULL
;
1551 timeout
.QuadPart
= (ULONGLONG
)NMPWAIT_USE_DEFAULT_WAIT
* -10000;
1552 /* generate a unique pipe name (system wide) */
1555 static const WCHAR nameFmt
[] = { '\\','?','?','\\','p','i','p','e',
1556 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1557 'u','.','%','0','8','u','\0' };
1559 snprintfW(name
, sizeof(name
) / sizeof(name
[0]), nameFmt
,
1560 GetCurrentProcessId(), ++index
);
1561 RtlInitUnicodeString(&nt_name
, name
);
1562 status
= NtCreateNamedPipeFile(&hr
, GENERIC_READ
| SYNCHRONIZE
, &attr
, &iosb
,
1563 0, FILE_OVERWRITE_IF
,
1564 FILE_SYNCHRONOUS_IO_ALERT
| FILE_PIPE_INBOUND
,
1565 FALSE
, FALSE
, FALSE
,
1566 1, size
, size
, &timeout
);
1569 SetLastError( RtlNtStatusToDosError(status
) );
1570 hr
= INVALID_HANDLE_VALUE
;
1572 } while (hr
== INVALID_HANDLE_VALUE
&& index
!= in_index
);
1573 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1574 if (hr
== INVALID_HANDLE_VALUE
) return FALSE
;
1576 status
= NtOpenFile(&hw
, GENERIC_WRITE
| SYNCHRONIZE
, &attr
, &iosb
, 0,
1577 FILE_SYNCHRONOUS_IO_ALERT
| FILE_NON_DIRECTORY_FILE
);
1581 SetLastError( RtlNtStatusToDosError(status
) );
1592 /******************************************************************************
1593 * CreateMailslotA [KERNEL32.@]
1595 * See CreatMailslotW.
1597 HANDLE WINAPI
CreateMailslotA( LPCSTR lpName
, DWORD nMaxMessageSize
,
1598 DWORD lReadTimeout
, LPSECURITY_ATTRIBUTES sa
)
1604 TRACE("%s %d %d %p\n", debugstr_a(lpName
),
1605 nMaxMessageSize
, lReadTimeout
, sa
);
1609 len
= MultiByteToWideChar( CP_ACP
, 0, lpName
, -1, NULL
, 0 );
1610 name
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1611 MultiByteToWideChar( CP_ACP
, 0, lpName
, -1, name
, len
);
1614 handle
= CreateMailslotW( name
, nMaxMessageSize
, lReadTimeout
, sa
);
1616 HeapFree( GetProcessHeap(), 0, name
);
1622 /******************************************************************************
1623 * CreateMailslotW [KERNEL32.@]
1625 * Create a mailslot with specified name.
1628 * lpName [I] Pointer to string for mailslot name
1629 * nMaxMessageSize [I] Maximum message size
1630 * lReadTimeout [I] Milliseconds before read time-out
1631 * sa [I] Pointer to security structure
1634 * Success: Handle to mailslot
1635 * Failure: INVALID_HANDLE_VALUE
1637 HANDLE WINAPI
CreateMailslotW( LPCWSTR lpName
, DWORD nMaxMessageSize
,
1638 DWORD lReadTimeout
, LPSECURITY_ATTRIBUTES sa
)
1640 HANDLE handle
= INVALID_HANDLE_VALUE
;
1641 OBJECT_ATTRIBUTES attr
;
1642 UNICODE_STRING nameW
;
1643 LARGE_INTEGER timeout
;
1644 IO_STATUS_BLOCK iosb
;
1647 TRACE("%s %d %d %p\n", debugstr_w(lpName
),
1648 nMaxMessageSize
, lReadTimeout
, sa
);
1650 if (!RtlDosPathNameToNtPathName_U( lpName
, &nameW
, NULL
, NULL
))
1652 SetLastError( ERROR_PATH_NOT_FOUND
);
1653 return INVALID_HANDLE_VALUE
;
1656 if (nameW
.Length
>= MAX_PATH
* sizeof(WCHAR
) )
1658 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1659 RtlFreeUnicodeString( &nameW
);
1660 return INVALID_HANDLE_VALUE
;
1663 attr
.Length
= sizeof(attr
);
1664 attr
.RootDirectory
= 0;
1665 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1666 attr
.ObjectName
= &nameW
;
1667 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1668 attr
.SecurityQualityOfService
= NULL
;
1670 if (lReadTimeout
!= MAILSLOT_WAIT_FOREVER
)
1671 timeout
.QuadPart
= (ULONGLONG
) lReadTimeout
* -10000;
1673 timeout
.QuadPart
= ((LONGLONG
)0x7fffffff << 32) | 0xffffffff;
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 %d\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, %08x): stub.\n",
1777 hFileHandle
, hExistingCompletionPort
, CompletionKey
, dwNumberOfConcurrentThreads
);
1778 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1783 /******************************************************************************
1784 * GetQueuedCompletionStatus (KERNEL32.@)
1786 BOOL WINAPI
GetQueuedCompletionStatus( HANDLE CompletionPort
, LPDWORD lpNumberOfBytesTransferred
,
1787 PULONG_PTR pCompletionKey
, LPOVERLAPPED
*lpOverlapped
,
1788 DWORD dwMilliseconds
)
1790 FIXME("(%p,%p,%p,%p,%d), stub!\n",
1791 CompletionPort
,lpNumberOfBytesTransferred
,pCompletionKey
,lpOverlapped
,dwMilliseconds
);
1792 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1796 BOOL WINAPI
PostQueuedCompletionStatus( HANDLE CompletionPort
, DWORD dwNumberOfBytes
,
1797 ULONG_PTR dwCompletionKey
, LPOVERLAPPED lpOverlapped
)
1799 FIXME("%p %d %08lx %p\n", CompletionPort
, dwNumberOfBytes
, dwCompletionKey
, lpOverlapped
);
1800 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1804 /******************************************************************************
1805 * CreateJobObjectW (KERNEL32.@)
1807 HANDLE WINAPI
CreateJobObjectW( LPSECURITY_ATTRIBUTES attr
, LPCWSTR name
)
1809 FIXME("%p %s\n", attr
, debugstr_w(name
) );
1810 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1814 /******************************************************************************
1815 * CreateJobObjectA (KERNEL32.@)
1817 HANDLE WINAPI
CreateJobObjectA( LPSECURITY_ATTRIBUTES attr
, LPCSTR name
)
1823 TRACE("%p %s\n", attr
, debugstr_a(name
) );
1827 len
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
1828 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1831 SetLastError( ERROR_OUTOFMEMORY
);
1834 len
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, str
, len
);
1837 r
= CreateJobObjectW( attr
, str
);
1839 HeapFree( GetProcessHeap(), 0, str
);
1844 /******************************************************************************
1845 * AssignProcessToJobObject (KERNEL32.@)
1847 BOOL WINAPI
AssignProcessToJobObject( HANDLE hJob
, HANDLE hProcess
)
1849 FIXME("%p %p\n", hJob
, hProcess
);
1855 /***********************************************************************
1856 * InterlockedCompareExchange (KERNEL32.@)
1858 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1859 __ASM_GLOBAL_FUNC(InterlockedCompareExchange
,
1860 "movl 12(%esp),%eax\n\t"
1861 "movl 8(%esp),%ecx\n\t"
1862 "movl 4(%esp),%edx\n\t"
1863 "lock; cmpxchgl %ecx,(%edx)\n\t"
1866 /***********************************************************************
1867 * InterlockedExchange (KERNEL32.@)
1869 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1870 __ASM_GLOBAL_FUNC(InterlockedExchange
,
1871 "movl 8(%esp),%eax\n\t"
1872 "movl 4(%esp),%edx\n\t"
1873 "lock; xchgl %eax,(%edx)\n\t"
1876 /***********************************************************************
1877 * InterlockedExchangeAdd (KERNEL32.@)
1879 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1880 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd
,
1881 "movl 8(%esp),%eax\n\t"
1882 "movl 4(%esp),%edx\n\t"
1883 "lock; xaddl %eax,(%edx)\n\t"
1886 /***********************************************************************
1887 * InterlockedIncrement (KERNEL32.@)
1889 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1890 __ASM_GLOBAL_FUNC(InterlockedIncrement
,
1891 "movl 4(%esp),%edx\n\t"
1893 "lock; xaddl %eax,(%edx)\n\t"
1897 /***********************************************************************
1898 * InterlockedDecrement (KERNEL32.@)
1900 __ASM_GLOBAL_FUNC(InterlockedDecrement
,
1901 "movl 4(%esp),%edx\n\t"
1903 "lock; xaddl %eax,(%edx)\n\t"
1907 #else /* __i386__ */
1909 /***********************************************************************
1910 * InterlockedCompareExchange (KERNEL32.@)
1912 * Atomically swap one value with another.
1915 * dest [I/O] The value to replace
1916 * xchq [I] The value to be swapped
1917 * compare [I] The value to compare to dest
1920 * The resulting value of dest.
1923 * dest is updated only if it is equal to compare, otherwise no swap is done.
1925 LONG WINAPI
InterlockedCompareExchange( LONG
volatile *dest
, LONG xchg
, LONG compare
)
1927 return interlocked_cmpxchg( (int *)dest
, xchg
, compare
);
1930 /***********************************************************************
1931 * InterlockedExchange (KERNEL32.@)
1933 * Atomically swap one value with another.
1936 * dest [I/O] The value to replace
1937 * val [I] The value to be swapped
1940 * The resulting value of dest.
1942 LONG WINAPI
InterlockedExchange( LONG
volatile *dest
, LONG val
)
1944 return interlocked_xchg( (int *)dest
, val
);
1947 /***********************************************************************
1948 * InterlockedExchangeAdd (KERNEL32.@)
1950 * Atomically add one value to another.
1953 * dest [I/O] The value to add to
1954 * incr [I] The value to be added
1957 * The resulting value of dest.
1959 LONG WINAPI
InterlockedExchangeAdd( LONG
volatile *dest
, LONG incr
)
1961 return interlocked_xchg_add( (int *)dest
, incr
);
1964 /***********************************************************************
1965 * InterlockedIncrement (KERNEL32.@)
1967 * Atomically increment a value.
1970 * dest [I/O] The value to increment
1973 * The resulting value of dest.
1975 LONG WINAPI
InterlockedIncrement( LONG
volatile *dest
)
1977 return interlocked_xchg_add( (int *)dest
, 1 ) + 1;
1980 /***********************************************************************
1981 * InterlockedDecrement (KERNEL32.@)
1983 * Atomically decrement a value.
1986 * dest [I/O] The value to decrement
1989 * The resulting value of dest.
1991 LONG WINAPI
InterlockedDecrement( LONG
volatile *dest
)
1993 return interlocked_xchg_add( (int *)dest
, -1 ) - 1;
1996 #endif /* __i386__ */