msi: Add a test for MsiDatabaseIsTablePersistent.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "ntstatus.h"
54 #define WIN32_NO_STATUS
55 #include "windef.h"
56 #include "thread.h"
57 #include "wine/server.h"
58 #include "wine/debug.h"
59 #include "ntdll_misc.h"
60
61 WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
62
63
64 /*
65  *      Semaphores
66  */
67
68 /******************************************************************************
69  *  NtCreateSemaphore (NTDLL.@)
70  */
71 NTSTATUS WINAPI NtCreateSemaphore( OUT PHANDLE SemaphoreHandle,
72                                    IN ACCESS_MASK access,
73                                    IN const OBJECT_ATTRIBUTES *attr OPTIONAL,
74                                    IN LONG InitialCount,
75                                    IN LONG MaximumCount )
76 {
77     DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
78     NTSTATUS ret;
79
80     if (MaximumCount <= 0 || InitialCount < 0 || InitialCount > MaximumCount)
81         return STATUS_INVALID_PARAMETER;
82     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
83
84     SERVER_START_REQ( create_semaphore )
85     {
86         req->access  = access;
87         req->attributes = (attr) ? attr->Attributes : 0;
88         req->rootdir = attr ? attr->RootDirectory : 0;
89         req->initial = InitialCount;
90         req->max     = MaximumCount;
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->attributes = (attr) ? attr->Attributes : 0;
115         req->rootdir = attr ? attr->RootDirectory : 0;
116         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
117         ret = wine_server_call( req );
118         *SemaphoreHandle = reply->handle;
119     }
120     SERVER_END_REQ;
121     return ret;
122 }
123
124 /******************************************************************************
125  *  NtQuerySemaphore (NTDLL.@)
126  */
127 NTSTATUS WINAPI NtQuerySemaphore(
128         HANDLE SemaphoreHandle,
129         SEMAPHORE_INFORMATION_CLASS SemaphoreInformationClass,
130         PVOID SemaphoreInformation,
131         ULONG Length,
132         PULONG ReturnLength)
133 {
134         FIXME("(%p,%d,%p,0x%08lx,%p) stub!\n",
135         SemaphoreHandle, SemaphoreInformationClass, SemaphoreInformation, Length, ReturnLength);
136         return STATUS_SUCCESS;
137 }
138
139 /******************************************************************************
140  *  NtReleaseSemaphore (NTDLL.@)
141  */
142 NTSTATUS WINAPI NtReleaseSemaphore( HANDLE handle, ULONG count, PULONG previous )
143 {
144     NTSTATUS ret;
145     SERVER_START_REQ( release_semaphore )
146     {
147         req->handle = handle;
148         req->count  = count;
149         if (!(ret = wine_server_call( req )))
150         {
151             if (previous) *previous = reply->prev_count;
152         }
153     }
154     SERVER_END_REQ;
155     return ret;
156 }
157
158 /*
159  *      Events
160  */
161
162 /**************************************************************************
163  * NtCreateEvent (NTDLL.@)
164  * ZwCreateEvent (NTDLL.@)
165  */
166 NTSTATUS WINAPI NtCreateEvent(
167         OUT PHANDLE EventHandle,
168         IN ACCESS_MASK DesiredAccess,
169         IN const OBJECT_ATTRIBUTES *attr,
170         IN BOOLEAN ManualReset,
171         IN BOOLEAN InitialState)
172 {
173     DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
174     NTSTATUS ret;
175
176     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
177
178     SERVER_START_REQ( create_event )
179     {
180         req->access = DesiredAccess;
181         req->attributes = (attr) ? attr->Attributes : 0;
182         req->rootdir = attr ? attr->RootDirectory : 0;
183         req->manual_reset = ManualReset;
184         req->initial_state = InitialState;
185         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
186         ret = wine_server_call( req );
187         *EventHandle = reply->handle;
188     }
189     SERVER_END_REQ;
190     return ret;
191 }
192
193 /******************************************************************************
194  *  NtOpenEvent (NTDLL.@)
195  *  ZwOpenEvent (NTDLL.@)
196  */
197 NTSTATUS WINAPI NtOpenEvent(
198         OUT PHANDLE EventHandle,
199         IN ACCESS_MASK DesiredAccess,
200         IN const OBJECT_ATTRIBUTES *attr )
201 {
202     DWORD len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
203     NTSTATUS ret;
204
205     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
206
207     SERVER_START_REQ( open_event )
208     {
209         req->access  = DesiredAccess;
210         req->attributes = (attr) ? attr->Attributes : 0;
211         req->rootdir = attr ? attr->RootDirectory : 0;
212         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
213         ret = wine_server_call( req );
214         *EventHandle = reply->handle;
215     }
216     SERVER_END_REQ;
217     return ret;
218 }
219
220
221 /******************************************************************************
222  *  NtSetEvent (NTDLL.@)
223  *  ZwSetEvent (NTDLL.@)
224  */
225 NTSTATUS WINAPI NtSetEvent( HANDLE handle, PULONG NumberOfThreadsReleased )
226 {
227     NTSTATUS ret;
228
229     /* FIXME: set NumberOfThreadsReleased */
230
231     SERVER_START_REQ( event_op )
232     {
233         req->handle = handle;
234         req->op     = SET_EVENT;
235         ret = wine_server_call( req );
236     }
237     SERVER_END_REQ;
238     return ret;
239 }
240
241 /******************************************************************************
242  *  NtResetEvent (NTDLL.@)
243  */
244 NTSTATUS WINAPI NtResetEvent( HANDLE handle, PULONG NumberOfThreadsReleased )
245 {
246     NTSTATUS ret;
247
248     /* resetting an event can't release any thread... */
249     if (NumberOfThreadsReleased) *NumberOfThreadsReleased = 0;
250
251     SERVER_START_REQ( event_op )
252     {
253         req->handle = handle;
254         req->op     = RESET_EVENT;
255         ret = wine_server_call( req );
256     }
257     SERVER_END_REQ;
258     return ret;
259 }
260
261 /******************************************************************************
262  *  NtClearEvent (NTDLL.@)
263  *
264  * FIXME
265  *   same as NtResetEvent ???
266  */
267 NTSTATUS WINAPI NtClearEvent ( HANDLE handle )
268 {
269     return NtResetEvent( handle, NULL );
270 }
271
272 /******************************************************************************
273  *  NtPulseEvent (NTDLL.@)
274  *
275  * FIXME
276  *   PulseCount
277  */
278 NTSTATUS WINAPI NtPulseEvent( HANDLE handle, PULONG PulseCount )
279 {
280     NTSTATUS ret;
281
282     if (PulseCount)
283       FIXME("(%p,%ld)\n", handle, *PulseCount);
284
285     SERVER_START_REQ( event_op )
286     {
287         req->handle = handle;
288         req->op     = PULSE_EVENT;
289         ret = wine_server_call( req );
290     }
291     SERVER_END_REQ;
292     return ret;
293 }
294
295 /******************************************************************************
296  *  NtQueryEvent (NTDLL.@)
297  */
298 NTSTATUS WINAPI NtQueryEvent (
299         IN  HANDLE EventHandle,
300         IN  UINT EventInformationClass,
301         OUT PVOID EventInformation,
302         IN  ULONG EventInformationLength,
303         OUT PULONG  ReturnLength)
304 {
305         FIXME("(%p)\n", EventHandle);
306         return STATUS_SUCCESS;
307 }
308
309 /*
310  *      Mutants (known as Mutexes in Kernel32)
311  */
312
313 /******************************************************************************
314  *              NtCreateMutant                          [NTDLL.@]
315  *              ZwCreateMutant                          [NTDLL.@]
316  */
317 NTSTATUS WINAPI NtCreateMutant(OUT HANDLE* MutantHandle,
318                                IN ACCESS_MASK access,
319                                IN const OBJECT_ATTRIBUTES* attr OPTIONAL,
320                                IN BOOLEAN InitialOwner)
321 {
322     NTSTATUS    status;
323     DWORD       len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
324
325     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
326
327     SERVER_START_REQ( create_mutex )
328     {
329         req->access  = access;
330         req->attributes = (attr) ? attr->Attributes : 0;
331         req->rootdir = attr ? attr->RootDirectory : 0;
332         req->owned   = InitialOwner;
333         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
334         status = wine_server_call( req );
335         *MutantHandle = reply->handle;
336     }
337     SERVER_END_REQ;
338     return status;
339 }
340
341 /**************************************************************************
342  *              NtOpenMutant                            [NTDLL.@]
343  *              ZwOpenMutant                            [NTDLL.@]
344  */
345 NTSTATUS WINAPI NtOpenMutant(OUT HANDLE* MutantHandle, 
346                              IN ACCESS_MASK access, 
347                              IN const OBJECT_ATTRIBUTES* attr )
348 {
349     NTSTATUS    status;
350     DWORD       len = attr && attr->ObjectName ? attr->ObjectName->Length : 0;
351
352     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
353
354     SERVER_START_REQ( open_mutex )
355     {
356         req->access  = access;
357         req->attributes = (attr) ? attr->Attributes : 0;
358         req->rootdir = attr ? attr->RootDirectory : 0;
359         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
360         status = wine_server_call( req );
361         *MutantHandle = reply->handle;
362     }
363     SERVER_END_REQ;
364     return status;
365 }
366
367 /**************************************************************************
368  *              NtReleaseMutant                         [NTDLL.@]
369  *              ZwReleaseMutant                         [NTDLL.@]
370  */
371 NTSTATUS WINAPI NtReleaseMutant( IN HANDLE handle, OUT PLONG prev_count OPTIONAL)
372 {
373     NTSTATUS    status;
374
375     SERVER_START_REQ( release_mutex )
376     {
377         req->handle = handle;
378         status = wine_server_call( req );
379         if (prev_count) *prev_count = reply->prev_count;
380     }
381     SERVER_END_REQ;
382     return status;
383 }
384
385 /******************************************************************
386  *              NtQueryMutant                   [NTDLL.@]
387  *              ZwQueryMutant                   [NTDLL.@]
388  */
389 NTSTATUS WINAPI NtQueryMutant(IN HANDLE handle, 
390                               IN MUTANT_INFORMATION_CLASS MutantInformationClass, 
391                               OUT PVOID MutantInformation, 
392                               IN ULONG MutantInformationLength, 
393                               OUT PULONG ResultLength OPTIONAL )
394 {
395     FIXME("(%p %u %p %lu %p): stub!\n", 
396           handle, MutantInformationClass, MutantInformation, MutantInformationLength, ResultLength);
397     return STATUS_NOT_IMPLEMENTED;
398 }
399
400 /*
401  *      Timers
402  */
403
404 /**************************************************************************
405  *              NtCreateTimer                           [NTDLL.@]
406  *              ZwCreateTimer                           [NTDLL.@]
407  */
408 NTSTATUS WINAPI NtCreateTimer(OUT HANDLE *handle,
409                               IN ACCESS_MASK access,
410                               IN const OBJECT_ATTRIBUTES *attr OPTIONAL,
411                               IN TIMER_TYPE timer_type)
412 {
413     DWORD       len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
414     NTSTATUS    status;
415
416     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
417
418     if (timer_type != NotificationTimer && timer_type != SynchronizationTimer)
419         return STATUS_INVALID_PARAMETER;
420
421     SERVER_START_REQ( create_timer )
422     {
423         req->access  = access;
424         req->attributes = (attr) ? attr->Attributes : 0;
425         req->rootdir = attr ? attr->RootDirectory : 0;
426         req->manual  = (timer_type == NotificationTimer) ? TRUE : FALSE;
427         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
428         status = wine_server_call( req );
429         *handle = reply->handle;
430     }
431     SERVER_END_REQ;
432     return status;
433
434 }
435
436 /**************************************************************************
437  *              NtOpenTimer                             [NTDLL.@]
438  *              ZwOpenTimer                             [NTDLL.@]
439  */
440 NTSTATUS WINAPI NtOpenTimer(OUT PHANDLE handle,
441                             IN ACCESS_MASK access,
442                             IN const OBJECT_ATTRIBUTES* attr )
443 {
444     DWORD       len = (attr && attr->ObjectName) ? attr->ObjectName->Length : 0;
445     NTSTATUS    status;
446
447     if (len >= MAX_PATH * sizeof(WCHAR)) return STATUS_NAME_TOO_LONG;
448
449     SERVER_START_REQ( open_timer )
450     {
451         req->access  = access;
452         req->attributes = (attr) ? attr->Attributes : 0;
453         req->rootdir = attr ? attr->RootDirectory : 0;
454         if (len) wine_server_add_data( req, attr->ObjectName->Buffer, len );
455         status = wine_server_call( req );
456         *handle = reply->handle;
457     }
458     SERVER_END_REQ;
459     return status;
460 }
461
462 /**************************************************************************
463  *              NtSetTimer                              [NTDLL.@]
464  *              ZwSetTimer                              [NTDLL.@]
465  */
466 NTSTATUS WINAPI NtSetTimer(IN HANDLE handle,
467                            IN const LARGE_INTEGER* when,
468                            IN PTIMER_APC_ROUTINE callback,
469                            IN PVOID callback_arg,
470                            IN BOOLEAN resume,
471                            IN ULONG period OPTIONAL,
472                            OUT PBOOLEAN state OPTIONAL)
473 {
474     NTSTATUS    status = STATUS_SUCCESS;
475
476     TRACE("(%p,%p,%p,%p,%08x,0x%08lx,%p) stub\n",
477           handle, when, callback, callback_arg, resume, period, state);
478
479     SERVER_START_REQ( set_timer )
480     {
481         if (!when->u.LowPart && !when->u.HighPart)
482         {
483             /* special case to start timeout on now+period without too many calculations */
484             req->expire.sec  = 0;
485             req->expire.usec = 0;
486         }
487         else NTDLL_get_server_abstime( &req->expire, when );
488
489         req->handle   = handle;
490         req->period   = period;
491         req->callback = callback;
492         req->arg      = callback_arg;
493         status = wine_server_call( req );
494         if (state) *state = reply->signaled;
495     }
496     SERVER_END_REQ;
497
498     /* set error but can still succeed */
499     if (resume && status == STATUS_SUCCESS) return STATUS_TIMER_RESUME_IGNORED;
500     return status;
501 }
502
503 /**************************************************************************
504  *              NtCancelTimer                           [NTDLL.@]
505  *              ZwCancelTimer                           [NTDLL.@]
506  */
507 NTSTATUS WINAPI NtCancelTimer(IN HANDLE handle, OUT BOOLEAN* state)
508 {
509     NTSTATUS    status;
510
511     SERVER_START_REQ( cancel_timer )
512     {
513         req->handle = handle;
514         status = wine_server_call( req );
515         if (state) *state = reply->signaled;
516     }
517     SERVER_END_REQ;
518     return status;
519 }
520
521 /******************************************************************************
522  *  NtQueryTimer (NTDLL.@)
523  *
524  * Retrieves information about a timer.
525  *
526  * PARAMS
527  *  TimerHandle           [I] The timer to retrieve information about.
528  *  TimerInformationClass [I] The type of information to retrieve.
529  *  TimerInformation      [O] Pointer to buffer to store information in.
530  *  Length                [I] The length of the buffer pointed to by TimerInformation.
531  *  ReturnLength          [O] Optional. The size of buffer actually used.
532  *
533  * RETURNS
534  *  Success: STATUS_SUCCESS
535  *  Failure: STATUS_INFO_LENGTH_MISMATCH, if Length doesn't match the required data
536  *           size for the class specified.
537  *           STATUS_INVALID_INFO_CLASS, if an invalid TimerInformationClass was specified.
538  *           STATUS_ACCESS_DENIED, if TimerHandle does not have TIMER_QUERY_STATE access
539  *           to the timer.
540  */
541 NTSTATUS WINAPI NtQueryTimer(
542     HANDLE TimerHandle,
543     TIMER_INFORMATION_CLASS TimerInformationClass,
544     PVOID TimerInformation,
545     ULONG Length,
546     PULONG ReturnLength)
547 {
548     TIMER_BASIC_INFORMATION * basic_info = (TIMER_BASIC_INFORMATION *)TimerInformation;
549     NTSTATUS status;
550     LARGE_INTEGER now;
551
552     TRACE("(%p,%d,%p,0x%08lx,%p)\n", TimerHandle, TimerInformationClass,
553        TimerInformation, Length, ReturnLength);
554
555     switch (TimerInformationClass)
556     {
557     case TimerBasicInformation:
558         if (Length < sizeof(TIMER_BASIC_INFORMATION))
559             return STATUS_INFO_LENGTH_MISMATCH;
560
561         SERVER_START_REQ(get_timer_info)
562         {
563             req->handle = TimerHandle;
564             status = wine_server_call(req);
565
566             /* convert server time to absolute NTDLL time */
567             NTDLL_from_server_abstime(&basic_info->RemainingTime, &reply->when);
568             basic_info->TimerState = reply->signaled;
569         }
570         SERVER_END_REQ;
571
572         /* convert from absolute into relative time */
573         NtQuerySystemTime(&now);
574         if (now.QuadPart > basic_info->RemainingTime.QuadPart)
575             basic_info->RemainingTime.QuadPart = 0;
576         else
577             basic_info->RemainingTime.QuadPart -= now.QuadPart;
578
579         if (ReturnLength) *ReturnLength = sizeof(TIMER_BASIC_INFORMATION);
580
581         return status;
582     }
583
584     FIXME("Unhandled class %d\n", TimerInformationClass);
585     return STATUS_INVALID_INFO_CLASS;
586 }
587
588
589 /******************************************************************************
590  * NtQueryTimerResolution [NTDLL.@]
591  */
592 NTSTATUS WINAPI NtQueryTimerResolution(OUT ULONG* min_resolution,
593                                        OUT ULONG* max_resolution,
594                                        OUT ULONG* current_resolution)
595 {
596     FIXME("(%p,%p,%p), stub!\n",
597           min_resolution, max_resolution, current_resolution);
598
599     return STATUS_NOT_IMPLEMENTED;
600 }
601
602 /******************************************************************************
603  * NtSetTimerResolution [NTDLL.@]
604  */
605 NTSTATUS WINAPI NtSetTimerResolution(IN ULONG resolution,
606                                      IN BOOLEAN set_resolution,
607                                      OUT ULONG* current_resolution )
608 {
609     FIXME("(%lu,%u,%p), stub!\n",
610           resolution, set_resolution, current_resolution);
611
612     return STATUS_NOT_IMPLEMENTED;
613 }
614
615
616 /***********************************************************************
617  *              wait_reply
618  *
619  * Wait for a reply on the waiting pipe of the current thread.
620  */
621 static int wait_reply( void *cookie )
622 {
623     int signaled;
624     struct wake_up_reply reply;
625     for (;;)
626     {
627         int ret;
628         ret = read( ntdll_get_thread_data()->wait_fd[0], &reply, sizeof(reply) );
629         if (ret == sizeof(reply))
630         {
631             if (!reply.cookie) break;  /* thread got killed */
632             if (reply.cookie == cookie) return reply.signaled;
633             /* we stole another reply, wait for the real one */
634             signaled = wait_reply( cookie );
635             /* and now put the wrong one back in the pipe */
636             for (;;)
637             {
638                 ret = write( ntdll_get_thread_data()->wait_fd[1], &reply, sizeof(reply) );
639                 if (ret == sizeof(reply)) break;
640                 if (ret >= 0) server_protocol_error( "partial wakeup write %d\n", ret );
641                 if (errno == EINTR) continue;
642                 server_protocol_perror("wakeup write");
643             }
644             return signaled;
645         }
646         if (ret >= 0) server_protocol_error( "partial wakeup read %d\n", ret );
647         if (errno == EINTR) continue;
648         server_protocol_perror("wakeup read");
649     }
650     /* the server closed the connection; time to die... */
651     server_abort_thread(0);
652 }
653
654
655 /***********************************************************************
656  *              call_apcs
657  *
658  * Call outstanding APCs.
659  */
660 static void call_apcs( BOOL alertable )
661 {
662     FARPROC proc;
663     LARGE_INTEGER time;
664     void *arg1, *arg2, *arg3;
665
666     for (;;)
667     {
668         int type = APC_NONE;
669         SERVER_START_REQ( get_apc )
670         {
671             req->alertable = alertable;
672             if (!wine_server_call( req )) type = reply->type;
673             proc = reply->func;
674             arg1 = reply->arg1;
675             arg2 = reply->arg2;
676             arg3 = reply->arg3;
677         }
678         SERVER_END_REQ;
679
680         switch (type)
681         {
682         case APC_NONE:
683             return;  /* no more APCs */
684         case APC_USER:
685             proc( arg1, arg2, arg3 );
686             break;
687         case APC_TIMER:
688             /* convert sec/usec to NT time */
689             RtlSecondsSince1970ToTime( (time_t)arg1, &time );
690             time.QuadPart += (DWORD)arg2 * 10;
691             proc( arg3, time.u.LowPart, time.u.HighPart );
692             break;
693         case APC_ASYNC_IO:
694             NtCurrentTeb()->num_async_io--;
695             proc( arg1, (IO_STATUS_BLOCK*)arg2, (ULONG)arg3 );
696             break;
697         default:
698             server_protocol_error( "get_apc_request: bad type %d\n", type );
699             break;
700         }
701     }
702 }
703
704
705 /***********************************************************************
706  *              NTDLL_wait_for_multiple_objects
707  *
708  * Implementation of NtWaitForMultipleObjects
709  */
710 NTSTATUS NTDLL_wait_for_multiple_objects( UINT count, const HANDLE *handles, UINT flags,
711                                           const LARGE_INTEGER *timeout, HANDLE signal_object )
712 {
713     NTSTATUS ret;
714     int cookie;
715
716     if (timeout) flags |= SELECT_TIMEOUT;
717     for (;;)
718     {
719         SERVER_START_REQ( select )
720         {
721             req->flags   = flags;
722             req->cookie  = &cookie;
723             req->signal  = signal_object;
724             NTDLL_get_server_abstime( &req->timeout, timeout );
725             wine_server_add_data( req, handles, count * sizeof(HANDLE) );
726             ret = wine_server_call( req );
727         }
728         SERVER_END_REQ;
729         if (ret == STATUS_PENDING) ret = wait_reply( &cookie );
730         if (ret != STATUS_USER_APC) break;
731         call_apcs( (flags & SELECT_ALERTABLE) != 0 );
732         if (flags & SELECT_ALERTABLE) break;
733         signal_object = 0;  /* don't signal it multiple times */
734     }
735
736     /* A test on Windows 2000 shows that Windows always yields during
737        a wait, but a wait that is hit by an event gets a priority
738        boost as well.  This seems to model that behavior the closest.  */
739     if (ret == WAIT_TIMEOUT) NtYieldExecution();
740
741     return ret;
742 }
743
744
745 /* wait operations */
746
747 /******************************************************************
748  *              NtWaitForMultipleObjects (NTDLL.@)
749  */
750 NTSTATUS WINAPI NtWaitForMultipleObjects( DWORD count, const HANDLE *handles,
751                                           BOOLEAN wait_all, BOOLEAN alertable,
752                                           const LARGE_INTEGER *timeout )
753 {
754     UINT flags = SELECT_INTERRUPTIBLE;
755
756     if (!count || count > MAXIMUM_WAIT_OBJECTS) return STATUS_INVALID_PARAMETER_1;
757
758     if (wait_all) flags |= SELECT_ALL;
759     if (alertable) flags |= SELECT_ALERTABLE;
760     return NTDLL_wait_for_multiple_objects( count, handles, flags, timeout, 0 );
761 }
762
763
764 /******************************************************************
765  *              NtWaitForSingleObject (NTDLL.@)
766  */
767 NTSTATUS WINAPI NtWaitForSingleObject(HANDLE handle, BOOLEAN alertable, const LARGE_INTEGER *timeout )
768 {
769     return NtWaitForMultipleObjects( 1, &handle, FALSE, alertable, timeout );
770 }
771
772
773 /******************************************************************
774  *              NtSignalAndWaitForSingleObject (NTDLL.@)
775  */
776 NTSTATUS WINAPI NtSignalAndWaitForSingleObject( HANDLE hSignalObject, HANDLE hWaitObject,
777                                                 BOOLEAN alertable, const LARGE_INTEGER *timeout )
778 {
779     UINT flags = SELECT_INTERRUPTIBLE;
780
781     if (!hSignalObject) return STATUS_INVALID_HANDLE;
782     if (alertable) flags |= SELECT_ALERTABLE;
783     return NTDLL_wait_for_multiple_objects( 1, &hWaitObject, flags, timeout, hSignalObject );
784 }
785
786
787 /******************************************************************
788  *              NtYieldExecution (NTDLL.@)
789  */
790 NTSTATUS WINAPI NtYieldExecution(void)
791 {
792 #ifdef HAVE_SCHED_YIELD
793     sched_yield();
794     return STATUS_SUCCESS;
795 #else
796     return STATUS_NO_YIELD_PERFORMED;
797 #endif
798 }
799
800
801 /******************************************************************
802  *              NtDelayExecution (NTDLL.@)
803  */
804 NTSTATUS WINAPI NtDelayExecution( BOOLEAN alertable, const LARGE_INTEGER *timeout )
805 {
806     /* if alertable or async I/O in progress, we need to query the server */
807     if (alertable || NtCurrentTeb()->num_async_io)
808     {
809         UINT flags = SELECT_INTERRUPTIBLE;
810         if (alertable) flags |= SELECT_ALERTABLE;
811         return NTDLL_wait_for_multiple_objects( 0, NULL, flags, timeout, 0 );
812     }
813
814     if (!timeout)  /* sleep forever */
815     {
816         for (;;) select( 0, NULL, NULL, NULL, NULL );
817     }
818     else
819     {
820         abs_time_t when;
821
822         NTDLL_get_server_abstime( &when, timeout );
823
824         /* Note that we yield after establishing the desired timeout */
825         NtYieldExecution();
826
827         for (;;)
828         {
829             struct timeval tv;
830             gettimeofday( &tv, 0 );
831             tv.tv_sec = when.sec - tv.tv_sec;
832             if ((tv.tv_usec = when.usec - tv.tv_usec) < 0)
833             {
834                 tv.tv_usec += 1000000;
835                 tv.tv_sec--;
836             }
837             /* if our yield already passed enough time, we're done */
838             if (tv.tv_sec < 0) break;
839
840             if (select( 0, NULL, NULL, NULL, &tv ) != -1) break;
841         }
842     }
843     return STATUS_SUCCESS;
844 }
845
846 /******************************************************************
847  *              NtCreateIoCompletion (NTDLL.@)
848  */
849 NTSTATUS WINAPI NtCreateIoCompletion( PHANDLE CompletionPort, ACCESS_MASK DesiredAccess,
850                                       POBJECT_ATTRIBUTES ObjectAttributes, ULONG NumberOfConcurrentThreads )
851 {
852     FIXME("(%p, %lx, %p, %ld)\n", CompletionPort, DesiredAccess,
853           ObjectAttributes, NumberOfConcurrentThreads);
854     return STATUS_NOT_IMPLEMENTED;
855 }
856
857 NTSTATUS WINAPI NtSetIoCompletion( HANDLE CompletionPort, ULONG_PTR CompletionKey,
858                                    PIO_STATUS_BLOCK iosb, ULONG NumberOfBytesTransferred,
859                                    ULONG NumberOfBytesToTransfer )
860 {
861     FIXME("(%p, %lx, %p, %ld, %ld)\n", CompletionPort, CompletionKey,
862           iosb, NumberOfBytesTransferred, NumberOfBytesToTransfer);
863     return STATUS_NOT_IMPLEMENTED;
864 }
865
866 NTSTATUS WINAPI NtRemoveIoCompletion( HANDLE CompletionPort, PULONG_PTR CompletionKey,
867                                       PIO_STATUS_BLOCK iosb, PULONG OperationStatus,
868                                       PLARGE_INTEGER WaitTime )
869 {
870     FIXME("(%p, %p, %p, %p, %p)\n", CompletionPort, CompletionKey,
871           iosb, OperationStatus, WaitTime);
872     return STATUS_NOT_IMPLEMENTED;
873 }