#include <mono/metadata/monitor.h>
#include <mono/metadata/gc-internal.h>
#include <mono/metadata/marshal.h>
+#include <mono/metadata/runtime.h>
#include <mono/io-layer/io-layer.h>
#ifndef HOST_WIN32
#include <mono/io-layer/threads.h>
if (mono_thread_cleanup_fn)
mono_thread_cleanup_fn (thread);
- MONO_GC_UNREGISTER_ROOT (thread->thread_pinning_ref);
+ if (mono_gc_is_moving ()) {
+ MONO_GC_UNREGISTER_ROOT (thread->thread_pinning_ref);
+ thread->thread_pinning_ref = NULL;
+ }
+
}
static gpointer
mono_g_hash_table_insert (thread_start_args, thread, start_info->start_arg);
}
-MonoInternalThread* mono_thread_create_internal (MonoDomain *domain, gpointer func, gpointer arg, gboolean threadpool_thread, guint32 stack_size)
+/*
+ * mono_thread_create_internal:
+ *
+ * If NO_DETACH is TRUE, then the thread is not detached using pthread_detach (). This is needed to fix the race condition where waiting for a thred to exit only waits for its exit event to be
+ * signalled, which can cause shutdown crashes if the thread shutdown code accesses data already freed by the runtime shutdown.
+ * Currently, this is only used for the finalizer thread.
+ */
+MonoInternalThread*
+mono_thread_create_internal (MonoDomain *domain, gpointer func, gpointer arg, gboolean threadpool_thread, gboolean no_detach, guint32 stack_size)
{
MonoThread *thread;
MonoInternalThread *internal;
HANDLE thread_handle;
struct StartInfo *start_info;
gsize tid;
+ guint32 create_flags;
thread = create_thread_object (domain);
internal = create_internal_thread_object ();
/* Create suspended, so we can do some housekeeping before the thread
* starts
*/
+ create_flags = CREATE_SUSPENDED;
+#ifndef HOST_WIN32
+ if (no_detach)
+ create_flags |= CREATE_NO_DETACH;
+#endif
thread_handle = mono_create_thread (NULL, stack_size, (LPTHREAD_START_ROUTINE)start_wrapper, start_info,
- CREATE_SUSPENDED, &tid);
+ create_flags, &tid);
THREAD_DEBUG (g_message ("%s: Started thread ID %"G_GSIZE_FORMAT" (handle %p)", __func__, tid, thread_handle));
if (thread_handle == NULL) {
/* The thread couldn't be created, so throw an exception */
internal->handle=thread_handle;
internal->tid=tid;
internal->apartment_state=ThreadApartmentState_Unknown;
- internal->thread_pinning_ref = internal;
internal->managed_id = get_next_managed_thread_id ();
- MONO_GC_REGISTER_ROOT_PINNING (internal->thread_pinning_ref);
+ if (mono_gc_is_moving ()) {
+ internal->thread_pinning_ref = internal;
+ MONO_GC_REGISTER_ROOT_PINNING (internal->thread_pinning_ref);
+ }
internal->synch_cs = g_new0 (CRITICAL_SECTION, 1);
InitializeCriticalSection (internal->synch_cs);
void
mono_thread_create (MonoDomain *domain, gpointer func, gpointer arg)
{
- mono_thread_create_internal (domain, func, arg, FALSE, 0);
+ mono_thread_create_internal (domain, func, arg, FALSE, FALSE, 0);
}
/*
thread->android_tid = (gpointer) gettid ();
#endif
thread->apartment_state=ThreadApartmentState_Unknown;
- thread->thread_pinning_ref = thread;
thread->managed_id = get_next_managed_thread_id ();
- MONO_GC_REGISTER_ROOT_PINNING (thread->thread_pinning_ref);
+ if (mono_gc_is_moving ()) {
+ thread->thread_pinning_ref = thread;
+ MONO_GC_REGISTER_ROOT_PINNING (thread->thread_pinning_ref);
+ }
thread->stack_ptr = &tid;
internal->handle=thread;
internal->tid=tid;
- internal->thread_pinning_ref = internal;
- MONO_GC_REGISTER_ROOT_PINNING (internal->thread_pinning_ref);
+ if (mono_gc_is_moving ()) {
+ internal->thread_pinning_ref = internal;
+ MONO_GC_REGISTER_ROOT_PINNING (internal->thread_pinning_ref);
+ }
/* Don't call handle_store() here, delay it to Start.
{
#ifdef HOST_WIN32
return -1;
-#else
-#ifndef SIGRTMIN
+#elif defined(PLATFORM_ANDROID)
+ return SIGUNUSED;
+#elif !defined (SIGRTMIN)
#ifdef SIGUSR1
return SIGUSR1;
#else
return -1;
-#endif
+#endif /* SIGUSR1 */
#else
static int abort_signum = -1;
int i;
}
/* fallback to the old way */
return SIGRTMIN;
-#endif
#endif /* HOST_WIN32 */
}
return *((volatile float *) (ptr));
}
+MonoObject*
+ves_icall_System_Threading_Volatile_Read_T (void *ptr)
+{
+ return (MonoObject*)*((volatile MonoObject**)ptr);
+}
+
void
ves_icall_System_Threading_Thread_VolatileWrite1 (void *ptr, gint8 value)
{
*((volatile float *) ptr) = value;
}
+void
+ves_icall_System_Threading_Volatile_Write_T (void *ptr, MonoObject *value)
+{
+ *((volatile MonoObject **) ptr) = value;
+ mono_gc_wbarrier_generic_nostore (ptr);
+}
+
void mono_thread_init (MonoThreadStartCB start_cb,
MonoThreadAttachCB attach_cb)
{
THREAD_DEBUG (g_message ("%s: I have %d threads after waiting.", __func__, wait->num));
} while(wait->num>0);
+#ifndef MONO_CROSS_COMPILE
+ mono_runtime_shutdown ();
+#endif
+
mono_threads_set_shutting_down ();
/* No new threads will be created after this point */
mono_thread_info_get_last_managed (MonoThreadInfo *info)
{
MonoJitInfo *ji = NULL;
+ if (!info)
+ return NULL;
mono_get_eh_callbacks ()->mono_walk_stack_with_state (last_managed, &info->suspend_state, MONO_UNWIND_SIGNAL_SAFE, &ji);
return ji;
}
transition_to_suspended (thread);
mono_thread_info_self_suspend ();
} else {
- MonoThreadInfo *info = mono_thread_info_safe_suspend_sync ((MonoNativeThreadId)(gsize)thread->tid, interrupt);
- MonoJitInfo *ji = mono_thread_info_get_last_managed (info);
- gboolean protected_wrapper = ji && mono_threads_is_critical_method (ji->method);
- gboolean running_managed = mono_jit_info_match (ji, MONO_CONTEXT_GET_IP (&info->suspend_state.ctx));
+ MonoThreadInfo *info;
+ MonoJitInfo *ji;
+ gboolean protected_wrapper;
+ gboolean running_managed;
+
+ /*A null info usually means the thread is already dead. */
+ if (!(info = mono_thread_info_safe_suspend_sync ((MonoNativeThreadId)(gsize)thread->tid, interrupt))) {
+ LeaveCriticalSection (thread->synch_cs);
+ return;
+ }
+
+ ji = mono_thread_info_get_last_managed (info);
+ protected_wrapper = ji && mono_threads_is_critical_method (ji->method);
+ running_managed = mono_jit_info_match (ji, MONO_CONTEXT_GET_IP (&info->suspend_state.ctx));
if (running_managed && !protected_wrapper) {
transition_to_suspended (thread);