2 * wait.c: wait for handles to become signalled
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002-2006 Novell, 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
= _wapi_thread_handle_from_id (pthread_self ());
97 if (current_thread
== NULL
) {
98 SetLastError (ERROR_INVALID_HANDLE
);
102 if (handle
== _WAPI_THREAD_CURRENT
) {
103 handle
= _wapi_thread_handle_from_id (pthread_self ());
104 if (handle
== NULL
) {
105 SetLastError (ERROR_INVALID_HANDLE
);
110 if (_wapi_handle_test_capabilities (handle
,
111 WAPI_HANDLE_CAP_WAIT
) == FALSE
) {
113 g_message ("%s: handle %p can't be waited for", __func__
,
120 _wapi_handle_ops_prewait (handle
);
122 if (_wapi_handle_test_capabilities (handle
, WAPI_HANDLE_CAP_SPECIAL_WAIT
) == TRUE
) {
124 g_message ("%s: handle %p has special wait", __func__
, handle
);
127 ret
= _wapi_handle_ops_special_wait (handle
, timeout
);
129 if (alertable
&& _wapi_thread_apc_pending (current_thread
)) {
131 ret
= WAIT_IO_COMPLETION
;
139 g_message ("%s: locking handle %p", __func__
, handle
);
142 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle
,
144 thr_ret
= _wapi_handle_lock_handle (handle
);
145 g_assert (thr_ret
== 0);
147 if (_wapi_handle_test_capabilities (handle
,
148 WAPI_HANDLE_CAP_OWN
) == TRUE
) {
149 if (own_if_owned (handle
) == TRUE
) {
151 g_message ("%s: handle %p already owned", __func__
,
159 if (alertable
&& _wapi_thread_apc_pending (current_thread
)) {
161 ret
= WAIT_IO_COMPLETION
;
165 if (own_if_signalled (handle
) == TRUE
) {
167 g_message ("%s: handle %p already signalled", __func__
,
179 /* Have to wait for it */
180 if (timeout
!= INFINITE
) {
181 _wapi_calc_timeout (&abstime
, timeout
);
185 /* Check before waiting on the condition, just in case
187 _wapi_handle_ops_prewait (handle
);
189 if (own_if_signalled (handle
)) {
191 g_message ("%s: handle %p signalled", __func__
,
199 if (timeout
== INFINITE
) {
200 waited
= _wapi_handle_wait_signal_handle (handle
, alertable
);
202 waited
= _wapi_handle_timedwait_signal_handle (handle
, &abstime
, alertable
);
206 apc_pending
= _wapi_thread_apc_pending (current_thread
);
208 if(waited
==0 && !apc_pending
) {
209 /* Condition was signalled, so hopefully
210 * handle is signalled now. (It might not be
211 * if someone else got in before us.)
213 if (own_if_signalled (handle
)) {
215 g_message ("%s: handle %p signalled", __func__
,
223 /* Better luck next time */
225 } while(waited
== 0 && !apc_pending
);
227 /* Timeout or other error */
229 g_message ("%s: wait on handle %p error: %s", __func__
, handle
,
238 g_message ("%s: unlocking handle %p", __func__
, handle
);
241 thr_ret
= _wapi_handle_unlock_handle (handle
);
242 g_assert (thr_ret
== 0);
243 pthread_cleanup_pop (0);
247 _wapi_thread_dispatch_apc_queue (current_thread
);
248 ret
= WAIT_IO_COMPLETION
;
254 guint32
WaitForSingleObject(gpointer handle
, guint32 timeout
)
256 return WaitForSingleObjectEx (handle
, timeout
, FALSE
);
261 * SignalObjectAndWait:
262 * @signal_handle: An object to signal
263 * @wait: An object to wait for
264 * @timeout: The maximum time in milliseconds to wait for
265 * @alertable: Specifies whether the function returnes when the system
266 * queues an I/O completion routine or an APC for the calling thread.
268 * Atomically signals @signal and waits for @wait to become signalled,
269 * or @timeout ms elapses. If @timeout is zero, the object's state is
270 * tested and the function returns immediately. If @timeout is
271 * %INFINITE, the function waits forever.
273 * @signal can be a semaphore, mutex or event object.
275 * If @alertable is %TRUE and the system queues an I/O completion
276 * routine or an APC for the calling thread, the function returns and
277 * the thread calls the completion routine or APC function. If
278 * %FALSE, the function does not return, and the thread does not call
279 * the completion routine or APC function. A completion routine is
280 * queued when the ReadFileEx() or WriteFileEx() function in which it
281 * was specified has completed. The calling thread is the thread that
282 * initiated the read or write operation. An APC is queued when
283 * QueueUserAPC() is called. Currently completion routines and APC
284 * functions are not supported.
286 * Return value: %WAIT_ABANDONED - @wait is a mutex that was not
287 * released by the owning thread when it exited. Ownershop of the
288 * mutex object is granted to the calling thread and the mutex is set
289 * to nonsignalled. %WAIT_IO_COMPLETION - the wait was ended by one
290 * or more user-mode asynchronous procedure calls queued to the
291 * thread. %WAIT_OBJECT_0 - The state of @wait is signalled.
292 * %WAIT_TIMEOUT - The @timeout interval elapsed and @wait's state is
293 * still not signalled. %WAIT_FAILED - an error occurred.
295 guint32
SignalObjectAndWait(gpointer signal_handle
, gpointer wait
,
296 guint32 timeout
, gboolean alertable
)
299 struct timespec abstime
;
301 gboolean apc_pending
= FALSE
;
302 gpointer current_thread
= _wapi_thread_handle_from_id (pthread_self ());
304 if (current_thread
== NULL
) {
305 SetLastError (ERROR_INVALID_HANDLE
);
309 if (signal_handle
== _WAPI_THREAD_CURRENT
) {
310 signal_handle
= _wapi_thread_handle_from_id (pthread_self ());
311 if (signal_handle
== NULL
) {
312 SetLastError (ERROR_INVALID_HANDLE
);
317 if (wait
== _WAPI_THREAD_CURRENT
) {
318 wait
= _wapi_thread_handle_from_id (pthread_self ());
320 SetLastError (ERROR_INVALID_HANDLE
);
325 if (_wapi_handle_test_capabilities (signal_handle
,
326 WAPI_HANDLE_CAP_SIGNAL
)==FALSE
) {
330 if (_wapi_handle_test_capabilities (wait
,
331 WAPI_HANDLE_CAP_WAIT
)==FALSE
) {
335 _wapi_handle_ops_prewait (wait
);
337 if (_wapi_handle_test_capabilities (wait
, WAPI_HANDLE_CAP_SPECIAL_WAIT
) == TRUE
) {
338 g_warning ("%s: handle %p has special wait, implement me!!",
341 return (WAIT_FAILED
);
345 g_message ("%s: locking handle %p", __func__
, wait
);
348 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle
,
350 thr_ret
= _wapi_handle_lock_handle (wait
);
351 g_assert (thr_ret
== 0);
353 _wapi_handle_ops_signal (signal_handle
);
355 if (_wapi_handle_test_capabilities (wait
, WAPI_HANDLE_CAP_OWN
)==TRUE
) {
356 if (own_if_owned (wait
)) {
358 g_message ("%s: handle %p already owned", __func__
,
366 if (alertable
&& _wapi_thread_apc_pending (current_thread
)) {
368 ret
= WAIT_IO_COMPLETION
;
372 if (own_if_signalled (wait
)) {
374 g_message ("%s: handle %p already signalled", __func__
, wait
);
381 /* Have to wait for it */
382 if (timeout
!= INFINITE
) {
383 _wapi_calc_timeout (&abstime
, timeout
);
387 /* Check before waiting on the condition, just in case
389 _wapi_handle_ops_prewait (wait
);
391 if (own_if_signalled (wait
)) {
393 g_message ("%s: handle %p signalled", __func__
, wait
);
400 if (timeout
== INFINITE
) {
401 waited
= _wapi_handle_wait_signal_handle (wait
, alertable
);
403 waited
= _wapi_handle_timedwait_signal_handle (wait
, &abstime
, alertable
);
407 apc_pending
= _wapi_thread_apc_pending (current_thread
);
410 if (waited
==0 && !apc_pending
) {
411 /* Condition was signalled, so hopefully
412 * handle is signalled now. (It might not be
413 * if someone else got in before us.)
415 if (own_if_signalled (wait
)) {
417 g_message ("%s: handle %p signalled", __func__
,
425 /* Better luck next time */
427 } while(waited
== 0 && !apc_pending
);
429 /* Timeout or other error */
431 g_message ("%s: wait on handle %p error: %s", __func__
, wait
,
440 g_message ("%s: unlocking handle %p", __func__
, wait
);
443 thr_ret
= _wapi_handle_unlock_handle (wait
);
444 g_assert (thr_ret
== 0);
445 pthread_cleanup_pop (0);
448 _wapi_thread_dispatch_apc_queue (current_thread
);
449 ret
= WAIT_IO_COMPLETION
;
455 struct handle_cleanup_data
461 static void handle_cleanup (void *data
)
463 struct handle_cleanup_data
*handles
= (struct handle_cleanup_data
*)data
;
465 _wapi_handle_unlock_handles (handles
->numobjects
, handles
->handles
);
468 static gboolean
test_and_own (guint32 numobjects
, gpointer
*handles
,
469 gboolean waitall
, guint32
*count
,
472 struct handle_cleanup_data cleanup_data
;
477 g_message ("%s: locking handles", __func__
);
479 cleanup_data
.numobjects
= numobjects
;
480 cleanup_data
.handles
= handles
;
482 pthread_cleanup_push (handle_cleanup
, (void *)&cleanup_data
);
483 done
= _wapi_handle_count_signalled_handles (numobjects
, handles
,
484 waitall
, count
, lowest
);
486 if (waitall
== TRUE
) {
487 for (i
= 0; i
< numobjects
; i
++) {
488 own_if_signalled (handles
[i
]);
491 own_if_signalled (handles
[*lowest
]);
496 g_message ("%s: unlocking handles", __func__
);
499 /* calls the unlock function */
500 pthread_cleanup_pop (1);
508 * WaitForMultipleObjectsEx:
509 * @numobjects: The number of objects in @handles. The maximum allowed
510 * is %MAXIMUM_WAIT_OBJECTS.
511 * @handles: An array of object handles. Duplicates are not allowed.
512 * @waitall: If %TRUE, this function waits until all of the handles
513 * are signalled. If %FALSE, this function returns when any object is
515 * @timeout: The maximum time in milliseconds to wait for.
516 * @alertable: if TRUE, the wait can be interrupted by an APC call
518 * This function returns when either one or more of @handles is
519 * signalled, or @timeout ms elapses. If @timeout is zero, the state
520 * of each item of @handles is tested and the function returns
521 * immediately. If @timeout is %INFINITE, the function waits forever.
523 * Return value: %WAIT_OBJECT_0 to %WAIT_OBJECT_0 + @numobjects - 1 -
524 * if @waitall is %TRUE, indicates that all objects are signalled. If
525 * @waitall is %FALSE, the return value minus %WAIT_OBJECT_0 indicates
526 * the first index into @handles of the objects that are signalled.
527 * %WAIT_ABANDONED_0 to %WAIT_ABANDONED_0 + @numobjects - 1 - if
528 * @waitall is %TRUE, indicates that all objects are signalled, and at
529 * least one object is an abandoned mutex object (See
530 * WaitForSingleObject() for a description of abandoned mutexes.) If
531 * @waitall is %FALSE, the return value minus %WAIT_ABANDONED_0
532 * indicates the first index into @handles of an abandoned mutex.
533 * %WAIT_TIMEOUT - The @timeout interval elapsed and no objects in
534 * @handles are signalled. %WAIT_FAILED - an error occurred.
535 * %WAIT_IO_COMPLETION - the wait was ended by an APC.
537 guint32
WaitForMultipleObjectsEx(guint32 numobjects
, gpointer
*handles
,
538 gboolean waitall
, guint32 timeout
,
542 gboolean duplicate
= FALSE
, bogustype
= FALSE
, done
;
543 guint32 count
, lowest
;
544 struct timespec abstime
;
548 gpointer current_thread
= _wapi_thread_handle_from_id (pthread_self ());
550 if (current_thread
== NULL
) {
551 SetLastError (ERROR_INVALID_HANDLE
);
555 if (numobjects
> MAXIMUM_WAIT_OBJECTS
) {
557 g_message ("%s: Too many handles: %d", __func__
, numobjects
);
563 if (numobjects
== 1) {
564 return WaitForSingleObjectEx (handles
[0], timeout
, alertable
);
567 /* Check for duplicates */
568 dups
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
569 for (i
= 0; i
< numobjects
; i
++) {
572 if (handles
[i
] == _WAPI_THREAD_CURRENT
) {
573 handles
[i
] = _wapi_thread_handle_from_id (pthread_self ());
575 if (handles
[i
] == NULL
) {
577 g_message ("%s: Handle %d bogus", __func__
, i
);
585 exists
= g_hash_table_lookup (dups
, handles
[i
]);
586 if (exists
!= NULL
) {
588 g_message ("%s: Handle %p duplicated", __func__
,
596 if (_wapi_handle_test_capabilities (handles
[i
], WAPI_HANDLE_CAP_WAIT
) == FALSE
) {
598 g_message ("%s: Handle %p can't be waited for",
599 __func__
, handles
[i
]);
605 g_hash_table_insert (dups
, handles
[i
], handles
[i
]);
606 _wapi_handle_ops_prewait (handles
[i
]);
608 g_hash_table_destroy (dups
);
610 if (duplicate
== TRUE
) {
612 g_message ("%s: Returning due to duplicates", __func__
);
618 if (bogustype
== TRUE
) {
620 g_message ("%s: Returning due to bogus type", __func__
);
626 done
= test_and_own (numobjects
, handles
, waitall
, &count
, &lowest
);
628 return(WAIT_OBJECT_0
+lowest
);
634 /* Have to wait for some or all handles to become signalled
637 if(timeout
!=INFINITE
) {
638 _wapi_calc_timeout (&abstime
, timeout
);
641 if (alertable
&& _wapi_thread_apc_pending (current_thread
)) {
642 _wapi_thread_dispatch_apc_queue (current_thread
);
643 return WAIT_IO_COMPLETION
;
647 /* Prod all handles with prewait methods and
648 * special-wait handles that aren't already signalled
650 for (i
= 0; i
< numobjects
; i
++) {
651 _wapi_handle_ops_prewait (handles
[i
]);
653 if (_wapi_handle_test_capabilities (handles
[i
], WAPI_HANDLE_CAP_SPECIAL_WAIT
) == TRUE
&& _wapi_handle_issignalled (handles
[i
]) == FALSE
) {
654 _wapi_handle_ops_special_wait (handles
[i
], 0);
658 /* Check before waiting on the condition, just in case
660 done
= test_and_own (numobjects
, handles
, waitall
,
663 return(WAIT_OBJECT_0
+ lowest
);
667 g_message ("%s: locking signal mutex", __func__
);
670 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_signal_mutex
, NULL
);
671 thr_ret
= _wapi_handle_lock_signal_mutex ();
672 g_assert (thr_ret
== 0);
674 if (timeout
== INFINITE
) {
675 ret
= _wapi_handle_wait_signal ();
677 ret
= _wapi_handle_timedwait_signal (&abstime
);
681 g_message ("%s: unlocking signal mutex", __func__
);
684 thr_ret
= _wapi_handle_unlock_signal_mutex (NULL
);
685 g_assert (thr_ret
== 0);
686 pthread_cleanup_pop (0);
688 if (alertable
&& _wapi_thread_apc_pending (current_thread
)) {
689 _wapi_thread_dispatch_apc_queue (current_thread
);
690 return WAIT_IO_COMPLETION
;
693 /* Check if everything is signalled, as we can't
694 * guarantee to notice a shared signal even if the
697 done
= test_and_own (numobjects
, handles
, waitall
,
700 return(WAIT_OBJECT_0
+lowest
);
701 } else if (ret
!= 0) {
702 /* Didn't get all handles, and there was a
703 * timeout or other error
706 g_message ("%s: wait returned error: %s", __func__
,
711 return(WAIT_TIMEOUT
);
719 guint32
WaitForMultipleObjects(guint32 numobjects
, gpointer
*handles
,
720 gboolean waitall
, guint32 timeout
)
722 return WaitForMultipleObjectsEx(numobjects
, handles
, waitall
, timeout
, FALSE
);