Merge pull request #2799 from BrzVlad/fix-conc-card-clean
[mono.git] / mono / sgen / sgen-gc.c
index 3314ffbb1266ea6276add6e053597e6859d7b994..62e1e88fe8e0f21b26e9dc398b3beb6ef89e2e3c 100644 (file)
@@ -239,18 +239,14 @@ static gboolean do_concurrent_checks = FALSE;
    each collection */
 static gboolean do_scan_starts_check = FALSE;
 
-/*
- * If the major collector is concurrent and this is FALSE, we will
- * never initiate a synchronous major collection, unless requested via
- * GC.Collect().
- */
-static gboolean allow_synchronous_major = TRUE;
 static gboolean disable_minor_collections = FALSE;
 static gboolean disable_major_collections = FALSE;
 static gboolean do_verify_nursery = FALSE;
 static gboolean do_dump_nursery_content = FALSE;
 static gboolean enable_nursery_canaries = FALSE;
 
+static gboolean precleaning_enabled = TRUE;
+
 #ifdef HEAVY_STATISTICS
 guint64 stat_objects_alloced_degraded = 0;
 guint64 stat_bytes_alloced_degraded = 0;
@@ -346,7 +342,7 @@ nursery_canaries_enabled (void)
  * ########  Global data.
  * ######################################################################
  */
-LOCK_DECLARE (gc_mutex);
+MonoCoopMutex gc_mutex;
 gboolean sgen_try_free_some_memory;
 
 #define SCAN_START_SIZE        SGEN_SCAN_START_SIZE
@@ -359,7 +355,7 @@ GCMemSection *nursery_section = NULL;
 static volatile mword lowest_heap_address = ~(mword)0;
 static volatile mword highest_heap_address = 0;
 
-LOCK_DECLARE (sgen_interruption_mutex);
+MonoCoopMutex sgen_interruption_mutex;
 
 int current_collection_generation = -1;
 static volatile gboolean concurrent_collection_in_progress = FALSE;
@@ -444,7 +440,7 @@ gray_queue_redirect (SgenGrayQueue *queue)
                GrayQueueSection *section = sgen_gray_object_dequeue_section (queue);
                if (!section)
                        break;
-               sgen_section_gray_queue_enqueue (queue->alloc_prepare_data, section);
+               sgen_section_gray_queue_enqueue ((SgenSectionGrayQueue *)queue->alloc_prepare_data, section);
                wake = TRUE;
        }
 
@@ -477,7 +473,7 @@ sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc
                }
 
                if (allow_flags) {
-                       if (!(obj = SGEN_OBJECT_IS_FORWARDED (start)))
+                       if (!(obj = (char *)SGEN_OBJECT_IS_FORWARDED (start)))
                                obj = start;
                } else {
                        obj = start;
@@ -506,7 +502,7 @@ sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc
  * lock must be held.  For serial collectors that is not necessary.
  */
 void
-sgen_add_to_global_remset (gpointer ptr, gpointer obj)
+sgen_add_to_global_remset (gpointer ptr, GCObject *obj)
 {
        SGEN_ASSERT (5, sgen_ptr_in_nursery (obj), "Target pointer of global remset must be in the nursery");
 
@@ -539,30 +535,25 @@ sgen_add_to_global_remset (gpointer ptr, gpointer obj)
  * frequently after each object is copied, to achieve better locality and cache
  * usage.
  *
- * max_objs is the maximum number of objects to scan, or -1 to scan until the stack is
- * empty.
  */
 gboolean
-sgen_drain_gray_stack (int max_objs, ScanCopyContext ctx)
+sgen_drain_gray_stack (ScanCopyContext ctx)
 {
        ScanObjectFunc scan_func = ctx.ops->scan_object;
        GrayQueue *queue = ctx.queue;
 
-       if (current_collection_generation == GENERATION_OLD && major_collector.drain_gray_stack)
-               return major_collector.drain_gray_stack (ctx);
+       if (ctx.ops->drain_gray_stack)
+               return ctx.ops->drain_gray_stack (queue);
 
-       do {
-               int i;
-               for (i = 0; i != max_objs; ++i) {
-                       GCObject *obj;
-                       SgenDescriptor desc;
-                       GRAY_OBJECT_DEQUEUE (queue, &obj, &desc);
-                       if (!obj)
-                               return TRUE;
-                       SGEN_LOG (9, "Precise gray object scan %p (%s)", obj, sgen_client_vtable_get_name (SGEN_LOAD_VTABLE (obj)));
-                       scan_func (obj, desc, queue);
-               }
-       } while (max_objs < 0);
+       for (;;) {
+               GCObject *obj;
+               SgenDescriptor desc;
+               GRAY_OBJECT_DEQUEUE (queue, &obj, &desc);
+               if (!obj)
+                       return TRUE;
+               SGEN_LOG (9, "Precise gray object scan %p (%s)", obj, sgen_client_vtable_get_name (SGEN_LOAD_VTABLE (obj)));
+               scan_func (obj, desc, queue);
+       }
        return FALSE;
 }
 
@@ -822,6 +813,8 @@ sgen_conservatively_pin_objects_from (void **start, void **end, void *start_nurs
 {
        int count = 0;
 
+       SGEN_ASSERT (0, ((mword)start & (SIZEOF_VOID_P - 1)) == 0, "Why are we scanning for references in unaligned memory ?");
+
 #if defined(VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE) && !defined(_WIN64)
        VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE (start, (char*)end - (char*)start);
 #endif
@@ -871,7 +864,7 @@ pin_from_roots (void *start_nursery, void *end_nursery, ScanCopyContext ctx)
        RootRecord *root;
        SGEN_LOG (2, "Scanning pinned roots (%d bytes, %d/%d entries)", (int)roots_size, roots_hash [ROOT_TYPE_NORMAL].num_entries, roots_hash [ROOT_TYPE_PINNED].num_entries);
        /* objects pinned from the API are inside these roots */
-       SGEN_HASH_TABLE_FOREACH (&roots_hash [ROOT_TYPE_PINNED], start_root, root) {
+       SGEN_HASH_TABLE_FOREACH (&roots_hash [ROOT_TYPE_PINNED], void **, start_root, RootRecord *, root) {
                SGEN_LOG (6, "Pinned roots %p-%p", start_root, root->end_root);
                sgen_conservatively_pin_objects_from (start_root, (void**)root->end_root, start_nursery, end_nursery, PIN_TYPE_OTHER);
        } SGEN_HASH_TABLE_FOREACH_END;
@@ -885,24 +878,10 @@ pin_from_roots (void *start_nursery, void *end_nursery, ScanCopyContext ctx)
        sgen_client_scan_thread_data (start_nursery, end_nursery, FALSE, ctx);
 }
 
-static void
-unpin_objects_from_queue (SgenGrayQueue *queue)
-{
-       for (;;) {
-               GCObject *addr;
-               SgenDescriptor desc;
-               GRAY_OBJECT_DEQUEUE (queue, &addr, &desc);
-               if (!addr)
-                       break;
-               g_assert (SGEN_OBJECT_IS_PINNED (addr));
-               SGEN_UNPIN_OBJECT (addr);
-       }
-}
-
 static void
 single_arg_user_copy_or_mark (GCObject **obj, void *gc_data)
 {
-       ScanCopyContext *ctx = gc_data;
+       ScanCopyContext *ctx = (ScanCopyContext *)gc_data;
        ctx->ops->copy_or_mark_object (obj, ctx->queue);
 }
 
@@ -933,7 +912,7 @@ precisely_scan_objects_from (void** start_root, void** end_root, char* n_start,
                }
                return;
        case ROOT_DESC_COMPLEX: {
-               gsize *bitmap_data = sgen_get_complex_descriptor_bitmap (desc);
+               gsize *bitmap_data = (gsize *)sgen_get_complex_descriptor_bitmap (desc);
                gsize bwords = (*bitmap_data) - 1;
                void **start_run = start_root;
                bitmap_data++;
@@ -1009,21 +988,21 @@ alloc_nursery (void)
         * objects in the existing nursery.
         */
        /* FIXME: handle OOM */
-       section = sgen_alloc_internal (INTERNAL_MEM_SECTION);
+       section = (GCMemSection *)sgen_alloc_internal (INTERNAL_MEM_SECTION);
 
        alloc_size = sgen_nursery_size;
 
        /* If there isn't enough space even for the nursery we should simply abort. */
        g_assert (sgen_memgov_try_alloc_space (alloc_size, SPACE_NURSERY));
 
-       data = major_collector.alloc_heap (alloc_size, alloc_size, DEFAULT_NURSERY_BITS);
+       data = (char *)major_collector.alloc_heap (alloc_size, alloc_size, DEFAULT_NURSERY_BITS);
        sgen_update_heap_boundaries ((mword)data, (mword)(data + sgen_nursery_size));
        SGEN_LOG (4, "Expanding nursery size (%p-%p): %lu, total: %lu", data, data + alloc_size, (unsigned long)sgen_nursery_size, (unsigned long)sgen_gc_get_total_heap_allocation ());
        section->data = section->next_data = data;
        section->size = alloc_size;
        section->end_data = data + sgen_nursery_size;
        scan_starts = (alloc_size + SCAN_START_SIZE - 1) / SCAN_START_SIZE;
-       section->scan_starts = sgen_alloc_internal_dynamic (sizeof (char*) * scan_starts, INTERNAL_MEM_SCAN_STARTS, TRUE);
+       section->scan_starts = (char **)sgen_alloc_internal_dynamic (sizeof (char*) * scan_starts, INTERNAL_MEM_SCAN_STARTS, TRUE);
        section->num_scan_start = scan_starts;
 
        nursery_section = section;
@@ -1045,7 +1024,7 @@ scan_finalizer_entries (SgenPointerQueue *fin_queue, ScanCopyContext ctx)
        size_t i;
 
        for (i = 0; i < fin_queue->next_slot; ++i) {
-               GCObject *obj = fin_queue->data [i];
+               GCObject *obj = (GCObject *)fin_queue->data [i];
                if (!obj)
                        continue;
                SGEN_LOG (5, "Scan of fin ready object: %p (%s)\n", obj, sgen_client_vtable_get_name (SGEN_LOAD_VTABLE (obj)));
@@ -1079,6 +1058,7 @@ finish_gray_stack (int generation, ScanCopyContext ctx)
        char *end_addr = generation == GENERATION_NURSERY ? sgen_get_nursery_end () : (char*)-1;
        SgenGrayQueue *queue = ctx.queue;
 
+       binary_protocol_finish_gray_stack_start (sgen_timestamp (), generation);
        /*
         * We copied all the reachable objects. Now it's the time to copy
         * the objects that were not referenced by the roots, but by the copied objects.
@@ -1092,7 +1072,7 @@ finish_gray_stack (int generation, ScanCopyContext ctx)
         *   To achieve better cache locality and cache usage, we drain the gray stack 
         * frequently, after each object is copied, and just finish the work here.
         */
-       sgen_drain_gray_stack (-1, ctx);
+       sgen_drain_gray_stack (ctx);
        TV_GETTIME (atv);
        SGEN_LOG (2, "%s generation done", generation_name (generation));
 
@@ -1114,7 +1094,7 @@ finish_gray_stack (int generation, ScanCopyContext ctx)
        done_with_ephemerons = 0;
        do {
                done_with_ephemerons = sgen_client_mark_ephemerons (ctx);
-               sgen_drain_gray_stack (-1, ctx);
+               sgen_drain_gray_stack (ctx);
                ++ephemeron_rounds;
        } while (!done_with_ephemerons);
 
@@ -1122,7 +1102,7 @@ finish_gray_stack (int generation, ScanCopyContext ctx)
 
        if (sgen_client_bridge_need_processing ()) {
                /*Make sure the gray stack is empty before we process bridge objects so we get liveness right*/
-               sgen_drain_gray_stack (-1, ctx);
+               sgen_drain_gray_stack (ctx);
                sgen_collect_bridge_objects (generation, ctx);
                if (generation == GENERATION_OLD)
                        sgen_collect_bridge_objects (GENERATION_NURSERY, ctx);
@@ -1146,15 +1126,15 @@ finish_gray_stack (int generation, ScanCopyContext ctx)
        Make sure we drain the gray stack before processing disappearing links and finalizers.
        If we don't make sure it is empty we might wrongly see a live object as dead.
        */
-       sgen_drain_gray_stack (-1, ctx);
+       sgen_drain_gray_stack (ctx);
 
        /*
        We must clear weak links that don't track resurrection before processing object ready for
        finalization so they can be cleared before that.
        */
-       sgen_null_link_in_range (generation, TRUE, ctx);
+       sgen_null_link_in_range (generation, ctx, FALSE);
        if (generation == GENERATION_OLD)
-               sgen_null_link_in_range (GENERATION_NURSERY, TRUE, ctx);
+               sgen_null_link_in_range (GENERATION_NURSERY, ctx, FALSE);
 
 
        /* walk the finalization queue and move also the objects that need to be
@@ -1167,7 +1147,7 @@ finish_gray_stack (int generation, ScanCopyContext ctx)
                sgen_finalize_in_range (GENERATION_NURSERY, ctx);
        /* drain the new stack that might have been created */
        SGEN_LOG (6, "Precise scan of gray area post fin");
-       sgen_drain_gray_stack (-1, ctx);
+       sgen_drain_gray_stack (ctx);
 
        /*
         * This must be done again after processing finalizable objects since CWL slots are cleared only after the key is finalized.
@@ -1175,7 +1155,7 @@ finish_gray_stack (int generation, ScanCopyContext ctx)
        done_with_ephemerons = 0;
        do {
                done_with_ephemerons = sgen_client_mark_ephemerons (ctx);
-               sgen_drain_gray_stack (-1, ctx);
+               sgen_drain_gray_stack (ctx);
                ++ephemeron_rounds;
        } while (!done_with_ephemerons);
 
@@ -1189,7 +1169,7 @@ finish_gray_stack (int generation, ScanCopyContext ctx)
        sgen_client_clear_togglerefs (start_addr, end_addr, ctx);
 
        TV_GETTIME (btv);
-       SGEN_LOG (2, "Finalize queue handling scan for %s generation: %ld usecs %d ephemeron rounds", generation_name (generation), TV_ELAPSED (atv, btv), ephemeron_rounds);
+       SGEN_LOG (2, "Finalize queue handling scan for %s generation: %lld usecs %d ephemeron rounds", generation_name (generation), TV_ELAPSED (atv, btv), ephemeron_rounds);
 
        /*
         * handle disappearing links
@@ -1201,17 +1181,18 @@ finish_gray_stack (int generation, ScanCopyContext ctx)
         */
        g_assert (sgen_gray_object_queue_is_empty (queue));
        for (;;) {
-               sgen_null_link_in_range (generation, FALSE, ctx);
+               sgen_null_link_in_range (generation, ctx, TRUE);
                if (generation == GENERATION_OLD)
-                       sgen_null_link_in_range (GENERATION_NURSERY, FALSE, ctx);
+                       sgen_null_link_in_range (GENERATION_NURSERY, ctx, TRUE);
                if (sgen_gray_object_queue_is_empty (queue))
                        break;
-               sgen_drain_gray_stack (-1, ctx);
+               sgen_drain_gray_stack (ctx);
        }
 
        g_assert (sgen_gray_object_queue_is_empty (queue));
 
        sgen_gray_object_queue_trim_free_list (queue);
+       binary_protocol_finish_gray_stack_end (sgen_timestamp (), generation);
 }
 
 void
@@ -1240,7 +1221,7 @@ scan_from_registered_roots (char *addr_start, char *addr_end, int root_type, Sca
 {
        void **start_root;
        RootRecord *root;
-       SGEN_HASH_TABLE_FOREACH (&roots_hash [root_type], start_root, root) {
+       SGEN_HASH_TABLE_FOREACH (&roots_hash [root_type], void **, start_root, RootRecord *, root) {
                SGEN_LOG (6, "Precise root scan %p-%p (desc: %p)", start_root, root->end_root, (void*)root->root_desc);
                precisely_scan_objects_from (start_root, (void**)root->end_root, addr_start, addr_end, root->root_desc, ctx);
        } SGEN_HASH_TABLE_FOREACH_END;
@@ -1348,7 +1329,7 @@ typedef struct {
 static void
 job_remembered_set_scan (void *worker_data_untyped, SgenThreadPoolJob *job)
 {
-       WorkerData *worker_data = worker_data_untyped;
+       WorkerData *worker_data = (WorkerData *)worker_data_untyped;
        ScanJob *job_data = (ScanJob*)job;
        ScanCopyContext ctx = CONTEXT_FROM_OBJECT_OPERATIONS (job_data->ops, sgen_workers_get_job_gray_queue (worker_data));
        remset.scan_remsets (ctx);
@@ -1365,7 +1346,7 @@ typedef struct {
 static void
 job_scan_from_registered_roots (void *worker_data_untyped, SgenThreadPoolJob *job)
 {
-       WorkerData *worker_data = worker_data_untyped;
+       WorkerData *worker_data = (WorkerData *)worker_data_untyped;
        ScanFromRegisteredRootsJob *job_data = (ScanFromRegisteredRootsJob*)job;
        ScanCopyContext ctx = CONTEXT_FROM_OBJECT_OPERATIONS (job_data->ops, sgen_workers_get_job_gray_queue (worker_data));
 
@@ -1382,7 +1363,7 @@ typedef struct {
 static void
 job_scan_thread_data (void *worker_data_untyped, SgenThreadPoolJob *job)
 {
-       WorkerData *worker_data = worker_data_untyped;
+       WorkerData *worker_data = (WorkerData *)worker_data_untyped;
        ScanThreadDataJob *job_data = (ScanThreadDataJob*)job;
        ScanCopyContext ctx = CONTEXT_FROM_OBJECT_OPERATIONS (job_data->ops, sgen_workers_get_job_gray_queue (worker_data));
 
@@ -1398,7 +1379,7 @@ typedef struct {
 static void
 job_scan_finalizer_entries (void *worker_data_untyped, SgenThreadPoolJob *job)
 {
-       WorkerData *worker_data = worker_data_untyped;
+       WorkerData *worker_data = (WorkerData *)worker_data_untyped;
        ScanFinalizerEntriesJob *job_data = (ScanFinalizerEntriesJob*)job;
        ScanCopyContext ctx = CONTEXT_FROM_OBJECT_OPERATIONS (job_data->ops, sgen_workers_get_job_gray_queue (worker_data));
 
@@ -1408,35 +1389,48 @@ job_scan_finalizer_entries (void *worker_data_untyped, SgenThreadPoolJob *job)
 static void
 job_scan_major_mod_union_card_table (void *worker_data_untyped, SgenThreadPoolJob *job)
 {
-       WorkerData *worker_data = worker_data_untyped;
+       WorkerData *worker_data = (WorkerData *)worker_data_untyped;
        ScanJob *job_data = (ScanJob*)job;
        ScanCopyContext ctx = CONTEXT_FROM_OBJECT_OPERATIONS (job_data->ops, sgen_workers_get_job_gray_queue (worker_data));
 
        g_assert (concurrent_collection_in_progress);
-       major_collector.scan_card_table (TRUE, ctx);
+       major_collector.scan_card_table (CARDTABLE_SCAN_MOD_UNION, ctx);
 }
 
 static void
 job_scan_los_mod_union_card_table (void *worker_data_untyped, SgenThreadPoolJob *job)
 {
-       WorkerData *worker_data = worker_data_untyped;
+       WorkerData *worker_data = (WorkerData *)worker_data_untyped;
        ScanJob *job_data = (ScanJob*)job;
        ScanCopyContext ctx = CONTEXT_FROM_OBJECT_OPERATIONS (job_data->ops, sgen_workers_get_job_gray_queue (worker_data));
 
        g_assert (concurrent_collection_in_progress);
-       sgen_los_scan_card_table (TRUE, ctx);
+       sgen_los_scan_card_table (CARDTABLE_SCAN_MOD_UNION, ctx);
 }
 
 static void
-init_gray_queue (void)
+job_mod_union_preclean (void *worker_data_untyped, SgenThreadPoolJob *job)
 {
-       if (sgen_collection_is_concurrent ())
+       WorkerData *worker_data = (WorkerData *)worker_data_untyped;
+       ScanJob *job_data = (ScanJob*)job;
+       ScanCopyContext ctx = CONTEXT_FROM_OBJECT_OPERATIONS (job_data->ops, sgen_workers_get_job_gray_queue (worker_data));
+
+       g_assert (concurrent_collection_in_progress);
+
+       major_collector.scan_card_table (CARDTABLE_SCAN_MOD_UNION_PRECLEAN, ctx);
+       sgen_los_scan_card_table (CARDTABLE_SCAN_MOD_UNION_PRECLEAN, ctx);
+}
+
+static void
+init_gray_queue (gboolean use_workers)
+{
+       if (use_workers)
                sgen_workers_init_distribute_gray_queue ();
        sgen_gray_object_queue_init (&gray_queue, NULL);
 }
 
 static void
-enqueue_scan_from_roots_jobs (char *heap_start, char *heap_end, SgenObjectOperations *ops)
+enqueue_scan_from_roots_jobs (char *heap_start, char *heap_end, SgenObjectOperations *ops, gboolean enqueue)
 {
        ScanFromRegisteredRootsJob *scrrj;
        ScanThreadDataJob *stdj;
@@ -1449,33 +1443,33 @@ enqueue_scan_from_roots_jobs (char *heap_start, char *heap_end, SgenObjectOperat
        scrrj->heap_start = heap_start;
        scrrj->heap_end = heap_end;
        scrrj->root_type = ROOT_TYPE_NORMAL;
-       sgen_workers_enqueue_job (&scrrj->job);
+       sgen_workers_enqueue_job (&scrrj->job, enqueue);
 
        scrrj = (ScanFromRegisteredRootsJob*)sgen_thread_pool_job_alloc ("scan from registered roots wbarrier", job_scan_from_registered_roots, sizeof (ScanFromRegisteredRootsJob));
        scrrj->ops = ops;
        scrrj->heap_start = heap_start;
        scrrj->heap_end = heap_end;
        scrrj->root_type = ROOT_TYPE_WBARRIER;
-       sgen_workers_enqueue_job (&scrrj->job);
+       sgen_workers_enqueue_job (&scrrj->job, enqueue);
 
        /* Threads */
 
        stdj = (ScanThreadDataJob*)sgen_thread_pool_job_alloc ("scan thread data", job_scan_thread_data, sizeof (ScanThreadDataJob));
        stdj->heap_start = heap_start;
        stdj->heap_end = heap_end;
-       sgen_workers_enqueue_job (&stdj->job);
+       sgen_workers_enqueue_job (&stdj->job, enqueue);
 
        /* Scan the list of objects ready for finalization. */
 
        sfej = (ScanFinalizerEntriesJob*)sgen_thread_pool_job_alloc ("scan finalizer entries", job_scan_finalizer_entries, sizeof (ScanFinalizerEntriesJob));
        sfej->queue = &fin_ready_queue;
        sfej->ops = ops;
-       sgen_workers_enqueue_job (&sfej->job);
+       sgen_workers_enqueue_job (&sfej->job, enqueue);
 
        sfej = (ScanFinalizerEntriesJob*)sgen_thread_pool_job_alloc ("scan critical finalizer entries", job_scan_finalizer_entries, sizeof (ScanFinalizerEntriesJob));
        sfej->queue = &critical_fin_queue;
        sfej->ops = ops;
-       sgen_workers_enqueue_job (&sfej->job);
+       sgen_workers_enqueue_job (&sfej->job, enqueue);
 }
 
 /*
@@ -1539,7 +1533,7 @@ collect_nursery (SgenGrayQueue *unpin_queue, gboolean finish_up_concurrent_mark)
 
        sgen_memgov_minor_collection_start ();
 
-       init_gray_queue ();
+       init_gray_queue (FALSE);
 
        gc_stats.minor_gc_count ++;
 
@@ -1551,7 +1545,6 @@ collect_nursery (SgenGrayQueue *unpin_queue, gboolean finish_up_concurrent_mark)
                sgen_check_consistency ();
 
        sgen_process_fin_stage_entries ();
-       sgen_process_dislink_stage_entries ();
 
        /* pin from pinned handles */
        sgen_init_pinning ();
@@ -1568,34 +1561,27 @@ collect_nursery (SgenGrayQueue *unpin_queue, gboolean finish_up_concurrent_mark)
 
        TV_GETTIME (atv);
        time_minor_pinning += TV_ELAPSED (btv, atv);
-       SGEN_LOG (2, "Finding pinned pointers: %zd in %ld usecs", sgen_get_pinned_count (), TV_ELAPSED (btv, atv));
+       SGEN_LOG (2, "Finding pinned pointers: %zd in %lld usecs", sgen_get_pinned_count (), TV_ELAPSED (btv, atv));
        SGEN_LOG (4, "Start scan with %zd pinned objects", sgen_get_pinned_count ());
 
-       /*
-        * FIXME: When we finish a concurrent collection we do a nursery collection first,
-        * as part of which we scan the card table.  Then, later, we scan the mod union
-        * cardtable.  We should only have to do one.
-        */
        sj = (ScanJob*)sgen_thread_pool_job_alloc ("scan remset", job_remembered_set_scan, sizeof (ScanJob));
        sj->ops = object_ops;
-       sgen_workers_enqueue_job (&sj->job);
+       sgen_workers_enqueue_job (&sj->job, FALSE);
 
        /* we don't have complete write barrier yet, so we scan all the old generation sections */
        TV_GETTIME (btv);
        time_minor_scan_remsets += TV_ELAPSED (atv, btv);
-       SGEN_LOG (2, "Old generation scan: %ld usecs", TV_ELAPSED (atv, btv));
+       SGEN_LOG (2, "Old generation scan: %lld usecs", TV_ELAPSED (atv, btv));
 
        sgen_pin_stats_print_class_stats ();
 
-       sgen_drain_gray_stack (-1, ctx);
-
        /* FIXME: Why do we do this at this specific, seemingly random, point? */
        sgen_client_collecting_minor (&fin_ready_queue, &critical_fin_queue);
 
        TV_GETTIME (atv);
        time_minor_scan_pinned += TV_ELAPSED (btv, atv);
 
-       enqueue_scan_from_roots_jobs (sgen_get_nursery_start (), nursery_next, object_ops);
+       enqueue_scan_from_roots_jobs (sgen_get_nursery_start (), nursery_next, object_ops, FALSE);
 
        TV_GETTIME (btv);
        time_minor_scan_roots += TV_ELAPSED (atv, btv);
@@ -1626,7 +1612,7 @@ collect_nursery (SgenGrayQueue *unpin_queue, gboolean finish_up_concurrent_mark)
        sgen_client_binary_protocol_reclaim_end (GENERATION_NURSERY);
        TV_GETTIME (btv);
        time_minor_fragment_creation += TV_ELAPSED (atv, btv);
-       SGEN_LOG (2, "Fragment creation: %ld usecs, %lu bytes available", TV_ELAPSED (atv, btv), (unsigned long)fragment_total);
+       SGEN_LOG (2, "Fragment creation: %lld usecs, %lu bytes available", TV_ELAPSED (atv, btv), (unsigned long)fragment_total);
 
        if (consistency_check_at_minor_collection)
                sgen_check_major_refs ();
@@ -1671,23 +1657,6 @@ collect_nursery (SgenGrayQueue *unpin_queue, gboolean finish_up_concurrent_mark)
        return needs_major;
 }
 
-static void
-scan_nursery_objects_callback (GCObject *obj, size_t size, ScanCopyContext *ctx)
-{
-       /*
-        * This is called on all objects in the nursery, including pinned ones, so we need
-        * to use sgen_obj_get_descriptor_safe(), which masks out the vtable tag bits.
-        */
-       ctx->ops->scan_object (obj, sgen_obj_get_descriptor_safe (obj), ctx->queue);
-}
-
-static void
-scan_nursery_objects (ScanCopyContext ctx)
-{
-       sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data,
-                       (IterateObjectCallbackFunc)scan_nursery_objects_callback, (void*)&ctx, FALSE, TRUE);
-}
-
 typedef enum {
        COPY_OR_MARK_FROM_ROOTS_SERIAL,
        COPY_OR_MARK_FROM_ROOTS_START_CONCURRENT,
@@ -1695,7 +1664,7 @@ typedef enum {
 } CopyOrMarkFromRootsMode;
 
 static void
-major_copy_or_mark_from_roots (size_t *old_next_pin_slot, CopyOrMarkFromRootsMode mode, gboolean scan_whole_nursery, SgenObjectOperations *object_ops)
+major_copy_or_mark_from_roots (size_t *old_next_pin_slot, CopyOrMarkFromRootsMode mode, SgenObjectOperations *object_ops)
 {
        LOSObject *bigobj;
        TV_DECLARE (atv);
@@ -1710,10 +1679,7 @@ major_copy_or_mark_from_roots (size_t *old_next_pin_slot, CopyOrMarkFromRootsMod
 
        SGEN_ASSERT (0, !!concurrent == !!concurrent_collection_in_progress, "We've been called with the wrong mode.");
 
-       if (scan_whole_nursery)
-               SGEN_ASSERT (0, mode == COPY_OR_MARK_FROM_ROOTS_FINISH_CONCURRENT, "Scanning whole nursery only makes sense when we're finishing a concurrent collection.");
-
-       if (concurrent) {
+       if (mode == COPY_OR_MARK_FROM_ROOTS_START_CONCURRENT) {
                /*This cleans up unused fragments */
                sgen_nursery_allocator_prepare_for_pinning ();
 
@@ -1724,7 +1690,7 @@ major_copy_or_mark_from_roots (size_t *old_next_pin_slot, CopyOrMarkFromRootsMod
                sgen_nursery_alloc_prepare_for_major ();
        }
 
-       init_gray_queue ();
+       init_gray_queue (mode == COPY_OR_MARK_FROM_ROOTS_START_CONCURRENT);
 
        TV_GETTIME (atv);
 
@@ -1747,33 +1713,31 @@ major_copy_or_mark_from_roots (size_t *old_next_pin_slot, CopyOrMarkFromRootsMod
 
        sgen_client_pre_collection_checks ();
 
-       if (!concurrent) {
+       if (mode != COPY_OR_MARK_FROM_ROOTS_START_CONCURRENT) {
                /* Remsets are not useful for a major collection */
                remset.clear_cards ();
        }
 
        sgen_process_fin_stage_entries ();
-       sgen_process_dislink_stage_entries ();
 
        TV_GETTIME (atv);
        sgen_init_pinning ();
        SGEN_LOG (6, "Collecting pinned addresses");
        pin_from_roots ((void*)lowest_heap_address, (void*)highest_heap_address, ctx);
-
-       if (mode != COPY_OR_MARK_FROM_ROOTS_START_CONCURRENT) {
-               if (major_collector.is_concurrent) {
-                       /*
-                        * The concurrent major collector cannot evict
-                        * yet, so we need to pin cemented objects to
-                        * not break some asserts.
-                        *
-                        * FIXME: We could evict now!
-                        */
-                       sgen_pin_cemented_objects ();
-               }
+       if (mode == COPY_OR_MARK_FROM_ROOTS_FINISH_CONCURRENT) {
+               /* Pin cemented objects that were forced */
+               sgen_pin_cemented_objects ();
        }
-
        sgen_optimize_pin_queue ();
+       if (mode == COPY_OR_MARK_FROM_ROOTS_START_CONCURRENT) {
+               /*
+                * Cemented objects that are in the pinned list will be marked. When
+                * marking concurrently we won't mark mod-union cards for these objects.
+                * Instead they will remain cemented until the next major collection,
+                * when we will recheck if they are still pinned in the roots.
+                */
+               sgen_cement_force_pinned ();
+       }
 
        sgen_client_collecting_major_1 ();
 
@@ -1814,41 +1778,10 @@ major_copy_or_mark_from_roots (size_t *old_next_pin_slot, CopyOrMarkFromRootsMod
                        sgen_client_pinned_los_object (bigobj->data);
                }
        }
-       /* second pass for the sections */
 
-       /*
-        * Concurrent mark never follows references into the nursery.  In the start and
-        * finish pauses we must scan live nursery objects, though.
-        *
-        * In the finish pause we do this conservatively by scanning all nursery objects.
-        * Previously we would only scan pinned objects here.  We assumed that all objects
-        * that were pinned during the nursery collection immediately preceding this finish
-        * mark would be pinned again here.  Due to the way we get the stack end for the GC
-        * thread, however, that's not necessarily the case: we scan part of the stack used
-        * by the GC itself, which changes constantly, so pinning isn't entirely
-        * deterministic.
-        *
-        * The split nursery also complicates things because non-pinned objects can survive
-        * in the nursery.  That's why we need to do a full scan of the nursery for it, too.
-        *
-        * In the future we shouldn't do a preceding nursery collection at all and instead
-        * do the finish pause with promotion from the nursery.
-        *
-        * A further complication arises when we have late-pinned objects from the preceding
-        * nursery collection.  Those are the result of being out of memory when trying to
-        * evacuate objects.  They won't be found from the roots, so we just scan the whole
-        * nursery.
-        *
-        * Non-concurrent mark evacuates from the nursery, so it's
-        * sufficient to just scan pinned nursery objects.
-        */
-       if (scan_whole_nursery || mode == COPY_OR_MARK_FROM_ROOTS_FINISH_CONCURRENT || (concurrent && sgen_minor_collector.is_split)) {
-               scan_nursery_objects (ctx);
-       } else {
-               pin_objects_in_nursery (concurrent, ctx);
-               if (check_nursery_objects_pinned && !sgen_minor_collector.is_split)
-                       sgen_check_nursery_objects_pinned (mode != COPY_OR_MARK_FROM_ROOTS_START_CONCURRENT);
-       }
+       pin_objects_in_nursery (mode == COPY_OR_MARK_FROM_ROOTS_START_CONCURRENT, ctx);
+       if (check_nursery_objects_pinned && !sgen_minor_collector.is_split)
+               sgen_check_nursery_objects_pinned (mode != COPY_OR_MARK_FROM_ROOTS_START_CONCURRENT);
 
        major_collector.pin_objects (WORKERS_DISTRIBUTE_GRAY_QUEUE);
        if (old_next_pin_slot)
@@ -1856,21 +1789,21 @@ major_copy_or_mark_from_roots (size_t *old_next_pin_slot, CopyOrMarkFromRootsMod
 
        TV_GETTIME (btv);
        time_major_pinning += TV_ELAPSED (atv, btv);
-       SGEN_LOG (2, "Finding pinned pointers: %zd in %ld usecs", sgen_get_pinned_count (), TV_ELAPSED (atv, btv));
+       SGEN_LOG (2, "Finding pinned pointers: %zd in %lld usecs", sgen_get_pinned_count (), TV_ELAPSED (atv, btv));
        SGEN_LOG (4, "Start scan with %zd pinned objects", sgen_get_pinned_count ());
 
        major_collector.init_to_space ();
 
-       /*
-        * The concurrent collector doesn't move objects, neither on
-        * the major heap nor in the nursery, so we can mark even
-        * before pinning has finished.  For the non-concurrent
-        * collector we start the workers after pinning.
-        */
-       if (mode != COPY_OR_MARK_FROM_ROOTS_SERIAL) {
-               SGEN_ASSERT (0, sgen_workers_all_done (), "Why are the workers not done when we start or finish a major collection?");
-               sgen_workers_start_all_workers (object_ops);
-               gray_queue_enable_redirect (WORKERS_DISTRIBUTE_GRAY_QUEUE);
+       SGEN_ASSERT (0, sgen_workers_all_done (), "Why are the workers not done when we start or finish a major collection?");
+       if (mode == COPY_OR_MARK_FROM_ROOTS_FINISH_CONCURRENT) {
+               if (sgen_workers_have_idle_work ()) {
+                       /*
+                        * We force the finish of the worker with the new object ops context
+                        * which can also do copying. We need to have finished pinning.
+                        */
+                       sgen_workers_start_all_workers (object_ops, NULL);
+                       sgen_workers_join ();
+               }
        }
 
 #ifdef SGEN_DEBUG_INTERNAL_ALLOC
@@ -1884,26 +1817,40 @@ major_copy_or_mark_from_roots (size_t *old_next_pin_slot, CopyOrMarkFromRootsMod
 
        sgen_client_collecting_major_3 (&fin_ready_queue, &critical_fin_queue);
 
-       /*
-        * FIXME: is this the right context?  It doesn't seem to contain a copy function
-        * unless we're concurrent.
-        */
-       enqueue_scan_from_roots_jobs (heap_start, heap_end, object_ops);
+       enqueue_scan_from_roots_jobs (heap_start, heap_end, object_ops, FALSE);
 
        TV_GETTIME (btv);
        time_major_scan_roots += TV_ELAPSED (atv, btv);
 
+       /*
+        * We start the concurrent worker after pinning and after we scanned the roots
+        * in order to make sure that the worker does not finish before handling all
+        * the roots.
+        */
+       if (mode == COPY_OR_MARK_FROM_ROOTS_START_CONCURRENT) {
+               if (precleaning_enabled) {
+                       ScanJob *sj;
+                       /* Mod union preclean job */
+                       sj = (ScanJob*)sgen_thread_pool_job_alloc ("preclean mod union cardtable", job_mod_union_preclean, sizeof (ScanJob));
+                       sj->ops = object_ops;
+                       sgen_workers_start_all_workers (object_ops, &sj->job);
+               } else {
+                       sgen_workers_start_all_workers (object_ops, NULL);
+               }
+               gray_queue_enable_redirect (WORKERS_DISTRIBUTE_GRAY_QUEUE);
+       }
+
        if (mode == COPY_OR_MARK_FROM_ROOTS_FINISH_CONCURRENT) {
                ScanJob *sj;
 
                /* Mod union card table */
                sj = (ScanJob*)sgen_thread_pool_job_alloc ("scan mod union cardtable", job_scan_major_mod_union_card_table, sizeof (ScanJob));
                sj->ops = object_ops;
-               sgen_workers_enqueue_job (&sj->job);
+               sgen_workers_enqueue_job (&sj->job, FALSE);
 
                sj = (ScanJob*)sgen_thread_pool_job_alloc ("scan LOS mod union cardtable", job_scan_los_mod_union_card_table, sizeof (ScanJob));
                sj->ops = object_ops;
-               sgen_workers_enqueue_job (&sj->job);
+               sgen_workers_enqueue_job (&sj->job, FALSE);
 
                TV_GETTIME (atv);
                time_major_scan_mod_union += TV_ELAPSED (btv, atv);
@@ -1913,22 +1860,16 @@ major_copy_or_mark_from_roots (size_t *old_next_pin_slot, CopyOrMarkFromRootsMod
 }
 
 static void
-major_finish_copy_or_mark (void)
+major_finish_copy_or_mark (CopyOrMarkFromRootsMode mode)
 {
-       if (!concurrent_collection_in_progress)
-               return;
-
-       /*
-        * Prepare the pin queue for the next collection.  Since pinning runs on the worker
-        * threads we must wait for the jobs to finish before we can reset it.
-        */
-       sgen_workers_wait_for_jobs_finished ();
-       sgen_finish_pinning ();
+       if (mode == COPY_OR_MARK_FROM_ROOTS_START_CONCURRENT) {
+               sgen_finish_pinning ();
 
-       sgen_pin_stats_reset ();
+               sgen_pin_stats_reset ();
 
-       if (do_concurrent_checks)
-               sgen_debug_check_nursery_is_clean ();
+               if (do_concurrent_checks)
+                       sgen_debug_check_nursery_is_clean ();
+       }
 }
 
 static void
@@ -1942,7 +1883,8 @@ major_start_collection (gboolean concurrent, size_t *old_next_pin_slot)
 
        g_assert (sgen_section_gray_queue_is_empty (sgen_workers_get_distribute_section_gray_queue ()));
 
-       sgen_cement_reset ();
+       if (!concurrent)
+               sgen_cement_reset ();
 
        if (concurrent) {
                g_assert (major_collector.is_concurrent);
@@ -1969,15 +1911,16 @@ major_start_collection (gboolean concurrent, size_t *old_next_pin_slot)
        if (major_collector.start_major_collection)
                major_collector.start_major_collection ();
 
-       major_copy_or_mark_from_roots (old_next_pin_slot, concurrent ? COPY_OR_MARK_FROM_ROOTS_START_CONCURRENT : COPY_OR_MARK_FROM_ROOTS_SERIAL, FALSE, object_ops);
-       major_finish_copy_or_mark ();
+       major_copy_or_mark_from_roots (old_next_pin_slot, concurrent ? COPY_OR_MARK_FROM_ROOTS_START_CONCURRENT : COPY_OR_MARK_FROM_ROOTS_SERIAL, object_ops);
+       major_finish_copy_or_mark (concurrent ? COPY_OR_MARK_FROM_ROOTS_START_CONCURRENT : COPY_OR_MARK_FROM_ROOTS_SERIAL);
 }
 
 static void
-major_finish_collection (const char *reason, size_t old_next_pin_slot, gboolean forced, gboolean scan_whole_nursery)
+major_finish_collection (const char *reason, size_t old_next_pin_slot, gboolean forced)
 {
        ScannedObjectCounts counts;
        SgenObjectOperations *object_ops;
+       mword fragment_total;
        TV_DECLARE (atv);
        TV_DECLARE (btv);
 
@@ -1986,31 +1929,17 @@ major_finish_collection (const char *reason, size_t old_next_pin_slot, gboolean
        if (concurrent_collection_in_progress) {
                object_ops = &major_collector.major_ops_concurrent_finish;
 
-               major_copy_or_mark_from_roots (NULL, COPY_OR_MARK_FROM_ROOTS_FINISH_CONCURRENT, scan_whole_nursery, object_ops);
-
-               major_finish_copy_or_mark ();
+               major_copy_or_mark_from_roots (NULL, COPY_OR_MARK_FROM_ROOTS_FINISH_CONCURRENT, object_ops);
 
-               sgen_workers_join ();
-
-               SGEN_ASSERT (0, sgen_gray_object_queue_is_empty (&gray_queue), "Why is the gray queue not empty after workers have finished working?");
+               major_finish_copy_or_mark (COPY_OR_MARK_FROM_ROOTS_FINISH_CONCURRENT);
 
 #ifdef SGEN_DEBUG_INTERNAL_ALLOC
                main_gc_thread = NULL;
 #endif
-
-               if (do_concurrent_checks)
-                       sgen_debug_check_nursery_is_clean ();
        } else {
-               SGEN_ASSERT (0, !scan_whole_nursery, "scan_whole_nursery only applies to concurrent collections");
                object_ops = &major_collector.major_ops_serial;
        }
 
-       /*
-        * The workers have stopped so we need to finish gray queue
-        * work that might result from finalization in the main GC
-        * thread.  Redirection must therefore be turned off.
-        */
-       sgen_gray_object_queue_disable_alloc_prepare (&gray_queue);
        g_assert (sgen_section_gray_queue_is_empty (sgen_workers_get_distribute_section_gray_queue ()));
 
        /* all the objects in the heap */
@@ -2045,22 +1974,25 @@ major_finish_collection (const char *reason, size_t old_next_pin_slot, gboolean
        reset_heap_boundaries ();
        sgen_update_heap_boundaries ((mword)sgen_get_nursery_start (), (mword)sgen_get_nursery_end ());
 
-       if (!concurrent_collection_in_progress) {
-               /* walk the pin_queue, build up the fragment list of free memory, unmark
-                * pinned objects as we go, memzero() the empty fragments so they are ready for the
-                * next allocations.
-                */
-               if (!sgen_build_nursery_fragments (nursery_section, NULL))
-                       degraded_mode = 1;
+       /* walk the pin_queue, build up the fragment list of free memory, unmark
+        * pinned objects as we go, memzero() the empty fragments so they are ready for the
+        * next allocations.
+        */
+       fragment_total = sgen_build_nursery_fragments (nursery_section, NULL);
+       if (!fragment_total)
+               degraded_mode = 1;
+       SGEN_LOG (4, "Free space in nursery after major %ld", (long)fragment_total);
 
-               /* prepare the pin queue for the next collection */
-               sgen_finish_pinning ();
+       if (do_concurrent_checks && concurrent_collection_in_progress)
+               sgen_debug_check_nursery_is_clean ();
 
-               /* Clear TLABs for all threads */
-               sgen_clear_tlabs ();
+       /* prepare the pin queue for the next collection */
+       sgen_finish_pinning ();
 
-               sgen_pin_stats_reset ();
-       }
+       /* Clear TLABs for all threads */
+       sgen_clear_tlabs ();
+
+       sgen_pin_stats_reset ();
 
        sgen_cement_clear_below_threshold ();
 
@@ -2071,6 +2003,7 @@ major_finish_collection (const char *reason, size_t old_next_pin_slot, gboolean
        time_major_fragment_creation += TV_ELAPSED (atv, btv);
 
        binary_protocol_sweep_begin (GENERATION_OLD, !major_collector.sweeps_lazily);
+       sgen_memgov_major_pre_sweep ();
 
        TV_GETTIME (atv);
        time_major_free_bigobjs += TV_ELAPSED (btv, atv);
@@ -2136,7 +2069,7 @@ major_do_collection (const char *reason, gboolean forced)
        TV_GETTIME (time_start);
 
        major_start_collection (FALSE, &old_next_pin_slot);
-       major_finish_collection (reason, old_next_pin_slot, forced, FALSE);
+       major_finish_collection (reason, old_next_pin_slot, forced);
 
        TV_GETTIME (time_end);
        gc_stats.major_gc_time += TV_ELAPSED (time_start, time_end);
@@ -2211,21 +2144,17 @@ major_finish_concurrent_collection (gboolean forced)
 {
        TV_DECLARE (total_start);
        TV_DECLARE (total_end);
-       gboolean late_pinned;
-       SgenGrayQueue unpin_queue;
-       memset (&unpin_queue, 0, sizeof (unpin_queue));
 
        TV_GETTIME (total_start);
 
        binary_protocol_concurrent_finish ();
 
        /*
-        * The major collector can add global remsets which are processed in the finishing
-        * nursery collection, below.  That implies that the workers must have finished
-        * marking before the nursery collection is allowed to run, otherwise we might miss
-        * some remsets.
+        * We need to stop all workers since we're updating the cardtable below.
+        * The workers will be resumed with a finishing pause context to avoid
+        * additional cardtable and object scanning.
         */
-       sgen_workers_wait ();
+       sgen_workers_stop_all_workers ();
 
        SGEN_TV_GETTIME (time_major_conc_collection_end);
        gc_stats.major_gc_time_concurrent += SGEN_TV_ELAPSED (time_major_conc_collection_start, time_major_conc_collection_end);
@@ -2233,20 +2162,16 @@ major_finish_concurrent_collection (gboolean forced)
        major_collector.update_cardtable_mod_union ();
        sgen_los_update_cardtable_mod_union ();
 
-       late_pinned = collect_nursery (&unpin_queue, TRUE);
-
        if (mod_union_consistency_check)
                sgen_check_mod_union_consistency ();
 
        current_collection_generation = GENERATION_OLD;
-       major_finish_collection ("finishing", -1, forced, late_pinned);
+       sgen_cement_reset ();
+       major_finish_collection ("finishing", -1, forced);
 
        if (whole_heap_check_before_collection)
                sgen_check_whole_heap (FALSE);
 
-       unpin_objects_from_queue (&unpin_queue);
-       sgen_gray_object_queue_deinit (&unpin_queue);
-
        TV_GETTIME (total_end);
        gc_stats.major_gc_time += TV_ELAPSED (total_start, total_end) - TV_ELAPSED (last_minor_collection_start_tv, last_minor_collection_end_tv);
 
@@ -2259,12 +2184,12 @@ major_finish_concurrent_collection (gboolean forced)
  * LOCKING: The GC lock MUST be held.
  */
 void
-sgen_ensure_free_space (size_t size)
+sgen_ensure_free_space (size_t size, int generation)
 {
        int generation_to_collect = -1;
        const char *reason = NULL;
 
-       if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
+       if (generation == GENERATION_OLD) {
                if (sgen_need_major_collection (size)) {
                        reason = "LOS overflow";
                        generation_to_collect = GENERATION_OLD;
@@ -2276,7 +2201,7 @@ sgen_ensure_free_space (size_t size)
                                generation_to_collect = GENERATION_OLD;
                        }
                } else if (sgen_need_major_collection (size)) {
-                       reason = "Minor allowance";
+                       reason = concurrent_collection_in_progress ? "Forced finish concurrent collection" : "Minor allowance";
                        generation_to_collect = GENERATION_OLD;
                } else {
                        generation_to_collect = GENERATION_NURSERY;
@@ -2323,34 +2248,23 @@ sgen_perform_collection (size_t requested_size, int generation_to_collect, const
 
        if (concurrent_collection_in_progress) {
                /*
-                * We update the concurrent collection.  If it finished, we're done.  If
-                * not, and we've been asked to do a nursery collection, we do that.
+                * If the concurrent worker is finished or we are asked to do a major collection
+                * then we finish the concurrent collection.
                 */
-               gboolean finish = major_should_finish_concurrent_collection () || (wait_to_finish && generation_to_collect == GENERATION_OLD);
+               gboolean finish = major_should_finish_concurrent_collection () || generation_to_collect == GENERATION_OLD;
 
                if (finish) {
                        major_finish_concurrent_collection (wait_to_finish);
                        oldest_generation_collected = GENERATION_OLD;
                } else {
+                       SGEN_ASSERT (0, generation_to_collect == GENERATION_NURSERY, "Why aren't we finishing the concurrent collection?");
                        major_update_concurrent_collection ();
-                       if (generation_to_collect == GENERATION_NURSERY)
-                               collect_nursery (NULL, FALSE);
+                       collect_nursery (NULL, FALSE);
                }
 
                goto done;
        }
 
-       /*
-        * If we've been asked to do a major collection, and the major collector wants to
-        * run synchronously (to evacuate), we set the flag to do that.
-        */
-       if (generation_to_collect == GENERATION_OLD &&
-                       allow_synchronous_major &&
-                       major_collector.want_synchronous_collection &&
-                       *major_collector.want_synchronous_collection) {
-               wait_to_finish = TRUE;
-       }
-
        SGEN_ASSERT (0, !concurrent_collection_in_progress, "Why did this not get handled above?");
 
        /*
@@ -2545,11 +2459,11 @@ sgen_gc_invoke_finalizers (void)
                if (!sgen_pointer_queue_is_empty (&fin_ready_queue)) {
                        pending_unqueued_finalizer = TRUE;
                        mono_memory_write_barrier ();
-                       obj = sgen_pointer_queue_pop (&fin_ready_queue);
+                       obj = (GCObject *)sgen_pointer_queue_pop (&fin_ready_queue);
                } else if (!sgen_pointer_queue_is_empty (&critical_fin_queue)) {
                        pending_unqueued_finalizer = TRUE;
                        mono_memory_write_barrier ();
-                       obj = sgen_pointer_queue_pop (&critical_fin_queue);
+                       obj = (GCObject *)sgen_pointer_queue_pop (&critical_fin_queue);
                } else {
                        obj = NULL;
                }
@@ -2592,18 +2506,20 @@ sgen_have_pending_finalizers (void)
  * We do not coalesce roots.
  */
 int
-sgen_register_root (char *start, size_t size, SgenDescriptor descr, int root_type)
+sgen_register_root (char *start, size_t size, SgenDescriptor descr, int root_type, int source, const char *msg)
 {
        RootRecord new_root;
        int i;
        LOCK_GC;
        for (i = 0; i < ROOT_TYPE_NUM; ++i) {
-               RootRecord *root = sgen_hash_table_lookup (&roots_hash [i], start);
+               RootRecord *root = (RootRecord *)sgen_hash_table_lookup (&roots_hash [i], start);
                /* we allow changing the size and the descriptor (for thread statics etc) */
                if (root) {
                        size_t old_size = root->end_root - start;
                        root->end_root = start + size;
                        SGEN_ASSERT (0, !!root->root_desc == !!descr, "Can't change whether a root is precise or conservative.");
+                       SGEN_ASSERT (0, root->source == source, "Can't change a root's source identifier.");
+                       SGEN_ASSERT (0, !!root->msg == !!msg, "Can't change a root's message.");
                        root->root_desc = descr;
                        roots_size += size;
                        roots_size -= old_size;
@@ -2614,11 +2530,13 @@ sgen_register_root (char *start, size_t size, SgenDescriptor descr, int root_typ
 
        new_root.end_root = start + size;
        new_root.root_desc = descr;
+       new_root.source = source;
+       new_root.msg = msg;
 
        sgen_hash_table_replace (&roots_hash [root_type], start, &new_root, NULL);
        roots_size += size;
 
-       SGEN_LOG (3, "Added root for range: %p-%p, descr: %llx  (%d/%d bytes)", start, new_root.end_root, descr, (int)size, (int)roots_size);
+       SGEN_LOG (3, "Added root for range: %p-%p, descr: %llx  (%d/%d bytes)", start, new_root.end_root, (long long)descr, (int)size, (int)roots_size);
 
        UNLOCK_GC;
        return TRUE;
@@ -2740,7 +2658,7 @@ mono_gc_wbarrier_generic_store (gpointer ptr, GCObject* value)
 {
        SGEN_LOG (8, "Wbarrier store at %p to %p (%s)", ptr, value, value ? sgen_client_vtable_get_name (SGEN_LOAD_VTABLE (value)) : "null");
        SGEN_UPDATE_REFERENCE_ALLOW_NULL (ptr, value);
-       if (ptr_in_nursery (value))
+       if (ptr_in_nursery (value) || concurrent_collection_in_progress)
                mono_gc_wbarrier_generic_nostore (ptr);
        sgen_dummy_use (value);
 }
@@ -2755,9 +2673,9 @@ mono_gc_wbarrier_generic_store_atomic (gpointer ptr, GCObject *value)
 
        SGEN_LOG (8, "Wbarrier atomic store at %p to %p (%s)", ptr, value, value ? sgen_client_vtable_get_name (SGEN_LOAD_VTABLE (value)) : "null");
 
-       InterlockedWritePointer (ptr, value);
+       InterlockedWritePointer ((volatile gpointer *)ptr, value);
 
-       if (ptr_in_nursery (value))
+       if (ptr_in_nursery (value) || concurrent_collection_in_progress)
                mono_gc_wbarrier_generic_nostore (ptr);
 
        sgen_dummy_use (value);
@@ -2766,8 +2684,8 @@ mono_gc_wbarrier_generic_store_atomic (gpointer ptr, GCObject *value)
 void
 sgen_wbarrier_value_copy_bitmap (gpointer _dest, gpointer _src, int size, unsigned bitmap)
 {
-       GCObject **dest = _dest;
-       GCObject **src = _src;
+       GCObject **dest = (GCObject **)_dest;
+       GCObject **src = (GCObject **)_src;
 
        while (size) {
                if (bitmap & 0x1)
@@ -2818,58 +2736,6 @@ sgen_gc_get_used_size (void)
        return tot;
 }
 
-GCObject*
-sgen_weak_link_get (void **link_addr)
-{
-       void * volatile *link_addr_volatile;
-       void *ptr;
-       GCObject *obj;
- retry:
-       link_addr_volatile = link_addr;
-       ptr = (void*)*link_addr_volatile;
-       /*
-        * At this point we have a hidden pointer.  If the GC runs
-        * here, it will not recognize the hidden pointer as a
-        * reference, and if the object behind it is not referenced
-        * elsewhere, it will be freed.  Once the world is restarted
-        * we reveal the pointer, giving us a pointer to a freed
-        * object.  To make sure we don't return it, we load the
-        * hidden pointer again.  If it's still the same, we can be
-        * sure the object reference is valid.
-        */
-       if (ptr)
-               obj = (GCObject*) REVEAL_POINTER (ptr);
-       else
-               return NULL;
-
-       mono_memory_barrier ();
-
-       /*
-        * During the second bridge processing step the world is
-        * running again.  That step processes all weak links once
-        * more to null those that refer to dead objects.  Before that
-        * is completed, those links must not be followed, so we
-        * conservatively wait for bridge processing when any weak
-        * link is dereferenced.
-        */
-       sgen_client_bridge_wait_for_processing ();
-
-       if ((void*)*link_addr_volatile != ptr)
-               goto retry;
-
-       return obj;
-}
-
-gboolean
-sgen_set_allow_synchronous_major (gboolean flag)
-{
-       if (!major_collector.is_concurrent)
-               return flag;
-
-       allow_synchronous_major = flag;
-       return TRUE;
-}
-
 void
 sgen_env_var_error (const char *env_var, const char *fallback, const char *description_format, ...)
 {
@@ -2925,7 +2791,7 @@ sgen_gc_init (void)
                        return;
                case -1:
                        /* being inited by another thread */
-                       g_usleep (1000);
+                       mono_thread_info_usleep (1000);
                        break;
                case 0:
                        /* we will init it */
@@ -2941,11 +2807,11 @@ sgen_gc_init (void)
        mono_thread_smr_init ();
 #endif
 
-       LOCK_INIT (gc_mutex);
+       mono_coop_mutex_init (&gc_mutex);
 
        gc_debug_file = stderr;
 
-       LOCK_INIT (sgen_interruption_mutex);
+       mono_coop_mutex_init (&sgen_interruption_mutex);
 
        if ((env = g_getenv (MONO_GC_PARAMS_NAME))) {
                opts = g_strsplit (env, ",", -1);
@@ -2971,6 +2837,7 @@ sgen_gc_init (void)
        sgen_init_descriptors ();
        sgen_init_gray_queues ();
        sgen_init_allocator ();
+       sgen_init_gchandles ();
 
        sgen_register_fixed_internal_mem_type (INTERNAL_MEM_SECTION, SGEN_SIZEOF_GC_MEM_SECTION);
        sgen_register_fixed_internal_mem_type (INTERNAL_MEM_GRAY_QUEUE, sizeof (GrayQueueSection));
@@ -3003,9 +2870,6 @@ sgen_gc_init (void)
 
        sgen_nursery_size = DEFAULT_NURSERY_SIZE;
 
-       if (major_collector.is_concurrent)
-               cement_enabled = FALSE;
-
        if (opts) {
                gboolean usage_printed = FALSE;
 
@@ -3088,23 +2952,6 @@ sgen_gc_init (void)
                                }
                                continue;
                        }
-                       if (g_str_has_prefix (opt, "allow-synchronous-major=")) {
-                               if (!major_collector.is_concurrent) {
-                                       sgen_env_var_error (MONO_GC_PARAMS_NAME, "Ignoring.", "`allow-synchronous-major` is only valid for the concurrent major collector.");
-                                       continue;
-                               }
-
-                               opt = strchr (opt, '=') + 1;
-
-                               if (!strcmp (opt, "yes")) {
-                                       allow_synchronous_major = TRUE;
-                               } else if (!strcmp (opt, "no")) {
-                                       allow_synchronous_major = FALSE;
-                               } else {
-                                       sgen_env_var_error (MONO_GC_PARAMS_NAME, "Using default value.", "`allow-synchronous-major` must be either `yes' or `no'.");
-                                       continue;
-                               }
-                       }
 
                        if (!strcmp (opt, "cementing")) {
                                cement_enabled = TRUE;
@@ -3115,6 +2962,15 @@ sgen_gc_init (void)
                                continue;
                        }
 
+                       if (!strcmp (opt, "precleaning")) {
+                               precleaning_enabled = TRUE;
+                               continue;
+                       }
+                       if (!strcmp (opt, "no-precleaning")) {
+                               precleaning_enabled = FALSE;
+                               continue;
+                       }
+
                        if (major_collector.handle_gc_param && major_collector.handle_gc_param (opt))
                                continue;
 
@@ -3137,8 +2993,6 @@ sgen_gc_init (void)
                        fprintf (stderr, "  minor=COLLECTOR (where COLLECTOR is `simple' or `split')\n");
                        fprintf (stderr, "  wbarrier=WBARRIER (where WBARRIER is `remset' or `cardtable')\n");
                        fprintf (stderr, "  [no-]cementing\n");
-                       if (major_collector.is_concurrent)
-                               fprintf (stderr, "  allow-synchronous-major=FLAG (where FLAG is `yes' or `no')\n");
                        if (major_collector.print_gc_param_usage)
                                major_collector.print_gc_param_usage ();
                        if (sgen_minor_collector.print_gc_param_usage)
@@ -3162,11 +3016,6 @@ sgen_gc_init (void)
 
        alloc_nursery ();
 
-       if (major_collector.is_concurrent && cement_enabled) {
-               sgen_env_var_error (MONO_GC_PARAMS_NAME, "Ignoring.", "`cementing` is not supported on concurrent major collectors.");
-               cement_enabled = FALSE;
-       }
-
        sgen_cement_init (cement_enabled);
 
        if ((env = g_getenv (MONO_GC_DEBUG_NAME))) {
@@ -3239,6 +3088,7 @@ sgen_gc_init (void)
                                        sgen_env_var_error (MONO_GC_DEBUG_NAME, "Ignoring.", "`check-concurrent` only works with concurrent major collectors.");
                                        continue;
                                }
+                               nursery_clear_policy = CLEAR_AT_GC;
                                do_concurrent_checks = TRUE;
                        } else if (!strcmp (opt, "dump-nursery-at-minor-gc")) {
                                do_dump_nursery_content = TRUE;
@@ -3253,7 +3103,7 @@ sgen_gc_init (void)
                        } else if (g_str_has_prefix (opt, "binary-protocol=")) {
                                char *filename = strchr (opt, '=') + 1;
                                char *colon = strrchr (filename, ':');
-                               size_t limit = -1;
+                               size_t limit = 0;
                                if (colon) {
                                        if (!mono_gc_parse_environment_string_extract_number (colon + 1, &limit)) {
                                                sgen_env_var_error (MONO_GC_DEBUG_NAME, "Ignoring limit.", "Binary protocol file size limit must be an integer.");
@@ -3317,6 +3167,8 @@ sgen_gc_init (void)
 
        sgen_card_table_init (&remset);
 
+       sgen_register_root (NULL, 0, sgen_make_user_root_descriptor (sgen_mark_normal_gc_handles), ROOT_TYPE_NORMAL, MONO_ROOT_SOURCE_GC_HANDLE, "normal gc handles");
+
        gc_initialized = 1;
 }
 
@@ -3329,7 +3181,7 @@ sgen_get_nursery_clear_policy (void)
 void
 sgen_gc_lock (void)
 {
-       LOCK_GC;
+       mono_coop_mutex_lock (&gc_mutex);
 }
 
 void
@@ -3337,7 +3189,7 @@ sgen_gc_unlock (void)
 {
        gboolean try_free = sgen_try_free_some_memory;
        sgen_try_free_some_memory = FALSE;
-       mono_mutex_unlock (&gc_mutex);
+       mono_coop_mutex_unlock (&gc_mutex);
        if (try_free)
                mono_thread_hazardous_try_free_some ();
 }
@@ -3377,7 +3229,7 @@ sgen_stop_world (int generation)
 
        SGEN_ASSERT (0, !world_is_stopped, "Why are we stopping a stopped world?");
 
-       binary_protocol_world_stopping (generation, sgen_timestamp (), (gpointer)mono_native_thread_id_get ());
+       binary_protocol_world_stopping (generation, sgen_timestamp (), (gpointer) (gsize) mono_native_thread_id_get ());
 
        sgen_client_stop_world (generation);