Merge pull request #2247 from ivmai/match-ext-libgc-api
[mono.git] / mono / metadata / boehm-gc.c
index 540c2b94cf647416e3ff835678b41e4d44454795..0401c5cf2d777df2b013c8ec612d2a7842e17156 100644 (file)
@@ -11,7 +11,7 @@
 #include <string.h>
 
 #define GC_I_HIDE_POINTERS
-#include <mono/metadata/gc-internal.h>
+#include <mono/metadata/gc-internals.h>
 #include <mono/metadata/mono-gc.h>
 #include <mono/metadata/profiler-private.h>
 #include <mono/metadata/class-internals.h>
 #include <mono/metadata/runtime.h>
 #include <mono/metadata/sgen-toggleref.h>
 #include <mono/utils/atomic.h>
-#include <mono/utils/mono-logger-internal.h>
+#include <mono/utils/mono-logger-internals.h>
 #include <mono/utils/mono-memory-model.h>
 #include <mono/utils/mono-time.h>
 #include <mono/utils/mono-threads.h>
 #include <mono/utils/dtrace.h>
 #include <mono/utils/gc_wrapper.h>
-#include <mono/utils/mono-mutex.h>
+#include <mono/utils/mono-os-mutex.h>
 #include <mono/utils/mono-counters.h>
 
 #if HAVE_BOEHM_GC
@@ -64,8 +64,8 @@ static MonoGCFinalizerCallbacks fin_callbacks;
 /* GC Handles */
 
 static mono_mutex_t handle_section;
-#define lock_handles(handles) mono_mutex_lock (&handle_section)
-#define unlock_handles(handles) mono_mutex_unlock (&handle_section)
+#define lock_handles(handles) mono_os_mutex_lock (&handle_section)
+#define unlock_handles(handles) mono_os_mutex_unlock (&handle_section)
 
 typedef struct {
        guint32  *bitmap;
@@ -180,7 +180,7 @@ mono_gc_base_init (void)
                        for (char **ptr = opts; ptr && *ptr; ptr ++) {
                                char *opt = *ptr;
                                if (!strcmp (opt, "do-not-finalize")) {
-                                       do_not_finalize = 1;
+                                       mono_do_not_finalize = 1;
                                } else if (!strcmp (opt, "log-finalizers")) {
                                        log_finalizers = 1;
                                }
@@ -196,6 +196,7 @@ mono_gc_base_init (void)
        GC_finalizer_notifier = mono_gc_finalize_notify;
 
        GC_init_gcj_malloc (5, NULL);
+       GC_allow_register_threads ();
 
        if ((env = g_getenv ("MONO_GC_PARAMS"))) {
                char **ptr, **opts = g_strsplit (env, ",", -1);
@@ -237,8 +238,8 @@ mono_gc_base_init (void)
        cb.mono_method_is_critical = (gpointer)mono_runtime_is_critical_method;
 
        mono_threads_init (&cb, sizeof (MonoThreadInfo));
-       mono_mutex_init (&mono_gc_lock);
-       mono_mutex_init_recursive (&handle_section);
+       mono_os_mutex_init (&mono_gc_lock);
+       mono_os_mutex_init_recursive (&handle_section);
 
        mono_thread_info_attach (&dummy);
 
@@ -370,8 +371,6 @@ mono_gc_is_gc_thread (void)
        return GC_thread_is_registered ();
 }
 
-extern int GC_thread_register_foreign (void *base_addr);
-
 gboolean
 mono_gc_register_thread (void *baseptr)
 {
@@ -381,13 +380,15 @@ mono_gc_register_thread (void *baseptr)
 static void*
 boehm_thread_register (MonoThreadInfo* info, void *baseptr)
 {
-       if (mono_gc_is_gc_thread())
-               return info;
-#if !defined(HOST_WIN32)
-       return GC_thread_register_foreign (baseptr) ? info : NULL;
-#else
-       return NULL;
-#endif
+       struct GC_stack_base sb;
+       int res;
+
+       /* TODO: use GC_get_stack_base instead of baseptr. */
+       sb.mem_base = baseptr;
+       res = GC_register_my_thread (&sb);
+       if (res == GC_UNIMPLEMENTED)
+           return NULL; /* Cannot happen with GC v7+. */
+       return info;
 }
 
 static void
@@ -416,7 +417,7 @@ mono_gc_walk_heap (int flags, MonoGCReferences callback, void *data)
 static gint64 gc_start_time;
 
 static void
-on_gc_notification (GCEventType event)
+on_gc_notification (GC_EventType event)
 {
        MonoGCEvent e = (MonoGCEvent)event;
 
@@ -495,7 +496,7 @@ on_gc_heap_resize (size_t new_size)
 void
 mono_gc_enable_events (void)
 {
-       GC_notify_event = on_gc_notification;
+       GC_set_on_collection_event (on_gc_notification);
        GC_on_heap_resize = on_gc_heap_resize;
 }
 
@@ -805,7 +806,7 @@ mono_gc_get_suspend_signal (void)
 int
 mono_gc_get_restart_signal (void)
 {
-       return GC_get_restart_signal ();
+       return GC_get_thr_restart_signal ();
 }
 
 #if defined(USE_COMPILER_TLS) && defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
@@ -840,7 +841,7 @@ create_allocator (int atype, int tls_key, gboolean slowpath)
        MonoMethod *res;
        MonoMethodSignature *csig;
        const char *name = NULL;
-       AllocatorWrapperInfo *info;
+       WrapperInfo *info;
 
        if (atype == ATYPE_FREEPTR) {
                name = slowpath ? "SlowAllocPtrfree" : "AllocPtrfree";
@@ -924,11 +925,17 @@ create_allocator (int atype, int tls_key, gboolean slowpath)
        mono_mb_emit_byte (mb, 0x0D); /* CEE_MONO_TLS */
        mono_mb_emit_i4 (mb, tls_key);
        if (atype == ATYPE_FREEPTR || atype == ATYPE_FREEPTR_FOR_BOX || atype == ATYPE_STRING)
-               mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, ptrfree_freelists));
+               mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, tlfs)
+                                       + G_STRUCT_OFFSET (struct thread_local_freelists,
+                                                          ptrfree_freelists));
        else if (atype == ATYPE_NORMAL)
-               mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, normal_freelists));
+               mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, tlfs)
+                                       + G_STRUCT_OFFSET (struct thread_local_freelists,
+                                                          normal_freelists));
        else if (atype == ATYPE_GCJ)
-               mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, gcj_freelists));
+               mono_mb_emit_icon (mb, G_STRUCT_OFFSET (struct GC_Thread_Rep, tlfs)
+                                       + G_STRUCT_OFFSET (struct thread_local_freelists,
+                                                          gcj_freelists));
        else
                g_assert_not_reached ();
        mono_mb_emit_byte (mb, MONO_CEE_ADD);
@@ -1038,15 +1045,14 @@ create_allocator (int atype, int tls_key, gboolean slowpath)
 
        mono_mb_emit_byte (mb, MONO_CEE_RET);
 
-       res = mono_mb_create_method (mb, csig, 8);
+       info = mono_wrapper_info_create (mb, WRAPPER_SUBTYPE_NONE);
+       info->d.alloc.gc_name = "boehm";
+       info->d.alloc.alloc_type = atype;
+
+       res = mono_mb_create (mb, csig, 8, info);
        mono_mb_free (mb);
        mono_method_get_header (res)->init_locals = FALSE;
 
-       info = mono_image_alloc0 (mono_defaults.corlib, sizeof (AllocatorWrapperInfo));
-       info->gc_name = "boehm";
-       info->alloc_type = atype;
-       mono_marshal_set_wrapper_info (res, info);
-
        return res;
 }
 
@@ -1142,7 +1148,7 @@ mono_gc_get_managed_allocator_by_type (int atype, gboolean slowpath)
                return res;
 
        res = create_allocator (atype, TLS_KEY_BOEHM_GC_THREAD, slowpath);
-       mono_mutex_lock (&mono_gc_lock);
+       mono_os_mutex_lock (&mono_gc_lock);
        if (cache [atype]) {
                mono_free_method (res);
                res = cache [atype];
@@ -1150,7 +1156,7 @@ mono_gc_get_managed_allocator_by_type (int atype, gboolean slowpath)
                mono_memory_barrier ();
                cache [atype] = res;
        }
-       mono_mutex_unlock (&mono_gc_lock);
+       mono_os_mutex_unlock (&mono_gc_lock);
        return res;
 }
 
@@ -1369,10 +1375,10 @@ BOOL APIENTRY mono_gc_dllmain (HMODULE module_handle, DWORD reason, LPVOID reser
 #endif
 
 guint
-mono_gc_get_vtable_bits (MonoClass *class)
+mono_gc_get_vtable_bits (MonoClass *klass)
 {
        if (fin_callbacks.is_class_finalization_aware) {
-               if (fin_callbacks.is_class_finalization_aware (class))
+               if (fin_callbacks.is_class_finalization_aware (klass))
                        return BOEHM_GC_BIT_FINALIZER_AWARE;
        }
        return 0;
@@ -1431,13 +1437,14 @@ mono_gc_set_allow_synchronous_major (gboolean flag)
 void
 mono_gc_toggleref_add (MonoObject *object, mono_bool strong_ref)
 {
-       GC_toggleref_add ((GC_PTR)object, (int)strong_ref);
+       if (GC_toggleref_add ((GC_PTR)object, (int)strong_ref) != GC_SUCCESS)
+           g_error ("GC_toggleref_add failed\n");
 }
 
 void
 mono_gc_toggleref_register_callback (MonoToggleRefStatus (*proccess_toggleref) (MonoObject *obj))
 {
-       GC_toggleref_register_callback ((int (*) (GC_PTR obj)) proccess_toggleref);
+       GC_set_toggleref_func ((GC_ToggleRefStatus (*) (GC_PTR obj)) proccess_toggleref);
 }
 
 /* Test support code */
@@ -1486,7 +1493,7 @@ mono_gc_register_finalizer_callbacks (MonoGCFinalizerCallbacks *callbacks)
 
        fin_callbacks = *callbacks;
 
-       GC_set_finalizer_notify_proc ((void (*) (GC_PTR))fin_notifier);
+       GC_set_await_finalize_proc ((void (*) (GC_PTR))fin_notifier);
 }
 
 #define BITMAP_SIZE (sizeof (*((HandleData *)NULL)->bitmap) * CHAR_BIT)