annotate src/share/vm/utilities/workgroup.cpp @ 4834:74d14a44c398

Added tag jdk7u60-b01 for changeset 8fd0e931efa5
author asaha
date Wed, 27 Nov 2013 14:57:31 -0800
parents 441e946dc1af
children
rev   line source
duke@0 1 /*
ysr@2216 2 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #include "precompiled.hpp"
stefank@1879 26 #include "memory/allocation.hpp"
stefank@1879 27 #include "memory/allocation.inline.hpp"
stefank@1879 28 #include "runtime/os.hpp"
stefank@1879 29 #include "utilities/workgroup.hpp"
duke@0 30
duke@0 31 // Definitions of WorkGang methods.
duke@0 32
duke@0 33 AbstractWorkGang::AbstractWorkGang(const char* name,
ysr@342 34 bool are_GC_task_threads,
ysr@342 35 bool are_ConcurrentGC_threads) :
duke@0 36 _name(name),
ysr@342 37 _are_GC_task_threads(are_GC_task_threads),
ysr@342 38 _are_ConcurrentGC_threads(are_ConcurrentGC_threads) {
ysr@342 39
ysr@342 40 assert(!(are_GC_task_threads && are_ConcurrentGC_threads),
ysr@342 41 "They cannot both be STW GC and Concurrent threads" );
ysr@342 42
duke@0 43 // Other initialization.
duke@0 44 _monitor = new Monitor(/* priority */ Mutex::leaf,
duke@0 45 /* name */ "WorkGroup monitor",
ysr@342 46 /* allow_vm_block */ are_GC_task_threads);
duke@0 47 assert(monitor() != NULL, "Failed to allocate monitor");
duke@0 48 _terminate = false;
duke@0 49 _task = NULL;
duke@0 50 _sequence_number = 0;
duke@0 51 _started_workers = 0;
duke@0 52 _finished_workers = 0;
duke@0 53 }
duke@0 54
duke@0 55 WorkGang::WorkGang(const char* name,
jmasa@3008 56 uint workers,
ysr@342 57 bool are_GC_task_threads,
ysr@342 58 bool are_ConcurrentGC_threads) :
jmasa@1753 59 AbstractWorkGang(name, are_GC_task_threads, are_ConcurrentGC_threads) {
duke@0 60 _total_workers = workers;
jmasa@1753 61 }
jmasa@1753 62
jmasa@3008 63 GangWorker* WorkGang::allocate_worker(uint which) {
jmasa@1753 64 GangWorker* new_worker = new GangWorker(this, which);
jmasa@1753 65 return new_worker;
jmasa@1753 66 }
jmasa@1753 67
jmasa@1753 68 // The current implementation will exit if the allocation
jmasa@1753 69 // of any worker fails. Still, return a boolean so that
jmasa@1753 70 // a future implementation can possibly do a partial
jmasa@1753 71 // initialization of the workers and report such to the
jmasa@1753 72 // caller.
jmasa@1753 73 bool WorkGang::initialize_workers() {
ysr@342 74
duke@0 75 if (TraceWorkGang) {
jmasa@1753 76 tty->print_cr("Constructing work gang %s with %d threads",
jmasa@1753 77 name(),
jmasa@1753 78 total_workers());
duke@0 79 }
zgu@3863 80 _gang_workers = NEW_C_HEAP_ARRAY(GangWorker*, total_workers(), mtInternal);
ysr@342 81 if (gang_workers() == NULL) {
ysr@342 82 vm_exit_out_of_memory(0, "Cannot create GangWorker array.");
jmasa@1753 83 return false;
jmasa@1753 84 }
jmasa@1753 85 os::ThreadType worker_type;
jmasa@1753 86 if (are_ConcurrentGC_threads()) {
jmasa@1753 87 worker_type = os::cgc_thread;
jmasa@1753 88 } else {
jmasa@1753 89 worker_type = os::pgc_thread;
ysr@342 90 }
jmasa@3008 91 for (uint worker = 0; worker < total_workers(); worker += 1) {
jmasa@1753 92 GangWorker* new_worker = allocate_worker(worker);
duke@0 93 assert(new_worker != NULL, "Failed to allocate GangWorker");
duke@0 94 _gang_workers[worker] = new_worker;
jmasa@1753 95 if (new_worker == NULL || !os::create_thread(new_worker, worker_type)) {
duke@0 96 vm_exit_out_of_memory(0, "Cannot create worker GC thread. Out of system resources.");
jmasa@1753 97 return false;
jmasa@1753 98 }
duke@0 99 if (!DisableStartThread) {
duke@0 100 os::start_thread(new_worker);
duke@0 101 }
duke@0 102 }
jmasa@1753 103 return true;
duke@0 104 }
duke@0 105
duke@0 106 AbstractWorkGang::~AbstractWorkGang() {
duke@0 107 if (TraceWorkGang) {
duke@0 108 tty->print_cr("Destructing work gang %s", name());
duke@0 109 }
duke@0 110 stop(); // stop all the workers
jmasa@3008 111 for (uint worker = 0; worker < total_workers(); worker += 1) {
duke@0 112 delete gang_worker(worker);
duke@0 113 }
duke@0 114 delete gang_workers();
duke@0 115 delete monitor();
duke@0 116 }
duke@0 117
jmasa@3008 118 GangWorker* AbstractWorkGang::gang_worker(uint i) const {
duke@0 119 // Array index bounds checking.
duke@0 120 GangWorker* result = NULL;
duke@0 121 assert(gang_workers() != NULL, "No workers for indexing");
duke@0 122 assert(((i >= 0) && (i < total_workers())), "Worker index out of bounds");
duke@0 123 result = _gang_workers[i];
duke@0 124 assert(result != NULL, "Indexing to null worker");
duke@0 125 return result;
duke@0 126 }
duke@0 127
duke@0 128 void WorkGang::run_task(AbstractGangTask* task) {
jmasa@2941 129 run_task(task, total_workers());
jmasa@2941 130 }
jmasa@2941 131
jmasa@2941 132 void WorkGang::run_task(AbstractGangTask* task, uint no_of_parallel_workers) {
jmasa@2941 133 task->set_for_termination(no_of_parallel_workers);
jmasa@2941 134
duke@0 135 // This thread is executed by the VM thread which does not block
duke@0 136 // on ordinary MutexLocker's.
duke@0 137 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
duke@0 138 if (TraceWorkGang) {
duke@0 139 tty->print_cr("Running work gang %s task %s", name(), task->name());
duke@0 140 }
duke@0 141 // Tell all the workers to run a task.
duke@0 142 assert(task != NULL, "Running a null task");
duke@0 143 // Initialize.
duke@0 144 _task = task;
duke@0 145 _sequence_number += 1;
duke@0 146 _started_workers = 0;
duke@0 147 _finished_workers = 0;
duke@0 148 // Tell the workers to get to work.
duke@0 149 monitor()->notify_all();
duke@0 150 // Wait for them to be finished
jmasa@3008 151 while (finished_workers() < no_of_parallel_workers) {
duke@0 152 if (TraceWorkGang) {
duke@0 153 tty->print_cr("Waiting in work gang %s: %d/%d finished sequence %d",
jmasa@2941 154 name(), finished_workers(), no_of_parallel_workers,
duke@0 155 _sequence_number);
duke@0 156 }
duke@0 157 monitor()->wait(/* no_safepoint_check */ true);
duke@0 158 }
duke@0 159 _task = NULL;
duke@0 160 if (TraceWorkGang) {
jmasa@2941 161 tty->print_cr("\nFinished work gang %s: %d/%d sequence %d",
jmasa@2941 162 name(), finished_workers(), no_of_parallel_workers,
duke@0 163 _sequence_number);
jmasa@2941 164 Thread* me = Thread::current();
jmasa@2941 165 tty->print_cr(" T: 0x%x VM_thread: %d", me, me->is_VM_thread());
ysr@2216 166 }
duke@0 167 }
duke@0 168
jmasa@2941 169 void FlexibleWorkGang::run_task(AbstractGangTask* task) {
jmasa@2941 170 // If active_workers() is passed, _finished_workers
jmasa@2941 171 // must only be incremented for workers that find non_null
jmasa@2941 172 // work (as opposed to all those that just check that the
jmasa@2941 173 // task is not null).
jmasa@2941 174 WorkGang::run_task(task, (uint) active_workers());
jmasa@2941 175 }
jmasa@2941 176
duke@0 177 void AbstractWorkGang::stop() {
duke@0 178 // Tell all workers to terminate, then wait for them to become inactive.
duke@0 179 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
duke@0 180 if (TraceWorkGang) {
duke@0 181 tty->print_cr("Stopping work gang %s task %s", name(), task()->name());
duke@0 182 }
duke@0 183 _task = NULL;
duke@0 184 _terminate = true;
duke@0 185 monitor()->notify_all();
jmasa@2941 186 while (finished_workers() < active_workers()) {
duke@0 187 if (TraceWorkGang) {
duke@0 188 tty->print_cr("Waiting in work gang %s: %d/%d finished",
jmasa@2941 189 name(), finished_workers(), active_workers());
duke@0 190 }
duke@0 191 monitor()->wait(/* no_safepoint_check */ true);
duke@0 192 }
duke@0 193 }
duke@0 194
duke@0 195 void AbstractWorkGang::internal_worker_poll(WorkData* data) const {
duke@0 196 assert(monitor()->owned_by_self(), "worker_poll is an internal method");
duke@0 197 assert(data != NULL, "worker data is null");
duke@0 198 data->set_terminate(terminate());
duke@0 199 data->set_task(task());
duke@0 200 data->set_sequence_number(sequence_number());
duke@0 201 }
duke@0 202
duke@0 203 void AbstractWorkGang::internal_note_start() {
duke@0 204 assert(monitor()->owned_by_self(), "note_finish is an internal method");
duke@0 205 _started_workers += 1;
duke@0 206 }
duke@0 207
duke@0 208 void AbstractWorkGang::internal_note_finish() {
duke@0 209 assert(monitor()->owned_by_self(), "note_finish is an internal method");
duke@0 210 _finished_workers += 1;
duke@0 211 }
duke@0 212
duke@0 213 void AbstractWorkGang::print_worker_threads_on(outputStream* st) const {
duke@0 214 uint num_thr = total_workers();
duke@0 215 for (uint i = 0; i < num_thr; i++) {
duke@0 216 gang_worker(i)->print_on(st);
duke@0 217 st->cr();
duke@0 218 }
duke@0 219 }
duke@0 220
duke@0 221 void AbstractWorkGang::threads_do(ThreadClosure* tc) const {
duke@0 222 assert(tc != NULL, "Null ThreadClosure");
duke@0 223 uint num_thr = total_workers();
duke@0 224 for (uint i = 0; i < num_thr; i++) {
duke@0 225 tc->do_thread(gang_worker(i));
duke@0 226 }
duke@0 227 }
duke@0 228
duke@0 229 // GangWorker methods.
duke@0 230
duke@0 231 GangWorker::GangWorker(AbstractWorkGang* gang, uint id) {
duke@0 232 _gang = gang;
duke@0 233 set_id(id);
duke@0 234 set_name("Gang worker#%d (%s)", id, gang->name());
duke@0 235 }
duke@0 236
duke@0 237 void GangWorker::run() {
duke@0 238 initialize();
duke@0 239 loop();
duke@0 240 }
duke@0 241
duke@0 242 void GangWorker::initialize() {
duke@0 243 this->initialize_thread_local_storage();
zgu@3863 244 this->record_stack_base_and_size();
duke@0 245 assert(_gang != NULL, "No gang to run in");
duke@0 246 os::set_priority(this, NearMaxPriority);
duke@0 247 if (TraceWorkGang) {
duke@0 248 tty->print_cr("Running gang worker for gang %s id %d",
duke@0 249 gang()->name(), id());
duke@0 250 }
duke@0 251 // The VM thread should not execute here because MutexLocker's are used
duke@0 252 // as (opposed to MutexLockerEx's).
duke@0 253 assert(!Thread::current()->is_VM_thread(), "VM thread should not be part"
duke@0 254 " of a work gang");
duke@0 255 }
duke@0 256
duke@0 257 void GangWorker::loop() {
duke@0 258 int previous_sequence_number = 0;
duke@0 259 Monitor* gang_monitor = gang()->monitor();
duke@0 260 for ( ; /* !terminate() */; ) {
duke@0 261 WorkData data;
duke@0 262 int part; // Initialized below.
duke@0 263 {
duke@0 264 // Grab the gang mutex.
duke@0 265 MutexLocker ml(gang_monitor);
duke@0 266 // Wait for something to do.
duke@0 267 // Polling outside the while { wait } avoids missed notifies
duke@0 268 // in the outer loop.
duke@0 269 gang()->internal_worker_poll(&data);
duke@0 270 if (TraceWorkGang) {
duke@0 271 tty->print("Polled outside for work in gang %s worker %d",
duke@0 272 gang()->name(), id());
duke@0 273 tty->print(" terminate: %s",
duke@0 274 data.terminate() ? "true" : "false");
duke@0 275 tty->print(" sequence: %d (prev: %d)",
duke@0 276 data.sequence_number(), previous_sequence_number);
duke@0 277 if (data.task() != NULL) {
duke@0 278 tty->print(" task: %s", data.task()->name());
duke@0 279 } else {
duke@0 280 tty->print(" task: NULL");
duke@0 281 }
duke@0 282 tty->cr();
duke@0 283 }
duke@0 284 for ( ; /* break or return */; ) {
duke@0 285 // Terminate if requested.
duke@0 286 if (data.terminate()) {
duke@0 287 gang()->internal_note_finish();
duke@0 288 gang_monitor->notify_all();
duke@0 289 return;
duke@0 290 }
duke@0 291 // Check for new work.
duke@0 292 if ((data.task() != NULL) &&
duke@0 293 (data.sequence_number() != previous_sequence_number)) {
jmasa@2941 294 if (gang()->needs_more_workers()) {
jmasa@2941 295 gang()->internal_note_start();
jmasa@2941 296 gang_monitor->notify_all();
jmasa@2941 297 part = gang()->started_workers() - 1;
jmasa@2941 298 break;
jmasa@2941 299 }
duke@0 300 }
duke@0 301 // Nothing to do.
duke@0 302 gang_monitor->wait(/* no_safepoint_check */ true);
duke@0 303 gang()->internal_worker_poll(&data);
duke@0 304 if (TraceWorkGang) {
duke@0 305 tty->print("Polled inside for work in gang %s worker %d",
duke@0 306 gang()->name(), id());
duke@0 307 tty->print(" terminate: %s",
duke@0 308 data.terminate() ? "true" : "false");
duke@0 309 tty->print(" sequence: %d (prev: %d)",
duke@0 310 data.sequence_number(), previous_sequence_number);
duke@0 311 if (data.task() != NULL) {
duke@0 312 tty->print(" task: %s", data.task()->name());
duke@0 313 } else {
duke@0 314 tty->print(" task: NULL");
duke@0 315 }
duke@0 316 tty->cr();
duke@0 317 }
duke@0 318 }
duke@0 319 // Drop gang mutex.
duke@0 320 }
duke@0 321 if (TraceWorkGang) {
duke@0 322 tty->print("Work for work gang %s id %d task %s part %d",
duke@0 323 gang()->name(), id(), data.task()->name(), part);
duke@0 324 }
duke@0 325 assert(data.task() != NULL, "Got null task");
duke@0 326 data.task()->work(part);
duke@0 327 {
duke@0 328 if (TraceWorkGang) {
duke@0 329 tty->print("Finish for work gang %s id %d task %s part %d",
duke@0 330 gang()->name(), id(), data.task()->name(), part);
duke@0 331 }
duke@0 332 // Grab the gang mutex.
duke@0 333 MutexLocker ml(gang_monitor);
duke@0 334 gang()->internal_note_finish();
duke@0 335 // Tell the gang you are done.
duke@0 336 gang_monitor->notify_all();
duke@0 337 // Drop the gang mutex.
duke@0 338 }
duke@0 339 previous_sequence_number = data.sequence_number();
duke@0 340 }
duke@0 341 }
duke@0 342
duke@0 343 bool GangWorker::is_GC_task_thread() const {
ysr@342 344 return gang()->are_GC_task_threads();
ysr@342 345 }
ysr@342 346
ysr@342 347 bool GangWorker::is_ConcurrentGC_thread() const {
ysr@342 348 return gang()->are_ConcurrentGC_threads();
duke@0 349 }
duke@0 350
duke@0 351 void GangWorker::print_on(outputStream* st) const {
duke@0 352 st->print("\"%s\" ", name());
duke@0 353 Thread::print_on(st);
duke@0 354 st->cr();
duke@0 355 }
duke@0 356
duke@0 357 // Printing methods
duke@0 358
duke@0 359 const char* AbstractWorkGang::name() const {
duke@0 360 return _name;
duke@0 361 }
duke@0 362
duke@0 363 #ifndef PRODUCT
duke@0 364
duke@0 365 const char* AbstractGangTask::name() const {
duke@0 366 return _name;
duke@0 367 }
duke@0 368
duke@0 369 #endif /* PRODUCT */
duke@0 370
jmasa@2941 371 // FlexibleWorkGang
jmasa@2941 372
jmasa@2941 373
duke@0 374 // *** WorkGangBarrierSync
duke@0 375
duke@0 376 WorkGangBarrierSync::WorkGangBarrierSync()
duke@0 377 : _monitor(Mutex::safepoint, "work gang barrier sync", true),
ysr@342 378 _n_workers(0), _n_completed(0), _should_reset(false) {
duke@0 379 }
duke@0 380
jmasa@3008 381 WorkGangBarrierSync::WorkGangBarrierSync(uint n_workers, const char* name)
duke@0 382 : _monitor(Mutex::safepoint, name, true),
ysr@342 383 _n_workers(n_workers), _n_completed(0), _should_reset(false) {
duke@0 384 }
duke@0 385
jmasa@3008 386 void WorkGangBarrierSync::set_n_workers(uint n_workers) {
duke@0 387 _n_workers = n_workers;
duke@0 388 _n_completed = 0;
ysr@342 389 _should_reset = false;
duke@0 390 }
duke@0 391
duke@0 392 void WorkGangBarrierSync::enter() {
duke@0 393 MutexLockerEx x(monitor(), Mutex::_no_safepoint_check_flag);
ysr@342 394 if (should_reset()) {
ysr@342 395 // The should_reset() was set and we are the first worker to enter
ysr@342 396 // the sync barrier. We will zero the n_completed() count which
ysr@342 397 // effectively resets the barrier.
ysr@342 398 zero_completed();
ysr@342 399 set_should_reset(false);
ysr@342 400 }
duke@0 401 inc_completed();
duke@0 402 if (n_completed() == n_workers()) {
ysr@342 403 // At this point we would like to reset the barrier to be ready in
ysr@342 404 // case it is used again. However, we cannot set n_completed() to
ysr@342 405 // 0, even after the notify_all(), given that some other workers
ysr@342 406 // might still be waiting for n_completed() to become ==
ysr@342 407 // n_workers(). So, if we set n_completed() to 0, those workers
ysr@342 408 // will get stuck (as they will wake up, see that n_completed() !=
ysr@342 409 // n_workers() and go back to sleep). Instead, we raise the
ysr@342 410 // should_reset() flag and the barrier will be reset the first
ysr@342 411 // time a worker enters it again.
ysr@342 412 set_should_reset(true);
duke@0 413 monitor()->notify_all();
ysr@342 414 } else {
duke@0 415 while (n_completed() != n_workers()) {
duke@0 416 monitor()->wait(/* no_safepoint_check */ true);
duke@0 417 }
duke@0 418 }
duke@0 419 }
duke@0 420
duke@0 421 // SubTasksDone functions.
duke@0 422
jmasa@3008 423 SubTasksDone::SubTasksDone(uint n) :
duke@0 424 _n_tasks(n), _n_threads(1), _tasks(NULL) {
zgu@3863 425 _tasks = NEW_C_HEAP_ARRAY(uint, n, mtInternal);
duke@0 426 guarantee(_tasks != NULL, "alloc failure");
duke@0 427 clear();
duke@0 428 }
duke@0 429
duke@0 430 bool SubTasksDone::valid() {
duke@0 431 return _tasks != NULL;
duke@0 432 }
duke@0 433
jmasa@3008 434 void SubTasksDone::set_n_threads(uint t) {
duke@0 435 assert(_claimed == 0 || _threads_completed == _n_threads,
duke@0 436 "should not be called while tasks are being processed!");
duke@0 437 _n_threads = (t == 0 ? 1 : t);
duke@0 438 }
duke@0 439
duke@0 440 void SubTasksDone::clear() {
jmasa@3008 441 for (uint i = 0; i < _n_tasks; i++) {
duke@0 442 _tasks[i] = 0;
duke@0 443 }
duke@0 444 _threads_completed = 0;
duke@0 445 #ifdef ASSERT
duke@0 446 _claimed = 0;
duke@0 447 #endif
duke@0 448 }
duke@0 449
jmasa@3008 450 bool SubTasksDone::is_task_claimed(uint t) {
duke@0 451 assert(0 <= t && t < _n_tasks, "bad task id.");
jmasa@3008 452 uint old = _tasks[t];
duke@0 453 if (old == 0) {
duke@0 454 old = Atomic::cmpxchg(1, &_tasks[t], 0);
duke@0 455 }
duke@0 456 assert(_tasks[t] == 1, "What else?");
duke@0 457 bool res = old != 0;
duke@0 458 #ifdef ASSERT
duke@0 459 if (!res) {
duke@0 460 assert(_claimed < _n_tasks, "Too many tasks claimed; missing clear?");
jmasa@3008 461 Atomic::inc((volatile jint*) &_claimed);
duke@0 462 }
duke@0 463 #endif
duke@0 464 return res;
duke@0 465 }
duke@0 466
duke@0 467 void SubTasksDone::all_tasks_completed() {
duke@0 468 jint observed = _threads_completed;
duke@0 469 jint old;
duke@0 470 do {
duke@0 471 old = observed;
duke@0 472 observed = Atomic::cmpxchg(old+1, &_threads_completed, old);
duke@0 473 } while (observed != old);
duke@0 474 // If this was the last thread checking in, clear the tasks.
jmasa@3008 475 if (observed+1 == (jint)_n_threads) clear();
duke@0 476 }
duke@0 477
duke@0 478
duke@0 479 SubTasksDone::~SubTasksDone() {
zgu@3863 480 if (_tasks != NULL) FREE_C_HEAP_ARRAY(jint, _tasks, mtInternal);
duke@0 481 }
duke@0 482
duke@0 483 // *** SequentialSubTasksDone
duke@0 484
duke@0 485 void SequentialSubTasksDone::clear() {
duke@0 486 _n_tasks = _n_claimed = 0;
duke@0 487 _n_threads = _n_completed = 0;
duke@0 488 }
duke@0 489
duke@0 490 bool SequentialSubTasksDone::valid() {
duke@0 491 return _n_threads > 0;
duke@0 492 }
duke@0 493
jmasa@3008 494 bool SequentialSubTasksDone::is_task_claimed(uint& t) {
jmasa@3008 495 uint* n_claimed_ptr = &_n_claimed;
duke@0 496 t = *n_claimed_ptr;
duke@0 497 while (t < _n_tasks) {
duke@0 498 jint res = Atomic::cmpxchg(t+1, n_claimed_ptr, t);
jmasa@3008 499 if (res == (jint)t) {
duke@0 500 return false;
duke@0 501 }
duke@0 502 t = *n_claimed_ptr;
duke@0 503 }
duke@0 504 return true;
duke@0 505 }
duke@0 506
duke@0 507 bool SequentialSubTasksDone::all_tasks_completed() {
jmasa@3008 508 uint* n_completed_ptr = &_n_completed;
jmasa@3008 509 uint complete = *n_completed_ptr;
duke@0 510 while (true) {
jmasa@3008 511 uint res = Atomic::cmpxchg(complete+1, n_completed_ptr, complete);
duke@0 512 if (res == complete) {
duke@0 513 break;
duke@0 514 }
duke@0 515 complete = res;
duke@0 516 }
duke@0 517 if (complete+1 == _n_threads) {
duke@0 518 clear();
duke@0 519 return true;
duke@0 520 }
duke@0 521 return false;
duke@0 522 }
ysr@342 523
ysr@342 524 bool FreeIdSet::_stat_init = false;
ysr@342 525 FreeIdSet* FreeIdSet::_sets[NSets];
ysr@342 526 bool FreeIdSet::_safepoint;
ysr@342 527
ysr@342 528 FreeIdSet::FreeIdSet(int sz, Monitor* mon) :
ysr@342 529 _sz(sz), _mon(mon), _hd(0), _waiters(0), _index(-1), _claimed(0)
ysr@342 530 {
ysr@342 531 _ids = new int[sz];
ysr@342 532 for (int i = 0; i < sz; i++) _ids[i] = i+1;
ysr@342 533 _ids[sz-1] = end_of_list; // end of list.
ysr@342 534 if (_stat_init) {
ysr@342 535 for (int j = 0; j < NSets; j++) _sets[j] = NULL;
ysr@342 536 _stat_init = true;
ysr@342 537 }
ysr@342 538 // Add to sets. (This should happen while the system is still single-threaded.)
ysr@342 539 for (int j = 0; j < NSets; j++) {
ysr@342 540 if (_sets[j] == NULL) {
ysr@342 541 _sets[j] = this;
ysr@342 542 _index = j;
ysr@342 543 break;
ysr@342 544 }
ysr@342 545 }
ysr@342 546 guarantee(_index != -1, "Too many FreeIdSets in use!");
ysr@342 547 }
ysr@342 548
ysr@342 549 FreeIdSet::~FreeIdSet() {
ysr@342 550 _sets[_index] = NULL;
ysr@342 551 }
ysr@342 552
ysr@342 553 void FreeIdSet::set_safepoint(bool b) {
ysr@342 554 _safepoint = b;
ysr@342 555 if (b) {
ysr@342 556 for (int j = 0; j < NSets; j++) {
ysr@342 557 if (_sets[j] != NULL && _sets[j]->_waiters > 0) {
ysr@342 558 Monitor* mon = _sets[j]->_mon;
ysr@342 559 mon->lock_without_safepoint_check();
ysr@342 560 mon->notify_all();
ysr@342 561 mon->unlock();
ysr@342 562 }
ysr@342 563 }
ysr@342 564 }
ysr@342 565 }
ysr@342 566
ysr@342 567 #define FID_STATS 0
ysr@342 568
ysr@342 569 int FreeIdSet::claim_par_id() {
ysr@342 570 #if FID_STATS
ysr@342 571 thread_t tslf = thr_self();
ysr@342 572 tty->print("claim_par_id[%d]: sz = %d, claimed = %d\n", tslf, _sz, _claimed);
ysr@342 573 #endif
ysr@342 574 MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
ysr@342 575 while (!_safepoint && _hd == end_of_list) {
ysr@342 576 _waiters++;
ysr@342 577 #if FID_STATS
ysr@342 578 if (_waiters > 5) {
ysr@342 579 tty->print("claim_par_id waiting[%d]: %d waiters, %d claimed.\n",
ysr@342 580 tslf, _waiters, _claimed);
ysr@342 581 }
ysr@342 582 #endif
ysr@342 583 _mon->wait(Mutex::_no_safepoint_check_flag);
ysr@342 584 _waiters--;
ysr@342 585 }
ysr@342 586 if (_hd == end_of_list) {
ysr@342 587 #if FID_STATS
ysr@342 588 tty->print("claim_par_id[%d]: returning EOL.\n", tslf);
ysr@342 589 #endif
ysr@342 590 return -1;
ysr@342 591 } else {
ysr@342 592 int res = _hd;
ysr@342 593 _hd = _ids[res];
ysr@342 594 _ids[res] = claimed; // For debugging.
ysr@342 595 _claimed++;
ysr@342 596 #if FID_STATS
ysr@342 597 tty->print("claim_par_id[%d]: returning %d, claimed = %d.\n",
ysr@342 598 tslf, res, _claimed);
ysr@342 599 #endif
ysr@342 600 return res;
ysr@342 601 }
ysr@342 602 }
ysr@342 603
ysr@342 604 bool FreeIdSet::claim_perm_id(int i) {
ysr@342 605 assert(0 <= i && i < _sz, "Out of range.");
ysr@342 606 MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
ysr@342 607 int prev = end_of_list;
ysr@342 608 int cur = _hd;
ysr@342 609 while (cur != end_of_list) {
ysr@342 610 if (cur == i) {
ysr@342 611 if (prev == end_of_list) {
ysr@342 612 _hd = _ids[cur];
ysr@342 613 } else {
ysr@342 614 _ids[prev] = _ids[cur];
ysr@342 615 }
ysr@342 616 _ids[cur] = claimed;
ysr@342 617 _claimed++;
ysr@342 618 return true;
ysr@342 619 } else {
ysr@342 620 prev = cur;
ysr@342 621 cur = _ids[cur];
ysr@342 622 }
ysr@342 623 }
ysr@342 624 return false;
ysr@342 625
ysr@342 626 }
ysr@342 627
ysr@342 628 void FreeIdSet::release_par_id(int id) {
ysr@342 629 MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
ysr@342 630 assert(_ids[id] == claimed, "Precondition.");
ysr@342 631 _ids[id] = _hd;
ysr@342 632 _hd = id;
ysr@342 633 _claimed--;
ysr@342 634 #if FID_STATS
ysr@342 635 tty->print("[%d] release_par_id(%d), waiters =%d, claimed = %d.\n",
ysr@342 636 thr_self(), id, _waiters, _claimed);
ysr@342 637 #endif
ysr@342 638 if (_waiters > 0)
ysr@342 639 // Notify all would be safer, but this is OK, right?
ysr@342 640 _mon->notify_all();
ysr@342 641 }