src/hotspot/share/gc/shared/ptrQueue.cpp
author kbarrett
Tue, 21 May 2019 19:19:44 -0400
changeset 54970 76d3d96a8bc2
parent 54810 258170da6d3a
child 55498 e64383344f14
child 58678 9cf78a70fa4f
permissions -rw-r--r--
8224167: Refactor PtrQueue completed buffer processing Summary: Add handle_completed_buffer and refactor. Reviewed-by: tschatzl, shade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     1
/*
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
     2
 * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     4
 *
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     8
 *
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    13
 * accompanied this code).
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    14
 *
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5082
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5082
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5082
diff changeset
    21
 * questions.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    22
 *
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    23
 */
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
51441
2e91d927e00c 8154343: Make SATB related code available to other GCs
kbarrett
parents: 51332
diff changeset
    26
#include "gc/shared/ptrQueue.hpp"
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
    27
#include "logging/log.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "memory/allocation.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "memory/allocation.inline.hpp"
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
    30
#include "runtime/atomic.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    31
#include "runtime/mutex.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    32
#include "runtime/mutexLocker.hpp"
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
    33
#include "runtime/orderAccess.hpp"
14583
d70ee55535f4 8003935: Simplify the needed includes for using Thread::current()
stefank
parents: 13963
diff changeset
    34
#include "runtime/thread.inline.hpp"
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
    35
#include "utilities/globalCounter.inline.hpp"
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    36
36354
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
    37
#include <new>
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
    38
54255
c81fbf340ceb 8220240: Refactor shared dirty card queue
kbarrett
parents: 53404
diff changeset
    39
PtrQueue::PtrQueue(PtrQueueSet* qset, bool active) :
46443
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    40
  _qset(qset),
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    41
  _active(active),
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    42
  _index(0),
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    43
  _capacity_in_bytes(0),
54255
c81fbf340ceb 8220240: Refactor shared dirty card queue
kbarrett
parents: 53404
diff changeset
    44
  _buf(NULL)
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    45
{}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    46
28507
354ef83ee258 8048949: Requeue queue implementation
tschatzl
parents: 27880
diff changeset
    47
PtrQueue::~PtrQueue() {
54255
c81fbf340ceb 8220240: Refactor shared dirty card queue
kbarrett
parents: 53404
diff changeset
    48
  assert(_buf == NULL, "queue must be flushed before delete");
28507
354ef83ee258 8048949: Requeue queue implementation
tschatzl
parents: 27880
diff changeset
    49
}
354ef83ee258 8048949: Requeue queue implementation
tschatzl
parents: 27880
diff changeset
    50
354ef83ee258 8048949: Requeue queue implementation
tschatzl
parents: 27880
diff changeset
    51
void PtrQueue::flush_impl() {
46305
bff6d23aa1e3 8175221: Cleanup DirtyCardQueueSet::concatenate_log
kbarrett
parents: 37065
diff changeset
    52
  if (_buf != NULL) {
46443
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    53
    BufferNode* node = BufferNode::make_node_from_buffer(_buf, index());
37065
c00d1c2ffb7c 8150676: Use BufferNode index
kbarrett
parents: 36371
diff changeset
    54
    if (is_empty()) {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    55
      // No work to do.
37065
c00d1c2ffb7c 8150676: Use BufferNode index
kbarrett
parents: 36371
diff changeset
    56
      qset()->deallocate_buffer(node);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    57
    } else {
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
    58
      qset()->enqueue_completed_buffer(node);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    59
    }
1560
1b328492b7f8 6770608: G1: Mutator thread can flush barrier and satb queues during safepoint
iveresov
parents: 1374
diff changeset
    60
    _buf = NULL;
46443
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    61
    set_index(0);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    62
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    63
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    64
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    65
void PtrQueue::enqueue_known_active(void* ptr) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    66
  while (_index == 0) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    67
    handle_zero_index();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    68
  }
4481
de92ec484f5e 6862387: tune concurrent refinement further
iveresov
parents: 4454
diff changeset
    69
46443
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    70
  assert(_buf != NULL, "postcondition");
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    71
  assert(index() > 0, "postcondition");
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    72
  assert(index() <= capacity(), "invariant");
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    73
  _index -= _element_size;
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    74
  _buf[index()] = ptr;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    75
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    76
54970
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    77
void PtrQueue::handle_zero_index() {
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    78
  assert(index() == 0, "precondition");
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    79
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    80
  if (_buf != NULL) {
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    81
    handle_completed_buffer();
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    82
  } else {
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    83
    // Bootstrapping kludge; lazily initialize capacity.  The initial
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    84
    // thread's queues are constructed before the second phase of the
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    85
    // two-phase initialization of the associated qsets.  As a result,
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    86
    // we can't initialize _capacity_in_bytes in the queue constructor.
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    87
    if (_capacity_in_bytes == 0) {
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    88
      _capacity_in_bytes = index_to_byte_index(qset()->buffer_size());
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    89
    }
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    90
    allocate_buffer();
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    91
  }
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    92
}
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    93
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    94
void PtrQueue::allocate_buffer() {
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    95
  _buf = qset()->allocate_buffer();
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    96
  reset();
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    97
}
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    98
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    99
void PtrQueue::enqueue_completed_buffer() {
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
   100
  assert(_buf != NULL, "precondition");
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
   101
  BufferNode* node = BufferNode::make_node_from_buffer(_buf, index());
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
   102
  qset()->enqueue_completed_buffer(node);
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
   103
  allocate_buffer();
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
   104
}
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
   105
46443
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
   106
BufferNode* BufferNode::allocate(size_t size) {
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
   107
  size_t byte_size = size * sizeof(void*);
36354
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   108
  void* data = NEW_C_HEAP_ARRAY(char, buffer_offset() + byte_size, mtGC);
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   109
  return new (data) BufferNode;
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   110
}
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   111
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   112
void BufferNode::deallocate(BufferNode* node) {
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   113
  node->~BufferNode();
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   114
  FREE_C_HEAP_ARRAY(char, node);
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   115
}
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   116
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   117
BufferNode::Allocator::Allocator(const char* name, size_t buffer_size) :
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   118
  _buffer_size(buffer_size),
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   119
  _pending_list(),
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   120
  _free_list(),
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   121
  _pending_count(0),
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   122
  _free_count(0),
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   123
  _transfer_lock(false)
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   124
{
54810
258170da6d3a 8223186: HotSpot compile warnings from GCC 9
ysuenaga
parents: 54623
diff changeset
   125
  strncpy(_name, name, sizeof(_name) - 1);
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   126
  _name[sizeof(_name) - 1] = '\0';
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   127
}
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   128
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   129
BufferNode::Allocator::~Allocator() {
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   130
  delete_list(_free_list.pop_all());
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   131
  delete_list(_pending_list.pop_all());
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   132
}
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   133
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   134
void BufferNode::Allocator::delete_list(BufferNode* list) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   135
  while (list != NULL) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   136
    BufferNode* next = list->next();
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   137
    DEBUG_ONLY(list->set_next(NULL);)
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   138
    BufferNode::deallocate(list);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   139
    list = next;
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   140
  }
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   141
}
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   142
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   143
size_t BufferNode::Allocator::free_count() const {
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   144
  return Atomic::load(&_free_count);
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   145
}
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   146
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   147
BufferNode* BufferNode::Allocator::allocate() {
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   148
  BufferNode* node;
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   149
  {
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   150
    // Protect against ABA; see release().
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   151
    GlobalCounter::CriticalSection cs(Thread::current());
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   152
    node = _free_list.pop();
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   153
  }
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   154
  if (node == NULL) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   155
    node = BufferNode::allocate(_buffer_size);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   156
  } else {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   157
    // Decrement count after getting buffer from free list.  This, along
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   158
    // with incrementing count before adding to free list, ensures count
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   159
    // never underflows.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   160
    size_t count = Atomic::sub(1u, &_free_count);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   161
    assert((count + 1) != 0, "_free_count underflow");
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   162
  }
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   163
  return node;
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   164
}
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   165
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   166
// To solve the ABA problem for lock-free stack pop, allocate does the
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   167
// pop inside a critical section, and release synchronizes on the
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   168
// critical sections before adding to the _free_list.  But we don't
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   169
// want to make every release have to do a synchronize.  Instead, we
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   170
// initially place released nodes on the _pending_list, and transfer
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   171
// them to the _free_list in batches.  Only one transfer at a time is
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   172
// permitted, with a lock bit to control access to that phase.  A
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   173
// transfer takes all the nodes from the _pending_list, synchronizes on
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   174
// the _free_list pops, and then adds the former pending nodes to the
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   175
// _free_list.  While that's happening, other threads might be adding
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   176
// other nodes to the _pending_list, to be dealt with by some later
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   177
// transfer.
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   178
void BufferNode::Allocator::release(BufferNode* node) {
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   179
  assert(node != NULL, "precondition");
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   180
  assert(node->next() == NULL, "precondition");
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   181
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   182
  // Desired minimum transfer batch size.  There is relatively little
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   183
  // importance to the specific number.  It shouldn't be too big, else
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   184
  // we're wasting space when the release rate is low.  If the release
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   185
  // rate is high, we might accumulate more than this before being
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   186
  // able to start a new transfer, but that's okay.  Also note that
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   187
  // the allocation rate and the release rate are going to be fairly
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   188
  // similar, due to how the buffers are used.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   189
  const size_t trigger_transfer = 10;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   190
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   191
  // Add to pending list. Update count first so no underflow in transfer.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   192
  size_t pending_count = Atomic::add(1u, &_pending_count);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   193
  _pending_list.push(*node);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   194
  if (pending_count > trigger_transfer) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   195
    try_transfer_pending();
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   196
  }
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   197
}
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   198
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   199
// Try to transfer nodes from _pending_list to _free_list, with a
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   200
// synchronization delay for any in-progress pops from the _free_list,
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   201
// to solve ABA there.  Return true if performed a (possibly empty)
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   202
// transfer, false if blocked from doing so by some other thread's
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   203
// in-progress transfer.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   204
bool BufferNode::Allocator::try_transfer_pending() {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   205
  // Attempt to claim the lock.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   206
  if (Atomic::load(&_transfer_lock) || // Skip CAS if likely to fail.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   207
      Atomic::cmpxchg(true, &_transfer_lock, false)) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   208
    return false;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   209
  }
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   210
  // Have the lock; perform the transfer.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   211
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   212
  // Claim all the pending nodes.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   213
  BufferNode* first = _pending_list.pop_all();
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   214
  if (first != NULL) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   215
    // Prepare to add the claimed nodes, and update _pending_count.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   216
    BufferNode* last = first;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   217
    size_t count = 1;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   218
    for (BufferNode* next = first->next(); next != NULL; next = next->next()) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   219
      last = next;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   220
      ++count;
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   221
    }
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   222
    Atomic::sub(count, &_pending_count);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   223
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   224
    // Wait for any in-progress pops, to avoid ABA for them.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   225
    GlobalCounter::write_synchronize();
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   226
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   227
    // Add synchronized nodes to _free_list.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   228
    // Update count first so no underflow in allocate().
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   229
    Atomic::add(count, &_free_count);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   230
    _free_list.prepend(*first, *last);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   231
    log_trace(gc, ptrqueue, freelist)
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   232
             ("Transferred %s pending to free: " SIZE_FORMAT, name(), count);
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   233
  }
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   234
  OrderAccess::release_store(&_transfer_lock, false);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   235
  return true;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   236
}
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   237
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   238
size_t BufferNode::Allocator::reduce_free_list(size_t remove_goal) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   239
  try_transfer_pending();
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   240
  size_t removed = 0;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   241
  for ( ; removed < remove_goal; ++removed) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   242
    BufferNode* node = _free_list.pop();
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   243
    if (node == NULL) break;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   244
    BufferNode::deallocate(node);
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   245
  }
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   246
  size_t new_count = Atomic::sub(removed, &_free_count);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   247
  log_debug(gc, ptrqueue, freelist)
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   248
           ("Reduced %s free list by " SIZE_FORMAT " to " SIZE_FORMAT,
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   249
            name(), removed, new_count);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   250
  return removed;
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   251
}
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   252
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   253
PtrQueueSet::PtrQueueSet(bool notify_when_complete) :
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   254
  _allocator(NULL),
51332
c25572739e7c 8208669: GC changes to allow enabling -Wreorder
tschatzl
parents: 47216
diff changeset
   255
  _cbl_mon(NULL),
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   256
  _completed_buffers_head(NULL),
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   257
  _completed_buffers_tail(NULL),
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   258
  _n_completed_buffers(0),
52726
9cfa2e273b77 8214278: Cleanup process_completed_threshold and related state
kbarrett
parents: 52684
diff changeset
   259
  _process_completed_buffers_threshold(ProcessCompletedBuffersThresholdNever),
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   260
  _process_completed_buffers(false),
51332
c25572739e7c 8208669: GC changes to allow enabling -Wreorder
tschatzl
parents: 47216
diff changeset
   261
  _notify_when_complete(notify_when_complete),
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   262
  _all_active(false)
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   263
{}
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   264
33761
329db4b51480 6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents: 30764
diff changeset
   265
PtrQueueSet::~PtrQueueSet() {
329db4b51480 6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents: 30764
diff changeset
   266
  // There are presently only a couple (derived) instances ever
329db4b51480 6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents: 30764
diff changeset
   267
  // created, and they are permanent, so no harm currently done by
329db4b51480 6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents: 30764
diff changeset
   268
  // doing nothing here.
329db4b51480 6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents: 30764
diff changeset
   269
}
329db4b51480 6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents: 30764
diff changeset
   270
329db4b51480 6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents: 30764
diff changeset
   271
void PtrQueueSet::initialize(Monitor* cbl_mon,
52726
9cfa2e273b77 8214278: Cleanup process_completed_threshold and related state
kbarrett
parents: 52684
diff changeset
   272
                             BufferNode::Allocator* allocator) {
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   273
  assert(cbl_mon != NULL && allocator != NULL, "Init order issue?");
33761
329db4b51480 6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents: 30764
diff changeset
   274
  _cbl_mon = cbl_mon;
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   275
  _allocator = allocator;
33761
329db4b51480 6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents: 30764
diff changeset
   276
}
329db4b51480 6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents: 30764
diff changeset
   277
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   278
void** PtrQueueSet::allocate_buffer() {
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   279
  BufferNode* node = _allocator->allocate();
36354
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   280
  return BufferNode::make_buffer_from_node(node);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   281
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   282
37065
c00d1c2ffb7c 8150676: Use BufferNode index
kbarrett
parents: 36371
diff changeset
   283
void PtrQueueSet::deallocate_buffer(BufferNode* node) {
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   284
  _allocator->release(node);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   285
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   286
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   287
void PtrQueueSet::enqueue_completed_buffer(BufferNode* cbn) {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54255
diff changeset
   288
  MutexLocker x(_cbl_mon, Mutex::_no_safepoint_check_flag);
36354
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   289
  cbn->set_next(NULL);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   290
  if (_completed_buffers_tail == NULL) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   291
    assert(_completed_buffers_head == NULL, "Well-formedness");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   292
    _completed_buffers_head = cbn;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   293
    _completed_buffers_tail = cbn;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   294
  } else {
4481
de92ec484f5e 6862387: tune concurrent refinement further
iveresov
parents: 4454
diff changeset
   295
    _completed_buffers_tail->set_next(cbn);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   296
    _completed_buffers_tail = cbn;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   297
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   298
  _n_completed_buffers++;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   299
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   300
  if (!_process_completed_buffers &&
52726
9cfa2e273b77 8214278: Cleanup process_completed_threshold and related state
kbarrett
parents: 52684
diff changeset
   301
      (_n_completed_buffers > _process_completed_buffers_threshold)) {
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   302
    _process_completed_buffers = true;
36371
fd81a4f0ea00 8139651: ConcurrentG1Refine uses ints for many of its members that should be unsigned types
jprovino
parents: 36354
diff changeset
   303
    if (_notify_when_complete) {
4481
de92ec484f5e 6862387: tune concurrent refinement further
iveresov
parents: 4454
diff changeset
   304
      _cbl_mon->notify();
36371
fd81a4f0ea00 8139651: ConcurrentG1Refine uses ints for many of its members that should be unsigned types
jprovino
parents: 36354
diff changeset
   305
    }
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   306
  }
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   307
  assert_completed_buffers_list_len_correct_locked();
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   308
}
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   309
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   310
BufferNode* PtrQueueSet::get_completed_buffer(size_t stop_at) {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54255
diff changeset
   311
  MutexLocker x(_cbl_mon, Mutex::_no_safepoint_check_flag);
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   312
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   313
  if (_n_completed_buffers <= stop_at) {
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   314
    return NULL;
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   315
  }
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   316
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   317
  assert(_n_completed_buffers > 0, "invariant");
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   318
  assert(_completed_buffers_head != NULL, "invariant");
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   319
  assert(_completed_buffers_tail != NULL, "invariant");
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   320
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   321
  BufferNode* bn = _completed_buffers_head;
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   322
  _n_completed_buffers--;
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   323
  _completed_buffers_head = bn->next();
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   324
  if (_completed_buffers_head == NULL) {
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   325
    assert(_n_completed_buffers == 0, "invariant");
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   326
    _completed_buffers_tail = NULL;
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   327
    _process_completed_buffers = false;
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   328
  }
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   329
  assert_completed_buffers_list_len_correct_locked();
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   330
  bn->set_next(NULL);
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   331
  return bn;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   332
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   333
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   334
void PtrQueueSet::abandon_completed_buffers() {
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   335
  BufferNode* buffers_to_delete = NULL;
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   336
  {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54255
diff changeset
   337
    MutexLocker x(_cbl_mon, Mutex::_no_safepoint_check_flag);
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   338
    buffers_to_delete = _completed_buffers_head;
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   339
    _completed_buffers_head = NULL;
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   340
    _completed_buffers_tail = NULL;
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   341
    _n_completed_buffers = 0;
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   342
    _process_completed_buffers = false;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   343
  }
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   344
  while (buffers_to_delete != NULL) {
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   345
    BufferNode* bn = buffers_to_delete;
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   346
    buffers_to_delete = bn->next();
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   347
    bn->set_next(NULL);
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   348
    deallocate_buffer(bn);
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   349
  }
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   350
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   351
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   352
#ifdef ASSERT
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   353
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   354
void PtrQueueSet::assert_completed_buffers_list_len_correct_locked() {
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   355
  assert_lock_strong(_cbl_mon);
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   356
  size_t n = 0;
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   357
  for (BufferNode* bn = _completed_buffers_head; bn != NULL; bn = bn->next()) {
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   358
    ++n;
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   359
  }
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   360
  assert(n == _n_completed_buffers,
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   361
         "Completed buffer length is wrong: counted: " SIZE_FORMAT
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   362
         ", expected: " SIZE_FORMAT, n, _n_completed_buffers);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   363
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   364
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   365
#endif // ASSERT
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   366
4481
de92ec484f5e 6862387: tune concurrent refinement further
iveresov
parents: 4454
diff changeset
   367
// Merge lists of buffers. Notify the processing threads.
de92ec484f5e 6862387: tune concurrent refinement further
iveresov
parents: 4454
diff changeset
   368
// The source queue is emptied as a result. The queues
2142
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   369
// must share the monitor.
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   370
void PtrQueueSet::merge_bufferlists(PtrQueueSet *src) {
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   371
  assert(_cbl_mon == src->_cbl_mon, "Should share the same lock");
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54255
diff changeset
   372
  MutexLocker x(_cbl_mon, Mutex::_no_safepoint_check_flag);
2142
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   373
  if (_completed_buffers_tail == NULL) {
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   374
    assert(_completed_buffers_head == NULL, "Well-formedness");
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   375
    _completed_buffers_head = src->_completed_buffers_head;
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   376
    _completed_buffers_tail = src->_completed_buffers_tail;
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   377
  } else {
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   378
    assert(_completed_buffers_head != NULL, "Well formedness");
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   379
    if (src->_completed_buffers_head != NULL) {
4481
de92ec484f5e 6862387: tune concurrent refinement further
iveresov
parents: 4454
diff changeset
   380
      _completed_buffers_tail->set_next(src->_completed_buffers_head);
2142
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   381
      _completed_buffers_tail = src->_completed_buffers_tail;
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   382
    }
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   383
  }
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   384
  _n_completed_buffers += src->_n_completed_buffers;
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   385
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   386
  src->_n_completed_buffers = 0;
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   387
  src->_completed_buffers_head = NULL;
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   388
  src->_completed_buffers_tail = NULL;
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   389
  src->_process_completed_buffers = false;
2142
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   390
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   391
  assert(_completed_buffers_head == NULL && _completed_buffers_tail == NULL ||
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   392
         _completed_buffers_head != NULL && _completed_buffers_tail != NULL,
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   393
         "Sanity");
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   394
  assert_completed_buffers_list_len_correct_locked();
2142
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   395
}
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   396
4481
de92ec484f5e 6862387: tune concurrent refinement further
iveresov
parents: 4454
diff changeset
   397
void PtrQueueSet::notify_if_necessary() {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 54255
diff changeset
   398
  MutexLocker x(_cbl_mon, Mutex::_no_safepoint_check_flag);
52726
9cfa2e273b77 8214278: Cleanup process_completed_threshold and related state
kbarrett
parents: 52684
diff changeset
   399
  if (_n_completed_buffers > _process_completed_buffers_threshold) {
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   400
    _process_completed_buffers = true;
4481
de92ec484f5e 6862387: tune concurrent refinement further
iveresov
parents: 4454
diff changeset
   401
    if (_notify_when_complete)
de92ec484f5e 6862387: tune concurrent refinement further
iveresov
parents: 4454
diff changeset
   402
      _cbl_mon->notify();
2142
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   403
  }
032f4652700c 6720309: G1: don't synchronously update RSet during evacuation pauses
iveresov
parents: 1623
diff changeset
   404
}