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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
58 #include "wine/server.h"
59 #include "wine/debug.h"
60 #include "ntdll_misc.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
69 /******************************************************************************
70 * NtCreateSemaphore (NTDLL.@)
72 NTSTATUS WINAPI NtCreateSemaphore( OUT PHANDLE SemaphoreHandle,
73 IN ACCESS_MASK access,
74 IN const OBJECT_ATTRIBUTES *attr OPTIONAL,
76 IN LONG MaximumCount )
78 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
81 if (MaximumCount <= 0 || InitialCount < 0 || InitialCount > MaximumCount)
82 return STATUS_INVALID_PARAMETER;
83 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
85 SERVER_START_REQ( create_semaphore )
88 req->initial = InitialCount;
89 req->max = MaximumCount;
90 req->inherit = attr && (attr->Attributes & OBJ_INHERIT);
91 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
92 ret = wine_server_call( req );
93 *SemaphoreHandle = reply->handle;
99 /******************************************************************************
100 * NtOpenSemaphore (NTDLL.@)
102 NTSTATUS WINAPI NtOpenSemaphore( OUT PHANDLE SemaphoreHandle,
103 IN ACCESS_MASK access,
104 IN const OBJECT_ATTRIBUTES *attr )
106 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
109 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
111 SERVER_START_REQ( open_semaphore )
113 req->access = access;
114 req->inherit = attr && (attr->Attributes & OBJ_INHERIT);
115 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
116 ret = wine_server_call( req );
117 *SemaphoreHandle = reply->handle;
123 /******************************************************************************
124 * NtQuerySemaphore (NTDLL.@)
126 NTSTATUS WINAPI NtQuerySemaphore(
127 HANDLE SemaphoreHandle,
128 PVOID SemaphoreInformationClass,
129 OUT PVOID SemaphoreInformation,
133 FIXME("(%p,%p,%p,0x%08lx,%p) stub!\n",
134 SemaphoreHandle, SemaphoreInformationClass, SemaphoreInformation, Length, ReturnLength);
135 return STATUS_SUCCESS;
138 /******************************************************************************
139 * NtReleaseSemaphore (NTDLL.@)
141 NTSTATUS WINAPI NtReleaseSemaphore( HANDLE handle, ULONG count, PULONG previous )
144 SERVER_START_REQ( release_semaphore )
146 req->handle = handle;
148 if (!(ret = wine_server_call( req )))
150 if (previous) *previous = reply->prev_count;
161 /**************************************************************************
162 * NtCreateEvent (NTDLL.@)
163 * ZwCreateEvent (NTDLL.@)
165 NTSTATUS WINAPI NtCreateEvent(
166 OUT PHANDLE EventHandle,
167 IN ACCESS_MASK DesiredAccess,
168 IN const OBJECT_ATTRIBUTES *attr,
169 IN BOOLEAN ManualReset,
170 IN BOOLEAN InitialState)
172 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
175 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
177 SERVER_START_REQ( create_event )
179 req->access = DesiredAccess;
180 req->manual_reset = ManualReset;
181 req->initial_state = InitialState;
182 req->inherit = attr && (attr->Attributes & OBJ_INHERIT);
183 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
184 ret = wine_server_call( req );
185 *EventHandle = reply->handle;
191 /******************************************************************************
192 * NtOpenEvent (NTDLL.@)
193 * ZwOpenEvent (NTDLL.@)
195 NTSTATUS WINAPI NtOpenEvent(
196 OUT PHANDLE EventHandle,
197 IN ACCESS_MASK DesiredAccess,
198 IN const OBJECT_ATTRIBUTES *attr )
200 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
203 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
205 SERVER_START_REQ( open_event )
207 req->access = DesiredAccess;
208 req->inherit = attr && (attr->Attributes & OBJ_INHERIT);
209 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
210 ret = wine_server_call( req );
211 *EventHandle = reply->handle;
218 /******************************************************************************
219 * NtSetEvent (NTDLL.@)
220 * ZwSetEvent (NTDLL.@)
222 NTSTATUS WINAPI NtSetEvent( HANDLE handle, PULONG NumberOfThreadsReleased )
226 /* FIXME: set NumberOfThreadsReleased */
228 SERVER_START_REQ( event_op )
230 req->handle = handle;
232 ret = wine_server_call( req );
238 /******************************************************************************
239 * NtResetEvent (NTDLL.@)
241 NTSTATUS WINAPI NtResetEvent( HANDLE handle, PULONG NumberOfThreadsReleased )
245 /* resetting an event can't release any thread... */
246 if (NumberOfThreadsReleased) *NumberOfThreadsReleased = 0;
248 SERVER_START_REQ( event_op )
250 req->handle = handle;
251 req->op = RESET_EVENT;
252 ret = wine_server_call( req );
258 /******************************************************************************
259 * NtClearEvent (NTDLL.@)
262 * same as NtResetEvent ???
264 NTSTATUS WINAPI NtClearEvent ( HANDLE handle )
266 return NtResetEvent( handle, NULL );
269 /******************************************************************************
270 * NtPulseEvent (NTDLL.@)
275 NTSTATUS WINAPI NtPulseEvent( HANDLE handle, PULONG PulseCount )
280 FIXME("(%p,%ld)\n", handle, *PulseCount);
282 SERVER_START_REQ( event_op )
284 req->handle = handle;
285 req->op = PULSE_EVENT;
286 ret = wine_server_call( req );
292 /******************************************************************************
293 * NtQueryEvent (NTDLL.@)
295 NTSTATUS WINAPI NtQueryEvent (
296 IN HANDLE EventHandle,
297 IN UINT EventInformationClass,
298 OUT PVOID EventInformation,
299 IN ULONG EventInformationLength,
300 OUT PULONG ReturnLength)
302 FIXME("(%p)\n", EventHandle);
303 return STATUS_SUCCESS;
307 * Mutants (known as Mutexes in Kernel32)
310 /******************************************************************************
311 * NtCreateMutant [NTDLL.@]
312 * ZwCreateMutant [NTDLL.@]
314 NTSTATUS WINAPI NtCreateMutant(OUT HANDLE* MutantHandle,
315 IN ACCESS_MASK access,
316 IN const OBJECT_ATTRIBUTES* attr OPTIONAL,
317 IN BOOLEAN InitialOwner)
320 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
322 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
324 SERVER_START_REQ( create_mutex )
326 req->access = access;
327 req->owned = InitialOwner;
328 req->inherit = attr && (attr->Attributes & OBJ_INHERIT);
329 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
330 status = wine_server_call( req );
331 *MutantHandle = reply->handle;
337 /**************************************************************************
338 * NtOpenMutant [NTDLL.@]
339 * ZwOpenMutant [NTDLL.@]
341 NTSTATUS WINAPI NtOpenMutant(OUT HANDLE* MutantHandle,
342 IN ACCESS_MASK access,
343 IN const OBJECT_ATTRIBUTES* attr )
346 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
348 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
350 SERVER_START_REQ( open_mutex )
352 req->access = access;
353 req->inherit = attr && (attr->Attributes & OBJ_INHERIT);
354 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
355 status = wine_server_call( req );
356 *MutantHandle = reply->handle;
362 /**************************************************************************
363 * NtReleaseMutant [NTDLL.@]
364 * ZwReleaseMutant [NTDLL.@]
366 NTSTATUS WINAPI NtReleaseMutant( IN HANDLE handle, OUT PLONG prev_count OPTIONAL)
370 SERVER_START_REQ( release_mutex )
372 req->handle = handle;
373 status = wine_server_call( req );
374 if (prev_count) *prev_count = reply->prev_count;
380 /******************************************************************
381 * NtQueryMutant [NTDLL.@]
382 * ZwQueryMutant [NTDLL.@]
384 NTSTATUS WINAPI NtQueryMutant(IN HANDLE handle,
385 IN MUTANT_INFORMATION_CLASS MutantInformationClass,
386 OUT PVOID MutantInformation,
387 IN ULONG MutantInformationLength,
388 OUT PULONG ResultLength OPTIONAL )
390 FIXME("(%p %u %p %lu %p): stub!\n",
391 handle, MutantInformationClass, MutantInformation, MutantInformationLength, ResultLength);
392 return STATUS_NOT_IMPLEMENTED;
399 /**************************************************************************
400 * NtCreateTimer [NTDLL.@]
401 * ZwCreateTimer [NTDLL.@]
403 NTSTATUS WINAPI NtCreateTimer(OUT HANDLE *handle,
404 IN ACCESS_MASK access,
405 IN const OBJECT_ATTRIBUTES *attr OPTIONAL,
406 IN TIMER_TYPE timer_type)
408 DWORD len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
411 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
413 if (timer_type != NotificationTimer && timer_type != SynchronizationTimer)
414 return STATUS_INVALID_PARAMETER;
416 SERVER_START_REQ( create_timer )
418 req->access = access;
419 req->manual = (timer_type == NotificationTimer) ? TRUE : FALSE;
420 req->inherit = attr && (attr->Attributes & OBJ_INHERIT);
421 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
422 status = wine_server_call( req );
423 *handle = reply->handle;
430 /**************************************************************************
431 * NtOpenTimer [NTDLL.@]
432 * ZwOpenTimer [NTDLL.@]
434 NTSTATUS WINAPI NtOpenTimer(OUT PHANDLE handle,
435 IN ACCESS_MASK access,
436 IN const OBJECT_ATTRIBUTES* attr )
438 DWORD len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
441 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
443 SERVER_START_REQ( open_timer )
445 req->access = access;
446 req->inherit = attr && (attr->Attributes & OBJ_INHERIT);
447 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
448 status = wine_server_call( req );
449 *handle = reply->handle;
455 /**************************************************************************
456 * NtSetTimer [NTDLL.@]
457 * ZwSetTimer [NTDLL.@]
459 NTSTATUS WINAPI NtSetTimer(IN HANDLE handle,
460 IN const LARGE_INTEGER* when,
461 IN PTIMERAPCROUTINE callback,
462 IN PVOID callback_arg,
464 IN ULONG period OPTIONAL,
465 OUT PBOOLEAN state OPTIONAL)
467 NTSTATUS status = STATUS_SUCCESS;
469 TRACE("(%p,%p,%p,%p,%08x,0x%08lx,%p) stub\n",
470 handle, when, callback, callback_arg, resume, period, state);
472 SERVER_START_REQ( set_timer )
474 if (!when->u.LowPart && !when->u.HighPart)
476 /* special case to start timeout on now+period without too many calculations */
478 req->expire.usec = 0;
480 else NTDLL_get_server_timeout( &req->expire, when );
482 req->handle = handle;
483 req->period = period;
484 req->callback = callback;
485 req->arg = callback_arg;
486 status = wine_server_call( req );
487 if (state) *state = reply->signaled;
491 /* set error but can still succeed */
492 if (resume && status == STATUS_SUCCESS) return STATUS_TIMER_RESUME_IGNORED;
496 /**************************************************************************
497 * NtCancelTimer [NTDLL.@]
498 * ZwCancelTimer [NTDLL.@]
500 NTSTATUS WINAPI NtCancelTimer(IN HANDLE handle, OUT BOOLEAN* state)
504 SERVER_START_REQ( cancel_timer )
506 req->handle = handle;
507 status = wine_server_call( req );
508 if (state) *state = reply->signaled;
514 /******************************************************************************
515 * NtQueryTimer (NTDLL.@)
517 * Retrieves information about a timer.
520 * TimerHandle [I] The timer to retrieve information about.
521 * TimerInformationClass [I] The type of information to retrieve.
522 * TimerInformation [O] Pointer to buffer to store information in.
523 * Length [I] The length of the buffer pointed to by TimerInformation.
524 * ReturnLength [O] Optional. The size of buffer actually used.
527 * Success: STATUS_SUCCESS
528 * Failure: STATUS_INFO_LENGTH_MISMATCH, if Length doesn't match the required data
529 * size for the class specified.
530 * STATUS_INVALID_INFO_CLASS, if an invalid TimerInformationClass was specified.
531 * STATUS_ACCESS_DENIED, if TimerHandle does not have TIMER_QUERY_STATE access
534 NTSTATUS WINAPI NtQueryTimer(
536 TIMER_INFORMATION_CLASS TimerInformationClass,
537 PVOID TimerInformation,
541 TIMER_BASIC_INFORMATION * basic_info = (TIMER_BASIC_INFORMATION *)TimerInformation;
545 TRACE("(%p,%d,%p,0x%08lx,%p)\n", TimerHandle, TimerInformationClass,
546 TimerInformation, Length, ReturnLength);
548 switch (TimerInformationClass)
550 case TimerBasicInformation:
551 if (Length < sizeof(TIMER_BASIC_INFORMATION))
552 return STATUS_INFO_LENGTH_MISMATCH;
554 SERVER_START_REQ(get_timer_info)
556 req->handle = TimerHandle;
557 status = wine_server_call(req);
559 /* convert server time to absolute NTDLL time */
560 NTDLL_from_server_timeout(&basic_info->RemainingTime, &reply->when);
561 basic_info->TimerState = reply->signaled;
565 /* convert from absolute into relative time */
566 NtQuerySystemTime(&now);
567 if (now.QuadPart > basic_info->RemainingTime.QuadPart)
568 basic_info->RemainingTime.QuadPart = 0;
570 basic_info->RemainingTime.QuadPart -= now.QuadPart;
572 if (ReturnLength) *ReturnLength = sizeof(TIMER_BASIC_INFORMATION);
577 FIXME("Unhandled class %d\n", TimerInformationClass);
578 return STATUS_INVALID_INFO_CLASS;
582 /******************************************************************************
583 * NtQueryTimerResolution [NTDLL.@]
585 NTSTATUS WINAPI NtQueryTimerResolution(OUT ULONG* min_resolution,
586 OUT ULONG* max_resolution,
587 OUT ULONG* current_resolution)
589 FIXME("(%p,%p,%p), stub!\n",
590 min_resolution, max_resolution, current_resolution);
592 return STATUS_NOT_IMPLEMENTED;
595 /******************************************************************************
596 * NtSetTimerResolution [NTDLL.@]
598 NTSTATUS WINAPI NtSetTimerResolution(IN ULONG resolution,
599 IN BOOLEAN set_resolution,
600 OUT ULONG* current_resolution )
602 FIXME("(%lu,%u,%p), stub!\n",
603 resolution, set_resolution, current_resolution);
605 return STATUS_NOT_IMPLEMENTED;
609 /***********************************************************************
612 * Wait for a reply on the waiting pipe of the current thread.
614 static int wait_reply( void *cookie )
617 struct wake_up_reply reply;
621 ret = read( NtCurrentTeb()->wait_fd[0], &reply, sizeof(reply) );
622 if (ret == sizeof(reply))
624 if (!reply.cookie) break; /* thread got killed */
625 if (reply.cookie == cookie) return reply.signaled;
626 /* we stole another reply, wait for the real one */
627 signaled = wait_reply( cookie );
628 /* and now put the wrong one back in the pipe */
631 ret = write( NtCurrentTeb()->wait_fd[1], &reply, sizeof(reply) );
632 if (ret == sizeof(reply)) break;
633 if (ret >= 0) server_protocol_error( "partial wakeup write %d\n", ret );
634 if (errno == EINTR) continue;
635 server_protocol_perror("wakeup write");
639 if (ret >= 0) server_protocol_error( "partial wakeup read %d\n", ret );
640 if (errno == EINTR) continue;
641 server_protocol_perror("wakeup read");
643 /* the server closed the connection; time to die... */
644 server_abort_thread(0);
648 /***********************************************************************
651 * Call outstanding APCs.
653 static void call_apcs( BOOL alertable )
657 void *arg1, *arg2, *arg3;
662 SERVER_START_REQ( get_apc )
664 req->alertable = alertable;
665 if (!wine_server_call( req )) type = reply->type;
676 return; /* no more APCs */
678 proc( arg1, arg2, arg3 );
681 /* convert sec/usec to NT time */
682 RtlSecondsSince1970ToTime( (time_t)arg1, &time );
683 time.QuadPart += (DWORD)arg2 * 10;
684 proc( arg3, time.u.LowPart, time.u.HighPart );
687 NtCurrentTeb()->num_async_io--;
688 proc( arg1, (IO_STATUS_BLOCK*)arg2, (ULONG)arg3 );
691 server_protocol_error( "get_apc_request: bad type %d\n", type );
698 /***********************************************************************
699 * NTDLL_wait_for_multiple_objects
701 * Implementation of NtWaitForMultipleObjects
703 NTSTATUS NTDLL_wait_for_multiple_objects( UINT count, const HANDLE *handles, UINT flags,
704 const LARGE_INTEGER *timeout, HANDLE signal_object )
709 if (timeout) flags |= SELECT_TIMEOUT;
712 SERVER_START_REQ( select )
715 req->cookie = &cookie;
716 req->signal = signal_object;
717 NTDLL_get_server_timeout( &req->timeout, timeout );
718 wine_server_add_data( req, handles, count * sizeof(HANDLE) );
719 ret = wine_server_call( req );
722 if (ret == STATUS_PENDING) ret = wait_reply( &cookie );
723 if (ret != STATUS_USER_APC) break;
724 call_apcs( (flags & SELECT_ALERTABLE) != 0 );
725 if (flags & SELECT_ALERTABLE) break;
726 signal_object = 0; /* don't signal it multiple times */
729 /* A test on Windows 2000 shows that Windows always yields during
730 a wait, but a wait that is hit by an event gets a priority
731 boost as well. This seems to model that behavior the closest. */
732 if (ret == WAIT_TIMEOUT) NtYieldExecution();
738 /* wait operations */
740 /******************************************************************
741 * NtWaitForMultipleObjects (NTDLL.@)
743 NTSTATUS WINAPI NtWaitForMultipleObjects( DWORD count, const HANDLE *handles,
744 BOOLEAN wait_all, BOOLEAN alertable,
745 const LARGE_INTEGER *timeout )
747 UINT flags = SELECT_INTERRUPTIBLE;
749 if (!count || count > MAXIMUM_WAIT_OBJECTS) return STATUS_INVALID_PARAMETER_1;
751 if (wait_all) flags |= SELECT_ALL;
752 if (alertable) flags |= SELECT_ALERTABLE;
753 return NTDLL_wait_for_multiple_objects( count, handles, flags, timeout, 0 );
757 /******************************************************************
758 * NtWaitForSingleObject (NTDLL.@)
760 NTSTATUS WINAPI NtWaitForSingleObject(HANDLE handle, BOOLEAN alertable, const LARGE_INTEGER *timeout )
762 return NtWaitForMultipleObjects( 1, &handle, FALSE, alertable, timeout );
766 /******************************************************************
767 * NtSignalAndWaitForSingleObject (NTDLL.@)
769 NTSTATUS WINAPI NtSignalAndWaitForSingleObject( HANDLE hSignalObject, HANDLE hWaitObject,
770 BOOLEAN alertable, const LARGE_INTEGER *timeout )
772 UINT flags = SELECT_INTERRUPTIBLE;
774 if (!hSignalObject) return STATUS_INVALID_HANDLE;
775 if (alertable) flags |= SELECT_ALERTABLE;
776 return NTDLL_wait_for_multiple_objects( 1, &hWaitObject, flags, timeout, hSignalObject );
780 /******************************************************************
781 * NtYieldExecution (NTDLL.@)
783 NTSTATUS WINAPI NtYieldExecution(void)
785 #ifdef HAVE_SCHED_YIELD
787 return STATUS_SUCCESS;
789 return STATUS_NO_YIELD_PERFORMED;
794 /******************************************************************
795 * NtDelayExecution (NTDLL.@)
797 NTSTATUS WINAPI NtDelayExecution( BOOLEAN alertable, const LARGE_INTEGER *timeout )
799 /* if alertable or async I/O in progress, we need to query the server */
800 if (alertable || NtCurrentTeb()->num_async_io)
802 UINT flags = SELECT_INTERRUPTIBLE;
803 if (alertable) flags |= SELECT_ALERTABLE;
804 return NTDLL_wait_for_multiple_objects( 0, NULL, flags, timeout, 0 );
807 if (!timeout) /* sleep forever */
809 for (;;) select( 0, NULL, NULL, NULL, NULL );
815 NTDLL_get_server_timeout( &when, timeout );
817 /* Note that we yield after establishing the desired timeout */
823 gettimeofday( &tv, 0 );
824 tv.tv_sec = when.sec - tv.tv_sec;
825 if ((tv.tv_usec = when.usec - tv.tv_usec) < 0)
827 tv.tv_usec += 1000000;
830 /* if our yield already passed enough time, we're done */
831 if (tv.tv_sec < 0) break;
833 if (select( 0, NULL, NULL, NULL, &tv ) != -1) break;
836 return STATUS_SUCCESS;