void _wapi_thread_set_termination_details (gpointer handle, guint32 exitstatus) { struct _WapiHandle_thread *thread_handle; gboolean ok; int thr_ret; if (_wapi_handle_issignalled (handle) || _wapi_handle_type (handle) == WAPI_HANDLE_UNUSED) { /* We must have already deliberately finished with * this thread, so don't do any more now */ return; } #ifdef DEBUG g_message ("%s: Thread %p terminating", __func__, handle); #endif _wapi_thread_abandon_mutexes (handle); ok = _wapi_lookup_handle (handle, WAPI_HANDLE_THREAD, (gpointer *)&thread_handle); if (ok == FALSE) { g_warning ("%s: error looking up thread handle %p", __func__, handle); return; } pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_handle, handle); thr_ret = _wapi_handle_lock_handle (handle); g_assert (thr_ret == 0); thread_handle->exitstatus = exitstatus; thread_handle->state = THREAD_STATE_EXITED; MONO_SEM_DESTROY (&thread_handle->suspend_sem); g_ptr_array_free (thread_handle->owned_mutexes, TRUE); _wapi_handle_set_signal_state (handle, TRUE, TRUE); thr_ret = _wapi_handle_unlock_handle (handle); g_assert (thr_ret == 0); pthread_cleanup_pop (0); #ifdef DEBUG g_message("%s: Recording thread handle %p id %ld status as %d", __func__, handle, thread_handle->id, exitstatus); #endif /* The thread is no longer active, so unref it */ _wapi_handle_unref (handle); }
static gboolean own_if_signalled(gpointer handle) { gboolean ret = FALSE; if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) { if (_wapi_handle_trylock_shared_handles () == EBUSY) { return (FALSE); } } if (_wapi_handle_issignalled (handle)) { _wapi_handle_ops_own (handle); ret = TRUE; } if (_WAPI_SHARED_HANDLE (_wapi_handle_type (handle))) { _wapi_handle_unlock_shared_handles (); } return(ret); }
/** * WaitForMultipleObjectsEx: * @numobjects: The number of objects in @handles. The maximum allowed * is %MAXIMUM_WAIT_OBJECTS. * @handles: An array of object handles. Duplicates are not allowed. * @waitall: If %TRUE, this function waits until all of the handles * are signalled. If %FALSE, this function returns when any object is * signalled. * @timeout: The maximum time in milliseconds to wait for. * @alertable: if TRUE, the wait can be interrupted by an APC call * * This function returns when either one or more of @handles is * signalled, or @timeout ms elapses. If @timeout is zero, the state * of each item of @handles is tested and the function returns * immediately. If @timeout is %INFINITE, the function waits forever. * * Return value: %WAIT_OBJECT_0 to %WAIT_OBJECT_0 + @numobjects - 1 - * if @waitall is %TRUE, indicates that all objects are signalled. If * @waitall is %FALSE, the return value minus %WAIT_OBJECT_0 indicates * the first index into @handles of the objects that are signalled. * %WAIT_ABANDONED_0 to %WAIT_ABANDONED_0 + @numobjects - 1 - if * @waitall is %TRUE, indicates that all objects are signalled, and at * least one object is an abandoned mutex object (See * WaitForSingleObject() for a description of abandoned mutexes.) If * @waitall is %FALSE, the return value minus %WAIT_ABANDONED_0 * indicates the first index into @handles of an abandoned mutex. * %WAIT_TIMEOUT - The @timeout interval elapsed and no objects in * @handles are signalled. %WAIT_FAILED - an error occurred. * %WAIT_IO_COMPLETION - the wait was ended by an APC. */ guint32 WaitForMultipleObjectsEx(guint32 numobjects, gpointer *handles, gboolean waitall, guint32 timeout, gboolean alertable) { gboolean duplicate = FALSE, bogustype = FALSE, done; guint32 count, lowest; guint i; guint32 ret; int thr_ret; gpointer current_thread = wapi_get_current_thread_handle (); guint32 retval; gboolean poll; gpointer sorted_handles [MAXIMUM_WAIT_OBJECTS]; gboolean apc_pending = FALSE; gint64 now, end; if (current_thread == NULL) { SetLastError (ERROR_INVALID_HANDLE); return(WAIT_FAILED); } if (numobjects > MAXIMUM_WAIT_OBJECTS) { MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Too many handles: %d", __func__, numobjects); return(WAIT_FAILED); } if (numobjects == 1) { return WaitForSingleObjectEx (handles [0], timeout, alertable); } /* Check for duplicates */ for (i = 0; i < numobjects; i++) { if (handles[i] == _WAPI_THREAD_CURRENT) { handles[i] = wapi_get_current_thread_handle (); if (handles[i] == NULL) { MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Handle %d bogus", __func__, i); bogustype = TRUE; break; } } if ((GPOINTER_TO_UINT (handles[i]) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) { MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Handle %d pseudo process", __func__, i); bogustype = TRUE; break; } if (_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_WAIT) == FALSE) { MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Handle %p can't be waited for", __func__, handles[i]); bogustype = TRUE; break; } sorted_handles [i] = handles [i]; _wapi_handle_ops_prewait (handles[i]); } qsort (sorted_handles, numobjects, sizeof (gpointer), g_direct_equal); for (i = 1; i < numobjects; i++) { if (sorted_handles [i - 1] == sorted_handles [i]) { duplicate = TRUE; break; } } if (duplicate == TRUE) { MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning due to duplicates", __func__); return(WAIT_FAILED); } if (bogustype == TRUE) { MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: Returning due to bogus type", __func__); return(WAIT_FAILED); } poll = FALSE; for (i = 0; i < numobjects; ++i) if (_wapi_handle_type (handles [i]) == WAPI_HANDLE_PROCESS || _WAPI_SHARED_HANDLE (_wapi_handle_type (handles[i]))) /* Can't wait for a process handle + another handle without polling */ poll = TRUE; done = test_and_own (numobjects, handles, waitall, &count, &lowest); if (done == TRUE) { return(WAIT_OBJECT_0+lowest); } if (timeout == 0) { return WAIT_TIMEOUT; } if (timeout != INFINITE) end = mono_100ns_ticks () + timeout * 1000 * 10; /* Have to wait for some or all handles to become signalled */ for (i = 0; i < numobjects; i++) { /* Add a reference, as we need to ensure the handle wont * disappear from under us while we're waiting in the loop * (not lock, as we don't want exclusive access here) */ _wapi_handle_ref (handles[i]); } while(1) { /* Prod all handles with prewait methods and * special-wait handles that aren't already signalled */ for (i = 0; i < numobjects; i++) { _wapi_handle_ops_prewait (handles[i]); if (_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_SPECIAL_WAIT) == TRUE && _wapi_handle_issignalled (handles[i]) == FALSE) { _wapi_handle_ops_special_wait (handles[i], 0, alertable); } } MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: locking signal mutex", __func__); thr_ret = _wapi_handle_lock_signal_mutex (); g_assert (thr_ret == 0); /* Check the signalled state of handles inside the critical section */ if (waitall) { done = TRUE; for (i = 0; i < numobjects; i++) if (!_wapi_handle_issignalled (handles [i])) done = FALSE; } else { done = FALSE; for (i = 0; i < numobjects; i++) if (_wapi_handle_issignalled (handles [i])) done = TRUE; } if (!done) { /* Enter the wait */ if (timeout == INFINITE) { ret = _wapi_handle_timedwait_signal (INFINITE, poll, &apc_pending); } else { now = mono_100ns_ticks (); if (end < now) { ret = WAIT_TIMEOUT; } else { ret = _wapi_handle_timedwait_signal ((end - now) / 10 / 1000, poll, &apc_pending); } } } else { /* No need to wait */ ret = 0; } MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: unlocking signal mutex", __func__); thr_ret = _wapi_handle_unlock_signal_mutex (NULL); g_assert (thr_ret == 0); if (alertable && apc_pending) { retval = WAIT_IO_COMPLETION; break; } /* Check if everything is signalled, as we can't * guarantee to notice a shared signal even if the * wait timed out */ done = test_and_own (numobjects, handles, waitall, &count, &lowest); if (done == TRUE) { retval = WAIT_OBJECT_0+lowest; break; } else if (ret != 0) { /* Didn't get all handles, and there was a * timeout or other error */ MONO_TRACE (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: wait returned error: %s", __func__, strerror (ret)); if(ret==ETIMEDOUT) { retval = WAIT_TIMEOUT; } else { retval = WAIT_FAILED; } break; } } for (i = 0; i < numobjects; i++) { /* Unref everything we reffed above */ _wapi_handle_unref (handles[i]); } return retval; }
/** * WaitForMultipleObjectsEx: * @numobjects: The number of objects in @handles. The maximum allowed * is %MAXIMUM_WAIT_OBJECTS. * @handles: An array of object handles. Duplicates are not allowed. * @waitall: If %TRUE, this function waits until all of the handles * are signalled. If %FALSE, this function returns when any object is * signalled. * @timeout: The maximum time in milliseconds to wait for. * @alertable: if TRUE, the wait can be interrupted by an APC call * * This function returns when either one or more of @handles is * signalled, or @timeout ms elapses. If @timeout is zero, the state * of each item of @handles is tested and the function returns * immediately. If @timeout is %INFINITE, the function waits forever. * * Return value: %WAIT_OBJECT_0 to %WAIT_OBJECT_0 + @numobjects - 1 - * if @waitall is %TRUE, indicates that all objects are signalled. If * @waitall is %FALSE, the return value minus %WAIT_OBJECT_0 indicates * the first index into @handles of the objects that are signalled. * %WAIT_ABANDONED_0 to %WAIT_ABANDONED_0 + @numobjects - 1 - if * @waitall is %TRUE, indicates that all objects are signalled, and at * least one object is an abandoned mutex object (See * WaitForSingleObject() for a description of abandoned mutexes.) If * @waitall is %FALSE, the return value minus %WAIT_ABANDONED_0 * indicates the first index into @handles of an abandoned mutex. * %WAIT_TIMEOUT - The @timeout interval elapsed and no objects in * @handles are signalled. %WAIT_FAILED - an error occurred. * %WAIT_IO_COMPLETION - the wait was ended by an APC. */ guint32 WaitForMultipleObjectsEx(guint32 numobjects, gpointer *handles, gboolean waitall, guint32 timeout, gboolean alertable) { GHashTable *dups; gboolean duplicate = FALSE, bogustype = FALSE, done; guint32 count, lowest; struct timespec abstime; guint i; guint32 ret; int thr_ret; gpointer current_thread = _wapi_thread_handle_from_id (pthread_self ()); guint32 retval; gboolean poll; if (current_thread == NULL) { SetLastError (ERROR_INVALID_HANDLE); return(WAIT_FAILED); } if (numobjects > MAXIMUM_WAIT_OBJECTS) { #ifdef DEBUG g_message ("%s: Too many handles: %d", __func__, numobjects); #endif return(WAIT_FAILED); } if (numobjects == 1) { return WaitForSingleObjectEx (handles [0], timeout, alertable); } /* Check for duplicates */ dups = g_hash_table_new (g_direct_hash, g_direct_equal); for (i = 0; i < numobjects; i++) { gpointer exists; if (handles[i] == _WAPI_THREAD_CURRENT) { handles[i] = _wapi_thread_handle_from_id (pthread_self ()); if (handles[i] == NULL) { #ifdef DEBUG g_message ("%s: Handle %d bogus", __func__, i); #endif bogustype = TRUE; break; } } if ((GPOINTER_TO_UINT (handles[i]) & _WAPI_PROCESS_UNHANDLED) == _WAPI_PROCESS_UNHANDLED) { #ifdef DEBUG g_message ("%s: Handle %d pseudo process", __func__, i); #endif bogustype = TRUE; break; } exists = g_hash_table_lookup (dups, handles[i]); if (exists != NULL) { #ifdef DEBUG g_message ("%s: Handle %p duplicated", __func__, handles[i]); #endif duplicate = TRUE; break; } if (_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_WAIT) == FALSE) { #ifdef DEBUG g_message ("%s: Handle %p can't be waited for", __func__, handles[i]); #endif bogustype = TRUE; break; } g_hash_table_insert (dups, handles[i], handles[i]); _wapi_handle_ops_prewait (handles[i]); } g_hash_table_destroy (dups); if (duplicate == TRUE) { #ifdef DEBUG g_message ("%s: Returning due to duplicates", __func__); #endif return(WAIT_FAILED); } if (bogustype == TRUE) { #ifdef DEBUG g_message ("%s: Returning due to bogus type", __func__); #endif return(WAIT_FAILED); } poll = FALSE; for (i = 0; i < numobjects; ++i) if (_wapi_handle_type (handles [i]) == WAPI_HANDLE_PROCESS) /* Can't wait for a process handle + another handle without polling */ poll = TRUE; done = test_and_own (numobjects, handles, waitall, &count, &lowest); if (done == TRUE) { return(WAIT_OBJECT_0+lowest); } if (timeout == 0) { return WAIT_TIMEOUT; } /* Have to wait for some or all handles to become signalled */ if(timeout!=INFINITE) { _wapi_calc_timeout (&abstime, timeout); } if (alertable && _wapi_thread_apc_pending (current_thread)) { _wapi_thread_dispatch_apc_queue (current_thread); return WAIT_IO_COMPLETION; } for (i = 0; i < numobjects; i++) { /* Add a reference, as we need to ensure the handle wont * disappear from under us while we're waiting in the loop * (not lock, as we don't want exclusive access here) */ _wapi_handle_ref (handles[i]); } while(1) { /* Prod all handles with prewait methods and * special-wait handles that aren't already signalled */ for (i = 0; i < numobjects; i++) { _wapi_handle_ops_prewait (handles[i]); if (_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_SPECIAL_WAIT) == TRUE && _wapi_handle_issignalled (handles[i]) == FALSE) { _wapi_handle_ops_special_wait (handles[i], 0); } } #ifdef DEBUG g_message ("%s: locking signal mutex", __func__); #endif pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_signal_mutex, NULL); thr_ret = _wapi_handle_lock_signal_mutex (); g_assert (thr_ret == 0); /* Check the signalled state of handles inside the critical section */ if (waitall) { done = TRUE; for (i = 0; i < numobjects; i++) if (!_wapi_handle_issignalled (handles [i])) done = FALSE; } else { done = FALSE; for (i = 0; i < numobjects; i++) if (_wapi_handle_issignalled (handles [i])) done = TRUE; } if (!done) { /* Enter the wait */ if (timeout == INFINITE) { ret = _wapi_handle_wait_signal (poll); } else { ret = _wapi_handle_timedwait_signal (&abstime, poll); } } else { /* No need to wait */ ret = 0; } #ifdef DEBUG g_message ("%s: unlocking signal mutex", __func__); #endif thr_ret = _wapi_handle_unlock_signal_mutex (NULL); g_assert (thr_ret == 0); pthread_cleanup_pop (0); if (alertable && _wapi_thread_apc_pending (current_thread)) { _wapi_thread_dispatch_apc_queue (current_thread); retval = WAIT_IO_COMPLETION; break; } /* Check if everything is signalled, as we can't * guarantee to notice a shared signal even if the * wait timed out */ done = test_and_own (numobjects, handles, waitall, &count, &lowest); if (done == TRUE) { retval = WAIT_OBJECT_0+lowest; break; } else if (ret != 0) { /* Didn't get all handles, and there was a * timeout or other error */ #ifdef DEBUG g_message ("%s: wait returned error: %s", __func__, strerror (ret)); #endif if(ret==ETIMEDOUT) { retval = WAIT_TIMEOUT; } else { retval = WAIT_FAILED; } break; } } for (i = 0; i < numobjects; i++) { /* Unref everything we reffed above */ _wapi_handle_unref (handles[i]); } return retval; }
/** * WaitForMultipleObjectsEx: * @numobjects: The number of objects in @handles. The maximum allowed * is %IO_LAYER_MAXIMUM_WAIT_OBJECTS. * @handles: An array of object handles. Duplicates are not allowed. * @waitall: If %TRUE, this function waits until all of the handles * are signalled. If %FALSE, this function returns when any object is * signalled. * @timeout: The maximum time in milliseconds to wait for. * @alertable: if TRUE, the wait can be interrupted by an APC call * * This function returns when either one or more of @handles is * signalled, or @timeout ms elapses. If @timeout is zero, the state * of each item of @handles is tested and the function returns * immediately. If @timeout is %INFINITE, the function waits forever. * * Return value: %WAIT_OBJECT_0 to %WAIT_OBJECT_0 + @numobjects - 1 - * if @waitall is %TRUE, indicates that all objects are signalled. If * @waitall is %FALSE, the return value minus %WAIT_OBJECT_0 indicates * the first index into @handles of the objects that are signalled. * %WAIT_ABANDONED_0 to %WAIT_ABANDONED_0 + @numobjects - 1 - if * @waitall is %TRUE, indicates that all objects are signalled, and at * least one object is an abandoned mutex object (See * WaitForSingleObject() for a description of abandoned mutexes.) If * @waitall is %FALSE, the return value minus %WAIT_ABANDONED_0 * indicates the first index into @handles of an abandoned mutex. * %WAIT_TIMEOUT - The @timeout interval elapsed and no objects in * @handles are signalled. %WAIT_FAILED - an error occurred. * %WAIT_IO_COMPLETION - the wait was ended by an APC. */ guint32 WaitForMultipleObjectsEx(guint32 numobjects, gpointer *handles, gboolean waitall, guint32 timeout, gboolean alertable) { GHashTable *dups; gboolean duplicate = FALSE, bogustype = FALSE, done; guint32 count, lowest; struct timespec abstime; guint i; guint32 ret; int thr_ret; gpointer current_thread = GetCurrentThread (); if (numobjects > IO_LAYER_MAXIMUM_WAIT_OBJECTS) { #ifdef DEBUG g_message ("%s: Too many handles: %d", __func__, numobjects); #endif return(WAIT_FAILED); } if (numobjects == 1) { return WaitForSingleObjectEx (handles [0], timeout, alertable); } /* Check for duplicates */ dups = g_hash_table_new (g_direct_hash, g_direct_equal); for (i = 0; i < numobjects; i++) { gpointer exists = g_hash_table_lookup (dups, handles[i]); if (exists != NULL) { #ifdef DEBUG g_message ("%s: Handle %p duplicated", __func__, handles[i]); #endif duplicate = TRUE; break; } if (_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_WAIT) == FALSE) { #ifdef DEBUG g_message ("%s: Handle %p can't be waited for", __func__, handles[i]); #endif bogustype = TRUE; } g_hash_table_insert (dups, handles[i], handles[i]); } g_hash_table_destroy (dups); if (duplicate == TRUE) { #ifdef DEBUG g_message ("%s: Returning due to duplicates", __func__); #endif return(WAIT_FAILED); } if (bogustype == TRUE) { #ifdef DEBUG g_message ("%s: Returning due to bogus type", __func__); #endif return(WAIT_FAILED); } done = test_and_own (numobjects, handles, waitall, &count, &lowest); if (done == TRUE) { return(WAIT_OBJECT_0+lowest); } /* Have to wait for some or all handles to become signalled */ if(timeout!=INFINITE) { _wapi_calc_timeout (&abstime, timeout); } if (alertable && _wapi_thread_apc_pending (current_thread)) { _wapi_thread_dispatch_apc_queue (current_thread); return WAIT_IO_COMPLETION; } while(1) { /* Prod all special-wait handles that aren't already * signalled */ for (i = 0; i < numobjects; i++) { if (_wapi_handle_test_capabilities (handles[i], WAPI_HANDLE_CAP_SPECIAL_WAIT) == TRUE && _wapi_handle_issignalled (handles[i]) == FALSE) { _wapi_handle_ops_special_wait (handles[i], 0); } } /* Check before waiting on the condition, just in case */ done = test_and_own (numobjects, handles, waitall, &count, &lowest); if (done == TRUE) { return(WAIT_OBJECT_0 + lowest); } #ifdef DEBUG g_message ("%s: locking signal mutex", __func__); #endif pthread_cleanup_push ((void(*)(void *))_wapi_handle_unlock_signal_mutex, NULL); thr_ret = _wapi_handle_lock_signal_mutex (); g_assert (thr_ret == 0); if (timeout == INFINITE) { ret = _wapi_handle_wait_signal (); } else { ret = _wapi_handle_timedwait_signal (&abstime); } #ifdef DEBUG g_message ("%s: unlocking signal mutex", __func__); #endif thr_ret = _wapi_handle_unlock_signal_mutex (NULL); g_assert (thr_ret == 0); pthread_cleanup_pop (0); if (alertable && _wapi_thread_apc_pending (current_thread)) { _wapi_thread_dispatch_apc_queue (current_thread); return WAIT_IO_COMPLETION; } /* Check if everything is signalled, as we can't * guarantee to notice a shared signal even if the * wait timed out */ done = test_and_own (numobjects, handles, waitall, &count, &lowest); if (done == TRUE) { return(WAIT_OBJECT_0+lowest); } else if (ret != 0) { /* Didn't get all handles, and there was a * timeout or other error */ #ifdef DEBUG g_message ("%s: wait returned error: %s", __func__, strerror (ret)); #endif if(ret==ETIMEDOUT) { return(WAIT_TIMEOUT); } else { return(WAIT_FAILED); } } } }