2006-05-31 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / gc.c
index 00fecb0bd33c658e896e75293f60bc3290b68252..9b91210d194a3ea2e001ab4e11d5bfa0eb7ccb9d 100644 (file)
@@ -46,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;
@@ -64,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);
@@ -73,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);
@@ -156,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
 }
 
@@ -203,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);
 
@@ -222,7 +225,7 @@ mono_domain_finalize (MonoDomain *domain, guint32 timeout)
        mono_finalizer_unlock ();
 
        /* Tell the finalizer thread to finalize this appdomain */
-       finalize_notify ();
+       mono_gc_finalize_notify ();
 
        res = WaitForSingleObjectEx (done_event, timeout, TRUE);
 
@@ -287,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)
@@ -296,7 +299,7 @@ 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"); */
@@ -675,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");
@@ -729,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);
@@ -769,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);
        }
@@ -793,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)
-{
-       mono_allocator_lock ();
-}
-
-static void mono_gc_unlock (void)
-{
-       mono_allocator_unlock ();
-}
-
-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
 
@@ -862,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
@@ -885,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) {
@@ -893,7 +846,9 @@ void mono_gc_cleanup (void)
                        }
                }
                gc_thread = NULL;
+#ifdef HAVE_BOEHM_GC
                GC_finalizer_notifier = NULL;
+#endif
        }
 
 #endif