1 /* 2 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_UTILITIES_TASKQUEUE_HPP 26 #define SHARE_VM_UTILITIES_TASKQUEUE_HPP 27 28 #include "memory/allocation.hpp" 29 #include "memory/allocation.inline.hpp" 30 #include "runtime/mutex.hpp" 31 #ifdef TARGET_OS_ARCH_linux_x86 32 # include "orderAccess_linux_x86.inline.hpp" 33 #endif 34 #ifdef TARGET_OS_ARCH_linux_sparc 35 # include "orderAccess_linux_sparc.inline.hpp" 36 #endif 37 #ifdef TARGET_OS_ARCH_linux_zero 38 # include "orderAccess_linux_zero.inline.hpp" 39 #endif 40 #ifdef TARGET_OS_ARCH_solaris_x86 41 # include "orderAccess_solaris_x86.inline.hpp" 42 #endif 43 #ifdef TARGET_OS_ARCH_solaris_sparc 44 # include "orderAccess_solaris_sparc.inline.hpp" 45 #endif 46 #ifdef TARGET_OS_ARCH_windows_x86 47 # include "orderAccess_windows_x86.inline.hpp" 48 #endif 49 50 // Simple TaskQueue stats that are collected by default in debug builds. 51 52 #if !defined(TASKQUEUE_STATS) && defined(ASSERT) 53 #define TASKQUEUE_STATS 1 54 #elif !defined(TASKQUEUE_STATS) 55 #define TASKQUEUE_STATS 0 56 #endif 57 58 #if TASKQUEUE_STATS 59 #define TASKQUEUE_STATS_ONLY(code) code 60 #else 61 #define TASKQUEUE_STATS_ONLY(code) 62 #endif // TASKQUEUE_STATS 63 64 #if TASKQUEUE_STATS 65 class TaskQueueStats { 66 public: 67 enum StatId { 68 push, // number of taskqueue pushes 69 pop, // number of taskqueue pops 70 pop_slow, // subset of taskqueue pops that were done slow-path 71 steal_attempt, // number of taskqueue steal attempts 72 steal, // number of taskqueue steals 73 overflow, // number of overflow pushes 74 overflow_max_len, // max length of overflow stack 75 last_stat_id 76 }; 77 78 public: 79 inline TaskQueueStats() { reset(); } 80 81 inline void record_push() { ++_stats[push]; } 82 inline void record_pop() { ++_stats[pop]; } 83 inline void record_pop_slow() { record_pop(); ++_stats[pop_slow]; } 84 inline void record_steal(bool success); 85 inline void record_overflow(size_t new_length); 86 87 TaskQueueStats & operator +=(const TaskQueueStats & addend); 88 89 inline size_t get(StatId id) const { return _stats[id]; } 90 inline const size_t* get() const { return _stats; } 91 92 inline void reset(); 93 94 // Print the specified line of the header (does not include a line separator). 95 static void print_header(unsigned int line, outputStream* const stream = tty, 96 unsigned int width = 10); 97 // Print the statistics (does not include a line separator). 98 void print(outputStream* const stream = tty, unsigned int width = 10) const; 99 100 DEBUG_ONLY(void verify() const;) 101 102 private: 103 size_t _stats[last_stat_id]; 104 static const char * const _names[last_stat_id]; 105 }; 106 107 void TaskQueueStats::record_steal(bool success) { 108 ++_stats[steal_attempt]; 109 if (success) ++_stats[steal]; 110 } 111 112 void TaskQueueStats::record_overflow(size_t new_len) { 113 ++_stats[overflow]; 114 if (new_len > _stats[overflow_max_len]) _stats[overflow_max_len] = new_len; 115 } 116 117 void TaskQueueStats::reset() { 118 memset(_stats, 0, sizeof(_stats)); 119 } 120 #endif // TASKQUEUE_STATS 121 122 template <unsigned int N> 123 class TaskQueueSuper: public CHeapObj { 124 protected: 125 // Internal type for indexing the queue; also used for the tag. 126 typedef NOT_LP64(uint16_t) LP64_ONLY(uint32_t) idx_t; 127 128 // The first free element after the last one pushed (mod N). 129 volatile uint _bottom; 130 131 enum { MOD_N_MASK = N - 1 }; 132 133 class Age { 134 public: 135 Age(size_t data = 0) { _data = data; } 136 Age(const Age& age) { _data = age._data; } 137 Age(idx_t top, idx_t tag) { _fields._top = top; _fields._tag = tag; } 138 139 Age get() const volatile { return _data; } 140 void set(Age age) volatile { _data = age._data; } 141 142 idx_t top() const volatile { return _fields._top; } 143 idx_t tag() const volatile { return _fields._tag; } 144 145 // Increment top; if it wraps, increment tag also. 146 void increment() { 147 _fields._top = increment_index(_fields._top); 148 if (_fields._top == 0) ++_fields._tag; 149 } 150 151 Age cmpxchg(const Age new_age, const Age old_age) volatile { 152 return (size_t) Atomic::cmpxchg_ptr((intptr_t)new_age._data, 153 (volatile intptr_t *)&_data, 154 (intptr_t)old_age._data); 155 } 156 157 bool operator ==(const Age& other) const { return _data == other._data; } 158 159 private: 160 struct fields { 161 idx_t _top; 162 idx_t _tag; 163 }; 164 union { 165 size_t _data; 166 fields _fields; 167 }; 168 }; 169 170 volatile Age _age; 171 172 // These both operate mod N. 173 static uint increment_index(uint ind) { 174 return (ind + 1) & MOD_N_MASK; 175 } 176 static uint decrement_index(uint ind) { 177 return (ind - 1) & MOD_N_MASK; 178 } 179 180 // Returns a number in the range [0..N). If the result is "N-1", it should be 181 // interpreted as 0. 182 uint dirty_size(uint bot, uint top) const { 183 return (bot - top) & MOD_N_MASK; 184 } 185 186 // Returns the size corresponding to the given "bot" and "top". 187 uint size(uint bot, uint top) const { 188 uint sz = dirty_size(bot, top); 189 // Has the queue "wrapped", so that bottom is less than top? There's a 190 // complicated special case here. A pair of threads could perform pop_local 191 // and pop_global operations concurrently, starting from a state in which 192 // _bottom == _top+1. The pop_local could succeed in decrementing _bottom, 193 // and the pop_global in incrementing _top (in which case the pop_global 194 // will be awarded the contested queue element.) The resulting state must 195 // be interpreted as an empty queue. (We only need to worry about one such 196 // event: only the queue owner performs pop_local's, and several concurrent 197 // threads attempting to perform the pop_global will all perform the same 198 // CAS, and only one can succeed.) Any stealing thread that reads after 199 // either the increment or decrement will see an empty queue, and will not 200 // join the competitors. The "sz == -1 || sz == N-1" state will not be 201 // modified by concurrent queues, so the owner thread can reset the state to 202 // _bottom == top so subsequent pushes will be performed normally. 203 return (sz == N - 1) ? 0 : sz; 204 } 205 206 public: 207 TaskQueueSuper() : _bottom(0), _age() {} 208 209 // Return true if the TaskQueue contains/does not contain any tasks. 210 bool peek() const { return _bottom != _age.top(); } 211 bool is_empty() const { return size() == 0; } 212 213 // Return an estimate of the number of elements in the queue. 214 // The "careful" version admits the possibility of pop_local/pop_global 215 // races. 216 uint size() const { 217 return size(_bottom, _age.top()); 218 } 219 220 uint dirty_size() const { 221 return dirty_size(_bottom, _age.top()); 222 } 223 224 void set_empty() { 225 _bottom = 0; 226 _age.set(0); 227 } 228 229 // Maximum number of elements allowed in the queue. This is two less 230 // than the actual queue size, for somewhat complicated reasons. 231 uint max_elems() const { return N - 2; } 232 233 // Total size of queue. 234 static const uint total_size() { return N; } 235 236 TASKQUEUE_STATS_ONLY(TaskQueueStats stats;) 237 }; 238 239 template<class E, unsigned int N = TASKQUEUE_SIZE> 240 class GenericTaskQueue: public TaskQueueSuper<N> { 241 protected: 242 typedef typename TaskQueueSuper<N>::Age Age; 243 typedef typename TaskQueueSuper<N>::idx_t idx_t; 244 245 using TaskQueueSuper<N>::_bottom; 246 using TaskQueueSuper<N>::_age; 247 using TaskQueueSuper<N>::increment_index; 248 using TaskQueueSuper<N>::decrement_index; 249 using TaskQueueSuper<N>::dirty_size; 250 251 public: 252 using TaskQueueSuper<N>::max_elems; 253 using TaskQueueSuper<N>::size; 254 TASKQUEUE_STATS_ONLY(using TaskQueueSuper<N>::stats;) 255 256 private: 257 // Slow paths for push, pop_local. (pop_global has no fast path.) 258 bool push_slow(E t, uint dirty_n_elems); 259 bool pop_local_slow(uint localBot, Age oldAge); 260 261 public: 262 typedef E element_type; 263 264 // Initializes the queue to empty. 265 GenericTaskQueue(); 266 267 void initialize(); 268 269 // Push the task "t" on the queue. Returns "false" iff the queue is full. 270 inline bool push(E t); 271 272 // Attempts to claim a task from the "local" end of the queue (the most 273 // recently pushed). If successful, returns true and sets t to the task; 274 // otherwise, returns false (the queue is empty). 275 inline bool pop_local(E& t); 276 277 // Like pop_local(), but uses the "global" end of the queue (the least 278 // recently pushed). 279 bool pop_global(E& t); 280 281 // Delete any resource associated with the queue. 282 ~GenericTaskQueue(); 283 284 // apply the closure to all elements in the task queue 285 void oops_do(OopClosure* f); 286 287 private: 288 // Element array. 289 volatile E* _elems; 290 }; 291 292 template<class E, unsigned int N> 293 GenericTaskQueue<E, N>::GenericTaskQueue() { 294 assert(sizeof(Age) == sizeof(size_t), "Depends on this."); 295 } 296 297 template<class E, unsigned int N> 298 void GenericTaskQueue<E, N>::initialize() { 299 _elems = NEW_C_HEAP_ARRAY(E, N); 300 } 301 302 template<class E, unsigned int N> 303 void GenericTaskQueue<E, N>::oops_do(OopClosure* f) { 304 // tty->print_cr("START OopTaskQueue::oops_do"); 305 uint iters = size(); 306 uint index = _bottom; 307 for (uint i = 0; i < iters; ++i) { 308 index = decrement_index(index); 309 // tty->print_cr(" doing entry %d," INTPTR_T " -> " INTPTR_T, 310 // index, &_elems[index], _elems[index]); 311 E* t = (E*)&_elems[index]; // cast away volatility 312 oop* p = (oop*)t; 313 assert((*t)->is_oop_or_null(), "Not an oop or null"); 314 f->do_oop(p); 315 } 316 // tty->print_cr("END OopTaskQueue::oops_do"); 317 } 318 319 template<class E, unsigned int N> 320 bool GenericTaskQueue<E, N>::push_slow(E t, uint dirty_n_elems) { 321 if (dirty_n_elems == N - 1) { 322 // Actually means 0, so do the push. 323 uint localBot = _bottom; 324 // g++ complains if the volatile result of the assignment is unused. 325 const_cast<E&>(_elems[localBot] = t); 326 OrderAccess::release_store(&_bottom, increment_index(localBot)); 327 TASKQUEUE_STATS_ONLY(stats.record_push()); 328 return true; 329 } 330 return false; 331 } 332 333 template<class E, unsigned int N> 334 bool GenericTaskQueue<E, N>::pop_local_slow(uint localBot, Age oldAge) { 335 // This queue was observed to contain exactly one element; either this 336 // thread will claim it, or a competing "pop_global". In either case, 337 // the queue will be logically empty afterwards. Create a new Age value 338 // that represents the empty queue for the given value of "_bottom". (We 339 // must also increment "tag" because of the case where "bottom == 1", 340 // "top == 0". A pop_global could read the queue element in that case, 341 // then have the owner thread do a pop followed by another push. Without 342 // the incrementing of "tag", the pop_global's CAS could succeed, 343 // allowing it to believe it has claimed the stale element.) 344 Age newAge((idx_t)localBot, oldAge.tag() + 1); 345 // Perhaps a competing pop_global has already incremented "top", in which 346 // case it wins the element. 347 if (localBot == oldAge.top()) { 348 // No competing pop_global has yet incremented "top"; we'll try to 349 // install new_age, thus claiming the element. 350 Age tempAge = _age.cmpxchg(newAge, oldAge); 351 if (tempAge == oldAge) { 352 // We win. 353 assert(dirty_size(localBot, _age.top()) != N - 1, "sanity"); 354 TASKQUEUE_STATS_ONLY(stats.record_pop_slow()); 355 return true; 356 } 357 } 358 // We lose; a completing pop_global gets the element. But the queue is empty 359 // and top is greater than bottom. Fix this representation of the empty queue 360 // to become the canonical one. 361 _age.set(newAge); 362 assert(dirty_size(localBot, _age.top()) != N - 1, "sanity"); 363 return false; 364 } 365 366 template<class E, unsigned int N> 367 bool GenericTaskQueue<E, N>::pop_global(E& t) { 368 Age oldAge = _age.get(); 369 uint localBot = _bottom; 370 uint n_elems = size(localBot, oldAge.top()); 371 if (n_elems == 0) { 372 return false; 373 } 374 375 const_cast<E&>(t = _elems[oldAge.top()]); 376 Age newAge(oldAge); 377 newAge.increment(); 378 Age resAge = _age.cmpxchg(newAge, oldAge); 379 380 // Note that using "_bottom" here might fail, since a pop_local might 381 // have decremented it. 382 assert(dirty_size(localBot, newAge.top()) != N - 1, "sanity"); 383 return resAge == oldAge; 384 } 385 386 template<class E, unsigned int N> 387 GenericTaskQueue<E, N>::~GenericTaskQueue() { 388 FREE_C_HEAP_ARRAY(E, _elems); 389 } 390 391 // OverflowTaskQueue is a TaskQueue that also includes an overflow stack for 392 // elements that do not fit in the TaskQueue. 393 // 394 // Three methods from super classes are overridden: 395 // 396 // initialize() - initialize the super classes and create the overflow stack 397 // push() - push onto the task queue or, if that fails, onto the overflow stack 398 // is_empty() - return true if both the TaskQueue and overflow stack are empty 399 // 400 // Note that size() is not overridden--it returns the number of elements in the 401 // TaskQueue, and does not include the size of the overflow stack. This 402 // simplifies replacement of GenericTaskQueues with OverflowTaskQueues. 403 template<class E, unsigned int N = TASKQUEUE_SIZE> 404 class OverflowTaskQueue: public GenericTaskQueue<E, N> 405 { 406 public: 407 typedef GrowableArray<E> overflow_t; 408 typedef GenericTaskQueue<E, N> taskqueue_t; 409 410 TASKQUEUE_STATS_ONLY(using taskqueue_t::stats;) 411 412 OverflowTaskQueue(); 413 ~OverflowTaskQueue(); 414 void initialize(); 415 416 inline overflow_t* overflow_stack() const { return _overflow_stack; } 417 418 // Push task t onto the queue or onto the overflow stack. Return true. 419 inline bool push(E t); 420 421 // Attempt to pop from the overflow stack; return true if anything was popped. 422 inline bool pop_overflow(E& t); 423 424 inline bool taskqueue_empty() const { return taskqueue_t::is_empty(); } 425 inline bool overflow_empty() const { return overflow_stack()->is_empty(); } 426 inline bool is_empty() const { 427 return taskqueue_empty() && overflow_empty(); 428 } 429 430 private: 431 overflow_t* _overflow_stack; 432 }; 433 434 template <class E, unsigned int N> 435 OverflowTaskQueue<E, N>::OverflowTaskQueue() 436 { 437 _overflow_stack = NULL; 438 } 439 440 template <class E, unsigned int N> 441 OverflowTaskQueue<E, N>::~OverflowTaskQueue() 442 { 443 if (_overflow_stack != NULL) { 444 delete _overflow_stack; 445 _overflow_stack = NULL; 446 } 447 } 448 449 template <class E, unsigned int N> 450 void OverflowTaskQueue<E, N>::initialize() 451 { 452 taskqueue_t::initialize(); 453 assert(_overflow_stack == NULL, "memory leak"); 454 _overflow_stack = new (ResourceObj::C_HEAP) GrowableArray<E>(10, true); 455 } 456 457 template <class E, unsigned int N> 458 bool OverflowTaskQueue<E, N>::push(E t) 459 { 460 if (!taskqueue_t::push(t)) { 461 overflow_stack()->push(t); 462 TASKQUEUE_STATS_ONLY(stats.record_overflow(overflow_stack()->length())); 463 } 464 return true; 465 } 466 467 template <class E, unsigned int N> 468 bool OverflowTaskQueue<E, N>::pop_overflow(E& t) 469 { 470 if (overflow_empty()) return false; 471 t = overflow_stack()->pop(); 472 return true; 473 } 474 475 class TaskQueueSetSuper: public CHeapObj { 476 protected: 477 static int randomParkAndMiller(int* seed0); 478 public: 479 // Returns "true" if some TaskQueue in the set contains a task. 480 virtual bool peek() = 0; 481 }; 482 483 template<class T> 484 class GenericTaskQueueSet: public TaskQueueSetSuper { 485 private: 486 uint _n; 487 T** _queues; 488 489 public: 490 typedef typename T::element_type E; 491 492 GenericTaskQueueSet(int n) : _n(n) { 493 typedef T* GenericTaskQueuePtr; 494 _queues = NEW_C_HEAP_ARRAY(GenericTaskQueuePtr, n); 495 for (int i = 0; i < n; i++) { 496 _queues[i] = NULL; 497 } 498 } 499 500 bool steal_1_random(uint queue_num, int* seed, E& t); 501 bool steal_best_of_2(uint queue_num, int* seed, E& t); 502 bool steal_best_of_all(uint queue_num, int* seed, E& t); 503 504 void register_queue(uint i, T* q); 505 506 T* queue(uint n); 507 508 // The thread with queue number "queue_num" (and whose random number seed is 509 // at "seed") is trying to steal a task from some other queue. (It may try 510 // several queues, according to some configuration parameter.) If some steal 511 // succeeds, returns "true" and sets "t" to the stolen task, otherwise returns 512 // false. 513 bool steal(uint queue_num, int* seed, E& t); 514 515 bool peek(); 516 }; 517 518 template<class T> void 519 GenericTaskQueueSet<T>::register_queue(uint i, T* q) { 520 assert(i < _n, "index out of range."); 521 _queues[i] = q; 522 } 523 524 template<class T> T* 525 GenericTaskQueueSet<T>::queue(uint i) { 526 return _queues[i]; 527 } 528 529 template<class T> bool 530 GenericTaskQueueSet<T>::steal(uint queue_num, int* seed, E& t) { 531 for (uint i = 0; i < 2 * _n; i++) { 532 if (steal_best_of_2(queue_num, seed, t)) { 533 TASKQUEUE_STATS_ONLY(queue(queue_num)->stats.record_steal(true)); 534 return true; 535 } 536 } 537 TASKQUEUE_STATS_ONLY(queue(queue_num)->stats.record_steal(false)); 538 return false; 539 } 540 541 template<class T> bool 542 GenericTaskQueueSet<T>::steal_best_of_all(uint queue_num, int* seed, E& t) { 543 if (_n > 2) { 544 int best_k; 545 uint best_sz = 0; 546 for (uint k = 0; k < _n; k++) { 547 if (k == queue_num) continue; 548 uint sz = _queues[k]->size(); 549 if (sz > best_sz) { 550 best_sz = sz; 551 best_k = k; 552 } 553 } 554 return best_sz > 0 && _queues[best_k]->pop_global(t); 555 } else if (_n == 2) { 556 // Just try the other one. 557 int k = (queue_num + 1) % 2; 558 return _queues[k]->pop_global(t); 559 } else { 560 assert(_n == 1, "can't be zero."); 561 return false; 562 } 563 } 564 565 template<class T> bool 566 GenericTaskQueueSet<T>::steal_1_random(uint queue_num, int* seed, E& t) { 567 if (_n > 2) { 568 uint k = queue_num; 569 while (k == queue_num) k = randomParkAndMiller(seed) % _n; 570 return _queues[2]->pop_global(t); 571 } else if (_n == 2) { 572 // Just try the other one. 573 int k = (queue_num + 1) % 2; 574 return _queues[k]->pop_global(t); 575 } else { 576 assert(_n == 1, "can't be zero."); 577 return false; 578 } 579 } 580 581 template<class T> bool 582 GenericTaskQueueSet<T>::steal_best_of_2(uint queue_num, int* seed, E& t) { 583 if (_n > 2) { 584 uint k1 = queue_num; 585 while (k1 == queue_num) k1 = randomParkAndMiller(seed) % _n; 586 uint k2 = queue_num; 587 while (k2 == queue_num || k2 == k1) k2 = randomParkAndMiller(seed) % _n; 588 // Sample both and try the larger. 589 uint sz1 = _queues[k1]->size(); 590 uint sz2 = _queues[k2]->size(); 591 if (sz2 > sz1) return _queues[k2]->pop_global(t); 592 else return _queues[k1]->pop_global(t); 593 } else if (_n == 2) { 594 // Just try the other one. 595 uint k = (queue_num + 1) % 2; 596 return _queues[k]->pop_global(t); 597 } else { 598 assert(_n == 1, "can't be zero."); 599 return false; 600 } 601 } 602 603 template<class T> 604 bool GenericTaskQueueSet<T>::peek() { 605 // Try all the queues. 606 for (uint j = 0; j < _n; j++) { 607 if (_queues[j]->peek()) 608 return true; 609 } 610 return false; 611 } 612 613 // When to terminate from the termination protocol. 614 class TerminatorTerminator: public CHeapObj { 615 public: 616 virtual bool should_exit_termination() = 0; 617 }; 618 619 // A class to aid in the termination of a set of parallel tasks using 620 // TaskQueueSet's for work stealing. 621 622 #undef TRACESPINNING 623 624 class ParallelTaskTerminator: public StackObj { 625 private: 626 int _n_threads; 627 TaskQueueSetSuper* _queue_set; 628 int _offered_termination; 629 630 #ifdef TRACESPINNING 631 static uint _total_yields; 632 static uint _total_spins; 633 static uint _total_peeks; 634 #endif 635 636 bool peek_in_queue_set(); 637 protected: 638 virtual void yield(); 639 void sleep(uint millis); 640 641 public: 642 643 // "n_threads" is the number of threads to be terminated. "queue_set" is a 644 // queue sets of work queues of other threads. 645 ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set); 646 647 // The current thread has no work, and is ready to terminate if everyone 648 // else is. If returns "true", all threads are terminated. If returns 649 // "false", available work has been observed in one of the task queues, 650 // so the global task is not complete. 651 bool offer_termination() { 652 return offer_termination(NULL); 653 } 654 655 // As above, but it also terminates if the should_exit_termination() 656 // method of the terminator parameter returns true. If terminator is 657 // NULL, then it is ignored. 658 bool offer_termination(TerminatorTerminator* terminator); 659 660 // Reset the terminator, so that it may be reused again. 661 // The caller is responsible for ensuring that this is done 662 // in an MT-safe manner, once the previous round of use of 663 // the terminator is finished. 664 void reset_for_reuse(); 665 666 #ifdef TRACESPINNING 667 static uint total_yields() { return _total_yields; } 668 static uint total_spins() { return _total_spins; } 669 static uint total_peeks() { return _total_peeks; } 670 static void print_termination_counts(); 671 #endif 672 }; 673 674 template<class E, unsigned int N> inline bool 675 GenericTaskQueue<E, N>::push(E t) { 676 uint localBot = _bottom; 677 assert((localBot >= 0) && (localBot < N), "_bottom out of range."); 678 idx_t top = _age.top(); 679 uint dirty_n_elems = dirty_size(localBot, top); 680 assert(dirty_n_elems < N, "n_elems out of range."); 681 if (dirty_n_elems < max_elems()) { 682 // g++ complains if the volatile result of the assignment is unused. 683 const_cast<E&>(_elems[localBot] = t); 684 OrderAccess::release_store(&_bottom, increment_index(localBot)); 685 TASKQUEUE_STATS_ONLY(stats.record_push()); 686 return true; 687 } else { 688 return push_slow(t, dirty_n_elems); 689 } 690 } 691 692 template<class E, unsigned int N> inline bool 693 GenericTaskQueue<E, N>::pop_local(E& t) { 694 uint localBot = _bottom; 695 // This value cannot be N-1. That can only occur as a result of 696 // the assignment to bottom in this method. If it does, this method 697 // resets the size to 0 before the next call (which is sequential, 698 // since this is pop_local.) 699 uint dirty_n_elems = dirty_size(localBot, _age.top()); 700 assert(dirty_n_elems != N - 1, "Shouldn't be possible..."); 701 if (dirty_n_elems == 0) return false; 702 localBot = decrement_index(localBot); 703 _bottom = localBot; 704 // This is necessary to prevent any read below from being reordered 705 // before the store just above. 706 OrderAccess::fence(); 707 const_cast<E&>(t = _elems[localBot]); 708 // This is a second read of "age"; the "size()" above is the first. 709 // If there's still at least one element in the queue, based on the 710 // "_bottom" and "age" we've read, then there can be no interference with 711 // a "pop_global" operation, and we're done. 712 idx_t tp = _age.top(); // XXX 713 if (size(localBot, tp) > 0) { 714 assert(dirty_size(localBot, tp) != N - 1, "sanity"); 715 TASKQUEUE_STATS_ONLY(stats.record_pop()); 716 return true; 717 } else { 718 // Otherwise, the queue contained exactly one element; we take the slow 719 // path. 720 return pop_local_slow(localBot, _age.get()); 721 } 722 } 723 724 typedef GenericTaskQueue<oop> OopTaskQueue; 725 typedef GenericTaskQueueSet<OopTaskQueue> OopTaskQueueSet; 726 727 #ifdef _MSC_VER 728 #pragma warning(push) 729 // warning C4522: multiple assignment operators specified 730 #pragma warning(disable:4522) 731 #endif 732 733 // This is a container class for either an oop* or a narrowOop*. 734 // Both are pushed onto a task queue and the consumer will test is_narrow() 735 // to determine which should be processed. 736 class StarTask { 737 void* _holder; // either union oop* or narrowOop* 738 739 enum { COMPRESSED_OOP_MASK = 1 }; 740 741 public: 742 StarTask(narrowOop* p) { 743 assert(((uintptr_t)p & COMPRESSED_OOP_MASK) == 0, "Information loss!"); 744 _holder = (void *)((uintptr_t)p | COMPRESSED_OOP_MASK); 745 } 746 StarTask(oop* p) { 747 assert(((uintptr_t)p & COMPRESSED_OOP_MASK) == 0, "Information loss!"); 748 _holder = (void*)p; 749 } 750 StarTask() { _holder = NULL; } 751 operator oop*() { return (oop*)_holder; } 752 operator narrowOop*() { 753 return (narrowOop*)((uintptr_t)_holder & ~COMPRESSED_OOP_MASK); 754 } 755 756 StarTask& operator=(const StarTask& t) { 757 _holder = t._holder; 758 return *this; 759 } 760 volatile StarTask& operator=(const volatile StarTask& t) volatile { 761 _holder = t._holder; 762 return *this; 763 } 764 765 bool is_narrow() const { 766 return (((uintptr_t)_holder & COMPRESSED_OOP_MASK) != 0); 767 } 768 }; 769 770 class ObjArrayTask 771 { 772 public: 773 ObjArrayTask(oop o = NULL, int idx = 0): _obj(o), _index(idx) { } 774 ObjArrayTask(oop o, size_t idx): _obj(o), _index(int(idx)) { 775 assert(idx <= size_t(max_jint), "too big"); 776 } 777 ObjArrayTask(const ObjArrayTask& t): _obj(t._obj), _index(t._index) { } 778 779 ObjArrayTask& operator =(const ObjArrayTask& t) { 780 _obj = t._obj; 781 _index = t._index; 782 return *this; 783 } 784 volatile ObjArrayTask& 785 operator =(const volatile ObjArrayTask& t) volatile { 786 _obj = t._obj; 787 _index = t._index; 788 return *this; 789 } 790 791 inline oop obj() const { return _obj; } 792 inline int index() const { return _index; } 793 794 DEBUG_ONLY(bool is_valid() const); // Tasks to be pushed/popped must be valid. 795 796 private: 797 oop _obj; 798 int _index; 799 }; 800 801 #ifdef _MSC_VER 802 #pragma warning(pop) 803 #endif 804 805 typedef OverflowTaskQueue<StarTask> OopStarTaskQueue; 806 typedef GenericTaskQueueSet<OopStarTaskQueue> OopStarTaskQueueSet; 807 808 typedef OverflowTaskQueue<size_t> RegionTaskQueue; 809 typedef GenericTaskQueueSet<RegionTaskQueue> RegionTaskQueueSet; 810 811 #endif // SHARE_VM_UTILITIES_TASKQUEUE_HPP