hotspot/src/share/vm/utilities/yieldingWorkgroup.cpp
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1 489c9b5090e2
child 1374 4c24294029a9
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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/_yieldingWorkgroup.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// Forward declaration of classes declared here.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
class GangWorker;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
class WorkData;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
YieldingFlexibleWorkGang::YieldingFlexibleWorkGang(
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  const char* name, int workers, bool are_GC_threads) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  AbstractWorkGang(name, are_GC_threads) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  // Save arguments.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  _total_workers = workers;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  assert(_total_workers > 0, "Must have more than 1 worker");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  _yielded_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
    tty->print_cr("Constructing work gang %s with %d threads", name, workers);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  _gang_workers = NEW_C_HEAP_ARRAY(GangWorker*, workers);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  assert(gang_workers() != NULL, "Failed to allocate gang workers");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  for (int worker = 0; worker < total_workers(); worker += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    YieldingFlexibleGangWorker* new_worker =
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
      new YieldingFlexibleGangWorker(this, worker);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
    assert(new_worker != NULL, "Failed to allocate YieldingFlexibleGangWorker");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    _gang_workers[worker] = new_worker;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    if (new_worker == NULL || !os::create_thread(new_worker, os::pgc_thread))
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
      vm_exit_out_of_memory(0, "Cannot create worker GC thread. Out of system resources.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    if (!DisableStartThread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
      os::start_thread(new_worker);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
// Run a task; returns when the task is done, or the workers yield,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
// or the task is aborted, or the work gang is terminated via stop().
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
// A task that has been yielded can be continued via this interface
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
// by using the same task repeatedly as the argument to the call.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
// It is expected that the YieldingFlexibleGangTask carries the appropriate
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
// continuation information used by workers to continue the task
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
// from its last yield point. Thus, a completed task will return
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
// immediately with no actual work having been done by the workers.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
/////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
// Implementatiuon notes: remove before checking XXX
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
Each gang is working on a task at a certain time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
Some subset of workers may have yielded and some may
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
have finished their quota of work. Until this task has
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
been completed, the workers are bound to that task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
Once the task has been completed, the gang unbounds
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
itself from the task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
The yielding work gang thus exports two invokation
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
interfaces: run_task() and continue_task(). The
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
first is used to initiate a new task and bind it
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
to the workers; the second is used to continue an
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
already bound task that has yielded. Upon completion
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
the binding is released and a new binding may be
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
created.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
The shape of a yielding work gang is as follows:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
Overseer invokes run_task(*task).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
   Lock gang monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
   Check that there is no existing binding for the gang
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
   If so, abort with an error
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
   Else, create a new binding of this gang to the given task
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
   Set number of active workers (as asked)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
   Notify workers that work is ready to be done
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
     [the requisite # workers would then start up
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      and do the task]
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
   Wait on the monitor until either
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
     all work is completed or the task has yielded
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
     -- this is normally done through
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
        yielded + completed == active
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
        [completed workers are rest to idle state by overseer?]
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
   return appropriate status to caller
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
Overseer invokes continue_task(*task),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
   Lock gang monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
   Check that task is the same as current binding
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
   If not, abort with an error
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
   Else, set the number of active workers as requested?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
   Notify workers that they can continue from yield points
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    New workers can also start up as required
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
      while satisfying the constraint that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
         active + yielded does not exceed required number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
   Wait (as above).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
NOTE: In the above, for simplicity in a first iteration
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  our gangs will be of fixed population and will not
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  therefore be flexible work gangs, just yielding work
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  gangs. Once this works well, we will in a second
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  iteration.refinement introduce flexibility into
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  the work gang.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
NOTE: we can always create a new gang per each iteration
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  in order to get the flexibility, but we will for now
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  desist that simplified route.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
/////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
void YieldingFlexibleWorkGang::start_task(YieldingFlexibleGangTask* new_task) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  assert(task() == NULL, "Gang currently tied to a task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  assert(new_task != NULL, "Null task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  // Bind task to gang
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  _task = new_task;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  new_task->set_gang(this);  // Establish 2-way binding to support yielding
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  _sequence_number++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  int requested_size = new_task->requested_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  assert(requested_size >= 0, "Should be non-negative");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  if (requested_size != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    _active_workers = MIN2(requested_size, total_workers());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
    _active_workers = total_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  new_task->set_actual_size(_active_workers);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  assert(_started_workers == 0, "Tabula rasa non");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  assert(_finished_workers == 0, "Tabula rasa non");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  assert(_yielded_workers == 0, "Tabula rasa non");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  yielding_task()->set_status(ACTIVE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  // Wake up all the workers, the first few will get to work,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  // and the rest will go back to sleep
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  wait_for_gang();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
void YieldingFlexibleWorkGang::wait_for_gang() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  assert(monitor()->owned_by_self(), "Data race");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  // Wait for task to complete or yield
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  for (Status status = yielding_task()->status();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
       status != COMPLETED && status != YIELDED && status != ABORTED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
       status = yielding_task()->status()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
    assert(started_workers() <= active_workers(), "invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
    assert(finished_workers() <= active_workers(), "invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    assert(yielded_workers() <= active_workers(), "invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    monitor()->wait(Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  switch (yielding_task()->status()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    case COMPLETED:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    case ABORTED: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
      assert(finished_workers() == active_workers(), "Inconsistent status");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
      assert(yielded_workers() == 0, "Invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
      reset();   // for next task; gang<->task binding released
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
    case YIELDED: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
      assert(yielded_workers() > 0, "Invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
      assert(yielded_workers() + finished_workers() == active_workers(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
             "Inconsistent counts");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
    case ACTIVE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
    case INACTIVE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    case COMPLETING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    case YIELDING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    case ABORTING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
      ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
void YieldingFlexibleWorkGang::continue_task(
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  YieldingFlexibleGangTask* gang_task) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  assert(task() != NULL && task() == gang_task, "Incorrect usage");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  // assert(_active_workers == total_workers(), "For now");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  assert(_started_workers == _active_workers, "Precondition");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  assert(_yielded_workers > 0 && yielding_task()->status() == YIELDED,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
         "Else why are we calling continue_task()");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  // Restart the yielded gang workers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  yielding_task()->set_status(ACTIVE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  wait_for_gang();
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 YieldingFlexibleWorkGang::reset() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  _started_workers  = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  _finished_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  _active_workers   = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  yielding_task()->set_gang(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  _task = NULL;    // unbind gang from task
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
void YieldingFlexibleWorkGang::yield() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  assert(task() != NULL, "Inconsistency; should have task binding");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  assert(yielded_workers() < active_workers(), "Consistency check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  if (yielding_task()->status() == ABORTING) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    // Do not yield; we need to abort as soon as possible
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
    // XXX NOTE: This can cause a performance pathology in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
    // current implementation in Mustang, as of today, and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    // pre-Mustang in that as soon as an overflow occurs,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
    // yields will not be honoured. The right way to proceed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    // of course is to fix bug # TBF, so that abort's cause
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
    // us to return at each potential yield point.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  if (++_yielded_workers + finished_workers() == active_workers()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    yielding_task()->set_status(YIELDED);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
    monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    yielding_task()->set_status(YIELDING);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  while (true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
    switch (yielding_task()->status()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
      case YIELDING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
      case YIELDED: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
        monitor()->wait(Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
        break;  // from switch
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
      case ACTIVE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
      case ABORTING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
      case COMPLETING: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
        assert(_yielded_workers > 0, "Else why am i here?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
        _yielded_workers--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
        return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
      case INACTIVE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
      case ABORTED:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
      case COMPLETED:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
      default: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
        ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  // Only return is from inside switch statement above
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  ShouldNotReachHere();
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 YieldingFlexibleWorkGang::abort() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  assert(task() != NULL, "Inconsistency; should have task binding");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
  assert(yielded_workers() < active_workers(), "Consistency check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  #ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
    switch (yielding_task()->status()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
      // allowed states
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
      case ACTIVE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
      case ABORTING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
      case COMPLETING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
      case YIELDING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
      // not allowed states
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
      case INACTIVE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
      case ABORTED:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
      case COMPLETED:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
      case YIELDED:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
      default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
        ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  #endif // !PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  Status prev_status = yielding_task()->status();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  yielding_task()->set_status(ABORTING);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  if (prev_status == YIELDING) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
    assert(yielded_workers() > 0, "Inconsistency");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
    // At least one thread has yielded, wake it up
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
    // so it can go back to waiting stations ASAP.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
    monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
///////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
// YieldingFlexibleGangTask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
///////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
void YieldingFlexibleGangTask::yield() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  assert(gang() != NULL, "No gang to signal");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  gang()->yield();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
void YieldingFlexibleGangTask::abort() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
  assert(gang() != NULL, "No gang to signal");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
  gang()->abort();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
///////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
// YieldingFlexibleGangWorker
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
///////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
void YieldingFlexibleGangWorker::loop() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  int previous_sequence_number = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  Monitor* gang_monitor = gang()->monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  MutexLockerEx ml(gang_monitor, Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  WorkData data;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  int id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  while (true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
    // Check if there is work to do or if we have been asked
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
    // to terminate
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
    gang()->internal_worker_poll(&data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
    if (data.terminate()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
      // We have been asked to terminate.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
      assert(gang()->task() == NULL, "No task binding");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
      // set_status(TERMINATED);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
    } else if (data.task() != NULL &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
               data.sequence_number() != previous_sequence_number) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
      // There is work to be done.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
      // First check if we need to become active or if there
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
      // are already the requisite number of workers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
      if (gang()->started_workers() == yf_gang()->active_workers()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
        // There are already enough workers, we do not need to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
        // to run; fall through and wait on monitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
        // We need to pitch in and do the work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
        assert(gang()->started_workers() < yf_gang()->active_workers(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
               "Unexpected state");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
        id = gang()->started_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
        gang()->internal_note_start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
        // Now, release the gang mutex and do the work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
        {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
          MutexUnlockerEx mul(gang_monitor, Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
          data.task()->work(id);   // This might include yielding
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
        // Reacquire monitor and note completion of this worker
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
        gang()->internal_note_finish();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
        // Update status of task based on whether all workers have
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
        // finished or some have yielded
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
        assert(data.task() == gang()->task(), "Confused task binding");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
        if (gang()->finished_workers() == yf_gang()->active_workers()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
          switch (data.yf_task()->status()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
            case ABORTING: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
              data.yf_task()->set_status(ABORTED);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
              break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
            case ACTIVE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
            case COMPLETING: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
              data.yf_task()->set_status(COMPLETED);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
              break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
            default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
              ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
          gang_monitor->notify_all();  // Notify overseer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
        } else { // at least one worker is still working or yielded
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
          assert(gang()->finished_workers() < yf_gang()->active_workers(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
                 "Counts inconsistent");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
          switch (data.yf_task()->status()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
            case ACTIVE: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
              // first, but not only thread to complete
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
              data.yf_task()->set_status(COMPLETING);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
              break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
            case YIELDING: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
              if (gang()->finished_workers() + yf_gang()->yielded_workers()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
                  == yf_gang()->active_workers()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
                data.yf_task()->set_status(YIELDED);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
                gang_monitor->notify_all();  // notify overseer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
              }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
              break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
            case ABORTING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
            case COMPLETING: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
              break; // nothing to do
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
            default: // everything else: INACTIVE, YIELDED, ABORTED, COMPLETED
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
              ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
    // Remember the sequence number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
    previous_sequence_number = data.sequence_number();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
    // Wait for more work
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
    gang_monitor->wait(Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
}