Implemented DosFileHandleToWin32Handle, Win32HandleToDosFileHandle and
[wine/multimedia.git] / scheduler / synchro.c
blob5718b1afa33991856685aa5663aeb467f4eca654
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 * call_apcs
21 * Call outstanding APCs.
23 static void call_apcs(void)
25 FARPROC proc = NULL;
26 FILETIME ft;
27 void *args[4];
29 for (;;)
31 int type = APC_NONE;
32 SERVER_START_REQ
34 struct get_apc_request *req = server_alloc_req( sizeof(*req), sizeof(args) );
35 if (!server_call( REQ_GET_APC ))
37 type = req->type;
38 proc = req->func;
39 memcpy( args, server_data_ptr(req), server_data_size(req) );
42 SERVER_END_REQ;
44 switch(type)
46 case APC_NONE:
47 return; /* no more APCs */
48 case APC_ASYNC:
49 proc( &args[0] );
50 break;
51 case APC_USER:
52 proc( args[0] );
53 break;
54 case APC_TIMER:
55 /* convert sec/usec to NT time */
56 DOSFS_UnixTimeToFileTime( (time_t)args[0], &ft, (DWORD)args[1] * 10 );
57 proc( args[2], ft.dwLowDateTime, ft.dwHighDateTime );
58 break;
59 default:
60 server_protocol_error( "get_apc_request: bad type %d\n", type );
61 break;
66 /***********************************************************************
67 * Sleep (KERNEL32.679)
69 VOID WINAPI Sleep( DWORD timeout )
71 WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
74 /******************************************************************************
75 * SleepEx (KERNEL32.680)
77 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
79 DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
80 if (ret != WAIT_IO_COMPLETION) ret = 0;
81 return ret;
85 /***********************************************************************
86 * WaitForSingleObject (KERNEL32.723)
88 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
90 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
94 /***********************************************************************
95 * WaitForSingleObjectEx (KERNEL32.724)
97 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
98 BOOL alertable )
100 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
104 /***********************************************************************
105 * WaitForMultipleObjects (KERNEL32.721)
107 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
108 BOOL wait_all, DWORD timeout )
110 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
114 /***********************************************************************
115 * WaitForMultipleObjectsEx (KERNEL32.722)
117 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
118 BOOL wait_all, DWORD timeout,
119 BOOL alertable )
121 int i, ret;
123 if (count > MAXIMUM_WAIT_OBJECTS)
125 SetLastError( ERROR_INVALID_PARAMETER );
126 return WAIT_FAILED;
129 SERVER_START_REQ
131 struct select_request *req = server_alloc_req( sizeof(*req), count * sizeof(int) );
132 int *data = server_data_ptr( req );
134 req->flags = 0;
135 req->timeout = timeout;
136 for (i = 0; i < count; i++) data[i] = handles[i];
138 if (wait_all) req->flags |= SELECT_ALL;
139 if (alertable) req->flags |= SELECT_ALERTABLE;
140 if (timeout != INFINITE) req->flags |= SELECT_TIMEOUT;
142 server_call( REQ_SELECT );
143 ret = req->signaled;
145 SERVER_END_REQ;
146 if (ret == STATUS_USER_APC) call_apcs();
147 return ret;
151 /***********************************************************************
152 * WIN16_WaitForSingleObject (KERNEL.460)
154 DWORD WINAPI WIN16_WaitForSingleObject( HANDLE handle, DWORD timeout )
156 DWORD retval, mutex_count;
158 ReleaseThunkLock( &mutex_count );
159 retval = WaitForSingleObject( handle, timeout );
160 RestoreThunkLock( mutex_count );
161 return retval;
164 /***********************************************************************
165 * WIN16_WaitForMultipleObjects (KERNEL.461)
167 DWORD WINAPI WIN16_WaitForMultipleObjects( DWORD count, const HANDLE *handles,
168 BOOL wait_all, DWORD timeout )
170 DWORD retval, mutex_count;
172 ReleaseThunkLock( &mutex_count );
173 retval = WaitForMultipleObjects( count, handles, wait_all, timeout );
174 RestoreThunkLock( mutex_count );
175 return retval;
178 /***********************************************************************
179 * WIN16_WaitForMultipleObjectsEx (KERNEL.495)
181 DWORD WINAPI WIN16_WaitForMultipleObjectsEx( DWORD count,
182 const HANDLE *handles,
183 BOOL wait_all, DWORD timeout,
184 BOOL alertable )
186 DWORD retval, mutex_count;
188 ReleaseThunkLock( &mutex_count );
189 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
190 RestoreThunkLock( mutex_count );
191 return retval;