[threads] Rework MonoThreadInfoCallbacks.thread_{register,detach,unregister} callback...
[mono.git] / mono / metadata / sgen-mono.c
index 0314150604f207eb9bd6c48a6232521398abd376..508a9c68e7a9d286d896eba003c39c4f8ff09c3b 100644 (file)
@@ -72,7 +72,7 @@ ptr_on_stack (void *ptr)
        gpointer stack_start = &stack_start;
        SgenThreadInfo *info = mono_thread_info_current ();
 
-       if (ptr >= stack_start && ptr < (gpointer)info->client_info.stack_end)
+       if (ptr >= stack_start && ptr < (gpointer)info->client_info.info.stack_end)
                return TRUE;
        return FALSE;
 }
@@ -2209,11 +2209,8 @@ mono_gc_get_gc_callbacks ()
 }
 
 void
-sgen_client_thread_register (SgenThreadInfo* info, void *stack_bottom_fallback)
+sgen_client_thread_attach (SgenThreadInfo* info)
 {
-       size_t stsize = 0;
-       guint8 *staddr = NULL;
-
        mono_tls_set_sgen_thread_info (info);
 
        info->client_info.skip = 0;
@@ -2225,17 +2222,6 @@ sgen_client_thread_register (SgenThreadInfo* info, void *stack_bottom_fallback)
        info->client_info.signal = 0;
 #endif
 
-       mono_thread_info_get_stack_bounds (&staddr, &stsize);
-       if (staddr) {
-               info->client_info.stack_start_limit = staddr;
-               info->client_info.stack_end = staddr + stsize;
-       } else {
-               gsize stack_bottom = (gsize)stack_bottom_fallback;
-               stack_bottom += 4095;
-               stack_bottom &= ~4095;
-               info->client_info.stack_end = (char*)stack_bottom;
-       }
-
        memset (&info->client_info.ctx, 0, sizeof (MonoContext));
 
        if (mono_gc_get_gc_callbacks ()->thread_attach_func)
@@ -2243,13 +2229,13 @@ sgen_client_thread_register (SgenThreadInfo* info, void *stack_bottom_fallback)
 
        binary_protocol_thread_register ((gpointer)mono_thread_info_get_tid (info));
 
-       SGEN_LOG (3, "registered thread %p (%p) stack end %p", info, (gpointer)mono_thread_info_get_tid (info), info->client_info.stack_end);
+       SGEN_LOG (3, "registered thread %p (%p) stack end %p", info, (gpointer)mono_thread_info_get_tid (info), info->client_info.info.stack_end);
 
        info->client_info.info.handle_stack = mono_handle_stack_alloc ();
 }
 
 void
-sgen_client_thread_unregister (SgenThreadInfo *p)
+sgen_client_thread_detach_with_lock (SgenThreadInfo *p)
 {
        MonoNativeThreadId tid;
 
@@ -2297,13 +2283,6 @@ thread_in_critical_region (SgenThreadInfo *info)
        return info->client_info.in_critical_region;
 }
 
-static void
-sgen_thread_attach (SgenThreadInfo *info)
-{
-       if (mono_gc_get_gc_callbacks ()->thread_attach_func && !info->client_info.runtime_data)
-               info->client_info.runtime_data = mono_gc_get_gc_callbacks ()->thread_attach_func ();
-}
-
 static void
 sgen_thread_detach (SgenThreadInfo *p)
 {
@@ -2317,15 +2296,6 @@ sgen_thread_detach (SgenThreadInfo *p)
                mono_thread_detach_internal (mono_thread_internal_current ());
 }
 
-/**
- * mono_gc_register_thread:
- */
-gboolean
-mono_gc_register_thread (void *baseptr)
-{
-       return mono_thread_info_attach (baseptr) != NULL;
-}
-
 /**
  * mono_gc_is_gc_thread:
  */
@@ -2393,20 +2363,20 @@ sgen_client_scan_thread_data (void *start_nursery, void *end_nursery, gboolean p
                void *aligned_stack_start;
 
                if (info->client_info.skip) {
-                       SGEN_LOG (3, "Skipping dead thread %p, range: %p-%p, size: %zd", info, info->client_info.stack_start, info->client_info.stack_end, (char*)info->client_info.stack_end - (char*)info->client_info.stack_start);
+                       SGEN_LOG (3, "Skipping dead thread %p, range: %p-%p, size: %zd", info, info->client_info.stack_start, info->client_info.info.stack_end, (char*)info->client_info.info.stack_end - (char*)info->client_info.stack_start);
                        skip_reason = 1;
                } else if (info->client_info.gc_disabled) {
-                       SGEN_LOG (3, "GC disabled for thread %p, range: %p-%p, size: %zd", info, info->client_info.stack_start, info->client_info.stack_end, (char*)info->client_info.stack_end - (char*)info->client_info.stack_start);
+                       SGEN_LOG (3, "GC disabled for thread %p, range: %p-%p, size: %zd", info, info->client_info.stack_start, info->client_info.info.stack_end, (char*)info->client_info.info.stack_end - (char*)info->client_info.stack_start);
                        skip_reason = 2;
                } else if (!mono_thread_info_is_live (info)) {
-                       SGEN_LOG (3, "Skipping non-running thread %p, range: %p-%p, size: %zd (state %x)", info, info->client_info.stack_start, info->client_info.stack_end, (char*)info->client_info.stack_end - (char*)info->client_info.stack_start, info->client_info.info.thread_state);
+                       SGEN_LOG (3, "Skipping non-running thread %p, range: %p-%p, size: %zd (state %x)", info, info->client_info.stack_start, info->client_info.info.stack_end, (char*)info->client_info.info.stack_end - (char*)info->client_info.stack_start, info->client_info.info.thread_state);
                        skip_reason = 3;
                } else if (!info->client_info.stack_start) {
                        SGEN_LOG (3, "Skipping starting or detaching thread %p", info);
                        skip_reason = 4;
                }
 
-               binary_protocol_scan_stack ((gpointer)mono_thread_info_get_tid (info), info->client_info.stack_start, info->client_info.stack_end, skip_reason);
+               binary_protocol_scan_stack ((gpointer)mono_thread_info_get_tid (info), info->client_info.stack_start, info->client_info.info.stack_end, skip_reason);
 
                if (skip_reason) {
                        if (precise) {
@@ -2421,7 +2391,7 @@ sgen_client_scan_thread_data (void *start_nursery, void *end_nursery, gboolean p
                }
 
                g_assert (info->client_info.stack_start);
-               g_assert (info->client_info.stack_end);
+               g_assert (info->client_info.info.stack_end);
 
                aligned_stack_start = (void*)(mword) ALIGN_TO ((mword)info->client_info.stack_start, SIZEOF_VOID_P);
 #ifdef HOST_WIN32
@@ -2441,16 +2411,16 @@ sgen_client_scan_thread_data (void *start_nursery, void *end_nursery, gboolean p
 #endif
 
                g_assert (info->client_info.suspend_done);
-               SGEN_LOG (3, "Scanning thread %p, range: %p-%p, size: %zd, pinned=%zd", info, info->client_info.stack_start, info->client_info.stack_end, (char*)info->client_info.stack_end - (char*)info->client_info.stack_start, sgen_get_pinned_count ());
+               SGEN_LOG (3, "Scanning thread %p, range: %p-%p, size: %zd, pinned=%zd", info, info->client_info.stack_start, info->client_info.info.stack_end, (char*)info->client_info.info.stack_end - (char*)info->client_info.stack_start, sgen_get_pinned_count ());
                if (mono_gc_get_gc_callbacks ()->thread_mark_func && !conservative_stack_mark) {
-                       mono_gc_get_gc_callbacks ()->thread_mark_func (info->client_info.runtime_data, (guint8 *)aligned_stack_start, (guint8 *)info->client_info.stack_end, precise, &ctx);
+                       mono_gc_get_gc_callbacks ()->thread_mark_func (info->client_info.runtime_data, (guint8 *)aligned_stack_start, (guint8 *)info->client_info.info.stack_end, precise, &ctx);
                } else if (!precise) {
                        if (!conservative_stack_mark) {
                                fprintf (stderr, "Precise stack mark not supported - disabling.\n");
                                conservative_stack_mark = TRUE;
                        }
                        //FIXME we should eventually use the new stack_mark from coop
-                       sgen_conservatively_pin_objects_from ((void **)aligned_stack_start, (void **)info->client_info.stack_end, start_nursery, end_nursery, PIN_TYPE_STACK);
+                       sgen_conservatively_pin_objects_from ((void **)aligned_stack_start, (void **)info->client_info.info.stack_end, start_nursery, end_nursery, PIN_TYPE_STACK);
                }
 
                if (!precise) {
@@ -2460,7 +2430,7 @@ sgen_client_scan_thread_data (void *start_nursery, void *end_nursery, gboolean p
                        {
                                // This is used on Coop GC for platforms where we cannot get the data for individual registers.
                                // We force a spill of all registers into the stack and pass a chunk of data into sgen.
-                               //FIXME under coop, for now, what we need to ensure is that we scan any extra memory from info->client_info.stack_end to stack_mark
+                               //FIXME under coop, for now, what we need to ensure is that we scan any extra memory from info->client_info.info.stack_end to stack_mark
                                MonoThreadUnwindState *state = &info->client_info.info.thread_saved_state [SELF_SUSPEND_STATE_INDEX];
                                if (state && state->gc_stackdata) {
                                        sgen_conservatively_pin_objects_from ((void **)state->gc_stackdata, (void**)((char*)state->gc_stackdata + state->gc_stackdata_size),
@@ -2501,8 +2471,8 @@ mono_gc_set_stack_end (void *stack_end)
        LOCK_GC;
        info = mono_thread_info_current ();
        if (info) {
-               SGEN_ASSERT (0, stack_end < info->client_info.stack_end, "Can only lower stack end");
-               info->client_info.stack_end = stack_end;
+               SGEN_ASSERT (0, stack_end < info->client_info.info.stack_end, "Can only lower stack end");
+               info->client_info.info.stack_end = stack_end;
        }
        UNLOCK_GC;
 }
@@ -2887,13 +2857,11 @@ sgen_client_vtable_get_name (MonoVTable *vt)
 void
 sgen_client_init (void)
 {
-       int dummy;
        MonoThreadInfoCallbacks cb;
 
-       cb.thread_register = sgen_thread_register;
-       cb.thread_detach = sgen_thread_detach;
-       cb.thread_unregister = sgen_thread_unregister;
        cb.thread_attach = sgen_thread_attach;
+       cb.thread_detach = sgen_thread_detach;
+       cb.thread_detach_with_lock = sgen_thread_detach_with_lock;
        cb.mono_thread_in_critical_region = thread_in_critical_region;
        cb.ip_in_critical_region = ip_in_critical_region;
 
@@ -2909,7 +2877,7 @@ sgen_client_init (void)
 
        mono_tls_init_gc_keys ();
 
-       mono_gc_register_thread (&dummy);
+       mono_thread_info_attach ();
 }
 
 gboolean