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
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
35 #ifdef HAVE_SYS_POLL_H
36 # include <sys/poll.h>
50 #define NONAMELESSUNION
51 #define NONAMELESSSTRUCT
54 #define WIN32_NO_STATUS
57 #include "wine/server.h"
58 #include "wine/debug.h"
59 #include "ntdll_misc.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
63 /* creates a struct security_descriptor and contained information in one contiguous piece of memory */
64 NTSTATUS NTDLL_create_struct_sd(PSECURITY_DESCRIPTOR nt_sd, struct security_descriptor **server_sd,
65 data_size_t *server_sd_len)
70 BOOLEAN owner_present, group_present, dacl_present, sacl_present;
79 return STATUS_SUCCESS;
82 len = sizeof(struct security_descriptor);
84 status = RtlGetOwnerSecurityDescriptor(nt_sd, &owner, &owner_present);
85 if (status != STATUS_SUCCESS) return status;
86 status = RtlGetGroupSecurityDescriptor(nt_sd, &group, &group_present);
87 if (status != STATUS_SUCCESS) return status;
88 status = RtlGetSaclSecurityDescriptor(nt_sd, &sacl_present, &sacl, &defaulted);
89 if (status != STATUS_SUCCESS) return status;
90 status = RtlGetDaclSecurityDescriptor(nt_sd, &dacl_present, &dacl, &defaulted);
91 if (status != STATUS_SUCCESS) return status;
94 len += RtlLengthSid(owner);
96 len += RtlLengthSid(group);
97 if (sacl_present && sacl)
99 if (dacl_present && dacl)
100 len += dacl->AclSize;
102 /* fix alignment for the Unicode name that follows the structure */
103 len = (len + sizeof(WCHAR) - 1) & ~(sizeof(WCHAR) - 1);
104 *server_sd = RtlAllocateHeap(GetProcessHeap(), 0, len);
105 if (!*server_sd) return STATUS_NO_MEMORY;
107 (*server_sd)->control = ((SECURITY_DESCRIPTOR *)nt_sd)->Control & ~SE_SELF_RELATIVE;
108 (*server_sd)->owner_len = owner_present ? RtlLengthSid(owner) : 0;
109 (*server_sd)->group_len = group_present ? RtlLengthSid(group) : 0;
110 (*server_sd)->sacl_len = (sacl_present && sacl) ? sacl->AclSize : 0;
111 (*server_sd)->dacl_len = (dacl_present && dacl) ? dacl->AclSize : 0;
113 ptr = (unsigned char *)(*server_sd + 1);
114 memcpy(ptr, owner, (*server_sd)->owner_len);
115 ptr += (*server_sd)->owner_len;
116 memcpy(ptr, group, (*server_sd)->group_len);
117 ptr += (*server_sd)->group_len;
118 memcpy(ptr, sacl, (*server_sd)->sacl_len);
119 ptr += (*server_sd)->sacl_len;
120 memcpy(ptr, dacl, (*server_sd)->dacl_len);
122 *server_sd_len = len;
124 return STATUS_SUCCESS;
127 /* frees a struct security_descriptor allocated by NTDLL_create_struct_sd */
128 void NTDLL_free_struct_sd(struct security_descriptor *server_sd)
130 RtlFreeHeap(GetProcessHeap(), 0, server_sd);
137 /******************************************************************************
138 * NtCreateSemaphore (NTDLL.@)
140 NTSTATUS WINAPI NtCreateSemaphore( OUT PHANDLE SemaphoreHandle,
141 IN ACCESS_MASK access,
142 IN const OBJECT_ATTRIBUTES *attr OPTIONAL,
143 IN LONG InitialCount,
144 IN LONG MaximumCount )
146 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
148 struct object_attributes objattr;
149 struct security_descriptor *sd = NULL;
151 if (MaximumCount <= 0 || InitialCount < 0 || InitialCount > MaximumCount)
152 return STATUS_INVALID_PARAMETER;
153 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
155 objattr.rootdir = attr ? attr->RootDirectory : 0;
157 objattr.name_len = len;
160 ret = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
161 if (ret != STATUS_SUCCESS) return ret;
164 SERVER_START_REQ( create_semaphore )
166 req->access = access;
167 req->attributes = (attr) ? attr->Attributes : 0;
168 req->initial = InitialCount;
169 req->max = MaximumCount;
170 wine_server_add_data( req, &objattr, sizeof(objattr) );
171 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
172 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
173 ret = wine_server_call( req );
174 *SemaphoreHandle = reply->handle;
178 NTDLL_free_struct_sd( sd );
183 /******************************************************************************
184 * NtOpenSemaphore (NTDLL.@)
186 NTSTATUS WINAPI NtOpenSemaphore( OUT PHANDLE SemaphoreHandle,
187 IN ACCESS_MASK access,
188 IN const OBJECT_ATTRIBUTES *attr )
190 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
193 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
195 SERVER_START_REQ( open_semaphore )
197 req->access = access;
198 req->attributes = (attr) ? attr->Attributes : 0;
199 req->rootdir = attr ? attr->RootDirectory : 0;
200 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
201 ret = wine_server_call( req );
202 *SemaphoreHandle = reply->handle;
208 /******************************************************************************
209 * NtQuerySemaphore (NTDLL.@)
211 NTSTATUS WINAPI NtQuerySemaphore(
212 HANDLE SemaphoreHandle,
213 SEMAPHORE_INFORMATION_CLASS SemaphoreInformationClass,
214 PVOID SemaphoreInformation,
218 FIXME("(%p,%d,%p,0x%08x,%p) stub!\n",
219 SemaphoreHandle, SemaphoreInformationClass, SemaphoreInformation, Length, ReturnLength);
220 return STATUS_SUCCESS;
223 /******************************************************************************
224 * NtReleaseSemaphore (NTDLL.@)
226 NTSTATUS WINAPI NtReleaseSemaphore( HANDLE handle, ULONG count, PULONG previous )
229 SERVER_START_REQ( release_semaphore )
231 req->handle = handle;
233 if (!(ret = wine_server_call( req )))
235 if (previous) *previous = reply->prev_count;
246 /**************************************************************************
247 * NtCreateEvent (NTDLL.@)
248 * ZwCreateEvent (NTDLL.@)
250 NTSTATUS WINAPI NtCreateEvent(
251 OUT PHANDLE EventHandle,
252 IN ACCESS_MASK DesiredAccess,
253 IN const OBJECT_ATTRIBUTES *attr,
254 IN BOOLEAN ManualReset,
255 IN BOOLEAN InitialState)
257 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
259 struct security_descriptor *sd = NULL;
260 struct object_attributes objattr;
262 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
264 objattr.rootdir = attr ? attr->RootDirectory : 0;
266 objattr.name_len = len;
269 ret = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
270 if (ret != STATUS_SUCCESS) return ret;
273 SERVER_START_REQ( create_event )
275 req->access = DesiredAccess;
276 req->attributes = (attr) ? attr->Attributes : 0;
277 req->manual_reset = ManualReset;
278 req->initial_state = InitialState;
279 wine_server_add_data( req, &objattr, sizeof(objattr) );
280 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
281 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
282 ret = wine_server_call( req );
283 *EventHandle = reply->handle;
287 NTDLL_free_struct_sd( sd );
292 /******************************************************************************
293 * NtOpenEvent (NTDLL.@)
294 * ZwOpenEvent (NTDLL.@)
296 NTSTATUS WINAPI NtOpenEvent(
297 OUT PHANDLE EventHandle,
298 IN ACCESS_MASK DesiredAccess,
299 IN const OBJECT_ATTRIBUTES *attr )
301 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
304 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
306 SERVER_START_REQ( open_event )
308 req->access = DesiredAccess;
309 req->attributes = (attr) ? attr->Attributes : 0;
310 req->rootdir = attr ? attr->RootDirectory : 0;
311 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
312 ret = wine_server_call( req );
313 *EventHandle = reply->handle;
320 /******************************************************************************
321 * NtSetEvent (NTDLL.@)
322 * ZwSetEvent (NTDLL.@)
324 NTSTATUS WINAPI NtSetEvent( HANDLE handle, PULONG NumberOfThreadsReleased )
328 /* FIXME: set NumberOfThreadsReleased */
330 SERVER_START_REQ( event_op )
332 req->handle = handle;
334 ret = wine_server_call( req );
340 /******************************************************************************
341 * NtResetEvent (NTDLL.@)
343 NTSTATUS WINAPI NtResetEvent( HANDLE handle, PULONG NumberOfThreadsReleased )
347 /* resetting an event can't release any thread... */
348 if (NumberOfThreadsReleased) *NumberOfThreadsReleased = 0;
350 SERVER_START_REQ( event_op )
352 req->handle = handle;
353 req->op = RESET_EVENT;
354 ret = wine_server_call( req );
360 /******************************************************************************
361 * NtClearEvent (NTDLL.@)
364 * same as NtResetEvent ???
366 NTSTATUS WINAPI NtClearEvent ( HANDLE handle )
368 return NtResetEvent( handle, NULL );
371 /******************************************************************************
372 * NtPulseEvent (NTDLL.@)
377 NTSTATUS WINAPI NtPulseEvent( HANDLE handle, PULONG PulseCount )
382 FIXME("(%p,%d)\n", handle, *PulseCount);
384 SERVER_START_REQ( event_op )
386 req->handle = handle;
387 req->op = PULSE_EVENT;
388 ret = wine_server_call( req );
394 /******************************************************************************
395 * NtQueryEvent (NTDLL.@)
397 NTSTATUS WINAPI NtQueryEvent (
398 IN HANDLE EventHandle,
399 IN EVENT_INFORMATION_CLASS EventInformationClass,
400 OUT PVOID EventInformation,
401 IN ULONG EventInformationLength,
402 OUT PULONG ReturnLength)
404 FIXME("(%p)\n", EventHandle);
405 return STATUS_SUCCESS;
409 * Mutants (known as Mutexes in Kernel32)
412 /******************************************************************************
413 * NtCreateMutant [NTDLL.@]
414 * ZwCreateMutant [NTDLL.@]
416 NTSTATUS WINAPI NtCreateMutant(OUT HANDLE* MutantHandle,
417 IN ACCESS_MASK access,
418 IN const OBJECT_ATTRIBUTES* attr OPTIONAL,
419 IN BOOLEAN InitialOwner)
422 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
423 struct security_descriptor *sd = NULL;
424 struct object_attributes objattr;
426 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
428 objattr.rootdir = attr ? attr->RootDirectory : 0;
430 objattr.name_len = len;
433 status = NTDLL_create_struct_sd( attr->SecurityDescriptor, &sd, &objattr.sd_len );
434 if (status != STATUS_SUCCESS) return status;
437 SERVER_START_REQ( create_mutex )
439 req->access = access;
440 req->attributes = (attr) ? attr->Attributes : 0;
441 req->owned = InitialOwner;
442 wine_server_add_data( req, &objattr, sizeof(objattr) );
443 if (objattr.sd_len) wine_server_add_data( req, sd, objattr.sd_len );
444 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
445 status = wine_server_call( req );
446 *MutantHandle = reply->handle;
450 NTDLL_free_struct_sd( sd );
455 /**************************************************************************
456 * NtOpenMutant [NTDLL.@]
457 * ZwOpenMutant [NTDLL.@]
459 NTSTATUS WINAPI NtOpenMutant(OUT HANDLE* MutantHandle,
460 IN ACCESS_MASK access,
461 IN const OBJECT_ATTRIBUTES* attr )
464 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
466 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
468 SERVER_START_REQ( open_mutex )
470 req->access = access;
471 req->attributes = (attr) ? attr->Attributes : 0;
472 req->rootdir = attr ? attr->RootDirectory : 0;
473 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
474 status = wine_server_call( req );
475 *MutantHandle = reply->handle;
481 /**************************************************************************
482 * NtReleaseMutant [NTDLL.@]
483 * ZwReleaseMutant [NTDLL.@]
485 NTSTATUS WINAPI NtReleaseMutant( IN HANDLE handle, OUT PLONG prev_count OPTIONAL)
489 SERVER_START_REQ( release_mutex )
491 req->handle = handle;
492 status = wine_server_call( req );
493 if (prev_count) *prev_count = reply->prev_count;
499 /******************************************************************
500 * NtQueryMutant [NTDLL.@]
501 * ZwQueryMutant [NTDLL.@]
503 NTSTATUS WINAPI NtQueryMutant(IN HANDLE handle,
504 IN MUTANT_INFORMATION_CLASS MutantInformationClass,
505 OUT PVOID MutantInformation,
506 IN ULONG MutantInformationLength,
507 OUT PULONG ResultLength OPTIONAL )
509 FIXME("(%p %u %p %u %p): stub!\n",
510 handle, MutantInformationClass, MutantInformation, MutantInformationLength, ResultLength);
511 return STATUS_NOT_IMPLEMENTED;
518 /******************************************************************************
519 * NtCreateJobObject [NTDLL.@]
520 * ZwCreateJobObject [NTDLL.@]
522 NTSTATUS WINAPI NtCreateJobObject( PHANDLE handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
524 FIXME( "stub: %p %x %s\n", handle, access, attr ? debugstr_us(attr->ObjectName) : "" );
525 *handle = (HANDLE)0xdead;
526 return STATUS_SUCCESS;
529 /******************************************************************************
530 * NtOpenJobObject [NTDLL.@]
531 * ZwOpenJobObject [NTDLL.@]
533 NTSTATUS WINAPI NtOpenJobObject( PHANDLE handle, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
535 FIXME( "stub: %p %x %s\n", handle, access, attr ? debugstr_us(attr->ObjectName) : "" );
536 return STATUS_NOT_IMPLEMENTED;
539 /******************************************************************************
540 * NtTerminateJobObject [NTDLL.@]
541 * ZwTerminateJobObject [NTDLL.@]
543 NTSTATUS WINAPI NtTerminateJobObject( HANDLE handle, NTSTATUS status )
545 FIXME( "stub: %p %x\n", handle, status );
546 return STATUS_SUCCESS;
549 /******************************************************************************
550 * NtQueryInformationJobObject [NTDLL.@]
551 * ZwQueryInformationJobObject [NTDLL.@]
553 NTSTATUS WINAPI NtQueryInformationJobObject( HANDLE handle, JOBOBJECTINFOCLASS class, PVOID info,
554 ULONG len, PULONG ret_len )
556 FIXME( "stub: %p %u %p %u %p\n", handle, class, info, len, ret_len );
557 return STATUS_NOT_IMPLEMENTED;
560 /******************************************************************************
561 * NtSetInformationJobObject [NTDLL.@]
562 * ZwSetInformationJobObject [NTDLL.@]
564 NTSTATUS WINAPI NtSetInformationJobObject( HANDLE handle, JOBOBJECTINFOCLASS class, PVOID info, ULONG len )
566 FIXME( "stub: %p %u %p %u\n", handle, class, info, len );
567 return STATUS_SUCCESS;
570 /******************************************************************************
571 * NtIsProcessInJob [NTDLL.@]
572 * ZwIsProcessInJob [NTDLL.@]
574 NTSTATUS WINAPI NtIsProcessInJob( HANDLE process, HANDLE job )
576 FIXME( "stub: %p %p\n", process, job );
577 return STATUS_PROCESS_NOT_IN_JOB;
580 /******************************************************************************
581 * NtAssignProcessToJobObject [NTDLL.@]
582 * ZwAssignProcessToJobObject [NTDLL.@]
584 NTSTATUS WINAPI NtAssignProcessToJobObject( HANDLE job, HANDLE process )
586 FIXME( "stub: %p %p\n", job, process );
587 return STATUS_SUCCESS;
594 /**************************************************************************
595 * NtCreateTimer [NTDLL.@]
596 * ZwCreateTimer [NTDLL.@]
598 NTSTATUS WINAPI NtCreateTimer(OUT HANDLE *handle,
599 IN ACCESS_MASK access,
600 IN const OBJECT_ATTRIBUTES *attr OPTIONAL,
601 IN TIMER_TYPE timer_type)
603 DWORD len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
606 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
608 if (timer_type != NotificationTimer && timer_type != SynchronizationTimer)
609 return STATUS_INVALID_PARAMETER;
611 SERVER_START_REQ( create_timer )
613 req->access = access;
614 req->attributes = (attr) ? attr->Attributes : 0;
615 req->rootdir = attr ? attr->RootDirectory : 0;
616 req->manual = (timer_type == NotificationTimer) ? TRUE : FALSE;
617 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
618 status = wine_server_call( req );
619 *handle = reply->handle;
626 /**************************************************************************
627 * NtOpenTimer [NTDLL.@]
628 * ZwOpenTimer [NTDLL.@]
630 NTSTATUS WINAPI NtOpenTimer(OUT PHANDLE handle,
631 IN ACCESS_MASK access,
632 IN const OBJECT_ATTRIBUTES* attr )
634 DWORD len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
637 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
639 SERVER_START_REQ( open_timer )
641 req->access = access;
642 req->attributes = (attr) ? attr->Attributes : 0;
643 req->rootdir = attr ? attr->RootDirectory : 0;
644 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
645 status = wine_server_call( req );
646 *handle = reply->handle;
652 /**************************************************************************
653 * NtSetTimer [NTDLL.@]
654 * ZwSetTimer [NTDLL.@]
656 NTSTATUS WINAPI NtSetTimer(IN HANDLE handle,
657 IN const LARGE_INTEGER* when,
658 IN PTIMER_APC_ROUTINE callback,
659 IN PVOID callback_arg,
661 IN ULONG period OPTIONAL,
662 OUT PBOOLEAN state OPTIONAL)
664 NTSTATUS status = STATUS_SUCCESS;
666 TRACE("(%p,%p,%p,%p,%08x,0x%08x,%p) stub\n",
667 handle, when, callback, callback_arg, resume, period, state);
669 SERVER_START_REQ( set_timer )
671 req->handle = handle;
672 req->period = period;
673 req->expire = when->QuadPart;
674 req->callback = callback;
675 req->arg = callback_arg;
676 status = wine_server_call( req );
677 if (state) *state = reply->signaled;
681 /* set error but can still succeed */
682 if (resume && status == STATUS_SUCCESS) return STATUS_TIMER_RESUME_IGNORED;
686 /**************************************************************************
687 * NtCancelTimer [NTDLL.@]
688 * ZwCancelTimer [NTDLL.@]
690 NTSTATUS WINAPI NtCancelTimer(IN HANDLE handle, OUT BOOLEAN* state)
694 SERVER_START_REQ( cancel_timer )
696 req->handle = handle;
697 status = wine_server_call( req );
698 if (state) *state = reply->signaled;
704 /******************************************************************************
705 * NtQueryTimer (NTDLL.@)
707 * Retrieves information about a timer.
710 * TimerHandle [I] The timer to retrieve information about.
711 * TimerInformationClass [I] The type of information to retrieve.
712 * TimerInformation [O] Pointer to buffer to store information in.
713 * Length [I] The length of the buffer pointed to by TimerInformation.
714 * ReturnLength [O] Optional. The size of buffer actually used.
717 * Success: STATUS_SUCCESS
718 * Failure: STATUS_INFO_LENGTH_MISMATCH, if Length doesn't match the required data
719 * size for the class specified.
720 * STATUS_INVALID_INFO_CLASS, if an invalid TimerInformationClass was specified.
721 * STATUS_ACCESS_DENIED, if TimerHandle does not have TIMER_QUERY_STATE access
724 NTSTATUS WINAPI NtQueryTimer(
726 TIMER_INFORMATION_CLASS TimerInformationClass,
727 PVOID TimerInformation,
731 TIMER_BASIC_INFORMATION * basic_info = (TIMER_BASIC_INFORMATION *)TimerInformation;
735 TRACE("(%p,%d,%p,0x%08x,%p)\n", TimerHandle, TimerInformationClass,
736 TimerInformation, Length, ReturnLength);
738 switch (TimerInformationClass)
740 case TimerBasicInformation:
741 if (Length < sizeof(TIMER_BASIC_INFORMATION))
742 return STATUS_INFO_LENGTH_MISMATCH;
744 SERVER_START_REQ(get_timer_info)
746 req->handle = TimerHandle;
747 status = wine_server_call(req);
749 /* convert server time to absolute NTDLL time */
750 basic_info->RemainingTime.QuadPart = reply->when;
751 basic_info->TimerState = reply->signaled;
755 /* convert from absolute into relative time */
756 NtQuerySystemTime(&now);
757 if (now.QuadPart > basic_info->RemainingTime.QuadPart)
758 basic_info->RemainingTime.QuadPart = 0;
760 basic_info->RemainingTime.QuadPart -= now.QuadPart;
762 if (ReturnLength) *ReturnLength = sizeof(TIMER_BASIC_INFORMATION);
767 FIXME("Unhandled class %d\n", TimerInformationClass);
768 return STATUS_INVALID_INFO_CLASS;
772 /******************************************************************************
773 * NtQueryTimerResolution [NTDLL.@]
775 NTSTATUS WINAPI NtQueryTimerResolution(OUT ULONG* min_resolution,
776 OUT ULONG* max_resolution,
777 OUT ULONG* current_resolution)
779 FIXME("(%p,%p,%p), stub!\n",
780 min_resolution, max_resolution, current_resolution);
782 return STATUS_NOT_IMPLEMENTED;
785 /******************************************************************************
786 * NtSetTimerResolution [NTDLL.@]
788 NTSTATUS WINAPI NtSetTimerResolution(IN ULONG resolution,
789 IN BOOLEAN set_resolution,
790 OUT ULONG* current_resolution )
792 FIXME("(%u,%u,%p), stub!\n",
793 resolution, set_resolution, current_resolution);
795 return STATUS_NOT_IMPLEMENTED;
799 /***********************************************************************
802 * Wait for a reply on the waiting pipe of the current thread.
804 static int wait_reply( void *cookie )
807 struct wake_up_reply reply;
811 ret = read( ntdll_get_thread_data()->wait_fd[0], &reply, sizeof(reply) );
812 if (ret == sizeof(reply))
814 if (!reply.cookie) break; /* thread got killed */
815 if (reply.cookie == cookie) return reply.signaled;
816 /* we stole another reply, wait for the real one */
817 signaled = wait_reply( cookie );
818 /* and now put the wrong one back in the pipe */
821 ret = write( ntdll_get_thread_data()->wait_fd[1], &reply, sizeof(reply) );
822 if (ret == sizeof(reply)) break;
823 if (ret >= 0) server_protocol_error( "partial wakeup write %d\n", ret );
824 if (errno == EINTR) continue;
825 server_protocol_perror("wakeup write");
829 if (ret >= 0) server_protocol_error( "partial wakeup read %d\n", ret );
830 if (errno == EINTR) continue;
831 server_protocol_perror("wakeup read");
833 /* the server closed the connection; time to die... */
834 server_abort_thread(0);
838 /***********************************************************************
841 * Invoke a single APC. Return TRUE if a user APC has been run.
843 static BOOL invoke_apc( const apc_call_t *call, apc_result_t *result )
845 BOOL user_apc = FALSE;
847 memset( result, 0, sizeof(*result) );
854 call->user.func( call->user.args[0], call->user.args[1], call->user.args[2] );
858 call->timer.func( call->timer.arg, (DWORD)call->timer.time, (DWORD)(call->timer.time >> 32) );
862 result->type = call->type;
863 result->async_io.status = call->async_io.func( call->async_io.user,
865 call->async_io.status,
866 &result->async_io.total );
868 case APC_VIRTUAL_ALLOC:
869 result->type = call->type;
870 result->virtual_alloc.addr = call->virtual_alloc.addr;
871 result->virtual_alloc.size = call->virtual_alloc.size;
872 result->virtual_alloc.status = NtAllocateVirtualMemory( NtCurrentProcess(),
873 &result->virtual_alloc.addr,
874 call->virtual_alloc.zero_bits,
875 &result->virtual_alloc.size,
876 call->virtual_alloc.op_type,
877 call->virtual_alloc.prot );
879 case APC_VIRTUAL_FREE:
880 result->type = call->type;
881 result->virtual_free.addr = call->virtual_free.addr;
882 result->virtual_free.size = call->virtual_free.size;
883 result->virtual_free.status = NtFreeVirtualMemory( NtCurrentProcess(),
884 &result->virtual_free.addr,
885 &result->virtual_free.size,
886 call->virtual_free.op_type );
888 case APC_VIRTUAL_QUERY:
890 MEMORY_BASIC_INFORMATION info;
891 result->type = call->type;
892 result->virtual_query.status = NtQueryVirtualMemory( NtCurrentProcess(),
893 call->virtual_query.addr,
894 MemoryBasicInformation, &info,
895 sizeof(info), NULL );
896 if (result->virtual_query.status == STATUS_SUCCESS)
898 result->virtual_query.base = info.BaseAddress;
899 result->virtual_query.alloc_base = info.AllocationBase;
900 result->virtual_query.size = info.RegionSize;
901 result->virtual_query.state = info.State;
902 result->virtual_query.prot = info.Protect;
903 result->virtual_query.alloc_prot = info.AllocationProtect;
904 result->virtual_query.alloc_type = info.Type;
908 case APC_VIRTUAL_PROTECT:
909 result->type = call->type;
910 result->virtual_protect.addr = call->virtual_protect.addr;
911 result->virtual_protect.size = call->virtual_protect.size;
912 result->virtual_protect.status = NtProtectVirtualMemory( NtCurrentProcess(),
913 &result->virtual_protect.addr,
914 &result->virtual_protect.size,
915 call->virtual_protect.prot,
916 &result->virtual_protect.prot );
918 case APC_VIRTUAL_FLUSH:
919 result->type = call->type;
920 result->virtual_flush.addr = call->virtual_flush.addr;
921 result->virtual_flush.size = call->virtual_flush.size;
922 result->virtual_flush.status = NtFlushVirtualMemory( NtCurrentProcess(),
923 &result->virtual_flush.addr,
924 &result->virtual_flush.size, 0 );
926 case APC_VIRTUAL_LOCK:
927 result->type = call->type;
928 result->virtual_lock.addr = call->virtual_lock.addr;
929 result->virtual_lock.size = call->virtual_lock.size;
930 result->virtual_lock.status = NtLockVirtualMemory( NtCurrentProcess(),
931 &result->virtual_lock.addr,
932 &result->virtual_lock.size, 0 );
934 case APC_VIRTUAL_UNLOCK:
935 result->type = call->type;
936 result->virtual_unlock.addr = call->virtual_unlock.addr;
937 result->virtual_unlock.size = call->virtual_unlock.size;
938 result->virtual_unlock.status = NtUnlockVirtualMemory( NtCurrentProcess(),
939 &result->virtual_unlock.addr,
940 &result->virtual_unlock.size, 0 );
944 LARGE_INTEGER offset;
945 result->type = call->type;
946 result->map_view.addr = call->map_view.addr;
947 result->map_view.size = call->map_view.size;
948 offset.QuadPart = call->map_view.offset;
949 result->map_view.status = NtMapViewOfSection( call->map_view.handle, NtCurrentProcess(),
950 &result->map_view.addr, call->map_view.zero_bits,
951 0, &offset, &result->map_view.size, ViewShare,
952 call->map_view.alloc_type, call->map_view.prot );
953 NtClose( call->map_view.handle );
957 result->type = call->type;
958 result->unmap_view.status = NtUnmapViewOfSection( NtCurrentProcess(), call->unmap_view.addr );
960 case APC_CREATE_THREAD:
963 result->type = call->type;
964 result->create_thread.status = RtlCreateUserThread( NtCurrentProcess(), NULL,
965 call->create_thread.suspend, NULL,
966 call->create_thread.reserve,
967 call->create_thread.commit,
968 call->create_thread.func,
969 call->create_thread.arg,
970 &result->create_thread.handle, &id );
971 result->create_thread.tid = HandleToULong(id.UniqueThread);
975 server_protocol_error( "get_apc_request: bad type %d\n", call->type );
981 /***********************************************************************
982 * NTDLL_queue_process_apc
984 NTSTATUS NTDLL_queue_process_apc( HANDLE process, const apc_call_t *call, apc_result_t *result )
992 SERVER_START_REQ( queue_apc )
994 req->process = process;
996 if (!(ret = wine_server_call( req )))
998 handle = reply->handle;
1003 if (ret != STATUS_SUCCESS) return ret;
1007 invoke_apc( call, result );
1011 NtWaitForSingleObject( handle, FALSE, NULL );
1013 SERVER_START_REQ( get_apc_result )
1015 req->handle = handle;
1016 if (!(ret = wine_server_call( req ))) *result = reply->result;
1020 if (!ret && result->type == APC_NONE) continue; /* APC didn't run, try again */
1021 if (ret) NtClose( handle );
1028 /***********************************************************************
1029 * NTDLL_wait_for_multiple_objects
1031 * Implementation of NtWaitForMultipleObjects
1033 NTSTATUS NTDLL_wait_for_multiple_objects( UINT count, const HANDLE *handles, UINT flags,
1034 const LARGE_INTEGER *timeout, HANDLE signal_object )
1038 BOOL user_apc = FALSE;
1039 obj_handle_t apc_handle = 0;
1041 apc_result_t result;
1042 timeout_t abs_timeout = timeout ? timeout->QuadPart : TIMEOUT_INFINITE;
1044 memset( &result, 0, sizeof(result) );
1048 SERVER_START_REQ( select )
1051 req->cookie = &cookie;
1052 req->signal = signal_object;
1053 req->prev_apc = apc_handle;
1054 req->timeout = abs_timeout;
1055 wine_server_add_data( req, &result, sizeof(result) );
1056 wine_server_add_data( req, handles, count * sizeof(HANDLE) );
1057 ret = wine_server_call( req );
1058 abs_timeout = reply->timeout;
1059 apc_handle = reply->apc_handle;
1063 if (ret == STATUS_PENDING) ret = wait_reply( &cookie );
1064 if (ret != STATUS_USER_APC) break;
1065 if (invoke_apc( &call, &result ))
1067 /* if we ran a user apc we have to check once more if an object got signaled,
1068 * but we don't want to wait */
1072 signal_object = 0; /* don't signal it multiple times */
1075 if (ret == STATUS_TIMEOUT && user_apc) ret = STATUS_USER_APC;
1077 /* A test on Windows 2000 shows that Windows always yields during
1078 a wait, but a wait that is hit by an event gets a priority
1079 boost as well. This seems to model that behavior the closest. */
1080 if (ret == STATUS_TIMEOUT) NtYieldExecution();
1086 /* wait operations */
1088 /******************************************************************
1089 * NtWaitForMultipleObjects (NTDLL.@)
1091 NTSTATUS WINAPI NtWaitForMultipleObjects( DWORD count, const HANDLE *handles,
1092 BOOLEAN wait_all, BOOLEAN alertable,
1093 const LARGE_INTEGER *timeout )
1095 UINT flags = SELECT_INTERRUPTIBLE;
1097 if (!count || count > MAXIMUM_WAIT_OBJECTS) return STATUS_INVALID_PARAMETER_1;
1099 if (wait_all) flags |= SELECT_ALL;
1100 if (alertable) flags |= SELECT_ALERTABLE;
1101 return NTDLL_wait_for_multiple_objects( count, handles, flags, timeout, 0 );
1105 /******************************************************************
1106 * NtWaitForSingleObject (NTDLL.@)
1108 NTSTATUS WINAPI NtWaitForSingleObject(HANDLE handle, BOOLEAN alertable, const LARGE_INTEGER *timeout )
1110 return NtWaitForMultipleObjects( 1, &handle, FALSE, alertable, timeout );
1114 /******************************************************************
1115 * NtSignalAndWaitForSingleObject (NTDLL.@)
1117 NTSTATUS WINAPI NtSignalAndWaitForSingleObject( HANDLE hSignalObject, HANDLE hWaitObject,
1118 BOOLEAN alertable, const LARGE_INTEGER *timeout )
1120 UINT flags = SELECT_INTERRUPTIBLE;
1122 if (!hSignalObject) return STATUS_INVALID_HANDLE;
1123 if (alertable) flags |= SELECT_ALERTABLE;
1124 return NTDLL_wait_for_multiple_objects( 1, &hWaitObject, flags, timeout, hSignalObject );
1128 /******************************************************************
1129 * NtYieldExecution (NTDLL.@)
1131 NTSTATUS WINAPI NtYieldExecution(void)
1133 #ifdef HAVE_SCHED_YIELD
1135 return STATUS_SUCCESS;
1137 return STATUS_NO_YIELD_PERFORMED;
1142 /******************************************************************
1143 * NtDelayExecution (NTDLL.@)
1145 NTSTATUS WINAPI NtDelayExecution( BOOLEAN alertable, const LARGE_INTEGER *timeout )
1147 /* if alertable, we need to query the server */
1149 return NTDLL_wait_for_multiple_objects( 0, NULL, SELECT_INTERRUPTIBLE | SELECT_ALERTABLE,
1152 if (!timeout || timeout->QuadPart == TIMEOUT_INFINITE) /* sleep forever */
1154 for (;;) select( 0, NULL, NULL, NULL, NULL );
1159 timeout_t when, diff;
1161 if ((when = timeout->QuadPart) < 0)
1163 NtQuerySystemTime( &now );
1164 when = now.QuadPart - when;
1167 /* Note that we yield after establishing the desired timeout */
1169 if (!when) return STATUS_SUCCESS;
1174 NtQuerySystemTime( &now );
1175 diff = (when - now.QuadPart + 9) / 10;
1176 if (diff <= 0) break;
1177 tv.tv_sec = diff / 1000000;
1178 tv.tv_usec = diff % 1000000;
1179 if (select( 0, NULL, NULL, NULL, &tv ) != -1) break;
1182 return STATUS_SUCCESS;
1185 /******************************************************************
1186 * NtCreateIoCompletion (NTDLL.@)
1187 * ZwCreateIoCompletion (NTDLL.@)
1189 * Creates I/O completion object.
1192 * CompletionPort [O] created completion object handle will be placed there
1193 * DesiredAccess [I] desired access to a handle (combination of IO_COMPLETION_*)
1194 * ObjectAttributes [I] completion object attributes
1195 * NumberOfConcurrentThreads [I] desired number of concurrent active worker threads
1198 NTSTATUS WINAPI NtCreateIoCompletion( PHANDLE CompletionPort, ACCESS_MASK DesiredAccess,
1199 POBJECT_ATTRIBUTES ObjectAttributes, ULONG NumberOfConcurrentThreads )
1203 TRACE("(%p, %x, %p, %d)\n", CompletionPort, DesiredAccess,
1204 ObjectAttributes, NumberOfConcurrentThreads);
1206 if (!CompletionPort)
1207 return STATUS_INVALID_PARAMETER;
1209 SERVER_START_REQ( create_completion )
1211 req->access = DesiredAccess;
1212 req->attributes = ObjectAttributes ? ObjectAttributes->Attributes : 0;
1213 req->rootdir = ObjectAttributes ? ObjectAttributes->RootDirectory : NULL;
1214 req->concurrent = NumberOfConcurrentThreads;
1215 if (ObjectAttributes && ObjectAttributes->ObjectName)
1216 wine_server_add_data( req, ObjectAttributes->ObjectName->Buffer,
1217 ObjectAttributes->ObjectName->Length );
1218 if (!(status = wine_server_call( req )))
1219 *CompletionPort = reply->handle;
1225 /******************************************************************
1226 * NtSetIoCompletion (NTDLL.@)
1227 * ZwSetIoCompletion (NTDLL.@)
1229 * Inserts completion message into queue
1232 * CompletionPort [I] HANDLE to completion object
1233 * CompletionKey [I] completion key
1234 * CompletionValue [I] completion value (usually pointer to OVERLAPPED)
1235 * Status [I] operation status
1236 * NumberOfBytesTransferred [I] number of bytes transferred
1238 NTSTATUS WINAPI NtSetIoCompletion( HANDLE CompletionPort, ULONG_PTR CompletionKey,
1239 ULONG_PTR CompletionValue, NTSTATUS Status,
1240 ULONG NumberOfBytesTransferred )
1244 TRACE("(%p, %lx, %lx, %x, %d)\n", CompletionPort, CompletionKey,
1245 CompletionValue, Status, NumberOfBytesTransferred);
1247 SERVER_START_REQ( add_completion )
1249 req->handle = CompletionPort;
1250 req->ckey = CompletionKey;
1251 req->cvalue = CompletionValue;
1252 req->status = Status;
1253 req->information = NumberOfBytesTransferred;
1254 status = wine_server_call( req );
1260 /******************************************************************
1261 * NtRemoveIoCompletion (NTDLL.@)
1262 * ZwRemoveIoCompletion (NTDLL.@)
1264 * (Wait for and) retrieve first completion message from completion object's queue
1267 * CompletionPort [I] HANDLE to I/O completion object
1268 * CompletionKey [O] completion key
1269 * CompletionValue [O] Completion value given in NtSetIoCompletion or in async operation
1270 * iosb [O] IO_STATUS_BLOCK of completed asynchronous operation
1271 * WaitTime [I] optional wait time in NTDLL format
1274 NTSTATUS WINAPI NtRemoveIoCompletion( HANDLE CompletionPort, PULONG_PTR CompletionKey,
1275 PULONG_PTR CompletionValue, PIO_STATUS_BLOCK iosb,
1276 PLARGE_INTEGER WaitTime )
1280 TRACE("(%p, %p, %p, %p, %p)\n", CompletionPort, CompletionKey,
1281 CompletionValue, iosb, WaitTime);
1285 SERVER_START_REQ( remove_completion )
1287 req->handle = CompletionPort;
1288 if (!(status = wine_server_call( req )))
1290 *CompletionKey = reply->ckey;
1291 *CompletionValue = reply->cvalue;
1292 iosb->Information = reply->information;
1293 iosb->u.Status = reply->status;
1297 if (status != STATUS_PENDING) break;
1299 status = NtWaitForSingleObject( CompletionPort, FALSE, WaitTime );
1300 if (status != WAIT_OBJECT_0) break;
1305 /******************************************************************
1306 * NtOpenIoCompletion (NTDLL.@)
1307 * ZwOpenIoCompletion (NTDLL.@)
1309 * Opens I/O completion object
1312 * CompletionPort [O] completion object handle will be placed there
1313 * DesiredAccess [I] desired access to a handle (combination of IO_COMPLETION_*)
1314 * ObjectAttributes [I] completion object name
1317 NTSTATUS WINAPI NtOpenIoCompletion( PHANDLE CompletionPort, ACCESS_MASK DesiredAccess,
1318 POBJECT_ATTRIBUTES ObjectAttributes )
1322 TRACE("(%p, 0x%x, %p)\n", CompletionPort, DesiredAccess, ObjectAttributes);
1324 if (!CompletionPort || !ObjectAttributes || !ObjectAttributes->ObjectName)
1325 return STATUS_INVALID_PARAMETER;
1327 SERVER_START_REQ( open_completion )
1329 req->access = DesiredAccess;
1330 req->rootdir = ObjectAttributes->RootDirectory;
1331 wine_server_add_data( req, ObjectAttributes->ObjectName->Buffer,
1332 ObjectAttributes->ObjectName->Length );
1333 if (!(status = wine_server_call( req )))
1334 *CompletionPort = reply->handle;
1340 /******************************************************************
1341 * NtQueryIoCompletion (NTDLL.@)
1342 * ZwQueryIoCompletion (NTDLL.@)
1344 * Requests information about given I/O completion object
1347 * CompletionPort [I] HANDLE to completion port to request
1348 * InformationClass [I] information class
1349 * CompletionInformation [O] user-provided buffer for data
1350 * BufferLength [I] buffer length
1351 * RequiredLength [O] required buffer length
1354 NTSTATUS WINAPI NtQueryIoCompletion( HANDLE CompletionPort, IO_COMPLETION_INFORMATION_CLASS InformationClass,
1355 PVOID CompletionInformation, ULONG BufferLength, PULONG RequiredLength )
1359 TRACE("(%p, %d, %p, 0x%x, %p)\n", CompletionPort, InformationClass, CompletionInformation,
1360 BufferLength, RequiredLength);
1362 if (!CompletionInformation) return STATUS_INVALID_PARAMETER;
1363 switch( InformationClass )
1365 case IoCompletionBasicInformation:
1367 ULONG *info = (ULONG *)CompletionInformation;
1369 if (RequiredLength) *RequiredLength = sizeof(*info);
1370 if (BufferLength != sizeof(*info))
1371 status = STATUS_INFO_LENGTH_MISMATCH;
1374 SERVER_START_REQ( query_completion )
1376 req->handle = CompletionPort;
1377 if (!(status = wine_server_call( req )))
1378 *info = reply->depth;
1385 status = STATUS_INVALID_PARAMETER;
1391 NTSTATUS NTDLL_AddCompletion( HANDLE hFile, ULONG_PTR CompletionValue, NTSTATUS CompletionStatus, ULONG_PTR Information )
1395 SERVER_START_REQ( add_fd_completion )
1397 req->handle = hFile;
1398 req->cvalue = CompletionValue;
1399 req->status = CompletionStatus;
1400 req->information = Information;
1401 status = wine_server_call( req );