Prepare switching to unicode of builtin widgets.
[wine.git] / scheduler / synchro.c
blobf5d447001e229b223930260db6cd75005cfebb14
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 "syslevel.h"
16 #include "server.h"
19 /***********************************************************************
20 * call_apcs
22 * Call outstanding APCs.
24 static void call_apcs(void)
26 FARPROC proc = NULL;
27 FILETIME ft;
28 void *args[4];
30 for (;;)
32 int type = APC_NONE;
33 SERVER_START_REQ
35 struct get_apc_request *req = server_alloc_req( sizeof(*req), sizeof(args) );
36 if (!server_call( REQ_GET_APC ))
38 type = req->type;
39 proc = req->func;
40 memcpy( args, server_data_ptr(req), server_data_size(req) );
43 SERVER_END_REQ;
45 switch(type)
47 case APC_NONE:
48 return; /* no more APCs */
49 case APC_ASYNC:
50 proc( &args[0] );
51 break;
52 case APC_USER:
53 proc( args[0] );
54 break;
55 case APC_TIMER:
56 /* convert sec/usec to NT time */
57 DOSFS_UnixTimeToFileTime( (time_t)args[0], &ft, (DWORD)args[1] * 10 );
58 proc( args[2], ft.dwLowDateTime, ft.dwHighDateTime );
59 break;
60 default:
61 server_protocol_error( "get_apc_request: bad type %d\n", type );
62 break;
67 /***********************************************************************
68 * Sleep (KERNEL32.679)
70 VOID WINAPI Sleep( DWORD timeout )
72 WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
75 /******************************************************************************
76 * SleepEx (KERNEL32.680)
78 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
80 DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
81 if (ret != WAIT_IO_COMPLETION) ret = 0;
82 return ret;
86 /***********************************************************************
87 * WaitForSingleObject (KERNEL32.723)
89 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
91 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
95 /***********************************************************************
96 * WaitForSingleObjectEx (KERNEL32.724)
98 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
99 BOOL alertable )
101 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
105 /***********************************************************************
106 * WaitForMultipleObjects (KERNEL32.721)
108 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
109 BOOL wait_all, DWORD timeout )
111 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
115 /***********************************************************************
116 * WaitForMultipleObjectsEx (KERNEL32.722)
118 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
119 BOOL wait_all, DWORD timeout,
120 BOOL alertable )
122 int i, ret;
124 if (count > MAXIMUM_WAIT_OBJECTS)
126 SetLastError( ERROR_INVALID_PARAMETER );
127 return WAIT_FAILED;
130 SERVER_START_REQ
132 struct select_request *req = server_alloc_req( sizeof(*req), count * sizeof(int) );
133 int *data = server_data_ptr( req );
135 req->flags = 0;
136 req->timeout = timeout;
137 for (i = 0; i < count; i++) data[i] = handles[i];
139 if (wait_all) req->flags |= SELECT_ALL;
140 if (alertable) req->flags |= SELECT_ALERTABLE;
141 if (timeout != INFINITE) req->flags |= SELECT_TIMEOUT;
143 server_call( REQ_SELECT );
144 ret = req->signaled;
146 SERVER_END_REQ;
147 if (ret == STATUS_USER_APC) call_apcs();
148 return ret;
152 /***********************************************************************
153 * WIN16_WaitForSingleObject (KERNEL.460)
155 DWORD WINAPI WIN16_WaitForSingleObject( HANDLE handle, DWORD timeout )
157 DWORD retval;
159 SYSLEVEL_ReleaseWin16Lock();
160 retval = WaitForSingleObject( handle, timeout );
161 SYSLEVEL_RestoreWin16Lock();
163 return retval;
166 /***********************************************************************
167 * WIN16_WaitForMultipleObjects (KERNEL.461)
169 DWORD WINAPI WIN16_WaitForMultipleObjects( DWORD count, const HANDLE *handles,
170 BOOL wait_all, DWORD timeout )
172 DWORD retval;
174 SYSLEVEL_ReleaseWin16Lock();
175 retval = WaitForMultipleObjects( count, handles, wait_all, timeout );
176 SYSLEVEL_RestoreWin16Lock();
178 return retval;
181 /***********************************************************************
182 * WIN16_WaitForMultipleObjectsEx (KERNEL.495)
184 DWORD WINAPI WIN16_WaitForMultipleObjectsEx( DWORD count,
185 const HANDLE *handles,
186 BOOL wait_all, DWORD timeout,
187 BOOL alertable )
189 DWORD retval;
191 SYSLEVEL_ReleaseWin16Lock();
192 retval = WaitForMultipleObjectsEx( count, handles,
193 wait_all, timeout, alertable );
194 SYSLEVEL_RestoreWin16Lock();
196 return retval;