1 /*
   2  * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_GC_SHARED_REFERENCEPROCESSOR_HPP
  26 #define SHARE_VM_GC_SHARED_REFERENCEPROCESSOR_HPP
  27 
  28 #include "gc/shared/referencePolicy.hpp"
  29 #include "gc/shared/referenceProcessorPhaseTimes.hpp"
  30 #include "gc/shared/referenceProcessorStats.hpp"
  31 #include "memory/referenceType.hpp"
  32 #include "oops/instanceRefKlass.hpp"
  33 
  34 class GCTimer;
  35 
  36 // ReferenceProcessor class encapsulates the per-"collector" processing
  37 // of java.lang.Reference objects for GC. The interface is useful for supporting
  38 // a generational abstraction, in particular when there are multiple
  39 // generations that are being independently collected -- possibly
  40 // concurrently and/or incrementally.
  41 // ReferenceProcessor class abstracts away from a generational setting
  42 // by using a closure that determines whether a given reference or referent are
  43 // subject to this ReferenceProcessor's discovery, thus allowing its use in a
  44 // straightforward manner in a general, non-generational, non-contiguous generation
  45 // (or heap) setting.
  46 //
  47 
  48 // forward references
  49 class ReferencePolicy;
  50 class AbstractRefProcTaskExecutor;
  51 
  52 // List of discovered references.
  53 class DiscoveredList {
  54 public:
  55   DiscoveredList() : _len(0), _compressed_head(0), _oop_head(NULL) { }
  56   inline oop head() const;
  57   HeapWord* adr_head() {
  58     return UseCompressedOops ? (HeapWord*)&_compressed_head :
  59                                (HeapWord*)&_oop_head;
  60   }
  61   inline void set_head(oop o);
  62   inline bool is_empty() const;
  63   size_t length()               { return _len; }
  64   void   set_length(size_t len) { _len = len;  }
  65   void   inc_length(size_t inc) { _len += inc; assert(_len > 0, "Error"); }
  66   void   dec_length(size_t dec) { _len -= dec; }
  67 private:
  68   // Set value depending on UseCompressedOops. This could be a template class
  69   // but then we have to fix all the instantiations and declarations that use this class.
  70   oop       _oop_head;
  71   narrowOop _compressed_head;
  72   size_t _len;
  73 };
  74 
  75 // Iterator for the list of discovered references.
  76 class DiscoveredListIterator {
  77 private:
  78   DiscoveredList&    _refs_list;
  79   HeapWord*          _prev_discovered_addr;
  80   oop                _prev_discovered;
  81   oop                _current_discovered;
  82   HeapWord*          _discovered_addr;
  83   oop                _next_discovered;
  84 
  85   HeapWord*          _referent_addr;
  86   oop                _referent;
  87 
  88   OopClosure*        _keep_alive;
  89   BoolObjectClosure* _is_alive;
  90 
  91   DEBUG_ONLY(
  92   oop                _first_seen; // cyclic linked list check
  93   )
  94 
  95   NOT_PRODUCT(
  96   size_t             _processed;
  97   size_t             _removed;
  98   )
  99 
 100 public:
 101   inline DiscoveredListIterator(DiscoveredList&    refs_list,
 102                                 OopClosure*        keep_alive,
 103                                 BoolObjectClosure* is_alive);
 104 
 105   // End Of List.
 106   inline bool has_next() const { return _current_discovered != NULL; }
 107 
 108   // Get oop to the Reference object.
 109   inline oop obj() const { return _current_discovered; }
 110 
 111   // Get oop to the referent object.
 112   inline oop referent() const { return _referent; }
 113 
 114   // Returns true if referent is alive.
 115   inline bool is_referent_alive() const {
 116     return _is_alive->do_object_b(_referent);
 117   }
 118 
 119   // Loads data for the current reference.
 120   // The "allow_null_referent" argument tells us to allow for the possibility
 121   // of a NULL referent in the discovered Reference object. This typically
 122   // happens in the case of concurrent collectors that may have done the
 123   // discovery concurrently, or interleaved, with mutator execution.
 124   void load_ptrs(DEBUG_ONLY(bool allow_null_referent));
 125 
 126   // Move to the next discovered reference.
 127   inline void next() {
 128     _prev_discovered_addr = _discovered_addr;
 129     _prev_discovered = _current_discovered;
 130     move_to_next();
 131   }
 132 
 133   // Remove the current reference from the list
 134   void remove();
 135 
 136   // Make the referent alive.
 137   inline void make_referent_alive() {
 138     if (UseCompressedOops) {
 139       _keep_alive->do_oop((narrowOop*)_referent_addr);
 140     } else {
 141       _keep_alive->do_oop((oop*)_referent_addr);
 142     }
 143   }
 144 
 145   // NULL out referent pointer.
 146   void clear_referent();
 147 
 148   // Statistics
 149   NOT_PRODUCT(
 150   inline size_t processed() const { return _processed; }
 151   inline size_t removed() const   { return _removed; }
 152   )
 153 
 154   inline void move_to_next() {
 155     if (_current_discovered == _next_discovered) {
 156       // End of the list.
 157       _current_discovered = NULL;
 158     } else {
 159       _current_discovered = _next_discovered;
 160     }
 161     assert(_current_discovered != _first_seen, "cyclic ref_list found");
 162     NOT_PRODUCT(_processed++);
 163   }
 164 };
 165 
 166 class ReferenceProcessor : public CHeapObj<mtGC> {
 167   size_t total_count(DiscoveredList lists[]) const;
 168 
 169   // The SoftReference master timestamp clock
 170   static jlong _soft_ref_timestamp_clock;
 171 
 172   BoolObjectClosure* _is_subject_to_discovery; // determines whether a given oop is subject
 173                                                // to this ReferenceProcessor's discovery
 174                                                // (and further processing).
 175 
 176   bool        _discovering_refs;        // true when discovery enabled
 177   bool        _discovery_is_atomic;     // if discovery is atomic wrt
 178                                         // other collectors in configuration
 179   bool        _discovery_is_mt;         // true if reference discovery is MT.
 180 
 181   bool        _enqueuing_is_done;       // true if all weak references enqueued
 182   bool        _processing_is_mt;        // true during phases when
 183                                         // reference processing is MT.
 184   uint        _next_id;                 // round-robin mod _num_queues counter in
 185                                         // support of work distribution
 186 
 187   // For collectors that do not keep GC liveness information
 188   // in the object header, this field holds a closure that
 189   // helps the reference processor determine the reachability
 190   // of an oop. It is currently initialized to NULL for all
 191   // collectors except for CMS and G1.
 192   BoolObjectClosure* _is_alive_non_header;
 193 
 194   // Soft ref clearing policies
 195   // . the default policy
 196   static ReferencePolicy*   _default_soft_ref_policy;
 197   // . the "clear all" policy
 198   static ReferencePolicy*   _always_clear_soft_ref_policy;
 199   // . the current policy below is either one of the above
 200   ReferencePolicy*          _current_soft_ref_policy;
 201 
 202   // The discovered ref lists themselves
 203 
 204   // The active MT'ness degree of the queues below
 205   uint            _num_queues;
 206   // The maximum MT'ness degree of the queues below
 207   uint            _max_num_queues;
 208 
 209   // Master array of discovered oops
 210   DiscoveredList* _discovered_refs;
 211 
 212   // Arrays of lists of oops, one per thread (pointers into master array above)
 213   DiscoveredList* _discoveredSoftRefs;
 214   DiscoveredList* _discoveredWeakRefs;
 215   DiscoveredList* _discoveredFinalRefs;
 216   DiscoveredList* _discoveredPhantomRefs;
 217 
 218  public:
 219   static int number_of_subclasses_of_ref() { return (REF_PHANTOM - REF_OTHER); }
 220 
 221   uint num_queues() const                  { return _num_queues; }
 222   uint max_num_queues() const              { return _max_num_queues; }
 223   void set_active_mt_degree(uint v);
 224 
 225   DiscoveredList* discovered_refs()        { return _discovered_refs; }
 226 
 227   ReferencePolicy* setup_policy(bool always_clear) {
 228     _current_soft_ref_policy = always_clear ?
 229       _always_clear_soft_ref_policy : _default_soft_ref_policy;
 230     _current_soft_ref_policy->setup();   // snapshot the policy threshold
 231     return _current_soft_ref_policy;
 232   }
 233 
 234   // Process references with a certain reachability level.
 235   void process_discovered_reflist(DiscoveredList                refs_lists[],
 236                                   ReferencePolicy*              policy,
 237                                   bool                          clear_referent,
 238                                   BoolObjectClosure*            is_alive,
 239                                   OopClosure*                   keep_alive,
 240                                   VoidClosure*                  complete_gc,
 241                                   AbstractRefProcTaskExecutor*  task_executor,
 242                                   ReferenceProcessorPhaseTimes* phase_times);
 243 
 244   // Work methods used by the method process_discovered_reflist
 245   // Phase1: keep alive all those referents that are otherwise
 246   // dead but which must be kept alive by policy (and their closure).
 247   void process_phase1(DiscoveredList&     refs_list,
 248                       ReferencePolicy*    policy,
 249                       BoolObjectClosure*  is_alive,
 250                       OopClosure*         keep_alive,
 251                       VoidClosure*        complete_gc);
 252   // Phase2: remove all those references whose referents are
 253   // reachable.
 254   void process_phase2(DiscoveredList&    refs_list,
 255                       BoolObjectClosure* is_alive,
 256                       OopClosure*        keep_alive,
 257                       VoidClosure*       complete_gc);
 258   // Work methods in support of process_phase2
 259   void pp2_work(DiscoveredList&    refs_list,
 260                 BoolObjectClosure* is_alive,
 261                 OopClosure*        keep_alive);
 262   void pp2_work_concurrent_discovery(
 263                 DiscoveredList&    refs_list,
 264                 BoolObjectClosure* is_alive,
 265                 OopClosure*        keep_alive,
 266                 VoidClosure*       complete_gc);
 267   // Phase3: process the referents by either clearing them
 268   // or keeping them alive (and their closure), and enqueuing them.
 269   void process_phase3(DiscoveredList&    refs_list,
 270                       bool               clear_referent,
 271                       BoolObjectClosure* is_alive,
 272                       OopClosure*        keep_alive,
 273                       VoidClosure*       complete_gc);
 274 
 275   // Enqueue references with a certain reachability level
 276   void enqueue_discovered_reflist(DiscoveredList& refs_list);
 277 
 278   // "Preclean" all the discovered reference lists
 279   // by removing references with strongly reachable referents.
 280   // The first argument is a predicate on an oop that indicates
 281   // its (strong) reachability and the second is a closure that
 282   // may be used to incrementalize or abort the precleaning process.
 283   // The caller is responsible for taking care of potential
 284   // interference with concurrent operations on these lists
 285   // (or predicates involved) by other threads. Currently
 286   // only used by the CMS collector.
 287   void preclean_discovered_references(BoolObjectClosure* is_alive,
 288                                       OopClosure*        keep_alive,
 289                                       VoidClosure*       complete_gc,
 290                                       YieldClosure*      yield,
 291                                       GCTimer*           gc_timer);
 292 
 293   // Returns the name of the discovered reference list
 294   // occupying the i / _num_queues slot.
 295   const char* list_name(uint i);
 296 
 297   void enqueue_discovered_reflists(AbstractRefProcTaskExecutor* task_executor,
 298                                    ReferenceProcessorPhaseTimes* phase_times);
 299 
 300   // "Preclean" the given discovered reference list
 301   // by removing references with strongly reachable referents.
 302   // Currently used in support of CMS only.
 303   void preclean_discovered_reflist(DiscoveredList&    refs_list,
 304                                    BoolObjectClosure* is_alive,
 305                                    OopClosure*        keep_alive,
 306                                    VoidClosure*       complete_gc,
 307                                    YieldClosure*      yield);
 308 private:
 309   // round-robin mod _num_queues (not: _not_ mod _max_num_queues)
 310   uint next_id() {
 311     uint id = _next_id;
 312     assert(!_discovery_is_mt, "Round robin should only be used in serial discovery");
 313     if (++_next_id == _num_queues) {
 314       _next_id = 0;
 315     }
 316     assert(_next_id < _num_queues, "_next_id %u _num_queues %u _max_num_queues %u", _next_id, _num_queues, _max_num_queues);
 317     return id;
 318   }
 319   DiscoveredList* get_discovered_list(ReferenceType rt);
 320   inline void add_to_discovered_list_mt(DiscoveredList& refs_list, oop obj,
 321                                         HeapWord* discovered_addr);
 322 
 323   void clear_discovered_references(DiscoveredList& refs_list);
 324 
 325   void log_reflist_counts(DiscoveredList ref_lists[], uint active_length, size_t total_count) PRODUCT_RETURN;
 326 
 327   // Balances reference queues.
 328   void balance_queues(DiscoveredList ref_lists[]);
 329 
 330   // Update (advance) the soft ref master clock field.
 331   void update_soft_ref_master_clock();
 332 
 333   template <class T>
 334   bool is_subject_to_discovery(T const obj) const;
 335 public:
 336   // Default parameters give you a vanilla reference processor.
 337   ReferenceProcessor(BoolObjectClosure* is_subject_to_discovery,
 338                      bool mt_processing = false, uint mt_processing_degree = 1,
 339                      bool mt_discovery  = false, uint mt_discovery_degree  = 1,
 340                      bool atomic_discovery = true,
 341                      BoolObjectClosure* is_alive_non_header = NULL);
 342 
 343   // RefDiscoveryPolicy values
 344   enum DiscoveryPolicy {
 345     ReferenceBasedDiscovery = 0,
 346     ReferentBasedDiscovery  = 1,
 347     DiscoveryPolicyMin      = ReferenceBasedDiscovery,
 348     DiscoveryPolicyMax      = ReferentBasedDiscovery
 349   };
 350 
 351   static void init_statics();
 352 
 353  public:
 354   // get and set "is_alive_non_header" field
 355   BoolObjectClosure* is_alive_non_header() {
 356     return _is_alive_non_header;
 357   }
 358   void set_is_alive_non_header(BoolObjectClosure* is_alive_non_header) {
 359     _is_alive_non_header = is_alive_non_header;
 360   }
 361 
 362   BoolObjectClosure* is_subject_to_discovery_closure() const { return _is_subject_to_discovery; }
 363   void set_is_subject_to_discovery_closure(BoolObjectClosure* cl) { _is_subject_to_discovery = cl; }
 364 
 365   // start and stop weak ref discovery
 366   void enable_discovery(bool check_no_refs = true);
 367   void disable_discovery()  { _discovering_refs = false; }
 368   bool discovery_enabled()  { return _discovering_refs;  }
 369 
 370   // whether discovery is atomic wrt other collectors
 371   bool discovery_is_atomic() const { return _discovery_is_atomic; }
 372   void set_atomic_discovery(bool atomic) { _discovery_is_atomic = atomic; }
 373 
 374   // whether discovery is done by multiple threads same-old-timeously
 375   bool discovery_is_mt() const { return _discovery_is_mt; }
 376   void set_mt_discovery(bool mt) { _discovery_is_mt = mt; }
 377 
 378   // Whether we are in a phase when _processing_ is MT.
 379   bool processing_is_mt() const { return _processing_is_mt; }
 380   void set_mt_processing(bool mt) { _processing_is_mt = mt; }
 381 
 382   // whether all enqueueing of weak references is complete
 383   bool enqueuing_is_done()  { return _enqueuing_is_done; }
 384   void set_enqueuing_is_done(bool v) { _enqueuing_is_done = v; }
 385 
 386   // iterate over oops
 387   void weak_oops_do(OopClosure* f);       // weak roots
 388 
 389   // Balance each of the discovered lists.
 390   void balance_all_queues();
 391   void verify_list(DiscoveredList& ref_list);
 392 
 393   // Discover a Reference object, using appropriate discovery criteria
 394   bool discover_reference(oop obj, ReferenceType rt);
 395 
 396   // Has discovered references that need handling
 397   bool has_discovered_references();
 398 
 399   // Process references found during GC (called by the garbage collector)
 400   ReferenceProcessorStats
 401   process_discovered_references(BoolObjectClosure*            is_alive,
 402                                 OopClosure*                   keep_alive,
 403                                 VoidClosure*                  complete_gc,
 404                                 AbstractRefProcTaskExecutor*  task_executor,
 405                                 ReferenceProcessorPhaseTimes* phase_times);
 406 
 407   // Enqueue references at end of GC (called by the garbage collector)
 408   void enqueue_discovered_references(AbstractRefProcTaskExecutor* task_executor,
 409                                      ReferenceProcessorPhaseTimes* phase_times);
 410 
 411   // If a discovery is in process that is being superceded, abandon it: all
 412   // the discovered lists will be empty, and all the objects on them will
 413   // have NULL discovered fields.  Must be called only at a safepoint.
 414   void abandon_partial_discovery();
 415 
 416   size_t total_reference_count(ReferenceType rt) const;
 417 
 418   // debugging
 419   void verify_no_references_recorded() PRODUCT_RETURN;
 420   void verify_referent(oop obj)        PRODUCT_RETURN;
 421 };
 422 
 423 // A reference processor that uses a single memory span to determine the area that
 424 // is subject to discovery. Useful for collectors which have contiguous generations.
 425 class SpanReferenceProcessor : public ReferenceProcessor {
 426   class SpanBasedDiscoverer : public BoolObjectClosure {
 427   public:
 428     MemRegion _span;
 429 
 430     SpanBasedDiscoverer(MemRegion span) : BoolObjectClosure(), _span(span) { }
 431 
 432     virtual bool do_object_b(oop obj) {
 433       return _span.contains(obj);
 434     }
 435   };
 436 
 437   SpanBasedDiscoverer _span_based_discoverer;
 438 public:
 439   SpanReferenceProcessor(MemRegion span,
 440                               bool mt_processing = false, uint mt_processing_degree = 1,
 441                               bool mt_discovery  = false, uint mt_discovery_degree  = 1,
 442                               bool atomic_discovery = true,
 443                               BoolObjectClosure* is_alive_non_header = NULL);
 444 
 445   // get and set span
 446   MemRegion span()                   { return _span_based_discoverer._span; }
 447   void      set_span(MemRegion span) { _span_based_discoverer._span = span; }
 448 };
 449 
 450 // A utility class to disable reference discovery in
 451 // the scope which contains it, for given ReferenceProcessor.
 452 class NoRefDiscovery: StackObj {
 453  private:
 454   ReferenceProcessor* _rp;
 455   bool _was_discovering_refs;
 456  public:
 457   NoRefDiscovery(ReferenceProcessor* rp) : _rp(rp) {
 458     _was_discovering_refs = _rp->discovery_enabled();
 459     if (_was_discovering_refs) {
 460       _rp->disable_discovery();
 461     }
 462   }
 463 
 464   ~NoRefDiscovery() {
 465     if (_was_discovering_refs) {
 466       _rp->enable_discovery(false /*check_no_refs*/);
 467     }
 468   }
 469 };
 470 
 471 // A utility class to temporarily mutate the subject discovery closure of the
 472 // given ReferenceProcessor in the scope that contains it.
 473 class ReferenceProcessorSubjectToDiscoveryMutator : StackObj {
 474  private:
 475   ReferenceProcessor* _rp;
 476   BoolObjectClosure* _saved_cl;
 477 
 478  public:
 479   ReferenceProcessorSubjectToDiscoveryMutator(ReferenceProcessor* rp, BoolObjectClosure* cl):
 480     _rp(rp) {
 481     _saved_cl = _rp->is_subject_to_discovery_closure();
 482     _rp->set_is_subject_to_discovery_closure(cl);
 483   }
 484 
 485   ~ReferenceProcessorSubjectToDiscoveryMutator() {
 486     _rp->set_is_subject_to_discovery_closure(_saved_cl);
 487   }
 488 };
 489 
 490 // A utility class to temporarily mutate the span of the
 491 // given ReferenceProcessor in the scope that contains it.
 492 class ReferenceProcessorSpanMutator: StackObj {
 493  private:
 494   SpanReferenceProcessor* _rp;
 495   MemRegion _saved_span;
 496 
 497  public:
 498   ReferenceProcessorSpanMutator(SpanReferenceProcessor* rp,
 499                                 MemRegion span):
 500     _rp(rp) {
 501     _saved_span = _rp->span();
 502     _rp->set_span(span);
 503   }
 504 
 505   ~ReferenceProcessorSpanMutator() {
 506     _rp->set_span(_saved_span);
 507   }
 508 };
 509 
 510 // A utility class to temporarily change the MT'ness of
 511 // reference discovery for the given ReferenceProcessor
 512 // in the scope that contains it.
 513 class ReferenceProcessorMTDiscoveryMutator: StackObj {
 514  private:
 515   ReferenceProcessor* _rp;
 516   bool                _saved_mt;
 517 
 518  public:
 519   ReferenceProcessorMTDiscoveryMutator(ReferenceProcessor* rp,
 520                                        bool mt):
 521     _rp(rp) {
 522     _saved_mt = _rp->discovery_is_mt();
 523     _rp->set_mt_discovery(mt);
 524   }
 525 
 526   ~ReferenceProcessorMTDiscoveryMutator() {
 527     _rp->set_mt_discovery(_saved_mt);
 528   }
 529 };
 530 
 531 // A utility class to temporarily change the disposition
 532 // of the "is_alive_non_header" closure field of the
 533 // given ReferenceProcessor in the scope that contains it.
 534 class ReferenceProcessorIsAliveMutator: StackObj {
 535  private:
 536   ReferenceProcessor* _rp;
 537   BoolObjectClosure*  _saved_cl;
 538 
 539  public:
 540   ReferenceProcessorIsAliveMutator(ReferenceProcessor* rp,
 541                                    BoolObjectClosure*  cl):
 542     _rp(rp) {
 543     _saved_cl = _rp->is_alive_non_header();
 544     _rp->set_is_alive_non_header(cl);
 545   }
 546 
 547   ~ReferenceProcessorIsAliveMutator() {
 548     _rp->set_is_alive_non_header(_saved_cl);
 549   }
 550 };
 551 
 552 // A utility class to temporarily change the disposition
 553 // of the "discovery_is_atomic" field of the
 554 // given ReferenceProcessor in the scope that contains it.
 555 class ReferenceProcessorAtomicMutator: StackObj {
 556  private:
 557   ReferenceProcessor* _rp;
 558   bool                _saved_atomic_discovery;
 559 
 560  public:
 561   ReferenceProcessorAtomicMutator(ReferenceProcessor* rp,
 562                                   bool atomic):
 563     _rp(rp) {
 564     _saved_atomic_discovery = _rp->discovery_is_atomic();
 565     _rp->set_atomic_discovery(atomic);
 566   }
 567 
 568   ~ReferenceProcessorAtomicMutator() {
 569     _rp->set_atomic_discovery(_saved_atomic_discovery);
 570   }
 571 };
 572 
 573 
 574 // A utility class to temporarily change the MT processing
 575 // disposition of the given ReferenceProcessor instance
 576 // in the scope that contains it.
 577 class ReferenceProcessorMTProcMutator: StackObj {
 578  private:
 579   ReferenceProcessor* _rp;
 580   bool  _saved_mt;
 581 
 582  public:
 583   ReferenceProcessorMTProcMutator(ReferenceProcessor* rp,
 584                                   bool mt):
 585     _rp(rp) {
 586     _saved_mt = _rp->processing_is_mt();
 587     _rp->set_mt_processing(mt);
 588   }
 589 
 590   ~ReferenceProcessorMTProcMutator() {
 591     _rp->set_mt_processing(_saved_mt);
 592   }
 593 };
 594 
 595 
 596 // This class is an interface used to implement task execution for the
 597 // reference processing.
 598 class AbstractRefProcTaskExecutor {
 599 public:
 600 
 601   // Abstract tasks to execute.
 602   class ProcessTask;
 603   class EnqueueTask;
 604 
 605   // Executes a task using worker threads.
 606   virtual void execute(ProcessTask& task) = 0;
 607   virtual void execute(EnqueueTask& task) = 0;
 608 
 609   // Switch to single threaded mode.
 610   virtual void set_single_threaded_mode() { };
 611 };
 612 
 613 // Abstract reference processing task to execute.
 614 class AbstractRefProcTaskExecutor::ProcessTask {
 615 protected:
 616   ProcessTask(ReferenceProcessor&           ref_processor,
 617               DiscoveredList                refs_lists[],
 618               bool                          marks_oops_alive,
 619               ReferenceProcessorPhaseTimes* phase_times)
 620     : _ref_processor(ref_processor),
 621       _refs_lists(refs_lists),
 622       _phase_times(phase_times),
 623       _marks_oops_alive(marks_oops_alive)
 624   { }
 625 
 626 public:
 627   virtual void work(unsigned int work_id, BoolObjectClosure& is_alive,
 628                     OopClosure& keep_alive,
 629                     VoidClosure& complete_gc) = 0;
 630 
 631   // Returns true if a task marks some oops as alive.
 632   bool marks_oops_alive() const
 633   { return _marks_oops_alive; }
 634 
 635 protected:
 636   ReferenceProcessor&           _ref_processor;
 637   DiscoveredList*               _refs_lists;
 638   ReferenceProcessorPhaseTimes* _phase_times;
 639   const bool                    _marks_oops_alive;
 640 };
 641 
 642 // Abstract reference processing task to execute.
 643 class AbstractRefProcTaskExecutor::EnqueueTask {
 644 protected:
 645   EnqueueTask(ReferenceProcessor&           ref_processor,
 646               DiscoveredList                refs_lists[],
 647               int                           n_queues,
 648               ReferenceProcessorPhaseTimes* phase_times)
 649     : _ref_processor(ref_processor),
 650       _refs_lists(refs_lists),
 651       _n_queues(n_queues),
 652       _phase_times(phase_times)
 653   { }
 654 
 655 public:
 656   virtual void work(unsigned int work_id) = 0;
 657 
 658 protected:
 659   ReferenceProcessor&           _ref_processor;
 660   DiscoveredList*               _refs_lists;
 661   ReferenceProcessorPhaseTimes* _phase_times;
 662   int                           _n_queues;
 663 };
 664 
 665 #endif // SHARE_VM_GC_SHARED_REFERENCEPROCESSOR_HPP