Added LGPL standard comment, and copyright notices where necessary.
[wine/multimedia.git] / scheduler / synchro.c
blob065620b68f724a262e0993c77944c02b51656037
1 /*
2 * Win32 process and thread synchronisation
4 * Copyright 1997 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <assert.h>
22 #include <errno.h>
23 #include <signal.h>
24 #include <sys/time.h>
25 #include <sys/poll.h>
26 #include <unistd.h>
27 #include <string.h>
29 #include "file.h" /* for DOSFS_UnixTimeToFileTime */
30 #include "thread.h"
31 #include "winerror.h"
32 #include "wine/server.h"
35 /***********************************************************************
36 * get_timeout
38 inline static void get_timeout( struct timeval *when, int timeout )
40 gettimeofday( when, 0 );
41 if (timeout)
43 long sec = timeout / 1000;
44 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
46 when->tv_usec -= 1000000;
47 when->tv_sec++;
49 when->tv_sec += sec;
53 static void CALLBACK call_completion_routine(ULONG_PTR data)
55 async_private* ovp = (async_private*)data;
57 ovp->completion_func(ovp->lpOverlapped->Internal,
58 ovp->lpOverlapped->InternalHigh,
59 ovp->lpOverlapped);
60 ovp->completion_func=NULL;
61 HeapFree(GetProcessHeap(), 0, ovp);
64 void finish_async(async_private *ovp, DWORD status)
66 ovp->lpOverlapped->Internal=status;
68 /* call ReadFileEx/WriteFileEx's overlapped completion function */
69 if(ovp->completion_func)
71 QueueUserAPC(call_completion_routine,GetCurrentThread(),(ULONG_PTR)ovp);
74 /* remove it from the active list */
75 if(ovp->prev)
76 ovp->prev->next = ovp->next;
77 else
78 NtCurrentTeb()->pending_list = ovp->next;
80 if(ovp->next)
81 ovp->next->prev = ovp->prev;
83 ovp->next=NULL;
84 ovp->prev=NULL;
86 close(ovp->fd);
87 if(!ovp->completion_func) HeapFree(GetProcessHeap(), 0, ovp);
90 /***********************************************************************
91 * check_async_list
93 * Process a status event from the server.
95 void WINAPI check_async_list(LPOVERLAPPED overlapped, DWORD status)
97 async_private *ovp;
99 /* fprintf(stderr,"overlapped %p status %x\n",overlapped,status); */
101 for(ovp = NtCurrentTeb()->pending_list; ovp; ovp = ovp->next)
102 if(ovp->lpOverlapped == overlapped)
103 break;
105 if(!ovp)
106 return;
108 if(status != STATUS_ALERTED)
109 ovp->lpOverlapped->Internal = status;
111 if(ovp->lpOverlapped->Internal==STATUS_PENDING)
113 ovp->func(ovp);
114 FILE_StartAsync(ovp->handle, ovp->lpOverlapped, ovp->type, 0, ovp->lpOverlapped->Internal);
117 if(ovp->lpOverlapped->Internal!=STATUS_PENDING)
118 finish_async(ovp,ovp->lpOverlapped->Internal);
122 /***********************************************************************
123 * wait_reply
125 * Wait for a reply on the waiting pipe of the current thread.
127 static int wait_reply( void *cookie )
129 int signaled;
130 struct wake_up_reply reply;
131 for (;;)
133 int ret;
134 ret = read( NtCurrentTeb()->wait_fd[0], &reply, sizeof(reply) );
135 if (ret == sizeof(reply))
137 if (!reply.cookie) break; /* thread got killed */
138 if (reply.cookie == cookie) return reply.signaled;
139 /* we stole another reply, wait for the real one */
140 signaled = wait_reply( cookie );
141 /* and now put the wrong one back in the pipe */
142 for (;;)
144 ret = write( NtCurrentTeb()->wait_fd[1], &reply, sizeof(reply) );
145 if (ret == sizeof(reply)) break;
146 if (ret >= 0) server_protocol_error( "partial wakeup write %d\n", ret );
147 if (errno == EINTR) continue;
148 server_protocol_perror("wakeup write");
150 return signaled;
152 if (ret >= 0) server_protocol_error( "partial wakeup read %d\n", ret );
153 if (errno == EINTR) continue;
154 server_protocol_perror("wakeup read");
156 /* the server closed the connection; time to die... */
157 SYSDEPS_AbortThread(0);
161 /***********************************************************************
162 * call_apcs
164 * Call outstanding APCs.
166 static void call_apcs( BOOL alertable )
168 FARPROC proc = NULL;
169 FILETIME ft;
170 void *args[4];
172 for (;;)
174 int type = APC_NONE;
175 SERVER_START_REQ( get_apc )
177 req->alertable = alertable;
178 wine_server_set_reply( req, args, sizeof(args) );
179 if (!wine_server_call( req ))
181 type = reply->type;
182 proc = reply->func;
185 SERVER_END_REQ;
187 switch(type)
189 case APC_NONE:
190 return; /* no more APCs */
191 case APC_ASYNC:
192 proc( args[0], args[1]);
193 break;
194 case APC_USER:
195 proc( args[0] );
196 break;
197 case APC_TIMER:
198 /* convert sec/usec to NT time */
199 DOSFS_UnixTimeToFileTime( (time_t)args[0], &ft, (DWORD)args[1] * 10 );
200 proc( args[2], ft.dwLowDateTime, ft.dwHighDateTime );
201 break;
202 default:
203 server_protocol_error( "get_apc_request: bad type %d\n", type );
204 break;
209 /***********************************************************************
210 * Sleep (KERNEL32.@)
212 VOID WINAPI Sleep( DWORD timeout )
214 WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
217 /******************************************************************************
218 * SleepEx (KERNEL32.@)
220 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
222 DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
223 if (ret != WAIT_IO_COMPLETION) ret = 0;
224 return ret;
228 /***********************************************************************
229 * WaitForSingleObject (KERNEL32.@)
231 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
233 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
237 /***********************************************************************
238 * WaitForSingleObjectEx (KERNEL32.@)
240 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
241 BOOL alertable )
243 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
247 /***********************************************************************
248 * WaitForMultipleObjects (KERNEL32.@)
250 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
251 BOOL wait_all, DWORD timeout )
253 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
257 /***********************************************************************
258 * WaitForMultipleObjectsEx (KERNEL32.@)
260 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
261 BOOL wait_all, DWORD timeout,
262 BOOL alertable )
264 int ret, cookie;
265 struct timeval tv;
267 if (count > MAXIMUM_WAIT_OBJECTS)
269 SetLastError( ERROR_INVALID_PARAMETER );
270 return WAIT_FAILED;
273 if (timeout == INFINITE) tv.tv_sec = tv.tv_usec = 0;
274 else get_timeout( &tv, timeout );
276 for (;;)
278 SERVER_START_REQ( select )
280 req->flags = SELECT_INTERRUPTIBLE;
281 req->cookie = &cookie;
282 req->sec = tv.tv_sec;
283 req->usec = tv.tv_usec;
284 wine_server_add_data( req, handles, count * sizeof(HANDLE) );
286 if (wait_all) req->flags |= SELECT_ALL;
287 if (alertable) req->flags |= SELECT_ALERTABLE;
288 if (timeout != INFINITE) req->flags |= SELECT_TIMEOUT;
290 ret = wine_server_call( req );
292 SERVER_END_REQ;
293 if (ret == STATUS_PENDING) ret = wait_reply( &cookie );
294 if (ret != STATUS_USER_APC) break;
295 call_apcs( alertable );
296 if (alertable) break;
298 if (HIWORD(ret)) /* is it an error code? */
300 SetLastError( RtlNtStatusToDosError(ret) );
301 ret = WAIT_FAILED;
303 return ret;
307 /***********************************************************************
308 * WaitForSingleObject (KERNEL.460)
310 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
312 DWORD retval, mutex_count;
314 ReleaseThunkLock( &mutex_count );
315 retval = WaitForSingleObject( handle, timeout );
316 RestoreThunkLock( mutex_count );
317 return retval;
320 /***********************************************************************
321 * WaitForMultipleObjects (KERNEL.461)
323 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
324 BOOL wait_all, DWORD timeout )
326 DWORD retval, mutex_count;
328 ReleaseThunkLock( &mutex_count );
329 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
330 RestoreThunkLock( mutex_count );
331 return retval;
334 /***********************************************************************
335 * WaitForMultipleObjectsEx (KERNEL.495)
337 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
338 BOOL wait_all, DWORD timeout, BOOL alertable )
340 DWORD retval, mutex_count;
342 ReleaseThunkLock( &mutex_count );
343 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
344 RestoreThunkLock( mutex_count );
345 return retval;