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 static void CALLBACK call_completion_routine(ULONG_PTR data)
41 async_private* ovp = (async_private*)data;
43 ovp->completion_func(ovp->lpOverlapped->Internal,
44 ovp->lpOverlapped->InternalHigh,
46 ovp->completion_func=NULL;
47 HeapFree(GetProcessHeap(), 0, ovp);
50 static void finish_async(async_private *ovp, DWORD status)
52 ovp->lpOverlapped->Internal=status;
54 /* call ReadFileEx/WriteFileEx's overlapped completion function */
55 if(ovp->completion_func)
57 QueueUserAPC(call_completion_routine,GetCurrentThread(),(ULONG_PTR)ovp);
60 /* remove it from the active list */
62 ovp->prev->next = ovp->next;
64 NtCurrentTeb()->pending_list = ovp->next;
67 ovp->next->prev = ovp->prev;
73 NtSetEvent(ovp->lpOverlapped->hEvent,NULL);
74 if(!ovp->completion_func) HeapFree(GetProcessHeap(), 0, ovp);
77 /***********************************************************************
80 * Process a status event from the server.
82 void check_async_list(LPOVERLAPPED overlapped, DWORD status)
86 /* fprintf(stderr,"overlapped %p status %x\n",overlapped,status); */
88 for(ovp = NtCurrentTeb()->pending_list; ovp; ovp = ovp->next)
89 if(ovp->lpOverlapped == overlapped)
95 if(status != STATUS_ALERTED)
96 ovp->lpOverlapped->Internal = status;
98 if(ovp->lpOverlapped->Internal==STATUS_PENDING)
101 FILE_StartAsync(ovp->handle, ovp->lpOverlapped, ovp->type, 0, ovp->lpOverlapped->Internal);
104 if(ovp->lpOverlapped->Internal!=STATUS_PENDING)
105 finish_async(ovp,ovp->lpOverlapped->Internal);
109 /***********************************************************************
112 * Wait for a reply on the waiting pipe of the current thread.
114 static int wait_reply( void *cookie )
117 struct wake_up_reply reply;
121 ret = read( NtCurrentTeb()->wait_fd[0], &reply, sizeof(reply) );
122 if (ret == sizeof(reply))
124 if (!reply.cookie) break; /* thread got killed */
125 if (reply.cookie == cookie) return reply.signaled;
126 /* we stole another reply, wait for the real one */
127 signaled = wait_reply( cookie );
128 /* and now put the wrong one back in the pipe */
131 ret = write( NtCurrentTeb()->wait_fd[1], &reply, sizeof(reply) );
132 if (ret == sizeof(reply)) break;
133 if (ret >= 0) server_protocol_error( "partial wakeup write %d\n", ret );
134 if (errno == EINTR) continue;
135 server_protocol_perror("wakeup write");
139 if (ret >= 0) server_protocol_error( "partial wakeup read %d\n", ret );
140 if (errno == EINTR) continue;
141 server_protocol_perror("wakeup read");
143 /* the server closed the connection; time to die... */
144 SYSDEPS_ExitThread(0);
148 /***********************************************************************
151 * Call outstanding APCs.
153 static void call_apcs( BOOL alertable )
162 SERVER_START_REQ( get_apc )
164 req->alertable = alertable;
165 wine_server_set_reply( req, args, sizeof(args) );
166 if (!wine_server_call( req ))
177 return; /* no more APCs */
179 proc( args[0], args[1]);
185 /* convert sec/usec to NT time */
186 DOSFS_UnixTimeToFileTime( (time_t)args[0], &ft, (DWORD)args[1] * 10 );
187 proc( args[2], ft.dwLowDateTime, ft.dwHighDateTime );
190 server_protocol_error( "get_apc_request: bad type %d\n", type );
196 /***********************************************************************
199 VOID WINAPI Sleep( DWORD timeout )
201 WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
204 /******************************************************************************
205 * SleepEx (KERNEL32.@)
207 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
209 DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
210 if (ret != WAIT_IO_COMPLETION) ret = 0;
215 /***********************************************************************
216 * WaitForSingleObject (KERNEL32.@)
218 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
220 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
224 /***********************************************************************
225 * WaitForSingleObjectEx (KERNEL32.@)
227 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
230 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
234 /***********************************************************************
235 * WaitForMultipleObjects (KERNEL32.@)
237 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
238 BOOL wait_all, DWORD timeout )
240 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
244 /***********************************************************************
245 * WaitForMultipleObjectsEx (KERNEL32.@)
247 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
248 BOOL wait_all, DWORD timeout,
254 if (count > MAXIMUM_WAIT_OBJECTS)
256 SetLastError( ERROR_INVALID_PARAMETER );
260 if (timeout == INFINITE) tv.tv_sec = tv.tv_usec = 0;
261 else get_timeout( &tv, timeout );
265 SERVER_START_REQ( select )
267 req->flags = SELECT_INTERRUPTIBLE;
268 req->cookie = &cookie;
269 req->sec = tv.tv_sec;
270 req->usec = tv.tv_usec;
271 wine_server_add_data( req, handles, count * sizeof(HANDLE) );
273 if (wait_all) req->flags |= SELECT_ALL;
274 if (alertable) req->flags |= SELECT_ALERTABLE;
275 if (timeout != INFINITE) req->flags |= SELECT_TIMEOUT;
277 ret = wine_server_call( req );
280 if (ret == STATUS_PENDING) ret = wait_reply( &cookie );
281 if (ret != STATUS_USER_APC) break;
282 call_apcs( alertable );
283 if (alertable) break;
285 if (HIWORD(ret)) /* is it an error code? */
287 SetLastError( RtlNtStatusToDosError(ret) );
294 /***********************************************************************
295 * WaitForSingleObject (KERNEL.460)
297 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
299 DWORD retval, mutex_count;
301 ReleaseThunkLock( &mutex_count );
302 retval = WaitForSingleObject( handle, timeout );
303 RestoreThunkLock( mutex_count );
307 /***********************************************************************
308 * WaitForMultipleObjects (KERNEL.461)
310 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
311 BOOL wait_all, DWORD timeout )
313 DWORD retval, mutex_count;
315 ReleaseThunkLock( &mutex_count );
316 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
317 RestoreThunkLock( mutex_count );
321 /***********************************************************************
322 * WaitForMultipleObjectsEx (KERNEL.495)
324 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
325 BOOL wait_all, DWORD timeout, BOOL alertable )
327 DWORD retval, mutex_count;
329 ReleaseThunkLock( &mutex_count );
330 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
331 RestoreThunkLock( mutex_count );