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>
52 #define WIN32_NO_STATUS
53 #define NONAMELESSUNION
56 #include "wine/server.h"
57 #include "wine/debug.h"
58 #include "ntdll_misc.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
62 HANDLE keyed_event
= NULL
;
64 static inline int interlocked_dec_if_nonzero( int *dest
)
67 for (val
= *dest
;; val
= tmp
)
69 if (!val
|| (tmp
= interlocked_cmpxchg( dest
, val
- 1, val
)) == val
)
75 /* creates a struct security_descriptor and contained information in one contiguous piece of memory */
76 NTSTATUS
NTDLL_create_struct_sd(PSECURITY_DESCRIPTOR nt_sd
, struct security_descriptor
**server_sd
,
77 data_size_t
*server_sd_len
)
82 BOOLEAN owner_present
, group_present
, dacl_present
, sacl_present
;
91 return STATUS_SUCCESS
;
94 len
= sizeof(struct security_descriptor
);
96 status
= RtlGetOwnerSecurityDescriptor(nt_sd
, &owner
, &owner_present
);
97 if (status
!= STATUS_SUCCESS
) return status
;
98 status
= RtlGetGroupSecurityDescriptor(nt_sd
, &group
, &group_present
);
99 if (status
!= STATUS_SUCCESS
) return status
;
100 status
= RtlGetSaclSecurityDescriptor(nt_sd
, &sacl_present
, &sacl
, &defaulted
);
101 if (status
!= STATUS_SUCCESS
) return status
;
102 status
= RtlGetDaclSecurityDescriptor(nt_sd
, &dacl_present
, &dacl
, &defaulted
);
103 if (status
!= STATUS_SUCCESS
) return status
;
106 len
+= RtlLengthSid(owner
);
108 len
+= RtlLengthSid(group
);
109 if (sacl_present
&& sacl
)
110 len
+= sacl
->AclSize
;
111 if (dacl_present
&& dacl
)
112 len
+= dacl
->AclSize
;
114 /* fix alignment for the Unicode name that follows the structure */
115 len
= (len
+ sizeof(WCHAR
) - 1) & ~(sizeof(WCHAR
) - 1);
116 *server_sd
= RtlAllocateHeap(GetProcessHeap(), 0, len
);
117 if (!*server_sd
) return STATUS_NO_MEMORY
;
119 (*server_sd
)->control
= ((SECURITY_DESCRIPTOR
*)nt_sd
)->Control
& ~SE_SELF_RELATIVE
;
120 (*server_sd
)->owner_len
= owner_present
? RtlLengthSid(owner
) : 0;
121 (*server_sd
)->group_len
= group_present
? RtlLengthSid(group
) : 0;
122 (*server_sd
)->sacl_len
= (sacl_present
&& sacl
) ? sacl
->AclSize
: 0;
123 (*server_sd
)->dacl_len
= (dacl_present
&& dacl
) ? dacl
->AclSize
: 0;
125 ptr
= (unsigned char *)(*server_sd
+ 1);
126 memcpy(ptr
, owner
, (*server_sd
)->owner_len
);
127 ptr
+= (*server_sd
)->owner_len
;
128 memcpy(ptr
, group
, (*server_sd
)->group_len
);
129 ptr
+= (*server_sd
)->group_len
;
130 memcpy(ptr
, sacl
, (*server_sd
)->sacl_len
);
131 ptr
+= (*server_sd
)->sacl_len
;
132 memcpy(ptr
, dacl
, (*server_sd
)->dacl_len
);
134 *server_sd_len
= len
;
136 return STATUS_SUCCESS
;
139 /* frees a struct security_descriptor allocated by NTDLL_create_struct_sd */
140 void NTDLL_free_struct_sd(struct security_descriptor
*server_sd
)
142 RtlFreeHeap(GetProcessHeap(), 0, server_sd
);
149 /******************************************************************************
150 * NtCreateSemaphore (NTDLL.@)
152 NTSTATUS WINAPI
NtCreateSemaphore( OUT PHANDLE SemaphoreHandle
,
153 IN ACCESS_MASK access
,
154 IN
const OBJECT_ATTRIBUTES
*attr OPTIONAL
,
155 IN LONG InitialCount
,
156 IN LONG MaximumCount
)
158 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
160 struct object_attributes objattr
;
161 struct security_descriptor
*sd
= NULL
;
163 if (MaximumCount
<= 0 || InitialCount
< 0 || InitialCount
> MaximumCount
)
164 return STATUS_INVALID_PARAMETER
;
165 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
167 objattr
.rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
169 objattr
.name_len
= len
;
172 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
173 if (ret
!= STATUS_SUCCESS
) return ret
;
176 SERVER_START_REQ( create_semaphore
)
178 req
->access
= access
;
179 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
180 req
->initial
= InitialCount
;
181 req
->max
= MaximumCount
;
182 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
183 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
184 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
185 ret
= wine_server_call( req
);
186 *SemaphoreHandle
= wine_server_ptr_handle( reply
->handle
);
190 NTDLL_free_struct_sd( sd
);
195 /******************************************************************************
196 * NtOpenSemaphore (NTDLL.@)
198 NTSTATUS WINAPI
NtOpenSemaphore( OUT PHANDLE SemaphoreHandle
,
199 IN ACCESS_MASK access
,
200 IN
const OBJECT_ATTRIBUTES
*attr
)
202 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
205 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
207 SERVER_START_REQ( open_semaphore
)
209 req
->access
= access
;
210 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
211 req
->rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
212 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
213 ret
= wine_server_call( req
);
214 *SemaphoreHandle
= wine_server_ptr_handle( reply
->handle
);
220 /******************************************************************************
221 * NtQuerySemaphore (NTDLL.@)
223 NTSTATUS WINAPI
NtQuerySemaphore( HANDLE handle
, SEMAPHORE_INFORMATION_CLASS
class,
224 void *info
, ULONG len
, ULONG
*ret_len
)
227 SEMAPHORE_BASIC_INFORMATION
*out
= info
;
229 if (class != SemaphoreBasicInformation
)
231 FIXME("(%p,%d,%u) Unknown class\n", handle
, class, len
);
232 return STATUS_INVALID_INFO_CLASS
;
235 if (len
!= sizeof(SEMAPHORE_BASIC_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
237 SERVER_START_REQ( query_semaphore
)
239 req
->handle
= wine_server_obj_handle( handle
);
240 if (!(ret
= wine_server_call( req
)))
242 out
->CurrentCount
= reply
->current
;
243 out
->MaximumCount
= reply
->max
;
244 if (ret_len
) *ret_len
= sizeof(SEMAPHORE_BASIC_INFORMATION
);
252 /******************************************************************************
253 * NtReleaseSemaphore (NTDLL.@)
255 NTSTATUS WINAPI
NtReleaseSemaphore( HANDLE handle
, ULONG count
, PULONG previous
)
258 SERVER_START_REQ( release_semaphore
)
260 req
->handle
= wine_server_obj_handle( handle
);
262 if (!(ret
= wine_server_call( req
)))
264 if (previous
) *previous
= reply
->prev_count
;
275 /**************************************************************************
276 * NtCreateEvent (NTDLL.@)
277 * ZwCreateEvent (NTDLL.@)
279 NTSTATUS WINAPI
NtCreateEvent( PHANDLE EventHandle
, ACCESS_MASK DesiredAccess
,
280 const OBJECT_ATTRIBUTES
*attr
, EVENT_TYPE type
, BOOLEAN InitialState
)
282 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
284 struct security_descriptor
*sd
= NULL
;
285 struct object_attributes objattr
;
287 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
289 objattr
.rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
291 objattr
.name_len
= len
;
294 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
295 if (ret
!= STATUS_SUCCESS
) return ret
;
298 SERVER_START_REQ( create_event
)
300 req
->access
= DesiredAccess
;
301 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
302 req
->manual_reset
= (type
== NotificationEvent
);
303 req
->initial_state
= InitialState
;
304 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
305 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
306 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
307 ret
= wine_server_call( req
);
308 *EventHandle
= wine_server_ptr_handle( reply
->handle
);
312 NTDLL_free_struct_sd( sd
);
317 /******************************************************************************
318 * NtOpenEvent (NTDLL.@)
319 * ZwOpenEvent (NTDLL.@)
321 NTSTATUS WINAPI
NtOpenEvent(
322 OUT PHANDLE EventHandle
,
323 IN ACCESS_MASK DesiredAccess
,
324 IN
const OBJECT_ATTRIBUTES
*attr
)
326 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
329 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
331 SERVER_START_REQ( open_event
)
333 req
->access
= DesiredAccess
;
334 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
335 req
->rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
336 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
337 ret
= wine_server_call( req
);
338 *EventHandle
= wine_server_ptr_handle( reply
->handle
);
345 /******************************************************************************
346 * NtSetEvent (NTDLL.@)
347 * ZwSetEvent (NTDLL.@)
349 NTSTATUS WINAPI
NtSetEvent( HANDLE handle
, PULONG NumberOfThreadsReleased
)
353 /* FIXME: set NumberOfThreadsReleased */
355 SERVER_START_REQ( event_op
)
357 req
->handle
= wine_server_obj_handle( handle
);
359 ret
= wine_server_call( req
);
365 /******************************************************************************
366 * NtResetEvent (NTDLL.@)
368 NTSTATUS WINAPI
NtResetEvent( HANDLE handle
, PULONG NumberOfThreadsReleased
)
372 /* resetting an event can't release any thread... */
373 if (NumberOfThreadsReleased
) *NumberOfThreadsReleased
= 0;
375 SERVER_START_REQ( event_op
)
377 req
->handle
= wine_server_obj_handle( handle
);
378 req
->op
= RESET_EVENT
;
379 ret
= wine_server_call( req
);
385 /******************************************************************************
386 * NtClearEvent (NTDLL.@)
389 * same as NtResetEvent ???
391 NTSTATUS WINAPI
NtClearEvent ( HANDLE handle
)
393 return NtResetEvent( handle
, NULL
);
396 /******************************************************************************
397 * NtPulseEvent (NTDLL.@)
402 NTSTATUS WINAPI
NtPulseEvent( HANDLE handle
, PULONG PulseCount
)
407 FIXME("(%p,%d)\n", handle
, *PulseCount
);
409 SERVER_START_REQ( event_op
)
411 req
->handle
= wine_server_obj_handle( handle
);
412 req
->op
= PULSE_EVENT
;
413 ret
= wine_server_call( req
);
419 /******************************************************************************
420 * NtQueryEvent (NTDLL.@)
422 NTSTATUS WINAPI
NtQueryEvent( HANDLE handle
, EVENT_INFORMATION_CLASS
class,
423 void *info
, ULONG len
, ULONG
*ret_len
)
426 EVENT_BASIC_INFORMATION
*out
= info
;
428 if (class != EventBasicInformation
)
430 FIXME("(%p, %d, %d) Unknown class\n",
432 return STATUS_INVALID_INFO_CLASS
;
435 if (len
!= sizeof(EVENT_BASIC_INFORMATION
)) return STATUS_INFO_LENGTH_MISMATCH
;
437 SERVER_START_REQ( query_event
)
439 req
->handle
= wine_server_obj_handle( handle
);
440 if (!(ret
= wine_server_call( req
)))
442 out
->EventType
= reply
->manual_reset
? NotificationEvent
: SynchronizationEvent
;
443 out
->EventState
= reply
->state
;
444 if (ret_len
) *ret_len
= sizeof(EVENT_BASIC_INFORMATION
);
453 * Mutants (known as Mutexes in Kernel32)
456 /******************************************************************************
457 * NtCreateMutant [NTDLL.@]
458 * ZwCreateMutant [NTDLL.@]
460 NTSTATUS WINAPI
NtCreateMutant(OUT HANDLE
* MutantHandle
,
461 IN ACCESS_MASK access
,
462 IN
const OBJECT_ATTRIBUTES
* attr OPTIONAL
,
463 IN BOOLEAN InitialOwner
)
466 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
467 struct security_descriptor
*sd
= NULL
;
468 struct object_attributes objattr
;
470 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
472 objattr
.rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
474 objattr
.name_len
= len
;
477 status
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
478 if (status
!= STATUS_SUCCESS
) return status
;
481 SERVER_START_REQ( create_mutex
)
483 req
->access
= access
;
484 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
485 req
->owned
= InitialOwner
;
486 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
487 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
488 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
489 status
= wine_server_call( req
);
490 *MutantHandle
= wine_server_ptr_handle( reply
->handle
);
494 NTDLL_free_struct_sd( sd
);
499 /**************************************************************************
500 * NtOpenMutant [NTDLL.@]
501 * ZwOpenMutant [NTDLL.@]
503 NTSTATUS WINAPI
NtOpenMutant(OUT HANDLE
* MutantHandle
,
504 IN ACCESS_MASK access
,
505 IN
const OBJECT_ATTRIBUTES
* attr
)
508 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
510 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
512 SERVER_START_REQ( open_mutex
)
514 req
->access
= access
;
515 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
516 req
->rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
517 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
518 status
= wine_server_call( req
);
519 *MutantHandle
= wine_server_ptr_handle( reply
->handle
);
525 /**************************************************************************
526 * NtReleaseMutant [NTDLL.@]
527 * ZwReleaseMutant [NTDLL.@]
529 NTSTATUS WINAPI
NtReleaseMutant( IN HANDLE handle
, OUT PLONG prev_count OPTIONAL
)
533 SERVER_START_REQ( release_mutex
)
535 req
->handle
= wine_server_obj_handle( handle
);
536 status
= wine_server_call( req
);
537 if (prev_count
) *prev_count
= reply
->prev_count
;
543 /******************************************************************
544 * NtQueryMutant [NTDLL.@]
545 * ZwQueryMutant [NTDLL.@]
547 NTSTATUS WINAPI
NtQueryMutant(IN HANDLE handle
,
548 IN MUTANT_INFORMATION_CLASS MutantInformationClass
,
549 OUT PVOID MutantInformation
,
550 IN ULONG MutantInformationLength
,
551 OUT PULONG ResultLength OPTIONAL
)
553 FIXME("(%p %u %p %u %p): stub!\n",
554 handle
, MutantInformationClass
, MutantInformation
, MutantInformationLength
, ResultLength
);
555 return STATUS_NOT_IMPLEMENTED
;
562 /******************************************************************************
563 * NtCreateJobObject [NTDLL.@]
564 * ZwCreateJobObject [NTDLL.@]
566 NTSTATUS WINAPI
NtCreateJobObject( PHANDLE handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
568 FIXME( "stub: %p %x %s\n", handle
, access
, attr
? debugstr_us(attr
->ObjectName
) : "" );
569 *handle
= (HANDLE
)0xdead;
570 return STATUS_SUCCESS
;
573 /******************************************************************************
574 * NtOpenJobObject [NTDLL.@]
575 * ZwOpenJobObject [NTDLL.@]
577 NTSTATUS WINAPI
NtOpenJobObject( PHANDLE handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
579 FIXME( "stub: %p %x %s\n", handle
, access
, attr
? debugstr_us(attr
->ObjectName
) : "" );
580 return STATUS_NOT_IMPLEMENTED
;
583 /******************************************************************************
584 * NtTerminateJobObject [NTDLL.@]
585 * ZwTerminateJobObject [NTDLL.@]
587 NTSTATUS WINAPI
NtTerminateJobObject( HANDLE handle
, NTSTATUS status
)
589 FIXME( "stub: %p %x\n", handle
, status
);
590 return STATUS_SUCCESS
;
593 /******************************************************************************
594 * NtQueryInformationJobObject [NTDLL.@]
595 * ZwQueryInformationJobObject [NTDLL.@]
597 NTSTATUS WINAPI
NtQueryInformationJobObject( HANDLE handle
, JOBOBJECTINFOCLASS
class, PVOID info
,
598 ULONG len
, PULONG ret_len
)
600 FIXME( "stub: %p %u %p %u %p\n", handle
, class, info
, len
, ret_len
);
601 return STATUS_NOT_IMPLEMENTED
;
604 /******************************************************************************
605 * NtSetInformationJobObject [NTDLL.@]
606 * ZwSetInformationJobObject [NTDLL.@]
608 NTSTATUS WINAPI
NtSetInformationJobObject( HANDLE handle
, JOBOBJECTINFOCLASS
class, PVOID info
, ULONG len
)
610 FIXME( "stub: %p %u %p %u\n", handle
, class, info
, len
);
611 return STATUS_SUCCESS
;
614 /******************************************************************************
615 * NtIsProcessInJob [NTDLL.@]
616 * ZwIsProcessInJob [NTDLL.@]
618 NTSTATUS WINAPI
NtIsProcessInJob( HANDLE process
, HANDLE job
)
620 FIXME( "stub: %p %p\n", process
, job
);
621 return STATUS_PROCESS_NOT_IN_JOB
;
624 /******************************************************************************
625 * NtAssignProcessToJobObject [NTDLL.@]
626 * ZwAssignProcessToJobObject [NTDLL.@]
628 NTSTATUS WINAPI
NtAssignProcessToJobObject( HANDLE job
, HANDLE process
)
630 FIXME( "stub: %p %p\n", job
, process
);
631 return STATUS_SUCCESS
;
638 /**************************************************************************
639 * NtCreateTimer [NTDLL.@]
640 * ZwCreateTimer [NTDLL.@]
642 NTSTATUS WINAPI
NtCreateTimer(OUT HANDLE
*handle
,
643 IN ACCESS_MASK access
,
644 IN
const OBJECT_ATTRIBUTES
*attr OPTIONAL
,
645 IN TIMER_TYPE timer_type
)
647 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
650 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
652 if (timer_type
!= NotificationTimer
&& timer_type
!= SynchronizationTimer
)
653 return STATUS_INVALID_PARAMETER
;
655 SERVER_START_REQ( create_timer
)
657 req
->access
= access
;
658 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
659 req
->rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
660 req
->manual
= (timer_type
== NotificationTimer
);
661 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
662 status
= wine_server_call( req
);
663 *handle
= wine_server_ptr_handle( reply
->handle
);
670 /**************************************************************************
671 * NtOpenTimer [NTDLL.@]
672 * ZwOpenTimer [NTDLL.@]
674 NTSTATUS WINAPI
NtOpenTimer(OUT PHANDLE handle
,
675 IN ACCESS_MASK access
,
676 IN
const OBJECT_ATTRIBUTES
* attr
)
678 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
681 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
683 SERVER_START_REQ( open_timer
)
685 req
->access
= access
;
686 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
687 req
->rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
688 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
689 status
= wine_server_call( req
);
690 *handle
= wine_server_ptr_handle( reply
->handle
);
696 /**************************************************************************
697 * NtSetTimer [NTDLL.@]
698 * ZwSetTimer [NTDLL.@]
700 NTSTATUS WINAPI
NtSetTimer(IN HANDLE handle
,
701 IN
const LARGE_INTEGER
* when
,
702 IN PTIMER_APC_ROUTINE callback
,
703 IN PVOID callback_arg
,
705 IN ULONG period OPTIONAL
,
706 OUT PBOOLEAN state OPTIONAL
)
708 NTSTATUS status
= STATUS_SUCCESS
;
710 TRACE("(%p,%p,%p,%p,%08x,0x%08x,%p) stub\n",
711 handle
, when
, callback
, callback_arg
, resume
, period
, state
);
713 SERVER_START_REQ( set_timer
)
715 req
->handle
= wine_server_obj_handle( handle
);
716 req
->period
= period
;
717 req
->expire
= when
->QuadPart
;
718 req
->callback
= wine_server_client_ptr( callback
);
719 req
->arg
= wine_server_client_ptr( callback_arg
);
720 status
= wine_server_call( req
);
721 if (state
) *state
= reply
->signaled
;
725 /* set error but can still succeed */
726 if (resume
&& status
== STATUS_SUCCESS
) return STATUS_TIMER_RESUME_IGNORED
;
730 /**************************************************************************
731 * NtCancelTimer [NTDLL.@]
732 * ZwCancelTimer [NTDLL.@]
734 NTSTATUS WINAPI
NtCancelTimer(IN HANDLE handle
, OUT BOOLEAN
* state
)
738 SERVER_START_REQ( cancel_timer
)
740 req
->handle
= wine_server_obj_handle( handle
);
741 status
= wine_server_call( req
);
742 if (state
) *state
= reply
->signaled
;
748 /******************************************************************************
749 * NtQueryTimer (NTDLL.@)
751 * Retrieves information about a timer.
754 * TimerHandle [I] The timer to retrieve information about.
755 * TimerInformationClass [I] The type of information to retrieve.
756 * TimerInformation [O] Pointer to buffer to store information in.
757 * Length [I] The length of the buffer pointed to by TimerInformation.
758 * ReturnLength [O] Optional. The size of buffer actually used.
761 * Success: STATUS_SUCCESS
762 * Failure: STATUS_INFO_LENGTH_MISMATCH, if Length doesn't match the required data
763 * size for the class specified.
764 * STATUS_INVALID_INFO_CLASS, if an invalid TimerInformationClass was specified.
765 * STATUS_ACCESS_DENIED, if TimerHandle does not have TIMER_QUERY_STATE access
768 NTSTATUS WINAPI
NtQueryTimer(
770 TIMER_INFORMATION_CLASS TimerInformationClass
,
771 PVOID TimerInformation
,
775 TIMER_BASIC_INFORMATION
* basic_info
= TimerInformation
;
779 TRACE("(%p,%d,%p,0x%08x,%p)\n", TimerHandle
, TimerInformationClass
,
780 TimerInformation
, Length
, ReturnLength
);
782 switch (TimerInformationClass
)
784 case TimerBasicInformation
:
785 if (Length
< sizeof(TIMER_BASIC_INFORMATION
))
786 return STATUS_INFO_LENGTH_MISMATCH
;
788 SERVER_START_REQ(get_timer_info
)
790 req
->handle
= wine_server_obj_handle( TimerHandle
);
791 status
= wine_server_call(req
);
793 /* convert server time to absolute NTDLL time */
794 basic_info
->RemainingTime
.QuadPart
= reply
->when
;
795 basic_info
->TimerState
= reply
->signaled
;
799 /* convert from absolute into relative time */
800 NtQuerySystemTime(&now
);
801 if (now
.QuadPart
> basic_info
->RemainingTime
.QuadPart
)
802 basic_info
->RemainingTime
.QuadPart
= 0;
804 basic_info
->RemainingTime
.QuadPart
-= now
.QuadPart
;
806 if (ReturnLength
) *ReturnLength
= sizeof(TIMER_BASIC_INFORMATION
);
811 FIXME("Unhandled class %d\n", TimerInformationClass
);
812 return STATUS_INVALID_INFO_CLASS
;
816 /******************************************************************************
817 * NtQueryTimerResolution [NTDLL.@]
819 NTSTATUS WINAPI
NtQueryTimerResolution(OUT ULONG
* min_resolution
,
820 OUT ULONG
* max_resolution
,
821 OUT ULONG
* current_resolution
)
823 FIXME("(%p,%p,%p), stub!\n",
824 min_resolution
, max_resolution
, current_resolution
);
826 return STATUS_NOT_IMPLEMENTED
;
829 /******************************************************************************
830 * NtSetTimerResolution [NTDLL.@]
832 NTSTATUS WINAPI
NtSetTimerResolution(IN ULONG resolution
,
833 IN BOOLEAN set_resolution
,
834 OUT ULONG
* current_resolution
)
836 FIXME("(%u,%u,%p), stub!\n",
837 resolution
, set_resolution
, current_resolution
);
839 return STATUS_NOT_IMPLEMENTED
;
844 /* wait operations */
846 /******************************************************************
847 * NtWaitForMultipleObjects (NTDLL.@)
849 NTSTATUS WINAPI
NtWaitForMultipleObjects( DWORD count
, const HANDLE
*handles
,
850 BOOLEAN wait_any
, BOOLEAN alertable
,
851 const LARGE_INTEGER
*timeout
)
853 select_op_t select_op
;
854 UINT i
, flags
= SELECT_INTERRUPTIBLE
;
856 if (!count
|| count
> MAXIMUM_WAIT_OBJECTS
) return STATUS_INVALID_PARAMETER_1
;
858 if (alertable
) flags
|= SELECT_ALERTABLE
;
859 select_op
.wait
.op
= wait_any
? SELECT_WAIT
: SELECT_WAIT_ALL
;
860 for (i
= 0; i
< count
; i
++) select_op
.wait
.handles
[i
] = wine_server_obj_handle( handles
[i
] );
861 return server_select( &select_op
, offsetof( select_op_t
, wait
.handles
[count
] ), flags
, timeout
);
865 /******************************************************************
866 * NtWaitForSingleObject (NTDLL.@)
868 NTSTATUS WINAPI
NtWaitForSingleObject(HANDLE handle
, BOOLEAN alertable
, const LARGE_INTEGER
*timeout
)
870 return NtWaitForMultipleObjects( 1, &handle
, FALSE
, alertable
, timeout
);
874 /******************************************************************
875 * NtSignalAndWaitForSingleObject (NTDLL.@)
877 NTSTATUS WINAPI
NtSignalAndWaitForSingleObject( HANDLE hSignalObject
, HANDLE hWaitObject
,
878 BOOLEAN alertable
, const LARGE_INTEGER
*timeout
)
880 select_op_t select_op
;
881 UINT flags
= SELECT_INTERRUPTIBLE
;
883 if (!hSignalObject
) return STATUS_INVALID_HANDLE
;
885 if (alertable
) flags
|= SELECT_ALERTABLE
;
886 select_op
.signal_and_wait
.op
= SELECT_SIGNAL_AND_WAIT
;
887 select_op
.signal_and_wait
.wait
= wine_server_obj_handle( hWaitObject
);
888 select_op
.signal_and_wait
.signal
= wine_server_obj_handle( hSignalObject
);
889 return server_select( &select_op
, sizeof(select_op
.signal_and_wait
), flags
, timeout
);
893 /******************************************************************
894 * NtYieldExecution (NTDLL.@)
896 NTSTATUS WINAPI
NtYieldExecution(void)
898 #ifdef HAVE_SCHED_YIELD
900 return STATUS_SUCCESS
;
902 return STATUS_NO_YIELD_PERFORMED
;
907 /******************************************************************
908 * NtDelayExecution (NTDLL.@)
910 NTSTATUS WINAPI
NtDelayExecution( BOOLEAN alertable
, const LARGE_INTEGER
*timeout
)
912 /* if alertable, we need to query the server */
914 return server_select( NULL
, 0, SELECT_INTERRUPTIBLE
| SELECT_ALERTABLE
, timeout
);
916 if (!timeout
|| timeout
->QuadPart
== TIMEOUT_INFINITE
) /* sleep forever */
918 for (;;) select( 0, NULL
, NULL
, NULL
, NULL
);
923 timeout_t when
, diff
;
925 if ((when
= timeout
->QuadPart
) < 0)
927 NtQuerySystemTime( &now
);
928 when
= now
.QuadPart
- when
;
931 /* Note that we yield after establishing the desired timeout */
933 if (!when
) return STATUS_SUCCESS
;
938 NtQuerySystemTime( &now
);
939 diff
= (when
- now
.QuadPart
+ 9) / 10;
940 if (diff
<= 0) break;
941 tv
.tv_sec
= diff
/ 1000000;
942 tv
.tv_usec
= diff
% 1000000;
943 if (select( 0, NULL
, NULL
, NULL
, &tv
) != -1) break;
946 return STATUS_SUCCESS
;
950 /******************************************************************************
951 * NtCreateKeyedEvent (NTDLL.@)
953 NTSTATUS WINAPI
NtCreateKeyedEvent( HANDLE
*handle
, ACCESS_MASK access
,
954 const OBJECT_ATTRIBUTES
*attr
, ULONG flags
)
956 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
958 struct security_descriptor
*sd
= NULL
;
959 struct object_attributes objattr
;
961 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
963 objattr
.rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
965 objattr
.name_len
= len
;
968 ret
= NTDLL_create_struct_sd( attr
->SecurityDescriptor
, &sd
, &objattr
.sd_len
);
969 if (ret
!= STATUS_SUCCESS
) return ret
;
972 SERVER_START_REQ( create_keyed_event
)
974 req
->access
= access
;
975 req
->attributes
= attr
? attr
->Attributes
: 0;
976 wine_server_add_data( req
, &objattr
, sizeof(objattr
) );
977 if (objattr
.sd_len
) wine_server_add_data( req
, sd
, objattr
.sd_len
);
978 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
979 ret
= wine_server_call( req
);
980 *handle
= wine_server_ptr_handle( reply
->handle
);
984 NTDLL_free_struct_sd( sd
);
988 /******************************************************************************
989 * NtOpenKeyedEvent (NTDLL.@)
991 NTSTATUS WINAPI
NtOpenKeyedEvent( HANDLE
*handle
, ACCESS_MASK access
, const OBJECT_ATTRIBUTES
*attr
)
993 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
996 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
998 SERVER_START_REQ( open_keyed_event
)
1000 req
->access
= access
;
1001 req
->attributes
= attr
? attr
->Attributes
: 0;
1002 req
->rootdir
= wine_server_obj_handle( attr
? attr
->RootDirectory
: 0 );
1003 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
1004 ret
= wine_server_call( req
);
1005 *handle
= wine_server_ptr_handle( reply
->handle
);
1011 /******************************************************************************
1012 * NtWaitForKeyedEvent (NTDLL.@)
1014 NTSTATUS WINAPI
NtWaitForKeyedEvent( HANDLE handle
, const void *key
,
1015 BOOLEAN alertable
, const LARGE_INTEGER
*timeout
)
1017 select_op_t select_op
;
1018 UINT flags
= SELECT_INTERRUPTIBLE
;
1020 if ((ULONG_PTR
)key
& 1) return STATUS_INVALID_PARAMETER_1
;
1021 if (alertable
) flags
|= SELECT_ALERTABLE
;
1022 select_op
.keyed_event
.op
= SELECT_KEYED_EVENT_WAIT
;
1023 select_op
.keyed_event
.handle
= wine_server_obj_handle( handle
);
1024 select_op
.keyed_event
.key
= wine_server_client_ptr( key
);
1025 return server_select( &select_op
, sizeof(select_op
.keyed_event
), flags
, timeout
);
1028 /******************************************************************************
1029 * NtReleaseKeyedEvent (NTDLL.@)
1031 NTSTATUS WINAPI
NtReleaseKeyedEvent( HANDLE handle
, const void *key
,
1032 BOOLEAN alertable
, const LARGE_INTEGER
*timeout
)
1034 select_op_t select_op
;
1035 UINT flags
= SELECT_INTERRUPTIBLE
;
1037 if ((ULONG_PTR
)key
& 1) return STATUS_INVALID_PARAMETER_1
;
1038 if (alertable
) flags
|= SELECT_ALERTABLE
;
1039 select_op
.keyed_event
.op
= SELECT_KEYED_EVENT_RELEASE
;
1040 select_op
.keyed_event
.handle
= wine_server_obj_handle( handle
);
1041 select_op
.keyed_event
.key
= wine_server_client_ptr( key
);
1042 return server_select( &select_op
, sizeof(select_op
.keyed_event
), flags
, timeout
);
1045 /******************************************************************
1046 * NtCreateIoCompletion (NTDLL.@)
1047 * ZwCreateIoCompletion (NTDLL.@)
1049 * Creates I/O completion object.
1052 * CompletionPort [O] created completion object handle will be placed there
1053 * DesiredAccess [I] desired access to a handle (combination of IO_COMPLETION_*)
1054 * ObjectAttributes [I] completion object attributes
1055 * NumberOfConcurrentThreads [I] desired number of concurrent active worker threads
1058 NTSTATUS WINAPI
NtCreateIoCompletion( PHANDLE CompletionPort
, ACCESS_MASK DesiredAccess
,
1059 POBJECT_ATTRIBUTES ObjectAttributes
, ULONG NumberOfConcurrentThreads
)
1063 TRACE("(%p, %x, %p, %d)\n", CompletionPort
, DesiredAccess
,
1064 ObjectAttributes
, NumberOfConcurrentThreads
);
1066 if (!CompletionPort
)
1067 return STATUS_INVALID_PARAMETER
;
1069 SERVER_START_REQ( create_completion
)
1071 req
->access
= DesiredAccess
;
1072 req
->attributes
= ObjectAttributes
? ObjectAttributes
->Attributes
: 0;
1073 req
->rootdir
= wine_server_obj_handle( ObjectAttributes
? ObjectAttributes
->RootDirectory
: 0 );
1074 req
->concurrent
= NumberOfConcurrentThreads
;
1075 if (ObjectAttributes
&& ObjectAttributes
->ObjectName
)
1076 wine_server_add_data( req
, ObjectAttributes
->ObjectName
->Buffer
,
1077 ObjectAttributes
->ObjectName
->Length
);
1078 if (!(status
= wine_server_call( req
)))
1079 *CompletionPort
= wine_server_ptr_handle( reply
->handle
);
1085 /******************************************************************
1086 * NtSetIoCompletion (NTDLL.@)
1087 * ZwSetIoCompletion (NTDLL.@)
1089 * Inserts completion message into queue
1092 * CompletionPort [I] HANDLE to completion object
1093 * CompletionKey [I] completion key
1094 * CompletionValue [I] completion value (usually pointer to OVERLAPPED)
1095 * Status [I] operation status
1096 * NumberOfBytesTransferred [I] number of bytes transferred
1098 NTSTATUS WINAPI
NtSetIoCompletion( HANDLE CompletionPort
, ULONG_PTR CompletionKey
,
1099 ULONG_PTR CompletionValue
, NTSTATUS Status
,
1100 SIZE_T NumberOfBytesTransferred
)
1104 TRACE("(%p, %lx, %lx, %x, %lx)\n", CompletionPort
, CompletionKey
,
1105 CompletionValue
, Status
, NumberOfBytesTransferred
);
1107 SERVER_START_REQ( add_completion
)
1109 req
->handle
= wine_server_obj_handle( CompletionPort
);
1110 req
->ckey
= CompletionKey
;
1111 req
->cvalue
= CompletionValue
;
1112 req
->status
= Status
;
1113 req
->information
= NumberOfBytesTransferred
;
1114 status
= wine_server_call( req
);
1120 /******************************************************************
1121 * NtRemoveIoCompletion (NTDLL.@)
1122 * ZwRemoveIoCompletion (NTDLL.@)
1124 * (Wait for and) retrieve first completion message from completion object's queue
1127 * CompletionPort [I] HANDLE to I/O completion object
1128 * CompletionKey [O] completion key
1129 * CompletionValue [O] Completion value given in NtSetIoCompletion or in async operation
1130 * iosb [O] IO_STATUS_BLOCK of completed asynchronous operation
1131 * WaitTime [I] optional wait time in NTDLL format
1134 NTSTATUS WINAPI
NtRemoveIoCompletion( HANDLE CompletionPort
, PULONG_PTR CompletionKey
,
1135 PULONG_PTR CompletionValue
, PIO_STATUS_BLOCK iosb
,
1136 PLARGE_INTEGER WaitTime
)
1140 TRACE("(%p, %p, %p, %p, %p)\n", CompletionPort
, CompletionKey
,
1141 CompletionValue
, iosb
, WaitTime
);
1145 SERVER_START_REQ( remove_completion
)
1147 req
->handle
= wine_server_obj_handle( CompletionPort
);
1148 if (!(status
= wine_server_call( req
)))
1150 *CompletionKey
= reply
->ckey
;
1151 *CompletionValue
= reply
->cvalue
;
1152 iosb
->Information
= reply
->information
;
1153 iosb
->u
.Status
= reply
->status
;
1157 if (status
!= STATUS_PENDING
) break;
1159 status
= NtWaitForSingleObject( CompletionPort
, FALSE
, WaitTime
);
1160 if (status
!= WAIT_OBJECT_0
) break;
1165 /******************************************************************
1166 * NtOpenIoCompletion (NTDLL.@)
1167 * ZwOpenIoCompletion (NTDLL.@)
1169 * Opens I/O completion object
1172 * CompletionPort [O] completion object handle will be placed there
1173 * DesiredAccess [I] desired access to a handle (combination of IO_COMPLETION_*)
1174 * ObjectAttributes [I] completion object name
1177 NTSTATUS WINAPI
NtOpenIoCompletion( PHANDLE CompletionPort
, ACCESS_MASK DesiredAccess
,
1178 POBJECT_ATTRIBUTES ObjectAttributes
)
1182 TRACE("(%p, 0x%x, %p)\n", CompletionPort
, DesiredAccess
, ObjectAttributes
);
1184 if (!CompletionPort
|| !ObjectAttributes
|| !ObjectAttributes
->ObjectName
)
1185 return STATUS_INVALID_PARAMETER
;
1187 SERVER_START_REQ( open_completion
)
1189 req
->access
= DesiredAccess
;
1190 req
->rootdir
= wine_server_obj_handle( ObjectAttributes
->RootDirectory
);
1191 wine_server_add_data( req
, ObjectAttributes
->ObjectName
->Buffer
,
1192 ObjectAttributes
->ObjectName
->Length
);
1193 if (!(status
= wine_server_call( req
)))
1194 *CompletionPort
= wine_server_ptr_handle( reply
->handle
);
1200 /******************************************************************
1201 * NtQueryIoCompletion (NTDLL.@)
1202 * ZwQueryIoCompletion (NTDLL.@)
1204 * Requests information about given I/O completion object
1207 * CompletionPort [I] HANDLE to completion port to request
1208 * InformationClass [I] information class
1209 * CompletionInformation [O] user-provided buffer for data
1210 * BufferLength [I] buffer length
1211 * RequiredLength [O] required buffer length
1214 NTSTATUS WINAPI
NtQueryIoCompletion( HANDLE CompletionPort
, IO_COMPLETION_INFORMATION_CLASS InformationClass
,
1215 PVOID CompletionInformation
, ULONG BufferLength
, PULONG RequiredLength
)
1219 TRACE("(%p, %d, %p, 0x%x, %p)\n", CompletionPort
, InformationClass
, CompletionInformation
,
1220 BufferLength
, RequiredLength
);
1222 if (!CompletionInformation
) return STATUS_INVALID_PARAMETER
;
1223 switch( InformationClass
)
1225 case IoCompletionBasicInformation
:
1227 ULONG
*info
= CompletionInformation
;
1229 if (RequiredLength
) *RequiredLength
= sizeof(*info
);
1230 if (BufferLength
!= sizeof(*info
))
1231 status
= STATUS_INFO_LENGTH_MISMATCH
;
1234 SERVER_START_REQ( query_completion
)
1236 req
->handle
= wine_server_obj_handle( CompletionPort
);
1237 if (!(status
= wine_server_call( req
)))
1238 *info
= reply
->depth
;
1245 status
= STATUS_INVALID_PARAMETER
;
1251 NTSTATUS
NTDLL_AddCompletion( HANDLE hFile
, ULONG_PTR CompletionValue
,
1252 NTSTATUS CompletionStatus
, ULONG Information
)
1256 SERVER_START_REQ( add_fd_completion
)
1258 req
->handle
= wine_server_obj_handle( hFile
);
1259 req
->cvalue
= CompletionValue
;
1260 req
->status
= CompletionStatus
;
1261 req
->information
= Information
;
1262 status
= wine_server_call( req
);
1268 /******************************************************************
1269 * RtlRunOnceInitialize (NTDLL.@)
1271 void WINAPI
RtlRunOnceInitialize( RTL_RUN_ONCE
*once
)
1276 /******************************************************************
1277 * RtlRunOnceBeginInitialize (NTDLL.@)
1279 DWORD WINAPI
RtlRunOnceBeginInitialize( RTL_RUN_ONCE
*once
, ULONG flags
, void **context
)
1281 if (flags
& RTL_RUN_ONCE_CHECK_ONLY
)
1283 ULONG_PTR val
= (ULONG_PTR
)once
->Ptr
;
1285 if (flags
& RTL_RUN_ONCE_ASYNC
) return STATUS_INVALID_PARAMETER
;
1286 if ((val
& 3) != 2) return STATUS_UNSUCCESSFUL
;
1287 if (context
) *context
= (void *)(val
& ~3);
1288 return STATUS_SUCCESS
;
1293 ULONG_PTR next
, val
= (ULONG_PTR
)once
->Ptr
;
1297 case 0: /* first time */
1298 if (!interlocked_cmpxchg_ptr( &once
->Ptr
,
1299 (flags
& RTL_RUN_ONCE_ASYNC
) ? (void *)3 : (void *)1, 0 ))
1300 return STATUS_PENDING
;
1303 case 1: /* in progress, wait */
1304 if (flags
& RTL_RUN_ONCE_ASYNC
) return STATUS_INVALID_PARAMETER
;
1306 if (interlocked_cmpxchg_ptr( &once
->Ptr
, (void *)((ULONG_PTR
)&next
| 1),
1307 (void *)val
) == (void *)val
)
1308 NtWaitForKeyedEvent( keyed_event
, &next
, FALSE
, NULL
);
1312 if (context
) *context
= (void *)(val
& ~3);
1313 return STATUS_SUCCESS
;
1315 case 3: /* in progress, async */
1316 if (!(flags
& RTL_RUN_ONCE_ASYNC
)) return STATUS_INVALID_PARAMETER
;
1317 return STATUS_PENDING
;
1322 /******************************************************************
1323 * RtlRunOnceComplete (NTDLL.@)
1325 DWORD WINAPI
RtlRunOnceComplete( RTL_RUN_ONCE
*once
, ULONG flags
, void *context
)
1327 if ((ULONG_PTR
)context
& 3) return STATUS_INVALID_PARAMETER
;
1329 if (flags
& RTL_RUN_ONCE_INIT_FAILED
)
1331 if (context
) return STATUS_INVALID_PARAMETER
;
1332 if (flags
& RTL_RUN_ONCE_ASYNC
) return STATUS_INVALID_PARAMETER
;
1334 else context
= (void *)((ULONG_PTR
)context
| 2);
1338 ULONG_PTR val
= (ULONG_PTR
)once
->Ptr
;
1342 case 1: /* in progress */
1343 if (interlocked_cmpxchg_ptr( &once
->Ptr
, context
, (void *)val
) != (void *)val
) break;
1347 ULONG_PTR next
= *(ULONG_PTR
*)val
;
1348 NtReleaseKeyedEvent( keyed_event
, (void *)val
, FALSE
, NULL
);
1351 return STATUS_SUCCESS
;
1353 case 3: /* in progress, async */
1354 if (!(flags
& RTL_RUN_ONCE_ASYNC
)) return STATUS_INVALID_PARAMETER
;
1355 if (interlocked_cmpxchg_ptr( &once
->Ptr
, context
, (void *)val
) != (void *)val
) break;
1356 return STATUS_SUCCESS
;
1359 return STATUS_UNSUCCESSFUL
;
1364 /******************************************************************
1365 * RtlRunOnceExecuteOnce (NTDLL.@)
1367 DWORD WINAPI
RtlRunOnceExecuteOnce( RTL_RUN_ONCE
*once
, PRTL_RUN_ONCE_INIT_FN func
,
1368 void *param
, void **context
)
1370 DWORD ret
= RtlRunOnceBeginInitialize( once
, 0, context
);
1372 if (ret
!= STATUS_PENDING
) return ret
;
1374 if (!func( once
, param
, context
))
1376 RtlRunOnceComplete( once
, RTL_RUN_ONCE_INIT_FAILED
, NULL
);
1377 return STATUS_UNSUCCESSFUL
;
1380 return RtlRunOnceComplete( once
, 0, context
? *context
: NULL
);
1384 /* SRW locks implementation
1386 * The memory layout used by the lock is:
1389 * ________________ ________________
1390 * | X| #exclusive | #shared |
1391 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
1392 * Since there is no space left for a separate counter of shared access
1393 * threads inside the locked section the #shared field is used for multiple
1394 * purposes. The following table lists all possible states the lock can be
1395 * in, notation: [X, #exclusive, #shared]:
1397 * [0, 0, N] -> locked by N shared access threads, if N=0 its unlocked
1398 * [0, >=1, >=1] -> threads are requesting exclusive locks, but there are
1399 * still shared access threads inside. #shared should not be incremented
1401 * [1, >=1, >=0] -> lock is owned by an exclusive thread and the #shared
1402 * counter can be used again to count the number of threads waiting in the
1403 * queue for shared access.
1405 * the following states are invalid and will never occur:
1406 * [0, >=1, 0], [1, 0, >=0]
1408 * The main problem arising from the fact that we have no separate counter
1409 * of shared access threads inside the locked section is that in the state
1410 * [0, >=1, >=1] above we cannot add additional waiting threads to the
1411 * shared access queue - it wouldn't be possible to distinguish waiting
1412 * threads and those that are still inside. To solve this problem the lock
1413 * uses the following approach: a thread that isn't able to allocate a
1414 * shared lock just uses the exclusive queue instead. As soon as the thread
1415 * is woken up it is in the state [1, >=1, >=0]. In this state it's again
1416 * possible to use the shared access queue. The thread atomically moves
1417 * itself to the shared access queue and releases the exclusive lock, so
1418 * that the "real" exclusive access threads have a chance. As soon as they
1419 * are all ready the shared access threads are processed.
1422 #define SRWLOCK_MASK_IN_EXCLUSIVE 0x80000000
1423 #define SRWLOCK_MASK_EXCLUSIVE_QUEUE 0x7fff0000
1424 #define SRWLOCK_MASK_SHARED_QUEUE 0x0000ffff
1425 #define SRWLOCK_RES_EXCLUSIVE 0x00010000
1426 #define SRWLOCK_RES_SHARED 0x00000001
1428 #ifdef WORDS_BIGENDIAN
1429 #define srwlock_key_exclusive(lock) (&lock->Ptr)
1430 #define srwlock_key_shared(lock) ((void *)((char *)&lock->Ptr + 2))
1432 #define srwlock_key_exclusive(lock) ((void *)((char *)&lock->Ptr + 2))
1433 #define srwlock_key_shared(lock) (&lock->Ptr)
1436 static inline void srwlock_check_invalid( unsigned int val
)
1438 /* Throw exception if it's impossible to acquire/release this lock. */
1439 if ((val
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
) == SRWLOCK_MASK_EXCLUSIVE_QUEUE
||
1440 (val
& SRWLOCK_MASK_SHARED_QUEUE
) == SRWLOCK_MASK_SHARED_QUEUE
)
1441 RtlRaiseStatus(STATUS_RESOURCE_NOT_OWNED
);
1444 static inline unsigned int srwlock_lock_exclusive( unsigned int *dest
, int incr
)
1446 unsigned int val
, tmp
;
1447 /* Atomically modifies the value of *dest by adding incr. If the shared
1448 * queue is empty and there are threads waiting for exclusive access, then
1449 * sets the mark SRWLOCK_MASK_IN_EXCLUSIVE to signal other threads that
1450 * they are allowed again to use the shared queue counter. */
1451 for (val
= *dest
;; val
= tmp
)
1454 srwlock_check_invalid( tmp
);
1455 if ((tmp
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
) && !(tmp
& SRWLOCK_MASK_SHARED_QUEUE
))
1456 tmp
|= SRWLOCK_MASK_IN_EXCLUSIVE
;
1457 if ((tmp
= interlocked_cmpxchg( (int *)dest
, tmp
, val
)) == val
)
1463 static inline unsigned int srwlock_unlock_exclusive( unsigned int *dest
, int incr
)
1465 unsigned int val
, tmp
;
1466 /* Atomically modifies the value of *dest by adding incr. If the queue of
1467 * threads waiting for exclusive access is empty, then remove the
1468 * SRWLOCK_MASK_IN_EXCLUSIVE flag (only the shared queue counter will
1470 for (val
= *dest
;; val
= tmp
)
1473 srwlock_check_invalid( tmp
);
1474 if (!(tmp
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
))
1475 tmp
&= SRWLOCK_MASK_SHARED_QUEUE
;
1476 if ((tmp
= interlocked_cmpxchg( (int *)dest
, tmp
, val
)) == val
)
1482 static inline void srwlock_leave_exclusive( RTL_SRWLOCK
*lock
, unsigned int val
)
1484 /* Used when a thread leaves an exclusive section. If there are other
1485 * exclusive access threads they are processed first, followed by
1486 * the shared waiters. */
1487 if (val
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
)
1488 NtReleaseKeyedEvent( keyed_event
, srwlock_key_exclusive(lock
), FALSE
, NULL
);
1491 val
&= SRWLOCK_MASK_SHARED_QUEUE
; /* remove SRWLOCK_MASK_IN_EXCLUSIVE */
1493 NtReleaseKeyedEvent( keyed_event
, srwlock_key_shared(lock
), FALSE
, NULL
);
1497 static inline void srwlock_leave_shared( RTL_SRWLOCK
*lock
, unsigned int val
)
1499 /* Wake up one exclusive thread as soon as the last shared access thread
1501 if ((val
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
) && !(val
& SRWLOCK_MASK_SHARED_QUEUE
))
1502 NtReleaseKeyedEvent( keyed_event
, srwlock_key_exclusive(lock
), FALSE
, NULL
);
1505 /***********************************************************************
1506 * RtlInitializeSRWLock (NTDLL.@)
1509 * Please note that SRWLocks do not keep track of the owner of a lock.
1510 * It doesn't make any difference which thread for example unlocks an
1511 * SRWLock (see corresponding tests). This implementation uses two
1512 * keyed events (one for the exclusive waiters and one for the shared
1513 * waiters) and is limited to 2^15-1 waiting threads.
1515 void WINAPI
RtlInitializeSRWLock( RTL_SRWLOCK
*lock
)
1520 /***********************************************************************
1521 * RtlAcquireSRWLockExclusive (NTDLL.@)
1524 * Unlike RtlAcquireResourceExclusive this function doesn't allow
1525 * nested calls from the same thread. "Upgrading" a shared access lock
1526 * to an exclusive access lock also doesn't seem to be supported.
1528 void WINAPI
RtlAcquireSRWLockExclusive( RTL_SRWLOCK
*lock
)
1530 if (srwlock_lock_exclusive( (unsigned int *)&lock
->Ptr
, SRWLOCK_RES_EXCLUSIVE
))
1531 NtWaitForKeyedEvent( keyed_event
, srwlock_key_exclusive(lock
), FALSE
, NULL
);
1534 /***********************************************************************
1535 * RtlAcquireSRWLockShared (NTDLL.@)
1538 * Do not call this function recursively - it will only succeed when
1539 * there are no threads waiting for an exclusive lock!
1541 void WINAPI
RtlAcquireSRWLockShared( RTL_SRWLOCK
*lock
)
1543 unsigned int val
, tmp
;
1544 /* Acquires a shared lock. If it's currently not possible to add elements to
1545 * the shared queue, then request exclusive access instead. */
1546 for (val
= *(unsigned int *)&lock
->Ptr
;; val
= tmp
)
1548 if ((val
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
) && !(val
& SRWLOCK_MASK_IN_EXCLUSIVE
))
1549 tmp
= val
+ SRWLOCK_RES_EXCLUSIVE
;
1551 tmp
= val
+ SRWLOCK_RES_SHARED
;
1552 if ((tmp
= interlocked_cmpxchg( (int *)&lock
->Ptr
, tmp
, val
)) == val
)
1556 /* Drop exclusive access again and instead requeue for shared access. */
1557 if ((val
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
) && !(val
& SRWLOCK_MASK_IN_EXCLUSIVE
))
1559 NtWaitForKeyedEvent( keyed_event
, srwlock_key_exclusive(lock
), FALSE
, NULL
);
1560 val
= srwlock_unlock_exclusive( (unsigned int *)&lock
->Ptr
, (SRWLOCK_RES_SHARED
1561 - SRWLOCK_RES_EXCLUSIVE
) ) - SRWLOCK_RES_EXCLUSIVE
;
1562 srwlock_leave_exclusive( lock
, val
);
1565 if (val
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
)
1566 NtWaitForKeyedEvent( keyed_event
, srwlock_key_shared(lock
), FALSE
, NULL
);
1569 /***********************************************************************
1570 * RtlReleaseSRWLockExclusive (NTDLL.@)
1572 void WINAPI
RtlReleaseSRWLockExclusive( RTL_SRWLOCK
*lock
)
1574 srwlock_leave_exclusive( lock
, srwlock_unlock_exclusive( (unsigned int *)&lock
->Ptr
,
1575 - SRWLOCK_RES_EXCLUSIVE
) - SRWLOCK_RES_EXCLUSIVE
);
1578 /***********************************************************************
1579 * RtlReleaseSRWLockShared (NTDLL.@)
1581 void WINAPI
RtlReleaseSRWLockShared( RTL_SRWLOCK
*lock
)
1583 srwlock_leave_shared( lock
, srwlock_lock_exclusive( (unsigned int *)&lock
->Ptr
,
1584 - SRWLOCK_RES_SHARED
) - SRWLOCK_RES_SHARED
);
1587 /***********************************************************************
1588 * RtlTryAcquireSRWLockExclusive (NTDLL.@)
1591 * Similar to AcquireSRWLockExclusive recusive calls are not allowed
1592 * and will fail with return value FALSE.
1594 BOOLEAN WINAPI
RtlTryAcquireSRWLockExclusive( RTL_SRWLOCK
*lock
)
1596 return interlocked_cmpxchg( (int *)&lock
->Ptr
, SRWLOCK_MASK_IN_EXCLUSIVE
|
1597 SRWLOCK_RES_EXCLUSIVE
, 0 ) == 0;
1600 /***********************************************************************
1601 * RtlTryAcquireSRWLockShared (NTDLL.@)
1603 BOOLEAN WINAPI
RtlTryAcquireSRWLockShared( RTL_SRWLOCK
*lock
)
1605 unsigned int val
, tmp
;
1606 for (val
= *(unsigned int *)&lock
->Ptr
;; val
= tmp
)
1608 if (val
& SRWLOCK_MASK_EXCLUSIVE_QUEUE
)
1610 if ((tmp
= interlocked_cmpxchg( (int *)&lock
->Ptr
, val
+ SRWLOCK_RES_SHARED
, val
)) == val
)
1616 /***********************************************************************
1617 * RtlInitializeConditionVariable (NTDLL.@)
1619 * Initializes the condition variable with NULL.
1622 * variable [O] condition variable
1627 void WINAPI
RtlInitializeConditionVariable( RTL_CONDITION_VARIABLE
*variable
)
1629 variable
->Ptr
= NULL
;
1632 /***********************************************************************
1633 * RtlWakeConditionVariable (NTDLL.@)
1635 * Wakes up one thread waiting on the condition variable.
1638 * variable [I/O] condition variable to wake up.
1644 * The calling thread does not have to own any lock in order to call
1647 void WINAPI
RtlWakeConditionVariable( RTL_CONDITION_VARIABLE
*variable
)
1649 if (interlocked_dec_if_nonzero( (int *)&variable
->Ptr
))
1650 NtReleaseKeyedEvent( keyed_event
, &variable
->Ptr
, FALSE
, NULL
);
1653 /***********************************************************************
1654 * RtlWakeAllConditionVariable (NTDLL.@)
1656 * See WakeConditionVariable, wakes up all waiting threads.
1658 void WINAPI
RtlWakeAllConditionVariable( RTL_CONDITION_VARIABLE
*variable
)
1660 int val
= interlocked_xchg( (int *)&variable
->Ptr
, 0 );
1662 NtReleaseKeyedEvent( keyed_event
, &variable
->Ptr
, FALSE
, NULL
);
1665 /***********************************************************************
1666 * RtlSleepConditionVariableCS (NTDLL.@)
1668 * Atomically releases the critical section and suspends the thread,
1669 * waiting for a Wake(All)ConditionVariable event. Afterwards it enters
1670 * the critical section again and returns.
1673 * variable [I/O] condition variable
1674 * crit [I/O] critical section to leave temporarily
1675 * timeout [I] timeout
1678 * see NtWaitForKeyedEvent for all possible return values.
1680 NTSTATUS WINAPI
RtlSleepConditionVariableCS( RTL_CONDITION_VARIABLE
*variable
, RTL_CRITICAL_SECTION
*crit
,
1681 const LARGE_INTEGER
*timeout
)
1684 interlocked_xchg_add( (int *)&variable
->Ptr
, 1 );
1685 RtlLeaveCriticalSection( crit
);
1687 status
= NtWaitForKeyedEvent( keyed_event
, &variable
->Ptr
, FALSE
, timeout
);
1688 if (status
!= STATUS_SUCCESS
)
1690 if (!interlocked_dec_if_nonzero( (int *)&variable
->Ptr
))
1691 status
= NtWaitForKeyedEvent( keyed_event
, &variable
->Ptr
, FALSE
, NULL
);
1694 RtlEnterCriticalSection( crit
);
1698 /***********************************************************************
1699 * RtlSleepConditionVariableSRW (NTDLL.@)
1701 * Atomically releases the SRWLock and suspends the thread,
1702 * waiting for a Wake(All)ConditionVariable event. Afterwards it enters
1703 * the SRWLock again with the same access rights and returns.
1706 * variable [I/O] condition variable
1707 * lock [I/O] SRWLock to leave temporarily
1708 * timeout [I] timeout
1709 * flags [I] type of the current lock (exclusive / shared)
1712 * see NtWaitForKeyedEvent for all possible return values.
1715 * the behaviour is undefined if the thread doesn't own the lock.
1717 NTSTATUS WINAPI
RtlSleepConditionVariableSRW( RTL_CONDITION_VARIABLE
*variable
, RTL_SRWLOCK
*lock
,
1718 const LARGE_INTEGER
*timeout
, ULONG flags
)
1721 interlocked_xchg_add( (int *)&variable
->Ptr
, 1 );
1723 if (flags
& RTL_CONDITION_VARIABLE_LOCKMODE_SHARED
)
1724 RtlReleaseSRWLockShared( lock
);
1726 RtlReleaseSRWLockExclusive( lock
);
1728 status
= NtWaitForKeyedEvent( keyed_event
, &variable
->Ptr
, FALSE
, timeout
);
1729 if (status
!= STATUS_SUCCESS
)
1731 if (!interlocked_dec_if_nonzero( (int *)&variable
->Ptr
))
1732 status
= NtWaitForKeyedEvent( keyed_event
, &variable
->Ptr
, FALSE
, NULL
);
1735 if (flags
& RTL_CONDITION_VARIABLE_LOCKMODE_SHARED
)
1736 RtlAcquireSRWLockShared( lock
);
1738 RtlAcquireSRWLockExclusive( lock
);