2 * Win32 process and thread synchronisation
4 * Copyright 1997 Alexandre Julliard
15 #include "file.h" /* for DOSFS_UnixTimeToFileTime */
18 #include "wine/server.h"
21 /***********************************************************************
24 inline static void get_timeout( struct timeval
*when
, int timeout
)
26 gettimeofday( when
, 0 );
29 long sec
= timeout
/ 1000;
30 if ((when
->tv_usec
+= (timeout
- 1000*sec
) * 1000) >= 1000000)
32 when
->tv_usec
-= 1000000;
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
,
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 */
62 ovp
->prev
->next
= ovp
->next
;
64 NtCurrentTeb()->pending_list
= ovp
->next
;
67 ovp
->next
->prev
= ovp
->prev
;
73 if(!ovp
->completion_func
) HeapFree(GetProcessHeap(), 0, ovp
);
76 /***********************************************************************
79 * Process a status event from the server.
81 void WINAPI
check_async_list(LPOVERLAPPED overlapped
, DWORD status
)
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
)
94 if(status
!= STATUS_ALERTED
)
95 ovp
->lpOverlapped
->Internal
= status
;
97 if(ovp
->lpOverlapped
->Internal
==STATUS_PENDING
)
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 /***********************************************************************
111 * Wait for a reply on the waiting pipe of the current thread.
113 static int wait_reply( void *cookie
)
116 struct wake_up_reply reply
;
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 */
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");
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 /***********************************************************************
150 * Call outstanding APCs.
152 static void call_apcs( BOOL alertable
)
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
))
176 return; /* no more APCs */
178 proc( args
[0], args
[1]);
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
);
189 server_protocol_error( "get_apc_request: bad type %d\n", type
);
195 /***********************************************************************
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;
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
,
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
,
253 if (count
> MAXIMUM_WAIT_OBJECTS
)
255 SetLastError( ERROR_INVALID_PARAMETER
);
259 if (timeout
== INFINITE
) tv
.tv_sec
= tv
.tv_usec
= 0;
260 else get_timeout( &tv
, timeout
);
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
);
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
) );
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
);
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
);
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
);