*
* Author:
* Dietmar Maurer (dietmar@ximian.com)
+ * Patrik Torstensson
*
* (C) 2001 Ximian, Inc.
*/
#include <glib.h>
#include <string.h>
-#if HAVE_BOEHM_GC
-#include <gc/gc.h>
-#endif
+#include <mono/os/gc_wrapper.h>
#include <mono/metadata/object.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/cil-coff.h>
+#include <mono/metadata/rawbuffer.h>
+
+//#define DEBUG_DOMAIN_UNLOAD
+
+static guint32 appdomain_thread_id = -1;
+static guint32 context_thread_id = -1;
-static guint32 appdomain_thread_id = 0;
+static gint32 appdomain_id_counter = 0;
+
+static MonoGHashTable * appdomains_list = NULL;
+
+static CRITICAL_SECTION appdomains_mutex;
MonoDomain *mono_root_domain = NULL;
mono_jit_info_table_find (MonoDomain *domain, char *addr)
{
MonoJitInfoTable *table = domain->jit_info_table;
- int left = 0, right = table->len;
+ int left = 0, right;
+ mono_domain_lock (domain);
+
+ right = table->len;
while (left < right) {
int pos = (left + right) / 2;
MonoJitInfo *ji = g_array_index (table, gpointer, pos);
right = pos;
else if (addr >= end)
left = pos + 1;
- else
+ else {
+ mono_domain_unlock (domain);
return ji;
+ }
}
+ mono_domain_unlock (domain);
- /* maybe irt is shared code, so we also search in the root domain */
+ /* maybe it is shared code, so we also search in the root domain */
if (domain != mono_root_domain)
return mono_jit_info_table_find (mono_root_domain, addr);
{
MonoJitInfoTable *table = domain->jit_info_table;
gpointer start = ji->code_start;
- int pos = mono_jit_info_table_index (table, start);
+ int pos;
+
+ mono_domain_lock (domain);
+ pos = mono_jit_info_table_index (table, start);
g_array_insert_val (table, pos, ji);
+ mono_domain_unlock (domain);
}
static int
const char *end;
len = mono_metadata_decode_blob_size (str, &str) - 1;
end = str + len;
- h = *str;
+ /* if len == 0 *str will point to the mark byte */
+ h = len? *str: 0;
/*
* FIXME: The distribution may not be so nice with lots of
* null chars in the string.
return memcmp (str1, str2, len) == 0;
}
+static gboolean
+mono_string_equal (MonoString *s1, MonoString *s2)
+{
+ int l1 = mono_string_length (s1);
+ int l2 = mono_string_length (s2);
+
+ if (l1 != l2)
+ return FALSE;
+
+ return memcmp (mono_string_chars (s1), mono_string_chars (s2), l1) == 0;
+}
+
+static guint
+mono_string_hash (MonoString *s)
+{
+ const guint16 *p = mono_string_chars (s);
+ int i, len = mono_string_length (s);
+ guint h = 0;
+
+ for (i = 0; i < len; i++) {
+ h = (h << 5) - h + *p;
+ p++;
+ }
+
+ return h;
+}
+
#if HAVE_BOEHM_GC
static void
domain_finalizer (void *obj, void *data) {
MonoDomain *domain;
#if HAVE_BOEHM_GC
- domain = GC_malloc (sizeof (MonoDomain));
- GC_register_finalizer (domain, domain_finalizer, NULL, NULL, NULL);
+ domain = GC_MALLOC (sizeof (MonoDomain));
+ GC_REGISTER_FINALIZER (domain, domain_finalizer, NULL, NULL, NULL);
#else
domain = g_new0 (MonoDomain, 1);
#endif
domain->domain = NULL;
domain->setup = NULL;
domain->friendly_name = NULL;
+ domain->search_path = NULL;
domain->mp = mono_mempool_new ();
- domain->env = g_hash_table_new (g_str_hash, g_str_equal);
+ domain->code_mp = mono_mempool_new ();
+ domain->env = mono_g_hash_table_new ((GHashFunc)mono_string_hash, (GCompareFunc)mono_string_equal);
domain->assemblies = g_hash_table_new (g_str_hash, g_str_equal);
domain->class_vtable_hash = mono_g_hash_table_new (NULL, NULL);
domain->proxy_vtable_hash = mono_g_hash_table_new (NULL, NULL);
domain->jit_code_hash = g_hash_table_new (NULL, NULL);
domain->ldstr_table = mono_g_hash_table_new ((GHashFunc)ldstr_hash, (GCompareFunc)ldstr_equal);
domain->jit_info_table = mono_jit_info_table_new ();
+ domain->class_init_trampoline_hash = mono_g_hash_table_new (NULL, NULL);
+ domain->finalizable_objects_hash = g_hash_table_new (NULL, NULL);
+ domain->domain_id = InterlockedIncrement (&appdomain_id_counter);
InitializeCriticalSection (&domain->lock);
+
+ EnterCriticalSection (&appdomains_mutex);
+ mono_g_hash_table_insert(appdomains_list, GINT_TO_POINTER(domain->domain_id), domain);
+ LeaveCriticalSection (&appdomains_mutex);
+
return domain;
}
g_assert_not_reached ();
appdomain_thread_id = TlsAlloc ();
+ context_thread_id = TlsAlloc ();
+
+ InitializeCriticalSection (&appdomains_mutex);
+
+ mono_metadata_init ();
+ mono_raw_buffer_init ();
+ mono_images_init ();
+ mono_assemblies_init ();
+ mono_loader_init ();
+
+ // FIXME: When should we release this memory?
+ appdomains_list = mono_g_hash_table_new (g_direct_hash, g_direct_equal);
domain = mono_domain_create ();
mono_root_domain = domain;
mono_defaults.corlib, "System", "Array");
g_assert (mono_defaults.array_class != 0);
+ mono_defaults.delegate_class = mono_class_from_name (
+ mono_defaults.corlib, "System", "Delegate");
+ g_assert (mono_defaults.delegate_class != 0 );
+
mono_defaults.multicastdelegate_class = mono_class_from_name (
mono_defaults.corlib, "System", "MulticastDelegate");
g_assert (mono_defaults.multicastdelegate_class != 0 );
mono_defaults.corlib, "System", "Exception");
g_assert (mono_defaults.exception_class != 0);
+ mono_defaults.threadabortexception_class = mono_class_from_name (
+ mono_defaults.corlib, "System.Threading", "ThreadAbortException");
+ g_assert (mono_defaults.threadabortexception_class != 0);
+
mono_defaults.thread_class = mono_class_from_name (
mono_defaults.corlib, "System.Threading", "Thread");
g_assert (mono_defaults.thread_class != 0);
mono_defaults.corlib, "System.Runtime.Remoting.Messaging", "MonoMethodMessage");
g_assert (mono_defaults.mono_method_message_class != 0);
+ mono_defaults.field_info_class = mono_class_from_name (
+ mono_defaults.corlib, "System.Reflection", "FieldInfo");
+ g_assert (mono_defaults.field_info_class != 0);
+
+ mono_defaults.method_info_class = mono_class_from_name (
+ mono_defaults.corlib, "System.Reflection", "MethodInfo");
+ g_assert (mono_defaults.method_info_class != 0);
+
+ mono_defaults.stringbuilder_class = mono_class_from_name (
+ mono_defaults.corlib, "System.Text", "StringBuilder");
+ g_assert (mono_defaults.stringbuilder_class != 0);
+
+ mono_defaults.math_class = mono_class_from_name (
+ mono_defaults.corlib, "System", "Math");
+ g_assert (mono_defaults.math_class != 0);
+
+ mono_defaults.stack_frame_class = mono_class_from_name (
+ mono_defaults.corlib, "System.Diagnostics", "StackFrame");
+ g_assert (mono_defaults.stack_frame_class != 0);
+
+ mono_defaults.stack_trace_class = mono_class_from_name (
+ mono_defaults.corlib, "System.Diagnostics", "StackTrace");
+ g_assert (mono_defaults.stack_trace_class != 0);
+
+ mono_defaults.marshal_class = mono_class_from_name (
+ mono_defaults.corlib, "System.Runtime.InteropServices", "Marshal");
+ g_assert (mono_defaults.marshal_class != 0);
+
+ mono_defaults.iserializeable_class = mono_class_from_name (
+ mono_defaults.corlib, "System.Runtime.Serialization", "ISerializable");
+ g_assert (mono_defaults.iserializeable_class != 0);
+
+ mono_defaults.serializationinfo_class = mono_class_from_name (
+ mono_defaults.corlib, "System.Runtime.Serialization", "SerializationInfo");
+ g_assert (mono_defaults.serializationinfo_class != 0);
+
+ mono_defaults.streamingcontext_class = mono_class_from_name (
+ mono_defaults.corlib, "System.Runtime.Serialization", "StreamingContext");
+ g_assert (mono_defaults.streamingcontext_class != 0);
+
+ mono_defaults.typed_reference_class = mono_class_from_name (
+ mono_defaults.corlib, "System", "TypedReference");
+ g_assert (mono_defaults.typed_reference_class != 0);
+
+ mono_defaults.argumenthandle_class = mono_class_from_name (
+ mono_defaults.corlib, "System", "RuntimeArgumentHandle");
+ g_assert (mono_defaults.argumenthandle_class != 0);
+
+ mono_defaults.marshalbyrefobject_class = mono_class_from_name (
+ mono_defaults.corlib, "System", "MarshalByRefObject");
+ g_assert (mono_defaults.marshalbyrefobject_class != 0);
+
+ mono_defaults.monitor_class = mono_class_from_name (
+ mono_defaults.corlib, "System.Threading", "Monitor");
+ g_assert (mono_defaults.monitor_class != 0);
+
domain->friendly_name = g_path_get_basename (filename);
return domain;
}
/**
- * mono_domain_set:
+ * mono_domain_set_internal:
* @domain: the new domain
*
* Sets the current domain to @domain.
*/
inline void
-mono_domain_set (MonoDomain *domain)
+mono_domain_set_internal (MonoDomain *domain)
{
TlsSetValue (appdomain_thread_id, domain);
+ TlsSetValue (context_thread_id, domain->default_context);
+}
+
+typedef struct {
+ MonoDomainFunc func;
+ gpointer user_data;
+} DomainInfo;
+
+static void
+copy_hash_entry (gpointer key, gpointer data, gpointer user_data)
+{
+ MonoGHashTable *dest = (MonoGHashTable*)user_data;
+
+ mono_g_hash_table_insert (dest, key, data);
+}
+
+static void
+foreach_domain (gpointer key, gpointer data, gpointer user_data)
+{
+ DomainInfo *dom_info = user_data;
+
+ dom_info->func ((MonoDomain*)data, dom_info->user_data);
+}
+
+void
+mono_domain_foreach (MonoDomainFunc func, gpointer user_data)
+{
+ DomainInfo dom_info;
+ MonoGHashTable *copy;
+
+ /*
+ * Create a copy of the hashtable to avoid calling the user callback
+ * inside the lock because that could lead to deadlocks.
+ * We can do this because this function is not perf. critical.
+ */
+ copy = mono_g_hash_table_new (NULL, NULL);
+ EnterCriticalSection (&appdomains_mutex);
+ mono_g_hash_table_foreach (appdomains_list, copy_hash_entry, copy);
+ LeaveCriticalSection (&appdomains_mutex);
+
+ dom_info.func = func;
+ dom_info.user_data = user_data;
+ mono_g_hash_table_foreach (copy, foreach_domain, &dom_info);
+
+ mono_g_hash_table_destroy (copy);
}
/**
* fixme: maybe we should integrate this with mono_assembly_open ??
*/
MonoAssembly *
-mono_domain_assembly_open (MonoDomain *domain, char *name)
+mono_domain_assembly_open (MonoDomain *domain, const char *name)
{
- MonoAssembly *ass, *tmp;
- int i;
+ MonoAssembly *ass;
- if ((ass = g_hash_table_lookup (domain->assemblies, name)))
+ mono_domain_lock (domain);
+ if ((ass = g_hash_table_lookup (domain->assemblies, name))) {
+ mono_domain_unlock (domain);
return ass;
+ }
+ mono_domain_unlock (domain);
if (!(ass = mono_assembly_open (name, NULL)))
return NULL;
- mono_domain_lock (domain);
- g_hash_table_insert (domain->assemblies, ass->aname.name, ass);
- mono_domain_unlock (domain);
-
- /* FIXME: maybe this must be recursive ? */
- for (i = 0; (tmp = ass->image->references [i]) != NULL; i++) {
- if (!g_hash_table_lookup (domain->assemblies, tmp->aname.name))
- g_hash_table_insert (domain->assemblies, tmp->aname.name, tmp);
- }
-
return ass;
}
mono_assembly_close ((MonoAssembly *)value);
}
+static void
+delete_jump_list (gpointer key, gpointer value, gpointer user_data)
+{
+ g_slist_free (value);
+}
+
void
-mono_domain_unload (MonoDomain *domain, gboolean force)
+mono_domain_free (MonoDomain *domain, gboolean force)
{
if ((domain == mono_root_domain) && !force) {
g_warning ("cant unload root domain");
return;
}
+ EnterCriticalSection (&appdomains_mutex);
+ mono_g_hash_table_remove (appdomains_list, GINT_TO_POINTER(domain->domain_id));
+ LeaveCriticalSection (&appdomains_mutex);
+
g_free (domain->friendly_name);
g_hash_table_foreach (domain->assemblies, remove_assembly, NULL);
- g_hash_table_destroy (domain->env);
+ mono_g_hash_table_destroy (domain->env);
g_hash_table_destroy (domain->assemblies);
mono_g_hash_table_destroy (domain->class_vtable_hash);
mono_g_hash_table_destroy (domain->proxy_vtable_hash);
g_hash_table_destroy (domain->jit_code_hash);
mono_g_hash_table_destroy (domain->ldstr_table);
mono_jit_info_table_free (domain->jit_info_table);
+#ifdef DEBUG_DOMAIN_UNLOAD
+ mono_mempool_invalidate (domain->mp);
+ mono_mempool_invalidate (domain->code_mp);
+#else
mono_mempool_destroy (domain->mp);
+ mono_mempool_destroy (domain->code_mp);
+#endif
+ if (domain->jump_target_hash) {
+ g_hash_table_foreach (domain->jump_target_hash, delete_jump_list, NULL);
+ g_hash_table_destroy (domain->jump_target_hash);
+ }
+ mono_g_hash_table_destroy (domain->class_init_trampoline_hash);
+ g_hash_table_destroy (domain->finalizable_objects_hash);
+ if (domain->thread_static_fields)
+ g_hash_table_destroy (domain->thread_static_fields);
DeleteCriticalSection (&domain->lock);
-
+ domain->setup = NULL;
+
/* FIXME: anything else required ? */
#if HAVE_BOEHM_GC
mono_root_domain = NULL;
}
+/**
+ * mono_domain_get_id:
+ *
+ * Returns the a domain for a specific domain id.
+ */
+MonoDomain *
+mono_domain_get_by_id (gint32 domainid)
+{
+ MonoDomain * domain;
+
+ EnterCriticalSection (&appdomains_mutex);
+ domain = mono_g_hash_table_lookup (appdomains_list, GINT_TO_POINTER(domainid));
+ LeaveCriticalSection (&appdomains_mutex);
+
+ return domain;
+}
+
+void
+mono_context_set (MonoAppContext * new_context)
+{
+ TlsSetValue (context_thread_id, new_context);
+}
+
+MonoAppContext *
+mono_context_get ()
+{
+ return ((MonoAppContext *)TlsGetValue (context_thread_id));
+}