Merge pull request #5010 from Unity-Technologies/boehm-gc-alloc-fixed-sre
authorAleksey Kliger (λgeek) <akliger@gmail.com>
Fri, 16 Jun 2017 14:38:37 +0000 (10:38 -0400)
committerGitHub <noreply@github.com>
Fri, 16 Jun 2017 14:38:37 +0000 (10:38 -0400)
Boehm GC Fixes - SRE and Handle Stack

mono/metadata/boehm-gc.c
mono/metadata/dynamic-image.c
mono/metadata/handle.c
mono/metadata/sre.c
mono/utils/mono-threads.c

index edc1210587834cd3c50ae1e0b8c5ad92d8517f34..42f4d16597d1b44d5cd5ac7c7faa7a8314387a41 100644 (file)
@@ -408,6 +408,8 @@ boehm_thread_unregister (MonoThreadInfo *p)
 
        if (p->runtime_thread)
                mono_threads_add_joinable_thread ((gpointer)tid);
+
+       mono_handle_stack_free (p->handle_stack);
 }
 
 static void
index b846347126cb0bf39f5baea7359c3544099cf384..9ea2bcf0ee5c5252c362d8b8106a623db82faa87 100644 (file)
@@ -314,12 +314,7 @@ mono_dynamic_image_create (MonoDynamicAssembly *assembly, char *assembly_name, c
        else
                version = mono_get_runtime_info ()->runtime_version;
 
-#if HAVE_BOEHM_GC
-       /* The MonoGHashTable's need GC tracking */
-       image = (MonoDynamicImage *)GC_MALLOC (sizeof (MonoDynamicImage));
-#else
        image = g_new0 (MonoDynamicImage, 1);
-#endif
 
        mono_profiler_module_event (&image->image, MONO_PROFILE_START_LOAD);
        
@@ -549,10 +544,5 @@ mono_dynamic_image_free (MonoDynamicImage *image)
 void
 mono_dynamic_image_free_image (MonoDynamicImage *image)
 {
-       /* See create_dynamic_mono_image () */
-#if HAVE_BOEHM_GC
-       /* Allocated using GC_MALLOC */
-#else
        g_free (image);
-#endif
 }
index 59bde7fbe7750ef52ee801160bae8f94b6c87ba3..736fd56a7a94e7aa4d9715e43d51a13f6beb7177 100644 (file)
@@ -69,6 +69,55 @@ Combine: MonoDefaults, GENERATE_GET_CLASS_WITH_CACHE, TYPED_HANDLE_DECL and frie
  * points to a valid value.
  */
 
+#ifdef HAVE_BOEHM_GC
+static HandleStack*
+new_handle_stack ()
+{
+       return (HandleStack *)mono_gc_alloc_fixed (sizeof (HandleStack), MONO_GC_DESCRIPTOR_NULL, MONO_ROOT_SOURCE_HANDLE, "Thread Handle Stack");
+}
+
+static void
+free_handle_stack (HandleStack *stack)
+{
+       mono_gc_free_fixed (stack);
+}
+
+static HandleChunk*
+new_handle_chunk ()
+{
+       return (HandleChunk *)GC_MALLOC (sizeof (HandleChunk));
+}
+
+static void
+free_handle_chunk (HandleChunk *chunk)
+{
+}
+#else
+static HandleStack*
+new_handle_stack ()
+{
+       return g_new (HandleStack, 1);
+}
+
+static void
+free_handle_stack (HandleStack *stack)
+{
+       g_free (stack);
+}
+
+static HandleChunk*
+new_handle_chunk ()
+{
+       return g_new (HandleChunk, 1);
+}
+
+static void
+free_handle_chunk (HandleChunk *chunk)
+{
+       g_free (chunk);
+}
+#endif
+
 const MonoObjectHandle mono_null_value_handle = NULL;
 
 #define THIS_IS_AN_OK_NUMBER_OF_HANDLES 100
@@ -197,7 +246,7 @@ retry:
                handles->top = top;
                goto retry;
        }
-       HandleChunk *new_chunk = g_new (HandleChunk, 1);
+       HandleChunk *new_chunk = new_handle_chunk ();
        new_chunk->size = 0;
        new_chunk->prev = top;
        new_chunk->next = NULL;
@@ -245,10 +294,14 @@ mono_handle_new_interior (gpointer rawptr, const char *owner)
 HandleStack*
 mono_handle_stack_alloc (void)
 {
-       HandleStack *stack = g_new0 (HandleStack, 1);
-       HandleChunk *chunk = g_new0 (HandleChunk, 1);
-       HandleChunk *interior = g_new0 (HandleChunk, 1);
-
+       HandleStack *stack = new_handle_stack ();
+       HandleChunk *chunk = new_handle_chunk ();
+       HandleChunk *interior = new_handle_chunk ();
+
+       chunk->prev = chunk->next = NULL;
+       chunk->size = 0;
+       interior->prev = interior->next = NULL;
+       interior->size = 0;
        mono_memory_write_barrier ();
        stack->top = stack->bottom = chunk;
        stack->interior = interior;
@@ -268,12 +321,12 @@ mono_handle_stack_free (HandleStack *stack)
        mono_memory_write_barrier ();
        while (c) {
                HandleChunk *next = c->next;
-               g_free (c);
+               free_handle_chunk (c);
                c = next;
        }
-       g_free (c);
-       g_free (stack->interior);
-       g_free (stack);
+       free_handle_chunk (c);
+       free_handle_chunk (stack->interior);
+       free_handle_stack (stack);
 }
 
 void
index 47dd369faf6f1e608c4f9f79dc95de23364ac246..63a9a99aaa3762e294aa6056cdf59769f25469ff 100644 (file)
@@ -1222,12 +1222,7 @@ mono_reflection_dynimage_basic_init (MonoReflectionAssemblyBuilder *assemblyb)
        if (assemblyb->dynamic_assembly)
                return;
 
-#if HAVE_BOEHM_GC
-       /* assembly->assembly.image might be GC allocated */
-       assembly = assemblyb->dynamic_assembly = (MonoDynamicAssembly *)GC_MALLOC (sizeof (MonoDynamicAssembly));
-#else
        assembly = assemblyb->dynamic_assembly = g_new0 (MonoDynamicAssembly, 1);
-#endif
 
        mono_profiler_assembly_event (&assembly->assembly, MONO_PROFILE_START_LOAD);
        
index dac28b7037dbe4f739cfa543457f1d8514305c14..6afb74daf8de35618dbe6a1374f9100aaf64c124 100644 (file)
@@ -1029,16 +1029,21 @@ STW to make sure no unsafe pending suspend is in progress.
 static void
 mono_thread_info_suspend_lock_with_info (MonoThreadInfo *info)
 {
-       g_assert (info);
-       g_assert (mono_thread_info_is_current (info));
-       g_assert (mono_thread_info_is_live (info));
+       if (mono_threads_is_coop_enabled ()) {
+               g_assert (info);
+               g_assert (mono_thread_info_is_current (info));
+               g_assert (mono_thread_info_is_live (info));
 
-       MONO_ENTER_GC_SAFE_WITH_INFO(info);
+               MONO_ENTER_GC_SAFE_WITH_INFO(info);
 
-       int res = mono_os_sem_wait (&global_suspend_semaphore, MONO_SEM_FLAGS_NONE);
-       g_assert (res != -1);
+               int res = mono_os_sem_wait (&global_suspend_semaphore, MONO_SEM_FLAGS_NONE);
+               g_assert (res != -1);
 
-       MONO_EXIT_GC_SAFE_WITH_INFO;
+               MONO_EXIT_GC_SAFE_WITH_INFO;
+       } else {
+               int res = mono_os_sem_wait (&global_suspend_semaphore, MONO_SEM_FLAGS_NONE);
+               g_assert (res != -1);
+       }
 }
 
 void