src/share/vm/utilities/taskqueue.cpp

Wed, 09 Apr 2008 15:10:22 -0700

author
rasbold
date
Wed, 09 Apr 2008 15:10:22 -0700
changeset 544
9f4457a14b58
parent 435
a61af66fc99e
child 777
37f87013dfd8
permissions
-rw-r--r--

Merge

duke@435 1 /*
duke@435 2 * Copyright 2001-2006 Sun Microsystems, Inc. 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 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_taskqueue.cpp.incl"
duke@435 27
duke@435 28 bool TaskQueueSuper::peek() {
duke@435 29 return _bottom != _age.top();
duke@435 30 }
duke@435 31
duke@435 32 int TaskQueueSetSuper::randomParkAndMiller(int *seed0) {
duke@435 33 const int a = 16807;
duke@435 34 const int m = 2147483647;
duke@435 35 const int q = 127773; /* m div a */
duke@435 36 const int r = 2836; /* m mod a */
duke@435 37 assert(sizeof(int) == 4, "I think this relies on that");
duke@435 38 int seed = *seed0;
duke@435 39 int hi = seed / q;
duke@435 40 int lo = seed % q;
duke@435 41 int test = a * lo - r * hi;
duke@435 42 if (test > 0)
duke@435 43 seed = test;
duke@435 44 else
duke@435 45 seed = test + m;
duke@435 46 *seed0 = seed;
duke@435 47 return seed;
duke@435 48 }
duke@435 49
duke@435 50 ParallelTaskTerminator::
duke@435 51 ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set) :
duke@435 52 _n_threads(n_threads),
duke@435 53 _queue_set(queue_set),
duke@435 54 _offered_termination(0) {}
duke@435 55
duke@435 56 bool ParallelTaskTerminator::peek_in_queue_set() {
duke@435 57 return _queue_set->peek();
duke@435 58 }
duke@435 59
duke@435 60 void ParallelTaskTerminator::yield() {
duke@435 61 os::yield();
duke@435 62 }
duke@435 63
duke@435 64 void ParallelTaskTerminator::sleep(uint millis) {
duke@435 65 os::sleep(Thread::current(), millis, false);
duke@435 66 }
duke@435 67
duke@435 68 bool ParallelTaskTerminator::offer_termination() {
duke@435 69 Atomic::inc(&_offered_termination);
duke@435 70
duke@435 71 juint yield_count = 0;
duke@435 72 while (true) {
duke@435 73 if (_offered_termination == _n_threads) {
duke@435 74 //inner_termination_loop();
duke@435 75 return true;
duke@435 76 } else {
duke@435 77 if (yield_count <= WorkStealingYieldsBeforeSleep) {
duke@435 78 yield_count++;
duke@435 79 yield();
duke@435 80 } else {
duke@435 81 if (PrintGCDetails && Verbose) {
duke@435 82 gclog_or_tty->print_cr("ParallelTaskTerminator::offer_termination() "
duke@435 83 "thread %d sleeps after %d yields",
duke@435 84 Thread::current(), yield_count);
duke@435 85 }
duke@435 86 yield_count = 0;
duke@435 87 // A sleep will cause this processor to seek work on another processor's
duke@435 88 // runqueue, if it has nothing else to run (as opposed to the yield
duke@435 89 // which may only move the thread to the end of the this processor's
duke@435 90 // runqueue).
duke@435 91 sleep(WorkStealingSleepMillis);
duke@435 92 }
duke@435 93
duke@435 94 if (peek_in_queue_set()) {
duke@435 95 Atomic::dec(&_offered_termination);
duke@435 96 return false;
duke@435 97 }
duke@435 98 }
duke@435 99 }
duke@435 100 }
duke@435 101
duke@435 102 void ParallelTaskTerminator::reset_for_reuse() {
duke@435 103 if (_offered_termination != 0) {
duke@435 104 assert(_offered_termination == _n_threads,
duke@435 105 "Terminator may still be in use");
duke@435 106 _offered_termination = 0;
duke@435 107 }
duke@435 108 }
duke@435 109
duke@435 110 bool ChunkTaskQueueWithOverflow::is_empty() {
duke@435 111 return (_chunk_queue.size() == 0) &&
duke@435 112 (_overflow_stack->length() == 0);
duke@435 113 }
duke@435 114
duke@435 115 bool ChunkTaskQueueWithOverflow::stealable_is_empty() {
duke@435 116 return _chunk_queue.size() == 0;
duke@435 117 }
duke@435 118
duke@435 119 bool ChunkTaskQueueWithOverflow::overflow_is_empty() {
duke@435 120 return _overflow_stack->length() == 0;
duke@435 121 }
duke@435 122
duke@435 123 void ChunkTaskQueueWithOverflow::initialize() {
duke@435 124 _chunk_queue.initialize();
duke@435 125 assert(_overflow_stack == 0, "Creating memory leak");
duke@435 126 _overflow_stack =
duke@435 127 new (ResourceObj::C_HEAP) GrowableArray<ChunkTask>(10, true);
duke@435 128 }
duke@435 129
duke@435 130 void ChunkTaskQueueWithOverflow::save(ChunkTask t) {
duke@435 131 if (TraceChunkTasksQueuing && Verbose) {
duke@435 132 gclog_or_tty->print_cr("CTQ: save " PTR_FORMAT, t);
duke@435 133 }
duke@435 134 if(!_chunk_queue.push(t)) {
duke@435 135 _overflow_stack->push(t);
duke@435 136 }
duke@435 137 }
duke@435 138
duke@435 139 // Note that using this method will retrieve all chunks
duke@435 140 // that have been saved but that it will always check
duke@435 141 // the overflow stack. It may be more efficient to
duke@435 142 // check the stealable queue and the overflow stack
duke@435 143 // separately.
duke@435 144 bool ChunkTaskQueueWithOverflow::retrieve(ChunkTask& chunk_task) {
duke@435 145 bool result = retrieve_from_overflow(chunk_task);
duke@435 146 if (!result) {
duke@435 147 result = retrieve_from_stealable_queue(chunk_task);
duke@435 148 }
duke@435 149 if (TraceChunkTasksQueuing && Verbose && result) {
duke@435 150 gclog_or_tty->print_cr(" CTQ: retrieve " PTR_FORMAT, result);
duke@435 151 }
duke@435 152 return result;
duke@435 153 }
duke@435 154
duke@435 155 bool ChunkTaskQueueWithOverflow::retrieve_from_stealable_queue(
duke@435 156 ChunkTask& chunk_task) {
duke@435 157 bool result = _chunk_queue.pop_local(chunk_task);
duke@435 158 if (TraceChunkTasksQueuing && Verbose) {
duke@435 159 gclog_or_tty->print_cr("CTQ: retrieve_stealable " PTR_FORMAT, chunk_task);
duke@435 160 }
duke@435 161 return result;
duke@435 162 }
duke@435 163
duke@435 164 bool ChunkTaskQueueWithOverflow::retrieve_from_overflow(
duke@435 165 ChunkTask& chunk_task) {
duke@435 166 bool result;
duke@435 167 if (!_overflow_stack->is_empty()) {
duke@435 168 chunk_task = _overflow_stack->pop();
duke@435 169 result = true;
duke@435 170 } else {
duke@435 171 chunk_task = (ChunkTask) NULL;
duke@435 172 result = false;
duke@435 173 }
duke@435 174 if (TraceChunkTasksQueuing && Verbose) {
duke@435 175 gclog_or_tty->print_cr("CTQ: retrieve_stealable " PTR_FORMAT, chunk_task);
duke@435 176 }
duke@435 177 return result;
duke@435 178 }

mercurial