hotspot/src/share/vm/gc/shared/workgroup.cpp
author brutisso
Fri, 09 Oct 2015 20:31:56 +0200
changeset 33152 6ad7fe735042
parent 33107 77bf0d2069a3
child 34633 2a6c7c7b30a7
permissions -rw-r--r--
8139293: TestGCEventMixedWithG1ConcurrentMark.java fails after JDK-8134953 Reviewed-by: ecaspole, jwilhelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
29326
ebaa169c6dc3 8073464: GC workers do not have thread names
david
parents: 28163
diff changeset
     2
 * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1374
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1374
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: 1374
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    25
#include "precompiled.hpp"
33107
77bf0d2069a3 8134953: Make the GC ID available in a central place
brutisso
parents: 33105
diff changeset
    26
#include "gc/shared/gcId.hpp"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30584
diff changeset
    27
#include "gc/shared/workgroup.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    28
#include "memory/allocation.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    29
#include "memory/allocation.inline.hpp"
25351
7c198a690050 8044775: Improve usage of umbrella header atomic.inline.hpp.
goetz
parents: 24470
diff changeset
    30
#include "runtime/atomic.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    31
#include "runtime/os.hpp"
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
    32
#include "runtime/semaphore.hpp"
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
    33
#include "runtime/thread.inline.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// Definitions of WorkGang methods.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    37
// The current implementation will exit if the allocation
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    38
// of any worker fails.  Still, return a boolean so that
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    39
// a future implementation can possibly do a partial
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    40
// initialization of the workers and report such to the
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    41
// caller.
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    42
bool AbstractWorkGang::initialize_workers() {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    43
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  if (TraceWorkGang) {
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    45
    tty->print_cr("Constructing work gang %s with %d threads",
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    46
                  name(),
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    47
                  total_workers());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  }
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    49
  _workers = NEW_C_HEAP_ARRAY(AbstractGangWorker*, total_workers(), mtInternal);
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    50
  if (_workers == NULL) {
17087
f0b76c4c93a0 8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents: 17031
diff changeset
    51
    vm_exit_out_of_memory(0, OOM_MALLOC_ERROR, "Cannot create GangWorker array.");
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    52
    return false;
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    53
  }
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    54
  os::ThreadType worker_type;
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    55
  if (are_ConcurrentGC_threads()) {
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    56
    worker_type = os::cgc_thread;
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    57
  } else {
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    58
    worker_type = os::pgc_thread;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    59
  }
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
    60
  for (uint worker = 0; worker < total_workers(); worker += 1) {
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    61
    AbstractGangWorker* new_worker = allocate_worker(worker);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    assert(new_worker != NULL, "Failed to allocate GangWorker");
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    63
    _workers[worker] = new_worker;
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    64
    if (new_worker == NULL || !os::create_thread(new_worker, worker_type)) {
17087
f0b76c4c93a0 8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents: 17031
diff changeset
    65
      vm_exit_out_of_memory(0, OOM_MALLOC_ERROR,
f0b76c4c93a0 8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents: 17031
diff changeset
    66
              "Cannot create worker GC thread. Out of system resources.");
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    67
      return false;
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    68
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    if (!DisableStartThread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
      os::start_thread(new_worker);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  }
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    73
  return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    76
AbstractGangWorker* AbstractWorkGang::worker(uint i) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  // Array index bounds checking.
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    78
  AbstractGangWorker* result = NULL;
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    79
  assert(_workers != NULL, "No workers for indexing");
29580
a67a581cfe11 8073315: Enable gcc -Wtype-limits and fix upcoming issues.
goetz
parents: 29464
diff changeset
    80
  assert(i < total_workers(), "Worker index out of bounds");
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    81
  result = _workers[i];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  assert(result != NULL, "Indexing to null worker");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    86
void AbstractWorkGang::print_worker_threads_on(outputStream* st) const {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    87
  uint workers = total_workers();
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    88
  for (uint i = 0; i < workers; i++) {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    89
    worker(i)->print_on(st);
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    90
    st->cr();
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    91
  }
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    92
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    93
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    94
void AbstractWorkGang::threads_do(ThreadClosure* tc) const {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    95
  assert(tc != NULL, "Null ThreadClosure");
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    96
  uint workers = total_workers();
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    97
  for (uint i = 0; i < workers; i++) {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    98
    tc->do_thread(worker(i));
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    99
  }
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   100
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   101
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   102
// WorkGang dispatcher implemented with semaphores.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   103
//
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   104
// Semaphores don't require the worker threads to re-claim the lock when they wake up.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   105
// This helps lowering the latency when starting and stopping the worker threads.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   106
class SemaphoreGangTaskDispatcher : public GangTaskDispatcher {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   107
  // The task currently being dispatched to the GangWorkers.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   108
  AbstractGangTask* _task;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   109
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   110
  volatile uint _started;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   111
  volatile uint _not_finished;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   112
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   113
  // Semaphore used to start the GangWorkers.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   114
  Semaphore* _start_semaphore;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   115
  // Semaphore used to notify the coordinator that all workers are done.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   116
  Semaphore* _end_semaphore;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   117
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   118
public:
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   119
  SemaphoreGangTaskDispatcher() :
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   120
      _task(NULL),
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   121
      _started(0),
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   122
      _not_finished(0),
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   123
      _start_semaphore(new Semaphore()),
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   124
      _end_semaphore(new Semaphore())
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   125
{ }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   126
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   127
  ~SemaphoreGangTaskDispatcher() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   128
    delete _start_semaphore;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   129
    delete _end_semaphore;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   130
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   131
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   132
  void coordinator_execute_on_workers(AbstractGangTask* task, uint num_workers) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   133
    // No workers are allowed to read the state variables until they have been signaled.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   134
    _task         = task;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   135
    _not_finished = num_workers;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   136
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   137
    // Dispatch 'num_workers' number of tasks.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   138
    _start_semaphore->signal(num_workers);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   139
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   140
    // Wait for the last worker to signal the coordinator.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   141
    _end_semaphore->wait();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   142
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   143
    // No workers are allowed to read the state variables after the coordinator has been signaled.
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 32361
diff changeset
   144
    assert(_not_finished == 0, "%d not finished workers?", _not_finished);
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   145
    _task    = NULL;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   146
    _started = 0;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   147
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   148
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   149
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   150
  WorkData worker_wait_for_task() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   151
    // Wait for the coordinator to dispatch a task.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   152
    _start_semaphore->wait();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   153
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   154
    uint num_started = (uint) Atomic::add(1, (volatile jint*)&_started);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   155
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   156
    // Subtract one to get a zero-indexed worker id.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   157
    uint worker_id = num_started - 1;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   158
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   159
    return WorkData(_task, worker_id);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   160
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   161
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   162
  void worker_done_with_task() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   163
    // Mark that the worker is done with the task.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   164
    // The worker is not allowed to read the state variables after this line.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   165
    uint not_finished = (uint) Atomic::add(-1, (volatile jint*)&_not_finished);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   166
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   167
    // The last worker signals to the coordinator that all work is completed.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   168
    if (not_finished == 0) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   169
      _end_semaphore->signal();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   170
    }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   171
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   172
};
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   173
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   174
class MutexGangTaskDispatcher : public GangTaskDispatcher {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   175
  AbstractGangTask* _task;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   176
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   177
  volatile uint _started;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   178
  volatile uint _finished;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   179
  volatile uint _num_workers;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   180
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   181
  Monitor* _monitor;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   182
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   183
 public:
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   184
  MutexGangTaskDispatcher()
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   185
      : _task(NULL),
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   186
        _monitor(new Monitor(Monitor::leaf, "WorkGang dispatcher lock", false, Monitor::_safepoint_check_never)),
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   187
        _started(0),
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   188
        _finished(0),
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   189
        _num_workers(0) {}
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   190
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   191
  ~MutexGangTaskDispatcher() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   192
    delete _monitor;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   193
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   194
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   195
  void coordinator_execute_on_workers(AbstractGangTask* task, uint num_workers) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   196
    MutexLockerEx ml(_monitor, Mutex::_no_safepoint_check_flag);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   197
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   198
    _task        = task;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   199
    _num_workers = num_workers;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   200
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   201
    // Tell the workers to get to work.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   202
    _monitor->notify_all();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   203
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   204
    // Wait for them to finish.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   205
    while (_finished < _num_workers) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   206
      _monitor->wait(/* no_safepoint_check */ true);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   207
    }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   208
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   209
    _task        = NULL;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   210
    _num_workers = 0;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   211
    _started     = 0;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   212
    _finished    = 0;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   213
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   214
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   215
  WorkData worker_wait_for_task() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   216
    MonitorLockerEx ml(_monitor, Mutex::_no_safepoint_check_flag);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   217
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   218
    while (_num_workers == 0 || _started == _num_workers) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   219
      _monitor->wait(/* no_safepoint_check */ true);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   220
    }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   221
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   222
    _started++;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   223
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   224
    // Subtract one to get a zero-indexed worker id.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   225
    uint worker_id = _started - 1;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   226
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   227
    return WorkData(_task, worker_id);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   228
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   229
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   230
  void worker_done_with_task() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   231
    MonitorLockerEx ml(_monitor, Mutex::_no_safepoint_check_flag);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   232
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   233
    _finished++;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   234
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   235
    if (_finished == _num_workers) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   236
      // This will wake up all workers and not only the coordinator.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   237
      _monitor->notify_all();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   238
    }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   239
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   240
};
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   241
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   242
static GangTaskDispatcher* create_dispatcher() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   243
  if (UseSemaphoreGCThreadsSynchronization) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   244
    return new SemaphoreGangTaskDispatcher();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   245
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   246
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   247
  return new MutexGangTaskDispatcher();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   248
}
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   249
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   250
WorkGang::WorkGang(const char* name,
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   251
                   uint  workers,
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   252
                   bool  are_GC_task_threads,
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   253
                   bool  are_ConcurrentGC_threads) :
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   254
    AbstractWorkGang(name, workers, are_GC_task_threads, are_ConcurrentGC_threads),
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   255
    _dispatcher(create_dispatcher())
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   256
{ }
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   257
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   258
AbstractGangWorker* WorkGang::allocate_worker(uint worker_id) {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   259
  return new GangWorker(this, worker_id);
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   260
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   261
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
void WorkGang::run_task(AbstractGangTask* task) {
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   263
  _dispatcher->coordinator_execute_on_workers(task, active_workers());
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   264
}
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   265
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   266
AbstractGangWorker::AbstractGangWorker(AbstractWorkGang* gang, uint id) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  _gang = gang;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  set_id(id);
29464
02c245ad3ec9 8073545: Use shorter and more descriptive names for GC worker threads
david
parents: 29326
diff changeset
   269
  set_name("%s#%d", gang->name(), id);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   272
void AbstractGangWorker::run() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  loop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   277
void AbstractGangWorker::initialize() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  this->initialize_thread_local_storage();
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 11396
diff changeset
   279
  this->record_stack_base_and_size();
29326
ebaa169c6dc3 8073464: GC workers do not have thread names
david
parents: 28163
diff changeset
   280
  this->initialize_named_thread();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  assert(_gang != NULL, "No gang to run in");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  os::set_priority(this, NearMaxPriority);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  if (TraceWorkGang) {
30584
821c80d31b43 8079337: Format string issues in workgroup.cpp and taskqueue.cpp
david
parents: 29580
diff changeset
   284
    tty->print_cr("Running gang worker for gang %s id %u",
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
                  gang()->name(), id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  // The VM thread should not execute here because MutexLocker's are used
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  // as (opposed to MutexLockerEx's).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
  assert(!Thread::current()->is_VM_thread(), "VM thread should not be part"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
         " of a work gang");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   293
bool AbstractGangWorker::is_GC_task_thread() const {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   294
  return gang()->are_GC_task_threads();
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   295
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   296
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   297
bool AbstractGangWorker::is_ConcurrentGC_thread() const {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   298
  return gang()->are_ConcurrentGC_threads();
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   299
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   300
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   301
void AbstractGangWorker::print_on(outputStream* st) const {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   302
  st->print("\"%s\" ", name());
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   303
  Thread::print_on(st);
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   304
  st->cr();
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   305
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   306
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   307
WorkData GangWorker::wait_for_task() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   308
  return gang()->dispatcher()->worker_wait_for_task();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   309
}
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   310
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   311
void GangWorker::signal_task_done() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   312
  gang()->dispatcher()->worker_done_with_task();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   313
}
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   314
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   315
void GangWorker::print_task_started(WorkData data) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   316
  if (TraceWorkGang) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   317
    tty->print_cr("Running work gang %s task %s worker %u", name(), data._task->name(), data._worker_id);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   318
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   319
}
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   320
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   321
void GangWorker::print_task_done(WorkData data) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   322
  if (TraceWorkGang) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   323
    tty->print_cr("\nFinished work gang %s task %s worker %u", name(), data._task->name(), data._worker_id);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   324
    Thread* me = Thread::current();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   325
    tty->print_cr("  T: " PTR_FORMAT "  VM_thread: %d", p2i(me), me->is_VM_thread());
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   326
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   327
}
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   328
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   329
void GangWorker::run_task(WorkData data) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   330
  print_task_started(data);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   331
33107
77bf0d2069a3 8134953: Make the GC ID available in a central place
brutisso
parents: 33105
diff changeset
   332
  GCIdMark gc_id_mark(data._task->gc_id());
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   333
  data._task->work(data._worker_id);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   334
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   335
  print_task_done(data);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   336
}
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   337
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
void GangWorker::loop() {
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   339
  while (true) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   340
    WorkData data = wait_for_task();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   341
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   342
    run_task(data);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   343
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   344
    signal_task_done();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
// *** WorkGangBarrierSync
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
WorkGangBarrierSync::WorkGangBarrierSync()
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 27880
diff changeset
   351
  : _monitor(Mutex::safepoint, "work gang barrier sync", true,
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 27880
diff changeset
   352
             Monitor::_safepoint_check_never),
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   353
    _n_workers(0), _n_completed(0), _should_reset(false), _aborted(false) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   356
WorkGangBarrierSync::WorkGangBarrierSync(uint n_workers, const char* name)
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 27880
diff changeset
   357
  : _monitor(Mutex::safepoint, name, true, Monitor::_safepoint_check_never),
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   358
    _n_workers(n_workers), _n_completed(0), _should_reset(false), _aborted(false) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   361
void WorkGangBarrierSync::set_n_workers(uint n_workers) {
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   362
  _n_workers    = n_workers;
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   363
  _n_completed  = 0;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   364
  _should_reset = false;
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   365
  _aborted      = false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   368
bool WorkGangBarrierSync::enter() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
  MutexLockerEx x(monitor(), Mutex::_no_safepoint_check_flag);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   370
  if (should_reset()) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   371
    // The should_reset() was set and we are the first worker to enter
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   372
    // the sync barrier. We will zero the n_completed() count which
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   373
    // effectively resets the barrier.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   374
    zero_completed();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   375
    set_should_reset(false);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   376
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  inc_completed();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  if (n_completed() == n_workers()) {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   379
    // At this point we would like to reset the barrier to be ready in
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   380
    // case it is used again. However, we cannot set n_completed() to
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   381
    // 0, even after the notify_all(), given that some other workers
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   382
    // might still be waiting for n_completed() to become ==
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   383
    // n_workers(). So, if we set n_completed() to 0, those workers
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   384
    // will get stuck (as they will wake up, see that n_completed() !=
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   385
    // n_workers() and go back to sleep). Instead, we raise the
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   386
    // should_reset() flag and the barrier will be reset the first
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   387
    // time a worker enters it again.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   388
    set_should_reset(true);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
    monitor()->notify_all();
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   390
  } else {
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   391
    while (n_completed() != n_workers() && !aborted()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
      monitor()->wait(/* no_safepoint_check */ true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
  }
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   395
  return !aborted();
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   396
}
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   397
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   398
void WorkGangBarrierSync::abort() {
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   399
  MutexLockerEx x(monitor(), Mutex::_no_safepoint_check_flag);
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   400
  set_aborted();
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   401
  monitor()->notify_all();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
// SubTasksDone functions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   406
SubTasksDone::SubTasksDone(uint n) :
30869
d5cbedffb50b 8080111: Remove SubTaskDone::_n_threads
stefank
parents: 30764
diff changeset
   407
  _n_tasks(n), _tasks(NULL) {
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 11396
diff changeset
   408
  _tasks = NEW_C_HEAP_ARRAY(uint, n, mtInternal);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  guarantee(_tasks != NULL, "alloc failure");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
bool SubTasksDone::valid() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
  return _tasks != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
void SubTasksDone::clear() {
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   418
  for (uint i = 0; i < _n_tasks; i++) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
    _tasks[i] = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
  _threads_completed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
  _claimed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   427
bool SubTasksDone::is_task_claimed(uint t) {
29580
a67a581cfe11 8073315: Enable gcc -Wtype-limits and fix upcoming issues.
goetz
parents: 29464
diff changeset
   428
  assert(t < _n_tasks, "bad task id.");
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   429
  uint old = _tasks[t];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
  if (old == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
    old = Atomic::cmpxchg(1, &_tasks[t], 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  assert(_tasks[t] == 1, "What else?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  bool res = old != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
  if (!res) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
    assert(_claimed < _n_tasks, "Too many tasks claimed; missing clear?");
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   438
    Atomic::inc((volatile jint*) &_claimed);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
30869
d5cbedffb50b 8080111: Remove SubTaskDone::_n_threads
stefank
parents: 30764
diff changeset
   444
void SubTasksDone::all_tasks_completed(uint n_threads) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  jint observed = _threads_completed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
  jint old;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
    old = observed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
    observed = Atomic::cmpxchg(old+1, &_threads_completed, old);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
  } while (observed != old);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
  // If this was the last thread checking in, clear the tasks.
30869
d5cbedffb50b 8080111: Remove SubTaskDone::_n_threads
stefank
parents: 30764
diff changeset
   452
  uint adjusted_thread_count = (n_threads == 0 ? 1 : n_threads);
d5cbedffb50b 8080111: Remove SubTaskDone::_n_threads
stefank
parents: 30764
diff changeset
   453
  if (observed + 1 == (jint)adjusted_thread_count) {
d5cbedffb50b 8080111: Remove SubTaskDone::_n_threads
stefank
parents: 30764
diff changeset
   454
    clear();
d5cbedffb50b 8080111: Remove SubTaskDone::_n_threads
stefank
parents: 30764
diff changeset
   455
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
SubTasksDone::~SubTasksDone() {
27880
afb974a04396 8060074: os::free() takes MemoryTrackingLevel but doesn't need it
coleenp
parents: 25351
diff changeset
   460
  if (_tasks != NULL) FREE_C_HEAP_ARRAY(jint, _tasks);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
// *** SequentialSubTasksDone
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
void SequentialSubTasksDone::clear() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
  _n_tasks   = _n_claimed   = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
  _n_threads = _n_completed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
bool SequentialSubTasksDone::valid() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  return _n_threads > 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   474
bool SequentialSubTasksDone::is_task_claimed(uint& t) {
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   475
  uint* n_claimed_ptr = &_n_claimed;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  t = *n_claimed_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
  while (t < _n_tasks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
    jint res = Atomic::cmpxchg(t+1, n_claimed_ptr, t);
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   479
    if (res == (jint)t) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
    t = *n_claimed_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
bool SequentialSubTasksDone::all_tasks_completed() {
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   488
  uint* n_completed_ptr = &_n_completed;
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   489
  uint  complete        = *n_completed_ptr;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
  while (true) {
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   491
    uint res = Atomic::cmpxchg(complete+1, n_completed_ptr, complete);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
    if (res == complete) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
    complete = res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  if (complete+1 == _n_threads) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
    clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
}
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   503
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   504
bool FreeIdSet::_stat_init = false;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   505
FreeIdSet* FreeIdSet::_sets[NSets];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   506
bool FreeIdSet::_safepoint;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   507
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   508
FreeIdSet::FreeIdSet(int sz, Monitor* mon) :
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   509
  _sz(sz), _mon(mon), _hd(0), _waiters(0), _index(-1), _claimed(0)
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   510
{
17376
4ee999c3c007 8012902: remove use of global operator new - take 2
minqi
parents: 17087
diff changeset
   511
  _ids = NEW_C_HEAP_ARRAY(int, sz, mtInternal);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   512
  for (int i = 0; i < sz; i++) _ids[i] = i+1;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   513
  _ids[sz-1] = end_of_list; // end of list.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   514
  if (_stat_init) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   515
    for (int j = 0; j < NSets; j++) _sets[j] = NULL;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   516
    _stat_init = true;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   517
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   518
  // Add to sets.  (This should happen while the system is still single-threaded.)
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   519
  for (int j = 0; j < NSets; j++) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   520
    if (_sets[j] == NULL) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   521
      _sets[j] = this;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   522
      _index = j;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   523
      break;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   524
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   525
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   526
  guarantee(_index != -1, "Too many FreeIdSets in use!");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   527
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   528
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   529
FreeIdSet::~FreeIdSet() {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   530
  _sets[_index] = NULL;
27880
afb974a04396 8060074: os::free() takes MemoryTrackingLevel but doesn't need it
coleenp
parents: 25351
diff changeset
   531
  FREE_C_HEAP_ARRAY(int, _ids);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   532
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   533
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   534
void FreeIdSet::set_safepoint(bool b) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   535
  _safepoint = b;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   536
  if (b) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   537
    for (int j = 0; j < NSets; j++) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   538
      if (_sets[j] != NULL && _sets[j]->_waiters > 0) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   539
        Monitor* mon = _sets[j]->_mon;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   540
        mon->lock_without_safepoint_check();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   541
        mon->notify_all();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   542
        mon->unlock();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   543
      }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   544
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   545
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   546
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   547
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   548
#define FID_STATS 0
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   549
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   550
int FreeIdSet::claim_par_id() {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   551
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   552
  thread_t tslf = thr_self();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   553
  tty->print("claim_par_id[%d]: sz = %d, claimed = %d\n", tslf, _sz, _claimed);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   554
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   555
  MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   556
  while (!_safepoint && _hd == end_of_list) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   557
    _waiters++;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   558
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   559
    if (_waiters > 5) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   560
      tty->print("claim_par_id waiting[%d]: %d waiters, %d claimed.\n",
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   561
                 tslf, _waiters, _claimed);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   562
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   563
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   564
    _mon->wait(Mutex::_no_safepoint_check_flag);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   565
    _waiters--;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   566
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   567
  if (_hd == end_of_list) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   568
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   569
    tty->print("claim_par_id[%d]: returning EOL.\n", tslf);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   570
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   571
    return -1;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   572
  } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   573
    int res = _hd;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   574
    _hd = _ids[res];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   575
    _ids[res] = claimed;  // For debugging.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   576
    _claimed++;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   577
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   578
    tty->print("claim_par_id[%d]: returning %d, claimed = %d.\n",
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   579
               tslf, res, _claimed);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   580
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   581
    return res;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   582
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   583
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   584
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   585
bool FreeIdSet::claim_perm_id(int i) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   586
  assert(0 <= i && i < _sz, "Out of range.");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   587
  MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   588
  int prev = end_of_list;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   589
  int cur = _hd;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   590
  while (cur != end_of_list) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   591
    if (cur == i) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   592
      if (prev == end_of_list) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   593
        _hd = _ids[cur];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   594
      } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   595
        _ids[prev] = _ids[cur];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   596
      }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   597
      _ids[cur] = claimed;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   598
      _claimed++;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   599
      return true;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   600
    } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   601
      prev = cur;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   602
      cur = _ids[cur];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   603
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   604
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   605
  return false;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   606
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   607
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   608
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   609
void FreeIdSet::release_par_id(int id) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   610
  MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   611
  assert(_ids[id] == claimed, "Precondition.");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   612
  _ids[id] = _hd;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   613
  _hd = id;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   614
  _claimed--;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   615
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   616
  tty->print("[%d] release_par_id(%d), waiters =%d,  claimed = %d.\n",
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   617
             thr_self(), id, _waiters, _claimed);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   618
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   619
  if (_waiters > 0)
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   620
    // Notify all would be safer, but this is OK, right?
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   621
    _mon->notify_all();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   622
}