Fixed resource loading.
[wine.git] / scheduler / synchro.c
blobaa98c0730ad9aa2f768a1f09930061a2626df6bb
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 "server.h"
19 /***********************************************************************
20 * call_apcs
22 * Call outstanding APCs.
24 static void call_apcs(void)
26 #define MAX_APCS 16
27 int i;
28 void *buffer[MAX_APCS * 2];
29 struct get_apcs_request *req = get_req_buffer();
31 if (server_call( REQ_GET_APCS ) || !req->count) return;
32 assert( req->count <= MAX_APCS );
33 memcpy( buffer, req->apcs, req->count * 2 * sizeof(req->apcs[0]) );
34 for (i = 0; i < req->count * 2; i += 2)
36 PAPCFUNC func = (PAPCFUNC)req->apcs[i];
37 if (func) func( (ULONG_PTR)req->apcs[i+1] );
41 /***********************************************************************
42 * Sleep (KERNEL32.679)
44 VOID WINAPI Sleep( DWORD timeout )
46 WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
49 /******************************************************************************
50 * SleepEx (KERNEL32.680)
52 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
54 DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
55 if (ret != WAIT_IO_COMPLETION) ret = 0;
56 return ret;
60 /***********************************************************************
61 * WaitForSingleObject (KERNEL32.723)
63 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
65 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
69 /***********************************************************************
70 * WaitForSingleObjectEx (KERNEL32.724)
72 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
73 BOOL alertable )
75 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
79 /***********************************************************************
80 * WaitForMultipleObjects (KERNEL32.721)
82 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
83 BOOL wait_all, DWORD timeout )
85 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
89 /***********************************************************************
90 * WaitForMultipleObjectsEx (KERNEL32.722)
92 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
93 BOOL wait_all, DWORD timeout,
94 BOOL alertable )
96 struct select_request *req = get_req_buffer();
97 int i, ret;
99 if (count > MAXIMUM_WAIT_OBJECTS)
101 SetLastError( ERROR_INVALID_PARAMETER );
102 return WAIT_FAILED;
105 req->count = count;
106 req->flags = 0;
107 req->timeout = timeout;
108 for (i = 0; i < count; i++) req->handles[i] = handles[i];
110 if (wait_all) req->flags |= SELECT_ALL;
111 if (alertable) req->flags |= SELECT_ALERTABLE;
112 if (timeout != INFINITE) req->flags |= SELECT_TIMEOUT;
114 server_call( REQ_SELECT );
115 if ((ret = req->signaled) == STATUS_USER_APC) call_apcs();
116 return ret;
120 /***********************************************************************
121 * WIN16_WaitForSingleObject (KERNEL.460)
123 DWORD WINAPI WIN16_WaitForSingleObject( HANDLE handle, DWORD timeout )
125 DWORD retval;
127 SYSLEVEL_ReleaseWin16Lock();
128 retval = WaitForSingleObject( handle, timeout );
129 SYSLEVEL_RestoreWin16Lock();
131 return retval;
134 /***********************************************************************
135 * WIN16_WaitForMultipleObjects (KERNEL.461)
137 DWORD WINAPI WIN16_WaitForMultipleObjects( DWORD count, const HANDLE *handles,
138 BOOL wait_all, DWORD timeout )
140 DWORD retval;
142 SYSLEVEL_ReleaseWin16Lock();
143 retval = WaitForMultipleObjects( count, handles, wait_all, timeout );
144 SYSLEVEL_RestoreWin16Lock();
146 return retval;
149 /***********************************************************************
150 * WIN16_WaitForMultipleObjectsEx (KERNEL.495)
152 DWORD WINAPI WIN16_WaitForMultipleObjectsEx( DWORD count,
153 const HANDLE *handles,
154 BOOL wait_all, DWORD timeout,
155 BOOL alertable )
157 DWORD retval;
159 SYSLEVEL_ReleaseWin16Lock();
160 retval = WaitForMultipleObjectsEx( count, handles,
161 wait_all, timeout, alertable );
162 SYSLEVEL_RestoreWin16Lock();
164 return retval;