hotspot/src/share/vm/utilities/workgroup.cpp
author trims
Thu, 27 May 2010 19:08:38 -0700
changeset 5547 f4b087cbb361
parent 1374 4c24294029a9
child 6759 67b1a69ef5aa
permissions -rw-r--r--
6941466: Oracle rebranding changes for Hotspot repositories Summary: Change all the Sun copyrights to Oracle copyright Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1374
diff changeset
     2
 * Copyright (c) 2001, 2007, 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
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_workgroup.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// Definitions of WorkGang methods.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
AbstractWorkGang::AbstractWorkGang(const char* name,
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    31
                                   bool  are_GC_task_threads,
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    32
                                   bool  are_ConcurrentGC_threads) :
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  _name(name),
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    34
  _are_GC_task_threads(are_GC_task_threads),
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    35
  _are_ConcurrentGC_threads(are_ConcurrentGC_threads) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    36
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    37
  assert(!(are_GC_task_threads && are_ConcurrentGC_threads),
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    38
         "They cannot both be STW GC and Concurrent threads" );
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    39
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  // Other initialization.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  _monitor = new Monitor(/* priority */       Mutex::leaf,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
                         /* name */           "WorkGroup monitor",
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    43
                         /* allow_vm_block */ are_GC_task_threads);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  assert(monitor() != NULL, "Failed to allocate monitor");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  _terminate = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  _task = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  _sequence_number = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  _started_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  _finished_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
WorkGang::WorkGang(const char* name,
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    53
                   int         workers,
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    54
                   bool        are_GC_task_threads,
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    55
                   bool        are_ConcurrentGC_threads) :
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    56
  AbstractWorkGang(name, are_GC_task_threads, are_ConcurrentGC_threads)
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    57
{
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  // Save arguments.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  _total_workers = workers;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    60
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    tty->print_cr("Constructing work gang %s with %d threads", name, workers);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  _gang_workers = NEW_C_HEAP_ARRAY(GangWorker*, workers);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    65
  if (gang_workers() == NULL) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    66
    vm_exit_out_of_memory(0, "Cannot create GangWorker array.");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    67
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  for (int worker = 0; worker < total_workers(); worker += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    GangWorker* new_worker = new GangWorker(this, worker);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    assert(new_worker != NULL, "Failed to allocate GangWorker");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    _gang_workers[worker] = new_worker;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
    if (new_worker == NULL || !os::create_thread(new_worker, os::pgc_thread))
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
      vm_exit_out_of_memory(0, "Cannot create worker GC thread. Out of system resources.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    if (!DisableStartThread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
      os::start_thread(new_worker);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
AbstractWorkGang::~AbstractWorkGang() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
    tty->print_cr("Destructing work gang %s", name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  stop();   // stop all the workers
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  for (int worker = 0; worker < total_workers(); worker += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    delete gang_worker(worker);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  delete gang_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  delete monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
GangWorker* AbstractWorkGang::gang_worker(int i) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  // Array index bounds checking.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  GangWorker* result = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  assert(gang_workers() != NULL, "No workers for indexing");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  assert(((i >= 0) && (i < total_workers())), "Worker index out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  result = _gang_workers[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  assert(result != NULL, "Indexing to null worker");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
void WorkGang::run_task(AbstractGangTask* task) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  // This thread is executed by the VM thread which does not block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  // on ordinary MutexLocker's.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    tty->print_cr("Running work gang %s task %s", name(), task->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  // Tell all the workers to run a task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  assert(task != NULL, "Running a null task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  // Initialize.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  _task = task;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  _sequence_number += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  _started_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  _finished_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  // Tell the workers to get to work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  // Wait for them to be finished
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  while (finished_workers() < total_workers()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
      tty->print_cr("Waiting in work gang %s: %d/%d finished sequence %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
                    name(), finished_workers(), total_workers(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
                    _sequence_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
    monitor()->wait(/* no_safepoint_check */ true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  _task = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    tty->print_cr("/nFinished work gang %s: %d/%d sequence %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
                  name(), finished_workers(), total_workers(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
                  _sequence_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
void AbstractWorkGang::stop() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  // Tell all workers to terminate, then wait for them to become inactive.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
    tty->print_cr("Stopping work gang %s task %s", name(), task()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  _task = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  _terminate = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  while (finished_workers() < total_workers()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
      tty->print_cr("Waiting in work gang %s: %d/%d finished",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
                    name(), finished_workers(), total_workers());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    monitor()->wait(/* no_safepoint_check */ true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
void AbstractWorkGang::internal_worker_poll(WorkData* data) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  assert(monitor()->owned_by_self(), "worker_poll is an internal method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  assert(data != NULL, "worker data is null");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  data->set_terminate(terminate());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  data->set_task(task());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  data->set_sequence_number(sequence_number());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
void AbstractWorkGang::internal_note_start() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  assert(monitor()->owned_by_self(), "note_finish is an internal method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  _started_workers += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
void AbstractWorkGang::internal_note_finish() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  assert(monitor()->owned_by_self(), "note_finish is an internal method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  _finished_workers += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
void AbstractWorkGang::print_worker_threads_on(outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  uint    num_thr = total_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  for (uint i = 0; i < num_thr; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    gang_worker(i)->print_on(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
void AbstractWorkGang::threads_do(ThreadClosure* tc) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  assert(tc != NULL, "Null ThreadClosure");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  uint num_thr = total_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  for (uint i = 0; i < num_thr; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
    tc->do_thread(gang_worker(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
// GangWorker methods.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
GangWorker::GangWorker(AbstractWorkGang* gang, uint id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  _gang = gang;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  set_id(id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  set_name("Gang worker#%d (%s)", id, gang->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
void GangWorker::run() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  loop();
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 GangWorker::initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  this->initialize_thread_local_storage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  assert(_gang != NULL, "No gang to run in");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  os::set_priority(this, NearMaxPriority);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    tty->print_cr("Running gang worker for gang %s id %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
                  gang()->name(), id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  // The VM thread should not execute here because MutexLocker's are used
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  // as (opposed to MutexLockerEx's).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  assert(!Thread::current()->is_VM_thread(), "VM thread should not be part"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
         " of a work gang");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
void GangWorker::loop() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  int previous_sequence_number = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  Monitor* gang_monitor = gang()->monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  for ( ; /* !terminate() */; ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    WorkData data;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
    int part;  // Initialized below.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
      // Grab the gang mutex.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
      MutexLocker ml(gang_monitor);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
      // Wait for something to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
      // Polling outside the while { wait } avoids missed notifies
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
      // in the outer loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
      gang()->internal_worker_poll(&data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
      if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
        tty->print("Polled outside for work in gang %s worker %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
                   gang()->name(), id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
        tty->print("  terminate: %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
                   data.terminate() ? "true" : "false");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
        tty->print("  sequence: %d (prev: %d)",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
                   data.sequence_number(), previous_sequence_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
        if (data.task() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
          tty->print("  task: %s", data.task()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
          tty->print("  task: NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
        tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
      for ( ; /* break or return */; ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
        // Terminate if requested.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
        if (data.terminate()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
          gang()->internal_note_finish();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
          gang_monitor->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
          return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
        // Check for new work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
        if ((data.task() != NULL) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
            (data.sequence_number() != previous_sequence_number)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
          gang()->internal_note_start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
          gang_monitor->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
          part = gang()->started_workers() - 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
          break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
        // Nothing to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
        gang_monitor->wait(/* no_safepoint_check */ true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
        gang()->internal_worker_poll(&data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
        if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
          tty->print("Polled inside for work in gang %s worker %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
                     gang()->name(), id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
          tty->print("  terminate: %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
                     data.terminate() ? "true" : "false");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
          tty->print("  sequence: %d (prev: %d)",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
                     data.sequence_number(), previous_sequence_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
          if (data.task() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
            tty->print("  task: %s", data.task()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
          } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
            tty->print("  task: NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
          tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
      // Drop gang mutex.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
    if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
      tty->print("Work for work gang %s id %d task %s part %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
                 gang()->name(), id(), data.task()->name(), part);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
    assert(data.task() != NULL, "Got null task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
    data.task()->work(part);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
      if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
        tty->print("Finish for work gang %s id %d task %s part %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
                   gang()->name(), id(), data.task()->name(), part);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
      // Grab the gang mutex.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
      MutexLocker ml(gang_monitor);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
      gang()->internal_note_finish();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
      // Tell the gang you are done.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
      gang_monitor->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
      // Drop the gang mutex.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
    previous_sequence_number = data.sequence_number();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
bool GangWorker::is_GC_task_thread() const {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   299
  return gang()->are_GC_task_threads();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   300
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   301
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   302
bool GangWorker::is_ConcurrentGC_thread() const {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   303
  return gang()->are_ConcurrentGC_threads();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
void GangWorker::print_on(outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  st->print("\"%s\" ", name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  Thread::print_on(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
// Printing methods
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
const char* AbstractWorkGang::name() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  return _name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
const char* AbstractGangTask::name() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  return _name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
#endif /* PRODUCT */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
// *** WorkGangBarrierSync
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
WorkGangBarrierSync::WorkGangBarrierSync()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
  : _monitor(Mutex::safepoint, "work gang barrier sync", true),
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   330
    _n_workers(0), _n_completed(0), _should_reset(false) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
WorkGangBarrierSync::WorkGangBarrierSync(int n_workers, const char* name)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  : _monitor(Mutex::safepoint, name, true),
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   335
    _n_workers(n_workers), _n_completed(0), _should_reset(false) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
void WorkGangBarrierSync::set_n_workers(int n_workers) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
  _n_workers   = n_workers;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
  _n_completed = 0;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   341
  _should_reset = false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
void WorkGangBarrierSync::enter() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  MutexLockerEx x(monitor(), Mutex::_no_safepoint_check_flag);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   346
  if (should_reset()) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   347
    // 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
   348
    // 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
   349
    // effectively resets the barrier.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   350
    zero_completed();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   351
    set_should_reset(false);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   352
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
  inc_completed();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
  if (n_completed() == n_workers()) {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   355
    // 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
   356
    // 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
   357
    // 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
   358
    // might still be waiting for n_completed() to become ==
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   359
    // 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
   360
    // 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
   361
    // 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
   362
    // 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
   363
    // time a worker enters it again.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   364
    set_should_reset(true);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
    monitor()->notify_all();
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   366
  } else {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
    while (n_completed() != n_workers()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
      monitor()->wait(/* no_safepoint_check */ true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
// SubTasksDone functions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
SubTasksDone::SubTasksDone(int n) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
  _n_tasks(n), _n_threads(1), _tasks(NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  _tasks = NEW_C_HEAP_ARRAY(jint, n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  guarantee(_tasks != NULL, "alloc failure");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
  clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
bool SubTasksDone::valid() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
  return _tasks != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
void SubTasksDone::set_par_threads(int t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
  assert(_claimed == 0 || _threads_completed == _n_threads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
         "should not be called while tasks are being processed!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
  _n_threads = (t == 0 ? 1 : t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
void SubTasksDone::clear() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  for (int i = 0; i < _n_tasks; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
    _tasks[i] = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
  _threads_completed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
  _claimed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
bool SubTasksDone::is_task_claimed(int t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
  assert(0 <= t && t < _n_tasks, "bad task id.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
  jint old = _tasks[t];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
  if (old == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
    old = Atomic::cmpxchg(1, &_tasks[t], 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  assert(_tasks[t] == 1, "What else?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
  bool res = old != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
  if (!res) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
    assert(_claimed < _n_tasks, "Too many tasks claimed; missing clear?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
    Atomic::inc(&_claimed);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
void SubTasksDone::all_tasks_completed() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
  jint observed = _threads_completed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
  jint old;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
  do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
    old = observed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
    observed = Atomic::cmpxchg(old+1, &_threads_completed, old);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  } while (observed != old);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
  // If this was the last thread checking in, clear the tasks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
  if (observed+1 == _n_threads) clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
SubTasksDone::~SubTasksDone() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  if (_tasks != NULL) FREE_C_HEAP_ARRAY(jint, _tasks);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
// *** SequentialSubTasksDone
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
void SequentialSubTasksDone::clear() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
  _n_tasks   = _n_claimed   = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
  _n_threads = _n_completed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
bool SequentialSubTasksDone::valid() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  return _n_threads > 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
bool SequentialSubTasksDone::is_task_claimed(int& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  jint* n_claimed_ptr = &_n_claimed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
  t = *n_claimed_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
  while (t < _n_tasks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
    jint res = Atomic::cmpxchg(t+1, n_claimed_ptr, t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
    if (res == t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
    t = *n_claimed_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
bool SequentialSubTasksDone::all_tasks_completed() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
  jint* n_completed_ptr = &_n_completed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
  jint  complete        = *n_completed_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
  while (true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
    jint res = Atomic::cmpxchg(complete+1, n_completed_ptr, complete);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
    if (res == complete) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
    complete = res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  if (complete+1 == _n_threads) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
    clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
}
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   477
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   478
bool FreeIdSet::_stat_init = false;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   479
FreeIdSet* FreeIdSet::_sets[NSets];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   480
bool FreeIdSet::_safepoint;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   481
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   482
FreeIdSet::FreeIdSet(int sz, Monitor* mon) :
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   483
  _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
   484
{
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   485
  _ids = new int[sz];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   486
  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
   487
  _ids[sz-1] = end_of_list; // end of list.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   488
  if (_stat_init) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   489
    for (int j = 0; j < NSets; j++) _sets[j] = NULL;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   490
    _stat_init = true;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   491
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   492
  // 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
   493
  for (int j = 0; j < NSets; j++) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   494
    if (_sets[j] == NULL) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   495
      _sets[j] = this;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   496
      _index = j;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   497
      break;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   498
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   499
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   500
  guarantee(_index != -1, "Too many FreeIdSets in use!");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   501
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   502
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   503
FreeIdSet::~FreeIdSet() {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   504
  _sets[_index] = NULL;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   505
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   506
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   507
void FreeIdSet::set_safepoint(bool b) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   508
  _safepoint = b;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   509
  if (b) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   510
    for (int j = 0; j < NSets; j++) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   511
      if (_sets[j] != NULL && _sets[j]->_waiters > 0) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   512
        Monitor* mon = _sets[j]->_mon;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   513
        mon->lock_without_safepoint_check();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   514
        mon->notify_all();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   515
        mon->unlock();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   516
      }
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
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   519
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   520
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   521
#define FID_STATS 0
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   522
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   523
int FreeIdSet::claim_par_id() {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   524
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   525
  thread_t tslf = thr_self();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   526
  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
   527
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   528
  MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   529
  while (!_safepoint && _hd == end_of_list) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   530
    _waiters++;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   531
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   532
    if (_waiters > 5) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   533
      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
   534
                 tslf, _waiters, _claimed);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   535
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   536
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   537
    _mon->wait(Mutex::_no_safepoint_check_flag);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   538
    _waiters--;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   539
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   540
  if (_hd == end_of_list) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   541
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   542
    tty->print("claim_par_id[%d]: returning EOL.\n", tslf);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   543
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   544
    return -1;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   545
  } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   546
    int res = _hd;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   547
    _hd = _ids[res];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   548
    _ids[res] = claimed;  // For debugging.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   549
    _claimed++;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   550
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   551
    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
   552
               tslf, res, _claimed);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   553
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   554
    return res;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   555
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   556
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   557
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   558
bool FreeIdSet::claim_perm_id(int i) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   559
  assert(0 <= i && i < _sz, "Out of range.");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   560
  MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   561
  int prev = end_of_list;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   562
  int cur = _hd;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   563
  while (cur != end_of_list) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   564
    if (cur == i) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   565
      if (prev == end_of_list) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   566
        _hd = _ids[cur];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   567
      } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   568
        _ids[prev] = _ids[cur];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   569
      }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   570
      _ids[cur] = claimed;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   571
      _claimed++;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   572
      return true;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   573
    } else {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   574
      prev = cur;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   575
      cur = _ids[cur];
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   576
    }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   577
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   578
  return false;
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
void FreeIdSet::release_par_id(int id) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   583
  MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   584
  assert(_ids[id] == claimed, "Precondition.");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   585
  _ids[id] = _hd;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   586
  _hd = id;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   587
  _claimed--;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   588
#if FID_STATS
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   589
  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
   590
             thr_self(), id, _waiters, _claimed);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   591
#endif
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   592
  if (_waiters > 0)
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   593
    // Notify all would be safer, but this is OK, right?
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   594
    _mon->notify_all();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   595
}