hotspot/src/share/vm/utilities/workgroup.cpp
author goetz
Mon, 16 Feb 2015 14:07:36 +0100
changeset 29580 a67a581cfe11
parent 29464 02c245ad3ec9
child 30584 821c80d31b43
permissions -rw-r--r--
8073315: Enable gcc -Wtype-limits and fix upcoming issues. Summary: Relevant fixes in blockOffsetTable.cpp, os_linux.cpp, parCardTableModRefBS.cpp. Reviewed-by: jwilhelm, kbarrett, simonis
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"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    26
#include "memory/allocation.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    27
#include "memory/allocation.inline.hpp"
25351
7c198a690050 8044775: Improve usage of umbrella header atomic.inline.hpp.
goetz
parents: 24470
diff changeset
    28
#include "runtime/atomic.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    29
#include "runtime/os.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    30
#include "utilities/workgroup.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 17376
diff changeset
    32
PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 17376
diff changeset
    33
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// Definitions of WorkGang methods.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
AbstractWorkGang::AbstractWorkGang(const char* name,
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    37
                                   bool  are_GC_task_threads,
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    38
                                   bool  are_ConcurrentGC_threads) :
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  _name(name),
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    40
  _are_GC_task_threads(are_GC_task_threads),
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    41
  _are_ConcurrentGC_threads(are_ConcurrentGC_threads) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    42
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    43
  assert(!(are_GC_task_threads && are_ConcurrentGC_threads),
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    44
         "They cannot both be STW GC and Concurrent threads" );
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    45
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  // Other initialization.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  _monitor = new Monitor(/* priority */       Mutex::leaf,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
                         /* name */           "WorkGroup monitor",
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 27880
diff changeset
    49
                         /* allow_vm_block */ are_GC_task_threads,
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 27880
diff changeset
    50
                                              Monitor::_safepoint_check_sometimes);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  assert(monitor() != NULL, "Failed to allocate monitor");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  _terminate = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  _task = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  _sequence_number = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  _started_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  _finished_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
WorkGang::WorkGang(const char* name,
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
    60
                   uint        workers,
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    61
                   bool        are_GC_task_threads,
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    62
                   bool        are_ConcurrentGC_threads) :
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    63
  AbstractWorkGang(name, are_GC_task_threads, are_ConcurrentGC_threads) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  _total_workers = workers;
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    65
}
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    66
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
    67
GangWorker* WorkGang::allocate_worker(uint which) {
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    68
  GangWorker* new_worker = new GangWorker(this, which);
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    69
  return new_worker;
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    70
}
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    71
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    72
// The current implementation will exit if the allocation
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    73
// of any worker fails.  Still, return a boolean so that
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    74
// a future implementation can possibly do a partial
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    75
// initialization of the workers and report such to the
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    76
// caller.
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    77
bool WorkGang::initialize_workers() {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    78
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  if (TraceWorkGang) {
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    80
    tty->print_cr("Constructing work gang %s with %d threads",
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    81
                  name(),
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    82
                  total_workers());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  }
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 11396
diff changeset
    84
  _gang_workers = NEW_C_HEAP_ARRAY(GangWorker*, total_workers(), mtInternal);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    85
  if (gang_workers() == NULL) {
17087
f0b76c4c93a0 8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents: 17031
diff changeset
    86
    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
    87
    return false;
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    88
  }
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    89
  os::ThreadType worker_type;
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    90
  if (are_ConcurrentGC_threads()) {
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    91
    worker_type = os::cgc_thread;
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    92
  } else {
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    93
    worker_type = os::pgc_thread;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    94
  }
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
    95
  for (uint worker = 0; worker < total_workers(); worker += 1) {
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    96
    GangWorker* new_worker = allocate_worker(worker);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    assert(new_worker != NULL, "Failed to allocate GangWorker");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    _gang_workers[worker] = new_worker;
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    99
    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
   100
      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
   101
              "Cannot create worker GC thread. Out of system resources.");
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
   102
      return false;
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
   103
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    if (!DisableStartThread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
      os::start_thread(new_worker);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  }
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
   108
  return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
AbstractWorkGang::~AbstractWorkGang() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    tty->print_cr("Destructing work gang %s", name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  stop();   // stop all the workers
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   116
  for (uint worker = 0; worker < total_workers(); worker += 1) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    delete gang_worker(worker);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  delete gang_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  delete monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   123
GangWorker* AbstractWorkGang::gang_worker(uint i) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  // Array index bounds checking.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  GangWorker* result = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  assert(gang_workers() != NULL, "No workers for indexing");
29580
a67a581cfe11 8073315: Enable gcc -Wtype-limits and fix upcoming issues.
goetz
parents: 29464
diff changeset
   127
  assert(i < total_workers(), "Worker index out of bounds");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  result = _gang_workers[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  assert(result != NULL, "Indexing to null worker");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
void WorkGang::run_task(AbstractGangTask* task) {
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   134
  run_task(task, total_workers());
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   135
}
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   136
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   137
void WorkGang::run_task(AbstractGangTask* task, uint no_of_parallel_workers) {
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   138
  task->set_for_termination(no_of_parallel_workers);
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   139
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  // This thread is executed by the VM thread which does not block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  // on ordinary MutexLocker's.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    tty->print_cr("Running work gang %s task %s", name(), task->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  // Tell all the workers to run a task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  assert(task != NULL, "Running a null task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  // Initialize.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  _task = task;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  _sequence_number += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  _started_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  _finished_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  // Tell the workers to get to work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  // Wait for them to be finished
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   156
  while (finished_workers() < no_of_parallel_workers) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
      tty->print_cr("Waiting in work gang %s: %d/%d finished sequence %d",
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   159
                    name(), finished_workers(), no_of_parallel_workers,
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
                    _sequence_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
    monitor()->wait(/* no_safepoint_check */ true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  _task = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  if (TraceWorkGang) {
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   166
    tty->print_cr("\nFinished work gang %s: %d/%d sequence %d",
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   167
                  name(), finished_workers(), no_of_parallel_workers,
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
                  _sequence_number);
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   169
    Thread* me = Thread::current();
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   170
    tty->print_cr("  T: 0x%x  VM_thread: %d", me, me->is_VM_thread());
8688
493d12ccc6db 6668573: CMS: reference processing crash if ParallelCMSThreads > ParallelGCThreads
ysr
parents: 7397
diff changeset
   171
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   174
void FlexibleWorkGang::run_task(AbstractGangTask* task) {
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   175
  // If active_workers() is passed, _finished_workers
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   176
  // must only be incremented for workers that find non_null
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   177
  // work (as opposed to all those that just check that the
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   178
  // task is not null).
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   179
  WorkGang::run_task(task, (uint) active_workers());
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   180
}
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   181
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
void AbstractWorkGang::stop() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  // Tell all workers to terminate, then wait for them to become inactive.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    tty->print_cr("Stopping work gang %s task %s", name(), task()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  _task = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  _terminate = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  monitor()->notify_all();
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   191
  while (finished_workers() < active_workers()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
      tty->print_cr("Waiting in work gang %s: %d/%d finished",
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   194
                    name(), finished_workers(), active_workers());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    monitor()->wait(/* no_safepoint_check */ true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
void AbstractWorkGang::internal_worker_poll(WorkData* data) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  assert(monitor()->owned_by_self(), "worker_poll is an internal method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  assert(data != NULL, "worker data is null");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  data->set_terminate(terminate());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  data->set_task(task());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  data->set_sequence_number(sequence_number());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
void AbstractWorkGang::internal_note_start() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  assert(monitor()->owned_by_self(), "note_finish is an internal method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  _started_workers += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
void AbstractWorkGang::internal_note_finish() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  assert(monitor()->owned_by_self(), "note_finish is an internal method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  _finished_workers += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
void AbstractWorkGang::print_worker_threads_on(outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  uint    num_thr = total_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  for (uint i = 0; i < num_thr; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    gang_worker(i)->print_on(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
    st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
void AbstractWorkGang::threads_do(ThreadClosure* tc) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  assert(tc != NULL, "Null ThreadClosure");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  uint num_thr = total_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  for (uint i = 0; i < num_thr; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    tc->do_thread(gang_worker(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
// GangWorker methods.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
GangWorker::GangWorker(AbstractWorkGang* gang, uint id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  _gang = gang;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  set_id(id);
29464
02c245ad3ec9 8073545: Use shorter and more descriptive names for GC worker threads
david
parents: 29326
diff changeset
   239
  set_name("%s#%d", gang->name(), id);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
void GangWorker::run() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  loop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
void GangWorker::initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
  this->initialize_thread_local_storage();
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 11396
diff changeset
   249
  this->record_stack_base_and_size();
29326
ebaa169c6dc3 8073464: GC workers do not have thread names
david
parents: 28163
diff changeset
   250
  this->initialize_named_thread();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  assert(_gang != NULL, "No gang to run in");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  os::set_priority(this, NearMaxPriority);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
    tty->print_cr("Running gang worker for gang %s id %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
                  gang()->name(), id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  // The VM thread should not execute here because MutexLocker's are used
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
  // as (opposed to MutexLockerEx's).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  assert(!Thread::current()->is_VM_thread(), "VM thread should not be part"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
         " of a work gang");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
void GangWorker::loop() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  int previous_sequence_number = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  Monitor* gang_monitor = gang()->monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
  for ( ; /* !terminate() */; ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
    WorkData data;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
    int part;  // Initialized below.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
      // Grab the gang mutex.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
      MutexLocker ml(gang_monitor);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
      // Wait for something to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
      // Polling outside the while { wait } avoids missed notifies
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
      // in the outer loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
      gang()->internal_worker_poll(&data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
      if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
        tty->print("Polled outside for work in gang %s worker %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
                   gang()->name(), id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
        tty->print("  terminate: %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
                   data.terminate() ? "true" : "false");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
        tty->print("  sequence: %d (prev: %d)",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
                   data.sequence_number(), previous_sequence_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
        if (data.task() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
          tty->print("  task: %s", data.task()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
          tty->print("  task: NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
        tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
      for ( ; /* break or return */; ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
        // Terminate if requested.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
        if (data.terminate()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
          gang()->internal_note_finish();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
          gang_monitor->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
          return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
        // Check for new work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
        if ((data.task() != NULL) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
            (data.sequence_number() != previous_sequence_number)) {
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   300
          if (gang()->needs_more_workers()) {
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   301
            gang()->internal_note_start();
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   302
            gang_monitor->notify_all();
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   303
            part = gang()->started_workers() - 1;
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   304
            break;
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   305
          }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
        // Nothing to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
        gang_monitor->wait(/* no_safepoint_check */ true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
        gang()->internal_worker_poll(&data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
        if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
          tty->print("Polled inside for work in gang %s worker %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
                     gang()->name(), id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
          tty->print("  terminate: %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
                     data.terminate() ? "true" : "false");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
          tty->print("  sequence: %d (prev: %d)",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
                     data.sequence_number(), previous_sequence_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
          if (data.task() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
            tty->print("  task: %s", data.task()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
          } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
            tty->print("  task: NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
          tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
      // Drop gang mutex.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
      tty->print("Work for work gang %s id %d task %s part %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
                 gang()->name(), id(), data.task()->name(), part);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    assert(data.task() != NULL, "Got null task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
    data.task()->work(part);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
      if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
        tty->print("Finish for work gang %s id %d task %s part %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
                   gang()->name(), id(), data.task()->name(), part);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
      // Grab the gang mutex.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
      MutexLocker ml(gang_monitor);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
      gang()->internal_note_finish();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
      // Tell the gang you are done.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
      gang_monitor->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
      // Drop the gang mutex.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
    previous_sequence_number = data.sequence_number();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
bool GangWorker::is_GC_task_thread() const {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   350
  return gang()->are_GC_task_threads();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   351
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   352
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   353
bool GangWorker::is_ConcurrentGC_thread() const {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   354
  return gang()->are_ConcurrentGC_threads();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
void GangWorker::print_on(outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  st->print("\"%s\" ", name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
  Thread::print_on(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
  st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
// Printing methods
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
const char* AbstractWorkGang::name() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  return _name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
const char* AbstractGangTask::name() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  return _name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
#endif /* PRODUCT */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   377
// FlexibleWorkGang
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   378
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   379
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
// *** WorkGangBarrierSync
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
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
   383
  : _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
   384
             Monitor::_safepoint_check_never),
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   385
    _n_workers(0), _n_completed(0), _should_reset(false), _aborted(false) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   388
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
   389
  : _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
   390
    _n_workers(n_workers), _n_completed(0), _should_reset(false), _aborted(false) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   393
void WorkGangBarrierSync::set_n_workers(uint n_workers) {
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   394
  _n_workers    = n_workers;
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   395
  _n_completed  = 0;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   396
  _should_reset = false;
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   397
  _aborted      = false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   400
bool WorkGangBarrierSync::enter() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
  MutexLockerEx x(monitor(), Mutex::_no_safepoint_check_flag);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   402
  if (should_reset()) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   403
    // 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
   404
    // 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
   405
    // effectively resets the barrier.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   406
    zero_completed();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   407
    set_should_reset(false);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   408
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  inc_completed();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  if (n_completed() == n_workers()) {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   411
    // 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
   412
    // 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
   413
    // 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
   414
    // might still be waiting for n_completed() to become ==
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   415
    // 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
   416
    // 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
   417
    // 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
   418
    // 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
   419
    // time a worker enters it again.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   420
    set_should_reset(true);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
    monitor()->notify_all();
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   422
  } else {
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   423
    while (n_completed() != n_workers() && !aborted()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
      monitor()->wait(/* no_safepoint_check */ true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  }
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   427
  return !aborted();
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   428
}
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   429
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   430
void WorkGangBarrierSync::abort() {
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   431
  MutexLockerEx x(monitor(), Mutex::_no_safepoint_check_flag);
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   432
  set_aborted();
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   433
  monitor()->notify_all();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
// SubTasksDone functions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   438
SubTasksDone::SubTasksDone(uint n) :
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
  _n_tasks(n), _n_threads(1), _tasks(NULL) {
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 11396
diff changeset
   440
  _tasks = NEW_C_HEAP_ARRAY(uint, n, mtInternal);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
  guarantee(_tasks != NULL, "alloc failure");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
  clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
bool SubTasksDone::valid() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
  return _tasks != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   449
void SubTasksDone::set_n_threads(uint t) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
  assert(_claimed == 0 || _threads_completed == _n_threads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
         "should not be called while tasks are being processed!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
  _n_threads = (t == 0 ? 1 : t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
void SubTasksDone::clear() {
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   456
  for (uint i = 0; i < _n_tasks; i++) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
    _tasks[i] = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
  _threads_completed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
  _claimed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   465
bool SubTasksDone::is_task_claimed(uint t) {
29580
a67a581cfe11 8073315: Enable gcc -Wtype-limits and fix upcoming issues.
goetz
parents: 29464
diff changeset
   466
  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
   467
  uint old = _tasks[t];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
  if (old == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
    old = Atomic::cmpxchg(1, &_tasks[t], 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  assert(_tasks[t] == 1, "What else?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  bool res = old != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  if (!res) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
    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
   476
    Atomic::inc((volatile jint*) &_claimed);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
void SubTasksDone::all_tasks_completed() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  jint observed = _threads_completed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  jint old;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
  do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
    old = observed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
    observed = Atomic::cmpxchg(old+1, &_threads_completed, old);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  } while (observed != old);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  // If this was the last thread checking in, clear the tasks.
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   490
  if (observed+1 == (jint)_n_threads) clear();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
SubTasksDone::~SubTasksDone() {
27880
afb974a04396 8060074: os::free() takes MemoryTrackingLevel but doesn't need it
coleenp
parents: 25351
diff changeset
   495
  if (_tasks != NULL) FREE_C_HEAP_ARRAY(jint, _tasks);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
// *** SequentialSubTasksDone
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
void SequentialSubTasksDone::clear() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
  _n_tasks   = _n_claimed   = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
  _n_threads = _n_completed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
bool SequentialSubTasksDone::valid() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
  return _n_threads > 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   509
bool SequentialSubTasksDone::is_task_claimed(uint& t) {
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   510
  uint* n_claimed_ptr = &_n_claimed;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
  t = *n_claimed_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
  while (t < _n_tasks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
    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
   514
    if (res == (jint)t) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
    t = *n_claimed_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
bool SequentialSubTasksDone::all_tasks_completed() {
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   523
  uint* n_completed_ptr = &_n_completed;
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   524
  uint  complete        = *n_completed_ptr;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
  while (true) {
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   526
    uint res = Atomic::cmpxchg(complete+1, n_completed_ptr, complete);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
    if (res == complete) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
    complete = res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
  if (complete+1 == _n_threads) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
    clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
}
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   538
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   539
bool FreeIdSet::_stat_init = false;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   540
FreeIdSet* FreeIdSet::_sets[NSets];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   541
bool FreeIdSet::_safepoint;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   542
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   543
FreeIdSet::FreeIdSet(int sz, Monitor* mon) :
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   544
  _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
   545
{
17376
4ee999c3c007 8012902: remove use of global operator new - take 2
minqi
parents: 17087
diff changeset
   546
  _ids = NEW_C_HEAP_ARRAY(int, sz, mtInternal);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   547
  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
   548
  _ids[sz-1] = end_of_list; // end of list.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   549
  if (_stat_init) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   550
    for (int j = 0; j < NSets; j++) _sets[j] = NULL;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   551
    _stat_init = true;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   552
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   553
  // 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
   554
  for (int j = 0; j < NSets; j++) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   555
    if (_sets[j] == NULL) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   556
      _sets[j] = this;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   557
      _index = j;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   558
      break;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   559
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   560
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   561
  guarantee(_index != -1, "Too many FreeIdSets in use!");
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
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   564
FreeIdSet::~FreeIdSet() {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   565
  _sets[_index] = NULL;
27880
afb974a04396 8060074: os::free() takes MemoryTrackingLevel but doesn't need it
coleenp
parents: 25351
diff changeset
   566
  FREE_C_HEAP_ARRAY(int, _ids);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   567
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   568
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   569
void FreeIdSet::set_safepoint(bool b) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   570
  _safepoint = b;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   571
  if (b) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   572
    for (int j = 0; j < NSets; j++) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   573
      if (_sets[j] != NULL && _sets[j]->_waiters > 0) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   574
        Monitor* mon = _sets[j]->_mon;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   575
        mon->lock_without_safepoint_check();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   576
        mon->notify_all();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   577
        mon->unlock();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   578
      }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   579
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   580
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   581
}
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
#define FID_STATS 0
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
int FreeIdSet::claim_par_id() {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   586
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   587
  thread_t tslf = thr_self();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   588
  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
   589
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   590
  MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   591
  while (!_safepoint && _hd == end_of_list) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   592
    _waiters++;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   593
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   594
    if (_waiters > 5) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   595
      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
   596
                 tslf, _waiters, _claimed);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   597
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   598
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   599
    _mon->wait(Mutex::_no_safepoint_check_flag);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   600
    _waiters--;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   601
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   602
  if (_hd == end_of_list) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   603
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   604
    tty->print("claim_par_id[%d]: returning EOL.\n", tslf);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   605
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   606
    return -1;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   607
  } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   608
    int res = _hd;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   609
    _hd = _ids[res];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   610
    _ids[res] = claimed;  // For debugging.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   611
    _claimed++;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   612
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   613
    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
   614
               tslf, res, _claimed);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   615
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   616
    return res;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   617
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   618
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   619
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   620
bool FreeIdSet::claim_perm_id(int i) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   621
  assert(0 <= i && i < _sz, "Out of range.");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   622
  MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   623
  int prev = end_of_list;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   624
  int cur = _hd;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   625
  while (cur != end_of_list) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   626
    if (cur == i) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   627
      if (prev == end_of_list) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   628
        _hd = _ids[cur];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   629
      } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   630
        _ids[prev] = _ids[cur];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   631
      }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   632
      _ids[cur] = claimed;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   633
      _claimed++;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   634
      return true;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   635
    } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   636
      prev = cur;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   637
      cur = _ids[cur];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   638
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   639
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   640
  return false;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   641
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   642
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   643
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   644
void FreeIdSet::release_par_id(int id) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   645
  MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   646
  assert(_ids[id] == claimed, "Precondition.");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   647
  _ids[id] = _hd;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   648
  _hd = id;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   649
  _claimed--;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   650
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   651
  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
   652
             thr_self(), id, _waiters, _claimed);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   653
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   654
  if (_waiters > 0)
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   655
    // Notify all would be safer, but this is OK, right?
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   656
    _mon->notify_all();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   657
}