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/mono-mutex.h>
19 #include <mono/io-layer/misc-private.h>
22 #define DEBUG(...) g_message(__VA_ARGS__)
27 static gboolean
own_if_signalled(gpointer handle
)
31 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle
))) {
32 if (_wapi_handle_trylock_shared_handles () == EBUSY
) {
37 if (_wapi_handle_issignalled (handle
)) {
38 _wapi_handle_ops_own (handle
);
42 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle
))) {
43 _wapi_handle_unlock_shared_handles ();
49 static gboolean
own_if_owned(gpointer handle
)
53 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle
))) {
54 if (_wapi_handle_trylock_shared_handles () == EBUSY
) {
59 if (_wapi_handle_ops_isowned (handle
)) {
60 _wapi_handle_ops_own (handle
);
64 if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle
))) {
65 _wapi_handle_unlock_shared_handles ();
72 * WaitForSingleObjectEx:
73 * @handle: an object to wait for
74 * @timeout: the maximum time in milliseconds to wait for
75 * @alertable: if TRUE, the wait can be interrupted by an APC call
77 * This function returns when either @handle is signalled, or @timeout
78 * ms elapses. If @timeout is zero, the object's state is tested and
79 * the function returns immediately. If @timeout is %INFINITE, the
80 * function waits forever.
82 * Return value: %WAIT_ABANDONED - @handle is a mutex that was not
83 * released by the owning thread when it exited. Ownership of the
84 * mutex object is granted to the calling thread and the mutex is set
85 * to nonsignalled. %WAIT_OBJECT_0 - The state of @handle is
86 * signalled. %WAIT_TIMEOUT - The @timeout interval elapsed and
87 * @handle's state is still not signalled. %WAIT_FAILED - an error
88 * occurred. %WAIT_IO_COMPLETION - the wait was ended by an APC.
90 guint32
WaitForSingleObjectEx(gpointer handle
, guint32 timeout
,
94 struct timespec abstime
;
96 gboolean apc_pending
= FALSE
;
97 gpointer current_thread
= _wapi_thread_handle_from_id (pthread_self ());
99 if (current_thread
== NULL
) {
100 SetLastError (ERROR_INVALID_HANDLE
);
104 if (handle
== _WAPI_THREAD_CURRENT
) {
105 handle
= _wapi_thread_handle_from_id (pthread_self ());
106 if (handle
== NULL
) {
107 SetLastError (ERROR_INVALID_HANDLE
);
112 if ((GPOINTER_TO_UINT (handle
) & _WAPI_PROCESS_UNHANDLED
) == _WAPI_PROCESS_UNHANDLED
) {
113 SetLastError (ERROR_INVALID_HANDLE
);
117 if (_wapi_handle_test_capabilities (handle
,
118 WAPI_HANDLE_CAP_WAIT
) == FALSE
) {
119 DEBUG ("%s: handle %p can't be waited for", __func__
,
125 _wapi_handle_ops_prewait (handle
);
127 if (_wapi_handle_test_capabilities (handle
, WAPI_HANDLE_CAP_SPECIAL_WAIT
) == TRUE
) {
128 DEBUG ("%s: handle %p has special wait", __func__
, handle
);
130 ret
= _wapi_handle_ops_special_wait (handle
, timeout
, alertable
);
132 if (alertable
&& _wapi_thread_apc_pending (current_thread
)) {
134 ret
= WAIT_IO_COMPLETION
;
141 DEBUG ("%s: locking handle %p", __func__
, handle
);
143 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle
,
145 thr_ret
= _wapi_handle_lock_handle (handle
);
146 g_assert (thr_ret
== 0);
148 if (_wapi_handle_test_capabilities (handle
,
149 WAPI_HANDLE_CAP_OWN
) == TRUE
) {
150 if (own_if_owned (handle
) == TRUE
) {
151 DEBUG ("%s: handle %p already owned", __func__
,
158 if (alertable
&& _wapi_thread_apc_pending (current_thread
)) {
160 ret
= WAIT_IO_COMPLETION
;
164 if (own_if_signalled (handle
) == TRUE
) {
165 DEBUG ("%s: handle %p already signalled", __func__
,
176 /* Have to wait for it */
177 if (timeout
!= INFINITE
) {
178 _wapi_calc_timeout (&abstime
, timeout
);
182 /* Check before waiting on the condition, just in case
184 _wapi_handle_ops_prewait (handle
);
186 if (own_if_signalled (handle
)) {
187 DEBUG ("%s: handle %p signalled", __func__
,
194 if (timeout
== INFINITE
) {
195 waited
= _wapi_handle_wait_signal_handle (handle
, alertable
);
197 waited
= _wapi_handle_timedwait_signal_handle (handle
, &abstime
, alertable
, FALSE
);
201 apc_pending
= _wapi_thread_apc_pending (current_thread
);
203 if(waited
==0 && !apc_pending
) {
204 /* Condition was signalled, so hopefully
205 * handle is signalled now. (It might not be
206 * if someone else got in before us.)
208 if (own_if_signalled (handle
)) {
209 DEBUG ("%s: handle %p signalled", __func__
,
216 /* Better luck next time */
218 } while(waited
== 0 && !apc_pending
);
220 /* Timeout or other error */
221 DEBUG ("%s: wait on handle %p error: %s", __func__
, handle
,
228 DEBUG ("%s: unlocking handle %p", __func__
, handle
);
230 thr_ret
= _wapi_handle_unlock_handle (handle
);
231 g_assert (thr_ret
== 0);
232 pthread_cleanup_pop (0);
236 _wapi_thread_dispatch_apc_queue (current_thread
);
237 ret
= WAIT_IO_COMPLETION
;
243 guint32
WaitForSingleObject(gpointer handle
, guint32 timeout
)
245 return WaitForSingleObjectEx (handle
, timeout
, FALSE
);
250 * SignalObjectAndWait:
251 * @signal_handle: An object to signal
252 * @wait: An object to wait for
253 * @timeout: The maximum time in milliseconds to wait for
254 * @alertable: Specifies whether the function returnes when the system
255 * queues an I/O completion routine or an APC for the calling thread.
257 * Atomically signals @signal and waits for @wait to become signalled,
258 * or @timeout ms elapses. If @timeout is zero, the object's state is
259 * tested and the function returns immediately. If @timeout is
260 * %INFINITE, the function waits forever.
262 * @signal can be a semaphore, mutex or event object.
264 * If @alertable is %TRUE and the system queues an I/O completion
265 * routine or an APC for the calling thread, the function returns and
266 * the thread calls the completion routine or APC function. If
267 * %FALSE, the function does not return, and the thread does not call
268 * the completion routine or APC function. A completion routine is
269 * queued when the ReadFileEx() or WriteFileEx() function in which it
270 * was specified has completed. The calling thread is the thread that
271 * initiated the read or write operation. An APC is queued when
272 * QueueUserAPC() is called. Currently completion routines and APC
273 * functions are not supported.
275 * Return value: %WAIT_ABANDONED - @wait is a mutex that was not
276 * released by the owning thread when it exited. Ownershop of the
277 * mutex object is granted to the calling thread and the mutex is set
278 * to nonsignalled. %WAIT_IO_COMPLETION - the wait was ended by one
279 * or more user-mode asynchronous procedure calls queued to the
280 * thread. %WAIT_OBJECT_0 - The state of @wait is signalled.
281 * %WAIT_TIMEOUT - The @timeout interval elapsed and @wait's state is
282 * still not signalled. %WAIT_FAILED - an error occurred.
284 guint32
SignalObjectAndWait(gpointer signal_handle
, gpointer wait
,
285 guint32 timeout
, gboolean alertable
)
288 struct timespec abstime
;
290 gboolean apc_pending
= FALSE
;
291 gpointer current_thread
= _wapi_thread_handle_from_id (pthread_self ());
293 if (current_thread
== NULL
) {
294 SetLastError (ERROR_INVALID_HANDLE
);
298 if (signal_handle
== _WAPI_THREAD_CURRENT
) {
299 signal_handle
= _wapi_thread_handle_from_id (pthread_self ());
300 if (signal_handle
== NULL
) {
301 SetLastError (ERROR_INVALID_HANDLE
);
306 if (wait
== _WAPI_THREAD_CURRENT
) {
307 wait
= _wapi_thread_handle_from_id (pthread_self ());
309 SetLastError (ERROR_INVALID_HANDLE
);
314 if ((GPOINTER_TO_UINT (signal_handle
) & _WAPI_PROCESS_UNHANDLED
) == _WAPI_PROCESS_UNHANDLED
) {
315 SetLastError (ERROR_INVALID_HANDLE
);
319 if ((GPOINTER_TO_UINT (wait
) & _WAPI_PROCESS_UNHANDLED
) == _WAPI_PROCESS_UNHANDLED
) {
320 SetLastError (ERROR_INVALID_HANDLE
);
324 if (_wapi_handle_test_capabilities (signal_handle
,
325 WAPI_HANDLE_CAP_SIGNAL
)==FALSE
) {
329 if (_wapi_handle_test_capabilities (wait
,
330 WAPI_HANDLE_CAP_WAIT
)==FALSE
) {
334 _wapi_handle_ops_prewait (wait
);
336 if (_wapi_handle_test_capabilities (wait
, WAPI_HANDLE_CAP_SPECIAL_WAIT
) == TRUE
) {
337 g_warning ("%s: handle %p has special wait, implement me!!",
340 return (WAIT_FAILED
);
343 DEBUG ("%s: locking handle %p", __func__
, wait
);
345 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle
,
347 thr_ret
= _wapi_handle_lock_handle (wait
);
348 g_assert (thr_ret
== 0);
350 _wapi_handle_ops_signal (signal_handle
);
352 if (_wapi_handle_test_capabilities (wait
, WAPI_HANDLE_CAP_OWN
)==TRUE
) {
353 if (own_if_owned (wait
)) {
354 DEBUG ("%s: handle %p already owned", __func__
,
361 if (alertable
&& _wapi_thread_apc_pending (current_thread
)) {
363 ret
= WAIT_IO_COMPLETION
;
367 if (own_if_signalled (wait
)) {
368 DEBUG ("%s: handle %p already signalled", __func__
, wait
);
374 /* Have to wait for it */
375 if (timeout
!= INFINITE
) {
376 _wapi_calc_timeout (&abstime
, timeout
);
380 /* Check before waiting on the condition, just in case
382 _wapi_handle_ops_prewait (wait
);
384 if (own_if_signalled (wait
)) {
385 DEBUG ("%s: handle %p signalled", __func__
, wait
);
391 if (timeout
== INFINITE
) {
392 waited
= _wapi_handle_wait_signal_handle (wait
, alertable
);
394 waited
= _wapi_handle_timedwait_signal_handle (wait
, &abstime
, alertable
, FALSE
);
398 apc_pending
= _wapi_thread_apc_pending (current_thread
);
401 if (waited
==0 && !apc_pending
) {
402 /* Condition was signalled, so hopefully
403 * handle is signalled now. (It might not be
404 * if someone else got in before us.)
406 if (own_if_signalled (wait
)) {
407 DEBUG ("%s: handle %p signalled", __func__
,
414 /* Better luck next time */
416 } while(waited
== 0 && !apc_pending
);
418 /* Timeout or other error */
419 DEBUG ("%s: wait on handle %p error: %s", __func__
, wait
,
426 DEBUG ("%s: unlocking handle %p", __func__
, wait
);
428 thr_ret
= _wapi_handle_unlock_handle (wait
);
429 g_assert (thr_ret
== 0);
430 pthread_cleanup_pop (0);
433 _wapi_thread_dispatch_apc_queue (current_thread
);
434 ret
= WAIT_IO_COMPLETION
;
440 struct handle_cleanup_data
446 static void handle_cleanup (void *data
)
448 struct handle_cleanup_data
*handles
= (struct handle_cleanup_data
*)data
;
450 _wapi_handle_unlock_handles (handles
->numobjects
, handles
->handles
);
453 static gboolean
test_and_own (guint32 numobjects
, gpointer
*handles
,
454 gboolean waitall
, guint32
*count
,
457 struct handle_cleanup_data cleanup_data
;
461 DEBUG ("%s: locking handles", __func__
);
462 cleanup_data
.numobjects
= numobjects
;
463 cleanup_data
.handles
= handles
;
465 pthread_cleanup_push (handle_cleanup
, (void *)&cleanup_data
);
466 done
= _wapi_handle_count_signalled_handles (numobjects
, handles
,
467 waitall
, count
, lowest
);
469 if (waitall
== TRUE
) {
470 for (i
= 0; i
< numobjects
; i
++) {
471 own_if_signalled (handles
[i
]);
474 own_if_signalled (handles
[*lowest
]);
478 DEBUG ("%s: unlocking handles", __func__
);
480 /* calls the unlock function */
481 pthread_cleanup_pop (1);
487 * WaitForMultipleObjectsEx:
488 * @numobjects: The number of objects in @handles. The maximum allowed
489 * is %MAXIMUM_WAIT_OBJECTS.
490 * @handles: An array of object handles. Duplicates are not allowed.
491 * @waitall: If %TRUE, this function waits until all of the handles
492 * are signalled. If %FALSE, this function returns when any object is
494 * @timeout: The maximum time in milliseconds to wait for.
495 * @alertable: if TRUE, the wait can be interrupted by an APC call
497 * This function returns when either one or more of @handles is
498 * signalled, or @timeout ms elapses. If @timeout is zero, the state
499 * of each item of @handles is tested and the function returns
500 * immediately. If @timeout is %INFINITE, the function waits forever.
502 * Return value: %WAIT_OBJECT_0 to %WAIT_OBJECT_0 + @numobjects - 1 -
503 * if @waitall is %TRUE, indicates that all objects are signalled. If
504 * @waitall is %FALSE, the return value minus %WAIT_OBJECT_0 indicates
505 * the first index into @handles of the objects that are signalled.
506 * %WAIT_ABANDONED_0 to %WAIT_ABANDONED_0 + @numobjects - 1 - if
507 * @waitall is %TRUE, indicates that all objects are signalled, and at
508 * least one object is an abandoned mutex object (See
509 * WaitForSingleObject() for a description of abandoned mutexes.) If
510 * @waitall is %FALSE, the return value minus %WAIT_ABANDONED_0
511 * indicates the first index into @handles of an abandoned mutex.
512 * %WAIT_TIMEOUT - The @timeout interval elapsed and no objects in
513 * @handles are signalled. %WAIT_FAILED - an error occurred.
514 * %WAIT_IO_COMPLETION - the wait was ended by an APC.
516 guint32
WaitForMultipleObjectsEx(guint32 numobjects
, gpointer
*handles
,
517 gboolean waitall
, guint32 timeout
,
520 gboolean duplicate
= FALSE
, bogustype
= FALSE
, done
;
521 guint32 count
, lowest
;
522 struct timespec abstime
;
526 gpointer current_thread
= _wapi_thread_handle_from_id (pthread_self ());
529 gpointer sorted_handles
[MAXIMUM_WAIT_OBJECTS
];
531 if (current_thread
== NULL
) {
532 SetLastError (ERROR_INVALID_HANDLE
);
536 if (numobjects
> MAXIMUM_WAIT_OBJECTS
) {
537 DEBUG ("%s: Too many handles: %d", __func__
, numobjects
);
542 if (numobjects
== 1) {
543 return WaitForSingleObjectEx (handles
[0], timeout
, alertable
);
546 /* Check for duplicates */
547 for (i
= 0; i
< numobjects
; i
++) {
548 if (handles
[i
] == _WAPI_THREAD_CURRENT
) {
549 handles
[i
] = _wapi_thread_handle_from_id (pthread_self ());
551 if (handles
[i
] == NULL
) {
552 DEBUG ("%s: Handle %d bogus", __func__
, i
);
559 if ((GPOINTER_TO_UINT (handles
[i
]) & _WAPI_PROCESS_UNHANDLED
) == _WAPI_PROCESS_UNHANDLED
) {
560 DEBUG ("%s: Handle %d pseudo process", __func__
,
567 if (_wapi_handle_test_capabilities (handles
[i
], WAPI_HANDLE_CAP_WAIT
) == FALSE
) {
568 DEBUG ("%s: Handle %p can't be waited for",
569 __func__
, handles
[i
]);
575 sorted_handles
[i
] = handles
[i
];
576 _wapi_handle_ops_prewait (handles
[i
]);
579 qsort (sorted_handles
, numobjects
, sizeof (gpointer
), g_direct_equal
);
580 for (i
= 1; i
< numobjects
; i
++) {
581 if (sorted_handles
[i
- 1] == sorted_handles
[i
]) {
587 if (duplicate
== TRUE
) {
588 DEBUG ("%s: Returning due to duplicates", __func__
);
593 if (bogustype
== TRUE
) {
594 DEBUG ("%s: Returning due to bogus type", __func__
);
600 for (i
= 0; i
< numobjects
; ++i
)
601 if (_wapi_handle_type (handles
[i
]) == WAPI_HANDLE_PROCESS
|| _WAPI_SHARED_HANDLE (_wapi_handle_type (handles
[i
])))
602 /* Can't wait for a process handle + another handle without polling */
605 done
= test_and_own (numobjects
, handles
, waitall
, &count
, &lowest
);
607 return(WAIT_OBJECT_0
+lowest
);
613 /* Have to wait for some or all handles to become signalled
616 if(timeout
!=INFINITE
) {
617 _wapi_calc_timeout (&abstime
, timeout
);
620 if (alertable
&& _wapi_thread_apc_pending (current_thread
)) {
621 _wapi_thread_dispatch_apc_queue (current_thread
);
622 return WAIT_IO_COMPLETION
;
625 for (i
= 0; i
< numobjects
; i
++) {
626 /* Add a reference, as we need to ensure the handle wont
627 * disappear from under us while we're waiting in the loop
628 * (not lock, as we don't want exclusive access here)
630 _wapi_handle_ref (handles
[i
]);
634 /* Prod all handles with prewait methods and
635 * special-wait handles that aren't already signalled
637 for (i
= 0; i
< numobjects
; i
++) {
638 _wapi_handle_ops_prewait (handles
[i
]);
640 if (_wapi_handle_test_capabilities (handles
[i
], WAPI_HANDLE_CAP_SPECIAL_WAIT
) == TRUE
&& _wapi_handle_issignalled (handles
[i
]) == FALSE
) {
641 _wapi_handle_ops_special_wait (handles
[i
], 0, alertable
);
645 DEBUG ("%s: locking signal mutex", __func__
);
647 pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_signal_mutex
, NULL
);
648 thr_ret
= _wapi_handle_lock_signal_mutex ();
649 g_assert (thr_ret
== 0);
651 /* Check the signalled state of handles inside the critical section */
654 for (i
= 0; i
< numobjects
; i
++)
655 if (!_wapi_handle_issignalled (handles
[i
]))
659 for (i
= 0; i
< numobjects
; i
++)
660 if (_wapi_handle_issignalled (handles
[i
]))
666 if (timeout
== INFINITE
) {
667 ret
= _wapi_handle_wait_signal (poll
);
669 ret
= _wapi_handle_timedwait_signal (&abstime
, poll
);
672 /* No need to wait */
676 DEBUG ("%s: unlocking signal mutex", __func__
);
678 thr_ret
= _wapi_handle_unlock_signal_mutex (NULL
);
679 g_assert (thr_ret
== 0);
680 pthread_cleanup_pop (0);
682 if (alertable
&& _wapi_thread_apc_pending (current_thread
)) {
683 _wapi_thread_dispatch_apc_queue (current_thread
);
684 retval
= WAIT_IO_COMPLETION
;
688 /* Check if everything is signalled, as we can't
689 * guarantee to notice a shared signal even if the
692 done
= test_and_own (numobjects
, handles
, waitall
,
695 retval
= WAIT_OBJECT_0
+lowest
;
697 } else if (ret
!= 0) {
698 /* Didn't get all handles, and there was a
699 * timeout or other error
701 DEBUG ("%s: wait returned error: %s", __func__
,
705 retval
= WAIT_TIMEOUT
;
707 retval
= WAIT_FAILED
;
713 for (i
= 0; i
< numobjects
; i
++) {
714 /* Unref everything we reffed above */
715 _wapi_handle_unref (handles
[i
]);
721 guint32
WaitForMultipleObjects(guint32 numobjects
, gpointer
*handles
,
722 gboolean waitall
, guint32 timeout
)
724 return WaitForMultipleObjectsEx(numobjects
, handles
, waitall
, timeout
, FALSE
);
729 * @handle: a handle to the process to wait for
730 * @timeout: the maximum time in milliseconds to wait for
732 * This function returns when either @handle process is waiting
733 * for input, or @timeout ms elapses. If @timeout is zero, the
734 * process state is tested and the function returns immediately.
735 * If @timeout is %INFINITE, the function waits forever.
737 * Return value: 0 - @handle process is waiting for input.
738 * %WAIT_TIMEOUT - The @timeout interval elapsed and
739 * @handle process is not waiting for input. %WAIT_FAILED - an error
742 guint32
WaitForInputIdle(gpointer handle
, guint32 timeout
)
744 /*TODO: Not implemented*/