*
* Author: Paolo Molaro <lupus@ximian.com>
*
- * (C) 2002 Ximian, Inc.
+ * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
*/
#include <config.h>
#include <mono/metadata/profiler-private.h>
#include <mono/metadata/domain-internals.h>
#include <mono/metadata/class-internals.h>
+#include <mono/metadata/metadata-internals.h>
#include <mono/metadata/mono-mlist.h>
#include <mono/metadata/threadpool.h>
+#include <mono/metadata/threads-types.h>
#include <mono/utils/mono-logger.h>
#include <mono/metadata/gc-internal.h>
#include <mono/metadata/marshal.h> /* for mono_delegate_free_ftnptr () */
* has the header and functions, but they don't work at all): in other cases
* we fall back to the io-layer slightly slower and signal-unsafe Event.
*/
-#ifdef __linux__
+#if defined (__linux__) || (defined(__APPLE__) && defined(__arm__))
#define USE_POSIX_SEM 1
#endif
#endif
static void object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*));
+static void mono_gchandle_set_target (guint32 gchandle, MonoObject *obj);
+
#ifndef HAVE_NULL_GC
static HANDLE pending_done_event;
static HANDLE shutdown_event;
-static HANDLE thread_started_event;
#endif
static void
* 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...
*/
-static void
-run_finalize (void *obj, void *data)
+void
+mono_gc_run_finalize (void *obj, void *data)
{
MonoObject *exc = NULL;
- MonoObject *o, *o2;
+ MonoObject *o;
+#ifndef HAVE_SGEN_GC
+ MonoObject *o2;
+#endif
MonoMethod* finalizer = NULL;
+ MonoDomain *caller_domain = mono_domain_get ();
+ MonoDomain *domain;
+ RuntimeInvokeFunction runtime_invoke;
+ GSList *l, *refs = NULL;
+
o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data));
if (suspend_finalizers)
return;
+ domain = o->vtable->domain;
+
#ifndef HAVE_SGEN_GC
- mono_domain_lock (o->vtable->domain);
+ mono_domain_finalizers_lock (domain);
+
+ o2 = g_hash_table_lookup (domain->finalizable_objects_hash, o);
- o2 = g_hash_table_lookup (o->vtable->domain->finalizable_objects_hash, o);
+ refs = mono_gc_remove_weak_track_object (domain, o);
- mono_domain_unlock (o->vtable->domain);
+ mono_domain_finalizers_unlock (domain);
if (!o2)
/* Already finalized somehow */
return;
#endif
+ if (refs) {
+ /*
+ * Support for GCHandles of type WeakTrackResurrection:
+ *
+ * Its not exactly clear how these are supposed to work, or how their
+ * semantics can be implemented. We only implement one crucial thing:
+ * these handles are only cleared after the finalizer has ran.
+ */
+ for (l = refs; l; l = l->next) {
+ guint32 gchandle = GPOINTER_TO_UINT (l->data);
+
+ mono_gchandle_set_target (gchandle, o);
+ }
+
+ g_slist_free (refs);
+ }
+
/* make sure the finalizer is not called again if the object is resurrected */
object_register_finalizer (obj, NULL);
/* g_print ("Finalize run on %p %s.%s\n", o, mono_object_class (o)->name_space, mono_object_class (o)->name); */
/* Use _internal here, since this thread can enter a doomed appdomain */
- mono_domain_set_internal (mono_object_domain (o));
+ mono_domain_set_internal (mono_object_domain (o));
/* delegates that have a native function pointer allocated are
* registered for finalization, but they don't have a Finalize
finalizer = mono_class_get_finalizer (o->vtable->klass);
+#ifndef DISABLE_COM
/* If object has a CCW but has no finalizer, it was only
* registered for finalization in order to free the CCW.
* Else it needs the regular finalizer run.
* FIXME: what to do about ressurection and suppression
* of finalizer on object with CCW.
*/
- if (mono_marshal_free_ccw (o) && !finalizer)
+ if (mono_marshal_free_ccw (o) && !finalizer) {
+ mono_domain_set_internal (caller_domain);
return;
+ }
+#endif
+
+ /*
+ * To avoid the locking plus the other overhead of mono_runtime_invoke (),
+ * create and precompile a wrapper which calls the finalize method using
+ * a CALLVIRT.
+ */
+ if (!domain->finalize_runtime_invoke) {
+ MonoMethod *invoke = mono_marshal_get_runtime_invoke (mono_class_get_method_from_name_flags (mono_defaults.object_class, "Finalize", 0, 0), TRUE);
+
+ domain->finalize_runtime_invoke = mono_compile_method (invoke);
+ }
+
+ runtime_invoke = domain->finalize_runtime_invoke;
+
+ mono_runtime_class_init (o->vtable);
- mono_runtime_invoke (finalizer, o, NULL, &exc);
+ runtime_invoke (o, NULL, &exc, NULL);
if (exc) {
/* fixme: do something useful */
}
+
+ mono_domain_set_internal (caller_domain);
}
void
thread->threadpool_thread = FALSE;
mono_object_register_finalizer ((MonoObject*)thread);
- run_finalize (thread, NULL);
+ mono_gc_run_finalize (thread, NULL);
threads_to_finalize = mono_mlist_next (threads_to_finalize);
}
*/
return;
- mono_domain_lock (domain);
+ mono_domain_finalizers_lock (domain);
if (callback)
g_hash_table_insert (domain->finalizable_objects_hash, obj, obj);
else
g_hash_table_remove (domain->finalizable_objects_hash, obj);
- mono_domain_unlock (domain);
+ mono_domain_finalizers_unlock (domain);
GC_REGISTER_FINALIZER_NO_ORDER ((char*)obj - offset, callback, GUINT_TO_POINTER (offset), NULL, NULL);
#elif defined(HAVE_SGEN_GC)
mono_object_register_finalizer (MonoObject *obj)
{
/* g_print ("Registered finalizer on %p %s.%s\n", obj, mono_object_class (obj)->name_space, mono_object_class (obj)->name); */
- object_register_finalizer (obj, run_finalize);
+ object_register_finalizer (obj, mono_gc_run_finalize);
}
/**
{
MONO_ARCH_SAVE_REGS;
- object_register_finalizer (obj, run_finalize);
+ object_register_finalizer (obj, mono_gc_run_finalize);
}
void
HANDLE_PINNED
} HandleType;
-static void mono_gchandle_set_target (guint32 gchandle, MonoObject *obj);
-
static HandleType mono_gchandle_get_type (guint32 gchandle);
MonoObject *
}
static guint32
-alloc_handle (HandleData *handles, MonoObject *obj)
+alloc_handle (HandleData *handles, MonoObject *obj, gboolean track)
{
gint slot, i;
lock_handles (handles);
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);
+ mono_gc_weak_link_add (&(entries [i]), obj, track);
}
}
g_free (handles->entries);
handles->entries [slot] = obj;
if (handles->type <= HANDLE_WEAK_TRACK) {
if (obj)
- mono_gc_weak_link_add (&(handles->entries [slot]), obj);
+ mono_gc_weak_link_add (&(handles->entries [slot]), obj, track);
}
mono_perfcounters->gc_num_handles++;
guint32
mono_gchandle_new (MonoObject *obj, gboolean pinned)
{
- return alloc_handle (&gc_handles [pinned? HANDLE_PINNED: HANDLE_NORMAL], obj);
+ return alloc_handle (&gc_handles [pinned? HANDLE_PINNED: HANDLE_NORMAL], obj, FALSE);
}
/**
guint32
mono_gchandle_new_weakref (MonoObject *obj, gboolean track_resurrection)
{
- return alloc_handle (&gc_handles [track_resurrection? HANDLE_WEAK_TRACK: HANDLE_WEAK], obj);
+ guint32 handle = alloc_handle (&gc_handles [track_resurrection? HANDLE_WEAK_TRACK: HANDLE_WEAK], obj, track_resurrection);
+
+#ifndef HAVE_SGEN_GC
+ if (track_resurrection)
+ mono_gc_add_weak_track_handle (obj, handle);
+#endif
+
+ return handle;
}
static HandleType
guint slot = gchandle >> 3;
guint type = (gchandle & 7) - 1;
HandleData *handles = &gc_handles [type];
+ MonoObject *old_obj = NULL;
+
if (type > 3)
return;
lock_handles (handles);
if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
if (handles->type <= HANDLE_WEAK_TRACK) {
+ old_obj = 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);
+ mono_gc_weak_link_add (&handles->entries [slot], obj, handles->type == HANDLE_WEAK_TRACK);
} else {
handles->entries [slot] = obj;
}
}
/*g_print ("changed entry %d of type %d to object %p (in slot: %p)\n", slot, handles->type, obj, handles->entries [slot]);*/
unlock_handles (handles);
+
+#ifndef HAVE_SGEN_GC
+ if (type == HANDLE_WEAK_TRACK)
+ mono_gc_change_weak_track_handle (old_obj, obj, gchandle);
+#endif
}
/**
HandleData *handles = &gc_handles [type];
if (type > 3)
return;
+#ifndef HAVE_SGEN_GC
+ if (type == HANDLE_WEAK_TRACK)
+ mono_gc_remove_weak_track_handle (gchandle);
+#endif
+
lock_handles (handles);
if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
if (handles->type <= HANDLE_WEAK_TRACK) {
#endif
}
+#ifdef HAVE_BOEHM_GC
+
static void
collect_objects (gpointer key, gpointer value, gpointer user_data)
{
g_ptr_array_add (arr, key);
}
+#endif
+
/*
* finalize_domain_objects:
*
for (i = 0; i < objs->len; ++i) {
MonoObject *o = (MonoObject*)g_ptr_array_index (objs, i);
/* FIXME: Avoid finalizing threads, etc */
- run_finalize (o, 0);
+ mono_gc_run_finalize (o, 0);
}
g_ptr_array_free (objs, TRUE);
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);
+ mono_gc_run_finalize (to_finalize [i], 0);
}
}
#endif
static guint32
finalizer_thread (gpointer unused)
{
- gc_thread = mono_thread_current ();
-
- SetEvent (thread_started_event);
-
while (!finished) {
/* Wait to be notified that there's at least one
* finaliser to run
finalizer_event = CreateEvent (NULL, FALSE, FALSE, NULL);
pending_done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
shutdown_event = CreateEvent (NULL, TRUE, FALSE, NULL);
- thread_started_event = CreateEvent (NULL, TRUE, FALSE, NULL);
- if (finalizer_event == NULL || pending_done_event == NULL || shutdown_event == NULL || thread_started_event == NULL) {
+ if (finalizer_event == NULL || pending_done_event == NULL || shutdown_event == NULL) {
g_assert_not_reached ();
}
#if USE_POSIX_SEM
sem_init (&finalizer_sem, 0, 0);
#endif
- mono_thread_create (mono_domain_get (), finalizer_thread, NULL);
-
- /*
- * Waiting for a new thread would result in a deadlock when the runtime is
- * initialized from _CorDllMain that is called while the OS loader lock is
- * held by LoadLibrary. Avoiding waiting for the finalizer thread being
- * created should not cause any issues on Windows.
- */
- /*
- * Wait until the finalizer thread sets gc_thread since its value is needed
- * by mono_thread_attach ()
- */
- WaitForSingleObjectEx (thread_started_event, INFINITE, FALSE);
+ gc_thread = mono_thread_create_internal (mono_domain_get (), finalizer_thread, NULL, FALSE);
}
void