src/share/vm/utilities/workgroup.cpp

Mon, 25 Jun 2012 21:33:35 -0400

author
coleenp
date
Mon, 25 Jun 2012 21:33:35 -0400
changeset 3875
246d977b51f2
parent 3357
441e946dc1af
child 3900
d2a62e0f25eb
permissions
-rw-r--r--

7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
Summary: Cannot delete _buckets and HashtableEntries in shared space (CDS)
Reviewed-by: acorn, kvn, dlong, dcubed, kamg

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

mercurial