Always return success instead of overflow if caller didn't specify a
[wine/multimedia.git] / scheduler / synchro.c
blob6d42029a5d30803e7be09b63e98f3839feaaf855
1 /*
2 * Win32 process and thread synchronisation
4 * Copyright 1997 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <signal.h>
9 #include <sys/time.h>
10 #include <unistd.h>
11 #include "heap.h"
12 #include "file.h" /* for DOSFS_UnixTimeToFileTime */
13 #include "thread.h"
14 #include "winerror.h"
15 #include "server.h"
18 /***********************************************************************
19 * get_timeout
21 inline static void get_timeout( struct timeval *when, int timeout )
23 gettimeofday( when, 0 );
24 if (timeout)
26 long sec = timeout / 1000;
27 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
29 when->tv_usec -= 1000000;
30 when->tv_sec++;
32 when->tv_sec += sec;
37 /***********************************************************************
38 * call_apcs
40 * Call outstanding APCs.
42 static void call_apcs( BOOL alertable )
44 FARPROC proc = NULL;
45 FILETIME ft;
46 void *args[4];
48 for (;;)
50 int type = APC_NONE;
51 SERVER_START_REQ
53 struct get_apc_request *req = server_alloc_req( sizeof(*req), sizeof(args) );
54 req->alertable = alertable;
55 if (!server_call( REQ_GET_APC ))
57 type = req->type;
58 proc = req->func;
59 memcpy( args, server_data_ptr(req), server_data_size(req) );
62 SERVER_END_REQ;
64 switch(type)
66 case APC_NONE:
67 return; /* no more APCs */
68 case APC_ASYNC:
69 proc( &args[0] );
70 break;
71 case APC_USER:
72 proc( args[0] );
73 break;
74 case APC_TIMER:
75 /* convert sec/usec to NT time */
76 DOSFS_UnixTimeToFileTime( (time_t)args[0], &ft, (DWORD)args[1] * 10 );
77 proc( args[2], ft.dwLowDateTime, ft.dwHighDateTime );
78 break;
79 default:
80 server_protocol_error( "get_apc_request: bad type %d\n", type );
81 break;
86 /***********************************************************************
87 * Sleep (KERNEL32.679)
89 VOID WINAPI Sleep( DWORD timeout )
91 WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
94 /******************************************************************************
95 * SleepEx (KERNEL32.680)
97 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
99 DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
100 if (ret != WAIT_IO_COMPLETION) ret = 0;
101 return ret;
105 /***********************************************************************
106 * WaitForSingleObject (KERNEL32.723)
108 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
110 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
114 /***********************************************************************
115 * WaitForSingleObjectEx (KERNEL32.724)
117 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
118 BOOL alertable )
120 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
124 /***********************************************************************
125 * WaitForMultipleObjects (KERNEL32.721)
127 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
128 BOOL wait_all, DWORD timeout )
130 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
134 /***********************************************************************
135 * WaitForMultipleObjectsEx (KERNEL32.722)
137 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
138 BOOL wait_all, DWORD timeout,
139 BOOL alertable )
141 int i, ret;
142 struct timeval tv;
144 if (count > MAXIMUM_WAIT_OBJECTS)
146 SetLastError( ERROR_INVALID_PARAMETER );
147 return WAIT_FAILED;
150 if (timeout == INFINITE) tv.tv_sec = tv.tv_usec = 0;
151 else get_timeout( &tv, timeout );
153 for (;;)
155 SERVER_START_REQ
157 struct select_request *req = server_alloc_req( sizeof(*req), count * sizeof(int) );
158 int *data = server_data_ptr( req );
160 req->flags = SELECT_INTERRUPTIBLE;
161 req->sec = tv.tv_sec;
162 req->usec = tv.tv_usec;
163 for (i = 0; i < count; i++) data[i] = handles[i];
165 if (wait_all) req->flags |= SELECT_ALL;
166 if (alertable) req->flags |= SELECT_ALERTABLE;
167 if (timeout != INFINITE) req->flags |= SELECT_TIMEOUT;
169 server_call( REQ_SELECT );
170 ret = req->signaled;
172 SERVER_END_REQ;
173 if (ret != STATUS_USER_APC) break;
174 call_apcs( alertable );
175 if (alertable) break;
177 return ret;
181 /***********************************************************************
182 * WIN16_WaitForSingleObject (KERNEL.460)
184 DWORD WINAPI WIN16_WaitForSingleObject( HANDLE handle, DWORD timeout )
186 DWORD retval, mutex_count;
188 ReleaseThunkLock( &mutex_count );
189 retval = WaitForSingleObject( handle, timeout );
190 RestoreThunkLock( mutex_count );
191 return retval;
194 /***********************************************************************
195 * WIN16_WaitForMultipleObjects (KERNEL.461)
197 DWORD WINAPI WIN16_WaitForMultipleObjects( DWORD count, const HANDLE *handles,
198 BOOL wait_all, DWORD timeout )
200 DWORD retval, mutex_count;
202 ReleaseThunkLock( &mutex_count );
203 retval = WaitForMultipleObjects( count, handles, wait_all, timeout );
204 RestoreThunkLock( mutex_count );
205 return retval;
208 /***********************************************************************
209 * WIN16_WaitForMultipleObjectsEx (KERNEL.495)
211 DWORD WINAPI WIN16_WaitForMultipleObjectsEx( DWORD count,
212 const HANDLE *handles,
213 BOOL wait_all, DWORD timeout,
214 BOOL alertable )
216 DWORD retval, mutex_count;
218 ReleaseThunkLock( &mutex_count );
219 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
220 RestoreThunkLock( mutex_count );
221 return retval;