Updated to reflect current status.
[wine/testsucceed.git] / scheduler / synchro.c
blob7c57e5168818c3b3c36268e354bacaeb8d868a4f
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 "process.h"
13 #include "thread.h"
14 #include "winerror.h"
15 #include "syslevel.h"
16 #include "message.h"
17 #include "x11drv.h"
18 #include "server.h"
20 /***********************************************************************
21 * call_apcs
23 * Call outstanding APCs.
25 static void call_apcs(void)
27 #define MAX_APCS 16
28 int i;
29 void *buffer[MAX_APCS * 2];
30 struct get_apcs_request *req = get_req_buffer();
32 if (server_call( REQ_GET_APCS ) || !req->count) return;
33 assert( req->count <= MAX_APCS );
34 memcpy( buffer, req->apcs, req->count * 2 * sizeof(req->apcs[0]) );
35 for (i = 0; i < req->count * 2; i += 2)
37 PAPCFUNC func = (PAPCFUNC)req->apcs[i];
38 if (func) func( (ULONG_PTR)req->apcs[i+1] );
42 /***********************************************************************
43 * Sleep (KERNEL32.679)
45 VOID WINAPI Sleep( DWORD timeout )
47 WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
50 /******************************************************************************
51 * SleepEx (KERNEL32.680)
53 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
55 DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
56 if (ret != WAIT_IO_COMPLETION) ret = 0;
57 return ret;
61 /***********************************************************************
62 * WaitForSingleObject (KERNEL32.723)
64 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
66 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
70 /***********************************************************************
71 * WaitForSingleObjectEx (KERNEL32.724)
73 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
74 BOOL alertable )
76 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
80 /***********************************************************************
81 * WaitForMultipleObjects (KERNEL32.721)
83 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
84 BOOL wait_all, DWORD timeout )
86 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
90 /***********************************************************************
91 * WaitForMultipleObjectsEx (KERNEL32.722)
93 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
94 BOOL wait_all, DWORD timeout,
95 BOOL alertable )
97 struct select_request *req = get_req_buffer();
98 int i, ret;
100 if (count > MAXIMUM_WAIT_OBJECTS)
102 SetLastError( ERROR_INVALID_PARAMETER );
103 return WAIT_FAILED;
106 /* FIXME: This is extremely ugly, but needed to avoid endless
107 * recursion due to EVENT_Synchronize itself using
108 * EnterCriticalSection( &X11DRV_CritSection ) ...
110 if ( count == 0 || handles[0] != X11DRV_CritSection.LockSemaphore )
112 /* Before we might possibly block, we need to push outstanding
113 * graphics output to the X server ... This needs to be done
114 * here so that it also works with native USER.
116 if ( timeout != 0 )
117 EVENT_Synchronize( FALSE );
120 req->count = count;
121 req->flags = 0;
122 req->timeout = timeout;
123 for (i = 0; i < count; i++) req->handles[i] = handles[i];
125 if (wait_all) req->flags |= SELECT_ALL;
126 if (alertable) req->flags |= SELECT_ALERTABLE;
127 if (timeout != INFINITE) req->flags |= SELECT_TIMEOUT;
129 server_call( REQ_SELECT );
130 if ((ret = req->signaled) == STATUS_USER_APC) call_apcs();
131 return ret;
135 /***********************************************************************
136 * WIN16_WaitForSingleObject (KERNEL.460)
138 DWORD WINAPI WIN16_WaitForSingleObject( HANDLE handle, DWORD timeout )
140 DWORD retval;
142 SYSLEVEL_ReleaseWin16Lock();
143 retval = WaitForSingleObject( handle, timeout );
144 SYSLEVEL_RestoreWin16Lock();
146 return retval;
149 /***********************************************************************
150 * WIN16_WaitForMultipleObjects (KERNEL.461)
152 DWORD WINAPI WIN16_WaitForMultipleObjects( DWORD count, const HANDLE *handles,
153 BOOL wait_all, DWORD timeout )
155 DWORD retval;
157 SYSLEVEL_ReleaseWin16Lock();
158 retval = WaitForMultipleObjects( count, handles, wait_all, timeout );
159 SYSLEVEL_RestoreWin16Lock();
161 return retval;
164 /***********************************************************************
165 * WIN16_WaitForMultipleObjectsEx (KERNEL.495)
167 DWORD WINAPI WIN16_WaitForMultipleObjectsEx( DWORD count,
168 const HANDLE *handles,
169 BOOL wait_all, DWORD timeout,
170 BOOL alertable )
172 DWORD retval;
174 SYSLEVEL_ReleaseWin16Lock();
175 retval = WaitForMultipleObjectsEx( count, handles,
176 wait_all, timeout, alertable );
177 SYSLEVEL_RestoreWin16Lock();
179 return retval;