duke@435: /* ysr@2651: * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "runtime/os.hpp" stefank@2314: #include "utilities/workgroup.hpp" duke@435: duke@435: // Definitions of WorkGang methods. duke@435: duke@435: AbstractWorkGang::AbstractWorkGang(const char* name, ysr@777: bool are_GC_task_threads, ysr@777: bool are_ConcurrentGC_threads) : duke@435: _name(name), ysr@777: _are_GC_task_threads(are_GC_task_threads), ysr@777: _are_ConcurrentGC_threads(are_ConcurrentGC_threads) { ysr@777: ysr@777: assert(!(are_GC_task_threads && are_ConcurrentGC_threads), ysr@777: "They cannot both be STW GC and Concurrent threads" ); ysr@777: duke@435: // Other initialization. duke@435: _monitor = new Monitor(/* priority */ Mutex::leaf, duke@435: /* name */ "WorkGroup monitor", ysr@777: /* allow_vm_block */ are_GC_task_threads); duke@435: assert(monitor() != NULL, "Failed to allocate monitor"); duke@435: _terminate = false; duke@435: _task = NULL; duke@435: _sequence_number = 0; duke@435: _started_workers = 0; duke@435: _finished_workers = 0; duke@435: } duke@435: duke@435: WorkGang::WorkGang(const char* name, jmasa@3357: uint workers, ysr@777: bool are_GC_task_threads, ysr@777: bool are_ConcurrentGC_threads) : jmasa@2188: AbstractWorkGang(name, are_GC_task_threads, are_ConcurrentGC_threads) { duke@435: _total_workers = workers; jmasa@2188: } jmasa@2188: jmasa@3357: GangWorker* WorkGang::allocate_worker(uint which) { jmasa@2188: GangWorker* new_worker = new GangWorker(this, which); jmasa@2188: return new_worker; jmasa@2188: } jmasa@2188: jmasa@2188: // The current implementation will exit if the allocation jmasa@2188: // of any worker fails. Still, return a boolean so that jmasa@2188: // a future implementation can possibly do a partial jmasa@2188: // initialization of the workers and report such to the jmasa@2188: // caller. jmasa@2188: bool WorkGang::initialize_workers() { ysr@777: duke@435: if (TraceWorkGang) { jmasa@2188: tty->print_cr("Constructing work gang %s with %d threads", jmasa@2188: name(), jmasa@2188: total_workers()); duke@435: } jmasa@2188: _gang_workers = NEW_C_HEAP_ARRAY(GangWorker*, total_workers()); ysr@777: if (gang_workers() == NULL) { ysr@777: vm_exit_out_of_memory(0, "Cannot create GangWorker array."); jmasa@2188: return false; jmasa@2188: } jmasa@2188: os::ThreadType worker_type; jmasa@2188: if (are_ConcurrentGC_threads()) { jmasa@2188: worker_type = os::cgc_thread; jmasa@2188: } else { jmasa@2188: worker_type = os::pgc_thread; ysr@777: } jmasa@3357: for (uint worker = 0; worker < total_workers(); worker += 1) { jmasa@2188: GangWorker* new_worker = allocate_worker(worker); duke@435: assert(new_worker != NULL, "Failed to allocate GangWorker"); duke@435: _gang_workers[worker] = new_worker; jmasa@2188: if (new_worker == NULL || !os::create_thread(new_worker, worker_type)) { duke@435: vm_exit_out_of_memory(0, "Cannot create worker GC thread. Out of system resources."); jmasa@2188: return false; jmasa@2188: } duke@435: if (!DisableStartThread) { duke@435: os::start_thread(new_worker); duke@435: } duke@435: } jmasa@2188: return true; duke@435: } duke@435: duke@435: AbstractWorkGang::~AbstractWorkGang() { duke@435: if (TraceWorkGang) { duke@435: tty->print_cr("Destructing work gang %s", name()); duke@435: } duke@435: stop(); // stop all the workers jmasa@3357: for (uint worker = 0; worker < total_workers(); worker += 1) { duke@435: delete gang_worker(worker); duke@435: } duke@435: delete gang_workers(); duke@435: delete monitor(); duke@435: } duke@435: jmasa@3357: GangWorker* AbstractWorkGang::gang_worker(uint i) const { duke@435: // Array index bounds checking. duke@435: GangWorker* result = NULL; duke@435: assert(gang_workers() != NULL, "No workers for indexing"); duke@435: assert(((i >= 0) && (i < total_workers())), "Worker index out of bounds"); duke@435: result = _gang_workers[i]; duke@435: assert(result != NULL, "Indexing to null worker"); duke@435: return result; duke@435: } duke@435: duke@435: void WorkGang::run_task(AbstractGangTask* task) { jmasa@3294: run_task(task, total_workers()); jmasa@3294: } jmasa@3294: jmasa@3294: void WorkGang::run_task(AbstractGangTask* task, uint no_of_parallel_workers) { jmasa@3294: task->set_for_termination(no_of_parallel_workers); jmasa@3294: duke@435: // This thread is executed by the VM thread which does not block duke@435: // on ordinary MutexLocker's. duke@435: MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag); duke@435: if (TraceWorkGang) { duke@435: tty->print_cr("Running work gang %s task %s", name(), task->name()); duke@435: } duke@435: // Tell all the workers to run a task. duke@435: assert(task != NULL, "Running a null task"); duke@435: // Initialize. duke@435: _task = task; duke@435: _sequence_number += 1; duke@435: _started_workers = 0; duke@435: _finished_workers = 0; duke@435: // Tell the workers to get to work. duke@435: monitor()->notify_all(); duke@435: // Wait for them to be finished jmasa@3357: while (finished_workers() < no_of_parallel_workers) { duke@435: if (TraceWorkGang) { duke@435: tty->print_cr("Waiting in work gang %s: %d/%d finished sequence %d", jmasa@3294: name(), finished_workers(), no_of_parallel_workers, duke@435: _sequence_number); duke@435: } duke@435: monitor()->wait(/* no_safepoint_check */ true); duke@435: } duke@435: _task = NULL; duke@435: if (TraceWorkGang) { jmasa@3294: tty->print_cr("\nFinished work gang %s: %d/%d sequence %d", jmasa@3294: name(), finished_workers(), no_of_parallel_workers, duke@435: _sequence_number); jmasa@3294: Thread* me = Thread::current(); jmasa@3294: tty->print_cr(" T: 0x%x VM_thread: %d", me, me->is_VM_thread()); ysr@2651: } duke@435: } duke@435: jmasa@3294: void FlexibleWorkGang::run_task(AbstractGangTask* task) { jmasa@3294: // If active_workers() is passed, _finished_workers jmasa@3294: // must only be incremented for workers that find non_null jmasa@3294: // work (as opposed to all those that just check that the jmasa@3294: // task is not null). jmasa@3294: WorkGang::run_task(task, (uint) active_workers()); jmasa@3294: } jmasa@3294: duke@435: void AbstractWorkGang::stop() { duke@435: // Tell all workers to terminate, then wait for them to become inactive. duke@435: MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag); duke@435: if (TraceWorkGang) { duke@435: tty->print_cr("Stopping work gang %s task %s", name(), task()->name()); duke@435: } duke@435: _task = NULL; duke@435: _terminate = true; duke@435: monitor()->notify_all(); jmasa@3294: while (finished_workers() < active_workers()) { duke@435: if (TraceWorkGang) { duke@435: tty->print_cr("Waiting in work gang %s: %d/%d finished", jmasa@3294: name(), finished_workers(), active_workers()); duke@435: } duke@435: monitor()->wait(/* no_safepoint_check */ true); duke@435: } duke@435: } duke@435: duke@435: void AbstractWorkGang::internal_worker_poll(WorkData* data) const { duke@435: assert(monitor()->owned_by_self(), "worker_poll is an internal method"); duke@435: assert(data != NULL, "worker data is null"); duke@435: data->set_terminate(terminate()); duke@435: data->set_task(task()); duke@435: data->set_sequence_number(sequence_number()); duke@435: } duke@435: duke@435: void AbstractWorkGang::internal_note_start() { duke@435: assert(monitor()->owned_by_self(), "note_finish is an internal method"); duke@435: _started_workers += 1; duke@435: } duke@435: duke@435: void AbstractWorkGang::internal_note_finish() { duke@435: assert(monitor()->owned_by_self(), "note_finish is an internal method"); duke@435: _finished_workers += 1; duke@435: } duke@435: duke@435: void AbstractWorkGang::print_worker_threads_on(outputStream* st) const { duke@435: uint num_thr = total_workers(); duke@435: for (uint i = 0; i < num_thr; i++) { duke@435: gang_worker(i)->print_on(st); duke@435: st->cr(); duke@435: } duke@435: } duke@435: duke@435: void AbstractWorkGang::threads_do(ThreadClosure* tc) const { duke@435: assert(tc != NULL, "Null ThreadClosure"); duke@435: uint num_thr = total_workers(); duke@435: for (uint i = 0; i < num_thr; i++) { duke@435: tc->do_thread(gang_worker(i)); duke@435: } duke@435: } duke@435: duke@435: // GangWorker methods. duke@435: duke@435: GangWorker::GangWorker(AbstractWorkGang* gang, uint id) { duke@435: _gang = gang; duke@435: set_id(id); duke@435: set_name("Gang worker#%d (%s)", id, gang->name()); duke@435: } duke@435: duke@435: void GangWorker::run() { duke@435: initialize(); duke@435: loop(); duke@435: } duke@435: duke@435: void GangWorker::initialize() { duke@435: this->initialize_thread_local_storage(); duke@435: assert(_gang != NULL, "No gang to run in"); duke@435: os::set_priority(this, NearMaxPriority); duke@435: if (TraceWorkGang) { duke@435: tty->print_cr("Running gang worker for gang %s id %d", duke@435: gang()->name(), id()); duke@435: } duke@435: // The VM thread should not execute here because MutexLocker's are used duke@435: // as (opposed to MutexLockerEx's). duke@435: assert(!Thread::current()->is_VM_thread(), "VM thread should not be part" duke@435: " of a work gang"); duke@435: } duke@435: duke@435: void GangWorker::loop() { duke@435: int previous_sequence_number = 0; duke@435: Monitor* gang_monitor = gang()->monitor(); duke@435: for ( ; /* !terminate() */; ) { duke@435: WorkData data; duke@435: int part; // Initialized below. duke@435: { duke@435: // Grab the gang mutex. duke@435: MutexLocker ml(gang_monitor); duke@435: // Wait for something to do. duke@435: // Polling outside the while { wait } avoids missed notifies duke@435: // in the outer loop. duke@435: gang()->internal_worker_poll(&data); duke@435: if (TraceWorkGang) { duke@435: tty->print("Polled outside for work in gang %s worker %d", duke@435: gang()->name(), id()); duke@435: tty->print(" terminate: %s", duke@435: data.terminate() ? "true" : "false"); duke@435: tty->print(" sequence: %d (prev: %d)", duke@435: data.sequence_number(), previous_sequence_number); duke@435: if (data.task() != NULL) { duke@435: tty->print(" task: %s", data.task()->name()); duke@435: } else { duke@435: tty->print(" task: NULL"); duke@435: } duke@435: tty->cr(); duke@435: } duke@435: for ( ; /* break or return */; ) { duke@435: // Terminate if requested. duke@435: if (data.terminate()) { duke@435: gang()->internal_note_finish(); duke@435: gang_monitor->notify_all(); duke@435: return; duke@435: } duke@435: // Check for new work. duke@435: if ((data.task() != NULL) && duke@435: (data.sequence_number() != previous_sequence_number)) { jmasa@3294: if (gang()->needs_more_workers()) { jmasa@3294: gang()->internal_note_start(); jmasa@3294: gang_monitor->notify_all(); jmasa@3294: part = gang()->started_workers() - 1; jmasa@3294: break; jmasa@3294: } duke@435: } duke@435: // Nothing to do. duke@435: gang_monitor->wait(/* no_safepoint_check */ true); duke@435: gang()->internal_worker_poll(&data); duke@435: if (TraceWorkGang) { duke@435: tty->print("Polled inside for work in gang %s worker %d", duke@435: gang()->name(), id()); duke@435: tty->print(" terminate: %s", duke@435: data.terminate() ? "true" : "false"); duke@435: tty->print(" sequence: %d (prev: %d)", duke@435: data.sequence_number(), previous_sequence_number); duke@435: if (data.task() != NULL) { duke@435: tty->print(" task: %s", data.task()->name()); duke@435: } else { duke@435: tty->print(" task: NULL"); duke@435: } duke@435: tty->cr(); duke@435: } duke@435: } duke@435: // Drop gang mutex. duke@435: } duke@435: if (TraceWorkGang) { duke@435: tty->print("Work for work gang %s id %d task %s part %d", duke@435: gang()->name(), id(), data.task()->name(), part); duke@435: } duke@435: assert(data.task() != NULL, "Got null task"); duke@435: data.task()->work(part); duke@435: { duke@435: if (TraceWorkGang) { duke@435: tty->print("Finish for work gang %s id %d task %s part %d", duke@435: gang()->name(), id(), data.task()->name(), part); duke@435: } duke@435: // Grab the gang mutex. duke@435: MutexLocker ml(gang_monitor); duke@435: gang()->internal_note_finish(); duke@435: // Tell the gang you are done. duke@435: gang_monitor->notify_all(); duke@435: // Drop the gang mutex. duke@435: } duke@435: previous_sequence_number = data.sequence_number(); duke@435: } duke@435: } duke@435: duke@435: bool GangWorker::is_GC_task_thread() const { ysr@777: return gang()->are_GC_task_threads(); ysr@777: } ysr@777: ysr@777: bool GangWorker::is_ConcurrentGC_thread() const { ysr@777: return gang()->are_ConcurrentGC_threads(); duke@435: } duke@435: duke@435: void GangWorker::print_on(outputStream* st) const { duke@435: st->print("\"%s\" ", name()); duke@435: Thread::print_on(st); duke@435: st->cr(); duke@435: } duke@435: duke@435: // Printing methods duke@435: duke@435: const char* AbstractWorkGang::name() const { duke@435: return _name; duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: const char* AbstractGangTask::name() const { duke@435: return _name; duke@435: } duke@435: duke@435: #endif /* PRODUCT */ duke@435: jmasa@3294: // FlexibleWorkGang jmasa@3294: jmasa@3294: duke@435: // *** WorkGangBarrierSync duke@435: duke@435: WorkGangBarrierSync::WorkGangBarrierSync() duke@435: : _monitor(Mutex::safepoint, "work gang barrier sync", true), ysr@777: _n_workers(0), _n_completed(0), _should_reset(false) { duke@435: } duke@435: jmasa@3357: WorkGangBarrierSync::WorkGangBarrierSync(uint n_workers, const char* name) duke@435: : _monitor(Mutex::safepoint, name, true), ysr@777: _n_workers(n_workers), _n_completed(0), _should_reset(false) { duke@435: } duke@435: jmasa@3357: void WorkGangBarrierSync::set_n_workers(uint n_workers) { duke@435: _n_workers = n_workers; duke@435: _n_completed = 0; ysr@777: _should_reset = false; duke@435: } duke@435: duke@435: void WorkGangBarrierSync::enter() { duke@435: MutexLockerEx x(monitor(), Mutex::_no_safepoint_check_flag); ysr@777: if (should_reset()) { ysr@777: // The should_reset() was set and we are the first worker to enter ysr@777: // the sync barrier. We will zero the n_completed() count which ysr@777: // effectively resets the barrier. ysr@777: zero_completed(); ysr@777: set_should_reset(false); ysr@777: } duke@435: inc_completed(); duke@435: if (n_completed() == n_workers()) { ysr@777: // At this point we would like to reset the barrier to be ready in ysr@777: // case it is used again. However, we cannot set n_completed() to ysr@777: // 0, even after the notify_all(), given that some other workers ysr@777: // might still be waiting for n_completed() to become == ysr@777: // n_workers(). So, if we set n_completed() to 0, those workers ysr@777: // will get stuck (as they will wake up, see that n_completed() != ysr@777: // n_workers() and go back to sleep). Instead, we raise the ysr@777: // should_reset() flag and the barrier will be reset the first ysr@777: // time a worker enters it again. ysr@777: set_should_reset(true); duke@435: monitor()->notify_all(); ysr@777: } else { duke@435: while (n_completed() != n_workers()) { duke@435: monitor()->wait(/* no_safepoint_check */ true); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // SubTasksDone functions. duke@435: jmasa@3357: SubTasksDone::SubTasksDone(uint n) : duke@435: _n_tasks(n), _n_threads(1), _tasks(NULL) { jmasa@3357: _tasks = NEW_C_HEAP_ARRAY(uint, n); duke@435: guarantee(_tasks != NULL, "alloc failure"); duke@435: clear(); duke@435: } duke@435: duke@435: bool SubTasksDone::valid() { duke@435: return _tasks != NULL; duke@435: } duke@435: jmasa@3357: void SubTasksDone::set_n_threads(uint t) { duke@435: assert(_claimed == 0 || _threads_completed == _n_threads, duke@435: "should not be called while tasks are being processed!"); duke@435: _n_threads = (t == 0 ? 1 : t); duke@435: } duke@435: duke@435: void SubTasksDone::clear() { jmasa@3357: for (uint i = 0; i < _n_tasks; i++) { duke@435: _tasks[i] = 0; duke@435: } duke@435: _threads_completed = 0; duke@435: #ifdef ASSERT duke@435: _claimed = 0; duke@435: #endif duke@435: } duke@435: jmasa@3357: bool SubTasksDone::is_task_claimed(uint t) { duke@435: assert(0 <= t && t < _n_tasks, "bad task id."); jmasa@3357: uint old = _tasks[t]; duke@435: if (old == 0) { duke@435: old = Atomic::cmpxchg(1, &_tasks[t], 0); duke@435: } duke@435: assert(_tasks[t] == 1, "What else?"); duke@435: bool res = old != 0; duke@435: #ifdef ASSERT duke@435: if (!res) { duke@435: assert(_claimed < _n_tasks, "Too many tasks claimed; missing clear?"); jmasa@3357: Atomic::inc((volatile jint*) &_claimed); duke@435: } duke@435: #endif duke@435: return res; duke@435: } duke@435: duke@435: void SubTasksDone::all_tasks_completed() { duke@435: jint observed = _threads_completed; duke@435: jint old; duke@435: do { duke@435: old = observed; duke@435: observed = Atomic::cmpxchg(old+1, &_threads_completed, old); duke@435: } while (observed != old); duke@435: // If this was the last thread checking in, clear the tasks. jmasa@3357: if (observed+1 == (jint)_n_threads) clear(); duke@435: } duke@435: duke@435: duke@435: SubTasksDone::~SubTasksDone() { duke@435: if (_tasks != NULL) FREE_C_HEAP_ARRAY(jint, _tasks); duke@435: } duke@435: duke@435: // *** SequentialSubTasksDone duke@435: duke@435: void SequentialSubTasksDone::clear() { duke@435: _n_tasks = _n_claimed = 0; duke@435: _n_threads = _n_completed = 0; duke@435: } duke@435: duke@435: bool SequentialSubTasksDone::valid() { duke@435: return _n_threads > 0; duke@435: } duke@435: jmasa@3357: bool SequentialSubTasksDone::is_task_claimed(uint& t) { jmasa@3357: uint* n_claimed_ptr = &_n_claimed; duke@435: t = *n_claimed_ptr; duke@435: while (t < _n_tasks) { duke@435: jint res = Atomic::cmpxchg(t+1, n_claimed_ptr, t); jmasa@3357: if (res == (jint)t) { duke@435: return false; duke@435: } duke@435: t = *n_claimed_ptr; duke@435: } duke@435: return true; duke@435: } duke@435: duke@435: bool SequentialSubTasksDone::all_tasks_completed() { jmasa@3357: uint* n_completed_ptr = &_n_completed; jmasa@3357: uint complete = *n_completed_ptr; duke@435: while (true) { jmasa@3357: uint res = Atomic::cmpxchg(complete+1, n_completed_ptr, complete); duke@435: if (res == complete) { duke@435: break; duke@435: } duke@435: complete = res; duke@435: } duke@435: if (complete+1 == _n_threads) { duke@435: clear(); duke@435: return true; duke@435: } duke@435: return false; duke@435: } ysr@777: ysr@777: bool FreeIdSet::_stat_init = false; ysr@777: FreeIdSet* FreeIdSet::_sets[NSets]; ysr@777: bool FreeIdSet::_safepoint; ysr@777: ysr@777: FreeIdSet::FreeIdSet(int sz, Monitor* mon) : ysr@777: _sz(sz), _mon(mon), _hd(0), _waiters(0), _index(-1), _claimed(0) ysr@777: { ysr@777: _ids = new int[sz]; ysr@777: for (int i = 0; i < sz; i++) _ids[i] = i+1; ysr@777: _ids[sz-1] = end_of_list; // end of list. ysr@777: if (_stat_init) { ysr@777: for (int j = 0; j < NSets; j++) _sets[j] = NULL; ysr@777: _stat_init = true; ysr@777: } ysr@777: // Add to sets. (This should happen while the system is still single-threaded.) ysr@777: for (int j = 0; j < NSets; j++) { ysr@777: if (_sets[j] == NULL) { ysr@777: _sets[j] = this; ysr@777: _index = j; ysr@777: break; ysr@777: } ysr@777: } ysr@777: guarantee(_index != -1, "Too many FreeIdSets in use!"); ysr@777: } ysr@777: ysr@777: FreeIdSet::~FreeIdSet() { ysr@777: _sets[_index] = NULL; ysr@777: } ysr@777: ysr@777: void FreeIdSet::set_safepoint(bool b) { ysr@777: _safepoint = b; ysr@777: if (b) { ysr@777: for (int j = 0; j < NSets; j++) { ysr@777: if (_sets[j] != NULL && _sets[j]->_waiters > 0) { ysr@777: Monitor* mon = _sets[j]->_mon; ysr@777: mon->lock_without_safepoint_check(); ysr@777: mon->notify_all(); ysr@777: mon->unlock(); ysr@777: } ysr@777: } ysr@777: } ysr@777: } ysr@777: ysr@777: #define FID_STATS 0 ysr@777: ysr@777: int FreeIdSet::claim_par_id() { ysr@777: #if FID_STATS ysr@777: thread_t tslf = thr_self(); ysr@777: tty->print("claim_par_id[%d]: sz = %d, claimed = %d\n", tslf, _sz, _claimed); ysr@777: #endif ysr@777: MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag); ysr@777: while (!_safepoint && _hd == end_of_list) { ysr@777: _waiters++; ysr@777: #if FID_STATS ysr@777: if (_waiters > 5) { ysr@777: tty->print("claim_par_id waiting[%d]: %d waiters, %d claimed.\n", ysr@777: tslf, _waiters, _claimed); ysr@777: } ysr@777: #endif ysr@777: _mon->wait(Mutex::_no_safepoint_check_flag); ysr@777: _waiters--; ysr@777: } ysr@777: if (_hd == end_of_list) { ysr@777: #if FID_STATS ysr@777: tty->print("claim_par_id[%d]: returning EOL.\n", tslf); ysr@777: #endif ysr@777: return -1; ysr@777: } else { ysr@777: int res = _hd; ysr@777: _hd = _ids[res]; ysr@777: _ids[res] = claimed; // For debugging. ysr@777: _claimed++; ysr@777: #if FID_STATS ysr@777: tty->print("claim_par_id[%d]: returning %d, claimed = %d.\n", ysr@777: tslf, res, _claimed); ysr@777: #endif ysr@777: return res; ysr@777: } ysr@777: } ysr@777: ysr@777: bool FreeIdSet::claim_perm_id(int i) { ysr@777: assert(0 <= i && i < _sz, "Out of range."); ysr@777: MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag); ysr@777: int prev = end_of_list; ysr@777: int cur = _hd; ysr@777: while (cur != end_of_list) { ysr@777: if (cur == i) { ysr@777: if (prev == end_of_list) { ysr@777: _hd = _ids[cur]; ysr@777: } else { ysr@777: _ids[prev] = _ids[cur]; ysr@777: } ysr@777: _ids[cur] = claimed; ysr@777: _claimed++; ysr@777: return true; ysr@777: } else { ysr@777: prev = cur; ysr@777: cur = _ids[cur]; ysr@777: } ysr@777: } ysr@777: return false; ysr@777: ysr@777: } ysr@777: ysr@777: void FreeIdSet::release_par_id(int id) { ysr@777: MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag); ysr@777: assert(_ids[id] == claimed, "Precondition."); ysr@777: _ids[id] = _hd; ysr@777: _hd = id; ysr@777: _claimed--; ysr@777: #if FID_STATS ysr@777: tty->print("[%d] release_par_id(%d), waiters =%d, claimed = %d.\n", ysr@777: thr_self(), id, _waiters, _claimed); ysr@777: #endif ysr@777: if (_waiters > 0) ysr@777: // Notify all would be safer, but this is OK, right? ysr@777: _mon->notify_all(); ysr@777: }