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