Implemented DuplicateIcon().
[wine/gsoc_dplay.git] / scheduler / synchro.c
blob0f0f2faf4917f674983a110136d5ef47f86f34ed
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 "thread.h"
13 #include "winerror.h"
14 #include "syslevel.h"
15 #include "server.h"
17 /***********************************************************************
18 * call_apcs
20 * Call outstanding APCs.
22 static void call_apcs(void)
24 #define MAX_APCS 16
25 int i;
26 void *buffer[MAX_APCS * 2];
27 int count;
28 struct get_apcs_request *req = get_req_buffer();
30 if (server_call( REQ_GET_APCS ) || !req->count) return;
31 assert( req->count <= MAX_APCS );
33 /* make a copy of the request buffer... it may get trashed *
34 * when the apcs are called */
35 memcpy( buffer, req->apcs, req->count * 2 * sizeof(req->apcs[0]) );
36 count = req->count;
38 for (i = 0; i < count * 2; i += 2)
40 PAPCFUNC func = (PAPCFUNC)buffer[i];
41 if (func) func( (ULONG_PTR)buffer[i+1] );
45 /***********************************************************************
46 * Sleep (KERNEL32.679)
48 VOID WINAPI Sleep( DWORD timeout )
50 WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
53 /******************************************************************************
54 * SleepEx (KERNEL32.680)
56 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
58 DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
59 if (ret != WAIT_IO_COMPLETION) ret = 0;
60 return ret;
64 /***********************************************************************
65 * WaitForSingleObject (KERNEL32.723)
67 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
69 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
73 /***********************************************************************
74 * WaitForSingleObjectEx (KERNEL32.724)
76 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
77 BOOL alertable )
79 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
83 /***********************************************************************
84 * WaitForMultipleObjects (KERNEL32.721)
86 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
87 BOOL wait_all, DWORD timeout )
89 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
93 /***********************************************************************
94 * WaitForMultipleObjectsEx (KERNEL32.722)
96 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
97 BOOL wait_all, DWORD timeout,
98 BOOL alertable )
100 struct select_request *req = get_req_buffer();
101 int i, ret;
103 if (count > MAXIMUM_WAIT_OBJECTS)
105 SetLastError( ERROR_INVALID_PARAMETER );
106 return WAIT_FAILED;
109 req->count = count;
110 req->flags = 0;
111 req->timeout = timeout;
112 for (i = 0; i < count; i++) req->handles[i] = handles[i];
114 if (wait_all) req->flags |= SELECT_ALL;
115 if (alertable) req->flags |= SELECT_ALERTABLE;
116 if (timeout != INFINITE) req->flags |= SELECT_TIMEOUT;
118 server_call( REQ_SELECT );
119 if ((ret = req->signaled) == STATUS_USER_APC) call_apcs();
120 return ret;
124 /***********************************************************************
125 * WIN16_WaitForSingleObject (KERNEL.460)
127 DWORD WINAPI WIN16_WaitForSingleObject( HANDLE handle, DWORD timeout )
129 DWORD retval;
131 SYSLEVEL_ReleaseWin16Lock();
132 retval = WaitForSingleObject( handle, timeout );
133 SYSLEVEL_RestoreWin16Lock();
135 return retval;
138 /***********************************************************************
139 * WIN16_WaitForMultipleObjects (KERNEL.461)
141 DWORD WINAPI WIN16_WaitForMultipleObjects( DWORD count, const HANDLE *handles,
142 BOOL wait_all, DWORD timeout )
144 DWORD retval;
146 SYSLEVEL_ReleaseWin16Lock();
147 retval = WaitForMultipleObjects( count, handles, wait_all, timeout );
148 SYSLEVEL_RestoreWin16Lock();
150 return retval;
153 /***********************************************************************
154 * WIN16_WaitForMultipleObjectsEx (KERNEL.495)
156 DWORD WINAPI WIN16_WaitForMultipleObjectsEx( DWORD count,
157 const HANDLE *handles,
158 BOOL wait_all, DWORD timeout,
159 BOOL alertable )
161 DWORD retval;
163 SYSLEVEL_ReleaseWin16Lock();
164 retval = WaitForMultipleObjectsEx( count, handles,
165 wait_all, timeout, alertable );
166 SYSLEVEL_RestoreWin16Lock();
168 return retval;