* loader.c: Fixed bug 79684.
[mono.git] / mono / metadata / gc.c
index c636cbf2dd47256f52e907e7aaad6f2111f01c85..2071f610b3529476814f9fd747e9669fe8217f8a 100644 (file)
@@ -63,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);
@@ -72,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);
@@ -155,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
 }
 
@@ -278,6 +282,13 @@ ves_icall_System_GC_SuppressFinalize (MonoObject *obj)
 {
        MONO_ARCH_SAVE_REGS;
 
+       /* delegates have no finalizers, but we register them to deal with the
+        * unmanaged->managed trampoline. We don't let the user suppress it
+        * otherwise we'd leak it.
+        */
+       if (obj->vtable->klass->delegate)
+               return;
+
        object_register_finalizer (obj, NULL);
 }
 
@@ -471,7 +482,8 @@ alloc_handle (HandleData *handles, MonoObject *obj)
                        memcpy (domain_ids, handles->domain_ids, sizeof (guint16) * handles->size);
                        for (i = 0; i < handles->size; ++i) {
                                MonoObject *obj = mono_gc_weak_link_get (&(handles->entries [i]));
-                               mono_gc_weak_link_remove (&(handles->entries [i]));
+                               if (handles->entries [i])
+                                       mono_gc_weak_link_remove (&(handles->entries [i]));
                                /*g_print ("reg/unreg entry %d of type %d at %p to object %p (%p), was: %p\n", i, handles->type, &(entries [i]), obj, entries [i], handles->entries [i]);*/
                                if (obj) {
                                        mono_gc_weak_link_add (&(entries [i]), obj);
@@ -596,7 +608,8 @@ mono_gchandle_set_target (guint32 gchandle, MonoObject *obj)
        lock_handles (handles);
        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]);
+                       if (handles->entries [slot])
+                               mono_gc_weak_link_remove (&handles->entries [slot]);
                        if (obj)
                                mono_gc_weak_link_add (&handles->entries [slot], obj);
                } else {
@@ -662,10 +675,12 @@ mono_gchandle_free (guint32 gchandle)
                return;
        lock_handles (handles);
        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]);
-               else
+               if (handles->type <= HANDLE_WEAK_TRACK) {
+                       if (handles->entries [slot])
+                               mono_gc_weak_link_remove (&handles->entries [slot]);
+               } else {
                        handles->entries [slot] = NULL;
+               }
                handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
        } else {
                /* print a warning? */
@@ -674,6 +689,43 @@ mono_gchandle_free (guint32 gchandle)
        unlock_handles (handles);
 }
 
+/**
+ * mono_gchandle_free_domain:
+ * @domain: domain that is unloading
+ *
+ * Function used internally to cleanup any GC handle for objects belonging
+ * to the specified domain during appdomain unload.
+ */
+void
+mono_gchandle_free_domain (MonoDomain *domain)
+{
+       guint type;
+
+       for (type = 0; type < 3; ++type) {
+               guint slot;
+               HandleData *handles = &gc_handles [type];
+               lock_handles (handles);
+               for (slot = 0; slot < handles->size; ++slot) {
+                       if (!(handles->bitmap [slot / 32] & (1 << (slot % 32))))
+                               continue;
+                       if (type <= HANDLE_WEAK_TRACK) {
+                               if (domain->domain_id == handles->domain_ids [slot]) {
+                                       handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
+                                       if (handles->entries [slot])
+                                               mono_gc_weak_link_remove (&handles->entries [slot]);
+                               }
+                       } else {
+                               if (handles->entries [slot] && mono_object_domain (handles->entries [slot]) == domain) {
+                                       handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
+                                       handles->entries [slot] = NULL;
+                               }
+                       }
+               }
+               unlock_handles (handles);
+       }
+
+}
+
 #ifndef HAVE_NULL_GC
 
 static HANDLE finalizer_event;
@@ -704,11 +756,12 @@ collect_objects (gpointer key, gpointer value, gpointer user_data)
 static void
 finalize_domain_objects (DomainFinalizationReq *req)
 {
-       int i;
-       GPtrArray *objs;
        MonoDomain *domain = req->domain;
-       
+
+#ifdef HAVE_BOEHM_GC
        while (g_hash_table_size (domain->finalizable_objects_hash) > 0) {
+               int i;
+               GPtrArray *objs;
                /* 
                 * Since the domain is unloading, nobody is allowed to put
                 * new entries into the hash table. But finalize_object might
@@ -727,6 +780,17 @@ finalize_domain_objects (DomainFinalizationReq *req)
 
                g_ptr_array_free (objs, TRUE);
        }
+#elif defined(HAVE_SGEN_GC)
+#define NUM_FOBJECTS 64
+       MonoObject *to_finalize [NUM_FOBJECTS];
+       int count;
+       while ((count = mono_gc_finalizers_for_domain (domain, to_finalize, NUM_FOBJECTS))) {
+               int i;
+               for (i = 0; i < count; ++i) {
+                       run_finalize (to_finalize [i], 0);
+               }
+       }
+#endif
 
        /* Process finalizers which are already in the queue */
        mono_gc_invoke_finalizers ();
@@ -748,7 +812,8 @@ static guint32 finalizer_thread (gpointer unused)
                /* Wait to be notified that there's at least one
                 * finaliser to run
                 */
-               WaitForSingleObjectEx (finalizer_event, INFINITE, TRUE);
+               /* Use alertable=FALSE since we will be asked to exit using the event too */
+               WaitForSingleObjectEx (finalizer_event, INFINITE, FALSE);
 
                if (domains_to_finalize) {
                        mono_finalizer_lock ();
@@ -848,6 +913,10 @@ void mono_gc_cleanup (void)
        }
 
 #endif
+
+       DeleteCriticalSection (&handle_section);
+       DeleteCriticalSection (&allocator_section);
+       DeleteCriticalSection (&finalizer_mutex);
 }
 
 #else