src/hotspot/share/gc/cms/yieldingWorkgroup.cpp
author coleenp
Thu, 25 Apr 2019 10:56:31 -0400
changeset 54623 1126f0607c70
parent 47216 71c04702a3d5
child 54663 f03d5a093093
permissions -rw-r--r--
8222811: Consolidate MutexLockerEx and MutexLocker Summary: Make MutexLocker be MutexLockerEx implementation, remove MutexLockerEx calls. Reviewed-by: dcubed, dholmes, pliden, rehn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1374
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1374
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1374
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    25
#include "precompiled.hpp"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 29580
diff changeset
    26
#include "gc/cms/yieldingWorkgroup.hpp"
33107
77bf0d2069a3 8134953: Make the GC ID available in a central place
brutisso
parents: 32360
diff changeset
    27
#include "gc/shared/gcId.hpp"
15482
470d0b0c09f1 8005915: Unify SERIALGC and INCLUDE_ALTERNATE_GCS
jprovino
parents: 11396
diff changeset
    28
#include "utilities/macros.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    30
YieldingFlexibleGangWorker::YieldingFlexibleGangWorker(YieldingFlexibleWorkGang* gang, int id)
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    31
    : AbstractGangWorker(gang, id) {}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
YieldingFlexibleWorkGang::YieldingFlexibleWorkGang(
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    34
    const char* name, uint workers, bool are_GC_task_threads) :
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    35
         AbstractWorkGang(name, workers, are_GC_task_threads, false),
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    36
         _yielded_workers(0),
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    37
         _started_workers(0),
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    38
         _finished_workers(0),
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    39
         _sequence_number(0),
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    40
         _task(NULL) {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    41
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    42
  // Other initialization.
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    43
  _monitor = new Monitor(/* priority */       Mutex::leaf,
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    44
                         /* name */           "WorkGroup monitor",
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    45
                         /* allow_vm_block */ are_GC_task_threads,
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    46
                                              Monitor::_safepoint_check_sometimes);
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    47
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    48
  assert(monitor() != NULL, "Failed to allocate monitor");
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    49
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    51
AbstractGangWorker* YieldingFlexibleWorkGang::allocate_worker(uint which) {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    52
  return new YieldingFlexibleGangWorker(this, which);
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    53
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    54
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    55
void YieldingFlexibleWorkGang::internal_worker_poll(YieldingWorkData* data) const {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    56
  assert(data != NULL, "worker data is null");
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    57
  data->set_task(task());
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    58
  data->set_sequence_number(sequence_number());
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    59
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    60
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    61
void YieldingFlexibleWorkGang::internal_note_start() {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    62
  assert(monitor()->owned_by_self(), "note_finish is an internal method");
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    63
  _started_workers += 1;
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    64
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    65
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    66
void YieldingFlexibleWorkGang::internal_note_finish() {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    67
  assert(monitor()->owned_by_self(), "note_finish is an internal method");
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    68
  _finished_workers += 1;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
// Run a task; returns when the task is done, or the workers yield,
31030
811c2501a5a6 8081682: AbstractWorkGang::_terminate is never used
stefank
parents: 30764
diff changeset
    72
// or the task is aborted.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
// A task that has been yielded can be continued via this interface
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
// by using the same task repeatedly as the argument to the call.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
// It is expected that the YieldingFlexibleGangTask carries the appropriate
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
// continuation information used by workers to continue the task
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
// from its last yield point. Thus, a completed task will return
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
// immediately with no actual work having been done by the workers.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
/////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
// Implementatiuon notes: remove before checking XXX
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
Each gang is working on a task at a certain time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
Some subset of workers may have yielded and some may
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
have finished their quota of work. Until this task has
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
been completed, the workers are bound to that task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
Once the task has been completed, the gang unbounds
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
itself from the task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
The yielding work gang thus exports two invokation
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
interfaces: run_task() and continue_task(). The
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
first is used to initiate a new task and bind it
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
to the workers; the second is used to continue an
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
already bound task that has yielded. Upon completion
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
the binding is released and a new binding may be
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
created.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
The shape of a yielding work gang is as follows:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
Overseer invokes run_task(*task).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
   Lock gang monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
   Check that there is no existing binding for the gang
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
   If so, abort with an error
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
   Else, create a new binding of this gang to the given task
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
   Set number of active workers (as asked)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
   Notify workers that work is ready to be done
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
     [the requisite # workers would then start up
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
      and do the task]
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
   Wait on the monitor until either
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
     all work is completed or the task has yielded
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
     -- this is normally done through
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
        yielded + completed == active
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
        [completed workers are rest to idle state by overseer?]
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
   return appropriate status to caller
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
Overseer invokes continue_task(*task),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
   Lock gang monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
   Check that task is the same as current binding
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
   If not, abort with an error
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
   Else, set the number of active workers as requested?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
   Notify workers that they can continue from yield points
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    New workers can also start up as required
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
      while satisfying the constraint that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
         active + yielded does not exceed required number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
   Wait (as above).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
NOTE: In the above, for simplicity in a first iteration
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  our gangs will be of fixed population and will not
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  therefore be flexible work gangs, just yielding work
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  gangs. Once this works well, we will in a second
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  iteration.refinement introduce flexibility into
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  the work gang.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
NOTE: we can always create a new gang per each iteration
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  in order to get the flexibility, but we will for now
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  desist that simplified route.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
/////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
void YieldingFlexibleWorkGang::start_task(YieldingFlexibleGangTask* new_task) {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 47216
diff changeset
   140
  MutexLocker ml(monitor(), Mutex::_no_safepoint_check_flag);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  assert(task() == NULL, "Gang currently tied to a task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  assert(new_task != NULL, "Null task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  // Bind task to gang
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  _task = new_task;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  new_task->set_gang(this);  // Establish 2-way binding to support yielding
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  _sequence_number++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   148
  uint requested_size = new_task->requested_size();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  if (requested_size != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    _active_workers = MIN2(requested_size, total_workers());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  } else {
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 10273
diff changeset
   152
    _active_workers = active_workers();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  new_task->set_actual_size(_active_workers);
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
   155
  new_task->set_for_termination(_active_workers);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  assert(_started_workers == 0, "Tabula rasa non");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  assert(_finished_workers == 0, "Tabula rasa non");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  assert(_yielded_workers == 0, "Tabula rasa non");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  yielding_task()->set_status(ACTIVE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  // Wake up all the workers, the first few will get to work,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  // and the rest will go back to sleep
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  wait_for_gang();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
void YieldingFlexibleWorkGang::wait_for_gang() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  assert(monitor()->owned_by_self(), "Data race");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  // Wait for task to complete or yield
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  for (Status status = yielding_task()->status();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
       status != COMPLETED && status != YIELDED && status != ABORTED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
       status = yielding_task()->status()) {
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 10273
diff changeset
   175
    assert(started_workers() <= active_workers(), "invariant");
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 10273
diff changeset
   176
    assert(finished_workers() <= active_workers(), "invariant");
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 10273
diff changeset
   177
    assert(yielded_workers() <= active_workers(), "invariant");
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 47216
diff changeset
   178
    monitor()->wait_without_safepoint_check();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  switch (yielding_task()->status()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
    case COMPLETED:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    case ABORTED: {
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 10273
diff changeset
   183
      assert(finished_workers() == active_workers(), "Inconsistent status");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
      assert(yielded_workers() == 0, "Invariant");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
      reset();   // for next task; gang<->task binding released
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    case YIELDED: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
      assert(yielded_workers() > 0, "Invariant");
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 10273
diff changeset
   190
      assert(yielded_workers() + finished_workers() == active_workers(),
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
             "Inconsistent counts");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
    case ACTIVE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    case INACTIVE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    case COMPLETING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    case YIELDING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    case ABORTING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
      ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
void YieldingFlexibleWorkGang::continue_task(
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  YieldingFlexibleGangTask* gang_task) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 47216
diff changeset
   207
  MutexLocker ml(monitor(), Mutex::_no_safepoint_check_flag);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  assert(task() != NULL && task() == gang_task, "Incorrect usage");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  assert(_started_workers == _active_workers, "Precondition");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  assert(_yielded_workers > 0 && yielding_task()->status() == YIELDED,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
         "Else why are we calling continue_task()");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  // Restart the yielded gang workers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  yielding_task()->set_status(ACTIVE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  wait_for_gang();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
void YieldingFlexibleWorkGang::reset() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  _started_workers  = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  _finished_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  yielding_task()->set_gang(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  _task = NULL;    // unbind gang from task
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
void YieldingFlexibleWorkGang::yield() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  assert(task() != NULL, "Inconsistency; should have task binding");
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 47216
diff changeset
   227
  MutexLocker ml(monitor(), Mutex::_no_safepoint_check_flag);
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 10273
diff changeset
   228
  assert(yielded_workers() < active_workers(), "Consistency check");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  if (yielding_task()->status() == ABORTING) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    // Do not yield; we need to abort as soon as possible
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    // XXX NOTE: This can cause a performance pathology in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
    // current implementation in Mustang, as of today, and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
    // pre-Mustang in that as soon as an overflow occurs,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    // yields will not be honoured. The right way to proceed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
    // of course is to fix bug # TBF, so that abort's cause
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
    // us to return at each potential yield point.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  }
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 10273
diff changeset
   239
  if (++_yielded_workers + finished_workers() == active_workers()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
    yielding_task()->set_status(YIELDED);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
    yielding_task()->set_status(YIELDING);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  while (true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
    switch (yielding_task()->status()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
      case YIELDING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
      case YIELDED: {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 47216
diff changeset
   250
        monitor()->wait_without_safepoint_check();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
        break;  // from switch
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
      case ACTIVE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
      case ABORTING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
      case COMPLETING: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
        assert(_yielded_workers > 0, "Else why am i here?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
        _yielded_workers--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
        return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
      case INACTIVE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
      case ABORTED:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
      case COMPLETED:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
      default: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
        ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  // Only return is from inside switch statement above
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
void YieldingFlexibleWorkGang::abort() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  assert(task() != NULL, "Inconsistency; should have task binding");
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 47216
diff changeset
   274
  MutexLocker ml(monitor(), Mutex::_no_safepoint_check_flag);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  assert(yielded_workers() < active_workers(), "Consistency check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  #ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
    switch (yielding_task()->status()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
      // allowed states
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
      case ACTIVE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
      case ABORTING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
      case COMPLETING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
      case YIELDING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
      // not allowed states
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
      case INACTIVE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
      case ABORTED:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
      case COMPLETED:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
      case YIELDED:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
      default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
        ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  #endif // !PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  Status prev_status = yielding_task()->status();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  yielding_task()->set_status(ABORTING);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
  if (prev_status == YIELDING) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
    assert(yielded_workers() > 0, "Inconsistency");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
    // At least one thread has yielded, wake it up
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
    // so it can go back to waiting stations ASAP.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
    monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
///////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
// YieldingFlexibleGangTask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
///////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
void YieldingFlexibleGangTask::yield() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  assert(gang() != NULL, "No gang to signal");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  gang()->yield();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
void YieldingFlexibleGangTask::abort() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  assert(gang() != NULL, "No gang to signal");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  gang()->abort();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
///////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
// YieldingFlexibleGangWorker
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
///////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
void YieldingFlexibleGangWorker::loop() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  int previous_sequence_number = 0;
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   321
  Monitor* gang_monitor = yf_gang()->monitor();
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 47216
diff changeset
   322
  MutexLocker ml(gang_monitor, Mutex::_no_safepoint_check_flag);
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   323
  YieldingWorkData data;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  int id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
  while (true) {
31030
811c2501a5a6 8081682: AbstractWorkGang::_terminate is never used
stefank
parents: 30764
diff changeset
   326
    // Check if there is work to do.
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   327
    yf_gang()->internal_worker_poll(&data);
31030
811c2501a5a6 8081682: AbstractWorkGang::_terminate is never used
stefank
parents: 30764
diff changeset
   328
    if (data.task() != NULL && data.sequence_number() != previous_sequence_number) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
      // There is work to be done.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
      // First check if we need to become active or if there
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
      // are already the requisite number of workers
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   332
      if (yf_gang()->started_workers() == yf_gang()->active_workers()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
        // There are already enough workers, we do not need to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
        // to run; fall through and wait on monitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
        // We need to pitch in and do the work.
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   337
        assert(yf_gang()->started_workers() < yf_gang()->active_workers(),
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
               "Unexpected state");
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   339
        id = yf_gang()->started_workers();
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   340
        yf_gang()->internal_note_start();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
        // Now, release the gang mutex and do the work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
        {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 47216
diff changeset
   343
          MutexUnlocker mul(gang_monitor, Mutex::_no_safepoint_check_flag);
33107
77bf0d2069a3 8134953: Make the GC ID available in a central place
brutisso
parents: 32360
diff changeset
   344
          GCIdMark gc_id_mark(data.task()->gc_id());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
          data.task()->work(id);   // This might include yielding
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
        // Reacquire monitor and note completion of this worker
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   348
        yf_gang()->internal_note_finish();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
        // Update status of task based on whether all workers have
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
        // finished or some have yielded
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   351
        assert(data.task() == yf_gang()->task(), "Confused task binding");
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   352
        if (yf_gang()->finished_workers() == yf_gang()->active_workers()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
          switch (data.yf_task()->status()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
            case ABORTING: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
              data.yf_task()->set_status(ABORTED);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
              break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
            case ACTIVE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
            case COMPLETING: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
              data.yf_task()->set_status(COMPLETED);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
              break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
            default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
              ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
          gang_monitor->notify_all();  // Notify overseer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
        } else { // at least one worker is still working or yielded
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   368
          assert(yf_gang()->finished_workers() < yf_gang()->active_workers(),
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
                 "Counts inconsistent");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
          switch (data.yf_task()->status()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
            case ACTIVE: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
              // first, but not only thread to complete
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
              data.yf_task()->set_status(COMPLETING);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
              break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
            case YIELDING: {
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   377
              if (yf_gang()->finished_workers() + yf_gang()->yielded_workers()
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
                  == yf_gang()->active_workers()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
                data.yf_task()->set_status(YIELDED);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
                gang_monitor->notify_all();  // notify overseer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
              }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
              break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
            case ABORTING:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
            case COMPLETING: {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
              break; // nothing to do
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
            default: // everything else: INACTIVE, YIELDED, ABORTED, COMPLETED
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
              ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
    // Remember the sequence number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
    previous_sequence_number = data.sequence_number();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
    // Wait for more work
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 47216
diff changeset
   397
    gang_monitor->wait_without_safepoint_check();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
}