Some docs.
[mono.git] / mono / metadata / gc.c
1 /*
2  * metadata/gc.c: GC icalls.
3  *
4  * Author: Paolo Molaro <lupus@ximian.com>
5  *
6  * (C) 2002 Ximian, Inc.
7  */
8
9 #include <config.h>
10 #include <glib.h>
11 #include <string.h>
12
13 #include <mono/metadata/gc-internal.h>
14 #include <mono/metadata/threads.h>
15 #include <mono/metadata/tabledefs.h>
16 #include <mono/metadata/exception.h>
17 #include <mono/metadata/domain-internals.h>
18 #include <mono/metadata/class-internals.h>
19 #include <mono/utils/mono-logger.h>
20 #define GC_I_HIDE_POINTERS
21 #include <mono/os/gc_wrapper.h>
22
23 #ifndef HIDE_POINTER
24 #define HIDE_POINTER(v)         (v)
25 #define REVEAL_POINTER(v)       (v)
26 #endif
27
28 typedef struct DomainFinalizationReq {
29         MonoDomain *domain;
30         HANDLE done_event;
31 } DomainFinalizationReq;
32
33 #ifdef PLATFORM_WINCE /* FIXME: add accessors to gc.dll API */
34 extern void (*__imp_GC_finalizer_notifier)(void);
35 #define GC_finalizer_notifier __imp_GC_finalizer_notifier
36 extern int __imp_GC_finalize_on_demand;
37 #define GC_finalize_on_demand __imp_GC_finalize_on_demand
38 #endif
39
40 static int finalize_slot = -1;
41
42 static gboolean gc_disabled = FALSE;
43
44 static CRITICAL_SECTION finalizer_mutex;
45
46 static GSList *domains_to_finalize= NULL;
47
48 static MonoThread *gc_thread;
49
50 static void object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*));
51
52 #if HAVE_BOEHM_GC
53 static void finalize_notify (void);
54 static HANDLE pending_done_event;
55 static HANDLE shutdown_event;
56 static HANDLE thread_started_event;
57 #endif
58
59 /* 
60  * actually, we might want to queue the finalize requests in a separate thread,
61  * but we need to be careful about the execution domain of the thread...
62  */
63 static void
64 run_finalize (void *obj, void *data)
65 {
66         MonoObject *exc = NULL;
67         MonoObject *o, *o2;
68         o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data));
69
70         if (finalize_slot < 0) {
71                 int i;
72                 MonoClass* obj_class = mono_get_object_class ();
73                 for (i = 0; i < obj_class->vtable_size; ++i) {
74                         MonoMethod *cm = obj_class->vtable [i];
75                
76                         if (!strcmp (mono_method_get_name (cm), "Finalize")) {
77                                 finalize_slot = i;
78                                 break;
79                         }
80                 }
81         }
82
83         mono_domain_lock (o->vtable->domain);
84
85         o2 = g_hash_table_lookup (o->vtable->domain->finalizable_objects_hash, o);
86
87         mono_domain_unlock (o->vtable->domain);
88
89         if (!o2)
90                 /* Already finalized somehow */
91                 return;
92
93         /* make sure the finalizer is not called again if the object is resurrected */
94         object_register_finalizer (obj, NULL);
95
96         if (o->vtable->klass == mono_get_thread_class ())
97                 if (mono_gc_is_finalizer_thread ((MonoThread*)o))
98                         /* Avoid finalizing ourselves */
99                         return;
100
101         /* speedup later... and use a timeout */
102         /* g_print ("Finalize run on %p %s.%s\n", o, mono_object_class (o)->name_space, mono_object_class (o)->name); */
103
104         /* Use _internal here, since this thread can enter a doomed appdomain */
105         mono_domain_set_internal (mono_object_domain (o));              
106
107         mono_runtime_invoke (o->vtable->klass->vtable [finalize_slot], o, NULL, &exc);
108
109         if (exc) {
110                 /* fixme: do something useful */
111         }
112 }
113
114 gpointer
115 mono_gc_out_of_memory (size_t size)
116 {
117         /* 
118          * we could allocate at program startup some memory that we could release 
119          * back to the system at this point if we're really low on memory (ie, size is
120          * lower than the memory we set apart)
121          */
122         mono_raise_exception (mono_domain_get ()->out_of_memory_ex);
123
124         return NULL;
125 }
126
127 /*
128  * Some of our objects may point to a different address than the address returned by GC_malloc()
129  * (because of the GetHashCode hack), but we need to pass the real address to register_finalizer.
130  * This also means that in the callback we need to adjust the pointer to get back the real
131  * MonoObject*.
132  * We also need to be consistent in the use of the GC_debug* variants of malloc and register_finalizer, 
133  * since that, too, can cause the underlying pointer to be offset.
134  */
135 static void
136 object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*))
137 {
138 #if HAVE_BOEHM_GC
139         guint offset = 0;
140
141 #ifndef GC_DEBUG
142         /* This assertion is not valid when GC_DEBUG is defined */
143         g_assert (GC_base (obj) == (char*)obj - offset);
144 #endif
145
146         if (mono_domain_is_unloading (obj->vtable->domain) && (callback != NULL))
147                 /*
148                  * Can't register finalizers in a dying appdomain, since they
149                  * could be invoked after the appdomain has been unloaded.
150                  */
151                 return;
152
153         mono_domain_lock (obj->vtable->domain);
154
155         if (callback)
156                 g_hash_table_insert (obj->vtable->domain->finalizable_objects_hash, obj,
157                                                          obj);
158         else
159                 g_hash_table_remove (obj->vtable->domain->finalizable_objects_hash, obj);
160
161         mono_domain_unlock (obj->vtable->domain);
162
163         GC_REGISTER_FINALIZER_NO_ORDER ((char*)obj - offset, callback, GUINT_TO_POINTER (offset), NULL, NULL);
164 #endif
165 }
166
167 void
168 mono_object_register_finalizer (MonoObject *obj)
169 {
170         /* g_print ("Registered finalizer on %p %s.%s\n", obj, mono_object_class (obj)->name_space, mono_object_class (obj)->name); */
171         object_register_finalizer (obj, run_finalize);
172 }
173
174 /*
175  * mono_domain_finalize:
176  *
177  *  Request finalization of all finalizable objects inside @domain. Wait
178  * @timeout msecs for the finalization to complete.
179  * Returns: TRUE if succeeded, FALSE if there was a timeout
180  */
181
182 gboolean
183 mono_domain_finalize (MonoDomain *domain, guint32 timeout) 
184 {
185         DomainFinalizationReq *req;
186         guint32 res;
187         HANDLE done_event;
188
189         /* 
190          * No need to create another thread 'cause the finalizer thread
191          * is still working and will take care of running the finalizers
192          */ 
193         
194 #if HAVE_BOEHM_GC
195         if (gc_disabled)
196                 return TRUE;
197
198         GC_gcollect ();
199
200         done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
201
202         req = g_new0 (DomainFinalizationReq, 1);
203         req->domain = domain;
204         req->done_event = done_event;
205         
206         EnterCriticalSection (&finalizer_mutex);
207
208         domains_to_finalize = g_slist_append (domains_to_finalize, req);
209
210         LeaveCriticalSection (&finalizer_mutex);
211
212         /* Tell the finalizer thread to finalize this appdomain */
213         finalize_notify ();
214
215         res = WaitForSingleObjectEx (done_event, timeout, TRUE);
216
217         /* printf ("WAIT RES: %d.\n", res); */
218         if (res == WAIT_TIMEOUT) {
219                 /* We leak the handle here */
220                 return FALSE;
221         }
222
223         CloseHandle (done_event);
224         return TRUE;
225 #else
226         /* We don't support domain finalization without a GC */
227         return FALSE;
228 #endif
229 }
230
231 void
232 ves_icall_System_GC_InternalCollect (int generation)
233 {
234         MONO_ARCH_SAVE_REGS;
235
236 #if HAVE_BOEHM_GC
237         GC_gcollect ();
238 #endif
239 }
240
241 gint64
242 ves_icall_System_GC_GetTotalMemory (MonoBoolean forceCollection)
243 {
244         MONO_ARCH_SAVE_REGS;
245
246 #if HAVE_BOEHM_GC
247         if (forceCollection)
248                 GC_gcollect ();
249         return GC_get_heap_size () - GC_get_free_bytes ();
250 #else
251         return 0;
252 #endif
253 }
254
255 void
256 ves_icall_System_GC_KeepAlive (MonoObject *obj)
257 {
258         MONO_ARCH_SAVE_REGS;
259
260         /*
261          * Does nothing.
262          */
263 }
264
265 void
266 ves_icall_System_GC_ReRegisterForFinalize (MonoObject *obj)
267 {
268         MONO_ARCH_SAVE_REGS;
269
270         object_register_finalizer (obj, run_finalize);
271 }
272
273 void
274 ves_icall_System_GC_SuppressFinalize (MonoObject *obj)
275 {
276         MONO_ARCH_SAVE_REGS;
277
278         object_register_finalizer (obj, NULL);
279 }
280
281 void
282 ves_icall_System_GC_WaitForPendingFinalizers (void)
283 {
284         MONO_ARCH_SAVE_REGS;
285         
286 #if HAVE_BOEHM_GC
287         if (!GC_should_invoke_finalizers ())
288                 return;
289
290         if (mono_thread_current () == gc_thread)
291                 /* Avoid deadlocks */
292                 return;
293
294         ResetEvent (pending_done_event);
295         finalize_notify ();
296         /* g_print ("Waiting for pending finalizers....\n"); */
297         WaitForSingleObjectEx (pending_done_event, INFINITE, TRUE);
298         /* g_print ("Done pending....\n"); */
299 #else
300 #endif
301 }
302
303 static CRITICAL_SECTION allocator_section;
304 static CRITICAL_SECTION handle_section;
305 static guint32 next_handle = 0;
306 static gpointer *gc_handles = NULL;
307 static guint8 *gc_handle_types = NULL;
308 static guint32 array_size = 0;
309
310 /*
311  * The handle type is encoded in the lower two bits of the handle value:
312  * 0 -> normal
313  * 1 -> pinned
314  * 2 -> weak
315  */
316
317 typedef enum {
318         HANDLE_WEAK,
319         HANDLE_WEAK_TRACK,
320         HANDLE_NORMAL,
321         HANDLE_PINNED
322 } HandleType;
323
324 /*
325  * FIXME: make thread safe and reuse the array entries.
326  */
327 MonoObject *
328 ves_icall_System_GCHandle_GetTarget (guint32 handle)
329 {
330         MonoObject *obj;
331         gint32 type;
332
333         MONO_ARCH_SAVE_REGS;
334
335         if (gc_handles) {
336                 type = handle & 0x3;
337                 EnterCriticalSection (&handle_section);
338                 g_assert (type == gc_handle_types [handle >> 2]);
339                 obj = gc_handles [handle >> 2];
340                 LeaveCriticalSection (&handle_section);
341                 if (!obj)
342                         return NULL;
343
344                 if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK))
345                         return REVEAL_POINTER (obj);
346                 else
347                         return obj;
348         }
349         return NULL;
350 }
351
352 guint32
353 ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint32 type)
354 {
355         gpointer val = obj;
356         guint32 h, idx;
357
358         MONO_ARCH_SAVE_REGS;
359
360         EnterCriticalSection (&handle_section);
361         /* Indexes start from 1 since 0 means the handle is not allocated */
362         idx = ++next_handle;
363         if (idx >= array_size) {
364                 gpointer *new_array;
365                 guint8 *new_type_array;
366                 if (!array_size)
367                         array_size = 16;
368 #if HAVE_BOEHM_GC
369                 new_array = GC_MALLOC (sizeof (gpointer) * (array_size * 2));
370                 new_type_array = GC_MALLOC (sizeof (guint8) * (array_size * 2));
371 #else
372                 new_array = g_malloc0 (sizeof (gpointer) * (array_size * 2));
373                 new_type_array = g_malloc0 (sizeof (guint8) * (array_size * 2));
374 #endif
375                 if (gc_handles) {
376                         int i;
377                         memcpy (new_array, gc_handles, sizeof (gpointer) * array_size);
378                         memcpy (new_type_array, gc_handle_types, sizeof (guint8) * array_size);
379                         /* need to re-register links for weak refs. test if GC_realloc needs the same */
380                         for (i = 0; i < array_size; ++i) {
381 #if 0 /* This breaks the threaded finalizer, by causing segfaults deep
382        * inside libgc.  I assume it will also break without the
383        * threaded finalizer, just that the stress test (bug 31333)
384        * deadlocks too early without it.  Reverting to the previous
385        * version here stops the segfault.
386        */
387                                 if ((gc_handle_types[i] == HANDLE_WEAK) || (gc_handle_types[i] == HANDLE_WEAK_TRACK)) { /* all and only disguised pointers have it set */
388 #else
389                                 if (((gulong)new_array [i]) & 0x1) {
390 #endif
391 #if HAVE_BOEHM_GC
392                                         if (gc_handles [i] != (gpointer)-1)
393                                                 GC_unregister_disappearing_link (&(gc_handles [i]));
394                                         if (new_array [i] != (gpointer)-1)
395                                                 GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(new_array [i]), REVEAL_POINTER (new_array [i]));
396 #endif
397                                 }
398                         }
399                 }
400                 array_size *= 2;
401 #ifndef HAVE_BOEHM_GC
402                 g_free (gc_handles);
403                 g_free (gc_handle_types);
404 #endif
405                 gc_handles = new_array;
406                 gc_handle_types = new_type_array;
407         }
408
409         /* resuse the type from the old target */
410         if (type == -1)
411                 type =  handle & 0x3;
412         h = (idx << 2) | type;
413         switch (type) {
414         case HANDLE_WEAK:
415         case HANDLE_WEAK_TRACK:
416                 val = (gpointer)HIDE_POINTER (val);
417                 gc_handles [idx] = val;
418                 gc_handle_types [idx] = type;
419 #if HAVE_BOEHM_GC
420                 if (gc_handles [idx] != (gpointer)-1)
421                         GC_GENERAL_REGISTER_DISAPPEARING_LINK (&(gc_handles [idx]), obj);
422 #endif
423                 break;
424         default:
425                 gc_handles [idx] = val;
426                 gc_handle_types [idx] = type;
427                 break;
428         }
429         LeaveCriticalSection (&handle_section);
430         return h;
431 }
432
433 void
434 ves_icall_System_GCHandle_FreeHandle (guint32 handle)
435 {
436         int idx = handle >> 2;
437         int type = handle & 0x3;
438
439         MONO_ARCH_SAVE_REGS;
440
441         EnterCriticalSection (&handle_section);
442
443 #ifdef HAVE_BOEHM_GC
444         g_assert (type == gc_handle_types [idx]);
445         if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK)) {
446                 if (gc_handles [idx] != (gpointer)-1)
447                         GC_unregister_disappearing_link (&(gc_handles [idx]));
448         }
449 #endif
450
451         gc_handles [idx] = (gpointer)-1;
452         gc_handle_types [idx] = (guint8)-1;
453         LeaveCriticalSection (&handle_section);
454 }
455
456 gpointer
457 ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle)
458 {
459         MonoObject *obj;
460         int type = handle & 0x3;
461
462         MONO_ARCH_SAVE_REGS;
463
464         if (gc_handles) {
465                 EnterCriticalSection (&handle_section);
466                 obj = gc_handles [handle >> 2];
467                 g_assert (gc_handle_types [handle >> 2] == type);
468                 LeaveCriticalSection (&handle_section);
469                 if ((type == HANDLE_WEAK) || (type == HANDLE_WEAK_TRACK)) {
470                         obj = REVEAL_POINTER (obj);
471                         if (obj == (MonoObject *) -1)
472                                 return NULL;
473                 }
474                 if (obj) {
475                         MonoClass *klass = mono_object_class (obj);
476                         if (klass == mono_defaults.string_class) {
477                                 return mono_string_chars ((MonoString*)obj);
478                         } else if (klass->rank) {
479                                 return mono_array_addr ((MonoArray*)obj, char, 0);
480                         } else {
481                                 /* the C# code will check and throw the exception */
482                                 /* FIXME: missing !klass->blittable test, see bug #61134 */
483                                 if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT)
484                                         return (gpointer)-1;
485                                 return (char*)obj + sizeof (MonoObject);
486                         }
487                 }
488         }
489         return NULL;
490 }
491
492 guint32
493 mono_gchandle_new (MonoObject *obj, gboolean pinned)
494 {
495         return ves_icall_System_GCHandle_GetTargetHandle (obj, 0, pinned? HANDLE_PINNED: HANDLE_NORMAL);
496 }
497
498 guint32
499 mono_gchandle_new_weakref (MonoObject *obj, gboolean track_resurrection)
500 {
501         return ves_icall_System_GCHandle_GetTargetHandle (obj, 0, track_resurrection? HANDLE_WEAK_TRACK: HANDLE_WEAK);
502 }
503
504 /* This will return NULL for a collected object if using a weakref handle */
505 MonoObject*
506 mono_gchandle_get_target (guint32 gchandle)
507 {
508         return ves_icall_System_GCHandle_GetTarget (gchandle);
509 }
510
511 void
512 mono_gchandle_free (guint32 gchandle)
513 {
514         ves_icall_System_GCHandle_FreeHandle (gchandle);
515 }
516
517 #if HAVE_BOEHM_GC
518
519 static HANDLE finalizer_event;
520 static volatile gboolean finished=FALSE;
521
522 static void finalize_notify (void)
523 {
524 #ifdef DEBUG
525         g_message (G_GNUC_PRETTY_FUNCTION ": prodding finalizer");
526 #endif
527
528         SetEvent (finalizer_event);
529 }
530
531 static void
532 collect_objects (gpointer key, gpointer value, gpointer user_data)
533 {
534         GPtrArray *arr = (GPtrArray*)user_data;
535         g_ptr_array_add (arr, key);
536 }
537
538 /*
539  * finalize_domain_objects:
540  *
541  *  Run the finalizers of all finalizable objects in req->domain.
542  */
543 static void
544 finalize_domain_objects (DomainFinalizationReq *req)
545 {
546         int i;
547         GPtrArray *objs;
548         MonoDomain *domain = req->domain;
549         
550         while (g_hash_table_size (domain->finalizable_objects_hash) > 0) {
551                 /* 
552                  * Since the domain is unloading, nobody is allowed to put
553                  * new entries into the hash table. But finalize_object might
554                  * remove entries from the hash table, so we make a copy.
555                  */
556                 objs = g_ptr_array_new ();
557                 g_hash_table_foreach (domain->finalizable_objects_hash, 
558                                                           collect_objects, objs);
559                 /* printf ("FINALIZING %d OBJECTS.\n", objs->len); */
560
561                 for (i = 0; i < objs->len; ++i) {
562                         MonoObject *o = (MonoObject*)g_ptr_array_index (objs, i);
563                         /* FIXME: Avoid finalizing threads, etc */
564                         run_finalize (o, 0);
565                 }
566
567                 g_ptr_array_free (objs, TRUE);
568         }
569
570         /* printf ("DONE.\n"); */
571         SetEvent (req->done_event);
572
573         /* The event is closed in mono_domain_finalize if we get here */
574         g_free (req);
575 }
576
577 static guint32 finalizer_thread (gpointer unused)
578 {
579         gc_thread = mono_thread_current ();
580
581         SetEvent (thread_started_event);
582
583         while(!finished) {
584                 /* Wait to be notified that there's at least one
585                  * finaliser to run
586                  */
587                 WaitForSingleObjectEx (finalizer_event, INFINITE, TRUE);
588
589                 if (domains_to_finalize) {
590                         EnterCriticalSection (&finalizer_mutex);
591                         if (domains_to_finalize) {
592                                 DomainFinalizationReq *req = domains_to_finalize->data;
593                                 domains_to_finalize = g_slist_remove (domains_to_finalize, req);
594                                 LeaveCriticalSection (&finalizer_mutex);
595
596                                 finalize_domain_objects (req);
597                         }
598                         else
599                                 LeaveCriticalSection (&finalizer_mutex);
600                 }                               
601
602 #ifdef DEBUG
603                 g_message (G_GNUC_PRETTY_FUNCTION ": invoking finalizers");
604 #endif
605
606                 /* If finished == TRUE, mono_gc_cleanup has been called (from mono_runtime_cleanup),
607                  * before the domain is unloaded.
608                  *
609                  * There is a bug in GC_invoke_finalizer () in versions <= 6.2alpha4:
610                  * the 'mem_freed' variable is not initialized when there are no
611                  * objects to finalize, which leads to strange behavior later on.
612                  * The check is necessary to work around that bug.
613                  */
614                 if (GC_should_invoke_finalizers ()) {
615                         GC_invoke_finalizers ();
616                 }
617
618                 SetEvent (pending_done_event);
619         }
620
621         SetEvent (shutdown_event);
622         return(0);
623 }
624
625 /* 
626  * Enable or disable the separate finalizer thread.
627  * It's currently disabled because it still requires some
628  * work in the rest of the runtime.
629  */
630 #define ENABLE_FINALIZER_THREAD
631
632 #ifdef WITH_INCLUDED_LIBGC
633 /* from threads.c */
634 extern void mono_gc_stop_world (void);
635 extern void mono_gc_start_world (void);
636 extern void mono_gc_push_all_stacks (void);
637
638 static void mono_gc_lock (void)
639 {
640         EnterCriticalSection (&allocator_section);
641 }
642
643 static void mono_gc_unlock (void)
644 {
645         LeaveCriticalSection (&allocator_section);
646 }
647
648 static GCThreadFunctions mono_gc_thread_vtable = {
649         NULL,
650
651         mono_gc_lock,
652         mono_gc_unlock,
653
654         mono_gc_stop_world,
655         NULL,
656         mono_gc_push_all_stacks,
657         mono_gc_start_world
658 };
659 #endif /* WITH_INCLUDED_LIBGC */
660
661 static void
662 mono_gc_warning (char *msg, GC_word arg)
663 {
664         mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_GC, msg, (unsigned long)arg);
665 }
666
667 void mono_gc_init (void)
668 {
669         InitializeCriticalSection (&handle_section);
670         InitializeCriticalSection (&allocator_section);
671
672         InitializeCriticalSection (&finalizer_mutex);
673
674 #ifdef WITH_INCLUDED_LIBGC
675         gc_thread_vtable = &mono_gc_thread_vtable;
676 #endif
677         
678         MONO_GC_REGISTER_ROOT (gc_handles);
679         MONO_GC_REGISTER_ROOT (gc_handle_types);
680         GC_no_dls = TRUE;
681
682         GC_oom_fn = mono_gc_out_of_memory;
683
684         GC_set_warn_proc (mono_gc_warning);
685
686 #ifdef ENABLE_FINALIZER_THREAD
687
688         if (g_getenv ("GC_DONT_GC")) {
689                 gc_disabled = TRUE;
690                 return;
691         }
692         
693         finalizer_event = CreateEvent (NULL, FALSE, FALSE, NULL);
694         pending_done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
695         shutdown_event = CreateEvent (NULL, TRUE, FALSE, NULL);
696         thread_started_event = CreateEvent (NULL, TRUE, FALSE, NULL);
697         if (finalizer_event == NULL || pending_done_event == NULL || shutdown_event == NULL || thread_started_event == NULL) {
698                 g_assert_not_reached ();
699         }
700
701         GC_finalize_on_demand = 1;
702         GC_finalizer_notifier = finalize_notify;
703
704         mono_thread_create (mono_domain_get (), finalizer_thread, NULL);
705         /*
706          * Wait until the finalizer thread sets gc_thread since its value is needed
707          * by mono_thread_attach ()
708          */
709         WaitForSingleObjectEx (thread_started_event, INFINITE, FALSE);
710 #endif
711 }
712
713 void mono_gc_cleanup (void)
714 {
715 #ifdef DEBUG
716         g_message (G_GNUC_PRETTY_FUNCTION ": cleaning up finalizer");
717 #endif
718
719 #ifdef ENABLE_FINALIZER_THREAD
720         if (!gc_disabled) {
721                 ResetEvent (shutdown_event);
722                 finished = TRUE;
723                 finalize_notify ();
724                 /* Finishing the finalizer thread, so wait a little bit... */
725                 /* MS seems to wait for about 2 seconds */
726                 if (WaitForSingleObjectEx (shutdown_event, 2000, FALSE) == WAIT_TIMEOUT) {
727                         mono_thread_stop (gc_thread);
728                 }
729         }
730
731 #endif
732 }
733
734 void
735 mono_gc_disable (void)
736 {
737 #ifdef HAVE_GC_ENABLE
738         GC_disable ();
739 #else
740         g_assert_not_reached ();
741 #endif
742 }
743
744 void
745 mono_gc_enable (void)
746 {
747 #ifdef HAVE_GC_ENABLE
748         GC_enable ();
749 #else
750         g_assert_not_reached ();
751 #endif
752 }
753
754 #else
755
756 /* no Boehm GC support. */
757 void mono_gc_init (void)
758 {
759         InitializeCriticalSection (&handle_section);
760 }
761
762 void mono_gc_cleanup (void)
763 {
764 }
765
766 void
767 mono_gc_disable (void)
768 {
769 }
770
771 void
772 mono_gc_enable (void)
773 {
774 }
775
776 #endif
777
778 gboolean
779 mono_gc_is_finalizer_thread (MonoThread *thread)
780 {
781         return thread == gc_thread;
782 }
783
784