#include <mono/metadata/metadata-internals.h>
#include <mono/metadata/mono-mlist.h>
#include <mono/metadata/threads-types.h>
-#include <mono/metadata/threadpool-ms.h>
+#include <mono/metadata/threadpool.h>
#include <mono/sgen/sgen-conf.h>
#include <mono/sgen/sgen-gc.h>
#include <mono/utils/mono-logger-internals.h>
-#include <mono/metadata/gc-internals.h>
#include <mono/metadata/marshal.h> /* for mono_delegate_free_ftnptr () */
#include <mono/metadata/attach.h>
#include <mono/metadata/console-io.h>
#include <mono/utils/atomic.h>
#include <mono/utils/mono-coop-semaphore.h>
#include <mono/utils/hazard-pointer.h>
+#include <mono/utils/w32api.h>
#ifndef HOST_WIN32
#include <pthread.h>
MonoCoopSem done;
} DomainFinalizationReq;
-static gboolean gc_disabled = FALSE;
+static gboolean gc_disabled;
-static gboolean finalizing_root_domain = FALSE;
+static gboolean finalizing_root_domain;
-gboolean log_finalizers = FALSE;
-gboolean mono_do_not_finalize = FALSE;
-volatile gboolean suspend_finalizers = FALSE;
-gchar **mono_do_not_finalize_class_names = NULL;
+gboolean log_finalizers;
+gboolean mono_do_not_finalize;
+volatile gboolean suspend_finalizers;
+gchar **mono_do_not_finalize_class_names ;
#define mono_finalizer_lock() mono_coop_mutex_lock (&finalizer_mutex)
#define mono_finalizer_unlock() mono_coop_mutex_unlock (&finalizer_mutex)
static MonoCoopMutex finalizer_mutex;
static MonoCoopMutex reference_queue_mutex;
-static GSList *domains_to_finalize= NULL;
-static MonoMList *threads_to_finalize = NULL;
+static GSList *domains_to_finalize;
static gboolean finalizer_thread_exited;
/* Uses finalizer_mutex */
static MonoInternalThread *gc_thread;
+#ifdef TARGET_WIN32
+static HANDLE pending_done_event;
+#else
+static gboolean pending_done;
+static MonoCoopCond pending_done_cond;
+static MonoCoopMutex pending_done_mutex;
+#endif
+
static void object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*));
static void reference_queue_proccess_all (void);
static void mono_reference_queue_cleanup (void);
static void reference_queue_clear_for_domain (MonoDomain *domain);
-static HANDLE pending_done_event;
-static guint32
-guarded_wait (HANDLE handle, guint32 timeout, gboolean alertable)
+
+static MonoThreadInfoWaitRet
+guarded_wait (MonoThreadHandle *thread_handle, guint32 timeout, gboolean alertable)
{
- guint32 result;
+ MonoThreadInfoWaitRet result;
MONO_ENTER_GC_SAFE;
- result = WaitForSingleObjectEx (handle, timeout, alertable);
+ result = mono_thread_info_wait_one_handle (thread_handle, timeout, alertable);
MONO_EXIT_GC_SAFE;
return result;
}
-static gboolean
-add_thread_to_finalize (MonoInternalThread *thread, MonoError *error)
+typedef struct {
+ MonoCoopCond *cond;
+ MonoCoopMutex *mutex;
+} BreakCoopAlertableWaitUD;
+
+static inline void
+break_coop_alertable_wait (gpointer user_data)
{
- mono_error_init (error);
- mono_finalizer_lock ();
- if (!threads_to_finalize)
- MONO_GC_REGISTER_ROOT_SINGLE (threads_to_finalize, MONO_ROOT_SOURCE_FINALIZER_QUEUE, "finalizable threads list");
- threads_to_finalize = mono_mlist_append_checked (threads_to_finalize, (MonoObject*)thread, error);
- mono_finalizer_unlock ();
- return is_ok (error);
+ BreakCoopAlertableWaitUD *ud = (BreakCoopAlertableWaitUD*)user_data;
+
+ mono_coop_mutex_lock (ud->mutex);
+ mono_coop_cond_signal (ud->cond);
+ mono_coop_mutex_unlock (ud->mutex);
+
+ g_free (ud);
+}
+
+/*
+ * coop_cond_timedwait_alertable:
+ *
+ * Wait on COND/MUTEX. If ALERTABLE is non-null, the wait can be interrupted.
+ * In that case, *ALERTABLE will be set to TRUE, and 0 is returned.
+ */
+static inline gint
+coop_cond_timedwait_alertable (MonoCoopCond *cond, MonoCoopMutex *mutex, guint32 timeout_ms, gboolean *alertable)
+{
+ BreakCoopAlertableWaitUD *ud;
+ int res;
+
+ if (alertable) {
+ ud = g_new0 (BreakCoopAlertableWaitUD, 1);
+ ud->cond = cond;
+ ud->mutex = mutex;
+
+ mono_thread_info_install_interrupt (break_coop_alertable_wait, ud, alertable);
+ if (*alertable) {
+ g_free (ud);
+ return 0;
+ }
+ }
+ res = mono_coop_cond_timedwait (cond, mutex, timeout_ms);
+ if (alertable) {
+ mono_thread_info_uninstall_interrupt (alertable);
+ if (*alertable)
+ return 0;
+ else {
+ /* the interrupt token has not been taken by another
+ * thread, so it's our responsability to free it up. */
+ g_free (ud);
+ }
+ }
+ return res;
}
/*
if (mono_gc_is_finalizer_internal_thread (t))
/* Avoid finalizing ourselves */
return;
-
- if (t->threadpool_thread && finalizing_root_domain) {
- /* Don't finalize threadpool threads when
- shutting down - they're finalized when the
- threadpool shuts down. */
- if (!add_thread_to_finalize (t, &error))
- goto unhandled_error;
- return;
- }
}
if (o->vtable->klass->image == mono_defaults.corlib && !strcmp (o->vtable->klass->name, "DynamicMethod") && finalizing_root_domain) {
if (log_finalizers)
g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Calling finalizer.", o->vtable->klass->name, o);
+ mono_profiler_gc_finalize_object_begin (o);
+
runtime_invoke (o, NULL, &exc, NULL);
+ mono_profiler_gc_finalize_object_end (o);
+
if (log_finalizers)
g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Returned from finalizer.", o->vtable->klass->name, o);
mono_domain_set_internal (caller_domain);
}
-void
-mono_gc_finalize_threadpool_threads (void)
-{
- while (threads_to_finalize) {
- MonoInternalThread *thread = (MonoInternalThread*) mono_mlist_get_data (threads_to_finalize);
-
- /* Force finalization of the thread. */
- thread->threadpool_thread = FALSE;
- mono_object_register_finalizer ((MonoObject*)thread);
-
- mono_gc_run_finalize (thread, NULL);
-
- threads_to_finalize = mono_mlist_next (threads_to_finalize);
- }
-}
-
gpointer
mono_gc_out_of_memory (size_t size)
{
MonoInternalThread *thread = mono_thread_internal_current ();
gint res;
gboolean ret;
+ gint64 start;
#if defined(__native_client__)
return FALSE;
mono_gc_finalize_notify ();
if (timeout == -1)
- timeout = INFINITE;
+ timeout = MONO_INFINITE_WAIT;
+ if (timeout != MONO_INFINITE_WAIT)
+ start = mono_msec_ticks ();
ret = TRUE;
for (;;) {
- res = mono_coop_sem_timedwait (&req->done, timeout, MONO_SEM_FLAGS_ALERTABLE);
+ if (timeout == MONO_INFINITE_WAIT) {
+ res = mono_coop_sem_wait (&req->done, MONO_SEM_FLAGS_ALERTABLE);
+ } else {
+ gint64 elapsed = mono_msec_ticks () - start;
+ if (elapsed >= timeout) {
+ ret = FALSE;
+ break;
+ }
+
+ res = mono_coop_sem_timedwait (&req->done, timeout - elapsed, MONO_SEM_FLAGS_ALERTABLE);
+ }
+
if (res == MONO_SEM_TIMEDWAIT_RET_SUCCESS) {
break;
} else if (res == MONO_SEM_TIMEDWAIT_RET_ALERTED) {
goto done;
}
- if (domain == mono_get_root_domain ()) {
- mono_threadpool_ms_cleanup ();
- mono_gc_finalize_threadpool_threads ();
- }
-
- mono_profiler_appdomain_event (domain, MONO_PROFILE_END_UNLOAD);
-
done:
if (InterlockedDecrement (&req->ref) == 0) {
mono_coop_sem_destroy (&req->done);
if (gc_thread == NULL)
return;
+#ifdef TARGET_WIN32
ResetEvent (pending_done_event);
mono_gc_finalize_notify ();
/* g_print ("Waiting for pending finalizers....\n"); */
- guarded_wait (pending_done_event, INFINITE, TRUE);
+ MONO_ENTER_GC_SAFE;
+ WaitForSingleObjectEx (pending_done_event, INFINITE, TRUE);
+ MONO_EXIT_GC_SAFE;
/* g_print ("Done pending....\n"); */
+#else
+ gboolean alerted = FALSE;
+ mono_coop_mutex_lock (&pending_done_mutex);
+ pending_done = FALSE;
+ mono_gc_finalize_notify ();
+ while (!pending_done) {
+ coop_cond_timedwait_alertable (&pending_done_cond, &pending_done_mutex, MONO_INFINITE_WAIT, &alerted);
+ if (alerted)
+ break;
+ }
+ mono_coop_mutex_unlock (&pending_done_mutex);
+#endif
}
void
} else {
/* the C# code will check and throw the exception */
/* FIXME: missing !klass->blittable test, see bug #61134 */
- if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT)
+ if (mono_class_is_auto_layout (klass))
return (gpointer)-1;
return (char*)obj + sizeof (MonoObject);
}
}
static MonoCoopSem finalizer_sem;
-static volatile gboolean finished=FALSE;
+static volatile gboolean finished;
+/*
+ * mono_gc_finalize_notify:
+ *
+ * Notify the finalizer thread that finalizers etc.
+ * are available to be processed.
+ */
void
mono_gc_finalize_notify (void)
{
}
}
-static guint32
+static gsize WINAPI
finalizer_thread (gpointer unused)
{
MonoError error;
- mono_thread_set_name_internal (mono_thread_internal_current (), mono_string_new (mono_get_root_domain (), "Finalizer"), FALSE, &error);
- mono_error_assert_ok (&error);
-
gboolean wait = TRUE;
+ mono_thread_set_name_internal (mono_thread_internal_current (), mono_string_new (mono_get_root_domain (), "Finalizer"), FALSE, FALSE, &error);
+ mono_error_assert_ok (&error);
+
/* Register a hazard free queue pump callback */
mono_hazard_pointer_install_free_queue_size_callback (hazard_free_queue_is_too_big);
finalize_domain_objects ();
+ mono_profiler_gc_finalize_begin ();
+
/* If finished == TRUE, mono_gc_cleanup has been called (from mono_runtime_cleanup),
* before the domain is unloaded.
*/
mono_gc_invoke_finalizers ();
+ mono_profiler_gc_finalize_end ();
+
mono_threads_join_threads ();
reference_queue_proccess_all ();
/* Don't wait again at the start of the loop */
wait = FALSE;
} else {
+#ifdef TARGET_WIN32
SetEvent (pending_done_event);
+#else
+ mono_coop_mutex_lock (&pending_done_mutex);
+ pending_done = TRUE;
+ mono_coop_cond_signal (&pending_done_cond);
+ mono_coop_mutex_unlock (&pending_done_mutex);
+#endif
}
}
return;
}
+#ifdef TARGET_WIN32
pending_done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
g_assert (pending_done_event);
+#else
+ mono_coop_cond_init (&pending_done_cond);
+ mono_coop_mutex_init (&pending_done_mutex);
+#endif
+
mono_coop_cond_init (&exited_cond);
mono_coop_sem_init (&finalizer_sem, 0);
if (!gc_disabled) {
finished = TRUE;
if (mono_thread_internal_current () != gc_thread) {
- gint64 start_ticks = mono_msec_ticks ();
- gint64 end_ticks = start_ticks + 2000;
+ int ret;
+ gint64 start;
+ const gint64 timeout = 40 * 1000;
mono_gc_finalize_notify ();
- /* Finishing the finalizer thread, so wait a little bit... */
- /* MS seems to wait for about 2 seconds */
- while (!finalizer_thread_exited) {
- gint64 current_ticks = mono_msec_ticks ();
- guint32 timeout;
- if (current_ticks >= end_ticks)
- break;
- else
- timeout = end_ticks - current_ticks;
- mono_finalizer_lock ();
- if (!finalizer_thread_exited)
- mono_coop_cond_timedwait (&exited_cond, &finalizer_mutex, timeout);
- mono_finalizer_unlock ();
- }
+ start = mono_msec_ticks ();
- if (!finalizer_thread_exited) {
- int ret;
+ /* Finishing the finalizer thread, so wait a little bit... */
+ /* MS seems to wait for about 2 seconds per finalizer thread */
+ /* and 40 seconds for all finalizers to finish */
+ for (;;) {
+ gint64 elapsed;
+
+ if (finalizer_thread_exited) {
+ /* Wait for the thread to actually exit. We don't want the wait
+ * to be alertable, because we assert on the result to be SUCCESS_0 */
+ ret = guarded_wait (gc_thread->handle, MONO_INFINITE_WAIT, FALSE);
+ g_assert (ret == MONO_THREAD_INFO_WAIT_RET_SUCCESS_0);
+
+ mono_thread_join (GUINT_TO_POINTER (gc_thread->tid));
+ break;
+ }
- /* Set a flag which the finalizer thread can check */
- suspend_finalizers = TRUE;
- mono_gc_suspend_finalizers ();
+ elapsed = mono_msec_ticks () - start;
+ if (elapsed >= timeout) {
+ /* timeout */
- /* Try to abort the thread, in the hope that it is running managed code */
- mono_thread_internal_abort (gc_thread);
+ /* Set a flag which the finalizer thread can check */
+ suspend_finalizers = TRUE;
+ mono_gc_suspend_finalizers ();
- /* Wait for it to stop */
- ret = guarded_wait (gc_thread->handle, 100, TRUE);
+ /* Try to abort the thread, in the hope that it is running managed code */
+ mono_thread_internal_abort (gc_thread);
- if (ret == WAIT_TIMEOUT) {
- /*
- * The finalizer thread refused to exit. Make it stop.
- */
- mono_thread_internal_stop (gc_thread);
- ret = guarded_wait (gc_thread->handle, 100, TRUE);
- g_assert (ret != WAIT_TIMEOUT);
- /* The thread can't set this flag */
- finalizer_thread_exited = TRUE;
- }
- }
+ /* Wait for it to stop */
+ ret = guarded_wait (gc_thread->handle, 100, FALSE);
+ if (ret == MONO_THREAD_INFO_WAIT_RET_TIMEOUT) {
+ /* The finalizer thread refused to exit, suspend it forever. */
+ mono_thread_internal_suspend_for_shutdown (gc_thread);
+ break;
+ }
- int ret;
+ g_assert (ret == MONO_THREAD_INFO_WAIT_RET_SUCCESS_0);
- /* Wait for the thread to actually exit */
- ret = guarded_wait (gc_thread->handle, INFINITE, TRUE);
- g_assert (ret == WAIT_OBJECT_0);
+ mono_thread_join (GUINT_TO_POINTER (gc_thread->tid));
+ break;
+ }
- mono_thread_join (GUINT_TO_POINTER (gc_thread->tid));
- g_assert (finalizer_thread_exited);
+ mono_finalizer_lock ();
+ if (!finalizer_thread_exited)
+ mono_coop_cond_timedwait (&exited_cond, &finalizer_mutex, timeout - elapsed);
+ mono_finalizer_unlock ();
+ }
}
gc_thread = NULL;
mono_gc_base_cleanup ();