2003-04-24 Martin Baulig <martin@ximian.com>
[mono.git] / mono / metadata / gc.c
index 038de60a23bfe12369522c5e0c420eb3556899b0..9a0dba59c4d36f7543ef3bd987d31444dd6da225 100644 (file)
 #include <glib.h>
 #include <string.h>
 
-#include <mono/metadata/gc.h>
+#include <mono/metadata/gc-internal.h>
 #include <mono/metadata/threads.h>
 #include <mono/metadata/tabledefs.h>
+#include <mono/metadata/exception.h>
 #define GC_I_HIDE_POINTERS
 #include <mono/os/gc_wrapper.h>
 
@@ -25,6 +26,11 @@ static int finalize_slot = -1;
 
 static void object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*));
 
+#if HAVE_BOEHM_GC
+static void finalize_notify (void);
+static HANDLE pending_done_event;
+#endif
+
 /* 
  * actually, we might want to queue the finalize requests in a separate thread,
  * but we need to be careful about the execution domain of the thread...
@@ -47,11 +53,13 @@ run_finalize (void *obj, void *data)
                        }
                }
        }
+
        /* make sure the finalizer is not called again if the object is resurrected */
        object_register_finalizer (obj, NULL);
        /* speedup later... and use a timeout */
        /*g_print ("Finalize run on %p %s.%s\n", o, mono_object_class (o)->name_space, mono_object_class (o)->name);*/
        mono_domain_set (mono_object_domain (o));
+
        mono_runtime_invoke (o->vtable->klass->vtable [finalize_slot], o, NULL, &exc);
 
        if (exc) {
@@ -156,20 +164,31 @@ finalize_static_data (MonoClass *class, MonoVTable *vtable, GHashTable *todo) {
 }
 
 void
-mono_domain_finalize (MonoDomain *domain) {
-
+mono_domain_finalize (MonoDomain *domain) 
+{
        GHashTable *todo = g_hash_table_new (NULL, NULL);
+
+       /* 
+        * No need to create another thread 'cause the finalizer thread
+        * is still working and will take care of running the finalizers
+        */ 
+       mono_thread_new_init (GetCurrentThreadId (), todo, NULL);
+       
 #if HAVE_BOEHM_GC
        GC_gcollect ();
 #endif
        mono_g_hash_table_foreach (domain->class_vtable_hash, (GHFunc)finalize_static_data, todo);
        /* FIXME: finalize objects in todo... */
        g_hash_table_destroy (todo);
+
+       return(0);
 }
 
 void
 ves_icall_System_GC_InternalCollect (int generation)
 {
+       MONO_ARCH_SAVE_REGS;
+
 #if HAVE_BOEHM_GC
        GC_gcollect ();
 #endif
@@ -178,6 +197,8 @@ ves_icall_System_GC_InternalCollect (int generation)
 gint64
 ves_icall_System_GC_GetTotalMemory (MonoBoolean forceCollection)
 {
+       MONO_ARCH_SAVE_REGS;
+
 #if HAVE_BOEHM_GC
        if (forceCollection)
                GC_gcollect ();
@@ -190,6 +211,8 @@ ves_icall_System_GC_GetTotalMemory (MonoBoolean forceCollection)
 void
 ves_icall_System_GC_KeepAlive (MonoObject *obj)
 {
+       MONO_ARCH_SAVE_REGS;
+
        /*
         * Does nothing.
         */
@@ -198,21 +221,39 @@ ves_icall_System_GC_KeepAlive (MonoObject *obj)
 void
 ves_icall_System_GC_ReRegisterForFinalize (MonoObject *obj)
 {
+       MONO_ARCH_SAVE_REGS;
+
        object_register_finalizer (obj, run_finalize);
 }
 
 void
 ves_icall_System_GC_SuppressFinalize (MonoObject *obj)
 {
+       MONO_ARCH_SAVE_REGS;
+
        object_register_finalizer (obj, NULL);
 }
 
 void
 ves_icall_System_GC_WaitForPendingFinalizers (void)
 {
+       MONO_ARCH_SAVE_REGS;
+       
+#if HAVE_BOEHM_GC
+       if (!GC_should_invoke_finalizers ())
+               return;
+
+       ResetEvent (pending_done_event);
+       finalize_notify ();
+       /* g_print ("Waiting for pending finalizers....\n"); */
+       WaitForSingleObject (pending_done_event, INFINITE);
+       /* g_print ("Done pending....\n"); */
+#else
+#endif
 }
 
-/*static CRITICAL_SECTION handle_section;*/
+static CRITICAL_SECTION allocator_section;
+static CRITICAL_SECTION handle_section;
 static guint32 next_handle = 0;
 static gpointer *gc_handles = NULL;
 static guint8 *gc_handle_types = NULL;
@@ -241,17 +282,21 @@ ves_icall_System_GCHandle_GetTarget (guint32 handle)
        MonoObject *obj;
        gint32 type;
 
+       MONO_ARCH_SAVE_REGS;
+
        if (gc_handles) {
                type = handle & 0x3;
+               EnterCriticalSection (&handle_section);
                g_assert (type == gc_handle_types [handle >> 2]);
                obj = gc_handles [handle >> 2];
+               LeaveCriticalSection (&handle_section);
                if (!obj)
                        return NULL;
 
                if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK))
                        return REVEAL_POINTER (obj);
                else
-               return obj;
+                       return obj;
        }
        return NULL;
 }
@@ -260,8 +305,12 @@ guint32
 ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint32 type)
 {
        gpointer val = obj;
-       guint32 h, idx = next_handle++;
+       guint32 h, idx;
 
+       MONO_ARCH_SAVE_REGS;
+
+       EnterCriticalSection (&handle_section);
+       idx = next_handle++;
        if (idx >= array_size) {
 #if HAVE_BOEHM_GC
                gpointer *new_array;
@@ -297,7 +346,7 @@ ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint
                gc_handles = new_array;
                gc_handle_types = new_type_array;
 #else
-               g_error ("No GCHandle support built-in");
+               mono_raise_exception (mono_get_exception_execution_engine ("No GCHandle support built-in"));
 #endif
        }
 
@@ -315,7 +364,7 @@ ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint
                if (gc_handles [idx] != (gpointer)-1)
                        GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(gc_handles [idx]), obj);
 #else
-               g_error ("No weakref support");
+               mono_raise_exception (mono_get_exception_execution_engine ("No weakref support"));
 #endif
                break;
        default:
@@ -323,6 +372,7 @@ ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint
                gc_handle_types [idx] = type;
                break;
        }
+       LeaveCriticalSection (&handle_section);
        return h;
 }
 
@@ -332,6 +382,10 @@ ves_icall_System_GCHandle_FreeHandle (guint32 handle)
        int idx = handle >> 2;
        int type = handle & 0x3;
 
+       MONO_ARCH_SAVE_REGS;
+
+       EnterCriticalSection (&handle_section);
+
 #ifdef HAVE_BOEHM_GC
        g_assert (type == gc_handle_types [idx]);
        if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK)) {
@@ -339,11 +393,12 @@ ves_icall_System_GCHandle_FreeHandle (guint32 handle)
                        GC_unregister_disappearing_link (&(gc_handles [idx]));
        }
 #else
-       g_error ("No GCHandle support");
+       mono_raise_exception (mono_get_exception_execution_engine ("No GCHandle support"));
 #endif
 
        gc_handles [idx] = (gpointer)-1;
        gc_handle_types [idx] = (guint8)-1;
+       LeaveCriticalSection (&handle_section);
 }
 
 gpointer
@@ -352,9 +407,13 @@ ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle)
        MonoObject *obj;
        int type = handle & 0x3;
 
+       MONO_ARCH_SAVE_REGS;
+
        if (gc_handles) {
+               EnterCriticalSection (&handle_section);
                obj = gc_handles [handle >> 2];
                g_assert (gc_handle_types [handle >> 2] == type);
+               LeaveCriticalSection (&handle_section);
                if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK)) {
                        obj = REVEAL_POINTER (obj);
                        if (obj == (MonoObject *) -1)
@@ -365,23 +424,32 @@ ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle)
        return NULL;
 }
 
+#if HAVE_BOEHM_GC
+
 static HANDLE finalizer_event;
 static volatile gboolean finished=FALSE;
 
 static void finalize_notify (void)
 {
+       gboolean pending = GC_should_invoke_finalizers ();
 #ifdef DEBUG
        g_message (G_GNUC_PRETTY_FUNCTION ": prodding finalizer");
 #endif
 
        SetEvent (finalizer_event);
+       if (finished && pending) {
+               /* Finishing the finalizer thread, so wait a little bit... */
+               /* MS seems to wait for about 2 seconds */
+               ResetEvent (pending_done_event);
+               WaitForSingleObject (pending_done_event, 2000);
+       }
 }
 
 static guint32 finalizer_thread (gpointer unused)
 {
        guint32 stack_start;
        
-       mono_new_thread_init (NULL, &stack_start);
+       mono_thread_new_init (GetCurrentThreadId (), &stack_start, NULL);
        
        while(!finished) {
                /* Wait to be notified that there's at least one
@@ -394,34 +462,82 @@ static guint32 finalizer_thread (gpointer unused)
 #endif
 
                GC_invoke_finalizers ();
+               SetEvent (pending_done_event);
        }
        
        return(0);
 }
 
+/* 
+ * Enable or disable the separate finalizer thread.
+ * It's currently disabled because it still requires some
+ * work in the rest of the runtime.
+ */
+#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 */
+
 void mono_gc_init (void)
 {
        HANDLE gc_thread;
 
+       InitializeCriticalSection (&handle_section);
+       InitializeCriticalSection (&allocator_section);
+
+#ifdef WITH_INCLUDED_LIBGC
+       gc_thread_vtable = &mono_gc_thread_vtable;
+#endif
+
+#ifdef ENABLE_FINALIZER_THREAD
+
        if (getenv ("GC_DONT_GC"))
                return;
        
-       finalizer_event=CreateEvent (NULL, FALSE, FALSE, NULL);
-       if(finalizer_event==NULL) {
+       finalizer_event = CreateEvent (NULL, FALSE, FALSE, NULL);
+       pending_done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
+       if (finalizer_event == NULL || pending_done_event == NULL) {
                g_assert_not_reached ();
        }
-       
-       GC_finalize_on_demand=1;
-       GC_finalizer_notifier=finalize_notify;
+
+       GC_finalize_on_demand = 1;
+       GC_finalizer_notifier = finalize_notify;
        
        /* Don't use mono_thread_create here, because we don't want
         * the runtime to wait for this thread to exit when it's
         * cleaning up.
         */
-       gc_thread=CreateThread (NULL, 0, finalizer_thread, NULL, 0, NULL);
-       if(gc_thread==NULL) {
+       gc_thread = CreateThread (NULL, 0, finalizer_thread, NULL, 0, NULL);
+       if (gc_thread == NULL) {
                g_assert_not_reached ();
        }
+#endif
 }
 
 void mono_gc_cleanup (void)
@@ -430,6 +546,23 @@ void mono_gc_cleanup (void)
        g_message (G_GNUC_PRETTY_FUNCTION ": cleaning up finalizer");
 #endif
 
-       finished=TRUE;
+#ifdef ENABLE_FINALIZER_THREAD
+       finished = TRUE;
        finalize_notify ();
+#endif
+}
+
+#else
+
+/* no Boehm GC support. */
+void mono_gc_init (void)
+{
+       InitializeCriticalSection (&handle_section);
+}
+
+void mono_gc_cleanup (void)
+{
 }
+
+#endif
+