2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2016-04-01 07:08:40 +02:00
|
|
|
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-27 19:08:38 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "precompiled.hpp"
|
2015-09-30 09:07:21 +02:00
|
|
|
#include "gc/shared/gcId.hpp"
|
2015-05-13 15:16:06 +02:00
|
|
|
#include "gc/shared/workgroup.hpp"
|
2015-08-06 22:07:30 -07:00
|
|
|
#include "gc/shared/workerManager.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "memory/allocation.hpp"
|
|
|
|
#include "memory/allocation.inline.hpp"
|
2016-08-21 20:56:37 -04:00
|
|
|
#include "runtime/atomic.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "runtime/os.hpp"
|
2015-06-29 11:11:12 +02:00
|
|
|
#include "runtime/semaphore.hpp"
|
|
|
|
#include "runtime/thread.inline.hpp"
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// Definitions of WorkGang methods.
|
|
|
|
|
2010-09-20 14:38:38 -07:00
|
|
|
// The current implementation will exit if the allocation
|
2015-08-06 22:07:30 -07:00
|
|
|
// of any worker fails.
|
|
|
|
void AbstractWorkGang::initialize_workers() {
|
2016-04-01 07:08:40 +02:00
|
|
|
log_develop_trace(gc, workgang)("Constructing work gang %s with %u threads", name(), total_workers());
|
2015-06-29 11:09:39 +02:00
|
|
|
_workers = NEW_C_HEAP_ARRAY(AbstractGangWorker*, total_workers(), mtInternal);
|
|
|
|
if (_workers == NULL) {
|
2013-04-30 11:56:52 -07:00
|
|
|
vm_exit_out_of_memory(0, OOM_MALLOC_ERROR, "Cannot create GangWorker array.");
|
2010-09-20 14:38:38 -07:00
|
|
|
}
|
2015-08-06 22:07:30 -07:00
|
|
|
|
|
|
|
add_workers(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AbstractGangWorker* AbstractWorkGang::install_worker(uint worker_id) {
|
|
|
|
AbstractGangWorker* new_worker = allocate_worker(worker_id);
|
|
|
|
set_thread(worker_id, new_worker);
|
|
|
|
return new_worker;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractWorkGang::add_workers(bool initializing) {
|
2016-05-25 09:28:20 -07:00
|
|
|
add_workers(_active_workers, initializing);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractWorkGang::add_workers(uint active_workers, bool initializing) {
|
2015-08-06 22:07:30 -07:00
|
|
|
|
2010-09-20 14:38:38 -07:00
|
|
|
os::ThreadType worker_type;
|
|
|
|
if (are_ConcurrentGC_threads()) {
|
|
|
|
worker_type = os::cgc_thread;
|
|
|
|
} else {
|
|
|
|
worker_type = os::pgc_thread;
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
2016-05-19 13:59:50 -07:00
|
|
|
uint previous_created_workers = _created_workers;
|
2015-08-06 22:07:30 -07:00
|
|
|
|
|
|
|
_created_workers = WorkerManager::add_workers(this,
|
2016-05-25 09:28:20 -07:00
|
|
|
active_workers,
|
2015-08-06 22:07:30 -07:00
|
|
|
_total_workers,
|
|
|
|
_created_workers,
|
|
|
|
worker_type,
|
|
|
|
initializing);
|
|
|
|
_active_workers = MIN2(_created_workers, _active_workers);
|
2016-05-19 13:59:50 -07:00
|
|
|
|
|
|
|
WorkerManager::log_worker_creation(this, previous_created_workers, _active_workers, _created_workers, initializing);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 11:09:39 +02:00
|
|
|
AbstractGangWorker* AbstractWorkGang::worker(uint i) const {
|
2007-12-01 00:00:00 +00:00
|
|
|
// Array index bounds checking.
|
2015-06-29 11:09:39 +02:00
|
|
|
AbstractGangWorker* result = NULL;
|
|
|
|
assert(_workers != NULL, "No workers for indexing");
|
2015-02-16 14:07:36 +01:00
|
|
|
assert(i < total_workers(), "Worker index out of bounds");
|
2015-06-29 11:09:39 +02:00
|
|
|
result = _workers[i];
|
2007-12-01 00:00:00 +00:00
|
|
|
assert(result != NULL, "Indexing to null worker");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-06-29 11:09:39 +02:00
|
|
|
void AbstractWorkGang::print_worker_threads_on(outputStream* st) const {
|
2015-08-06 22:07:30 -07:00
|
|
|
uint workers = created_workers();
|
2015-06-29 11:09:39 +02:00
|
|
|
for (uint i = 0; i < workers; i++) {
|
|
|
|
worker(i)->print_on(st);
|
|
|
|
st->cr();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractWorkGang::threads_do(ThreadClosure* tc) const {
|
|
|
|
assert(tc != NULL, "Null ThreadClosure");
|
2015-08-06 22:07:30 -07:00
|
|
|
uint workers = created_workers();
|
2015-06-29 11:09:39 +02:00
|
|
|
for (uint i = 0; i < workers; i++) {
|
|
|
|
tc->do_thread(worker(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-29 11:11:12 +02:00
|
|
|
// WorkGang dispatcher implemented with semaphores.
|
|
|
|
//
|
|
|
|
// Semaphores don't require the worker threads to re-claim the lock when they wake up.
|
|
|
|
// This helps lowering the latency when starting and stopping the worker threads.
|
|
|
|
class SemaphoreGangTaskDispatcher : public GangTaskDispatcher {
|
|
|
|
// The task currently being dispatched to the GangWorkers.
|
|
|
|
AbstractGangTask* _task;
|
2015-06-29 11:09:39 +02:00
|
|
|
|
2015-06-29 11:11:12 +02:00
|
|
|
volatile uint _started;
|
|
|
|
volatile uint _not_finished;
|
2015-06-29 11:09:39 +02:00
|
|
|
|
2015-06-29 11:11:12 +02:00
|
|
|
// Semaphore used to start the GangWorkers.
|
|
|
|
Semaphore* _start_semaphore;
|
|
|
|
// Semaphore used to notify the coordinator that all workers are done.
|
|
|
|
Semaphore* _end_semaphore;
|
2011-08-09 10:16:01 -07:00
|
|
|
|
2015-06-29 11:11:12 +02:00
|
|
|
public:
|
|
|
|
SemaphoreGangTaskDispatcher() :
|
|
|
|
_task(NULL),
|
|
|
|
_started(0),
|
|
|
|
_not_finished(0),
|
|
|
|
_start_semaphore(new Semaphore()),
|
|
|
|
_end_semaphore(new Semaphore())
|
|
|
|
{ }
|
|
|
|
|
|
|
|
~SemaphoreGangTaskDispatcher() {
|
|
|
|
delete _start_semaphore;
|
|
|
|
delete _end_semaphore;
|
|
|
|
}
|
|
|
|
|
|
|
|
void coordinator_execute_on_workers(AbstractGangTask* task, uint num_workers) {
|
|
|
|
// No workers are allowed to read the state variables until they have been signaled.
|
|
|
|
_task = task;
|
|
|
|
_not_finished = num_workers;
|
|
|
|
|
|
|
|
// Dispatch 'num_workers' number of tasks.
|
|
|
|
_start_semaphore->signal(num_workers);
|
|
|
|
|
|
|
|
// Wait for the last worker to signal the coordinator.
|
|
|
|
_end_semaphore->wait();
|
|
|
|
|
|
|
|
// No workers are allowed to read the state variables after the coordinator has been signaled.
|
2015-09-29 11:02:08 +02:00
|
|
|
assert(_not_finished == 0, "%d not finished workers?", _not_finished);
|
2015-06-29 11:11:12 +02:00
|
|
|
_task = NULL;
|
|
|
|
_started = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkData worker_wait_for_task() {
|
|
|
|
// Wait for the coordinator to dispatch a task.
|
|
|
|
_start_semaphore->wait();
|
|
|
|
|
|
|
|
uint num_started = (uint) Atomic::add(1, (volatile jint*)&_started);
|
|
|
|
|
|
|
|
// Subtract one to get a zero-indexed worker id.
|
|
|
|
uint worker_id = num_started - 1;
|
|
|
|
|
|
|
|
return WorkData(_task, worker_id);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
2015-06-29 11:11:12 +02:00
|
|
|
|
|
|
|
void worker_done_with_task() {
|
|
|
|
// Mark that the worker is done with the task.
|
|
|
|
// The worker is not allowed to read the state variables after this line.
|
|
|
|
uint not_finished = (uint) Atomic::add(-1, (volatile jint*)&_not_finished);
|
|
|
|
|
|
|
|
// The last worker signals to the coordinator that all work is completed.
|
|
|
|
if (not_finished == 0) {
|
|
|
|
_end_semaphore->signal();
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-29 11:11:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class MutexGangTaskDispatcher : public GangTaskDispatcher {
|
|
|
|
AbstractGangTask* _task;
|
|
|
|
|
|
|
|
volatile uint _started;
|
|
|
|
volatile uint _finished;
|
|
|
|
volatile uint _num_workers;
|
|
|
|
|
|
|
|
Monitor* _monitor;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MutexGangTaskDispatcher()
|
|
|
|
: _task(NULL),
|
|
|
|
_monitor(new Monitor(Monitor::leaf, "WorkGang dispatcher lock", false, Monitor::_safepoint_check_never)),
|
|
|
|
_started(0),
|
|
|
|
_finished(0),
|
|
|
|
_num_workers(0) {}
|
|
|
|
|
|
|
|
~MutexGangTaskDispatcher() {
|
|
|
|
delete _monitor;
|
2011-03-17 10:32:46 -07:00
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2015-06-29 11:11:12 +02:00
|
|
|
void coordinator_execute_on_workers(AbstractGangTask* task, uint num_workers) {
|
|
|
|
MutexLockerEx ml(_monitor, Mutex::_no_safepoint_check_flag);
|
|
|
|
|
|
|
|
_task = task;
|
|
|
|
_num_workers = num_workers;
|
|
|
|
|
|
|
|
// Tell the workers to get to work.
|
|
|
|
_monitor->notify_all();
|
|
|
|
|
|
|
|
// Wait for them to finish.
|
|
|
|
while (_finished < _num_workers) {
|
|
|
|
_monitor->wait(/* no_safepoint_check */ true);
|
|
|
|
}
|
|
|
|
|
|
|
|
_task = NULL;
|
|
|
|
_num_workers = 0;
|
|
|
|
_started = 0;
|
|
|
|
_finished = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkData worker_wait_for_task() {
|
|
|
|
MonitorLockerEx ml(_monitor, Mutex::_no_safepoint_check_flag);
|
|
|
|
|
|
|
|
while (_num_workers == 0 || _started == _num_workers) {
|
|
|
|
_monitor->wait(/* no_safepoint_check */ true);
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2015-06-29 11:11:12 +02:00
|
|
|
_started++;
|
|
|
|
|
|
|
|
// Subtract one to get a zero-indexed worker id.
|
|
|
|
uint worker_id = _started - 1;
|
|
|
|
|
|
|
|
return WorkData(_task, worker_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void worker_done_with_task() {
|
|
|
|
MonitorLockerEx ml(_monitor, Mutex::_no_safepoint_check_flag);
|
|
|
|
|
|
|
|
_finished++;
|
|
|
|
|
|
|
|
if (_finished == _num_workers) {
|
|
|
|
// This will wake up all workers and not only the coordinator.
|
|
|
|
_monitor->notify_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static GangTaskDispatcher* create_dispatcher() {
|
|
|
|
if (UseSemaphoreGCThreadsSynchronization) {
|
|
|
|
return new SemaphoreGangTaskDispatcher();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MutexGangTaskDispatcher();
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 11:11:12 +02:00
|
|
|
WorkGang::WorkGang(const char* name,
|
|
|
|
uint workers,
|
|
|
|
bool are_GC_task_threads,
|
|
|
|
bool are_ConcurrentGC_threads) :
|
|
|
|
AbstractWorkGang(name, workers, are_GC_task_threads, are_ConcurrentGC_threads),
|
|
|
|
_dispatcher(create_dispatcher())
|
|
|
|
{ }
|
|
|
|
|
|
|
|
AbstractGangWorker* WorkGang::allocate_worker(uint worker_id) {
|
|
|
|
return new GangWorker(this, worker_id);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 11:11:12 +02:00
|
|
|
void WorkGang::run_task(AbstractGangTask* task) {
|
2016-05-02 10:24:41 +02:00
|
|
|
run_task(task, active_workers());
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkGang::run_task(AbstractGangTask* task, uint num_workers) {
|
2016-05-25 09:28:20 -07:00
|
|
|
guarantee(num_workers <= total_workers(),
|
|
|
|
"Trying to execute task %s with %u workers which is more than the amount of total workers %u.",
|
|
|
|
task->name(), num_workers, total_workers());
|
2016-05-02 10:24:41 +02:00
|
|
|
guarantee(num_workers > 0, "Trying to execute task %s with zero workers", task->name());
|
2016-06-08 14:11:51 -07:00
|
|
|
uint old_num_workers = _active_workers;
|
|
|
|
update_active_workers(num_workers);
|
2016-05-02 10:24:41 +02:00
|
|
|
_dispatcher->coordinator_execute_on_workers(task, num_workers);
|
2016-06-08 14:11:51 -07:00
|
|
|
update_active_workers(old_num_workers);
|
2015-06-29 11:11:12 +02:00
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
|
2015-06-29 11:09:39 +02:00
|
|
|
AbstractGangWorker::AbstractGangWorker(AbstractWorkGang* gang, uint id) {
|
2007-12-01 00:00:00 +00:00
|
|
|
_gang = gang;
|
|
|
|
set_id(id);
|
2015-03-05 16:43:26 +01:00
|
|
|
set_name("%s#%d", gang->name(), id);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 11:09:39 +02:00
|
|
|
void AbstractGangWorker::run() {
|
2007-12-01 00:00:00 +00:00
|
|
|
initialize();
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2015-06-29 11:09:39 +02:00
|
|
|
void AbstractGangWorker::initialize() {
|
2012-06-28 17:03:16 -04:00
|
|
|
this->record_stack_base_and_size();
|
2015-03-03 12:19:35 +01:00
|
|
|
this->initialize_named_thread();
|
2007-12-01 00:00:00 +00:00
|
|
|
assert(_gang != NULL, "No gang to run in");
|
|
|
|
os::set_priority(this, NearMaxPriority);
|
2016-04-01 07:08:40 +02:00
|
|
|
log_develop_trace(gc, workgang)("Running gang worker for gang %s id %u", gang()->name(), id());
|
2007-12-01 00:00:00 +00:00
|
|
|
// The VM thread should not execute here because MutexLocker's are used
|
|
|
|
// as (opposed to MutexLockerEx's).
|
|
|
|
assert(!Thread::current()->is_VM_thread(), "VM thread should not be part"
|
|
|
|
" of a work gang");
|
|
|
|
}
|
|
|
|
|
2015-06-29 11:09:39 +02:00
|
|
|
bool AbstractGangWorker::is_GC_task_thread() const {
|
|
|
|
return gang()->are_GC_task_threads();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AbstractGangWorker::is_ConcurrentGC_thread() const {
|
|
|
|
return gang()->are_ConcurrentGC_threads();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractGangWorker::print_on(outputStream* st) const {
|
|
|
|
st->print("\"%s\" ", name());
|
|
|
|
Thread::print_on(st);
|
|
|
|
st->cr();
|
|
|
|
}
|
|
|
|
|
2015-06-29 11:11:12 +02:00
|
|
|
WorkData GangWorker::wait_for_task() {
|
|
|
|
return gang()->dispatcher()->worker_wait_for_task();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GangWorker::signal_task_done() {
|
|
|
|
gang()->dispatcher()->worker_done_with_task();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GangWorker::run_task(WorkData data) {
|
2015-09-30 09:07:21 +02:00
|
|
|
GCIdMark gc_id_mark(data._task->gc_id());
|
2016-04-01 07:08:40 +02:00
|
|
|
log_develop_trace(gc, workgang)("Running work gang: %s task: %s worker: %u", name(), data._task->name(), data._worker_id);
|
|
|
|
|
2015-06-29 11:11:12 +02:00
|
|
|
data._task->work(data._worker_id);
|
|
|
|
|
2016-04-01 07:08:40 +02:00
|
|
|
log_develop_trace(gc, workgang)("Finished work gang: %s task: %s worker: %u thread: " PTR_FORMAT,
|
|
|
|
name(), data._task->name(), data._worker_id, p2i(Thread::current()));
|
2015-06-29 11:11:12 +02:00
|
|
|
}
|
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
void GangWorker::loop() {
|
2015-06-29 11:11:12 +02:00
|
|
|
while (true) {
|
|
|
|
WorkData data = wait_for_task();
|
|
|
|
|
|
|
|
run_task(data);
|
|
|
|
|
|
|
|
signal_task_done();
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// *** WorkGangBarrierSync
|
|
|
|
|
|
|
|
WorkGangBarrierSync::WorkGangBarrierSync()
|
2014-12-11 21:34:43 -05:00
|
|
|
: _monitor(Mutex::safepoint, "work gang barrier sync", true,
|
|
|
|
Monitor::_safepoint_check_never),
|
2014-05-14 13:32:44 +02:00
|
|
|
_n_workers(0), _n_completed(0), _should_reset(false), _aborted(false) {
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2011-12-14 13:34:57 -08:00
|
|
|
WorkGangBarrierSync::WorkGangBarrierSync(uint n_workers, const char* name)
|
2014-12-11 21:34:43 -05:00
|
|
|
: _monitor(Mutex::safepoint, name, true, Monitor::_safepoint_check_never),
|
2014-05-14 13:32:44 +02:00
|
|
|
_n_workers(n_workers), _n_completed(0), _should_reset(false), _aborted(false) {
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2011-12-14 13:34:57 -08:00
|
|
|
void WorkGangBarrierSync::set_n_workers(uint n_workers) {
|
2014-05-14 13:32:44 +02:00
|
|
|
_n_workers = n_workers;
|
|
|
|
_n_completed = 0;
|
2008-06-05 15:57:56 -07:00
|
|
|
_should_reset = false;
|
2014-05-14 13:32:44 +02:00
|
|
|
_aborted = false;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:32:44 +02:00
|
|
|
bool WorkGangBarrierSync::enter() {
|
2007-12-01 00:00:00 +00:00
|
|
|
MutexLockerEx x(monitor(), Mutex::_no_safepoint_check_flag);
|
2008-06-05 15:57:56 -07:00
|
|
|
if (should_reset()) {
|
|
|
|
// The should_reset() was set and we are the first worker to enter
|
|
|
|
// the sync barrier. We will zero the n_completed() count which
|
|
|
|
// effectively resets the barrier.
|
|
|
|
zero_completed();
|
|
|
|
set_should_reset(false);
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
inc_completed();
|
|
|
|
if (n_completed() == n_workers()) {
|
2008-06-05 15:57:56 -07:00
|
|
|
// At this point we would like to reset the barrier to be ready in
|
|
|
|
// case it is used again. However, we cannot set n_completed() to
|
|
|
|
// 0, even after the notify_all(), given that some other workers
|
|
|
|
// might still be waiting for n_completed() to become ==
|
|
|
|
// n_workers(). So, if we set n_completed() to 0, those workers
|
|
|
|
// will get stuck (as they will wake up, see that n_completed() !=
|
|
|
|
// n_workers() and go back to sleep). Instead, we raise the
|
|
|
|
// should_reset() flag and the barrier will be reset the first
|
|
|
|
// time a worker enters it again.
|
|
|
|
set_should_reset(true);
|
2007-12-01 00:00:00 +00:00
|
|
|
monitor()->notify_all();
|
2008-06-05 15:57:56 -07:00
|
|
|
} else {
|
2014-05-14 13:32:44 +02:00
|
|
|
while (n_completed() != n_workers() && !aborted()) {
|
2007-12-01 00:00:00 +00:00
|
|
|
monitor()->wait(/* no_safepoint_check */ true);
|
|
|
|
}
|
|
|
|
}
|
2014-05-14 13:32:44 +02:00
|
|
|
return !aborted();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkGangBarrierSync::abort() {
|
|
|
|
MutexLockerEx x(monitor(), Mutex::_no_safepoint_check_flag);
|
|
|
|
set_aborted();
|
|
|
|
monitor()->notify_all();
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SubTasksDone functions.
|
|
|
|
|
2011-12-14 13:34:57 -08:00
|
|
|
SubTasksDone::SubTasksDone(uint n) :
|
2015-05-21 09:23:46 +02:00
|
|
|
_n_tasks(n), _tasks(NULL) {
|
2012-06-28 17:03:16 -04:00
|
|
|
_tasks = NEW_C_HEAP_ARRAY(uint, n, mtInternal);
|
2007-12-01 00:00:00 +00:00
|
|
|
guarantee(_tasks != NULL, "alloc failure");
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SubTasksDone::valid() {
|
|
|
|
return _tasks != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubTasksDone::clear() {
|
2011-12-14 13:34:57 -08:00
|
|
|
for (uint i = 0; i < _n_tasks; i++) {
|
2007-12-01 00:00:00 +00:00
|
|
|
_tasks[i] = 0;
|
|
|
|
}
|
|
|
|
_threads_completed = 0;
|
|
|
|
#ifdef ASSERT
|
|
|
|
_claimed = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-12-14 13:34:57 -08:00
|
|
|
bool SubTasksDone::is_task_claimed(uint t) {
|
2015-02-16 14:07:36 +01:00
|
|
|
assert(t < _n_tasks, "bad task id.");
|
2011-12-14 13:34:57 -08:00
|
|
|
uint old = _tasks[t];
|
2007-12-01 00:00:00 +00:00
|
|
|
if (old == 0) {
|
|
|
|
old = Atomic::cmpxchg(1, &_tasks[t], 0);
|
|
|
|
}
|
|
|
|
assert(_tasks[t] == 1, "What else?");
|
|
|
|
bool res = old != 0;
|
|
|
|
#ifdef ASSERT
|
|
|
|
if (!res) {
|
|
|
|
assert(_claimed < _n_tasks, "Too many tasks claimed; missing clear?");
|
2011-12-14 13:34:57 -08:00
|
|
|
Atomic::inc((volatile jint*) &_claimed);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-05-21 09:23:46 +02:00
|
|
|
void SubTasksDone::all_tasks_completed(uint n_threads) {
|
2007-12-01 00:00:00 +00:00
|
|
|
jint observed = _threads_completed;
|
|
|
|
jint old;
|
|
|
|
do {
|
|
|
|
old = observed;
|
|
|
|
observed = Atomic::cmpxchg(old+1, &_threads_completed, old);
|
|
|
|
} while (observed != old);
|
|
|
|
// If this was the last thread checking in, clear the tasks.
|
2015-05-21 09:23:46 +02:00
|
|
|
uint adjusted_thread_count = (n_threads == 0 ? 1 : n_threads);
|
|
|
|
if (observed + 1 == (jint)adjusted_thread_count) {
|
|
|
|
clear();
|
|
|
|
}
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SubTasksDone::~SubTasksDone() {
|
2014-12-01 12:16:15 -05:00
|
|
|
if (_tasks != NULL) FREE_C_HEAP_ARRAY(jint, _tasks);
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// *** SequentialSubTasksDone
|
|
|
|
|
|
|
|
void SequentialSubTasksDone::clear() {
|
|
|
|
_n_tasks = _n_claimed = 0;
|
|
|
|
_n_threads = _n_completed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SequentialSubTasksDone::valid() {
|
|
|
|
return _n_threads > 0;
|
|
|
|
}
|
|
|
|
|
2011-12-14 13:34:57 -08:00
|
|
|
bool SequentialSubTasksDone::is_task_claimed(uint& t) {
|
2016-09-20 11:41:43 +02:00
|
|
|
t = _n_claimed;
|
2007-12-01 00:00:00 +00:00
|
|
|
while (t < _n_tasks) {
|
2016-09-20 11:41:43 +02:00
|
|
|
jint res = Atomic::cmpxchg(t+1, &_n_claimed, t);
|
2011-12-14 13:34:57 -08:00
|
|
|
if (res == (jint)t) {
|
2007-12-01 00:00:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-20 11:41:43 +02:00
|
|
|
t = res;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SequentialSubTasksDone::all_tasks_completed() {
|
2016-09-20 11:41:43 +02:00
|
|
|
uint complete = _n_completed;
|
2007-12-01 00:00:00 +00:00
|
|
|
while (true) {
|
2016-09-20 11:41:43 +02:00
|
|
|
uint res = Atomic::cmpxchg(complete+1, &_n_completed, complete);
|
2007-12-01 00:00:00 +00:00
|
|
|
if (res == complete) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
complete = res;
|
|
|
|
}
|
|
|
|
if (complete+1 == _n_threads) {
|
|
|
|
clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|