2 * metadata/gc.c: GC icalls.
4 * Author: Paolo Molaro <lupus@ximian.com>
6 * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
7 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
8 * Copyright 2012 Xamarin Inc (http://www.xamarin.com)
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include <mono/metadata/gc-internals.h>
17 #include <mono/metadata/mono-gc.h>
18 #include <mono/metadata/threads.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/exception.h>
21 #include <mono/metadata/profiler-private.h>
22 #include <mono/metadata/domain-internals.h>
23 #include <mono/metadata/class-internals.h>
24 #include <mono/metadata/metadata-internals.h>
25 #include <mono/metadata/mono-mlist.h>
26 #include <mono/metadata/threads-types.h>
27 #include <mono/metadata/threadpool-ms.h>
28 #include <mono/sgen/sgen-conf.h>
29 #include <mono/sgen/sgen-gc.h>
30 #include <mono/utils/mono-logger-internals.h>
31 #include <mono/metadata/gc-internals.h>
32 #include <mono/metadata/marshal.h> /* for mono_delegate_free_ftnptr () */
33 #include <mono/metadata/attach.h>
34 #include <mono/metadata/console-io.h>
35 #include <mono/utils/mono-os-semaphore.h>
36 #include <mono/utils/mono-memory-model.h>
37 #include <mono/utils/mono-counters.h>
38 #include <mono/utils/mono-time.h>
39 #include <mono/utils/dtrace.h>
40 #include <mono/utils/mono-threads.h>
41 #include <mono/utils/mono-threads-coop.h>
42 #include <mono/utils/atomic.h>
43 #include <mono/utils/mono-coop-semaphore.h>
44 #include <mono/utils/hazard-pointer.h>
50 typedef struct DomainFinalizationReq {
54 } DomainFinalizationReq;
56 static gboolean gc_disabled;
58 static gboolean finalizing_root_domain;
60 gboolean log_finalizers;
61 gboolean mono_do_not_finalize;
62 volatile gboolean suspend_finalizers;
63 gchar **mono_do_not_finalize_class_names ;
65 #define mono_finalizer_lock() mono_coop_mutex_lock (&finalizer_mutex)
66 #define mono_finalizer_unlock() mono_coop_mutex_unlock (&finalizer_mutex)
67 static MonoCoopMutex finalizer_mutex;
68 static MonoCoopMutex reference_queue_mutex;
70 static GSList *domains_to_finalize;
71 static MonoMList *threads_to_finalize;
73 static gboolean finalizer_thread_exited;
74 /* Uses finalizer_mutex */
75 static MonoCoopCond exited_cond;
77 static MonoInternalThread *gc_thread;
80 static HANDLE pending_done_event;
82 static gboolean pending_done;
83 static MonoCoopCond pending_done_cond;
84 static MonoCoopMutex pending_done_mutex;
87 static void object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*));
89 static void reference_queue_proccess_all (void);
90 static void mono_reference_queue_cleanup (void);
91 static void reference_queue_clear_for_domain (MonoDomain *domain);
95 guarded_wait (HANDLE handle, guint32 timeout, gboolean alertable)
100 result = WaitForSingleObjectEx (handle, timeout, alertable);
108 MonoCoopMutex *mutex;
109 } BreakCoopAlertableWaitUD;
112 break_coop_alertable_wait (gpointer user_data)
114 BreakCoopAlertableWaitUD *ud = (BreakCoopAlertableWaitUD*)user_data;
116 mono_coop_mutex_lock (ud->mutex);
117 mono_coop_cond_signal (ud->cond);
118 mono_coop_mutex_unlock (ud->mutex);
124 * coop_cond_timedwait_alertable:
126 * Wait on COND/MUTEX. If ALERTABLE is non-null, the wait can be interrupted.
127 * In that case, *ALERTABLE will be set to TRUE, and 0 is returned.
130 coop_cond_timedwait_alertable (MonoCoopCond *cond, MonoCoopMutex *mutex, guint32 timeout_ms, gboolean *alertable)
132 BreakCoopAlertableWaitUD *ud;
136 ud = g_new0 (BreakCoopAlertableWaitUD, 1);
140 mono_thread_info_install_interrupt (break_coop_alertable_wait, ud, alertable);
146 res = mono_coop_cond_timedwait (cond, mutex, timeout_ms);
148 mono_thread_info_uninstall_interrupt (alertable);
152 /* the interrupt token has not been taken by another
153 * thread, so it's our responsability to free it up. */
161 add_thread_to_finalize (MonoInternalThread *thread, MonoError *error)
163 mono_error_init (error);
164 mono_finalizer_lock ();
165 if (!threads_to_finalize)
166 MONO_GC_REGISTER_ROOT_SINGLE (threads_to_finalize, MONO_ROOT_SOURCE_FINALIZER_QUEUE, "finalizable threads list");
167 threads_to_finalize = mono_mlist_append_checked (threads_to_finalize, (MonoObject*)thread, error);
168 mono_finalizer_unlock ();
169 return is_ok (error);
173 * actually, we might want to queue the finalize requests in a separate thread,
174 * but we need to be careful about the execution domain of the thread...
177 mono_gc_run_finalize (void *obj, void *data)
180 MonoObject *exc = NULL;
185 MonoMethod* finalizer = NULL;
186 MonoDomain *caller_domain = mono_domain_get ();
188 RuntimeInvokeFunction runtime_invoke;
190 // This function is called from the innards of the GC, so our best alternative for now is to do polling here
191 mono_threads_safepoint ();
193 o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data));
195 if (mono_do_not_finalize) {
196 if (!mono_do_not_finalize_class_names)
199 size_t namespace_len = strlen (o->vtable->klass->name_space);
200 for (int i = 0; mono_do_not_finalize_class_names [i]; ++i) {
201 const char *name = mono_do_not_finalize_class_names [i];
202 if (strncmp (name, o->vtable->klass->name_space, namespace_len))
204 if (name [namespace_len] != '.')
206 if (strcmp (name + namespace_len + 1, o->vtable->klass->name))
213 g_log ("mono-gc-finalizers", G_LOG_LEVEL_DEBUG, "<%s at %p> Starting finalizer checks.", o->vtable->klass->name, o);
215 if (suspend_finalizers)
218 domain = o->vtable->domain;
221 mono_domain_finalizers_lock (domain);
223 o2 = (MonoObject *)g_hash_table_lookup (domain->finalizable_objects_hash, o);
225 mono_domain_finalizers_unlock (domain);
228 /* Already finalized somehow */
232 /* make sure the finalizer is not called again if the object is resurrected */
233 object_register_finalizer ((MonoObject *)obj, NULL);
236 g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Registered finalizer as processed.", o->vtable->klass->name, o);
238 if (o->vtable->klass == mono_defaults.internal_thread_class) {
239 MonoInternalThread *t = (MonoInternalThread*)o;
241 if (mono_gc_is_finalizer_internal_thread (t))
242 /* Avoid finalizing ourselves */
245 if (t->threadpool_thread && finalizing_root_domain) {
246 /* Don't finalize threadpool threads when
247 shutting down - they're finalized when the
248 threadpool shuts down. */
249 if (!add_thread_to_finalize (t, &error))
250 goto unhandled_error;
255 if (o->vtable->klass->image == mono_defaults.corlib && !strcmp (o->vtable->klass->name, "DynamicMethod") && finalizing_root_domain) {
257 * These can't be finalized during unloading/shutdown, since that would
258 * free the native code which can still be referenced by other
260 * FIXME: This is not perfect, objects dying at the same time as
261 * dynamic methods can still reference them even when !shutdown.
266 if (mono_runtime_get_no_exec ())
269 /* speedup later... and use a timeout */
270 /* g_print ("Finalize run on %p %s.%s\n", o, mono_object_class (o)->name_space, mono_object_class (o)->name); */
272 /* Use _internal here, since this thread can enter a doomed appdomain */
273 mono_domain_set_internal (mono_object_domain (o));
275 /* delegates that have a native function pointer allocated are
276 * registered for finalization, but they don't have a Finalize
277 * method, because in most cases it's not needed and it's just a waste.
279 if (o->vtable->klass->delegate) {
280 MonoDelegate* del = (MonoDelegate*)o;
281 if (del->delegate_trampoline)
282 mono_delegate_free_ftnptr ((MonoDelegate*)o);
283 mono_domain_set_internal (caller_domain);
287 finalizer = mono_class_get_finalizer (o->vtable->klass);
289 /* If object has a CCW but has no finalizer, it was only
290 * registered for finalization in order to free the CCW.
291 * Else it needs the regular finalizer run.
292 * FIXME: what to do about ressurection and suppression
293 * of finalizer on object with CCW.
295 if (mono_marshal_free_ccw (o) && !finalizer) {
296 mono_domain_set_internal (caller_domain);
301 * To avoid the locking plus the other overhead of mono_runtime_invoke_checked (),
302 * create and precompile a wrapper which calls the finalize method using
306 g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Compiling finalizer.", o->vtable->klass->name, o);
308 if (!domain->finalize_runtime_invoke) {
309 MonoMethod *invoke = mono_marshal_get_runtime_invoke (mono_class_get_method_from_name_flags (mono_defaults.object_class, "Finalize", 0, 0), TRUE);
311 domain->finalize_runtime_invoke = mono_compile_method_checked (invoke, &error);
312 mono_error_assert_ok (&error); /* expect this not to fail */
315 runtime_invoke = (RuntimeInvokeFunction)domain->finalize_runtime_invoke;
317 mono_runtime_class_init_full (o->vtable, &error);
319 goto unhandled_error;
321 if (G_UNLIKELY (MONO_GC_FINALIZE_INVOKE_ENABLED ())) {
322 MONO_GC_FINALIZE_INVOKE ((unsigned long)o, mono_object_get_size (o),
323 o->vtable->klass->name_space, o->vtable->klass->name);
327 g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Calling finalizer.", o->vtable->klass->name, o);
329 runtime_invoke (o, NULL, &exc, NULL);
332 g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Returned from finalizer.", o->vtable->klass->name, o);
336 exc = (MonoObject*)mono_error_convert_to_exception (&error);
338 mono_thread_internal_unhandled_exception (exc);
340 mono_domain_set_internal (caller_domain);
344 mono_gc_finalize_threadpool_threads (void)
346 while (threads_to_finalize) {
347 MonoInternalThread *thread = (MonoInternalThread*) mono_mlist_get_data (threads_to_finalize);
349 /* Force finalization of the thread. */
350 thread->threadpool_thread = FALSE;
351 mono_object_register_finalizer ((MonoObject*)thread);
353 mono_gc_run_finalize (thread, NULL);
355 threads_to_finalize = mono_mlist_next (threads_to_finalize);
360 mono_gc_out_of_memory (size_t size)
363 * we could allocate at program startup some memory that we could release
364 * back to the system at this point if we're really low on memory (ie, size is
365 * lower than the memory we set apart)
367 mono_raise_exception (mono_domain_get ()->out_of_memory_ex);
373 * Some of our objects may point to a different address than the address returned by GC_malloc()
374 * (because of the GetHashCode hack), but we need to pass the real address to register_finalizer.
375 * This also means that in the callback we need to adjust the pointer to get back the real
377 * We also need to be consistent in the use of the GC_debug* variants of malloc and register_finalizer,
378 * since that, too, can cause the underlying pointer to be offset.
381 object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*))
385 g_assert (obj != NULL);
387 domain = obj->vtable->domain;
390 if (mono_domain_is_unloading (domain) && (callback != NULL))
392 * Can't register finalizers in a dying appdomain, since they
393 * could be invoked after the appdomain has been unloaded.
397 mono_domain_finalizers_lock (domain);
400 g_hash_table_insert (domain->finalizable_objects_hash, obj, obj);
402 g_hash_table_remove (domain->finalizable_objects_hash, obj);
404 mono_domain_finalizers_unlock (domain);
406 mono_gc_register_for_finalization (obj, callback);
407 #elif defined(HAVE_SGEN_GC)
409 * If we register finalizers for domains that are unloading we might
410 * end up running them while or after the domain is being cleared, so
411 * the objects will not be valid anymore.
413 if (!mono_domain_is_unloading (domain))
414 mono_gc_register_for_finalization (obj, callback);
419 * mono_object_register_finalizer:
420 * @obj: object to register
422 * Records that object @obj has a finalizer, this will call the
423 * Finalize method when the garbage collector disposes the object.
427 mono_object_register_finalizer (MonoObject *obj)
429 /* g_print ("Registered finalizer on %p %s.%s\n", obj, mono_object_class (obj)->name_space, mono_object_class (obj)->name); */
430 object_register_finalizer (obj, mono_gc_run_finalize);
434 * mono_domain_finalize:
435 * @domain: the domain to finalize
436 * @timeout: msects to wait for the finalization to complete, -1 to wait indefinitely
438 * Request finalization of all finalizable objects inside @domain. Wait
439 * @timeout msecs for the finalization to complete.
441 * Returns: TRUE if succeeded, FALSE if there was a timeout
445 mono_domain_finalize (MonoDomain *domain, guint32 timeout)
447 DomainFinalizationReq *req;
448 MonoInternalThread *thread = mono_thread_internal_current ();
453 #if defined(__native_client__)
457 if (mono_thread_internal_current () == gc_thread)
458 /* We are called from inside a finalizer, not much we can do here */
462 * No need to create another thread 'cause the finalizer thread
463 * is still working and will take care of running the finalizers
469 /* We don't support domain finalization without a GC */
470 if (mono_gc_is_null ())
473 mono_gc_collect (mono_gc_max_generation ());
475 req = g_new0 (DomainFinalizationReq, 1);
477 req->domain = domain;
478 mono_coop_sem_init (&req->done, 0);
480 if (domain == mono_get_root_domain ())
481 finalizing_root_domain = TRUE;
483 mono_finalizer_lock ();
485 domains_to_finalize = g_slist_append (domains_to_finalize, req);
487 mono_finalizer_unlock ();
489 /* Tell the finalizer thread to finalize this appdomain */
490 mono_gc_finalize_notify ();
494 if (timeout != INFINITE)
495 start = mono_msec_ticks ();
500 if (timeout == INFINITE) {
501 res = mono_coop_sem_wait (&req->done, MONO_SEM_FLAGS_ALERTABLE);
503 gint64 elapsed = mono_msec_ticks () - start;
504 if (elapsed >= timeout) {
509 res = mono_coop_sem_timedwait (&req->done, timeout - elapsed, MONO_SEM_FLAGS_ALERTABLE);
512 if (res == MONO_SEM_TIMEDWAIT_RET_SUCCESS) {
514 } else if (res == MONO_SEM_TIMEDWAIT_RET_ALERTED) {
515 if ((thread->state & (ThreadState_StopRequested | ThreadState_SuspendRequested)) != 0) {
519 } else if (res == MONO_SEM_TIMEDWAIT_RET_TIMEDOUT) {
523 g_error ("%s: unknown result %d", __func__, res);
528 /* Try removing the req from domains_to_finalize:
529 * - if it's not found: the domain is being finalized,
530 * so we the ref count is already decremented
531 * - if it's found: the domain is not yet being finalized,
532 * so we can safely decrement the ref */
536 mono_finalizer_lock ();
538 found = g_slist_index (domains_to_finalize, req) != -1;
540 domains_to_finalize = g_slist_remove (domains_to_finalize, req);
542 mono_finalizer_unlock ();
545 /* We have to decrement it wherever we
546 * remove it from domains_to_finalize */
547 if (InterlockedDecrement (&req->ref) != 1)
548 g_error ("%s: req->ref should be 1, as we are the first one to decrement it", __func__);
554 if (domain == mono_get_root_domain ()) {
555 mono_threadpool_ms_cleanup ();
556 mono_gc_finalize_threadpool_threads ();
559 mono_profiler_appdomain_event (domain, MONO_PROFILE_END_UNLOAD);
562 if (InterlockedDecrement (&req->ref) == 0) {
563 mono_coop_sem_destroy (&req->done);
571 ves_icall_System_GC_InternalCollect (int generation)
573 mono_gc_collect (generation);
577 ves_icall_System_GC_GetTotalMemory (MonoBoolean forceCollection)
580 mono_gc_collect (mono_gc_max_generation ());
581 return mono_gc_get_used_size ();
585 ves_icall_System_GC_KeepAlive (MonoObject *obj)
593 ves_icall_System_GC_ReRegisterForFinalize (MonoObject *obj)
595 MONO_CHECK_ARG_NULL (obj,);
597 object_register_finalizer (obj, mono_gc_run_finalize);
601 ves_icall_System_GC_SuppressFinalize (MonoObject *obj)
603 MONO_CHECK_ARG_NULL (obj,);
605 /* delegates have no finalizers, but we register them to deal with the
606 * unmanaged->managed trampoline. We don't let the user suppress it
607 * otherwise we'd leak it.
609 if (obj->vtable->klass->delegate)
612 /* FIXME: Need to handle case where obj has COM Callable Wrapper
613 * generated for it that needs cleaned up, but user wants to suppress
614 * their derived object finalizer. */
616 object_register_finalizer (obj, NULL);
620 ves_icall_System_GC_WaitForPendingFinalizers (void)
622 if (mono_gc_is_null ())
625 if (!mono_gc_pending_finalizers ())
628 if (mono_thread_internal_current () == gc_thread)
629 /* Avoid deadlocks */
633 If the finalizer thread is not live, lets pretend no finalizers are pending since the current thread might
634 be the one responsible for starting it up.
636 if (gc_thread == NULL)
640 ResetEvent (pending_done_event);
641 mono_gc_finalize_notify ();
642 /* g_print ("Waiting for pending finalizers....\n"); */
643 guarded_wait (pending_done_event, INFINITE, TRUE);
644 /* g_print ("Done pending....\n"); */
646 gboolean alerted = FALSE;
647 mono_coop_mutex_lock (&pending_done_mutex);
648 pending_done = FALSE;
649 mono_gc_finalize_notify ();
650 while (!pending_done) {
651 coop_cond_timedwait_alertable (&pending_done_cond, &pending_done_mutex, INFINITE, &alerted);
655 mono_coop_mutex_unlock (&pending_done_mutex);
660 ves_icall_System_GC_register_ephemeron_array (MonoObject *array)
663 if (!mono_gc_ephemeron_array_add (array)) {
664 mono_set_pending_exception (mono_object_domain (array)->out_of_memory_ex);
671 ves_icall_System_GC_get_ephemeron_tombstone (void)
673 return mono_domain_get ()->ephemeron_tombstone;
677 ves_icall_System_GCHandle_GetTarget (guint32 handle)
679 return mono_gchandle_get_target (handle);
683 * if type == -1, change the target of the handle, otherwise allocate a new handle.
686 ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint32 type)
689 mono_gchandle_set_target (handle, obj);
690 /* the handle doesn't change */
695 return mono_gchandle_new_weakref (obj, FALSE);
696 case HANDLE_WEAK_TRACK:
697 return mono_gchandle_new_weakref (obj, TRUE);
699 return mono_gchandle_new (obj, FALSE);
701 return mono_gchandle_new (obj, TRUE);
703 g_assert_not_reached ();
709 ves_icall_System_GCHandle_FreeHandle (guint32 handle)
711 mono_gchandle_free (handle);
715 ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle)
719 if (MONO_GC_HANDLE_TYPE (handle) != HANDLE_PINNED)
721 obj = mono_gchandle_get_target (handle);
723 MonoClass *klass = mono_object_class (obj);
724 if (klass == mono_defaults.string_class) {
725 return mono_string_chars ((MonoString*)obj);
726 } else if (klass->rank) {
727 return mono_array_addr ((MonoArray*)obj, char, 0);
729 /* the C# code will check and throw the exception */
730 /* FIXME: missing !klass->blittable test, see bug #61134 */
731 if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT)
733 return (char*)obj + sizeof (MonoObject);
740 mono_gc_GCHandle_CheckCurrentDomain (guint32 gchandle)
742 return mono_gchandle_is_in_domain (gchandle, mono_domain_get ());
745 static MonoCoopSem finalizer_sem;
746 static volatile gboolean finished;
749 * mono_gc_finalize_notify:
751 * Notify the finalizer thread that finalizers etc.
752 * are available to be processed.
755 mono_gc_finalize_notify (void)
758 g_message ( "%s: prodding finalizer", __func__);
761 if (mono_gc_is_null ())
764 mono_coop_sem_post (&finalizer_sem);
768 This is the number of entries allowed in the hazard free queue before
769 we explicitly cycle the finalizer thread to trigger pumping the queue.
771 It was picked empirically by running the corlib test suite in a stress
772 scenario where all hazard entries are queued.
774 In this extreme scenario we double the number of times we cycle the finalizer
775 thread compared to just GC calls.
777 Entries are usually in the order of 100's of bytes each, so we're limiting
778 floating garbage to be in the order of a dozen kb.
780 static gboolean finalizer_thread_pulsed;
781 #define HAZARD_QUEUE_OVERFLOW_SIZE 20
784 hazard_free_queue_is_too_big (size_t size)
786 if (size < HAZARD_QUEUE_OVERFLOW_SIZE)
789 if (finalizer_thread_pulsed || InterlockedCompareExchange (&finalizer_thread_pulsed, TRUE, FALSE))
792 mono_gc_finalize_notify ();
796 hazard_free_queue_pump (void)
798 mono_thread_hazardous_try_free_all ();
799 finalizer_thread_pulsed = FALSE;
805 collect_objects (gpointer key, gpointer value, gpointer user_data)
807 GPtrArray *arr = (GPtrArray*)user_data;
808 g_ptr_array_add (arr, key);
814 * finalize_domain_objects:
816 * Run the finalizers of all finalizable objects in req->domain.
819 finalize_domain_objects (void)
821 DomainFinalizationReq *req = NULL;
824 if (domains_to_finalize) {
825 mono_finalizer_lock ();
826 if (domains_to_finalize) {
827 req = (DomainFinalizationReq *)domains_to_finalize->data;
828 domains_to_finalize = g_slist_remove (domains_to_finalize, req);
830 mono_finalizer_unlock ();
836 domain = req->domain;
838 /* Process finalizers which are already in the queue */
839 mono_gc_invoke_finalizers ();
842 while (g_hash_table_size (domain->finalizable_objects_hash) > 0) {
846 * Since the domain is unloading, nobody is allowed to put
847 * new entries into the hash table. But finalize_object might
848 * remove entries from the hash table, so we make a copy.
850 objs = g_ptr_array_new ();
851 g_hash_table_foreach (domain->finalizable_objects_hash, collect_objects, objs);
852 /* printf ("FINALIZING %d OBJECTS.\n", objs->len); */
854 for (i = 0; i < objs->len; ++i) {
855 MonoObject *o = (MonoObject*)g_ptr_array_index (objs, i);
856 /* FIXME: Avoid finalizing threads, etc */
857 mono_gc_run_finalize (o, 0);
860 g_ptr_array_free (objs, TRUE);
862 #elif defined(HAVE_SGEN_GC)
863 mono_gc_finalize_domain (domain);
864 mono_gc_invoke_finalizers ();
867 /* cleanup the reference queue */
868 reference_queue_clear_for_domain (domain);
870 /* printf ("DONE.\n"); */
871 mono_coop_sem_post (&req->done);
873 if (InterlockedDecrement (&req->ref) == 0) {
874 /* mono_domain_finalize already returned, and
875 * doesn't hold a reference to req anymore. */
876 mono_coop_sem_destroy (&req->done);
882 finalizer_thread (gpointer unused)
885 gboolean wait = TRUE;
887 mono_thread_set_name_internal (mono_thread_internal_current (), mono_string_new (mono_get_root_domain (), "Finalizer"), FALSE, &error);
888 mono_error_assert_ok (&error);
890 /* Register a hazard free queue pump callback */
891 mono_hazard_pointer_install_free_queue_size_callback (hazard_free_queue_is_too_big);
894 /* Wait to be notified that there's at least one
898 g_assert (mono_domain_get () == mono_get_root_domain ());
899 mono_gc_set_skip_thread (TRUE);
902 /* An alertable wait is required so this thread can be suspended on windows */
903 mono_coop_sem_wait (&finalizer_sem, MONO_SEM_FLAGS_ALERTABLE);
907 mono_gc_set_skip_thread (FALSE);
909 mono_threads_perform_thread_dump ();
911 mono_console_handle_async_ops ();
913 mono_attach_maybe_start ();
915 finalize_domain_objects ();
917 /* If finished == TRUE, mono_gc_cleanup has been called (from mono_runtime_cleanup),
918 * before the domain is unloaded.
920 mono_gc_invoke_finalizers ();
922 mono_threads_join_threads ();
924 reference_queue_proccess_all ();
926 hazard_free_queue_pump ();
928 /* Avoid posting the pending done event until there are pending finalizers */
929 if (mono_coop_sem_timedwait (&finalizer_sem, 0, MONO_SEM_FLAGS_NONE) == MONO_SEM_TIMEDWAIT_RET_SUCCESS) {
930 /* Don't wait again at the start of the loop */
934 SetEvent (pending_done_event);
936 mono_coop_mutex_lock (&pending_done_mutex);
938 mono_coop_cond_signal (&pending_done_cond);
939 mono_coop_mutex_unlock (&pending_done_mutex);
944 mono_finalizer_lock ();
945 finalizer_thread_exited = TRUE;
946 mono_coop_cond_signal (&exited_cond);
947 mono_finalizer_unlock ();
952 #ifndef LAZY_GC_THREAD_CREATION
956 mono_gc_init_finalizer_thread (void)
959 gc_thread = mono_thread_create_internal (mono_domain_get (), finalizer_thread, NULL, FALSE, 0, &error);
960 mono_error_assert_ok (&error);
966 mono_coop_mutex_init_recursive (&finalizer_mutex);
967 mono_coop_mutex_init_recursive (&reference_queue_mutex);
969 mono_counters_register ("Minor GC collections", MONO_COUNTER_GC | MONO_COUNTER_UINT, &gc_stats.minor_gc_count);
970 mono_counters_register ("Major GC collections", MONO_COUNTER_GC | MONO_COUNTER_UINT, &gc_stats.major_gc_count);
971 mono_counters_register ("Minor GC time", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &gc_stats.minor_gc_time);
972 mono_counters_register ("Major GC time", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &gc_stats.major_gc_time);
973 mono_counters_register ("Major GC time concurrent", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &gc_stats.major_gc_time_concurrent);
975 mono_gc_base_init ();
977 if (mono_gc_is_disabled ()) {
983 pending_done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
984 g_assert (pending_done_event);
986 mono_coop_cond_init (&pending_done_cond);
987 mono_coop_mutex_init (&pending_done_mutex);
990 mono_coop_cond_init (&exited_cond);
991 mono_coop_sem_init (&finalizer_sem, 0);
993 #ifndef LAZY_GC_THREAD_CREATION
994 mono_gc_init_finalizer_thread ();
999 mono_gc_cleanup (void)
1002 g_message ("%s: cleaning up finalizer", __func__);
1005 if (mono_gc_is_null ())
1010 if (mono_thread_internal_current () != gc_thread) {
1011 gint64 start_ticks = mono_msec_ticks ();
1012 gint64 end_ticks = start_ticks + 2000;
1014 mono_gc_finalize_notify ();
1015 /* Finishing the finalizer thread, so wait a little bit... */
1016 /* MS seems to wait for about 2 seconds */
1017 while (!finalizer_thread_exited) {
1018 gint64 current_ticks = mono_msec_ticks ();
1021 if (current_ticks >= end_ticks)
1024 timeout = end_ticks - current_ticks;
1025 mono_finalizer_lock ();
1026 if (!finalizer_thread_exited)
1027 mono_coop_cond_timedwait (&exited_cond, &finalizer_mutex, timeout);
1028 mono_finalizer_unlock ();
1031 if (!finalizer_thread_exited) {
1034 /* Set a flag which the finalizer thread can check */
1035 suspend_finalizers = TRUE;
1036 mono_gc_suspend_finalizers ();
1038 /* Try to abort the thread, in the hope that it is running managed code */
1039 mono_thread_internal_abort (gc_thread);
1041 /* Wait for it to stop */
1042 ret = guarded_wait (gc_thread->handle, 100, TRUE);
1044 if (ret == WAIT_TIMEOUT) {
1046 * The finalizer thread refused to exit. Make it stop.
1048 mono_thread_internal_stop (gc_thread);
1049 ret = guarded_wait (gc_thread->handle, 100, TRUE);
1050 g_assert (ret != WAIT_TIMEOUT);
1051 /* The thread can't set this flag */
1052 finalizer_thread_exited = TRUE;
1058 /* Wait for the thread to actually exit */
1059 ret = guarded_wait (gc_thread->handle, INFINITE, TRUE);
1060 g_assert (ret == WAIT_OBJECT_0);
1062 mono_thread_join (GUINT_TO_POINTER (gc_thread->tid));
1063 g_assert (finalizer_thread_exited);
1066 mono_gc_base_cleanup ();
1069 mono_reference_queue_cleanup ();
1071 mono_coop_mutex_destroy (&finalizer_mutex);
1072 mono_coop_mutex_destroy (&reference_queue_mutex);
1076 mono_gc_is_finalizer_internal_thread (MonoInternalThread *thread)
1078 return thread == gc_thread;
1082 * mono_gc_is_finalizer_thread:
1083 * @thread: the thread to test.
1085 * In Mono objects are finalized asynchronously on a separate thread.
1086 * This routine tests whether the @thread argument represents the
1087 * finalization thread.
1089 * Returns: TRUE if @thread is the finalization thread.
1092 mono_gc_is_finalizer_thread (MonoThread *thread)
1094 return mono_gc_is_finalizer_internal_thread (thread->internal_thread);
1097 #if defined(__MACH__)
1098 static pthread_t mach_exception_thread;
1101 mono_gc_register_mach_exception_thread (pthread_t thread)
1103 mach_exception_thread = thread;
1107 mono_gc_get_mach_exception_thread (void)
1109 return mach_exception_thread;
1113 static MonoReferenceQueue *ref_queues;
1116 ref_list_remove_element (RefQueueEntry **prev, RefQueueEntry *element)
1119 /* Guard if head is changed concurrently. */
1120 while (*prev != element)
1121 prev = &(*prev)->next;
1122 } while (prev && InterlockedCompareExchangePointer ((volatile gpointer *)prev, element->next, element) != element);
1126 ref_list_push (RefQueueEntry **head, RefQueueEntry *value)
1128 RefQueueEntry *current;
1131 value->next = current;
1132 STORE_STORE_FENCE; /*Must make sure the previous store is visible before the CAS. */
1133 } while (InterlockedCompareExchangePointer ((volatile gpointer *)head, value, current) != current);
1137 reference_queue_proccess (MonoReferenceQueue *queue)
1139 RefQueueEntry **iter = &queue->queue;
1140 RefQueueEntry *entry;
1141 while ((entry = *iter)) {
1142 if (queue->should_be_deleted || !mono_gchandle_get_target (entry->gchandle)) {
1143 mono_gchandle_free ((guint32)entry->gchandle);
1144 ref_list_remove_element (iter, entry);
1145 queue->callback (entry->user_data);
1148 iter = &entry->next;
1154 reference_queue_proccess_all (void)
1156 MonoReferenceQueue **iter;
1157 MonoReferenceQueue *queue = ref_queues;
1158 for (; queue; queue = queue->next)
1159 reference_queue_proccess (queue);
1162 mono_coop_mutex_lock (&reference_queue_mutex);
1163 for (iter = &ref_queues; *iter;) {
1165 if (!queue->should_be_deleted) {
1166 iter = &queue->next;
1170 mono_coop_mutex_unlock (&reference_queue_mutex);
1171 reference_queue_proccess (queue);
1174 *iter = queue->next;
1177 mono_coop_mutex_unlock (&reference_queue_mutex);
1181 mono_reference_queue_cleanup (void)
1183 MonoReferenceQueue *queue = ref_queues;
1184 for (; queue; queue = queue->next)
1185 queue->should_be_deleted = TRUE;
1186 reference_queue_proccess_all ();
1190 reference_queue_clear_for_domain (MonoDomain *domain)
1192 MonoReferenceQueue *queue = ref_queues;
1193 for (; queue; queue = queue->next) {
1194 RefQueueEntry **iter = &queue->queue;
1195 RefQueueEntry *entry;
1196 while ((entry = *iter)) {
1197 if (entry->domain == domain) {
1198 mono_gchandle_free ((guint32)entry->gchandle);
1199 ref_list_remove_element (iter, entry);
1200 queue->callback (entry->user_data);
1203 iter = &entry->next;
1209 * mono_gc_reference_queue_new:
1210 * @callback callback used when processing collected entries.
1212 * Create a new reference queue used to process collected objects.
1213 * A reference queue let you add a pair of (managed object, user data)
1214 * using the mono_gc_reference_queue_add method.
1216 * Once the managed object is collected @callback will be called
1217 * in the finalizer thread with 'user data' as argument.
1219 * The callback is called from the finalizer thread without any locks held.
1220 * When a AppDomain is unloaded, all callbacks for objects belonging to it
1223 * @returns the new queue.
1226 mono_gc_reference_queue_new (mono_reference_queue_callback callback)
1228 MonoReferenceQueue *res = g_new0 (MonoReferenceQueue, 1);
1229 res->callback = callback;
1231 mono_coop_mutex_lock (&reference_queue_mutex);
1232 res->next = ref_queues;
1234 mono_coop_mutex_unlock (&reference_queue_mutex);
1240 * mono_gc_reference_queue_add:
1241 * @queue the queue to add the reference to.
1242 * @obj the object to be watched for collection
1243 * @user_data parameter to be passed to the queue callback
1245 * Queue an object to be watched for collection, when the @obj is
1246 * collected, the callback that was registered for the @queue will
1247 * be invoked with @user_data as argument.
1249 * @returns false if the queue is scheduled to be freed.
1252 mono_gc_reference_queue_add (MonoReferenceQueue *queue, MonoObject *obj, void *user_data)
1254 RefQueueEntry *entry;
1255 if (queue->should_be_deleted)
1258 g_assert (obj != NULL);
1260 entry = g_new0 (RefQueueEntry, 1);
1261 entry->user_data = user_data;
1262 entry->domain = mono_object_domain (obj);
1264 entry->gchandle = mono_gchandle_new_weakref (obj, TRUE);
1265 mono_object_register_finalizer (obj);
1267 ref_list_push (&queue->queue, entry);
1272 * mono_gc_reference_queue_free:
1273 * @queue the queue that should be freed.
1275 * This operation signals that @queue should be freed. This operation is deferred
1276 * as it happens on the finalizer thread.
1278 * After this call, no further objects can be queued. It's the responsibility of the
1279 * caller to make sure that no further attempt to access queue will be made.
1282 mono_gc_reference_queue_free (MonoReferenceQueue *queue)
1284 queue->should_be_deleted = TRUE;