Merge pull request #2237 from xmcclure/container-owner
[mono.git] / mono / mini / aot-runtime.c
index 82e78f2a48b27da3de064c0cd0d95a05990da095..f56ff6e19a2d4d8477c403f75656b8a90e582813 100644 (file)
@@ -155,8 +155,8 @@ typedef struct {
 } TrampolinePage;
 
 static GHashTable *aot_modules;
-#define mono_aot_lock() mono_mutex_lock (&aot_mutex)
-#define mono_aot_unlock() mono_mutex_unlock (&aot_mutex)
+#define mono_aot_lock() mono_os_mutex_lock (&aot_mutex)
+#define mono_aot_unlock() mono_os_mutex_unlock (&aot_mutex)
 static mono_mutex_t aot_mutex;
 
 /* 
@@ -201,8 +201,8 @@ static GHashTable *aot_jit_icall_hash;
 #define USE_PAGE_TRAMPOLINES 0
 #endif
 
-#define mono_aot_page_lock() mono_mutex_lock (&aot_page_mutex)
-#define mono_aot_page_unlock() mono_mutex_unlock (&aot_page_mutex)
+#define mono_aot_page_lock() mono_os_mutex_lock (&aot_page_mutex)
+#define mono_aot_page_unlock() mono_os_mutex_unlock (&aot_page_mutex)
 static mono_mutex_t aot_page_mutex;
 
 static MonoAotModule *mscorlib_aot_module;
@@ -222,13 +222,13 @@ decode_patches (MonoAotModule *amodule, MonoMemPool *mp, int n_patches, gboolean
 static inline void
 amodule_lock (MonoAotModule *amodule)
 {
-       mono_mutex_lock (&amodule->mutex);
+       mono_os_mutex_lock (&amodule->mutex);
 }
 
 static inline void
 amodule_unlock (MonoAotModule *amodule)
 {
-       mono_mutex_unlock (&amodule->mutex);
+       mono_os_mutex_unlock (&amodule->mutex);
 }
 
 /*
@@ -1122,9 +1122,17 @@ decode_method_ref_with_target (MonoAotModule *module, MethodRef *ref, MonoMethod
                                        g_assert_not_reached ();
                                        break;
                                }
-                               if (target && wrapper != target)
-                                       return FALSE;
-                               ref->method = wrapper;
+                               if (target) {
+                                       /*
+                                        * Due to the way mini_get_shared_method () works, we could end up with
+                                        * multiple copies of the same wrapper.
+                                        */
+                                       if (wrapper->klass != target->klass)
+                                               return FALSE;
+                                       ref->method = target;
+                               } else {
+                                       ref->method = wrapper;
+                               }
                        } else {
                                /*
                                 * These wrappers are associated with a signature, not with a method.
@@ -1882,7 +1890,7 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
                                mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module '%s' not found: %s\n", aot_name, err);
                                g_free (err);
 
-                               aot_name = g_strdup_printf ("%s/mono/aot-cache/%s/%s%s", mono_assembly_getrootdir(), ARCHITECTURE, g_path_get_basename (assembly->image->name), MONO_SOLIB_EXT);
+                               aot_name = g_strdup_printf ("%s/mono/aot-cache/%s/%s%s", mono_assembly_getrootdir(), MONO_ARCHITECTURE, g_path_get_basename (assembly->image->name), MONO_SOLIB_EXT);
                                sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
                                if (!sofile) {
                                        mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module '%s' not found: %s\n", aot_name, err);
@@ -1958,7 +1966,7 @@ load_aot_module (MonoAssembly *assembly, gpointer user_data)
        amodule->blob = blob;
        amodule->shared_got = g_new0 (gpointer, info->nshared_got_entries);
 
-       mono_mutex_init_recursive (&amodule->mutex);
+       mono_os_mutex_init_recursive (&amodule->mutex);
 
        /* Read image table */
        {
@@ -2184,8 +2192,8 @@ mono_aot_register_module (gpointer *aot_info)
 void
 mono_aot_init (void)
 {
-       mono_mutex_init_recursive (&aot_mutex);
-       mono_mutex_init_recursive (&aot_page_mutex);
+       mono_os_mutex_init_recursive (&aot_mutex);
+       mono_os_mutex_init_recursive (&aot_page_mutex);
        aot_modules = g_hash_table_new (NULL, NULL);
 
 #ifndef __native_client__
@@ -3445,15 +3453,6 @@ decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guin
        case MONO_PATCH_INFO_AOT_MODULE:
        case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR:
                break;
-       case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: {
-               MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp));
-
-               imt_tramp->method = decode_resolve_method_ref (aot_module, p, &p);
-               imt_tramp->vt_offset = decode_value (p, &p);
-               
-               ji->data.imt_tramp = imt_tramp;
-               break;
-       }
        case MONO_PATCH_INFO_SIGNATURE:
                ji->data.target = decode_signature (aot_module, p, &p);
                break;
@@ -4096,9 +4095,15 @@ gpointer
 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
 {
        MonoClass *klass = method->klass;
+       MonoMethod *orig_method = method;
        guint32 method_index;
        MonoAotModule *amodule = klass->image->aot_module;
        guint8 *code;
+       gboolean cache_result = FALSE;
+
+       if (domain != mono_get_root_domain ())
+               /* Non shared AOT code can't be used in other appdomains */
+               return NULL;
 
        if (enable_aot_cache && !amodule && domain->entry_assembly && klass->image == mono_defaults.corlib) {
                /* This cannot be AOTed during startup, so do it now */
@@ -4157,6 +4162,7 @@ mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
                if (code)
                        return code;
 
+               cache_result = TRUE;
                method_index = find_aot_method (method, &amodule);
                /*
                 * Special case the ICollection<T> wrappers for arrays, as they cannot
@@ -4305,7 +4311,13 @@ mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
                method_index = mono_metadata_token_index (method->token) - 1;
        }
 
-       return load_method (domain, amodule, klass->image, method, method->token, method_index);
+       code = load_method (domain, amodule, klass->image, method, method->token, method_index);
+       if (code && cache_result) {
+               amodule_lock (amodule);
+               g_hash_table_insert (amodule->method_to_code, orig_method, code);
+               amodule_unlock (amodule);
+       }
+       return code;
 }
 
 /**