2 * sgen-fin-weak-hash.c: Finalizers and weak links.
5 * Paolo Molaro (lupus@ximian.com)
6 * Rodrigo Kumpera (kumpera@gmail.com)
8 * Copyright 2005-2011 Novell, Inc (http://www.novell.com)
9 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
10 * Copyright 2011 Xamarin, Inc.
11 * Copyright (C) 2012 Xamarin Inc
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License 2.0 as published by the Free Software Foundation;
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License 2.0 along with this library; if not, write to the Free
24 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include "mono/sgen/sgen-gc.h"
31 #include "mono/sgen/sgen-gray.h"
32 #include "mono/sgen/sgen-protocol.h"
33 #include "mono/sgen/sgen-pointer-queue.h"
34 #include "mono/sgen/sgen-client.h"
35 #include "mono/sgen/gc-internal-agnostic.h"
36 #include "mono/utils/mono-membar.h"
38 #define ptr_in_nursery sgen_ptr_in_nursery
40 typedef SgenGrayQueue GrayQueue;
42 static int no_finalize = 0;
45 * The finalizable hash has the object as the key, the
46 * disappearing_link hash, has the link address as key.
48 * Copyright 2011 Xamarin Inc.
51 #define TAG_MASK ((mword)0x1)
53 static inline GCObject*
54 tagged_object_get_object (GCObject *object)
56 return (GCObject*)(((mword)object) & ~TAG_MASK);
60 tagged_object_get_tag (GCObject *object)
62 return ((mword)object) & TAG_MASK;
65 static inline GCObject*
66 tagged_object_apply (void *object, int tag_bits)
68 return (GCObject*)((mword)object | (mword)tag_bits);
72 tagged_object_hash (GCObject *o)
74 return sgen_aligned_addr_hash (tagged_object_get_object (o));
78 tagged_object_equals (GCObject *a, GCObject *b)
80 return tagged_object_get_object (a) == tagged_object_get_object (b);
83 static SgenHashTable minor_finalizable_hash = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_FIN_TABLE, INTERNAL_MEM_FINALIZE_ENTRY, 0, (GHashFunc)tagged_object_hash, (GEqualFunc)tagged_object_equals);
84 static SgenHashTable major_finalizable_hash = SGEN_HASH_TABLE_INIT (INTERNAL_MEM_FIN_TABLE, INTERNAL_MEM_FINALIZE_ENTRY, 0, (GHashFunc)tagged_object_hash, (GEqualFunc)tagged_object_equals);
87 get_finalize_entry_hash_table (int generation)
90 case GENERATION_NURSERY: return &minor_finalizable_hash;
91 case GENERATION_OLD: return &major_finalizable_hash;
92 default: g_assert_not_reached ();
96 #define BRIDGE_OBJECT_MARKED 0x1
98 /* LOCKING: requires that the GC lock is held */
100 sgen_mark_bridge_object (GCObject *obj)
102 SgenHashTable *hash_table = get_finalize_entry_hash_table (ptr_in_nursery (obj) ? GENERATION_NURSERY : GENERATION_OLD);
104 sgen_hash_table_set_key (hash_table, obj, tagged_object_apply (obj, BRIDGE_OBJECT_MARKED));
107 /* LOCKING: requires that the GC lock is held */
109 sgen_collect_bridge_objects (int generation, ScanCopyContext ctx)
111 CopyOrMarkObjectFunc copy_func = ctx.ops->copy_or_mark_object;
112 GrayQueue *queue = ctx.queue;
113 SgenHashTable *hash_table = get_finalize_entry_hash_table (generation);
115 gpointer dummy G_GNUC_UNUSED;
117 SgenPointerQueue moved_fin_objects;
119 sgen_pointer_queue_init (&moved_fin_objects, INTERNAL_MEM_TEMPORARY);
124 SGEN_HASH_TABLE_FOREACH (hash_table, GCObject *, object, gpointer, dummy) {
125 int tag = tagged_object_get_tag (object);
126 object = tagged_object_get_object (object);
128 /* Bridge code told us to ignore this one */
129 if (tag == BRIDGE_OBJECT_MARKED)
132 /* Object is a bridge object and major heap says it's dead */
133 if (major_collector.is_object_live (object))
136 /* Nursery says the object is dead. */
137 if (!sgen_gc_is_object_ready_for_finalization (object))
140 if (!sgen_client_bridge_is_bridge_object (object))
144 copy_func (©, queue);
146 sgen_client_bridge_register_finalized_object (copy);
148 if (hash_table == &minor_finalizable_hash && !ptr_in_nursery (copy)) {
149 /* remove from the list */
150 SGEN_HASH_TABLE_FOREACH_REMOVE (TRUE);
152 /* insert it into the major hash */
153 sgen_hash_table_replace (&major_finalizable_hash, tagged_object_apply (copy, tag), NULL, NULL);
155 SGEN_LOG (5, "Promoting finalization of object %p (%s) (was at %p) to major table", copy, sgen_client_vtable_get_name (SGEN_LOAD_VTABLE (copy)), object);
158 } else if (copy != object) {
160 SGEN_HASH_TABLE_FOREACH_REMOVE (TRUE);
162 /* register for reinsertion */
163 sgen_pointer_queue_add (&moved_fin_objects, tagged_object_apply (copy, tag));
165 SGEN_LOG (5, "Updating object for finalization: %p (%s) (was at %p)", copy, sgen_client_vtable_get_name (SGEN_LOAD_VTABLE (copy)), object);
169 } SGEN_HASH_TABLE_FOREACH_END;
171 while (!sgen_pointer_queue_is_empty (&moved_fin_objects)) {
172 sgen_hash_table_replace (hash_table, sgen_pointer_queue_pop (&moved_fin_objects), NULL, NULL);
175 sgen_pointer_queue_free (&moved_fin_objects);
179 /* LOCKING: requires that the GC lock is held */
181 sgen_finalize_in_range (int generation, ScanCopyContext ctx)
183 CopyOrMarkObjectFunc copy_func = ctx.ops->copy_or_mark_object;
184 GrayQueue *queue = ctx.queue;
185 SgenHashTable *hash_table = get_finalize_entry_hash_table (generation);
187 gpointer dummy G_GNUC_UNUSED;
188 SgenPointerQueue moved_fin_objects;
190 sgen_pointer_queue_init (&moved_fin_objects, INTERNAL_MEM_TEMPORARY);
194 SGEN_HASH_TABLE_FOREACH (hash_table, GCObject *, object, gpointer, dummy) {
195 int tag = tagged_object_get_tag (object);
196 object = tagged_object_get_object (object);
197 if (!major_collector.is_object_live (object)) {
198 gboolean is_fin_ready = sgen_gc_is_object_ready_for_finalization (object);
199 GCObject *copy = object;
200 copy_func (©, queue);
202 /* remove and put in fin_ready_list */
203 SGEN_HASH_TABLE_FOREACH_REMOVE (TRUE);
204 sgen_queue_finalization_entry (copy);
205 /* Make it survive */
206 SGEN_LOG (5, "Queueing object for finalization: %p (%s) (was at %p) (%d)", copy, sgen_client_vtable_get_name (SGEN_LOAD_VTABLE (copy)), object, sgen_hash_table_num_entries (hash_table));
209 if (hash_table == &minor_finalizable_hash && !ptr_in_nursery (copy)) {
210 /* remove from the list */
211 SGEN_HASH_TABLE_FOREACH_REMOVE (TRUE);
213 /* insert it into the major hash */
214 sgen_hash_table_replace (&major_finalizable_hash, tagged_object_apply (copy, tag), NULL, NULL);
216 SGEN_LOG (5, "Promoting finalization of object %p (%s) (was at %p) to major table", copy, sgen_client_vtable_get_name (SGEN_LOAD_VTABLE (copy)), object);
219 } else if (copy != object) {
221 SGEN_HASH_TABLE_FOREACH_REMOVE (TRUE);
223 /* register for reinsertion */
224 sgen_pointer_queue_add (&moved_fin_objects, tagged_object_apply (copy, tag));
226 SGEN_LOG (5, "Updating object for finalization: %p (%s) (was at %p)", copy, sgen_client_vtable_get_name (SGEN_LOAD_VTABLE (copy)), object);
232 } SGEN_HASH_TABLE_FOREACH_END;
234 while (!sgen_pointer_queue_is_empty (&moved_fin_objects)) {
235 sgen_hash_table_replace (hash_table, sgen_pointer_queue_pop (&moved_fin_objects), NULL, NULL);
238 sgen_pointer_queue_free (&moved_fin_objects);
241 /* LOCKING: requires that the GC lock is held */
243 register_for_finalization (GCObject *obj, void *user_data, int generation)
245 SgenHashTable *hash_table = get_finalize_entry_hash_table (generation);
251 if (sgen_hash_table_replace (hash_table, obj, NULL, NULL)) {
252 GCVTable vt = SGEN_LOAD_VTABLE_UNCHECKED (obj);
253 SGEN_LOG (5, "Added finalizer for object: %p (%s) (%d) to %s table", obj, sgen_client_vtable_get_name (vt), hash_table->num_entries, sgen_generation_name (generation));
256 if (sgen_hash_table_remove (hash_table, obj, NULL)) {
257 GCVTable vt = SGEN_LOAD_VTABLE_UNCHECKED (obj);
258 SGEN_LOG (5, "Removed finalizer for object: %p (%s) (%d)", obj, sgen_client_vtable_get_name (vt), hash_table->num_entries);
264 * We're using (mostly) non-locking staging queues for finalizers and weak links to speed
265 * up registering them. Otherwise we'd have to take the GC lock.
267 * The queues are arrays of `StageEntry`, plus a `next_entry` index. Threads add entries to
268 * the queue via `add_stage_entry()` in a linear fashion until it fills up, in which case
269 * `process_stage_entries()` is called to drain it. A garbage collection will also drain
270 * the queues via the same function. That implies that `add_stage_entry()`, since it
271 * doesn't take a lock, must be able to run concurrently with `process_stage_entries()`,
272 * though it doesn't have to make progress while the queue is drained. In fact, once it
273 * detects that the queue is being drained, it blocks until the draining is done.
275 * The protocol must guarantee that entries in the queue are causally ordered, otherwise two
276 * entries for the same location might get switched, resulting in the earlier one being
277 * committed and the later one ignored.
279 * `next_entry` is the index of the next entry to be filled, or `-1` if the queue is
280 * currently being drained. Each entry has a state:
282 * `STAGE_ENTRY_FREE`: The entry is free. Its data fields must be `NULL`.
284 * `STAGE_ENTRY_BUSY`: The entry is currently being filled in.
286 * `STAGE_ENTRY_USED`: The entry is completely filled in and must be processed in the next
289 * `STAGE_ENTRY_INVALID`: The entry was busy during queue draining and therefore
290 * invalidated. Entries that are `BUSY` can obviously not be processed during a drain, but
291 * we can't leave them in place because new entries might be inserted before them, including
292 * from the same thread, violating causality. An alternative would be not to reset
293 * `next_entry` to `0` after a drain, but to the index of the last `BUSY` entry plus one,
294 * but that can potentially waste the whole queue.
298 * | from | to | filler? | drainer? |
299 * +---------+---------+---------+----------+
300 * | FREE | BUSY | X | |
301 * | BUSY | FREE | X | |
302 * | BUSY | USED | X | |
303 * | BUSY | INVALID | | X |
304 * | USED | FREE | | X |
305 * | INVALID | FREE | X | |
307 * `next_entry` can be incremented either by the filler thread that set the corresponding
308 * entry to `BUSY`, or by another filler thread that's trying to get a `FREE` slot. If that
309 * other thread wasn't allowed to increment, it would block on the first filler thread.
311 * An entry's state, once it's set from `FREE` to `BUSY` by a filler thread, can only be
312 * changed by that same thread or by the drained. The drainer can only set a `BUSY` thread
313 * to `INVALID`, so it needs to be set to `FREE` again by the original filler thread.
316 #define STAGE_ENTRY_FREE 0
317 #define STAGE_ENTRY_BUSY 1
318 #define STAGE_ENTRY_USED 2
319 #define STAGE_ENTRY_INVALID 3
322 volatile gint32 state;
327 #define NUM_FIN_STAGE_ENTRIES 1024
329 static volatile gint32 next_fin_stage_entry = 0;
330 static StageEntry fin_stage_entries [NUM_FIN_STAGE_ENTRIES];
333 * This is used to lock the stage when processing is forced, i.e. when it's triggered by a
334 * garbage collection. In that case, the world is already stopped and there's only one
335 * thread operating on the queue.
338 lock_stage_for_processing (volatile gint32 *next_entry)
344 * When processing is triggered by an overflow, we don't want to take the GC lock
345 * immediately, and then set `next_index` to `-1`, because another thread might have drained
346 * the queue in the mean time. Instead, we make sure the overflow is still there, we
347 * atomically set `next_index`, and only once that happened do we take the GC lock.
350 try_lock_stage_for_processing (int num_entries, volatile gint32 *next_entry)
352 gint32 old = *next_entry;
353 if (old < num_entries)
355 return InterlockedCompareExchange (next_entry, -1, old) == old;
358 /* LOCKING: requires that the GC lock is held */
360 process_stage_entries (int num_entries, volatile gint32 *next_entry, StageEntry *entries, void (*process_func) (GCObject*, void*, int))
365 * This can happen if after setting `next_index` to `-1` in
366 * `try_lock_stage_for_processing()`, a GC was triggered, which then drained the
367 * queue and reset `next_entry`.
369 * We have the GC lock now, so if it's still `-1`, we can't be interrupted by a GC.
371 if (*next_entry != -1)
374 for (i = 0; i < num_entries; ++i) {
378 state = entries [i].state;
381 case STAGE_ENTRY_FREE:
382 case STAGE_ENTRY_INVALID:
384 case STAGE_ENTRY_BUSY:
385 /* BUSY -> INVALID */
387 * This must be done atomically, because the filler thread can set
388 * the entry to `USED`, in which case we must process it, so we must
389 * detect that eventuality.
391 if (InterlockedCompareExchange (&entries [i].state, STAGE_ENTRY_INVALID, STAGE_ENTRY_BUSY) != STAGE_ENTRY_BUSY)
394 case STAGE_ENTRY_USED:
397 SGEN_ASSERT (0, FALSE, "Invalid stage entry state");
403 process_func (entries [i].obj, entries [i].user_data, i);
405 entries [i].obj = NULL;
406 entries [i].user_data = NULL;
408 mono_memory_write_barrier ();
412 * This transition only happens here, so we don't have to do it atomically.
414 entries [i].state = STAGE_ENTRY_FREE;
417 mono_memory_write_barrier ();
422 #ifdef HEAVY_STATISTICS
423 static guint64 stat_overflow_abort = 0;
424 static guint64 stat_wait_for_processing = 0;
425 static guint64 stat_increment_other_thread = 0;
426 static guint64 stat_index_decremented = 0;
427 static guint64 stat_entry_invalidated = 0;
428 static guint64 stat_success = 0;
432 add_stage_entry (int num_entries, volatile gint32 *next_entry, StageEntry *entries, GCObject *obj, void *user_data)
434 gint32 index, new_next_entry, old_next_entry;
435 gint32 previous_state;
440 if (index >= num_entries) {
441 HEAVY_STAT (++stat_overflow_abort);
446 * Backed-off waiting is way more efficient than even using a
447 * dedicated lock for this.
449 while ((index = *next_entry) < 0) {
451 * This seems like a good value. Determined by timing
452 * sgen-weakref-stress.exe.
454 mono_thread_info_usleep (200);
455 HEAVY_STAT (++stat_wait_for_processing);
460 if (entries [index].state != STAGE_ENTRY_FREE ||
461 InterlockedCompareExchange (&entries [index].state, STAGE_ENTRY_BUSY, STAGE_ENTRY_FREE) != STAGE_ENTRY_FREE) {
463 * If we can't get the entry it must be because another thread got
464 * it first. We don't want to wait for that thread to increment
465 * `next_entry`, so we try to do it ourselves. Whether we succeed
466 * or not, we start over.
468 if (*next_entry == index) {
469 InterlockedCompareExchange (next_entry, index + 1, index);
470 //g_print ("tried increment for other thread\n");
471 HEAVY_STAT (++stat_increment_other_thread);
475 /* state is BUSY now */
476 mono_memory_write_barrier ();
478 * Incrementing `next_entry` must happen after setting the state to `BUSY`.
479 * If it were the other way around, it would be possible that after a filler
480 * incremented the index, other threads fill up the queue, the queue is
481 * drained, the original filler finally fills in the slot, but `next_entry`
482 * ends up at the start of the queue, and new entries are written in the
483 * queue in front of, not behind, the original filler's entry.
485 * We don't actually require that the CAS succeeds, but we do require that
486 * the value of `next_entry` is not lower than our index. Since the drainer
487 * sets it to `-1`, that also takes care of the case that the drainer is
490 old_next_entry = InterlockedCompareExchange (next_entry, index + 1, index);
491 if (old_next_entry < index) {
493 /* INVALID -> FREE */
495 * The state might still be `BUSY`, or the drainer could have set it
496 * to `INVALID`. In either case, there's no point in CASing. Set
497 * it to `FREE` and start over.
499 entries [index].state = STAGE_ENTRY_FREE;
500 HEAVY_STAT (++stat_index_decremented);
506 SGEN_ASSERT (0, index >= 0 && index < num_entries, "Invalid index");
508 entries [index].obj = obj;
509 entries [index].user_data = user_data;
511 mono_memory_write_barrier ();
513 new_next_entry = *next_entry;
514 mono_memory_read_barrier ();
517 * A `BUSY` entry will either still be `BUSY` or the drainer will have set it to
518 * `INVALID`. In the former case, we set it to `USED` and we're finished. In the
519 * latter case, we reset it to `FREE` and start over.
521 previous_state = InterlockedCompareExchange (&entries [index].state, STAGE_ENTRY_USED, STAGE_ENTRY_BUSY);
522 if (previous_state == STAGE_ENTRY_BUSY) {
523 SGEN_ASSERT (0, new_next_entry >= index || new_next_entry < 0, "Invalid next entry index - as long as we're busy, other thread can only increment or invalidate it");
524 HEAVY_STAT (++stat_success);
528 SGEN_ASSERT (0, previous_state == STAGE_ENTRY_INVALID, "Invalid state transition - other thread can only make busy state invalid");
529 entries [index].obj = NULL;
530 entries [index].user_data = NULL;
531 mono_memory_write_barrier ();
532 /* INVALID -> FREE */
533 entries [index].state = STAGE_ENTRY_FREE;
535 HEAVY_STAT (++stat_entry_invalidated);
540 /* LOCKING: requires that the GC lock is held */
542 process_fin_stage_entry (GCObject *obj, void *user_data, int index)
544 if (ptr_in_nursery (obj))
545 register_for_finalization (obj, user_data, GENERATION_NURSERY);
547 register_for_finalization (obj, user_data, GENERATION_OLD);
550 /* LOCKING: requires that the GC lock is held */
552 sgen_process_fin_stage_entries (void)
554 lock_stage_for_processing (&next_fin_stage_entry);
555 process_stage_entries (NUM_FIN_STAGE_ENTRIES, &next_fin_stage_entry, fin_stage_entries, process_fin_stage_entry);
559 sgen_object_register_for_finalization (GCObject *obj, void *user_data)
561 while (add_stage_entry (NUM_FIN_STAGE_ENTRIES, &next_fin_stage_entry, fin_stage_entries, obj, user_data) == -1) {
562 if (try_lock_stage_for_processing (NUM_FIN_STAGE_ENTRIES, &next_fin_stage_entry)) {
564 process_stage_entries (NUM_FIN_STAGE_ENTRIES, &next_fin_stage_entry, fin_stage_entries, process_fin_stage_entry);
570 /* LOCKING: requires that the GC lock is held */
572 finalizers_with_predicate (SgenObjectPredicateFunc predicate, void *user_data, GCObject **out_array, int out_size, SgenHashTable *hash_table)
575 gpointer dummy G_GNUC_UNUSED;
578 if (no_finalize || !out_size || !out_array)
581 SGEN_HASH_TABLE_FOREACH (hash_table, GCObject *, object, gpointer, dummy) {
582 object = tagged_object_get_object (object);
584 if (predicate (object, user_data)) {
585 /* remove and put in out_array */
586 SGEN_HASH_TABLE_FOREACH_REMOVE (TRUE);
587 out_array [count ++] = object;
588 SGEN_LOG (5, "Collecting object for finalization: %p (%s) (%d)", object, sgen_client_vtable_get_name (SGEN_LOAD_VTABLE (object)), sgen_hash_table_num_entries (hash_table));
589 if (count == out_size)
593 } SGEN_HASH_TABLE_FOREACH_END;
598 * sgen_gather_finalizers_if:
599 * @predicate: predicate function
600 * @user_data: predicate function data argument
601 * @out_array: output array
602 * @out_size: size of output array
604 * Store inside @out_array up to @out_size objects that match @predicate. Returns the number
605 * of stored items. Can be called repeteadly until it returns 0.
607 * The items are removed from the finalizer data structure, so the caller is supposed
610 * @out_array me be on the stack, or registered as a root, to allow the GC to know the
611 * objects are still alive.
614 sgen_gather_finalizers_if (SgenObjectPredicateFunc predicate, void *user_data, GCObject **out_array, int out_size)
619 sgen_process_fin_stage_entries ();
620 result = finalizers_with_predicate (predicate, user_data, (GCObject**)out_array, out_size, &minor_finalizable_hash);
621 if (result < out_size) {
622 result += finalizers_with_predicate (predicate, user_data, (GCObject**)out_array + result, out_size - result,
623 &major_finalizable_hash);
631 sgen_remove_finalizers_if (SgenObjectPredicateFunc predicate, void *user_data, int generation)
633 SgenHashTable *hash_table = get_finalize_entry_hash_table (generation);
635 gpointer dummy G_GNUC_UNUSED;
637 SGEN_HASH_TABLE_FOREACH (hash_table, GCObject *, object, gpointer, dummy) {
638 object = tagged_object_get_object (object);
640 if (predicate (object, user_data)) {
641 SGEN_HASH_TABLE_FOREACH_REMOVE (TRUE);
644 } SGEN_HASH_TABLE_FOREACH_END;
648 sgen_init_fin_weak_hash (void)
650 #ifdef HEAVY_STATISTICS
651 mono_counters_register ("FinWeak Successes", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_success);
652 mono_counters_register ("FinWeak Overflow aborts", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_overflow_abort);
653 mono_counters_register ("FinWeak Wait for processing", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_wait_for_processing);
654 mono_counters_register ("FinWeak Increment other thread", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_increment_other_thread);
655 mono_counters_register ("FinWeak Index decremented", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_index_decremented);
656 mono_counters_register ("FinWeak Entry invalidated", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_entry_invalidated);
660 #endif /* HAVE_SGEN_GC */