Prefix the internal name of all register functions with __regs_ for
[wine] / dlls / ntdll / sync.c
1 /*
2  *      Process synchronisation
3  *
4  * Copyright 1996, 1997, 1998 Marcus Meissner
5  * Copyright 1997, 1999 Alexandre Julliard
6  * Copyright 1999, 2000 Juergen Schmied
7  * Copyright 2003 Eric Pouech
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "config.h"
25
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>
49
50 #define NONAMELESSUNION
51 #define NONAMELESSSTRUCT
52
53 #include "windef.h"
54 #include "winbase.h"
55 #include "winreg.h"
56 #include "winternl.h"
57 #include "thread.h"
58 #include "wine/server.h"
59 #include "wine/debug.h"
60 #include "ntdll_misc.h"
61
62 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
63
64
65 /*
66  *      Semaphores
67  */
68
69 /******************************************************************************
70  *  NtCreateSemaphore (NTDLL.@)
71  */
72 NTSTATUS WINAPI NtCreateSemaphore( OUT PHANDLE SemaphoreHandle,
73                                    IN ACCESS_MASK access,
74                                    IN const OBJECT_ATTRIBUTES *attr OPTIONAL,
75                                    IN LONG InitialCount,
76                                    IN LONG MaximumCount )
77 {
78     DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
79     NTSTATUS ret;
80
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;
84
85     SERVER_START_REQ( create_semaphore )
86     {
87         req->access  = access;
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;
94     }
95     SERVER_END_REQ;
96     return ret;
97 }
98
99 /******************************************************************************
100  *  NtOpenSemaphore (NTDLL.@)
101  */
102 NTSTATUS WINAPI NtOpenSemaphore( OUT PHANDLE SemaphoreHandle,
103                                  IN ACCESS_MASK access,
104                                  IN const OBJECT_ATTRIBUTES *attr )
105 {
106     DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
107     NTSTATUS ret;
108
109     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
110
111     SERVER_START_REQ( open_semaphore )
112     {
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;
118     }
119     SERVER_END_REQ;
120     return ret;
121 }
122
123 /******************************************************************************
124  *  NtQuerySemaphore (NTDLL.@)
125  */
126 NTSTATUS WINAPI NtQuerySemaphore(
127         HANDLE SemaphoreHandle,
128         PVOID SemaphoreInformationClass,
129         OUT PVOID SemaphoreInformation,
130         ULONG Length,
131         PULONG ReturnLength)
132 {
133         FIXME("(%p,%p,%p,0x%08lx,%p) stub!\n",
134         SemaphoreHandle, SemaphoreInformationClass, SemaphoreInformation, Length, ReturnLength);
135         return STATUS_SUCCESS;
136 }
137
138 /******************************************************************************
139  *  NtReleaseSemaphore (NTDLL.@)
140  */
141 NTSTATUS WINAPI NtReleaseSemaphore( HANDLE handle, ULONG count, PULONG previous )
142 {
143     NTSTATUS ret;
144     SERVER_START_REQ( release_semaphore )
145     {
146         req->handle = handle;
147         req->count  = count;
148         if (!(ret = wine_server_call( req )))
149         {
150             if (previous) *previous = reply->prev_count;
151         }
152     }
153     SERVER_END_REQ;
154     return ret;
155 }
156
157 /*
158  *      Events
159  */
160
161 /**************************************************************************
162  * NtCreateEvent (NTDLL.@)
163  * ZwCreateEvent (NTDLL.@)
164  */
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)
171 {
172     DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
173     NTSTATUS ret;
174
175     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
176
177     SERVER_START_REQ( create_event )
178     {
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;
186     }
187     SERVER_END_REQ;
188     return ret;
189 }
190
191 /******************************************************************************
192  *  NtOpenEvent (NTDLL.@)
193  *  ZwOpenEvent (NTDLL.@)
194  */
195 NTSTATUS WINAPI NtOpenEvent(
196         OUT PHANDLE EventHandle,
197         IN ACCESS_MASK DesiredAccess,
198         IN const OBJECT_ATTRIBUTES *attr )
199 {
200     DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
201     NTSTATUS ret;
202
203     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
204
205     SERVER_START_REQ( open_event )
206     {
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;
212     }
213     SERVER_END_REQ;
214     return ret;
215 }
216
217
218 /******************************************************************************
219  *  NtSetEvent (NTDLL.@)
220  *  ZwSetEvent (NTDLL.@)
221  */
222 NTSTATUS WINAPI NtSetEvent( HANDLE handle, PULONG NumberOfThreadsReleased )
223 {
224     NTSTATUS ret;
225
226     /* FIXME: set NumberOfThreadsReleased */
227
228     SERVER_START_REQ( event_op )
229     {
230         req->handle = handle;
231         req->op     = SET_EVENT;
232         ret = wine_server_call( req );
233     }
234     SERVER_END_REQ;
235     return ret;
236 }
237
238 /******************************************************************************
239  *  NtResetEvent (NTDLL.@)
240  */
241 NTSTATUS WINAPI NtResetEvent( HANDLE handle, PULONG NumberOfThreadsReleased )
242 {
243     NTSTATUS ret;
244
245     /* resetting an event can't release any thread... */
246     if (NumberOfThreadsReleased) *NumberOfThreadsReleased = 0;
247
248     SERVER_START_REQ( event_op )
249     {
250         req->handle = handle;
251         req->op     = RESET_EVENT;
252         ret = wine_server_call( req );
253     }
254     SERVER_END_REQ;
255     return ret;
256 }
257
258 /******************************************************************************
259  *  NtClearEvent (NTDLL.@)
260  *
261  * FIXME
262  *   same as NtResetEvent ???
263  */
264 NTSTATUS WINAPI NtClearEvent ( HANDLE handle )
265 {
266     return NtResetEvent( handle, NULL );
267 }
268
269 /******************************************************************************
270  *  NtPulseEvent (NTDLL.@)
271  *
272  * FIXME
273  *   PulseCount
274  */
275 NTSTATUS WINAPI NtPulseEvent( HANDLE handle, PULONG PulseCount )
276 {
277     NTSTATUS ret;
278
279     if (PulseCount)
280       FIXME("(%p,%ld)\n", handle, *PulseCount);
281
282     SERVER_START_REQ( event_op )
283     {
284         req->handle = handle;
285         req->op     = PULSE_EVENT;
286         ret = wine_server_call( req );
287     }
288     SERVER_END_REQ;
289     return ret;
290 }
291
292 /******************************************************************************
293  *  NtQueryEvent (NTDLL.@)
294  */
295 NTSTATUS WINAPI NtQueryEvent (
296         IN  HANDLE EventHandle,
297         IN  UINT EventInformationClass,
298         OUT PVOID EventInformation,
299         IN  ULONG EventInformationLength,
300         OUT PULONG  ReturnLength)
301 {
302         FIXME("(%p)\n", EventHandle);
303         return STATUS_SUCCESS;
304 }
305
306 /*
307  *      Mutants (known as Mutexes in Kernel32)
308  */
309
310 /******************************************************************************
311  *              NtCreateMutant                          [NTDLL.@]
312  *              ZwCreateMutant                          [NTDLL.@]
313  */
314 NTSTATUS WINAPI NtCreateMutant(OUT HANDLE* MutantHandle,
315                                IN ACCESS_MASK access,
316                                IN const OBJECT_ATTRIBUTES* attr OPTIONAL,
317                                IN BOOLEAN InitialOwner)
318 {
319     NTSTATUS    status;
320     DWORD       len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
321
322     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
323
324     SERVER_START_REQ( create_mutex )
325     {
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;
332     }
333     SERVER_END_REQ;
334     return status;
335 }
336
337 /**************************************************************************
338  *              NtOpenMutant                            [NTDLL.@]
339  *              ZwOpenMutant                            [NTDLL.@]
340  */
341 NTSTATUS WINAPI NtOpenMutant(OUT HANDLE* MutantHandle, 
342                              IN ACCESS_MASK access, 
343                              IN const OBJECT_ATTRIBUTES* attr )
344 {
345     NTSTATUS    status;
346     DWORD       len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
347
348     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
349
350     SERVER_START_REQ( open_mutex )
351     {
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;
357     }
358     SERVER_END_REQ;
359     return status;
360 }
361
362 /**************************************************************************
363  *              NtReleaseMutant                         [NTDLL.@]
364  *              ZwReleaseMutant                         [NTDLL.@]
365  */
366 NTSTATUS WINAPI NtReleaseMutant( IN HANDLE handle, OUT PLONG prev_count OPTIONAL)
367 {
368     NTSTATUS    status;
369
370     SERVER_START_REQ( release_mutex )
371     {
372         req->handle = handle;
373         status = wine_server_call( req );
374         if (prev_count) *prev_count = reply->prev_count;
375     }
376     SERVER_END_REQ;
377     return status;
378 }
379
380 /******************************************************************
381  *              NtQueryMutant                   [NTDLL.@]
382  *              ZwQueryMutant                   [NTDLL.@]
383  */
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 )
389 {
390     FIXME("(%p %u %p %lu %p): stub!\n", 
391           handle, MutantInformationClass, MutantInformation, MutantInformationLength, ResultLength);
392     return STATUS_NOT_IMPLEMENTED;
393 }
394
395 /*
396  *      Timers
397  */
398
399 /**************************************************************************
400  *              NtCreateTimer                           [NTDLL.@]
401  *              ZwCreateTimer                           [NTDLL.@]
402  */
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)
407 {
408     DWORD       len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
409     NTSTATUS    status;
410
411     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
412
413     if (timer_type != NotificationTimer && timer_type != SynchronizationTimer)
414         return STATUS_INVALID_PARAMETER;
415
416     SERVER_START_REQ( create_timer )
417     {
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;
424     }
425     SERVER_END_REQ;
426     return status;
427
428 }
429
430 /**************************************************************************
431  *              NtOpenTimer                             [NTDLL.@]
432  *              ZwOpenTimer                             [NTDLL.@]
433  */
434 NTSTATUS WINAPI NtOpenTimer(OUT PHANDLE handle,
435                             IN ACCESS_MASK access,
436                             IN const OBJECT_ATTRIBUTES* attr )
437 {
438     DWORD       len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
439     NTSTATUS    status;
440
441     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
442
443     SERVER_START_REQ( open_timer )
444     {
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;
450     }
451     SERVER_END_REQ;
452     return status;
453 }
454
455 /**************************************************************************
456  *              NtSetTimer                              [NTDLL.@]
457  *              ZwSetTimer                              [NTDLL.@]
458  */
459 NTSTATUS WINAPI NtSetTimer(IN HANDLE handle,
460                            IN const LARGE_INTEGER* when,
461                            IN PTIMERAPCROUTINE callback,
462                            IN PVOID callback_arg,
463                            IN BOOLEAN resume,
464                            IN ULONG period OPTIONAL,
465                            OUT PBOOLEAN state OPTIONAL)
466 {
467     NTSTATUS    status = STATUS_SUCCESS;
468
469     TRACE("(%p,%p,%p,%p,%08x,0x%08lx,%p) stub\n",
470           handle, when, callback, callback_arg, resume, period, state);
471
472     SERVER_START_REQ( set_timer )
473     {
474         if (!when->u.LowPart && !when->u.HighPart)
475         {
476             /* special case to start timeout on now+period without too many calculations */
477             req->expire.sec  = 0;
478             req->expire.usec = 0;
479         }
480         else NTDLL_get_server_timeout( &req->expire, when );
481
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;
488     }
489     SERVER_END_REQ;
490
491     /* set error but can still succeed */
492     if (resume && status == STATUS_SUCCESS) return STATUS_TIMER_RESUME_IGNORED;
493     return status;
494 }
495
496 /**************************************************************************
497  *              NtCancelTimer                           [NTDLL.@]
498  *              ZwCancelTimer                           [NTDLL.@]
499  */
500 NTSTATUS WINAPI NtCancelTimer(IN HANDLE handle, OUT BOOLEAN* state)
501 {
502     NTSTATUS    status;
503
504     SERVER_START_REQ( cancel_timer )
505     {
506         req->handle = handle;
507         status = wine_server_call( req );
508         if (state) *state = reply->signaled;
509     }
510     SERVER_END_REQ;
511     return status;
512 }
513
514 /******************************************************************************
515  *  NtQueryTimer (NTDLL.@)
516  *
517  * Retrieves information about a timer.
518  *
519  * PARAMS
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.
525  *
526  * RETURNS
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
532  *           to the timer.
533  */
534 NTSTATUS WINAPI NtQueryTimer(
535     HANDLE TimerHandle,
536     TIMER_INFORMATION_CLASS TimerInformationClass,
537     PVOID TimerInformation,
538     ULONG Length,
539     PULONG ReturnLength)
540 {
541     TIMER_BASIC_INFORMATION * basic_info = (TIMER_BASIC_INFORMATION *)TimerInformation;
542     NTSTATUS status;
543     LARGE_INTEGER now;
544
545     TRACE("(%p,%d,%p,0x%08lx,%p)\n", TimerHandle, TimerInformationClass,
546        TimerInformation, Length, ReturnLength);
547
548     switch (TimerInformationClass)
549     {
550     case TimerBasicInformation:
551         if (Length < sizeof(TIMER_BASIC_INFORMATION))
552             return STATUS_INFO_LENGTH_MISMATCH;
553
554         SERVER_START_REQ(get_timer_info)
555         {
556             req->handle = TimerHandle;
557             status = wine_server_call(req);
558
559             /* convert server time to absolute NTDLL time */
560             NTDLL_from_server_timeout(&basic_info->RemainingTime, &reply->when);
561             basic_info->TimerState = reply->signaled;
562         }
563         SERVER_END_REQ;
564
565         /* convert from absolute into relative time */
566         NtQuerySystemTime(&now);
567         if (now.QuadPart > basic_info->RemainingTime.QuadPart)
568             basic_info->RemainingTime.QuadPart = 0;
569         else
570             basic_info->RemainingTime.QuadPart -= now.QuadPart;
571
572         if (ReturnLength) *ReturnLength = sizeof(TIMER_BASIC_INFORMATION);
573
574         return status;
575     }
576
577     FIXME("Unhandled class %d\n", TimerInformationClass);
578     return STATUS_INVALID_INFO_CLASS;
579 }
580
581
582 /******************************************************************************
583  * NtQueryTimerResolution [NTDLL.@]
584  */
585 NTSTATUS WINAPI NtQueryTimerResolution(OUT ULONG* min_resolution,
586                                        OUT ULONG* max_resolution,
587                                        OUT ULONG* current_resolution)
588 {
589     FIXME("(%p,%p,%p), stub!\n",
590           min_resolution, max_resolution, current_resolution);
591
592     return STATUS_NOT_IMPLEMENTED;
593 }
594
595 /******************************************************************************
596  * NtSetTimerResolution [NTDLL.@]
597  */
598 NTSTATUS WINAPI NtSetTimerResolution(IN ULONG resolution,
599                                      IN BOOLEAN set_resolution,
600                                      OUT ULONG* current_resolution )
601 {
602     FIXME("(%lu,%u,%p), stub!\n",
603           resolution, set_resolution, current_resolution);
604
605     return STATUS_NOT_IMPLEMENTED;
606 }
607
608
609 /***********************************************************************
610  *              wait_reply
611  *
612  * Wait for a reply on the waiting pipe of the current thread.
613  */
614 static int wait_reply( void *cookie )
615 {
616     int signaled;
617     struct wake_up_reply reply;
618     for (;;)
619     {
620         int ret;
621         ret = read( NtCurrentTeb()->wait_fd[0], &reply, sizeof(reply) );
622         if (ret == sizeof(reply))
623         {
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 */
629             for (;;)
630             {
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");
636             }
637             return signaled;
638         }
639         if (ret >= 0) server_protocol_error( "partial wakeup read %d\n", ret );
640         if (errno == EINTR) continue;
641         server_protocol_perror("wakeup read");
642     }
643     /* the server closed the connection; time to die... */
644     server_abort_thread(0);
645 }
646
647
648 /***********************************************************************
649  *              call_apcs
650  *
651  * Call outstanding APCs.
652  */
653 static void call_apcs( BOOL alertable )
654 {
655     FARPROC proc;
656     LARGE_INTEGER time;
657     void *arg1, *arg2, *arg3;
658
659     for (;;)
660     {
661         int type = APC_NONE;
662         SERVER_START_REQ( get_apc )
663         {
664             req->alertable = alertable;
665             if (!wine_server_call( req )) type = reply->type;
666             proc = reply->func;
667             arg1 = reply->arg1;
668             arg2 = reply->arg2;
669             arg3 = reply->arg3;
670         }
671         SERVER_END_REQ;
672
673         switch (type)
674         {
675         case APC_NONE:
676             return;  /* no more APCs */
677         case APC_USER:
678             proc( arg1, arg2, arg3 );
679             break;
680         case APC_TIMER:
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 );
685             break;
686         case APC_ASYNC_IO:
687             NtCurrentTeb()->num_async_io--;
688             proc( arg1, (IO_STATUS_BLOCK*)arg2, (ULONG)arg3 );
689             break;
690         default:
691             server_protocol_error( "get_apc_request: bad type %d\n", type );
692             break;
693         }
694     }
695 }
696
697
698 /***********************************************************************
699  *              NTDLL_wait_for_multiple_objects
700  *
701  * Implementation of NtWaitForMultipleObjects
702  */
703 NTSTATUS NTDLL_wait_for_multiple_objects( UINT count, const HANDLE *handles, UINT flags,
704                                           const LARGE_INTEGER *timeout, HANDLE signal_object )
705 {
706     NTSTATUS ret;
707     int cookie;
708
709     if (timeout) flags |= SELECT_TIMEOUT;
710     for (;;)
711     {
712         SERVER_START_REQ( select )
713         {
714             req->flags   = flags;
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 );
720         }
721         SERVER_END_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 */
727     }
728
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();
733
734     return ret;
735 }
736
737
738 /* wait operations */
739
740 /******************************************************************
741  *              NtWaitForMultipleObjects (NTDLL.@)
742  */
743 NTSTATUS WINAPI NtWaitForMultipleObjects( DWORD count, const HANDLE *handles,
744                                           BOOLEAN wait_all, BOOLEAN alertable,
745                                           const LARGE_INTEGER *timeout )
746 {
747     UINT flags = SELECT_INTERRUPTIBLE;
748
749     if (!count || count > MAXIMUM_WAIT_OBJECTS) return STATUS_INVALID_PARAMETER_1;
750
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 );
754 }
755
756
757 /******************************************************************
758  *              NtWaitForSingleObject (NTDLL.@)
759  */
760 NTSTATUS WINAPI NtWaitForSingleObject(HANDLE handle, BOOLEAN alertable, const LARGE_INTEGER *timeout )
761 {
762     return NtWaitForMultipleObjects( 1, &handle, FALSE, alertable, timeout );
763 }
764
765
766 /******************************************************************
767  *              NtSignalAndWaitForSingleObject (NTDLL.@)
768  */
769 NTSTATUS WINAPI NtSignalAndWaitForSingleObject( HANDLE hSignalObject, HANDLE hWaitObject,
770                                                 BOOLEAN alertable, const LARGE_INTEGER *timeout )
771 {
772     UINT flags = SELECT_INTERRUPTIBLE;
773
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 );
777 }
778
779
780 /******************************************************************
781  *              NtYieldExecution (NTDLL.@)
782  */
783 NTSTATUS WINAPI NtYieldExecution(void)
784 {
785 #ifdef HAVE_SCHED_YIELD
786     sched_yield();
787     return STATUS_SUCCESS;
788 #else
789     return STATUS_NO_YIELD_PERFORMED;
790 #endif
791 }
792
793
794 /******************************************************************
795  *              NtDelayExecution (NTDLL.@)
796  */
797 NTSTATUS WINAPI NtDelayExecution( BOOLEAN alertable, const LARGE_INTEGER *timeout )
798 {
799     /* if alertable or async I/O in progress, we need to query the server */
800     if (alertable || NtCurrentTeb()->num_async_io)
801     {
802         UINT flags = SELECT_INTERRUPTIBLE;
803         if (alertable) flags |= SELECT_ALERTABLE;
804         return NTDLL_wait_for_multiple_objects( 0, NULL, flags, timeout, 0 );
805     }
806
807     if (!timeout)  /* sleep forever */
808     {
809         for (;;) select( 0, NULL, NULL, NULL, NULL );
810     }
811     else
812     {
813         abs_time_t when;
814
815         NTDLL_get_server_timeout( &when, timeout );
816
817         /* Note that we yield after establishing the desired timeout */
818         NtYieldExecution();
819
820         for (;;)
821         {
822             struct timeval tv;
823             gettimeofday( &tv, 0 );
824             tv.tv_sec = when.sec - tv.tv_sec;
825             if ((tv.tv_usec = when.usec - tv.tv_usec) < 0)
826             {
827                 tv.tv_usec += 1000000;
828                 tv.tv_sec--;
829             }
830             /* if our yield already passed enough time, we're done */
831             if (tv.tv_sec < 0) break;
832
833             if (select( 0, NULL, NULL, NULL, &tv ) != -1) break;
834         }
835     }
836     return STATUS_SUCCESS;
837 }