annotate src/share/vm/utilities/taskqueue.hpp @ 196:d1605aabd0a1 jdk7-b30

6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
author xdono
date Wed, 02 Jul 2008 12:55:16 -0700
parents ba764ed4b6f2
children 1ee8caae33af
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
a61af66fc99e Initial load
duke
parents:
diff changeset
123 // Maximum number of elements allowed in the queue. This is two less
a61af66fc99e Initial load
duke
parents:
diff changeset
124 // than the actual queue size, for somewhat complicated reasons.
a61af66fc99e Initial load
duke
parents:
diff changeset
125 juint max_elems() { return n() - 2; }
a61af66fc99e Initial load
duke
parents:
diff changeset
126
a61af66fc99e Initial load
duke
parents:
diff changeset
127 };
a61af66fc99e Initial load
duke
parents:
diff changeset
128
a61af66fc99e Initial load
duke
parents:
diff changeset
129 template<class E> class GenericTaskQueue: public TaskQueueSuper {
a61af66fc99e Initial load
duke
parents:
diff changeset
130 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
131 // Slow paths for push, pop_local. (pop_global has no fast path.)
a61af66fc99e Initial load
duke
parents:
diff changeset
132 bool push_slow(E t, juint dirty_n_elems);
a61af66fc99e Initial load
duke
parents:
diff changeset
133 bool pop_local_slow(juint localBot, Age oldAge);
a61af66fc99e Initial load
duke
parents:
diff changeset
134
a61af66fc99e Initial load
duke
parents:
diff changeset
135 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
136 // Initializes the queue to empty.
a61af66fc99e Initial load
duke
parents:
diff changeset
137 GenericTaskQueue();
a61af66fc99e Initial load
duke
parents:
diff changeset
138
a61af66fc99e Initial load
duke
parents:
diff changeset
139 void initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 // Push the task "t" on the queue. Returns "false" iff the queue is
a61af66fc99e Initial load
duke
parents:
diff changeset
142 // full.
a61af66fc99e Initial load
duke
parents:
diff changeset
143 inline bool push(E t);
a61af66fc99e Initial load
duke
parents:
diff changeset
144
a61af66fc99e Initial load
duke
parents:
diff changeset
145 // If succeeds in claiming a task (from the 'local' end, that is, the
a61af66fc99e Initial load
duke
parents:
diff changeset
146 // most recently pushed task), returns "true" and sets "t" to that task.
a61af66fc99e Initial load
duke
parents:
diff changeset
147 // Otherwise, the queue is empty and returns false.
a61af66fc99e Initial load
duke
parents:
diff changeset
148 inline bool pop_local(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 'global' end, that is, the
a61af66fc99e Initial load
duke
parents:
diff changeset
151 // least 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 bool pop_global(E& t);
a61af66fc99e Initial load
duke
parents:
diff changeset
154
a61af66fc99e Initial load
duke
parents:
diff changeset
155 // Delete any resource associated with the queue.
a61af66fc99e Initial load
duke
parents:
diff changeset
156 ~GenericTaskQueue();
a61af66fc99e Initial load
duke
parents:
diff changeset
157
a61af66fc99e Initial load
duke
parents:
diff changeset
158 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
159 // Element array.
a61af66fc99e Initial load
duke
parents:
diff changeset
160 volatile E* _elems;
a61af66fc99e Initial load
duke
parents:
diff changeset
161 };
a61af66fc99e Initial load
duke
parents:
diff changeset
162
a61af66fc99e Initial load
duke
parents:
diff changeset
163 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
164 GenericTaskQueue<E>::GenericTaskQueue():TaskQueueSuper() {
a61af66fc99e Initial load
duke
parents:
diff changeset
165 assert(sizeof(Age) == sizeof(jint), "Depends on this.");
a61af66fc99e Initial load
duke
parents:
diff changeset
166 }
a61af66fc99e Initial load
duke
parents:
diff changeset
167
a61af66fc99e Initial load
duke
parents:
diff changeset
168 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
169 void GenericTaskQueue<E>::initialize() {
a61af66fc99e Initial load
duke
parents:
diff changeset
170 _elems = NEW_C_HEAP_ARRAY(E, n());
a61af66fc99e Initial load
duke
parents:
diff changeset
171 guarantee(_elems != NULL, "Allocation failed.");
a61af66fc99e Initial load
duke
parents:
diff changeset
172 }
a61af66fc99e Initial load
duke
parents:
diff changeset
173
a61af66fc99e Initial load
duke
parents:
diff changeset
174 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
175 bool GenericTaskQueue<E>::push_slow(E t, juint dirty_n_elems) {
a61af66fc99e Initial load
duke
parents:
diff changeset
176 if (dirty_n_elems == n() - 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
177 // Actually means 0, so do the push.
a61af66fc99e Initial load
duke
parents:
diff changeset
178 juint localBot = _bottom;
a61af66fc99e Initial load
duke
parents:
diff changeset
179 _elems[localBot] = t;
a61af66fc99e Initial load
duke
parents:
diff changeset
180 _bottom = increment_index(localBot);
a61af66fc99e Initial load
duke
parents:
diff changeset
181 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
182 } else
a61af66fc99e Initial load
duke
parents:
diff changeset
183 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
184 }
a61af66fc99e Initial load
duke
parents:
diff changeset
185
a61af66fc99e Initial load
duke
parents:
diff changeset
186 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
187 bool GenericTaskQueue<E>::
a61af66fc99e Initial load
duke
parents:
diff changeset
188 pop_local_slow(juint localBot, Age oldAge) {
a61af66fc99e Initial load
duke
parents:
diff changeset
189 // This queue was observed to contain exactly one element; either this
a61af66fc99e Initial load
duke
parents:
diff changeset
190 // thread will claim it, or a competing "pop_global". In either case,
a61af66fc99e Initial load
duke
parents:
diff changeset
191 // the queue will be logically empty afterwards. Create a new Age value
a61af66fc99e Initial load
duke
parents:
diff changeset
192 // that represents the empty queue for the given value of "_bottom". (We
a61af66fc99e Initial load
duke
parents:
diff changeset
193 // must also increment "tag" because of the case where "bottom == 1",
a61af66fc99e Initial load
duke
parents:
diff changeset
194 // "top == 0". A pop_global could read the queue element in that case,
a61af66fc99e Initial load
duke
parents:
diff changeset
195 // then have the owner thread do a pop followed by another push. Without
a61af66fc99e Initial load
duke
parents:
diff changeset
196 // the incrementing of "tag", the pop_global's CAS could succeed,
a61af66fc99e Initial load
duke
parents:
diff changeset
197 // allowing it to believe it has claimed the stale element.)
a61af66fc99e Initial load
duke
parents:
diff changeset
198 Age newAge;
a61af66fc99e Initial load
duke
parents:
diff changeset
199 newAge._top = localBot;
a61af66fc99e Initial load
duke
parents:
diff changeset
200 newAge._tag = oldAge.tag() + 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
201 // Perhaps a competing pop_global has already incremented "top", in which
a61af66fc99e Initial load
duke
parents:
diff changeset
202 // case it wins the element.
a61af66fc99e Initial load
duke
parents:
diff changeset
203 if (localBot == oldAge.top()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
204 Age tempAge;
a61af66fc99e Initial load
duke
parents:
diff changeset
205 // No competing pop_global has yet incremented "top"; we'll try to
a61af66fc99e Initial load
duke
parents:
diff changeset
206 // install new_age, thus claiming the element.
a61af66fc99e Initial load
duke
parents:
diff changeset
207 assert(sizeof(Age) == sizeof(jint) && sizeof(jint) == sizeof(juint),
a61af66fc99e Initial load
duke
parents:
diff changeset
208 "Assumption about CAS unit.");
a61af66fc99e Initial load
duke
parents:
diff changeset
209 *(jint*)&tempAge = Atomic::cmpxchg(*(jint*)&newAge, (volatile jint*)&_age, *(jint*)&oldAge);
a61af66fc99e Initial load
duke
parents:
diff changeset
210 if (tempAge == oldAge) {
a61af66fc99e Initial load
duke
parents:
diff changeset
211 // We win.
a61af66fc99e Initial load
duke
parents:
diff changeset
212 assert(dirty_size(localBot, get_top()) != n() - 1,
a61af66fc99e Initial load
duke
parents:
diff changeset
213 "Shouldn't be possible...");
a61af66fc99e Initial load
duke
parents:
diff changeset
214 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
215 }
a61af66fc99e Initial load
duke
parents:
diff changeset
216 }
a61af66fc99e Initial load
duke
parents:
diff changeset
217 // We fail; a completing pop_global gets the element. But the queue is
a61af66fc99e Initial load
duke
parents:
diff changeset
218 // empty (and top is greater than bottom.) Fix this representation of
a61af66fc99e Initial load
duke
parents:
diff changeset
219 // the empty queue to become the canonical one.
a61af66fc99e Initial load
duke
parents:
diff changeset
220 set_age(newAge);
a61af66fc99e Initial load
duke
parents:
diff changeset
221 assert(dirty_size(localBot, get_top()) != n() - 1,
a61af66fc99e Initial load
duke
parents:
diff changeset
222 "Shouldn't be possible...");
a61af66fc99e Initial load
duke
parents:
diff changeset
223 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
224 }
a61af66fc99e Initial load
duke
parents:
diff changeset
225
a61af66fc99e Initial load
duke
parents:
diff changeset
226 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
227 bool GenericTaskQueue<E>::pop_global(E& t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
228 Age newAge;
a61af66fc99e Initial load
duke
parents:
diff changeset
229 Age oldAge = get_age();
a61af66fc99e Initial load
duke
parents:
diff changeset
230 juint localBot = _bottom;
a61af66fc99e Initial load
duke
parents:
diff changeset
231 juint n_elems = size(localBot, oldAge.top());
a61af66fc99e Initial load
duke
parents:
diff changeset
232 if (n_elems == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
233 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
234 }
a61af66fc99e Initial load
duke
parents:
diff changeset
235 t = _elems[oldAge.top()];
a61af66fc99e Initial load
duke
parents:
diff changeset
236 newAge = oldAge;
a61af66fc99e Initial load
duke
parents:
diff changeset
237 newAge._top = increment_index(newAge.top());
a61af66fc99e Initial load
duke
parents:
diff changeset
238 if ( newAge._top == 0 ) newAge._tag++;
a61af66fc99e Initial load
duke
parents:
diff changeset
239 Age resAge;
a61af66fc99e Initial load
duke
parents:
diff changeset
240 *(jint*)&resAge = Atomic::cmpxchg(*(jint*)&newAge, (volatile jint*)&_age, *(jint*)&oldAge);
a61af66fc99e Initial load
duke
parents:
diff changeset
241 // Note that using "_bottom" here might fail, since a pop_local might
a61af66fc99e Initial load
duke
parents:
diff changeset
242 // have decremented it.
a61af66fc99e Initial load
duke
parents:
diff changeset
243 assert(dirty_size(localBot, newAge._top) != n() - 1,
a61af66fc99e Initial load
duke
parents:
diff changeset
244 "Shouldn't be possible...");
a61af66fc99e Initial load
duke
parents:
diff changeset
245 return (resAge == oldAge);
a61af66fc99e Initial load
duke
parents:
diff changeset
246 }
a61af66fc99e Initial load
duke
parents:
diff changeset
247
a61af66fc99e Initial load
duke
parents:
diff changeset
248 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
249 GenericTaskQueue<E>::~GenericTaskQueue() {
a61af66fc99e Initial load
duke
parents:
diff changeset
250 FREE_C_HEAP_ARRAY(E, _elems);
a61af66fc99e Initial load
duke
parents:
diff changeset
251 }
a61af66fc99e Initial load
duke
parents:
diff changeset
252
a61af66fc99e Initial load
duke
parents:
diff changeset
253 // Inherits the typedef of "Task" from above.
a61af66fc99e Initial load
duke
parents:
diff changeset
254 class TaskQueueSetSuper: public CHeapObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
255 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
256 static int randomParkAndMiller(int* seed0);
a61af66fc99e Initial load
duke
parents:
diff changeset
257 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
258 // Returns "true" if some TaskQueue in the set contains a task.
a61af66fc99e Initial load
duke
parents:
diff changeset
259 virtual bool peek() = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
260 };
a61af66fc99e Initial load
duke
parents:
diff changeset
261
a61af66fc99e Initial load
duke
parents:
diff changeset
262 template<class E> class GenericTaskQueueSet: public TaskQueueSetSuper {
a61af66fc99e Initial load
duke
parents:
diff changeset
263 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
264 int _n;
a61af66fc99e Initial load
duke
parents:
diff changeset
265 GenericTaskQueue<E>** _queues;
a61af66fc99e Initial load
duke
parents:
diff changeset
266
a61af66fc99e Initial load
duke
parents:
diff changeset
267 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
268 GenericTaskQueueSet(int n) : _n(n) {
a61af66fc99e Initial load
duke
parents:
diff changeset
269 typedef GenericTaskQueue<E>* GenericTaskQueuePtr;
a61af66fc99e Initial load
duke
parents:
diff changeset
270 _queues = NEW_C_HEAP_ARRAY(GenericTaskQueuePtr, n);
a61af66fc99e Initial load
duke
parents:
diff changeset
271 guarantee(_queues != NULL, "Allocation failure.");
a61af66fc99e Initial load
duke
parents:
diff changeset
272 for (int i = 0; i < n; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
273 _queues[i] = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
274 }
a61af66fc99e Initial load
duke
parents:
diff changeset
275 }
a61af66fc99e Initial load
duke
parents:
diff changeset
276
a61af66fc99e Initial load
duke
parents:
diff changeset
277 bool steal_1_random(int queue_num, int* seed, E& t);
a61af66fc99e Initial load
duke
parents:
diff changeset
278 bool steal_best_of_2(int queue_num, int* seed, E& t);
a61af66fc99e Initial load
duke
parents:
diff changeset
279 bool steal_best_of_all(int queue_num, int* seed, E& t);
a61af66fc99e Initial load
duke
parents:
diff changeset
280
a61af66fc99e Initial load
duke
parents:
diff changeset
281 void register_queue(int i, GenericTaskQueue<E>* q);
a61af66fc99e Initial load
duke
parents:
diff changeset
282
a61af66fc99e Initial load
duke
parents:
diff changeset
283 GenericTaskQueue<E>* queue(int n);
a61af66fc99e Initial load
duke
parents:
diff changeset
284
a61af66fc99e Initial load
duke
parents:
diff changeset
285 // The thread with queue number "queue_num" (and whose random number seed
a61af66fc99e Initial load
duke
parents:
diff changeset
286 // is at "seed") is trying to steal a task from some other queue. (It
a61af66fc99e Initial load
duke
parents:
diff changeset
287 // may try several queues, according to some configuration parameter.)
a61af66fc99e Initial load
duke
parents:
diff changeset
288 // If some steal succeeds, returns "true" and sets "t" the stolen task,
a61af66fc99e Initial load
duke
parents:
diff changeset
289 // otherwise returns false.
a61af66fc99e Initial load
duke
parents:
diff changeset
290 bool steal(int queue_num, int* seed, E& t);
a61af66fc99e Initial load
duke
parents:
diff changeset
291
a61af66fc99e Initial load
duke
parents:
diff changeset
292 bool peek();
a61af66fc99e Initial load
duke
parents:
diff changeset
293 };
a61af66fc99e Initial load
duke
parents:
diff changeset
294
a61af66fc99e Initial load
duke
parents:
diff changeset
295 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
296 void GenericTaskQueueSet<E>::register_queue(int i, GenericTaskQueue<E>* q) {
a61af66fc99e Initial load
duke
parents:
diff changeset
297 assert(0 <= i && i < _n, "index out of range.");
a61af66fc99e Initial load
duke
parents:
diff changeset
298 _queues[i] = q;
a61af66fc99e Initial load
duke
parents:
diff changeset
299 }
a61af66fc99e Initial load
duke
parents:
diff changeset
300
a61af66fc99e Initial load
duke
parents:
diff changeset
301 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
302 GenericTaskQueue<E>* GenericTaskQueueSet<E>::queue(int i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
303 return _queues[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
304 }
a61af66fc99e Initial load
duke
parents:
diff changeset
305
a61af66fc99e Initial load
duke
parents:
diff changeset
306 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
307 bool GenericTaskQueueSet<E>::steal(int queue_num, int* seed, E& t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
308 for (int i = 0; i < 2 * _n; i++)
a61af66fc99e Initial load
duke
parents:
diff changeset
309 if (steal_best_of_2(queue_num, seed, t))
a61af66fc99e Initial load
duke
parents:
diff changeset
310 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
311 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
312 }
a61af66fc99e Initial load
duke
parents:
diff changeset
313
a61af66fc99e Initial load
duke
parents:
diff changeset
314 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
315 bool GenericTaskQueueSet<E>::steal_best_of_all(int queue_num, int* seed, E& t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
316 if (_n > 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
317 int best_k;
a61af66fc99e Initial load
duke
parents:
diff changeset
318 jint best_sz = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
319 for (int k = 0; k < _n; k++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
320 if (k == queue_num) continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
321 jint sz = _queues[k]->size();
a61af66fc99e Initial load
duke
parents:
diff changeset
322 if (sz > best_sz) {
a61af66fc99e Initial load
duke
parents:
diff changeset
323 best_sz = sz;
a61af66fc99e Initial load
duke
parents:
diff changeset
324 best_k = k;
a61af66fc99e Initial load
duke
parents:
diff changeset
325 }
a61af66fc99e Initial load
duke
parents:
diff changeset
326 }
a61af66fc99e Initial load
duke
parents:
diff changeset
327 return best_sz > 0 && _queues[best_k]->pop_global(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
328 } else if (_n == 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
329 // Just try the other one.
a61af66fc99e Initial load
duke
parents:
diff changeset
330 int k = (queue_num + 1) % 2;
a61af66fc99e Initial load
duke
parents:
diff changeset
331 return _queues[k]->pop_global(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
332 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
333 assert(_n == 1, "can't be zero.");
a61af66fc99e Initial load
duke
parents:
diff changeset
334 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
335 }
a61af66fc99e Initial load
duke
parents:
diff changeset
336 }
a61af66fc99e Initial load
duke
parents:
diff changeset
337
a61af66fc99e Initial load
duke
parents:
diff changeset
338 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
339 bool GenericTaskQueueSet<E>::steal_1_random(int queue_num, int* seed, E& t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
340 if (_n > 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
341 int k = queue_num;
a61af66fc99e Initial load
duke
parents:
diff changeset
342 while (k == queue_num) k = randomParkAndMiller(seed) % _n;
a61af66fc99e Initial load
duke
parents:
diff changeset
343 return _queues[2]->pop_global(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
344 } else if (_n == 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
345 // Just try the other one.
a61af66fc99e Initial load
duke
parents:
diff changeset
346 int k = (queue_num + 1) % 2;
a61af66fc99e Initial load
duke
parents:
diff changeset
347 return _queues[k]->pop_global(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
348 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
349 assert(_n == 1, "can't be zero.");
a61af66fc99e Initial load
duke
parents:
diff changeset
350 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
351 }
a61af66fc99e Initial load
duke
parents:
diff changeset
352 }
a61af66fc99e Initial load
duke
parents:
diff changeset
353
a61af66fc99e Initial load
duke
parents:
diff changeset
354 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
355 bool GenericTaskQueueSet<E>::steal_best_of_2(int queue_num, int* seed, E& t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
356 if (_n > 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
357 int k1 = queue_num;
a61af66fc99e Initial load
duke
parents:
diff changeset
358 while (k1 == queue_num) k1 = randomParkAndMiller(seed) % _n;
a61af66fc99e Initial load
duke
parents:
diff changeset
359 int k2 = queue_num;
a61af66fc99e Initial load
duke
parents:
diff changeset
360 while (k2 == queue_num || k2 == k1) k2 = randomParkAndMiller(seed) % _n;
a61af66fc99e Initial load
duke
parents:
diff changeset
361 // Sample both and try the larger.
a61af66fc99e Initial load
duke
parents:
diff changeset
362 juint sz1 = _queues[k1]->size();
a61af66fc99e Initial load
duke
parents:
diff changeset
363 juint sz2 = _queues[k2]->size();
a61af66fc99e Initial load
duke
parents:
diff changeset
364 if (sz2 > sz1) return _queues[k2]->pop_global(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
365 else return _queues[k1]->pop_global(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
366 } else if (_n == 2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
367 // Just try the other one.
a61af66fc99e Initial load
duke
parents:
diff changeset
368 int k = (queue_num + 1) % 2;
a61af66fc99e Initial load
duke
parents:
diff changeset
369 return _queues[k]->pop_global(t);
a61af66fc99e Initial load
duke
parents:
diff changeset
370 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
371 assert(_n == 1, "can't be zero.");
a61af66fc99e Initial load
duke
parents:
diff changeset
372 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
373 }
a61af66fc99e Initial load
duke
parents:
diff changeset
374 }
a61af66fc99e Initial load
duke
parents:
diff changeset
375
a61af66fc99e Initial load
duke
parents:
diff changeset
376 template<class E>
a61af66fc99e Initial load
duke
parents:
diff changeset
377 bool GenericTaskQueueSet<E>::peek() {
a61af66fc99e Initial load
duke
parents:
diff changeset
378 // Try all the queues.
a61af66fc99e Initial load
duke
parents:
diff changeset
379 for (int j = 0; j < _n; j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
380 if (_queues[j]->peek())
a61af66fc99e Initial load
duke
parents:
diff changeset
381 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
382 }
a61af66fc99e Initial load
duke
parents:
diff changeset
383 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
384 }
a61af66fc99e Initial load
duke
parents:
diff changeset
385
a61af66fc99e Initial load
duke
parents:
diff changeset
386 // A class to aid in the termination of a set of parallel tasks using
a61af66fc99e Initial load
duke
parents:
diff changeset
387 // TaskQueueSet's for work stealing.
a61af66fc99e Initial load
duke
parents:
diff changeset
388
a61af66fc99e Initial load
duke
parents:
diff changeset
389 class ParallelTaskTerminator: public StackObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
390 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
391 int _n_threads;
a61af66fc99e Initial load
duke
parents:
diff changeset
392 TaskQueueSetSuper* _queue_set;
a61af66fc99e Initial load
duke
parents:
diff changeset
393 jint _offered_termination;
a61af66fc99e Initial load
duke
parents:
diff changeset
394
a61af66fc99e Initial load
duke
parents:
diff changeset
395 bool peek_in_queue_set();
a61af66fc99e Initial load
duke
parents:
diff changeset
396 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
397 virtual void yield();
a61af66fc99e Initial load
duke
parents:
diff changeset
398 void sleep(uint millis);
a61af66fc99e Initial load
duke
parents:
diff changeset
399
a61af66fc99e Initial load
duke
parents:
diff changeset
400 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
401
a61af66fc99e Initial load
duke
parents:
diff changeset
402 // "n_threads" is the number of threads to be terminated. "queue_set" is a
a61af66fc99e Initial load
duke
parents:
diff changeset
403 // queue sets of work queues of other threads.
a61af66fc99e Initial load
duke
parents:
diff changeset
404 ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set);
a61af66fc99e Initial load
duke
parents:
diff changeset
405
a61af66fc99e Initial load
duke
parents:
diff changeset
406 // The current thread has no work, and is ready to terminate if everyone
a61af66fc99e Initial load
duke
parents:
diff changeset
407 // else is. If returns "true", all threads are terminated. If returns
a61af66fc99e Initial load
duke
parents:
diff changeset
408 // "false", available work has been observed in one of the task queues,
a61af66fc99e Initial load
duke
parents:
diff changeset
409 // so the global task is not complete.
a61af66fc99e Initial load
duke
parents:
diff changeset
410 bool offer_termination();
a61af66fc99e Initial load
duke
parents:
diff changeset
411
a61af66fc99e Initial load
duke
parents:
diff changeset
412 // Reset the terminator, so that it may be reused again.
a61af66fc99e Initial load
duke
parents:
diff changeset
413 // The caller is responsible for ensuring that this is done
a61af66fc99e Initial load
duke
parents:
diff changeset
414 // in an MT-safe manner, once the previous round of use of
a61af66fc99e Initial load
duke
parents:
diff changeset
415 // the terminator is finished.
a61af66fc99e Initial load
duke
parents:
diff changeset
416 void reset_for_reuse();
a61af66fc99e Initial load
duke
parents:
diff changeset
417
a61af66fc99e Initial load
duke
parents:
diff changeset
418 };
a61af66fc99e Initial load
duke
parents:
diff changeset
419
a61af66fc99e Initial load
duke
parents:
diff changeset
420 #define SIMPLE_STACK 0
a61af66fc99e Initial load
duke
parents:
diff changeset
421
a61af66fc99e Initial load
duke
parents:
diff changeset
422 template<class E> inline bool GenericTaskQueue<E>::push(E t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
423 #if SIMPLE_STACK
a61af66fc99e Initial load
duke
parents:
diff changeset
424 juint localBot = _bottom;
a61af66fc99e Initial load
duke
parents:
diff changeset
425 if (_bottom < max_elems()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
426 _elems[localBot] = t;
a61af66fc99e Initial load
duke
parents:
diff changeset
427 _bottom = localBot + 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
428 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
429 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
430 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
431 }
a61af66fc99e Initial load
duke
parents:
diff changeset
432 #else
a61af66fc99e Initial load
duke
parents:
diff changeset
433 juint localBot = _bottom;
a61af66fc99e Initial load
duke
parents:
diff changeset
434 assert((localBot >= 0) && (localBot < n()), "_bottom out of range.");
a61af66fc99e Initial load
duke
parents:
diff changeset
435 jushort top = get_top();
a61af66fc99e Initial load
duke
parents:
diff changeset
436 juint dirty_n_elems = dirty_size(localBot, top);
a61af66fc99e Initial load
duke
parents:
diff changeset
437 assert((dirty_n_elems >= 0) && (dirty_n_elems < n()),
a61af66fc99e Initial load
duke
parents:
diff changeset
438 "n_elems out of range.");
a61af66fc99e Initial load
duke
parents:
diff changeset
439 if (dirty_n_elems < max_elems()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
440 _elems[localBot] = t;
a61af66fc99e Initial load
duke
parents:
diff changeset
441 _bottom = increment_index(localBot);
a61af66fc99e Initial load
duke
parents:
diff changeset
442 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
443 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
444 return push_slow(t, dirty_n_elems);
a61af66fc99e Initial load
duke
parents:
diff changeset
445 }
a61af66fc99e Initial load
duke
parents:
diff changeset
446 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
447 }
a61af66fc99e Initial load
duke
parents:
diff changeset
448
a61af66fc99e Initial load
duke
parents:
diff changeset
449 template<class E> inline bool GenericTaskQueue<E>::pop_local(E& t) {
a61af66fc99e Initial load
duke
parents:
diff changeset
450 #if SIMPLE_STACK
a61af66fc99e Initial load
duke
parents:
diff changeset
451 juint localBot = _bottom;
a61af66fc99e Initial load
duke
parents:
diff changeset
452 assert(localBot > 0, "precondition.");
a61af66fc99e Initial load
duke
parents:
diff changeset
453 localBot--;
a61af66fc99e Initial load
duke
parents:
diff changeset
454 t = _elems[localBot];
a61af66fc99e Initial load
duke
parents:
diff changeset
455 _bottom = localBot;
a61af66fc99e Initial load
duke
parents:
diff changeset
456 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
457 #else
a61af66fc99e Initial load
duke
parents:
diff changeset
458 juint localBot = _bottom;
a61af66fc99e Initial load
duke
parents:
diff changeset
459 // This value cannot be n-1. That can only occur as a result of
a61af66fc99e Initial load
duke
parents:
diff changeset
460 // the assignment to bottom in this method. If it does, this method
a61af66fc99e Initial load
duke
parents:
diff changeset
461 // resets the size( to 0 before the next call (which is sequential,
a61af66fc99e Initial load
duke
parents:
diff changeset
462 // since this is pop_local.)
a61af66fc99e Initial load
duke
parents:
diff changeset
463 juint dirty_n_elems = dirty_size(localBot, get_top());
a61af66fc99e Initial load
duke
parents:
diff changeset
464 assert(dirty_n_elems != n() - 1, "Shouldn't be possible...");
a61af66fc99e Initial load
duke
parents:
diff changeset
465 if (dirty_n_elems == 0) return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
466 localBot = decrement_index(localBot);
a61af66fc99e Initial load
duke
parents:
diff changeset
467 _bottom = localBot;
a61af66fc99e Initial load
duke
parents:
diff changeset
468 // This is necessary to prevent any read below from being reordered
a61af66fc99e Initial load
duke
parents:
diff changeset
469 // before the store just above.
a61af66fc99e Initial load
duke
parents:
diff changeset
470 OrderAccess::fence();
a61af66fc99e Initial load
duke
parents:
diff changeset
471 t = _elems[localBot];
a61af66fc99e Initial load
duke
parents:
diff changeset
472 // This is a second read of "age"; the "size()" above is the first.
a61af66fc99e Initial load
duke
parents:
diff changeset
473 // If there's still at least one element in the queue, based on the
a61af66fc99e Initial load
duke
parents:
diff changeset
474 // "_bottom" and "age" we've read, then there can be no interference with
a61af66fc99e Initial load
duke
parents:
diff changeset
475 // a "pop_global" operation, and we're done.
a61af66fc99e Initial load
duke
parents:
diff changeset
476 juint tp = get_top();
a61af66fc99e Initial load
duke
parents:
diff changeset
477 if (size(localBot, tp) > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
478 assert(dirty_size(localBot, tp) != n() - 1,
a61af66fc99e Initial load
duke
parents:
diff changeset
479 "Shouldn't be possible...");
a61af66fc99e Initial load
duke
parents:
diff changeset
480 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
481 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
482 // Otherwise, the queue contained exactly one element; we take the slow
a61af66fc99e Initial load
duke
parents:
diff changeset
483 // path.
a61af66fc99e Initial load
duke
parents:
diff changeset
484 return pop_local_slow(localBot, get_age());
a61af66fc99e Initial load
duke
parents:
diff changeset
485 }
a61af66fc99e Initial load
duke
parents:
diff changeset
486 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
487 }
a61af66fc99e Initial load
duke
parents:
diff changeset
488
a61af66fc99e Initial load
duke
parents:
diff changeset
489 typedef oop Task;
a61af66fc99e Initial load
duke
parents:
diff changeset
490 typedef GenericTaskQueue<Task> OopTaskQueue;
a61af66fc99e Initial load
duke
parents:
diff changeset
491 typedef GenericTaskQueueSet<Task> OopTaskQueueSet;
a61af66fc99e Initial load
duke
parents:
diff changeset
492
113
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
493
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
494 #define COMPRESSED_OOP_MASK 1
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
495
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
496 // 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
497 // 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
498 // 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
499 class StarTask {
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
500 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
501 public:
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
502 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
503 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
504 StarTask() { _holder = NULL; }
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
505 operator oop*() { return (oop*)_holder; }
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
506 operator narrowOop*() {
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
507 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
508 }
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
509
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
510 // 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
511 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
512
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
513 bool is_narrow() const {
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
514 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
515 }
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
516 };
ba764ed4b6f2 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 0
diff changeset
517
0
a61af66fc99e Initial load
duke
parents:
diff changeset
518 typedef GenericTaskQueue<StarTask> OopStarTaskQueue;
a61af66fc99e Initial load
duke
parents:
diff changeset
519 typedef GenericTaskQueueSet<StarTask> OopStarTaskQueueSet;
a61af66fc99e Initial load
duke
parents:
diff changeset
520
a61af66fc99e Initial load
duke
parents:
diff changeset
521 typedef size_t ChunkTask; // index for chunk
a61af66fc99e Initial load
duke
parents:
diff changeset
522 typedef GenericTaskQueue<ChunkTask> ChunkTaskQueue;
a61af66fc99e Initial load
duke
parents:
diff changeset
523 typedef GenericTaskQueueSet<ChunkTask> ChunkTaskQueueSet;
a61af66fc99e Initial load
duke
parents:
diff changeset
524
a61af66fc99e Initial load
duke
parents:
diff changeset
525 class ChunkTaskQueueWithOverflow: public CHeapObj {
a61af66fc99e Initial load
duke
parents:
diff changeset
526 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
527 ChunkTaskQueue _chunk_queue;
a61af66fc99e Initial load
duke
parents:
diff changeset
528 GrowableArray<ChunkTask>* _overflow_stack;
a61af66fc99e Initial load
duke
parents:
diff changeset
529
a61af66fc99e Initial load
duke
parents:
diff changeset
530 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
531 ChunkTaskQueueWithOverflow() : _overflow_stack(NULL) {}
a61af66fc99e Initial load
duke
parents:
diff changeset
532 // Initialize both stealable queue and overflow
a61af66fc99e Initial load
duke
parents:
diff changeset
533 void initialize();
a61af66fc99e Initial load
duke
parents:
diff changeset
534 // Save first to stealable queue and then to overflow
a61af66fc99e Initial load
duke
parents:
diff changeset
535 void save(ChunkTask t);
a61af66fc99e Initial load
duke
parents:
diff changeset
536 // Retrieve first from overflow and then from stealable queue
a61af66fc99e Initial load
duke
parents:
diff changeset
537 bool retrieve(ChunkTask& chunk_index);
a61af66fc99e Initial load
duke
parents:
diff changeset
538 // Retrieve from stealable queue
a61af66fc99e Initial load
duke
parents:
diff changeset
539 bool retrieve_from_stealable_queue(ChunkTask& chunk_index);
a61af66fc99e Initial load
duke
parents:
diff changeset
540 // Retrieve from overflow
a61af66fc99e Initial load
duke
parents:
diff changeset
541 bool retrieve_from_overflow(ChunkTask& chunk_index);
a61af66fc99e Initial load
duke
parents:
diff changeset
542 bool is_empty();
a61af66fc99e Initial load
duke
parents:
diff changeset
543 bool stealable_is_empty();
a61af66fc99e Initial load
duke
parents:
diff changeset
544 bool overflow_is_empty();
a61af66fc99e Initial load
duke
parents:
diff changeset
545 juint stealable_size() { return _chunk_queue.size(); }
a61af66fc99e Initial load
duke
parents:
diff changeset
546 ChunkTaskQueue* task_queue() { return &_chunk_queue; }
a61af66fc99e Initial load
duke
parents:
diff changeset
547 };
a61af66fc99e Initial load
duke
parents:
diff changeset
548
a61af66fc99e Initial load
duke
parents:
diff changeset
549 #define USE_ChunkTaskQueueWithOverflow