Merge pull request #1708 from alexanderkyte/always_use_imt
[mono.git] / mono / mini / mini-runtime.c
index 8495f441c4e998576d71c5d21ed1c16c3386a652..c0943117994ef469b57b90cd38a9dc3ce5f561a9 100755 (executable)
@@ -86,8 +86,7 @@ MONO_FAST_TLS_DECLARE(mono_jit_tls);
 gboolean mono_compile_aot = FALSE;
 /* If this is set, no code is generated dynamically, everything is taken from AOT files */
 gboolean mono_aot_only = FALSE;
-/* Whenever to use IMT */
-gboolean mono_use_imt = TRUE;
+
 const char *mono_build_date;
 gboolean mono_do_signal_chaining;
 gboolean mono_do_crash_chaining;
@@ -925,9 +924,9 @@ free_jit_tls_data (MonoJitTlsData *jit_tls)
 static void
 mono_thread_start_cb (intptr_t tid, gpointer stack_start, gpointer func)
 {
-       MonoInternalThread *thread;
+       MonoThreadInfo *thread;
        void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort);
-       thread = mono_thread_internal_current ();
+       thread = mono_thread_info_current_unchecked ();
        if (thread)
                thread->jit_data = jit_tls;
 
@@ -948,9 +947,9 @@ mono_thread_abort_dummy (MonoObject *obj)
 static void
 mono_thread_attach_cb (intptr_t tid, gpointer stack_start)
 {
-       MonoInternalThread *thread;
+       MonoThreadInfo *thread;
        void *jit_tls = setup_jit_tls_data (stack_start, mono_thread_abort_dummy);
-       thread = mono_thread_internal_current ();
+       thread = mono_thread_info_current_unchecked ();
        if (thread)
                thread->jit_data = jit_tls;
        if (mono_profiler_get_events () & MONO_PROFILE_STATISTICAL)
@@ -960,30 +959,41 @@ mono_thread_attach_cb (intptr_t tid, gpointer stack_start)
 }
 
 static void
-mini_thread_cleanup (MonoInternalThread *thread)
+mini_thread_cleanup (MonoNativeThreadId tid)
 {
-       MonoJitTlsData *jit_tls = thread->jit_data;
+       MonoJitTlsData *jit_tls = NULL;
+       MonoThreadInfo *info;
 
-       if (jit_tls) {
-               /* We can't clean up tls information if we are on another thread, it will clean up the wrong stuff
-                * It would be nice to issue a warning when this happens outside of the shutdown sequence. but it's
-                * not a trivial thing.
-                *
-                * The current offender is mono_thread_manage which cleanup threads from the outside.
-                */
-               if (thread == mono_thread_internal_current ())
-                       mono_set_jit_tls (NULL);
+       info = mono_thread_info_current_unchecked ();
+
+       /* We can't clean up tls information if we are on another thread, it will clean up the wrong stuff
+        * It would be nice to issue a warning when this happens outside of the shutdown sequence. but it's
+        * not a trivial thing.
+        *
+        * The current offender is mono_thread_manage which cleanup threads from the outside.
+        */
+       if (info && mono_thread_info_get_tid (info) == tid) {
+               jit_tls = info->jit_data;
+               info->jit_data = NULL;
+
+               mono_set_jit_tls (NULL);
 
                /* If we attach a thread but never call into managed land, we might never get an lmf.*/
                if (mono_get_lmf ()) {
                        mono_set_lmf (NULL);
                        mono_set_lmf_addr (NULL);
                }
+       } else {
+               info = mono_thread_info_lookup (tid);
+               if (info) {
+                       jit_tls = info->jit_data;
+                       info->jit_data = NULL;
+               }
+               mono_hazard_pointer_clear (mono_hazard_pointer_get (), 1);
+       }
 
+       if (jit_tls)
                free_jit_tls_data (jit_tls);
-
-               thread->jit_data = NULL;
-       }
 }
 
 int
@@ -1097,6 +1107,16 @@ mono_patch_info_dup_mp (MonoMemPool *mp, MonoJumpInfo *patch_info)
                //memcpy (info->locals_types, oinfo->locals_types, info->nlocals * sizeof (MonoType*));
                break;
        }
+       case MONO_PATCH_INFO_VIRT_METHOD: {
+               MonoJumpInfoVirtMethod *info;
+               MonoJumpInfoVirtMethod *oinfo;
+
+               oinfo = patch_info->data.virt_method;
+               info = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoVirtMethod));
+               res->data.virt_method = info;
+               memcpy (info, oinfo, sizeof (MonoJumpInfoVirtMethod));
+               break;
+       }
        default:
                break;
        }
@@ -1149,6 +1169,7 @@ mono_patch_info_hash (gconstpointer data)
        case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
        case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR:
        case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
+       case MONO_PATCH_INFO_GC_NURSERY_START:
        case MONO_PATCH_INFO_JIT_TLS_ID:
        case MONO_PATCH_INFO_MONITOR_ENTER:
        case MONO_PATCH_INFO_MONITOR_ENTER_V4:
@@ -1168,6 +1189,11 @@ mono_patch_info_hash (gconstpointer data)
                return (ji->type << 8) | (gsize)ji->data.del_tramp->klass | (gsize)ji->data.del_tramp->method | (gsize)ji->data.del_tramp->virtual;
        case MONO_PATCH_INFO_LDSTR_LIT:
                return g_str_hash (ji->data.target);
+       case MONO_PATCH_INFO_VIRT_METHOD: {
+               MonoJumpInfoVirtMethod *info = ji->data.virt_method;
+
+               return (ji->type << 8) | (gssize)info->klass | (gssize)info->method;
+       }
        default:
                printf ("info type: %d\n", ji->type);
                mono_print_ji (ji); printf ("\n");
@@ -1225,6 +1251,8 @@ mono_patch_info_equal (gconstpointer ka, gconstpointer kb)
                return ji1->data.del_tramp->klass == ji2->data.del_tramp->klass && ji1->data.del_tramp->method == ji2->data.del_tramp->method && ji1->data.del_tramp->virtual == ji2->data.del_tramp->virtual;
        case MONO_PATCH_INFO_CASTCLASS_CACHE:
                return ji1->data.index == ji2->data.index;
+       case MONO_PATCH_INFO_VIRT_METHOD:
+               return ji1->data.virt_method->klass == ji2->data.virt_method->klass && ji1->data.virt_method->method == ji2->data.virt_method->method;
        default:
                if (ji1->data.target != ji2->data.target)
                        return 0;
@@ -1562,6 +1590,15 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
                        slot = mono_method_lookup_or_register_info (entry->method, entry->in_mrgctx, info, entry->info_type, mono_method_get_context (entry->method));
                        break;
                }
+               case MONO_PATCH_INFO_VIRT_METHOD: {
+                       MonoJumpInfoVirtMethod *info;
+                       MonoJumpInfoVirtMethod *oinfo = entry->data->data.virt_method;
+
+                       info = g_malloc0 (sizeof (MonoJumpInfoVirtMethod));
+                       memcpy (info, oinfo, sizeof (MonoJumpInfoVirtMethod));
+                       slot = mono_method_lookup_or_register_info (entry->method, entry->in_mrgctx, info, entry->info_type, mono_method_get_context (entry->method));
+                       break;
+               }
                default:
                        g_assert_not_reached ();
                        break;
@@ -1606,6 +1643,13 @@ mono_resolve_patch_target (MonoMethod *method, MonoDomain *domain, guint8 *code,
                target = mono_gc_get_card_table (&card_table_shift_bits, &card_table_mask);
                break;
        }
+       case MONO_PATCH_INFO_GC_NURSERY_START: {
+               int shift_bits;
+               size_t size;
+
+               target = mono_gc_get_nursery (&shift_bits, &size);
+               break;
+       }
        case MONO_PATCH_INFO_CASTCLASS_CACHE: {
                target = mono_domain_alloc0 (domain, sizeof (gpointer));
                break;
@@ -1745,6 +1789,12 @@ lookup_method (MonoDomain *domain, MonoMethod *method)
        return ji;
 }
 
+MonoJitInfo *
+mono_get_jit_info_from_method (MonoDomain *domain, MonoMethod *method)
+{
+       return lookup_method (domain, method);
+}
+
 #if ENABLE_JIT_MAP
 static FILE* perf_map_file;
 
@@ -2639,7 +2689,7 @@ mini_parse_debug_options (void)
                else if (!strcmp (arg, "explicit-null-checks"))
                        debug_options.explicit_null_checks = TRUE;
                else if (!strcmp (arg, "gen-seq-points"))
-                       debug_options.gen_seq_points_debug_data = TRUE;
+                       debug_options.gen_sdb_seq_points = TRUE;
                else if (!strcmp (arg, "gen-compact-seq-points"))
                        debug_options.gen_seq_points_compact_data = TRUE;
                else if (!strcmp (arg, "init-stacks"))
@@ -2933,10 +2983,8 @@ mini_init (const char *filename, const char *runtime_version)
        callbacks.debug_log_is_enabled = mono_debugger_agent_debug_log_is_enabled;
        callbacks.tls_key_supported = mini_tls_key_supported;
 
-       if (mono_use_imt) {
-               callbacks.get_vtable_trampoline = mini_get_vtable_trampoline;
-               callbacks.get_imt_trampoline = mini_get_imt_trampoline;
-       }
+       callbacks.get_vtable_trampoline = mini_get_vtable_trampoline;
+       callbacks.get_imt_trampoline = mini_get_imt_trampoline;
 
        mono_install_callbacks (&callbacks);
 
@@ -2962,6 +3010,7 @@ mini_init (const char *filename, const char *runtime_version)
 
        mono_unwind_init ();
 
+#ifdef XDEBUG_ENABLED
        if (g_getenv ("MONO_XDEBUG")) {
                const char *xdebug_opts = g_getenv ("MONO_XDEBUG");
                mono_xdebug_init (xdebug_opts);
@@ -2973,6 +3022,7 @@ mini_init (const char *filename, const char *runtime_version)
                mono_dont_free_domains = TRUE;
                mono_using_xdebug = TRUE;
        }
+#endif
 
 #ifdef ENABLE_LLVM
        if (mono_use_llvm) {
@@ -3043,12 +3093,10 @@ mini_init (const char *filename, const char *runtime_version)
                mono_marshal_use_aot_wrappers (TRUE);
        }
 
-       if (mono_use_imt) {
-               if (mono_aot_only)
-                       mono_install_imt_thunk_builder (mono_aot_get_imt_thunk);
-               else
-                       mono_install_imt_thunk_builder (mono_arch_build_imt_thunk);
-       }
+       if (mono_aot_only)
+               mono_install_imt_thunk_builder (mono_aot_get_imt_thunk);
+       else
+               mono_install_imt_thunk_builder (mono_arch_build_imt_thunk);
 
        /*Init arch tls information only after the metadata side is inited to make sure we see dynamic appdomain tls keys*/
        mono_arch_finish_init ();