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)
16 #include <mono/metadata/gc-internal.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/threadpool.h>
27 #include <mono/metadata/threadpool-internals.h>
28 #include <mono/metadata/threads-types.h>
29 #include <mono/utils/mono-logger-internal.h>
30 #include <mono/metadata/gc-internal.h>
31 #include <mono/metadata/marshal.h> /* for mono_delegate_free_ftnptr () */
32 #include <mono/metadata/attach.h>
33 #include <mono/metadata/console-io.h>
34 #include <mono/utils/mono-semaphore.h>
35 #include <mono/utils/mono-memory-model.h>
36 #include <mono/utils/mono-counters.h>
37 #include <mono/utils/dtrace.h>
43 typedef struct DomainFinalizationReq {
46 } DomainFinalizationReq;
48 #ifdef PLATFORM_WINCE /* FIXME: add accessors to gc.dll API */
49 extern void (*__imp_GC_finalizer_notifier)(void);
50 #define GC_finalizer_notifier __imp_GC_finalizer_notifier
51 extern int __imp_GC_finalize_on_demand;
52 #define GC_finalize_on_demand __imp_GC_finalize_on_demand
55 static gboolean gc_disabled = FALSE;
57 static gboolean finalizing_root_domain = FALSE;
59 #define mono_finalizer_lock() EnterCriticalSection (&finalizer_mutex)
60 #define mono_finalizer_unlock() LeaveCriticalSection (&finalizer_mutex)
61 static CRITICAL_SECTION finalizer_mutex;
62 static CRITICAL_SECTION reference_queue_mutex;
64 static GSList *domains_to_finalize= NULL;
65 static MonoMList *threads_to_finalize = NULL;
67 static MonoInternalThread *gc_thread;
69 static void object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*));
71 static void mono_gchandle_set_target (guint32 gchandle, MonoObject *obj);
73 static void reference_queue_proccess_all (void);
74 static void mono_reference_queue_cleanup (void);
75 static void reference_queue_clear_for_domain (MonoDomain *domain);
77 static HANDLE pending_done_event;
78 static HANDLE shutdown_event;
84 add_thread_to_finalize (MonoInternalThread *thread)
86 mono_finalizer_lock ();
87 if (!threads_to_finalize)
88 MONO_GC_REGISTER_ROOT_SINGLE (threads_to_finalize);
89 threads_to_finalize = mono_mlist_append (threads_to_finalize, (MonoObject*)thread);
90 mono_finalizer_unlock ();
93 static gboolean suspend_finalizers = FALSE;
95 * actually, we might want to queue the finalize requests in a separate thread,
96 * but we need to be careful about the execution domain of the thread...
99 mono_gc_run_finalize (void *obj, void *data)
101 MonoObject *exc = NULL;
106 MonoMethod* finalizer = NULL;
107 MonoDomain *caller_domain = mono_domain_get ();
109 RuntimeInvokeFunction runtime_invoke;
111 o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data));
113 if (suspend_finalizers)
116 domain = o->vtable->domain;
119 mono_domain_finalizers_lock (domain);
121 o2 = g_hash_table_lookup (domain->finalizable_objects_hash, o);
123 mono_domain_finalizers_unlock (domain);
126 /* Already finalized somehow */
130 /* make sure the finalizer is not called again if the object is resurrected */
131 object_register_finalizer (obj, NULL);
133 if (o->vtable->klass == mono_defaults.internal_thread_class) {
134 MonoInternalThread *t = (MonoInternalThread*)o;
136 if (mono_gc_is_finalizer_internal_thread (t))
137 /* Avoid finalizing ourselves */
140 if (t->threadpool_thread && finalizing_root_domain) {
141 /* Don't finalize threadpool threads when
142 shutting down - they're finalized when the
143 threadpool shuts down. */
144 add_thread_to_finalize (t);
149 if (o->vtable->klass->image == mono_defaults.corlib && !strcmp (o->vtable->klass->name, "DynamicMethod") && finalizing_root_domain) {
151 * These can't be finalized during unloading/shutdown, since that would
152 * free the native code which can still be referenced by other
154 * FIXME: This is not perfect, objects dying at the same time as
155 * dynamic methods can still reference them even when !shutdown.
160 if (mono_runtime_get_no_exec ())
163 /* speedup later... and use a timeout */
164 /* g_print ("Finalize run on %p %s.%s\n", o, mono_object_class (o)->name_space, mono_object_class (o)->name); */
166 /* Use _internal here, since this thread can enter a doomed appdomain */
167 mono_domain_set_internal (mono_object_domain (o));
169 /* delegates that have a native function pointer allocated are
170 * registered for finalization, but they don't have a Finalize
171 * method, because in most cases it's not needed and it's just a waste.
173 if (o->vtable->klass->delegate) {
174 MonoDelegate* del = (MonoDelegate*)o;
175 if (del->delegate_trampoline)
176 mono_delegate_free_ftnptr ((MonoDelegate*)o);
177 mono_domain_set_internal (caller_domain);
181 finalizer = mono_class_get_finalizer (o->vtable->klass);
184 /* If object has a CCW but has no finalizer, it was only
185 * registered for finalization in order to free the CCW.
186 * Else it needs the regular finalizer run.
187 * FIXME: what to do about ressurection and suppression
188 * of finalizer on object with CCW.
190 if (mono_marshal_free_ccw (o) && !finalizer) {
191 mono_domain_set_internal (caller_domain);
197 * To avoid the locking plus the other overhead of mono_runtime_invoke (),
198 * create and precompile a wrapper which calls the finalize method using
201 if (!domain->finalize_runtime_invoke) {
202 MonoMethod *invoke = mono_marshal_get_runtime_invoke (mono_class_get_method_from_name_flags (mono_defaults.object_class, "Finalize", 0, 0), TRUE);
204 domain->finalize_runtime_invoke = mono_compile_method (invoke);
207 runtime_invoke = domain->finalize_runtime_invoke;
209 mono_runtime_class_init (o->vtable);
211 if (G_UNLIKELY (MONO_GC_FINALIZE_INVOKE_ENABLED ())) {
212 MONO_GC_FINALIZE_INVOKE ((unsigned long)o, mono_object_get_size (o),
213 o->vtable->klass->name_space, o->vtable->klass->name);
216 runtime_invoke (o, NULL, &exc, NULL);
219 mono_internal_thread_unhandled_exception (exc);
221 mono_domain_set_internal (caller_domain);
225 mono_gc_finalize_threadpool_threads (void)
227 while (threads_to_finalize) {
228 MonoInternalThread *thread = (MonoInternalThread*) mono_mlist_get_data (threads_to_finalize);
230 /* Force finalization of the thread. */
231 thread->threadpool_thread = FALSE;
232 mono_object_register_finalizer ((MonoObject*)thread);
234 mono_gc_run_finalize (thread, NULL);
236 threads_to_finalize = mono_mlist_next (threads_to_finalize);
241 mono_gc_out_of_memory (size_t size)
244 * we could allocate at program startup some memory that we could release
245 * back to the system at this point if we're really low on memory (ie, size is
246 * lower than the memory we set apart)
248 mono_raise_exception (mono_domain_get ()->out_of_memory_ex);
254 * Some of our objects may point to a different address than the address returned by GC_malloc()
255 * (because of the GetHashCode hack), but we need to pass the real address to register_finalizer.
256 * This also means that in the callback we need to adjust the pointer to get back the real
258 * We also need to be consistent in the use of the GC_debug* variants of malloc and register_finalizer,
259 * since that, too, can cause the underlying pointer to be offset.
262 object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*))
269 mono_raise_exception (mono_get_exception_argument_null ("obj"));
271 domain = obj->vtable->domain;
274 /* This assertion is not valid when GC_DEBUG is defined */
275 g_assert (GC_base (obj) == (char*)obj - offset);
278 if (mono_domain_is_unloading (domain) && (callback != NULL))
280 * Can't register finalizers in a dying appdomain, since they
281 * could be invoked after the appdomain has been unloaded.
285 mono_domain_finalizers_lock (domain);
288 g_hash_table_insert (domain->finalizable_objects_hash, obj, obj);
290 g_hash_table_remove (domain->finalizable_objects_hash, obj);
292 mono_domain_finalizers_unlock (domain);
294 GC_REGISTER_FINALIZER_NO_ORDER ((char*)obj - offset, callback, GUINT_TO_POINTER (offset), NULL, NULL);
295 #elif defined(HAVE_SGEN_GC)
297 mono_raise_exception (mono_get_exception_argument_null ("obj"));
300 * If we register finalizers for domains that are unloading we might
301 * end up running them while or after the domain is being cleared, so
302 * the objects will not be valid anymore.
304 if (!mono_domain_is_unloading (obj->vtable->domain))
305 mono_gc_register_for_finalization (obj, callback);
310 * mono_object_register_finalizer:
311 * @obj: object to register
313 * Records that object @obj has a finalizer, this will call the
314 * Finalize method when the garbage collector disposes the object.
318 mono_object_register_finalizer (MonoObject *obj)
320 /* g_print ("Registered finalizer on %p %s.%s\n", obj, mono_object_class (obj)->name_space, mono_object_class (obj)->name); */
321 object_register_finalizer (obj, mono_gc_run_finalize);
325 * mono_domain_finalize:
326 * @domain: the domain to finalize
327 * @timeout: msects to wait for the finalization to complete, -1 to wait indefinitely
329 * Request finalization of all finalizable objects inside @domain. Wait
330 * @timeout msecs for the finalization to complete.
332 * Returns: TRUE if succeeded, FALSE if there was a timeout
336 mono_domain_finalize (MonoDomain *domain, guint32 timeout)
338 DomainFinalizationReq *req;
341 MonoInternalThread *thread = mono_thread_internal_current ();
343 if (mono_thread_internal_current () == gc_thread)
344 /* We are called from inside a finalizer, not much we can do here */
348 * No need to create another thread 'cause the finalizer thread
349 * is still working and will take care of running the finalizers
356 mono_gc_collect (mono_gc_max_generation ());
358 done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
359 if (done_event == NULL) {
363 req = g_new0 (DomainFinalizationReq, 1);
364 req->domain = domain;
365 req->done_event = done_event;
367 if (domain == mono_get_root_domain ())
368 finalizing_root_domain = TRUE;
370 mono_finalizer_lock ();
372 domains_to_finalize = g_slist_append (domains_to_finalize, req);
374 mono_finalizer_unlock ();
376 /* Tell the finalizer thread to finalize this appdomain */
377 mono_gc_finalize_notify ();
383 res = WaitForSingleObjectEx (done_event, timeout, TRUE);
384 /* printf ("WAIT RES: %d.\n", res); */
386 if (res == WAIT_IO_COMPLETION) {
387 if ((thread->state & (ThreadState_StopRequested | ThreadState_SuspendRequested)) != 0)
389 } else if (res == WAIT_TIMEOUT) {
390 /* We leak the handle here */
397 CloseHandle (done_event);
399 if (domain == mono_get_root_domain ()) {
400 mono_thread_pool_cleanup ();
401 mono_gc_finalize_threadpool_threads ();
406 /* We don't support domain finalization without a GC */
412 ves_icall_System_GC_InternalCollect (int generation)
414 mono_gc_collect (generation);
418 ves_icall_System_GC_GetTotalMemory (MonoBoolean forceCollection)
423 mono_gc_collect (mono_gc_max_generation ());
424 return mono_gc_get_used_size ();
428 ves_icall_System_GC_KeepAlive (MonoObject *obj)
438 ves_icall_System_GC_ReRegisterForFinalize (MonoObject *obj)
441 mono_raise_exception (mono_get_exception_argument_null ("obj"));
443 object_register_finalizer (obj, mono_gc_run_finalize);
447 ves_icall_System_GC_SuppressFinalize (MonoObject *obj)
450 mono_raise_exception (mono_get_exception_argument_null ("obj"));
452 /* delegates have no finalizers, but we register them to deal with the
453 * unmanaged->managed trampoline. We don't let the user suppress it
454 * otherwise we'd leak it.
456 if (obj->vtable->klass->delegate)
459 /* FIXME: Need to handle case where obj has COM Callable Wrapper
460 * generated for it that needs cleaned up, but user wants to suppress
461 * their derived object finalizer. */
463 object_register_finalizer (obj, NULL);
467 ves_icall_System_GC_WaitForPendingFinalizers (void)
470 if (!mono_gc_pending_finalizers ())
473 if (mono_thread_internal_current () == gc_thread)
474 /* Avoid deadlocks */
478 If the finalizer thread is not live, lets pretend no finalizers are pending since the current thread might
479 be the one responsible for starting it up.
481 if (gc_thread == NULL)
484 ResetEvent (pending_done_event);
485 mono_gc_finalize_notify ();
486 /* g_print ("Waiting for pending finalizers....\n"); */
487 WaitForSingleObjectEx (pending_done_event, INFINITE, TRUE);
488 /* g_print ("Done pending....\n"); */
493 ves_icall_System_GC_register_ephemeron_array (MonoObject *array)
496 if (!mono_gc_ephemeron_array_add (array))
497 mono_raise_exception (mono_object_domain (array)->out_of_memory_ex);
502 ves_icall_System_GC_get_ephemeron_tombstone (void)
504 return mono_domain_get ()->ephemeron_tombstone;
507 #define mono_allocator_lock() EnterCriticalSection (&allocator_section)
508 #define mono_allocator_unlock() LeaveCriticalSection (&allocator_section)
509 static CRITICAL_SECTION allocator_section;
510 static CRITICAL_SECTION handle_section;
519 static HandleType mono_gchandle_get_type (guint32 gchandle);
522 ves_icall_System_GCHandle_GetTarget (guint32 handle)
524 return mono_gchandle_get_target (handle);
528 * if type == -1, change the target of the handle, otherwise allocate a new handle.
531 ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint32 type)
534 mono_gchandle_set_target (handle, obj);
535 /* the handle doesn't change */
540 return mono_gchandle_new_weakref (obj, FALSE);
541 case HANDLE_WEAK_TRACK:
542 return mono_gchandle_new_weakref (obj, TRUE);
544 return mono_gchandle_new (obj, FALSE);
546 return mono_gchandle_new (obj, TRUE);
548 g_assert_not_reached ();
554 ves_icall_System_GCHandle_FreeHandle (guint32 handle)
556 mono_gchandle_free (handle);
560 ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle)
564 if (mono_gchandle_get_type (handle) != HANDLE_PINNED)
566 obj = mono_gchandle_get_target (handle);
568 MonoClass *klass = mono_object_class (obj);
569 if (klass == mono_defaults.string_class) {
570 return mono_string_chars ((MonoString*)obj);
571 } else if (klass->rank) {
572 return mono_array_addr ((MonoArray*)obj, char, 0);
574 /* the C# code will check and throw the exception */
575 /* FIXME: missing !klass->blittable test, see bug #61134 */
576 if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT)
578 return (char*)obj + sizeof (MonoObject);
585 ves_icall_Mono_Runtime_SetGCAllowSynchronousMajor (MonoBoolean flag)
587 return mono_gc_set_allow_synchronous_major (flag);
595 guint slot_hint : 24; /* starting slot for search */
596 /* 2^16 appdomains should be enough for everyone (though I know I'll regret this in 20 years) */
597 /* we alloc this only for weak refs, since we can get the domain directly in the other cases */
601 /* weak and weak-track arrays will be allocated in malloc memory
603 static HandleData gc_handles [] = {
604 {NULL, NULL, 0, HANDLE_WEAK, 0},
605 {NULL, NULL, 0, HANDLE_WEAK_TRACK, 0},
606 {NULL, NULL, 0, HANDLE_NORMAL, 0},
607 {NULL, NULL, 0, HANDLE_PINNED, 0}
610 #define lock_handles(handles) EnterCriticalSection (&handle_section)
611 #define unlock_handles(handles) LeaveCriticalSection (&handle_section)
614 find_first_unset (guint32 bitmap)
617 for (i = 0; i < 32; ++i) {
618 if (!(bitmap & (1 << i)))
625 make_root_descr_all_refs (int numbits, gboolean pinned)
631 return mono_gc_make_root_descr_all_refs (numbits);
635 alloc_handle (HandleData *handles, MonoObject *obj, gboolean track)
639 lock_handles (handles);
640 if (!handles->size) {
642 if (handles->type > HANDLE_WEAK_TRACK) {
643 handles->entries = mono_gc_alloc_fixed (sizeof (gpointer) * handles->size, make_root_descr_all_refs (handles->size, handles->type == HANDLE_PINNED));
645 handles->entries = g_malloc0 (sizeof (gpointer) * handles->size);
646 handles->domain_ids = g_malloc0 (sizeof (guint16) * handles->size);
648 handles->bitmap = g_malloc0 (handles->size / 8);
651 for (slot = handles->slot_hint; slot < handles->size / 32; ++slot) {
652 if (handles->bitmap [slot] != 0xffffffff) {
653 i = find_first_unset (handles->bitmap [slot]);
654 handles->slot_hint = slot;
658 if (i == -1 && handles->slot_hint != 0) {
659 for (slot = 0; slot < handles->slot_hint; ++slot) {
660 if (handles->bitmap [slot] != 0xffffffff) {
661 i = find_first_unset (handles->bitmap [slot]);
662 handles->slot_hint = slot;
669 guint32 new_size = handles->size * 2; /* always double: we memset to 0 based on this below */
671 /* resize and copy the bitmap */
672 new_bitmap = g_malloc0 (new_size / 8);
673 memcpy (new_bitmap, handles->bitmap, handles->size / 8);
674 g_free (handles->bitmap);
675 handles->bitmap = new_bitmap;
677 /* resize and copy the entries */
678 if (handles->type > HANDLE_WEAK_TRACK) {
681 entries = mono_gc_alloc_fixed (sizeof (gpointer) * new_size, make_root_descr_all_refs (new_size, handles->type == HANDLE_PINNED));
682 mono_gc_memmove (entries, handles->entries, sizeof (gpointer) * handles->size);
684 mono_gc_free_fixed (handles->entries);
685 handles->entries = entries;
689 domain_ids = g_malloc0 (sizeof (guint16) * new_size);
690 entries = g_malloc0 (sizeof (gpointer) * new_size);
691 memcpy (domain_ids, handles->domain_ids, sizeof (guint16) * handles->size);
692 for (i = 0; i < handles->size; ++i) {
693 MonoObject *obj = mono_gc_weak_link_get (&(handles->entries [i]));
695 mono_gc_weak_link_add (&(entries [i]), obj, track);
696 mono_gc_weak_link_remove (&(handles->entries [i]), track);
698 g_assert (!handles->entries [i]);
701 g_free (handles->entries);
702 g_free (handles->domain_ids);
703 handles->entries = entries;
704 handles->domain_ids = domain_ids;
707 /* set i and slot to the next free position */
709 slot = (handles->size + 1) / 32;
710 handles->slot_hint = handles->size + 1;
711 handles->size = new_size;
713 handles->bitmap [slot] |= 1 << i;
714 slot = slot * 32 + i;
715 handles->entries [slot] = NULL;
716 if (handles->type <= HANDLE_WEAK_TRACK) {
717 /*FIXME, what to use when obj == null?*/
718 handles->domain_ids [slot] = (obj ? mono_object_get_domain (obj) : mono_domain_get ())->domain_id;
720 mono_gc_weak_link_add (&(handles->entries [slot]), obj, track);
722 handles->entries [slot] = obj;
725 #ifndef DISABLE_PERFCOUNTERS
726 mono_perfcounters->gc_num_handles++;
728 unlock_handles (handles);
729 /*g_print ("allocated entry %d of type %d to object %p (in slot: %p)\n", slot, handles->type, obj, handles->entries [slot]);*/
730 res = (slot << 3) | (handles->type + 1);
731 mono_profiler_gc_handle (MONO_PROFILER_GC_HANDLE_CREATED, handles->type, res, obj);
737 * @obj: managed object to get a handle for
738 * @pinned: whether the object should be pinned
740 * This returns a handle that wraps the object, this is used to keep a
741 * reference to a managed object from the unmanaged world and preventing the
742 * object from being disposed.
744 * If @pinned is false the address of the object can not be obtained, if it is
745 * true the address of the object can be obtained. This will also pin the
746 * object so it will not be possible by a moving garbage collector to move the
749 * Returns: a handle that can be used to access the object from
753 mono_gchandle_new (MonoObject *obj, gboolean pinned)
755 return alloc_handle (&gc_handles [pinned? HANDLE_PINNED: HANDLE_NORMAL], obj, FALSE);
759 * mono_gchandle_new_weakref:
760 * @obj: managed object to get a handle for
761 * @pinned: whether the object should be pinned
763 * This returns a weak handle that wraps the object, this is used to
764 * keep a reference to a managed object from the unmanaged world.
765 * Unlike the mono_gchandle_new the object can be reclaimed by the
766 * garbage collector. In this case the value of the GCHandle will be
769 * If @pinned is false the address of the object can not be obtained, if it is
770 * true the address of the object can be obtained. This will also pin the
771 * object so it will not be possible by a moving garbage collector to move the
774 * Returns: a handle that can be used to access the object from
778 mono_gchandle_new_weakref (MonoObject *obj, gboolean track_resurrection)
780 guint32 handle = alloc_handle (&gc_handles [track_resurrection? HANDLE_WEAK_TRACK: HANDLE_WEAK], obj, track_resurrection);
786 mono_gchandle_get_type (guint32 gchandle)
788 guint type = (gchandle & 7) - 1;
794 * mono_gchandle_get_target:
795 * @gchandle: a GCHandle's handle.
797 * The handle was previously created by calling mono_gchandle_new or
798 * mono_gchandle_new_weakref.
800 * Returns a pointer to the MonoObject represented by the handle or
801 * NULL for a collected object if using a weakref handle.
804 mono_gchandle_get_target (guint32 gchandle)
806 guint slot = gchandle >> 3;
807 guint type = (gchandle & 7) - 1;
808 HandleData *handles = &gc_handles [type];
809 MonoObject *obj = NULL;
812 lock_handles (handles);
813 if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
814 if (handles->type <= HANDLE_WEAK_TRACK) {
815 obj = mono_gc_weak_link_get (&handles->entries [slot]);
817 obj = handles->entries [slot];
820 /* print a warning? */
822 unlock_handles (handles);
823 /*g_print ("get target of entry %d of type %d: %p\n", slot, handles->type, obj);*/
828 mono_gchandle_set_target (guint32 gchandle, MonoObject *obj)
830 guint slot = gchandle >> 3;
831 guint type = (gchandle & 7) - 1;
832 HandleData *handles = &gc_handles [type];
833 MonoObject *old_obj = NULL;
837 lock_handles (handles);
838 if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
839 if (handles->type <= HANDLE_WEAK_TRACK) {
840 old_obj = handles->entries [slot];
841 if (handles->entries [slot])
842 mono_gc_weak_link_remove (&handles->entries [slot], handles->type == HANDLE_WEAK_TRACK);
844 mono_gc_weak_link_add (&handles->entries [slot], obj, handles->type == HANDLE_WEAK_TRACK);
845 /*FIXME, what to use when obj == null?*/
846 handles->domain_ids [slot] = (obj ? mono_object_get_domain (obj) : mono_domain_get ())->domain_id;
848 handles->entries [slot] = obj;
851 /* print a warning? */
853 /*g_print ("changed entry %d of type %d to object %p (in slot: %p)\n", slot, handles->type, obj, handles->entries [slot]);*/
854 unlock_handles (handles);
858 * mono_gchandle_is_in_domain:
859 * @gchandle: a GCHandle's handle.
860 * @domain: An application domain.
862 * Returns: true if the object wrapped by the @gchandle belongs to the specific @domain.
865 mono_gchandle_is_in_domain (guint32 gchandle, MonoDomain *domain)
867 guint slot = gchandle >> 3;
868 guint type = (gchandle & 7) - 1;
869 HandleData *handles = &gc_handles [type];
870 gboolean result = FALSE;
873 lock_handles (handles);
874 if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
875 if (handles->type <= HANDLE_WEAK_TRACK) {
876 result = domain->domain_id == handles->domain_ids [slot];
879 obj = handles->entries [slot];
883 result = domain == mono_object_domain (obj);
886 /* print a warning? */
888 unlock_handles (handles);
893 * mono_gchandle_free:
894 * @gchandle: a GCHandle's handle.
896 * Frees the @gchandle handle. If there are no outstanding
897 * references, the garbage collector can reclaim the memory of the
901 mono_gchandle_free (guint32 gchandle)
903 guint slot = gchandle >> 3;
904 guint type = (gchandle & 7) - 1;
905 HandleData *handles = &gc_handles [type];
909 lock_handles (handles);
910 if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
911 if (handles->type <= HANDLE_WEAK_TRACK) {
912 if (handles->entries [slot])
913 mono_gc_weak_link_remove (&handles->entries [slot], handles->type == HANDLE_WEAK_TRACK);
915 handles->entries [slot] = NULL;
917 handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
919 /* print a warning? */
921 #ifndef DISABLE_PERFCOUNTERS
922 mono_perfcounters->gc_num_handles--;
924 /*g_print ("freed entry %d of type %d\n", slot, handles->type);*/
925 unlock_handles (handles);
926 mono_profiler_gc_handle (MONO_PROFILER_GC_HANDLE_DESTROYED, handles->type, gchandle, NULL);
930 * mono_gchandle_free_domain:
931 * @domain: domain that is unloading
933 * Function used internally to cleanup any GC handle for objects belonging
934 * to the specified domain during appdomain unload.
937 mono_gchandle_free_domain (MonoDomain *domain)
941 for (type = 0; type < 3; ++type) {
943 HandleData *handles = &gc_handles [type];
944 lock_handles (handles);
945 for (slot = 0; slot < handles->size; ++slot) {
946 if (!(handles->bitmap [slot / 32] & (1 << (slot % 32))))
948 if (type <= HANDLE_WEAK_TRACK) {
949 if (domain->domain_id == handles->domain_ids [slot]) {
950 handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
951 if (handles->entries [slot])
952 mono_gc_weak_link_remove (&handles->entries [slot], handles->type == HANDLE_WEAK_TRACK);
955 if (handles->entries [slot] && mono_object_domain (handles->entries [slot]) == domain) {
956 handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
957 handles->entries [slot] = NULL;
961 unlock_handles (handles);
967 GCHandle_CheckCurrentDomain (guint32 gchandle)
969 return mono_gchandle_is_in_domain (gchandle, mono_domain_get ());
974 #ifdef MONO_HAS_SEMAPHORES
975 static MonoSemType finalizer_sem;
977 static HANDLE finalizer_event;
978 static volatile gboolean finished=FALSE;
981 mono_gc_finalize_notify (void)
984 g_message ( "%s: prodding finalizer", __func__);
987 #ifdef MONO_HAS_SEMAPHORES
988 MONO_SEM_POST (&finalizer_sem);
990 SetEvent (finalizer_event);
997 collect_objects (gpointer key, gpointer value, gpointer user_data)
999 GPtrArray *arr = (GPtrArray*)user_data;
1000 g_ptr_array_add (arr, key);
1006 * finalize_domain_objects:
1008 * Run the finalizers of all finalizable objects in req->domain.
1011 finalize_domain_objects (DomainFinalizationReq *req)
1013 MonoDomain *domain = req->domain;
1016 #define NUM_FOBJECTS 64
1017 MonoObject *to_finalize [NUM_FOBJECTS];
1021 /* Process finalizers which are already in the queue */
1022 mono_gc_invoke_finalizers ();
1024 #ifdef HAVE_BOEHM_GC
1025 while (g_hash_table_size (domain->finalizable_objects_hash) > 0) {
1029 * Since the domain is unloading, nobody is allowed to put
1030 * new entries into the hash table. But finalize_object might
1031 * remove entries from the hash table, so we make a copy.
1033 objs = g_ptr_array_new ();
1034 g_hash_table_foreach (domain->finalizable_objects_hash, collect_objects, objs);
1035 /* printf ("FINALIZING %d OBJECTS.\n", objs->len); */
1037 for (i = 0; i < objs->len; ++i) {
1038 MonoObject *o = (MonoObject*)g_ptr_array_index (objs, i);
1039 /* FIXME: Avoid finalizing threads, etc */
1040 mono_gc_run_finalize (o, 0);
1043 g_ptr_array_free (objs, TRUE);
1045 #elif defined(HAVE_SGEN_GC)
1046 while ((count = mono_gc_finalizers_for_domain (domain, to_finalize, NUM_FOBJECTS))) {
1048 for (i = 0; i < count; ++i) {
1049 mono_gc_run_finalize (to_finalize [i], 0);
1054 /* cleanup the reference queue */
1055 reference_queue_clear_for_domain (domain);
1057 /* printf ("DONE.\n"); */
1058 SetEvent (req->done_event);
1060 /* The event is closed in mono_domain_finalize if we get here */
1065 finalizer_thread (gpointer unused)
1068 /* Wait to be notified that there's at least one
1072 g_assert (mono_domain_get () == mono_get_root_domain ());
1074 /* An alertable wait is required so this thread can be suspended on windows */
1075 #ifdef MONO_HAS_SEMAPHORES
1076 MONO_SEM_WAIT_ALERTABLE (&finalizer_sem, TRUE);
1078 WaitForSingleObjectEx (finalizer_event, INFINITE, TRUE);
1081 mono_threads_perform_thread_dump ();
1083 mono_console_handle_async_ops ();
1085 #ifndef DISABLE_ATTACH
1086 mono_attach_maybe_start ();
1089 if (domains_to_finalize) {
1090 mono_finalizer_lock ();
1091 if (domains_to_finalize) {
1092 DomainFinalizationReq *req = domains_to_finalize->data;
1093 domains_to_finalize = g_slist_remove (domains_to_finalize, req);
1094 mono_finalizer_unlock ();
1096 finalize_domain_objects (req);
1098 mono_finalizer_unlock ();
1102 /* If finished == TRUE, mono_gc_cleanup has been called (from mono_runtime_cleanup),
1103 * before the domain is unloaded.
1105 mono_gc_invoke_finalizers ();
1107 reference_queue_proccess_all ();
1109 SetEvent (pending_done_event);
1112 SetEvent (shutdown_event);
1116 #ifndef LAZY_GC_THREAD_CREATION
1120 mono_gc_init_finalizer_thread (void)
1122 gc_thread = mono_thread_create_internal (mono_domain_get (), finalizer_thread, NULL, FALSE, TRUE, 0);
1123 ves_icall_System_Threading_Thread_SetName_internal (gc_thread, mono_string_new (mono_domain_get (), "Finalizer"));
1129 InitializeCriticalSection (&handle_section);
1130 InitializeCriticalSection (&allocator_section);
1132 InitializeCriticalSection (&finalizer_mutex);
1133 InitializeCriticalSection (&reference_queue_mutex);
1135 MONO_GC_REGISTER_ROOT_FIXED (gc_handles [HANDLE_NORMAL].entries);
1136 MONO_GC_REGISTER_ROOT_FIXED (gc_handles [HANDLE_PINNED].entries);
1138 mono_counters_register ("Minor GC collections", MONO_COUNTER_GC | MONO_COUNTER_INT, &gc_stats.minor_gc_count);
1139 mono_counters_register ("Major GC collections", MONO_COUNTER_GC | MONO_COUNTER_INT, &gc_stats.major_gc_count);
1140 mono_counters_register ("Minor GC time", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &gc_stats.minor_gc_time_usecs);
1141 mono_counters_register ("Major GC time", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &gc_stats.major_gc_time_usecs);
1143 mono_gc_base_init ();
1145 if (mono_gc_is_disabled ()) {
1150 finalizer_event = CreateEvent (NULL, FALSE, FALSE, NULL);
1151 pending_done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1152 shutdown_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1153 if (finalizer_event == NULL || pending_done_event == NULL || shutdown_event == NULL) {
1154 g_assert_not_reached ();
1156 #ifdef MONO_HAS_SEMAPHORES
1157 MONO_SEM_INIT (&finalizer_sem, 0);
1160 #ifndef LAZY_GC_THREAD_CREATION
1161 mono_gc_init_finalizer_thread ();
1166 mono_gc_cleanup (void)
1169 g_message ("%s: cleaning up finalizer", __func__);
1173 ResetEvent (shutdown_event);
1175 if (mono_thread_internal_current () != gc_thread) {
1176 mono_gc_finalize_notify ();
1177 /* Finishing the finalizer thread, so wait a little bit... */
1178 /* MS seems to wait for about 2 seconds */
1179 if (WaitForSingleObjectEx (shutdown_event, 2000, FALSE) == WAIT_TIMEOUT) {
1182 /* Set a flag which the finalizer thread can check */
1183 suspend_finalizers = TRUE;
1185 /* Try to abort the thread, in the hope that it is running managed code */
1186 mono_thread_internal_stop (gc_thread);
1188 /* Wait for it to stop */
1189 ret = WaitForSingleObjectEx (gc_thread->handle, 100, TRUE);
1191 if (ret == WAIT_TIMEOUT) {
1193 * The finalizer thread refused to die. There is not much we
1194 * can do here, since the runtime is shutting down so the
1195 * state the finalizer thread depends on will vanish.
1197 g_warning ("Shutting down finalizer thread timed out.");
1200 * FIXME: On unix, when the above wait returns, the thread
1201 * might still be running io-layer code, or pthreads code.
1208 /* Wait for the thread to actually exit */
1209 ret = WaitForSingleObjectEx (gc_thread->handle, INFINITE, TRUE);
1210 g_assert (ret == WAIT_OBJECT_0);
1214 * The above wait only waits for the exited event to be signalled, the thread might still be running. To fix this race, we
1215 * create the finalizer thread without calling pthread_detach () on it, so we can wait for it manually.
1217 ret = pthread_join ((gpointer)(gsize)gc_thread->tid, NULL);
1218 g_assert (ret == 0);
1223 #ifdef HAVE_BOEHM_GC
1224 GC_finalizer_notifier = NULL;
1228 mono_reference_queue_cleanup ();
1230 DeleteCriticalSection (&handle_section);
1231 DeleteCriticalSection (&allocator_section);
1232 DeleteCriticalSection (&finalizer_mutex);
1233 DeleteCriticalSection (&reference_queue_mutex);
1238 /* Null GC dummy functions */
1240 mono_gc_finalize_notify (void)
1244 void mono_gc_init (void)
1246 InitializeCriticalSection (&handle_section);
1249 void mono_gc_cleanup (void)
1256 mono_gc_is_finalizer_internal_thread (MonoInternalThread *thread)
1258 return thread == gc_thread;
1262 * mono_gc_is_finalizer_thread:
1263 * @thread: the thread to test.
1265 * In Mono objects are finalized asynchronously on a separate thread.
1266 * This routine tests whether the @thread argument represents the
1267 * finalization thread.
1269 * Returns true if @thread is the finalization thread.
1272 mono_gc_is_finalizer_thread (MonoThread *thread)
1274 return mono_gc_is_finalizer_internal_thread (thread->internal_thread);
1277 #if defined(__MACH__)
1278 static pthread_t mach_exception_thread;
1281 mono_gc_register_mach_exception_thread (pthread_t thread)
1283 mach_exception_thread = thread;
1287 mono_gc_get_mach_exception_thread (void)
1289 return mach_exception_thread;
1294 * mono_gc_parse_environment_string_extract_number:
1296 * @str: points to the first digit of the number
1297 * @out: pointer to the variable that will receive the value
1299 * Tries to extract a number from the passed string, taking in to account m, k
1302 * Returns true if passing was successful
1305 mono_gc_parse_environment_string_extract_number (const char *str, glong *out)
1308 int len = strlen (str), shift = 0;
1310 gboolean is_suffix = FALSE;
1316 suffix = str [len - 1];
1331 if (!isdigit (suffix))
1337 val = strtol (str, &endptr, 10);
1339 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
1340 || (errno != 0 && val == 0) || (endptr == str))
1346 if (val < 0) /* negative numbers cannot be suffixed */
1348 if (*(endptr + 1)) /* Invalid string. */
1351 unshifted = (gulong)val;
1353 if (val < 0) /* overflow */
1355 if (((gulong)val >> shift) != unshifted) /* value too large */
1363 #ifndef HAVE_SGEN_GC
1365 mono_gc_alloc_mature (MonoVTable *vtable)
1367 return mono_object_new_specific (vtable);
1372 static MonoReferenceQueue *ref_queues;
1375 ref_list_remove_element (RefQueueEntry **prev, RefQueueEntry *element)
1378 /* Guard if head is changed concurrently. */
1379 while (*prev != element)
1380 prev = &(*prev)->next;
1381 } while (prev && InterlockedCompareExchangePointer ((void*)prev, element->next, element) != element);
1385 ref_list_push (RefQueueEntry **head, RefQueueEntry *value)
1387 RefQueueEntry *current;
1390 value->next = current;
1391 STORE_STORE_FENCE; /*Must make sure the previous store is visible before the CAS. */
1392 } while (InterlockedCompareExchangePointer ((void*)head, value, current) != current);
1396 reference_queue_proccess (MonoReferenceQueue *queue)
1398 RefQueueEntry **iter = &queue->queue;
1399 RefQueueEntry *entry;
1400 while ((entry = *iter)) {
1402 if (queue->should_be_deleted || !mono_gc_weak_link_get (&entry->dis_link)) {
1403 mono_gc_weak_link_remove (&entry->dis_link, TRUE);
1405 if (queue->should_be_deleted || !mono_gchandle_get_target (entry->gchandle)) {
1406 mono_gchandle_free ((guint32)entry->gchandle);
1408 ref_list_remove_element (iter, entry);
1409 queue->callback (entry->user_data);
1412 iter = &entry->next;
1418 reference_queue_proccess_all (void)
1420 MonoReferenceQueue **iter;
1421 MonoReferenceQueue *queue = ref_queues;
1422 for (; queue; queue = queue->next)
1423 reference_queue_proccess (queue);
1426 EnterCriticalSection (&reference_queue_mutex);
1427 for (iter = &ref_queues; *iter;) {
1429 if (!queue->should_be_deleted) {
1430 iter = &queue->next;
1434 LeaveCriticalSection (&reference_queue_mutex);
1435 reference_queue_proccess (queue);
1438 *iter = queue->next;
1441 LeaveCriticalSection (&reference_queue_mutex);
1445 mono_reference_queue_cleanup (void)
1447 MonoReferenceQueue *queue = ref_queues;
1448 for (; queue; queue = queue->next)
1449 queue->should_be_deleted = TRUE;
1450 reference_queue_proccess_all ();
1454 reference_queue_clear_for_domain (MonoDomain *domain)
1456 MonoReferenceQueue *queue = ref_queues;
1457 for (; queue; queue = queue->next) {
1458 RefQueueEntry **iter = &queue->queue;
1459 RefQueueEntry *entry;
1460 while ((entry = *iter)) {
1463 obj = mono_gc_weak_link_get (&entry->dis_link);
1464 if (obj && mono_object_domain (obj) == domain) {
1465 mono_gc_weak_link_remove (&entry->dis_link, TRUE);
1467 obj = mono_gchandle_get_target (entry->gchandle);
1468 if (obj && mono_object_domain (obj) == domain) {
1469 mono_gchandle_free ((guint32)entry->gchandle);
1471 ref_list_remove_element (iter, entry);
1472 queue->callback (entry->user_data);
1475 iter = &entry->next;
1481 * mono_gc_reference_queue_new:
1482 * @callback callback used when processing dead entries.
1484 * Create a new reference queue used to process collected objects.
1485 * A reference queue let you queue a pair (managed object, user data)
1486 * using the mono_gc_reference_queue_add method.
1488 * Once the managed object is collected @callback will be called
1489 * in the finalizer thread with 'user data' as argument.
1491 * The callback is called without any locks held.
1494 mono_gc_reference_queue_new (mono_reference_queue_callback callback)
1496 MonoReferenceQueue *res = g_new0 (MonoReferenceQueue, 1);
1497 res->callback = callback;
1499 EnterCriticalSection (&reference_queue_mutex);
1500 res->next = ref_queues;
1502 LeaveCriticalSection (&reference_queue_mutex);
1508 * mono_gc_reference_queue_add:
1509 * @queue the queue to add the reference to.
1510 * @obj the object to be watched for collection
1511 * @user_data parameter to be passed to the queue callback
1513 * Queue an object to be watched for collection, when the @obj is
1514 * collected, the callback that was registered for the @queue will
1515 * be invoked with the @obj and @user_data arguments.
1517 * @returns false if the queue is scheduled to be freed.
1520 mono_gc_reference_queue_add (MonoReferenceQueue *queue, MonoObject *obj, void *user_data)
1522 RefQueueEntry *entry;
1523 if (queue->should_be_deleted)
1526 entry = g_new0 (RefQueueEntry, 1);
1527 entry->user_data = user_data;
1530 mono_gc_weak_link_add (&entry->dis_link, obj, TRUE);
1532 entry->gchandle = mono_gchandle_new_weakref (obj, TRUE);
1533 mono_object_register_finalizer (obj);
1536 ref_list_push (&queue->queue, entry);
1541 * mono_gc_reference_queue_free:
1542 * @queue the queue that should be deleted.
1544 * This operation signals that @queue should be deleted. This operation is deferred
1545 * as it happens on the finalizer thread.
1547 * After this call, no further objects can be queued. It's the responsibility of the
1548 * caller to make sure that no further attempt to access queue will be made.
1551 mono_gc_reference_queue_free (MonoReferenceQueue *queue)
1553 queue->should_be_deleted = TRUE;
1556 #define ptr_mask ((sizeof (void*) - 1))
1557 #define _toi(ptr) ((size_t)ptr)
1558 #define unaligned_bytes(ptr) (_toi(ptr) & ptr_mask)
1559 #define align_down(ptr) ((void*)(_toi(ptr) & ~ptr_mask))
1560 #define align_up(ptr) ((void*) ((_toi(ptr) + ptr_mask) & ~ptr_mask))
1564 * @dest: address to start to clear
1565 * @size: size of the region to clear
1567 * Zero @size bytes starting at @dest.
1569 * Use this to zero memory that can hold managed pointers.
1571 * FIXME borrow faster code from some BSD libc or bionic
1574 mono_gc_bzero (void *dest, size_t size)
1576 char *p = (char*)dest;
1577 char *end = p + size;
1578 char *align_end = align_up (p);
1581 while (p < align_end)
1584 word_end = align_down (end);
1585 while (p < word_end) {
1586 *((void**)p) = NULL;
1587 p += sizeof (void*);
1597 * @dest: destination of the move
1599 * @size: size of the block to move
1601 * Move @size bytes from @src to @dest.
1602 * size MUST be a multiple of sizeof (gpointer)
1604 * FIXME borrow faster code from some BSD libc or bionic
1607 mono_gc_memmove (void *dest, const void *src, size_t size)
1610 * If dest and src are differently aligned with respect to
1611 * pointer size then it makes no sense to do aligned copying.
1612 * In fact, we would end up with unaligned loads which is
1613 * incorrect on some architectures.
1615 if ((char*)dest - (char*)align_down (dest) != (char*)src - (char*)align_down (src)) {
1616 memmove (dest, src, size);
1621 * A bit of explanation on why we align only dest before doing word copies.
1622 * Pointers to managed objects must always be stored in word aligned addresses, so
1623 * even if dest is misaligned, src will be by the same amount - this ensure proper atomicity of reads.
1625 if (dest > src && ((size_t)((char*)dest - (char*)src) < size)) {
1626 char *p = (char*)dest + size;
1627 char *s = (char*)src + size;
1628 char *start = (char*)dest;
1629 char *align_end = MAX((char*)dest, (char*)align_down (p));
1632 while (p > align_end)
1635 word_start = align_up (start);
1636 while (p > word_start) {
1637 p -= sizeof (void*);
1638 s -= sizeof (void*);
1639 *((void**)p) = *((void**)s);
1645 char *p = (char*)dest;
1646 char *s = (char*)src;
1647 char *end = p + size;
1648 char *align_end = MIN ((char*)end, (char*)align_up (p));
1651 while (p < align_end)
1654 word_end = align_down (end);
1655 while (p < word_end) {
1656 *((void**)p) = *((void**)s);
1657 p += sizeof (void*);
1658 s += sizeof (void*);