2 * Process synchronisation
4 * Copyright 1996, 1997, 1998 Marcus Meissner
5 * Copyright 1997, 1999 Alexandre Julliard
6 * Copyright 1999, 2000 Juergen Schmied
7 * Copyright 2003 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
30 #ifdef HAVE_SYS_TIME_H
31 # include <sys/time.h>
36 #ifdef HAVE_SYS_POLL_H
37 # include <sys/poll.h>
51 #define NONAMELESSUNION
52 #define NONAMELESSSTRUCT
55 #define WIN32_NO_STATUS
58 #include "wine/server.h"
59 #include "wine/debug.h"
60 #include "ntdll_misc.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
64 HANDLE keyed_event
= NULL
;
66 static inline int interlocked_dec_if_nonzero( int *dest
)
69 for (val
= *dest
;; val
= tmp
)
71 if (!val
|| (tmp
= interlocked_cmpxchg( dest
, val
- 1, val
)) == val
)
77 /* creates a struct security_descriptor and contained information in one contiguous piece of memory */
78 NTSTATUS
NTDLL_create_struct_sd(PSECURITY_DESCRIPTOR nt_sd
, struct security_descriptor
**server_sd
,
79 data_size_t
*server_sd_len
)
84 BOOLEAN owner_present
, group_present
, dacl_present
, sacl_present
;
93 return STATUS_SUCCESS
;
96 len
= sizeof(struct security_descriptor
);
98 status
= RtlGetOwnerSecurityDescriptor(nt_sd
, &owner
, &owner_present
);
99 if (status
!= STATUS_SUCCESS
) return status
;
100 status
= RtlGetGroupSecurityDescriptor(nt_sd
, &group
, &group_present
);
101 if (status
!= STATUS_SUCCESS
) return status
;
102 status
= RtlGetSaclSecurityDescriptor(nt_sd
, &sacl_present
, &sacl
, &defaulted
);
103 if (status
!= STATUS_SUCCESS
) return status
;
104 status
= RtlGetDaclSecurityDescriptor(nt_sd
, &dacl_present
, &dacl
, &defaulted
);
105 if (status
!= STATUS_SUCCESS
) return status
;
108 len
+= RtlLengthSid(owner
);
110 len
+= RtlLengthSid(group
);
111 if (sacl_present
&& sacl
)
112 len
+= sacl
->AclSize
;
113 if (dacl_present
&& dacl
)
114 len
+= dacl
->AclSize
;
116 /* fix alignment for the Unicode name that follows the structure */
117 len
= (len
+ sizeof(WCHAR
) - 1) & ~(sizeof(WCHAR
) - 1);
118 *server_sd
= RtlAllocateHeap(GetProcessHeap(), 0, len
);
119 if (!*server_sd
) return STATUS_NO_MEMORY
;
121 (*server_sd
)->control
= ((SECURITY_DESCRIPTOR
*)nt_sd
)->Control
& ~SE_SELF_RELATIVE
;
122 (*server_sd
)->owner_len
= owner_present
? RtlLengthSid(owner
) : 0;
123 (*server_sd
)->group_len
= group_present
? RtlLengthSid(group
) : 0;
124 (*server_sd
)->sacl_len
= (sacl_present
&& sacl
) ? sacl
->AclSize
: 0;
125 (*server_sd
)->dacl_len
= (dacl_present
&& dacl
) ? dacl
->AclSize
: 0;
127 ptr
= (unsigned char *)(*server_sd
+ 1);
128 memcpy(ptr
, owner
, (*server_sd
)->owner_len
);
129 ptr
+= (*server_sd
)->owner_len
;
130 memcpy(ptr
, group
, (*server_sd
)->group_len
);
131 ptr
+= (*server_sd
)->group_len
;
132 memcpy(ptr
, sacl
, (*server_sd
)->sacl_len
);
133 ptr
+= (*server_sd
)->sacl_len
;
134 memcpy(ptr
, dacl
, (*server_sd
)->dacl_len
);
136 *server_sd_len
= len
;
138 return STATUS_SUCCESS
;
141 /* frees a struct security_descriptor allocated by NTDLL_create_struct_sd */
142 void NTDLL_free_struct_sd(struct security_descriptor
*server_sd
)
144 RtlFreeHeap(GetProcessHeap(), 0, server_sd
);
151 /******************************************************************************
152 * NtCreateSemaphore (NTDLL.@)
154 NTSTATUS WINAPI
NtCreateSemaphore( OUT PHANDLE SemaphoreHandle
,
155 IN ACCESS_MASK access
,
156 IN
const OBJECT_ATTRIBUTES
*attr OPTIONAL
,
157 IN LONG InitialCount
,
158 IN LONG MaximumCount
)
160 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
162 struct object_attributes objattr
;
163 struct security_descriptor
*sd
= NULL
;
165 if (MaximumCount
<= 0 || InitialCount
< 0 || InitialCount
> MaximumCount
)
166 return STATUS_INVALID_PARAMETER
;
167 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
169 objattr
.rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
171 objattr
.name_len
= len
;
174 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
175 if (ret
!= STATUS_SUCCESS
) return ret
;
178 SERVER_START_REQ( create_semaphore
)
180 req
->access
= access
;
181 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
182 req
->initial
= InitialCount
;
183 req
->max
= MaximumCount
;
184 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
185 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
186 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
187 ret
= wine_server_call( req
);
188 *SemaphoreHandle
= wine_server_ptr_handle( reply
->handle
);
192 NTDLL_free_struct_sd( sd
);
197 /******************************************************************************
198 * NtOpenSemaphore (NTDLL.@)
200 NTSTATUS WINAPI
NtOpenSemaphore( OUT PHANDLE SemaphoreHandle
,
201 IN ACCESS_MASK access
,
202 IN
const OBJECT_ATTRIBUTES
*attr
)
204 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
207 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
209 SERVER_START_REQ( open_semaphore
)
211 req
->access
= access
;
212 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
213 req
->rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
214 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
215 ret
= wine_server_call( req
);
216 *SemaphoreHandle
= wine_server_ptr_handle( reply
->handle
);
222 /******************************************************************************
223 * NtQuerySemaphore (NTDLL.@)
225 NTSTATUS WINAPI
NtQuerySemaphore( HANDLE handle
, SEMAPHORE_INFORMATION_CLASS
class,
226 void *info
, ULONG len
, ULONG
*ret_len
)
229 SEMAPHORE_BASIC_INFORMATION
*out
= info
;
231 if (class != SemaphoreBasicInformation
)
233 FIXME("(%p,%d,%u) Unknown class\n", handle
, class, len
);
234 return STATUS_INVALID_INFO_CLASS
;
237 if (len
!= sizeof(SEMAPHORE_BASIC_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
239 SERVER_START_REQ( query_semaphore
)
241 req
->handle
= wine_server_obj_handle( handle
);
242 if (!(ret
= wine_server_call( req
)))
244 out
->CurrentCount
= reply
->current
;
245 out
->MaximumCount
= reply
->max
;
246 if (ret_len
) *ret_len
= sizeof(SEMAPHORE_BASIC_INFORMATION
);
254 /******************************************************************************
255 * NtReleaseSemaphore (NTDLL.@)
257 NTSTATUS WINAPI
NtReleaseSemaphore( HANDLE handle
, ULONG count
, PULONG previous
)
260 SERVER_START_REQ( release_semaphore
)
262 req
->handle
= wine_server_obj_handle( handle
);
264 if (!(ret
= wine_server_call( req
)))
266 if (previous
) *previous
= reply
->prev_count
;
277 /**************************************************************************
278 * NtCreateEvent (NTDLL.@)
279 * ZwCreateEvent (NTDLL.@)
281 NTSTATUS WINAPI
NtCreateEvent( PHANDLE EventHandle
, ACCESS_MASK DesiredAccess
,
282 const OBJECT_ATTRIBUTES
*attr
, EVENT_TYPE type
, BOOLEAN InitialState
)
284 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
286 struct security_descriptor
*sd
= NULL
;
287 struct object_attributes objattr
;
289 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
291 objattr
.rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
293 objattr
.name_len
= len
;
296 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
297 if (ret
!= STATUS_SUCCESS
) return ret
;
300 SERVER_START_REQ( create_event
)
302 req
->access
= DesiredAccess
;
303 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
304 req
->manual_reset
= (type
== NotificationEvent
);
305 req
->initial_state
= InitialState
;
306 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
307 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
308 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
309 ret
= wine_server_call( req
);
310 *EventHandle
= wine_server_ptr_handle( reply
->handle
);
314 NTDLL_free_struct_sd( sd
);
319 /******************************************************************************
320 * NtOpenEvent (NTDLL.@)
321 * ZwOpenEvent (NTDLL.@)
323 NTSTATUS WINAPI
NtOpenEvent(
324 OUT PHANDLE EventHandle
,
325 IN ACCESS_MASK DesiredAccess
,
326 IN
const OBJECT_ATTRIBUTES
*attr
)
328 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
331 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
333 SERVER_START_REQ( open_event
)
335 req
->access
= DesiredAccess
;
336 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
337 req
->rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
338 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
339 ret
= wine_server_call( req
);
340 *EventHandle
= wine_server_ptr_handle( reply
->handle
);
347 /******************************************************************************
348 * NtSetEvent (NTDLL.@)
349 * ZwSetEvent (NTDLL.@)
351 NTSTATUS WINAPI
NtSetEvent( HANDLE handle
, PULONG NumberOfThreadsReleased
)
355 /* FIXME: set NumberOfThreadsReleased */
357 SERVER_START_REQ( event_op
)
359 req
->handle
= wine_server_obj_handle( handle
);
361 ret
= wine_server_call( req
);
367 /******************************************************************************
368 * NtResetEvent (NTDLL.@)
370 NTSTATUS WINAPI
NtResetEvent( HANDLE handle
, PULONG NumberOfThreadsReleased
)
374 /* resetting an event can't release any thread... */
375 if (NumberOfThreadsReleased
) *NumberOfThreadsReleased
= 0;
377 SERVER_START_REQ( event_op
)
379 req
->handle
= wine_server_obj_handle( handle
);
380 req
->op
= RESET_EVENT
;
381 ret
= wine_server_call( req
);
387 /******************************************************************************
388 * NtClearEvent (NTDLL.@)
391 * same as NtResetEvent ???
393 NTSTATUS WINAPI
NtClearEvent ( HANDLE handle
)
395 return NtResetEvent( handle
, NULL
);
398 /******************************************************************************
399 * NtPulseEvent (NTDLL.@)
404 NTSTATUS WINAPI
NtPulseEvent( HANDLE handle
, PULONG PulseCount
)
409 FIXME("(%p,%d)\n", handle
, *PulseCount
);
411 SERVER_START_REQ( event_op
)
413 req
->handle
= wine_server_obj_handle( handle
);
414 req
->op
= PULSE_EVENT
;
415 ret
= wine_server_call( req
);
421 /******************************************************************************
422 * NtQueryEvent (NTDLL.@)
424 NTSTATUS WINAPI
NtQueryEvent( HANDLE handle
, EVENT_INFORMATION_CLASS
class,
425 void *info
, ULONG len
, ULONG
*ret_len
)
428 EVENT_BASIC_INFORMATION
*out
= info
;
430 if (class != EventBasicInformation
)
432 FIXME("(%p, %d, %d) Unknown class\n",
434 return STATUS_INVALID_INFO_CLASS
;
437 if (len
!= sizeof(EVENT_BASIC_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
439 SERVER_START_REQ( query_event
)
441 req
->handle
= wine_server_obj_handle( handle
);
442 if (!(ret
= wine_server_call( req
)))
444 out
->EventType
= reply
->manual_reset
? NotificationEvent
: SynchronizationEvent
;
445 out
->EventState
= reply
->state
;
446 if (ret_len
) *ret_len
= sizeof(EVENT_BASIC_INFORMATION
);
455 * Mutants (known as Mutexes in Kernel32)
458 /******************************************************************************
459 * NtCreateMutant [NTDLL.@]
460 * ZwCreateMutant [NTDLL.@]
462 NTSTATUS WINAPI
NtCreateMutant(OUT HANDLE
* MutantHandle
,
463 IN ACCESS_MASK access
,
464 IN
const OBJECT_ATTRIBUTES
* attr OPTIONAL
,
465 IN BOOLEAN InitialOwner
)
468 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
469 struct security_descriptor
*sd
= NULL
;
470 struct object_attributes objattr
;
472 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
474 objattr
.rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
476 objattr
.name_len
= len
;
479 status
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
480 if (status
!= STATUS_SUCCESS
) return status
;
483 SERVER_START_REQ( create_mutex
)
485 req
->access
= access
;
486 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
487 req
->owned
= InitialOwner
;
488 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
489 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
490 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
491 status
= wine_server_call( req
);
492 *MutantHandle
= wine_server_ptr_handle( reply
->handle
);
496 NTDLL_free_struct_sd( sd
);
501 /**************************************************************************
502 * NtOpenMutant [NTDLL.@]
503 * ZwOpenMutant [NTDLL.@]
505 NTSTATUS WINAPI
NtOpenMutant(OUT HANDLE
* MutantHandle
,
506 IN ACCESS_MASK access
,
507 IN
const OBJECT_ATTRIBUTES
* attr
)
510 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
512 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
514 SERVER_START_REQ( open_mutex
)
516 req
->access
= access
;
517 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
518 req
->rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
519 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
520 status
= wine_server_call( req
);
521 *MutantHandle
= wine_server_ptr_handle( reply
->handle
);
527 /**************************************************************************
528 * NtReleaseMutant [NTDLL.@]
529 * ZwReleaseMutant [NTDLL.@]
531 NTSTATUS WINAPI
NtReleaseMutant( IN HANDLE handle
, OUT PLONG prev_count OPTIONAL
)
535 SERVER_START_REQ( release_mutex
)
537 req
->handle
= wine_server_obj_handle( handle
);
538 status
= wine_server_call( req
);
539 if (prev_count
) *prev_count
= reply
->prev_count
;
545 /******************************************************************
546 * NtQueryMutant [NTDLL.@]
547 * ZwQueryMutant [NTDLL.@]
549 NTSTATUS WINAPI
NtQueryMutant(IN HANDLE handle
,
550 IN MUTANT_INFORMATION_CLASS MutantInformationClass
,
551 OUT PVOID MutantInformation
,
552 IN ULONG MutantInformationLength
,
553 OUT PULONG ResultLength OPTIONAL
)
555 FIXME("(%p %u %p %u %p): stub!\n",
556 handle
, MutantInformationClass
, MutantInformation
, MutantInformationLength
, ResultLength
);
557 return STATUS_NOT_IMPLEMENTED
;
564 /******************************************************************************
565 * NtCreateJobObject [NTDLL.@]
566 * ZwCreateJobObject [NTDLL.@]
568 NTSTATUS WINAPI
NtCreateJobObject( PHANDLE handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
570 FIXME( "stub: %p %x %s\n", handle
, access
, attr
? debugstr_us(attr
->ObjectName
) : "" );
571 *handle
= (HANDLE
)0xdead;
572 return STATUS_SUCCESS
;
575 /******************************************************************************
576 * NtOpenJobObject [NTDLL.@]
577 * ZwOpenJobObject [NTDLL.@]
579 NTSTATUS WINAPI
NtOpenJobObject( PHANDLE handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
581 FIXME( "stub: %p %x %s\n", handle
, access
, attr
? debugstr_us(attr
->ObjectName
) : "" );
582 return STATUS_NOT_IMPLEMENTED
;
585 /******************************************************************************
586 * NtTerminateJobObject [NTDLL.@]
587 * ZwTerminateJobObject [NTDLL.@]
589 NTSTATUS WINAPI
NtTerminateJobObject( HANDLE handle
, NTSTATUS status
)
591 FIXME( "stub: %p %x\n", handle
, status
);
592 return STATUS_SUCCESS
;
595 /******************************************************************************
596 * NtQueryInformationJobObject [NTDLL.@]
597 * ZwQueryInformationJobObject [NTDLL.@]
599 NTSTATUS WINAPI
NtQueryInformationJobObject( HANDLE handle
, JOBOBJECTINFOCLASS
class, PVOID info
,
600 ULONG len
, PULONG ret_len
)
602 FIXME( "stub: %p %u %p %u %p\n", handle
, class, info
, len
, ret_len
);
603 return STATUS_NOT_IMPLEMENTED
;
606 /******************************************************************************
607 * NtSetInformationJobObject [NTDLL.@]
608 * ZwSetInformationJobObject [NTDLL.@]
610 NTSTATUS WINAPI
NtSetInformationJobObject( HANDLE handle
, JOBOBJECTINFOCLASS
class, PVOID info
, ULONG len
)
612 FIXME( "stub: %p %u %p %u\n", handle
, class, info
, len
);
613 return STATUS_SUCCESS
;
616 /******************************************************************************
617 * NtIsProcessInJob [NTDLL.@]
618 * ZwIsProcessInJob [NTDLL.@]
620 NTSTATUS WINAPI
NtIsProcessInJob( HANDLE process
, HANDLE job
)
622 FIXME( "stub: %p %p\n", process
, job
);
623 return STATUS_PROCESS_NOT_IN_JOB
;
626 /******************************************************************************
627 * NtAssignProcessToJobObject [NTDLL.@]
628 * ZwAssignProcessToJobObject [NTDLL.@]
630 NTSTATUS WINAPI
NtAssignProcessToJobObject( HANDLE job
, HANDLE process
)
632 FIXME( "stub: %p %p\n", job
, process
);
633 return STATUS_SUCCESS
;
640 /**************************************************************************
641 * NtCreateTimer [NTDLL.@]
642 * ZwCreateTimer [NTDLL.@]
644 NTSTATUS WINAPI
NtCreateTimer(OUT HANDLE
*handle
,
645 IN ACCESS_MASK access
,
646 IN
const OBJECT_ATTRIBUTES
*attr OPTIONAL
,
647 IN TIMER_TYPE timer_type
)
649 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
652 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
654 if (timer_type
!= NotificationTimer
&& timer_type
!= SynchronizationTimer
)
655 return STATUS_INVALID_PARAMETER
;
657 SERVER_START_REQ( create_timer
)
659 req
->access
= access
;
660 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
661 req
->rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
662 req
->manual
= (timer_type
== NotificationTimer
);
663 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
664 status
= wine_server_call( req
);
665 *handle
= wine_server_ptr_handle( reply
->handle
);
672 /**************************************************************************
673 * NtOpenTimer [NTDLL.@]
674 * ZwOpenTimer [NTDLL.@]
676 NTSTATUS WINAPI
NtOpenTimer(OUT PHANDLE handle
,
677 IN ACCESS_MASK access
,
678 IN
const OBJECT_ATTRIBUTES
* attr
)
680 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
683 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
685 SERVER_START_REQ( open_timer
)
687 req
->access
= access
;
688 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
689 req
->rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
690 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
691 status
= wine_server_call( req
);
692 *handle
= wine_server_ptr_handle( reply
->handle
);
698 /**************************************************************************
699 * NtSetTimer [NTDLL.@]
700 * ZwSetTimer [NTDLL.@]
702 NTSTATUS WINAPI
NtSetTimer(IN HANDLE handle
,
703 IN
const LARGE_INTEGER
* when
,
704 IN PTIMER_APC_ROUTINE callback
,
705 IN PVOID callback_arg
,
707 IN ULONG period OPTIONAL
,
708 OUT PBOOLEAN state OPTIONAL
)
710 NTSTATUS status
= STATUS_SUCCESS
;
712 TRACE("(%p,%p,%p,%p,%08x,0x%08x,%p) stub\n",
713 handle
, when
, callback
, callback_arg
, resume
, period
, state
);
715 SERVER_START_REQ( set_timer
)
717 req
->handle
= wine_server_obj_handle( handle
);
718 req
->period
= period
;
719 req
->expire
= when
->QuadPart
;
720 req
->callback
= wine_server_client_ptr( callback
);
721 req
->arg
= wine_server_client_ptr( callback_arg
);
722 status
= wine_server_call( req
);
723 if (state
) *state
= reply
->signaled
;
727 /* set error but can still succeed */
728 if (resume
&& status
== STATUS_SUCCESS
) return STATUS_TIMER_RESUME_IGNORED
;
732 /**************************************************************************
733 * NtCancelTimer [NTDLL.@]
734 * ZwCancelTimer [NTDLL.@]
736 NTSTATUS WINAPI
NtCancelTimer(IN HANDLE handle
, OUT BOOLEAN
* state
)
740 SERVER_START_REQ( cancel_timer
)
742 req
->handle
= wine_server_obj_handle( handle
);
743 status
= wine_server_call( req
);
744 if (state
) *state
= reply
->signaled
;
750 /******************************************************************************
751 * NtQueryTimer (NTDLL.@)
753 * Retrieves information about a timer.
756 * TimerHandle [I] The timer to retrieve information about.
757 * TimerInformationClass [I] The type of information to retrieve.
758 * TimerInformation [O] Pointer to buffer to store information in.
759 * Length [I] The length of the buffer pointed to by TimerInformation.
760 * ReturnLength [O] Optional. The size of buffer actually used.
763 * Success: STATUS_SUCCESS
764 * Failure: STATUS_INFO_LENGTH_MISMATCH, if Length doesn't match the required data
765 * size for the class specified.
766 * STATUS_INVALID_INFO_CLASS, if an invalid TimerInformationClass was specified.
767 * STATUS_ACCESS_DENIED, if TimerHandle does not have TIMER_QUERY_STATE access
770 NTSTATUS WINAPI
NtQueryTimer(
772 TIMER_INFORMATION_CLASS TimerInformationClass
,
773 PVOID TimerInformation
,
777 TIMER_BASIC_INFORMATION
* basic_info
= TimerInformation
;
781 TRACE("(%p,%d,%p,0x%08x,%p)\n", TimerHandle
, TimerInformationClass
,
782 TimerInformation
, Length
, ReturnLength
);
784 switch (TimerInformationClass
)
786 case TimerBasicInformation
:
787 if (Length
< sizeof(TIMER_BASIC_INFORMATION
))
788 return STATUS_INFO_LENGTH_MISMATCH
;
790 SERVER_START_REQ(get_timer_info
)
792 req
->handle
= wine_server_obj_handle( TimerHandle
);
793 status
= wine_server_call(req
);
795 /* convert server time to absolute NTDLL time */
796 basic_info
->RemainingTime
.QuadPart
= reply
->when
;
797 basic_info
->TimerState
= reply
->signaled
;
801 /* convert from absolute into relative time */
802 NtQuerySystemTime(&now
);
803 if (now
.QuadPart
> basic_info
->RemainingTime
.QuadPart
)
804 basic_info
->RemainingTime
.QuadPart
= 0;
806 basic_info
->RemainingTime
.QuadPart
-= now
.QuadPart
;
808 if (ReturnLength
) *ReturnLength
= sizeof(TIMER_BASIC_INFORMATION
);
813 FIXME("Unhandled class %d\n", TimerInformationClass
);
814 return STATUS_INVALID_INFO_CLASS
;
818 /******************************************************************************
819 * NtQueryTimerResolution [NTDLL.@]
821 NTSTATUS WINAPI
NtQueryTimerResolution(OUT ULONG
* min_resolution
,
822 OUT ULONG
* max_resolution
,
823 OUT ULONG
* current_resolution
)
825 FIXME("(%p,%p,%p), stub!\n",
826 min_resolution
, max_resolution
, current_resolution
);
828 return STATUS_NOT_IMPLEMENTED
;
831 /******************************************************************************
832 * NtSetTimerResolution [NTDLL.@]
834 NTSTATUS WINAPI
NtSetTimerResolution(IN ULONG resolution
,
835 IN BOOLEAN set_resolution
,
836 OUT ULONG
* current_resolution
)
838 FIXME("(%u,%u,%p), stub!\n",
839 resolution
, set_resolution
, current_resolution
);
841 return STATUS_NOT_IMPLEMENTED
;
846 /* wait operations */
848 /******************************************************************
849 * NtWaitForMultipleObjects (NTDLL.@)
851 NTSTATUS WINAPI
NtWaitForMultipleObjects( DWORD count
, const HANDLE
*handles
,
852 BOOLEAN wait_all
, BOOLEAN alertable
,
853 const LARGE_INTEGER
*timeout
)
855 select_op_t select_op
;
856 UINT i
, flags
= SELECT_INTERRUPTIBLE
;
858 if (!count
|| count
> MAXIMUM_WAIT_OBJECTS
) return STATUS_INVALID_PARAMETER_1
;
860 if (alertable
) flags
|= SELECT_ALERTABLE
;
861 select_op
.wait
.op
= wait_all
? SELECT_WAIT_ALL
: SELECT_WAIT
;
862 for (i
= 0; i
< count
; i
++) select_op
.wait
.handles
[i
] = wine_server_obj_handle( handles
[i
] );
863 return server_select( &select_op
, offsetof( select_op_t
, wait
.handles
[count
] ), flags
, timeout
);
867 /******************************************************************
868 * NtWaitForSingleObject (NTDLL.@)
870 NTSTATUS WINAPI
NtWaitForSingleObject(HANDLE handle
, BOOLEAN alertable
, const LARGE_INTEGER
*timeout
)
872 return NtWaitForMultipleObjects( 1, &handle
, FALSE
, alertable
, timeout
);
876 /******************************************************************
877 * NtSignalAndWaitForSingleObject (NTDLL.@)
879 NTSTATUS WINAPI
NtSignalAndWaitForSingleObject( HANDLE hSignalObject
, HANDLE hWaitObject
,
880 BOOLEAN alertable
, const LARGE_INTEGER
*timeout
)
882 select_op_t select_op
;
883 UINT flags
= SELECT_INTERRUPTIBLE
;
885 if (!hSignalObject
) return STATUS_INVALID_HANDLE
;
887 if (alertable
) flags
|= SELECT_ALERTABLE
;
888 select_op
.signal_and_wait
.op
= SELECT_SIGNAL_AND_WAIT
;
889 select_op
.signal_and_wait
.wait
= wine_server_obj_handle( hWaitObject
);
890 select_op
.signal_and_wait
.signal
= wine_server_obj_handle( hSignalObject
);
891 return server_select( &select_op
, sizeof(select_op
.signal_and_wait
), flags
, timeout
);
895 /******************************************************************
896 * NtYieldExecution (NTDLL.@)
898 NTSTATUS WINAPI
NtYieldExecution(void)
900 #ifdef HAVE_SCHED_YIELD
902 return STATUS_SUCCESS
;
904 return STATUS_NO_YIELD_PERFORMED
;
909 /******************************************************************
910 * NtDelayExecution (NTDLL.@)
912 NTSTATUS WINAPI
NtDelayExecution( BOOLEAN alertable
, const LARGE_INTEGER
*timeout
)
914 /* if alertable, we need to query the server */
916 return server_select( NULL
, 0, SELECT_INTERRUPTIBLE
| SELECT_ALERTABLE
, timeout
);
918 if (!timeout
|| timeout
->QuadPart
== TIMEOUT_INFINITE
) /* sleep forever */
920 for (;;) select( 0, NULL
, NULL
, NULL
, NULL
);
925 timeout_t when
, diff
;
927 if ((when
= timeout
->QuadPart
) < 0)
929 NtQuerySystemTime( &now
);
930 when
= now
.QuadPart
- when
;
933 /* Note that we yield after establishing the desired timeout */
935 if (!when
) return STATUS_SUCCESS
;
940 NtQuerySystemTime( &now
);
941 diff
= (when
- now
.QuadPart
+ 9) / 10;
942 if (diff
<= 0) break;
943 tv
.tv_sec
= diff
/ 1000000;
944 tv
.tv_usec
= diff
% 1000000;
945 if (select( 0, NULL
, NULL
, NULL
, &tv
) != -1) break;
948 return STATUS_SUCCESS
;
952 /******************************************************************************
953 * NtCreateKeyedEvent (NTDLL.@)
955 NTSTATUS WINAPI
NtCreateKeyedEvent( HANDLE
*handle
, ACCESS_MASK access
,
956 const OBJECT_ATTRIBUTES
*attr
, ULONG flags
)
958 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
960 struct security_descriptor
*sd
= NULL
;
961 struct object_attributes objattr
;
963 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
965 objattr
.rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
967 objattr
.name_len
= len
;
970 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
971 if (ret
!= STATUS_SUCCESS
) return ret
;
974 SERVER_START_REQ( create_keyed_event
)
976 req
->access
= access
;
977 req
->attributes
= attr
? attr
->Attributes
: 0;
978 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
979 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
980 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
981 ret
= wine_server_call( req
);
982 *handle
= wine_server_ptr_handle( reply
->handle
);
986 NTDLL_free_struct_sd( sd
);
990 /******************************************************************************
991 * NtOpenKeyedEvent (NTDLL.@)
993 NTSTATUS WINAPI
NtOpenKeyedEvent( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
995 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
998 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
1000 SERVER_START_REQ( open_keyed_event
)
1002 req
->access
= access
;
1003 req
->attributes
= attr
? attr
->Attributes
: 0;
1004 req
->rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
1005 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1006 ret
= wine_server_call( req
);
1007 *handle
= wine_server_ptr_handle( reply
->handle
);
1013 /******************************************************************************
1014 * NtWaitForKeyedEvent (NTDLL.@)
1016 NTSTATUS WINAPI
NtWaitForKeyedEvent( HANDLE handle
, const void *key
,
1017 BOOLEAN alertable
, const LARGE_INTEGER
*timeout
)
1019 select_op_t select_op
;
1020 UINT flags
= SELECT_INTERRUPTIBLE
;
1022 if ((ULONG_PTR
)key
& 1) return STATUS_INVALID_PARAMETER_1
;
1023 if (alertable
) flags
|= SELECT_ALERTABLE
;
1024 select_op
.keyed_event
.op
= SELECT_KEYED_EVENT_WAIT
;
1025 select_op
.keyed_event
.handle
= wine_server_obj_handle( handle
);
1026 select_op
.keyed_event
.key
= wine_server_client_ptr( key
);
1027 return server_select( &select_op
, sizeof(select_op
.keyed_event
), flags
, timeout
);
1030 /******************************************************************************
1031 * NtReleaseKeyedEvent (NTDLL.@)
1033 NTSTATUS WINAPI
NtReleaseKeyedEvent( HANDLE handle
, const void *key
,
1034 BOOLEAN alertable
, const LARGE_INTEGER
*timeout
)
1036 select_op_t select_op
;
1037 UINT flags
= SELECT_INTERRUPTIBLE
;
1039 if ((ULONG_PTR
)key
& 1) return STATUS_INVALID_PARAMETER_1
;
1040 if (alertable
) flags
|= SELECT_ALERTABLE
;
1041 select_op
.keyed_event
.op
= SELECT_KEYED_EVENT_RELEASE
;
1042 select_op
.keyed_event
.handle
= wine_server_obj_handle( handle
);
1043 select_op
.keyed_event
.key
= wine_server_client_ptr( key
);
1044 return server_select( &select_op
, sizeof(select_op
.keyed_event
), flags
, timeout
);
1047 /******************************************************************
1048 * NtCreateIoCompletion (NTDLL.@)
1049 * ZwCreateIoCompletion (NTDLL.@)
1051 * Creates I/O completion object.
1054 * CompletionPort [O] created completion object handle will be placed there
1055 * DesiredAccess [I] desired access to a handle (combination of IO_COMPLETION_*)
1056 * ObjectAttributes [I] completion object attributes
1057 * NumberOfConcurrentThreads [I] desired number of concurrent active worker threads
1060 NTSTATUS WINAPI
NtCreateIoCompletion( PHANDLE CompletionPort
, ACCESS_MASK DesiredAccess
,
1061 POBJECT_ATTRIBUTES ObjectAttributes
, ULONG NumberOfConcurrentThreads
)
1065 TRACE("(%p, %x, %p, %d)\n", CompletionPort
, DesiredAccess
,
1066 ObjectAttributes
, NumberOfConcurrentThreads
);
1068 if (!CompletionPort
)
1069 return STATUS_INVALID_PARAMETER
;
1071 SERVER_START_REQ( create_completion
)
1073 req
->access
= DesiredAccess
;
1074 req
->attributes
= ObjectAttributes
? ObjectAttributes
->Attributes
: 0;
1075 req
->rootdir
= wine_server_obj_handle( ObjectAttributes
? ObjectAttributes
->RootDirectory
: 0 );
1076 req
->concurrent
= NumberOfConcurrentThreads
;
1077 if (ObjectAttributes
&& ObjectAttributes
->ObjectName
)
1078 wine_server_add_data( req
, ObjectAttributes
->ObjectName
->Buffer
,
1079 ObjectAttributes
->ObjectName
->Length
);
1080 if (!(status
= wine_server_call( req
)))
1081 *CompletionPort
= wine_server_ptr_handle( reply
->handle
);
1087 /******************************************************************
1088 * NtSetIoCompletion (NTDLL.@)
1089 * ZwSetIoCompletion (NTDLL.@)
1091 * Inserts completion message into queue
1094 * CompletionPort [I] HANDLE to completion object
1095 * CompletionKey [I] completion key
1096 * CompletionValue [I] completion value (usually pointer to OVERLAPPED)
1097 * Status [I] operation status
1098 * NumberOfBytesTransferred [I] number of bytes transferred
1100 NTSTATUS WINAPI
NtSetIoCompletion( HANDLE CompletionPort
, ULONG_PTR CompletionKey
,
1101 ULONG_PTR CompletionValue
, NTSTATUS Status
,
1102 SIZE_T NumberOfBytesTransferred
)
1106 TRACE("(%p, %lx, %lx, %x, %lx)\n", CompletionPort
, CompletionKey
,
1107 CompletionValue
, Status
, NumberOfBytesTransferred
);
1109 SERVER_START_REQ( add_completion
)
1111 req
->handle
= wine_server_obj_handle( CompletionPort
);
1112 req
->ckey
= CompletionKey
;
1113 req
->cvalue
= CompletionValue
;
1114 req
->status
= Status
;
1115 req
->information
= NumberOfBytesTransferred
;
1116 status
= wine_server_call( req
);
1122 /******************************************************************
1123 * NtRemoveIoCompletion (NTDLL.@)
1124 * ZwRemoveIoCompletion (NTDLL.@)
1126 * (Wait for and) retrieve first completion message from completion object's queue
1129 * CompletionPort [I] HANDLE to I/O completion object
1130 * CompletionKey [O] completion key
1131 * CompletionValue [O] Completion value given in NtSetIoCompletion or in async operation
1132 * iosb [O] IO_STATUS_BLOCK of completed asynchronous operation
1133 * WaitTime [I] optional wait time in NTDLL format
1136 NTSTATUS WINAPI
NtRemoveIoCompletion( HANDLE CompletionPort
, PULONG_PTR CompletionKey
,
1137 PULONG_PTR CompletionValue
, PIO_STATUS_BLOCK iosb
,
1138 PLARGE_INTEGER WaitTime
)
1142 TRACE("(%p, %p, %p, %p, %p)\n", CompletionPort
, CompletionKey
,
1143 CompletionValue
, iosb
, WaitTime
);
1147 SERVER_START_REQ( remove_completion
)
1149 req
->handle
= wine_server_obj_handle( CompletionPort
);
1150 if (!(status
= wine_server_call( req
)))
1152 *CompletionKey
= reply
->ckey
;
1153 *CompletionValue
= reply
->cvalue
;
1154 iosb
->Information
= reply
->information
;
1155 iosb
->u
.Status
= reply
->status
;
1159 if (status
!= STATUS_PENDING
) break;
1161 status
= NtWaitForSingleObject( CompletionPort
, FALSE
, WaitTime
);
1162 if (status
!= WAIT_OBJECT_0
) break;
1167 /******************************************************************
1168 * NtOpenIoCompletion (NTDLL.@)
1169 * ZwOpenIoCompletion (NTDLL.@)
1171 * Opens I/O completion object
1174 * CompletionPort [O] completion object handle will be placed there
1175 * DesiredAccess [I] desired access to a handle (combination of IO_COMPLETION_*)
1176 * ObjectAttributes [I] completion object name
1179 NTSTATUS WINAPI
NtOpenIoCompletion( PHANDLE CompletionPort
, ACCESS_MASK DesiredAccess
,
1180 POBJECT_ATTRIBUTES ObjectAttributes
)
1184 TRACE("(%p, 0x%x, %p)\n", CompletionPort
, DesiredAccess
, ObjectAttributes
);
1186 if (!CompletionPort
|| !ObjectAttributes
|| !ObjectAttributes
->ObjectName
)
1187 return STATUS_INVALID_PARAMETER
;
1189 SERVER_START_REQ( open_completion
)
1191 req
->access
= DesiredAccess
;
1192 req
->rootdir
= wine_server_obj_handle( ObjectAttributes
->RootDirectory
);
1193 wine_server_add_data( req
, ObjectAttributes
->ObjectName
->Buffer
,
1194 ObjectAttributes
->ObjectName
->Length
);
1195 if (!(status
= wine_server_call( req
)))
1196 *CompletionPort
= wine_server_ptr_handle( reply
->handle
);
1202 /******************************************************************
1203 * NtQueryIoCompletion (NTDLL.@)
1204 * ZwQueryIoCompletion (NTDLL.@)
1206 * Requests information about given I/O completion object
1209 * CompletionPort [I] HANDLE to completion port to request
1210 * InformationClass [I] information class
1211 * CompletionInformation [O] user-provided buffer for data
1212 * BufferLength [I] buffer length
1213 * RequiredLength [O] required buffer length
1216 NTSTATUS WINAPI
NtQueryIoCompletion( HANDLE CompletionPort
, IO_COMPLETION_INFORMATION_CLASS InformationClass
,
1217 PVOID CompletionInformation
, ULONG BufferLength
, PULONG RequiredLength
)
1221 TRACE("(%p, %d, %p, 0x%x, %p)\n", CompletionPort
, InformationClass
, CompletionInformation
,
1222 BufferLength
, RequiredLength
);
1224 if (!CompletionInformation
) return STATUS_INVALID_PARAMETER
;
1225 switch( InformationClass
)
1227 case IoCompletionBasicInformation
:
1229 ULONG
*info
= CompletionInformation
;
1231 if (RequiredLength
) *RequiredLength
= sizeof(*info
);
1232 if (BufferLength
!= sizeof(*info
))
1233 status
= STATUS_INFO_LENGTH_MISMATCH
;
1236 SERVER_START_REQ( query_completion
)
1238 req
->handle
= wine_server_obj_handle( CompletionPort
);
1239 if (!(status
= wine_server_call( req
)))
1240 *info
= reply
->depth
;
1247 status
= STATUS_INVALID_PARAMETER
;
1253 NTSTATUS
NTDLL_AddCompletion( HANDLE hFile
, ULONG_PTR CompletionValue
,
1254 NTSTATUS CompletionStatus
, ULONG Information
)
1258 SERVER_START_REQ( add_fd_completion
)
1260 req
->handle
= wine_server_obj_handle( hFile
);
1261 req
->cvalue
= CompletionValue
;
1262 req
->status
= CompletionStatus
;
1263 req
->information
= Information
;
1264 status
= wine_server_call( req
);
1270 /******************************************************************
1271 * RtlRunOnceInitialize (NTDLL.@)
1273 void WINAPI
RtlRunOnceInitialize( RTL_RUN_ONCE
*once
)
1278 /******************************************************************
1279 * RtlRunOnceBeginInitialize (NTDLL.@)
1281 DWORD WINAPI
RtlRunOnceBeginInitialize( RTL_RUN_ONCE
*once
, ULONG flags
, void **context
)
1283 if (flags
& RTL_RUN_ONCE_CHECK_ONLY
)
1285 ULONG_PTR val
= (ULONG_PTR
)once
->Ptr
;
1287 if (flags
& RTL_RUN_ONCE_ASYNC
) return STATUS_INVALID_PARAMETER
;
1288 if ((val
& 3) != 2) return STATUS_UNSUCCESSFUL
;
1289 if (context
) *context
= (void *)(val
& ~3);
1290 return STATUS_SUCCESS
;
1295 ULONG_PTR next
, val
= (ULONG_PTR
)once
->Ptr
;
1299 case 0: /* first time */
1300 if (!interlocked_cmpxchg_ptr( &once
->Ptr
,
1301 (flags
& RTL_RUN_ONCE_ASYNC
) ? (void *)3 : (void *)1, 0 ))
1302 return STATUS_PENDING
;
1305 case 1: /* in progress, wait */
1306 if (flags
& RTL_RUN_ONCE_ASYNC
) return STATUS_INVALID_PARAMETER
;
1308 if (interlocked_cmpxchg_ptr( &once
->Ptr
, (void *)((ULONG_PTR
)&next
| 1),
1309 (void *)val
) == (void *)val
)
1310 NtWaitForKeyedEvent( keyed_event
, &next
, FALSE
, NULL
);
1314 if (context
) *context
= (void *)(val
& ~3);
1315 return STATUS_SUCCESS
;
1317 case 3: /* in progress, async */
1318 if (!(flags
& RTL_RUN_ONCE_ASYNC
)) return STATUS_INVALID_PARAMETER
;
1319 return STATUS_PENDING
;
1324 /******************************************************************
1325 * RtlRunOnceComplete (NTDLL.@)
1327 DWORD WINAPI
RtlRunOnceComplete( RTL_RUN_ONCE
*once
, ULONG flags
, void *context
)
1329 if ((ULONG_PTR
)context
& 3) return STATUS_INVALID_PARAMETER
;
1331 if (flags
& RTL_RUN_ONCE_INIT_FAILED
)
1333 if (context
) return STATUS_INVALID_PARAMETER
;
1334 if (flags
& RTL_RUN_ONCE_ASYNC
) return STATUS_INVALID_PARAMETER
;
1336 else context
= (void *)((ULONG_PTR
)context
| 2);
1340 ULONG_PTR val
= (ULONG_PTR
)once
->Ptr
;
1344 case 1: /* in progress */
1345 if (interlocked_cmpxchg_ptr( &once
->Ptr
, context
, (void *)val
) != (void *)val
) break;
1349 ULONG_PTR next
= *(ULONG_PTR
*)val
;
1350 NtReleaseKeyedEvent( keyed_event
, (void *)val
, FALSE
, NULL
);
1353 return STATUS_SUCCESS
;
1355 case 3: /* in progress, async */
1356 if (!(flags
& RTL_RUN_ONCE_ASYNC
)) return STATUS_INVALID_PARAMETER
;
1357 if (interlocked_cmpxchg_ptr( &once
->Ptr
, context
, (void *)val
) != (void *)val
) break;
1358 return STATUS_SUCCESS
;
1361 return STATUS_UNSUCCESSFUL
;
1366 /******************************************************************
1367 * RtlRunOnceExecuteOnce (NTDLL.@)
1369 DWORD WINAPI
RtlRunOnceExecuteOnce( RTL_RUN_ONCE
*once
, PRTL_RUN_ONCE_INIT_FN func
,
1370 void *param
, void **context
)
1372 DWORD ret
= RtlRunOnceBeginInitialize( once
, 0, context
);
1374 if (ret
!= STATUS_PENDING
) return ret
;
1376 if (!func( once
, param
, context
))
1378 RtlRunOnceComplete( once
, RTL_RUN_ONCE_INIT_FAILED
, NULL
);
1379 return STATUS_UNSUCCESSFUL
;
1382 return RtlRunOnceComplete( once
, 0, context
? *context
: NULL
);
1386 /* SRW locks implementation
1388 * The memory layout used by the lock is:
1391 * ________________ ________________
1392 * | X| #exclusive | #shared |
1393 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1394 * Since there is no space left for a separate counter of shared access
1395 * threads inside the locked section the #shared field is used for multiple
1396 * purposes. The following table lists all possible states the lock can be
1397 * in, notation: [X, #exclusive, #shared]:
1399 * [0, 0, N] -> locked by N shared access threads, if N=0 its unlocked
1400 * [0, >=1, >=1] -> threads are requesting exclusive locks, but there are
1401 * still shared access threads inside. #shared should not be incremented
1403 * [1, >=1, >=0] -> lock is owned by an exclusive thread and the #shared
1404 * counter can be used again to count the number of threads waiting in the
1405 * queue for shared access.
1407 * the following states are invalid and will never occur:
1408 * [0, >=1, 0], [1, 0, >=0]
1410 * The main problem arising from the fact that we have no separate counter
1411 * of shared access threads inside the locked section is that in the state
1412 * [0, >=1, >=1] above we cannot add additional waiting threads to the
1413 * shared access queue - it wouldn't be possible to distinguish waiting
1414 * threads and those that are still inside. To solve this problem the lock
1415 * uses the following approach: a thread that isn't able to allocate a
1416 * shared lock just uses the exclusive queue instead. As soon as the thread
1417 * is woken up it is in the state [1, >=1, >=0]. In this state it's again
1418 * possible to use the shared access queue. The thread atomically moves
1419 * itself to the shared access queue and releases the exclusive lock, so
1420 * that the "real" exclusive access threads have a chance. As soon as they
1421 * are all ready the shared access threads are processed.
1424 #define SRWLOCK_MASK_IN_EXCLUSIVE 0x80000000
1425 #define SRWLOCK_MASK_EXCLUSIVE_QUEUE 0x7fff0000
1426 #define SRWLOCK_MASK_SHARED_QUEUE 0x0000ffff
1427 #define SRWLOCK_RES_EXCLUSIVE 0x00010000
1428 #define SRWLOCK_RES_SHARED 0x00000001
1430 #ifdef WORDS_BIGENDIAN
1431 #define srwlock_key_exclusive(lock) (&lock->Ptr)
1432 #define srwlock_key_shared(lock) ((void *)((char *)&lock->Ptr + 2))
1434 #define srwlock_key_exclusive(lock) ((void *)((char *)&lock->Ptr + 2))
1435 #define srwlock_key_shared(lock) (&lock->Ptr)
1438 static inline void srwlock_check_invalid( unsigned int val
)
1440 /* Throw exception if it's impossible to acquire/release this lock. */
1441 if ((val
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
) == SRWLOCK_MASK_EXCLUSIVE_QUEUE
||
1442 (val
& SRWLOCK_MASK_SHARED_QUEUE
) == SRWLOCK_MASK_SHARED_QUEUE
)
1443 RtlRaiseStatus(STATUS_RESOURCE_NOT_OWNED
);
1446 static inline unsigned int srwlock_lock_exclusive( unsigned int *dest
, int incr
)
1448 unsigned int val
, tmp
;
1449 /* Atomically modifies the value of *dest by adding incr. If the shared
1450 * queue is empty and there are threads waiting for exclusive access, then
1451 * sets the mark SRWLOCK_MASK_IN_EXCLUSIVE to signal other threads that
1452 * they are allowed again to use the shared queue counter. */
1453 for (val
= *dest
;; val
= tmp
)
1456 srwlock_check_invalid( tmp
);
1457 if ((tmp
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
) && !(tmp
& SRWLOCK_MASK_SHARED_QUEUE
))
1458 tmp
|= SRWLOCK_MASK_IN_EXCLUSIVE
;
1459 if ((tmp
= interlocked_cmpxchg( (int *)dest
, tmp
, val
)) == val
)
1465 static inline unsigned int srwlock_unlock_exclusive( unsigned int *dest
, int incr
)
1467 unsigned int val
, tmp
;
1468 /* Atomically modifies the value of *dest by adding incr. If the queue of
1469 * threads waiting for exclusive access is empty, then remove the
1470 * SRWLOCK_MASK_IN_EXCLUSIVE flag (only the shared queue counter will
1472 for (val
= *dest
;; val
= tmp
)
1475 srwlock_check_invalid( tmp
);
1476 if (!(tmp
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
))
1477 tmp
&= SRWLOCK_MASK_SHARED_QUEUE
;
1478 if ((tmp
= interlocked_cmpxchg( (int *)dest
, tmp
, val
)) == val
)
1484 static inline void srwlock_leave_exclusive( RTL_SRWLOCK
*lock
, unsigned int val
)
1486 /* Used when a thread leaves an exclusive section. If there are other
1487 * exclusive access threads they are processed first, followed by
1488 * the shared waiters. */
1489 if (val
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
)
1490 NtReleaseKeyedEvent( keyed_event
, srwlock_key_exclusive(lock
), FALSE
, NULL
);
1493 val
&= SRWLOCK_MASK_SHARED_QUEUE
; /* remove SRWLOCK_MASK_IN_EXCLUSIVE */
1495 NtReleaseKeyedEvent( keyed_event
, srwlock_key_shared(lock
), FALSE
, NULL
);
1499 static inline void srwlock_leave_shared( RTL_SRWLOCK
*lock
, unsigned int val
)
1501 /* Wake up one exclusive thread as soon as the last shared access thread
1503 if ((val
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
) && !(val
& SRWLOCK_MASK_SHARED_QUEUE
))
1504 NtReleaseKeyedEvent( keyed_event
, srwlock_key_exclusive(lock
), FALSE
, NULL
);
1507 /***********************************************************************
1508 * RtlInitializeSRWLock (NTDLL.@)
1511 * Please note that SRWLocks do not keep track of the owner of a lock.
1512 * It doesn't make any difference which thread for example unlocks an
1513 * SRWLock (see corresponding tests). This implementation uses two
1514 * keyed events (one for the exclusive waiters and one for the shared
1515 * waiters) and is limited to 2^15-1 waiting threads.
1517 void WINAPI
RtlInitializeSRWLock( RTL_SRWLOCK
*lock
)
1522 /***********************************************************************
1523 * RtlAcquireSRWLockExclusive (NTDLL.@)
1526 * Unlike RtlAcquireResourceExclusive this function doesn't allow
1527 * nested calls from the same thread. "Upgrading" a shared access lock
1528 * to an exclusive access lock also doesn't seem to be supported.
1530 void WINAPI
RtlAcquireSRWLockExclusive( RTL_SRWLOCK
*lock
)
1532 if (srwlock_lock_exclusive( (unsigned int *)&lock
->Ptr
, SRWLOCK_RES_EXCLUSIVE
))
1533 NtWaitForKeyedEvent( keyed_event
, srwlock_key_exclusive(lock
), FALSE
, NULL
);
1536 /***********************************************************************
1537 * RtlAcquireSRWLockShared (NTDLL.@)
1540 * Do not call this function recursively - it will only succeed when
1541 * there are no threads waiting for an exclusive lock!
1543 void WINAPI
RtlAcquireSRWLockShared( RTL_SRWLOCK
*lock
)
1545 unsigned int val
, tmp
;
1546 /* Acquires a shared lock. If it's currently not possible to add elements to
1547 * the shared queue, then request exclusive access instead. */
1548 for (val
= *(unsigned int *)&lock
->Ptr
;; val
= tmp
)
1550 if ((val
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
) && !(val
& SRWLOCK_MASK_IN_EXCLUSIVE
))
1551 tmp
= val
+ SRWLOCK_RES_EXCLUSIVE
;
1553 tmp
= val
+ SRWLOCK_RES_SHARED
;
1554 if ((tmp
= interlocked_cmpxchg( (int *)&lock
->Ptr
, tmp
, val
)) == val
)
1558 /* Drop exclusive access again and instead requeue for shared access. */
1559 if ((val
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
) && !(val
& SRWLOCK_MASK_IN_EXCLUSIVE
))
1561 NtWaitForKeyedEvent( keyed_event
, srwlock_key_exclusive(lock
), FALSE
, NULL
);
1562 val
= srwlock_unlock_exclusive( (unsigned int *)&lock
->Ptr
, (SRWLOCK_RES_SHARED
1563 - SRWLOCK_RES_EXCLUSIVE
) ) - SRWLOCK_RES_EXCLUSIVE
;
1564 srwlock_leave_exclusive( lock
, val
);
1567 if (val
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
)
1568 NtWaitForKeyedEvent( keyed_event
, srwlock_key_shared(lock
), FALSE
, NULL
);
1571 /***********************************************************************
1572 * RtlReleaseSRWLockExclusive (NTDLL.@)
1574 void WINAPI
RtlReleaseSRWLockExclusive( RTL_SRWLOCK
*lock
)
1576 srwlock_leave_exclusive( lock
, srwlock_unlock_exclusive( (unsigned int *)&lock
->Ptr
,
1577 - SRWLOCK_RES_EXCLUSIVE
) - SRWLOCK_RES_EXCLUSIVE
);
1580 /***********************************************************************
1581 * RtlReleaseSRWLockShared (NTDLL.@)
1583 void WINAPI
RtlReleaseSRWLockShared( RTL_SRWLOCK
*lock
)
1585 srwlock_leave_shared( lock
, srwlock_lock_exclusive( (unsigned int *)&lock
->Ptr
,
1586 - SRWLOCK_RES_SHARED
) - SRWLOCK_RES_SHARED
);
1589 /***********************************************************************
1590 * RtlTryAcquireSRWLockExclusive (NTDLL.@)
1593 * Similar to AcquireSRWLockExclusive recusive calls are not allowed
1594 * and will fail with return value FALSE.
1596 BOOLEAN WINAPI
RtlTryAcquireSRWLockExclusive( RTL_SRWLOCK
*lock
)
1598 return interlocked_cmpxchg( (int *)&lock
->Ptr
, SRWLOCK_MASK_IN_EXCLUSIVE
|
1599 SRWLOCK_RES_EXCLUSIVE
, 0 ) == 0;
1602 /***********************************************************************
1603 * RtlTryAcquireSRWLockShared (NTDLL.@)
1605 BOOLEAN WINAPI
RtlTryAcquireSRWLockShared( RTL_SRWLOCK
*lock
)
1607 unsigned int val
, tmp
;
1608 for (val
= *(unsigned int *)&lock
->Ptr
;; val
= tmp
)
1610 if (val
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
)
1612 if ((tmp
= interlocked_cmpxchg( (int *)&lock
->Ptr
, val
+ SRWLOCK_RES_SHARED
, val
)) == val
)
1618 /***********************************************************************
1619 * RtlInitializeConditionVariable (NTDLL.@)
1621 * Initializes the condition variable with NULL.
1624 * variable [O] condition variable
1629 void WINAPI
RtlInitializeConditionVariable( RTL_CONDITION_VARIABLE
*variable
)
1631 variable
->Ptr
= NULL
;
1634 /***********************************************************************
1635 * RtlWakeConditionVariable (NTDLL.@)
1637 * Wakes up one thread waiting on the condition variable.
1640 * variable [I/O] condition variable to wake up.
1646 * The calling thread does not have to own any lock in order to call
1649 void WINAPI
RtlWakeConditionVariable( RTL_CONDITION_VARIABLE
*variable
)
1651 if (interlocked_dec_if_nonzero( (int *)&variable
->Ptr
))
1652 NtReleaseKeyedEvent( keyed_event
, &variable
->Ptr
, FALSE
, NULL
);
1655 /***********************************************************************
1656 * RtlWakeAllConditionVariable (NTDLL.@)
1658 * See WakeConditionVariable, wakes up all waiting threads.
1660 void WINAPI
RtlWakeAllConditionVariable( RTL_CONDITION_VARIABLE
*variable
)
1662 int val
= interlocked_xchg( (int *)&variable
->Ptr
, 0 );
1664 NtReleaseKeyedEvent( keyed_event
, &variable
->Ptr
, FALSE
, NULL
);
1667 /***********************************************************************
1668 * RtlSleepConditionVariableCS (NTDLL.@)
1670 * Atomically releases the critical section and suspends the thread,
1671 * waiting for a Wake(All)ConditionVariable event. Afterwards it enters
1672 * the critical section again and returns.
1675 * variable [I/O] condition variable
1676 * crit [I/O] critical section to leave temporarily
1677 * timeout [I] timeout
1680 * see NtWaitForKeyedEvent for all possible return values.
1682 NTSTATUS WINAPI
RtlSleepConditionVariableCS( RTL_CONDITION_VARIABLE
*variable
, RTL_CRITICAL_SECTION
*crit
,
1683 const LARGE_INTEGER
*timeout
)
1686 interlocked_xchg_add( (int *)&variable
->Ptr
, 1 );
1687 RtlLeaveCriticalSection( crit
);
1689 status
= NtWaitForKeyedEvent( keyed_event
, &variable
->Ptr
, FALSE
, timeout
);
1690 if (status
!= STATUS_SUCCESS
)
1692 if (!interlocked_dec_if_nonzero( (int *)&variable
->Ptr
))
1693 status
= NtWaitForKeyedEvent( keyed_event
, &variable
->Ptr
, FALSE
, NULL
);
1696 RtlEnterCriticalSection( crit
);
1700 /***********************************************************************
1701 * RtlSleepConditionVariableSRW (NTDLL.@)
1703 * Atomically releases the SRWLock and suspends the thread,
1704 * waiting for a Wake(All)ConditionVariable event. Afterwards it enters
1705 * the SRWLock again with the same access rights and returns.
1708 * variable [I/O] condition variable
1709 * lock [I/O] SRWLock to leave temporarily
1710 * timeout [I] timeout
1711 * flags [I] type of the current lock (exclusive / shared)
1714 * see NtWaitForKeyedEvent for all possible return values.
1717 * the behaviour is undefined if the thread doesn't own the lock.
1719 NTSTATUS WINAPI
RtlSleepConditionVariableSRW( RTL_CONDITION_VARIABLE
*variable
, RTL_SRWLOCK
*lock
,
1720 const LARGE_INTEGER
*timeout
, ULONG flags
)
1723 interlocked_xchg_add( (int *)&variable
->Ptr
, 1 );
1725 if (flags
& RTL_CONDITION_VARIABLE_LOCKMODE_SHARED
)
1726 RtlReleaseSRWLockShared( lock
);
1728 RtlReleaseSRWLockExclusive( lock
);
1730 status
= NtWaitForKeyedEvent( keyed_event
, &variable
->Ptr
, FALSE
, timeout
);
1731 if (status
!= STATUS_SUCCESS
)
1733 if (!interlocked_dec_if_nonzero( (int *)&variable
->Ptr
))
1734 status
= NtWaitForKeyedEvent( keyed_event
, &variable
->Ptr
, FALSE
, NULL
);
1737 if (flags
& RTL_CONDITION_VARIABLE_LOCKMODE_SHARED
)
1738 RtlAcquireSRWLockShared( lock
);
1740 RtlAcquireSRWLockExclusive( lock
);