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
48 #define WIN32_NO_STATUS
57 #include "wine/server.h"
58 #include "wine/unicode.h"
59 #include "wine/winbase16.h"
60 #include "kernel_private.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(sync
);
66 /* check if current version is NT or Win95 */
67 inline static int is_version_nt(void)
69 return !(GetVersion() & 0x80000000);
72 /* returns directory handle to \\BaseNamedObjects */
73 HANDLE
get_BaseNamedObjects_handle(void)
75 static HANDLE handle
= NULL
;
76 static const WCHAR basenameW
[] =
77 {'\\','B','a','s','e','N','a','m','e','d','O','b','j','e','c','t','s',0};
79 OBJECT_ATTRIBUTES attr
;
85 RtlInitUnicodeString(&str
, basenameW
);
86 InitializeObjectAttributes(&attr
, &str
, 0, 0, NULL
);
87 NtOpenDirectoryObject(&dir
, DIRECTORY_CREATE_OBJECT
|DIRECTORY_TRAVERSE
,
89 if (InterlockedCompareExchangePointer( (PVOID
)&handle
, dir
, 0 ) != 0)
91 /* someone beat us here... */
98 /***********************************************************************
101 VOID WINAPI
Sleep( DWORD timeout
)
103 SleepEx( timeout
, FALSE
);
106 /******************************************************************************
107 * SleepEx (KERNEL32.@)
109 DWORD WINAPI
SleepEx( DWORD timeout
, BOOL alertable
)
113 if (timeout
== INFINITE
) status
= NtDelayExecution( alertable
, NULL
);
118 time
.QuadPart
= timeout
* (ULONGLONG
)10000;
119 time
.QuadPart
= -time
.QuadPart
;
120 status
= NtDelayExecution( alertable
, &time
);
122 if (status
!= STATUS_USER_APC
) status
= STATUS_SUCCESS
;
127 /***********************************************************************
128 * SwitchToThread (KERNEL32.@)
130 BOOL WINAPI
SwitchToThread(void)
132 return (NtYieldExecution() != STATUS_NO_YIELD_PERFORMED
);
136 /***********************************************************************
137 * WaitForSingleObject (KERNEL32.@)
139 DWORD WINAPI
WaitForSingleObject( HANDLE handle
, DWORD timeout
)
141 return WaitForMultipleObjectsEx( 1, &handle
, FALSE
, timeout
, FALSE
);
145 /***********************************************************************
146 * WaitForSingleObjectEx (KERNEL32.@)
148 DWORD WINAPI
WaitForSingleObjectEx( HANDLE handle
, DWORD timeout
,
151 return WaitForMultipleObjectsEx( 1, &handle
, FALSE
, timeout
, alertable
);
155 /***********************************************************************
156 * WaitForMultipleObjects (KERNEL32.@)
158 DWORD WINAPI
WaitForMultipleObjects( DWORD count
, const HANDLE
*handles
,
159 BOOL wait_all
, DWORD timeout
)
161 return WaitForMultipleObjectsEx( count
, handles
, wait_all
, timeout
, FALSE
);
165 /***********************************************************************
166 * WaitForMultipleObjectsEx (KERNEL32.@)
168 DWORD WINAPI
WaitForMultipleObjectsEx( DWORD count
, const HANDLE
*handles
,
169 BOOL wait_all
, DWORD timeout
,
173 HANDLE hloc
[MAXIMUM_WAIT_OBJECTS
];
176 if (count
> MAXIMUM_WAIT_OBJECTS
)
178 SetLastError(ERROR_INVALID_PARAMETER
);
181 for (i
= 0; i
< count
; i
++)
183 if ((handles
[i
] == (HANDLE
)STD_INPUT_HANDLE
) ||
184 (handles
[i
] == (HANDLE
)STD_OUTPUT_HANDLE
) ||
185 (handles
[i
] == (HANDLE
)STD_ERROR_HANDLE
))
186 hloc
[i
] = GetStdHandle( (DWORD
)handles
[i
] );
188 hloc
[i
] = handles
[i
];
190 /* yes, even screen buffer console handles are waitable, and are
191 * handled as a handle to the console itself !!
193 if (is_console_handle(hloc
[i
]))
195 if (!VerifyConsoleIoHandle(hloc
[i
]))
199 hloc
[i
] = GetConsoleInputWaitHandle();
203 if (timeout
== INFINITE
)
205 status
= NtWaitForMultipleObjects( count
, hloc
, wait_all
, alertable
, NULL
);
211 time
.QuadPart
= timeout
* (ULONGLONG
)10000;
212 time
.QuadPart
= -time
.QuadPart
;
213 status
= NtWaitForMultipleObjects( count
, hloc
, wait_all
, alertable
, &time
);
216 if (HIWORD(status
)) /* is it an error code? */
218 SetLastError( RtlNtStatusToDosError(status
) );
219 status
= WAIT_FAILED
;
225 /***********************************************************************
226 * WaitForSingleObject (KERNEL.460)
228 DWORD WINAPI
WaitForSingleObject16( HANDLE handle
, DWORD timeout
)
230 DWORD retval
, mutex_count
;
232 ReleaseThunkLock( &mutex_count
);
233 retval
= WaitForSingleObject( handle
, timeout
);
234 RestoreThunkLock( mutex_count
);
238 /***********************************************************************
239 * WaitForMultipleObjects (KERNEL.461)
241 DWORD WINAPI
WaitForMultipleObjects16( DWORD count
, const HANDLE
*handles
,
242 BOOL wait_all
, DWORD timeout
)
244 DWORD retval
, mutex_count
;
246 ReleaseThunkLock( &mutex_count
);
247 retval
= WaitForMultipleObjectsEx( count
, handles
, wait_all
, timeout
, FALSE
);
248 RestoreThunkLock( mutex_count
);
252 /***********************************************************************
253 * WaitForMultipleObjectsEx (KERNEL.495)
255 DWORD WINAPI
WaitForMultipleObjectsEx16( DWORD count
, const HANDLE
*handles
,
256 BOOL wait_all
, DWORD timeout
, BOOL alertable
)
258 DWORD retval
, mutex_count
;
260 ReleaseThunkLock( &mutex_count
);
261 retval
= WaitForMultipleObjectsEx( count
, handles
, wait_all
, timeout
, alertable
);
262 RestoreThunkLock( mutex_count
);
266 /***********************************************************************
267 * RegisterWaitForSingleObject (KERNEL32.@)
269 BOOL WINAPI
RegisterWaitForSingleObject(PHANDLE phNewWaitObject
, HANDLE hObject
,
270 WAITORTIMERCALLBACK Callback
, PVOID Context
,
271 ULONG dwMilliseconds
, ULONG dwFlags
)
273 FIXME("%p %p %p %p %ld %ld\n",
274 phNewWaitObject
,hObject
,Callback
,Context
,dwMilliseconds
,dwFlags
);
278 /***********************************************************************
279 * RegisterWaitForSingleObjectEx (KERNEL32.@)
281 HANDLE WINAPI
RegisterWaitForSingleObjectEx( HANDLE hObject
,
282 WAITORTIMERCALLBACK Callback
, PVOID Context
,
283 ULONG dwMilliseconds
, ULONG dwFlags
)
285 FIXME("%p %p %p %ld %ld\n",
286 hObject
,Callback
,Context
,dwMilliseconds
,dwFlags
);
290 /***********************************************************************
291 * UnregisterWait (KERNEL32.@)
293 BOOL WINAPI
UnregisterWait( HANDLE WaitHandle
)
295 FIXME("%p\n",WaitHandle
);
299 /***********************************************************************
300 * UnregisterWaitEx (KERNEL32.@)
302 BOOL WINAPI
UnregisterWaitEx( HANDLE WaitHandle
, HANDLE CompletionEvent
)
304 FIXME("%p %p\n",WaitHandle
, CompletionEvent
);
308 /***********************************************************************
309 * SignalObjectAndWait (KERNEL32.@)
311 * Allows to atomically signal any of the synchro objects (semaphore,
312 * mutex, event) and wait on another.
314 DWORD WINAPI
SignalObjectAndWait( HANDLE hObjectToSignal
, HANDLE hObjectToWaitOn
,
315 DWORD dwMilliseconds
, BOOL bAlertable
)
318 LARGE_INTEGER timeout
, *ptimeout
= NULL
;
320 TRACE("%p %p %ld %d\n", hObjectToSignal
,
321 hObjectToWaitOn
, dwMilliseconds
, bAlertable
);
323 if (dwMilliseconds
!= INFINITE
)
325 timeout
.QuadPart
= dwMilliseconds
* (ULONGLONG
)10000;
326 timeout
.QuadPart
= -timeout
.QuadPart
;
330 status
= NtSignalAndWaitForSingleObject( hObjectToSignal
, hObjectToWaitOn
,
331 bAlertable
, ptimeout
);
334 SetLastError( RtlNtStatusToDosError(status
) );
335 status
= WAIT_FAILED
;
340 /***********************************************************************
341 * InitializeCriticalSection (KERNEL32.@)
343 * Initialise a critical section before use.
346 * crit [O] Critical section to initialise.
349 * Nothing. If the function fails an exception is raised.
351 void WINAPI
InitializeCriticalSection( CRITICAL_SECTION
*crit
)
353 NTSTATUS ret
= RtlInitializeCriticalSection( crit
);
354 if (ret
) RtlRaiseStatus( ret
);
357 /***********************************************************************
358 * InitializeCriticalSectionAndSpinCount (KERNEL32.@)
360 * Initialise a critical section with a spin count.
363 * crit [O] Critical section to initialise.
364 * spincount [I] Number of times to spin upon contention.
368 * Failure: Nothing. If the function fails an exception is raised.
371 * spincount is ignored on uni-processor systems.
373 BOOL WINAPI
InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION
*crit
, DWORD spincount
)
375 NTSTATUS ret
= RtlInitializeCriticalSectionAndSpinCount( crit
, spincount
);
376 if (ret
) RtlRaiseStatus( ret
);
380 /***********************************************************************
381 * MakeCriticalSectionGlobal (KERNEL32.@)
383 void WINAPI
MakeCriticalSectionGlobal( CRITICAL_SECTION
*crit
)
385 /* let's assume that only one thread at a time will try to do this */
386 HANDLE sem
= crit
->LockSemaphore
;
387 if (!sem
) NtCreateSemaphore( &sem
, SEMAPHORE_ALL_ACCESS
, NULL
, 0, 1 );
388 crit
->LockSemaphore
= ConvertToGlobalHandle( sem
);
389 RtlFreeHeap( GetProcessHeap(), 0, crit
->DebugInfo
);
390 crit
->DebugInfo
= NULL
;
394 /***********************************************************************
395 * ReinitializeCriticalSection (KERNEL32.@)
397 * Initialise an already used critical section.
400 * crit [O] Critical section to initialise.
405 void WINAPI
ReinitializeCriticalSection( CRITICAL_SECTION
*crit
)
407 if ( !crit
->LockSemaphore
)
408 RtlInitializeCriticalSection( crit
);
412 /***********************************************************************
413 * UninitializeCriticalSection (KERNEL32.@)
415 * UnInitialise a critical section after use.
418 * crit [O] Critical section to uninitialise (destroy).
423 void WINAPI
UninitializeCriticalSection( CRITICAL_SECTION
*crit
)
425 RtlDeleteCriticalSection( crit
);
429 /***********************************************************************
430 * CreateEventA (KERNEL32.@)
432 HANDLE WINAPI
CreateEventA( SECURITY_ATTRIBUTES
*sa
, BOOL manual_reset
,
433 BOOL initial_state
, LPCSTR name
)
435 WCHAR buffer
[MAX_PATH
];
437 if (!name
) return CreateEventW( sa
, manual_reset
, initial_state
, NULL
);
439 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
441 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
444 return CreateEventW( sa
, manual_reset
, initial_state
, buffer
);
448 /***********************************************************************
449 * CreateEventW (KERNEL32.@)
451 HANDLE WINAPI
CreateEventW( SECURITY_ATTRIBUTES
*sa
, BOOL manual_reset
,
452 BOOL initial_state
, LPCWSTR name
)
455 UNICODE_STRING nameW
;
456 OBJECT_ATTRIBUTES attr
;
459 /* one buggy program needs this
460 * ("Van Dale Groot woordenboek der Nederlandse taal")
462 if (sa
&& IsBadReadPtr(sa
,sizeof(SECURITY_ATTRIBUTES
)))
464 ERR("Bad security attributes pointer %p\n",sa
);
465 SetLastError( ERROR_INVALID_PARAMETER
);
469 attr
.Length
= sizeof(attr
);
470 attr
.RootDirectory
= 0;
471 attr
.ObjectName
= NULL
;
472 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
|
473 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
474 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
475 attr
.SecurityQualityOfService
= NULL
;
478 RtlInitUnicodeString( &nameW
, name
);
479 attr
.ObjectName
= &nameW
;
480 attr
.RootDirectory
= get_BaseNamedObjects_handle();
483 status
= NtCreateEvent( &ret
, EVENT_ALL_ACCESS
, &attr
, manual_reset
, initial_state
);
484 if (status
== STATUS_OBJECT_NAME_EXISTS
)
485 SetLastError( ERROR_ALREADY_EXISTS
);
487 SetLastError( RtlNtStatusToDosError(status
) );
492 /***********************************************************************
493 * CreateW32Event (KERNEL.457)
495 HANDLE WINAPI
WIN16_CreateEvent( BOOL manual_reset
, BOOL initial_state
)
497 return CreateEventW( NULL
, manual_reset
, initial_state
, NULL
);
501 /***********************************************************************
502 * OpenEventA (KERNEL32.@)
504 HANDLE WINAPI
OpenEventA( DWORD access
, BOOL inherit
, LPCSTR name
)
506 WCHAR buffer
[MAX_PATH
];
508 if (!name
) return OpenEventW( access
, inherit
, NULL
);
510 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
512 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
515 return OpenEventW( access
, inherit
, buffer
);
519 /***********************************************************************
520 * OpenEventW (KERNEL32.@)
522 HANDLE WINAPI
OpenEventW( DWORD access
, BOOL inherit
, LPCWSTR name
)
525 UNICODE_STRING nameW
;
526 OBJECT_ATTRIBUTES attr
;
529 if (!is_version_nt()) access
= EVENT_ALL_ACCESS
;
531 attr
.Length
= sizeof(attr
);
532 attr
.RootDirectory
= 0;
533 attr
.ObjectName
= NULL
;
534 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| (inherit
? OBJ_INHERIT
: 0);
535 attr
.SecurityDescriptor
= NULL
;
536 attr
.SecurityQualityOfService
= NULL
;
539 RtlInitUnicodeString( &nameW
, name
);
540 attr
.ObjectName
= &nameW
;
541 attr
.RootDirectory
= get_BaseNamedObjects_handle();
544 status
= NtOpenEvent( &ret
, access
, &attr
);
545 if (status
!= STATUS_SUCCESS
)
547 SetLastError( RtlNtStatusToDosError(status
) );
553 /***********************************************************************
554 * PulseEvent (KERNEL32.@)
556 BOOL WINAPI
PulseEvent( HANDLE handle
)
560 if ((status
= NtPulseEvent( handle
, NULL
)))
561 SetLastError( RtlNtStatusToDosError(status
) );
566 /***********************************************************************
567 * SetW32Event (KERNEL.458)
568 * SetEvent (KERNEL32.@)
570 BOOL WINAPI
SetEvent( HANDLE handle
)
574 if ((status
= NtSetEvent( handle
, NULL
)))
575 SetLastError( RtlNtStatusToDosError(status
) );
580 /***********************************************************************
581 * ResetW32Event (KERNEL.459)
582 * ResetEvent (KERNEL32.@)
584 BOOL WINAPI
ResetEvent( HANDLE handle
)
588 if ((status
= NtResetEvent( handle
, NULL
)))
589 SetLastError( RtlNtStatusToDosError(status
) );
594 /***********************************************************************
595 * NOTE: The Win95 VWin32_Event routines given below are really low-level
596 * routines implemented directly by VWin32. The user-mode libraries
597 * implement Win32 synchronisation routines on top of these low-level
598 * primitives. We do it the other way around here :-)
601 /***********************************************************************
602 * VWin32_EventCreate (KERNEL.442)
604 HANDLE WINAPI
VWin32_EventCreate(VOID
)
606 HANDLE hEvent
= CreateEventW( NULL
, FALSE
, 0, NULL
);
607 return ConvertToGlobalHandle( hEvent
);
610 /***********************************************************************
611 * VWin32_EventDestroy (KERNEL.443)
613 VOID WINAPI
VWin32_EventDestroy(HANDLE event
)
615 CloseHandle( event
);
618 /***********************************************************************
619 * VWin32_EventWait (KERNEL.450)
621 VOID WINAPI
VWin32_EventWait(HANDLE event
)
625 ReleaseThunkLock( &mutex_count
);
626 WaitForSingleObject( event
, INFINITE
);
627 RestoreThunkLock( mutex_count
);
630 /***********************************************************************
631 * VWin32_EventSet (KERNEL.451)
632 * KERNEL_479 (KERNEL.479)
634 VOID WINAPI
VWin32_EventSet(HANDLE event
)
641 /***********************************************************************
642 * CreateMutexA (KERNEL32.@)
644 HANDLE WINAPI
CreateMutexA( SECURITY_ATTRIBUTES
*sa
, BOOL owner
, LPCSTR name
)
646 WCHAR buffer
[MAX_PATH
];
648 if (!name
) return CreateMutexW( sa
, owner
, NULL
);
650 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
652 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
655 return CreateMutexW( sa
, owner
, buffer
);
659 /***********************************************************************
660 * CreateMutexW (KERNEL32.@)
662 HANDLE WINAPI
CreateMutexW( SECURITY_ATTRIBUTES
*sa
, BOOL owner
, LPCWSTR name
)
665 UNICODE_STRING nameW
;
666 OBJECT_ATTRIBUTES attr
;
669 attr
.Length
= sizeof(attr
);
670 attr
.RootDirectory
= 0;
671 attr
.ObjectName
= NULL
;
672 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
|
673 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
674 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
675 attr
.SecurityQualityOfService
= NULL
;
678 RtlInitUnicodeString( &nameW
, name
);
679 attr
.ObjectName
= &nameW
;
680 attr
.RootDirectory
= get_BaseNamedObjects_handle();
683 status
= NtCreateMutant( &ret
, MUTEX_ALL_ACCESS
, &attr
, owner
);
684 if (status
== STATUS_OBJECT_NAME_EXISTS
)
685 SetLastError( ERROR_ALREADY_EXISTS
);
687 SetLastError( RtlNtStatusToDosError(status
) );
692 /***********************************************************************
693 * OpenMutexA (KERNEL32.@)
695 HANDLE WINAPI
OpenMutexA( DWORD access
, BOOL inherit
, LPCSTR name
)
697 WCHAR buffer
[MAX_PATH
];
699 if (!name
) return OpenMutexW( access
, inherit
, NULL
);
701 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
703 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
706 return OpenMutexW( access
, inherit
, buffer
);
710 /***********************************************************************
711 * OpenMutexW (KERNEL32.@)
713 HANDLE WINAPI
OpenMutexW( DWORD access
, BOOL inherit
, LPCWSTR name
)
716 UNICODE_STRING nameW
;
717 OBJECT_ATTRIBUTES attr
;
720 if (!is_version_nt()) access
= MUTEX_ALL_ACCESS
;
722 attr
.Length
= sizeof(attr
);
723 attr
.RootDirectory
= 0;
724 attr
.ObjectName
= NULL
;
725 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| (inherit
? OBJ_INHERIT
: 0);
726 attr
.SecurityDescriptor
= NULL
;
727 attr
.SecurityQualityOfService
= NULL
;
730 RtlInitUnicodeString( &nameW
, name
);
731 attr
.ObjectName
= &nameW
;
732 attr
.RootDirectory
= get_BaseNamedObjects_handle();
735 status
= NtOpenMutant( &ret
, access
, &attr
);
736 if (status
!= STATUS_SUCCESS
)
738 SetLastError( RtlNtStatusToDosError(status
) );
745 /***********************************************************************
746 * ReleaseMutex (KERNEL32.@)
748 BOOL WINAPI
ReleaseMutex( HANDLE handle
)
752 status
= NtReleaseMutant(handle
, NULL
);
753 if (status
!= STATUS_SUCCESS
)
755 SetLastError( RtlNtStatusToDosError(status
) );
767 /***********************************************************************
768 * CreateSemaphoreA (KERNEL32.@)
770 HANDLE WINAPI
CreateSemaphoreA( SECURITY_ATTRIBUTES
*sa
, LONG initial
, LONG max
, LPCSTR name
)
772 WCHAR buffer
[MAX_PATH
];
774 if (!name
) return CreateSemaphoreW( sa
, initial
, max
, NULL
);
776 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
778 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
781 return CreateSemaphoreW( sa
, initial
, max
, buffer
);
785 /***********************************************************************
786 * CreateSemaphoreW (KERNEL32.@)
788 HANDLE WINAPI
CreateSemaphoreW( SECURITY_ATTRIBUTES
*sa
, LONG initial
,
789 LONG max
, LPCWSTR name
)
792 UNICODE_STRING nameW
;
793 OBJECT_ATTRIBUTES attr
;
796 attr
.Length
= sizeof(attr
);
797 attr
.RootDirectory
= 0;
798 attr
.ObjectName
= NULL
;
799 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
|
800 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
801 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
802 attr
.SecurityQualityOfService
= NULL
;
805 RtlInitUnicodeString( &nameW
, name
);
806 attr
.ObjectName
= &nameW
;
807 attr
.RootDirectory
= get_BaseNamedObjects_handle();
810 status
= NtCreateSemaphore( &ret
, SEMAPHORE_ALL_ACCESS
, &attr
, initial
, max
);
811 if (status
== STATUS_OBJECT_NAME_EXISTS
)
812 SetLastError( ERROR_ALREADY_EXISTS
);
814 SetLastError( RtlNtStatusToDosError(status
) );
819 /***********************************************************************
820 * OpenSemaphoreA (KERNEL32.@)
822 HANDLE WINAPI
OpenSemaphoreA( DWORD access
, BOOL inherit
, LPCSTR name
)
824 WCHAR buffer
[MAX_PATH
];
826 if (!name
) return OpenSemaphoreW( access
, inherit
, NULL
);
828 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
830 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
833 return OpenSemaphoreW( access
, inherit
, buffer
);
837 /***********************************************************************
838 * OpenSemaphoreW (KERNEL32.@)
840 HANDLE WINAPI
OpenSemaphoreW( DWORD access
, BOOL inherit
, LPCWSTR name
)
843 UNICODE_STRING nameW
;
844 OBJECT_ATTRIBUTES attr
;
847 if (!is_version_nt()) access
= SEMAPHORE_ALL_ACCESS
;
849 attr
.Length
= sizeof(attr
);
850 attr
.RootDirectory
= 0;
851 attr
.ObjectName
= NULL
;
852 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| (inherit
? OBJ_INHERIT
: 0);
853 attr
.SecurityDescriptor
= NULL
;
854 attr
.SecurityQualityOfService
= NULL
;
857 RtlInitUnicodeString( &nameW
, name
);
858 attr
.ObjectName
= &nameW
;
859 attr
.RootDirectory
= get_BaseNamedObjects_handle();
862 status
= NtOpenSemaphore( &ret
, access
, &attr
);
863 if (status
!= STATUS_SUCCESS
)
865 SetLastError( RtlNtStatusToDosError(status
) );
872 /***********************************************************************
873 * ReleaseSemaphore (KERNEL32.@)
875 BOOL WINAPI
ReleaseSemaphore( HANDLE handle
, LONG count
, LONG
*previous
)
877 NTSTATUS status
= NtReleaseSemaphore( handle
, count
, (PULONG
)previous
);
878 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
888 /***********************************************************************
889 * CreateWaitableTimerA (KERNEL32.@)
891 HANDLE WINAPI
CreateWaitableTimerA( SECURITY_ATTRIBUTES
*sa
, BOOL manual
, LPCSTR name
)
893 WCHAR buffer
[MAX_PATH
];
895 if (!name
) return CreateWaitableTimerW( sa
, manual
, NULL
);
897 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
899 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
902 return CreateWaitableTimerW( sa
, manual
, buffer
);
906 /***********************************************************************
907 * CreateWaitableTimerW (KERNEL32.@)
909 HANDLE WINAPI
CreateWaitableTimerW( SECURITY_ATTRIBUTES
*sa
, BOOL manual
, LPCWSTR name
)
913 UNICODE_STRING nameW
;
914 OBJECT_ATTRIBUTES attr
;
916 attr
.Length
= sizeof(attr
);
917 attr
.RootDirectory
= 0;
918 attr
.ObjectName
= NULL
;
919 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
|
920 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
921 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
922 attr
.SecurityQualityOfService
= NULL
;
925 RtlInitUnicodeString( &nameW
, name
);
926 attr
.ObjectName
= &nameW
;
927 attr
.RootDirectory
= get_BaseNamedObjects_handle();
930 status
= NtCreateTimer(&handle
, TIMER_ALL_ACCESS
, &attr
,
931 manual
? NotificationTimer
: SynchronizationTimer
);
932 if (status
== STATUS_OBJECT_NAME_EXISTS
)
933 SetLastError( ERROR_ALREADY_EXISTS
);
935 SetLastError( RtlNtStatusToDosError(status
) );
940 /***********************************************************************
941 * OpenWaitableTimerA (KERNEL32.@)
943 HANDLE WINAPI
OpenWaitableTimerA( DWORD access
, BOOL inherit
, LPCSTR name
)
945 WCHAR buffer
[MAX_PATH
];
947 if (!name
) return OpenWaitableTimerW( access
, inherit
, NULL
);
949 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
951 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
954 return OpenWaitableTimerW( access
, inherit
, buffer
);
958 /***********************************************************************
959 * OpenWaitableTimerW (KERNEL32.@)
961 HANDLE WINAPI
OpenWaitableTimerW( DWORD access
, BOOL inherit
, LPCWSTR name
)
964 UNICODE_STRING nameW
;
965 OBJECT_ATTRIBUTES attr
;
968 if (!is_version_nt()) access
= TIMER_ALL_ACCESS
;
970 attr
.Length
= sizeof(attr
);
971 attr
.RootDirectory
= 0;
972 attr
.ObjectName
= NULL
;
973 attr
.Attributes
= OBJ_CASE_INSENSITIVE
| (inherit
? OBJ_INHERIT
: 0);
974 attr
.SecurityDescriptor
= NULL
;
975 attr
.SecurityQualityOfService
= NULL
;
978 RtlInitUnicodeString( &nameW
, name
);
979 attr
.ObjectName
= &nameW
;
980 attr
.RootDirectory
= get_BaseNamedObjects_handle();
983 status
= NtOpenTimer(&handle
, access
, &attr
);
984 if (status
!= STATUS_SUCCESS
)
986 SetLastError( RtlNtStatusToDosError(status
) );
993 /***********************************************************************
994 * SetWaitableTimer (KERNEL32.@)
996 BOOL WINAPI
SetWaitableTimer( HANDLE handle
, const LARGE_INTEGER
*when
, LONG period
,
997 PTIMERAPCROUTINE callback
, LPVOID arg
, BOOL resume
)
999 NTSTATUS status
= NtSetTimer(handle
, when
, (PTIMER_APC_ROUTINE
)callback
,
1000 arg
, resume
, period
, NULL
);
1002 if (status
!= STATUS_SUCCESS
)
1004 SetLastError( RtlNtStatusToDosError(status
) );
1005 if (status
!= STATUS_TIMER_RESUME_IGNORED
) return FALSE
;
1011 /***********************************************************************
1012 * CancelWaitableTimer (KERNEL32.@)
1014 BOOL WINAPI
CancelWaitableTimer( HANDLE handle
)
1018 status
= NtCancelTimer(handle
, NULL
);
1019 if (status
!= STATUS_SUCCESS
)
1021 SetLastError( RtlNtStatusToDosError(status
) );
1028 /***********************************************************************
1029 * CreateTimerQueue (KERNEL32.@)
1031 HANDLE WINAPI
CreateTimerQueue(void)
1034 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1039 /***********************************************************************
1040 * DeleteTimerQueueEx (KERNEL32.@)
1042 BOOL WINAPI
DeleteTimerQueueEx(HANDLE TimerQueue
, HANDLE CompletionEvent
)
1044 FIXME("(%p, %p): stub\n", TimerQueue
, CompletionEvent
);
1045 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1049 /***********************************************************************
1050 * CreateTimerQueueTimer (KERNEL32.@)
1052 * Creates a timer-queue timer. This timer expires at the specified due
1053 * time (in ms), then after every specified period (in ms). When the timer
1054 * expires, the callback function is called.
1057 * nonzero on success or zero on faillure
1062 BOOL WINAPI
CreateTimerQueueTimer( PHANDLE phNewTimer
, HANDLE TimerQueue
,
1063 WAITORTIMERCALLBACK Callback
, PVOID Parameter
,
1064 DWORD DueTime
, DWORD Period
, ULONG Flags
)
1067 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1071 /***********************************************************************
1072 * DeleteTimerQueueTimer (KERNEL32.@)
1074 * Cancels a timer-queue timer.
1077 * nonzero on success or zero on faillure
1082 BOOL WINAPI
DeleteTimerQueueTimer( HANDLE TimerQueue
, HANDLE Timer
,
1083 HANDLE CompletionEvent
)
1086 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1096 /***********************************************************************
1097 * CreateNamedPipeA (KERNEL32.@)
1099 HANDLE WINAPI
CreateNamedPipeA( LPCSTR name
, DWORD dwOpenMode
,
1100 DWORD dwPipeMode
, DWORD nMaxInstances
,
1101 DWORD nOutBufferSize
, DWORD nInBufferSize
,
1102 DWORD nDefaultTimeOut
, LPSECURITY_ATTRIBUTES attr
)
1104 WCHAR buffer
[MAX_PATH
];
1106 if (!name
) return CreateNamedPipeW( NULL
, dwOpenMode
, dwPipeMode
, nMaxInstances
,
1107 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, attr
);
1109 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1111 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1112 return INVALID_HANDLE_VALUE
;
1114 return CreateNamedPipeW( buffer
, dwOpenMode
, dwPipeMode
, nMaxInstances
,
1115 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, attr
);
1119 /***********************************************************************
1120 * CreateNamedPipeW (KERNEL32.@)
1122 HANDLE WINAPI
CreateNamedPipeW( LPCWSTR name
, DWORD dwOpenMode
,
1123 DWORD dwPipeMode
, DWORD nMaxInstances
,
1124 DWORD nOutBufferSize
, DWORD nInBufferSize
,
1125 DWORD nDefaultTimeOut
, LPSECURITY_ATTRIBUTES sa
)
1128 UNICODE_STRING nt_name
;
1129 OBJECT_ATTRIBUTES attr
;
1131 BOOLEAN pipe_type
, read_mode
, non_block
;
1133 IO_STATUS_BLOCK iosb
;
1134 LARGE_INTEGER timeout
;
1136 TRACE("(%s, %#08lx, %#08lx, %ld, %ld, %ld, %ld, %p)\n",
1137 debugstr_w(name
), dwOpenMode
, dwPipeMode
, nMaxInstances
,
1138 nOutBufferSize
, nInBufferSize
, nDefaultTimeOut
, sa
);
1140 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1142 SetLastError( ERROR_PATH_NOT_FOUND
);
1143 return INVALID_HANDLE_VALUE
;
1145 if (nt_name
.Length
>= MAX_PATH
* sizeof(WCHAR
) )
1147 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1148 RtlFreeUnicodeString( &nt_name
);
1149 return INVALID_HANDLE_VALUE
;
1152 attr
.Length
= sizeof(attr
);
1153 attr
.RootDirectory
= 0;
1154 attr
.ObjectName
= &nt_name
;
1155 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
1156 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
1157 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1158 attr
.SecurityQualityOfService
= NULL
;
1161 if (dwOpenMode
& FILE_FLAG_WRITE_THROUGH
) options
|= FILE_WRITE_THROUGH
;
1162 if (!(dwOpenMode
& FILE_FLAG_OVERLAPPED
)) options
|= FILE_SYNCHRONOUS_IO_ALERT
;
1163 if ((dwOpenMode
& PIPE_ACCESS_DUPLEX
) == PIPE_ACCESS_DUPLEX
)
1164 options
|= FILE_PIPE_FULL_DUPLEX
;
1165 else if (dwOpenMode
& PIPE_ACCESS_INBOUND
) options
|= FILE_PIPE_INBOUND
;
1166 else if (dwOpenMode
& PIPE_ACCESS_OUTBOUND
) options
|= FILE_PIPE_OUTBOUND
;
1167 pipe_type
= (dwPipeMode
& PIPE_TYPE_MESSAGE
) ? TRUE
: FALSE
;
1168 read_mode
= (dwPipeMode
& PIPE_READMODE_MESSAGE
) ? TRUE
: FALSE
;
1169 non_block
= (dwPipeMode
& PIPE_NOWAIT
) ? TRUE
: FALSE
;
1170 if (nMaxInstances
>= PIPE_UNLIMITED_INSTANCES
) nMaxInstances
= ~0UL;
1172 timeout
.QuadPart
= (ULONGLONG
)nDefaultTimeOut
* -10000;
1176 status
= NtCreateNamedPipeFile(&handle
, GENERIC_READ
|GENERIC_WRITE
, &attr
, &iosb
,
1177 0, FILE_OVERWRITE_IF
, options
, pipe_type
,
1178 read_mode
, non_block
, nMaxInstances
,
1179 nInBufferSize
, nOutBufferSize
, &timeout
);
1181 RtlFreeUnicodeString( &nt_name
);
1184 handle
= INVALID_HANDLE_VALUE
;
1185 SetLastError( RtlNtStatusToDosError(status
) );
1191 /***********************************************************************
1192 * PeekNamedPipe (KERNEL32.@)
1194 BOOL WINAPI
PeekNamedPipe( HANDLE hPipe
, LPVOID lpvBuffer
, DWORD cbBuffer
,
1195 LPDWORD lpcbRead
, LPDWORD lpcbAvail
, LPDWORD lpcbMessage
)
1198 int avail
=0, fd
, ret
, flags
;
1200 TRACE("(%p,%p,%lu,%p,%p,%p)\n", hPipe
, lpvBuffer
, cbBuffer
, lpcbRead
, lpcbAvail
, lpcbMessage
);
1202 ret
= wine_server_handle_to_fd( hPipe
, FILE_READ_DATA
, &fd
, &flags
);
1205 SetLastError( RtlNtStatusToDosError(ret
) );
1208 if (flags
& FD_FLAG_RECV_SHUTDOWN
)
1210 wine_server_release_fd( hPipe
, fd
);
1211 SetLastError ( ERROR_PIPE_NOT_CONNECTED
);
1215 if (ioctl(fd
,FIONREAD
, &avail
) != 0)
1217 TRACE("FIONREAD failed reason: %s\n",strerror(errno
));
1218 wine_server_release_fd( hPipe
, fd
);
1221 if (!avail
) /* check for closed pipe */
1223 struct pollfd pollfd
;
1225 pollfd
.events
= POLLIN
;
1227 switch (poll( &pollfd
, 1, 0 ))
1231 case 1: /* got something */
1232 if (!(pollfd
.revents
& (POLLHUP
| POLLERR
))) break;
1233 TRACE("POLLHUP | POLLERR\n");
1236 wine_server_release_fd( hPipe
, fd
);
1237 SetLastError(ERROR_BROKEN_PIPE
);
1241 TRACE(" 0x%08x bytes available\n", avail
);
1247 if (avail
&& lpvBuffer
&& cbBuffer
)
1249 int readbytes
= (avail
< cbBuffer
) ? avail
: cbBuffer
;
1250 readbytes
= recv(fd
, lpvBuffer
, readbytes
, MSG_PEEK
);
1253 WARN("failed to peek socket (%d)\n", errno
);
1257 *lpcbRead
= readbytes
;
1259 wine_server_release_fd( hPipe
, fd
);
1261 #endif /* defined(FIONREAD) */
1263 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1264 FIXME("function not implemented\n");
1268 /***********************************************************************
1269 * WaitNamedPipeA (KERNEL32.@)
1271 BOOL WINAPI
WaitNamedPipeA (LPCSTR name
, DWORD nTimeOut
)
1273 WCHAR buffer
[MAX_PATH
];
1275 if (!name
) return WaitNamedPipeW( NULL
, nTimeOut
);
1277 if (!MultiByteToWideChar( CP_ACP
, 0, name
, -1, buffer
, MAX_PATH
))
1279 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1282 return WaitNamedPipeW( buffer
, nTimeOut
);
1286 /***********************************************************************
1287 * WaitNamedPipeW (KERNEL32.@)
1289 * Waits for a named pipe instance to become available
1292 * name [I] Pointer to a named pipe name to wait for
1293 * nTimeOut [I] How long to wait in ms
1296 * TRUE: Success, named pipe can be opened with CreteFile
1297 * FALSE: Failure, GetLastError can be called for further details
1299 BOOL WINAPI
WaitNamedPipeW (LPCWSTR name
, DWORD nTimeOut
)
1301 static const WCHAR leadin
[] = {'\\','?','?','\\','P','I','P','E','\\'};
1303 UNICODE_STRING nt_name
, pipe_dev_name
;
1304 FILE_PIPE_WAIT_FOR_BUFFER
*pipe_wait
;
1305 IO_STATUS_BLOCK iosb
;
1306 OBJECT_ATTRIBUTES attr
;
1310 TRACE("%s 0x%08lx\n",debugstr_w(name
),nTimeOut
);
1312 if (!RtlDosPathNameToNtPathName_U( name
, &nt_name
, NULL
, NULL
))
1315 if (nt_name
.Length
>= MAX_PATH
* sizeof(WCHAR
) ||
1316 nt_name
.Length
< sizeof(leadin
) ||
1317 strncmpiW( nt_name
.Buffer
, leadin
, sizeof(leadin
)/sizeof(WCHAR
) != 0))
1319 RtlFreeUnicodeString( &nt_name
);
1320 SetLastError( ERROR_PATH_NOT_FOUND
);
1324 sz_pipe_wait
= sizeof(*pipe_wait
) + nt_name
.Length
- sizeof(leadin
) - sizeof(WCHAR
);
1325 if (!(pipe_wait
= HeapAlloc( GetProcessHeap(), 0, sz_pipe_wait
)))
1327 RtlFreeUnicodeString( &nt_name
);
1328 SetLastError( ERROR_OUTOFMEMORY
);
1332 pipe_dev_name
.Buffer
= nt_name
.Buffer
;
1333 pipe_dev_name
.Length
= sizeof(leadin
);
1334 pipe_dev_name
.MaximumLength
= sizeof(leadin
);
1335 InitializeObjectAttributes(&attr
,&pipe_dev_name
, OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
1336 status
= NtOpenFile( &pipe_dev
, FILE_READ_ATTRIBUTES
, &attr
,
1337 &iosb
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1338 FILE_SYNCHRONOUS_IO_NONALERT
);
1339 if (status
!= ERROR_SUCCESS
)
1341 SetLastError( ERROR_PATH_NOT_FOUND
);
1345 pipe_wait
->TimeoutSpecified
= !(nTimeOut
== NMPWAIT_USE_DEFAULT_WAIT
);
1346 pipe_wait
->Timeout
.QuadPart
= nTimeOut
* -10000L;
1347 pipe_wait
->NameLength
= nt_name
.Length
- sizeof(leadin
);
1348 memcpy(pipe_wait
->Name
, nt_name
.Buffer
+ sizeof(leadin
)/sizeof(WCHAR
),
1349 pipe_wait
->NameLength
);
1350 RtlFreeUnicodeString( &nt_name
);
1352 status
= NtFsControlFile( pipe_dev
, NULL
, NULL
, NULL
, &iosb
, FSCTL_PIPE_WAIT
,
1353 pipe_wait
, sz_pipe_wait
, NULL
, 0 );
1355 HeapFree( GetProcessHeap(), 0, pipe_wait
);
1356 NtClose( pipe_dev
);
1358 if(status
!= STATUS_SUCCESS
)
1360 SetLastError(RtlNtStatusToDosError(status
));
1368 /***********************************************************************
1369 * ConnectNamedPipe (KERNEL32.@)
1371 * Connects to a named pipe
1374 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1375 * overlapped: Optional OVERLAPPED struct
1379 * FALSE: Failure, GetLastError can be called for further details
1381 BOOL WINAPI
ConnectNamedPipe(HANDLE hPipe
, LPOVERLAPPED overlapped
)
1384 IO_STATUS_BLOCK status_block
;
1386 TRACE("(%p,%p)\n", hPipe
, overlapped
);
1389 overlapped
->Internal
= STATUS_PENDING
;
1391 status
= NtFsControlFile(hPipe
, overlapped
? overlapped
->hEvent
: NULL
, NULL
, NULL
,
1392 overlapped
? (IO_STATUS_BLOCK
*)overlapped
: &status_block
,
1393 FSCTL_PIPE_LISTEN
, NULL
, 0, NULL
, 0);
1395 if (status
== STATUS_SUCCESS
) return TRUE
;
1396 SetLastError( RtlNtStatusToDosError(status
) );
1400 /***********************************************************************
1401 * DisconnectNamedPipe (KERNEL32.@)
1403 * Disconnects from a named pipe
1406 * hPipe: A handle to a named pipe returned by CreateNamedPipe
1410 * FALSE: Failure, GetLastError can be called for further details
1412 BOOL WINAPI
DisconnectNamedPipe(HANDLE hPipe
)
1415 IO_STATUS_BLOCK io_block
;
1417 TRACE("(%p)\n",hPipe
);
1419 status
= NtFsControlFile(hPipe
, 0, NULL
, NULL
, &io_block
, FSCTL_PIPE_DISCONNECT
,
1421 if (status
== STATUS_SUCCESS
) return TRUE
;
1422 SetLastError( RtlNtStatusToDosError(status
) );
1426 /***********************************************************************
1427 * TransactNamedPipe (KERNEL32.@)
1430 * should be done as a single operation in the wineserver or kernel
1432 BOOL WINAPI
TransactNamedPipe(
1433 HANDLE handle
, LPVOID lpInput
, DWORD dwInputSize
, LPVOID lpOutput
,
1434 DWORD dwOutputSize
, LPDWORD lpBytesRead
, LPOVERLAPPED lpOverlapped
)
1439 TRACE("%p %p %ld %p %ld %p %p\n",
1440 handle
, lpInput
, dwInputSize
, lpOutput
,
1441 dwOutputSize
, lpBytesRead
, lpOverlapped
);
1445 FIXME("Doesn't support overlapped operation as yet\n");
1449 r
= WriteFile(handle
, lpOutput
, dwOutputSize
, &count
, NULL
);
1451 r
= ReadFile(handle
, lpInput
, dwInputSize
, lpBytesRead
, NULL
);
1456 /***********************************************************************
1457 * GetNamedPipeInfo (KERNEL32.@)
1459 BOOL WINAPI
GetNamedPipeInfo(
1460 HANDLE hNamedPipe
, LPDWORD lpFlags
, LPDWORD lpOutputBufferSize
,
1461 LPDWORD lpInputBufferSize
, LPDWORD lpMaxInstances
)
1465 TRACE("%p %p %p %p %p\n", hNamedPipe
, lpFlags
,
1466 lpOutputBufferSize
, lpInputBufferSize
, lpMaxInstances
);
1468 SERVER_START_REQ( get_named_pipe_info
)
1470 req
->handle
= hNamedPipe
;
1471 ret
= !wine_server_call_err( req
);
1475 if (reply
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
)
1476 *lpFlags
|= PIPE_TYPE_MESSAGE
;
1477 if (reply
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
)
1478 *lpFlags
|= PIPE_READMODE_MESSAGE
;
1479 if (reply
->flags
& NAMED_PIPE_NONBLOCKING_MODE
)
1480 *lpFlags
|= PIPE_NOWAIT
;
1482 if (lpOutputBufferSize
) *lpOutputBufferSize
= reply
->outsize
;
1483 if (lpInputBufferSize
) *lpInputBufferSize
= reply
->outsize
;
1484 if (lpMaxInstances
) *lpMaxInstances
= reply
->maxinstances
;
1491 /***********************************************************************
1492 * GetNamedPipeHandleStateA (KERNEL32.@)
1494 BOOL WINAPI
GetNamedPipeHandleStateA(
1495 HANDLE hNamedPipe
, LPDWORD lpState
, LPDWORD lpCurInstances
,
1496 LPDWORD lpMaxCollectionCount
, LPDWORD lpCollectDataTimeout
,
1497 LPSTR lpUsername
, DWORD nUsernameMaxSize
)
1499 FIXME("%p %p %p %p %p %p %ld\n",
1500 hNamedPipe
, lpState
, lpCurInstances
,
1501 lpMaxCollectionCount
, lpCollectDataTimeout
,
1502 lpUsername
, nUsernameMaxSize
);
1507 /***********************************************************************
1508 * GetNamedPipeHandleStateW (KERNEL32.@)
1510 BOOL WINAPI
GetNamedPipeHandleStateW(
1511 HANDLE hNamedPipe
, LPDWORD lpState
, LPDWORD lpCurInstances
,
1512 LPDWORD lpMaxCollectionCount
, LPDWORD lpCollectDataTimeout
,
1513 LPWSTR lpUsername
, DWORD nUsernameMaxSize
)
1515 FIXME("%p %p %p %p %p %p %ld\n",
1516 hNamedPipe
, lpState
, lpCurInstances
,
1517 lpMaxCollectionCount
, lpCollectDataTimeout
,
1518 lpUsername
, nUsernameMaxSize
);
1523 /***********************************************************************
1524 * SetNamedPipeHandleState (KERNEL32.@)
1526 BOOL WINAPI
SetNamedPipeHandleState(
1527 HANDLE hNamedPipe
, LPDWORD lpMode
, LPDWORD lpMaxCollectionCount
,
1528 LPDWORD lpCollectDataTimeout
)
1530 /* should be a fixme, but this function is called a lot by the RPC
1531 * runtime, and it slows down InstallShield a fair bit. */
1532 WARN("stub: %p %p/%ld %p %p\n",
1533 hNamedPipe
, lpMode
, lpMode
? *lpMode
: 0, lpMaxCollectionCount
, lpCollectDataTimeout
);
1537 /***********************************************************************
1538 * CallNamedPipeA (KERNEL32.@)
1540 BOOL WINAPI
CallNamedPipeA(
1541 LPCSTR lpNamedPipeName
, LPVOID lpInput
, DWORD dwInputSize
,
1542 LPVOID lpOutput
, DWORD dwOutputSize
,
1543 LPDWORD lpBytesRead
, DWORD nTimeout
)
1549 TRACE("%s %p %ld %p %ld %p %ld\n",
1550 debugstr_a(lpNamedPipeName
), lpInput
, dwInputSize
,
1551 lpOutput
, dwOutputSize
, lpBytesRead
, nTimeout
);
1553 if( lpNamedPipeName
)
1555 len
= MultiByteToWideChar( CP_ACP
, 0, lpNamedPipeName
, -1, NULL
, 0 );
1556 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1557 MultiByteToWideChar( CP_ACP
, 0, lpNamedPipeName
, -1, str
, len
);
1559 ret
= CallNamedPipeW( str
, lpInput
, dwInputSize
, lpOutput
,
1560 dwOutputSize
, lpBytesRead
, nTimeout
);
1561 if( lpNamedPipeName
)
1562 HeapFree( GetProcessHeap(), 0, str
);
1567 /***********************************************************************
1568 * CallNamedPipeW (KERNEL32.@)
1570 BOOL WINAPI
CallNamedPipeW(
1571 LPCWSTR lpNamedPipeName
, LPVOID lpInput
, DWORD lpInputSize
,
1572 LPVOID lpOutput
, DWORD lpOutputSize
,
1573 LPDWORD lpBytesRead
, DWORD nTimeout
)
1575 FIXME("%s %p %ld %p %ld %p %ld\n",
1576 debugstr_w(lpNamedPipeName
), lpInput
, lpInputSize
,
1577 lpOutput
, lpOutputSize
, lpBytesRead
, nTimeout
);
1581 /******************************************************************
1582 * CreatePipe (KERNEL32.@)
1585 BOOL WINAPI
CreatePipe( PHANDLE hReadPipe
, PHANDLE hWritePipe
,
1586 LPSECURITY_ATTRIBUTES sa
, DWORD size
)
1588 static unsigned index
/* = 0 */;
1591 unsigned in_index
= index
;
1592 UNICODE_STRING nt_name
;
1593 OBJECT_ATTRIBUTES attr
;
1595 IO_STATUS_BLOCK iosb
;
1596 LARGE_INTEGER timeout
;
1598 *hReadPipe
= *hWritePipe
= INVALID_HANDLE_VALUE
;
1600 attr
.Length
= sizeof(attr
);
1601 attr
.RootDirectory
= 0;
1602 attr
.ObjectName
= &nt_name
;
1603 attr
.Attributes
= OBJ_CASE_INSENSITIVE
|
1604 ((sa
&& sa
->bInheritHandle
) ? OBJ_INHERIT
: 0);
1605 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1606 attr
.SecurityQualityOfService
= NULL
;
1608 timeout
.QuadPart
= (ULONGLONG
)NMPWAIT_USE_DEFAULT_WAIT
* -10000;
1609 /* generate a unique pipe name (system wide) */
1612 static const WCHAR nameFmt
[] = { '\\','?','?','\\','p','i','p','e',
1613 '\\','W','i','n','3','2','.','P','i','p','e','s','.','%','0','8','l',
1614 'u','.','%','0','8','u','\0' };
1616 snprintfW(name
, sizeof(name
) / sizeof(name
[0]), nameFmt
,
1617 GetCurrentProcessId(), ++index
);
1618 RtlInitUnicodeString(&nt_name
, name
);
1619 status
= NtCreateNamedPipeFile(&hr
, GENERIC_READ
| SYNCHRONIZE
, &attr
, &iosb
,
1620 0, FILE_OVERWRITE_IF
,
1621 FILE_SYNCHRONOUS_IO_ALERT
| FILE_PIPE_INBOUND
,
1622 FALSE
, FALSE
, FALSE
,
1623 1, size
, size
, &timeout
);
1626 SetLastError( RtlNtStatusToDosError(status
) );
1627 hr
= INVALID_HANDLE_VALUE
;
1629 } while (hr
== INVALID_HANDLE_VALUE
&& index
!= in_index
);
1630 /* from completion sakeness, I think system resources might be exhausted before this happens !! */
1631 if (hr
== INVALID_HANDLE_VALUE
) return FALSE
;
1633 status
= NtOpenFile(&hw
, GENERIC_WRITE
| SYNCHRONIZE
, &attr
, &iosb
, 0,
1634 FILE_SYNCHRONOUS_IO_ALERT
| FILE_NON_DIRECTORY_FILE
);
1638 SetLastError( RtlNtStatusToDosError(status
) );
1649 /******************************************************************************
1650 * CreateMailslotA [KERNEL32.@]
1652 * See CreatMailslotW.
1654 HANDLE WINAPI
CreateMailslotA( LPCSTR lpName
, DWORD nMaxMessageSize
,
1655 DWORD lReadTimeout
, LPSECURITY_ATTRIBUTES sa
)
1661 TRACE("%s %ld %ld %p\n", debugstr_a(lpName
),
1662 nMaxMessageSize
, lReadTimeout
, sa
);
1666 len
= MultiByteToWideChar( CP_ACP
, 0, lpName
, -1, NULL
, 0 );
1667 name
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1668 MultiByteToWideChar( CP_ACP
, 0, lpName
, -1, name
, len
);
1671 handle
= CreateMailslotW( name
, nMaxMessageSize
, lReadTimeout
, sa
);
1673 HeapFree( GetProcessHeap(), 0, name
);
1679 /******************************************************************************
1680 * CreateMailslotW [KERNEL32.@]
1682 * Create a mailslot with specified name.
1685 * lpName [I] Pointer to string for mailslot name
1686 * nMaxMessageSize [I] Maximum message size
1687 * lReadTimeout [I] Milliseconds before read time-out
1688 * sa [I] Pointer to security structure
1691 * Success: Handle to mailslot
1692 * Failure: INVALID_HANDLE_VALUE
1694 HANDLE WINAPI
CreateMailslotW( LPCWSTR lpName
, DWORD nMaxMessageSize
,
1695 DWORD lReadTimeout
, LPSECURITY_ATTRIBUTES sa
)
1697 HANDLE handle
= INVALID_HANDLE_VALUE
;
1698 OBJECT_ATTRIBUTES attr
;
1699 UNICODE_STRING nameW
;
1700 LARGE_INTEGER timeout
;
1701 IO_STATUS_BLOCK iosb
;
1704 TRACE("%s %ld %ld %p\n", debugstr_w(lpName
),
1705 nMaxMessageSize
, lReadTimeout
, sa
);
1707 if (!RtlDosPathNameToNtPathName_U( lpName
, &nameW
, NULL
, NULL
))
1709 SetLastError( ERROR_PATH_NOT_FOUND
);
1710 return INVALID_HANDLE_VALUE
;
1713 if (nameW
.Length
>= MAX_PATH
* sizeof(WCHAR
) )
1715 SetLastError( ERROR_FILENAME_EXCED_RANGE
);
1716 RtlFreeUnicodeString( &nameW
);
1717 return INVALID_HANDLE_VALUE
;
1720 attr
.Length
= sizeof(attr
);
1721 attr
.RootDirectory
= 0;
1722 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1723 attr
.ObjectName
= &nameW
;
1724 attr
.SecurityDescriptor
= sa
? sa
->lpSecurityDescriptor
: NULL
;
1725 attr
.SecurityQualityOfService
= NULL
;
1727 if (lReadTimeout
!= MAILSLOT_WAIT_FOREVER
)
1728 timeout
.QuadPart
= (ULONGLONG
) lReadTimeout
* -10000;
1730 timeout
.QuadPart
= ((LONGLONG
)0x7fffffff << 32) | 0xffffffff;
1732 status
= NtCreateMailslotFile( &handle
, GENERIC_READ
| GENERIC_WRITE
, &attr
,
1733 &iosb
, 0, 0, nMaxMessageSize
, &timeout
);
1736 SetLastError( RtlNtStatusToDosError(status
) );
1737 handle
= INVALID_HANDLE_VALUE
;
1740 RtlFreeUnicodeString( &nameW
);
1745 /******************************************************************************
1746 * GetMailslotInfo [KERNEL32.@]
1748 * Retrieve information about a mailslot.
1751 * hMailslot [I] Mailslot handle
1752 * lpMaxMessageSize [O] Address of maximum message size
1753 * lpNextSize [O] Address of size of next message
1754 * lpMessageCount [O] Address of number of messages
1755 * lpReadTimeout [O] Address of read time-out
1761 BOOL WINAPI
GetMailslotInfo( HANDLE hMailslot
, LPDWORD lpMaxMessageSize
,
1762 LPDWORD lpNextSize
, LPDWORD lpMessageCount
,
1763 LPDWORD lpReadTimeout
)
1765 FILE_MAILSLOT_QUERY_INFORMATION info
;
1766 IO_STATUS_BLOCK iosb
;
1769 TRACE("%p %p %p %p %p\n",hMailslot
, lpMaxMessageSize
,
1770 lpNextSize
, lpMessageCount
, lpReadTimeout
);
1772 status
= NtQueryInformationFile( hMailslot
, &iosb
, &info
, sizeof info
,
1773 FileMailslotQueryInformation
);
1775 if( status
!= STATUS_SUCCESS
)
1777 SetLastError( RtlNtStatusToDosError(status
) );
1781 if( lpMaxMessageSize
)
1782 *lpMaxMessageSize
= info
.MaximumMessageSize
;
1784 *lpNextSize
= info
.NextMessageSize
;
1785 if( lpMessageCount
)
1786 *lpMessageCount
= info
.MessagesAvailable
;
1788 *lpReadTimeout
= info
.ReadTimeout
.QuadPart
/ -10000;
1794 /******************************************************************************
1795 * SetMailslotInfo [KERNEL32.@]
1797 * Set the read timeout of a mailslot.
1800 * hMailslot [I] Mailslot handle
1801 * dwReadTimeout [I] Timeout in milliseconds.
1807 BOOL WINAPI
SetMailslotInfo( HANDLE hMailslot
, DWORD dwReadTimeout
)
1809 FILE_MAILSLOT_SET_INFORMATION info
;
1810 IO_STATUS_BLOCK iosb
;
1813 TRACE("%p %ld\n", hMailslot
, dwReadTimeout
);
1815 info
.ReadTimeout
.QuadPart
= dwReadTimeout
* -10000;
1816 status
= NtSetInformationFile( hMailslot
, &iosb
, &info
, sizeof info
,
1817 FileMailslotSetInformation
);
1818 if( status
!= STATUS_SUCCESS
)
1820 SetLastError( RtlNtStatusToDosError(status
) );
1827 /******************************************************************************
1828 * CreateIoCompletionPort (KERNEL32.@)
1830 HANDLE WINAPI
CreateIoCompletionPort(HANDLE hFileHandle
, HANDLE hExistingCompletionPort
,
1831 ULONG_PTR CompletionKey
, DWORD dwNumberOfConcurrentThreads
)
1833 FIXME("(%p, %p, %08lx, %08lx): stub.\n",
1834 hFileHandle
, hExistingCompletionPort
, CompletionKey
, dwNumberOfConcurrentThreads
);
1839 /******************************************************************************
1840 * GetQueuedCompletionStatus (KERNEL32.@)
1842 BOOL WINAPI
GetQueuedCompletionStatus( HANDLE CompletionPort
, LPDWORD lpNumberOfBytesTransferred
,
1843 PULONG_PTR pCompletionKey
, LPOVERLAPPED
*lpOverlapped
,
1844 DWORD dwMilliseconds
)
1846 FIXME("(%p,%p,%p,%p,%ld), stub!\n",
1847 CompletionPort
,lpNumberOfBytesTransferred
,pCompletionKey
,lpOverlapped
,dwMilliseconds
);
1848 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1852 BOOL WINAPI
PostQueuedCompletionStatus( HANDLE CompletionPort
, DWORD dwNumberOfBytes
,
1853 ULONG_PTR dwCompletionKey
, LPOVERLAPPED lpOverlapped
)
1855 FIXME("%p %ld %08lx %p\n", CompletionPort
, dwNumberOfBytes
, dwCompletionKey
, lpOverlapped
);
1856 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1860 /******************************************************************************
1861 * CreateJobObjectW (KERNEL32.@)
1863 HANDLE WINAPI
CreateJobObjectW( LPSECURITY_ATTRIBUTES attr
, LPCWSTR name
)
1865 FIXME("%p %s\n", attr
, debugstr_w(name
) );
1866 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1870 /******************************************************************************
1871 * CreateJobObjectA (KERNEL32.@)
1873 HANDLE WINAPI
CreateJobObjectA( LPSECURITY_ATTRIBUTES attr
, LPCSTR name
)
1879 TRACE("%p %s\n", attr
, debugstr_a(name
) );
1883 len
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, NULL
, 0 );
1884 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1887 SetLastError( ERROR_OUTOFMEMORY
);
1890 len
= MultiByteToWideChar( CP_ACP
, 0, name
, -1, str
, len
);
1893 r
= CreateJobObjectW( attr
, str
);
1895 HeapFree( GetProcessHeap(), 0, str
);
1900 /******************************************************************************
1901 * AssignProcessToJobObject (KERNEL32.@)
1903 BOOL WINAPI
AssignProcessToJobObject( HANDLE hJob
, HANDLE hProcess
)
1905 FIXME("%p %p\n", hJob
, hProcess
);
1911 /***********************************************************************
1912 * InterlockedCompareExchange (KERNEL32.@)
1914 /* LONG WINAPI InterlockedCompareExchange( PLONG dest, LONG xchg, LONG compare ); */
1915 __ASM_GLOBAL_FUNC(InterlockedCompareExchange
,
1916 "movl 12(%esp),%eax\n\t"
1917 "movl 8(%esp),%ecx\n\t"
1918 "movl 4(%esp),%edx\n\t"
1919 "lock; cmpxchgl %ecx,(%edx)\n\t"
1922 /***********************************************************************
1923 * InterlockedExchange (KERNEL32.@)
1925 /* LONG WINAPI InterlockedExchange( PLONG dest, LONG val ); */
1926 __ASM_GLOBAL_FUNC(InterlockedExchange
,
1927 "movl 8(%esp),%eax\n\t"
1928 "movl 4(%esp),%edx\n\t"
1929 "lock; xchgl %eax,(%edx)\n\t"
1932 /***********************************************************************
1933 * InterlockedExchangeAdd (KERNEL32.@)
1935 /* LONG WINAPI InterlockedExchangeAdd( PLONG dest, LONG incr ); */
1936 __ASM_GLOBAL_FUNC(InterlockedExchangeAdd
,
1937 "movl 8(%esp),%eax\n\t"
1938 "movl 4(%esp),%edx\n\t"
1939 "lock; xaddl %eax,(%edx)\n\t"
1942 /***********************************************************************
1943 * InterlockedIncrement (KERNEL32.@)
1945 /* LONG WINAPI InterlockedIncrement( PLONG dest ); */
1946 __ASM_GLOBAL_FUNC(InterlockedIncrement
,
1947 "movl 4(%esp),%edx\n\t"
1949 "lock; xaddl %eax,(%edx)\n\t"
1953 /***********************************************************************
1954 * InterlockedDecrement (KERNEL32.@)
1956 __ASM_GLOBAL_FUNC(InterlockedDecrement
,
1957 "movl 4(%esp),%edx\n\t"
1959 "lock; xaddl %eax,(%edx)\n\t"
1963 #else /* __i386__ */
1965 /***********************************************************************
1966 * InterlockedCompareExchange (KERNEL32.@)
1968 * Atomically swap one value with another.
1971 * dest [I/O] The value to replace
1972 * xchq [I] The value to be swapped
1973 * compare [I] The value to compare to dest
1976 * The resulting value of dest.
1979 * dest is updated only if it is equal to compare, otherwise no swap is done.
1981 LONG WINAPI
InterlockedCompareExchange( LONG
volatile *dest
, LONG xchg
, LONG compare
)
1983 return interlocked_cmpxchg( (int *)dest
, xchg
, compare
);
1986 /***********************************************************************
1987 * InterlockedExchange (KERNEL32.@)
1989 * Atomically swap one value with another.
1992 * dest [I/O] The value to replace
1993 * val [I] The value to be swapped
1996 * The resulting value of dest.
1998 LONG WINAPI
InterlockedExchange( LONG
volatile *dest
, LONG val
)
2000 return interlocked_xchg( (int *)dest
, val
);
2003 /***********************************************************************
2004 * InterlockedExchangeAdd (KERNEL32.@)
2006 * Atomically add one value to another.
2009 * dest [I/O] The value to add to
2010 * incr [I] The value to be added
2013 * The resulting value of dest.
2015 LONG WINAPI
InterlockedExchangeAdd( LONG
volatile *dest
, LONG incr
)
2017 return interlocked_xchg_add( (int *)dest
, incr
);
2020 /***********************************************************************
2021 * InterlockedIncrement (KERNEL32.@)
2023 * Atomically increment a value.
2026 * dest [I/O] The value to increment
2029 * The resulting value of dest.
2031 LONG WINAPI
InterlockedIncrement( LONG
volatile *dest
)
2033 return interlocked_xchg_add( (int *)dest
, 1 ) + 1;
2036 /***********************************************************************
2037 * InterlockedDecrement (KERNEL32.@)
2039 * Atomically decrement a value.
2042 * dest [I/O] The value to decrement
2045 * The resulting value of dest.
2047 LONG WINAPI
InterlockedDecrement( LONG
volatile *dest
)
2049 return interlocked_xchg_add( (int *)dest
, -1 ) - 1;
2052 #endif /* __i386__ */