annotate src/share/vm/utilities/taskqueue.cpp @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children 37f87013dfd8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 # include "incls/_precompiled.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
26 # include "incls/_taskqueue.cpp.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
27
a61af66fc99e Initial load
duke
parents:
diff changeset
28 bool TaskQueueSuper::peek() {
a61af66fc99e Initial load
duke
parents:
diff changeset
29 return _bottom != _age.top();
a61af66fc99e Initial load
duke
parents:
diff changeset
30 }
a61af66fc99e Initial load
duke
parents:
diff changeset
31
a61af66fc99e Initial load
duke
parents:
diff changeset
32 int TaskQueueSetSuper::randomParkAndMiller(int *seed0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
33 const int a = 16807;
a61af66fc99e Initial load
duke
parents:
diff changeset
34 const int m = 2147483647;
a61af66fc99e Initial load
duke
parents:
diff changeset
35 const int q = 127773; /* m div a */
a61af66fc99e Initial load
duke
parents:
diff changeset
36 const int r = 2836; /* m mod a */
a61af66fc99e Initial load
duke
parents:
diff changeset
37 assert(sizeof(int) == 4, "I think this relies on that");
a61af66fc99e Initial load
duke
parents:
diff changeset
38 int seed = *seed0;
a61af66fc99e Initial load
duke
parents:
diff changeset
39 int hi = seed / q;
a61af66fc99e Initial load
duke
parents:
diff changeset
40 int lo = seed % q;
a61af66fc99e Initial load
duke
parents:
diff changeset
41 int test = a * lo - r * hi;
a61af66fc99e Initial load
duke
parents:
diff changeset
42 if (test > 0)
a61af66fc99e Initial load
duke
parents:
diff changeset
43 seed = test;
a61af66fc99e Initial load
duke
parents:
diff changeset
44 else
a61af66fc99e Initial load
duke
parents:
diff changeset
45 seed = test + m;
a61af66fc99e Initial load
duke
parents:
diff changeset
46 *seed0 = seed;
a61af66fc99e Initial load
duke
parents:
diff changeset
47 return seed;
a61af66fc99e Initial load
duke
parents:
diff changeset
48 }
a61af66fc99e Initial load
duke
parents:
diff changeset
49
a61af66fc99e Initial load
duke
parents:
diff changeset
50 ParallelTaskTerminator::
a61af66fc99e Initial load
duke
parents:
diff changeset
51 ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set) :
a61af66fc99e Initial load
duke
parents:
diff changeset
52 _n_threads(n_threads),
a61af66fc99e Initial load
duke
parents:
diff changeset
53 _queue_set(queue_set),
a61af66fc99e Initial load
duke
parents:
diff changeset
54 _offered_termination(0) {}
a61af66fc99e Initial load
duke
parents:
diff changeset
55
a61af66fc99e Initial load
duke
parents:
diff changeset
56 bool ParallelTaskTerminator::peek_in_queue_set() {
a61af66fc99e Initial load
duke
parents:
diff changeset
57 return _queue_set->peek();
a61af66fc99e Initial load
duke
parents:
diff changeset
58 }
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60 void ParallelTaskTerminator::yield() {
a61af66fc99e Initial load
duke
parents:
diff changeset
61 os::yield();
a61af66fc99e Initial load
duke
parents:
diff changeset
62 }
a61af66fc99e Initial load
duke
parents:
diff changeset
63
a61af66fc99e Initial load
duke
parents:
diff changeset
64 void ParallelTaskTerminator::sleep(uint millis) {
a61af66fc99e Initial load
duke
parents:
diff changeset
65 os::sleep(Thread::current(), millis, false);
a61af66fc99e Initial load
duke
parents:
diff changeset
66 }
a61af66fc99e Initial load
duke
parents:
diff changeset
67
a61af66fc99e Initial load
duke
parents:
diff changeset
68 bool ParallelTaskTerminator::offer_termination() {
a61af66fc99e Initial load
duke
parents:
diff changeset
69 Atomic::inc(&_offered_termination);
a61af66fc99e Initial load
duke
parents:
diff changeset
70
a61af66fc99e Initial load
duke
parents:
diff changeset
71 juint yield_count = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
72 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
73 if (_offered_termination == _n_threads) {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 //inner_termination_loop();
a61af66fc99e Initial load
duke
parents:
diff changeset
75 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
76 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
77 if (yield_count <= WorkStealingYieldsBeforeSleep) {
a61af66fc99e Initial load
duke
parents:
diff changeset
78 yield_count++;
a61af66fc99e Initial load
duke
parents:
diff changeset
79 yield();
a61af66fc99e Initial load
duke
parents:
diff changeset
80 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
81 if (PrintGCDetails && Verbose) {
a61af66fc99e Initial load
duke
parents:
diff changeset
82 gclog_or_tty->print_cr("ParallelTaskTerminator::offer_termination() "
a61af66fc99e Initial load
duke
parents:
diff changeset
83 "thread %d sleeps after %d yields",
a61af66fc99e Initial load
duke
parents:
diff changeset
84 Thread::current(), yield_count);
a61af66fc99e Initial load
duke
parents:
diff changeset
85 }
a61af66fc99e Initial load
duke
parents:
diff changeset
86 yield_count = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
87 // A sleep will cause this processor to seek work on another processor's
a61af66fc99e Initial load
duke
parents:
diff changeset
88 // runqueue, if it has nothing else to run (as opposed to the yield
a61af66fc99e Initial load
duke
parents:
diff changeset
89 // which may only move the thread to the end of the this processor's
a61af66fc99e Initial load
duke
parents:
diff changeset
90 // runqueue).
a61af66fc99e Initial load
duke
parents:
diff changeset
91 sleep(WorkStealingSleepMillis);
a61af66fc99e Initial load
duke
parents:
diff changeset
92 }
a61af66fc99e Initial load
duke
parents:
diff changeset
93
a61af66fc99e Initial load
duke
parents:
diff changeset
94 if (peek_in_queue_set()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 Atomic::dec(&_offered_termination);
a61af66fc99e Initial load
duke
parents:
diff changeset
96 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
97 }
a61af66fc99e Initial load
duke
parents:
diff changeset
98 }
a61af66fc99e Initial load
duke
parents:
diff changeset
99 }
a61af66fc99e Initial load
duke
parents:
diff changeset
100 }
a61af66fc99e Initial load
duke
parents:
diff changeset
101
a61af66fc99e Initial load
duke
parents:
diff changeset
102 void ParallelTaskTerminator::reset_for_reuse() {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 if (_offered_termination != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
104 assert(_offered_termination == _n_threads,
a61af66fc99e Initial load
duke
parents:
diff changeset
105 "Terminator may still be in use");
a61af66fc99e Initial load
duke
parents:
diff changeset
106 _offered_termination = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
107 }
a61af66fc99e Initial load
duke
parents:
diff changeset
108 }
a61af66fc99e Initial load
duke
parents:
diff changeset
109
a61af66fc99e Initial load
duke
parents:
diff changeset
110 bool ChunkTaskQueueWithOverflow::is_empty() {
a61af66fc99e Initial load
duke
parents:
diff changeset
111 return (_chunk_queue.size() == 0) &&
a61af66fc99e Initial load
duke
parents:
diff changeset
112 (_overflow_stack->length() == 0);
a61af66fc99e Initial load
duke
parents:
diff changeset
113 }
a61af66fc99e Initial load
duke
parents:
diff changeset
114
a61af66fc99e Initial load
duke
parents:
diff changeset
115 bool ChunkTaskQueueWithOverflow::stealable_is_empty() {
a61af66fc99e Initial load
duke
parents:
diff changeset
116 return _chunk_queue.size() == 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
117 }
a61af66fc99e Initial load
duke
parents:
diff changeset
118
a61af66fc99e Initial load
duke
parents:
diff changeset
119 bool ChunkTaskQueueWithOverflow::overflow_is_empty() {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 return _overflow_stack->length() == 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
121 }
a61af66fc99e Initial load
duke
parents:
diff changeset
122
a61af66fc99e Initial load
duke
parents:
diff changeset
123 void ChunkTaskQueueWithOverflow::initialize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
124 _chunk_queue.initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
125 assert(_overflow_stack == 0, "Creating memory leak");
a61af66fc99e Initial load
duke
parents:
diff changeset
126 _overflow_stack =
a61af66fc99e Initial load
duke
parents:
diff changeset
127 new (ResourceObj::C_HEAP) GrowableArray<ChunkTask>(10, true);
a61af66fc99e Initial load
duke
parents:
diff changeset
128 }
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130 void ChunkTaskQueueWithOverflow::save(ChunkTask t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
131 if (TraceChunkTasksQueuing && Verbose) {
a61af66fc99e Initial load
duke
parents:
diff changeset
132 gclog_or_tty->print_cr("CTQ: save " PTR_FORMAT, t);
a61af66fc99e Initial load
duke
parents:
diff changeset
133 }
a61af66fc99e Initial load
duke
parents:
diff changeset
134 if(!_chunk_queue.push(t)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
135 _overflow_stack->push(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
136 }
a61af66fc99e Initial load
duke
parents:
diff changeset
137 }
a61af66fc99e Initial load
duke
parents:
diff changeset
138
a61af66fc99e Initial load
duke
parents:
diff changeset
139 // Note that using this method will retrieve all chunks
a61af66fc99e Initial load
duke
parents:
diff changeset
140 // that have been saved but that it will always check
a61af66fc99e Initial load
duke
parents:
diff changeset
141 // the overflow stack. It may be more efficient to
a61af66fc99e Initial load
duke
parents:
diff changeset
142 // check the stealable queue and the overflow stack
a61af66fc99e Initial load
duke
parents:
diff changeset
143 // separately.
a61af66fc99e Initial load
duke
parents:
diff changeset
144 bool ChunkTaskQueueWithOverflow::retrieve(ChunkTask& chunk_task) {
a61af66fc99e Initial load
duke
parents:
diff changeset
145 bool result = retrieve_from_overflow(chunk_task);
a61af66fc99e Initial load
duke
parents:
diff changeset
146 if (!result) {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 result = retrieve_from_stealable_queue(chunk_task);
a61af66fc99e Initial load
duke
parents:
diff changeset
148 }
a61af66fc99e Initial load
duke
parents:
diff changeset
149 if (TraceChunkTasksQueuing && Verbose && result) {
a61af66fc99e Initial load
duke
parents:
diff changeset
150 gclog_or_tty->print_cr(" CTQ: retrieve " PTR_FORMAT, result);
a61af66fc99e Initial load
duke
parents:
diff changeset
151 }
a61af66fc99e Initial load
duke
parents:
diff changeset
152 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
153 }
a61af66fc99e Initial load
duke
parents:
diff changeset
154
a61af66fc99e Initial load
duke
parents:
diff changeset
155 bool ChunkTaskQueueWithOverflow::retrieve_from_stealable_queue(
a61af66fc99e Initial load
duke
parents:
diff changeset
156 ChunkTask& chunk_task) {
a61af66fc99e Initial load
duke
parents:
diff changeset
157 bool result = _chunk_queue.pop_local(chunk_task);
a61af66fc99e Initial load
duke
parents:
diff changeset
158 if (TraceChunkTasksQueuing && Verbose) {
a61af66fc99e Initial load
duke
parents:
diff changeset
159 gclog_or_tty->print_cr("CTQ: retrieve_stealable " PTR_FORMAT, chunk_task);
a61af66fc99e Initial load
duke
parents:
diff changeset
160 }
a61af66fc99e Initial load
duke
parents:
diff changeset
161 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
162 }
a61af66fc99e Initial load
duke
parents:
diff changeset
163
a61af66fc99e Initial load
duke
parents:
diff changeset
164 bool ChunkTaskQueueWithOverflow::retrieve_from_overflow(
a61af66fc99e Initial load
duke
parents:
diff changeset
165 ChunkTask& chunk_task) {
a61af66fc99e Initial load
duke
parents:
diff changeset
166 bool result;
a61af66fc99e Initial load
duke
parents:
diff changeset
167 if (!_overflow_stack->is_empty()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
168 chunk_task = _overflow_stack->pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
169 result = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
170 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
171 chunk_task = (ChunkTask) NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
172 result = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
173 }
a61af66fc99e Initial load
duke
parents:
diff changeset
174 if (TraceChunkTasksQueuing && Verbose) {
a61af66fc99e Initial load
duke
parents:
diff changeset
175 gclog_or_tty->print_cr("CTQ: retrieve_stealable " PTR_FORMAT, chunk_task);
a61af66fc99e Initial load
duke
parents:
diff changeset
176 }
a61af66fc99e Initial load
duke
parents:
diff changeset
177 return result;
a61af66fc99e Initial load
duke
parents:
diff changeset
178 }