1 /*
   2  * Copyright (c) 2013, 2017, Red Hat, Inc. and/or its affiliates.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "classfile/stringTable.hpp"
  26 #include "gc/shared/gcTimer.hpp"
  27 #include "gc/shared/parallelCleaning.hpp"
  28 #include "gc/shared/referenceProcessor.hpp"
  29 #include "gc/shared/strongRootsScope.hpp"
  30 #include "gc/shared/suspendibleThreadSet.hpp"
  31 #include "gc/shenandoah/brooksPointer.hpp"
  32 #include "gc/shenandoah/shenandoahBarrierSet.inline.hpp"
  33 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  34 #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp"
  35 #include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
  36 #include "gc/shenandoah/shenandoahMarkCompact.hpp"
  37 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  38 #include "gc/shenandoah/shenandoahRootProcessor.hpp"
  39 #include "gc/shenandoah/shenandoah_specialized_oop_closures.hpp"
  40 #include "gc/shenandoah/shenandoahTaskqueue.hpp"
  41 #include "gc/shenandoah/shenandoahUtils.hpp"
  42 #include "gc/shared/weakProcessor.hpp"
  43 #include "code/codeCache.hpp"
  44 #include "classfile/symbolTable.hpp"
  45 #include "classfile/systemDictionary.hpp"
  46 #include "memory/iterator.inline.hpp"
  47 #include "oops/oop.inline.hpp"
  48 #include "gc/shared/taskqueue.inline.hpp"
  49 #include "logging/logStream.hpp"
  50 
  51 template<UpdateRefsMode UPDATE_REFS>
  52 class ShenandoahInitMarkRootsClosure : public OopClosure {
  53 private:
  54   ShenandoahObjToScanQueue* _queue;
  55   ShenandoahHeap* _heap;
  56 
  57   template <class T>
  58   inline void do_oop_nv(T* p) {
  59     ShenandoahConcurrentMark::mark_through_ref<T, UPDATE_REFS, false /* string dedup */>(p, _heap, _queue);
  60   }
  61 
  62 public:
  63   ShenandoahInitMarkRootsClosure(ShenandoahObjToScanQueue* q) :
  64     _queue(q), _heap(ShenandoahHeap::heap()) {};
  65 
  66   void do_oop(narrowOop* p) { do_oop_nv(p); }
  67   void do_oop(oop* p)       { do_oop_nv(p); }
  68 };
  69 
  70 ShenandoahMarkRefsSuperClosure::ShenandoahMarkRefsSuperClosure(ShenandoahObjToScanQueue* q, ReferenceProcessor* rp) :
  71   MetadataAwareOopClosure(rp),
  72   _queue(q),
  73   _dedup_queue(NULL),
  74   _heap(ShenandoahHeap::heap())
  75 { }
  76 
  77 
  78 ShenandoahMarkRefsSuperClosure::ShenandoahMarkRefsSuperClosure(ShenandoahObjToScanQueue* q, ShenandoahStrDedupQueue* dq, ReferenceProcessor* rp) :
  79   MetadataAwareOopClosure(rp),
  80   _queue(q),
  81   _dedup_queue(dq),
  82   _heap(ShenandoahHeap::heap())
  83 { }
  84 
  85 
  86 template<UpdateRefsMode UPDATE_REFS>
  87 class ShenandoahInitMarkRootsTask : public AbstractGangTask {
  88 private:
  89   ShenandoahRootProcessor* _rp;
  90   bool _process_refs;
  91 public:
  92   ShenandoahInitMarkRootsTask(ShenandoahRootProcessor* rp, bool process_refs) :
  93     AbstractGangTask("Shenandoah init mark roots task"),
  94     _rp(rp),
  95     _process_refs(process_refs) {
  96   }
  97 
  98   void work(uint worker_id) {
  99     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 100 
 101     ShenandoahHeap* heap = ShenandoahHeap::heap();
 102     ShenandoahObjToScanQueueSet* queues = heap->concurrentMark()->task_queues();
 103     assert(queues->get_reserved() > worker_id, "Queue has not been reserved for worker id: %d", worker_id);
 104 
 105     ShenandoahObjToScanQueue* q = queues->queue(worker_id);
 106     ShenandoahInitMarkRootsClosure<UPDATE_REFS> mark_cl(q);
 107     CLDToOopClosure cldCl(&mark_cl);
 108     MarkingCodeBlobClosure blobsCl(&mark_cl, ! CodeBlobToOopClosure::FixRelocations);
 109 
 110     // The rationale for selecting the roots to scan is as follows:
 111     //   a. With unload_classes = true, we only want to scan the actual strong roots from the
 112     //      code cache. This will allow us to identify the dead classes, unload them, *and*
 113     //      invalidate the relevant code cache blobs. This could be only done together with
 114     //      class unloading.
 115     //   b. With unload_classes = false, we have to nominally retain all the references from code
 116     //      cache, because there could be the case of embedded class/oop in the generated code,
 117     //      which we will never visit during mark. Without code cache invalidation, as in (a),
 118     //      we risk executing that code cache blob, and crashing.
 119     //   c. With ShenandoahConcurrentScanCodeRoots, we avoid scanning the entire code cache here,
 120     //      and instead do that in concurrent phase under the relevant lock. This saves init mark
 121     //      pause time.
 122 
 123     ResourceMark m;
 124     if (heap->concurrentMark()->unload_classes()) {
 125       _rp->process_strong_roots(&mark_cl, _process_refs ? NULL : &mark_cl, &cldCl, NULL, &blobsCl, NULL, worker_id);
 126     } else {
 127       if (ShenandoahConcurrentScanCodeRoots) {
 128         CodeBlobClosure* code_blobs = NULL;
 129 #ifdef ASSERT
 130         ShenandoahAssertToSpaceClosure assert_to_space_oops;
 131         CodeBlobToOopClosure assert_to_space(&assert_to_space_oops, !CodeBlobToOopClosure::FixRelocations);
 132         // If conc code cache evac is disabled, code cache should have only to-space ptrs.
 133         // Otherwise, it should have to-space ptrs only if mark does not update refs.
 134         if (!ShenandoahConcurrentEvacCodeRoots && !heap->has_forwarded_objects()) {
 135           code_blobs = &assert_to_space;
 136         }
 137 #endif
 138         _rp->process_all_roots(&mark_cl, _process_refs ? NULL : &mark_cl, &cldCl, code_blobs, NULL, worker_id);
 139       } else {
 140         _rp->process_all_roots(&mark_cl, _process_refs ? NULL : &mark_cl, &cldCl, &blobsCl, NULL, worker_id);
 141       }
 142     }
 143   }
 144 };
 145 
 146 class ShenandoahUpdateRootsTask : public AbstractGangTask {
 147 private:
 148   ShenandoahRootProcessor* _rp;
 149   const bool _update_code_cache;
 150 public:
 151   ShenandoahUpdateRootsTask(ShenandoahRootProcessor* rp, bool update_code_cache) :
 152     AbstractGangTask("Shenandoah update roots task"),
 153     _rp(rp),
 154     _update_code_cache(update_code_cache) {
 155   }
 156 
 157   void work(uint worker_id) {
 158     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 159 
 160     ShenandoahHeap* heap = ShenandoahHeap::heap();
 161     ShenandoahUpdateRefsClosure cl;
 162     CLDToOopClosure cldCl(&cl);
 163 
 164     CodeBlobClosure* code_blobs;
 165     CodeBlobToOopClosure update_blobs(&cl, CodeBlobToOopClosure::FixRelocations);
 166 #ifdef ASSERT
 167     ShenandoahAssertToSpaceClosure assert_to_space_oops;
 168     CodeBlobToOopClosure assert_to_space(&assert_to_space_oops, !CodeBlobToOopClosure::FixRelocations);
 169 #endif
 170     if (_update_code_cache) {
 171       code_blobs = &update_blobs;
 172     } else {
 173       code_blobs =
 174         DEBUG_ONLY(&assert_to_space)
 175         NOT_DEBUG(NULL);
 176     }
 177     _rp->process_all_roots(&cl, &cl, &cldCl, code_blobs, NULL, worker_id);
 178   }
 179 };
 180 
 181 class ShenandoahConcurrentMarkingTask : public AbstractGangTask {
 182 private:
 183   ShenandoahConcurrentMark* _cm;
 184   ParallelTaskTerminator* _terminator;
 185   bool _update_refs;
 186 
 187 public:
 188   ShenandoahConcurrentMarkingTask(ShenandoahConcurrentMark* cm, ParallelTaskTerminator* terminator, bool update_refs) :
 189     AbstractGangTask("Root Region Scan"), _cm(cm), _terminator(terminator), _update_refs(update_refs) {
 190   }
 191 
 192 
 193   void work(uint worker_id) {
 194     SuspendibleThreadSetJoiner stsj(ShenandoahSuspendibleWorkers);
 195     ShenandoahObjToScanQueue* q = _cm->get_queue(worker_id);
 196     jushort* live_data = _cm->get_liveness(worker_id);
 197     ReferenceProcessor* rp;
 198     if (_cm->process_references()) {
 199       rp = ShenandoahHeap::heap()->ref_processor();
 200       shenandoah_assert_rp_isalive_installed();
 201     } else {
 202       rp = NULL;
 203     }
 204 
 205     _cm->concurrent_scan_code_roots(worker_id, rp, _update_refs);
 206     _cm->mark_loop(worker_id, _terminator, rp,
 207                    true, // cancellable
 208                    true, // drain SATBs as we go
 209                    true, // count liveness
 210                    _cm->unload_classes(),
 211                    _update_refs,
 212                    ShenandoahStringDedup::is_enabled()); // perform string dedup
 213   }
 214 };
 215 
 216 class ShenandoahFinalMarkingTask : public AbstractGangTask {
 217 private:
 218   ShenandoahConcurrentMark* _cm;
 219   ParallelTaskTerminator* _terminator;
 220   bool _update_refs;
 221   bool _count_live;
 222   bool _unload_classes;
 223   bool _dedup_string;
 224 
 225 public:
 226   ShenandoahFinalMarkingTask(ShenandoahConcurrentMark* cm, ParallelTaskTerminator* terminator, bool update_refs,
 227     bool count_live, bool unload_classes, bool dedup_string = false) :
 228     AbstractGangTask("Shenandoah Final Marking"), _cm(cm), _terminator(terminator), _update_refs(update_refs),
 229     _count_live(count_live), _unload_classes(unload_classes), _dedup_string(dedup_string) {
 230   }
 231 
 232   void work(uint worker_id) {
 233     // First drain remaining SATB buffers.
 234     // Notice that this is not strictly necessary for mark-compact. But since
 235     // it requires a StrongRootsScope around the task, we need to claim the
 236     // threads, and performance-wise it doesn't really matter. Adds about 1ms to
 237     // full-gc.
 238     _cm->drain_satb_buffers(worker_id, true);
 239 
 240     ReferenceProcessor* rp;
 241     if (_cm->process_references()) {
 242       rp = ShenandoahHeap::heap()->ref_processor();
 243       shenandoah_assert_rp_isalive_installed();
 244     } else {
 245       rp = NULL;
 246     }
 247 
 248     // Degenerated cycle may bypass concurrent cycle, so code roots might not be scanned,
 249     // let's check here.
 250     _cm->concurrent_scan_code_roots(worker_id, rp, _update_refs);
 251     _cm->mark_loop(worker_id, _terminator, rp,
 252                    false, // not cancellable
 253                    false, // do not drain SATBs, already drained
 254                    _count_live,
 255                    _unload_classes,
 256                    _update_refs,
 257                    _dedup_string);
 258 
 259     assert(_cm->task_queues()->is_empty(), "Should be empty");
 260   }
 261 };
 262 
 263 void ShenandoahConcurrentMark::mark_roots(ShenandoahPhaseTimings::Phase root_phase) {
 264   assert(Thread::current()->is_VM_thread(), "can only do this in VMThread");
 265   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 266 
 267   ShenandoahHeap* heap = ShenandoahHeap::heap();
 268 
 269   ShenandoahGCPhase phase(root_phase);
 270 
 271   WorkGang* workers = heap->workers();
 272   uint nworkers = workers->active_workers();
 273 
 274   assert(nworkers <= task_queues()->size(), "Just check");
 275 
 276   ShenandoahRootProcessor root_proc(heap, nworkers, root_phase);
 277   TASKQUEUE_STATS_ONLY(reset_taskqueue_stats());
 278   task_queues()->reserve(nworkers);
 279 
 280   if (heap->has_forwarded_objects()) {
 281     ShenandoahInitMarkRootsTask<RESOLVE> mark_roots(&root_proc, process_references());
 282     workers->run_task(&mark_roots);
 283   } else {
 284     // No need to update references, which means the heap is stable.
 285     // Can save time not walking through forwarding pointers.
 286     ShenandoahInitMarkRootsTask<NONE> mark_roots(&root_proc, process_references());
 287     workers->run_task(&mark_roots);
 288   }
 289 
 290   if (ShenandoahConcurrentScanCodeRoots) {
 291     clear_claim_codecache();
 292   }
 293 }
 294 
 295 void ShenandoahConcurrentMark::init_mark_roots() {
 296   assert(Thread::current()->is_VM_thread(), "can only do this in VMThread");
 297   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 298 
 299   mark_roots(ShenandoahPhaseTimings::scan_roots);
 300 }
 301 
 302 void ShenandoahConcurrentMark::update_roots(ShenandoahPhaseTimings::Phase root_phase) {
 303   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 304 
 305   bool update_code_cache = true; // initialize to safer value
 306   switch (root_phase) {
 307     case ShenandoahPhaseTimings::update_roots:
 308     case ShenandoahPhaseTimings::final_update_refs_roots:
 309       // If code cache was evacuated concurrently, we need to update code cache roots.
 310       update_code_cache = ShenandoahConcurrentEvacCodeRoots;
 311       break;
 312     case ShenandoahPhaseTimings::full_gc_roots:
 313     case ShenandoahPhaseTimings::final_partial_gc_work:
 314       update_code_cache = true;
 315       break;
 316     default:
 317       ShouldNotReachHere();
 318   }
 319 
 320   ShenandoahHeap* heap = ShenandoahHeap::heap();
 321 
 322   ShenandoahGCPhase phase(root_phase);
 323 
 324 #if defined(COMPILER2) || INCLUDE_JVMCI
 325   DerivedPointerTable::clear();
 326 #endif
 327 
 328   uint nworkers = heap->workers()->active_workers();
 329 
 330   ShenandoahRootProcessor root_proc(heap, nworkers, root_phase);
 331   ShenandoahUpdateRootsTask update_roots(&root_proc, update_code_cache);
 332   heap->workers()->run_task(&update_roots);
 333 
 334 #if defined(COMPILER2) || INCLUDE_JVMCI
 335   DerivedPointerTable::update_pointers();
 336 #endif
 337 }
 338 
 339 void ShenandoahConcurrentMark::initialize(uint workers) {
 340   _heap = ShenandoahHeap::heap();
 341 
 342   uint num_queues = MAX2(workers, 1U);
 343 
 344   _task_queues = new ShenandoahObjToScanQueueSet((int) num_queues);
 345 
 346   for (uint i = 0; i < num_queues; ++i) {
 347     ShenandoahObjToScanQueue* task_queue = new ShenandoahObjToScanQueue();
 348     task_queue->initialize();
 349     _task_queues->register_queue(i, task_queue);
 350   }
 351 
 352   ShenandoahBarrierSet::satb_mark_queue_set().set_buffer_size(ShenandoahSATBBufferSize);
 353 
 354   size_t num_regions = ShenandoahHeap::heap()->num_regions();
 355   _liveness_local = NEW_C_HEAP_ARRAY(jushort*, workers, mtGC);
 356   for (uint worker = 0; worker < workers; worker++) {
 357      _liveness_local[worker] = NEW_C_HEAP_ARRAY(jushort, num_regions, mtGC);
 358   }
 359 }
 360 
 361 void ShenandoahConcurrentMark::concurrent_scan_code_roots(uint worker_id, ReferenceProcessor* rp, bool update_refs) {
 362   if (ShenandoahConcurrentScanCodeRoots && claim_codecache()) {
 363     ShenandoahObjToScanQueue* q = task_queues()->queue(worker_id);
 364     if (!unload_classes()) {
 365       MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 366       if (update_refs) {
 367         ShenandoahMarkResolveRefsClosure cl(q, rp);
 368         CodeBlobToOopClosure blobs(&cl, !CodeBlobToOopClosure::FixRelocations);
 369         CodeCache::blobs_do(&blobs);
 370       } else {
 371         ShenandoahMarkRefsClosure cl(q, rp);
 372         CodeBlobToOopClosure blobs(&cl, !CodeBlobToOopClosure::FixRelocations);
 373         CodeCache::blobs_do(&blobs);
 374       }
 375     }
 376   }
 377 }
 378 
 379 void ShenandoahConcurrentMark::mark_from_roots() {
 380   ShenandoahHeap* sh = ShenandoahHeap::heap();
 381   WorkGang* workers = sh->workers();
 382   uint nworkers = workers->active_workers();
 383 
 384   bool update_refs = sh->has_forwarded_objects();
 385 
 386   ShenandoahGCPhase conc_mark_phase(ShenandoahPhaseTimings::conc_mark);
 387 
 388   if (process_references()) {
 389     ReferenceProcessor* rp = sh->ref_processor();
 390     rp->set_active_mt_degree(nworkers);
 391 
 392     // enable ("weak") refs discovery
 393     rp->enable_discovery(true /*verify_no_refs*/);
 394     rp->setup_policy(sh->is_full_gc_in_progress()); // snapshot the soft ref policy to be used in this cycle
 395   }
 396 
 397   shenandoah_assert_rp_isalive_not_installed();
 398   ReferenceProcessorIsAliveMutator fix_isalive(sh->ref_processor(), sh->is_alive_closure());
 399 
 400   task_queues()->reserve(nworkers);
 401 
 402   if (UseShenandoahOWST) {
 403     ShenandoahTaskTerminator terminator(nworkers, task_queues());
 404     ShenandoahConcurrentMarkingTask markingTask = ShenandoahConcurrentMarkingTask(this, &terminator, update_refs);
 405     workers->run_task(&markingTask);
 406   } else {
 407     ParallelTaskTerminator terminator(nworkers, task_queues());
 408     ShenandoahConcurrentMarkingTask markingTask = ShenandoahConcurrentMarkingTask(this, &terminator, update_refs);
 409     workers->run_task(&markingTask);
 410   }
 411 
 412   assert(task_queues()->is_empty() || sh->cancelled_concgc(), "Should be empty when not cancelled");
 413   if (! sh->cancelled_concgc()) {
 414     TASKQUEUE_STATS_ONLY(print_taskqueue_stats());
 415   }
 416 
 417   TASKQUEUE_STATS_ONLY(reset_taskqueue_stats());
 418 }
 419 
 420 void ShenandoahConcurrentMark::finish_mark_from_roots() {
 421   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 422 
 423   ShenandoahHeap* sh = ShenandoahHeap::heap();
 424 
 425   TASKQUEUE_STATS_ONLY(reset_taskqueue_stats());
 426 
 427   shared_finish_mark_from_roots(/* full_gc = */ false);
 428 
 429   if (sh->has_forwarded_objects()) {
 430     update_roots(ShenandoahPhaseTimings::update_roots);
 431   }
 432 
 433   TASKQUEUE_STATS_ONLY(print_taskqueue_stats());
 434 }
 435 
 436 void ShenandoahConcurrentMark::shared_finish_mark_from_roots(bool full_gc) {
 437   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 438 
 439   ShenandoahHeap* sh = ShenandoahHeap::heap();
 440 
 441   uint nworkers = sh->workers()->active_workers();
 442 
 443   // Finally mark everything else we've got in our queues during the previous steps.
 444   // It does two different things for concurrent vs. mark-compact GC:
 445   // - For concurrent GC, it starts with empty task queues, drains the remaining
 446   //   SATB buffers, and then completes the marking closure.
 447   // - For mark-compact GC, it starts out with the task queues seeded by initial
 448   //   root scan, and completes the closure, thus marking through all live objects
 449   // The implementation is the same, so it's shared here.
 450   {
 451     ShenandoahGCPhase phase(full_gc ?
 452                                ShenandoahPhaseTimings::full_gc_mark_finish_queues :
 453                                ShenandoahPhaseTimings::finish_queues);
 454     bool count_live = !(ShenandoahNoLivenessFullGC && full_gc); // we do not need liveness data for full GC
 455     task_queues()->reserve(nworkers);
 456 
 457     shenandoah_assert_rp_isalive_not_installed();
 458     ReferenceProcessorIsAliveMutator fix_isalive(sh->ref_processor(), sh->is_alive_closure());
 459 
 460     StrongRootsScope scope(nworkers);
 461     if (UseShenandoahOWST) {
 462       ShenandoahTaskTerminator terminator(nworkers, task_queues());
 463       ShenandoahFinalMarkingTask task(this, &terminator, sh->has_forwarded_objects(), count_live,
 464         unload_classes(), full_gc && ShenandoahStringDedup::is_enabled());
 465       sh->workers()->run_task(&task);
 466     } else {
 467       ParallelTaskTerminator terminator(nworkers, task_queues());
 468       ShenandoahFinalMarkingTask task(this, &terminator, sh->has_forwarded_objects(), count_live,
 469         unload_classes(), full_gc && ShenandoahStringDedup::is_enabled());
 470       sh->workers()->run_task(&task);
 471     }
 472   }
 473 
 474   assert(task_queues()->is_empty(), "Should be empty");
 475 
 476   // When we're done marking everything, we process weak references.
 477   if (process_references()) {
 478     weak_refs_work(full_gc);
 479   }
 480 
 481   // And finally finish class unloading
 482   if (unload_classes()) {
 483     sh->unload_classes_and_cleanup_tables(full_gc);
 484   }
 485 
 486   assert(task_queues()->is_empty(), "Should be empty");
 487 
 488 }
 489 
 490 class ShenandoahSATBThreadsClosure : public ThreadClosure {
 491   ShenandoahSATBBufferClosure* _satb_cl;
 492   int _thread_parity;
 493 
 494  public:
 495   ShenandoahSATBThreadsClosure(ShenandoahSATBBufferClosure* satb_cl) :
 496     _satb_cl(satb_cl),
 497     _thread_parity(Threads::thread_claim_parity()) {}
 498 
 499   void do_thread(Thread* thread) {
 500     if (thread->is_Java_thread()) {
 501       if (thread->claim_oops_do(true, _thread_parity)) {
 502         JavaThread* jt = (JavaThread*)thread;
 503         ShenandoahThreadLocalData::satb_mark_queue(jt).apply_closure_and_empty(_satb_cl);
 504       }
 505     } else if (thread->is_VM_thread()) {
 506       if (thread->claim_oops_do(true, _thread_parity)) {
 507         ShenandoahBarrierSet::satb_mark_queue_set().shared_satb_queue()->apply_closure_and_empty(_satb_cl);
 508       }
 509     }
 510   }
 511 };
 512 
 513 void ShenandoahConcurrentMark::drain_satb_buffers(uint worker_id, bool remark) {
 514   ShenandoahObjToScanQueue* q = get_queue(worker_id);
 515   ShenandoahSATBBufferClosure cl(q);
 516 
 517   SATBMarkQueueSet& satb_mq_set = ShenandoahBarrierSet::satb_mark_queue_set();
 518   while (satb_mq_set.apply_closure_to_completed_buffer(&cl));
 519 
 520   if (remark) {
 521     ShenandoahSATBThreadsClosure tc(&cl);
 522     Threads::threads_do(&tc);
 523   }
 524 }
 525 
 526 #if TASKQUEUE_STATS
 527 void ShenandoahConcurrentMark::print_taskqueue_stats_hdr(outputStream* const st) {
 528   st->print_raw_cr("GC Task Stats");
 529   st->print_raw("thr "); TaskQueueStats::print_header(1, st); st->cr();
 530   st->print_raw("--- "); TaskQueueStats::print_header(2, st); st->cr();
 531 }
 532 
 533 void ShenandoahConcurrentMark::print_taskqueue_stats() const {
 534   if (!log_develop_is_enabled(Trace, gc, task, stats)) {
 535     return;
 536   }
 537   Log(gc, task, stats) log;
 538   ResourceMark rm;
 539   LogStream ls(log.trace());
 540   outputStream* st = &ls;
 541   print_taskqueue_stats_hdr(st);
 542 
 543   TaskQueueStats totals;
 544   const uint n = _task_queues->size();
 545   for (uint i = 0; i < n; ++i) {
 546     st->print(UINT32_FORMAT_W(3), i);
 547     _task_queues->queue(i)->stats.print(st);
 548     st->cr();
 549     totals += _task_queues->queue(i)->stats;
 550   }
 551   st->print("tot "); totals.print(st); st->cr();
 552   DEBUG_ONLY(totals.verify());
 553 
 554 }
 555 
 556 void ShenandoahConcurrentMark::reset_taskqueue_stats() {
 557   const uint n = task_queues()->size();
 558   for (uint i = 0; i < n; ++i) {
 559     task_queues()->queue(i)->stats.reset();
 560   }
 561 }
 562 #endif // TASKQUEUE_STATS
 563 
 564 // Weak Reference Closures
 565 class ShenandoahCMDrainMarkingStackClosure: public VoidClosure {
 566   uint _worker_id;
 567   ParallelTaskTerminator* _terminator;
 568   bool _reset_terminator;
 569 
 570 public:
 571   ShenandoahCMDrainMarkingStackClosure(uint worker_id, ParallelTaskTerminator* t, bool reset_terminator = false):
 572     _worker_id(worker_id),
 573     _terminator(t),
 574     _reset_terminator(reset_terminator) {
 575   }
 576 
 577   void do_void() {
 578     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 579 
 580     ShenandoahHeap* sh = ShenandoahHeap::heap();
 581     ShenandoahConcurrentMark* scm = sh->concurrentMark();
 582     assert(scm->process_references(), "why else would we be here?");
 583     ReferenceProcessor* rp = sh->ref_processor();
 584 
 585     shenandoah_assert_rp_isalive_installed();
 586 
 587     scm->mark_loop(_worker_id, _terminator, rp,
 588                    false, // not cancellable
 589                    false, // do not drain SATBs
 590                    true,  // count liveness
 591                    scm->unload_classes(),
 592                    sh->has_forwarded_objects());
 593 
 594     if (_reset_terminator) {
 595       _terminator->reset_for_reuse();
 596     }
 597   }
 598 };
 599 
 600 
 601 class ShenandoahCMKeepAliveClosure : public OopClosure {
 602 private:
 603   ShenandoahObjToScanQueue* _queue;
 604   ShenandoahHeap* _heap;
 605 
 606   template <class T>
 607   inline void do_oop_nv(T* p) {
 608     ShenandoahConcurrentMark::mark_through_ref<T, NONE, false /* string dedup */>(p, _heap, _queue);
 609   }
 610 
 611 public:
 612   ShenandoahCMKeepAliveClosure(ShenandoahObjToScanQueue* q) :
 613     _queue(q), _heap(ShenandoahHeap::heap()) {}
 614 
 615   void do_oop(narrowOop* p) { do_oop_nv(p); }
 616   void do_oop(oop* p)       { do_oop_nv(p); }
 617 };
 618 
 619 class ShenandoahCMKeepAliveUpdateClosure : public OopClosure {
 620 private:
 621   ShenandoahObjToScanQueue* _queue;
 622   ShenandoahHeap* _heap;
 623 
 624   template <class T>
 625   inline void do_oop_nv(T* p) {
 626     ShenandoahConcurrentMark::mark_through_ref<T, SIMPLE, false /* string dedup */>(p, _heap, _queue);
 627   }
 628 
 629 public:
 630   ShenandoahCMKeepAliveUpdateClosure(ShenandoahObjToScanQueue* q) :
 631     _queue(q), _heap(ShenandoahHeap::heap()) {}
 632 
 633   void do_oop(narrowOop* p) { do_oop_nv(p); }
 634   void do_oop(oop* p)       { do_oop_nv(p); }
 635 };
 636 
 637 class ShenandoahRefProcTaskProxy : public AbstractGangTask {
 638 
 639 private:
 640   AbstractRefProcTaskExecutor::ProcessTask& _proc_task;
 641   ParallelTaskTerminator* _terminator;
 642 public:
 643 
 644   ShenandoahRefProcTaskProxy(AbstractRefProcTaskExecutor::ProcessTask& proc_task,
 645                              ParallelTaskTerminator* t) :
 646     AbstractGangTask("Process reference objects in parallel"),
 647     _proc_task(proc_task),
 648     _terminator(t) {
 649   }
 650 
 651   void work(uint worker_id) {
 652     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 653     ShenandoahHeap* heap = ShenandoahHeap::heap();
 654     ShenandoahCMDrainMarkingStackClosure complete_gc(worker_id, _terminator);
 655     if (heap->has_forwarded_objects()) {
 656       ShenandoahForwardedIsAliveClosure is_alive;
 657       ShenandoahCMKeepAliveUpdateClosure keep_alive(heap->concurrentMark()->get_queue(worker_id));
 658       _proc_task.work(worker_id, is_alive, keep_alive, complete_gc);
 659     } else {
 660       ShenandoahIsAliveClosure is_alive;
 661       ShenandoahCMKeepAliveClosure keep_alive(heap->concurrentMark()->get_queue(worker_id));
 662       _proc_task.work(worker_id, is_alive, keep_alive, complete_gc);
 663     }
 664   }
 665 };
 666 
 667 class ShenandoahRefEnqueueTaskProxy : public AbstractGangTask {
 668 
 669 private:
 670   AbstractRefProcTaskExecutor::EnqueueTask& _enqueue_task;
 671 
 672 public:
 673 
 674   ShenandoahRefEnqueueTaskProxy(AbstractRefProcTaskExecutor::EnqueueTask& enqueue_task) :
 675     AbstractGangTask("Enqueue reference objects in parallel"),
 676     _enqueue_task(enqueue_task) {
 677   }
 678 
 679   void work(uint worker_id) {
 680     _enqueue_task.work(worker_id);
 681   }
 682 };
 683 
 684 class ShenandoahRefProcTaskExecutor : public AbstractRefProcTaskExecutor {
 685 
 686 private:
 687   WorkGang* _workers;
 688 
 689 public:
 690 
 691   ShenandoahRefProcTaskExecutor(WorkGang* workers) :
 692     _workers(workers) {
 693   }
 694 
 695   // Executes a task using worker threads.
 696   void execute(ProcessTask& task) {
 697     assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at a safepoint");
 698 
 699     // Shortcut execution if task is empty.
 700     // This should be replaced with the generic ReferenceProcessor shortcut,
 701     // see JDK-8181214, JDK-8043575, JDK-6938732.
 702     if (task.is_empty()) {
 703       return;
 704     }
 705 
 706     ShenandoahHeap* heap = ShenandoahHeap::heap();
 707     ShenandoahConcurrentMark* cm = heap->concurrentMark();
 708     uint nworkers = _workers->active_workers();
 709     cm->task_queues()->reserve(nworkers);
 710     if (UseShenandoahOWST) {
 711       ShenandoahTaskTerminator terminator(nworkers, cm->task_queues());
 712       ShenandoahRefProcTaskProxy proc_task_proxy(task, &terminator);
 713       _workers->run_task(&proc_task_proxy);
 714     } else {
 715       ParallelTaskTerminator terminator(nworkers, cm->task_queues());
 716       ShenandoahRefProcTaskProxy proc_task_proxy(task, &terminator);
 717       _workers->run_task(&proc_task_proxy);
 718     }
 719   }
 720 
 721   void execute(EnqueueTask& task) {
 722     ShenandoahRefEnqueueTaskProxy enqueue_task_proxy(task);
 723     _workers->run_task(&enqueue_task_proxy);
 724   }
 725 };
 726 
 727 
 728 void ShenandoahConcurrentMark::weak_refs_work(bool full_gc) {
 729   assert(process_references(), "sanity");
 730 
 731   ShenandoahHeap* sh = ShenandoahHeap::heap();
 732 
 733   ShenandoahPhaseTimings::Phase phase_root =
 734           full_gc ?
 735           ShenandoahPhaseTimings::full_gc_weakrefs :
 736           ShenandoahPhaseTimings::weakrefs;
 737 
 738   ShenandoahGCPhase phase(phase_root);
 739 
 740   ReferenceProcessor* rp = sh->ref_processor();
 741 
 742   // NOTE: We cannot shortcut on has_discovered_references() here, because
 743   // we will miss marking JNI Weak refs then, see implementation in
 744   // ReferenceProcessor::process_discovered_references.
 745   weak_refs_work_doit(full_gc);
 746 
 747   rp->verify_no_references_recorded();
 748   assert(!rp->discovery_enabled(), "Post condition");
 749 
 750 }
 751 
 752 void ShenandoahConcurrentMark::weak_refs_work_doit(bool full_gc) {
 753   ShenandoahHeap* sh = ShenandoahHeap::heap();
 754 
 755   assert(!sh->is_concurrent_partial_in_progress(), "cannot process weakrefs during conc-partial yet");
 756 
 757   ReferenceProcessor* rp = sh->ref_processor();
 758 
 759   ShenandoahPhaseTimings::Phase phase_process =
 760           full_gc ?
 761           ShenandoahPhaseTimings::full_gc_weakrefs_process :
 762           ShenandoahPhaseTimings::weakrefs_process;
 763 
 764   ShenandoahPhaseTimings::Phase phase_enqueue =
 765           full_gc ?
 766           ShenandoahPhaseTimings::full_gc_weakrefs_enqueue :
 767           ShenandoahPhaseTimings::weakrefs_enqueue;
 768 
 769   shenandoah_assert_rp_isalive_not_installed();
 770   ReferenceProcessorIsAliveMutator fix_isalive(rp, sh->is_alive_closure());
 771 
 772   WorkGang* workers = sh->workers();
 773   uint nworkers = workers->active_workers();
 774 
 775   // Setup collector policy for softref cleaning.
 776   bool clear_soft_refs = sh->soft_ref_policy()->use_should_clear_all_soft_refs(true /* bogus arg*/);
 777   log_develop_debug(gc, ref)("clearing soft refs: %s", BOOL_TO_STR(clear_soft_refs));
 778   rp->setup_policy(clear_soft_refs);
 779   rp->set_active_mt_degree(nworkers);
 780 
 781   assert(task_queues()->is_empty(), "Should be empty");
 782 
 783   // complete_gc and keep_alive closures instantiated here are only needed for
 784   // single-threaded path in RP. They share the queue 0 for tracking work, which
 785   // simplifies implementation. Since RP may decide to call complete_gc several
 786   // times, we need to be able to reuse the terminator.
 787   uint serial_worker_id = 0;
 788   ParallelTaskTerminator terminator(1, task_queues());
 789   ShenandoahCMDrainMarkingStackClosure complete_gc(serial_worker_id, &terminator, /* reset_terminator = */ true);
 790 
 791   ShenandoahRefProcTaskExecutor executor(workers);
 792 
 793   ReferenceProcessorPhaseTimes pt(sh->gc_timer(), rp->num_q());
 794 
 795   {
 796     ShenandoahGCPhase phase(phase_process);
 797 
 798     if (sh->has_forwarded_objects()) {
 799       ShenandoahForwardedIsAliveClosure is_alive;
 800       ShenandoahCMKeepAliveUpdateClosure keep_alive(get_queue(serial_worker_id));
 801       rp->process_discovered_references(&is_alive, &keep_alive,
 802                                         &complete_gc, &executor,
 803                                         &pt);
 804 
 805       WeakProcessor::weak_oops_do(&is_alive, &keep_alive);
 806     } else {
 807       ShenandoahIsAliveClosure is_alive;
 808       ShenandoahCMKeepAliveClosure keep_alive(get_queue(serial_worker_id));
 809       rp->process_discovered_references(&is_alive, &keep_alive,
 810                                         &complete_gc, &executor,
 811                                         &pt);
 812 
 813       WeakProcessor::weak_oops_do(&is_alive, &keep_alive);
 814     }
 815     pt.print_all_references();
 816 
 817     assert(task_queues()->is_empty(), "Should be empty");
 818   }
 819 
 820   {
 821     ShenandoahGCPhase phase(phase_enqueue);
 822     rp->enqueue_discovered_references(&executor, &pt);
 823     pt.print_enqueue_phase();
 824   }
 825 }
 826 
 827 class ShenandoahCancelledGCYieldClosure : public YieldClosure {
 828 private:
 829   ShenandoahHeap* const _heap;
 830 public:
 831   ShenandoahCancelledGCYieldClosure() : _heap(ShenandoahHeap::heap()) {};
 832   virtual bool should_return() { return _heap->cancelled_concgc(); }
 833 };
 834 
 835 class ShenandoahPrecleanCompleteGCClosure : public VoidClosure {
 836 public:
 837   void do_void() {
 838     ShenandoahHeap* sh = ShenandoahHeap::heap();
 839     ShenandoahConcurrentMark* scm = sh->concurrentMark();
 840     assert(scm->process_references(), "why else would we be here?");
 841     ParallelTaskTerminator terminator(1, scm->task_queues());
 842 
 843     ReferenceProcessor* rp = sh->ref_processor();
 844     shenandoah_assert_rp_isalive_installed();
 845 
 846     scm->mark_loop(0, &terminator, rp,
 847                    false, // not cancellable
 848                    true,  // drain SATBs
 849                    true,  // count liveness
 850                    scm->unload_classes(),
 851                    sh->has_forwarded_objects());
 852   }
 853 };
 854 
 855 class ShenandoahPrecleanKeepAliveUpdateClosure : public OopClosure {
 856 private:
 857   ShenandoahObjToScanQueue* _queue;
 858   ShenandoahHeap* _heap;
 859 
 860   template <class T>
 861   inline void do_oop_nv(T* p) {
 862     ShenandoahConcurrentMark::mark_through_ref<T, CONCURRENT, false /* string dedup */>(p, _heap, _queue);
 863   }
 864 
 865 public:
 866   ShenandoahPrecleanKeepAliveUpdateClosure(ShenandoahObjToScanQueue* q) :
 867     _queue(q), _heap(ShenandoahHeap::heap()) {}
 868 
 869   void do_oop(narrowOop* p) { do_oop_nv(p); }
 870   void do_oop(oop* p)       { do_oop_nv(p); }
 871 };
 872 
 873 void ShenandoahConcurrentMark::preclean_weak_refs() {
 874   // Pre-cleaning weak references before diving into STW makes sense at the
 875   // end of concurrent mark. This will filter out the references which referents
 876   // are alive. Note that ReferenceProcessor already filters out these on reference
 877   // discovery, and the bulk of work is done here. This phase processes leftovers
 878   // that missed the initial filtering, i.e. when referent was marked alive after
 879   // reference was discovered by RP.
 880 
 881   assert(process_references(), "sanity");
 882 
 883   ShenandoahHeap* sh = ShenandoahHeap::heap();
 884   ReferenceProcessor* rp = sh->ref_processor();
 885 
 886   // Shortcut if no references were discovered to avoid winding up threads.
 887   if (!rp->has_discovered_references()) {
 888     return;
 889   }
 890 
 891   ReferenceProcessorMTDiscoveryMutator fix_mt_discovery(rp, false);
 892 
 893   shenandoah_assert_rp_isalive_not_installed();
 894   ReferenceProcessorIsAliveMutator fix_isalive(rp, sh->is_alive_closure());
 895 
 896   // Interrupt on cancelled GC
 897   ShenandoahCancelledGCYieldClosure yield;
 898 
 899   assert(task_queues()->is_empty(), "Should be empty");
 900 
 901   ShenandoahPrecleanCompleteGCClosure complete_gc;
 902   if (sh->has_forwarded_objects()) {
 903     ShenandoahForwardedIsAliveClosure is_alive;
 904     ShenandoahPrecleanKeepAliveUpdateClosure keep_alive(get_queue(0));
 905     ResourceMark rm;
 906     rp->preclean_discovered_references(&is_alive, &keep_alive,
 907                                        &complete_gc, &yield,
 908                                        NULL);
 909   } else {
 910     ShenandoahIsAliveClosure is_alive;
 911     ShenandoahCMKeepAliveClosure keep_alive(get_queue(0));
 912     ResourceMark rm;
 913     rp->preclean_discovered_references(&is_alive, &keep_alive,
 914                                        &complete_gc, &yield,
 915                                        NULL);
 916   }
 917 
 918   assert(task_queues()->is_empty(), "Should be empty");
 919 }
 920 
 921 void ShenandoahConcurrentMark::cancel() {
 922   // Clean up marking stacks.
 923   ShenandoahObjToScanQueueSet* queues = task_queues();
 924   queues->clear();
 925 
 926   // Cancel SATB buffers.
 927   ShenandoahBarrierSet::satb_mark_queue_set().abandon_partial_marking();
 928 }
 929 
 930 ShenandoahObjToScanQueue* ShenandoahConcurrentMark::get_queue(uint worker_id) {
 931   assert(task_queues()->get_reserved() > worker_id, "No reserved queue for worker id: %d", worker_id);
 932   return _task_queues->queue(worker_id);
 933 }
 934 
 935 void ShenandoahConcurrentMark::clear_queue(ShenandoahObjToScanQueue *q) {
 936   q->set_empty();
 937   q->overflow_stack()->clear();
 938   q->clear_buffer();
 939 }
 940 
 941 template <bool CANCELLABLE, bool DRAIN_SATB, bool COUNT_LIVENESS>
 942 void ShenandoahConcurrentMark::mark_loop_prework(uint w, ParallelTaskTerminator *t, ReferenceProcessor *rp, bool class_unload, bool update_refs, bool strdedup) {
 943   ShenandoahObjToScanQueue* q = get_queue(w);
 944 
 945   jushort* ld;
 946   if (COUNT_LIVENESS) {
 947     ld = get_liveness(w);
 948     Copy::fill_to_bytes(ld, _heap->num_regions() * sizeof(jushort));
 949   } else {
 950     ld = NULL;
 951   }
 952 
 953   // TODO: We can clean up this if we figure out how to do templated oop closures that
 954   // play nice with specialized_oop_iterators.
 955   if (class_unload) {
 956     if (update_refs) {
 957       if (strdedup) {
 958         ShenandoahStrDedupQueue* dq = ShenandoahStringDedup::queue(w);
 959         ShenandoahMarkUpdateRefsMetadataDedupClosure cl(q, dq, rp);
 960         mark_loop_work<ShenandoahMarkUpdateRefsMetadataDedupClosure, CANCELLABLE, DRAIN_SATB, COUNT_LIVENESS>(&cl, ld, w, t);
 961       } else {
 962         ShenandoahMarkUpdateRefsMetadataClosure cl(q, rp);
 963         mark_loop_work<ShenandoahMarkUpdateRefsMetadataClosure, CANCELLABLE, DRAIN_SATB, COUNT_LIVENESS>(&cl, ld, w, t);
 964       }
 965     } else {
 966       if (strdedup) {
 967         ShenandoahStrDedupQueue* dq = ShenandoahStringDedup::queue(w);
 968         ShenandoahMarkRefsMetadataDedupClosure cl(q, dq, rp);
 969         mark_loop_work<ShenandoahMarkRefsMetadataDedupClosure, CANCELLABLE, DRAIN_SATB, COUNT_LIVENESS>(&cl, ld, w, t);
 970       } else {
 971         ShenandoahMarkRefsMetadataClosure cl(q, rp);
 972         mark_loop_work<ShenandoahMarkRefsMetadataClosure, CANCELLABLE, DRAIN_SATB, COUNT_LIVENESS>(&cl, ld, w, t);
 973       }
 974     }
 975   } else {
 976     if (update_refs) {
 977       if (strdedup) {
 978         ShenandoahStrDedupQueue* dq = ShenandoahStringDedup::queue(w);
 979         ShenandoahMarkUpdateRefsDedupClosure cl(q, dq, rp);
 980         mark_loop_work<ShenandoahMarkUpdateRefsDedupClosure, CANCELLABLE, DRAIN_SATB, COUNT_LIVENESS>(&cl, ld, w, t);
 981       } else {
 982         ShenandoahMarkUpdateRefsClosure cl(q, rp);
 983         mark_loop_work<ShenandoahMarkUpdateRefsClosure, CANCELLABLE, DRAIN_SATB, COUNT_LIVENESS>(&cl, ld, w, t);
 984       }
 985     } else {
 986       if (strdedup) {
 987         ShenandoahStrDedupQueue* dq = ShenandoahStringDedup::queue(w);
 988         ShenandoahMarkRefsDedupClosure cl(q, dq, rp);
 989         mark_loop_work<ShenandoahMarkRefsDedupClosure, CANCELLABLE, DRAIN_SATB, COUNT_LIVENESS>(&cl, ld, w, t);
 990       } else {
 991         ShenandoahMarkRefsClosure cl(q, rp);
 992         mark_loop_work<ShenandoahMarkRefsClosure, CANCELLABLE, DRAIN_SATB, COUNT_LIVENESS>(&cl, ld, w, t);
 993       }
 994     }
 995   }
 996   if (COUNT_LIVENESS) {
 997     for (uint i = 0; i < _heap->num_regions(); i++) {
 998       ShenandoahHeapRegion* r = _heap->get_region(i);
 999       jushort live = ld[i];
1000       if (live > 0) {
1001         r->increase_live_data_gc_words(live);
1002       }
1003     }
1004   }
1005 }
1006 
1007 template <class T, bool CANCELLABLE, bool DRAIN_SATB, bool COUNT_LIVENESS>
1008 void ShenandoahConcurrentMark::mark_loop_work(T* cl, jushort* live_data, uint worker_id, ParallelTaskTerminator *terminator) {
1009   int seed = 17;
1010   uintx stride = CANCELLABLE ? ShenandoahMarkLoopStride : 1;
1011 
1012   ShenandoahHeap* heap = ShenandoahHeap::heap();
1013   ShenandoahObjToScanQueueSet* queues = task_queues();
1014   ShenandoahObjToScanQueue* q;
1015   ShenandoahMarkTask t;
1016 
1017   /*
1018    * Process outstanding queues, if any.
1019    *
1020    * There can be more queues than workers. To deal with the imbalance, we claim
1021    * extra queues first. Since marking can push new tasks into the queue associated
1022    * with this worker id, we come back to process this queue in the normal loop.
1023    */
1024   assert(queues->get_reserved() == heap->workers()->active_workers(),
1025     "Need to reserve proper number of queues");
1026 
1027   q = queues->claim_next();
1028   while (q != NULL) {
1029     if (CANCELLABLE && heap->check_cancelled_concgc_and_yield()) {
1030       ShenandoahCancelledTerminatorTerminator tt;
1031       while (!terminator->offer_termination(&tt));
1032       return;
1033     }
1034 
1035     for (uint i = 0; i < stride; i++) {
1036       if (try_queue(q, t)) {
1037         do_task<T, COUNT_LIVENESS>(q, cl, live_data, &t);
1038       } else {
1039         assert(q->is_empty(), "Must be empty");
1040         q = queues->claim_next();
1041         break;
1042       }
1043     }
1044   }
1045   q = get_queue(worker_id);
1046 
1047   /*
1048    * Normal marking loop:
1049    */
1050   while (true) {
1051     if (CANCELLABLE && heap->check_cancelled_concgc_and_yield()) {
1052       ShenandoahCancelledTerminatorTerminator tt;
1053       while (!terminator->offer_termination(&tt));
1054       return;
1055     }
1056 
1057     for (uint i = 0; i < stride; i++) {
1058       if (try_queue(q, t) ||
1059               (DRAIN_SATB && try_draining_satb_buffer(q, t)) ||
1060               queues->steal(worker_id, &seed, t)) {
1061         do_task<T, COUNT_LIVENESS>(q, cl, live_data, &t);
1062       } else {
1063         // Need to leave the STS here otherwise it might block safepoints.
1064         SuspendibleThreadSetLeaver stsl(CANCELLABLE && ShenandoahSuspendibleWorkers);
1065         if (terminator->offer_termination()) return;
1066       }
1067     }
1068   }
1069 }
1070 
1071 bool ShenandoahConcurrentMark::process_references() const {
1072   return _heap->process_references();
1073 }
1074 
1075 bool ShenandoahConcurrentMark::unload_classes() const {
1076   return _heap->unload_classes();
1077 }
1078 
1079 bool ShenandoahConcurrentMark::claim_codecache() {
1080   assert(ShenandoahConcurrentScanCodeRoots, "must not be called otherwise");
1081   return _claimed_codecache.try_set();
1082 }
1083 
1084 void ShenandoahConcurrentMark::clear_claim_codecache() {
1085   assert(ShenandoahConcurrentScanCodeRoots, "must not be called otherwise");
1086   _claimed_codecache.unset();
1087 }
1088 
1089 jushort* ShenandoahConcurrentMark::get_liveness(uint worker_id) {
1090   return _liveness_local[worker_id];
1091 }
1092 
1093 // Generate Shenandoah specialized oop_oop_iterate functions.
1094 SPECIALIZED_OOP_OOP_ITERATE_CLOSURES_SHENANDOAH(ALL_KLASS_OOP_OOP_ITERATE_DEFN)