Change SW_??? options on list box scrolling so that they match what
[wine/testsucceed.git] / scheduler / synchro.c
blobeb262d6b234d0892fa8ce62786469c982046fc3f
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 "k32obj.h"
12 #include "heap.h"
13 #include "process.h"
14 #include "thread.h"
15 #include "winerror.h"
16 #include "syslevel.h"
17 #include "server.h"
18 #include "debug.h"
20 /***********************************************************************
21 * SYNC_BuildWaitStruct
23 static BOOL32 SYNC_BuildWaitStruct( DWORD count, const HANDLE32 *handles,
24 BOOL32 wait_all, WAIT_STRUCT *wait )
26 DWORD i;
27 K32OBJ **ptr;
29 SYSTEM_LOCK();
30 wait->count = count;
31 wait->wait_all = wait_all;
32 for (i = 0, ptr = wait->objs; i < count; i++, ptr++)
34 if (!(*ptr = HANDLE_GetObjPtr( PROCESS_Current(), handles[i],
35 K32OBJ_UNKNOWN, SYNCHRONIZE,
36 &wait->server[i] )))
38 ERR(win32, "Bad handle %08x\n", handles[i]);
39 break;
41 if (wait->server[i] == -1)
42 WARN(win32,"No server handle for %08x (type %d)\n",
43 handles[i], (*ptr)->type );
46 if (i != count)
48 /* There was an error */
49 while (i--) K32OBJ_DecCount( wait->objs[i] );
51 SYSTEM_UNLOCK();
52 return (i == count);
56 /***********************************************************************
57 * SYNC_FreeWaitStruct
59 static void SYNC_FreeWaitStruct( WAIT_STRUCT *wait )
61 DWORD i;
62 K32OBJ **ptr;
63 SYSTEM_LOCK();
64 for (i = 0, ptr = wait->objs; i < wait->count; i++, ptr++)
65 K32OBJ_DecCount( *ptr );
66 SYSTEM_UNLOCK();
69 /***********************************************************************
70 * Sleep (KERNEL32.679)
72 VOID WINAPI Sleep( DWORD timeout )
74 WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
77 /******************************************************************************
78 * SleepEx (KERNEL32.680)
80 DWORD WINAPI SleepEx( DWORD timeout, BOOL32 alertable )
82 DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
83 if (ret != WAIT_IO_COMPLETION) ret = 0;
84 return ret;
88 /***********************************************************************
89 * WaitForSingleObject (KERNEL32.723)
91 DWORD WINAPI WaitForSingleObject( HANDLE32 handle, DWORD timeout )
93 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
97 /***********************************************************************
98 * WaitForSingleObjectEx (KERNEL32.724)
100 DWORD WINAPI WaitForSingleObjectEx( HANDLE32 handle, DWORD timeout,
101 BOOL32 alertable )
103 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
107 /***********************************************************************
108 * WaitForMultipleObjects (KERNEL32.721)
110 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE32 *handles,
111 BOOL32 wait_all, DWORD timeout )
113 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
117 /***********************************************************************
118 * WaitForMultipleObjectsEx (KERNEL32.722)
120 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE32 *handles,
121 BOOL32 wait_all, DWORD timeout,
122 BOOL32 alertable )
124 WAIT_STRUCT *wait = &THREAD_Current()->wait_struct;
125 struct select_request req;
126 struct select_reply reply;
127 void *apc[32];
128 int len;
130 if (count > MAXIMUM_WAIT_OBJECTS)
132 SetLastError( ERROR_INVALID_PARAMETER );
133 return WAIT_FAILED;
136 if (!SYNC_BuildWaitStruct( count, handles, wait_all, wait ))
137 return WAIT_FAILED;
139 req.count = count;
140 req.flags = 0;
141 req.timeout = timeout;
143 if (wait_all) req.flags |= SELECT_ALL;
144 if (alertable) req.flags |= SELECT_ALERTABLE;
145 if (timeout != INFINITE32) req.flags |= SELECT_TIMEOUT;
147 CLIENT_SendRequest( REQ_SELECT, -1, 2,
148 &req, sizeof(req),
149 wait->server, count * sizeof(int) );
150 CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply),
151 apc, sizeof(apc) );
152 if ((reply.signaled == STATUS_USER_APC) && (len > sizeof(reply)))
154 int i;
155 len -= sizeof(reply);
156 for (i = 0; i < len / sizeof(void*); i += 2)
158 PAPCFUNC func = (PAPCFUNC)apc[i];
159 func( (ULONG_PTR)apc[i+1] );
162 SYNC_FreeWaitStruct( wait );
163 return reply.signaled;
167 /***********************************************************************
168 * WIN16_WaitForSingleObject (KERNEL.460)
170 DWORD WINAPI WIN16_WaitForSingleObject( HANDLE32 handle, DWORD timeout )
172 DWORD retval;
174 SYSLEVEL_ReleaseWin16Lock();
175 retval = WaitForSingleObject( handle, timeout );
176 SYSLEVEL_RestoreWin16Lock();
178 return retval;
181 /***********************************************************************
182 * WIN16_WaitForMultipleObjects (KERNEL.461)
184 DWORD WINAPI WIN16_WaitForMultipleObjects( DWORD count, const HANDLE32 *handles,
185 BOOL32 wait_all, DWORD timeout )
187 DWORD retval;
189 SYSLEVEL_ReleaseWin16Lock();
190 retval = WaitForMultipleObjects( count, handles, wait_all, timeout );
191 SYSLEVEL_RestoreWin16Lock();
193 return retval;
196 /***********************************************************************
197 * WIN16_WaitForMultipleObjectsEx (KERNEL.495)
199 DWORD WINAPI WIN16_WaitForMultipleObjectsEx( DWORD count,
200 const HANDLE32 *handles,
201 BOOL32 wait_all, DWORD timeout,
202 BOOL32 alertable )
204 DWORD retval;
206 SYSLEVEL_ReleaseWin16Lock();
207 retval = WaitForMultipleObjectsEx( count, handles,
208 wait_all, timeout, alertable );
209 SYSLEVEL_RestoreWin16Lock();
211 return retval;