#include <mono/metadata/gc-internals.h>
#include <mono/metadata/marshal.h>
#include <mono/metadata/runtime.h>
-#include <mono/io-layer/io-layer.h>
#include <mono/metadata/object-internals.h>
#include <mono/metadata/mono-debug-debugger.h>
#include <mono/utils/monobitset.h>
#include <mono/utils/mono-tls.h>
#include <mono/utils/atomic.h>
#include <mono/utils/mono-memory-model.h>
-#include <mono/utils/mono-threads-coop.h>
#include <mono/utils/mono-error-internals.h>
#include <mono/utils/os-event.h>
#include <mono/utils/mono-threads-debug.h>
#include <mono/metadata/w32event.h>
#include <mono/metadata/w32mutex.h>
-#include <mono/metadata/gc-internals.h>
#include <mono/metadata/reflection-internals.h>
#include <mono/metadata/abi-details.h>
+#include <mono/metadata/w32error.h>
+#include <mono/utils/w32api.h>
#ifdef HAVE_SIGNAL_H
#include <signal.h>
*/
static MonoGHashTable *threads_starting_up = NULL;
-/* The TLS key that holds the MonoObject assigned to each thread */
-static MonoNativeTlsKey current_object_key;
-
/* Contains tids */
/* Protected by the threads lock */
static GHashTable *joinable_threads;
static int joinable_thread_count;
-#ifdef MONO_HAVE_FAST_TLS
-/* we need to use both the Tls* functions and __thread because
- * the gc needs to see all the threads
- */
-MONO_FAST_TLS_DECLARE(tls_current_object);
-#define SET_CURRENT_OBJECT(x) do { \
- MONO_FAST_TLS_SET (tls_current_object, x); \
- mono_native_tls_set_value (current_object_key, x); \
-} while (FALSE)
-#define GET_CURRENT_OBJECT() ((MonoInternalThread*) MONO_FAST_TLS_GET (tls_current_object))
-#else
-#define SET_CURRENT_OBJECT(x) mono_native_tls_set_value (current_object_key, x)
-#define GET_CURRENT_OBJECT() (MonoInternalThread*) mono_native_tls_get_value (current_object_key)
-#endif
+#define SET_CURRENT_OBJECT(x) mono_tls_set_thread (x)
+#define GET_CURRENT_OBJECT() (MonoInternalThread*) mono_tls_get_thread ()
/* function called at thread start */
static MonoThreadStartCB mono_thread_start_cb = NULL;
static gint32 managed_thread_id_counter = 0;
/* Class lazy loading functions */
-static GENERATE_GET_CLASS_WITH_CACHE (appdomain_unloaded_exception, System, AppDomainUnloadedException)
+static GENERATE_GET_CLASS_WITH_CACHE (appdomain_unloaded_exception, "System", "AppDomainUnloadedException")
static void
mono_threads_lock (void)
return InterlockedIncrement (&managed_thread_id_counter);
}
-MonoNativeTlsKey
-mono_thread_get_tls_key (void)
+enum {
+ INTERRUPT_REQUESTED_BIT = 0x1,
+ INTERRUPT_REQUEST_DEFERRED_BIT = 0x2,
+ ABORT_PROT_BLOCK_SHIFT = 2,
+ ABORT_PROT_BLOCK_BITS = 8,
+ ABORT_PROT_BLOCK_MASK = (((1 << ABORT_PROT_BLOCK_BITS) - 1) << ABORT_PROT_BLOCK_SHIFT)
+};
+
+static int
+mono_thread_get_abort_prot_block_count (MonoInternalThread *thread)
{
- return current_object_key;
+ gsize state = thread->thread_state;
+ return (state & ABORT_PROT_BLOCK_MASK) >> ABORT_PROT_BLOCK_SHIFT;
}
-gint32
-mono_thread_get_tls_offset (void)
+static void
+verify_thread_state (gsize state)
{
- int offset = -1;
+ //can't have both INTERRUPT_REQUESTED_BIT and INTERRUPT_REQUEST_DEFERRED_BIT set at the same time
+ g_assert ((state & (INTERRUPT_REQUESTED_BIT | INTERRUPT_REQUEST_DEFERRED_BIT)) != (INTERRUPT_REQUESTED_BIT | INTERRUPT_REQUEST_DEFERRED_BIT));
-#ifdef HOST_WIN32
- if (current_object_key)
- offset = current_object_key;
-#else
- MONO_THREAD_VAR_OFFSET (tls_current_object,offset);
-#endif
- return offset;
+ //XXX This would be nice to be true, but can happen due to self-aborts (and possibly set-pending-exception)
+ //if prot_count > 0, INTERRUPT_REQUESTED_BIT must never be set
+ // int prot_count = (state & ABORT_PROT_BLOCK_MASK) >> ABORT_PROT_BLOCK_SHIFT;
+ // g_assert (!(prot_count > 0 && (state & INTERRUPT_REQUESTED_BIT)));
+}
+
+void
+mono_threads_begin_abort_protected_block (void)
+{
+ MonoInternalThread *thread = mono_thread_internal_current ();
+ gsize old_state, new_state;
+ do {
+ old_state = thread->thread_state;
+ verify_thread_state (old_state);
+
+ int new_val = ((old_state & ABORT_PROT_BLOCK_MASK) >> ABORT_PROT_BLOCK_SHIFT) + 1;
+
+ new_state = 0;
+ if (old_state & (INTERRUPT_REQUESTED_BIT | INTERRUPT_REQUEST_DEFERRED_BIT)) {
+ if (old_state & INTERRUPT_REQUESTED_BIT)
+ printf ("begin prot happy as it demoted interrupt to deferred interrupt\n");
+ new_state |= INTERRUPT_REQUEST_DEFERRED_BIT;
+ }
+
+ //bounds check abort_prot_count
+ g_assert (new_val > 0);
+ g_assert (new_val < (1 << ABORT_PROT_BLOCK_BITS));
+ new_state |= new_val << ABORT_PROT_BLOCK_SHIFT;
+
+ } while (InterlockedCompareExchangePointer ((volatile gpointer)&thread->thread_state, (gpointer)new_state, (gpointer)old_state) != (gpointer)old_state);
+}
+
+gboolean
+mono_threads_end_abort_protected_block (void)
+{
+ MonoInternalThread *thread = mono_thread_internal_current ();
+ gsize old_state, new_state;
+ do {
+ old_state = thread->thread_state;
+ verify_thread_state (old_state);
+
+ int new_val = ((old_state & ABORT_PROT_BLOCK_MASK) >> ABORT_PROT_BLOCK_SHIFT) - 1;
+ new_state = 0;
+
+ if ((old_state & INTERRUPT_REQUEST_DEFERRED_BIT) && new_val == 0) {
+ printf ("end abort on alert, promoted deferred to pront interrupt\n");
+ new_state |= INTERRUPT_REQUESTED_BIT;
+ }
+
+ //bounds check abort_prot_count
+ g_assert (new_val >= 0);
+ g_assert (new_val < (1 << ABORT_PROT_BLOCK_BITS));
+ new_state |= new_val << ABORT_PROT_BLOCK_SHIFT;
+
+ } while (InterlockedCompareExchangePointer ((volatile gpointer)&thread->thread_state, (gpointer)new_state, (gpointer)old_state) != (gpointer)old_state);
+ return (new_state & INTERRUPT_REQUESTED_BIT) == INTERRUPT_REQUESTED_BIT;
+}
+
+
+//Don't use this function, use inc/dec below
+static void
+mono_thread_abort_prot_block_count_add (MonoInternalThread *thread, int val)
+{
+ gsize old_state, new_state;
+ do {
+ old_state = thread->thread_state;
+ verify_thread_state (old_state);
+
+ int new_val = val + ((old_state & ABORT_PROT_BLOCK_MASK) >> ABORT_PROT_BLOCK_SHIFT);
+ //bounds check abort_prot_count
+ g_assert (new_val >= 0);
+ g_assert (new_val < (1 << ABORT_PROT_BLOCK_BITS));
+ new_state = (old_state & ~ABORT_PROT_BLOCK_MASK) | (new_val << ABORT_PROT_BLOCK_SHIFT);
+
+ } while (InterlockedCompareExchangePointer ((volatile gpointer)&thread->thread_state, (gpointer)new_state, (gpointer)old_state) != (gpointer)old_state);
+}
+
+static void
+mono_thread_inc_abort_prot_block_count (MonoInternalThread *thread)
+{
+ mono_thread_abort_prot_block_count_add (thread, 1);
+}
+
+static void
+mono_thread_dec_abort_prot_block_count (MonoInternalThread *thread)
+{
+ mono_thread_abort_prot_block_count_add (thread, -1);
+}
+
+static gboolean
+mono_thread_get_interruption_requested (MonoInternalThread *thread)
+{
+ gsize state = thread->thread_state;
+ return (state & INTERRUPT_REQUESTED_BIT) == INTERRUPT_REQUESTED_BIT;
+}
+
+/* Returns TRUE is there was a state change */
+static gboolean
+mono_thread_clear_interruption_requested (MonoInternalThread *thread)
+{
+ gsize old_state, new_state;
+ do {
+ old_state = thread->thread_state;
+ verify_thread_state (old_state);
+
+ //Already cleared
+ if (!(old_state & (INTERRUPT_REQUESTED_BIT | INTERRUPT_REQUEST_DEFERRED_BIT)))
+ return FALSE;
+ new_state = old_state & ~(INTERRUPT_REQUESTED_BIT | INTERRUPT_REQUEST_DEFERRED_BIT);
+ } while (InterlockedCompareExchangePointer ((volatile gpointer)&thread->thread_state, (gpointer)new_state, (gpointer)old_state) != (gpointer)old_state);
+ return TRUE;
+}
+
+/* Returns TRUE is there was a state change */
+static gboolean
+mono_thread_set_interruption_requested (MonoInternalThread *thread)
+{
+ //always force when the current thread is doing it to itself.
+ gboolean force_interrupt = thread == mono_thread_internal_current ();
+ gsize old_state, new_state;
+ do {
+ old_state = thread->thread_state;
+ verify_thread_state (old_state);
+
+ int prot_count = ((old_state & ABORT_PROT_BLOCK_MASK) >> ABORT_PROT_BLOCK_SHIFT);
+ //Already set
+ if (old_state & (INTERRUPT_REQUESTED_BIT | INTERRUPT_REQUEST_DEFERRED_BIT))
+ return FALSE;
+
+ //If there's an outstanding prot block, we queue it
+ if (prot_count && !force_interrupt) {
+ printf ("set interrupt unhappy, as it's only putting a deferred req %d\n", force_interrupt);
+ new_state = old_state | INTERRUPT_REQUEST_DEFERRED_BIT;
+ } else
+ new_state = old_state | INTERRUPT_REQUESTED_BIT;
+ } while (InterlockedCompareExchangePointer ((volatile gpointer)&thread->thread_state, (gpointer)new_state, (gpointer)old_state) != (gpointer)old_state);
+
+ return (new_state & INTERRUPT_REQUESTED_BIT) == INTERRUPT_REQUESTED_BIT;
}
static inline MonoNativeThreadId
return klass == mono_defaults.threadabortexception_class;
}
-/*
- * NOTE: this function can be called also for threads different from the current one:
- * make sure no code called from it will ever assume it is run on the thread that is
- * getting cleaned up.
- */
-static void thread_cleanup (MonoInternalThread *thread)
-{
- gboolean ret;
-
- g_assert (thread != NULL);
-
- if (thread->abort_state_handle) {
- mono_gchandle_free (thread->abort_state_handle);
- thread->abort_state_handle = 0;
- }
- thread->abort_exc = NULL;
- thread->current_appcontext = NULL;
-
- /*
- * This is necessary because otherwise we might have
- * cross-domain references which will not get cleaned up when
- * the target domain is unloaded.
- */
- if (thread->cached_culture_info) {
- int i;
- for (i = 0; i < NUM_CACHED_CULTURES * 2; ++i)
- mono_array_set (thread->cached_culture_info, MonoObject*, i, NULL);
- }
-
- /*
- * thread->synch_cs can be NULL if this was called after
- * ves_icall_System_Threading_InternalThread_Thread_free_internal.
- * This can happen only during shutdown.
- * The shutting_down flag is not always set, so we can't assert on it.
- */
- if (thread->synch_cs)
- LOCK_THREAD (thread);
-
- thread->state |= ThreadState_Stopped;
- thread->state &= ~ThreadState_Background;
-
- if (thread->synch_cs)
- UNLOCK_THREAD (thread);
-
- /*
- An interruption request has leaked to cleanup. Adjust the global counter.
-
- This can happen is the abort source thread finds the abortee (this) thread
- in unmanaged code. If this thread never trips back to managed code or check
- the local flag it will be left set and positively unbalance the global counter.
-
- Leaving the counter unbalanced will cause a performance degradation since all threads
- will now keep checking their local flags all the time.
- */
- if (InterlockedExchange (&thread->interruption_requested, 0))
- InterlockedDecrement (&thread_interruption_requested);
-
- mono_threads_lock ();
-
- if (!threads) {
- ret = FALSE;
- } else if (mono_g_hash_table_lookup (threads, (gpointer)thread->tid) != thread) {
- /* We have to check whether the thread object for the
- * tid is still the same in the table because the
- * thread might have been destroyed and the tid reused
- * in the meantime, in which case the tid would be in
- * the table, but with another thread object.
- */
- ret = FALSE;
- } else {
- mono_g_hash_table_remove (threads, (gpointer)thread->tid);
- ret = TRUE;
- }
-
- mono_threads_unlock ();
-
- /* Don't close the handle here, wait for the object finalizer
- * to do it. Otherwise, the following race condition applies:
- *
- * 1) Thread exits (and thread_cleanup() closes the handle)
- *
- * 2) Some other handle is reassigned the same slot
- *
- * 3) Another thread tries to join the first thread, and
- * blocks waiting for the reassigned handle to be signalled
- * (which might never happen). This is possible, because the
- * thread calling Join() still has a reference to the first
- * thread's object.
- */
-
- /* if the thread is not in the hash it has been removed already */
- if (!ret) {
- if (thread == mono_thread_internal_current ()) {
- mono_domain_unset ();
- mono_memory_barrier ();
- }
- if (mono_thread_cleanup_fn)
- mono_thread_cleanup_fn (thread_get_tid (thread));
- return;
- }
- mono_release_type_locks (thread);
-
- /* Can happen when we attach the profiler helper thread in order to heapshot. */
- if (!mono_thread_info_lookup (MONO_UINT_TO_NATIVE_THREAD_ID (thread->tid))->tools_thread)
- mono_profiler_thread_end (thread->tid);
-
- mono_hazard_pointer_clear (mono_hazard_pointer_get (), 1);
-
- if (thread == mono_thread_internal_current ()) {
- /*
- * This will signal async signal handlers that the thread has exited.
- * The profiler callback needs this to be set, so it cannot be done earlier.
- */
- mono_domain_unset ();
- mono_memory_barrier ();
- }
-
- if (thread == mono_thread_internal_current ())
- mono_thread_pop_appdomain_ref ();
-
- thread->cached_culture_info = NULL;
-
- mono_free_static_data (thread->static_data);
- thread->static_data = NULL;
- ref_stack_destroy (thread->appdomain_refs);
- thread->appdomain_refs = NULL;
-
- if (mono_thread_cleanup_fn)
- mono_thread_cleanup_fn (thread_get_tid (thread));
-
- if (mono_gc_is_moving ()) {
- MONO_GC_UNREGISTER_ROOT (thread->thread_pinning_ref);
- thread->thread_pinning_ref = NULL;
- }
-
-}
-
/*
* A special static data offset (guint32) consists of 3 parts:
*
thread->priority = MONO_THREAD_PRIORITY_NORMAL;
thread->suspended = g_new0 (MonoOSEvent, 1);
- mono_os_event_init (thread->suspended, TRUE, TRUE);
+ mono_os_event_init (thread->suspended, TRUE);
return thread;
}
param.sched_priority = 0;
break;
default:
- g_error ("%s: unknown policy %d", __func__, policy);
+ g_warning ("%s: unknown policy %d", __func__, policy);
+ return;
}
}
if (shutting_down && !force_attach) {
mono_threads_unlock ();
+ mono_thread_pop_appdomain_ref ();
return FALSE;
}
if (!threads) {
- MONO_GC_REGISTER_ROOT_FIXED (threads, MONO_ROOT_SOURCE_THREADING, "threads table");
threads = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_THREADING, "threads table");
}
/* If the thread calls ExitThread at all, this remaining code
* will not be executed, but the main thread will eventually
- * call thread_cleanup() on this thread's behalf.
+ * call mono_thread_detach_internal() on this thread's behalf.
*/
THREAD_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") Start wrapper terminating", __func__, mono_native_thread_id_get ()));
/* Do any cleanup needed for apartment state. This
- * cannot be done in thread_cleanup since thread_cleanup could be
- * called for a thread other than the current thread.
+ * cannot be done in mono_thread_detach_internal since
+ * mono_thread_detach_internal could be called for a
+ * thread other than the current thread.
* mono_thread_cleanup_apartment_state cleans up apartment
* for the current thead */
mono_thread_cleanup_apartment_state ();
*/
mono_threads_join_threads ();
- mono_error_init (error);
+ error_init (error);
mono_threads_lock ();
if (shutting_down) {
return FALSE;
}
if (threads_starting_up == NULL) {
- MONO_GC_REGISTER_ROOT_FIXED (threads_starting_up, MONO_ROOT_SOURCE_THREADING, "starting threads table");
threads_starting_up = mono_g_hash_table_new_type (NULL, NULL, MONO_HASH_KEY_VALUE_GC, MONO_ROOT_SOURCE_THREADING, "starting threads table");
}
mono_g_hash_table_insert (threads_starting_up, thread, thread);
mono_threads_lock ();
mono_g_hash_table_remove (threads_starting_up, thread);
mono_threads_unlock ();
- mono_error_set_execution_engine (error, "Couldn't create thread. Error 0x%x", GetLastError());
+ mono_error_set_execution_engine (error, "Couldn't create thread. Error 0x%x", mono_w32error_get_last());
/* ref is not going to be decremented in start_wrapper_internal */
InterlockedDecrement (&start_info->ref);
ret = FALSE;
MonoInternalThread *internal;
gboolean res;
- mono_error_init (error);
+ error_init (error);
internal = create_internal_thread_object ();
void
mono_thread_detach_internal (MonoInternalThread *thread)
{
- g_return_if_fail (thread != NULL);
+ gboolean removed;
+
+ g_assert (thread != NULL);
THREAD_DEBUG (g_message ("%s: mono_thread_detach for %p (%"G_GSIZE_FORMAT")", __func__, thread, (gsize)thread->tid));
mono_w32mutex_abandon ();
#endif
- thread_cleanup (thread);
+ if (thread->abort_state_handle) {
+ mono_gchandle_free (thread->abort_state_handle);
+ thread->abort_state_handle = 0;
+ }
+
+ thread->abort_exc = NULL;
+ thread->current_appcontext = NULL;
+
+ /*
+ * This is necessary because otherwise we might have
+ * cross-domain references which will not get cleaned up when
+ * the target domain is unloaded.
+ */
+ if (thread->cached_culture_info) {
+ int i;
+ for (i = 0; i < NUM_CACHED_CULTURES * 2; ++i)
+ mono_array_set (thread->cached_culture_info, MonoObject*, i, NULL);
+ }
+
+ /*
+ * thread->synch_cs can be NULL if this was called after
+ * ves_icall_System_Threading_InternalThread_Thread_free_internal.
+ * This can happen only during shutdown.
+ * The shutting_down flag is not always set, so we can't assert on it.
+ */
+ if (thread->synch_cs)
+ LOCK_THREAD (thread);
+ thread->state |= ThreadState_Stopped;
+ thread->state &= ~ThreadState_Background;
+
+ if (thread->synch_cs)
+ UNLOCK_THREAD (thread);
+
+ /*
+ An interruption request has leaked to cleanup. Adjust the global counter.
+
+ This can happen is the abort source thread finds the abortee (this) thread
+ in unmanaged code. If this thread never trips back to managed code or check
+ the local flag it will be left set and positively unbalance the global counter.
+
+ Leaving the counter unbalanced will cause a performance degradation since all threads
+ will now keep checking their local flags all the time.
+ */
+ if (mono_thread_clear_interruption_requested (thread))
+ InterlockedDecrement (&thread_interruption_requested);
+
+ mono_threads_lock ();
+
+ if (!threads) {
+ removed = FALSE;
+ } else if (mono_g_hash_table_lookup (threads, (gpointer)thread->tid) != thread) {
+ /* We have to check whether the thread object for the
+ * tid is still the same in the table because the
+ * thread might have been destroyed and the tid reused
+ * in the meantime, in which case the tid would be in
+ * the table, but with another thread object.
+ */
+ removed = FALSE;
+ } else {
+ mono_g_hash_table_remove (threads, (gpointer)thread->tid);
+ removed = TRUE;
+ }
+
+ mono_threads_unlock ();
+
+ /* Don't close the handle here, wait for the object finalizer
+ * to do it. Otherwise, the following race condition applies:
+ *
+ * 1) Thread exits (and mono_thread_detach_internal() closes the handle)
+ *
+ * 2) Some other handle is reassigned the same slot
+ *
+ * 3) Another thread tries to join the first thread, and
+ * blocks waiting for the reassigned handle to be signalled
+ * (which might never happen). This is possible, because the
+ * thread calling Join() still has a reference to the first
+ * thread's object.
+ */
+
+ /* if the thread is not in the hash it has been removed already */
+ if (!removed) {
+ mono_domain_unset ();
+ mono_memory_barrier ();
+
+ if (mono_thread_cleanup_fn)
+ mono_thread_cleanup_fn (thread_get_tid (thread));
+
+ goto done;
+ }
+
+ mono_release_type_locks (thread);
+
+ /* Can happen when we attach the profiler helper thread in order to heapshot. */
+ if (!mono_thread_info_lookup (MONO_UINT_TO_NATIVE_THREAD_ID (thread->tid))->tools_thread)
+ mono_profiler_thread_end (thread->tid);
+
+ mono_hazard_pointer_clear (mono_hazard_pointer_get (), 1);
+
+ /*
+ * This will signal async signal handlers that the thread has exited.
+ * The profiler callback needs this to be set, so it cannot be done earlier.
+ */
+ mono_domain_unset ();
+ mono_memory_barrier ();
+
+ if (thread == mono_thread_internal_current ())
+ mono_thread_pop_appdomain_ref ();
+
+ thread->cached_culture_info = NULL;
+
+ mono_free_static_data (thread->static_data);
+ thread->static_data = NULL;
+ ref_stack_destroy (thread->appdomain_refs);
+ thread->appdomain_refs = NULL;
+
+ g_assert (thread->suspended);
+ mono_os_event_destroy (thread->suspended);
+ g_free (thread->suspended);
+ thread->suspended = NULL;
+
+ if (mono_thread_cleanup_fn)
+ mono_thread_cleanup_fn (thread_get_tid (thread));
+
+ mono_memory_barrier ();
+
+ if (mono_gc_is_moving ()) {
+ MONO_GC_UNREGISTER_ROOT (thread->thread_pinning_ref);
+ thread->thread_pinning_ref = NULL;
+ }
+
+done:
SET_CURRENT_OBJECT (NULL);
mono_domain_unset ();
InterlockedCompareExchangePointer ((volatile gpointer *)&this_obj->internal_thread, internal, NULL);
}
+MonoThread *
+ves_icall_System_Threading_Thread_GetCurrentThread (void)
+{
+ return mono_thread_current ();
+}
+
HANDLE
ves_icall_System_Threading_Thread_Thread_internal (MonoThread *this_obj,
MonoObject *start)
THREAD_DEBUG (g_message ("%s: Closing thread %p, handle %p", __func__, this, this_obj->handle));
/*
- * Since threads keep a reference to their thread object while running, by the time this function is called,
- * the thread has already exited/detached, i.e. thread_cleanup () has ran. The exception is during shutdown,
- * when thread_cleanup () can be called after this.
+ * Since threads keep a reference to their thread object while running, by
+ * the time this function is called, the thread has already exited/detached,
+ * i.e. mono_thread_detach_internal () has ran. The exception is during
+ * shutdown, when mono_thread_detach_internal () can be called after this.
*/
if (this_obj->handle) {
mono_threads_close_thread_handle (this_obj->handle);
this_obj->name = NULL;
g_free (name);
}
-
- g_assert (this_obj->suspended);
- mono_os_event_destroy (this_obj->suspended);
- g_free (this_obj->suspended);
- this_obj->suspended = NULL;
}
void
if (exc) {
mono_raise_exception (exc);
} else {
- // FIXME: !INFINITE
- if (ms != INFINITE)
+ // FIXME: !MONO_INFINITE_WAIT
+ if (ms != MONO_INFINITE_WAIT)
break;
}
} else {
MonoError error;
MonoString* str;
- mono_error_init (&error);
+ error_init (&error);
LOCK_THREAD (this_obj);
}
void
-mono_thread_set_name_internal (MonoInternalThread *this_obj, MonoString *name, gboolean permanent, MonoError *error)
+mono_thread_set_name_internal (MonoInternalThread *this_obj, MonoString *name, gboolean permanent, gboolean reset, MonoError *error)
{
LOCK_THREAD (this_obj);
- mono_error_init (error);
+ error_init (error);
- if ((this_obj->flags & MONO_THREAD_FLAG_NAME_SET)) {
+ if (reset) {
+ this_obj->flags &= ~MONO_THREAD_FLAG_NAME_SET;
+ } else if (this_obj->flags & MONO_THREAD_FLAG_NAME_SET) {
UNLOCK_THREAD (this_obj);
mono_error_set_invalid_operation (error, "Thread.Name can only be set once.");
this_obj->name_len = 0;
}
if (name) {
- this_obj->name = g_new (gunichar2, mono_string_length (name));
- memcpy (this_obj->name, mono_string_chars (name), mono_string_length (name) * 2);
+ this_obj->name = g_memdup (mono_string_chars (name), mono_string_length (name) * sizeof (gunichar2));
this_obj->name_len = mono_string_length (name);
if (permanent)
ves_icall_System_Threading_Thread_SetName_internal (MonoInternalThread *this_obj, MonoString *name)
{
MonoError error;
- mono_thread_set_name_internal (this_obj, name, TRUE, &error);
+ mono_thread_set_name_internal (this_obj, name, TRUE, FALSE, &error);
mono_error_set_pending_exception (&error);
}
{
MonoArray *copy;
- mono_error_init (error);
+ error_init (error);
if (!arr)
return NULL;
return res;
}
+static MonoThreadInfoWaitRet
+mono_join_uninterrupted (MonoThreadHandle* thread_to_join, gint32 ms, MonoError *error)
+{
+ MonoException *exc;
+ MonoThreadInfoWaitRet ret;
+ gint64 start;
+ gint32 diff_ms;
+ gint32 wait = ms;
+
+ error_init (error);
+
+ start = (ms == -1) ? 0 : mono_msec_ticks ();
+ for (;;) {
+ MONO_ENTER_GC_SAFE;
+ ret = mono_thread_info_wait_one_handle (thread_to_join, ms, TRUE);
+ MONO_EXIT_GC_SAFE;
+
+ if (ret != MONO_THREAD_INFO_WAIT_RET_ALERTED)
+ return ret;
+
+ exc = mono_thread_execute_interruption ();
+ if (exc) {
+ mono_error_set_exception_instance (error, exc);
+ return ret;
+ }
+
+ if (ms == -1)
+ continue;
+
+ /* Re-calculate ms according to the time passed */
+ diff_ms = (gint32)(mono_msec_ticks () - start);
+ if (diff_ms >= ms) {
+ ret = MONO_THREAD_INFO_WAIT_RET_TIMEOUT;
+ return ret;
+ }
+ wait = ms - diff_ms;
+ }
+
+ return ret;
+}
+
gboolean
ves_icall_System_Threading_Thread_Join_internal(MonoThread *this_obj, int ms)
{
MonoThreadHandle *handle = thread->handle;
MonoInternalThread *cur_thread = mono_thread_internal_current ();
gboolean ret;
+ MonoError error;
if (mono_thread_current_check_pending_interrupt ())
return FALSE;
UNLOCK_THREAD (thread);
if(ms== -1) {
- ms=INFINITE;
+ ms=MONO_INFINITE_WAIT;
}
THREAD_DEBUG (g_message ("%s: joining thread handle %p, %d ms", __func__, handle, ms));
mono_thread_set_state (cur_thread, ThreadState_WaitSleepJoin);
- MONO_ENTER_GC_SAFE;
- ret=mono_thread_info_wait_one_handle (handle, ms, TRUE);
- MONO_EXIT_GC_SAFE;
+ ret=mono_join_uninterrupted (handle, ms, &error);
mono_thread_clr_state (cur_thread, ThreadState_WaitSleepJoin);
-
+
+ mono_error_set_pending_exception (&error);
+
if(ret==MONO_THREAD_INFO_WAIT_RET_SUCCESS_0) {
THREAD_DEBUG (g_message ("%s: join successful", __func__));
#define MANAGED_WAIT_FAILED 0x7fffffff
static gint32
-map_native_wait_result_to_managed (gint32 val)
-{
- /* WAIT_FAILED in waithandle.cs is different from WAIT_FAILED in Win32 API */
- return val == WAIT_FAILED ? MANAGED_WAIT_FAILED : val;
+map_native_wait_result_to_managed (MonoW32HandleWaitRet val, gsize numobjects)
+{
+ if (val >= MONO_W32HANDLE_WAIT_RET_SUCCESS_0 && val < MONO_W32HANDLE_WAIT_RET_SUCCESS_0 + numobjects) {
+ return WAIT_OBJECT_0 + (val - MONO_W32HANDLE_WAIT_RET_SUCCESS_0);
+ } else if (val >= MONO_W32HANDLE_WAIT_RET_ABANDONED_0 && val < MONO_W32HANDLE_WAIT_RET_ABANDONED_0 + numobjects) {
+ return WAIT_ABANDONED_0 + (val - MONO_W32HANDLE_WAIT_RET_ABANDONED_0);
+ } else if (val == MONO_W32HANDLE_WAIT_RET_ALERTED) {
+ return WAIT_IO_COMPLETION;
+ } else if (val == MONO_W32HANDLE_WAIT_RET_TIMEOUT) {
+ return WAIT_TIMEOUT;
+ } else if (val == MONO_W32HANDLE_WAIT_RET_FAILED) {
+ /* WAIT_FAILED in waithandle.cs is different from WAIT_FAILED in Win32 API */
+ return MANAGED_WAIT_FAILED;
+ } else {
+ g_error ("%s: unknown val value %d", __func__, val);
+ }
}
-static gint32
+static MonoW32HandleWaitRet
mono_wait_uninterrupted (MonoInternalThread *thread, guint32 numhandles, gpointer *handles, gboolean waitall, gint32 ms, MonoError *error)
{
MonoException *exc;
- guint32 ret;
+ MonoW32HandleWaitRet ret;
gint64 start;
gint32 diff_ms;
gint32 wait = ms;
- mono_error_init (error);
+ error_init (error);
start = (ms == -1) ? 0 : mono_100ns_ticks ();
do {
MONO_ENTER_GC_SAFE;
+#ifdef HOST_WIN32
if (numhandles != 1)
- ret = WaitForMultipleObjectsEx (numhandles, handles, waitall, wait, TRUE);
+ ret = mono_w32handle_convert_wait_ret (WaitForMultipleObjectsEx (numhandles, handles, waitall, wait, TRUE), numhandles);
else
- ret = WaitForSingleObjectEx (handles [0], ms, TRUE);
+ ret = mono_w32handle_convert_wait_ret (WaitForSingleObjectEx (handles [0], ms, TRUE), 1);
+#else
+ /* mono_w32handle_wait_multiple optimizes the case for numhandles == 1 */
+ ret = mono_w32handle_wait_multiple (handles, numhandles, waitall, wait, TRUE);
+#endif /* HOST_WIN32 */
MONO_EXIT_GC_SAFE;
- if (ret != WAIT_IO_COMPLETION)
+ if (ret != MONO_W32HANDLE_WAIT_RET_ALERTED)
break;
exc = mono_thread_execute_interruption ();
/* Re-calculate ms according to the time passed */
diff_ms = (gint32)((mono_100ns_ticks () - start) / 10000);
if (diff_ms >= ms) {
- ret = WAIT_TIMEOUT;
+ ret = MONO_W32HANDLE_WAIT_RET_TIMEOUT;
break;
}
wait = ms - diff_ms;
MonoError error;
HANDLE *handles;
guint32 numhandles;
- guint32 ret;
+ MonoW32HandleWaitRet ret;
guint32 i;
MonoObject *waitHandle;
MonoInternalThread *thread = mono_thread_internal_current ();
/* Do this WaitSleepJoin check before creating objects */
if (mono_thread_current_check_pending_interrupt ())
- return map_native_wait_result_to_managed (WAIT_FAILED);
+ return map_native_wait_result_to_managed (MONO_W32HANDLE_WAIT_RET_FAILED, 0);
/* We fail in managed if the array has more than 64 elements */
numhandles = (guint32)mono_array_length(mono_handles);
}
if(ms== -1) {
- ms=INFINITE;
+ ms=MONO_INFINITE_WAIT;
}
mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
mono_error_set_pending_exception (&error);
- /* WAIT_FAILED in waithandle.cs is different from WAIT_FAILED in Win32 API */
- return map_native_wait_result_to_managed (ret);
+ return map_native_wait_result_to_managed (ret, numhandles);
}
gint32 ves_icall_System_Threading_WaitHandle_WaitAny_internal(MonoArray *mono_handles, gint32 ms)
MonoError error;
HANDLE handles [MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS];
uintptr_t numhandles;
- guint32 ret;
+ MonoW32HandleWaitRet ret;
guint32 i;
MonoObject *waitHandle;
MonoInternalThread *thread = mono_thread_internal_current ();
/* Do this WaitSleepJoin check before creating objects */
if (mono_thread_current_check_pending_interrupt ())
- return map_native_wait_result_to_managed (WAIT_FAILED);
+ return map_native_wait_result_to_managed (MONO_W32HANDLE_WAIT_RET_FAILED, 0);
numhandles = mono_array_length(mono_handles);
if (numhandles > MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS)
- return map_native_wait_result_to_managed (WAIT_FAILED);
+ return map_native_wait_result_to_managed (MONO_W32HANDLE_WAIT_RET_FAILED, 0);
for(i = 0; i < numhandles; i++) {
waitHandle = mono_array_get(mono_handles, MonoObject*, i);
}
if(ms== -1) {
- ms=INFINITE;
+ ms=MONO_INFINITE_WAIT;
}
mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
mono_error_set_pending_exception (&error);
- /* WAIT_FAILED in waithandle.cs is different from WAIT_FAILED in Win32 API */
- return map_native_wait_result_to_managed (ret);
+ return map_native_wait_result_to_managed (ret, numhandles);
}
gint32 ves_icall_System_Threading_WaitHandle_WaitOne_internal(HANDLE handle, gint32 ms)
{
MonoError error;
- guint32 ret;
+ MonoW32HandleWaitRet ret;
MonoInternalThread *thread = mono_thread_internal_current ();
THREAD_WAIT_DEBUG (g_message ("%s: (%"G_GSIZE_FORMAT") waiting for %p, %d ms", __func__, mono_native_thread_id_get (), handle, ms));
if(ms== -1) {
- ms=INFINITE;
+ ms=MONO_INFINITE_WAIT;
}
if (mono_thread_current_check_pending_interrupt ())
- return map_native_wait_result_to_managed (WAIT_FAILED);
+ return map_native_wait_result_to_managed (MONO_W32HANDLE_WAIT_RET_FAILED, 0);
mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
mono_error_set_pending_exception (&error);
- return map_native_wait_result_to_managed (ret);
+ return map_native_wait_result_to_managed (ret, 1);
}
gint32
ves_icall_System_Threading_WaitHandle_SignalAndWait_Internal (HANDLE toSignal, HANDLE toWait, gint32 ms)
{
- guint32 ret;
+ MonoW32HandleWaitRet ret;
MonoInternalThread *thread = mono_thread_internal_current ();
if (ms == -1)
- ms = INFINITE;
+ ms = MONO_INFINITE_WAIT;
if (mono_thread_current_check_pending_interrupt ())
- return map_native_wait_result_to_managed (WAIT_FAILED);
+ return map_native_wait_result_to_managed (MONO_W32HANDLE_WAIT_RET_FAILED, 0);
mono_thread_set_state (thread, ThreadState_WaitSleepJoin);
MONO_ENTER_GC_SAFE;
- ret = SignalObjectAndWait (toSignal, toWait, ms, TRUE);
+#ifdef HOST_WIN32
+ ret = mono_w32handle_convert_wait_ret (SignalObjectAndWait (toSignal, toWait, ms, TRUE), 1);
+#else
+ ret = mono_w32handle_signal_and_wait (toSignal, toWait, ms, TRUE);
+#endif
MONO_EXIT_GC_SAFE;
mono_thread_clr_state (thread, ThreadState_WaitSleepJoin);
- return map_native_wait_result_to_managed (ret);
+ return map_native_wait_result_to_managed (ret, 1);
}
gint32 ves_icall_System_Threading_Interlocked_Increment_Int (gint32 *location)
mono_set_pending_exception (mono_get_exception_thread_state (msg));
return;
}
+
+ mono_get_eh_callbacks ()->mono_clear_abort_threshold ();
thread->abort_exc = NULL;
if (thread->abort_state_handle) {
mono_gchandle_free (thread->abort_state_handle);
thread->state &= ~ThreadState_AbortRequested;
if (thread->abort_exc) {
+ mono_get_eh_callbacks ()->mono_clear_abort_threshold ();
thread->abort_exc = NULL;
if (thread->abort_state_handle) {
mono_gchandle_free (thread->abort_state_handle);
}
thread->state |= ThreadState_SuspendRequested;
- if (mono_threads_is_coop_enabled ())
- mono_os_event_reset (thread->suspended);
+ mono_os_event_reset (thread->suspended);
if (thread == mono_thread_internal_current ()) {
/* calls UNLOCK_THREAD (thread) */
{
if ((thread->state & ThreadState_SuspendRequested) != 0) {
// MOSTLY_ASYNC_SAFE_PRINTF ("RESUME (1) thread %p\n", thread_get_tid (thread));
- if (mono_threads_is_coop_enabled ())
- mono_os_event_set (thread->suspended);
thread->state &= ~ThreadState_SuspendRequested;
+ mono_os_event_set (thread->suspended);
return TRUE;
}
// MOSTLY_ASYNC_SAFE_PRINTF ("RESUME (3) thread %p\n", thread_get_tid (thread));
- if (mono_threads_is_coop_enabled ()) {
- mono_os_event_set (thread->suspended);
- } else {
+ mono_os_event_set (thread->suspended);
+
+ if (!thread->self_suspended) {
UNLOCK_THREAD (thread);
/* Awake the thread */
mono_profiler_context_unloaded (ctx);
}
-void
-mono_thread_init_tls (void)
-{
- MONO_FAST_TLS_INIT (tls_current_object);
- mono_native_tls_alloc (¤t_object_key, NULL);
-}
-
void mono_thread_init (MonoThreadStartCB start_cb,
MonoThreadAttachCB attach_cb)
{
mono_os_mutex_init_recursive(&interlocked_mutex);
mono_os_mutex_init_recursive(&joinable_threads_mutex);
- mono_os_event_init (&background_change_event, TRUE, FALSE);
+ mono_os_event_init (&background_change_event, FALSE);
mono_init_static_data_info (&thread_static_info);
mono_init_static_data_info (&context_static_info);
- THREAD_DEBUG (g_message ("%s: Allocated current_object_key %d", __func__, current_object_key));
-
mono_thread_start_cb = start_cb;
mono_thread_attach_cb = attach_cb;
}
mono_os_mutex_destroy (&small_id_mutex);
mono_os_event_destroy (&background_change_event);
#endif
-
- mono_native_tls_free (current_object_key);
}
void
};
static void
-wait_for_tids (struct wait_data *wait, guint32 timeout)
-{
- guint32 i;
- MonoThreadInfoWaitRet ret;
-
- THREAD_DEBUG (g_message("%s: %d threads to wait for in this batch", __func__, wait->num));
-
- MONO_ENTER_GC_SAFE;
- ret = mono_thread_info_wait_multiple_handle(wait->handles, wait->num, NULL, TRUE, timeout, TRUE);
- MONO_EXIT_GC_SAFE;
-
- if(ret==MONO_THREAD_INFO_WAIT_RET_FAILED) {
- /* See the comment in build_wait_tids() */
- THREAD_DEBUG (g_message ("%s: Wait failed", __func__));
- return;
- }
-
- for(i=0; i<wait->num; i++)
- mono_threads_close_thread_handle (wait->handles [i]);
-
- if (ret == MONO_THREAD_INFO_WAIT_RET_TIMEOUT)
- return;
-
- for(i=0; i<wait->num; i++) {
- MonoInternalThread *internal;
-
- internal = wait->threads [i];
-
- mono_threads_lock ();
- if (mono_g_hash_table_lookup (threads, (gpointer) internal->tid) == internal)
- g_error ("%s: failed to call mono_thread_detach_internal on thread %p, InternalThread: %p", __func__, internal->tid, internal);
- mono_threads_unlock ();
- }
-}
-
-static void wait_for_tids_or_state_change (struct wait_data *wait, guint32 timeout)
+wait_for_tids (struct wait_data *wait, guint32 timeout, gboolean check_state_change)
{
guint32 i;
MonoThreadInfoWaitRet ret;
* up if a thread changes to background mode. */
MONO_ENTER_GC_SAFE;
- ret = mono_thread_info_wait_multiple_handle (wait->handles, wait->num, &background_change_event, FALSE, timeout, TRUE);
+ if (check_state_change)
+ ret = mono_thread_info_wait_multiple_handle (wait->handles, wait->num, &background_change_event, FALSE, timeout, TRUE);
+ else
+ ret = mono_thread_info_wait_multiple_handle (wait->handles, wait->num, NULL, TRUE, timeout, TRUE);
MONO_EXIT_GC_SAFE;
- if(ret==MONO_THREAD_INFO_WAIT_RET_FAILED) {
+ if (ret == MONO_THREAD_INFO_WAIT_RET_FAILED) {
/* See the comment in build_wait_tids() */
THREAD_DEBUG (g_message ("%s: Wait failed", __func__));
return;
}
- for(i=0; i<wait->num; i++)
+ for( i = 0; i < wait->num; i++)
mono_threads_close_thread_handle (wait->handles [i]);
if (ret == MONO_THREAD_INFO_WAIT_RET_TIMEOUT)
if (wait->num >= MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS)
return FALSE;
- /* The finalizer thread is not a background thread */
- if (!mono_native_thread_id_equals (thread_get_tid (thread), self)
- && (thread->state & ThreadState_Background) != 0
- && (thread->flags & MONO_THREAD_FLAG_DONT_MANAGE) == 0
- ) {
+ if (mono_native_thread_id_equals (thread_get_tid (thread), self))
+ return FALSE;
+ if (mono_gc_is_finalizer_internal_thread (thread))
+ return FALSE;
+
+ if ((thread->state & ThreadState_Background) && !(thread->flags & MONO_THREAD_FLAG_DONT_MANAGE)) {
wait->handles[wait->num] = mono_threads_open_thread_handle (thread->handle);
wait->threads[wait->num] = thread;
wait->num++;
THREAD_DEBUG (g_print ("%s: Aborting id: %"G_GSIZE_FORMAT"\n", __func__, (gsize)thread->tid));
mono_thread_internal_abort (thread);
- return TRUE;
}
- return !mono_native_thread_id_equals (thread_get_tid (thread), self)
- && !mono_gc_is_finalizer_internal_thread (thread);
+ return TRUE;
}
/**
mono_os_event_reset (&background_change_event);
wait->num=0;
- /*We must zero all InternalThread pointers to avoid making the GC unhappy.*/
+ /* We must zero all InternalThread pointers to avoid making the GC unhappy. */
memset (wait->threads, 0, MONO_W32HANDLE_MAXIMUM_WAIT_OBJECTS * SIZEOF_VOID_P);
mono_g_hash_table_foreach (threads, build_wait_tids, wait);
mono_threads_unlock ();
- if(wait->num>0) {
+ if (wait->num > 0)
/* Something to wait for */
- wait_for_tids_or_state_change (wait, INFINITE);
- }
+ wait_for_tids (wait, MONO_INFINITE_WAIT, TRUE);
THREAD_DEBUG (g_message ("%s: I have %d threads after waiting.", __func__, wait->num));
} while(wait->num>0);
mono_threads_unlock ();
THREAD_DEBUG (g_message ("%s: wait->num is now %d", __func__, wait->num));
- if(wait->num>0) {
+ if (wait->num > 0) {
/* Something to wait for */
- wait_for_tids (wait, INFINITE);
+ wait_for_tids (wait, MONO_INFINITE_WAIT, FALSE);
}
} while (wait->num > 0);
|| mono_gc_is_finalizer_internal_thread (thread)
|| (thread->flags & MONO_THREAD_FLAG_DONT_MANAGE)
) {
- //mono_threads_close_thread_handle (wait->handles [i]);
- wait->threads [i] = NULL; /* ignore this thread in next loop */
+ mono_threads_close_thread_handle (wait->handles [i]);
+ wait->threads [i] = NULL;
continue;
}
(thread->state & ThreadState_Stopped) != 0) {
UNLOCK_THREAD (thread);
mono_threads_close_thread_handle (wait->handles [i]);
- wait->threads [i] = NULL; /* ignore this thread in next loop */
+ wait->threads [i] = NULL;
continue;
}
thread->state &= ~ThreadState_AbortRequested;
thread->state |= ThreadState_SuspendRequested;
- if (mono_threads_is_coop_enabled ())
- mono_os_event_reset (thread->suspended);
+ mono_os_event_reset (thread->suspended);
/* Signal the thread to suspend + calls UNLOCK_THREAD (thread) */
async_suspend_internal (thread, TRUE);
+
+ mono_threads_close_thread_handle (wait->handles [i]);
+ wait->threads [i] = NULL;
}
if (eventidx <= 0) {
/*
MonoDebugSourceLocation *location;
int tindex, nthreads;
- mono_error_init (error);
+ error_init (error);
*out_threads = NULL;
*out_stack_frames = NULL;
* We should wait for the threads either to abort, or to leave the
* domain. We can't do the latter, so we wait with a timeout.
*/
- wait_for_tids (&user_data.wait, 100);
+ wait_for_tids (&user_data.wait, 100, FALSE);
}
/* Update remaining time */
{
MonoInternalThread *thread = mono_thread_internal_current ();
- if (thread && thread->abort_exc && !is_running_protected_wrapper ()) {
- /*
- * FIXME: Clear the abort exception and return an AppDomainUnloaded
- * exception if the thread no longer references a dying appdomain.
- */
- thread->abort_exc->trace_ips = NULL;
- thread->abort_exc->stack_trace = NULL;
- return thread->abort_exc;
- }
+ if (!(thread && thread->abort_exc && !is_running_protected_wrapper ()))
+ return NULL;
- return NULL;
+ // We don't want to have our exception effect calls made by
+ // the catching block
+
+ if (!mono_get_eh_callbacks ()->mono_above_abort_threshold ())
+ return NULL;
+
+ /*
+ * FIXME: Clear the abort exception and return an AppDomainUnloaded
+ * exception if the thread no longer references a dying appdomain.
+ */
+ thread->abort_exc->trace_ips = NULL;
+ thread->abort_exc->stack_trace = NULL;
+ return thread->abort_exc;
}
#if MONO_SMALL_CONFIG
LOCK_THREAD (thread);
/* MonoThread::interruption_requested can only be changed with atomics */
- if (InterlockedCompareExchange (&thread->interruption_requested, FALSE, TRUE)) {
+ if (mono_thread_clear_interruption_requested (thread)) {
/* this will consume pending APC calls */
#ifdef HOST_WIN32
WaitForSingleObjectEx (GetCurrentThread(), 0, TRUE);
thread->state & ThreadState_Background)
ExitThread (1);
#endif
- if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 1)
+ if (!mono_thread_set_interruption_requested (thread))
return NULL;
InterlockedIncrement (&thread_interruption_requested);
if (!still_aborting)
return FALSE;
- if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 1)
+ if (!mono_thread_set_interruption_requested (thread))
return NULL;
InterlockedIncrement (&thread_interruption_requested);
MonoInternalThread *thread = mono_thread_internal_current ();
/* The thread may already be stopping */
if (thread != NULL)
- return (thread->interruption_requested);
+ return mono_thread_get_interruption_requested (thread);
}
return FALSE;
}
MonoInternalThread *thread = mono_thread_internal_current ();
/* The thread may already be stopping */
- if (thread == NULL)
+ if (!thread)
+ return NULL;
+ if (!mono_thread_get_interruption_requested (thread))
+ return NULL;
+ if (!bypass_abort_protection && is_running_protected_wrapper ())
return NULL;
- if (thread->interruption_requested && (bypass_abort_protection || !is_running_protected_wrapper ())) {
- MonoException* exc = mono_thread_execute_interruption ();
- if (exc)
- return exc;
- }
- return NULL;
+ return mono_thread_execute_interruption ();
}
/*
return ret;
}
-static gboolean has_tls_get = FALSE;
-
-void
-mono_runtime_set_has_tls_get (gboolean val)
-{
- has_tls_get = val;
-}
-
-gboolean
-mono_runtime_has_tls_get (void)
-{
- return has_tls_get;
-}
-
static void
self_interrupt_thread (void *_unused)
{
The target thread is running at least one protected block, which must not be interrupted, so we give up.
The protected block code will give them a chance when appropriate.
*/
- if (thread->abort_protected_block_count)
+ if (mono_thread_get_abort_prot_block_count (thread) > 0)
return MonoResumeThread;
/*someone is already interrupting it*/
- if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 1)
+ if (!mono_thread_set_interruption_requested (thread))
return MonoResumeThread;
InterlockedIncrement (&thread_interruption_requested);
{
MonoException *exc;
- mono_error_init (error);
+ error_init (error);
/* FIXME this is insanely broken, it doesn't cause interruption to happen synchronously
* since passing FALSE to mono_thread_request_interruption makes sure it returns NULL */
return KeepSuspended;
}
} else {
- if (InterlockedCompareExchange (&thread->interruption_requested, 1, 0) == 0)
+ if (mono_thread_set_interruption_requested (thread))
InterlockedIncrement (&thread_interruption_requested);
if (data->interrupt)
data->interrupt_token = mono_thread_info_prepare_interrupt ((MonoThreadInfo *)thread->thread_info);
// MOSTLY_ASYNC_SAFE_PRINTF ("ASYNC SUSPEND thread %p\n", thread_get_tid (thread));
+ thread->self_suspended = FALSE;
+
data.thread = thread;
data.interrupt = interrupt;
data.interrupt_token = NULL;
self_suspend_internal (void)
{
MonoInternalThread *thread;
+ MonoOSEvent *event;
+ MonoOSEventWaitRet res;
thread = mono_thread_internal_current ();
// MOSTLY_ASYNC_SAFE_PRINTF ("SELF SUSPEND thread %p\n", thread_get_tid (thread));
- if (!mono_threads_is_coop_enabled ())
- mono_thread_info_begin_self_suspend ();
+ thread->self_suspended = TRUE;
thread->state &= ~ThreadState_SuspendRequested;
thread->state |= ThreadState_Suspended;
UNLOCK_THREAD (thread);
- if (!mono_threads_is_coop_enabled ())
- mono_thread_info_end_self_suspend ();
- else {
- MonoOSEvent *event;
- MonoOSEventWaitRet res;
+ event = thread->suspended;
- event = thread->suspended;
+ MONO_ENTER_GC_SAFE;
+ res = mono_os_event_wait_one (event, MONO_INFINITE_WAIT, TRUE);
+ g_assert (res == MONO_OS_EVENT_WAIT_RET_SUCCESS_0 || res == MONO_OS_EVENT_WAIT_RET_ALERTED);
+ MONO_EXIT_GC_SAFE;
+}
- MONO_ENTER_GC_SAFE;
- res = mono_os_event_wait_one (event, MONO_INFINITE_WAIT);
- g_assert (res == MONO_OS_EVENT_WAIT_RET_SUCCESS_0);
- MONO_EXIT_GC_SAFE;
- }
+static void
+suspend_for_shutdown_async_call (gpointer unused)
+{
+ for (;;)
+ mono_thread_info_yield ();
+}
+
+static SuspendThreadResult
+suspend_for_shutdown_critical (MonoThreadInfo *info, gpointer unused)
+{
+ mono_thread_info_setup_async_call (info, suspend_for_shutdown_async_call, NULL);
+ return MonoResumeThread;
+}
+
+void
+mono_thread_internal_suspend_for_shutdown (MonoInternalThread *thread)
+{
+ g_assert (thread != mono_thread_internal_current ());
+
+ mono_thread_info_safe_suspend_and_run (thread_get_tid (thread), FALSE, suspend_for_shutdown_critical, NULL);
}
/*
if (thread != pthread_self ()) {
MONO_ENTER_GC_SAFE;
/* This shouldn't block */
+ mono_threads_join_lock ();
mono_native_thread_join (thread);
+ mono_threads_join_unlock ();
MONO_EXIT_GC_SAFE;
}
} else {
}
}
-void
-mono_threads_begin_abort_protected_block (void)
-{
- MonoInternalThread *thread;
-
- thread = mono_thread_internal_current ();
- ++thread->abort_protected_block_count;
- mono_memory_barrier ();
-}
-
-void
-mono_threads_end_abort_protected_block (void)
-{
- MonoInternalThread *thread;
-
- thread = mono_thread_internal_current ();
-
- mono_memory_barrier ();
- --thread->abort_protected_block_count;
-}
-
MonoException*
mono_thread_try_resume_interruption (void)
{
MonoInternalThread *thread;
thread = mono_thread_internal_current ();
- if (thread->abort_protected_block_count || mono_get_eh_callbacks ()->mono_current_thread_has_handle_block_guard ())
+ if (!mono_get_eh_callbacks ()->mono_above_abort_threshold ())
+ return NULL;
+ if (mono_thread_get_abort_prot_block_count (thread) > 0 || mono_get_eh_callbacks ()->mono_current_thread_has_handle_block_guard ())
return NULL;
return mono_thread_resume_interruption ();
return FALSE;
}
- if (thread->abort_protected_block_count || mono_get_eh_callbacks ()->mono_current_thread_has_handle_block_guard ()) {
+ if (mono_thread_get_abort_prot_block_count (thread) || mono_get_eh_callbacks ()->mono_current_thread_has_handle_block_guard ()) {
UNLOCK_THREAD (thread);
return FALSE;
}