src/hotspot/share/gc/g1/g1RedirtyCardsQueue.cpp
changeset 55752 8ae33203d600
child 57507 f6b30bd6804e
equal deleted inserted replaced
55751:014decdb5086 55752:8ae33203d600
       
     1 /*
       
     2  * Copyright (c) 2019, 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 #include "precompiled.hpp"
       
    26 #include "gc/g1/g1RedirtyCardsQueue.hpp"
       
    27 #include "runtime/atomic.hpp"
       
    28 #include "utilities/debug.hpp"
       
    29 #include "utilities/macros.hpp"
       
    30 
       
    31 // G1RedirtyCardsBufferList
       
    32 
       
    33 G1RedirtyCardsBufferList::G1RedirtyCardsBufferList() :
       
    34   _head(NULL), _tail(NULL), _count(0) {}
       
    35 
       
    36 G1RedirtyCardsBufferList::G1RedirtyCardsBufferList(BufferNode* head,
       
    37                                                    BufferNode* tail,
       
    38                                                    size_t count) :
       
    39   _head(head), _tail(tail), _count(count)
       
    40 {
       
    41   assert((_head == NULL) == (_tail == NULL), "invariant");
       
    42   assert((_head == NULL) == (_count == 0), "invariant");
       
    43 }
       
    44 
       
    45 // G1RedirtyCardsQueueBase::LocalQSet
       
    46 
       
    47 G1RedirtyCardsQueueBase::LocalQSet::LocalQSet(G1RedirtyCardsQueueSet* shared_qset) :
       
    48   PtrQueueSet(),
       
    49   _shared_qset(shared_qset),
       
    50   _buffers()
       
    51 {
       
    52   PtrQueueSet::initialize(_shared_qset->allocator());
       
    53 }
       
    54 
       
    55 G1RedirtyCardsQueueBase::LocalQSet::~LocalQSet() {
       
    56   assert(_buffers._head == NULL, "unflushed qset");
       
    57   assert(_buffers._tail == NULL, "invariant");
       
    58   assert(_buffers._count == 0, "invariant");
       
    59 }
       
    60 
       
    61 void G1RedirtyCardsQueueBase::LocalQSet::enqueue_completed_buffer(BufferNode* node) {
       
    62   ++_buffers._count;
       
    63   node->set_next(_buffers._head);
       
    64   _buffers._head = node;
       
    65   if (_buffers._tail == NULL) {
       
    66     _buffers._tail = node;
       
    67   }
       
    68 }
       
    69 
       
    70 G1RedirtyCardsBufferList
       
    71 G1RedirtyCardsQueueBase::LocalQSet::take_all_completed_buffers() {
       
    72   G1RedirtyCardsBufferList result = _buffers;
       
    73   _buffers = G1RedirtyCardsBufferList();
       
    74   return result;
       
    75 }
       
    76 
       
    77 void G1RedirtyCardsQueueBase::LocalQSet::flush() {
       
    78   _shared_qset->merge_bufferlist(this);
       
    79 }
       
    80 
       
    81 // G1RedirtyCardsQueue
       
    82 
       
    83 G1RedirtyCardsQueue::G1RedirtyCardsQueue(G1RedirtyCardsQueueSet* qset) :
       
    84   G1RedirtyCardsQueueBase(qset), // Init _local_qset before passing to PtrQueue.
       
    85   PtrQueue(&_local_qset, true /* active (always) */)
       
    86 {}
       
    87 
       
    88 G1RedirtyCardsQueue::~G1RedirtyCardsQueue() {
       
    89   flush();
       
    90 }
       
    91 
       
    92 void G1RedirtyCardsQueue::handle_completed_buffer() {
       
    93   enqueue_completed_buffer();
       
    94 }
       
    95 
       
    96 void G1RedirtyCardsQueue::flush() {
       
    97   flush_impl();
       
    98   _local_qset.flush();
       
    99 }
       
   100 
       
   101 // G1RedirtyCardsQueueSet
       
   102 
       
   103 G1RedirtyCardsQueueSet::G1RedirtyCardsQueueSet() :
       
   104   PtrQueueSet(),
       
   105   _list(),
       
   106   _count(0),
       
   107   _tail(NULL)
       
   108   DEBUG_ONLY(COMMA _collecting(true))
       
   109 {}
       
   110 
       
   111 G1RedirtyCardsQueueSet::~G1RedirtyCardsQueueSet() {
       
   112   verify_empty();
       
   113 }
       
   114 
       
   115 #ifdef ASSERT
       
   116 void G1RedirtyCardsQueueSet::verify_empty() const {
       
   117   assert(_list.empty(), "precondition");
       
   118   assert(_tail == NULL, "invariant");
       
   119   assert(_count == 0, "invariant");
       
   120 }
       
   121 #endif // ASSERT
       
   122 
       
   123 BufferNode* G1RedirtyCardsQueueSet::all_completed_buffers() const {
       
   124   DEBUG_ONLY(_collecting = false;)
       
   125   return _list.top();
       
   126 }
       
   127 
       
   128 G1RedirtyCardsBufferList G1RedirtyCardsQueueSet::take_all_completed_buffers() {
       
   129   DEBUG_ONLY(_collecting = false;)
       
   130   G1RedirtyCardsBufferList result(_list.pop_all(), _tail, _count);
       
   131   _tail = NULL;
       
   132   _count = 0;
       
   133   DEBUG_ONLY(_collecting = true;)
       
   134   return result;
       
   135 }
       
   136 
       
   137 void G1RedirtyCardsQueueSet::update_tail(BufferNode* node) {
       
   138   // Node is the tail of a (possibly single element) list just prepended to
       
   139   // _list.  If, after that prepend, node's follower is NULL, then node is
       
   140   // also the tail of _list, so record it as such.
       
   141   if (node->next() == NULL) {
       
   142     assert(_tail == NULL, "invariant");
       
   143     _tail = node;
       
   144   }
       
   145 }
       
   146 
       
   147 void G1RedirtyCardsQueueSet::enqueue_completed_buffer(BufferNode* node) {
       
   148   assert(_collecting, "precondition");
       
   149   Atomic::inc(&_count);
       
   150   _list.push(*node);
       
   151   update_tail(node);
       
   152 }
       
   153 
       
   154 void G1RedirtyCardsQueueSet::merge_bufferlist(LocalQSet* src) {
       
   155   assert(_collecting, "precondition");
       
   156   const G1RedirtyCardsBufferList from = src->take_all_completed_buffers();
       
   157   if (from._head != NULL) {
       
   158     assert(from._tail != NULL, "invariant");
       
   159     Atomic::add(from._count, &_count);
       
   160     _list.prepend(*from._head, *from._tail);
       
   161     update_tail(from._tail);
       
   162   }
       
   163 }