referenceProcessor.hpp 25.2 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30
#ifndef SHARE_VM_MEMORY_REFERENCEPROCESSOR_HPP
#define SHARE_VM_MEMORY_REFERENCEPROCESSOR_HPP

#include "memory/referencePolicy.hpp"
#include "oops/instanceRefKlass.hpp"

D
duke 已提交
31
// ReferenceProcessor class encapsulates the per-"collector" processing
32
// of java.lang.Reference objects for GC. The interface is useful for supporting
D
duke 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
// a generational abstraction, in particular when there are multiple
// generations that are being independently collected -- possibly
// concurrently and/or incrementally.  Note, however, that the
// ReferenceProcessor class abstracts away from a generational setting
// by using only a heap interval (called "span" below), thus allowing
// its use in a straightforward manner in a general, non-generational
// setting.
//
// The basic idea is that each ReferenceProcessor object concerns
// itself with ("weak") reference processing in a specific "span"
// of the heap of interest to a specific collector. Currently,
// the span is a convex interval of the heap, but, efficiency
// apart, there seems to be no reason it couldn't be extended
// (with appropriate modifications) to any "non-convex interval".

// forward references
class ReferencePolicy;
class AbstractRefProcTaskExecutor;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

// List of discovered references.
class DiscoveredList {
public:
  DiscoveredList() : _len(0), _compressed_head(0), _oop_head(NULL) { }
  oop head() const     {
     return UseCompressedOops ?  oopDesc::decode_heap_oop(_compressed_head) :
                                _oop_head;
  }
  HeapWord* adr_head() {
    return UseCompressedOops ? (HeapWord*)&_compressed_head :
                               (HeapWord*)&_oop_head;
  }
  void set_head(oop o) {
    if (UseCompressedOops) {
      // Must compress the head ptr.
      _compressed_head = oopDesc::encode_heap_oop(o);
    } else {
      _oop_head = o;
    }
  }
  bool   is_empty() const       { return head() == NULL; }
  size_t length()               { return _len; }
  void   set_length(size_t len) { _len = len;  }
  void   inc_length(size_t inc) { _len += inc; assert(_len > 0, "Error"); }
  void   dec_length(size_t dec) { _len -= dec; }
private:
  // Set value depending on UseCompressedOops. This could be a template class
  // but then we have to fix all the instantiations and declarations that use this class.
  oop       _oop_head;
  narrowOop _compressed_head;
  size_t _len;
};

// Iterator for the list of discovered references.
class DiscoveredListIterator {
private:
  DiscoveredList&    _refs_list;
  HeapWord*          _prev_next;
  oop                _prev;
  oop                _ref;
  HeapWord*          _discovered_addr;
  oop                _next;
  HeapWord*          _referent_addr;
  oop                _referent;
  OopClosure*        _keep_alive;
  BoolObjectClosure* _is_alive;

  DEBUG_ONLY(
  oop                _first_seen; // cyclic linked list check
  )

  NOT_PRODUCT(
  size_t             _processed;
  size_t             _removed;
  )

public:
  inline DiscoveredListIterator(DiscoveredList&    refs_list,
                                OopClosure*        keep_alive,
                                BoolObjectClosure* is_alive):
    _refs_list(refs_list),
    _prev_next(refs_list.adr_head()),
    _prev(NULL),
    _ref(refs_list.head()),
#ifdef ASSERT
    _first_seen(refs_list.head()),
#endif
#ifndef PRODUCT
    _processed(0),
    _removed(0),
#endif
    _next(NULL),
    _keep_alive(keep_alive),
    _is_alive(is_alive)
{ }

  // End Of List.
  inline bool has_next() const { return _ref != NULL; }

  // Get oop to the Reference object.
  inline oop obj() const { return _ref; }

  // Get oop to the referent object.
  inline oop referent() const { return _referent; }

  // Returns true if referent is alive.
  inline bool is_referent_alive() const {
    return _is_alive->do_object_b(_referent);
  }

  // Loads data for the current reference.
  // The "allow_null_referent" argument tells us to allow for the possibility
  // of a NULL referent in the discovered Reference object. This typically
  // happens in the case of concurrent collectors that may have done the
  // discovery concurrently, or interleaved, with mutator execution.
  void load_ptrs(DEBUG_ONLY(bool allow_null_referent));

  // Move to the next discovered reference.
  inline void next() {
    _prev_next = _discovered_addr;
    _prev = _ref;
    move_to_next();
  }

  // Remove the current reference from the list
  void remove();

  // Make the Reference object active again.
  void make_active();

  // Make the referent alive.
  inline void make_referent_alive() {
    if (UseCompressedOops) {
      _keep_alive->do_oop((narrowOop*)_referent_addr);
    } else {
      _keep_alive->do_oop((oop*)_referent_addr);
    }
  }

  // Update the discovered field.
  inline void update_discovered() {
    // First _prev_next ref actually points into DiscoveredList (gross).
    if (UseCompressedOops) {
      if (!oopDesc::is_null(*(narrowOop*)_prev_next)) {
        _keep_alive->do_oop((narrowOop*)_prev_next);
      }
    } else {
      if (!oopDesc::is_null(*(oop*)_prev_next)) {
        _keep_alive->do_oop((oop*)_prev_next);
      }
    }
  }

  // NULL out referent pointer.
  void clear_referent();

  // Statistics
  NOT_PRODUCT(
  inline size_t processed() const { return _processed; }
  inline size_t removed() const   { return _removed; }
  )

  inline void move_to_next() {
    if (_ref == _next) {
      // End of the list.
      _ref = NULL;
    } else {
      _ref = _next;
    }
    assert(_ref != _first_seen, "cyclic ref_list found");
    NOT_PRODUCT(_processed++);
  }

};
D
duke 已提交
206 207 208

class ReferenceProcessor : public CHeapObj {
 protected:
209 210
  // Compatibility with pre-4965777 JDK's
  static bool _pending_list_uses_discovered_field;
211 212 213 214 215 216 217 218 219

  MemRegion   _span;                    // (right-open) interval of heap
                                        // subject to wkref discovery

  bool        _discovering_refs;        // true when discovery enabled
  bool        _discovery_is_atomic;     // if discovery is atomic wrt
                                        // other collectors in configuration
  bool        _discovery_is_mt;         // true if reference discovery is MT.

220 221 222 223 224 225 226
  // If true, setting "next" field of a discovered refs list requires
  // write barrier(s).  (Must be true if used in a collector in which
  // elements of a discovered list may be moved during discovery: for
  // example, a collector like Garbage-First that moves objects during a
  // long-term concurrent marking phase that does weak reference
  // discovery.)
  bool        _discovered_list_needs_barrier;
227 228 229 230 231 232 233 234 235

  BarrierSet* _bs;                      // Cached copy of BarrierSet.
  bool        _enqueuing_is_done;       // true if all weak references enqueued
  bool        _processing_is_mt;        // true during phases when
                                        // reference processing is MT.
  int         _next_id;                 // round-robin mod _num_q counter in
                                        // support of work distribution

  // For collectors that do not keep GC liveness information
D
duke 已提交
236 237
  // in the object header, this field holds a closure that
  // helps the reference processor determine the reachability
238 239
  // of an oop. It is currently initialized to NULL for all
  // collectors except for CMS and G1.
D
duke 已提交
240 241
  BoolObjectClosure* _is_alive_non_header;

242 243 244 245 246 247 248 249
  // Soft ref clearing policies
  // . the default policy
  static ReferencePolicy*   _default_soft_ref_policy;
  // . the "clear all" policy
  static ReferencePolicy*   _always_clear_soft_ref_policy;
  // . the current policy below is either one of the above
  ReferencePolicy*          _current_soft_ref_policy;

D
duke 已提交
250
  // The discovered ref lists themselves
251

252
  // The active MT'ness degree of the queues below
253
  int             _num_q;
254 255
  // The maximum MT'ness degree of the queues below
  int             _max_num_q;
256 257
  // Arrays of lists of oops, one per thread
  DiscoveredList* _discoveredSoftRefs;
D
duke 已提交
258 259 260 261 262
  DiscoveredList* _discoveredWeakRefs;
  DiscoveredList* _discoveredFinalRefs;
  DiscoveredList* _discoveredPhantomRefs;

 public:
263 264 265 266 267 268 269
  static int number_of_subclasses_of_ref() { return (REF_PHANTOM - REF_OTHER); }

  int num_q()                              { return _num_q; }
  int max_num_q()                          { return _max_num_q; }
  void set_active_mt_degree(int v)         { _num_q = v; }
  DiscoveredList* discovered_soft_refs()   { return _discoveredSoftRefs; }

270
  ReferencePolicy* setup_policy(bool always_clear) {
271 272
    _current_soft_ref_policy = always_clear ?
      _always_clear_soft_ref_policy : _default_soft_ref_policy;
273
    _current_soft_ref_policy->setup();   // snapshot the policy threshold
274 275
    return _current_soft_ref_policy;
  }
D
duke 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

  // Process references with a certain reachability level.
  void process_discovered_reflist(DiscoveredList               refs_lists[],
                                  ReferencePolicy*             policy,
                                  bool                         clear_referent,
                                  BoolObjectClosure*           is_alive,
                                  OopClosure*                  keep_alive,
                                  VoidClosure*                 complete_gc,
                                  AbstractRefProcTaskExecutor* task_executor);

  void process_phaseJNI(BoolObjectClosure* is_alive,
                        OopClosure*        keep_alive,
                        VoidClosure*       complete_gc);

  // Work methods used by the method process_discovered_reflist
  // Phase1: keep alive all those referents that are otherwise
  // dead but which must be kept alive by policy (and their closure).
293
  void process_phase1(DiscoveredList&     refs_list,
D
duke 已提交
294 295 296 297 298 299
                      ReferencePolicy*    policy,
                      BoolObjectClosure*  is_alive,
                      OopClosure*         keep_alive,
                      VoidClosure*        complete_gc);
  // Phase2: remove all those references whose referents are
  // reachable.
300
  inline void process_phase2(DiscoveredList&    refs_list,
D
duke 已提交
301 302 303 304 305
                             BoolObjectClosure* is_alive,
                             OopClosure*        keep_alive,
                             VoidClosure*       complete_gc) {
    if (discovery_is_atomic()) {
      // complete_gc is ignored in this case for this phase
306
      pp2_work(refs_list, is_alive, keep_alive);
D
duke 已提交
307 308
    } else {
      assert(complete_gc != NULL, "Error");
309
      pp2_work_concurrent_discovery(refs_list, is_alive,
D
duke 已提交
310 311 312 313
                                    keep_alive, complete_gc);
    }
  }
  // Work methods in support of process_phase2
314
  void pp2_work(DiscoveredList&    refs_list,
D
duke 已提交
315 316 317
                BoolObjectClosure* is_alive,
                OopClosure*        keep_alive);
  void pp2_work_concurrent_discovery(
318
                DiscoveredList&    refs_list,
D
duke 已提交
319 320 321 322 323
                BoolObjectClosure* is_alive,
                OopClosure*        keep_alive,
                VoidClosure*       complete_gc);
  // Phase3: process the referents by either clearing them
  // or keeping them alive (and their closure)
324
  void process_phase3(DiscoveredList&    refs_list,
D
duke 已提交
325 326 327 328 329 330
                      bool               clear_referent,
                      BoolObjectClosure* is_alive,
                      OopClosure*        keep_alive,
                      VoidClosure*       complete_gc);

  // Enqueue references with a certain reachability level
331
  void enqueue_discovered_reflist(DiscoveredList& refs_list, HeapWord* pending_list_addr);
D
duke 已提交
332 333 334 335 336 337 338 339 340

  // "Preclean" all the discovered reference lists
  // by removing references with strongly reachable referents.
  // The first argument is a predicate on an oop that indicates
  // its (strong) reachability and the second is a closure that
  // may be used to incrementalize or abort the precleaning process.
  // The caller is responsible for taking care of potential
  // interference with concurrent operations on these lists
  // (or predicates involved) by other threads. Currently
341 342
  // only used by the CMS collector.  should_unload_classes is
  // used to aid assertion checking when classes are collected.
D
duke 已提交
343 344 345
  void preclean_discovered_references(BoolObjectClosure* is_alive,
                                      OopClosure*        keep_alive,
                                      VoidClosure*       complete_gc,
346 347
                                      YieldClosure*      yield,
                                      bool               should_unload_classes);
D
duke 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

  // Delete entries in the discovered lists that have
  // either a null referent or are not active. Such
  // Reference objects can result from the clearing
  // or enqueueing of Reference objects concurrent
  // with their discovery by a (concurrent) collector.
  // For a definition of "active" see java.lang.ref.Reference;
  // Refs are born active, become inactive when enqueued,
  // and never become active again. The state of being
  // active is encoded as follows: A Ref is active
  // if and only if its "next" field is NULL.
  void clean_up_discovered_references();
  void clean_up_discovered_reflist(DiscoveredList& refs_list);

  // Returns the name of the discovered reference list
  // occupying the i / _num_q slot.
  const char* list_name(int i);

366 367
  void enqueue_discovered_reflists(HeapWord* pending_list_addr, AbstractRefProcTaskExecutor* task_executor);

D
duke 已提交
368
 protected:
369 370 371 372 373
  // Set the 'discovered' field of the given reference to
  // the given value - emitting barriers depending upon
  // the value of _discovered_list_needs_barrier.
  void set_discovered(oop ref, oop value);

D
duke 已提交
374 375 376 377 378 379 380 381 382
  // "Preclean" the given discovered reference list
  // by removing references with strongly reachable referents.
  // Currently used in support of CMS only.
  void preclean_discovered_reflist(DiscoveredList&    refs_list,
                                   BoolObjectClosure* is_alive,
                                   OopClosure*        keep_alive,
                                   VoidClosure*       complete_gc,
                                   YieldClosure*      yield);

383
  // round-robin mod _num_q (not: _not_ mode _max_num_q)
D
duke 已提交
384 385 386 387 388 389 390 391 392
  int next_id() {
    int id = _next_id;
    if (++_next_id == _num_q) {
      _next_id = 0;
    }
    return id;
  }
  DiscoveredList* get_discovered_list(ReferenceType rt);
  inline void add_to_discovered_list_mt(DiscoveredList& refs_list, oop obj,
393
                                        HeapWord* discovered_addr);
D
duke 已提交
394 395
  void verify_ok_to_handle_reflists() PRODUCT_RETURN;

396
  void clear_discovered_references(DiscoveredList& refs_list);
D
duke 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
  void abandon_partial_discovered_list(DiscoveredList& refs_list);

  // Calculate the number of jni handles.
  unsigned int count_jni_refs();

  // Balances reference queues.
  void balance_queues(DiscoveredList ref_lists[]);

  // Update (advance) the soft ref master clock field.
  void update_soft_ref_master_clock();

 public:
  // constructor
  ReferenceProcessor():
    _span((HeapWord*)NULL, (HeapWord*)NULL),
    _discoveredSoftRefs(NULL),  _discoveredWeakRefs(NULL),
    _discoveredFinalRefs(NULL), _discoveredPhantomRefs(NULL),
    _discovering_refs(false),
    _discovery_is_atomic(true),
    _enqueuing_is_done(false),
    _discovery_is_mt(false),
418 419
    _discovered_list_needs_barrier(false),
    _bs(NULL),
D
duke 已提交
420 421
    _is_alive_non_header(NULL),
    _num_q(0),
422
    _max_num_q(0),
D
duke 已提交
423 424
    _processing_is_mt(false),
    _next_id(0)
425 426 427 428 429 430 431 432
  { }

  // Default parameters give you a vanilla reference processor.
  ReferenceProcessor(MemRegion span,
                     bool mt_processing = false, int mt_processing_degree = 1,
                     bool mt_discovery  = false, int mt_discovery_degree  = 1,
                     bool atomic_discovery = true,
                     BoolObjectClosure* is_alive_non_header = NULL,
433
                     bool discovered_list_needs_barrier = false);
D
duke 已提交
434 435

  // RefDiscoveryPolicy values
436
  enum DiscoveryPolicy {
D
duke 已提交
437
    ReferenceBasedDiscovery = 0,
438 439 440
    ReferentBasedDiscovery  = 1,
    DiscoveryPolicyMin      = ReferenceBasedDiscovery,
    DiscoveryPolicyMax      = ReferentBasedDiscovery
D
duke 已提交
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
  };

  static void init_statics();

 public:
  // get and set "is_alive_non_header" field
  BoolObjectClosure* is_alive_non_header() {
    return _is_alive_non_header;
  }
  void set_is_alive_non_header(BoolObjectClosure* is_alive_non_header) {
    _is_alive_non_header = is_alive_non_header;
  }

  // get and set span
  MemRegion span()                   { return _span; }
  void      set_span(MemRegion span) { _span = span; }

  // start and stop weak ref discovery
459 460 461 462 463 464 465 466 467 468 469 470 471
  void enable_discovery(bool verify_disabled, bool check_no_refs) {
#ifdef ASSERT
    // Verify that we're not currently discovering refs
    assert(!verify_disabled || !_discovering_refs, "nested call?");

    if (check_no_refs) {
      // Verify that the discovered lists are empty
      verify_no_references_recorded();
    }
#endif // ASSERT
    _discovering_refs = true;
  }

D
duke 已提交
472 473 474 475 476 477 478
  void disable_discovery()  { _discovering_refs = false; }
  bool discovery_enabled()  { return _discovering_refs;  }

  // whether discovery is atomic wrt other collectors
  bool discovery_is_atomic() const { return _discovery_is_atomic; }
  void set_atomic_discovery(bool atomic) { _discovery_is_atomic = atomic; }

479 480 481 482 483 484 485
  // whether the JDK in which we are embedded is a pre-4965777 JDK,
  // and thus whether or not it uses the discovered field to chain
  // the entries in the pending list.
  static bool pending_list_uses_discovered_field() {
    return _pending_list_uses_discovered_field;
  }

D
duke 已提交
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
  // whether discovery is done by multiple threads same-old-timeously
  bool discovery_is_mt() const { return _discovery_is_mt; }
  void set_mt_discovery(bool mt) { _discovery_is_mt = mt; }

  // Whether we are in a phase when _processing_ is MT.
  bool processing_is_mt() const { return _processing_is_mt; }
  void set_mt_processing(bool mt) { _processing_is_mt = mt; }

  // whether all enqueuing of weak references is complete
  bool enqueuing_is_done()  { return _enqueuing_is_done; }
  void set_enqueuing_is_done(bool v) { _enqueuing_is_done = v; }

  // iterate over oops
  void weak_oops_do(OopClosure* f);       // weak roots

501 502 503
  // Balance each of the discovered lists.
  void balance_all_queues();

D
duke 已提交
504 505 506 507
  // Discover a Reference object, using appropriate discovery criteria
  bool discover_reference(oop obj, ReferenceType rt);

  // Process references found during GC (called by the garbage collector)
508
  void process_discovered_references(BoolObjectClosure*           is_alive,
D
duke 已提交
509 510 511 512 513 514 515 516
                                     OopClosure*                  keep_alive,
                                     VoidClosure*                 complete_gc,
                                     AbstractRefProcTaskExecutor* task_executor);

 public:
  // Enqueue references at end of GC (called by the garbage collector)
  bool enqueue_discovered_references(AbstractRefProcTaskExecutor* task_executor = NULL);

517 518 519 520 521
  // If a discovery is in process that is being superceded, abandon it: all
  // the discovered lists will be empty, and all the objects on them will
  // have NULL discovered fields.  Must be called only at a safepoint.
  void abandon_partial_discovery();

D
duke 已提交
522 523
  // debugging
  void verify_no_references_recorded() PRODUCT_RETURN;
524
  void verify_referent(oop obj)        PRODUCT_RETURN;
D
duke 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537

  // clear the discovered lists (unlinking each entry).
  void clear_discovered_references() PRODUCT_RETURN;
};

// A utility class to disable reference discovery in
// the scope which contains it, for given ReferenceProcessor.
class NoRefDiscovery: StackObj {
 private:
  ReferenceProcessor* _rp;
  bool _was_discovering_refs;
 public:
  NoRefDiscovery(ReferenceProcessor* rp) : _rp(rp) {
538 539
    _was_discovering_refs = _rp->discovery_enabled();
    if (_was_discovering_refs) {
D
duke 已提交
540 541 542 543 544 545
      _rp->disable_discovery();
    }
  }

  ~NoRefDiscovery() {
    if (_was_discovering_refs) {
546
      _rp->enable_discovery(true /*verify_disabled*/, false /*check_no_refs*/);
D
duke 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    }
  }
};


// A utility class to temporarily mutate the span of the
// given ReferenceProcessor in the scope that contains it.
class ReferenceProcessorSpanMutator: StackObj {
 private:
  ReferenceProcessor* _rp;
  MemRegion           _saved_span;

 public:
  ReferenceProcessorSpanMutator(ReferenceProcessor* rp,
                                MemRegion span):
    _rp(rp) {
    _saved_span = _rp->span();
    _rp->set_span(span);
  }

  ~ReferenceProcessorSpanMutator() {
    _rp->set_span(_saved_span);
  }
};

// A utility class to temporarily change the MT'ness of
// reference discovery for the given ReferenceProcessor
// in the scope that contains it.
575
class ReferenceProcessorMTDiscoveryMutator: StackObj {
D
duke 已提交
576 577 578 579 580
 private:
  ReferenceProcessor* _rp;
  bool                _saved_mt;

 public:
581 582
  ReferenceProcessorMTDiscoveryMutator(ReferenceProcessor* rp,
                                       bool mt):
D
duke 已提交
583 584 585 586 587
    _rp(rp) {
    _saved_mt = _rp->discovery_is_mt();
    _rp->set_mt_discovery(mt);
  }

588
  ~ReferenceProcessorMTDiscoveryMutator() {
D
duke 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
    _rp->set_mt_discovery(_saved_mt);
  }
};


// A utility class to temporarily change the disposition
// of the "is_alive_non_header" closure field of the
// given ReferenceProcessor in the scope that contains it.
class ReferenceProcessorIsAliveMutator: StackObj {
 private:
  ReferenceProcessor* _rp;
  BoolObjectClosure*  _saved_cl;

 public:
  ReferenceProcessorIsAliveMutator(ReferenceProcessor* rp,
                                   BoolObjectClosure*  cl):
    _rp(rp) {
    _saved_cl = _rp->is_alive_non_header();
    _rp->set_is_alive_non_header(cl);
  }

  ~ReferenceProcessorIsAliveMutator() {
    _rp->set_is_alive_non_header(_saved_cl);
  }
};

// A utility class to temporarily change the disposition
// of the "discovery_is_atomic" field of the
// given ReferenceProcessor in the scope that contains it.
class ReferenceProcessorAtomicMutator: StackObj {
 private:
  ReferenceProcessor* _rp;
  bool                _saved_atomic_discovery;

 public:
  ReferenceProcessorAtomicMutator(ReferenceProcessor* rp,
                                  bool atomic):
    _rp(rp) {
    _saved_atomic_discovery = _rp->discovery_is_atomic();
    _rp->set_atomic_discovery(atomic);
  }

  ~ReferenceProcessorAtomicMutator() {
    _rp->set_atomic_discovery(_saved_atomic_discovery);
  }
};


// A utility class to temporarily change the MT processing
// disposition of the given ReferenceProcessor instance
// in the scope that contains it.
class ReferenceProcessorMTProcMutator: StackObj {
 private:
  ReferenceProcessor* _rp;
  bool  _saved_mt;

 public:
  ReferenceProcessorMTProcMutator(ReferenceProcessor* rp,
                                  bool mt):
    _rp(rp) {
    _saved_mt = _rp->processing_is_mt();
    _rp->set_mt_processing(mt);
  }

  ~ReferenceProcessorMTProcMutator() {
    _rp->set_mt_processing(_saved_mt);
  }
};


// This class is an interface used to implement task execution for the
// reference processing.
class AbstractRefProcTaskExecutor {
public:

  // Abstract tasks to execute.
  class ProcessTask;
  class EnqueueTask;

  // Executes a task using worker threads.
  virtual void execute(ProcessTask& task) = 0;
  virtual void execute(EnqueueTask& task) = 0;

  // Switch to single threaded mode.
  virtual void set_single_threaded_mode() { };
};

// Abstract reference processing task to execute.
class AbstractRefProcTaskExecutor::ProcessTask {
protected:
  ProcessTask(ReferenceProcessor& ref_processor,
              DiscoveredList      refs_lists[],
              bool                marks_oops_alive)
    : _ref_processor(ref_processor),
      _refs_lists(refs_lists),
      _marks_oops_alive(marks_oops_alive)
  { }

public:
  virtual void work(unsigned int work_id, BoolObjectClosure& is_alive,
                    OopClosure& keep_alive,
                    VoidClosure& complete_gc) = 0;

  // Returns true if a task marks some oops as alive.
  bool marks_oops_alive() const
  { return _marks_oops_alive; }

protected:
  ReferenceProcessor& _ref_processor;
  DiscoveredList*     _refs_lists;
  const bool          _marks_oops_alive;
};

// Abstract reference processing task to execute.
class AbstractRefProcTaskExecutor::EnqueueTask {
protected:
  EnqueueTask(ReferenceProcessor& ref_processor,
              DiscoveredList      refs_lists[],
707
              HeapWord*           pending_list_addr,
D
duke 已提交
708 709 710 711 712 713 714 715 716 717 718 719 720
              int                 n_queues)
    : _ref_processor(ref_processor),
      _refs_lists(refs_lists),
      _pending_list_addr(pending_list_addr),
      _n_queues(n_queues)
  { }

public:
  virtual void work(unsigned int work_id) = 0;

protected:
  ReferenceProcessor& _ref_processor;
  DiscoveredList*     _refs_lists;
721
  HeapWord*           _pending_list_addr;
D
duke 已提交
722 723
  int                 _n_queues;
};
724 725

#endif // SHARE_VM_MEMORY_REFERENCEPROCESSOR_HPP