[aot] Fix the encoding/decoding of generic parameters after 835cc6eda9fb6edc6e5177444...
[mono.git] / mono / metadata / gc.c
1 /*
2  * metadata/gc.c: GC icalls.
3  *
4  * Author: Paolo Molaro <lupus@ximian.com>
5  *
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  */
10
11 #include <config.h>
12 #include <glib.h>
13 #include <string.h>
14
15 #include <mono/metadata/gc-internals.h>
16 #include <mono/metadata/mono-gc.h>
17 #include <mono/metadata/threads.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/exception.h>
20 #include <mono/metadata/profiler-private.h>
21 #include <mono/metadata/domain-internals.h>
22 #include <mono/metadata/class-internals.h>
23 #include <mono/metadata/metadata-internals.h>
24 #include <mono/metadata/mono-mlist.h>
25 #include <mono/metadata/threads-types.h>
26 #include <mono/metadata/threadpool-ms.h>
27 #include <mono/sgen/sgen-conf.h>
28 #include <mono/sgen/sgen-gc.h>
29 #include <mono/utils/mono-logger-internals.h>
30 #include <mono/metadata/gc-internals.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-os-semaphore.h>
35 #include <mono/utils/mono-memory-model.h>
36 #include <mono/utils/mono-counters.h>
37 #include <mono/utils/mono-time.h>
38 #include <mono/utils/dtrace.h>
39 #include <mono/utils/mono-threads.h>
40 #include <mono/utils/atomic.h>
41 #include <mono/utils/mono-coop-semaphore.h>
42
43 #ifndef HOST_WIN32
44 #include <pthread.h>
45 #endif
46
47 typedef struct DomainFinalizationReq {
48         MonoDomain *domain;
49         HANDLE done_event;
50 } DomainFinalizationReq;
51
52 static gboolean gc_disabled = FALSE;
53
54 static gboolean finalizing_root_domain = FALSE;
55
56 gboolean log_finalizers = FALSE;
57 gboolean mono_do_not_finalize = FALSE;
58 gchar **mono_do_not_finalize_class_names = NULL;
59
60 #define mono_finalizer_lock() mono_coop_mutex_lock (&finalizer_mutex)
61 #define mono_finalizer_unlock() mono_coop_mutex_unlock (&finalizer_mutex)
62 static MonoCoopMutex finalizer_mutex;
63 static MonoCoopMutex reference_queue_mutex;
64
65 static GSList *domains_to_finalize= NULL;
66 static MonoMList *threads_to_finalize = NULL;
67
68 static gboolean finalizer_thread_exited;
69 /* Uses finalizer_mutex */
70 static MonoCoopCond exited_cond;
71
72 static MonoInternalThread *gc_thread;
73
74 static void object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*));
75
76 static void reference_queue_proccess_all (void);
77 static void mono_reference_queue_cleanup (void);
78 static void reference_queue_clear_for_domain (MonoDomain *domain);
79 static HANDLE pending_done_event;
80
81 static guint32
82 guarded_wait (HANDLE handle, guint32 timeout, gboolean alertable)
83 {
84         guint32 result;
85
86         MONO_PREPARE_BLOCKING;
87         result = WaitForSingleObjectEx (handle, timeout, alertable);
88         MONO_FINISH_BLOCKING;
89
90         return result;
91 }
92
93 static void
94 add_thread_to_finalize (MonoInternalThread *thread)
95 {
96         mono_finalizer_lock ();
97         if (!threads_to_finalize)
98                 MONO_GC_REGISTER_ROOT_SINGLE (threads_to_finalize, MONO_ROOT_SOURCE_FINALIZER_QUEUE, "finalizable threads list");
99         threads_to_finalize = mono_mlist_append (threads_to_finalize, (MonoObject*)thread);
100         mono_finalizer_unlock ();
101 }
102
103 static gboolean suspend_finalizers = FALSE;
104 /* 
105  * actually, we might want to queue the finalize requests in a separate thread,
106  * but we need to be careful about the execution domain of the thread...
107  */
108 void
109 mono_gc_run_finalize (void *obj, void *data)
110 {
111         MonoObject *exc = NULL;
112         MonoObject *o;
113 #ifndef HAVE_SGEN_GC
114         MonoObject *o2;
115 #endif
116         MonoMethod* finalizer = NULL;
117         MonoDomain *caller_domain = mono_domain_get ();
118         MonoDomain *domain;
119         RuntimeInvokeFunction runtime_invoke;
120
121         // This function is called from the innards of the GC, so our best alternative for now is to do polling here
122         mono_threads_safepoint ();
123
124         o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data));
125
126         if (mono_do_not_finalize) {
127                 if (!mono_do_not_finalize_class_names)
128                         return;
129
130                 size_t namespace_len = strlen (o->vtable->klass->name_space);
131                 for (int i = 0; mono_do_not_finalize_class_names [i]; ++i) {
132                         const char *name = mono_do_not_finalize_class_names [i];
133                         if (strncmp (name, o->vtable->klass->name_space, namespace_len))
134                                 break;
135                         if (name [namespace_len] != '.')
136                                 break;
137                         if (strcmp (name + namespace_len + 1, o->vtable->klass->name))
138                                 break;
139                         return;
140                 }
141         }
142
143         if (log_finalizers)
144                 g_log ("mono-gc-finalizers", G_LOG_LEVEL_DEBUG, "<%s at %p> Starting finalizer checks.", o->vtable->klass->name, o);
145
146         if (suspend_finalizers)
147                 return;
148
149         domain = o->vtable->domain;
150
151 #ifndef HAVE_SGEN_GC
152         mono_domain_finalizers_lock (domain);
153
154         o2 = g_hash_table_lookup (domain->finalizable_objects_hash, o);
155
156         mono_domain_finalizers_unlock (domain);
157
158         if (!o2)
159                 /* Already finalized somehow */
160                 return;
161 #endif
162
163         /* make sure the finalizer is not called again if the object is resurrected */
164         object_register_finalizer (obj, NULL);
165
166         if (log_finalizers)
167                 g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Registered finalizer as processed.", o->vtable->klass->name, o);
168
169         if (o->vtable->klass == mono_defaults.internal_thread_class) {
170                 MonoInternalThread *t = (MonoInternalThread*)o;
171
172                 if (mono_gc_is_finalizer_internal_thread (t))
173                         /* Avoid finalizing ourselves */
174                         return;
175
176                 if (t->threadpool_thread && finalizing_root_domain) {
177                         /* Don't finalize threadpool threads when
178                            shutting down - they're finalized when the
179                            threadpool shuts down. */
180                         add_thread_to_finalize (t);
181                         return;
182                 }
183         }
184
185         if (o->vtable->klass->image == mono_defaults.corlib && !strcmp (o->vtable->klass->name, "DynamicMethod") && finalizing_root_domain) {
186                 /*
187                  * These can't be finalized during unloading/shutdown, since that would
188                  * free the native code which can still be referenced by other
189                  * finalizers.
190                  * FIXME: This is not perfect, objects dying at the same time as 
191                  * dynamic methods can still reference them even when !shutdown.
192                  */
193                 return;
194         }
195
196         if (mono_runtime_get_no_exec ())
197                 return;
198
199         /* speedup later... and use a timeout */
200         /* g_print ("Finalize run on %p %s.%s\n", o, mono_object_class (o)->name_space, mono_object_class (o)->name); */
201
202         /* Use _internal here, since this thread can enter a doomed appdomain */
203         mono_domain_set_internal (mono_object_domain (o));
204
205         /* delegates that have a native function pointer allocated are
206          * registered for finalization, but they don't have a Finalize
207          * method, because in most cases it's not needed and it's just a waste.
208          */
209         if (o->vtable->klass->delegate) {
210                 MonoDelegate* del = (MonoDelegate*)o;
211                 if (del->delegate_trampoline)
212                         mono_delegate_free_ftnptr ((MonoDelegate*)o);
213                 mono_domain_set_internal (caller_domain);
214                 return;
215         }
216
217         finalizer = mono_class_get_finalizer (o->vtable->klass);
218
219         /* If object has a CCW but has no finalizer, it was only
220          * registered for finalization in order to free the CCW.
221          * Else it needs the regular finalizer run.
222          * FIXME: what to do about ressurection and suppression
223          * of finalizer on object with CCW.
224          */
225         if (mono_marshal_free_ccw (o) && !finalizer) {
226                 mono_domain_set_internal (caller_domain);
227                 return;
228         }
229
230         /* 
231          * To avoid the locking plus the other overhead of mono_runtime_invoke (),
232          * create and precompile a wrapper which calls the finalize method using
233          * a CALLVIRT.
234          */
235         if (log_finalizers)
236                 g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Compiling finalizer.", o->vtable->klass->name, o);
237
238         if (!domain->finalize_runtime_invoke) {
239                 MonoMethod *invoke = mono_marshal_get_runtime_invoke (mono_class_get_method_from_name_flags (mono_defaults.object_class, "Finalize", 0, 0), TRUE, FALSE);
240
241                 domain->finalize_runtime_invoke = mono_compile_method (invoke);
242         }
243
244         runtime_invoke = domain->finalize_runtime_invoke;
245
246         mono_runtime_class_init (o->vtable);
247
248         if (G_UNLIKELY (MONO_GC_FINALIZE_INVOKE_ENABLED ())) {
249                 MONO_GC_FINALIZE_INVOKE ((unsigned long)o, mono_object_get_size (o),
250                                 o->vtable->klass->name_space, o->vtable->klass->name);
251         }
252
253         if (log_finalizers)
254                 g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Calling finalizer.", o->vtable->klass->name, o);
255
256         runtime_invoke (o, NULL, &exc, NULL);
257
258         if (log_finalizers)
259                 g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Returned from finalizer.", o->vtable->klass->name, o);
260
261         if (exc)
262                 mono_thread_internal_unhandled_exception (exc);
263
264         mono_domain_set_internal (caller_domain);
265 }
266
267 void
268 mono_gc_finalize_threadpool_threads (void)
269 {
270         while (threads_to_finalize) {
271                 MonoInternalThread *thread = (MonoInternalThread*) mono_mlist_get_data (threads_to_finalize);
272
273                 /* Force finalization of the thread. */
274                 thread->threadpool_thread = FALSE;
275                 mono_object_register_finalizer ((MonoObject*)thread);
276
277                 mono_gc_run_finalize (thread, NULL);
278
279                 threads_to_finalize = mono_mlist_next (threads_to_finalize);
280         }
281 }
282
283 gpointer
284 mono_gc_out_of_memory (size_t size)
285 {
286         /* 
287          * we could allocate at program startup some memory that we could release 
288          * back to the system at this point if we're really low on memory (ie, size is
289          * lower than the memory we set apart)
290          */
291         mono_raise_exception (mono_domain_get ()->out_of_memory_ex);
292
293         return NULL;
294 }
295
296 /*
297  * Some of our objects may point to a different address than the address returned by GC_malloc()
298  * (because of the GetHashCode hack), but we need to pass the real address to register_finalizer.
299  * This also means that in the callback we need to adjust the pointer to get back the real
300  * MonoObject*.
301  * We also need to be consistent in the use of the GC_debug* variants of malloc and register_finalizer, 
302  * since that, too, can cause the underlying pointer to be offset.
303  */
304 static void
305 object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*))
306 {
307         MonoDomain *domain;
308
309         if (obj == NULL)
310                 mono_raise_exception (mono_get_exception_argument_null ("obj"));
311
312         domain = obj->vtable->domain;
313
314 #if HAVE_BOEHM_GC
315         if (mono_domain_is_unloading (domain) && (callback != NULL))
316                 /*
317                  * Can't register finalizers in a dying appdomain, since they
318                  * could be invoked after the appdomain has been unloaded.
319                  */
320                 return;
321
322         mono_domain_finalizers_lock (domain);
323
324         if (callback)
325                 g_hash_table_insert (domain->finalizable_objects_hash, obj, obj);
326         else
327                 g_hash_table_remove (domain->finalizable_objects_hash, obj);
328
329         mono_domain_finalizers_unlock (domain);
330
331         mono_gc_register_for_finalization (obj, callback);
332 #elif defined(HAVE_SGEN_GC)
333         /*
334          * If we register finalizers for domains that are unloading we might
335          * end up running them while or after the domain is being cleared, so
336          * the objects will not be valid anymore.
337          */
338         if (!mono_domain_is_unloading (domain))
339                 mono_gc_register_for_finalization (obj, callback);
340 #endif
341 }
342
343 /**
344  * mono_object_register_finalizer:
345  * @obj: object to register
346  *
347  * Records that object @obj has a finalizer, this will call the
348  * Finalize method when the garbage collector disposes the object.
349  * 
350  */
351 void
352 mono_object_register_finalizer (MonoObject *obj)
353 {
354         /* g_print ("Registered finalizer on %p %s.%s\n", obj, mono_object_class (obj)->name_space, mono_object_class (obj)->name); */
355         object_register_finalizer (obj, mono_gc_run_finalize);
356 }
357
358 /**
359  * mono_domain_finalize:
360  * @domain: the domain to finalize
361  * @timeout: msects to wait for the finalization to complete, -1 to wait indefinitely
362  *
363  *  Request finalization of all finalizable objects inside @domain. Wait
364  * @timeout msecs for the finalization to complete.
365  *
366  * Returns: TRUE if succeeded, FALSE if there was a timeout
367  */
368
369 gboolean
370 mono_domain_finalize (MonoDomain *domain, guint32 timeout) 
371 {
372         DomainFinalizationReq *req;
373         guint32 res;
374         HANDLE done_event;
375         MonoInternalThread *thread = mono_thread_internal_current ();
376
377 #if defined(__native_client__)
378         return FALSE;
379 #endif
380
381         if (mono_thread_internal_current () == gc_thread)
382                 /* We are called from inside a finalizer, not much we can do here */
383                 return FALSE;
384
385         /* 
386          * No need to create another thread 'cause the finalizer thread
387          * is still working and will take care of running the finalizers
388          */ 
389         
390         if (gc_disabled)
391                 return TRUE;
392
393         /* We don't support domain finalization without a GC */
394         if (mono_gc_is_null ())
395                 return FALSE;
396
397         mono_gc_collect (mono_gc_max_generation ());
398
399         done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
400         if (done_event == NULL) {
401                 return FALSE;
402         }
403
404         req = g_new0 (DomainFinalizationReq, 1);
405         req->domain = domain;
406         req->done_event = done_event;
407
408         if (domain == mono_get_root_domain ())
409                 finalizing_root_domain = TRUE;
410         
411         mono_finalizer_lock ();
412
413         domains_to_finalize = g_slist_append (domains_to_finalize, req);
414
415         mono_finalizer_unlock ();
416
417         /* Tell the finalizer thread to finalize this appdomain */
418         mono_gc_finalize_notify ();
419
420         if (timeout == -1)
421                 timeout = INFINITE;
422
423         while (TRUE) {
424                 res = guarded_wait (done_event, timeout, TRUE);
425                 /* printf ("WAIT RES: %d.\n", res); */
426
427                 if (res == WAIT_IO_COMPLETION) {
428                         if ((thread->state & (ThreadState_StopRequested | ThreadState_SuspendRequested)) != 0)
429                                 return FALSE;
430                 } else if (res == WAIT_TIMEOUT) {
431                         /* We leak the handle here */
432                         return FALSE;
433                 } else {
434                         break;
435                 }
436         }
437
438         CloseHandle (done_event);
439
440         if (domain == mono_get_root_domain ()) {
441                 mono_threadpool_ms_cleanup ();
442                 mono_gc_finalize_threadpool_threads ();
443         }
444
445         return TRUE;
446 }
447
448 void
449 ves_icall_System_GC_InternalCollect (int generation)
450 {
451         mono_gc_collect (generation);
452 }
453
454 gint64
455 ves_icall_System_GC_GetTotalMemory (MonoBoolean forceCollection)
456 {
457         if (forceCollection)
458                 mono_gc_collect (mono_gc_max_generation ());
459         return mono_gc_get_used_size ();
460 }
461
462 void
463 ves_icall_System_GC_KeepAlive (MonoObject *obj)
464 {
465         /*
466          * Does nothing.
467          */
468 }
469
470 void
471 ves_icall_System_GC_ReRegisterForFinalize (MonoObject *obj)
472 {
473         MONO_CHECK_ARG_NULL (obj,);
474
475         object_register_finalizer (obj, mono_gc_run_finalize);
476 }
477
478 void
479 ves_icall_System_GC_SuppressFinalize (MonoObject *obj)
480 {
481         MONO_CHECK_ARG_NULL (obj,);
482
483         /* delegates have no finalizers, but we register them to deal with the
484          * unmanaged->managed trampoline. We don't let the user suppress it
485          * otherwise we'd leak it.
486          */
487         if (obj->vtable->klass->delegate)
488                 return;
489
490         /* FIXME: Need to handle case where obj has COM Callable Wrapper
491          * generated for it that needs cleaned up, but user wants to suppress
492          * their derived object finalizer. */
493
494         object_register_finalizer (obj, NULL);
495 }
496
497 void
498 ves_icall_System_GC_WaitForPendingFinalizers (void)
499 {
500         if (mono_gc_is_null ())
501                 return;
502
503         if (!mono_gc_pending_finalizers ())
504                 return;
505
506         if (mono_thread_internal_current () == gc_thread)
507                 /* Avoid deadlocks */
508                 return;
509
510         /*
511         If the finalizer thread is not live, lets pretend no finalizers are pending since the current thread might
512         be the one responsible for starting it up.
513         */
514         if (gc_thread == NULL)
515                 return;
516
517         ResetEvent (pending_done_event);
518         mono_gc_finalize_notify ();
519         /* g_print ("Waiting for pending finalizers....\n"); */
520         guarded_wait (pending_done_event, INFINITE, TRUE);
521         /* g_print ("Done pending....\n"); */
522 }
523
524 void
525 ves_icall_System_GC_register_ephemeron_array (MonoObject *array)
526 {
527 #ifdef HAVE_SGEN_GC
528         if (!mono_gc_ephemeron_array_add (array)) {
529                 mono_set_pending_exception (mono_object_domain (array)->out_of_memory_ex);
530                 return;
531         }
532 #endif
533 }
534
535 MonoObject*
536 ves_icall_System_GC_get_ephemeron_tombstone (void)
537 {
538         return mono_domain_get ()->ephemeron_tombstone;
539 }
540
541 MonoObject *
542 ves_icall_System_GCHandle_GetTarget (guint32 handle)
543 {
544         return mono_gchandle_get_target (handle);
545 }
546
547 /*
548  * if type == -1, change the target of the handle, otherwise allocate a new handle.
549  */
550 guint32
551 ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint32 type)
552 {
553         if (type == -1) {
554                 mono_gchandle_set_target (handle, obj);
555                 /* the handle doesn't change */
556                 return handle;
557         }
558         switch (type) {
559         case HANDLE_WEAK:
560                 return mono_gchandle_new_weakref (obj, FALSE);
561         case HANDLE_WEAK_TRACK:
562                 return mono_gchandle_new_weakref (obj, TRUE);
563         case HANDLE_NORMAL:
564                 return mono_gchandle_new (obj, FALSE);
565         case HANDLE_PINNED:
566                 return mono_gchandle_new (obj, TRUE);
567         default:
568                 g_assert_not_reached ();
569         }
570         return 0;
571 }
572
573 void
574 ves_icall_System_GCHandle_FreeHandle (guint32 handle)
575 {
576         mono_gchandle_free (handle);
577 }
578
579 gpointer
580 ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle)
581 {
582         MonoObject *obj;
583
584         if (MONO_GC_HANDLE_TYPE (handle) != HANDLE_PINNED)
585                 return (gpointer)-2;
586         obj = mono_gchandle_get_target (handle);
587         if (obj) {
588                 MonoClass *klass = mono_object_class (obj);
589                 if (klass == mono_defaults.string_class) {
590                         return mono_string_chars ((MonoString*)obj);
591                 } else if (klass->rank) {
592                         return mono_array_addr ((MonoArray*)obj, char, 0);
593                 } else {
594                         /* the C# code will check and throw the exception */
595                         /* FIXME: missing !klass->blittable test, see bug #61134 */
596                         if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT)
597                                 return (gpointer)-1;
598                         return (char*)obj + sizeof (MonoObject);
599                 }
600         }
601         return NULL;
602 }
603
604 MonoBoolean
605 ves_icall_Mono_Runtime_SetGCAllowSynchronousMajor (MonoBoolean flag)
606 {
607         return mono_gc_set_allow_synchronous_major (flag);
608 }
609
610 MonoBoolean
611 mono_gc_GCHandle_CheckCurrentDomain (guint32 gchandle)
612 {
613         return mono_gchandle_is_in_domain (gchandle, mono_domain_get ());
614 }
615
616 static MonoCoopSem finalizer_sem;
617 static volatile gboolean finished=FALSE;
618
619 void
620 mono_gc_finalize_notify (void)
621 {
622 #ifdef DEBUG
623         g_message ( "%s: prodding finalizer", __func__);
624 #endif
625
626         if (mono_gc_is_null ())
627                 return;
628
629         mono_coop_sem_post (&finalizer_sem);
630 }
631
632 #ifdef HAVE_BOEHM_GC
633
634 static void
635 collect_objects (gpointer key, gpointer value, gpointer user_data)
636 {
637         GPtrArray *arr = (GPtrArray*)user_data;
638         g_ptr_array_add (arr, key);
639 }
640
641 #endif
642
643 /*
644  * finalize_domain_objects:
645  *
646  *  Run the finalizers of all finalizable objects in req->domain.
647  */
648 static void
649 finalize_domain_objects (DomainFinalizationReq *req)
650 {
651         MonoDomain *domain = req->domain;
652
653 #if HAVE_SGEN_GC
654 #define NUM_FOBJECTS 64
655         MonoObject *to_finalize [NUM_FOBJECTS];
656         int count;
657 #endif
658
659         /* Process finalizers which are already in the queue */
660         mono_gc_invoke_finalizers ();
661
662 #ifdef HAVE_BOEHM_GC
663         while (g_hash_table_size (domain->finalizable_objects_hash) > 0) {
664                 int i;
665                 GPtrArray *objs;
666                 /* 
667                  * Since the domain is unloading, nobody is allowed to put
668                  * new entries into the hash table. But finalize_object might
669                  * remove entries from the hash table, so we make a copy.
670                  */
671                 objs = g_ptr_array_new ();
672                 g_hash_table_foreach (domain->finalizable_objects_hash, collect_objects, objs);
673                 /* printf ("FINALIZING %d OBJECTS.\n", objs->len); */
674
675                 for (i = 0; i < objs->len; ++i) {
676                         MonoObject *o = (MonoObject*)g_ptr_array_index (objs, i);
677                         /* FIXME: Avoid finalizing threads, etc */
678                         mono_gc_run_finalize (o, 0);
679                 }
680
681                 g_ptr_array_free (objs, TRUE);
682         }
683 #elif defined(HAVE_SGEN_GC)
684         while ((count = mono_gc_finalizers_for_domain (domain, to_finalize, NUM_FOBJECTS))) {
685                 int i;
686                 for (i = 0; i < count; ++i) {
687                         mono_gc_run_finalize (to_finalize [i], 0);
688                 }
689         }
690 #endif
691
692         /* cleanup the reference queue */
693         reference_queue_clear_for_domain (domain);
694         
695         /* printf ("DONE.\n"); */
696         SetEvent (req->done_event);
697
698         /* The event is closed in mono_domain_finalize if we get here */
699         g_free (req);
700 }
701
702 static guint32
703 finalizer_thread (gpointer unused)
704 {
705         gboolean wait = TRUE;
706
707         while (!finished) {
708                 /* Wait to be notified that there's at least one
709                  * finaliser to run
710                  */
711
712                 g_assert (mono_domain_get () == mono_get_root_domain ());
713                 mono_gc_set_skip_thread (TRUE);
714
715                 if (wait) {
716                         /* An alertable wait is required so this thread can be suspended on windows */
717                         mono_coop_sem_wait (&finalizer_sem, MONO_SEM_FLAGS_ALERTABLE);
718                 }
719                 wait = TRUE;
720
721                 mono_gc_set_skip_thread (FALSE);
722
723                 mono_threads_perform_thread_dump ();
724
725                 mono_console_handle_async_ops ();
726
727                 mono_attach_maybe_start ();
728
729                 if (domains_to_finalize) {
730                         mono_finalizer_lock ();
731                         if (domains_to_finalize) {
732                                 DomainFinalizationReq *req = domains_to_finalize->data;
733                                 domains_to_finalize = g_slist_remove (domains_to_finalize, req);
734                                 mono_finalizer_unlock ();
735
736                                 finalize_domain_objects (req);
737                         } else {
738                                 mono_finalizer_unlock ();
739                         }
740                 }                               
741
742                 /* If finished == TRUE, mono_gc_cleanup has been called (from mono_runtime_cleanup),
743                  * before the domain is unloaded.
744                  */
745                 mono_gc_invoke_finalizers ();
746
747                 mono_threads_join_threads ();
748
749                 reference_queue_proccess_all ();
750
751                 /* Avoid posting the pending done event until there are pending finalizers */
752                 if (mono_coop_sem_timedwait (&finalizer_sem, 0, MONO_SEM_FLAGS_NONE) == 0) {
753                         /* Don't wait again at the start of the loop */
754                         wait = FALSE;
755                 } else {
756                         SetEvent (pending_done_event);
757                 }
758         }
759
760         mono_finalizer_lock ();
761         finalizer_thread_exited = TRUE;
762         mono_coop_cond_signal (&exited_cond);
763         mono_finalizer_unlock ();
764
765         return 0;
766 }
767
768 #ifndef LAZY_GC_THREAD_CREATION
769 static
770 #endif
771 void
772 mono_gc_init_finalizer_thread (void)
773 {
774         gc_thread = mono_thread_create_internal (mono_domain_get (), finalizer_thread, NULL, FALSE, 0);
775         ves_icall_System_Threading_Thread_SetName_internal (gc_thread, mono_string_new (mono_domain_get (), "Finalizer"));
776 }
777
778 void
779 mono_gc_init (void)
780 {
781         mono_coop_mutex_init_recursive (&finalizer_mutex);
782         mono_coop_mutex_init_recursive (&reference_queue_mutex);
783
784         mono_counters_register ("Minor GC collections", MONO_COUNTER_GC | MONO_COUNTER_UINT, &gc_stats.minor_gc_count);
785         mono_counters_register ("Major GC collections", MONO_COUNTER_GC | MONO_COUNTER_UINT, &gc_stats.major_gc_count);
786         mono_counters_register ("Minor GC time", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &gc_stats.minor_gc_time);
787         mono_counters_register ("Major GC time", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &gc_stats.major_gc_time);
788         mono_counters_register ("Major GC time concurrent", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &gc_stats.major_gc_time_concurrent);
789
790         mono_gc_base_init ();
791
792         if (mono_gc_is_disabled ()) {
793                 gc_disabled = TRUE;
794                 return;
795         }
796
797         pending_done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
798         g_assert (pending_done_event);
799         mono_coop_cond_init (&exited_cond);
800         mono_coop_sem_init (&finalizer_sem, 0);
801
802 #ifndef LAZY_GC_THREAD_CREATION
803         mono_gc_init_finalizer_thread ();
804 #endif
805 }
806
807 void
808 mono_gc_cleanup (void)
809 {
810 #ifdef DEBUG
811         g_message ("%s: cleaning up finalizer", __func__);
812 #endif
813
814         if (mono_gc_is_null ())
815                 return;
816
817         if (!gc_disabled) {
818                 finished = TRUE;
819                 if (mono_thread_internal_current () != gc_thread) {
820                         gboolean timed_out = FALSE;
821                         guint32 start_ticks = mono_msec_ticks ();
822                         guint32 end_ticks = start_ticks + 2000;
823
824                         mono_gc_finalize_notify ();
825                         /* Finishing the finalizer thread, so wait a little bit... */
826                         /* MS seems to wait for about 2 seconds */
827                         while (!finalizer_thread_exited) {
828                                 guint32 current_ticks = mono_msec_ticks ();
829                                 guint32 timeout;
830
831                                 if (current_ticks >= end_ticks)
832                                         break;
833                                 else
834                                         timeout = end_ticks - current_ticks;
835                                 mono_finalizer_lock ();
836                                 if (!finalizer_thread_exited)
837                                         mono_coop_cond_timedwait (&exited_cond, &finalizer_mutex, timeout);
838                                 mono_finalizer_unlock ();
839                         }
840
841                         if (!finalizer_thread_exited) {
842                                 int ret;
843
844                                 /* Set a flag which the finalizer thread can check */
845                                 suspend_finalizers = TRUE;
846
847                                 /* Try to abort the thread, in the hope that it is running managed code */
848                                 mono_thread_internal_stop (gc_thread);
849
850                                 /* Wait for it to stop */
851                                 ret = guarded_wait (gc_thread->handle, 100, TRUE);
852
853                                 if (ret == WAIT_TIMEOUT) {
854                                         /* 
855                                          * The finalizer thread refused to die. There is not much we 
856                                          * can do here, since the runtime is shutting down so the 
857                                          * state the finalizer thread depends on will vanish.
858                                          */
859                                         g_warning ("Shutting down finalizer thread timed out.");
860                                         timed_out = TRUE;
861                                 }
862                         }
863
864                         if (!timed_out) {
865                                 int ret;
866
867                                 /* Wait for the thread to actually exit */
868                                 ret = guarded_wait (gc_thread->handle, INFINITE, TRUE);
869                                 g_assert (ret == WAIT_OBJECT_0);
870
871                                 mono_thread_join (GUINT_TO_POINTER (gc_thread->tid));
872                         }
873                 }
874                 gc_thread = NULL;
875                 mono_gc_base_cleanup ();
876         }
877
878         mono_reference_queue_cleanup ();
879
880         mono_coop_mutex_destroy (&finalizer_mutex);
881         mono_coop_mutex_destroy (&reference_queue_mutex);
882 }
883
884 gboolean
885 mono_gc_is_finalizer_internal_thread (MonoInternalThread *thread)
886 {
887         return thread == gc_thread;
888 }
889
890 /**
891  * mono_gc_is_finalizer_thread:
892  * @thread: the thread to test.
893  *
894  * In Mono objects are finalized asynchronously on a separate thread.
895  * This routine tests whether the @thread argument represents the
896  * finalization thread.
897  * 
898  * Returns true if @thread is the finalization thread.
899  */
900 gboolean
901 mono_gc_is_finalizer_thread (MonoThread *thread)
902 {
903         return mono_gc_is_finalizer_internal_thread (thread->internal_thread);
904 }
905
906 #if defined(__MACH__)
907 static pthread_t mach_exception_thread;
908
909 void
910 mono_gc_register_mach_exception_thread (pthread_t thread)
911 {
912         mach_exception_thread = thread;
913 }
914
915 pthread_t
916 mono_gc_get_mach_exception_thread (void)
917 {
918         return mach_exception_thread;
919 }
920 #endif
921
922 #ifndef HAVE_SGEN_GC
923 void*
924 mono_gc_alloc_mature (MonoVTable *vtable)
925 {
926         return mono_object_new_specific (vtable);
927 }
928 #endif
929
930
931 static MonoReferenceQueue *ref_queues;
932
933 static void
934 ref_list_remove_element (RefQueueEntry **prev, RefQueueEntry *element)
935 {
936         do {
937                 /* Guard if head is changed concurrently. */
938                 while (*prev != element)
939                         prev = &(*prev)->next;
940         } while (prev && InterlockedCompareExchangePointer ((void*)prev, element->next, element) != element);
941 }
942
943 static void
944 ref_list_push (RefQueueEntry **head, RefQueueEntry *value)
945 {
946         RefQueueEntry *current;
947         do {
948                 current = *head;
949                 value->next = current;
950                 STORE_STORE_FENCE; /*Must make sure the previous store is visible before the CAS. */
951         } while (InterlockedCompareExchangePointer ((void*)head, value, current) != current);
952 }
953
954 static void
955 reference_queue_proccess (MonoReferenceQueue *queue)
956 {
957         RefQueueEntry **iter = &queue->queue;
958         RefQueueEntry *entry;
959         while ((entry = *iter)) {
960                 if (queue->should_be_deleted || !mono_gchandle_get_target (entry->gchandle)) {
961                         mono_gchandle_free ((guint32)entry->gchandle);
962                         ref_list_remove_element (iter, entry);
963                         queue->callback (entry->user_data);
964                         g_free (entry);
965                 } else {
966                         iter = &entry->next;
967                 }
968         }
969 }
970
971 static void
972 reference_queue_proccess_all (void)
973 {
974         MonoReferenceQueue **iter;
975         MonoReferenceQueue *queue = ref_queues;
976         for (; queue; queue = queue->next)
977                 reference_queue_proccess (queue);
978
979 restart:
980         mono_coop_mutex_lock (&reference_queue_mutex);
981         for (iter = &ref_queues; *iter;) {
982                 queue = *iter;
983                 if (!queue->should_be_deleted) {
984                         iter = &queue->next;
985                         continue;
986                 }
987                 if (queue->queue) {
988                         mono_coop_mutex_unlock (&reference_queue_mutex);
989                         reference_queue_proccess (queue);
990                         goto restart;
991                 }
992                 *iter = queue->next;
993                 g_free (queue);
994         }
995         mono_coop_mutex_unlock (&reference_queue_mutex);
996 }
997
998 static void
999 mono_reference_queue_cleanup (void)
1000 {
1001         MonoReferenceQueue *queue = ref_queues;
1002         for (; queue; queue = queue->next)
1003                 queue->should_be_deleted = TRUE;
1004         reference_queue_proccess_all ();
1005 }
1006
1007 static void
1008 reference_queue_clear_for_domain (MonoDomain *domain)
1009 {
1010         MonoReferenceQueue *queue = ref_queues;
1011         for (; queue; queue = queue->next) {
1012                 RefQueueEntry **iter = &queue->queue;
1013                 RefQueueEntry *entry;
1014                 while ((entry = *iter)) {
1015                         if (entry->domain == domain) {
1016                                 mono_gchandle_free ((guint32)entry->gchandle);
1017                                 ref_list_remove_element (iter, entry);
1018                                 queue->callback (entry->user_data);
1019                                 g_free (entry);
1020                         } else {
1021                                 iter = &entry->next;
1022                         }
1023                 }
1024         }
1025 }
1026 /**
1027  * mono_gc_reference_queue_new:
1028  * @callback callback used when processing collected entries.
1029  *
1030  * Create a new reference queue used to process collected objects.
1031  * A reference queue let you add a pair of (managed object, user data)
1032  * using the mono_gc_reference_queue_add method.
1033  *
1034  * Once the managed object is collected @callback will be called
1035  * in the finalizer thread with 'user data' as argument.
1036  *
1037  * The callback is called from the finalizer thread without any locks held.
1038  * When a AppDomain is unloaded, all callbacks for objects belonging to it
1039  * will be invoked.
1040  *
1041  * @returns the new queue.
1042  */
1043 MonoReferenceQueue*
1044 mono_gc_reference_queue_new (mono_reference_queue_callback callback)
1045 {
1046         MonoReferenceQueue *res = g_new0 (MonoReferenceQueue, 1);
1047         res->callback = callback;
1048
1049         mono_coop_mutex_lock (&reference_queue_mutex);
1050         res->next = ref_queues;
1051         ref_queues = res;
1052         mono_coop_mutex_unlock (&reference_queue_mutex);
1053
1054         return res;
1055 }
1056
1057 /**
1058  * mono_gc_reference_queue_add:
1059  * @queue the queue to add the reference to.
1060  * @obj the object to be watched for collection
1061  * @user_data parameter to be passed to the queue callback
1062  *
1063  * Queue an object to be watched for collection, when the @obj is
1064  * collected, the callback that was registered for the @queue will
1065  * be invoked with @user_data as argument.
1066  *
1067  * @returns false if the queue is scheduled to be freed.
1068  */
1069 gboolean
1070 mono_gc_reference_queue_add (MonoReferenceQueue *queue, MonoObject *obj, void *user_data)
1071 {
1072         RefQueueEntry *entry;
1073         if (queue->should_be_deleted)
1074                 return FALSE;
1075
1076         entry = g_new0 (RefQueueEntry, 1);
1077         entry->user_data = user_data;
1078         entry->domain = mono_object_domain (obj);
1079
1080         entry->gchandle = mono_gchandle_new_weakref (obj, TRUE);
1081         mono_object_register_finalizer (obj);
1082
1083         ref_list_push (&queue->queue, entry);
1084         return TRUE;
1085 }
1086
1087 /**
1088  * mono_gc_reference_queue_free:
1089  * @queue the queue that should be freed.
1090  *
1091  * This operation signals that @queue should be freed. This operation is deferred
1092  * as it happens on the finalizer thread.
1093  *
1094  * After this call, no further objects can be queued. It's the responsibility of the
1095  * caller to make sure that no further attempt to access queue will be made.
1096  */
1097 void
1098 mono_gc_reference_queue_free (MonoReferenceQueue *queue)
1099 {
1100         queue->should_be_deleted = TRUE;
1101 }