Some dumb fixes.
[wine] / scheduler / synchro.c
1 /*
2  * Win32 process and thread synchronisation
3  *
4  * Copyright 1997 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <errno.h>
9 #include <signal.h>
10 #include <sys/time.h>
11 #include <sys/poll.h>
12 #include <unistd.h>
13 #include <string.h>
14
15 #include "file.h"  /* for DOSFS_UnixTimeToFileTime */
16 #include "thread.h"
17 #include "winerror.h"
18 #include "wine/server.h"
19
20
21 /***********************************************************************
22  *              get_timeout
23  */
24 inline static void get_timeout( struct timeval *when, int timeout )
25 {
26     gettimeofday( when, 0 );
27     if (timeout)
28     {
29         long sec = timeout / 1000;
30         if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
31         {
32             when->tv_usec -= 1000000;
33             when->tv_sec++;
34         }
35         when->tv_sec += sec;
36     }
37 }
38
39 #define MAX_NUMBER_OF_FDS 20
40
41 static inline int time_before( struct timeval *t1, struct timeval *t2 )
42 {
43     return ((t1->tv_sec < t2->tv_sec) ||
44             ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec < t2->tv_usec)));
45 }
46
47 static void CALLBACK call_completion_routine(ULONG_PTR data)
48 {
49     async_private* ovp = (async_private*)data;
50
51     ovp->completion_func(ovp->lpOverlapped->Internal,
52                          ovp->lpOverlapped->InternalHigh,
53                          ovp->lpOverlapped);
54     ovp->completion_func=NULL;
55     HeapFree(GetProcessHeap(), 0, ovp);
56 }
57
58 static void finish_async(async_private *ovp, DWORD status)
59 {
60     ovp->lpOverlapped->Internal=status;
61
62     /* call ReadFileEx/WriteFileEx's overlapped completion function */
63     if(ovp->completion_func)
64     {
65         QueueUserAPC(call_completion_routine,GetCurrentThread(),(ULONG_PTR)ovp);
66     }
67
68     /* remove it from the active list */
69     if(ovp->prev)
70         ovp->prev->next = ovp->next;
71     else
72         NtCurrentTeb()->pending_list = ovp->next;
73
74     if(ovp->next)
75         ovp->next->prev = ovp->prev;
76
77     ovp->next=NULL;
78     ovp->prev=NULL;
79
80     close(ovp->fd);
81     NtSetEvent(ovp->lpOverlapped->hEvent,NULL);
82     if(!ovp->completion_func) HeapFree(GetProcessHeap(), 0, ovp);
83 }
84
85 /***********************************************************************
86  *           check_async_list
87  *
88  *  Create a list of fds for poll to check while waiting on the server
89  *  FIXME: this loop is too large, cut into smaller functions
90  *         perhaps we could share/steal some of the code in server/select.c?
91  */
92 static void check_async_list(void)
93 {
94     /* FIXME: should really malloc these two arrays */
95     struct pollfd fds[MAX_NUMBER_OF_FDS];
96     async_private *user[MAX_NUMBER_OF_FDS], *tmp;
97     int i, n, r, timeout;
98     async_private *ovp, *timeout_user;
99     struct timeval now;
100
101     while(1)
102     {
103         /* the first fd belongs to the server connection */
104         fds[0].events=POLLIN;
105         fds[0].revents=0;
106         fds[0].fd = NtCurrentTeb()->wait_fd[0];
107
108         ovp = NtCurrentTeb()->pending_list;
109         timeout = -1;
110         timeout_user = NULL;
111         gettimeofday(&now,NULL);
112         for(n=1; ovp && (n<MAX_NUMBER_OF_FDS); ovp = tmp)
113         {
114             tmp = ovp->next;
115
116             if(ovp->lpOverlapped->Internal!=STATUS_PENDING)
117             {
118                 finish_async(ovp,STATUS_UNSUCCESSFUL);
119                 continue;
120             }
121
122             if(ovp->timeout && time_before(&ovp->tv,&now))
123             {
124                 finish_async(ovp,STATUS_TIMEOUT);
125                 continue;
126             }
127
128             fds[n].fd=ovp->fd;
129             fds[n].events=ovp->event;
130             fds[n].revents=0;
131             user[n] = ovp;
132
133             if(ovp->timeout && ( (!timeout_user) || time_before(&ovp->tv,&timeout_user->tv)))
134             {
135                 timeout = (ovp->tv.tv_sec - now.tv_sec) * 1000
136                         + (ovp->tv.tv_usec - now.tv_usec) / 1000;
137                 timeout_user = ovp;
138             }
139
140             n++;
141         }
142
143         /* if there aren't any active asyncs return */
144         if(n==1)
145             return;
146
147         r = poll(fds, n, timeout);
148
149         /* if there were any errors, return immediately */
150         if( (r<0) || (fds[0].revents==POLLNVAL) )
151             return;
152
153         if( r==0 )
154         {
155             finish_async(timeout_user, STATUS_TIMEOUT);
156             continue;
157         }
158
159         /* search for async operations that are ready */
160         for( i=1; i<n; i++)
161         {
162             if (fds[i].revents)
163                 user[i]->func(user[i],fds[i].revents);
164
165             if(user[i]->lpOverlapped->Internal!=STATUS_PENDING)
166                 finish_async(user[i],user[i]->lpOverlapped->Internal);
167         }
168
169         if(fds[0].revents == POLLIN)
170             return;
171     }
172 }
173
174
175 /***********************************************************************
176  *              wait_reply
177  *
178  * Wait for a reply on the waiting pipe of the current thread.
179  */
180 static int wait_reply( void *cookie )
181 {
182     int signaled;
183     struct wake_up_reply reply;
184     for (;;)
185     {
186         int ret;
187         if (NtCurrentTeb()->pending_list) check_async_list();
188         ret = read( NtCurrentTeb()->wait_fd[0], &reply, sizeof(reply) );
189         if (ret == sizeof(reply))
190         {
191             if (!reply.cookie) break;  /* thread got killed */
192             if (reply.cookie == cookie) return reply.signaled;
193             /* we stole another reply, wait for the real one */
194             signaled = wait_reply( cookie );
195             /* and now put the wrong one back in the pipe */
196             for (;;)
197             {
198                 ret = write( NtCurrentTeb()->wait_fd[1], &reply, sizeof(reply) );
199                 if (ret == sizeof(reply)) break;
200                 if (ret >= 0) server_protocol_error( "partial wakeup write %d\n", ret );
201                 if (errno == EINTR) continue;
202                 server_protocol_perror("wakeup write");
203             }
204             return signaled;
205         }
206         if (ret >= 0) server_protocol_error( "partial wakeup read %d\n", ret );
207         if (errno == EINTR) continue;
208         server_protocol_perror("wakeup read");
209     }
210     /* the server closed the connection; time to die... */
211     SYSDEPS_ExitThread(0);
212 }
213
214
215 /***********************************************************************
216  *              call_apcs
217  *
218  * Call outstanding APCs.
219  */
220 static void call_apcs( BOOL alertable )
221 {
222     FARPROC proc = NULL;
223     FILETIME ft;
224     void *args[4];
225
226     for (;;)
227     {
228         int type = APC_NONE;
229         SERVER_START_VAR_REQ( get_apc, sizeof(args) )
230         {
231             req->alertable = alertable;
232             if (!SERVER_CALL())
233             {
234                 type = req->type;
235                 proc = req->func;
236                 memcpy( args, server_data_ptr(req), server_data_size(req) );
237             }
238         }
239         SERVER_END_VAR_REQ;
240
241         switch(type)
242         {
243         case APC_NONE:
244             return;  /* no more APCs */
245         case APC_ASYNC:
246             break;
247         case APC_USER:
248             proc( args[0] );
249             break;
250         case APC_TIMER:
251             /* convert sec/usec to NT time */
252             DOSFS_UnixTimeToFileTime( (time_t)args[0], &ft, (DWORD)args[1] * 10 );
253             proc( args[2], ft.dwLowDateTime, ft.dwHighDateTime );
254             break;
255         default:
256             server_protocol_error( "get_apc_request: bad type %d\n", type );
257             break;
258         }
259     }
260 }
261
262 /***********************************************************************
263  *              Sleep  (KERNEL32.@)
264  */
265 VOID WINAPI Sleep( DWORD timeout )
266 {
267     WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
268 }
269
270 /******************************************************************************
271  *              SleepEx   (KERNEL32.@)
272  */
273 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
274 {
275     DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
276     if (ret != WAIT_IO_COMPLETION) ret = 0;
277     return ret;
278 }
279
280
281 /***********************************************************************
282  *           WaitForSingleObject   (KERNEL32.@)
283  */
284 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
285 {
286     return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
287 }
288
289
290 /***********************************************************************
291  *           WaitForSingleObjectEx   (KERNEL32.@)
292  */
293 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
294                                     BOOL alertable )
295 {
296     return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
297 }
298
299
300 /***********************************************************************
301  *           WaitForMultipleObjects   (KERNEL32.@)
302  */
303 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
304                                      BOOL wait_all, DWORD timeout )
305 {
306     return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
307 }
308
309
310 /***********************************************************************
311  *           WaitForMultipleObjectsEx   (KERNEL32.@)
312  */
313 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
314                                        BOOL wait_all, DWORD timeout,
315                                        BOOL alertable )
316 {
317     int i, ret, cookie;
318     struct timeval tv;
319
320     if (count > MAXIMUM_WAIT_OBJECTS)
321     {
322         SetLastError( ERROR_INVALID_PARAMETER );
323         return WAIT_FAILED;
324     }
325
326     if (timeout == INFINITE) tv.tv_sec = tv.tv_usec = 0;
327     else get_timeout( &tv, timeout );
328
329     for (;;)
330     {
331         SERVER_START_VAR_REQ( select, count * sizeof(int) )
332         {
333             int *data = server_data_ptr( req );
334
335             req->flags   = SELECT_INTERRUPTIBLE;
336             req->cookie  = &cookie;
337             req->sec     = tv.tv_sec;
338             req->usec    = tv.tv_usec;
339             for (i = 0; i < count; i++) data[i] = handles[i];
340
341             if (wait_all) req->flags |= SELECT_ALL;
342             if (alertable) req->flags |= SELECT_ALERTABLE;
343             if (timeout != INFINITE) req->flags |= SELECT_TIMEOUT;
344
345             ret = SERVER_CALL();
346         }
347         SERVER_END_VAR_REQ;
348         if (ret == STATUS_PENDING) ret = wait_reply( &cookie );
349         if (ret != STATUS_USER_APC) break;
350         call_apcs( alertable );
351         if (alertable) break;
352     }
353     if (HIWORD(ret))  /* is it an error code? */
354     {
355         SetLastError( RtlNtStatusToDosError(ret) );
356         ret = WAIT_FAILED;
357     }
358     return ret;
359 }
360
361
362 /***********************************************************************
363  *           WaitForSingleObject   (KERNEL.460)
364  */
365 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
366 {
367     DWORD retval, mutex_count;
368
369     ReleaseThunkLock( &mutex_count );
370     retval = WaitForSingleObject( handle, timeout );
371     RestoreThunkLock( mutex_count );
372     return retval;
373 }
374
375 /***********************************************************************
376  *           WaitForMultipleObjects   (KERNEL.461)
377  */
378 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
379                                        BOOL wait_all, DWORD timeout )
380 {
381     DWORD retval, mutex_count;
382
383     ReleaseThunkLock( &mutex_count );
384     retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
385     RestoreThunkLock( mutex_count );
386     return retval;
387 }
388
389 /***********************************************************************
390  *           WaitForMultipleObjectsEx   (KERNEL.495)
391  */
392 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
393                                          BOOL wait_all, DWORD timeout, BOOL alertable )
394 {
395     DWORD retval, mutex_count;
396
397     ReleaseThunkLock( &mutex_count );
398     retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
399     RestoreThunkLock( mutex_count );
400     return retval;
401 }