Fixed a couple of HWND type mismatches.
[wine/multimedia.git] / scheduler / synchro.c
blob77907a4a4f4a9c914bb913a7abffa58b0c2d87c1
1 /*
2 * Win32 process and thread synchronisation
4 * Copyright 1997 Alexandre Julliard
5 */
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>
15 #include "file.h" /* for DOSFS_UnixTimeToFileTime */
16 #include "thread.h"
17 #include "winerror.h"
18 #include "wine/server.h"
21 /***********************************************************************
22 * get_timeout
24 inline static void get_timeout( struct timeval *when, int timeout )
26 gettimeofday( when, 0 );
27 if (timeout)
29 long sec = timeout / 1000;
30 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
32 when->tv_usec -= 1000000;
33 when->tv_sec++;
35 when->tv_sec += sec;
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,
53 ovp->lpOverlapped);
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 */
69 if(ovp->prev)
70 ovp->prev->next = ovp->next;
71 else
72 NtCurrentTeb()->pending_list = ovp->next;
74 if(ovp->next)
75 ovp->next->prev = ovp->prev;
77 ovp->next=NULL;
78 ovp->prev=NULL;
80 close(ovp->fd);
81 NtSetEvent(ovp->lpOverlapped->hEvent,NULL);
82 if(!ovp->completion_func) HeapFree(GetProcessHeap(), 0, ovp);
85 /***********************************************************************
86 * check_async_list
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;
97 int i, n, r, timeout;
98 async_private *ovp, *timeout_user;
99 struct timeval now;
101 while(1)
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];
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)
114 tmp = ovp->next;
116 if(ovp->lpOverlapped->Internal!=STATUS_PENDING)
118 finish_async(ovp,STATUS_UNSUCCESSFUL);
119 continue;
122 if(ovp->timeout && time_before(&ovp->tv,&now))
124 finish_async(ovp,STATUS_TIMEOUT);
125 continue;
128 fds[n].fd=ovp->fd;
129 fds[n].events=ovp->event;
130 fds[n].revents=0;
131 user[n] = ovp;
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;
137 timeout_user = ovp;
140 n++;
143 /* if there aren't any active asyncs return */
144 if(n==1)
145 return;
147 r = poll(fds, n, timeout);
149 /* if there were any errors, return immediately */
150 if( (r<0) || (fds[0].revents==POLLNVAL) )
151 return;
153 if( r==0 )
155 finish_async(timeout_user, STATUS_TIMEOUT);
156 continue;
159 /* search for async operations that are ready */
160 for( i=1; i<n; i++)
162 if (fds[i].revents)
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)
170 return;
175 /***********************************************************************
176 * wait_reply
178 * Wait for a reply on the waiting pipe of the current thread.
180 static int wait_reply( void *cookie )
182 int signaled;
183 struct wake_up_reply reply;
184 for (;;)
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))
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 (;;)
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");
204 return signaled;
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 /***********************************************************************
216 * call_apcs
218 * Call outstanding APCs.
220 static void call_apcs( BOOL alertable )
222 FARPROC proc = NULL;
223 FILETIME ft;
224 void *args[4];
226 for (;;)
228 int type = APC_NONE;
229 SERVER_START_VAR_REQ( get_apc, sizeof(args) )
231 req->alertable = alertable;
232 if (!SERVER_CALL())
234 type = req->type;
235 proc = req->func;
236 memcpy( args, server_data_ptr(req), server_data_size(req) );
239 SERVER_END_VAR_REQ;
241 switch(type)
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;
262 /***********************************************************************
263 * Sleep (KERNEL32.@)
265 VOID WINAPI Sleep( DWORD timeout )
267 WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
270 /******************************************************************************
271 * SleepEx (KERNEL32.@)
273 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
275 DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
276 if (ret != WAIT_IO_COMPLETION) ret = 0;
277 return ret;
281 /***********************************************************************
282 * WaitForSingleObject (KERNEL32.@)
284 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
286 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
290 /***********************************************************************
291 * WaitForSingleObjectEx (KERNEL32.@)
293 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
294 BOOL alertable )
296 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
300 /***********************************************************************
301 * WaitForMultipleObjects (KERNEL32.@)
303 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
304 BOOL wait_all, DWORD timeout )
306 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
310 /***********************************************************************
311 * WaitForMultipleObjectsEx (KERNEL32.@)
313 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
314 BOOL wait_all, DWORD timeout,
315 BOOL alertable )
317 int i, ret, cookie;
318 struct timeval tv;
320 if (count > MAXIMUM_WAIT_OBJECTS)
322 SetLastError( ERROR_INVALID_PARAMETER );
323 return WAIT_FAILED;
326 if (timeout == INFINITE) tv.tv_sec = tv.tv_usec = 0;
327 else get_timeout( &tv, timeout );
329 for (;;)
331 SERVER_START_VAR_REQ( select, count * sizeof(int) )
333 int *data = server_data_ptr( req );
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];
341 if (wait_all) req->flags |= SELECT_ALL;
342 if (alertable) req->flags |= SELECT_ALERTABLE;
343 if (timeout != INFINITE) req->flags |= SELECT_TIMEOUT;
345 ret = SERVER_CALL();
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;
353 if (HIWORD(ret)) /* is it an error code? */
355 SetLastError( RtlNtStatusToDosError(ret) );
356 ret = WAIT_FAILED;
358 return ret;
362 /***********************************************************************
363 * WaitForSingleObject (KERNEL.460)
365 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
367 DWORD retval, mutex_count;
369 ReleaseThunkLock( &mutex_count );
370 retval = WaitForSingleObject( handle, timeout );
371 RestoreThunkLock( mutex_count );
372 return retval;
375 /***********************************************************************
376 * WaitForMultipleObjects (KERNEL.461)
378 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
379 BOOL wait_all, DWORD timeout )
381 DWORD retval, mutex_count;
383 ReleaseThunkLock( &mutex_count );
384 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
385 RestoreThunkLock( mutex_count );
386 return retval;
389 /***********************************************************************
390 * WaitForMultipleObjectsEx (KERNEL.495)
392 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
393 BOOL wait_all, DWORD timeout, BOOL alertable )
395 DWORD retval, mutex_count;
397 ReleaseThunkLock( &mutex_count );
398 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
399 RestoreThunkLock( mutex_count );
400 return retval;