2 * metadata/gc.c: GC icalls.
4 * Author: Paolo Molaro <lupus@ximian.com>
6 * (C) 2002 Ximian, Inc.
13 #include <mono/metadata/gc-internal.h>
14 #include <mono/metadata/mono-gc.h>
15 #include <mono/metadata/threads.h>
16 #include <mono/metadata/tabledefs.h>
17 #include <mono/metadata/exception.h>
18 #include <mono/metadata/profiler-private.h>
19 #include <mono/metadata/domain-internals.h>
20 #include <mono/metadata/class-internals.h>
21 #include <mono/utils/mono-logger.h>
22 #include <mono/os/gc_wrapper.h>
23 #include <mono/metadata/marshal.h> /* for mono_delegate_free_ftnptr () */
25 typedef struct DomainFinalizationReq {
28 } DomainFinalizationReq;
30 #ifdef PLATFORM_WINCE /* FIXME: add accessors to gc.dll API */
31 extern void (*__imp_GC_finalizer_notifier)(void);
32 #define GC_finalizer_notifier __imp_GC_finalizer_notifier
33 extern int __imp_GC_finalize_on_demand;
34 #define GC_finalize_on_demand __imp_GC_finalize_on_demand
37 static gboolean gc_disabled = FALSE;
39 #define mono_finalizer_lock() EnterCriticalSection (&finalizer_mutex)
40 #define mono_finalizer_unlock() LeaveCriticalSection (&finalizer_mutex)
41 static CRITICAL_SECTION finalizer_mutex;
43 static GSList *domains_to_finalize= NULL;
45 static MonoThread *gc_thread;
47 static void object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*));
50 static HANDLE pending_done_event;
51 static HANDLE shutdown_event;
52 static HANDLE thread_started_event;
56 * actually, we might want to queue the finalize requests in a separate thread,
57 * but we need to be careful about the execution domain of the thread...
60 run_finalize (void *obj, void *data)
62 MonoObject *exc = NULL;
64 MonoMethod* finalizer = NULL;
65 o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data));
68 mono_domain_lock (o->vtable->domain);
70 o2 = g_hash_table_lookup (o->vtable->domain->finalizable_objects_hash, o);
72 mono_domain_unlock (o->vtable->domain);
75 /* Already finalized somehow */
79 /* make sure the finalizer is not called again if the object is resurrected */
80 object_register_finalizer (obj, NULL);
82 if (o->vtable->klass == mono_get_thread_class ())
83 if (mono_gc_is_finalizer_thread ((MonoThread*)o))
84 /* Avoid finalizing ourselves */
87 /* speedup later... and use a timeout */
88 /* g_print ("Finalize run on %p %s.%s\n", o, mono_object_class (o)->name_space, mono_object_class (o)->name); */
90 /* Use _internal here, since this thread can enter a doomed appdomain */
91 mono_domain_set_internal (mono_object_domain (o));
93 /* delegates that have a native function pointer allocated are
94 * registered for finalization, but they don't have a Finalize
95 * method, because in most cases it's not needed and it's just a waste.
97 if (o->vtable->klass->delegate) {
98 MonoDelegate* del = (MonoDelegate*)o;
99 if (del->delegate_trampoline)
100 mono_delegate_free_ftnptr ((MonoDelegate*)o);
104 finalizer = mono_class_get_finalizer (o->vtable->klass);
106 /* If object has a CCW but has no finalizer, it was only
107 * registered for finalization in order to free the CCW.
108 * Else it needs the regular finalizer run.
109 * FIXME: what to do about ressurection and suppression
110 * of finalizer on object with CCW.
112 if (mono_marshal_free_ccw (o) && !finalizer)
115 mono_runtime_invoke (finalizer, o, NULL, &exc);
118 /* fixme: do something useful */
123 mono_gc_out_of_memory (size_t size)
126 * we could allocate at program startup some memory that we could release
127 * back to the system at this point if we're really low on memory (ie, size is
128 * lower than the memory we set apart)
130 mono_raise_exception (mono_domain_get ()->out_of_memory_ex);
136 * Some of our objects may point to a different address than the address returned by GC_malloc()
137 * (because of the GetHashCode hack), but we need to pass the real address to register_finalizer.
138 * This also means that in the callback we need to adjust the pointer to get back the real
140 * We also need to be consistent in the use of the GC_debug* variants of malloc and register_finalizer,
141 * since that, too, can cause the underlying pointer to be offset.
144 object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*))
150 /* This assertion is not valid when GC_DEBUG is defined */
151 g_assert (GC_base (obj) == (char*)obj - offset);
154 if (mono_domain_is_unloading (obj->vtable->domain) && (callback != NULL))
156 * Can't register finalizers in a dying appdomain, since they
157 * could be invoked after the appdomain has been unloaded.
161 mono_domain_lock (obj->vtable->domain);
164 g_hash_table_insert (obj->vtable->domain->finalizable_objects_hash, obj,
167 g_hash_table_remove (obj->vtable->domain->finalizable_objects_hash, obj);
169 mono_domain_unlock (obj->vtable->domain);
171 GC_REGISTER_FINALIZER_NO_ORDER ((char*)obj - offset, callback, GUINT_TO_POINTER (offset), NULL, NULL);
172 #elif defined(HAVE_SGEN_GC)
173 mono_gc_register_for_finalization (obj, callback);
178 * mono_object_register_finalizer:
179 * @obj: object to register
181 * Records that object @obj has a finalizer, this will call the
182 * Finalize method when the garbage collector disposes the object.
186 mono_object_register_finalizer (MonoObject *obj)
188 /* g_print ("Registered finalizer on %p %s.%s\n", obj, mono_object_class (obj)->name_space, mono_object_class (obj)->name); */
189 object_register_finalizer (obj, run_finalize);
193 * mono_domain_finalize:
194 * @domain: the domain to finalize
195 * @timeout: msects to wait for the finalization to complete
197 * Request finalization of all finalizable objects inside @domain. Wait
198 * @timeout msecs for the finalization to complete.
200 * Returns: TRUE if succeeded, FALSE if there was a timeout
204 mono_domain_finalize (MonoDomain *domain, guint32 timeout)
206 DomainFinalizationReq *req;
210 if (mono_thread_current () == gc_thread)
211 /* We are called from inside a finalizer, not much we can do here */
214 mono_profiler_appdomain_event (domain, MONO_PROFILE_START_UNLOAD);
217 * No need to create another thread 'cause the finalizer thread
218 * is still working and will take care of running the finalizers
225 mono_gc_collect (mono_gc_max_generation ());
227 done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
228 if (done_event == NULL) {
232 req = g_new0 (DomainFinalizationReq, 1);
233 req->domain = domain;
234 req->done_event = done_event;
236 mono_finalizer_lock ();
238 domains_to_finalize = g_slist_append (domains_to_finalize, req);
240 mono_finalizer_unlock ();
242 /* Tell the finalizer thread to finalize this appdomain */
243 mono_gc_finalize_notify ();
245 res = WaitForSingleObjectEx (done_event, timeout, TRUE);
247 /* printf ("WAIT RES: %d.\n", res); */
248 if (res == WAIT_TIMEOUT) {
249 /* We leak the handle here */
253 CloseHandle (done_event);
256 /* We don't support domain finalization without a GC */
262 ves_icall_System_GC_InternalCollect (int generation)
264 mono_gc_collect (generation);
268 ves_icall_System_GC_GetTotalMemory (MonoBoolean forceCollection)
273 mono_gc_collect (mono_gc_max_generation ());
274 return mono_gc_get_used_size ();
278 ves_icall_System_GC_KeepAlive (MonoObject *obj)
288 ves_icall_System_GC_ReRegisterForFinalize (MonoObject *obj)
292 object_register_finalizer (obj, run_finalize);
296 ves_icall_System_GC_SuppressFinalize (MonoObject *obj)
300 /* delegates have no finalizers, but we register them to deal with the
301 * unmanaged->managed trampoline. We don't let the user suppress it
302 * otherwise we'd leak it.
304 if (obj->vtable->klass->delegate)
307 /* FIXME: Need to handle case where obj has COM Callable Wrapper
308 * generated for it that needs cleaned up, but user wants to suppress
309 * their derived object finalizer. */
311 object_register_finalizer (obj, NULL);
315 ves_icall_System_GC_WaitForPendingFinalizers (void)
320 if (!mono_gc_pending_finalizers ())
323 if (mono_thread_current () == gc_thread)
324 /* Avoid deadlocks */
327 ResetEvent (pending_done_event);
328 mono_gc_finalize_notify ();
329 /* g_print ("Waiting for pending finalizers....\n"); */
330 WaitForSingleObjectEx (pending_done_event, INFINITE, TRUE);
331 /* g_print ("Done pending....\n"); */
335 #define mono_allocator_lock() EnterCriticalSection (&allocator_section)
336 #define mono_allocator_unlock() LeaveCriticalSection (&allocator_section)
337 static CRITICAL_SECTION allocator_section;
338 static CRITICAL_SECTION handle_section;
347 static void mono_gchandle_set_target (guint32 gchandle, MonoObject *obj);
350 ves_icall_System_GCHandle_GetTarget (guint32 handle)
352 return mono_gchandle_get_target (handle);
356 * if type == -1, change the target of the handle, otherwise allocate a new handle.
359 ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint32 type)
362 mono_gchandle_set_target (handle, obj);
363 /* the handle doesn't change */
368 return mono_gchandle_new_weakref (obj, FALSE);
369 case HANDLE_WEAK_TRACK:
370 return mono_gchandle_new_weakref (obj, TRUE);
372 return mono_gchandle_new (obj, FALSE);
374 return mono_gchandle_new (obj, TRUE);
376 g_assert_not_reached ();
382 ves_icall_System_GCHandle_FreeHandle (guint32 handle)
384 mono_gchandle_free (handle);
388 ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle)
392 obj = mono_gchandle_get_target (handle);
394 MonoClass *klass = mono_object_class (obj);
395 if (klass == mono_defaults.string_class) {
396 return mono_string_chars ((MonoString*)obj);
397 } else if (klass->rank) {
398 return mono_array_addr ((MonoArray*)obj, char, 0);
400 /* the C# code will check and throw the exception */
401 /* FIXME: missing !klass->blittable test, see bug #61134 */
402 if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT)
404 return (char*)obj + sizeof (MonoObject);
415 guint slot_hint : 24; /* starting slot for search */
416 /* 2^16 appdomains should be enough for everyone (though I know I'll regret this in 20 years) */
417 /* we alloc this only for weak refs, since we can get the domain directly in the other cases */
421 /* weak and weak-track arrays will be allocated in malloc memory
423 static HandleData gc_handles [] = {
424 {NULL, NULL, 0, HANDLE_WEAK, 0},
425 {NULL, NULL, 0, HANDLE_WEAK_TRACK, 0},
426 {NULL, NULL, 0, HANDLE_NORMAL, 0},
427 {NULL, NULL, 0, HANDLE_PINNED, 0}
430 #define lock_handles(handles) EnterCriticalSection (&handle_section)
431 #define unlock_handles(handles) LeaveCriticalSection (&handle_section)
434 find_first_unset (guint32 bitmap)
437 for (i = 0; i < 32; ++i) {
438 if (!(bitmap & (1 << i)))
445 alloc_handle (HandleData *handles, MonoObject *obj)
448 lock_handles (handles);
449 if (!handles->size) {
451 if (handles->type > HANDLE_WEAK_TRACK) {
452 handles->entries = mono_gc_alloc_fixed (sizeof (gpointer) * handles->size, NULL);
454 handles->entries = g_malloc0 (sizeof (gpointer) * handles->size);
455 handles->domain_ids = g_malloc0 (sizeof (guint16) * handles->size);
457 handles->bitmap = g_malloc0 (handles->size / 8);
460 for (slot = handles->slot_hint; slot < handles->size / 32; ++slot) {
461 if (handles->bitmap [slot] != 0xffffffff) {
462 i = find_first_unset (handles->bitmap [slot]);
463 handles->slot_hint = slot;
467 if (i == -1 && handles->slot_hint != 0) {
468 for (slot = 0; slot < handles->slot_hint; ++slot) {
469 if (handles->bitmap [slot] != 0xffffffff) {
470 i = find_first_unset (handles->bitmap [slot]);
471 handles->slot_hint = slot;
478 guint32 new_size = handles->size * 2; /* always double: we memset to 0 based on this below */
480 /* resize and copy the bitmap */
481 new_bitmap = g_malloc0 (new_size / 8);
482 memcpy (new_bitmap, handles->bitmap, handles->size / 8);
483 g_free (handles->bitmap);
484 handles->bitmap = new_bitmap;
486 /* resize and copy the entries */
487 if (handles->type > HANDLE_WEAK_TRACK) {
489 entries = mono_gc_alloc_fixed (sizeof (gpointer) * new_size, NULL);
490 memcpy (entries, handles->entries, sizeof (gpointer) * handles->size);
491 handles->entries = entries;
495 domain_ids = g_malloc0 (sizeof (guint16) * new_size);
496 entries = g_malloc (sizeof (gpointer) * new_size);
497 /* we disable GC because we could lose some disappearing link updates */
499 memcpy (entries, handles->entries, sizeof (gpointer) * handles->size);
500 memset (entries + handles->size, 0, sizeof (gpointer) * handles->size);
501 memcpy (domain_ids, handles->domain_ids, sizeof (guint16) * handles->size);
502 for (i = 0; i < handles->size; ++i) {
503 MonoObject *obj = mono_gc_weak_link_get (&(handles->entries [i]));
504 if (handles->entries [i])
505 mono_gc_weak_link_remove (&(handles->entries [i]));
506 /*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]);*/
508 mono_gc_weak_link_add (&(entries [i]), obj);
511 g_free (handles->entries);
512 g_free (handles->domain_ids);
513 handles->entries = entries;
514 handles->domain_ids = domain_ids;
518 /* set i and slot to the next free position */
520 slot = (handles->size + 1) / 32;
521 handles->slot_hint = handles->size + 1;
522 handles->size = new_size;
524 handles->bitmap [slot] |= 1 << i;
525 slot = slot * 32 + i;
526 handles->entries [slot] = obj;
527 if (handles->type <= HANDLE_WEAK_TRACK) {
529 mono_gc_weak_link_add (&(handles->entries [slot]), obj);
532 unlock_handles (handles);
533 /*g_print ("allocated entry %d of type %d to object %p (in slot: %p)\n", slot, handles->type, obj, handles->entries [slot]);*/
534 return (slot << 3) | (handles->type + 1);
539 * @obj: managed object to get a handle for
540 * @pinned: whether the object should be pinned
542 * This returns a handle that wraps the object, this is used to keep a
543 * reference to a managed object from the unmanaged world and preventing the
544 * object from being disposed.
546 * If @pinned is false the address of the object can not be obtained, if it is
547 * true the address of the object can be obtained. This will also pin the
548 * object so it will not be possible by a moving garbage collector to move the
551 * Returns: a handle that can be used to access the object from
555 mono_gchandle_new (MonoObject *obj, gboolean pinned)
557 return alloc_handle (&gc_handles [pinned? HANDLE_PINNED: HANDLE_NORMAL], obj);
561 * mono_gchandle_new_weakref:
562 * @obj: managed object to get a handle for
563 * @pinned: whether the object should be pinned
565 * This returns a weak handle that wraps the object, this is used to
566 * keep a reference to a managed object from the unmanaged world.
567 * Unlike the mono_gchandle_new the object can be reclaimed by the
568 * garbage collector. In this case the value of the GCHandle will be
571 * If @pinned is false the address of the object can not be obtained, if it is
572 * true the address of the object can be obtained. This will also pin the
573 * object so it will not be possible by a moving garbage collector to move the
576 * Returns: a handle that can be used to access the object from
580 mono_gchandle_new_weakref (MonoObject *obj, gboolean track_resurrection)
582 return alloc_handle (&gc_handles [track_resurrection? HANDLE_WEAK_TRACK: HANDLE_WEAK], obj);
586 * mono_gchandle_get_target:
587 * @gchandle: a GCHandle's handle.
589 * The handle was previously created by calling mono_gchandle_new or
590 * mono_gchandle_new_weakref.
592 * Returns a pointer to the MonoObject represented by the handle or
593 * NULL for a collected object if using a weakref handle.
596 mono_gchandle_get_target (guint32 gchandle)
598 guint slot = gchandle >> 3;
599 guint type = (gchandle & 7) - 1;
600 HandleData *handles = &gc_handles [type];
601 MonoObject *obj = NULL;
604 lock_handles (handles);
605 if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
606 if (handles->type <= HANDLE_WEAK_TRACK) {
607 obj = mono_gc_weak_link_get (&handles->entries [slot]);
609 obj = handles->entries [slot];
612 /* print a warning? */
614 unlock_handles (handles);
615 /*g_print ("get target of entry %d of type %d: %p\n", slot, handles->type, obj);*/
620 mono_gchandle_set_target (guint32 gchandle, MonoObject *obj)
622 guint slot = gchandle >> 3;
623 guint type = (gchandle & 7) - 1;
624 HandleData *handles = &gc_handles [type];
627 lock_handles (handles);
628 if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
629 if (handles->type <= HANDLE_WEAK_TRACK) {
630 if (handles->entries [slot])
631 mono_gc_weak_link_remove (&handles->entries [slot]);
633 mono_gc_weak_link_add (&handles->entries [slot], obj);
635 handles->entries [slot] = obj;
638 /* print a warning? */
640 /*g_print ("changed entry %d of type %d to object %p (in slot: %p)\n", slot, handles->type, obj, handles->entries [slot]);*/
641 unlock_handles (handles);
645 * mono_gchandle_is_in_domain:
646 * @gchandle: a GCHandle's handle.
647 * @domain: An application domain.
649 * Returns: true if the object wrapped by the @gchandle belongs to the specific @domain.
652 mono_gchandle_is_in_domain (guint32 gchandle, MonoDomain *domain)
654 guint slot = gchandle >> 3;
655 guint type = (gchandle & 7) - 1;
656 HandleData *handles = &gc_handles [type];
657 gboolean result = FALSE;
660 lock_handles (handles);
661 if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
662 if (handles->type <= HANDLE_WEAK_TRACK) {
663 result = domain->domain_id == handles->domain_ids [slot];
666 obj = handles->entries [slot];
670 result = domain == mono_object_domain (obj);
673 /* print a warning? */
675 unlock_handles (handles);
680 * mono_gchandle_free:
681 * @gchandle: a GCHandle's handle.
683 * Frees the @gchandle handle. If there are no outstanding
684 * references, the garbage collector can reclaim the memory of the
688 mono_gchandle_free (guint32 gchandle)
690 guint slot = gchandle >> 3;
691 guint type = (gchandle & 7) - 1;
692 HandleData *handles = &gc_handles [type];
695 lock_handles (handles);
696 if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
697 if (handles->type <= HANDLE_WEAK_TRACK) {
698 if (handles->entries [slot])
699 mono_gc_weak_link_remove (&handles->entries [slot]);
701 handles->entries [slot] = NULL;
703 handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
705 /* print a warning? */
707 /*g_print ("freed entry %d of type %d\n", slot, handles->type);*/
708 unlock_handles (handles);
712 * mono_gchandle_free_domain:
713 * @domain: domain that is unloading
715 * Function used internally to cleanup any GC handle for objects belonging
716 * to the specified domain during appdomain unload.
719 mono_gchandle_free_domain (MonoDomain *domain)
723 for (type = 0; type < 3; ++type) {
725 HandleData *handles = &gc_handles [type];
726 lock_handles (handles);
727 for (slot = 0; slot < handles->size; ++slot) {
728 if (!(handles->bitmap [slot / 32] & (1 << (slot % 32))))
730 if (type <= HANDLE_WEAK_TRACK) {
731 if (domain->domain_id == handles->domain_ids [slot]) {
732 handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
733 if (handles->entries [slot])
734 mono_gc_weak_link_remove (&handles->entries [slot]);
737 if (handles->entries [slot] && mono_object_domain (handles->entries [slot]) == domain) {
738 handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
739 handles->entries [slot] = NULL;
743 unlock_handles (handles);
750 static HANDLE finalizer_event;
751 static volatile gboolean finished=FALSE;
754 mono_gc_finalize_notify (void)
757 g_message (G_GNUC_PRETTY_FUNCTION ": prodding finalizer");
760 SetEvent (finalizer_event);
764 collect_objects (gpointer key, gpointer value, gpointer user_data)
766 GPtrArray *arr = (GPtrArray*)user_data;
767 g_ptr_array_add (arr, key);
771 * finalize_domain_objects:
773 * Run the finalizers of all finalizable objects in req->domain.
776 finalize_domain_objects (DomainFinalizationReq *req)
778 MonoDomain *domain = req->domain;
781 while (g_hash_table_size (domain->finalizable_objects_hash) > 0) {
785 * Since the domain is unloading, nobody is allowed to put
786 * new entries into the hash table. But finalize_object might
787 * remove entries from the hash table, so we make a copy.
789 objs = g_ptr_array_new ();
790 g_hash_table_foreach (domain->finalizable_objects_hash,
791 collect_objects, objs);
792 /* printf ("FINALIZING %d OBJECTS.\n", objs->len); */
794 for (i = 0; i < objs->len; ++i) {
795 MonoObject *o = (MonoObject*)g_ptr_array_index (objs, i);
796 /* FIXME: Avoid finalizing threads, etc */
800 g_ptr_array_free (objs, TRUE);
802 #elif defined(HAVE_SGEN_GC)
803 #define NUM_FOBJECTS 64
804 MonoObject *to_finalize [NUM_FOBJECTS];
806 while ((count = mono_gc_finalizers_for_domain (domain, to_finalize, NUM_FOBJECTS))) {
808 for (i = 0; i < count; ++i) {
809 run_finalize (to_finalize [i], 0);
814 /* Process finalizers which are already in the queue */
815 mono_gc_invoke_finalizers ();
817 /* printf ("DONE.\n"); */
818 SetEvent (req->done_event);
820 /* The event is closed in mono_domain_finalize if we get here */
824 static guint32 finalizer_thread (gpointer unused)
826 gc_thread = mono_thread_current ();
828 SetEvent (thread_started_event);
831 /* Wait to be notified that there's at least one
834 /* Use alertable=FALSE since we will be asked to exit using the event too */
835 WaitForSingleObjectEx (finalizer_event, INFINITE, FALSE);
837 if (domains_to_finalize) {
838 mono_finalizer_lock ();
839 if (domains_to_finalize) {
840 DomainFinalizationReq *req = domains_to_finalize->data;
841 domains_to_finalize = g_slist_remove (domains_to_finalize, req);
842 mono_finalizer_unlock ();
844 finalize_domain_objects (req);
847 mono_finalizer_unlock ();
851 g_message (G_GNUC_PRETTY_FUNCTION ": invoking finalizers");
854 /* If finished == TRUE, mono_gc_cleanup has been called (from mono_runtime_cleanup),
855 * before the domain is unloaded.
857 mono_gc_invoke_finalizers ();
859 SetEvent (pending_done_event);
862 SetEvent (shutdown_event);
867 * Enable or disable the separate finalizer thread.
868 * It's currently disabled because it still requires some
869 * work in the rest of the runtime.
871 #define ENABLE_FINALIZER_THREAD
876 InitializeCriticalSection (&handle_section);
877 InitializeCriticalSection (&allocator_section);
879 InitializeCriticalSection (&finalizer_mutex);
881 MONO_GC_REGISTER_ROOT (gc_handles [HANDLE_NORMAL].entries);
882 MONO_GC_REGISTER_ROOT (gc_handles [HANDLE_PINNED].entries);
884 mono_gc_base_init ();
886 #ifdef ENABLE_FINALIZER_THREAD
888 if (g_getenv ("GC_DONT_GC")) {
893 finalizer_event = CreateEvent (NULL, FALSE, FALSE, NULL);
894 pending_done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
895 shutdown_event = CreateEvent (NULL, TRUE, FALSE, NULL);
896 thread_started_event = CreateEvent (NULL, TRUE, FALSE, NULL);
897 if (finalizer_event == NULL || pending_done_event == NULL || shutdown_event == NULL || thread_started_event == NULL) {
898 g_assert_not_reached ();
901 mono_thread_create (mono_domain_get (), finalizer_thread, NULL);
903 * Wait until the finalizer thread sets gc_thread since its value is needed
904 * by mono_thread_attach ()
906 WaitForSingleObjectEx (thread_started_event, INFINITE, FALSE);
910 void mono_gc_cleanup (void)
913 g_message (G_GNUC_PRETTY_FUNCTION ": cleaning up finalizer");
916 #ifdef ENABLE_FINALIZER_THREAD
918 ResetEvent (shutdown_event);
920 if (mono_thread_current () != gc_thread) {
921 mono_gc_finalize_notify ();
922 /* Finishing the finalizer thread, so wait a little bit... */
923 /* MS seems to wait for about 2 seconds */
924 if (WaitForSingleObjectEx (shutdown_event, 2000, FALSE) == WAIT_TIMEOUT) {
925 mono_thread_stop (gc_thread);
930 GC_finalizer_notifier = NULL;
936 DeleteCriticalSection (&handle_section);
937 DeleteCriticalSection (&allocator_section);
938 DeleteCriticalSection (&finalizer_mutex);
943 /* no Boehm GC support. */
944 void mono_gc_init (void)
946 InitializeCriticalSection (&handle_section);
949 void mono_gc_cleanup (void)
956 * mono_gc_is_finalizer_thread:
957 * @thread: the thread to test.
959 * In Mono objects are finalized asynchronously on a separate thread.
960 * This routine tests whether the @thread argument represents the
961 * finalization thread.
963 * Returns true if @thread is the finalization thread.
966 mono_gc_is_finalizer_thread (MonoThread *thread)
968 return thread == gc_thread;