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/io-trace.h>
19 #include <mono/utils/mono-logger-internals.h>
20 #include <mono/utils/mono-time.h>
22 static gboolean own_if_signalled(gpointer handle)
26 if (_wapi_handle_issignalled (handle)) {
27 _wapi_handle_ops_own (handle);
34 static gboolean own_if_owned(gpointer handle)
38 if (_wapi_handle_ops_isowned (handle)) {
39 _wapi_handle_ops_own (handle);
47 * WaitForSingleObjectEx:
48 * @handle: an object to wait for
49 * @timeout: the maximum time in milliseconds to wait for
50 * @alertable: if TRUE, the wait can be interrupted by an APC call
52 * This function returns when either @handle is signalled, or @timeout
53 * ms elapses. If @timeout is zero, the object's state is tested and
54 * the function returns immediately. If @timeout is %INFINITE, the
55 * function waits forever.
57 * Return value: %WAIT_ABANDONED - @handle is a mutex that was not
58 * released by the owning thread when it exited. Ownership of the
59 * mutex object is granted to the calling thread and the mutex is set
60 * to nonsignalled. %WAIT_OBJECT_0 - The state of @handle is
61 * signalled. %WAIT_TIMEOUT - The @timeout interval elapsed and
62 * @handle's state is still not signalled. %WAIT_FAILED - an error
63 * occurred. %WAIT_IO_COMPLETION - the wait was ended by an APC.
65 guint32 WaitForSingleObjectEx(gpointer handle, guint32 timeout,
70 gboolean apc_pending = FALSE;
71 gpointer current_thread = wapi_get_current_thread_handle ();
74 if (current_thread == NULL) {
75 SetLastError (ERROR_INVALID_HANDLE);
79 if (handle == _WAPI_THREAD_CURRENT) {
80 handle = wapi_get_current_thread_handle ();
82 SetLastError (ERROR_INVALID_HANDLE);
87 if ((GPOINTER_TO_UINT (handle) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) {
88 SetLastError (ERROR_INVALID_HANDLE);
92 if (_wapi_handle_test_capabilities (handle,
93 WAPI_HANDLE_CAP_WAIT) == FALSE) {
94 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p can't be waited for", __func__,
100 _wapi_handle_ops_prewait (handle);
102 if (_wapi_handle_test_capabilities (handle, WAPI_HANDLE_CAP_SPECIAL_WAIT) == TRUE) {
103 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p has special wait", __func__, handle);
105 ret = _wapi_handle_ops_special_wait (handle, timeout, alertable);
107 if (alertable && _wapi_thread_cur_apc_pending ())
108 ret = WAIT_IO_COMPLETION;
114 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: locking handle %p", __func__, handle);
116 thr_ret = _wapi_handle_lock_handle (handle);
117 g_assert (thr_ret == 0);
119 if (_wapi_handle_test_capabilities (handle,
120 WAPI_HANDLE_CAP_OWN) == TRUE) {
121 if (own_if_owned (handle) == TRUE) {
122 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p already owned", __func__,
129 if (own_if_signalled (handle) == TRUE) {
130 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p already signalled", __func__,
142 if (timeout != INFINITE)
143 end = mono_100ns_ticks () + timeout * 1000 * 10;
146 /* Check before waiting on the condition, just in case
148 _wapi_handle_ops_prewait (handle);
150 if (own_if_signalled (handle)) {
151 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p signalled", __func__,
158 if (timeout == INFINITE) {
159 waited = _wapi_handle_timedwait_signal_handle (handle, INFINITE, FALSE, alertable ? &apc_pending : NULL);
161 now = mono_100ns_ticks ();
167 waited = _wapi_handle_timedwait_signal_handle (handle, (end - now) / 10 / 1000, FALSE, alertable ? &apc_pending : NULL);
170 if(waited==0 && !apc_pending) {
171 /* Condition was signalled, so hopefully
172 * handle is signalled now. (It might not be
173 * if someone else got in before us.)
175 if (own_if_signalled (handle)) {
176 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p signalled", __func__,
183 /* Better luck next time */
185 } while(waited == 0 && !apc_pending);
187 /* Timeout or other error */
188 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: wait on handle %p error: %s", __func__, handle,
191 ret = apc_pending ? WAIT_IO_COMPLETION : WAIT_TIMEOUT;
195 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unlocking handle %p", __func__, handle);
197 thr_ret = _wapi_handle_unlock_handle (handle);
198 g_assert (thr_ret == 0);
203 guint32 WaitForSingleObject(gpointer handle, guint32 timeout)
205 return WaitForSingleObjectEx (handle, timeout, FALSE);
210 * SignalObjectAndWait:
211 * @signal_handle: An object to signal
212 * @wait: An object to wait for
213 * @timeout: The maximum time in milliseconds to wait for
214 * @alertable: Specifies whether the function returnes when the system
215 * queues an I/O completion routine or an APC for the calling thread.
217 * Atomically signals @signal and waits for @wait to become signalled,
218 * or @timeout ms elapses. If @timeout is zero, the object's state is
219 * tested and the function returns immediately. If @timeout is
220 * %INFINITE, the function waits forever.
222 * @signal can be a semaphore, mutex or event object.
224 * If @alertable is %TRUE and the system queues an I/O completion
225 * routine or an APC for the calling thread, the function returns and
226 * the thread calls the completion routine or APC function. If
227 * %FALSE, the function does not return, and the thread does not call
228 * the completion routine or APC function. A completion routine is
229 * queued when the ReadFileEx() or WriteFileEx() function in which it
230 * was specified has completed. The calling thread is the thread that
231 * initiated the read or write operation. An APC is queued when
232 * QueueUserAPC() is called. Currently completion routines and APC
233 * functions are not supported.
235 * Return value: %WAIT_ABANDONED - @wait is a mutex that was not
236 * released by the owning thread when it exited. Ownershop of the
237 * mutex object is granted to the calling thread and the mutex is set
238 * to nonsignalled. %WAIT_IO_COMPLETION - the wait was ended by one
239 * or more user-mode asynchronous procedure calls queued to the
240 * thread. %WAIT_OBJECT_0 - The state of @wait is signalled.
241 * %WAIT_TIMEOUT - The @timeout interval elapsed and @wait's state is
242 * still not signalled. %WAIT_FAILED - an error occurred.
244 guint32 SignalObjectAndWait(gpointer signal_handle, gpointer wait,
245 guint32 timeout, gboolean alertable)
247 guint32 ret = 0, waited;
249 gboolean apc_pending = FALSE;
250 gpointer current_thread = wapi_get_current_thread_handle ();
253 if (current_thread == NULL) {
254 SetLastError (ERROR_INVALID_HANDLE);
258 if (signal_handle == _WAPI_THREAD_CURRENT) {
259 signal_handle = wapi_get_current_thread_handle ();
260 if (signal_handle == NULL) {
261 SetLastError (ERROR_INVALID_HANDLE);
266 if (wait == _WAPI_THREAD_CURRENT) {
267 wait = wapi_get_current_thread_handle ();
269 SetLastError (ERROR_INVALID_HANDLE);
274 if ((GPOINTER_TO_UINT (signal_handle) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) {
275 SetLastError (ERROR_INVALID_HANDLE);
279 if ((GPOINTER_TO_UINT (wait) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) {
280 SetLastError (ERROR_INVALID_HANDLE);
284 if (_wapi_handle_test_capabilities (signal_handle,
285 WAPI_HANDLE_CAP_SIGNAL)==FALSE) {
289 if (_wapi_handle_test_capabilities (wait,
290 WAPI_HANDLE_CAP_WAIT)==FALSE) {
294 _wapi_handle_ops_prewait (wait);
296 if (_wapi_handle_test_capabilities (wait, WAPI_HANDLE_CAP_SPECIAL_WAIT) == TRUE) {
297 g_warning ("%s: handle %p has special wait, implement me!!",
300 return (WAIT_FAILED);
303 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: locking handle %p", __func__, wait);
305 thr_ret = _wapi_handle_lock_handle (wait);
306 g_assert (thr_ret == 0);
308 _wapi_handle_ops_signal (signal_handle);
310 if (_wapi_handle_test_capabilities (wait, WAPI_HANDLE_CAP_OWN)==TRUE) {
311 if (own_if_owned (wait)) {
312 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p already owned", __func__,
319 if (own_if_signalled (wait)) {
320 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p already signalled", __func__, wait);
326 if (timeout != INFINITE)
327 end = mono_100ns_ticks () + timeout * 1000 * 10;
330 /* Check before waiting on the condition, just in case
332 _wapi_handle_ops_prewait (wait);
334 if (own_if_signalled (wait)) {
335 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p signalled", __func__, wait);
341 if (timeout == INFINITE) {
342 waited = _wapi_handle_timedwait_signal_handle (wait, INFINITE, FALSE, alertable ? &apc_pending : NULL);
344 now = mono_100ns_ticks ();
350 waited = _wapi_handle_timedwait_signal_handle (wait, (end - now) / 10 / 1000, FALSE, alertable ? &apc_pending : NULL);
353 if (waited==0 && !apc_pending) {
354 /* Condition was signalled, so hopefully
355 * handle is signalled now. (It might not be
356 * if someone else got in before us.)
358 if (own_if_signalled (wait)) {
359 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p signalled", __func__,
366 /* Better luck next time */
368 } while(waited == 0 && !apc_pending);
370 /* Timeout or other error */
371 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: wait on handle %p error: %s", __func__, wait, strerror (ret));
373 ret = apc_pending ? WAIT_IO_COMPLETION : WAIT_TIMEOUT;
377 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unlocking handle %p", __func__, wait);
379 thr_ret = _wapi_handle_unlock_handle (wait);
380 g_assert (thr_ret == 0);
385 static gboolean test_and_own (guint32 numobjects, gpointer *handles,
386 gboolean waitall, guint32 *count,
392 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: locking handles", __func__);
394 done = _wapi_handle_count_signalled_handles (numobjects, handles,
395 waitall, count, lowest);
397 if (waitall == TRUE) {
398 for (i = 0; i < numobjects; i++) {
399 own_if_signalled (handles[i]);
402 own_if_signalled (handles[*lowest]);
406 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unlocking handles", __func__);
408 _wapi_handle_unlock_handles (numobjects, handles);
414 * WaitForMultipleObjectsEx:
415 * @numobjects: The number of objects in @handles. The maximum allowed
416 * is %MAXIMUM_WAIT_OBJECTS.
417 * @handles: An array of object handles. Duplicates are not allowed.
418 * @waitall: If %TRUE, this function waits until all of the handles
419 * are signalled. If %FALSE, this function returns when any object is
421 * @timeout: The maximum time in milliseconds to wait for.
422 * @alertable: if TRUE, the wait can be interrupted by an APC call
424 * This function returns when either one or more of @handles is
425 * signalled, or @timeout ms elapses. If @timeout is zero, the state
426 * of each item of @handles is tested and the function returns
427 * immediately. If @timeout is %INFINITE, the function waits forever.
429 * Return value: %WAIT_OBJECT_0 to %WAIT_OBJECT_0 + @numobjects - 1 -
430 * if @waitall is %TRUE, indicates that all objects are signalled. If
431 * @waitall is %FALSE, the return value minus %WAIT_OBJECT_0 indicates
432 * the first index into @handles of the objects that are signalled.
433 * %WAIT_ABANDONED_0 to %WAIT_ABANDONED_0 + @numobjects - 1 - if
434 * @waitall is %TRUE, indicates that all objects are signalled, and at
435 * least one object is an abandoned mutex object (See
436 * WaitForSingleObject() for a description of abandoned mutexes.) If
437 * @waitall is %FALSE, the return value minus %WAIT_ABANDONED_0
438 * indicates the first index into @handles of an abandoned mutex.
439 * %WAIT_TIMEOUT - The @timeout interval elapsed and no objects in
440 * @handles are signalled. %WAIT_FAILED - an error occurred.
441 * %WAIT_IO_COMPLETION - the wait was ended by an APC.
443 guint32 WaitForMultipleObjectsEx(guint32 numobjects, gpointer *handles,
444 gboolean waitall, guint32 timeout,
447 gboolean duplicate = FALSE, bogustype = FALSE, done;
448 guint32 count, lowest;
452 gpointer current_thread = wapi_get_current_thread_handle ();
455 gpointer sorted_handles [MAXIMUM_WAIT_OBJECTS];
456 gboolean apc_pending = FALSE;
459 if (current_thread == NULL) {
460 SetLastError (ERROR_INVALID_HANDLE);
464 if (numobjects > MAXIMUM_WAIT_OBJECTS) {
465 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Too many handles: %d", __func__, numobjects);
470 if (numobjects == 1) {
471 return WaitForSingleObjectEx (handles [0], timeout, alertable);
474 /* Check for duplicates */
475 for (i = 0; i < numobjects; i++) {
476 if (handles[i] == _WAPI_THREAD_CURRENT) {
477 handles[i] = wapi_get_current_thread_handle ();
479 if (handles[i] == NULL) {
480 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Handle %d bogus", __func__, i);
487 if ((GPOINTER_TO_UINT (handles[i]) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) {
488 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Handle %d pseudo process", __func__,
495 if (_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_WAIT) == FALSE) {
496 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Handle %p can't be waited for",
497 __func__, handles[i]);
503 sorted_handles [i] = handles [i];
504 _wapi_handle_ops_prewait (handles[i]);
507 qsort (sorted_handles, numobjects, sizeof (gpointer), g_direct_equal);
508 for (i = 1; i < numobjects; i++) {
509 if (sorted_handles [i - 1] == sorted_handles [i]) {
515 if (duplicate == TRUE) {
516 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning due to duplicates", __func__);
521 if (bogustype == TRUE) {
522 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning due to bogus type", __func__);
528 for (i = 0; i < numobjects; ++i)
529 if (_wapi_handle_type (handles [i]) == WAPI_HANDLE_PROCESS)
530 /* Can't wait for a process handle + another handle without polling */
533 done = test_and_own (numobjects, handles, waitall, &count, &lowest);
535 return(WAIT_OBJECT_0+lowest);
542 if (timeout != INFINITE)
543 end = mono_100ns_ticks () + timeout * 1000 * 10;
545 /* Have to wait for some or all handles to become signalled
548 for (i = 0; i < numobjects; i++) {
549 /* Add a reference, as we need to ensure the handle wont
550 * disappear from under us while we're waiting in the loop
551 * (not lock, as we don't want exclusive access here)
553 _wapi_handle_ref (handles[i]);
557 /* Prod all handles with prewait methods and
558 * special-wait handles that aren't already signalled
560 for (i = 0; i < numobjects; i++) {
561 _wapi_handle_ops_prewait (handles[i]);
563 if (_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_SPECIAL_WAIT) == TRUE && _wapi_handle_issignalled (handles[i]) == FALSE) {
564 _wapi_handle_ops_special_wait (handles[i], 0, alertable);
568 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: locking signal mutex", __func__);
570 thr_ret = _wapi_handle_lock_signal_mutex ();
571 g_assert (thr_ret == 0);
573 /* Check the signalled state of handles inside the critical section */
576 for (i = 0; i < numobjects; i++)
577 if (!_wapi_handle_issignalled (handles [i]))
581 for (i = 0; i < numobjects; i++)
582 if (_wapi_handle_issignalled (handles [i]))
588 if (timeout == INFINITE) {
589 ret = _wapi_handle_timedwait_signal (INFINITE, poll, &apc_pending);
591 now = mono_100ns_ticks ();
595 ret = _wapi_handle_timedwait_signal ((end - now) / 10 / 1000, poll, &apc_pending);
599 /* No need to wait */
603 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unlocking signal mutex", __func__);
605 thr_ret = _wapi_handle_unlock_signal_mutex ();
606 g_assert (thr_ret == 0);
608 if (alertable && apc_pending) {
609 retval = WAIT_IO_COMPLETION;
613 /* Check if everything is signalled, as we can't
614 * guarantee to notice a shared signal even if the
617 done = test_and_own (numobjects, handles, waitall,
620 retval = WAIT_OBJECT_0+lowest;
622 } else if (ret != 0) {
623 /* Didn't get all handles, and there was a
624 * timeout or other error
626 MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: wait returned error: %s", __func__,
630 retval = WAIT_TIMEOUT;
632 retval = WAIT_FAILED;
638 for (i = 0; i < numobjects; i++) {
639 /* Unref everything we reffed above */
640 _wapi_handle_unref (handles[i]);
646 guint32 WaitForMultipleObjects(guint32 numobjects, gpointer *handles,
647 gboolean waitall, guint32 timeout)
649 return WaitForMultipleObjectsEx(numobjects, handles, waitall, timeout, FALSE);
654 * @handle: a handle to the process to wait for
655 * @timeout: the maximum time in milliseconds to wait for
657 * This function returns when either @handle process is waiting
658 * for input, or @timeout ms elapses. If @timeout is zero, the
659 * process state is tested and the function returns immediately.
660 * If @timeout is %INFINITE, the function waits forever.
662 * Return value: 0 - @handle process is waiting for input.
663 * %WAIT_TIMEOUT - The @timeout interval elapsed and
664 * @handle process is not waiting for input. %WAIT_FAILED - an error
667 guint32 WaitForInputIdle(gpointer handle, guint32 timeout)
669 /*TODO: Not implemented*/