2009-06-04 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / domain.c
index 8c9b7161e936fe059fd641e9ddee257a7f183684..f870683f131136693a4a31e3fb2ff8474bc35478 100644 (file)
  * but we can't depend on this).
  */
 static guint32 appdomain_thread_id = -1;
+
+/* 
+ * Avoid calling TlsSetValue () if possible, since in the io-layer, it acquires
+ * a global lock (!) so it is a contention point.
+ */
+#if (defined(__i386__) || defined(__x86_64__)) && !defined(PLATFORM_WIN32)
+#define NO_TLS_SET_VALUE
+#endif
  
 #ifdef HAVE_KW_THREAD
+
 static __thread MonoDomain * tls_appdomain MONO_TLS_FAST;
+
 #define GET_APPDOMAIN() tls_appdomain
+
+#ifdef NO_TLS_SET_VALUE
+#define SET_APPDOMAIN(x) do { \
+       tls_appdomain = x; \
+} while (FALSE)
+#else
 #define SET_APPDOMAIN(x) do { \
        tls_appdomain = x; \
        TlsSetValue (appdomain_thread_id, x); \
 } while (FALSE)
+#endif
 
-#else
+#else /* !HAVE_KW_THREAD */
 
 #define GET_APPDOMAIN() ((MonoDomain *)TlsGetValue (appdomain_thread_id))
 #define SET_APPDOMAIN(x) TlsSetValue (appdomain_thread_id, x);
@@ -141,7 +158,12 @@ mono_jit_info_find_aot_module (guint8* addr);
 guint32
 mono_domain_get_tls_key (void)
 {
+#ifdef NO_TLS_SET_VALUE
+       g_assert_not_reached ();
+       return 0;
+#else
        return appdomain_thread_id;
+#endif
 }
 
 gint32
@@ -1160,6 +1182,8 @@ mono_domain_create (void)
        domain->friendly_name = NULL;
        domain->search_path = NULL;
 
+       mono_gc_register_root ((char*)&(domain->MONO_DOMAIN_FIRST_GC_TRACKED), G_STRUCT_OFFSET (MonoDomain, MONO_DOMAIN_LAST_GC_TRACKED) - G_STRUCT_OFFSET (MonoDomain, MONO_DOMAIN_FIRST_GC_TRACKED), NULL);
+
        mono_profiler_appdomain_event (domain, MONO_PROFILE_START_LOAD);
 
        domain->mp = mono_mempool_new ();
@@ -1175,10 +1199,14 @@ mono_domain_create (void)
        domain->jit_info_table = jit_info_table_new (domain);
        domain->jit_info_free_queue = NULL;
        domain->finalizable_objects_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
+#ifndef HAVE_SGEN_GC
+       domain->track_resurrection_handles_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
+#endif
 
        InitializeCriticalSection (&domain->lock);
        InitializeCriticalSection (&domain->assemblies_lock);
        InitializeCriticalSection (&domain->jit_code_hash_lock);
+       InitializeCriticalSection (&domain->finalizable_objects_hash_lock);
 
        domain->method_rgctx_hash = NULL;
 
@@ -1537,6 +1565,9 @@ mono_init_internal (const char *filename, const char *exe_filename, const char *
        mono_defaults.internals_visible_class = mono_class_from_name (
                mono_defaults.corlib, "System.Runtime.CompilerServices", "InternalsVisibleToAttribute");
 
+       mono_defaults.critical_finalizer_object = mono_class_from_name (
+               mono_defaults.corlib, "System.Runtime.ConstrainedExecution", "CriticalFinalizerObject");
+
        /*
         * mscorlib needs a little help, only now it can load its friends list (after we have
         * loaded the InternalsVisibleToAttribute), load it now
@@ -1725,6 +1756,28 @@ mono_domain_get ()
        return GET_APPDOMAIN ();
 }
 
+void
+mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exception)
+{
+       MonoThread *thread;
+
+       if (mono_domain_get () == domain)
+               return;
+
+       SET_APPDOMAIN (domain);
+       SET_APPCONTEXT (domain->default_context);
+
+       if (migrate_exception) {
+               thread = mono_thread_current ();
+               if (!thread->abort_exc)
+                       return;
+
+               g_assert (thread->abort_exc->object.vtable->domain != domain);
+               MONO_OBJECT_SETREF (thread, abort_exc, mono_get_exception_thread_abort ());
+               g_assert (thread->abort_exc->object.vtable->domain == domain);
+       }
+}
+
 /**
  * mono_domain_set_internal:
  * @domain: the new domain
@@ -1734,8 +1787,7 @@ mono_domain_get ()
 void
 mono_domain_set_internal (MonoDomain *domain)
 {
-       SET_APPDOMAIN (domain);
-       SET_APPCONTEXT (domain->default_context);
+       mono_domain_set_internal_with_options (domain, TRUE);
 }
 
 void
@@ -1792,6 +1844,12 @@ mono_domain_assembly_open (MonoDomain *domain, const char *name)
        return ass;
 }
 
+static void
+free_slist (gpointer key, gpointer value, gpointer user_data)
+{
+       g_slist_free (value);
+}
+
 void
 mono_domain_free (MonoDomain *domain, gboolean force)
 {
@@ -1812,6 +1870,8 @@ mono_domain_free (MonoDomain *domain, gboolean force)
 
        mono_debug_domain_unload (domain);
 
+       mono_gc_clear_domain (domain);
+
        mono_appdomains_lock ();
        appdomains_list [domain->domain_id] = NULL;
        mono_appdomains_unlock ();
@@ -1885,20 +1945,27 @@ mono_domain_free (MonoDomain *domain, gboolean force)
        mono_code_manager_destroy (domain->code_mp);
        domain->code_mp = NULL;
 #endif 
+
+       mono_reflection_cleanup_domain (domain);
+       
        if (domain->type_hash) {
                mono_g_hash_table_destroy (domain->type_hash);
                domain->type_hash = NULL;
        }
-       if (domain->refobject_hash) {
-               mono_g_hash_table_destroy (domain->refobject_hash);
-               domain->refobject_hash = NULL;
-       }
        if (domain->type_init_exception_hash) {
                mono_g_hash_table_destroy (domain->type_init_exception_hash);
                domain->type_init_exception_hash = NULL;
        }
        g_hash_table_destroy (domain->finalizable_objects_hash);
        domain->finalizable_objects_hash = NULL;
+#ifndef HAVE_SGEN_GC
+       if (domain->track_resurrection_objects_hash) {
+               g_hash_table_foreach (domain->track_resurrection_objects_hash, free_slist, NULL);
+               g_hash_table_destroy (domain->track_resurrection_objects_hash);
+       }
+       if (domain->track_resurrection_handles_hash)
+               g_hash_table_destroy (domain->track_resurrection_handles_hash);
+#endif
        if (domain->method_rgctx_hash) {
                g_hash_table_destroy (domain->method_rgctx_hash);
                domain->method_rgctx_hash = NULL;
@@ -1908,6 +1975,7 @@ mono_domain_free (MonoDomain *domain, gboolean force)
                domain->generic_virtual_cases = NULL;
        }
 
+       DeleteCriticalSection (&domain->finalizable_objects_hash_lock);
        DeleteCriticalSection (&domain->assemblies_lock);
        DeleteCriticalSection (&domain->jit_code_hash_lock);
        DeleteCriticalSection (&domain->lock);
@@ -1988,6 +2056,71 @@ mono_domain_alloc0 (MonoDomain *domain, guint size)
        return res;
 }
 
+/*
+ * mono_domain_code_reserve:
+ *
+ * LOCKING: Acquires the domain lock.
+ */
+void*
+mono_domain_code_reserve (MonoDomain *domain, int size)
+{
+       gpointer res;
+
+       mono_domain_lock (domain);
+       res = mono_code_manager_reserve (domain->code_mp, size);
+       mono_domain_unlock (domain);
+
+       return res;
+}
+
+/*
+ * mono_domain_code_reserve_align:
+ *
+ * LOCKING: Acquires the domain lock.
+ */
+void*
+mono_domain_code_reserve_align (MonoDomain *domain, int size, int alignment)
+{
+       gpointer res;
+
+       mono_domain_lock (domain);
+       res = mono_code_manager_reserve_align (domain->code_mp, size, alignment);
+       mono_domain_unlock (domain);
+
+       return res;
+}
+
+/*
+ * mono_domain_code_commit:
+ *
+ * LOCKING: Acquires the domain lock.
+ */
+void
+mono_domain_code_commit (MonoDomain *domain, void *data, int size, int newsize)
+{
+       mono_domain_lock (domain);
+       mono_code_manager_commit (domain->code_mp, data, size, newsize);
+       mono_domain_unlock (domain);
+}
+
+/*
+ * mono_domain_code_foreach:
+ * Iterate over the code thunks of the code manager of @domain.
+ * 
+ * The @func callback MUST not take any locks. If it really needs to, it must respect
+ * the locking rules of the runtime: http://www.mono-project.com/Mono:Runtime:Documentation:ThreadSafety 
+ * LOCKING: Acquires the domain lock.
+ */
+
+void
+mono_domain_code_foreach (MonoDomain *domain, MonoCodeManagerFunc func, void *user_data)
+{
+       mono_domain_lock (domain);
+       mono_code_manager_foreach (domain->code_mp, func, user_data);
+       mono_domain_unlock (domain);
+}
+
+
 void 
 mono_context_set (MonoAppContext * new_context)
 {