- Send whole OBJECT_ATTRIBUTES.Attributes to the server not just an
[wine/hacks.git] / dlls / ntdll / sync.c
blob670da4d90d05f8dbd66032c521c2c23cdb24eb3b
1 /*
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
24 #include "config.h"
26 #include <assert.h>
27 #include <errno.h>
28 #include <signal.h>
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
31 #endif
32 #ifdef HAVE_POLL_H
33 #include <poll.h>
34 #endif
35 #ifdef HAVE_SYS_POLL_H
36 # include <sys/poll.h>
37 #endif
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41 #ifdef HAVE_SCHED_H
42 # include <sched.h>
43 #endif
44 #include <string.h>
45 #include <stdarg.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <time.h>
50 #define NONAMELESSUNION
51 #define NONAMELESSSTRUCT
53 #include "windef.h"
54 #include "thread.h"
55 #include "wine/server.h"
56 #include "wine/debug.h"
57 #include "ntdll_misc.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
63 * Semaphores
66 /******************************************************************************
67 * NtCreateSemaphore (NTDLL.@)
69 NTSTATUS WINAPI NtCreateSemaphore( OUT PHANDLE SemaphoreHandle,
70 IN ACCESS_MASK access,
71 IN const OBJECT_ATTRIBUTES *attr OPTIONAL,
72 IN LONG InitialCount,
73 IN LONG MaximumCount )
75 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
76 NTSTATUS ret;
78 if (MaximumCount <= 0 || InitialCount < 0 || InitialCount > MaximumCount)
79 return STATUS_INVALID_PARAMETER;
80 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
82 SERVER_START_REQ( create_semaphore )
84 req->access = access;
85 req->attributes = (attr) ? attr->Attributes : 0;
86 req->initial = InitialCount;
87 req->max = MaximumCount;
88 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
89 ret = wine_server_call( req );
90 *SemaphoreHandle = reply->handle;
92 SERVER_END_REQ;
93 return ret;
96 /******************************************************************************
97 * NtOpenSemaphore (NTDLL.@)
99 NTSTATUS WINAPI NtOpenSemaphore( OUT PHANDLE SemaphoreHandle,
100 IN ACCESS_MASK access,
101 IN const OBJECT_ATTRIBUTES *attr )
103 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
104 NTSTATUS ret;
106 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
108 SERVER_START_REQ( open_semaphore )
110 req->access = access;
111 req->attributes = (attr) ? attr->Attributes : 0;
112 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
113 ret = wine_server_call( req );
114 *SemaphoreHandle = reply->handle;
116 SERVER_END_REQ;
117 return ret;
120 /******************************************************************************
121 * NtQuerySemaphore (NTDLL.@)
123 NTSTATUS WINAPI NtQuerySemaphore(
124 HANDLE SemaphoreHandle,
125 SEMAPHORE_INFORMATION_CLASS SemaphoreInformationClass,
126 PVOID SemaphoreInformation,
127 ULONG Length,
128 PULONG ReturnLength)
130 FIXME("(%p,%d,%p,0x%08lx,%p) stub!\n",
131 SemaphoreHandle, SemaphoreInformationClass, SemaphoreInformation, Length, ReturnLength);
132 return STATUS_SUCCESS;
135 /******************************************************************************
136 * NtReleaseSemaphore (NTDLL.@)
138 NTSTATUS WINAPI NtReleaseSemaphore( HANDLE handle, ULONG count, PULONG previous )
140 NTSTATUS ret;
141 SERVER_START_REQ( release_semaphore )
143 req->handle = handle;
144 req->count = count;
145 if (!(ret = wine_server_call( req )))
147 if (previous) *previous = reply->prev_count;
150 SERVER_END_REQ;
151 return ret;
155 * Events
158 /**************************************************************************
159 * NtCreateEvent (NTDLL.@)
160 * ZwCreateEvent (NTDLL.@)
162 NTSTATUS WINAPI NtCreateEvent(
163 OUT PHANDLE EventHandle,
164 IN ACCESS_MASK DesiredAccess,
165 IN const OBJECT_ATTRIBUTES *attr,
166 IN BOOLEAN ManualReset,
167 IN BOOLEAN InitialState)
169 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
170 NTSTATUS ret;
172 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
174 SERVER_START_REQ( create_event )
176 req->access = DesiredAccess;
177 req->attributes = (attr) ? attr->Attributes : 0;
178 req->manual_reset = ManualReset;
179 req->initial_state = InitialState;
180 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
181 ret = wine_server_call( req );
182 *EventHandle = reply->handle;
184 SERVER_END_REQ;
185 return ret;
188 /******************************************************************************
189 * NtOpenEvent (NTDLL.@)
190 * ZwOpenEvent (NTDLL.@)
192 NTSTATUS WINAPI NtOpenEvent(
193 OUT PHANDLE EventHandle,
194 IN ACCESS_MASK DesiredAccess,
195 IN const OBJECT_ATTRIBUTES *attr )
197 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
198 NTSTATUS ret;
200 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
202 SERVER_START_REQ( open_event )
204 req->access = DesiredAccess;
205 req->attributes = (attr) ? attr->Attributes : 0;
206 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
207 ret = wine_server_call( req );
208 *EventHandle = reply->handle;
210 SERVER_END_REQ;
211 return ret;
215 /******************************************************************************
216 * NtSetEvent (NTDLL.@)
217 * ZwSetEvent (NTDLL.@)
219 NTSTATUS WINAPI NtSetEvent( HANDLE handle, PULONG NumberOfThreadsReleased )
221 NTSTATUS ret;
223 /* FIXME: set NumberOfThreadsReleased */
225 SERVER_START_REQ( event_op )
227 req->handle = handle;
228 req->op = SET_EVENT;
229 ret = wine_server_call( req );
231 SERVER_END_REQ;
232 return ret;
235 /******************************************************************************
236 * NtResetEvent (NTDLL.@)
238 NTSTATUS WINAPI NtResetEvent( HANDLE handle, PULONG NumberOfThreadsReleased )
240 NTSTATUS ret;
242 /* resetting an event can't release any thread... */
243 if (NumberOfThreadsReleased) *NumberOfThreadsReleased = 0;
245 SERVER_START_REQ( event_op )
247 req->handle = handle;
248 req->op = RESET_EVENT;
249 ret = wine_server_call( req );
251 SERVER_END_REQ;
252 return ret;
255 /******************************************************************************
256 * NtClearEvent (NTDLL.@)
258 * FIXME
259 * same as NtResetEvent ???
261 NTSTATUS WINAPI NtClearEvent ( HANDLE handle )
263 return NtResetEvent( handle, NULL );
266 /******************************************************************************
267 * NtPulseEvent (NTDLL.@)
269 * FIXME
270 * PulseCount
272 NTSTATUS WINAPI NtPulseEvent( HANDLE handle, PULONG PulseCount )
274 NTSTATUS ret;
276 if (PulseCount)
277 FIXME("(%p,%ld)\n", handle, *PulseCount);
279 SERVER_START_REQ( event_op )
281 req->handle = handle;
282 req->op = PULSE_EVENT;
283 ret = wine_server_call( req );
285 SERVER_END_REQ;
286 return ret;
289 /******************************************************************************
290 * NtQueryEvent (NTDLL.@)
292 NTSTATUS WINAPI NtQueryEvent (
293 IN HANDLE EventHandle,
294 IN UINT EventInformationClass,
295 OUT PVOID EventInformation,
296 IN ULONG EventInformationLength,
297 OUT PULONG ReturnLength)
299 FIXME("(%p)\n", EventHandle);
300 return STATUS_SUCCESS;
304 * Mutants (known as Mutexes in Kernel32)
307 /******************************************************************************
308 * NtCreateMutant [NTDLL.@]
309 * ZwCreateMutant [NTDLL.@]
311 NTSTATUS WINAPI NtCreateMutant(OUT HANDLE* MutantHandle,
312 IN ACCESS_MASK access,
313 IN const OBJECT_ATTRIBUTES* attr OPTIONAL,
314 IN BOOLEAN InitialOwner)
316 NTSTATUS status;
317 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
319 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
321 SERVER_START_REQ( create_mutex )
323 req->access = access;
324 req->attributes = (attr) ? attr->Attributes : 0;
325 req->owned = InitialOwner;
326 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
327 status = wine_server_call( req );
328 *MutantHandle = reply->handle;
330 SERVER_END_REQ;
331 return status;
334 /**************************************************************************
335 * NtOpenMutant [NTDLL.@]
336 * ZwOpenMutant [NTDLL.@]
338 NTSTATUS WINAPI NtOpenMutant(OUT HANDLE* MutantHandle,
339 IN ACCESS_MASK access,
340 IN const OBJECT_ATTRIBUTES* attr )
342 NTSTATUS status;
343 DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
345 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
347 SERVER_START_REQ( open_mutex )
349 req->access = access;
350 req->attributes = (attr) ? attr->Attributes : 0;
351 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
352 status = wine_server_call( req );
353 *MutantHandle = reply->handle;
355 SERVER_END_REQ;
356 return status;
359 /**************************************************************************
360 * NtReleaseMutant [NTDLL.@]
361 * ZwReleaseMutant [NTDLL.@]
363 NTSTATUS WINAPI NtReleaseMutant( IN HANDLE handle, OUT PLONG prev_count OPTIONAL)
365 NTSTATUS status;
367 SERVER_START_REQ( release_mutex )
369 req->handle = handle;
370 status = wine_server_call( req );
371 if (prev_count) *prev_count = reply->prev_count;
373 SERVER_END_REQ;
374 return status;
377 /******************************************************************
378 * NtQueryMutant [NTDLL.@]
379 * ZwQueryMutant [NTDLL.@]
381 NTSTATUS WINAPI NtQueryMutant(IN HANDLE handle,
382 IN MUTANT_INFORMATION_CLASS MutantInformationClass,
383 OUT PVOID MutantInformation,
384 IN ULONG MutantInformationLength,
385 OUT PULONG ResultLength OPTIONAL )
387 FIXME("(%p %u %p %lu %p): stub!\n",
388 handle, MutantInformationClass, MutantInformation, MutantInformationLength, ResultLength);
389 return STATUS_NOT_IMPLEMENTED;
393 * Timers
396 /**************************************************************************
397 * NtCreateTimer [NTDLL.@]
398 * ZwCreateTimer [NTDLL.@]
400 NTSTATUS WINAPI NtCreateTimer(OUT HANDLE *handle,
401 IN ACCESS_MASK access,
402 IN const OBJECT_ATTRIBUTES *attr OPTIONAL,
403 IN TIMER_TYPE timer_type)
405 DWORD len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
406 NTSTATUS status;
408 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
410 if (timer_type != NotificationTimer && timer_type != SynchronizationTimer)
411 return STATUS_INVALID_PARAMETER;
413 SERVER_START_REQ( create_timer )
415 req->access = access;
416 req->attributes = (attr) ? attr->Attributes : 0;
417 req->manual = (timer_type == NotificationTimer) ? TRUE : FALSE;
418 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
419 status = wine_server_call( req );
420 *handle = reply->handle;
422 SERVER_END_REQ;
423 return status;
427 /**************************************************************************
428 * NtOpenTimer [NTDLL.@]
429 * ZwOpenTimer [NTDLL.@]
431 NTSTATUS WINAPI NtOpenTimer(OUT PHANDLE handle,
432 IN ACCESS_MASK access,
433 IN const OBJECT_ATTRIBUTES* attr )
435 DWORD len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
436 NTSTATUS status;
438 if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
440 SERVER_START_REQ( open_timer )
442 req->access = access;
443 req->attributes = (attr) ? attr->Attributes : 0;
444 if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
445 status = wine_server_call( req );
446 *handle = reply->handle;
448 SERVER_END_REQ;
449 return status;
452 /**************************************************************************
453 * NtSetTimer [NTDLL.@]
454 * ZwSetTimer [NTDLL.@]
456 NTSTATUS WINAPI NtSetTimer(IN HANDLE handle,
457 IN const LARGE_INTEGER* when,
458 IN PTIMER_APC_ROUTINE callback,
459 IN PVOID callback_arg,
460 IN BOOLEAN resume,
461 IN ULONG period OPTIONAL,
462 OUT PBOOLEAN state OPTIONAL)
464 NTSTATUS status = STATUS_SUCCESS;
466 TRACE("(%p,%p,%p,%p,%08x,0x%08lx,%p) stub\n",
467 handle, when, callback, callback_arg, resume, period, state);
469 SERVER_START_REQ( set_timer )
471 if (!when->u.LowPart && !when->u.HighPart)
473 /* special case to start timeout on now+period without too many calculations */
474 req->expire.sec = 0;
475 req->expire.usec = 0;
477 else NTDLL_get_server_timeout( &req->expire, when );
479 req->handle = handle;
480 req->period = period;
481 req->callback = callback;
482 req->arg = callback_arg;
483 status = wine_server_call( req );
484 if (state) *state = reply->signaled;
486 SERVER_END_REQ;
488 /* set error but can still succeed */
489 if (resume && status == STATUS_SUCCESS) return STATUS_TIMER_RESUME_IGNORED;
490 return status;
493 /**************************************************************************
494 * NtCancelTimer [NTDLL.@]
495 * ZwCancelTimer [NTDLL.@]
497 NTSTATUS WINAPI NtCancelTimer(IN HANDLE handle, OUT BOOLEAN* state)
499 NTSTATUS status;
501 SERVER_START_REQ( cancel_timer )
503 req->handle = handle;
504 status = wine_server_call( req );
505 if (state) *state = reply->signaled;
507 SERVER_END_REQ;
508 return status;
511 /******************************************************************************
512 * NtQueryTimer (NTDLL.@)
514 * Retrieves information about a timer.
516 * PARAMS
517 * TimerHandle [I] The timer to retrieve information about.
518 * TimerInformationClass [I] The type of information to retrieve.
519 * TimerInformation [O] Pointer to buffer to store information in.
520 * Length [I] The length of the buffer pointed to by TimerInformation.
521 * ReturnLength [O] Optional. The size of buffer actually used.
523 * RETURNS
524 * Success: STATUS_SUCCESS
525 * Failure: STATUS_INFO_LENGTH_MISMATCH, if Length doesn't match the required data
526 * size for the class specified.
527 * STATUS_INVALID_INFO_CLASS, if an invalid TimerInformationClass was specified.
528 * STATUS_ACCESS_DENIED, if TimerHandle does not have TIMER_QUERY_STATE access
529 * to the timer.
531 NTSTATUS WINAPI NtQueryTimer(
532 HANDLE TimerHandle,
533 TIMER_INFORMATION_CLASS TimerInformationClass,
534 PVOID TimerInformation,
535 ULONG Length,
536 PULONG ReturnLength)
538 TIMER_BASIC_INFORMATION * basic_info = (TIMER_BASIC_INFORMATION *)TimerInformation;
539 NTSTATUS status;
540 LARGE_INTEGER now;
542 TRACE("(%p,%d,%p,0x%08lx,%p)\n", TimerHandle, TimerInformationClass,
543 TimerInformation, Length, ReturnLength);
545 switch (TimerInformationClass)
547 case TimerBasicInformation:
548 if (Length < sizeof(TIMER_BASIC_INFORMATION))
549 return STATUS_INFO_LENGTH_MISMATCH;
551 SERVER_START_REQ(get_timer_info)
553 req->handle = TimerHandle;
554 status = wine_server_call(req);
556 /* convert server time to absolute NTDLL time */
557 NTDLL_from_server_timeout(&basic_info->RemainingTime, &reply->when);
558 basic_info->TimerState = reply->signaled;
560 SERVER_END_REQ;
562 /* convert from absolute into relative time */
563 NtQuerySystemTime(&now);
564 if (now.QuadPart > basic_info->RemainingTime.QuadPart)
565 basic_info->RemainingTime.QuadPart = 0;
566 else
567 basic_info->RemainingTime.QuadPart -= now.QuadPart;
569 if (ReturnLength) *ReturnLength = sizeof(TIMER_BASIC_INFORMATION);
571 return status;
574 FIXME("Unhandled class %d\n", TimerInformationClass);
575 return STATUS_INVALID_INFO_CLASS;
579 /******************************************************************************
580 * NtQueryTimerResolution [NTDLL.@]
582 NTSTATUS WINAPI NtQueryTimerResolution(OUT ULONG* min_resolution,
583 OUT ULONG* max_resolution,
584 OUT ULONG* current_resolution)
586 FIXME("(%p,%p,%p), stub!\n",
587 min_resolution, max_resolution, current_resolution);
589 return STATUS_NOT_IMPLEMENTED;
592 /******************************************************************************
593 * NtSetTimerResolution [NTDLL.@]
595 NTSTATUS WINAPI NtSetTimerResolution(IN ULONG resolution,
596 IN BOOLEAN set_resolution,
597 OUT ULONG* current_resolution )
599 FIXME("(%lu,%u,%p), stub!\n",
600 resolution, set_resolution, current_resolution);
602 return STATUS_NOT_IMPLEMENTED;
606 /***********************************************************************
607 * wait_reply
609 * Wait for a reply on the waiting pipe of the current thread.
611 static int wait_reply( void *cookie )
613 int signaled;
614 struct wake_up_reply reply;
615 for (;;)
617 int ret;
618 ret = read( ntdll_get_thread_data()->wait_fd[0], &reply, sizeof(reply) );
619 if (ret == sizeof(reply))
621 if (!reply.cookie) break; /* thread got killed */
622 if (reply.cookie == cookie) return reply.signaled;
623 /* we stole another reply, wait for the real one */
624 signaled = wait_reply( cookie );
625 /* and now put the wrong one back in the pipe */
626 for (;;)
628 ret = write( ntdll_get_thread_data()->wait_fd[1], &reply, sizeof(reply) );
629 if (ret == sizeof(reply)) break;
630 if (ret >= 0) server_protocol_error( "partial wakeup write %d\n", ret );
631 if (errno == EINTR) continue;
632 server_protocol_perror("wakeup write");
634 return signaled;
636 if (ret >= 0) server_protocol_error( "partial wakeup read %d\n", ret );
637 if (errno == EINTR) continue;
638 server_protocol_perror("wakeup read");
640 /* the server closed the connection; time to die... */
641 server_abort_thread(0);
645 /***********************************************************************
646 * call_apcs
648 * Call outstanding APCs.
650 static void call_apcs( BOOL alertable )
652 FARPROC proc;
653 LARGE_INTEGER time;
654 void *arg1, *arg2, *arg3;
656 for (;;)
658 int type = APC_NONE;
659 SERVER_START_REQ( get_apc )
661 req->alertable = alertable;
662 if (!wine_server_call( req )) type = reply->type;
663 proc = reply->func;
664 arg1 = reply->arg1;
665 arg2 = reply->arg2;
666 arg3 = reply->arg3;
668 SERVER_END_REQ;
670 switch (type)
672 case APC_NONE:
673 return; /* no more APCs */
674 case APC_USER:
675 proc( arg1, arg2, arg3 );
676 break;
677 case APC_TIMER:
678 /* convert sec/usec to NT time */
679 RtlSecondsSince1970ToTime( (time_t)arg1, &time );
680 time.QuadPart += (DWORD)arg2 * 10;
681 proc( arg3, time.u.LowPart, time.u.HighPart );
682 break;
683 case APC_ASYNC_IO:
684 NtCurrentTeb()->num_async_io--;
685 proc( arg1, (IO_STATUS_BLOCK*)arg2, (ULONG)arg3 );
686 break;
687 default:
688 server_protocol_error( "get_apc_request: bad type %d\n", type );
689 break;
695 /***********************************************************************
696 * NTDLL_wait_for_multiple_objects
698 * Implementation of NtWaitForMultipleObjects
700 NTSTATUS NTDLL_wait_for_multiple_objects( UINT count, const HANDLE *handles, UINT flags,
701 const LARGE_INTEGER *timeout, HANDLE signal_object )
703 NTSTATUS ret;
704 int cookie;
706 if (timeout) flags |= SELECT_TIMEOUT;
707 for (;;)
709 SERVER_START_REQ( select )
711 req->flags = flags;
712 req->cookie = &cookie;
713 req->signal = signal_object;
714 NTDLL_get_server_timeout( &req->timeout, timeout );
715 wine_server_add_data( req, handles, count * sizeof(HANDLE) );
716 ret = wine_server_call( req );
718 SERVER_END_REQ;
719 if (ret == STATUS_PENDING) ret = wait_reply( &cookie );
720 if (ret != STATUS_USER_APC) break;
721 call_apcs( (flags & SELECT_ALERTABLE) != 0 );
722 if (flags & SELECT_ALERTABLE) break;
723 signal_object = 0; /* don't signal it multiple times */
726 /* A test on Windows 2000 shows that Windows always yields during
727 a wait, but a wait that is hit by an event gets a priority
728 boost as well. This seems to model that behavior the closest. */
729 if (ret == WAIT_TIMEOUT) NtYieldExecution();
731 return ret;
735 /* wait operations */
737 /******************************************************************
738 * NtWaitForMultipleObjects (NTDLL.@)
740 NTSTATUS WINAPI NtWaitForMultipleObjects( DWORD count, const HANDLE *handles,
741 BOOLEAN wait_all, BOOLEAN alertable,
742 const LARGE_INTEGER *timeout )
744 UINT flags = SELECT_INTERRUPTIBLE;
746 if (!count || count > MAXIMUM_WAIT_OBJECTS) return STATUS_INVALID_PARAMETER_1;
748 if (wait_all) flags |= SELECT_ALL;
749 if (alertable) flags |= SELECT_ALERTABLE;
750 return NTDLL_wait_for_multiple_objects( count, handles, flags, timeout, 0 );
754 /******************************************************************
755 * NtWaitForSingleObject (NTDLL.@)
757 NTSTATUS WINAPI NtWaitForSingleObject(HANDLE handle, BOOLEAN alertable, const LARGE_INTEGER *timeout )
759 return NtWaitForMultipleObjects( 1, &handle, FALSE, alertable, timeout );
763 /******************************************************************
764 * NtSignalAndWaitForSingleObject (NTDLL.@)
766 NTSTATUS WINAPI NtSignalAndWaitForSingleObject( HANDLE hSignalObject, HANDLE hWaitObject,
767 BOOLEAN alertable, const LARGE_INTEGER *timeout )
769 UINT flags = SELECT_INTERRUPTIBLE;
771 if (!hSignalObject) return STATUS_INVALID_HANDLE;
772 if (alertable) flags |= SELECT_ALERTABLE;
773 return NTDLL_wait_for_multiple_objects( 1, &hWaitObject, flags, timeout, hSignalObject );
777 /******************************************************************
778 * NtYieldExecution (NTDLL.@)
780 NTSTATUS WINAPI NtYieldExecution(void)
782 #ifdef HAVE_SCHED_YIELD
783 sched_yield();
784 return STATUS_SUCCESS;
785 #else
786 return STATUS_NO_YIELD_PERFORMED;
787 #endif
791 /******************************************************************
792 * NtDelayExecution (NTDLL.@)
794 NTSTATUS WINAPI NtDelayExecution( BOOLEAN alertable, const LARGE_INTEGER *timeout )
796 /* if alertable or async I/O in progress, we need to query the server */
797 if (alertable || NtCurrentTeb()->num_async_io)
799 UINT flags = SELECT_INTERRUPTIBLE;
800 if (alertable) flags |= SELECT_ALERTABLE;
801 return NTDLL_wait_for_multiple_objects( 0, NULL, flags, timeout, 0 );
804 if (!timeout) /* sleep forever */
806 for (;;) select( 0, NULL, NULL, NULL, NULL );
808 else
810 abs_time_t when;
812 NTDLL_get_server_timeout( &when, timeout );
814 /* Note that we yield after establishing the desired timeout */
815 NtYieldExecution();
817 for (;;)
819 struct timeval tv;
820 gettimeofday( &tv, 0 );
821 tv.tv_sec = when.sec - tv.tv_sec;
822 if ((tv.tv_usec = when.usec - tv.tv_usec) < 0)
824 tv.tv_usec += 1000000;
825 tv.tv_sec--;
827 /* if our yield already passed enough time, we're done */
828 if (tv.tv_sec < 0) break;
830 if (select( 0, NULL, NULL, NULL, &tv ) != -1) break;
833 return STATUS_SUCCESS;