2006-05-31 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / gc.c
index b955fd9f900113ae1beb479cd2cd59ec07580f3c..9b91210d194a3ea2e001ab4e11d5bfa0eb7ccb9d 100644 (file)
@@ -36,6 +36,8 @@ extern int __imp_GC_finalize_on_demand;
 
 static gboolean gc_disabled = FALSE;
 
+#define mono_finalizer_lock() EnterCriticalSection (&finalizer_mutex)
+#define mono_finalizer_unlock() LeaveCriticalSection (&finalizer_mutex)
 static CRITICAL_SECTION finalizer_mutex;
 
 static GSList *domains_to_finalize= NULL;
@@ -44,8 +46,7 @@ static MonoThread *gc_thread;
 
 static void object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*));
 
-#if HAVE_BOEHM_GC
-static void finalize_notify (void);
+#ifndef HAVE_NULL_GC
 static HANDLE pending_done_event;
 static HANDLE shutdown_event;
 static HANDLE thread_started_event;
@@ -62,6 +63,7 @@ run_finalize (void *obj, void *data)
        MonoObject *o, *o2;
        o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data));
 
+#ifndef HAVE_SGEN_GC
        mono_domain_lock (o->vtable->domain);
 
        o2 = g_hash_table_lookup (o->vtable->domain->finalizable_objects_hash, o);
@@ -71,6 +73,7 @@ run_finalize (void *obj, void *data)
        if (!o2)
                /* Already finalized somehow */
                return;
+#endif
 
        /* make sure the finalizer is not called again if the object is resurrected */
        object_register_finalizer (obj, NULL);
@@ -154,6 +157,8 @@ object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*))
        mono_domain_unlock (obj->vtable->domain);
 
        GC_REGISTER_FINALIZER_NO_ORDER ((char*)obj - offset, callback, GUINT_TO_POINTER (offset), NULL, NULL);
+#elif defined(HAVE_SGEN_GC)
+       mono_gc_register_for_finalization (obj, callback);
 #endif
 }
 
@@ -201,11 +206,11 @@ mono_domain_finalize (MonoDomain *domain, guint32 timeout)
         * is still working and will take care of running the finalizers
         */ 
        
-#if HAVE_BOEHM_GC
+#ifndef HAVE_NULL_GC
        if (gc_disabled)
                return TRUE;
 
-       GC_gcollect ();
+       mono_gc_collect (mono_gc_max_generation ());
 
        done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
 
@@ -213,14 +218,14 @@ mono_domain_finalize (MonoDomain *domain, guint32 timeout)
        req->domain = domain;
        req->done_event = done_event;
        
-       EnterCriticalSection (&finalizer_mutex);
+       mono_finalizer_lock ();
 
        domains_to_finalize = g_slist_append (domains_to_finalize, req);
 
-       LeaveCriticalSection (&finalizer_mutex);
+       mono_finalizer_unlock ();
 
        /* Tell the finalizer thread to finalize this appdomain */
-       finalize_notify ();
+       mono_gc_finalize_notify ();
 
        res = WaitForSingleObjectEx (done_event, timeout, TRUE);
 
@@ -285,8 +290,8 @@ ves_icall_System_GC_WaitForPendingFinalizers (void)
 {
        MONO_ARCH_SAVE_REGS;
        
-#if HAVE_BOEHM_GC
-       if (!GC_should_invoke_finalizers ())
+#ifndef HAVE_NULL_GC
+       if (!mono_gc_pending_finalizers ())
                return;
 
        if (mono_thread_current () == gc_thread)
@@ -294,14 +299,15 @@ ves_icall_System_GC_WaitForPendingFinalizers (void)
                return;
 
        ResetEvent (pending_done_event);
-       finalize_notify ();
+       mono_gc_finalize_notify ();
        /* g_print ("Waiting for pending finalizers....\n"); */
        WaitForSingleObjectEx (pending_done_event, INFINITE, TRUE);
        /* g_print ("Done pending....\n"); */
 #else
 #endif
 }
-
+#define mono_allocator_lock() EnterCriticalSection (&allocator_section)
+#define mono_allocator_unlock() LeaveCriticalSection (&allocator_section)
 static CRITICAL_SECTION allocator_section;
 static CRITICAL_SECTION handle_section;
 
@@ -662,7 +668,8 @@ mono_gchandle_free (guint32 gchandle)
        if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
                if (handles->type <= HANDLE_WEAK_TRACK)
                        mono_gc_weak_link_remove (&handles->entries [slot]);
-               handles->entries [slot] = NULL;
+               else
+                       handles->entries [slot] = NULL;
                handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
        } else {
                /* print a warning? */
@@ -671,12 +678,13 @@ mono_gchandle_free (guint32 gchandle)
        unlock_handles (handles);
 }
 
-#if HAVE_BOEHM_GC
+#ifndef HAVE_NULL_GC
 
 static HANDLE finalizer_event;
 static volatile gboolean finished=FALSE;
 
-static void finalize_notify (void)
+void
+mono_gc_finalize_notify (void)
 {
 #ifdef DEBUG
        g_message (G_GNUC_PRETTY_FUNCTION ": prodding finalizer");
@@ -725,7 +733,7 @@ finalize_domain_objects (DomainFinalizationReq *req)
        }
 
        /* Process finalizers which are already in the queue */
-       GC_invoke_finalizers ();
+       mono_gc_invoke_finalizers ();
 
        /* printf ("DONE.\n"); */
        SetEvent (req->done_event);
@@ -747,16 +755,16 @@ static guint32 finalizer_thread (gpointer unused)
                WaitForSingleObjectEx (finalizer_event, INFINITE, TRUE);
 
                if (domains_to_finalize) {
-                       EnterCriticalSection (&finalizer_mutex);
+                       mono_finalizer_lock ();
                        if (domains_to_finalize) {
                                DomainFinalizationReq *req = domains_to_finalize->data;
                                domains_to_finalize = g_slist_remove (domains_to_finalize, req);
-                               LeaveCriticalSection (&finalizer_mutex);
+                               mono_finalizer_unlock ();
 
                                finalize_domain_objects (req);
                        }
                        else
-                               LeaveCriticalSection (&finalizer_mutex);
+                               mono_finalizer_unlock ();
                }                               
 
 #ifdef DEBUG
@@ -765,15 +773,8 @@ static guint32 finalizer_thread (gpointer unused)
 
                /* If finished == TRUE, mono_gc_cleanup has been called (from mono_runtime_cleanup),
                 * before the domain is unloaded.
-                *
-                * There is a bug in GC_invoke_finalizer () in versions <= 6.2alpha4:
-                * the 'mem_freed' variable is not initialized when there are no
-                * objects to finalize, which leads to strange behavior later on.
-                * The check is necessary to work around that bug.
                 */
-               if (GC_should_invoke_finalizers ()) {
-                       GC_invoke_finalizers ();
-               }
+               mono_gc_invoke_finalizers ();
 
                SetEvent (pending_done_event);
        }
@@ -789,59 +790,18 @@ static guint32 finalizer_thread (gpointer unused)
  */
 #define ENABLE_FINALIZER_THREAD
 
-#ifdef WITH_INCLUDED_LIBGC
-/* from threads.c */
-extern void mono_gc_stop_world (void);
-extern void mono_gc_start_world (void);
-extern void mono_gc_push_all_stacks (void);
-
-static void mono_gc_lock (void)
-{
-       EnterCriticalSection (&allocator_section);
-}
-
-static void mono_gc_unlock (void)
-{
-       LeaveCriticalSection (&allocator_section);
-}
-
-static GCThreadFunctions mono_gc_thread_vtable = {
-       NULL,
-
-       mono_gc_lock,
-       mono_gc_unlock,
-
-       mono_gc_stop_world,
-       NULL,
-       mono_gc_push_all_stacks,
-       mono_gc_start_world
-};
-#endif /* WITH_INCLUDED_LIBGC */
-
-static void
-mono_gc_warning (char *msg, GC_word arg)
-{
-       mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_GC, msg, (unsigned long)arg);
-}
-
-void mono_gc_init (void)
+void
+mono_gc_init (void)
 {
        InitializeCriticalSection (&handle_section);
        InitializeCriticalSection (&allocator_section);
 
        InitializeCriticalSection (&finalizer_mutex);
 
-#ifdef WITH_INCLUDED_LIBGC
-       gc_thread_vtable = &mono_gc_thread_vtable;
-#endif
-       
        MONO_GC_REGISTER_ROOT (gc_handles [HANDLE_NORMAL].entries);
        MONO_GC_REGISTER_ROOT (gc_handles [HANDLE_PINNED].entries);
-       GC_no_dls = TRUE;
 
-       GC_oom_fn = mono_gc_out_of_memory;
-
-       GC_set_warn_proc (mono_gc_warning);
+       mono_gc_base_init ();
 
 #ifdef ENABLE_FINALIZER_THREAD
 
@@ -858,9 +818,6 @@ void mono_gc_init (void)
                g_assert_not_reached ();
        }
 
-       GC_finalize_on_demand = 1;
-       GC_finalizer_notifier = finalize_notify;
-
        mono_thread_create (mono_domain_get (), finalizer_thread, NULL);
        /*
         * Wait until the finalizer thread sets gc_thread since its value is needed
@@ -881,7 +838,7 @@ void mono_gc_cleanup (void)
                ResetEvent (shutdown_event);
                finished = TRUE;
                if (mono_thread_current () != gc_thread) {
-                       finalize_notify ();
+                       mono_gc_finalize_notify ();
                        /* Finishing the finalizer thread, so wait a little bit... */
                        /* MS seems to wait for about 2 seconds */
                        if (WaitForSingleObjectEx (shutdown_event, 2000, FALSE) == WAIT_TIMEOUT) {
@@ -889,7 +846,9 @@ void mono_gc_cleanup (void)
                        }
                }
                gc_thread = NULL;
+#ifdef HAVE_BOEHM_GC
                GC_finalizer_notifier = NULL;
+#endif
        }
 
 #endif