This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mono / metadata / gc.c
index 22208b1925fd7b71191bbc8db98da6c8fe8e84bb..3a9db3a1c62518de346ad3b6bbdca7abc4ebd28b 100644 (file)
@@ -14,6 +14,9 @@
 #include <mono/metadata/threads.h>
 #include <mono/metadata/tabledefs.h>
 #include <mono/metadata/exception.h>
+#include <mono/metadata/domain-internals.h>
+#include <mono/metadata/class-internals.h>
+#include <mono/utils/mono-logger.h>
 #define GC_I_HIDE_POINTERS
 #include <mono/os/gc_wrapper.h>
 
@@ -66,10 +69,11 @@ run_finalize (void *obj, void *data)
 
        if (finalize_slot < 0) {
                int i;
-               for (i = 0; i < mono_defaults.object_class->vtable_size; ++i) {
-                       MonoMethod *cm = mono_defaults.object_class->vtable [i];
+               MonoClass* obj_class = mono_get_object_class ();
+               for (i = 0; i < obj_class->vtable_size; ++i) {
+                       MonoMethod *cm = obj_class->vtable [i];
               
-                       if (!strcmp (cm->name, "Finalize")) {
+                       if (!strcmp (mono_method_get_name (cm), "Finalize")) {
                                finalize_slot = i;
                                break;
                        }
@@ -89,7 +93,7 @@ run_finalize (void *obj, void *data)
        /* make sure the finalizer is not called again if the object is resurrected */
        object_register_finalizer (obj, NULL);
 
-       if (o->vtable->klass == mono_defaults.thread_class)
+       if (o->vtable->klass == mono_get_thread_class ())
                if (mono_gc_is_finalizer_thread ((MonoThread*)o))
                        /* Avoid finalizing ourselves */
                        return;
@@ -195,14 +199,16 @@ mono_domain_finalize (MonoDomain *domain, guint32 timeout)
        /* Tell the finalizer thread to finalize this appdomain */
        finalize_notify ();
 
-       res = WaitForSingleObject (done_event, timeout);
+       res = WaitForSingleObjectEx (done_event, timeout, TRUE);
 
-       CloseHandle (done_event);
-       //printf ("WAIT RES: %d.\n", res);
-       if (res == WAIT_TIMEOUT)
+       /* printf ("WAIT RES: %d.\n", res); */
+       if (res == WAIT_TIMEOUT) {
+               /* We leak the handle here */
                return FALSE;
-       else
-               return TRUE;
+       }
+
+       CloseHandle (done_event);
+       return TRUE;
 #else
        /* We don't support domain finalization without a GC */
        return FALSE;
@@ -275,7 +281,7 @@ ves_icall_System_GC_WaitForPendingFinalizers (void)
        ResetEvent (pending_done_event);
        finalize_notify ();
        /* g_print ("Waiting for pending finalizers....\n"); */
-       WaitForSingleObject (pending_done_event, INFINITE);
+       WaitForSingleObjectEx (pending_done_event, INFINITE, TRUE);
        /* g_print ("Done pending....\n"); */
 #else
 #endif
@@ -342,13 +348,17 @@ ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint
        /* Indexes start from 1 since 0 means the handle is not allocated */
        idx = ++next_handle;
        if (idx >= array_size) {
-#if HAVE_BOEHM_GC
                gpointer *new_array;
                guint8 *new_type_array;
                if (!array_size)
                        array_size = 16;
+#if HAVE_BOEHM_GC
                new_array = GC_MALLOC (sizeof (gpointer) * (array_size * 2));
                new_type_array = GC_MALLOC (sizeof (guint8) * (array_size * 2));
+#else
+               new_array = g_malloc0 (sizeof (gpointer) * (array_size * 2));
+               new_type_array = g_malloc0 (sizeof (guint8) * (array_size * 2));
+#endif
                if (gc_handles) {
                        int i;
                        memcpy (new_array, gc_handles, sizeof (gpointer) * array_size);
@@ -365,19 +375,22 @@ ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint
 #else
                                if (((gulong)new_array [i]) & 0x1) {
 #endif
+#if HAVE_BOEHM_GC
                                        if (gc_handles [i] != (gpointer)-1)
                                                GC_unregister_disappearing_link (&(gc_handles [i]));
                                        if (new_array [i] != (gpointer)-1)
                                                GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(new_array [i]), REVEAL_POINTER (new_array [i]));
+#endif
                                }
                        }
                }
                array_size *= 2;
+#ifndef HAVE_BOEHM_GC
+               g_free (gc_handles);
+               g_free (gc_handle_types);
+#endif
                gc_handles = new_array;
                gc_handle_types = new_type_array;
-#else
-               mono_raise_exception (mono_get_exception_execution_engine ("No GCHandle support built-in"));
-#endif
        }
 
        /* resuse the type from the old target */
@@ -393,8 +406,6 @@ ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint
 #if HAVE_BOEHM_GC
                if (gc_handles [idx] != (gpointer)-1)
                        GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(gc_handles [idx]), obj);
-#else
-               mono_raise_exception (mono_get_exception_execution_engine ("No weakref support"));
 #endif
                break;
        default:
@@ -422,9 +433,6 @@ ves_icall_System_GCHandle_FreeHandle (guint32 handle)
                if (gc_handles [idx] != (gpointer)-1)
                        GC_unregister_disappearing_link (&(gc_handles [idx]));
        }
-#else
-       LeaveCriticalSection (&handle_section);
-       mono_raise_exception (mono_get_exception_execution_engine ("No GCHandle support"));
 #endif
 
        gc_handles [idx] = (gpointer)-1;
@@ -450,11 +458,50 @@ ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle)
                        if (obj == (MonoObject *) -1)
                                return NULL;
                }
-               return obj;
+               if (obj) {
+                       MonoClass *klass = mono_object_class (obj);
+                       if (klass == mono_defaults.string_class) {
+                               return mono_string_chars ((MonoString*)obj);
+                       } else if (klass->rank) {
+                               return mono_array_addr ((MonoArray*)obj, char, 0);
+                       } else {
+                               /* the C# code will check and throw the exception */
+                               /* FIXME: missing !klass->blittable test, see bug #61134,
+                                * disabled in 1.0 untill the blittable-using code is audited.
+                               if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT)
+                                       return (gpointer)-1; */
+                               return (char*)obj + sizeof (MonoObject);
+                       }
+               }
        }
        return NULL;
 }
 
+guint32
+mono_gchandle_new (MonoObject *obj, gboolean pinned)
+{
+       return ves_icall_System_GCHandle_GetTargetHandle (obj, 0, pinned? HANDLE_PINNED: HANDLE_NORMAL);
+}
+
+guint32
+mono_gchandle_new_weakref (MonoObject *obj, gboolean track_resurrection)
+{
+       return ves_icall_System_GCHandle_GetTargetHandle (obj, 0, track_resurrection? HANDLE_WEAK_TRACK: HANDLE_WEAK);
+}
+
+/* This will return NULL for a collected object if using a weakref handle */
+MonoObject*
+mono_gchandle_get_target (guint32 gchandle)
+{
+       return ves_icall_System_GCHandle_GetTarget (gchandle);
+}
+
+void
+mono_gchandle_free (guint32 gchandle)
+{
+       ves_icall_System_GCHandle_FreeHandle (gchandle);
+}
+
 #if HAVE_BOEHM_GC
 
 static HANDLE finalizer_event;
@@ -487,7 +534,7 @@ finalize_domain_objects (DomainFinalizationReq *req)
        int i;
        GPtrArray *objs;
        MonoDomain *domain = req->domain;
-
+       
        while (g_hash_table_size (domain->finalizable_objects_hash) > 0) {
                /* 
                 * Since the domain is unloading, nobody is allowed to put
@@ -497,7 +544,7 @@ finalize_domain_objects (DomainFinalizationReq *req)
                objs = g_ptr_array_new ();
                g_hash_table_foreach (domain->finalizable_objects_hash, 
                                                          collect_objects, objs);
-               //printf ("FINALIZING %d OBJECTS.\n", objs->len);
+               /* printf ("FINALIZING %d OBJECTS.\n", objs->len); */
 
                for (i = 0; i < objs->len; ++i) {
                        MonoObject *o = (MonoObject*)g_ptr_array_index (objs, i);
@@ -508,10 +555,10 @@ finalize_domain_objects (DomainFinalizationReq *req)
                g_ptr_array_free (objs, TRUE);
        }
 
-       //printf ("DONE.\n");
+       /* printf ("DONE.\n"); */
        SetEvent (req->done_event);
 
-       /* FIXME: How to delete the event ? */
+       /* The event is closed in mono_domain_finalize if we get here */
        g_free (req);
 }
 
@@ -525,7 +572,7 @@ static guint32 finalizer_thread (gpointer unused)
                /* Wait to be notified that there's at least one
                 * finaliser to run
                 */
-               WaitForSingleObject (finalizer_event, INFINITE);
+               WaitForSingleObjectEx (finalizer_event, INFINITE, TRUE);
 
                if (domains_to_finalize) {
                        EnterCriticalSection (&finalizer_mutex);
@@ -560,7 +607,6 @@ static guint32 finalizer_thread (gpointer unused)
        }
 
        SetEvent (shutdown_event);
-       
        return(0);
 }
 
@@ -600,6 +646,12 @@ static GCThreadFunctions mono_gc_thread_vtable = {
 };
 #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)
 {
        InitializeCriticalSection (&handle_section);
@@ -611,9 +663,11 @@ void mono_gc_init (void)
        gc_thread_vtable = &mono_gc_thread_vtable;
 #endif
 
+       GC_set_warn_proc (mono_gc_warning);
+       
 #ifdef ENABLE_FINALIZER_THREAD
 
-       if (getenv ("GC_DONT_GC")) {
+       if (g_getenv ("GC_DONT_GC")) {
                gc_disabled = TRUE;
                return;
        }
@@ -634,7 +688,7 @@ void mono_gc_init (void)
         * Wait until the finalizer thread sets gc_thread since its value is needed
         * by mono_thread_attach ()
         */
-       WaitForSingleObject (thread_started_event, INFINITE);
+       WaitForSingleObjectEx (thread_started_event, INFINITE, FALSE);
 #endif
 }
 
@@ -651,11 +705,9 @@ void mono_gc_cleanup (void)
                finalize_notify ();
                /* Finishing the finalizer thread, so wait a little bit... */
                /* MS seems to wait for about 2 seconds */
-               /* 
-                * FIXME: This is not thread safe. If the finalizer thread keeps
-                * running, and the runtime is shut down, it will lead to a crash.
-                */
-               WaitForSingleObject (shutdown_event, 2000);
+               if (WaitForSingleObjectEx (shutdown_event, 2000, FALSE) == WAIT_TIMEOUT) {
+                       mono_thread_stop (gc_thread);
+               }
        }
 
 #endif