annotate src/share/vm/utilities/taskqueue.hpp @ 375:81cd571500b0

6725697: par compact - rename class ChunkData to RegionData Reviewed-by: iveresov, tonyp
author jcoomes
date Tue, 30 Sep 2008 12:20:22 -0700
parents 1ee8caae33af
children 23673011938d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
196
d1605aabd0a1 6719955: Update copyright year
xdono
parents: 113
diff changeset
2 * Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved.
0
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 class TaskQueueSuper: public CHeapObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
26 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
27 // The first free element after the last one pushed (mod _n).
a61af66fc99e Initial load
duke
parents:
diff changeset
28 // (For now we'll assume only 32-bit CAS).
a61af66fc99e Initial load
duke
parents:
diff changeset
29 volatile juint _bottom;
a61af66fc99e Initial load
duke
parents:
diff changeset
30
a61af66fc99e Initial load
duke
parents:
diff changeset
31 // log2 of the size of the queue.
a61af66fc99e Initial load
duke
parents:
diff changeset
32 enum SomeProtectedConstants {
a61af66fc99e Initial load
duke
parents:
diff changeset
33 Log_n = 14
a61af66fc99e Initial load
duke
parents:
diff changeset
34 };
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36 // Size of the queue.
a61af66fc99e Initial load
duke
parents:
diff changeset
37 juint n() { return (1 << Log_n); }
a61af66fc99e Initial load
duke
parents:
diff changeset
38 // For computing "x mod n" efficiently.
a61af66fc99e Initial load
duke
parents:
diff changeset
39 juint n_mod_mask() { return n() - 1; }
a61af66fc99e Initial load
duke
parents:
diff changeset
40
a61af66fc99e Initial load
duke
parents:
diff changeset
41 struct Age {
a61af66fc99e Initial load
duke
parents:
diff changeset
42 jushort _top;
a61af66fc99e Initial load
duke
parents:
diff changeset
43 jushort _tag;
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 jushort tag() const { return _tag; }
a61af66fc99e Initial load
duke
parents:
diff changeset
46 jushort top() const { return _top; }
a61af66fc99e Initial load
duke
parents:
diff changeset
47
a61af66fc99e Initial load
duke
parents:
diff changeset
48 Age() { _tag = 0; _top = 0; }
a61af66fc99e Initial load
duke
parents:
diff changeset
49
a61af66fc99e Initial load
duke
parents:
diff changeset
50 friend bool operator ==(const Age& a1, const Age& a2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
51 return a1.tag() == a2.tag() && a1.top() == a2.top();
a61af66fc99e Initial load
duke
parents:
diff changeset
52 }
a61af66fc99e Initial load
duke
parents:
diff changeset
53
a61af66fc99e Initial load
duke
parents:
diff changeset
54 };
a61af66fc99e Initial load
duke
parents:
diff changeset
55 Age _age;
a61af66fc99e Initial load
duke
parents:
diff changeset
56 // These make sure we do single atomic reads and writes.
a61af66fc99e Initial load
duke
parents:
diff changeset
57 Age get_age() {
a61af66fc99e Initial load
duke
parents:
diff changeset
58 jint res = *(volatile jint*)(&_age);
a61af66fc99e Initial load
duke
parents:
diff changeset
59 return *(Age*)(&res);
a61af66fc99e Initial load
duke
parents:
diff changeset
60 }
a61af66fc99e Initial load
duke
parents:
diff changeset
61 void set_age(Age a) {
a61af66fc99e Initial load
duke
parents:
diff changeset
62 *(volatile jint*)(&_age) = *(int*)(&a);
a61af66fc99e Initial load
duke
parents:
diff changeset
63 }
a61af66fc99e Initial load
duke
parents:
diff changeset
64
a61af66fc99e Initial load
duke
parents:
diff changeset
65 jushort get_top() {
a61af66fc99e Initial load
duke
parents:
diff changeset
66 return get_age().top();
a61af66fc99e Initial load
duke
parents:
diff changeset
67 }
a61af66fc99e Initial load
duke
parents:
diff changeset
68
a61af66fc99e Initial load
duke
parents:
diff changeset
69 // These both operate mod _n.
a61af66fc99e Initial load
duke
parents:
diff changeset
70 juint increment_index(juint ind) {
a61af66fc99e Initial load
duke
parents:
diff changeset
71 return (ind + 1) & n_mod_mask();
a61af66fc99e Initial load
duke
parents:
diff changeset
72 }
a61af66fc99e Initial load
duke
parents:
diff changeset
73 juint decrement_index(juint ind) {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 return (ind - 1) & n_mod_mask();
a61af66fc99e Initial load
duke
parents:
diff changeset
75 }
a61af66fc99e Initial load
duke
parents:
diff changeset
76
a61af66fc99e Initial load
duke
parents:
diff changeset
77 // Returns a number in the range [0.._n). If the result is "n-1", it
a61af66fc99e Initial load
duke
parents:
diff changeset
78 // should be interpreted as 0.
a61af66fc99e Initial load
duke
parents:
diff changeset
79 juint dirty_size(juint bot, juint top) {
a61af66fc99e Initial load
duke
parents:
diff changeset
80 return ((jint)bot - (jint)top) & n_mod_mask();
a61af66fc99e Initial load
duke
parents:
diff changeset
81 }
a61af66fc99e Initial load
duke
parents:
diff changeset
82
a61af66fc99e Initial load
duke
parents:
diff changeset
83 // Returns the size corresponding to the given "bot" and "top".
a61af66fc99e Initial load
duke
parents:
diff changeset
84 juint size(juint bot, juint top) {
a61af66fc99e Initial load
duke
parents:
diff changeset
85 juint sz = dirty_size(bot, top);
a61af66fc99e Initial load
duke
parents:
diff changeset
86 // Has the queue "wrapped", so that bottom is less than top?
a61af66fc99e Initial load
duke
parents:
diff changeset
87 // There's a complicated special case here. A pair of threads could
a61af66fc99e Initial load
duke
parents:
diff changeset
88 // perform pop_local and pop_global operations concurrently, starting
a61af66fc99e Initial load
duke
parents:
diff changeset
89 // from a state in which _bottom == _top+1. The pop_local could
a61af66fc99e Initial load
duke
parents:
diff changeset
90 // succeed in decrementing _bottom, and the pop_global in incrementing
a61af66fc99e Initial load
duke
parents:
diff changeset
91 // _top (in which case the pop_global will be awarded the contested
a61af66fc99e Initial load
duke
parents:
diff changeset
92 // queue element.) The resulting state must be interpreted as an empty
a61af66fc99e Initial load
duke
parents:
diff changeset
93 // queue. (We only need to worry about one such event: only the queue
a61af66fc99e Initial load
duke
parents:
diff changeset
94 // owner performs pop_local's, and several concurrent threads
a61af66fc99e Initial load
duke
parents:
diff changeset
95 // attempting to perform the pop_global will all perform the same CAS,
a61af66fc99e Initial load
duke
parents:
diff changeset
96 // and only one can succeed. Any stealing thread that reads after
a61af66fc99e Initial load
duke
parents:
diff changeset
97 // either the increment or decrement will seen an empty queue, and will
a61af66fc99e Initial load
duke
parents:
diff changeset
98 // not join the competitors. The "sz == -1 || sz == _n-1" state will
a61af66fc99e Initial load
duke
parents:
diff changeset
99 // not be modified by concurrent queues, so the owner thread can reset
a61af66fc99e Initial load
duke
parents:
diff changeset
100 // the state to _bottom == top so subsequent pushes will be performed
a61af66fc99e Initial load
duke
parents:
diff changeset
101 // normally.
a61af66fc99e Initial load
duke
parents:
diff changeset
102 if (sz == (n()-1)) return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
103 else return sz;
a61af66fc99e Initial load
duke
parents:
diff changeset
104 }
a61af66fc99e Initial load
duke
parents:
diff changeset
105
a61af66fc99e Initial load
duke
parents:
diff changeset
106 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
107 TaskQueueSuper() : _bottom(0), _age() {}
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 // Return "true" if the TaskQueue contains any tasks.
a61af66fc99e Initial load
duke
parents:
diff changeset
110 bool peek();
a61af66fc99e Initial load
duke
parents:
diff changeset
111
a61af66fc99e Initial load
duke
parents:
diff changeset
112 // Return an estimate of the number of elements in the queue.
a61af66fc99e Initial load
duke
parents:
diff changeset
113 // The "careful" version admits the possibility of pop_local/pop_global
a61af66fc99e Initial load
duke
parents:
diff changeset
114 // races.
a61af66fc99e Initial load
duke
parents:
diff changeset
115 juint size() {
a61af66fc99e Initial load
duke
parents:
diff changeset
116 return size(_bottom, get_top());
a61af66fc99e Initial load
duke
parents:
diff changeset
117 }
a61af66fc99e Initial load
duke
parents:
diff changeset
118
a61af66fc99e Initial load
duke
parents:
diff changeset
119 juint dirty_size() {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 return dirty_size(_bottom, get_top());
a61af66fc99e Initial load
duke
parents:
diff changeset
121 }
a61af66fc99e Initial load
duke
parents:
diff changeset
122
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
123 void set_empty() {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
124 _bottom = 0;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
125 _age = Age();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
126 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
127
0
a61af66fc99e Initial load
duke
parents:
diff changeset
128 // Maximum number of elements allowed in the queue. This is two less
a61af66fc99e Initial load
duke
parents:
diff changeset
129 // than the actual queue size, for somewhat complicated reasons.
a61af66fc99e Initial load
duke
parents:
diff changeset
130 juint max_elems() { return n() - 2; }
a61af66fc99e Initial load
duke
parents:
diff changeset
131
a61af66fc99e Initial load
duke
parents:
diff changeset
132 };
a61af66fc99e Initial load
duke
parents:
diff changeset
133
a61af66fc99e Initial load
duke
parents:
diff changeset
134 template<class E> class GenericTaskQueue: public TaskQueueSuper {
a61af66fc99e Initial load
duke
parents:
diff changeset
135 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
136 // Slow paths for push, pop_local. (pop_global has no fast path.)
a61af66fc99e Initial load
duke
parents:
diff changeset
137 bool push_slow(E t, juint dirty_n_elems);
a61af66fc99e Initial load
duke
parents:
diff changeset
138 bool pop_local_slow(juint localBot, Age oldAge);
a61af66fc99e Initial load
duke
parents:
diff changeset
139
a61af66fc99e Initial load
duke
parents:
diff changeset
140 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
141 // Initializes the queue to empty.
a61af66fc99e Initial load
duke
parents:
diff changeset
142 GenericTaskQueue();
a61af66fc99e Initial load
duke
parents:
diff changeset
143
a61af66fc99e Initial load
duke
parents:
diff changeset
144 void initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
145
a61af66fc99e Initial load
duke
parents:
diff changeset
146 // Push the task "t" on the queue. Returns "false" iff the queue is
a61af66fc99e Initial load
duke
parents:
diff changeset
147 // full.
a61af66fc99e Initial load
duke
parents:
diff changeset
148 inline bool push(E t);
a61af66fc99e Initial load
duke
parents:
diff changeset
149
a61af66fc99e Initial load
duke
parents:
diff changeset
150 // If succeeds in claiming a task (from the 'local' end, that is, the
a61af66fc99e Initial load
duke
parents:
diff changeset
151 // most recently pushed task), returns "true" and sets "t" to that task.
a61af66fc99e Initial load
duke
parents:
diff changeset
152 // Otherwise, the queue is empty and returns false.
a61af66fc99e Initial load
duke
parents:
diff changeset
153 inline bool pop_local(E& t);
a61af66fc99e Initial load
duke
parents:
diff changeset
154
a61af66fc99e Initial load
duke
parents:
diff changeset
155 // If succeeds in claiming a task (from the 'global' end, that is, the
a61af66fc99e Initial load
duke
parents:
diff changeset
156 // least recently pushed task), returns "true" and sets "t" to that task.
a61af66fc99e Initial load
duke
parents:
diff changeset
157 // Otherwise, the queue is empty and returns false.
a61af66fc99e Initial load
duke
parents:
diff changeset
158 bool pop_global(E& t);
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 // Delete any resource associated with the queue.
a61af66fc99e Initial load
duke
parents:
diff changeset
161 ~GenericTaskQueue();
a61af66fc99e Initial load
duke
parents:
diff changeset
162
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
163 // apply the closure to all elements in the task queue
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
164 void oops_do(OopClosure* f);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
165
0
a61af66fc99e Initial load
duke
parents:
diff changeset
166 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
167 // Element array.
a61af66fc99e Initial load
duke
parents:
diff changeset
168 volatile E* _elems;
a61af66fc99e Initial load
duke
parents:
diff changeset
169 };
a61af66fc99e Initial load
duke
parents:
diff changeset
170
a61af66fc99e Initial load
duke
parents:
diff changeset
171 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
172 GenericTaskQueue<E>::GenericTaskQueue():TaskQueueSuper() {
a61af66fc99e Initial load
duke
parents:
diff changeset
173 assert(sizeof(Age) == sizeof(jint), "Depends on this.");
a61af66fc99e Initial load
duke
parents:
diff changeset
174 }
a61af66fc99e Initial load
duke
parents:
diff changeset
175
a61af66fc99e Initial load
duke
parents:
diff changeset
176 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
177 void GenericTaskQueue<E>::initialize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
178 _elems = NEW_C_HEAP_ARRAY(E, n());
a61af66fc99e Initial load
duke
parents:
diff changeset
179 guarantee(_elems != NULL, "Allocation failed.");
a61af66fc99e Initial load
duke
parents:
diff changeset
180 }
a61af66fc99e Initial load
duke
parents:
diff changeset
181
a61af66fc99e Initial load
duke
parents:
diff changeset
182 template<class E>
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
183 void GenericTaskQueue<E>::oops_do(OopClosure* f) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
184 // tty->print_cr("START OopTaskQueue::oops_do");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
185 int iters = size();
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
186 juint index = _bottom;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
187 for (int i = 0; i < iters; ++i) {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
188 index = decrement_index(index);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
189 // tty->print_cr(" doing entry %d," INTPTR_T " -> " INTPTR_T,
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
190 // index, &_elems[index], _elems[index]);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
191 E* t = (E*)&_elems[index]; // cast away volatility
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
192 oop* p = (oop*)t;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
193 assert((*t)->is_oop_or_null(), "Not an oop or null");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
194 f->do_oop(p);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
195 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
196 // tty->print_cr("END OopTaskQueue::oops_do");
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
197 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
198
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
199
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
200 template<class E>
0
a61af66fc99e Initial load
duke
parents:
diff changeset
201 bool GenericTaskQueue<E>::push_slow(E t, juint dirty_n_elems) {
a61af66fc99e Initial load
duke
parents:
diff changeset
202 if (dirty_n_elems == n() - 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
203 // Actually means 0, so do the push.
a61af66fc99e Initial load
duke
parents:
diff changeset
204 juint localBot = _bottom;
a61af66fc99e Initial load
duke
parents:
diff changeset
205 _elems[localBot] = t;
a61af66fc99e Initial load
duke
parents:
diff changeset
206 _bottom = increment_index(localBot);
a61af66fc99e Initial load
duke
parents:
diff changeset
207 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
208 } else
a61af66fc99e Initial load
duke
parents:
diff changeset
209 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
210 }
a61af66fc99e Initial load
duke
parents:
diff changeset
211
a61af66fc99e Initial load
duke
parents:
diff changeset
212 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
213 bool GenericTaskQueue<E>::
a61af66fc99e Initial load
duke
parents:
diff changeset
214 pop_local_slow(juint localBot, Age oldAge) {
a61af66fc99e Initial load
duke
parents:
diff changeset
215 // This queue was observed to contain exactly one element; either this
a61af66fc99e Initial load
duke
parents:
diff changeset
216 // thread will claim it, or a competing "pop_global". In either case,
a61af66fc99e Initial load
duke
parents:
diff changeset
217 // the queue will be logically empty afterwards. Create a new Age value
a61af66fc99e Initial load
duke
parents:
diff changeset
218 // that represents the empty queue for the given value of "_bottom". (We
a61af66fc99e Initial load
duke
parents:
diff changeset
219 // must also increment "tag" because of the case where "bottom == 1",
a61af66fc99e Initial load
duke
parents:
diff changeset
220 // "top == 0". A pop_global could read the queue element in that case,
a61af66fc99e Initial load
duke
parents:
diff changeset
221 // then have the owner thread do a pop followed by another push. Without
a61af66fc99e Initial load
duke
parents:
diff changeset
222 // the incrementing of "tag", the pop_global's CAS could succeed,
a61af66fc99e Initial load
duke
parents:
diff changeset
223 // allowing it to believe it has claimed the stale element.)
a61af66fc99e Initial load
duke
parents:
diff changeset
224 Age newAge;
a61af66fc99e Initial load
duke
parents:
diff changeset
225 newAge._top = localBot;
a61af66fc99e Initial load
duke
parents:
diff changeset
226 newAge._tag = oldAge.tag() + 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
227 // Perhaps a competing pop_global has already incremented "top", in which
a61af66fc99e Initial load
duke
parents:
diff changeset
228 // case it wins the element.
a61af66fc99e Initial load
duke
parents:
diff changeset
229 if (localBot == oldAge.top()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
230 Age tempAge;
a61af66fc99e Initial load
duke
parents:
diff changeset
231 // No competing pop_global has yet incremented "top"; we'll try to
a61af66fc99e Initial load
duke
parents:
diff changeset
232 // install new_age, thus claiming the element.
a61af66fc99e Initial load
duke
parents:
diff changeset
233 assert(sizeof(Age) == sizeof(jint) && sizeof(jint) == sizeof(juint),
a61af66fc99e Initial load
duke
parents:
diff changeset
234 "Assumption about CAS unit.");
a61af66fc99e Initial load
duke
parents:
diff changeset
235 *(jint*)&tempAge = Atomic::cmpxchg(*(jint*)&newAge, (volatile jint*)&_age, *(jint*)&oldAge);
a61af66fc99e Initial load
duke
parents:
diff changeset
236 if (tempAge == oldAge) {
a61af66fc99e Initial load
duke
parents:
diff changeset
237 // We win.
a61af66fc99e Initial load
duke
parents:
diff changeset
238 assert(dirty_size(localBot, get_top()) != n() - 1,
a61af66fc99e Initial load
duke
parents:
diff changeset
239 "Shouldn't be possible...");
a61af66fc99e Initial load
duke
parents:
diff changeset
240 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
241 }
a61af66fc99e Initial load
duke
parents:
diff changeset
242 }
a61af66fc99e Initial load
duke
parents:
diff changeset
243 // We fail; a completing pop_global gets the element. But the queue is
a61af66fc99e Initial load
duke
parents:
diff changeset
244 // empty (and top is greater than bottom.) Fix this representation of
a61af66fc99e Initial load
duke
parents:
diff changeset
245 // the empty queue to become the canonical one.
a61af66fc99e Initial load
duke
parents:
diff changeset
246 set_age(newAge);
a61af66fc99e Initial load
duke
parents:
diff changeset
247 assert(dirty_size(localBot, get_top()) != n() - 1,
a61af66fc99e Initial load
duke
parents:
diff changeset
248 "Shouldn't be possible...");
a61af66fc99e Initial load
duke
parents:
diff changeset
249 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
250 }
a61af66fc99e Initial load
duke
parents:
diff changeset
251
a61af66fc99e Initial load
duke
parents:
diff changeset
252 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
253 bool GenericTaskQueue<E>::pop_global(E& t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
254 Age newAge;
a61af66fc99e Initial load
duke
parents:
diff changeset
255 Age oldAge = get_age();
a61af66fc99e Initial load
duke
parents:
diff changeset
256 juint localBot = _bottom;
a61af66fc99e Initial load
duke
parents:
diff changeset
257 juint n_elems = size(localBot, oldAge.top());
a61af66fc99e Initial load
duke
parents:
diff changeset
258 if (n_elems == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
259 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
260 }
a61af66fc99e Initial load
duke
parents:
diff changeset
261 t = _elems[oldAge.top()];
a61af66fc99e Initial load
duke
parents:
diff changeset
262 newAge = oldAge;
a61af66fc99e Initial load
duke
parents:
diff changeset
263 newAge._top = increment_index(newAge.top());
a61af66fc99e Initial load
duke
parents:
diff changeset
264 if ( newAge._top == 0 ) newAge._tag++;
a61af66fc99e Initial load
duke
parents:
diff changeset
265 Age resAge;
a61af66fc99e Initial load
duke
parents:
diff changeset
266 *(jint*)&resAge = Atomic::cmpxchg(*(jint*)&newAge, (volatile jint*)&_age, *(jint*)&oldAge);
a61af66fc99e Initial load
duke
parents:
diff changeset
267 // Note that using "_bottom" here might fail, since a pop_local might
a61af66fc99e Initial load
duke
parents:
diff changeset
268 // have decremented it.
a61af66fc99e Initial load
duke
parents:
diff changeset
269 assert(dirty_size(localBot, newAge._top) != n() - 1,
a61af66fc99e Initial load
duke
parents:
diff changeset
270 "Shouldn't be possible...");
a61af66fc99e Initial load
duke
parents:
diff changeset
271 return (resAge == oldAge);
a61af66fc99e Initial load
duke
parents:
diff changeset
272 }
a61af66fc99e Initial load
duke
parents:
diff changeset
273
a61af66fc99e Initial load
duke
parents:
diff changeset
274 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
275 GenericTaskQueue<E>::~GenericTaskQueue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
276 FREE_C_HEAP_ARRAY(E, _elems);
a61af66fc99e Initial load
duke
parents:
diff changeset
277 }
a61af66fc99e Initial load
duke
parents:
diff changeset
278
a61af66fc99e Initial load
duke
parents:
diff changeset
279 // Inherits the typedef of "Task" from above.
a61af66fc99e Initial load
duke
parents:
diff changeset
280 class TaskQueueSetSuper: public CHeapObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
281 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
282 static int randomParkAndMiller(int* seed0);
a61af66fc99e Initial load
duke
parents:
diff changeset
283 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
284 // Returns "true" if some TaskQueue in the set contains a task.
a61af66fc99e Initial load
duke
parents:
diff changeset
285 virtual bool peek() = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
286 };
a61af66fc99e Initial load
duke
parents:
diff changeset
287
a61af66fc99e Initial load
duke
parents:
diff changeset
288 template<class E> class GenericTaskQueueSet: public TaskQueueSetSuper {
a61af66fc99e Initial load
duke
parents:
diff changeset
289 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
290 int _n;
a61af66fc99e Initial load
duke
parents:
diff changeset
291 GenericTaskQueue<E>** _queues;
a61af66fc99e Initial load
duke
parents:
diff changeset
292
a61af66fc99e Initial load
duke
parents:
diff changeset
293 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
294 GenericTaskQueueSet(int n) : _n(n) {
a61af66fc99e Initial load
duke
parents:
diff changeset
295 typedef GenericTaskQueue<E>* GenericTaskQueuePtr;
a61af66fc99e Initial load
duke
parents:
diff changeset
296 _queues = NEW_C_HEAP_ARRAY(GenericTaskQueuePtr, n);
a61af66fc99e Initial load
duke
parents:
diff changeset
297 guarantee(_queues != NULL, "Allocation failure.");
a61af66fc99e Initial load
duke
parents:
diff changeset
298 for (int i = 0; i < n; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
299 _queues[i] = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
300 }
a61af66fc99e Initial load
duke
parents:
diff changeset
301 }
a61af66fc99e Initial load
duke
parents:
diff changeset
302
a61af66fc99e Initial load
duke
parents:
diff changeset
303 bool steal_1_random(int queue_num, int* seed, E& t);
a61af66fc99e Initial load
duke
parents:
diff changeset
304 bool steal_best_of_2(int queue_num, int* seed, E& t);
a61af66fc99e Initial load
duke
parents:
diff changeset
305 bool steal_best_of_all(int queue_num, int* seed, E& t);
a61af66fc99e Initial load
duke
parents:
diff changeset
306
a61af66fc99e Initial load
duke
parents:
diff changeset
307 void register_queue(int i, GenericTaskQueue<E>* q);
a61af66fc99e Initial load
duke
parents:
diff changeset
308
a61af66fc99e Initial load
duke
parents:
diff changeset
309 GenericTaskQueue<E>* queue(int n);
a61af66fc99e Initial load
duke
parents:
diff changeset
310
a61af66fc99e Initial load
duke
parents:
diff changeset
311 // The thread with queue number "queue_num" (and whose random number seed
a61af66fc99e Initial load
duke
parents:
diff changeset
312 // is at "seed") is trying to steal a task from some other queue. (It
a61af66fc99e Initial load
duke
parents:
diff changeset
313 // may try several queues, according to some configuration parameter.)
a61af66fc99e Initial load
duke
parents:
diff changeset
314 // If some steal succeeds, returns "true" and sets "t" the stolen task,
a61af66fc99e Initial load
duke
parents:
diff changeset
315 // otherwise returns false.
a61af66fc99e Initial load
duke
parents:
diff changeset
316 bool steal(int queue_num, int* seed, E& t);
a61af66fc99e Initial load
duke
parents:
diff changeset
317
a61af66fc99e Initial load
duke
parents:
diff changeset
318 bool peek();
a61af66fc99e Initial load
duke
parents:
diff changeset
319 };
a61af66fc99e Initial load
duke
parents:
diff changeset
320
a61af66fc99e Initial load
duke
parents:
diff changeset
321 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
322 void GenericTaskQueueSet<E>::register_queue(int i, GenericTaskQueue<E>* q) {
a61af66fc99e Initial load
duke
parents:
diff changeset
323 assert(0 <= i && i < _n, "index out of range.");
a61af66fc99e Initial load
duke
parents:
diff changeset
324 _queues[i] = q;
a61af66fc99e Initial load
duke
parents:
diff changeset
325 }
a61af66fc99e Initial load
duke
parents:
diff changeset
326
a61af66fc99e Initial load
duke
parents:
diff changeset
327 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
328 GenericTaskQueue<E>* GenericTaskQueueSet<E>::queue(int i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
329 return _queues[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
330 }
a61af66fc99e Initial load
duke
parents:
diff changeset
331
a61af66fc99e Initial load
duke
parents:
diff changeset
332 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
333 bool GenericTaskQueueSet<E>::steal(int queue_num, int* seed, E& t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
334 for (int i = 0; i < 2 * _n; i++)
a61af66fc99e Initial load
duke
parents:
diff changeset
335 if (steal_best_of_2(queue_num, seed, t))
a61af66fc99e Initial load
duke
parents:
diff changeset
336 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
337 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
338 }
a61af66fc99e Initial load
duke
parents:
diff changeset
339
a61af66fc99e Initial load
duke
parents:
diff changeset
340 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
341 bool GenericTaskQueueSet<E>::steal_best_of_all(int queue_num, int* seed, E& t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
342 if (_n > 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
343 int best_k;
a61af66fc99e Initial load
duke
parents:
diff changeset
344 jint best_sz = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
345 for (int k = 0; k < _n; k++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
346 if (k == queue_num) continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
347 jint sz = _queues[k]->size();
a61af66fc99e Initial load
duke
parents:
diff changeset
348 if (sz > best_sz) {
a61af66fc99e Initial load
duke
parents:
diff changeset
349 best_sz = sz;
a61af66fc99e Initial load
duke
parents:
diff changeset
350 best_k = k;
a61af66fc99e Initial load
duke
parents:
diff changeset
351 }
a61af66fc99e Initial load
duke
parents:
diff changeset
352 }
a61af66fc99e Initial load
duke
parents:
diff changeset
353 return best_sz > 0 && _queues[best_k]->pop_global(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
354 } else if (_n == 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
355 // Just try the other one.
a61af66fc99e Initial load
duke
parents:
diff changeset
356 int k = (queue_num + 1) % 2;
a61af66fc99e Initial load
duke
parents:
diff changeset
357 return _queues[k]->pop_global(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
358 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
359 assert(_n == 1, "can't be zero.");
a61af66fc99e Initial load
duke
parents:
diff changeset
360 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
361 }
a61af66fc99e Initial load
duke
parents:
diff changeset
362 }
a61af66fc99e Initial load
duke
parents:
diff changeset
363
a61af66fc99e Initial load
duke
parents:
diff changeset
364 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
365 bool GenericTaskQueueSet<E>::steal_1_random(int queue_num, int* seed, E& t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
366 if (_n > 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
367 int k = queue_num;
a61af66fc99e Initial load
duke
parents:
diff changeset
368 while (k == queue_num) k = randomParkAndMiller(seed) % _n;
a61af66fc99e Initial load
duke
parents:
diff changeset
369 return _queues[2]->pop_global(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
370 } else if (_n == 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
371 // Just try the other one.
a61af66fc99e Initial load
duke
parents:
diff changeset
372 int k = (queue_num + 1) % 2;
a61af66fc99e Initial load
duke
parents:
diff changeset
373 return _queues[k]->pop_global(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
374 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
375 assert(_n == 1, "can't be zero.");
a61af66fc99e Initial load
duke
parents:
diff changeset
376 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
377 }
a61af66fc99e Initial load
duke
parents:
diff changeset
378 }
a61af66fc99e Initial load
duke
parents:
diff changeset
379
a61af66fc99e Initial load
duke
parents:
diff changeset
380 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
381 bool GenericTaskQueueSet<E>::steal_best_of_2(int queue_num, int* seed, E& t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
382 if (_n > 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
383 int k1 = queue_num;
a61af66fc99e Initial load
duke
parents:
diff changeset
384 while (k1 == queue_num) k1 = randomParkAndMiller(seed) % _n;
a61af66fc99e Initial load
duke
parents:
diff changeset
385 int k2 = queue_num;
a61af66fc99e Initial load
duke
parents:
diff changeset
386 while (k2 == queue_num || k2 == k1) k2 = randomParkAndMiller(seed) % _n;
a61af66fc99e Initial load
duke
parents:
diff changeset
387 // Sample both and try the larger.
a61af66fc99e Initial load
duke
parents:
diff changeset
388 juint sz1 = _queues[k1]->size();
a61af66fc99e Initial load
duke
parents:
diff changeset
389 juint sz2 = _queues[k2]->size();
a61af66fc99e Initial load
duke
parents:
diff changeset
390 if (sz2 > sz1) return _queues[k2]->pop_global(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
391 else return _queues[k1]->pop_global(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
392 } else if (_n == 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
393 // Just try the other one.
a61af66fc99e Initial load
duke
parents:
diff changeset
394 int k = (queue_num + 1) % 2;
a61af66fc99e Initial load
duke
parents:
diff changeset
395 return _queues[k]->pop_global(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
396 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
397 assert(_n == 1, "can't be zero.");
a61af66fc99e Initial load
duke
parents:
diff changeset
398 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
399 }
a61af66fc99e Initial load
duke
parents:
diff changeset
400 }
a61af66fc99e Initial load
duke
parents:
diff changeset
401
a61af66fc99e Initial load
duke
parents:
diff changeset
402 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
403 bool GenericTaskQueueSet<E>::peek() {
a61af66fc99e Initial load
duke
parents:
diff changeset
404 // Try all the queues.
a61af66fc99e Initial load
duke
parents:
diff changeset
405 for (int j = 0; j < _n; j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
406 if (_queues[j]->peek())
a61af66fc99e Initial load
duke
parents:
diff changeset
407 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
408 }
a61af66fc99e Initial load
duke
parents:
diff changeset
409 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
410 }
a61af66fc99e Initial load
duke
parents:
diff changeset
411
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
412 // When to terminate from the termination protocol.
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
413 class TerminatorTerminator: public CHeapObj {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
414 public:
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
415 virtual bool should_exit_termination() = 0;
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
416 };
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
417
0
a61af66fc99e Initial load
duke
parents:
diff changeset
418 // A class to aid in the termination of a set of parallel tasks using
a61af66fc99e Initial load
duke
parents:
diff changeset
419 // TaskQueueSet's for work stealing.
a61af66fc99e Initial load
duke
parents:
diff changeset
420
a61af66fc99e Initial load
duke
parents:
diff changeset
421 class ParallelTaskTerminator: public StackObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
422 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
423 int _n_threads;
a61af66fc99e Initial load
duke
parents:
diff changeset
424 TaskQueueSetSuper* _queue_set;
a61af66fc99e Initial load
duke
parents:
diff changeset
425 jint _offered_termination;
a61af66fc99e Initial load
duke
parents:
diff changeset
426
a61af66fc99e Initial load
duke
parents:
diff changeset
427 bool peek_in_queue_set();
a61af66fc99e Initial load
duke
parents:
diff changeset
428 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
429 virtual void yield();
a61af66fc99e Initial load
duke
parents:
diff changeset
430 void sleep(uint millis);
a61af66fc99e Initial load
duke
parents:
diff changeset
431
a61af66fc99e Initial load
duke
parents:
diff changeset
432 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
433
a61af66fc99e Initial load
duke
parents:
diff changeset
434 // "n_threads" is the number of threads to be terminated. "queue_set" is a
a61af66fc99e Initial load
duke
parents:
diff changeset
435 // queue sets of work queues of other threads.
a61af66fc99e Initial load
duke
parents:
diff changeset
436 ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set);
a61af66fc99e Initial load
duke
parents:
diff changeset
437
a61af66fc99e Initial load
duke
parents:
diff changeset
438 // The current thread has no work, and is ready to terminate if everyone
a61af66fc99e Initial load
duke
parents:
diff changeset
439 // else is. If returns "true", all threads are terminated. If returns
a61af66fc99e Initial load
duke
parents:
diff changeset
440 // "false", available work has been observed in one of the task queues,
a61af66fc99e Initial load
duke
parents:
diff changeset
441 // so the global task is not complete.
342
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
442 bool offer_termination() {
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
443 return offer_termination(NULL);
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
444 }
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
445
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
446 // As above, but it also terminates of the should_exit_termination()
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
447 // method of the terminator parameter returns true. If terminator is
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
448 // NULL, then it is ignored.
37f87013dfd8 6711316: Open source the Garbage-First garbage collector
ysr
parents: 113
diff changeset
449 bool offer_termination(TerminatorTerminator* terminator);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
450
a61af66fc99e Initial load
duke
parents:
diff changeset
451 // Reset the terminator, so that it may be reused again.
a61af66fc99e Initial load
duke
parents:
diff changeset
452 // The caller is responsible for ensuring that this is done
a61af66fc99e Initial load
duke
parents:
diff changeset
453 // in an MT-safe manner, once the previous round of use of
a61af66fc99e Initial load
duke
parents:
diff changeset
454 // the terminator is finished.
a61af66fc99e Initial load
duke
parents:
diff changeset
455 void reset_for_reuse();
a61af66fc99e Initial load
duke
parents:
diff changeset
456
a61af66fc99e Initial load
duke
parents:
diff changeset
457 };
a61af66fc99e Initial load
duke
parents:
diff changeset
458
a61af66fc99e Initial load
duke
parents:
diff changeset
459 #define SIMPLE_STACK 0
a61af66fc99e Initial load
duke
parents:
diff changeset
460
a61af66fc99e Initial load
duke
parents:
diff changeset
461 template<class E> inline bool GenericTaskQueue<E>::push(E t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
462 #if SIMPLE_STACK
a61af66fc99e Initial load
duke
parents:
diff changeset
463 juint localBot = _bottom;
a61af66fc99e Initial load
duke
parents:
diff changeset
464 if (_bottom < max_elems()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
465 _elems[localBot] = t;
a61af66fc99e Initial load
duke
parents:
diff changeset
466 _bottom = localBot + 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
467 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
468 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
469 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
470 }
a61af66fc99e Initial load
duke
parents:
diff changeset
471 #else
a61af66fc99e Initial load
duke
parents:
diff changeset
472 juint localBot = _bottom;
a61af66fc99e Initial load
duke
parents:
diff changeset
473 assert((localBot >= 0) && (localBot < n()), "_bottom out of range.");
a61af66fc99e Initial load
duke
parents:
diff changeset
474 jushort top = get_top();
a61af66fc99e Initial load
duke
parents:
diff changeset
475 juint dirty_n_elems = dirty_size(localBot, top);
a61af66fc99e Initial load
duke
parents:
diff changeset
476 assert((dirty_n_elems >= 0) && (dirty_n_elems < n()),
a61af66fc99e Initial load
duke
parents:
diff changeset
477 "n_elems out of range.");
a61af66fc99e Initial load
duke
parents:
diff changeset
478 if (dirty_n_elems < max_elems()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
479 _elems[localBot] = t;
a61af66fc99e Initial load
duke
parents:
diff changeset
480 _bottom = increment_index(localBot);
a61af66fc99e Initial load
duke
parents:
diff changeset
481 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
482 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
483 return push_slow(t, dirty_n_elems);
a61af66fc99e Initial load
duke
parents:
diff changeset
484 }
a61af66fc99e Initial load
duke
parents:
diff changeset
485 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
486 }
a61af66fc99e Initial load
duke
parents:
diff changeset
487
a61af66fc99e Initial load
duke
parents:
diff changeset
488 template<class E> inline bool GenericTaskQueue<E>::pop_local(E& t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
489 #if SIMPLE_STACK
a61af66fc99e Initial load
duke
parents:
diff changeset
490 juint localBot = _bottom;
a61af66fc99e Initial load
duke
parents:
diff changeset
491 assert(localBot > 0, "precondition.");
a61af66fc99e Initial load
duke
parents:
diff changeset
492 localBot--;
a61af66fc99e Initial load
duke
parents:
diff changeset
493 t = _elems[localBot];
a61af66fc99e Initial load
duke
parents:
diff changeset
494 _bottom = localBot;
a61af66fc99e Initial load
duke
parents:
diff changeset
495 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
496 #else
a61af66fc99e Initial load
duke
parents:
diff changeset
497 juint localBot = _bottom;
a61af66fc99e Initial load
duke
parents:
diff changeset
498 // This value cannot be n-1. That can only occur as a result of
a61af66fc99e Initial load
duke
parents:
diff changeset
499 // the assignment to bottom in this method. If it does, this method
a61af66fc99e Initial load
duke
parents:
diff changeset
500 // resets the size( to 0 before the next call (which is sequential,
a61af66fc99e Initial load
duke
parents:
diff changeset
501 // since this is pop_local.)
a61af66fc99e Initial load
duke
parents:
diff changeset
502 juint dirty_n_elems = dirty_size(localBot, get_top());
a61af66fc99e Initial load
duke
parents:
diff changeset
503 assert(dirty_n_elems != n() - 1, "Shouldn't be possible...");
a61af66fc99e Initial load
duke
parents:
diff changeset
504 if (dirty_n_elems == 0) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
505 localBot = decrement_index(localBot);
a61af66fc99e Initial load
duke
parents:
diff changeset
506 _bottom = localBot;
a61af66fc99e Initial load
duke
parents:
diff changeset
507 // This is necessary to prevent any read below from being reordered
a61af66fc99e Initial load
duke
parents:
diff changeset
508 // before the store just above.
a61af66fc99e Initial load
duke
parents:
diff changeset
509 OrderAccess::fence();
a61af66fc99e Initial load
duke
parents:
diff changeset
510 t = _elems[localBot];
a61af66fc99e Initial load
duke
parents:
diff changeset
511 // This is a second read of "age"; the "size()" above is the first.
a61af66fc99e Initial load
duke
parents:
diff changeset
512 // If there's still at least one element in the queue, based on the
a61af66fc99e Initial load
duke
parents:
diff changeset
513 // "_bottom" and "age" we've read, then there can be no interference with
a61af66fc99e Initial load
duke
parents:
diff changeset
514 // a "pop_global" operation, and we're done.
a61af66fc99e Initial load
duke
parents:
diff changeset
515 juint tp = get_top();
a61af66fc99e Initial load
duke
parents:
diff changeset
516 if (size(localBot, tp) > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
517 assert(dirty_size(localBot, tp) != n() - 1,
a61af66fc99e Initial load
duke
parents:
diff changeset
518 "Shouldn't be possible...");
a61af66fc99e Initial load
duke
parents:
diff changeset
519 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
520 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
521 // Otherwise, the queue contained exactly one element; we take the slow
a61af66fc99e Initial load
duke
parents:
diff changeset
522 // path.
a61af66fc99e Initial load
duke
parents:
diff changeset
523 return pop_local_slow(localBot, get_age());
a61af66fc99e Initial load
duke
parents:
diff changeset
524 }
a61af66fc99e Initial load
duke
parents:
diff changeset
525 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
526 }
a61af66fc99e Initial load
duke
parents:
diff changeset
527
a61af66fc99e Initial load
duke
parents:
diff changeset
528 typedef oop Task;
a61af66fc99e Initial load
duke
parents:
diff changeset
529 typedef GenericTaskQueue<Task> OopTaskQueue;
a61af66fc99e Initial load
duke
parents:
diff changeset
530 typedef GenericTaskQueueSet<Task> OopTaskQueueSet;
a61af66fc99e Initial load
duke
parents:
diff changeset
531
113
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
532
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
533 #define COMPRESSED_OOP_MASK 1
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
534
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
535 // This is a container class for either an oop* or a narrowOop*.
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
536 // Both are pushed onto a task queue and the consumer will test is_narrow()
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
537 // to determine which should be processed.
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
538 class StarTask {
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
539 void* _holder; // either union oop* or narrowOop*
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
540 public:
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
541 StarTask(narrowOop *p) { _holder = (void *)((uintptr_t)p | COMPRESSED_OOP_MASK); }
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
542 StarTask(oop *p) { _holder = (void*)p; }
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
543 StarTask() { _holder = NULL; }
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
544 operator oop*() { return (oop*)_holder; }
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
545 operator narrowOop*() {
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
546 return (narrowOop*)((uintptr_t)_holder & ~COMPRESSED_OOP_MASK);
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
547 }
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
548
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
549 // Operators to preserve const/volatile in assignments required by gcc
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
550 void operator=(const volatile StarTask& t) volatile { _holder = t._holder; }
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
551
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
552 bool is_narrow() const {
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
553 return (((uintptr_t)_holder & COMPRESSED_OOP_MASK) != 0);
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
554 }
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
555 };
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
556
0
a61af66fc99e Initial load
duke
parents:
diff changeset
557 typedef GenericTaskQueue<StarTask> OopStarTaskQueue;
a61af66fc99e Initial load
duke
parents:
diff changeset
558 typedef GenericTaskQueueSet<StarTask> OopStarTaskQueueSet;
a61af66fc99e Initial load
duke
parents:
diff changeset
559
375
81cd571500b0 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 356
diff changeset
560 typedef size_t RegionTask; // index for region
81cd571500b0 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 356
diff changeset
561 typedef GenericTaskQueue<RegionTask> RegionTaskQueue;
81cd571500b0 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 356
diff changeset
562 typedef GenericTaskQueueSet<RegionTask> RegionTaskQueueSet;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
563
375
81cd571500b0 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 356
diff changeset
564 class RegionTaskQueueWithOverflow: public CHeapObj {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
565 protected:
375
81cd571500b0 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 356
diff changeset
566 RegionTaskQueue _region_queue;
81cd571500b0 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 356
diff changeset
567 GrowableArray<RegionTask>* _overflow_stack;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
568
a61af66fc99e Initial load
duke
parents:
diff changeset
569 public:
375
81cd571500b0 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 356
diff changeset
570 RegionTaskQueueWithOverflow() : _overflow_stack(NULL) {}
0
a61af66fc99e Initial load
duke
parents:
diff changeset
571 // Initialize both stealable queue and overflow
a61af66fc99e Initial load
duke
parents:
diff changeset
572 void initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
573 // Save first to stealable queue and then to overflow
375
81cd571500b0 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 356
diff changeset
574 void save(RegionTask t);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
575 // Retrieve first from overflow and then from stealable queue
375
81cd571500b0 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 356
diff changeset
576 bool retrieve(RegionTask& region_index);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
577 // Retrieve from stealable queue
375
81cd571500b0 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 356
diff changeset
578 bool retrieve_from_stealable_queue(RegionTask& region_index);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
579 // Retrieve from overflow
375
81cd571500b0 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 356
diff changeset
580 bool retrieve_from_overflow(RegionTask& region_index);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
581 bool is_empty();
a61af66fc99e Initial load
duke
parents:
diff changeset
582 bool stealable_is_empty();
a61af66fc99e Initial load
duke
parents:
diff changeset
583 bool overflow_is_empty();
375
81cd571500b0 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 356
diff changeset
584 juint stealable_size() { return _region_queue.size(); }
81cd571500b0 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 356
diff changeset
585 RegionTaskQueue* task_queue() { return &_region_queue; }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
586 };
a61af66fc99e Initial load
duke
parents:
diff changeset
587
375
81cd571500b0 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 356
diff changeset
588 #define USE_RegionTaskQueueWithOverflow