Large-scale renaming of all Win32 functions and types to use the
[wine/hacks.git] / scheduler / synchro.c
blob9a23e53ebbe44c371bd1f403998523975d7e5a89
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"
21 /***********************************************************************
22 * Sleep (KERNEL32.679)
24 VOID WINAPI Sleep( DWORD timeout )
26 WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
29 /******************************************************************************
30 * SleepEx (KERNEL32.680)
32 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
34 DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
35 if (ret != WAIT_IO_COMPLETION) ret = 0;
36 return ret;
40 /***********************************************************************
41 * WaitForSingleObject (KERNEL32.723)
43 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
45 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
49 /***********************************************************************
50 * WaitForSingleObjectEx (KERNEL32.724)
52 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
53 BOOL alertable )
55 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
59 /***********************************************************************
60 * WaitForMultipleObjects (KERNEL32.721)
62 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
63 BOOL wait_all, DWORD timeout )
65 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
69 /***********************************************************************
70 * WaitForMultipleObjectsEx (KERNEL32.722)
72 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
73 BOOL wait_all, DWORD timeout,
74 BOOL alertable )
76 struct select_request req;
77 struct select_reply reply;
78 int server_handle[MAXIMUM_WAIT_OBJECTS];
79 void *apc[32];
80 int i, len;
82 if (count > MAXIMUM_WAIT_OBJECTS)
84 SetLastError( ERROR_INVALID_PARAMETER );
85 return WAIT_FAILED;
88 for (i = 0; i < count; i++)
90 TRACE(win32,"handle %d is %08x\n",i,handles[i]);
91 server_handle[i] = HANDLE_GetServerHandle( PROCESS_Current(), handles[i],
92 K32OBJ_UNKNOWN, SYNCHRONIZE );
93 if (server_handle[i] == -1)
95 ERR(win32,"No server handle for %08x\n",handles[i] );
96 return WAIT_FAILED;
100 req.count = count;
101 req.flags = 0;
102 req.timeout = timeout;
104 if (wait_all) req.flags |= SELECT_ALL;
105 if (alertable) req.flags |= SELECT_ALERTABLE;
106 if (timeout != INFINITE) req.flags |= SELECT_TIMEOUT;
108 CLIENT_SendRequest( REQ_SELECT, -1, 2,
109 &req, sizeof(req),
110 server_handle, count * sizeof(int) );
111 CLIENT_WaitReply( &len, NULL, 2, &reply, sizeof(reply),
112 apc, sizeof(apc) );
113 if ((reply.signaled == STATUS_USER_APC) && (len > sizeof(reply)))
115 int i;
116 len -= sizeof(reply);
117 for (i = 0; i < len / sizeof(void*); i += 2)
119 PAPCFUNC func = (PAPCFUNC)apc[i];
120 func( (ULONG_PTR)apc[i+1] );
123 return reply.signaled;
127 /***********************************************************************
128 * WIN16_WaitForSingleObject (KERNEL.460)
130 DWORD WINAPI WIN16_WaitForSingleObject( HANDLE handle, DWORD timeout )
132 DWORD retval;
134 SYSLEVEL_ReleaseWin16Lock();
135 retval = WaitForSingleObject( handle, timeout );
136 SYSLEVEL_RestoreWin16Lock();
138 return retval;
141 /***********************************************************************
142 * WIN16_WaitForMultipleObjects (KERNEL.461)
144 DWORD WINAPI WIN16_WaitForMultipleObjects( DWORD count, const HANDLE *handles,
145 BOOL wait_all, DWORD timeout )
147 DWORD retval;
149 SYSLEVEL_ReleaseWin16Lock();
150 retval = WaitForMultipleObjects( count, handles, wait_all, timeout );
151 SYSLEVEL_RestoreWin16Lock();
153 return retval;
156 /***********************************************************************
157 * WIN16_WaitForMultipleObjectsEx (KERNEL.495)
159 DWORD WINAPI WIN16_WaitForMultipleObjectsEx( DWORD count,
160 const HANDLE *handles,
161 BOOL wait_all, DWORD timeout,
162 BOOL alertable )
164 DWORD retval;
166 SYSLEVEL_ReleaseWin16Lock();
167 retval = WaitForMultipleObjectsEx( count, handles,
168 wait_all, timeout, alertable );
169 SYSLEVEL_RestoreWin16Lock();
171 return retval;