2 * Win32 process and thread synchronisation
4 * Copyright 1997 Alexandre Julliard
15 #include "file.h" /* for DOSFS_UnixTimeToFileTime */
18 #include "wine/server.h"
21 /***********************************************************************
24 inline static void get_timeout( struct timeval *when, int timeout )
26 gettimeofday( when, 0 );
29 long sec = timeout / 1000;
30 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
32 when->tv_usec -= 1000000;
39 #define MAX_NUMBER_OF_FDS 20
41 static inline int time_before( struct timeval *t1, struct timeval *t2 )
43 return ((t1->tv_sec < t2->tv_sec) ||
44 ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec < t2->tv_usec)));
47 static void CALLBACK call_completion_routine(ULONG_PTR data)
49 async_private* ovp = (async_private*)data;
51 ovp->completion_func(ovp->lpOverlapped->Internal,
52 ovp->lpOverlapped->InternalHigh,
54 ovp->completion_func=NULL;
55 HeapFree(GetProcessHeap(), 0, ovp);
58 static void finish_async(async_private *ovp, DWORD status)
60 ovp->lpOverlapped->Internal=status;
62 /* call ReadFileEx/WriteFileEx's overlapped completion function */
63 if(ovp->completion_func)
65 QueueUserAPC(call_completion_routine,GetCurrentThread(),(ULONG_PTR)ovp);
68 /* remove it from the active list */
70 ovp->prev->next = ovp->next;
72 NtCurrentTeb()->pending_list = ovp->next;
75 ovp->next->prev = ovp->prev;
81 NtSetEvent(ovp->lpOverlapped->hEvent,NULL);
82 if(!ovp->completion_func) HeapFree(GetProcessHeap(), 0, ovp);
85 /***********************************************************************
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?
92 static void check_async_list(void)
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;
98 async_private *ovp, *timeout_user;
103 /* the first fd belongs to the server connection */
104 fds[0].events=POLLIN;
106 fds[0].fd = NtCurrentTeb()->wait_fd[0];
108 ovp = NtCurrentTeb()->pending_list;
111 gettimeofday(&now,NULL);
112 for(n=1; ovp && (n<MAX_NUMBER_OF_FDS); ovp = tmp)
116 if(ovp->lpOverlapped->Internal!=STATUS_PENDING)
118 finish_async(ovp,STATUS_UNSUCCESSFUL);
122 if(ovp->timeout && time_before(&ovp->tv,&now))
124 finish_async(ovp,STATUS_TIMEOUT);
129 fds[n].events=ovp->event;
133 if(ovp->timeout && ( (!timeout_user) || time_before(&ovp->tv,&timeout_user->tv)))
135 timeout = (ovp->tv.tv_sec - now.tv_sec) * 1000
136 + (ovp->tv.tv_usec - now.tv_usec) / 1000;
143 /* if there aren't any active asyncs return */
147 r = poll(fds, n, timeout);
149 /* if there were any errors, return immediately */
150 if( (r<0) || (fds[0].revents==POLLNVAL) )
155 finish_async(timeout_user, STATUS_TIMEOUT);
159 /* search for async operations that are ready */
163 user[i]->func(user[i],fds[i].revents);
165 if(user[i]->lpOverlapped->Internal!=STATUS_PENDING)
166 finish_async(user[i],user[i]->lpOverlapped->Internal);
169 if(fds[0].revents == POLLIN)
175 /***********************************************************************
178 * Wait for a reply on the waiting pipe of the current thread.
180 static int wait_reply( void *cookie )
183 struct wake_up_reply reply;
187 if (NtCurrentTeb()->pending_list) check_async_list();
188 ret = read( NtCurrentTeb()->wait_fd[0], &reply, sizeof(reply) );
189 if (ret == sizeof(reply))
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 */
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");
206 if (ret >= 0) server_protocol_error( "partial wakeup read %d\n", ret );
207 if (errno == EINTR) continue;
208 server_protocol_perror("wakeup read");
210 /* the server closed the connection; time to die... */
211 SYSDEPS_ExitThread(0);
215 /***********************************************************************
218 * Call outstanding APCs.
220 static void call_apcs( BOOL alertable )
229 SERVER_START_REQ( get_apc )
231 req->alertable = alertable;
232 wine_server_set_reply( req, args, sizeof(args) );
233 if (!wine_server_call( req ))
244 return; /* no more APCs */
246 proc( args[0], args[1]);
252 /* convert sec/usec to NT time */
253 DOSFS_UnixTimeToFileTime( (time_t)args[0], &ft, (DWORD)args[1] * 10 );
254 proc( args[2], ft.dwLowDateTime, ft.dwHighDateTime );
257 server_protocol_error( "get_apc_request: bad type %d\n", type );
263 /***********************************************************************
266 VOID WINAPI Sleep( DWORD timeout )
268 WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
271 /******************************************************************************
272 * SleepEx (KERNEL32.@)
274 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
276 DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
277 if (ret != WAIT_IO_COMPLETION) ret = 0;
282 /***********************************************************************
283 * WaitForSingleObject (KERNEL32.@)
285 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
287 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
291 /***********************************************************************
292 * WaitForSingleObjectEx (KERNEL32.@)
294 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
297 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
301 /***********************************************************************
302 * WaitForMultipleObjects (KERNEL32.@)
304 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
305 BOOL wait_all, DWORD timeout )
307 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
311 /***********************************************************************
312 * WaitForMultipleObjectsEx (KERNEL32.@)
314 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
315 BOOL wait_all, DWORD timeout,
321 if (count > MAXIMUM_WAIT_OBJECTS)
323 SetLastError( ERROR_INVALID_PARAMETER );
327 if (timeout == INFINITE) tv.tv_sec = tv.tv_usec = 0;
328 else get_timeout( &tv, timeout );
332 SERVER_START_REQ( select )
334 req->flags = SELECT_INTERRUPTIBLE;
335 req->cookie = &cookie;
336 req->sec = tv.tv_sec;
337 req->usec = tv.tv_usec;
338 wine_server_add_data( req, handles, count * sizeof(HANDLE) );
340 if (wait_all) req->flags |= SELECT_ALL;
341 if (alertable) req->flags |= SELECT_ALERTABLE;
342 if (timeout != INFINITE) req->flags |= SELECT_TIMEOUT;
344 ret = wine_server_call( req );
347 if (ret == STATUS_PENDING) ret = wait_reply( &cookie );
348 if (ret != STATUS_USER_APC) break;
349 call_apcs( alertable );
350 if (alertable) break;
352 if (HIWORD(ret)) /* is it an error code? */
354 SetLastError( RtlNtStatusToDosError(ret) );
361 /***********************************************************************
362 * WaitForSingleObject (KERNEL.460)
364 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
366 DWORD retval, mutex_count;
368 ReleaseThunkLock( &mutex_count );
369 retval = WaitForSingleObject( handle, timeout );
370 RestoreThunkLock( mutex_count );
374 /***********************************************************************
375 * WaitForMultipleObjects (KERNEL.461)
377 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
378 BOOL wait_all, DWORD timeout )
380 DWORD retval, mutex_count;
382 ReleaseThunkLock( &mutex_count );
383 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
384 RestoreThunkLock( mutex_count );
388 /***********************************************************************
389 * WaitForMultipleObjectsEx (KERNEL.495)
391 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
392 BOOL wait_all, DWORD timeout, BOOL alertable )
394 DWORD retval, mutex_count;
396 ReleaseThunkLock( &mutex_count );
397 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
398 RestoreThunkLock( mutex_count );