#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 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)
enum {
INTERRUPT_REQUESTED_BIT = 0x1,
- ABORT_PROT_BLOCK_SHIFT = 1,
+ 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)
};
return (state & ABORT_PROT_BLOCK_MASK) >> ABORT_PROT_BLOCK_SHIFT;
}
+static void
+verify_thread_state (gsize state)
+{
+ //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));
+
+ //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);
gsize old_state, new_state;
do {
old_state = thread->thread_state;
+ verify_thread_state (old_state);
+
//Already cleared
- if (!(old_state & INTERRUPT_REQUESTED_BIT))
+ if (!(old_state & (INTERRUPT_REQUESTED_BIT | INTERRUPT_REQUEST_DEFERRED_BIT)))
return FALSE;
- new_state = old_state & ~INTERRUPT_REQUESTED_BIT;
+ 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;
}
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)
+ if (old_state & (INTERRUPT_REQUESTED_BIT | INTERRUPT_REQUEST_DEFERRED_BIT))
return FALSE;
- new_state = old_state | INTERRUPT_REQUESTED_BIT;
+
+ //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 TRUE;
+
+ return (new_state & INTERRUPT_REQUESTED_BIT) == INTERRUPT_REQUESTED_BIT;
}
static inline MonoNativeThreadId
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");
}
*/
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 ();
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;
gint32 diff_ms;
gint32 wait = ms;
- mono_error_init (error);
+ error_init (error);
start = (ms == -1) ? 0 : mono_msec_ticks ();
for (;;) {
gint32 diff_ms;
gint32 wait = ms;
- mono_error_init (error);
+ error_init (error);
start = (ms == -1) ? 0 : mono_100ns_ticks ();
do {
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;
}
/**
MonoDebugSourceLocation *location;
int tindex, nthreads;
- mono_error_init (error);
+ error_init (error);
*out_threads = NULL;
*out_stack_frames = NULL;
{
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 */
event = thread->suspended;
MONO_ENTER_GC_SAFE;
- res = mono_os_event_wait_one (event, MONO_INFINITE_WAIT);
+ 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;
}
+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);
+}
+
/*
* mono_thread_is_foreign:
* @thread: the thread to query
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 ();
- mono_thread_inc_abort_prot_block_count (thread);
- mono_memory_barrier ();
-}
-
-void
-mono_threads_end_abort_protected_block (void)
-{
- MonoInternalThread *thread;
-
- thread = mono_thread_internal_current ();
-
- mono_memory_barrier ();
- mono_thread_dec_abort_prot_block_count (thread);
-}
-
MonoException*
mono_thread_try_resume_interruption (void)
{
MonoInternalThread *thread;
thread = mono_thread_internal_current ();
+ 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;