duke@435: /* shshahma@8611: * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_UTILITIES_TASKQUEUE_HPP stefank@2314: #define SHARE_VM_UTILITIES_TASKQUEUE_HPP stefank@2314: stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "runtime/mutex.hpp" goetz@6911: #include "runtime/orderAccess.inline.hpp" zgu@9728: #include "utilities/globalDefinitions.hpp" stefank@2314: #include "utilities/stack.hpp" stefank@2314: jcoomes@2020: // Simple TaskQueue stats that are collected by default in debug builds. jcoomes@2020: jcoomes@2020: #if !defined(TASKQUEUE_STATS) && defined(ASSERT) jcoomes@2020: #define TASKQUEUE_STATS 1 jcoomes@2020: #elif !defined(TASKQUEUE_STATS) jcoomes@2020: #define TASKQUEUE_STATS 0 jcoomes@2020: #endif jcoomes@2020: jcoomes@2020: #if TASKQUEUE_STATS jcoomes@2020: #define TASKQUEUE_STATS_ONLY(code) code jcoomes@2020: #else jcoomes@2020: #define TASKQUEUE_STATS_ONLY(code) jcoomes@2020: #endif // TASKQUEUE_STATS jcoomes@2020: jcoomes@2020: #if TASKQUEUE_STATS jcoomes@2020: class TaskQueueStats { jcoomes@2020: public: jcoomes@2020: enum StatId { jcoomes@2020: push, // number of taskqueue pushes jcoomes@2020: pop, // number of taskqueue pops jcoomes@2020: pop_slow, // subset of taskqueue pops that were done slow-path jcoomes@2020: steal_attempt, // number of taskqueue steal attempts jcoomes@2020: steal, // number of taskqueue steals jcoomes@2020: overflow, // number of overflow pushes jcoomes@2020: overflow_max_len, // max length of overflow stack jcoomes@2020: last_stat_id jcoomes@2020: }; jcoomes@2020: jcoomes@2020: public: jcoomes@2020: inline TaskQueueStats() { reset(); } jcoomes@2020: jcoomes@2020: inline void record_push() { ++_stats[push]; } jcoomes@2020: inline void record_pop() { ++_stats[pop]; } jcoomes@2020: inline void record_pop_slow() { record_pop(); ++_stats[pop_slow]; } jcoomes@2020: inline void record_steal(bool success); jcoomes@2020: inline void record_overflow(size_t new_length); jcoomes@2020: jcoomes@2064: TaskQueueStats & operator +=(const TaskQueueStats & addend); jcoomes@2064: jcoomes@2020: inline size_t get(StatId id) const { return _stats[id]; } jcoomes@2020: inline const size_t* get() const { return _stats; } jcoomes@2020: jcoomes@2020: inline void reset(); jcoomes@2020: jcoomes@2064: // Print the specified line of the header (does not include a line separator). jcoomes@2020: static void print_header(unsigned int line, outputStream* const stream = tty, jcoomes@2020: unsigned int width = 10); jcoomes@2064: // Print the statistics (does not include a line separator). jcoomes@2020: void print(outputStream* const stream = tty, unsigned int width = 10) const; jcoomes@2020: jcoomes@2064: DEBUG_ONLY(void verify() const;) jcoomes@2064: jcoomes@2020: private: jcoomes@2020: size_t _stats[last_stat_id]; jcoomes@2020: static const char * const _names[last_stat_id]; jcoomes@2020: }; jcoomes@2020: jcoomes@2020: void TaskQueueStats::record_steal(bool success) { jcoomes@2020: ++_stats[steal_attempt]; jcoomes@2020: if (success) ++_stats[steal]; jcoomes@2020: } jcoomes@2020: jcoomes@2020: void TaskQueueStats::record_overflow(size_t new_len) { jcoomes@2020: ++_stats[overflow]; jcoomes@2020: if (new_len > _stats[overflow_max_len]) _stats[overflow_max_len] = new_len; jcoomes@2020: } jcoomes@2020: jcoomes@2020: void TaskQueueStats::reset() { jcoomes@2020: memset(_stats, 0, sizeof(_stats)); jcoomes@2020: } jcoomes@2020: #endif // TASKQUEUE_STATS jcoomes@2020: tschatzl@5555: // TaskQueueSuper collects functionality common to all GenericTaskQueue instances. tschatzl@5555: zgu@3900: template zgu@3900: class TaskQueueSuper: public CHeapObj { duke@435: protected: jcoomes@1342: // Internal type for indexing the queue; also used for the tag. jcoomes@1342: typedef NOT_LP64(uint16_t) LP64_ONLY(uint32_t) idx_t; jcoomes@1342: fujie@9138: #ifdef MIPS fujie@415: private: fujie@415: #endif jcoomes@1342: // The first free element after the last one pushed (mod N). ysr@976: volatile uint _bottom; duke@435: fujie@9138: #ifdef MIPS fujie@415: protected: fujie@415: inline uint get_bottom() const { fujie@415: return OrderAccess::load_acquire((volatile juint*)&_bottom); fujie@415: } fujie@415: fujie@415: inline void set_bottom(uint new_bottom) { fujie@415: OrderAccess::release_store(&_bottom, new_bottom); fujie@415: } fujie@415: #endif fujie@415: jcoomes@1746: enum { MOD_N_MASK = N - 1 }; duke@435: jcoomes@1342: class Age { jcoomes@1342: public: jcoomes@1342: Age(size_t data = 0) { _data = data; } jcoomes@1342: Age(const Age& age) { _data = age._data; } jcoomes@1342: Age(idx_t top, idx_t tag) { _fields._top = top; _fields._tag = tag; } duke@435: fujie@9138: #ifndef MIPS jcoomes@1342: Age get() const volatile { return _data; } jcoomes@1342: void set(Age age) volatile { _data = age._data; } duke@435: jcoomes@1342: idx_t top() const volatile { return _fields._top; } jcoomes@1342: idx_t tag() const volatile { return _fields._tag; } fujie@415: #else aoqi@8860: Age get() const volatile { fujie@415: size_t res = OrderAccess::load_ptr_acquire((volatile intptr_t*) &_data); fujie@415: return *(Age*)(&res); fujie@415: } aoqi@8860: void set(Age age) volatile { OrderAccess::release_store_ptr((volatile intptr_t*) &_data, *(size_t*)(&age._data)); } fujie@415: fujie@415: idx_t top() const volatile { return OrderAccess::load_acquire((volatile idx_t*) &(_fields._top)); } fujie@415: idx_t tag() const volatile { return OrderAccess::load_acquire((volatile idx_t*) &(_fields._tag)); } fujie@415: #endif duke@435: jcoomes@1342: // Increment top; if it wraps, increment tag also. jcoomes@1342: void increment() { jcoomes@1342: _fields._top = increment_index(_fields._top); jcoomes@1342: if (_fields._top == 0) ++_fields._tag; jcoomes@1342: } duke@435: jcoomes@1342: Age cmpxchg(const Age new_age, const Age old_age) volatile { jcoomes@1342: return (size_t) Atomic::cmpxchg_ptr((intptr_t)new_age._data, jcoomes@1342: (volatile intptr_t *)&_data, jcoomes@1342: (intptr_t)old_age._data); duke@435: } jcoomes@1342: jcoomes@1342: bool operator ==(const Age& other) const { return _data == other._data; } jcoomes@1342: jcoomes@1342: private: jcoomes@1342: struct fields { jcoomes@1342: idx_t _top; jcoomes@1342: idx_t _tag; jcoomes@1342: }; jcoomes@1342: union { jcoomes@1342: size_t _data; jcoomes@1342: fields _fields; jcoomes@1342: }; duke@435: }; jcoomes@1342: jcoomes@1342: volatile Age _age; jcoomes@1342: jcoomes@1342: // These both operate mod N. jcoomes@1342: static uint increment_index(uint ind) { jcoomes@1342: return (ind + 1) & MOD_N_MASK; duke@435: } jcoomes@1342: static uint decrement_index(uint ind) { jcoomes@1342: return (ind - 1) & MOD_N_MASK; duke@435: } duke@435: jcoomes@1342: // Returns a number in the range [0..N). If the result is "N-1", it should be jcoomes@1342: // interpreted as 0. jcoomes@1746: uint dirty_size(uint bot, uint top) const { jcoomes@1342: return (bot - top) & MOD_N_MASK; duke@435: } duke@435: duke@435: // Returns the size corresponding to the given "bot" and "top". jcoomes@1746: uint size(uint bot, uint top) const { ysr@976: uint sz = dirty_size(bot, top); jcoomes@1342: // Has the queue "wrapped", so that bottom is less than top? There's a jcoomes@1342: // complicated special case here. A pair of threads could perform pop_local jcoomes@1342: // and pop_global operations concurrently, starting from a state in which jcoomes@1342: // _bottom == _top+1. The pop_local could succeed in decrementing _bottom, jcoomes@1342: // and the pop_global in incrementing _top (in which case the pop_global jcoomes@1342: // will be awarded the contested queue element.) The resulting state must jcoomes@1342: // be interpreted as an empty queue. (We only need to worry about one such jcoomes@1342: // event: only the queue owner performs pop_local's, and several concurrent jcoomes@1342: // threads attempting to perform the pop_global will all perform the same jcoomes@1342: // CAS, and only one can succeed.) Any stealing thread that reads after jcoomes@1342: // either the increment or decrement will see an empty queue, and will not jcoomes@1342: // join the competitors. The "sz == -1 || sz == N-1" state will not be jcoomes@1342: // modified by concurrent queues, so the owner thread can reset the state to jcoomes@1342: // _bottom == top so subsequent pushes will be performed normally. jcoomes@1342: return (sz == N - 1) ? 0 : sz; duke@435: } duke@435: duke@435: public: duke@435: TaskQueueSuper() : _bottom(0), _age() {} duke@435: jcoomes@1993: // Return true if the TaskQueue contains/does not contain any tasks. aoqi@8860: bool peek() const { fujie@9138: #ifdef MIPS aoqi@8860: return get_bottom() != _age.top(); fujie@415: #else aoqi@8860: return _bottom != _age.top(); fujie@415: #endif fujie@415: } jcoomes@1993: bool is_empty() const { return size() == 0; } duke@435: duke@435: // Return an estimate of the number of elements in the queue. duke@435: // The "careful" version admits the possibility of pop_local/pop_global duke@435: // races. jcoomes@1746: uint size() const { fujie@9138: #ifdef MIPS fujie@415: return size(get_bottom(), _age.top()); fujie@415: #else jcoomes@1342: return size(_bottom, _age.top()); fujie@415: #endif duke@435: } duke@435: jcoomes@1746: uint dirty_size() const { fujie@9138: #ifdef MIPS fujie@415: return dirty_size(get_bottom(), _age.top()); fujie@415: #else jcoomes@1342: return dirty_size(_bottom, _age.top()); fujie@415: #endif duke@435: } duke@435: ysr@777: void set_empty() { fujie@9138: #ifdef MIPS fujie@415: set_bottom(0); fujie@415: #else ysr@777: _bottom = 0; fujie@415: #endif jcoomes@1342: _age.set(0); ysr@777: } ysr@777: duke@435: // Maximum number of elements allowed in the queue. This is two less duke@435: // than the actual queue size, for somewhat complicated reasons. jcoomes@1746: uint max_elems() const { return N - 2; } jmasa@1719: jmasa@1719: // Total size of queue. jmasa@1719: static const uint total_size() { return N; } jcoomes@2020: jcoomes@2020: TASKQUEUE_STATS_ONLY(TaskQueueStats stats;) duke@435: }; duke@435: tschatzl@5555: // tschatzl@5555: // GenericTaskQueue implements an ABP, Aurora-Blumofe-Plaxton, double- tschatzl@5555: // ended-queue (deque), intended for use in work stealing. Queue operations tschatzl@5555: // are non-blocking. tschatzl@5555: // tschatzl@5555: // A queue owner thread performs push() and pop_local() operations on one end tschatzl@5555: // of the queue, while other threads may steal work using the pop_global() tschatzl@5555: // method. tschatzl@5555: // tschatzl@5555: // The main difference to the original algorithm is that this tschatzl@5555: // implementation allows wrap-around at the end of its allocated tschatzl@5555: // storage, which is an array. tschatzl@5555: // tschatzl@5555: // The original paper is: tschatzl@5555: // tschatzl@5555: // Arora, N. S., Blumofe, R. D., and Plaxton, C. G. tschatzl@5555: // Thread scheduling for multiprogrammed multiprocessors. tschatzl@5555: // Theory of Computing Systems 34, 2 (2001), 115-144. tschatzl@5555: // tschatzl@5555: // The following paper provides an correctness proof and an tschatzl@5555: // implementation for weakly ordered memory models including (pseudo-) tschatzl@5555: // code containing memory barriers for a Chase-Lev deque. Chase-Lev is tschatzl@5555: // similar to ABP, with the main difference that it allows resizing of the tschatzl@5555: // underlying storage: tschatzl@5555: // tschatzl@5555: // Le, N. M., Pop, A., Cohen A., and Nardell, F. Z. tschatzl@5555: // Correct and efficient work-stealing for weak memory models tschatzl@5555: // Proceedings of the 18th ACM SIGPLAN symposium on Principles and tschatzl@5555: // practice of parallel programming (PPoPP 2013), 69-80 tschatzl@5555: // zgu@3900: zgu@3900: template zgu@3900: class GenericTaskQueue: public TaskQueueSuper { brutisso@4901: ArrayAllocator _array_allocator; jcoomes@1746: protected: zgu@3900: typedef typename TaskQueueSuper::Age Age; zgu@3900: typedef typename TaskQueueSuper::idx_t idx_t; jcoomes@1746: fujie@9138: #ifndef MIPS zgu@3900: using TaskQueueSuper::_bottom; fujie@415: #endif zgu@3900: using TaskQueueSuper::_age; zgu@3900: using TaskQueueSuper::increment_index; zgu@3900: using TaskQueueSuper::decrement_index; zgu@3900: using TaskQueueSuper::dirty_size; jcoomes@1746: jcoomes@1746: public: zgu@3900: using TaskQueueSuper::max_elems; zgu@3900: using TaskQueueSuper::size; zgu@3900: zgu@3900: #if TASKQUEUE_STATS zgu@3900: using TaskQueueSuper::stats; zgu@3900: #endif jcoomes@1746: duke@435: private: duke@435: // Slow paths for push, pop_local. (pop_global has no fast path.) ysr@976: bool push_slow(E t, uint dirty_n_elems); ysr@976: bool pop_local_slow(uint localBot, Age oldAge); duke@435: duke@435: public: jcoomes@1746: typedef E element_type; jcoomes@1746: duke@435: // Initializes the queue to empty. duke@435: GenericTaskQueue(); duke@435: duke@435: void initialize(); duke@435: jcoomes@1993: // Push the task "t" on the queue. Returns "false" iff the queue is full. duke@435: inline bool push(E t); duke@435: jcoomes@1993: // Attempts to claim a task from the "local" end of the queue (the most jcoomes@1993: // recently pushed). If successful, returns true and sets t to the task; jcoomes@1993: // otherwise, returns false (the queue is empty). hseigel@5784: inline bool pop_local(volatile E& t); duke@435: jcoomes@1993: // Like pop_local(), but uses the "global" end of the queue (the least jcoomes@1993: // recently pushed). hseigel@5784: bool pop_global(volatile E& t); duke@435: duke@435: // Delete any resource associated with the queue. duke@435: ~GenericTaskQueue(); duke@435: ysr@777: // apply the closure to all elements in the task queue ysr@777: void oops_do(OopClosure* f); ysr@777: duke@435: private: duke@435: // Element array. duke@435: volatile E* _elems; duke@435: }; duke@435: zgu@3900: template zgu@3900: GenericTaskQueue::GenericTaskQueue() { jcoomes@1342: assert(sizeof(Age) == sizeof(size_t), "Depends on this."); duke@435: } duke@435: zgu@3900: template zgu@3900: void GenericTaskQueue::initialize() { brutisso@4901: _elems = _array_allocator.allocate(N); duke@435: } duke@435: zgu@3900: template zgu@3900: void GenericTaskQueue::oops_do(OopClosure* f) { ysr@777: // tty->print_cr("START OopTaskQueue::oops_do"); ysr@976: uint iters = size(); fujie@9138: #ifdef MIPS fujie@415: uint index = this->get_bottom(); fujie@415: #else ysr@976: uint index = _bottom; fujie@415: #endif ysr@976: for (uint i = 0; i < iters; ++i) { ysr@777: index = decrement_index(index); ysr@777: // tty->print_cr(" doing entry %d," INTPTR_T " -> " INTPTR_T, ysr@777: // index, &_elems[index], _elems[index]); ysr@777: E* t = (E*)&_elems[index]; // cast away volatility ysr@777: oop* p = (oop*)t; ysr@777: assert((*t)->is_oop_or_null(), "Not an oop or null"); ysr@777: f->do_oop(p); ysr@777: } ysr@777: // tty->print_cr("END OopTaskQueue::oops_do"); ysr@777: } ysr@777: zgu@3900: template zgu@3900: bool GenericTaskQueue::push_slow(E t, uint dirty_n_elems) { jcoomes@1342: if (dirty_n_elems == N - 1) { duke@435: // Actually means 0, so do the push. fujie@9138: #ifdef MIPS fujie@415: uint localBot = this->get_bottom(); fujie@415: #else ysr@976: uint localBot = _bottom; fujie@415: #endif ccheung@5259: // g++ complains if the volatile result of the assignment is ccheung@5259: // unused, so we cast the volatile away. We cannot cast directly ccheung@5259: // to void, because gcc treats that as not using the result of the ccheung@5259: // assignment. However, casting to E& means that we trigger an ccheung@5259: // unused-value warning. So, we cast the E& to void. ccheung@5259: (void)const_cast(_elems[localBot] = t); fujie@9138: #ifdef MIPS fujie@415: this->set_bottom(increment_index(localBot)); fujie@415: #else bobv@1459: OrderAccess::release_store(&_bottom, increment_index(localBot)); fujie@415: #endif jcoomes@2020: TASKQUEUE_STATS_ONLY(stats.record_push()); duke@435: return true; jcoomes@1342: } jcoomes@1342: return false; duke@435: } duke@435: jmasa@2188: // pop_local_slow() is done by the owning thread and is trying to jmasa@2188: // get the last task in the queue. It will compete with pop_global() jmasa@2188: // that will be used by other threads. The tag age is incremented jmasa@2188: // whenever the queue goes empty which it will do here if this thread jmasa@2188: // gets the last task or in pop_global() if the queue wraps (top == 0 jmasa@2188: // and pop_global() succeeds, see pop_global()). zgu@3900: template zgu@3900: bool GenericTaskQueue::pop_local_slow(uint localBot, Age oldAge) { duke@435: // This queue was observed to contain exactly one element; either this duke@435: // thread will claim it, or a competing "pop_global". In either case, duke@435: // the queue will be logically empty afterwards. Create a new Age value duke@435: // that represents the empty queue for the given value of "_bottom". (We duke@435: // must also increment "tag" because of the case where "bottom == 1", duke@435: // "top == 0". A pop_global could read the queue element in that case, duke@435: // then have the owner thread do a pop followed by another push. Without duke@435: // the incrementing of "tag", the pop_global's CAS could succeed, duke@435: // allowing it to believe it has claimed the stale element.) jcoomes@1342: Age newAge((idx_t)localBot, oldAge.tag() + 1); duke@435: // Perhaps a competing pop_global has already incremented "top", in which duke@435: // case it wins the element. duke@435: if (localBot == oldAge.top()) { duke@435: // No competing pop_global has yet incremented "top"; we'll try to duke@435: // install new_age, thus claiming the element. jcoomes@1342: Age tempAge = _age.cmpxchg(newAge, oldAge); duke@435: if (tempAge == oldAge) { duke@435: // We win. jcoomes@1342: assert(dirty_size(localBot, _age.top()) != N - 1, "sanity"); jcoomes@2020: TASKQUEUE_STATS_ONLY(stats.record_pop_slow()); duke@435: return true; duke@435: } duke@435: } jcoomes@1342: // We lose; a completing pop_global gets the element. But the queue is empty jcoomes@1342: // and top is greater than bottom. Fix this representation of the empty queue jcoomes@1342: // to become the canonical one. jcoomes@1342: _age.set(newAge); jcoomes@1342: assert(dirty_size(localBot, _age.top()) != N - 1, "sanity"); duke@435: return false; duke@435: } duke@435: zgu@3900: template hseigel@5784: bool GenericTaskQueue::pop_global(volatile E& t) { jcoomes@1342: Age oldAge = _age.get(); vladidan@5483: // Architectures with weak memory model require a barrier here vladidan@5483: // to guarantee that bottom is not older than age, vladidan@5483: // which is crucial for the correctness of the algorithm. vladidan@5483: #if !(defined SPARC || defined IA32 || defined AMD64) vladidan@5483: OrderAccess::fence(); vladidan@5483: #endif fujie@9138: #ifdef MIPS fujie@415: uint localBot = this->get_bottom(); fujie@415: #else vladidan@5483: uint localBot = OrderAccess::load_acquire((volatile juint*)&_bottom); fujie@415: #endif ysr@976: uint n_elems = size(localBot, oldAge.top()); duke@435: if (n_elems == 0) { duke@435: return false; duke@435: } jcoomes@1342: ccheung@5259: // g++ complains if the volatile result of the assignment is ccheung@5259: // unused, so we cast the volatile away. We cannot cast directly ccheung@5259: // to void, because gcc treats that as not using the result of the ccheung@5259: // assignment. However, casting to E& means that we trigger an ccheung@5259: // unused-value warning. So, we cast the E& to void. ccheung@5259: (void) const_cast(t = _elems[oldAge.top()]); jcoomes@1342: Age newAge(oldAge); jcoomes@1342: newAge.increment(); jcoomes@1342: Age resAge = _age.cmpxchg(newAge, oldAge); jcoomes@1342: duke@435: // Note that using "_bottom" here might fail, since a pop_local might duke@435: // have decremented it. jcoomes@1342: assert(dirty_size(localBot, newAge.top()) != N - 1, "sanity"); jcoomes@1342: return resAge == oldAge; duke@435: } duke@435: zgu@3900: template zgu@3900: GenericTaskQueue::~GenericTaskQueue() { zgu@3900: FREE_C_HEAP_ARRAY(E, _elems, F); duke@435: } duke@435: jcoomes@1993: // OverflowTaskQueue is a TaskQueue that also includes an overflow stack for jcoomes@1993: // elements that do not fit in the TaskQueue. jcoomes@1993: // jcoomes@2191: // This class hides two methods from super classes: jcoomes@1993: // jcoomes@1993: // push() - push onto the task queue or, if that fails, onto the overflow stack jcoomes@1993: // is_empty() - return true if both the TaskQueue and overflow stack are empty jcoomes@1993: // jcoomes@2191: // Note that size() is not hidden--it returns the number of elements in the jcoomes@1993: // TaskQueue, and does not include the size of the overflow stack. This jcoomes@1993: // simplifies replacement of GenericTaskQueues with OverflowTaskQueues. zgu@3900: template zgu@3900: class OverflowTaskQueue: public GenericTaskQueue jcoomes@1993: { jcoomes@1993: public: zgu@3900: typedef Stack overflow_t; zgu@3900: typedef GenericTaskQueue taskqueue_t; jcoomes@1993: jcoomes@2020: TASKQUEUE_STATS_ONLY(using taskqueue_t::stats;) jcoomes@2020: jcoomes@1993: // Push task t onto the queue or onto the overflow stack. Return true. jcoomes@1993: inline bool push(E t); jcoomes@1993: shshahma@8611: // Try to push task t onto the queue only. Returns true if successful, false otherwise. shshahma@8611: inline bool try_push_to_taskqueue(E t); shshahma@8611: jcoomes@1993: // Attempt to pop from the overflow stack; return true if anything was popped. jcoomes@1993: inline bool pop_overflow(E& t); jcoomes@1993: jcoomes@2191: inline overflow_t* overflow_stack() { return &_overflow_stack; } jcoomes@2191: jcoomes@1993: inline bool taskqueue_empty() const { return taskqueue_t::is_empty(); } jcoomes@2191: inline bool overflow_empty() const { return _overflow_stack.is_empty(); } jcoomes@1993: inline bool is_empty() const { jcoomes@1993: return taskqueue_empty() && overflow_empty(); jcoomes@1993: } jcoomes@1993: jcoomes@1993: private: jcoomes@2191: overflow_t _overflow_stack; jcoomes@1993: }; jcoomes@1993: zgu@3900: template zgu@3900: bool OverflowTaskQueue::push(E t) jcoomes@1993: { jcoomes@1993: if (!taskqueue_t::push(t)) { jcoomes@1993: overflow_stack()->push(t); jcoomes@2191: TASKQUEUE_STATS_ONLY(stats.record_overflow(overflow_stack()->size())); jcoomes@1993: } jcoomes@1993: return true; jcoomes@1993: } jcoomes@1993: zgu@3900: template zgu@3900: bool OverflowTaskQueue::pop_overflow(E& t) jcoomes@1993: { jcoomes@1993: if (overflow_empty()) return false; jcoomes@1993: t = overflow_stack()->pop(); jcoomes@1993: return true; jcoomes@1993: } jcoomes@1993: shshahma@8611: template shshahma@8611: bool OverflowTaskQueue::try_push_to_taskqueue(E t) { shshahma@8611: return taskqueue_t::push(t); shshahma@8611: } zgu@3900: class TaskQueueSetSuper { duke@435: protected: duke@435: static int randomParkAndMiller(int* seed0); duke@435: public: duke@435: // Returns "true" if some TaskQueue in the set contains a task. duke@435: virtual bool peek() = 0; duke@435: }; duke@435: zgu@3900: template class TaskQueueSetSuperImpl: public CHeapObj, public TaskQueueSetSuper { zgu@3900: }; zgu@3900: zgu@3900: template zgu@3900: class GenericTaskQueueSet: public TaskQueueSetSuperImpl { duke@435: private: ysr@976: uint _n; jcoomes@1746: T** _queues; duke@435: duke@435: public: jcoomes@1746: typedef typename T::element_type E; jcoomes@1746: duke@435: GenericTaskQueueSet(int n) : _n(n) { jcoomes@1746: typedef T* GenericTaskQueuePtr; zgu@3900: _queues = NEW_C_HEAP_ARRAY(GenericTaskQueuePtr, n, F); duke@435: for (int i = 0; i < n; i++) { duke@435: _queues[i] = NULL; duke@435: } duke@435: } duke@435: ysr@976: bool steal_best_of_2(uint queue_num, int* seed, E& t); duke@435: jcoomes@1746: void register_queue(uint i, T* q); duke@435: jcoomes@1746: T* queue(uint n); duke@435: jcoomes@1993: // The thread with queue number "queue_num" (and whose random number seed is jcoomes@1993: // at "seed") is trying to steal a task from some other queue. (It may try jcoomes@1993: // several queues, according to some configuration parameter.) If some steal jcoomes@1993: // succeeds, returns "true" and sets "t" to the stolen task, otherwise returns jcoomes@1993: // false. ysr@976: bool steal(uint queue_num, int* seed, E& t); duke@435: duke@435: bool peek(); duke@435: }; duke@435: zgu@3900: template void zgu@3900: GenericTaskQueueSet::register_queue(uint i, T* q) { ysr@976: assert(i < _n, "index out of range."); duke@435: _queues[i] = q; duke@435: } duke@435: zgu@3900: template T* zgu@3900: GenericTaskQueueSet::queue(uint i) { duke@435: return _queues[i]; duke@435: } duke@435: zgu@3900: template bool zgu@3900: GenericTaskQueueSet::steal(uint queue_num, int* seed, E& t) { jcoomes@2020: for (uint i = 0; i < 2 * _n; i++) { jcoomes@2020: if (steal_best_of_2(queue_num, seed, t)) { jcoomes@2020: TASKQUEUE_STATS_ONLY(queue(queue_num)->stats.record_steal(true)); duke@435: return true; jcoomes@2020: } jcoomes@2020: } jcoomes@2020: TASKQUEUE_STATS_ONLY(queue(queue_num)->stats.record_steal(false)); duke@435: return false; duke@435: } duke@435: zgu@3900: template bool zgu@3900: GenericTaskQueueSet::steal_best_of_2(uint queue_num, int* seed, E& t) { duke@435: if (_n > 2) { ysr@976: uint k1 = queue_num; zgu@3900: while (k1 == queue_num) k1 = TaskQueueSetSuper::randomParkAndMiller(seed) % _n; ysr@976: uint k2 = queue_num; zgu@3900: while (k2 == queue_num || k2 == k1) k2 = TaskQueueSetSuper::randomParkAndMiller(seed) % _n; duke@435: // Sample both and try the larger. ysr@976: uint sz1 = _queues[k1]->size(); ysr@976: uint sz2 = _queues[k2]->size(); duke@435: if (sz2 > sz1) return _queues[k2]->pop_global(t); duke@435: else return _queues[k1]->pop_global(t); duke@435: } else if (_n == 2) { duke@435: // Just try the other one. ysr@976: uint k = (queue_num + 1) % 2; duke@435: return _queues[k]->pop_global(t); duke@435: } else { duke@435: assert(_n == 1, "can't be zero."); duke@435: return false; duke@435: } duke@435: } duke@435: zgu@3900: template zgu@3900: bool GenericTaskQueueSet::peek() { duke@435: // Try all the queues. ysr@976: for (uint j = 0; j < _n; j++) { duke@435: if (_queues[j]->peek()) duke@435: return true; duke@435: } duke@435: return false; duke@435: } duke@435: ysr@777: // When to terminate from the termination protocol. zgu@3900: class TerminatorTerminator: public CHeapObj { ysr@777: public: ysr@777: virtual bool should_exit_termination() = 0; ysr@777: }; ysr@777: duke@435: // A class to aid in the termination of a set of parallel tasks using duke@435: // TaskQueueSet's for work stealing. duke@435: jmasa@981: #undef TRACESPINNING jmasa@981: duke@435: class ParallelTaskTerminator: public StackObj { duke@435: private: duke@435: int _n_threads; duke@435: TaskQueueSetSuper* _queue_set; zgu@9728: char _pad_before[DEFAULT_CACHE_LINE_SIZE]; ysr@976: int _offered_termination; zgu@9728: char _pad_after[DEFAULT_CACHE_LINE_SIZE]; duke@435: jmasa@981: #ifdef TRACESPINNING jmasa@981: static uint _total_yields; jmasa@981: static uint _total_spins; jmasa@981: static uint _total_peeks; jmasa@981: #endif jmasa@981: duke@435: bool peek_in_queue_set(); duke@435: protected: duke@435: virtual void yield(); duke@435: void sleep(uint millis); duke@435: duke@435: public: duke@435: duke@435: // "n_threads" is the number of threads to be terminated. "queue_set" is a duke@435: // queue sets of work queues of other threads. duke@435: ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set); duke@435: duke@435: // The current thread has no work, and is ready to terminate if everyone duke@435: // else is. If returns "true", all threads are terminated. If returns duke@435: // "false", available work has been observed in one of the task queues, duke@435: // so the global task is not complete. ysr@777: bool offer_termination() { ysr@777: return offer_termination(NULL); ysr@777: } ysr@777: jcoomes@1342: // As above, but it also terminates if the should_exit_termination() ysr@777: // method of the terminator parameter returns true. If terminator is ysr@777: // NULL, then it is ignored. ysr@777: bool offer_termination(TerminatorTerminator* terminator); duke@435: duke@435: // Reset the terminator, so that it may be reused again. duke@435: // The caller is responsible for ensuring that this is done duke@435: // in an MT-safe manner, once the previous round of use of duke@435: // the terminator is finished. duke@435: void reset_for_reuse(); jmasa@2188: // Same as above but the number of parallel threads is set to the jmasa@2188: // given number. jmasa@2188: void reset_for_reuse(int n_threads); duke@435: jmasa@981: #ifdef TRACESPINNING jmasa@981: static uint total_yields() { return _total_yields; } jmasa@981: static uint total_spins() { return _total_spins; } jmasa@981: static uint total_peeks() { return _total_peeks; } jmasa@981: static void print_termination_counts(); jmasa@981: #endif duke@435: }; duke@435: zgu@3900: template inline bool zgu@3900: GenericTaskQueue::push(E t) { fujie@9138: #ifdef MIPS fujie@415: uint localBot = this->get_bottom(); fujie@415: #else ysr@976: uint localBot = _bottom; fujie@415: #endif vladidan@5483: assert(localBot < N, "_bottom out of range."); jcoomes@1342: idx_t top = _age.top(); ysr@976: uint dirty_n_elems = dirty_size(localBot, top); jcoomes@1746: assert(dirty_n_elems < N, "n_elems out of range."); duke@435: if (dirty_n_elems < max_elems()) { ccheung@5259: // g++ complains if the volatile result of the assignment is ccheung@5259: // unused, so we cast the volatile away. We cannot cast directly ccheung@5259: // to void, because gcc treats that as not using the result of the ccheung@5259: // assignment. However, casting to E& means that we trigger an ccheung@5259: // unused-value warning. So, we cast the E& to void. ccheung@5259: (void) const_cast(_elems[localBot] = t); fujie@9138: #ifdef MIPS fujie@415: this->set_bottom(increment_index(localBot)); fujie@415: #else bobv@1459: OrderAccess::release_store(&_bottom, increment_index(localBot)); fujie@415: #endif jcoomes@2020: TASKQUEUE_STATS_ONLY(stats.record_push()); duke@435: return true; duke@435: } else { duke@435: return push_slow(t, dirty_n_elems); duke@435: } duke@435: } duke@435: zgu@3900: template inline bool hseigel@5784: GenericTaskQueue::pop_local(volatile E& t) { fujie@9138: #ifdef MIPS fujie@415: uint localBot = this->get_bottom(); fujie@415: #else ysr@976: uint localBot = _bottom; fujie@415: #endif jcoomes@1342: // This value cannot be N-1. That can only occur as a result of duke@435: // the assignment to bottom in this method. If it does, this method jcoomes@1993: // resets the size to 0 before the next call (which is sequential, duke@435: // since this is pop_local.) jcoomes@1342: uint dirty_n_elems = dirty_size(localBot, _age.top()); jcoomes@1342: assert(dirty_n_elems != N - 1, "Shouldn't be possible..."); duke@435: if (dirty_n_elems == 0) return false; duke@435: localBot = decrement_index(localBot); fujie@9138: #ifdef MIPS fujie@415: this->set_bottom(localBot); fujie@415: #else duke@435: _bottom = localBot; fujie@415: #endif duke@435: // This is necessary to prevent any read below from being reordered duke@435: // before the store just above. duke@435: OrderAccess::fence(); ccheung@5259: // g++ complains if the volatile result of the assignment is ccheung@5259: // unused, so we cast the volatile away. We cannot cast directly ccheung@5259: // to void, because gcc treats that as not using the result of the ccheung@5259: // assignment. However, casting to E& means that we trigger an ccheung@5259: // unused-value warning. So, we cast the E& to void. ccheung@5259: (void) const_cast(t = _elems[localBot]); duke@435: // This is a second read of "age"; the "size()" above is the first. duke@435: // If there's still at least one element in the queue, based on the duke@435: // "_bottom" and "age" we've read, then there can be no interference with duke@435: // a "pop_global" operation, and we're done. jcoomes@1342: idx_t tp = _age.top(); // XXX duke@435: if (size(localBot, tp) > 0) { jcoomes@1342: assert(dirty_size(localBot, tp) != N - 1, "sanity"); jcoomes@2020: TASKQUEUE_STATS_ONLY(stats.record_pop()); duke@435: return true; duke@435: } else { duke@435: // Otherwise, the queue contained exactly one element; we take the slow duke@435: // path. jiefu@9784: jiefu@9784: // The barrier is required to prevent reordering the two reads of _age: jiefu@9784: // one is the _age.get() below, and the other is _age.top() above the if-stmt. jiefu@9784: // The algorithm may fail if _age.get() reads an older value than _age.top(). jiefu@9784: OrderAccess::loadload(); jcoomes@1342: return pop_local_slow(localBot, _age.get()); duke@435: } duke@435: } duke@435: zgu@3900: typedef GenericTaskQueue OopTaskQueue; zgu@3900: typedef GenericTaskQueueSet OopTaskQueueSet; duke@435: jcoomes@1746: #ifdef _MSC_VER jcoomes@1746: #pragma warning(push) jcoomes@1746: // warning C4522: multiple assignment operators specified jcoomes@1746: #pragma warning(disable:4522) jcoomes@1746: #endif coleenp@548: coleenp@548: // This is a container class for either an oop* or a narrowOop*. coleenp@548: // Both are pushed onto a task queue and the consumer will test is_narrow() coleenp@548: // to determine which should be processed. coleenp@548: class StarTask { coleenp@548: void* _holder; // either union oop* or narrowOop* jcoomes@1746: jcoomes@1746: enum { COMPRESSED_OOP_MASK = 1 }; jcoomes@1746: coleenp@548: public: ysr@1280: StarTask(narrowOop* p) { ysr@1280: assert(((uintptr_t)p & COMPRESSED_OOP_MASK) == 0, "Information loss!"); ysr@1280: _holder = (void *)((uintptr_t)p | COMPRESSED_OOP_MASK); ysr@1280: } ysr@1280: StarTask(oop* p) { ysr@1280: assert(((uintptr_t)p & COMPRESSED_OOP_MASK) == 0, "Information loss!"); ysr@1280: _holder = (void*)p; ysr@1280: } coleenp@548: StarTask() { _holder = NULL; } coleenp@548: operator oop*() { return (oop*)_holder; } coleenp@548: operator narrowOop*() { coleenp@548: return (narrowOop*)((uintptr_t)_holder & ~COMPRESSED_OOP_MASK); coleenp@548: } coleenp@548: jcoomes@1746: StarTask& operator=(const StarTask& t) { jcoomes@1746: _holder = t._holder; jcoomes@1746: return *this; jcoomes@1746: } jcoomes@1746: volatile StarTask& operator=(const volatile StarTask& t) volatile { jcoomes@1746: _holder = t._holder; jcoomes@1746: return *this; jcoomes@1746: } coleenp@548: coleenp@548: bool is_narrow() const { coleenp@548: return (((uintptr_t)_holder & COMPRESSED_OOP_MASK) != 0); coleenp@548: } coleenp@548: }; coleenp@548: jcoomes@1746: class ObjArrayTask jcoomes@1746: { jcoomes@1746: public: jcoomes@1746: ObjArrayTask(oop o = NULL, int idx = 0): _obj(o), _index(idx) { } jcoomes@1746: ObjArrayTask(oop o, size_t idx): _obj(o), _index(int(idx)) { jcoomes@1746: assert(idx <= size_t(max_jint), "too big"); jcoomes@1746: } jcoomes@1746: ObjArrayTask(const ObjArrayTask& t): _obj(t._obj), _index(t._index) { } jcoomes@1746: jcoomes@1746: ObjArrayTask& operator =(const ObjArrayTask& t) { jcoomes@1746: _obj = t._obj; jcoomes@1746: _index = t._index; jcoomes@1746: return *this; jcoomes@1746: } jcoomes@1746: volatile ObjArrayTask& jcoomes@1746: operator =(const volatile ObjArrayTask& t) volatile { hseigel@5784: (void)const_cast(_obj = t._obj); jcoomes@1746: _index = t._index; jcoomes@1746: return *this; jcoomes@1746: } jcoomes@1746: jcoomes@1746: inline oop obj() const { return _obj; } jcoomes@1746: inline int index() const { return _index; } jcoomes@1746: jcoomes@1746: DEBUG_ONLY(bool is_valid() const); // Tasks to be pushed/popped must be valid. jcoomes@1746: jcoomes@1746: private: jcoomes@1746: oop _obj; jcoomes@1746: int _index; jcoomes@1746: }; jcoomes@1746: jcoomes@1746: #ifdef _MSC_VER jcoomes@1746: #pragma warning(pop) jcoomes@1746: #endif jcoomes@1746: zgu@3900: typedef OverflowTaskQueue OopStarTaskQueue; zgu@3900: typedef GenericTaskQueueSet OopStarTaskQueueSet; duke@435: zgu@3900: typedef OverflowTaskQueue RegionTaskQueue; zgu@3900: typedef GenericTaskQueueSet RegionTaskQueueSet; jmasa@2188: stefank@2314: stefank@2314: #endif // SHARE_VM_UTILITIES_TASKQUEUE_HPP