taskqueue.cpp revision 375
869N/A/*
869N/A * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved.
869N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
869N/A *
869N/A * This code is free software; you can redistribute it and/or modify it
869N/A * under the terms of the GNU General Public License version 2 only, as
869N/A * published by the Free Software Foundation.
869N/A *
869N/A * This code is distributed in the hope that it will be useful, but WITHOUT
869N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
869N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
869N/A * version 2 for more details (a copy is included in the LICENSE file that
869N/A * accompanied this code).
869N/A *
869N/A * You should have received a copy of the GNU General Public License version
869N/A * 2 along with this work; if not, write to the Free Software Foundation,
869N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
873N/A *
869N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
869N/A * CA 95054 USA or visit www.sun.com if you need additional information or
869N/A * have any questions.
869N/A *
5005N/A */
5299N/A
869N/A# include "incls/_precompiled.incl"
869N/A# include "incls/_taskqueue.cpp.incl"
0N/A
0N/Abool TaskQueueSuper::peek() {
0N/A return _bottom != _age.top();
0N/A}
869N/A
0N/Aint TaskQueueSetSuper::randomParkAndMiller(int *seed0) {
0N/A const int a = 16807;
0N/A const int m = 2147483647;
869N/A const int q = 127773; /* m div a */
0N/A const int r = 2836; /* m mod a */
869N/A assert(sizeof(int) == 4, "I think this relies on that");
0N/A int seed = *seed0;
869N/A int hi = seed / q;
869N/A int lo = seed % q;
869N/A int test = a * lo - r * hi;
2624N/A if (test > 0)
869N/A seed = test;
48N/A else
869N/A seed = test + m;
0N/A *seed0 = seed;
869N/A return seed;
716N/A}
869N/A
1958N/AParallelTaskTerminator::
1963N/AParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set) :
2340N/A _n_threads(n_threads),
3103N/A _queue_set(queue_set),
3127N/A _offered_termination(0) {}
3679N/A
5244N/Abool ParallelTaskTerminator::peek_in_queue_set() {
1954N/A return _queue_set->peek();
1954N/A}
1954N/A
1954N/Avoid ParallelTaskTerminator::yield() {
1954N/A os::yield();
1954N/A}
4306N/A
4306N/Avoid ParallelTaskTerminator::sleep(uint millis) {
4306N/A os::sleep(Thread::current(), millis, false);
1954N/A}
1954N/A
1954N/Abool
1954N/AParallelTaskTerminator::offer_termination(TerminatorTerminator* terminator) {
5221N/A Atomic::inc(&_offered_termination);
0N/A
0N/A juint yield_count = 0;
869N/A while (true) {
868N/A if (_offered_termination == _n_threads) {
2624N/A //inner_termination_loop();
2693N/A return true;
2042N/A } else {
4563N/A if (yield_count <= WorkStealingYieldsBeforeSleep) {
5408N/A yield_count++;
4898N/A yield();
4898N/A } else {
2980N/A if (PrintGCDetails && Verbose) {
2963N/A gclog_or_tty->print_cr("ParallelTaskTerminator::offer_termination() "
1503N/A "thread %d sleeps after %d yields",
2915N/A Thread::current(), yield_count);
869N/A }
2624N/A yield_count = 0;
2624N/A // A sleep will cause this processor to seek work on another processor's
0N/A // runqueue, if it has nothing else to run (as opposed to the yield
2270N/A // which may only move the thread to the end of the this processor's
2270N/A // runqueue).
2270N/A sleep(WorkStealingSleepMillis);
2270N/A }
2270N/A
2270N/A if (peek_in_queue_set() ||
2270N/A (terminator != NULL && terminator->should_exit_termination())) {
0N/A Atomic::dec(&_offered_termination);
869N/A return false;
868N/A }
0N/A }
0N/A }
65N/A}
869N/A
868N/Avoid ParallelTaskTerminator::reset_for_reuse() {
65N/A if (_offered_termination != 0) {
869N/A assert(_offered_termination == _n_threads,
2624N/A "Terminator may still be in use");
2624N/A _offered_termination = 0;
65N/A }
65N/A}
65N/A
65N/Abool RegionTaskQueueWithOverflow::is_empty() {
65N/A return (_region_queue.size() == 0) &&
65N/A (_overflow_stack->length() == 0);
65N/A}
65N/A
65N/Abool RegionTaskQueueWithOverflow::stealable_is_empty() {
65N/A return _region_queue.size() == 0;
65N/A}
65N/A
65N/Abool RegionTaskQueueWithOverflow::overflow_is_empty() {
2266N/A return _overflow_stack->length() == 0;
2266N/A}
2266N/A
2266N/Avoid RegionTaskQueueWithOverflow::initialize() {
2266N/A _region_queue.initialize();
2624N/A assert(_overflow_stack == 0, "Creating memory leak");
2624N/A _overflow_stack =
2266N/A new (ResourceObj::C_HEAP) GrowableArray<RegionTask>(10, true);
2266N/A}
2624N/A
2266N/Avoid RegionTaskQueueWithOverflow::save(RegionTask t) {
2266N/A if (TraceRegionTasksQueuing && Verbose) {
2266N/A gclog_or_tty->print_cr("CTQ: save " PTR_FORMAT, t);
2266N/A }
2266N/A if(!_region_queue.push(t)) {
2266N/A _overflow_stack->push(t);
2266N/A }
2266N/A}
2266N/A
2266N/A// Note that using this method will retrieve all regions
2266N/A// that have been saved but that it will always check
2266N/A// the overflow stack. It may be more efficient to
2266N/A// check the stealable queue and the overflow stack
2266N/A// separately.
2266N/Abool RegionTaskQueueWithOverflow::retrieve(RegionTask& region_task) {
2266N/A bool result = retrieve_from_overflow(region_task);
2266N/A if (!result) {
2266N/A result = retrieve_from_stealable_queue(region_task);
2266N/A }
2266N/A if (TraceRegionTasksQueuing && Verbose && result) {
2266N/A gclog_or_tty->print_cr(" CTQ: retrieve " PTR_FORMAT, result);
2266N/A }
2266N/A return result;
2266N/A}
2266N/A
0N/Abool RegionTaskQueueWithOverflow::retrieve_from_stealable_queue(
869N/A RegionTask& region_task) {
868N/A bool result = _region_queue.pop_local(region_task);
0N/A if (TraceRegionTasksQueuing && Verbose) {
0N/A gclog_or_tty->print_cr("CTQ: retrieve_stealable " PTR_FORMAT, region_task);
0N/A }
0N/A return result;
0N/A}
2334N/A
0N/Abool
2624N/ARegionTaskQueueWithOverflow::retrieve_from_overflow(RegionTask& region_task) {
2624N/A bool result;
0N/A if (!_overflow_stack->is_empty()) {
0N/A region_task = _overflow_stack->pop();
869N/A result = true;
868N/A } else {
0N/A region_task = (RegionTask) NULL;
0N/A result = false;
869N/A }
0N/A if (TraceRegionTasksQueuing && Verbose) {
0N/A gclog_or_tty->print_cr("CTQ: retrieve_stealable " PTR_FORMAT, region_task);
2624N/A }
2624N/A return result;
2624N/A}
869N/A