2 * wait.c: wait for handles to become signalled
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002-2006 Novell, Inc.
15 #include <mono/io-layer/wapi.h>
16 #include <mono/io-layer/handles-private.h>
17 #include <mono/io-layer/wapi-private.h>
18 #include <mono/io-layer/misc-private.h>
20 #include <mono/utils/mono-mutex.h>
23 #define DEBUG(...) g_message(__VA_ARGS__)
28 static gboolean own_if_signalled(gpointer handle)
32 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
33 if (_wapi_handle_trylock_shared_handles () == EBUSY) {
38 if (_wapi_handle_issignalled (handle)) {
39 _wapi_handle_ops_own (handle);
43 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
44 _wapi_handle_unlock_shared_handles ();
50 static gboolean own_if_owned(gpointer handle)
54 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
55 if (_wapi_handle_trylock_shared_handles () == EBUSY) {
60 if (_wapi_handle_ops_isowned (handle)) {
61 _wapi_handle_ops_own (handle);
65 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) {
66 _wapi_handle_unlock_shared_handles ();
73 * WaitForSingleObjectEx:
74 * @handle: an object to wait for
75 * @timeout: the maximum time in milliseconds to wait for
76 * @alertable: if TRUE, the wait can be interrupted by an APC call
78 * This function returns when either @handle is signalled, or @timeout
79 * ms elapses. If @timeout is zero, the object's state is tested and
80 * the function returns immediately. If @timeout is %INFINITE, the
81 * function waits forever.
83 * Return value: %WAIT_ABANDONED - @handle is a mutex that was not
84 * released by the owning thread when it exited. Ownership of the
85 * mutex object is granted to the calling thread and the mutex is set
86 * to nonsignalled. %WAIT_OBJECT_0 - The state of @handle is
87 * signalled. %WAIT_TIMEOUT - The @timeout interval elapsed and
88 * @handle's state is still not signalled. %WAIT_FAILED - an error
89 * occurred. %WAIT_IO_COMPLETION - the wait was ended by an APC.
91 guint32 WaitForSingleObjectEx(gpointer handle, guint32 timeout,
95 struct timespec abstime;
97 gboolean apc_pending = FALSE;
98 gpointer current_thread = _wapi_thread_handle_from_id (pthread_self ());
100 if (current_thread == NULL) {
101 SetLastError (ERROR_INVALID_HANDLE);
105 if (handle == _WAPI_THREAD_CURRENT) {
106 handle = _wapi_thread_handle_from_id (pthread_self ());
107 if (handle == NULL) {
108 SetLastError (ERROR_INVALID_HANDLE);
113 if ((GPOINTER_TO_UINT (handle) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) {
114 SetLastError (ERROR_INVALID_HANDLE);
118 if (_wapi_handle_test_capabilities (handle,
119 WAPI_HANDLE_CAP_WAIT) == FALSE) {
120 DEBUG ("%s: handle %p can't be waited for", __func__,
126 _wapi_handle_ops_prewait (handle);
128 if (_wapi_handle_test_capabilities (handle, WAPI_HANDLE_CAP_SPECIAL_WAIT) == TRUE) {
129 DEBUG ("%s: handle %p has special wait", __func__, handle);
131 ret = _wapi_handle_ops_special_wait (handle, timeout, alertable);
133 if (alertable && _wapi_thread_apc_pending (current_thread)) {
135 ret = WAIT_IO_COMPLETION;
142 DEBUG ("%s: locking handle %p", __func__, handle);
144 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
146 thr_ret = _wapi_handle_lock_handle (handle);
147 g_assert (thr_ret == 0);
149 if (_wapi_handle_test_capabilities (handle,
150 WAPI_HANDLE_CAP_OWN) == TRUE) {
151 if (own_if_owned (handle) == TRUE) {
152 DEBUG ("%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) {
166 DEBUG ("%s: handle %p already signalled", __func__,
177 /* Have to wait for it */
178 if (timeout != INFINITE) {
179 _wapi_calc_timeout (&abstime, timeout);
183 /* Check before waiting on the condition, just in case
185 _wapi_handle_ops_prewait (handle);
187 if (own_if_signalled (handle)) {
188 DEBUG ("%s: handle %p signalled", __func__,
195 if (timeout == INFINITE) {
196 waited = _wapi_handle_wait_signal_handle (handle, alertable);
198 waited = _wapi_handle_timedwait_signal_handle (handle, &abstime, alertable, FALSE);
202 apc_pending = _wapi_thread_apc_pending (current_thread);
204 if(waited==0 && !apc_pending) {
205 /* Condition was signalled, so hopefully
206 * handle is signalled now. (It might not be
207 * if someone else got in before us.)
209 if (own_if_signalled (handle)) {
210 DEBUG ("%s: handle %p signalled", __func__,
217 /* Better luck next time */
219 } while(waited == 0 && !apc_pending);
221 /* Timeout or other error */
222 DEBUG ("%s: wait on handle %p error: %s", __func__, handle,
229 DEBUG ("%s: unlocking handle %p", __func__, handle);
231 thr_ret = _wapi_handle_unlock_handle (handle);
232 g_assert (thr_ret == 0);
233 pthread_cleanup_pop (0);
237 _wapi_thread_dispatch_apc_queue (current_thread);
238 ret = WAIT_IO_COMPLETION;
244 guint32 WaitForSingleObject(gpointer handle, guint32 timeout)
246 return WaitForSingleObjectEx (handle, timeout, FALSE);
251 * SignalObjectAndWait:
252 * @signal_handle: An object to signal
253 * @wait: An object to wait for
254 * @timeout: The maximum time in milliseconds to wait for
255 * @alertable: Specifies whether the function returnes when the system
256 * queues an I/O completion routine or an APC for the calling thread.
258 * Atomically signals @signal and waits for @wait to become signalled,
259 * or @timeout ms elapses. If @timeout is zero, the object's state is
260 * tested and the function returns immediately. If @timeout is
261 * %INFINITE, the function waits forever.
263 * @signal can be a semaphore, mutex or event object.
265 * If @alertable is %TRUE and the system queues an I/O completion
266 * routine or an APC for the calling thread, the function returns and
267 * the thread calls the completion routine or APC function. If
268 * %FALSE, the function does not return, and the thread does not call
269 * the completion routine or APC function. A completion routine is
270 * queued when the ReadFileEx() or WriteFileEx() function in which it
271 * was specified has completed. The calling thread is the thread that
272 * initiated the read or write operation. An APC is queued when
273 * QueueUserAPC() is called. Currently completion routines and APC
274 * functions are not supported.
276 * Return value: %WAIT_ABANDONED - @wait is a mutex that was not
277 * released by the owning thread when it exited. Ownershop of the
278 * mutex object is granted to the calling thread and the mutex is set
279 * to nonsignalled. %WAIT_IO_COMPLETION - the wait was ended by one
280 * or more user-mode asynchronous procedure calls queued to the
281 * thread. %WAIT_OBJECT_0 - The state of @wait is signalled.
282 * %WAIT_TIMEOUT - The @timeout interval elapsed and @wait's state is
283 * still not signalled. %WAIT_FAILED - an error occurred.
285 guint32 SignalObjectAndWait(gpointer signal_handle, gpointer wait,
286 guint32 timeout, gboolean alertable)
289 struct timespec abstime;
291 gboolean apc_pending = FALSE;
292 gpointer current_thread = _wapi_thread_handle_from_id (pthread_self ());
294 if (current_thread == NULL) {
295 SetLastError (ERROR_INVALID_HANDLE);
299 if (signal_handle == _WAPI_THREAD_CURRENT) {
300 signal_handle = _wapi_thread_handle_from_id (pthread_self ());
301 if (signal_handle == NULL) {
302 SetLastError (ERROR_INVALID_HANDLE);
307 if (wait == _WAPI_THREAD_CURRENT) {
308 wait = _wapi_thread_handle_from_id (pthread_self ());
310 SetLastError (ERROR_INVALID_HANDLE);
315 if ((GPOINTER_TO_UINT (signal_handle) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) {
316 SetLastError (ERROR_INVALID_HANDLE);
320 if ((GPOINTER_TO_UINT (wait) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) {
321 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);
344 DEBUG ("%s: locking handle %p", __func__, wait);
346 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle,
348 thr_ret = _wapi_handle_lock_handle (wait);
349 g_assert (thr_ret == 0);
351 _wapi_handle_ops_signal (signal_handle);
353 if (_wapi_handle_test_capabilities (wait, WAPI_HANDLE_CAP_OWN)==TRUE) {
354 if (own_if_owned (wait)) {
355 DEBUG ("%s: handle %p already owned", __func__,
362 if (alertable && _wapi_thread_apc_pending (current_thread)) {
364 ret = WAIT_IO_COMPLETION;
368 if (own_if_signalled (wait)) {
369 DEBUG ("%s: handle %p already signalled", __func__, wait);
375 /* Have to wait for it */
376 if (timeout != INFINITE) {
377 _wapi_calc_timeout (&abstime, timeout);
381 /* Check before waiting on the condition, just in case
383 _wapi_handle_ops_prewait (wait);
385 if (own_if_signalled (wait)) {
386 DEBUG ("%s: handle %p signalled", __func__, wait);
392 if (timeout == INFINITE) {
393 waited = _wapi_handle_wait_signal_handle (wait, alertable);
395 waited = _wapi_handle_timedwait_signal_handle (wait, &abstime, alertable, FALSE);
399 apc_pending = _wapi_thread_apc_pending (current_thread);
402 if (waited==0 && !apc_pending) {
403 /* Condition was signalled, so hopefully
404 * handle is signalled now. (It might not be
405 * if someone else got in before us.)
407 if (own_if_signalled (wait)) {
408 DEBUG ("%s: handle %p signalled", __func__,
415 /* Better luck next time */
417 } while(waited == 0 && !apc_pending);
419 /* Timeout or other error */
420 DEBUG ("%s: wait on handle %p error: %s", __func__, wait,
427 DEBUG ("%s: unlocking handle %p", __func__, wait);
429 thr_ret = _wapi_handle_unlock_handle (wait);
430 g_assert (thr_ret == 0);
431 pthread_cleanup_pop (0);
434 _wapi_thread_dispatch_apc_queue (current_thread);
435 ret = WAIT_IO_COMPLETION;
441 struct handle_cleanup_data
447 static void handle_cleanup (void *data)
449 struct handle_cleanup_data *handles = (struct handle_cleanup_data *)data;
451 _wapi_handle_unlock_handles (handles->numobjects, handles->handles);
454 static gboolean test_and_own (guint32 numobjects, gpointer *handles,
455 gboolean waitall, guint32 *count,
458 struct handle_cleanup_data cleanup_data;
462 DEBUG ("%s: locking handles", __func__);
463 cleanup_data.numobjects = numobjects;
464 cleanup_data.handles = handles;
466 pthread_cleanup_push (handle_cleanup, (void *)&cleanup_data);
467 done = _wapi_handle_count_signalled_handles (numobjects, handles,
468 waitall, count, lowest);
470 if (waitall == TRUE) {
471 for (i = 0; i < numobjects; i++) {
472 own_if_signalled (handles[i]);
475 own_if_signalled (handles[*lowest]);
479 DEBUG ("%s: unlocking handles", __func__);
481 /* calls the unlock function */
482 pthread_cleanup_pop (1);
488 * WaitForMultipleObjectsEx:
489 * @numobjects: The number of objects in @handles. The maximum allowed
490 * is %MAXIMUM_WAIT_OBJECTS.
491 * @handles: An array of object handles. Duplicates are not allowed.
492 * @waitall: If %TRUE, this function waits until all of the handles
493 * are signalled. If %FALSE, this function returns when any object is
495 * @timeout: The maximum time in milliseconds to wait for.
496 * @alertable: if TRUE, the wait can be interrupted by an APC call
498 * This function returns when either one or more of @handles is
499 * signalled, or @timeout ms elapses. If @timeout is zero, the state
500 * of each item of @handles is tested and the function returns
501 * immediately. If @timeout is %INFINITE, the function waits forever.
503 * Return value: %WAIT_OBJECT_0 to %WAIT_OBJECT_0 + @numobjects - 1 -
504 * if @waitall is %TRUE, indicates that all objects are signalled. If
505 * @waitall is %FALSE, the return value minus %WAIT_OBJECT_0 indicates
506 * the first index into @handles of the objects that are signalled.
507 * %WAIT_ABANDONED_0 to %WAIT_ABANDONED_0 + @numobjects - 1 - if
508 * @waitall is %TRUE, indicates that all objects are signalled, and at
509 * least one object is an abandoned mutex object (See
510 * WaitForSingleObject() for a description of abandoned mutexes.) If
511 * @waitall is %FALSE, the return value minus %WAIT_ABANDONED_0
512 * indicates the first index into @handles of an abandoned mutex.
513 * %WAIT_TIMEOUT - The @timeout interval elapsed and no objects in
514 * @handles are signalled. %WAIT_FAILED - an error occurred.
515 * %WAIT_IO_COMPLETION - the wait was ended by an APC.
517 guint32 WaitForMultipleObjectsEx(guint32 numobjects, gpointer *handles,
518 gboolean waitall, guint32 timeout,
521 gboolean duplicate = FALSE, bogustype = FALSE, done;
522 guint32 count, lowest;
523 struct timespec abstime;
527 gpointer current_thread = _wapi_thread_handle_from_id (pthread_self ());
530 gpointer sorted_handles [MAXIMUM_WAIT_OBJECTS];
532 if (current_thread == NULL) {
533 SetLastError (ERROR_INVALID_HANDLE);
537 if (numobjects > MAXIMUM_WAIT_OBJECTS) {
538 DEBUG ("%s: Too many handles: %d", __func__, numobjects);
543 if (numobjects == 1) {
544 return WaitForSingleObjectEx (handles [0], timeout, alertable);
547 /* Check for duplicates */
548 for (i = 0; i < numobjects; i++) {
549 if (handles[i] == _WAPI_THREAD_CURRENT) {
550 handles[i] = _wapi_thread_handle_from_id (pthread_self ());
552 if (handles[i] == NULL) {
553 DEBUG ("%s: Handle %d bogus", __func__, i);
560 if ((GPOINTER_TO_UINT (handles[i]) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) {
561 DEBUG ("%s: Handle %d pseudo process", __func__,
568 if (_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_WAIT) == FALSE) {
569 DEBUG ("%s: Handle %p can't be waited for",
570 __func__, handles[i]);
576 sorted_handles [i] = handles [i];
577 _wapi_handle_ops_prewait (handles[i]);
580 qsort (sorted_handles, numobjects, sizeof (gpointer), g_direct_equal);
581 for (i = 1; i < numobjects; i++) {
582 if (sorted_handles [i - 1] == sorted_handles [i]) {
588 if (duplicate == TRUE) {
589 DEBUG ("%s: Returning due to duplicates", __func__);
594 if (bogustype == TRUE) {
595 DEBUG ("%s: Returning due to bogus type", __func__);
601 for (i = 0; i < numobjects; ++i)
602 if (_wapi_handle_type (handles [i]) == WAPI_HANDLE_PROCESS || _WAPI_SHARED_HANDLE (_wapi_handle_type (handles[i])))
603 /* Can't wait for a process handle + another handle without polling */
606 done = test_and_own (numobjects, handles, waitall, &count, &lowest);
608 return(WAIT_OBJECT_0+lowest);
614 /* Have to wait for some or all handles to become signalled
617 if(timeout!=INFINITE) {
618 _wapi_calc_timeout (&abstime, timeout);
621 if (alertable && _wapi_thread_apc_pending (current_thread)) {
622 _wapi_thread_dispatch_apc_queue (current_thread);
623 return WAIT_IO_COMPLETION;
626 for (i = 0; i < numobjects; i++) {
627 /* Add a reference, as we need to ensure the handle wont
628 * disappear from under us while we're waiting in the loop
629 * (not lock, as we don't want exclusive access here)
631 _wapi_handle_ref (handles[i]);
635 /* Prod all handles with prewait methods and
636 * special-wait handles that aren't already signalled
638 for (i = 0; i < numobjects; i++) {
639 _wapi_handle_ops_prewait (handles[i]);
641 if (_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_SPECIAL_WAIT) == TRUE && _wapi_handle_issignalled (handles[i]) == FALSE) {
642 _wapi_handle_ops_special_wait (handles[i], 0, alertable);
646 DEBUG ("%s: locking signal mutex", __func__);
648 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_signal_mutex, NULL);
649 thr_ret = _wapi_handle_lock_signal_mutex ();
650 g_assert (thr_ret == 0);
652 /* Check the signalled state of handles inside the critical section */
655 for (i = 0; i < numobjects; i++)
656 if (!_wapi_handle_issignalled (handles [i]))
660 for (i = 0; i < numobjects; i++)
661 if (_wapi_handle_issignalled (handles [i]))
667 if (timeout == INFINITE) {
668 ret = _wapi_handle_wait_signal (poll);
670 ret = _wapi_handle_timedwait_signal (&abstime, poll);
673 /* No need to wait */
677 DEBUG ("%s: unlocking signal mutex", __func__);
679 thr_ret = _wapi_handle_unlock_signal_mutex (NULL);
680 g_assert (thr_ret == 0);
681 pthread_cleanup_pop (0);
683 if (alertable && _wapi_thread_apc_pending (current_thread)) {
684 _wapi_thread_dispatch_apc_queue (current_thread);
685 retval = WAIT_IO_COMPLETION;
689 /* Check if everything is signalled, as we can't
690 * guarantee to notice a shared signal even if the
693 done = test_and_own (numobjects, handles, waitall,
696 retval = WAIT_OBJECT_0+lowest;
698 } else if (ret != 0) {
699 /* Didn't get all handles, and there was a
700 * timeout or other error
702 DEBUG ("%s: wait returned error: %s", __func__,
706 retval = WAIT_TIMEOUT;
708 retval = WAIT_FAILED;
714 for (i = 0; i < numobjects; i++) {
715 /* Unref everything we reffed above */
716 _wapi_handle_unref (handles[i]);
722 guint32 WaitForMultipleObjects(guint32 numobjects, gpointer *handles,
723 gboolean waitall, guint32 timeout)
725 return WaitForMultipleObjectsEx(numobjects, handles, waitall, timeout, FALSE);
730 * @handle: a handle to the process to wait for
731 * @timeout: the maximum time in milliseconds to wait for
733 * This function returns when either @handle process is waiting
734 * for input, or @timeout ms elapses. If @timeout is zero, the
735 * process state is tested and the function returns immediately.
736 * If @timeout is %INFINITE, the function waits forever.
738 * Return value: 0 - @handle process is waiting for input.
739 * %WAIT_TIMEOUT - The @timeout interval elapsed and
740 * @handle process is not waiting for input. %WAIT_FAILED - an error
743 guint32 WaitForInputIdle(gpointer handle, guint32 timeout)
745 /*TODO: Not implemented*/