#include <mono/metadata/domain-internals.h>
#include <mono/metadata/profiler-private.h>
#include <mono/metadata/threads.h>
-#include <mono/metadata/threadpool.h>
#include <mono/metadata/threads-types.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/environment.h>
typedef union {
struct {
+#if G_BYTE_ORDER != G_LITTLE_ENDIAN
+ guint32 type : 1;
+ guint32 offset : 25;
+ guint32 index : 6;
+#else
guint32 index : 6;
guint32 offset : 25;
guint32 type : 1;
+#endif
} fields;
guint32 raw;
} SpecialStaticOffset;
mono_g_hash_table_remove (thread_start_args, start_info->obj);
mono_threads_unlock ();
- mono_thread_set_execution_context (start_info->obj->ec_to_set);
- start_info->obj->ec_to_set = NULL;
-
g_free (start_info);
THREAD_DEBUG (g_message ("%s: start_wrapper for %"G_GSIZE_FORMAT, __func__,
internal->tid));
}
int
-ves_icall_System_Threading_Thread_GetPriority (MonoInternalThread *thread)
+ves_icall_System_Threading_Thread_GetPriority (MonoThread *this)
{
return ThreadPriority_Lowest;
}
void
-ves_icall_System_Threading_Thread_SetPriority (MonoInternalThread *thread, int priority)
+ves_icall_System_Threading_Thread_SetPriority (MonoThread *this, int priority)
{
}
}
gboolean
-ves_icall_System_Threading_Thread_Join_internal(MonoInternalThread *this,
- int ms, HANDLE thread)
+ves_icall_System_Threading_Thread_Join_internal(MonoThread *this, int ms)
{
+ MonoInternalThread *this_obj = this->internal_thread;
+ HANDLE thread = this_obj->handle;
MonoInternalThread *cur_thread = mono_thread_internal_current ();
gboolean ret;
mono_thread_current_check_pending_interrupt ();
- LOCK_THREAD (this);
+ LOCK_THREAD (this_obj);
- if ((this->state & ThreadState_Unstarted) != 0) {
- UNLOCK_THREAD (this);
+ if ((this_obj->state & ThreadState_Unstarted) != 0) {
+ UNLOCK_THREAD (this_obj);
mono_set_pending_exception (mono_get_exception_thread_state ("Thread has not been started."));
return FALSE;
}
- UNLOCK_THREAD (this);
+ UNLOCK_THREAD (this_obj);
if(ms== -1) {
ms=INFINITE;
return state;
}
-void ves_icall_System_Threading_Thread_Interrupt_internal (MonoInternalThread *this)
+void ves_icall_System_Threading_Thread_Interrupt_internal (MonoThread *this)
{
MonoInternalThread *current;
gboolean throw;
+ MonoInternalThread *this_obj = this->internal_thread;
- LOCK_THREAD (this);
+ LOCK_THREAD (this_obj);
current = mono_thread_internal_current ();
- this->thread_interrupt_requested = TRUE;
- throw = current != this && (this->state & ThreadState_WaitSleepJoin);
+ this_obj->thread_interrupt_requested = TRUE;
+ throw = current != this_obj && (this_obj->state & ThreadState_WaitSleepJoin);
- UNLOCK_THREAD (this);
+ UNLOCK_THREAD (this_obj);
if (throw) {
- abort_thread_internal (this, TRUE, FALSE);
+ abort_thread_internal (this_obj, TRUE, FALSE);
}
}
}
void
-ves_icall_System_Threading_Thread_ResetAbort (void)
+ves_icall_System_Threading_Thread_ResetAbort (MonoThread *this)
{
MonoInternalThread *thread = mono_thread_internal_current ();
gboolean was_aborting;
}
void
-ves_icall_System_Threading_Thread_Suspend (MonoInternalThread *thread)
+ves_icall_System_Threading_Thread_Suspend (MonoThread *this)
{
- if (!mono_thread_suspend (thread)) {
+ if (!mono_thread_suspend (this->internal_thread)) {
mono_set_pending_exception (mono_get_exception_thread_state ("Thread has not been started, or is dead."));
return;
}
g_hash_table_insert (contexts, gch, gch);
mono_threads_unlock ();
+
+ mono_profiler_context_loaded (ctx);
+}
+
+void
+ves_icall_System_Runtime_Remoting_Contexts_Context_ReleaseContext (MonoAppContext *ctx)
+{
+ /*
+ * NOTE: Since finalizers are unreliable for the purposes of ensuring
+ * cleanup in exceptional circumstances, we don't actually do any
+ * cleanup work here. We instead do this when we iterate the `contexts`
+ * hash table. The only purpose of this finalizer, at the moment, is to
+ * notify the profiler.
+ */
+
+ //g_print ("Releasing context %d in domain %d\n", ctx->context_id, ctx->domain_id);
+
+ mono_profiler_context_unloaded (ctx);
}
void
void **p = ptr + idx;
if (*p)
- mark_func (p, gc_data);
+ mark_func ((MonoObject**)p, gc_data);
});
}
}
gpointer* static_data = *static_data_ptr;
if (!static_data) {
- static void *tls_desc = NULL;
- static void *ctx_desc = NULL;
+ static MonoGCDescriptor tls_desc = MONO_GC_DESCRIPTOR_NULL;
+ static MonoGCDescriptor ctx_desc = MONO_GC_DESCRIPTOR_NULL;
if (mono_gc_user_markers_supported ()) {
- if (!tls_desc)
+ if (tls_desc == MONO_GC_DESCRIPTOR_NULL)
tls_desc = mono_gc_make_root_descr_user (mark_tls_slots);
- if (!ctx_desc)
+ if (ctx_desc == MONO_GC_DESCRIPTOR_NULL)
ctx_desc = mono_gc_make_root_descr_user (mark_ctx_slots);
}
if (mono_gc_user_markers_supported ())
static_data [i] = g_malloc0 (static_data_size [i]);
else
- static_data [i] = mono_gc_alloc_fixed (static_data_size [i], NULL);
+ static_data [i] = mono_gc_alloc_fixed (static_data_size [i], MONO_GC_DESCRIPTOR_NULL);
}
}
mono_threads_unlock ();
}
+/*
+ * LOCKING: requires that threads_mutex is held
+ */
static void
context_adjust_static_data (MonoAppContext *ctx)
{
- mono_threads_lock ();
-
if (context_static_info.offset || context_static_info.idx > 0) {
guint32 offset = MAKE_SPECIAL_STATIC_OFFSET (context_static_info.idx, context_static_info.offset, 0);
mono_alloc_static_data (&ctx->static_data, offset, FALSE);
}
-
- mono_threads_unlock ();
}
/*
/*
* LOCKING: requires that threads_mutex is held
*/
-static void
+static gboolean
alloc_context_static_data_helper (gpointer key, gpointer value, gpointer user)
{
uint32_t gch = GPOINTER_TO_INT (key);
MonoAppContext *ctx = (MonoAppContext *) mono_gchandle_get_target (gch);
if (!ctx) {
- g_hash_table_remove (contexts, key);
mono_gchandle_free (gch);
- return;
+ return TRUE; // Remove this key/value pair
}
guint32 offset = GPOINTER_TO_UINT (user);
mono_alloc_static_data (&ctx->static_data, offset, FALSE);
+
+ return FALSE; // Don't remove it
}
static StaticDataFreeList*
mono_g_hash_table_foreach (threads, alloc_thread_static_data_helper, GUINT_TO_POINTER (offset));
} else {
if (contexts != NULL)
- g_hash_table_foreach (contexts, alloc_context_static_data_helper, GUINT_TO_POINTER (offset));
+ g_hash_table_foreach_remove (contexts, alloc_context_static_data_helper, GUINT_TO_POINTER (offset));
ACCESS_SPECIAL_STATIC_OFFSET (offset, type) = SPECIAL_STATIC_OFFSET_TYPE_CONTEXT;
}
/*
* LOCKING: requires that threads_mutex is held
*/
-static void
+static gboolean
free_context_static_data_helper (gpointer key, gpointer value, gpointer user)
{
uint32_t gch = GPOINTER_TO_INT (key);
MonoAppContext *ctx = (MonoAppContext *) mono_gchandle_get_target (gch);
if (!ctx) {
- g_hash_table_remove (contexts, key);
mono_gchandle_free (gch);
- return;
+ return TRUE; // Remove this key/value pair
}
OffsetSize *data = user;
char *ptr;
if (!ctx->static_data || !ctx->static_data [idx])
- return;
+ return FALSE; // Don't remove this key/value pair
ptr = ((char*) ctx->static_data [idx]) + off;
mono_gc_bzero_atomic (ptr, data->size);
+
+ return FALSE; // Don't remove this key/value pair
}
static void
mono_g_hash_table_foreach (threads, free_thread_static_data_helper, &data);
} else {
if (contexts != NULL)
- g_hash_table_foreach (contexts, free_context_static_data_helper, &data);
+ g_hash_table_foreach_remove (contexts, free_context_static_data_helper, &data);
}
if (!mono_runtime_is_shutting_down ()) {
mono_threads_unlock ();
}
-/*
- * allocates room in the thread local area for storing an instance of the struct type
- * the allocation is kept track of in domain->tlsrec_list.
- */
-uint32_t
-mono_thread_alloc_tls (MonoReflectionType *type)
-{
- MonoDomain *domain = mono_domain_get ();
- MonoClass *klass;
- MonoTlsDataRecord *tlsrec;
- int max_set = 0;
- gsize *bitmap;
- gsize default_bitmap [4] = {0};
- uint32_t tls_offset;
- guint32 size;
- gint32 align;
-
- klass = mono_class_from_mono_type (type->type);
- /* TlsDatum is a struct, so we subtract the object header size offset */
- bitmap = mono_class_compute_bitmap (klass, default_bitmap, sizeof (default_bitmap) * 8, - (int)(sizeof (MonoObject) / sizeof (gpointer)), &max_set, FALSE);
- size = mono_type_size (type->type, &align);
- tls_offset = mono_alloc_special_static_data (SPECIAL_STATIC_THREAD, size, align, (uintptr_t*)bitmap, max_set + 1);
- if (bitmap != default_bitmap)
- g_free (bitmap);
- tlsrec = g_new0 (MonoTlsDataRecord, 1);
- tlsrec->tls_offset = tls_offset;
- tlsrec->size = size;
- mono_domain_lock (domain);
- tlsrec->next = domain->tlsrec_list;
- domain->tlsrec_list = tlsrec;
- mono_domain_unlock (domain);
- return tls_offset;
-}
-
-static void
-destroy_tls (MonoDomain *domain, uint32_t tls_offset)
-{
- MonoTlsDataRecord *prev = NULL;
- MonoTlsDataRecord *cur;
- guint32 size = 0;
-
- mono_domain_lock (domain);
- cur = domain->tlsrec_list;
- while (cur) {
- if (cur->tls_offset == tls_offset) {
- if (prev)
- prev->next = cur->next;
- else
- domain->tlsrec_list = cur->next;
- size = cur->size;
- g_free (cur);
- break;
- }
- prev = cur;
- cur = cur->next;
- }
- mono_domain_unlock (domain);
- if (size)
- mono_special_static_data_free_slot (tls_offset, size);
-}
-
-void
-mono_thread_destroy_tls (uint32_t tls_offset)
-{
- destroy_tls (mono_domain_get (), tls_offset);
-}
-
-/*
- * This is just to ensure cleanup: the finalizers should have taken care, so this is not perf-critical.
- */
-void
-mono_thread_destroy_domain_tls (MonoDomain *domain)
-{
- while (domain->tlsrec_list)
- destroy_tls (domain, domain->tlsrec_list->tls_offset);
-}
-
-static MonoClassField *local_slots = NULL;
-
-typedef struct {
- /* local tls data to get locals_slot from a thread */
- guint32 offset;
- /* index in the locals_slot array */
- int slot;
-} LocalSlotID;
-
-static void
-clear_local_slot (gpointer key, gpointer value, gpointer user_data)
-{
- LocalSlotID *sid = user_data;
- MonoInternalThread *thread = (MonoInternalThread*)value;
- MonoArray *slots_array;
- /*
- * the static field is stored at: ((char*) thread->static_data [idx]) + (offset & 0xffffff);
- * it is for the right domain, so we need to check if it is allocated an initialized
- * for the current thread.
- */
- /*g_print ("handling thread %p\n", thread);*/
-
- int idx = ACCESS_SPECIAL_STATIC_OFFSET (sid->offset, index);
- int off = ACCESS_SPECIAL_STATIC_OFFSET (sid->offset, offset);
-
- if (!thread->static_data || !thread->static_data [idx])
- return;
- slots_array = *(MonoArray **)(((char*) thread->static_data [idx]) + off);
- if (!slots_array || sid->slot >= mono_array_length (slots_array))
- return;
- mono_array_set (slots_array, MonoObject*, sid->slot, NULL);
-}
-
-void
-mono_thread_free_local_slot_values (int slot, MonoBoolean thread_local)
-{
- MonoDomain *domain;
- LocalSlotID sid;
- sid.slot = slot;
- if (thread_local) {
- void *addr = NULL;
- if (!local_slots) {
- local_slots = mono_class_get_field_from_name (mono_defaults.thread_class, "local_slots");
- if (!local_slots) {
- g_warning ("local_slots field not found in Thread class");
- return;
- }
- }
- domain = mono_domain_get ();
- mono_domain_lock (domain);
- if (domain->special_static_fields)
- addr = g_hash_table_lookup (domain->special_static_fields, local_slots);
- mono_domain_unlock (domain);
- if (!addr)
- return;
- /*g_print ("freeing slot %d at %p\n", slot, addr);*/
- sid.offset = GPOINTER_TO_UINT (addr);
- mono_threads_lock ();
- mono_g_hash_table_foreach (threads, clear_local_slot, &sid);
- mono_threads_unlock ();
- } else {
- /* FIXME: clear the slot for MonoAppContexts, too */
- }
-}
-
#ifdef HOST_WIN32
static void CALLBACK dummy_apc (ULONG_PTR param)
{
return ret;
}
-//static MonoClassField *execution_context_field;
-
-static MonoObject**
-get_execution_context_addr (void)
-{
- MonoDomain *domain = mono_domain_get ();
- guint32 offset = domain->execution_context_field_offset;
-
- if (!offset) {
- MonoClassField *field = mono_class_get_field_from_name (mono_defaults.thread_class, "_ec");
- g_assert (field);
-
- g_assert (mono_class_try_get_vtable (domain, mono_defaults.appdomain_class));
-
- mono_domain_lock (domain);
- offset = GPOINTER_TO_UINT (g_hash_table_lookup (domain->special_static_fields, field));
- mono_domain_unlock (domain);
- g_assert (offset);
-
- domain->execution_context_field_offset = offset;
- }
-
- return (MonoObject**) mono_get_special_static_data (offset);
-}
-
-MonoObject*
-mono_thread_get_execution_context (void)
-{
- return *get_execution_context_addr ();
-}
-
-void
-mono_thread_set_execution_context (MonoObject *ec)
-{
- *get_execution_context_addr () = ec;
-}
-
static gboolean has_tls_get = FALSE;
void
pthread_join (thread, NULL);
#endif
}
+
+void
+mono_thread_internal_check_for_interruption_critical (MonoInternalThread *thread)
+{
+ if ((thread->state & (ThreadState_StopRequested | ThreadState_SuspendRequested)) != 0)
+ mono_thread_interruption_checkpoint ();
+}
+
+static inline gboolean
+is_appdomainunloaded_exception (MonoClass *klass)
+{
+ static MonoClass *app_domain_unloaded_exception_klass = NULL;
+
+ if (!app_domain_unloaded_exception_klass)
+ app_domain_unloaded_exception_klass = mono_class_from_name (mono_defaults.corlib, "System", "AppDomainUnloadedException");
+ g_assert (app_domain_unloaded_exception_klass);
+
+ return klass == app_domain_unloaded_exception_klass;
+}
+
+static inline gboolean
+is_threadabort_exception (MonoClass *klass)
+{
+ return klass == mono_defaults.threadabortexception_class;
+}
+
+void
+mono_thread_internal_unhandled_exception (MonoObject* exc)
+{
+ if (mono_runtime_unhandled_exception_policy_get () == MONO_UNHANDLED_POLICY_CURRENT) {
+ MonoClass *klass = exc->vtable->klass;
+ if (is_threadabort_exception (klass)) {
+ mono_thread_internal_reset_abort (mono_thread_internal_current ());
+ } else if (!is_appdomainunloaded_exception (klass)) {
+ mono_unhandled_exception (exc);
+ if (mono_environment_exitcode_get () == 1)
+ exit (255);
+ }
+ }
+}