|
1 /* |
|
2 * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 class TaskQueueSuper: public CHeapObj { |
|
26 protected: |
|
27 // The first free element after the last one pushed (mod _n). |
|
28 // (For now we'll assume only 32-bit CAS). |
|
29 volatile juint _bottom; |
|
30 |
|
31 // log2 of the size of the queue. |
|
32 enum SomeProtectedConstants { |
|
33 Log_n = 14 |
|
34 }; |
|
35 |
|
36 // Size of the queue. |
|
37 juint n() { return (1 << Log_n); } |
|
38 // For computing "x mod n" efficiently. |
|
39 juint n_mod_mask() { return n() - 1; } |
|
40 |
|
41 struct Age { |
|
42 jushort _top; |
|
43 jushort _tag; |
|
44 |
|
45 jushort tag() const { return _tag; } |
|
46 jushort top() const { return _top; } |
|
47 |
|
48 Age() { _tag = 0; _top = 0; } |
|
49 |
|
50 friend bool operator ==(const Age& a1, const Age& a2) { |
|
51 return a1.tag() == a2.tag() && a1.top() == a2.top(); |
|
52 } |
|
53 |
|
54 }; |
|
55 Age _age; |
|
56 // These make sure we do single atomic reads and writes. |
|
57 Age get_age() { |
|
58 jint res = *(volatile jint*)(&_age); |
|
59 return *(Age*)(&res); |
|
60 } |
|
61 void set_age(Age a) { |
|
62 *(volatile jint*)(&_age) = *(int*)(&a); |
|
63 } |
|
64 |
|
65 jushort get_top() { |
|
66 return get_age().top(); |
|
67 } |
|
68 |
|
69 // These both operate mod _n. |
|
70 juint increment_index(juint ind) { |
|
71 return (ind + 1) & n_mod_mask(); |
|
72 } |
|
73 juint decrement_index(juint ind) { |
|
74 return (ind - 1) & n_mod_mask(); |
|
75 } |
|
76 |
|
77 // Returns a number in the range [0.._n). If the result is "n-1", it |
|
78 // should be interpreted as 0. |
|
79 juint dirty_size(juint bot, juint top) { |
|
80 return ((jint)bot - (jint)top) & n_mod_mask(); |
|
81 } |
|
82 |
|
83 // Returns the size corresponding to the given "bot" and "top". |
|
84 juint size(juint bot, juint top) { |
|
85 juint sz = dirty_size(bot, top); |
|
86 // Has the queue "wrapped", so that bottom is less than top? |
|
87 // There's a complicated special case here. A pair of threads could |
|
88 // perform pop_local and pop_global operations concurrently, starting |
|
89 // from a state in which _bottom == _top+1. The pop_local could |
|
90 // succeed in decrementing _bottom, and the pop_global in incrementing |
|
91 // _top (in which case the pop_global will be awarded the contested |
|
92 // queue element.) The resulting state must be interpreted as an empty |
|
93 // queue. (We only need to worry about one such event: only the queue |
|
94 // owner performs pop_local's, and several concurrent threads |
|
95 // attempting to perform the pop_global will all perform the same CAS, |
|
96 // and only one can succeed. Any stealing thread that reads after |
|
97 // either the increment or decrement will seen an empty queue, and will |
|
98 // not join the competitors. The "sz == -1 || sz == _n-1" state will |
|
99 // not be modified by concurrent queues, so the owner thread can reset |
|
100 // the state to _bottom == top so subsequent pushes will be performed |
|
101 // normally. |
|
102 if (sz == (n()-1)) return 0; |
|
103 else return sz; |
|
104 } |
|
105 |
|
106 public: |
|
107 TaskQueueSuper() : _bottom(0), _age() {} |
|
108 |
|
109 // Return "true" if the TaskQueue contains any tasks. |
|
110 bool peek(); |
|
111 |
|
112 // Return an estimate of the number of elements in the queue. |
|
113 // The "careful" version admits the possibility of pop_local/pop_global |
|
114 // races. |
|
115 juint size() { |
|
116 return size(_bottom, get_top()); |
|
117 } |
|
118 |
|
119 juint dirty_size() { |
|
120 return dirty_size(_bottom, get_top()); |
|
121 } |
|
122 |
|
123 // Maximum number of elements allowed in the queue. This is two less |
|
124 // than the actual queue size, for somewhat complicated reasons. |
|
125 juint max_elems() { return n() - 2; } |
|
126 |
|
127 }; |
|
128 |
|
129 template<class E> class GenericTaskQueue: public TaskQueueSuper { |
|
130 private: |
|
131 // Slow paths for push, pop_local. (pop_global has no fast path.) |
|
132 bool push_slow(E t, juint dirty_n_elems); |
|
133 bool pop_local_slow(juint localBot, Age oldAge); |
|
134 |
|
135 public: |
|
136 // Initializes the queue to empty. |
|
137 GenericTaskQueue(); |
|
138 |
|
139 void initialize(); |
|
140 |
|
141 // Push the task "t" on the queue. Returns "false" iff the queue is |
|
142 // full. |
|
143 inline bool push(E t); |
|
144 |
|
145 // If succeeds in claiming a task (from the 'local' end, that is, the |
|
146 // most recently pushed task), returns "true" and sets "t" to that task. |
|
147 // Otherwise, the queue is empty and returns false. |
|
148 inline bool pop_local(E& t); |
|
149 |
|
150 // If succeeds in claiming a task (from the 'global' end, that is, the |
|
151 // least recently pushed task), returns "true" and sets "t" to that task. |
|
152 // Otherwise, the queue is empty and returns false. |
|
153 bool pop_global(E& t); |
|
154 |
|
155 // Delete any resource associated with the queue. |
|
156 ~GenericTaskQueue(); |
|
157 |
|
158 private: |
|
159 // Element array. |
|
160 volatile E* _elems; |
|
161 }; |
|
162 |
|
163 template<class E> |
|
164 GenericTaskQueue<E>::GenericTaskQueue():TaskQueueSuper() { |
|
165 assert(sizeof(Age) == sizeof(jint), "Depends on this."); |
|
166 } |
|
167 |
|
168 template<class E> |
|
169 void GenericTaskQueue<E>::initialize() { |
|
170 _elems = NEW_C_HEAP_ARRAY(E, n()); |
|
171 guarantee(_elems != NULL, "Allocation failed."); |
|
172 } |
|
173 |
|
174 template<class E> |
|
175 bool GenericTaskQueue<E>::push_slow(E t, juint dirty_n_elems) { |
|
176 if (dirty_n_elems == n() - 1) { |
|
177 // Actually means 0, so do the push. |
|
178 juint localBot = _bottom; |
|
179 _elems[localBot] = t; |
|
180 _bottom = increment_index(localBot); |
|
181 return true; |
|
182 } else |
|
183 return false; |
|
184 } |
|
185 |
|
186 template<class E> |
|
187 bool GenericTaskQueue<E>:: |
|
188 pop_local_slow(juint localBot, Age oldAge) { |
|
189 // This queue was observed to contain exactly one element; either this |
|
190 // thread will claim it, or a competing "pop_global". In either case, |
|
191 // the queue will be logically empty afterwards. Create a new Age value |
|
192 // that represents the empty queue for the given value of "_bottom". (We |
|
193 // must also increment "tag" because of the case where "bottom == 1", |
|
194 // "top == 0". A pop_global could read the queue element in that case, |
|
195 // then have the owner thread do a pop followed by another push. Without |
|
196 // the incrementing of "tag", the pop_global's CAS could succeed, |
|
197 // allowing it to believe it has claimed the stale element.) |
|
198 Age newAge; |
|
199 newAge._top = localBot; |
|
200 newAge._tag = oldAge.tag() + 1; |
|
201 // Perhaps a competing pop_global has already incremented "top", in which |
|
202 // case it wins the element. |
|
203 if (localBot == oldAge.top()) { |
|
204 Age tempAge; |
|
205 // No competing pop_global has yet incremented "top"; we'll try to |
|
206 // install new_age, thus claiming the element. |
|
207 assert(sizeof(Age) == sizeof(jint) && sizeof(jint) == sizeof(juint), |
|
208 "Assumption about CAS unit."); |
|
209 *(jint*)&tempAge = Atomic::cmpxchg(*(jint*)&newAge, (volatile jint*)&_age, *(jint*)&oldAge); |
|
210 if (tempAge == oldAge) { |
|
211 // We win. |
|
212 assert(dirty_size(localBot, get_top()) != n() - 1, |
|
213 "Shouldn't be possible..."); |
|
214 return true; |
|
215 } |
|
216 } |
|
217 // We fail; a completing pop_global gets the element. But the queue is |
|
218 // empty (and top is greater than bottom.) Fix this representation of |
|
219 // the empty queue to become the canonical one. |
|
220 set_age(newAge); |
|
221 assert(dirty_size(localBot, get_top()) != n() - 1, |
|
222 "Shouldn't be possible..."); |
|
223 return false; |
|
224 } |
|
225 |
|
226 template<class E> |
|
227 bool GenericTaskQueue<E>::pop_global(E& t) { |
|
228 Age newAge; |
|
229 Age oldAge = get_age(); |
|
230 juint localBot = _bottom; |
|
231 juint n_elems = size(localBot, oldAge.top()); |
|
232 if (n_elems == 0) { |
|
233 return false; |
|
234 } |
|
235 t = _elems[oldAge.top()]; |
|
236 newAge = oldAge; |
|
237 newAge._top = increment_index(newAge.top()); |
|
238 if ( newAge._top == 0 ) newAge._tag++; |
|
239 Age resAge; |
|
240 *(jint*)&resAge = Atomic::cmpxchg(*(jint*)&newAge, (volatile jint*)&_age, *(jint*)&oldAge); |
|
241 // Note that using "_bottom" here might fail, since a pop_local might |
|
242 // have decremented it. |
|
243 assert(dirty_size(localBot, newAge._top) != n() - 1, |
|
244 "Shouldn't be possible..."); |
|
245 return (resAge == oldAge); |
|
246 } |
|
247 |
|
248 template<class E> |
|
249 GenericTaskQueue<E>::~GenericTaskQueue() { |
|
250 FREE_C_HEAP_ARRAY(E, _elems); |
|
251 } |
|
252 |
|
253 // Inherits the typedef of "Task" from above. |
|
254 class TaskQueueSetSuper: public CHeapObj { |
|
255 protected: |
|
256 static int randomParkAndMiller(int* seed0); |
|
257 public: |
|
258 // Returns "true" if some TaskQueue in the set contains a task. |
|
259 virtual bool peek() = 0; |
|
260 }; |
|
261 |
|
262 template<class E> class GenericTaskQueueSet: public TaskQueueSetSuper { |
|
263 private: |
|
264 int _n; |
|
265 GenericTaskQueue<E>** _queues; |
|
266 |
|
267 public: |
|
268 GenericTaskQueueSet(int n) : _n(n) { |
|
269 typedef GenericTaskQueue<E>* GenericTaskQueuePtr; |
|
270 _queues = NEW_C_HEAP_ARRAY(GenericTaskQueuePtr, n); |
|
271 guarantee(_queues != NULL, "Allocation failure."); |
|
272 for (int i = 0; i < n; i++) { |
|
273 _queues[i] = NULL; |
|
274 } |
|
275 } |
|
276 |
|
277 bool steal_1_random(int queue_num, int* seed, E& t); |
|
278 bool steal_best_of_2(int queue_num, int* seed, E& t); |
|
279 bool steal_best_of_all(int queue_num, int* seed, E& t); |
|
280 |
|
281 void register_queue(int i, GenericTaskQueue<E>* q); |
|
282 |
|
283 GenericTaskQueue<E>* queue(int n); |
|
284 |
|
285 // The thread with queue number "queue_num" (and whose random number seed |
|
286 // is at "seed") is trying to steal a task from some other queue. (It |
|
287 // may try several queues, according to some configuration parameter.) |
|
288 // If some steal succeeds, returns "true" and sets "t" the stolen task, |
|
289 // otherwise returns false. |
|
290 bool steal(int queue_num, int* seed, E& t); |
|
291 |
|
292 bool peek(); |
|
293 }; |
|
294 |
|
295 template<class E> |
|
296 void GenericTaskQueueSet<E>::register_queue(int i, GenericTaskQueue<E>* q) { |
|
297 assert(0 <= i && i < _n, "index out of range."); |
|
298 _queues[i] = q; |
|
299 } |
|
300 |
|
301 template<class E> |
|
302 GenericTaskQueue<E>* GenericTaskQueueSet<E>::queue(int i) { |
|
303 return _queues[i]; |
|
304 } |
|
305 |
|
306 template<class E> |
|
307 bool GenericTaskQueueSet<E>::steal(int queue_num, int* seed, E& t) { |
|
308 for (int i = 0; i < 2 * _n; i++) |
|
309 if (steal_best_of_2(queue_num, seed, t)) |
|
310 return true; |
|
311 return false; |
|
312 } |
|
313 |
|
314 template<class E> |
|
315 bool GenericTaskQueueSet<E>::steal_best_of_all(int queue_num, int* seed, E& t) { |
|
316 if (_n > 2) { |
|
317 int best_k; |
|
318 jint best_sz = 0; |
|
319 for (int k = 0; k < _n; k++) { |
|
320 if (k == queue_num) continue; |
|
321 jint sz = _queues[k]->size(); |
|
322 if (sz > best_sz) { |
|
323 best_sz = sz; |
|
324 best_k = k; |
|
325 } |
|
326 } |
|
327 return best_sz > 0 && _queues[best_k]->pop_global(t); |
|
328 } else if (_n == 2) { |
|
329 // Just try the other one. |
|
330 int k = (queue_num + 1) % 2; |
|
331 return _queues[k]->pop_global(t); |
|
332 } else { |
|
333 assert(_n == 1, "can't be zero."); |
|
334 return false; |
|
335 } |
|
336 } |
|
337 |
|
338 template<class E> |
|
339 bool GenericTaskQueueSet<E>::steal_1_random(int queue_num, int* seed, E& t) { |
|
340 if (_n > 2) { |
|
341 int k = queue_num; |
|
342 while (k == queue_num) k = randomParkAndMiller(seed) % _n; |
|
343 return _queues[2]->pop_global(t); |
|
344 } else if (_n == 2) { |
|
345 // Just try the other one. |
|
346 int k = (queue_num + 1) % 2; |
|
347 return _queues[k]->pop_global(t); |
|
348 } else { |
|
349 assert(_n == 1, "can't be zero."); |
|
350 return false; |
|
351 } |
|
352 } |
|
353 |
|
354 template<class E> |
|
355 bool GenericTaskQueueSet<E>::steal_best_of_2(int queue_num, int* seed, E& t) { |
|
356 if (_n > 2) { |
|
357 int k1 = queue_num; |
|
358 while (k1 == queue_num) k1 = randomParkAndMiller(seed) % _n; |
|
359 int k2 = queue_num; |
|
360 while (k2 == queue_num || k2 == k1) k2 = randomParkAndMiller(seed) % _n; |
|
361 // Sample both and try the larger. |
|
362 juint sz1 = _queues[k1]->size(); |
|
363 juint sz2 = _queues[k2]->size(); |
|
364 if (sz2 > sz1) return _queues[k2]->pop_global(t); |
|
365 else return _queues[k1]->pop_global(t); |
|
366 } else if (_n == 2) { |
|
367 // Just try the other one. |
|
368 int k = (queue_num + 1) % 2; |
|
369 return _queues[k]->pop_global(t); |
|
370 } else { |
|
371 assert(_n == 1, "can't be zero."); |
|
372 return false; |
|
373 } |
|
374 } |
|
375 |
|
376 template<class E> |
|
377 bool GenericTaskQueueSet<E>::peek() { |
|
378 // Try all the queues. |
|
379 for (int j = 0; j < _n; j++) { |
|
380 if (_queues[j]->peek()) |
|
381 return true; |
|
382 } |
|
383 return false; |
|
384 } |
|
385 |
|
386 // A class to aid in the termination of a set of parallel tasks using |
|
387 // TaskQueueSet's for work stealing. |
|
388 |
|
389 class ParallelTaskTerminator: public StackObj { |
|
390 private: |
|
391 int _n_threads; |
|
392 TaskQueueSetSuper* _queue_set; |
|
393 jint _offered_termination; |
|
394 |
|
395 bool peek_in_queue_set(); |
|
396 protected: |
|
397 virtual void yield(); |
|
398 void sleep(uint millis); |
|
399 |
|
400 public: |
|
401 |
|
402 // "n_threads" is the number of threads to be terminated. "queue_set" is a |
|
403 // queue sets of work queues of other threads. |
|
404 ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set); |
|
405 |
|
406 // The current thread has no work, and is ready to terminate if everyone |
|
407 // else is. If returns "true", all threads are terminated. If returns |
|
408 // "false", available work has been observed in one of the task queues, |
|
409 // so the global task is not complete. |
|
410 bool offer_termination(); |
|
411 |
|
412 // Reset the terminator, so that it may be reused again. |
|
413 // The caller is responsible for ensuring that this is done |
|
414 // in an MT-safe manner, once the previous round of use of |
|
415 // the terminator is finished. |
|
416 void reset_for_reuse(); |
|
417 |
|
418 }; |
|
419 |
|
420 #define SIMPLE_STACK 0 |
|
421 |
|
422 template<class E> inline bool GenericTaskQueue<E>::push(E t) { |
|
423 #if SIMPLE_STACK |
|
424 juint localBot = _bottom; |
|
425 if (_bottom < max_elems()) { |
|
426 _elems[localBot] = t; |
|
427 _bottom = localBot + 1; |
|
428 return true; |
|
429 } else { |
|
430 return false; |
|
431 } |
|
432 #else |
|
433 juint localBot = _bottom; |
|
434 assert((localBot >= 0) && (localBot < n()), "_bottom out of range."); |
|
435 jushort top = get_top(); |
|
436 juint dirty_n_elems = dirty_size(localBot, top); |
|
437 assert((dirty_n_elems >= 0) && (dirty_n_elems < n()), |
|
438 "n_elems out of range."); |
|
439 if (dirty_n_elems < max_elems()) { |
|
440 _elems[localBot] = t; |
|
441 _bottom = increment_index(localBot); |
|
442 return true; |
|
443 } else { |
|
444 return push_slow(t, dirty_n_elems); |
|
445 } |
|
446 #endif |
|
447 } |
|
448 |
|
449 template<class E> inline bool GenericTaskQueue<E>::pop_local(E& t) { |
|
450 #if SIMPLE_STACK |
|
451 juint localBot = _bottom; |
|
452 assert(localBot > 0, "precondition."); |
|
453 localBot--; |
|
454 t = _elems[localBot]; |
|
455 _bottom = localBot; |
|
456 return true; |
|
457 #else |
|
458 juint localBot = _bottom; |
|
459 // This value cannot be n-1. That can only occur as a result of |
|
460 // the assignment to bottom in this method. If it does, this method |
|
461 // resets the size( to 0 before the next call (which is sequential, |
|
462 // since this is pop_local.) |
|
463 juint dirty_n_elems = dirty_size(localBot, get_top()); |
|
464 assert(dirty_n_elems != n() - 1, "Shouldn't be possible..."); |
|
465 if (dirty_n_elems == 0) return false; |
|
466 localBot = decrement_index(localBot); |
|
467 _bottom = localBot; |
|
468 // This is necessary to prevent any read below from being reordered |
|
469 // before the store just above. |
|
470 OrderAccess::fence(); |
|
471 t = _elems[localBot]; |
|
472 // This is a second read of "age"; the "size()" above is the first. |
|
473 // If there's still at least one element in the queue, based on the |
|
474 // "_bottom" and "age" we've read, then there can be no interference with |
|
475 // a "pop_global" operation, and we're done. |
|
476 juint tp = get_top(); |
|
477 if (size(localBot, tp) > 0) { |
|
478 assert(dirty_size(localBot, tp) != n() - 1, |
|
479 "Shouldn't be possible..."); |
|
480 return true; |
|
481 } else { |
|
482 // Otherwise, the queue contained exactly one element; we take the slow |
|
483 // path. |
|
484 return pop_local_slow(localBot, get_age()); |
|
485 } |
|
486 #endif |
|
487 } |
|
488 |
|
489 typedef oop Task; |
|
490 typedef GenericTaskQueue<Task> OopTaskQueue; |
|
491 typedef GenericTaskQueueSet<Task> OopTaskQueueSet; |
|
492 |
|
493 typedef oop* StarTask; |
|
494 typedef GenericTaskQueue<StarTask> OopStarTaskQueue; |
|
495 typedef GenericTaskQueueSet<StarTask> OopStarTaskQueueSet; |
|
496 |
|
497 typedef size_t ChunkTask; // index for chunk |
|
498 typedef GenericTaskQueue<ChunkTask> ChunkTaskQueue; |
|
499 typedef GenericTaskQueueSet<ChunkTask> ChunkTaskQueueSet; |
|
500 |
|
501 class ChunkTaskQueueWithOverflow: public CHeapObj { |
|
502 protected: |
|
503 ChunkTaskQueue _chunk_queue; |
|
504 GrowableArray<ChunkTask>* _overflow_stack; |
|
505 |
|
506 public: |
|
507 ChunkTaskQueueWithOverflow() : _overflow_stack(NULL) {} |
|
508 // Initialize both stealable queue and overflow |
|
509 void initialize(); |
|
510 // Save first to stealable queue and then to overflow |
|
511 void save(ChunkTask t); |
|
512 // Retrieve first from overflow and then from stealable queue |
|
513 bool retrieve(ChunkTask& chunk_index); |
|
514 // Retrieve from stealable queue |
|
515 bool retrieve_from_stealable_queue(ChunkTask& chunk_index); |
|
516 // Retrieve from overflow |
|
517 bool retrieve_from_overflow(ChunkTask& chunk_index); |
|
518 bool is_empty(); |
|
519 bool stealable_is_empty(); |
|
520 bool overflow_is_empty(); |
|
521 juint stealable_size() { return _chunk_queue.size(); } |
|
522 ChunkTaskQueue* task_queue() { return &_chunk_queue; } |
|
523 }; |
|
524 |
|
525 #define USE_ChunkTaskQueueWithOverflow |