hotspot/src/share/vm/gc_implementation/parallelScavenge/gcTaskManager.cpp
author stefank
Thu, 01 Sep 2011 16:18:17 +0200
changeset 10524 6594ca81279a
parent 7397 5b173b4ca846
child 11174 fccee5238e70
permissions -rw-r--r--
7085906: Replace the permgen allocated sentinelRef with a self-looped end Summary: Remove the sentinelRef and let the last Reference in a discovered chain point back to itself. Reviewed-by: ysr, jmasa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
     2
 * Copyright (c) 2002, 2010, 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: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
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: 1
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: 5547
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "gc_implementation/parallelScavenge/gcTaskManager.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "gc_implementation/parallelScavenge/gcTaskThread.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "memory/allocation.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "memory/allocation.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
#include "runtime/mutex.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    31
#include "runtime/mutexLocker.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// GCTask
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
const char* GCTask::Kind::to_string(kind value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  const char* result = "unknown GCTask kind";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  switch (value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    result = "unknown GCTask kind";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  case unknown_task:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
    result = "unknown task";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  case ordinary_task:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    result = "ordinary task";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  case barrier_task:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
    result = "barrier task";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  case noop_task:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    result = "noop task";
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
GCTask::GCTask() :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  _kind(Kind::ordinary_task),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  _affinity(GCTaskManager::sentinel_worker()){
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
GCTask::GCTask(Kind::kind kind) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  _kind(kind),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  _affinity(GCTaskManager::sentinel_worker()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
GCTask::GCTask(uint affinity) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  _kind(Kind::ordinary_task),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  _affinity(affinity) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
GCTask::GCTask(Kind::kind kind, uint affinity) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  _kind(kind),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  _affinity(affinity) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
void GCTask::initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  _older = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  _newer = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
void GCTask::destruct() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  assert(older() == NULL, "shouldn't have an older task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  assert(newer() == NULL, "shouldn't have a newer task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  // Nothing to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
NOT_PRODUCT(
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
void GCTask::print(const char* message) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  tty->print(INTPTR_FORMAT " <- " INTPTR_FORMAT "(%u) -> " INTPTR_FORMAT,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
             newer(), this, affinity(), older());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
// GCTaskQueue
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
GCTaskQueue* GCTaskQueue::create() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  GCTaskQueue* result = new GCTaskQueue(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  if (TraceGCTaskQueue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    tty->print_cr("GCTaskQueue::create()"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
                  " returns " INTPTR_FORMAT, result);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
GCTaskQueue* GCTaskQueue::create_on_c_heap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  GCTaskQueue* result = new(ResourceObj::C_HEAP) GCTaskQueue(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  if (TraceGCTaskQueue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    tty->print_cr("GCTaskQueue::create_on_c_heap()"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
                  " returns " INTPTR_FORMAT,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
                  result);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
GCTaskQueue::GCTaskQueue(bool on_c_heap) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  _is_c_heap_obj(on_c_heap) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  if (TraceGCTaskQueue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    tty->print_cr("[" INTPTR_FORMAT "]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
                  " GCTaskQueue::GCTaskQueue() constructor",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
                  this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
void GCTaskQueue::destruct() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  // Nothing to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
void GCTaskQueue::destroy(GCTaskQueue* that) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  if (TraceGCTaskQueue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    tty->print_cr("[" INTPTR_FORMAT "]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
                  " GCTaskQueue::destroy()"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
                  "  is_c_heap_obj:  %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
                  that,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
                  that->is_c_heap_obj() ? "true" : "false");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  // That instance may have been allocated as a CHeapObj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  // in which case we have to free it explicitly.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  if (that != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    that->destruct();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    assert(that->is_empty(), "should be empty");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    if (that->is_c_heap_obj()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
      FreeHeap(that);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  }
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 GCTaskQueue::initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  set_insert_end(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  set_remove_end(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  set_length(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
// Enqueue one task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
void GCTaskQueue::enqueue(GCTask* task) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  if (TraceGCTaskQueue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    tty->print_cr("[" INTPTR_FORMAT "]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
                  " GCTaskQueue::enqueue(task: "
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
                  INTPTR_FORMAT ")",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
                  this, task);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    print("before:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  assert(task != NULL, "shouldn't have null task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  assert(task->older() == NULL, "shouldn't be on queue");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
  assert(task->newer() == NULL, "shouldn't be on queue");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  task->set_newer(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  task->set_older(insert_end());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  if (is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    set_remove_end(task);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    insert_end()->set_newer(task);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  set_insert_end(task);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  increment_length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  if (TraceGCTaskQueue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    print("after:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
// Enqueue a whole list of tasks.  Empties the argument list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
void GCTaskQueue::enqueue(GCTaskQueue* list) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  if (TraceGCTaskQueue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    tty->print_cr("[" INTPTR_FORMAT "]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
                  " GCTaskQueue::enqueue(list: "
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
                  INTPTR_FORMAT ")",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
                  this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    print("before:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    list->print("list:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  if (list->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
    // Enqueuing the empty list: nothing to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  uint list_length = list->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  if (is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    // Enqueuing to empty list: just acquire elements.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    set_insert_end(list->insert_end());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    set_remove_end(list->remove_end());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    set_length(list_length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
    // Prepend argument list to our queue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
    list->remove_end()->set_older(insert_end());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
    insert_end()->set_newer(list->remove_end());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
    set_insert_end(list->insert_end());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
    // empty the argument list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  set_length(length() + list_length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  list->initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  if (TraceGCTaskQueue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
    print("after:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
    list->print("list:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
// Dequeue one task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
GCTask* GCTaskQueue::dequeue() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  if (TraceGCTaskQueue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
    tty->print_cr("[" INTPTR_FORMAT "]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
                  " GCTaskQueue::dequeue()", this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    print("before:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  assert(!is_empty(), "shouldn't dequeue from empty list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  GCTask* result = remove();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  assert(result != NULL, "shouldn't have NULL task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
  if (TraceGCTaskQueue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
    tty->print_cr("    return: " INTPTR_FORMAT, result);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
    print("after:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
// Dequeue one task, preferring one with affinity.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
GCTask* GCTaskQueue::dequeue(uint affinity) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  if (TraceGCTaskQueue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
    tty->print_cr("[" INTPTR_FORMAT "]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
                  " GCTaskQueue::dequeue(%u)", this, affinity);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
    print("before:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
  assert(!is_empty(), "shouldn't dequeue from empty list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
  // Look down to the next barrier for a task with this affinity.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  GCTask* result = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  for (GCTask* element = remove_end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
       element != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
       element = element->newer()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
    if (element->is_barrier_task()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
      // Don't consider barrier tasks, nor past them.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
      result = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
    if (element->affinity() == affinity) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
      result = remove(element);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  // If we didn't find anything with affinity, just take the next task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  if (result == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    result = remove();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  if (TraceGCTaskQueue) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    tty->print_cr("    return: " INTPTR_FORMAT, result);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
    print("after:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
GCTask* GCTaskQueue::remove() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  // Dequeue from remove end.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  GCTask* result = remove_end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  assert(result != NULL, "shouldn't have null task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  assert(result->older() == NULL, "not the remove_end");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  set_remove_end(result->newer());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  if (remove_end() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
    assert(insert_end() == result, "not a singleton");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
    set_insert_end(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
    remove_end()->set_older(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  result->set_newer(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  decrement_length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
  assert(result->newer() == NULL, "shouldn't be on queue");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  assert(result->older() == NULL, "shouldn't be on queue");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
GCTask* GCTaskQueue::remove(GCTask* task) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
  // This is slightly more work, and has slightly fewer asserts
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  // than removing from the remove end.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  assert(task != NULL, "shouldn't have null task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  GCTask* result = task;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  if (result->newer() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    result->newer()->set_older(result->older());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
    assert(insert_end() == result, "not youngest");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
    set_insert_end(result->older());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
  if (result->older() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    result->older()->set_newer(result->newer());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
    assert(remove_end() == result, "not oldest");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
    set_remove_end(result->newer());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  result->set_newer(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  result->set_older(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  decrement_length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
NOT_PRODUCT(
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
void GCTaskQueue::print(const char* message) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  tty->print_cr("[" INTPTR_FORMAT "] GCTaskQueue:"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
                "  insert_end: " INTPTR_FORMAT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
                "  remove_end: " INTPTR_FORMAT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
                "  %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
                this, insert_end(), remove_end(), message);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  for (GCTask* element = insert_end();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
       element != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
       element = element->older()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    element->print("    ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
    tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
// SynchronizedGCTaskQueue
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
SynchronizedGCTaskQueue::SynchronizedGCTaskQueue(GCTaskQueue* queue_arg,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
                                                 Monitor *       lock_arg) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
  _unsynchronized_queue(queue_arg),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
  _lock(lock_arg) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
  assert(unsynchronized_queue() != NULL, "null queue");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
  assert(lock() != NULL, "null lock");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
SynchronizedGCTaskQueue::~SynchronizedGCTaskQueue() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  // Nothing to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
// GCTaskManager
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
GCTaskManager::GCTaskManager(uint workers) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
  _workers(workers),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
  _ndc(NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
  initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
GCTaskManager::GCTaskManager(uint workers, NotifyDoneClosure* ndc) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
  _workers(workers),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
  _ndc(ndc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
  initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
void GCTaskManager::initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
    tty->print_cr("GCTaskManager::initialize: workers: %u", workers());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
  assert(workers() != 0, "no workers");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
  _monitor = new Monitor(Mutex::barrier,                // rank
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
                         "GCTaskManager monitor",       // name
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
                         Mutex::_allow_vm_block_flag);  // allow_vm_block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  // The queue for the GCTaskManager must be a CHeapObj.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  GCTaskQueue* unsynchronized_queue = GCTaskQueue::create_on_c_heap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  _queue = SynchronizedGCTaskQueue::create(unsynchronized_queue, lock());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
  _noop_task = NoopGCTask::create_on_c_heap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
  _resource_flag = NEW_C_HEAP_ARRAY(bool, workers());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
    // Set up worker threads.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
    //     Distribute the workers among the available processors,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
    //     unless we were told not to, or if the os doesn't want to.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
    uint* processor_assignment = NEW_C_HEAP_ARRAY(uint, workers());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
    if (!BindGCTaskThreadsToCPUs ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
        !os::distribute_processes(workers(), processor_assignment)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
      for (uint a = 0; a < workers(); a += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
        processor_assignment[a] = sentinel_worker();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
    _thread = NEW_C_HEAP_ARRAY(GCTaskThread*, workers());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
    for (uint t = 0; t < workers(); t += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
      set_thread(t, GCTaskThread::create(this, t, processor_assignment[t]));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
    if (TraceGCTaskThread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
      tty->print("GCTaskManager::initialize: distribution:");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
      for (uint t = 0; t < workers(); t += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
        tty->print("  %u", processor_assignment[t]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
      tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
    FREE_C_HEAP_ARRAY(uint, processor_assignment);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
  reset_busy_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
  set_unblocked();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
  for (uint w = 0; w < workers(); w += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
    set_resource_flag(w, false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
  reset_delivered_tasks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
  reset_completed_tasks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  reset_noop_tasks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  reset_barriers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  reset_emptied_queue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
  for (uint s = 0; s < workers(); s += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
    thread(s)->start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
GCTaskManager::~GCTaskManager() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  assert(busy_workers() == 0, "still have busy workers");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  assert(queue()->is_empty(), "still have queued work");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
  NoopGCTask::destroy(_noop_task);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
  _noop_task = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
  if (_thread != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
    for (uint i = 0; i < workers(); i += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
      GCTaskThread::destroy(thread(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
      set_thread(i, NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
    FREE_C_HEAP_ARRAY(GCTaskThread*, _thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
    _thread = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
  if (_resource_flag != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
    FREE_C_HEAP_ARRAY(bool, _resource_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
    _resource_flag = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  if (queue() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
    GCTaskQueue* unsynchronized_queue = queue()->unsynchronized_queue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
    GCTaskQueue::destroy(unsynchronized_queue);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
    SynchronizedGCTaskQueue::destroy(queue());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
    _queue = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
  if (monitor() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
    delete monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
    _monitor = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
void GCTaskManager::print_task_time_stamps() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
  for(uint i=0; i<ParallelGCThreads; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
    GCTaskThread* t = thread(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
    t->print_task_time_stamps();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
void GCTaskManager::print_threads_on(outputStream* st) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
  uint num_thr = workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
  for (uint i = 0; i < num_thr; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
    thread(i)->print_on(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
    st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
void GCTaskManager::threads_do(ThreadClosure* tc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
  assert(tc != NULL, "Null ThreadClosure");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
  uint num_thr = workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
  for (uint i = 0; i < num_thr; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
    tc->do_thread(thread(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
GCTaskThread* GCTaskManager::thread(uint which) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
  assert(which < workers(), "index out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
  assert(_thread[which] != NULL, "shouldn't have null thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  return _thread[which];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
void GCTaskManager::set_thread(uint which, GCTaskThread* value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
  assert(which < workers(), "index out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  assert(value != NULL, "shouldn't have null thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
  _thread[which] = value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
void GCTaskManager::add_task(GCTask* task) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
  assert(task != NULL, "shouldn't have null task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
    tty->print_cr("GCTaskManager::add_task(" INTPTR_FORMAT " [%s])",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
                  task, GCTask::Kind::to_string(task->kind()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
  queue()->enqueue(task);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  // Notify with the lock held to avoid missed notifies.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
    tty->print_cr("    GCTaskManager::add_task (%s)->notify_all",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
                  monitor()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
  (void) monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
  // Release monitor().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
void GCTaskManager::add_list(GCTaskQueue* list) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
  assert(list != NULL, "shouldn't have null task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
    tty->print_cr("GCTaskManager::add_list(%u)", list->length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
  queue()->enqueue(list);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
  // Notify with the lock held to avoid missed notifies.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
  if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
    tty->print_cr("    GCTaskManager::add_list (%s)->notify_all",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
                  monitor()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
  (void) monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
  // Release monitor().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
GCTask* GCTaskManager::get_task(uint which) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
  GCTask* result = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
  // Grab the queue lock.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
  // Wait while the queue is block or
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
  // there is nothing to do, except maybe release resources.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  while (is_blocked() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
         (queue()->is_empty() && !should_release_resources(which))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
    if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
      tty->print_cr("GCTaskManager::get_task(%u)"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
                    "  blocked: %s"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
                    "  empty: %s"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
                    "  release: %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
                    which,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
                    is_blocked() ? "true" : "false",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
                    queue()->is_empty() ? "true" : "false",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
                    should_release_resources(which) ? "true" : "false");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
      tty->print_cr("    => (%s)->wait()",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
                    monitor()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
    monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
  // We've reacquired the queue lock here.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
  // Figure out which condition caused us to exit the loop above.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
  if (!queue()->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
    if (UseGCTaskAffinity) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
      result = queue()->dequeue(which);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
      result = queue()->dequeue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
    if (result->is_barrier_task()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
      assert(which != sentinel_worker(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
             "blocker shouldn't be bogus");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
      set_blocking_worker(which);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
    // The queue is empty, but we were woken up.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
    // Just hand back a Noop task,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
    // in case someone wanted us to release resources, or whatever.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
    result = noop_task();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
    increment_noop_tasks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
  assert(result != NULL, "shouldn't have null task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
  if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
    tty->print_cr("GCTaskManager::get_task(%u) => " INTPTR_FORMAT " [%s]",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
                  which, result, GCTask::Kind::to_string(result->kind()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
    tty->print_cr("     %s", result->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
  increment_busy_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
  increment_delivered_tasks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
  // Release monitor().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
void GCTaskManager::note_completion(uint which) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
  if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
    tty->print_cr("GCTaskManager::note_completion(%u)", which);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
  // If we are blocked, check if the completing thread is the blocker.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
  if (blocking_worker() == which) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
    assert(blocking_worker() != sentinel_worker(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
           "blocker shouldn't be bogus");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
    increment_barriers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
    set_unblocked();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
  increment_completed_tasks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
  uint active = decrement_busy_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
  if ((active == 0) && (queue()->is_empty())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
    increment_emptied_queue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
    if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
      tty->print_cr("    GCTaskManager::note_completion(%u) done", which);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
    // Notify client that we are done.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
    NotifyDoneClosure* ndc = notify_done_closure();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
    if (ndc != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
      ndc->notify(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
  if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
    tty->print_cr("    GCTaskManager::note_completion(%u) (%s)->notify_all",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
                  which, monitor()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
    tty->print_cr("  "
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
                  "  blocked: %s"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
                  "  empty: %s"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
                  "  release: %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
                  is_blocked() ? "true" : "false",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
                  queue()->is_empty() ? "true" : "false",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
                  should_release_resources(which) ? "true" : "false");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
    tty->print_cr("  "
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
                  "  delivered: %u"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
                  "  completed: %u"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
                  "  barriers: %u"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
                  "  emptied: %u",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
                  delivered_tasks(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
                  completed_tasks(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
                  barriers(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
                  emptied_queue());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
  // Tell everyone that a task has completed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
  (void) monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
  // Release monitor().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
uint GCTaskManager::increment_busy_workers() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
  assert(queue()->own_lock(), "don't own the lock");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
  _busy_workers += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
  return _busy_workers;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
uint GCTaskManager::decrement_busy_workers() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
  assert(queue()->own_lock(), "don't own the lock");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
  _busy_workers -= 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
  return _busy_workers;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
void GCTaskManager::release_all_resources() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
  // If you want this to be done atomically, do it in a BarrierGCTask.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
  for (uint i = 0; i < workers(); i += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
    set_resource_flag(i, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
bool GCTaskManager::should_release_resources(uint which) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
  // This can be done without a lock because each thread reads one element.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
  return resource_flag(which);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
void GCTaskManager::note_release(uint which) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
  // This can be done without a lock because each thread writes one element.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
  set_resource_flag(which, false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
void GCTaskManager::execute_and_wait(GCTaskQueue* list) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
  WaitForBarrierGCTask* fin = WaitForBarrierGCTask::create();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
  list->enqueue(fin);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
  add_list(list);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
  fin->wait_for();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
  // We have to release the barrier tasks!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
  WaitForBarrierGCTask::destroy(fin);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
bool GCTaskManager::resource_flag(uint which) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
  assert(which < workers(), "index out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
  return _resource_flag[which];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
void GCTaskManager::set_resource_flag(uint which, bool value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
  assert(which < workers(), "index out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
  _resource_flag[which] = value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
// NoopGCTask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
NoopGCTask* NoopGCTask::create() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
  NoopGCTask* result = new NoopGCTask(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
NoopGCTask* NoopGCTask::create_on_c_heap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
  NoopGCTask* result = new(ResourceObj::C_HEAP) NoopGCTask(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
void NoopGCTask::destroy(NoopGCTask* that) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
  if (that != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
    that->destruct();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
    if (that->is_c_heap_obj()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
      FreeHeap(that);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
void NoopGCTask::destruct() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
  // This has to know it's superclass structure, just like the constructor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
  this->GCTask::destruct();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
  // Nothing else to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
// BarrierGCTask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
void BarrierGCTask::do_it(GCTaskManager* manager, uint which) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
  // Wait for this to be the only busy worker.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
  // ??? I thought of having a StackObj class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
  //     whose constructor would grab the lock and come to the barrier,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
  //     and whose destructor would release the lock,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
  //     but that seems like too much mechanism for two lines of code.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
  MutexLockerEx ml(manager->lock(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
  do_it_internal(manager, which);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
  // Release manager->lock().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
void BarrierGCTask::do_it_internal(GCTaskManager* manager, uint which) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
  // Wait for this to be the only busy worker.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
  assert(manager->monitor()->owned_by_self(), "don't own the lock");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
  assert(manager->is_blocked(), "manager isn't blocked");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
  while (manager->busy_workers() > 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
    if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
      tty->print_cr("BarrierGCTask::do_it(%u) waiting on %u workers",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
                    which, manager->busy_workers());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
    manager->monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
void BarrierGCTask::destruct() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
  this->GCTask::destruct();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
  // Nothing else to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
// ReleasingBarrierGCTask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
void ReleasingBarrierGCTask::do_it(GCTaskManager* manager, uint which) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
  MutexLockerEx ml(manager->lock(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
  do_it_internal(manager, which);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
  manager->release_all_resources();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
  // Release manager->lock().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
void ReleasingBarrierGCTask::destruct() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
  this->BarrierGCTask::destruct();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
  // Nothing else to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
// NotifyingBarrierGCTask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
void NotifyingBarrierGCTask::do_it(GCTaskManager* manager, uint which) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
  MutexLockerEx ml(manager->lock(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
  do_it_internal(manager, which);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
  NotifyDoneClosure* ndc = notify_done_closure();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
  if (ndc != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
    ndc->notify(manager);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
  // Release manager->lock().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
void NotifyingBarrierGCTask::destruct() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
  this->BarrierGCTask::destruct();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
  // Nothing else to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
// WaitForBarrierGCTask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
WaitForBarrierGCTask* WaitForBarrierGCTask::create() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
  WaitForBarrierGCTask* result = new WaitForBarrierGCTask(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
WaitForBarrierGCTask* WaitForBarrierGCTask::create_on_c_heap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
  WaitForBarrierGCTask* result = new WaitForBarrierGCTask(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
WaitForBarrierGCTask::WaitForBarrierGCTask(bool on_c_heap) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
  _is_c_heap_obj(on_c_heap) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
  _monitor = MonitorSupply::reserve();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
  set_should_wait(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
  if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
    tty->print_cr("[" INTPTR_FORMAT "]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
                  " WaitForBarrierGCTask::WaitForBarrierGCTask()"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
                  "  monitor: " INTPTR_FORMAT,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
                  this, monitor());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
void WaitForBarrierGCTask::destroy(WaitForBarrierGCTask* that) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
  if (that != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
    if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
      tty->print_cr("[" INTPTR_FORMAT "]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
                    " WaitForBarrierGCTask::destroy()"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
                    "  is_c_heap_obj: %s"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
                    "  monitor: " INTPTR_FORMAT,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
                    that,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
                    that->is_c_heap_obj() ? "true" : "false",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
                    that->monitor());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
    that->destruct();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
    if (that->is_c_heap_obj()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
      FreeHeap(that);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
void WaitForBarrierGCTask::destruct() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
  assert(monitor() != NULL, "monitor should not be NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
  if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
    tty->print_cr("[" INTPTR_FORMAT "]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
                  " WaitForBarrierGCTask::destruct()"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
                  "  monitor: " INTPTR_FORMAT,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   811
                  this, monitor());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
  this->BarrierGCTask::destruct();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
  // Clean up that should be in the destructor,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
  // except that ResourceMarks don't call destructors.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
   if (monitor() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
     MonitorSupply::release(monitor());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
  _monitor = (Monitor*) 0xDEAD000F;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
void WaitForBarrierGCTask::do_it(GCTaskManager* manager, uint which) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
  if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
    tty->print_cr("[" INTPTR_FORMAT "]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
                  " WaitForBarrierGCTask::do_it() waiting for idle"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
                  "  monitor: " INTPTR_FORMAT,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
                  this, monitor());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
    // First, wait for the barrier to arrive.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
    MutexLockerEx ml(manager->lock(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
    do_it_internal(manager, which);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
    // Release manager->lock().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
    // Then notify the waiter.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
    MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
    set_should_wait(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
    // Waiter doesn't miss the notify in the wait_for method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
    // since it checks the flag after grabbing the monitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
    if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
      tty->print_cr("[" INTPTR_FORMAT "]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
                    " WaitForBarrierGCTask::do_it()"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
                    "  [" INTPTR_FORMAT "] (%s)->notify_all()",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
                    this, monitor(), monitor()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
    monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
    // Release monitor().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
void WaitForBarrierGCTask::wait_for() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
  if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
    tty->print_cr("[" INTPTR_FORMAT "]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
                  " WaitForBarrierGCTask::wait_for()"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
      "  should_wait: %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
      this, should_wait() ? "true" : "false");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
    // Grab the lock and check again.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
    MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
    while (should_wait()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
      if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
        tty->print_cr("[" INTPTR_FORMAT "]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
                      " WaitForBarrierGCTask::wait_for()"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
          "  [" INTPTR_FORMAT "] (%s)->wait()",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
          this, monitor(), monitor()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
      monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
    // Reset the flag in case someone reuses this task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
    set_should_wait(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
    if (TraceGCTaskManager) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
      tty->print_cr("[" INTPTR_FORMAT "]"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
                    " WaitForBarrierGCTask::wait_for() returns"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
        "  should_wait: %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
        this, should_wait() ? "true" : "false");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
    // Release monitor().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
Mutex*                   MonitorSupply::_lock     = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
GrowableArray<Monitor*>* MonitorSupply::_freelist = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
Monitor* MonitorSupply::reserve() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
  Monitor* result = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
  // Lazy initialization: possible race.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
  if (lock() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
    _lock = new Mutex(Mutex::barrier,                  // rank
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
                      "MonitorSupply mutex",           // name
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
                      Mutex::_allow_vm_block_flag);    // allow_vm_block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
    MutexLockerEx ml(lock());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
    // Lazy initialization.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
    if (freelist() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
      _freelist =
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
        new(ResourceObj::C_HEAP) GrowableArray<Monitor*>(ParallelGCThreads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
                                                         true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   901
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
    if (! freelist()->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
      result = freelist()->pop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   904
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   905
      result = new Monitor(Mutex::barrier,                  // rank
489c9b5090e2 Initial load
duke
parents:
diff changeset
   906
                           "MonitorSupply monitor",         // name
489c9b5090e2 Initial load
duke
parents:
diff changeset
   907
                           Mutex::_allow_vm_block_flag);    // allow_vm_block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   909
    guarantee(result != NULL, "shouldn't return NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   910
    assert(!result->is_locked(), "shouldn't be locked");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
    // release lock().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   912
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
void MonitorSupply::release(Monitor* instance) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
  assert(instance != NULL, "shouldn't release NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
  assert(!instance->is_locked(), "shouldn't be locked");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
    MutexLockerEx ml(lock());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
    freelist()->push(instance);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
    // release lock().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
}