2 * wait.c: wait for handles to become signalled
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
15 #include <mono/os/gc_wrapper.h>
17 #include <mono/io-layer/wapi.h>
18 #include <mono/io-layer/handles-private.h>
19 #include <mono/io-layer/wapi-private.h>
20 #include <mono/io-layer/mono-mutex.h>
21 #include <mono/io-layer/misc-private.h>
25 static gboolean own_if_signalled(gpointer handle)
29 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
30 if (_wapi_handle_trylock_shared_handles () == EBUSY) {
35 if (_wapi_handle_issignalled (handle)) {
36 _wapi_handle_ops_own (handle);
40 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
41 _wapi_handle_unlock_shared_handles ();
47 static gboolean own_if_owned(gpointer handle)
51 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
52 if (_wapi_handle_trylock_shared_handles () == EBUSY) {
57 if (_wapi_handle_ops_isowned (handle)) {
58 _wapi_handle_ops_own (handle);
62 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
63 _wapi_handle_unlock_shared_handles ();
70 * WaitForSingleObjectEx:
71 * @handle: an object to wait for
72 * @timeout: the maximum time in milliseconds to wait for
73 * @alertable: if TRUE, the wait can be interrupted by an APC call
75 * This function returns when either @handle is signalled, or @timeout
76 * ms elapses. If @timeout is zero, the object's state is tested and
77 * the function returns immediately. If @timeout is %INFINITE, the
78 * function waits forever.
80 * Return value: %WAIT_ABANDONED - @handle is a mutex that was not
81 * released by the owning thread when it exited. Ownership of the
82 * mutex object is granted to the calling thread and the mutex is set
83 * to nonsignalled. %WAIT_OBJECT_0 - The state of @handle is
84 * signalled. %WAIT_TIMEOUT - The @timeout interval elapsed and
85 * @handle's state is still not signalled. %WAIT_FAILED - an error
86 * occurred. %WAIT_IO_COMPLETION - the wait was ended by an APC.
88 guint32 WaitForSingleObjectEx(gpointer handle, guint32 timeout,
92 struct timespec abstime;
94 gboolean apc_pending = FALSE;
95 gpointer current_thread = GetCurrentThread ();
97 if (_wapi_handle_test_capabilities (handle,
98 WAPI_HANDLE_CAP_WAIT) == FALSE) {
100 g_message ("%s: handle %p can't be waited for", __func__,
107 if (_wapi_handle_test_capabilities (handle, WAPI_HANDLE_CAP_SPECIAL_WAIT) == TRUE) {
109 g_message ("%s: handle %p has special wait", __func__, handle);
112 ret = _wapi_handle_ops_special_wait (handle, timeout);
114 if (alertable && _wapi_thread_apc_pending (current_thread)) {
116 ret = WAIT_IO_COMPLETION;
124 g_message ("%s: locking handle %p", __func__, handle);
127 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
129 thr_ret = _wapi_handle_lock_handle (handle);
130 g_assert (thr_ret == 0);
132 if (_wapi_handle_test_capabilities (handle,
133 WAPI_HANDLE_CAP_OWN) == TRUE) {
134 if (own_if_owned (handle) == TRUE) {
136 g_message ("%s: handle %p already owned", __func__,
144 if (alertable && _wapi_thread_apc_pending (current_thread)) {
146 ret = WAIT_IO_COMPLETION;
150 if (own_if_signalled (handle) == TRUE) {
152 g_message ("%s: handle %p already signalled", __func__,
160 /* Have to wait for it */
161 if (timeout != INFINITE) {
162 _wapi_calc_timeout (&abstime, timeout);
166 /* Check before waiting on the condition, just in case
168 if (own_if_signalled (handle)) {
170 g_message ("%s: handle %p signalled", __func__,
178 if (timeout == INFINITE) {
179 waited = _wapi_handle_wait_signal_handle (handle);
181 waited = _wapi_handle_timedwait_signal_handle (handle, &abstime);
185 apc_pending = _wapi_thread_apc_pending (current_thread);
187 if(waited==0 && !apc_pending) {
188 /* Condition was signalled, so hopefully
189 * handle is signalled now. (It might not be
190 * if someone else got in before us.)
192 if (own_if_signalled (handle)) {
194 g_message ("%s: handle %p signalled", __func__,
202 /* Better luck next time */
204 } while(waited == 0 && !apc_pending);
206 /* Timeout or other error */
208 g_message ("%s: wait on handle %p error: %s", __func__, handle,
217 g_message ("%s: unlocking handle %p", __func__, handle);
220 thr_ret = _wapi_handle_unlock_handle (handle);
221 g_assert (thr_ret == 0);
222 pthread_cleanup_pop (0);
226 _wapi_thread_dispatch_apc_queue (current_thread);
227 ret = WAIT_IO_COMPLETION;
233 guint32 WaitForSingleObject(gpointer handle, guint32 timeout)
235 return WaitForSingleObjectEx (handle, timeout, FALSE);
240 * SignalObjectAndWait:
241 * @signal_handle: An object to signal
242 * @wait: An object to wait for
243 * @timeout: The maximum time in milliseconds to wait for
244 * @alertable: Specifies whether the function returnes when the system
245 * queues an I/O completion routine or an APC for the calling thread.
247 * Atomically signals @signal and waits for @wait to become signalled,
248 * or @timeout ms elapses. If @timeout is zero, the object's state is
249 * tested and the function returns immediately. If @timeout is
250 * %INFINITE, the function waits forever.
252 * @signal can be a semaphore, mutex or event object.
254 * If @alertable is %TRUE and the system queues an I/O completion
255 * routine or an APC for the calling thread, the function returns and
256 * the thread calls the completion routine or APC function. If
257 * %FALSE, the function does not return, and the thread does not call
258 * the completion routine or APC function. A completion routine is
259 * queued when the ReadFileEx() or WriteFileEx() function in which it
260 * was specified has completed. The calling thread is the thread that
261 * initiated the read or write operation. An APC is queued when
262 * QueueUserAPC() is called. Currently completion routines and APC
263 * functions are not supported.
265 * Return value: %WAIT_ABANDONED - @wait is a mutex that was not
266 * released by the owning thread when it exited. Ownershop of the
267 * mutex object is granted to the calling thread and the mutex is set
268 * to nonsignalled. %WAIT_IO_COMPLETION - the wait was ended by one
269 * or more user-mode asynchronous procedure calls queued to the
270 * thread. %WAIT_OBJECT_0 - The state of @wait is signalled.
271 * %WAIT_TIMEOUT - The @timeout interval elapsed and @wait's state is
272 * still not signalled. %WAIT_FAILED - an error occurred.
274 guint32 SignalObjectAndWait(gpointer signal_handle, gpointer wait,
275 guint32 timeout, gboolean alertable)
278 struct timespec abstime;
280 gboolean apc_pending = FALSE;
281 gpointer current_thread = GetCurrentThread ();
283 if (_wapi_handle_test_capabilities (signal_handle,
284 WAPI_HANDLE_CAP_SIGNAL)==FALSE) {
288 if (_wapi_handle_test_capabilities (wait,
289 WAPI_HANDLE_CAP_WAIT)==FALSE) {
293 if (_wapi_handle_test_capabilities (wait, WAPI_HANDLE_CAP_SPECIAL_WAIT) == TRUE) {
294 g_warning ("%s: handle %p has special wait, implement me!!",
297 return (WAIT_FAILED);
301 g_message ("%s: locking handle %p", __func__, wait);
304 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
306 thr_ret = _wapi_handle_lock_handle (wait);
307 g_assert (thr_ret == 0);
309 _wapi_handle_ops_signal (signal_handle);
311 if (_wapi_handle_test_capabilities (wait, WAPI_HANDLE_CAP_OWN)==TRUE) {
312 if (own_if_owned (wait)) {
314 g_message ("%s: handle %p already owned", __func__,
322 if (alertable && _wapi_thread_apc_pending (current_thread)) {
324 ret = WAIT_IO_COMPLETION;
328 if (own_if_signalled (wait)) {
330 g_message ("%s: handle %p already signalled", __func__, wait);
337 /* Have to wait for it */
338 if (timeout != INFINITE) {
339 _wapi_calc_timeout (&abstime, timeout);
343 /* Check before waiting on the condition, just in case
345 if (own_if_signalled (wait)) {
347 g_message ("%s: handle %p signalled", __func__, wait);
354 if (timeout == INFINITE) {
355 waited = _wapi_handle_wait_signal_handle (wait);
357 waited = _wapi_handle_timedwait_signal_handle (wait, &abstime);
361 apc_pending = _wapi_thread_apc_pending (current_thread);
364 if (waited==0 && !apc_pending) {
365 /* Condition was signalled, so hopefully
366 * handle is signalled now. (It might not be
367 * if someone else got in before us.)
369 if (own_if_signalled (wait)) {
371 g_message ("%s: handle %p signalled", __func__,
379 /* Better luck next time */
381 } while(waited == 0 && !apc_pending);
383 /* Timeout or other error */
385 g_message ("%s: wait on handle %p error: %s", __func__, wait,
394 g_message ("%s: unlocking handle %p", __func__, wait);
397 thr_ret = _wapi_handle_unlock_handle (wait);
398 g_assert (thr_ret == 0);
399 pthread_cleanup_pop (0);
402 _wapi_thread_dispatch_apc_queue (current_thread);
403 ret = WAIT_IO_COMPLETION;
409 struct handle_cleanup_data
415 static void handle_cleanup (void *data)
417 struct handle_cleanup_data *handles = (struct handle_cleanup_data *)data;
419 _wapi_handle_unlock_handles (handles->numobjects, handles->handles);
422 static gboolean test_and_own (guint32 numobjects, gpointer *handles,
423 gboolean waitall, guint32 *count,
426 struct handle_cleanup_data cleanup_data;
431 g_message ("%s: locking handles", __func__);
433 cleanup_data.numobjects = numobjects;
434 cleanup_data.handles = handles;
436 pthread_cleanup_push (handle_cleanup, (void *)&cleanup_data);
437 done = _wapi_handle_count_signalled_handles (numobjects, handles,
438 waitall, count, lowest);
440 if (waitall == TRUE) {
441 for (i = 0; i < numobjects; i++) {
442 own_if_signalled (handles[i]);
445 own_if_signalled (handles[*lowest]);
450 g_message ("%s: unlocking handles", __func__);
453 /* calls the unlock function */
454 pthread_cleanup_pop (1);
462 * WaitForMultipleObjectsEx:
463 * @numobjects: The number of objects in @handles. The maximum allowed
464 * is %MAXIMUM_WAIT_OBJECTS.
465 * @handles: An array of object handles. Duplicates are not allowed.
466 * @waitall: If %TRUE, this function waits until all of the handles
467 * are signalled. If %FALSE, this function returns when any object is
469 * @timeout: The maximum time in milliseconds to wait for.
470 * @alertable: if TRUE, the wait can be interrupted by an APC call
472 * This function returns when either one or more of @handles is
473 * signalled, or @timeout ms elapses. If @timeout is zero, the state
474 * of each item of @handles is tested and the function returns
475 * immediately. If @timeout is %INFINITE, the function waits forever.
477 * Return value: %WAIT_OBJECT_0 to %WAIT_OBJECT_0 + @numobjects - 1 -
478 * if @waitall is %TRUE, indicates that all objects are signalled. If
479 * @waitall is %FALSE, the return value minus %WAIT_OBJECT_0 indicates
480 * the first index into @handles of the objects that are signalled.
481 * %WAIT_ABANDONED_0 to %WAIT_ABANDONED_0 + @numobjects - 1 - if
482 * @waitall is %TRUE, indicates that all objects are signalled, and at
483 * least one object is an abandoned mutex object (See
484 * WaitForSingleObject() for a description of abandoned mutexes.) If
485 * @waitall is %FALSE, the return value minus %WAIT_ABANDONED_0
486 * indicates the first index into @handles of an abandoned mutex.
487 * %WAIT_TIMEOUT - The @timeout interval elapsed and no objects in
488 * @handles are signalled. %WAIT_FAILED - an error occurred.
489 * %WAIT_IO_COMPLETION - the wait was ended by an APC.
491 guint32 WaitForMultipleObjectsEx(guint32 numobjects, gpointer *handles,
492 gboolean waitall, guint32 timeout,
496 gboolean duplicate = FALSE, bogustype = FALSE, done;
497 gboolean shared_wait = FALSE;
498 guint32 count, lowest;
499 struct timespec abstime;
503 gpointer current_thread = GetCurrentThread ();
505 if (numobjects > MAXIMUM_WAIT_OBJECTS) {
507 g_message ("%s: Too many handles: %d", __func__, numobjects);
513 if (numobjects == 1) {
514 return WaitForSingleObjectEx (handles [0], timeout, alertable);
517 /* Check for duplicates */
518 dups = g_hash_table_new (g_direct_hash, g_direct_equal);
519 for (i = 0; i < numobjects; i++) {
520 gpointer exists = g_hash_table_lookup (dups, handles[i]);
521 if (exists != NULL) {
523 g_message ("%s: Handle %p duplicated", __func__,
531 if (_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_WAIT) == FALSE) {
533 g_message ("%s: Handle %p can't be waited for",
534 __func__, handles[i]);
540 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handles[i]))) {
544 g_hash_table_insert (dups, handles[i], handles[i]);
546 g_hash_table_destroy (dups);
548 if (duplicate == TRUE) {
550 g_message ("%s: Returning due to duplicates", __func__);
556 if (bogustype == TRUE) {
558 g_message ("%s: Returning due to bogus type", __func__);
564 done = test_and_own (numobjects, handles, waitall, &count, &lowest);
566 return(WAIT_OBJECT_0+lowest);
569 /* Have to wait for some or all handles to become signalled
572 if(timeout!=INFINITE) {
573 _wapi_calc_timeout (&abstime, timeout);
576 if (alertable && _wapi_thread_apc_pending (current_thread)) {
577 _wapi_thread_dispatch_apc_queue (current_thread);
578 return WAIT_IO_COMPLETION;
582 /* Prod all special-wait handles that aren't already
585 for (i = 0; i < numobjects; i++) {
586 if (_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_SPECIAL_WAIT) == TRUE && _wapi_handle_issignalled (handles[i]) == FALSE) {
587 _wapi_handle_ops_special_wait (handles[i], 0);
591 /* Check before waiting on the condition, just in case
593 done = test_and_own (numobjects, handles, waitall,
596 return(WAIT_OBJECT_0 + lowest);
600 g_message ("%s: locking signal mutex", __func__);
603 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_signal_mutex, NULL);
604 thr_ret = _wapi_handle_lock_signal_mutex ();
605 g_assert (thr_ret == 0);
607 if (shared_wait == TRUE) {
608 if (timeout == INFINITE) {
609 ret = _wapi_handle_wait_signal_poll_share ();
611 ret = _wapi_handle_timedwait_signal_poll_share (&abstime);
614 if (timeout == INFINITE) {
615 ret = _wapi_handle_wait_signal ();
617 ret = _wapi_handle_timedwait_signal (&abstime);
622 g_message ("%s: unlocking signal mutex", __func__);
625 thr_ret = _wapi_handle_unlock_signal_mutex (NULL);
626 g_assert (thr_ret == 0);
627 pthread_cleanup_pop (0);
629 if (alertable && _wapi_thread_apc_pending (current_thread)) {
630 _wapi_thread_dispatch_apc_queue (current_thread);
631 return WAIT_IO_COMPLETION;
634 /* Check if everything is signalled, as we can't
635 * guarantee to notice a shared signal even if the
638 done = test_and_own (numobjects, handles, waitall,
641 return(WAIT_OBJECT_0+lowest);
642 } else if (ret != 0) {
643 /* Didn't get all handles, and there was a
644 * timeout or other error
647 g_message ("%s: wait returned error: %s", __func__,
652 return(WAIT_TIMEOUT);
660 guint32 WaitForMultipleObjects(guint32 numobjects, gpointer *handles,
661 gboolean waitall, guint32 timeout)
663 return WaitForMultipleObjectsEx(numobjects, handles, waitall, timeout, FALSE);