Fix previous patch: the ws_sockaddr_u2ws return value was wrong.
[wine.git] / scheduler / synchro.c
blob5446ce283fc958fcd7a19b223396ba62b1dec0ab
1 /*
2 * Win32 process and thread synchronisation
4 * Copyright 1997 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <errno.h>
9 #include <signal.h>
10 #include <sys/time.h>
11 #include <sys/poll.h>
12 #include <unistd.h>
13 #include <string.h>
15 #include "file.h" /* for DOSFS_UnixTimeToFileTime */
16 #include "thread.h"
17 #include "winerror.h"
18 #include "wine/server.h"
21 /***********************************************************************
22 * get_timeout
24 inline static void get_timeout( struct timeval *when, int timeout )
26 gettimeofday( when, 0 );
27 if (timeout)
29 long sec = timeout / 1000;
30 if ((when->tv_usec += (timeout - 1000*sec) * 1000) >= 1000000)
32 when->tv_usec -= 1000000;
33 when->tv_sec++;
35 when->tv_sec += sec;
39 static void CALLBACK call_completion_routine(ULONG_PTR data)
41 async_private* ovp = (async_private*)data;
43 ovp->completion_func(ovp->lpOverlapped->Internal,
44 ovp->lpOverlapped->InternalHigh,
45 ovp->lpOverlapped);
46 ovp->completion_func=NULL;
47 HeapFree(GetProcessHeap(), 0, ovp);
50 void finish_async(async_private *ovp, DWORD status)
52 ovp->lpOverlapped->Internal=status;
54 /* call ReadFileEx/WriteFileEx's overlapped completion function */
55 if(ovp->completion_func)
57 QueueUserAPC(call_completion_routine,GetCurrentThread(),(ULONG_PTR)ovp);
60 /* remove it from the active list */
61 if(ovp->prev)
62 ovp->prev->next = ovp->next;
63 else
64 NtCurrentTeb()->pending_list = ovp->next;
66 if(ovp->next)
67 ovp->next->prev = ovp->prev;
69 ovp->next=NULL;
70 ovp->prev=NULL;
72 close(ovp->fd);
73 if(!ovp->completion_func) HeapFree(GetProcessHeap(), 0, ovp);
76 /***********************************************************************
77 * check_async_list
79 * Process a status event from the server.
81 void WINAPI check_async_list(LPOVERLAPPED overlapped, DWORD status)
83 async_private *ovp;
85 /* fprintf(stderr,"overlapped %p status %x\n",overlapped,status); */
87 for(ovp = NtCurrentTeb()->pending_list; ovp; ovp = ovp->next)
88 if(ovp->lpOverlapped == overlapped)
89 break;
91 if(!ovp)
92 return;
94 if(status != STATUS_ALERTED)
95 ovp->lpOverlapped->Internal = status;
97 if(ovp->lpOverlapped->Internal==STATUS_PENDING)
99 ovp->func(ovp);
100 FILE_StartAsync(ovp->handle, ovp->lpOverlapped, ovp->type, 0, ovp->lpOverlapped->Internal);
103 if(ovp->lpOverlapped->Internal!=STATUS_PENDING)
104 finish_async(ovp,ovp->lpOverlapped->Internal);
108 /***********************************************************************
109 * wait_reply
111 * Wait for a reply on the waiting pipe of the current thread.
113 static int wait_reply( void *cookie )
115 int signaled;
116 struct wake_up_reply reply;
117 for (;;)
119 int ret;
120 ret = read( NtCurrentTeb()->wait_fd[0], &reply, sizeof(reply) );
121 if (ret == sizeof(reply))
123 if (!reply.cookie) break; /* thread got killed */
124 if (reply.cookie == cookie) return reply.signaled;
125 /* we stole another reply, wait for the real one */
126 signaled = wait_reply( cookie );
127 /* and now put the wrong one back in the pipe */
128 for (;;)
130 ret = write( NtCurrentTeb()->wait_fd[1], &reply, sizeof(reply) );
131 if (ret == sizeof(reply)) break;
132 if (ret >= 0) server_protocol_error( "partial wakeup write %d\n", ret );
133 if (errno == EINTR) continue;
134 server_protocol_perror("wakeup write");
136 return signaled;
138 if (ret >= 0) server_protocol_error( "partial wakeup read %d\n", ret );
139 if (errno == EINTR) continue;
140 server_protocol_perror("wakeup read");
142 /* the server closed the connection; time to die... */
143 SYSDEPS_AbortThread(0);
147 /***********************************************************************
148 * call_apcs
150 * Call outstanding APCs.
152 static void call_apcs( BOOL alertable )
154 FARPROC proc = NULL;
155 FILETIME ft;
156 void *args[4];
158 for (;;)
160 int type = APC_NONE;
161 SERVER_START_REQ( get_apc )
163 req->alertable = alertable;
164 wine_server_set_reply( req, args, sizeof(args) );
165 if (!wine_server_call( req ))
167 type = reply->type;
168 proc = reply->func;
171 SERVER_END_REQ;
173 switch(type)
175 case APC_NONE:
176 return; /* no more APCs */
177 case APC_ASYNC:
178 proc( args[0], args[1]);
179 break;
180 case APC_USER:
181 proc( args[0] );
182 break;
183 case APC_TIMER:
184 /* convert sec/usec to NT time */
185 DOSFS_UnixTimeToFileTime( (time_t)args[0], &ft, (DWORD)args[1] * 10 );
186 proc( args[2], ft.dwLowDateTime, ft.dwHighDateTime );
187 break;
188 default:
189 server_protocol_error( "get_apc_request: bad type %d\n", type );
190 break;
195 /***********************************************************************
196 * Sleep (KERNEL32.@)
198 VOID WINAPI Sleep( DWORD timeout )
200 WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, FALSE );
203 /******************************************************************************
204 * SleepEx (KERNEL32.@)
206 DWORD WINAPI SleepEx( DWORD timeout, BOOL alertable )
208 DWORD ret = WaitForMultipleObjectsEx( 0, NULL, FALSE, timeout, alertable );
209 if (ret != WAIT_IO_COMPLETION) ret = 0;
210 return ret;
214 /***********************************************************************
215 * WaitForSingleObject (KERNEL32.@)
217 DWORD WINAPI WaitForSingleObject( HANDLE handle, DWORD timeout )
219 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
223 /***********************************************************************
224 * WaitForSingleObjectEx (KERNEL32.@)
226 DWORD WINAPI WaitForSingleObjectEx( HANDLE handle, DWORD timeout,
227 BOOL alertable )
229 return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
233 /***********************************************************************
234 * WaitForMultipleObjects (KERNEL32.@)
236 DWORD WINAPI WaitForMultipleObjects( DWORD count, const HANDLE *handles,
237 BOOL wait_all, DWORD timeout )
239 return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
243 /***********************************************************************
244 * WaitForMultipleObjectsEx (KERNEL32.@)
246 DWORD WINAPI WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
247 BOOL wait_all, DWORD timeout,
248 BOOL alertable )
250 int ret, cookie;
251 struct timeval tv;
253 if (count > MAXIMUM_WAIT_OBJECTS)
255 SetLastError( ERROR_INVALID_PARAMETER );
256 return WAIT_FAILED;
259 if (timeout == INFINITE) tv.tv_sec = tv.tv_usec = 0;
260 else get_timeout( &tv, timeout );
262 for (;;)
264 SERVER_START_REQ( select )
266 req->flags = SELECT_INTERRUPTIBLE;
267 req->cookie = &cookie;
268 req->sec = tv.tv_sec;
269 req->usec = tv.tv_usec;
270 wine_server_add_data( req, handles, count * sizeof(HANDLE) );
272 if (wait_all) req->flags |= SELECT_ALL;
273 if (alertable) req->flags |= SELECT_ALERTABLE;
274 if (timeout != INFINITE) req->flags |= SELECT_TIMEOUT;
276 ret = wine_server_call( req );
278 SERVER_END_REQ;
279 if (ret == STATUS_PENDING) ret = wait_reply( &cookie );
280 if (ret != STATUS_USER_APC) break;
281 call_apcs( alertable );
282 if (alertable) break;
284 if (HIWORD(ret)) /* is it an error code? */
286 SetLastError( RtlNtStatusToDosError(ret) );
287 ret = WAIT_FAILED;
289 return ret;
293 /***********************************************************************
294 * WaitForSingleObject (KERNEL.460)
296 DWORD WINAPI WaitForSingleObject16( HANDLE handle, DWORD timeout )
298 DWORD retval, mutex_count;
300 ReleaseThunkLock( &mutex_count );
301 retval = WaitForSingleObject( handle, timeout );
302 RestoreThunkLock( mutex_count );
303 return retval;
306 /***********************************************************************
307 * WaitForMultipleObjects (KERNEL.461)
309 DWORD WINAPI WaitForMultipleObjects16( DWORD count, const HANDLE *handles,
310 BOOL wait_all, DWORD timeout )
312 DWORD retval, mutex_count;
314 ReleaseThunkLock( &mutex_count );
315 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
316 RestoreThunkLock( mutex_count );
317 return retval;
320 /***********************************************************************
321 * WaitForMultipleObjectsEx (KERNEL.495)
323 DWORD WINAPI WaitForMultipleObjectsEx16( DWORD count, const HANDLE *handles,
324 BOOL wait_all, DWORD timeout, BOOL alertable )
326 DWORD retval, mutex_count;
328 ReleaseThunkLock( &mutex_count );
329 retval = WaitForMultipleObjectsEx( count, handles, wait_all, timeout, alertable );
330 RestoreThunkLock( mutex_count );
331 return retval;