Merge pull request #1952 from esdrubal/proc_name
[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-internal.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/utils/mono-logger-internal.h>
29 #include <mono/metadata/gc-internal.h>
30 #include <mono/metadata/marshal.h> /* for mono_delegate_free_ftnptr () */
31 #include <mono/metadata/attach.h>
32 #include <mono/metadata/console-io.h>
33 #include <mono/utils/mono-semaphore.h>
34 #include <mono/utils/mono-memory-model.h>
35 #include <mono/utils/mono-counters.h>
36 #include <mono/utils/mono-time.h>
37 #include <mono/utils/dtrace.h>
38 #include <mono/utils/mono-threads.h>
39 #include <mono/utils/atomic.h>
40
41 #ifndef HOST_WIN32
42 #include <pthread.h>
43 #endif
44
45 typedef struct DomainFinalizationReq {
46         MonoDomain *domain;
47         HANDLE done_event;
48 } DomainFinalizationReq;
49
50 static gboolean gc_disabled = FALSE;
51
52 static gboolean finalizing_root_domain = FALSE;
53
54 gboolean log_finalizers = FALSE;
55 gboolean do_not_finalize = FALSE;
56
57 #define mono_finalizer_lock() mono_mutex_lock (&finalizer_mutex)
58 #define mono_finalizer_unlock() mono_mutex_unlock (&finalizer_mutex)
59 static mono_mutex_t finalizer_mutex;
60 static mono_mutex_t reference_queue_mutex;
61
62 static GSList *domains_to_finalize= NULL;
63 static MonoMList *threads_to_finalize = NULL;
64
65 static gboolean finalizer_thread_exited;
66 /* Uses finalizer_mutex */
67 static mono_cond_t exited_cond;
68
69 static MonoInternalThread *gc_thread;
70
71 static void object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*));
72
73 static void mono_gchandle_set_target (guint32 gchandle, MonoObject *obj);
74
75 static void reference_queue_proccess_all (void);
76 static void mono_reference_queue_cleanup (void);
77 static void reference_queue_clear_for_domain (MonoDomain *domain);
78 static HANDLE pending_done_event;
79
80 static guint32
81 guarded_wait (HANDLE handle, guint32 timeout, gboolean alertable)
82 {
83         guint32 result;
84
85         MONO_PREPARE_BLOCKING;
86         result = WaitForSingleObjectEx (handle, timeout, alertable);
87         MONO_FINISH_BLOCKING;
88
89         return result;
90 }
91
92 static void
93 add_thread_to_finalize (MonoInternalThread *thread)
94 {
95         mono_finalizer_lock ();
96         if (!threads_to_finalize)
97                 MONO_GC_REGISTER_ROOT_SINGLE (threads_to_finalize);
98         threads_to_finalize = mono_mlist_append (threads_to_finalize, (MonoObject*)thread);
99         mono_finalizer_unlock ();
100 }
101
102 static gboolean suspend_finalizers = FALSE;
103 /* 
104  * actually, we might want to queue the finalize requests in a separate thread,
105  * but we need to be careful about the execution domain of the thread...
106  */
107 void
108 mono_gc_run_finalize (void *obj, void *data)
109 {
110         if (do_not_finalize)
111                 return;
112
113         MonoObject *exc = NULL;
114         MonoObject *o;
115 #ifndef HAVE_SGEN_GC
116         MonoObject *o2;
117 #endif
118         MonoMethod* finalizer = NULL;
119         MonoDomain *caller_domain = mono_domain_get ();
120         MonoDomain *domain;
121         RuntimeInvokeFunction runtime_invoke;
122
123         o = (MonoObject*)((char*)obj + GPOINTER_TO_UINT (data));
124
125         if (log_finalizers)
126                 g_log ("mono-gc-finalizers", G_LOG_LEVEL_DEBUG, "<%s at %p> Starting finalizer checks.", o->vtable->klass->name, o);
127
128         if (suspend_finalizers)
129                 return;
130
131         domain = o->vtable->domain;
132
133 #ifndef HAVE_SGEN_GC
134         mono_domain_finalizers_lock (domain);
135
136         o2 = g_hash_table_lookup (domain->finalizable_objects_hash, o);
137
138         mono_domain_finalizers_unlock (domain);
139
140         if (!o2)
141                 /* Already finalized somehow */
142                 return;
143 #endif
144
145         /* make sure the finalizer is not called again if the object is resurrected */
146         object_register_finalizer (obj, NULL);
147
148         if (log_finalizers)
149                 g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Registered finalizer as processed.", o->vtable->klass->name, o);
150
151         if (o->vtable->klass == mono_defaults.internal_thread_class) {
152                 MonoInternalThread *t = (MonoInternalThread*)o;
153
154                 if (mono_gc_is_finalizer_internal_thread (t))
155                         /* Avoid finalizing ourselves */
156                         return;
157
158                 if (t->threadpool_thread && finalizing_root_domain) {
159                         /* Don't finalize threadpool threads when
160                            shutting down - they're finalized when the
161                            threadpool shuts down. */
162                         add_thread_to_finalize (t);
163                         return;
164                 }
165         }
166
167         if (o->vtable->klass->image == mono_defaults.corlib && !strcmp (o->vtable->klass->name, "DynamicMethod") && finalizing_root_domain) {
168                 /*
169                  * These can't be finalized during unloading/shutdown, since that would
170                  * free the native code which can still be referenced by other
171                  * finalizers.
172                  * FIXME: This is not perfect, objects dying at the same time as 
173                  * dynamic methods can still reference them even when !shutdown.
174                  */
175                 return;
176         }
177
178         if (mono_runtime_get_no_exec ())
179                 return;
180
181         /* speedup later... and use a timeout */
182         /* g_print ("Finalize run on %p %s.%s\n", o, mono_object_class (o)->name_space, mono_object_class (o)->name); */
183
184         /* Use _internal here, since this thread can enter a doomed appdomain */
185         mono_domain_set_internal (mono_object_domain (o));
186
187         /* delegates that have a native function pointer allocated are
188          * registered for finalization, but they don't have a Finalize
189          * method, because in most cases it's not needed and it's just a waste.
190          */
191         if (o->vtable->klass->delegate) {
192                 MonoDelegate* del = (MonoDelegate*)o;
193                 if (del->delegate_trampoline)
194                         mono_delegate_free_ftnptr ((MonoDelegate*)o);
195                 mono_domain_set_internal (caller_domain);
196                 return;
197         }
198
199         finalizer = mono_class_get_finalizer (o->vtable->klass);
200
201         /* If object has a CCW but has no finalizer, it was only
202          * registered for finalization in order to free the CCW.
203          * Else it needs the regular finalizer run.
204          * FIXME: what to do about ressurection and suppression
205          * of finalizer on object with CCW.
206          */
207         if (mono_marshal_free_ccw (o) && !finalizer) {
208                 mono_domain_set_internal (caller_domain);
209                 return;
210         }
211
212         /* 
213          * To avoid the locking plus the other overhead of mono_runtime_invoke (),
214          * create and precompile a wrapper which calls the finalize method using
215          * a CALLVIRT.
216          */
217         if (log_finalizers)
218                 g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Compiling finalizer.", o->vtable->klass->name, o);
219
220         if (!domain->finalize_runtime_invoke) {
221                 MonoMethod *invoke = mono_marshal_get_runtime_invoke (mono_class_get_method_from_name_flags (mono_defaults.object_class, "Finalize", 0, 0), TRUE);
222
223                 domain->finalize_runtime_invoke = mono_compile_method (invoke);
224         }
225
226         runtime_invoke = domain->finalize_runtime_invoke;
227
228         mono_runtime_class_init (o->vtable);
229
230         if (G_UNLIKELY (MONO_GC_FINALIZE_INVOKE_ENABLED ())) {
231                 MONO_GC_FINALIZE_INVOKE ((unsigned long)o, mono_object_get_size (o),
232                                 o->vtable->klass->name_space, o->vtable->klass->name);
233         }
234
235         if (log_finalizers)
236                 g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Calling finalizer.", o->vtable->klass->name, o);
237
238         runtime_invoke (o, NULL, &exc, NULL);
239
240         if (log_finalizers)
241                 g_log ("mono-gc-finalizers", G_LOG_LEVEL_MESSAGE, "<%s at %p> Returned from finalizer.", o->vtable->klass->name, o);
242
243         if (exc)
244                 mono_thread_internal_unhandled_exception (exc);
245
246         mono_domain_set_internal (caller_domain);
247 }
248
249 void
250 mono_gc_finalize_threadpool_threads (void)
251 {
252         while (threads_to_finalize) {
253                 MonoInternalThread *thread = (MonoInternalThread*) mono_mlist_get_data (threads_to_finalize);
254
255                 /* Force finalization of the thread. */
256                 thread->threadpool_thread = FALSE;
257                 mono_object_register_finalizer ((MonoObject*)thread);
258
259                 mono_gc_run_finalize (thread, NULL);
260
261                 threads_to_finalize = mono_mlist_next (threads_to_finalize);
262         }
263 }
264
265 gpointer
266 mono_gc_out_of_memory (size_t size)
267 {
268         /* 
269          * we could allocate at program startup some memory that we could release 
270          * back to the system at this point if we're really low on memory (ie, size is
271          * lower than the memory we set apart)
272          */
273         mono_raise_exception (mono_domain_get ()->out_of_memory_ex);
274
275         return NULL;
276 }
277
278 /*
279  * Some of our objects may point to a different address than the address returned by GC_malloc()
280  * (because of the GetHashCode hack), but we need to pass the real address to register_finalizer.
281  * This also means that in the callback we need to adjust the pointer to get back the real
282  * MonoObject*.
283  * We also need to be consistent in the use of the GC_debug* variants of malloc and register_finalizer, 
284  * since that, too, can cause the underlying pointer to be offset.
285  */
286 static void
287 object_register_finalizer (MonoObject *obj, void (*callback)(void *, void*))
288 {
289         MonoDomain *domain;
290
291         if (obj == NULL)
292                 mono_raise_exception (mono_get_exception_argument_null ("obj"));
293
294         domain = obj->vtable->domain;
295
296 #if HAVE_BOEHM_GC
297         if (mono_domain_is_unloading (domain) && (callback != NULL))
298                 /*
299                  * Can't register finalizers in a dying appdomain, since they
300                  * could be invoked after the appdomain has been unloaded.
301                  */
302                 return;
303
304         mono_domain_finalizers_lock (domain);
305
306         if (callback)
307                 g_hash_table_insert (domain->finalizable_objects_hash, obj, obj);
308         else
309                 g_hash_table_remove (domain->finalizable_objects_hash, obj);
310
311         mono_domain_finalizers_unlock (domain);
312
313         mono_gc_register_for_finalization (obj, callback);
314 #elif defined(HAVE_SGEN_GC)
315         /*
316          * If we register finalizers for domains that are unloading we might
317          * end up running them while or after the domain is being cleared, so
318          * the objects will not be valid anymore.
319          */
320         if (!mono_domain_is_unloading (domain))
321                 mono_gc_register_for_finalization (obj, callback);
322 #endif
323 }
324
325 /**
326  * mono_object_register_finalizer:
327  * @obj: object to register
328  *
329  * Records that object @obj has a finalizer, this will call the
330  * Finalize method when the garbage collector disposes the object.
331  * 
332  */
333 void
334 mono_object_register_finalizer (MonoObject *obj)
335 {
336         /* g_print ("Registered finalizer on %p %s.%s\n", obj, mono_object_class (obj)->name_space, mono_object_class (obj)->name); */
337         object_register_finalizer (obj, mono_gc_run_finalize);
338 }
339
340 /**
341  * mono_domain_finalize:
342  * @domain: the domain to finalize
343  * @timeout: msects to wait for the finalization to complete, -1 to wait indefinitely
344  *
345  *  Request finalization of all finalizable objects inside @domain. Wait
346  * @timeout msecs for the finalization to complete.
347  *
348  * Returns: TRUE if succeeded, FALSE if there was a timeout
349  */
350
351 gboolean
352 mono_domain_finalize (MonoDomain *domain, guint32 timeout) 
353 {
354         DomainFinalizationReq *req;
355         guint32 res;
356         HANDLE done_event;
357         MonoInternalThread *thread = mono_thread_internal_current ();
358
359 #if defined(__native_client__)
360         return FALSE;
361 #endif
362
363         if (mono_thread_internal_current () == gc_thread)
364                 /* We are called from inside a finalizer, not much we can do here */
365                 return FALSE;
366
367         /* 
368          * No need to create another thread 'cause the finalizer thread
369          * is still working and will take care of running the finalizers
370          */ 
371         
372         if (gc_disabled)
373                 return TRUE;
374
375         /* We don't support domain finalization without a GC */
376         if (mono_gc_is_null ())
377                 return FALSE;
378
379         mono_gc_collect (mono_gc_max_generation ());
380
381         done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
382         if (done_event == NULL) {
383                 return FALSE;
384         }
385
386         req = g_new0 (DomainFinalizationReq, 1);
387         req->domain = domain;
388         req->done_event = done_event;
389
390         if (domain == mono_get_root_domain ())
391                 finalizing_root_domain = TRUE;
392         
393         mono_finalizer_lock ();
394
395         domains_to_finalize = g_slist_append (domains_to_finalize, req);
396
397         mono_finalizer_unlock ();
398
399         /* Tell the finalizer thread to finalize this appdomain */
400         mono_gc_finalize_notify ();
401
402         if (timeout == -1)
403                 timeout = INFINITE;
404
405         while (TRUE) {
406                 res = guarded_wait (done_event, timeout, TRUE);
407                 /* printf ("WAIT RES: %d.\n", res); */
408
409                 if (res == WAIT_IO_COMPLETION) {
410                         if ((thread->state & (ThreadState_StopRequested | ThreadState_SuspendRequested)) != 0)
411                                 return FALSE;
412                 } else if (res == WAIT_TIMEOUT) {
413                         /* We leak the handle here */
414                         return FALSE;
415                 } else {
416                         break;
417                 }
418         }
419
420         CloseHandle (done_event);
421
422         if (domain == mono_get_root_domain ()) {
423                 mono_threadpool_ms_cleanup ();
424                 mono_gc_finalize_threadpool_threads ();
425         }
426
427         return TRUE;
428 }
429
430 void
431 ves_icall_System_GC_InternalCollect (int generation)
432 {
433         mono_gc_collect (generation);
434 }
435
436 gint64
437 ves_icall_System_GC_GetTotalMemory (MonoBoolean forceCollection)
438 {
439         if (forceCollection)
440                 mono_gc_collect (mono_gc_max_generation ());
441         return mono_gc_get_used_size ();
442 }
443
444 void
445 ves_icall_System_GC_KeepAlive (MonoObject *obj)
446 {
447         /*
448          * Does nothing.
449          */
450 }
451
452 void
453 ves_icall_System_GC_ReRegisterForFinalize (MonoObject *obj)
454 {
455         MONO_CHECK_ARG_NULL (obj,);
456
457         object_register_finalizer (obj, mono_gc_run_finalize);
458 }
459
460 void
461 ves_icall_System_GC_SuppressFinalize (MonoObject *obj)
462 {
463         MONO_CHECK_ARG_NULL (obj,);
464
465         /* delegates have no finalizers, but we register them to deal with the
466          * unmanaged->managed trampoline. We don't let the user suppress it
467          * otherwise we'd leak it.
468          */
469         if (obj->vtable->klass->delegate)
470                 return;
471
472         /* FIXME: Need to handle case where obj has COM Callable Wrapper
473          * generated for it that needs cleaned up, but user wants to suppress
474          * their derived object finalizer. */
475
476         object_register_finalizer (obj, NULL);
477 }
478
479 void
480 ves_icall_System_GC_WaitForPendingFinalizers (void)
481 {
482         if (mono_gc_is_null ())
483                 return;
484
485         if (!mono_gc_pending_finalizers ())
486                 return;
487
488         if (mono_thread_internal_current () == gc_thread)
489                 /* Avoid deadlocks */
490                 return;
491
492         /*
493         If the finalizer thread is not live, lets pretend no finalizers are pending since the current thread might
494         be the one responsible for starting it up.
495         */
496         if (gc_thread == NULL)
497                 return;
498
499         ResetEvent (pending_done_event);
500         mono_gc_finalize_notify ();
501         /* g_print ("Waiting for pending finalizers....\n"); */
502         guarded_wait (pending_done_event, INFINITE, TRUE);
503         /* g_print ("Done pending....\n"); */
504 }
505
506 void
507 ves_icall_System_GC_register_ephemeron_array (MonoObject *array)
508 {
509 #ifdef HAVE_SGEN_GC
510         if (!mono_gc_ephemeron_array_add (array)) {
511                 mono_set_pending_exception (mono_object_domain (array)->out_of_memory_ex);
512                 return;
513         }
514 #endif
515 }
516
517 MonoObject*
518 ves_icall_System_GC_get_ephemeron_tombstone (void)
519 {
520         return mono_domain_get ()->ephemeron_tombstone;
521 }
522
523 #define mono_allocator_lock() mono_mutex_lock (&allocator_section)
524 #define mono_allocator_unlock() mono_mutex_unlock (&allocator_section)
525 static mono_mutex_t allocator_section;
526 static mono_mutex_t handle_section;
527
528 typedef enum {
529         HANDLE_WEAK,
530         HANDLE_WEAK_TRACK,
531         HANDLE_NORMAL,
532         HANDLE_PINNED
533 } HandleType;
534
535 static HandleType mono_gchandle_get_type (guint32 gchandle);
536
537 MonoObject *
538 ves_icall_System_GCHandle_GetTarget (guint32 handle)
539 {
540         return mono_gchandle_get_target (handle);
541 }
542
543 /*
544  * if type == -1, change the target of the handle, otherwise allocate a new handle.
545  */
546 guint32
547 ves_icall_System_GCHandle_GetTargetHandle (MonoObject *obj, guint32 handle, gint32 type)
548 {
549         if (type == -1) {
550                 mono_gchandle_set_target (handle, obj);
551                 /* the handle doesn't change */
552                 return handle;
553         }
554         switch (type) {
555         case HANDLE_WEAK:
556                 return mono_gchandle_new_weakref (obj, FALSE);
557         case HANDLE_WEAK_TRACK:
558                 return mono_gchandle_new_weakref (obj, TRUE);
559         case HANDLE_NORMAL:
560                 return mono_gchandle_new (obj, FALSE);
561         case HANDLE_PINNED:
562                 return mono_gchandle_new (obj, TRUE);
563         default:
564                 g_assert_not_reached ();
565         }
566         return 0;
567 }
568
569 void
570 ves_icall_System_GCHandle_FreeHandle (guint32 handle)
571 {
572         mono_gchandle_free (handle);
573 }
574
575 gpointer
576 ves_icall_System_GCHandle_GetAddrOfPinnedObject (guint32 handle)
577 {
578         MonoObject *obj;
579
580         if (mono_gchandle_get_type (handle) != HANDLE_PINNED)
581                 return (gpointer)-2;
582         obj = mono_gchandle_get_target (handle);
583         if (obj) {
584                 MonoClass *klass = mono_object_class (obj);
585                 if (klass == mono_defaults.string_class) {
586                         return mono_string_chars ((MonoString*)obj);
587                 } else if (klass->rank) {
588                         return mono_array_addr ((MonoArray*)obj, char, 0);
589                 } else {
590                         /* the C# code will check and throw the exception */
591                         /* FIXME: missing !klass->blittable test, see bug #61134 */
592                         if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT)
593                                 return (gpointer)-1;
594                         return (char*)obj + sizeof (MonoObject);
595                 }
596         }
597         return NULL;
598 }
599
600 MonoBoolean
601 ves_icall_Mono_Runtime_SetGCAllowSynchronousMajor (MonoBoolean flag)
602 {
603         return mono_gc_set_allow_synchronous_major (flag);
604 }
605
606 typedef struct {
607         guint32  *bitmap;
608         gpointer *entries;
609         guint32   size;
610         guint8    type;
611         guint     slot_hint : 24; /* starting slot for search */
612         /* 2^16 appdomains should be enough for everyone (though I know I'll regret this in 20 years) */
613         /* we alloc this only for weak refs, since we can get the domain directly in the other cases */
614         guint16  *domain_ids;
615 } HandleData;
616
617 /* weak and weak-track arrays will be allocated in malloc memory 
618  */
619 static HandleData gc_handles [] = {
620         {NULL, NULL, 0, HANDLE_WEAK, 0},
621         {NULL, NULL, 0, HANDLE_WEAK_TRACK, 0},
622         {NULL, NULL, 0, HANDLE_NORMAL, 0},
623         {NULL, NULL, 0, HANDLE_PINNED, 0}
624 };
625
626 #define lock_handles(handles) mono_mutex_lock (&handle_section)
627 #define unlock_handles(handles) mono_mutex_unlock (&handle_section)
628
629 static int
630 find_first_unset (guint32 bitmap)
631 {
632         int i;
633         for (i = 0; i < 32; ++i) {
634                 if (!(bitmap & (1 << i)))
635                         return i;
636         }
637         return -1;
638 }
639
640 static MonoGCDescriptor
641 make_root_descr_all_refs (int numbits, gboolean pinned)
642 {
643 #ifdef HAVE_SGEN_GC
644         if (pinned)
645                 return MONO_GC_DESCRIPTOR_NULL;
646 #endif
647         return mono_gc_make_root_descr_all_refs (numbits);
648 }
649
650 static guint32
651 alloc_handle (HandleData *handles, MonoObject *obj, gboolean track)
652 {
653         gint slot, i;
654         guint32 res;
655         lock_handles (handles);
656         if (!handles->size) {
657                 handles->size = 32;
658                 if (handles->type > HANDLE_WEAK_TRACK) {
659                         handles->entries = mono_gc_alloc_fixed (sizeof (gpointer) * handles->size, make_root_descr_all_refs (handles->size, handles->type == HANDLE_PINNED));
660                 } else {
661                         handles->entries = g_malloc0 (sizeof (gpointer) * handles->size);
662                         handles->domain_ids = g_malloc0 (sizeof (guint16) * handles->size);
663                 }
664                 handles->bitmap = g_malloc0 (handles->size / 8);
665         }
666         i = -1;
667         for (slot = handles->slot_hint; slot < handles->size / 32; ++slot) {
668                 if (handles->bitmap [slot] != 0xffffffff) {
669                         i = find_first_unset (handles->bitmap [slot]);
670                         handles->slot_hint = slot;
671                         break;
672                 }
673         }
674         if (i == -1 && handles->slot_hint != 0) {
675                 for (slot = 0; slot < handles->slot_hint; ++slot) {
676                         if (handles->bitmap [slot] != 0xffffffff) {
677                                 i = find_first_unset (handles->bitmap [slot]);
678                                 handles->slot_hint = slot;
679                                 break;
680                         }
681                 }
682         }
683         if (i == -1) {
684                 guint32 *new_bitmap;
685                 guint32 new_size = handles->size * 2; /* always double: we memset to 0 based on this below */
686
687                 /* resize and copy the bitmap */
688                 new_bitmap = g_malloc0 (new_size / 8);
689                 memcpy (new_bitmap, handles->bitmap, handles->size / 8);
690                 g_free (handles->bitmap);
691                 handles->bitmap = new_bitmap;
692
693                 /* resize and copy the entries */
694                 if (handles->type > HANDLE_WEAK_TRACK) {
695                         gpointer *entries;
696
697                         entries = mono_gc_alloc_fixed (sizeof (gpointer) * new_size, make_root_descr_all_refs (new_size, handles->type == HANDLE_PINNED));
698                         mono_gc_memmove_aligned (entries, handles->entries, sizeof (gpointer) * handles->size);
699
700                         mono_gc_free_fixed (handles->entries);
701                         handles->entries = entries;
702                 } else {
703                         gpointer *entries;
704                         guint16 *domain_ids;
705                         domain_ids = g_malloc0 (sizeof (guint16) * new_size);
706                         entries = g_malloc0 (sizeof (gpointer) * new_size);
707                         memcpy (domain_ids, handles->domain_ids, sizeof (guint16) * handles->size);
708                         for (i = 0; i < handles->size; ++i) {
709                                 MonoObject *obj = mono_gc_weak_link_get (&(handles->entries [i]));
710                                 if (obj) {
711                                         mono_gc_weak_link_add (&(entries [i]), obj, track);
712                                         mono_gc_weak_link_remove (&(handles->entries [i]), track);
713                                 } else {
714                                         g_assert (!handles->entries [i]);
715                                 }
716                         }
717                         g_free (handles->entries);
718                         g_free (handles->domain_ids);
719                         handles->entries = entries;
720                         handles->domain_ids = domain_ids;
721                 }
722
723                 /* set i and slot to the next free position */
724                 i = 0;
725                 slot = (handles->size + 1) / 32;
726                 handles->slot_hint = handles->size + 1;
727                 handles->size = new_size;
728         }
729         handles->bitmap [slot] |= 1 << i;
730         slot = slot * 32 + i;
731         handles->entries [slot] = NULL;
732         if (handles->type <= HANDLE_WEAK_TRACK) {
733                 /*FIXME, what to use when obj == null?*/
734                 handles->domain_ids [slot] = (obj ? mono_object_get_domain (obj) : mono_domain_get ())->domain_id;
735                 if (obj)
736                         mono_gc_weak_link_add (&(handles->entries [slot]), obj, track);
737         } else {
738                 handles->entries [slot] = obj;
739         }
740
741 #ifndef DISABLE_PERFCOUNTERS
742         mono_perfcounters->gc_num_handles++;
743 #endif
744         unlock_handles (handles);
745         /*g_print ("allocated entry %d of type %d to object %p (in slot: %p)\n", slot, handles->type, obj, handles->entries [slot]);*/
746         res = (slot << 3) | (handles->type + 1);
747         mono_profiler_gc_handle (MONO_PROFILER_GC_HANDLE_CREATED, handles->type, res, obj);
748         return res;
749 }
750
751 /**
752  * mono_gchandle_new:
753  * @obj: managed object to get a handle for
754  * @pinned: whether the object should be pinned
755  *
756  * This returns a handle that wraps the object, this is used to keep a
757  * reference to a managed object from the unmanaged world and preventing the
758  * object from being disposed.
759  * 
760  * If @pinned is false the address of the object can not be obtained, if it is
761  * true the address of the object can be obtained.  This will also pin the
762  * object so it will not be possible by a moving garbage collector to move the
763  * object. 
764  * 
765  * Returns: a handle that can be used to access the object from
766  * unmanaged code.
767  */
768 guint32
769 mono_gchandle_new (MonoObject *obj, gboolean pinned)
770 {
771         return alloc_handle (&gc_handles [pinned? HANDLE_PINNED: HANDLE_NORMAL], obj, FALSE);
772 }
773
774 /**
775  * mono_gchandle_new_weakref:
776  * @obj: managed object to get a handle for
777  * @pinned: whether the object should be pinned
778  *
779  * This returns a weak handle that wraps the object, this is used to
780  * keep a reference to a managed object from the unmanaged world.
781  * Unlike the mono_gchandle_new the object can be reclaimed by the
782  * garbage collector.  In this case the value of the GCHandle will be
783  * set to zero.
784  * 
785  * If @pinned is false the address of the object can not be obtained, if it is
786  * true the address of the object can be obtained.  This will also pin the
787  * object so it will not be possible by a moving garbage collector to move the
788  * object. 
789  * 
790  * Returns: a handle that can be used to access the object from
791  * unmanaged code.
792  */
793 guint32
794 mono_gchandle_new_weakref (MonoObject *obj, gboolean track_resurrection)
795 {
796         guint32 handle = alloc_handle (&gc_handles [track_resurrection? HANDLE_WEAK_TRACK: HANDLE_WEAK], obj, track_resurrection);
797
798         return handle;
799 }
800
801 static HandleType
802 mono_gchandle_get_type (guint32 gchandle)
803 {
804         guint type = (gchandle & 7) - 1;
805
806         return type;
807 }
808
809 /**
810  * mono_gchandle_get_target:
811  * @gchandle: a GCHandle's handle.
812  *
813  * The handle was previously created by calling mono_gchandle_new or
814  * mono_gchandle_new_weakref. 
815  *
816  * Returns a pointer to the MonoObject represented by the handle or
817  * NULL for a collected object if using a weakref handle.
818  */
819 MonoObject*
820 mono_gchandle_get_target (guint32 gchandle)
821 {
822         guint slot = gchandle >> 3;
823         guint type = (gchandle & 7) - 1;
824         HandleData *handles = &gc_handles [type];
825         MonoObject *obj = NULL;
826         if (type > 3)
827                 return NULL;
828         lock_handles (handles);
829         if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
830                 if (handles->type <= HANDLE_WEAK_TRACK) {
831                         obj = mono_gc_weak_link_get (&handles->entries [slot]);
832                 } else {
833                         obj = handles->entries [slot];
834                 }
835         } else {
836                 /* print a warning? */
837         }
838         unlock_handles (handles);
839         /*g_print ("get target of entry %d of type %d: %p\n", slot, handles->type, obj);*/
840         return obj;
841 }
842
843 static void
844 mono_gchandle_set_target (guint32 gchandle, MonoObject *obj)
845 {
846         guint slot = gchandle >> 3;
847         guint type = (gchandle & 7) - 1;
848         HandleData *handles = &gc_handles [type];
849
850         if (type > 3)
851                 return;
852         lock_handles (handles);
853         if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
854                 if (handles->type <= HANDLE_WEAK_TRACK) {
855                         if (handles->entries [slot])
856                                 mono_gc_weak_link_remove (&handles->entries [slot], handles->type == HANDLE_WEAK_TRACK);
857                         if (obj)
858                                 mono_gc_weak_link_add (&handles->entries [slot], obj, handles->type == HANDLE_WEAK_TRACK);
859                         /*FIXME, what to use when obj == null?*/
860                         handles->domain_ids [slot] = (obj ? mono_object_get_domain (obj) : mono_domain_get ())->domain_id;
861                 } else {
862                         handles->entries [slot] = obj;
863                 }
864         } else {
865                 /* print a warning? */
866         }
867         /*g_print ("changed entry %d of type %d to object %p (in slot: %p)\n", slot, handles->type, obj, handles->entries [slot]);*/
868         unlock_handles (handles);
869 }
870
871 /**
872  * mono_gchandle_is_in_domain:
873  * @gchandle: a GCHandle's handle.
874  * @domain: An application domain.
875  *
876  * Returns: true if the object wrapped by the @gchandle belongs to the specific @domain.
877  */
878 gboolean
879 mono_gchandle_is_in_domain (guint32 gchandle, MonoDomain *domain)
880 {
881         guint slot = gchandle >> 3;
882         guint type = (gchandle & 7) - 1;
883         HandleData *handles = &gc_handles [type];
884         gboolean result = FALSE;
885         if (type > 3)
886                 return FALSE;
887         lock_handles (handles);
888         if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
889                 if (handles->type <= HANDLE_WEAK_TRACK) {
890                         result = domain->domain_id == handles->domain_ids [slot];
891                 } else {
892                         MonoObject *obj;
893                         obj = handles->entries [slot];
894                         if (obj == NULL)
895                                 result = TRUE;
896                         else
897                                 result = domain == mono_object_domain (obj);
898                 }
899         } else {
900                 /* print a warning? */
901         }
902         unlock_handles (handles);
903         return result;
904 }
905
906 /**
907  * mono_gchandle_free:
908  * @gchandle: a GCHandle's handle.
909  *
910  * Frees the @gchandle handle.  If there are no outstanding
911  * references, the garbage collector can reclaim the memory of the
912  * object wrapped. 
913  */
914 void
915 mono_gchandle_free (guint32 gchandle)
916 {
917         guint slot = gchandle >> 3;
918         guint type = (gchandle & 7) - 1;
919         HandleData *handles = &gc_handles [type];
920         if (type > 3)
921                 return;
922
923         lock_handles (handles);
924         if (slot < handles->size && (handles->bitmap [slot / 32] & (1 << (slot % 32)))) {
925                 if (handles->type <= HANDLE_WEAK_TRACK) {
926                         if (handles->entries [slot])
927                                 mono_gc_weak_link_remove (&handles->entries [slot], handles->type == HANDLE_WEAK_TRACK);
928                 } else {
929                         handles->entries [slot] = NULL;
930                 }
931                 handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
932         } else {
933                 /* print a warning? */
934         }
935 #ifndef DISABLE_PERFCOUNTERS
936         mono_perfcounters->gc_num_handles--;
937 #endif
938         /*g_print ("freed entry %d of type %d\n", slot, handles->type);*/
939         unlock_handles (handles);
940         mono_profiler_gc_handle (MONO_PROFILER_GC_HANDLE_DESTROYED, handles->type, gchandle, NULL);
941 }
942
943 /**
944  * mono_gchandle_free_domain:
945  * @domain: domain that is unloading
946  *
947  * Function used internally to cleanup any GC handle for objects belonging
948  * to the specified domain during appdomain unload.
949  */
950 void
951 mono_gchandle_free_domain (MonoDomain *domain)
952 {
953         guint type;
954
955         for (type = 0; type < 3; ++type) {
956                 guint slot;
957                 HandleData *handles = &gc_handles [type];
958                 lock_handles (handles);
959                 for (slot = 0; slot < handles->size; ++slot) {
960                         if (!(handles->bitmap [slot / 32] & (1 << (slot % 32))))
961                                 continue;
962                         if (type <= HANDLE_WEAK_TRACK) {
963                                 if (domain->domain_id == handles->domain_ids [slot]) {
964                                         handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
965                                         if (handles->entries [slot])
966                                                 mono_gc_weak_link_remove (&handles->entries [slot], handles->type == HANDLE_WEAK_TRACK);
967                                 }
968                         } else {
969                                 if (handles->entries [slot] && mono_object_domain (handles->entries [slot]) == domain) {
970                                         handles->bitmap [slot / 32] &= ~(1 << (slot % 32));
971                                         handles->entries [slot] = NULL;
972                                 }
973                         }
974                 }
975                 unlock_handles (handles);
976         }
977
978 }
979
980 MonoBoolean
981 mono_gc_GCHandle_CheckCurrentDomain (guint32 gchandle)
982 {
983         return mono_gchandle_is_in_domain (gchandle, mono_domain_get ());
984 }
985
986 #ifdef MONO_HAS_SEMAPHORES
987 static MonoSemType finalizer_sem;
988 #endif
989 static HANDLE finalizer_event;
990 static volatile gboolean finished=FALSE;
991
992 void
993 mono_gc_finalize_notify (void)
994 {
995 #ifdef DEBUG
996         g_message ( "%s: prodding finalizer", __func__);
997 #endif
998
999         if (mono_gc_is_null ())
1000                 return;
1001
1002 #ifdef MONO_HAS_SEMAPHORES
1003         MONO_SEM_POST (&finalizer_sem);
1004 #else
1005         SetEvent (finalizer_event);
1006 #endif
1007 }
1008
1009 #ifdef HAVE_BOEHM_GC
1010
1011 static void
1012 collect_objects (gpointer key, gpointer value, gpointer user_data)
1013 {
1014         GPtrArray *arr = (GPtrArray*)user_data;
1015         g_ptr_array_add (arr, key);
1016 }
1017
1018 #endif
1019
1020 /*
1021  * finalize_domain_objects:
1022  *
1023  *  Run the finalizers of all finalizable objects in req->domain.
1024  */
1025 static void
1026 finalize_domain_objects (DomainFinalizationReq *req)
1027 {
1028         MonoDomain *domain = req->domain;
1029
1030 #if HAVE_SGEN_GC
1031 #define NUM_FOBJECTS 64
1032         MonoObject *to_finalize [NUM_FOBJECTS];
1033         int count;
1034 #endif
1035
1036         /* Process finalizers which are already in the queue */
1037         mono_gc_invoke_finalizers ();
1038
1039 #ifdef HAVE_BOEHM_GC
1040         while (g_hash_table_size (domain->finalizable_objects_hash) > 0) {
1041                 int i;
1042                 GPtrArray *objs;
1043                 /* 
1044                  * Since the domain is unloading, nobody is allowed to put
1045                  * new entries into the hash table. But finalize_object might
1046                  * remove entries from the hash table, so we make a copy.
1047                  */
1048                 objs = g_ptr_array_new ();
1049                 g_hash_table_foreach (domain->finalizable_objects_hash, collect_objects, objs);
1050                 /* printf ("FINALIZING %d OBJECTS.\n", objs->len); */
1051
1052                 for (i = 0; i < objs->len; ++i) {
1053                         MonoObject *o = (MonoObject*)g_ptr_array_index (objs, i);
1054                         /* FIXME: Avoid finalizing threads, etc */
1055                         mono_gc_run_finalize (o, 0);
1056                 }
1057
1058                 g_ptr_array_free (objs, TRUE);
1059         }
1060 #elif defined(HAVE_SGEN_GC)
1061         while ((count = mono_gc_finalizers_for_domain (domain, to_finalize, NUM_FOBJECTS))) {
1062                 int i;
1063                 for (i = 0; i < count; ++i) {
1064                         mono_gc_run_finalize (to_finalize [i], 0);
1065                 }
1066         }
1067 #endif
1068
1069         /* cleanup the reference queue */
1070         reference_queue_clear_for_domain (domain);
1071         
1072         /* printf ("DONE.\n"); */
1073         SetEvent (req->done_event);
1074
1075         /* The event is closed in mono_domain_finalize if we get here */
1076         g_free (req);
1077 }
1078
1079 static guint32
1080 finalizer_thread (gpointer unused)
1081 {
1082         gboolean wait = TRUE;
1083
1084         while (!finished) {
1085                 /* Wait to be notified that there's at least one
1086                  * finaliser to run
1087                  */
1088
1089                 g_assert (mono_domain_get () == mono_get_root_domain ());
1090                 mono_gc_set_skip_thread (TRUE);
1091                 MONO_PREPARE_BLOCKING;
1092
1093                 if (wait) {
1094                 /* An alertable wait is required so this thread can be suspended on windows */
1095 #ifdef MONO_HAS_SEMAPHORES
1096                         MONO_SEM_WAIT_ALERTABLE (&finalizer_sem, TRUE);
1097 #else
1098                         WaitForSingleObjectEx (finalizer_event, INFINITE, TRUE);
1099 #endif
1100                 }
1101                 wait = TRUE;
1102                 MONO_FINISH_BLOCKING;
1103                 mono_gc_set_skip_thread (FALSE);
1104
1105                 mono_threads_perform_thread_dump ();
1106
1107                 mono_console_handle_async_ops ();
1108
1109                 mono_attach_maybe_start ();
1110
1111                 if (domains_to_finalize) {
1112                         mono_finalizer_lock ();
1113                         if (domains_to_finalize) {
1114                                 DomainFinalizationReq *req = domains_to_finalize->data;
1115                                 domains_to_finalize = g_slist_remove (domains_to_finalize, req);
1116                                 mono_finalizer_unlock ();
1117
1118                                 finalize_domain_objects (req);
1119                         } else {
1120                                 mono_finalizer_unlock ();
1121                         }
1122                 }                               
1123
1124                 /* If finished == TRUE, mono_gc_cleanup has been called (from mono_runtime_cleanup),
1125                  * before the domain is unloaded.
1126                  */
1127                 mono_gc_invoke_finalizers ();
1128
1129                 mono_threads_join_threads ();
1130
1131                 reference_queue_proccess_all ();
1132
1133 #ifdef MONO_HAS_SEMAPHORES
1134                 /* Avoid posting the pending done event until there are pending finalizers */
1135                 if (MONO_SEM_TIMEDWAIT (&finalizer_sem, 0) == 0)
1136                         /* Don't wait again at the start of the loop */
1137                         wait = FALSE;
1138                 else
1139                         SetEvent (pending_done_event);
1140 #else
1141                         SetEvent (pending_done_event);
1142 #endif
1143         }
1144
1145         mono_finalizer_lock ();
1146         finalizer_thread_exited = TRUE;
1147         mono_cond_signal (&exited_cond);
1148         mono_finalizer_unlock ();
1149
1150         return 0;
1151 }
1152
1153 #ifndef LAZY_GC_THREAD_CREATION
1154 static
1155 #endif
1156 void
1157 mono_gc_init_finalizer_thread (void)
1158 {
1159         gc_thread = mono_thread_create_internal (mono_domain_get (), finalizer_thread, NULL, FALSE, 0);
1160         ves_icall_System_Threading_Thread_SetName_internal (gc_thread, mono_string_new (mono_domain_get (), "Finalizer"));
1161 }
1162
1163 void
1164 mono_gc_init (void)
1165 {
1166         mono_mutex_init_recursive (&handle_section);
1167         mono_mutex_init_recursive (&allocator_section);
1168
1169         mono_mutex_init_recursive (&finalizer_mutex);
1170         mono_mutex_init_recursive (&reference_queue_mutex);
1171
1172         MONO_GC_REGISTER_ROOT_FIXED (gc_handles [HANDLE_NORMAL].entries);
1173         MONO_GC_REGISTER_ROOT_FIXED (gc_handles [HANDLE_PINNED].entries);
1174
1175         mono_counters_register ("Minor GC collections", MONO_COUNTER_GC | MONO_COUNTER_UINT, &gc_stats.minor_gc_count);
1176         mono_counters_register ("Major GC collections", MONO_COUNTER_GC | MONO_COUNTER_UINT, &gc_stats.major_gc_count);
1177         mono_counters_register ("Minor GC time", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &gc_stats.minor_gc_time);
1178         mono_counters_register ("Major GC time", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &gc_stats.major_gc_time);
1179         mono_counters_register ("Major GC time concurrent", MONO_COUNTER_GC | MONO_COUNTER_ULONG | MONO_COUNTER_TIME, &gc_stats.major_gc_time_concurrent);
1180
1181         mono_gc_base_init ();
1182
1183         if (mono_gc_is_disabled ()) {
1184                 gc_disabled = TRUE;
1185                 return;
1186         }
1187         
1188         finalizer_event = CreateEvent (NULL, FALSE, FALSE, NULL);
1189         g_assert (finalizer_event);
1190         pending_done_event = CreateEvent (NULL, TRUE, FALSE, NULL);
1191         g_assert (pending_done_event);
1192         mono_cond_init (&exited_cond, 0);
1193 #ifdef MONO_HAS_SEMAPHORES
1194         MONO_SEM_INIT (&finalizer_sem, 0);
1195 #endif
1196
1197 #ifndef LAZY_GC_THREAD_CREATION
1198         mono_gc_init_finalizer_thread ();
1199 #endif
1200 }
1201
1202 void
1203 mono_gc_cleanup (void)
1204 {
1205 #ifdef DEBUG
1206         g_message ("%s: cleaning up finalizer", __func__);
1207 #endif
1208
1209         if (mono_gc_is_null ())
1210                 return;
1211
1212         if (!gc_disabled) {
1213                 finished = TRUE;
1214                 if (mono_thread_internal_current () != gc_thread) {
1215                         gboolean timed_out = FALSE;
1216                         guint32 start_ticks = mono_msec_ticks ();
1217                         guint32 end_ticks = start_ticks + 2000;
1218
1219                         mono_gc_finalize_notify ();
1220                         /* Finishing the finalizer thread, so wait a little bit... */
1221                         /* MS seems to wait for about 2 seconds */
1222                         while (!finalizer_thread_exited) {
1223                                 guint32 current_ticks = mono_msec_ticks ();
1224                                 guint32 timeout;
1225
1226                                 if (current_ticks >= end_ticks)
1227                                         break;
1228                                 else
1229                                         timeout = end_ticks - current_ticks;
1230                                 MONO_PREPARE_BLOCKING;
1231                                 mono_finalizer_lock ();
1232                                 if (!finalizer_thread_exited)
1233                                         mono_cond_timedwait_ms (&exited_cond, &finalizer_mutex, timeout);
1234                                 mono_finalizer_unlock ();
1235                                 MONO_FINISH_BLOCKING;
1236                         }
1237
1238                         if (!finalizer_thread_exited) {
1239                                 int ret;
1240
1241                                 /* Set a flag which the finalizer thread can check */
1242                                 suspend_finalizers = TRUE;
1243
1244                                 /* Try to abort the thread, in the hope that it is running managed code */
1245                                 mono_thread_internal_stop (gc_thread);
1246
1247                                 /* Wait for it to stop */
1248                                 ret = guarded_wait (gc_thread->handle, 100, TRUE);
1249
1250                                 if (ret == WAIT_TIMEOUT) {
1251                                         /* 
1252                                          * The finalizer thread refused to die. There is not much we 
1253                                          * can do here, since the runtime is shutting down so the 
1254                                          * state the finalizer thread depends on will vanish.
1255                                          */
1256                                         g_warning ("Shutting down finalizer thread timed out.");
1257                                         timed_out = TRUE;
1258                                 }
1259                         }
1260
1261                         if (!timed_out) {
1262                                 int ret;
1263
1264                                 /* Wait for the thread to actually exit */
1265                                 ret = guarded_wait (gc_thread->handle, INFINITE, TRUE);
1266                                 g_assert (ret == WAIT_OBJECT_0);
1267
1268                                 mono_thread_join (GUINT_TO_POINTER (gc_thread->tid));
1269                         }
1270                 }
1271                 gc_thread = NULL;
1272                 mono_gc_base_cleanup ();
1273         }
1274
1275         mono_reference_queue_cleanup ();
1276
1277         mono_mutex_destroy (&allocator_section);
1278         mono_mutex_destroy (&finalizer_mutex);
1279         mono_mutex_destroy (&reference_queue_mutex);
1280 }
1281
1282 /**
1283  * mono_gc_mutex_cleanup:
1284  *
1285  * Destroy the mutexes that may still be used after the main cleanup routine.
1286  */
1287 void
1288 mono_gc_mutex_cleanup (void)
1289 {
1290         mono_mutex_destroy (&handle_section);
1291 }
1292
1293 gboolean
1294 mono_gc_is_finalizer_internal_thread (MonoInternalThread *thread)
1295 {
1296         return thread == gc_thread;
1297 }
1298
1299 /**
1300  * mono_gc_is_finalizer_thread:
1301  * @thread: the thread to test.
1302  *
1303  * In Mono objects are finalized asynchronously on a separate thread.
1304  * This routine tests whether the @thread argument represents the
1305  * finalization thread.
1306  * 
1307  * Returns true if @thread is the finalization thread.
1308  */
1309 gboolean
1310 mono_gc_is_finalizer_thread (MonoThread *thread)
1311 {
1312         return mono_gc_is_finalizer_internal_thread (thread->internal_thread);
1313 }
1314
1315 #if defined(__MACH__)
1316 static pthread_t mach_exception_thread;
1317
1318 void
1319 mono_gc_register_mach_exception_thread (pthread_t thread)
1320 {
1321         mach_exception_thread = thread;
1322 }
1323
1324 pthread_t
1325 mono_gc_get_mach_exception_thread (void)
1326 {
1327         return mach_exception_thread;
1328 }
1329 #endif
1330
1331 #ifndef HAVE_SGEN_GC
1332 void*
1333 mono_gc_alloc_mature (MonoVTable *vtable)
1334 {
1335         return mono_object_new_specific (vtable);
1336 }
1337 #endif
1338
1339
1340 static MonoReferenceQueue *ref_queues;
1341
1342 static void
1343 ref_list_remove_element (RefQueueEntry **prev, RefQueueEntry *element)
1344 {
1345         do {
1346                 /* Guard if head is changed concurrently. */
1347                 while (*prev != element)
1348                         prev = &(*prev)->next;
1349         } while (prev && InterlockedCompareExchangePointer ((void*)prev, element->next, element) != element);
1350 }
1351
1352 static void
1353 ref_list_push (RefQueueEntry **head, RefQueueEntry *value)
1354 {
1355         RefQueueEntry *current;
1356         do {
1357                 current = *head;
1358                 value->next = current;
1359                 STORE_STORE_FENCE; /*Must make sure the previous store is visible before the CAS. */
1360         } while (InterlockedCompareExchangePointer ((void*)head, value, current) != current);
1361 }
1362
1363 static void
1364 reference_queue_proccess (MonoReferenceQueue *queue)
1365 {
1366         RefQueueEntry **iter = &queue->queue;
1367         RefQueueEntry *entry;
1368         while ((entry = *iter)) {
1369 #ifdef HAVE_SGEN_GC
1370                 if (queue->should_be_deleted || !mono_gc_weak_link_get (&entry->dis_link)) {
1371                         mono_gc_weak_link_remove (&entry->dis_link, TRUE);
1372 #else
1373                 if (queue->should_be_deleted || !mono_gchandle_get_target (entry->gchandle)) {
1374                         mono_gchandle_free ((guint32)entry->gchandle);
1375 #endif
1376                         ref_list_remove_element (iter, entry);
1377                         queue->callback (entry->user_data);
1378                         g_free (entry);
1379                 } else {
1380                         iter = &entry->next;
1381                 }
1382         }
1383 }
1384
1385 static void
1386 reference_queue_proccess_all (void)
1387 {
1388         MonoReferenceQueue **iter;
1389         MonoReferenceQueue *queue = ref_queues;
1390         for (; queue; queue = queue->next)
1391                 reference_queue_proccess (queue);
1392
1393 restart:
1394         mono_mutex_lock (&reference_queue_mutex);
1395         for (iter = &ref_queues; *iter;) {
1396                 queue = *iter;
1397                 if (!queue->should_be_deleted) {
1398                         iter = &queue->next;
1399                         continue;
1400                 }
1401                 if (queue->queue) {
1402                         mono_mutex_unlock (&reference_queue_mutex);
1403                         reference_queue_proccess (queue);
1404                         goto restart;
1405                 }
1406                 *iter = queue->next;
1407                 g_free (queue);
1408         }
1409         mono_mutex_unlock (&reference_queue_mutex);
1410 }
1411
1412 static void
1413 mono_reference_queue_cleanup (void)
1414 {
1415         MonoReferenceQueue *queue = ref_queues;
1416         for (; queue; queue = queue->next)
1417                 queue->should_be_deleted = TRUE;
1418         reference_queue_proccess_all ();
1419 }
1420
1421 static void
1422 reference_queue_clear_for_domain (MonoDomain *domain)
1423 {
1424         MonoReferenceQueue *queue = ref_queues;
1425         for (; queue; queue = queue->next) {
1426                 RefQueueEntry **iter = &queue->queue;
1427                 RefQueueEntry *entry;
1428                 while ((entry = *iter)) {
1429                         if (entry->domain == domain) {
1430 #ifdef HAVE_SGEN_GC
1431                                 mono_gc_weak_link_remove (&entry->dis_link, TRUE);
1432 #else
1433                                 mono_gchandle_free ((guint32)entry->gchandle);
1434 #endif
1435                                 ref_list_remove_element (iter, entry);
1436                                 queue->callback (entry->user_data);
1437                                 g_free (entry);
1438                         } else {
1439                                 iter = &entry->next;
1440                         }
1441                 }
1442         }
1443 }
1444 /**
1445  * mono_gc_reference_queue_new:
1446  * @callback callback used when processing collected entries.
1447  *
1448  * Create a new reference queue used to process collected objects.
1449  * A reference queue let you add a pair of (managed object, user data)
1450  * using the mono_gc_reference_queue_add method.
1451  *
1452  * Once the managed object is collected @callback will be called
1453  * in the finalizer thread with 'user data' as argument.
1454  *
1455  * The callback is called from the finalizer thread without any locks held.
1456  * When a AppDomain is unloaded, all callbacks for objects belonging to it
1457  * will be invoked.
1458  *
1459  * @returns the new queue.
1460  */
1461 MonoReferenceQueue*
1462 mono_gc_reference_queue_new (mono_reference_queue_callback callback)
1463 {
1464         MonoReferenceQueue *res = g_new0 (MonoReferenceQueue, 1);
1465         res->callback = callback;
1466
1467         mono_mutex_lock (&reference_queue_mutex);
1468         res->next = ref_queues;
1469         ref_queues = res;
1470         mono_mutex_unlock (&reference_queue_mutex);
1471
1472         return res;
1473 }
1474
1475 /**
1476  * mono_gc_reference_queue_add:
1477  * @queue the queue to add the reference to.
1478  * @obj the object to be watched for collection
1479  * @user_data parameter to be passed to the queue callback
1480  *
1481  * Queue an object to be watched for collection, when the @obj is
1482  * collected, the callback that was registered for the @queue will
1483  * be invoked with @user_data as argument.
1484  *
1485  * @returns false if the queue is scheduled to be freed.
1486  */
1487 gboolean
1488 mono_gc_reference_queue_add (MonoReferenceQueue *queue, MonoObject *obj, void *user_data)
1489 {
1490         RefQueueEntry *entry;
1491         if (queue->should_be_deleted)
1492                 return FALSE;
1493
1494         entry = g_new0 (RefQueueEntry, 1);
1495         entry->user_data = user_data;
1496         entry->domain = mono_object_domain (obj);
1497
1498 #ifdef HAVE_SGEN_GC
1499         mono_gc_weak_link_add (&entry->dis_link, obj, TRUE);
1500 #else
1501         entry->gchandle = mono_gchandle_new_weakref (obj, TRUE);
1502         mono_object_register_finalizer (obj);
1503 #endif
1504
1505         ref_list_push (&queue->queue, entry);
1506         return TRUE;
1507 }
1508
1509 /**
1510  * mono_gc_reference_queue_free:
1511  * @queue the queue that should be freed.
1512  *
1513  * This operation signals that @queue should be freed. This operation is deferred
1514  * as it happens on the finalizer thread.
1515  *
1516  * After this call, no further objects can be queued. It's the responsibility of the
1517  * caller to make sure that no further attempt to access queue will be made.
1518  */
1519 void
1520 mono_gc_reference_queue_free (MonoReferenceQueue *queue)
1521 {
1522         queue->should_be_deleted = TRUE;
1523 }