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