2 * Win32 process and thread synchronisation
4 * Copyright 1997 Alexandre Julliard
15 #include "file.h" /* for DOSFS_UnixTimeToFileTime */
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 #define MAX_NUMBER_OF_FDS 20
41 static inline int time_before( struct timeval
*t1
, struct timeval
*t2
)
43 return ((t1
->tv_sec
< t2
->tv_sec
) ||
44 ((t1
->tv_sec
== t2
->tv_sec
) && (t1
->tv_usec
< t2
->tv_usec
)));
47 static void CALLBACK
call_completion_routine(ULONG_PTR data
)
49 async_private
* ovp
= (async_private
*)data
;
51 ovp
->completion_func(ovp
->lpOverlapped
->Internal
,
52 ovp
->lpOverlapped
->InternalHigh
,
54 ovp
->completion_func
=NULL
;
55 HeapFree(GetProcessHeap(), 0, ovp
);
58 static void finish_async(async_private
*ovp
, DWORD status
)
60 ovp
->lpOverlapped
->Internal
=status
;
62 /* call ReadFileEx/WriteFileEx's overlapped completion function */
63 if(ovp
->completion_func
)
65 QueueUserAPC(call_completion_routine
,GetCurrentThread(),(ULONG_PTR
)ovp
);
68 /* remove it from the active list */
70 ovp
->prev
->next
= ovp
->next
;
72 NtCurrentTeb()->pending_list
= ovp
->next
;
75 ovp
->next
->prev
= ovp
->prev
;
81 NtSetEvent(ovp
->lpOverlapped
->hEvent
,NULL
);
82 if(!ovp
->completion_func
) HeapFree(GetProcessHeap(), 0, ovp
);
85 /***********************************************************************
88 * Create a list of fds for poll to check while waiting on the server
89 * FIXME: this loop is too large, cut into smaller functions
90 * perhaps we could share/steal some of the code in server/select.c?
92 static void check_async_list(void)
94 /* FIXME: should really malloc these two arrays */
95 struct pollfd fds
[MAX_NUMBER_OF_FDS
];
96 async_private
*user
[MAX_NUMBER_OF_FDS
], *tmp
;
98 async_private
*ovp
, *timeout_user
;
103 /* the first fd belongs to the server connection */
104 fds
[0].events
=POLLIN
;
106 fds
[0].fd
= NtCurrentTeb()->wait_fd
[0];
108 ovp
= NtCurrentTeb()->pending_list
;
111 gettimeofday(&now
,NULL
);
112 for(n
=1; ovp
&& (n
<MAX_NUMBER_OF_FDS
); ovp
= tmp
)
116 if(ovp
->lpOverlapped
->Internal
!=STATUS_PENDING
)
118 finish_async(ovp
,STATUS_UNSUCCESSFUL
);
122 if(ovp
->timeout
&& time_before(&ovp
->tv
,&now
))
124 finish_async(ovp
,STATUS_TIMEOUT
);
129 fds
[n
].events
=ovp
->event
;
133 if(ovp
->timeout
&& ( (!timeout_user
) || time_before(&ovp
->tv
,&timeout_user
->tv
)))
135 timeout
= (ovp
->tv
.tv_sec
- now
.tv_sec
) * 1000
136 + (ovp
->tv
.tv_usec
- now
.tv_usec
) / 1000;
143 /* if there aren't any active asyncs return */
147 r
= poll(fds
, n
, timeout
);
149 /* if there were any errors, return immediately */
150 if( (r
<0) || (fds
[0].revents
==POLLNVAL
) )
155 finish_async(timeout_user
, STATUS_TIMEOUT
);
159 /* search for async operations that are ready */
163 user
[i
]->func(user
[i
],fds
[i
].revents
);
165 if(user
[i
]->lpOverlapped
->Internal
!=STATUS_PENDING
)
166 finish_async(user
[i
],user
[i
]->lpOverlapped
->Internal
);
169 if(fds
[0].revents
== POLLIN
)
175 /***********************************************************************
178 * Wait for a reply on the waiting pipe of the current thread.
180 static int wait_reply( void *cookie
)
183 struct wake_up_reply reply
;
187 if (NtCurrentTeb()->pending_list
) check_async_list();
188 ret
= read( NtCurrentTeb()->wait_fd
[0], &reply
, sizeof(reply
) );
189 if (ret
== sizeof(reply
))
191 if (!reply
.cookie
) break; /* thread got killed */
192 if (reply
.cookie
== cookie
) return reply
.signaled
;
193 /* we stole another reply, wait for the real one */
194 signaled
= wait_reply( cookie
);
195 /* and now put the wrong one back in the pipe */
198 ret
= write( NtCurrentTeb()->wait_fd
[1], &reply
, sizeof(reply
) );
199 if (ret
== sizeof(reply
)) break;
200 if (ret
>= 0) server_protocol_error( "partial wakeup write %d\n", ret
);
201 if (errno
== EINTR
) continue;
202 server_protocol_perror("wakeup write");
206 if (ret
>= 0) server_protocol_error( "partial wakeup read %d\n", ret
);
207 if (errno
== EINTR
) continue;
208 server_protocol_perror("wakeup read");
210 /* the server closed the connection; time to die... */
211 SYSDEPS_ExitThread(0);
215 /***********************************************************************
218 * Call outstanding APCs.
220 static void call_apcs( BOOL alertable
)
229 SERVER_START_VAR_REQ( get_apc
, sizeof(args
) )
231 req
->alertable
= alertable
;
236 memcpy( args
, server_data_ptr(req
), server_data_size(req
) );
244 return; /* no more APCs */
251 /* convert sec/usec to NT time */
252 DOSFS_UnixTimeToFileTime( (time_t)args
[0], &ft
, (DWORD
)args
[1] * 10 );
253 proc( args
[2], ft
.dwLowDateTime
, ft
.dwHighDateTime
);
256 server_protocol_error( "get_apc_request: bad type %d\n", type
);
262 /***********************************************************************
265 VOID WINAPI
Sleep( DWORD timeout
)
267 WaitForMultipleObjectsEx( 0, NULL
, FALSE
, timeout
, FALSE
);
270 /******************************************************************************
271 * SleepEx (KERNEL32.@)
273 DWORD WINAPI
SleepEx( DWORD timeout
, BOOL alertable
)
275 DWORD ret
= WaitForMultipleObjectsEx( 0, NULL
, FALSE
, timeout
, alertable
);
276 if (ret
!= WAIT_IO_COMPLETION
) ret
= 0;
281 /***********************************************************************
282 * WaitForSingleObject (KERNEL32.@)
284 DWORD WINAPI
WaitForSingleObject( HANDLE handle
, DWORD timeout
)
286 return WaitForMultipleObjectsEx( 1, &handle
, FALSE
, timeout
, FALSE
);
290 /***********************************************************************
291 * WaitForSingleObjectEx (KERNEL32.@)
293 DWORD WINAPI
WaitForSingleObjectEx( HANDLE handle
, DWORD timeout
,
296 return WaitForMultipleObjectsEx( 1, &handle
, FALSE
, timeout
, alertable
);
300 /***********************************************************************
301 * WaitForMultipleObjects (KERNEL32.@)
303 DWORD WINAPI
WaitForMultipleObjects( DWORD count
, const HANDLE
*handles
,
304 BOOL wait_all
, DWORD timeout
)
306 return WaitForMultipleObjectsEx( count
, handles
, wait_all
, timeout
, FALSE
);
310 /***********************************************************************
311 * WaitForMultipleObjectsEx (KERNEL32.@)
313 DWORD WINAPI
WaitForMultipleObjectsEx( DWORD count
, const HANDLE
*handles
,
314 BOOL wait_all
, DWORD timeout
,
320 if (count
> MAXIMUM_WAIT_OBJECTS
)
322 SetLastError( ERROR_INVALID_PARAMETER
);
326 if (timeout
== INFINITE
) tv
.tv_sec
= tv
.tv_usec
= 0;
327 else get_timeout( &tv
, timeout
);
331 SERVER_START_VAR_REQ( select
, count
* sizeof(int) )
333 int *data
= server_data_ptr( req
);
335 req
->flags
= SELECT_INTERRUPTIBLE
;
336 req
->cookie
= &cookie
;
337 req
->sec
= tv
.tv_sec
;
338 req
->usec
= tv
.tv_usec
;
339 for (i
= 0; i
< count
; i
++) data
[i
] = handles
[i
];
341 if (wait_all
) req
->flags
|= SELECT_ALL
;
342 if (alertable
) req
->flags
|= SELECT_ALERTABLE
;
343 if (timeout
!= INFINITE
) req
->flags
|= SELECT_TIMEOUT
;
348 if (ret
== STATUS_PENDING
) ret
= wait_reply( &cookie
);
349 if (ret
!= STATUS_USER_APC
) break;
350 call_apcs( alertable
);
351 if (alertable
) break;
353 if (HIWORD(ret
)) /* is it an error code? */
355 SetLastError( RtlNtStatusToDosError(ret
) );
362 /***********************************************************************
363 * WaitForSingleObject (KERNEL.460)
365 DWORD WINAPI
WaitForSingleObject16( HANDLE handle
, DWORD timeout
)
367 DWORD retval
, mutex_count
;
369 ReleaseThunkLock( &mutex_count
);
370 retval
= WaitForSingleObject( handle
, timeout
);
371 RestoreThunkLock( mutex_count
);
375 /***********************************************************************
376 * WaitForMultipleObjects (KERNEL.461)
378 DWORD WINAPI
WaitForMultipleObjects16( DWORD count
, const HANDLE
*handles
,
379 BOOL wait_all
, DWORD timeout
)
381 DWORD retval
, mutex_count
;
383 ReleaseThunkLock( &mutex_count
);
384 retval
= WaitForMultipleObjectsEx( count
, handles
, wait_all
, timeout
, FALSE
);
385 RestoreThunkLock( mutex_count
);
389 /***********************************************************************
390 * WaitForMultipleObjectsEx (KERNEL.495)
392 DWORD WINAPI
WaitForMultipleObjectsEx16( DWORD count
, const HANDLE
*handles
,
393 BOOL wait_all
, DWORD timeout
, BOOL alertable
)
395 DWORD retval
, mutex_count
;
397 ReleaseThunkLock( &mutex_count
);
398 retval
= WaitForMultipleObjectsEx( count
, handles
, wait_all
, timeout
, alertable
);
399 RestoreThunkLock( mutex_count
);