Only call GC_DllMain for embedded gc
[mono.git] / mono / metadata / domain-internals.h
index 52c5cab552c8df5715d4bb71edc80787bea6a933..815f605c4457d321b4fcda6e46a51c352c300029 100644 (file)
@@ -5,13 +5,25 @@
 #define __MONO_METADATA_DOMAIN_INTERNALS_H__
 
 #include <mono/metadata/appdomain.h>
+#include <mono/metadata/mempool.h>
+#include <mono/metadata/lock-tracer.h>
 #include <mono/utils/mono-codeman.h>
-#include <mono/utils/mono-hash.h>
+#include <mono/metadata/mono-hash.h>
 #include <mono/utils/mono-compiler.h>
 #include <mono/utils/mono-internal-hash.h>
 #include <mono/io-layer/io-layer.h>
+#include <mono/metadata/mempool-internals.h>
 
 extern CRITICAL_SECTION mono_delegate_section;
+extern CRITICAL_SECTION mono_strtod_mutex;
+
+/*
+ * If this is set, the memory belonging to appdomains is not freed when a domain is
+ * unloaded, and assemblies loaded by the appdomain are not unloaded either. This
+ * allows us to use typed gc in non-default appdomains too, leading to increased
+ * performance.
+ */ 
+extern gboolean mono_dont_free_domains;
 
 /* This is a copy of System.AppDomainSetup */
 typedef struct {
@@ -33,10 +45,11 @@ typedef struct {
        MonoBoolean disallow_code_downloads;
        MonoObject *activation_arguments; /* it is System.Object in 1.x, ActivationArguments in 2.0 */
        MonoObject *domain_initializer;
-       MonoArray *domain_initializer_args;
        MonoObject *application_trust; /* it is System.Object in 1.x, ApplicationTrust in 2.0 */
+       MonoArray *domain_initializer_args;
        MonoBoolean disallow_appbase_probe;
        MonoArray *configuration_bytes;
+       MonoArray *serialized_non_primitives;
 } MonoAppDomainSetup;
 
 typedef struct _MonoJitInfoTable MonoJitInfoTable;
@@ -54,10 +67,13 @@ struct _MonoJitInfoTableChunk
 
 struct _MonoJitInfoTable
 {
+       MonoDomain             *domain;
        int                     num_chunks;
        MonoJitInfoTableChunk  *chunks [MONO_ZERO_LEN_ARRAY];
 };
 
+#define MONO_SIZEOF_JIT_INFO_TABLE (sizeof (struct _MonoJitInfoTable) - MONO_ZERO_LEN_ARRAY * SIZEOF_VOID_P)
+
 typedef GArray MonoAotModuleInfoTable;
 
 typedef struct {
@@ -69,9 +85,63 @@ typedef struct {
        union {
                MonoClass *catch_class;
                gpointer filter;
+               gpointer handler_end;
        } data;
 } MonoJitExceptionInfo;
 
+/*
+ * Will contain information on the generic type arguments in the
+ * future.  For now, all arguments are always reference types.
+ */
+typedef struct {
+       int dummy;
+} MonoGenericSharingContext;
+
+typedef struct
+{
+       MonoGenericSharingContext *generic_sharing_context;
+       gint32 this_offset;
+       guint8 this_reg;
+       gboolean has_this:1;
+       gboolean this_in_reg:1;
+} MonoGenericJitInfo;
+
+/*
+A try block hole is used to represent a non-contiguous part of
+of a segment of native code protected by a given .try block.
+Usually, a try block is defined as a contiguous segment of code.
+But in some cases it's needed to have some parts of it to not be protected.
+For example, given "try {} finally {}", the code in the .try block to call
+the finally part looks like:
+
+try {
+    ...
+       call finally_block
+       adjust stack
+       jump outside try block
+       ...
+} finally {
+       ...
+}
+
+The instructions between the call and the jump should not be under the try block since they happen
+after the finally block executes, which means if an async exceptions happens at that point we would
+execute the finally clause twice. So, to avoid this, we introduce a hole in the try block to signal
+that those instructions are not protected.
+*/
+typedef struct
+{
+       guint32 offset;
+       guint16 clause;
+       guint16 length;
+} MonoTryBlockHoleJitInfo;
+
+typedef struct
+{
+       guint16 num_holes;
+       MonoTryBlockHoleJitInfo holes [MONO_ZERO_LEN_ARRAY];
+} MonoTryBlockHoleTableJitInfo;
+
 struct _MonoJitInfo {
        /* NOTE: These first two elements (method and
           next_jit_code_hash) must be in the same order and at the
@@ -80,9 +150,10 @@ struct _MonoJitInfo {
        MonoMethod *method;
        struct _MonoJitInfo *next_jit_code_hash;
        gpointer    code_start;
+       /* This might contain an id for the unwind info instead of a register mask */
        guint32     used_regs;
        int         code_size;
-       guint32     num_clauses:16;
+       guint32     num_clauses:15;
        /* Whenever the code is domain neutral or 'shared' */
        gboolean    domain_neutral:1;
        gboolean    cas_inited:1;
@@ -92,14 +163,20 @@ struct _MonoJitInfo {
        gboolean    cas_method_assert:1;
        gboolean    cas_method_deny:1;
        gboolean    cas_method_permitonly:1;
-       gboolean    generic_shared:1;
+       gboolean    has_generic_jit_info:1;
+       gboolean    has_try_block_holes:1;
+       gboolean    from_aot:1;
+       gboolean    from_llvm:1;
+
+       /* FIXME: Embed this after the structure later*/
+       gpointer    gc_info; /* Currently only used by SGen */
+       
        MonoJitExceptionInfo clauses [MONO_ZERO_LEN_ARRAY];
+       /* There is an optional MonoGenericJitInfo after the clauses */
+       /* There is an optional MonoTryBlockHoleTableJitInfo after MonoGenericJitInfo clauses*/
 };
 
-typedef struct {
-       MonoJitInfo *ji;
-       MonoCodeManager *code_mp;
-} MonoJitDynamicMethodInfo;
+#define MONO_SIZEOF_JIT_INFO (offsetof (struct _MonoJitInfo, clauses))
 
 struct _MonoAppContext {
        MonoObject obj;
@@ -108,15 +185,34 @@ struct _MonoAppContext {
        gpointer *static_data;
 };
 
+/*
+ * We have two unloading states because the domain
+ * must remain fully functional while AppDomain::DomainUnload is
+ * processed.
+ * After that unloading began and all domain facilities are teared down
+ * such as execution of new threadpool jobs.  
+ */
 typedef enum {
        MONO_APPDOMAIN_CREATED,
+       MONO_APPDOMAIN_UNLOADING_START,
        MONO_APPDOMAIN_UNLOADING,
        MONO_APPDOMAIN_UNLOADED
 } MonoAppDomainState;
 
+typedef struct _MonoThunkFreeList {
+       guint32 size;
+       int length;             /* only valid for the wait list */
+       struct _MonoThunkFreeList *next;
+} MonoThunkFreeList;
+
 typedef struct _MonoJitCodeHash MonoJitCodeHash;
 
 struct _MonoDomain {
+       /*
+        * This lock must never be taken before the loader lock,
+        * i.e. if both are taken by the same thread, the loader lock
+        * must taken first.
+        */
        CRITICAL_SECTION    lock;
        MonoMemPool        *mp;
        MonoCodeManager    *code_mp;
@@ -131,6 +227,16 @@ struct _MonoDomain {
        MonoException      *out_of_memory_ex;
        MonoException      *null_reference_ex;
        MonoException      *stack_overflow_ex;
+       /* typeof (void) */
+       MonoObject         *typeof_void;
+       /* Ephemeron Tombstone*/
+       MonoObject         *ephemeron_tombstone;
+       /* new MonoType [0] */
+       MonoArray          *empty_types;
+       /* 
+        * The fields between FIRST_GC_TRACKED and LAST_GC_TRACKED are roots, but
+        * not object references.
+        */
 #define MONO_DOMAIN_FIRST_GC_TRACKED env
        MonoGHashTable     *env;
        MonoGHashTable     *ldstr_table;
@@ -148,17 +254,20 @@ struct _MonoDomain {
        /* Needed by Thread:GetDomainID() */
        gint32             domain_id;
        gint32             shadow_serial;
+       unsigned char      inet_family_hint; // used in socket-io.c as a cache
        GSList             *domain_assemblies;
        MonoAssembly       *entry_assembly;
        char               *friendly_name;
-       GHashTable         *class_vtable_hash;
+       GPtrArray          *class_vtable_array;
        /* maps remote class key -> MonoRemoteClass */
        GHashTable         *proxy_vtable_hash;
+       /* Protected by 'jit_code_hash_lock' */
        MonoInternalHashTable jit_code_hash;
-       /* maps MonoMethod -> MonoJitDynamicMethodInfo */
-       GHashTable         *dynamic_code_hash;
+       CRITICAL_SECTION    jit_code_hash_lock;
+       int                 num_jit_info_tables;
        MonoJitInfoTable * 
          volatile          jit_info_table;
+       GSList             *jit_info_free_queue;
        /* Used when loading assemblies */
        gchar **search_path;
        gchar *private_bin_path;
@@ -168,20 +277,61 @@ struct _MonoDomain {
        MonoMethod         *private_invoke_method;
        /* Used to store offsets of thread and context static fields */
        GHashTable         *special_static_fields;
-       GHashTable         *jump_target_hash;
-       GHashTable         *class_init_trampoline_hash;
-       GHashTable         *jump_trampoline_hash;
-       GHashTable         *jit_trampoline_hash;
-       GHashTable         *delegate_trampoline_hash;
        /* 
         * This must be a GHashTable, since these objects can't be finalized
         * if the hashtable contains a GC visible reference to them.
         */
        GHashTable         *finalizable_objects_hash;
+
+       /* These two are boehm only */
+       /* Maps MonoObjects to a GSList of WeakTrackResurrection GCHandles pointing to them */
+       GHashTable         *track_resurrection_objects_hash;
+       /* Maps WeakTrackResurrection GCHandles to the MonoObjects they point to */
+       GHashTable         *track_resurrection_handles_hash;
+
+       /* Protects the three hashes above */
+       CRITICAL_SECTION   finalizable_objects_hash_lock;
        /* Used when accessing 'domain_assemblies' */
        CRITICAL_SECTION    assemblies_lock;
 
-       GHashTable         *shared_generics_hash;
+       GHashTable         *method_rgctx_hash;
+
+       GHashTable         *generic_virtual_cases;
+       MonoThunkFreeList **thunk_free_lists;
+
+       /* Information maintained by the JIT engine */
+       gpointer runtime_info;
+
+       /*thread pool jobs, used to coordinate shutdown.*/
+       volatile int                    threadpool_jobs;
+       HANDLE                          cleanup_semaphore;
+       
+       /* Contains the compiled runtime invoke wrapper used by finalizers */
+       gpointer            finalize_runtime_invoke;
+
+       /* Contains the compiled runtime invoke wrapper used by async resylt creation to capture thread context*/
+       gpointer            capture_context_runtime_invoke;
+
+       /* Contains the compiled method used by async resylt creation to capture thread context*/
+       gpointer            capture_context_method;
+
+       /* Assembly bindings, the per-domain part */
+       GSList *assembly_bindings;
+       gboolean assembly_bindings_parsed;
+
+       /* Used by socket-io.c */
+       /* These are domain specific, since the assembly can be unloaded */
+       MonoImage *socket_assembly;
+       MonoClass *sockaddr_class;
+       MonoClassField *sockaddr_data_field;
+
+       /* Used by threadpool.c */
+       MonoImage *system_image;
+       MonoImage *system_net_dll;
+       MonoClass *corlib_asyncresult_class;
+       MonoClass *socket_class;
+       MonoClass *ad_unloaded_ex_class;
+       MonoClass *process_class;
 };
 
 typedef struct  {
@@ -192,13 +342,33 @@ typedef struct  {
 typedef struct  {
        const char runtime_version [12];
        const char framework_version [4];
-       const AssemblyVersionSet version_sets [2];
+       const AssemblyVersionSet version_sets [3];
 } MonoRuntimeInfo;
 
-#define mono_domain_lock(domain)   EnterCriticalSection(&(domain)->lock)
-#define mono_domain_unlock(domain) LeaveCriticalSection(&(domain)->lock)
-#define mono_domain_assemblies_lock(domain)   EnterCriticalSection(&(domain)->assemblies_lock)
-#define mono_domain_assemblies_unlock(domain) LeaveCriticalSection(&(domain)->assemblies_lock)
+#define mono_domain_lock(domain) mono_locks_acquire(&(domain)->lock, DomainLock)
+#define mono_domain_unlock(domain) mono_locks_release(&(domain)->lock, DomainLock)
+#define mono_domain_assemblies_lock(domain) mono_locks_acquire(&(domain)->assemblies_lock, DomainAssembliesLock)
+#define mono_domain_assemblies_unlock(domain) mono_locks_release(&(domain)->assemblies_lock, DomainAssembliesLock)
+#define mono_domain_jit_code_hash_lock(domain) mono_locks_acquire(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
+#define mono_domain_jit_code_hash_unlock(domain) mono_locks_release(&(domain)->jit_code_hash_lock, DomainJitCodeHashLock)
+
+typedef MonoDomain* (*MonoLoadFunc) (const char *filename, const char *runtime_version);
+
+void
+mono_install_runtime_load  (MonoLoadFunc func) MONO_INTERNAL;
+
+MonoDomain*
+mono_runtime_load (const char *filename, const char *runtime_version) MONO_INTERNAL;
+
+typedef void (*MonoCreateDomainFunc) (MonoDomain *domain);
+
+void
+mono_install_create_domain_hook (MonoCreateDomainFunc func) MONO_INTERNAL;
+
+typedef void (*MonoFreeDomainFunc) (MonoDomain *domain);
+
+void
+mono_install_free_domain_hook (MonoFreeDomainFunc func) MONO_INTERNAL;
 
 void 
 mono_init_com_types (void) MONO_INTERNAL;
@@ -206,6 +376,9 @@ mono_init_com_types (void) MONO_INTERNAL;
 void 
 mono_cleanup (void) MONO_INTERNAL;
 
+void
+mono_close_exe_image (void) MONO_INTERNAL;
+
 void
 mono_jit_info_table_add    (MonoDomain *domain, MonoJitInfo *ji) MONO_INTERNAL;
 
@@ -215,12 +388,56 @@ mono_jit_info_table_remove (MonoDomain *domain, MonoJitInfo *ji) MONO_INTERNAL;
 void
 mono_jit_info_add_aot_module (MonoImage *image, gpointer start, gpointer end) MONO_INTERNAL;
 
+MonoGenericJitInfo*
+mono_jit_info_get_generic_jit_info (MonoJitInfo *ji) MONO_INTERNAL;
+
+MonoGenericSharingContext*
+mono_jit_info_get_generic_sharing_context (MonoJitInfo *ji) MONO_INTERNAL;
+
+void
+mono_jit_info_set_generic_sharing_context (MonoJitInfo *ji, MonoGenericSharingContext *gsctx) MONO_INTERNAL;
+
 MonoJitInfo*
 mono_domain_lookup_shared_generic (MonoDomain *domain, MonoMethod *method) MONO_INTERNAL;
 
+char *
+mono_make_shadow_copy (const char *filename) MONO_INTERNAL;
+
+gboolean
+mono_is_shadow_copy_enabled (MonoDomain *domain, const gchar *dir_name) MONO_INTERNAL;
+
+gpointer
+mono_domain_alloc  (MonoDomain *domain, guint size) MONO_INTERNAL;
+
+gpointer
+mono_domain_alloc0 (MonoDomain *domain, guint size) MONO_INTERNAL;
+
+void*
+mono_domain_code_reserve (MonoDomain *domain, int size) MONO_LLVM_INTERNAL;
+
+void*
+mono_domain_code_reserve_align (MonoDomain *domain, int size, int alignment) MONO_INTERNAL;
+
 void
-mono_domain_register_shared_generic (MonoDomain *domain, MonoMethod *method, MonoJitInfo *jit_info) MONO_INTERNAL;
+mono_domain_code_commit (MonoDomain *domain, void *data, int size, int newsize) MONO_INTERNAL;
 
+void *
+nacl_domain_get_code_dest (MonoDomain *domain, void *data) MONO_INTERNAL;
+
+void 
+nacl_domain_code_validate (MonoDomain *domain, guint8 **buf_base, int buf_size, guint8 **code_end) MONO_INTERNAL;
+
+void
+mono_domain_code_foreach (MonoDomain *domain, MonoCodeManagerFunc func, void *user_data) MONO_INTERNAL;
+
+void
+mono_domain_unset (void) MONO_INTERNAL;
+
+void
+mono_domain_set_internal_with_options (MonoDomain *domain, gboolean migrate_exception) MONO_INTERNAL;
+
+MonoTryBlockHoleTableJitInfo*
+mono_jit_info_get_try_block_hole_table_info (MonoJitInfo *ji) MONO_INTERNAL;
 
 /* 
  * Installs a new function which is used to return a MonoJitInfo for a method inside
@@ -286,9 +503,8 @@ ves_icall_System_AppDomain_InternalUnload          (gint32 domain_id) MONO_INTER
 
 gint32
 ves_icall_System_AppDomain_ExecuteAssembly         (MonoAppDomain *ad, 
-                                                   MonoString    *file, 
-                                                   MonoObject    *evidence,
-                                                   MonoArray     *args) MONO_INTERNAL;
+                                                                                                       MonoReflectionAssembly *refass,
+                                                                                                       MonoArray     *args) MONO_INTERNAL;
 
 MonoAppDomain * 
 ves_icall_System_AppDomain_InternalSetDomain      (MonoAppDomain *ad) MONO_INTERNAL;
@@ -326,11 +542,14 @@ mono_assembly_load_corlib (const MonoRuntimeInfo *runtime, MonoImageOpenStatus *
 const MonoRuntimeInfo*
 mono_get_runtime_info (void) MONO_INTERNAL;
 
+void
+mono_runtime_set_no_exec (gboolean val) MONO_INTERNAL;
+
 gboolean
-mono_assembly_name_parse (const char *name, MonoAssemblyName *aname) MONO_INTERNAL;
+mono_runtime_get_no_exec (void) MONO_INTERNAL;
 
-void
-mono_assembly_name_free (MonoAssemblyName *aname) MONO_INTERNAL;
+gboolean
+mono_assembly_name_parse (const char *name, MonoAssemblyName *aname) MONO_INTERNAL;
 
 MonoImage *mono_assembly_open_from_bundle (const char *filename,
                                           MonoImageOpenStatus *status,
@@ -347,4 +566,12 @@ MonoAssembly* mono_assembly_load_full_nosearch (MonoAssemblyName *aname,
                                                MonoImageOpenStatus *status,
                                                gboolean refonly) MONO_INTERNAL;
 
+void mono_set_private_bin_path_from_config (MonoDomain *domain) MONO_INTERNAL;
+
+int mono_framework_version (void) MONO_INTERNAL;
+
+void mono_reflection_cleanup_domain (MonoDomain *domain) MONO_INTERNAL;
+
+void mono_assembly_cleanup_domain_bindings (guint32 domain_id) MONO_INTERNAL;;
+
 #endif /* __MONO_METADATA_DOMAIN_INTERNALS_H__ */