taskqueue.hpp revision 2073
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#ifndef SHARE_VM_UTILITIES_TASKQUEUE_HPP
#define SHARE_VM_UTILITIES_TASKQUEUE_HPP
#include "memory/allocation.hpp"
#include "memory/allocation.inline.hpp"
#ifdef TARGET_OS_ARCH_linux_x86
# include "orderAccess_linux_x86.inline.hpp"
#endif
#ifdef TARGET_OS_ARCH_linux_sparc
# include "orderAccess_linux_sparc.inline.hpp"
#endif
#ifdef TARGET_OS_ARCH_linux_zero
# include "orderAccess_linux_zero.inline.hpp"
#endif
#ifdef TARGET_OS_ARCH_solaris_x86
# include "orderAccess_solaris_x86.inline.hpp"
#endif
#ifdef TARGET_OS_ARCH_solaris_sparc
# include "orderAccess_solaris_sparc.inline.hpp"
#endif
#ifdef TARGET_OS_ARCH_windows_x86
# include "orderAccess_windows_x86.inline.hpp"
#endif
#ifdef TARGET_OS_ARCH_linux_arm
# include "orderAccess_linux_arm.inline.hpp"
#endif
#ifdef TARGET_OS_ARCH_linux_ppc
# include "orderAccess_linux_ppc.inline.hpp"
#endif
// Simple TaskQueue stats that are collected by default in debug builds.
#if !defined(TASKQUEUE_STATS) && defined(ASSERT)
#define TASKQUEUE_STATS 1
#elif !defined(TASKQUEUE_STATS)
#define TASKQUEUE_STATS 0
#endif
#if TASKQUEUE_STATS
#else
#define TASKQUEUE_STATS_ONLY(code)
#endif // TASKQUEUE_STATS
#if TASKQUEUE_STATS
class TaskQueueStats {
public:
enum StatId {
push, // number of taskqueue pushes
pop, // number of taskqueue pops
pop_slow, // subset of taskqueue pops that were done slow-path
steal_attempt, // number of taskqueue steal attempts
steal, // number of taskqueue steals
overflow, // number of overflow pushes
overflow_max_len, // max length of overflow stack
};
public:
inline TaskQueueStats() { reset(); }
inline void record_steal(bool success);
inline void reset();
// Print the specified line of the header (does not include a line separator).
unsigned int width = 10);
// Print the statistics (does not include a line separator).
DEBUG_ONLY(void verify() const;)
private:
static const char * const _names[last_stat_id];
};
++_stats[steal_attempt];
}
}
void TaskQueueStats::reset() {
}
#endif // TASKQUEUE_STATS
template <unsigned int N>
class TaskQueueSuper: public CHeapObj {
protected:
// Internal type for indexing the queue; also used for the tag.
// The first free element after the last one pushed (mod N).
enum { MOD_N_MASK = N - 1 };
class Age {
public:
// Increment top; if it wraps, increment tag also.
void increment() {
}
}
private:
struct fields {
};
union {
};
};
// These both operate mod N.
}
}
// Returns a number in the range [0..N). If the result is "N-1", it should be
// interpreted as 0.
}
// Returns the size corresponding to the given "bot" and "top".
// Has the queue "wrapped", so that bottom is less than top? There's a
// complicated special case here. A pair of threads could perform pop_local
// and pop_global operations concurrently, starting from a state in which
// _bottom == _top+1. The pop_local could succeed in decrementing _bottom,
// and the pop_global in incrementing _top (in which case the pop_global
// will be awarded the contested queue element.) The resulting state must
// be interpreted as an empty queue. (We only need to worry about one such
// event: only the queue owner performs pop_local's, and several concurrent
// threads attempting to perform the pop_global will all perform the same
// CAS, and only one can succeed.) Any stealing thread that reads after
// either the increment or decrement will see an empty queue, and will not
// join the competitors. The "sz == -1 || sz == N-1" state will not be
// modified by concurrent queues, so the owner thread can reset the state to
// _bottom == top so subsequent pushes will be performed normally.
}
public:
// Return an estimate of the number of elements in the queue.
// The "careful" version admits the possibility of pop_local/pop_global
// races.
}
uint dirty_size() const {
}
void set_empty() {
_bottom = 0;
}
// Maximum number of elements allowed in the queue. This is two less
// than the actual queue size, for somewhat complicated reasons.
// Total size of queue.
static const uint total_size() { return N; }
};
template<class E, unsigned int N = TASKQUEUE_SIZE>
class GenericTaskQueue: public TaskQueueSuper<N> {
protected:
using TaskQueueSuper<N>::_bottom;
using TaskQueueSuper<N>::_age;
using TaskQueueSuper<N>::increment_index;
using TaskQueueSuper<N>::decrement_index;
using TaskQueueSuper<N>::dirty_size;
public:
using TaskQueueSuper<N>::max_elems;
using TaskQueueSuper<N>::size;
private:
// Slow paths for push, pop_local. (pop_global has no fast path.)
public:
typedef E element_type;
// Initializes the queue to empty.
void initialize();
// Push the task "t" on the queue. Returns "false" iff the queue is full.
inline bool push(E t);
// Attempts to claim a task from the "local" end of the queue (the most
// recently pushed). If successful, returns true and sets t to the task;
// otherwise, returns false (the queue is empty).
inline bool pop_local(E& t);
// Like pop_local(), but uses the "global" end of the queue (the least
// recently pushed).
bool pop_global(E& t);
// Delete any resource associated with the queue.
~GenericTaskQueue();
// apply the closure to all elements in the task queue
void oops_do(OopClosure* f);
private:
// Element array.
volatile E* _elems;
};
template<class E, unsigned int N>
GenericTaskQueue<E, N>::GenericTaskQueue() {
}
template<class E, unsigned int N>
void GenericTaskQueue<E, N>::initialize() {
_elems = NEW_C_HEAP_ARRAY(E, N);
}
template<class E, unsigned int N>
// tty->print_cr("START OopTaskQueue::oops_do");
// tty->print_cr(" doing entry %d," INTPTR_T " -> " INTPTR_T,
// index, &_elems[index], _elems[index]);
f->do_oop(p);
}
// tty->print_cr("END OopTaskQueue::oops_do");
}
template<class E, unsigned int N>
if (dirty_n_elems == N - 1) {
// Actually means 0, so do the push.
// g++ complains if the volatile result of the assignment is unused.
return true;
}
return false;
}
// pop_local_slow() is done by the owning thread and is trying to
// get the last task in the queue. It will compete with pop_global()
// that will be used by other threads. The tag age is incremented
// whenever the queue goes empty which it will do here if this thread
// gets the last task or in pop_global() if the queue wraps (top == 0
// and pop_global() succeeds, see pop_global()).
template<class E, unsigned int N>
// This queue was observed to contain exactly one element; either this
// thread will claim it, or a competing "pop_global". In either case,
// the queue will be logically empty afterwards. Create a new Age value
// that represents the empty queue for the given value of "_bottom". (We
// must also increment "tag" because of the case where "bottom == 1",
// "top == 0". A pop_global could read the queue element in that case,
// then have the owner thread do a pop followed by another push. Without
// the incrementing of "tag", the pop_global's CAS could succeed,
// allowing it to believe it has claimed the stale element.)
// Perhaps a competing pop_global has already incremented "top", in which
// case it wins the element.
// No competing pop_global has yet incremented "top"; we'll try to
// install new_age, thus claiming the element.
// We win.
return true;
}
}
// We lose; a completing pop_global gets the element. But the queue is empty
// and top is greater than bottom. Fix this representation of the empty queue
// to become the canonical one.
return false;
}
template<class E, unsigned int N>
bool GenericTaskQueue<E, N>::pop_global(E& t) {
if (n_elems == 0) {
return false;
}
// Note that using "_bottom" here might fail, since a pop_local might
// have decremented it.
}
template<class E, unsigned int N>
GenericTaskQueue<E, N>::~GenericTaskQueue() {
FREE_C_HEAP_ARRAY(E, _elems);
}
// OverflowTaskQueue is a TaskQueue that also includes an overflow stack for
// elements that do not fit in the TaskQueue.
//
// This class hides two methods from super classes:
//
// push() - push onto the task queue or, if that fails, onto the overflow stack
// is_empty() - return true if both the TaskQueue and overflow stack are empty
//
// Note that size() is not hidden--it returns the number of elements in the
// TaskQueue, and does not include the size of the overflow stack. This
// simplifies replacement of GenericTaskQueues with OverflowTaskQueues.
template<class E, unsigned int N = TASKQUEUE_SIZE>
class OverflowTaskQueue: public GenericTaskQueue<E, N>
{
public:
typedef Stack<E> overflow_t;
typedef GenericTaskQueue<E, N> taskqueue_t;
// Push task t onto the queue or onto the overflow stack. Return true.
inline bool push(E t);
// Attempt to pop from the overflow stack; return true if anything was popped.
inline bool pop_overflow(E& t);
inline bool is_empty() const {
return taskqueue_empty() && overflow_empty();
}
private:
};
template <class E, unsigned int N>
bool OverflowTaskQueue<E, N>::push(E t)
{
if (!taskqueue_t::push(t)) {
overflow_stack()->push(t);
}
return true;
}
template <class E, unsigned int N>
bool OverflowTaskQueue<E, N>::pop_overflow(E& t)
{
if (overflow_empty()) return false;
t = overflow_stack()->pop();
return true;
}
class TaskQueueSetSuper: public CHeapObj {
protected:
static int randomParkAndMiller(int* seed0);
public:
// Returns "true" if some TaskQueue in the set contains a task.
virtual bool peek() = 0;
};
template<class T>
class GenericTaskQueueSet: public TaskQueueSetSuper {
private:
T** _queues;
public:
typedef typename T::element_type E;
GenericTaskQueueSet(int n) : _n(n) {
typedef T* GenericTaskQueuePtr;
for (int i = 0; i < n; i++) {
}
}
void register_queue(uint i, T* q);
// The thread with queue number "queue_num" (and whose random number seed is
// at "seed") is trying to steal a task from some other queue. (It may try
// several queues, according to some configuration parameter.) If some steal
// succeeds, returns "true" and sets "t" to the stolen task, otherwise returns
// false.
bool peek();
};
template<class T> void
_queues[i] = q;
}
template<class T> T*
return _queues[i];
}
template<class T> bool
return true;
}
}
return false;
}
template<class T> bool
if (_n > 2) {
int best_k;
if (k == queue_num) continue;
best_k = k;
}
}
} else if (_n == 2) {
// Just try the other one.
return _queues[k]->pop_global(t);
} else {
return false;
}
}
template<class T> bool
if (_n > 2) {
} else if (_n == 2) {
// Just try the other one.
return _queues[k]->pop_global(t);
} else {
return false;
}
}
template<class T> bool
if (_n > 2) {
// Sample both and try the larger.
} else if (_n == 2) {
// Just try the other one.
return _queues[k]->pop_global(t);
} else {
return false;
}
}
template<class T>
bool GenericTaskQueueSet<T>::peek() {
// Try all the queues.
return true;
}
return false;
}
// When to terminate from the termination protocol.
class TerminatorTerminator: public CHeapObj {
public:
virtual bool should_exit_termination() = 0;
};
// A class to aid in the termination of a set of parallel tasks using
// TaskQueueSet's for work stealing.
class ParallelTaskTerminator: public StackObj {
private:
int _n_threads;
int _offered_termination;
#ifdef TRACESPINNING
static uint _total_yields;
static uint _total_spins;
static uint _total_peeks;
#endif
bool peek_in_queue_set();
protected:
virtual void yield();
public:
// "n_threads" is the number of threads to be terminated. "queue_set" is a
// queue sets of work queues of other threads.
// The current thread has no work, and is ready to terminate if everyone
// else is. If returns "true", all threads are terminated. If returns
// "false", available work has been observed in one of the task queues,
// so the global task is not complete.
bool offer_termination() {
return offer_termination(NULL);
}
// As above, but it also terminates if the should_exit_termination()
// method of the terminator parameter returns true. If terminator is
// NULL, then it is ignored.
// Reset the terminator, so that it may be reused again.
// The caller is responsible for ensuring that this is done
// in an MT-safe manner, once the previous round of use of
// the terminator is finished.
void reset_for_reuse();
// Same as above but the number of parallel threads is set to the
// given number.
void reset_for_reuse(int n_threads);
#ifdef TRACESPINNING
static void print_termination_counts();
#endif
};
template<class E, unsigned int N> inline bool
GenericTaskQueue<E, N>::push(E t) {
if (dirty_n_elems < max_elems()) {
// g++ complains if the volatile result of the assignment is unused.
return true;
} else {
return push_slow(t, dirty_n_elems);
}
}
template<class E, unsigned int N> inline bool
GenericTaskQueue<E, N>::pop_local(E& t) {
// This value cannot be N-1. That can only occur as a result of
// the assignment to bottom in this method. If it does, this method
// resets the size to 0 before the next call (which is sequential,
// since this is pop_local.)
if (dirty_n_elems == 0) return false;
// This is necessary to prevent any read below from being reordered
// before the store just above.
OrderAccess::fence();
// This is a second read of "age"; the "size()" above is the first.
// If there's still at least one element in the queue, based on the
// "_bottom" and "age" we've read, then there can be no interference with
// a "pop_global" operation, and we're done.
return true;
} else {
// Otherwise, the queue contained exactly one element; we take the slow
// path.
}
}
#ifdef _MSC_VER
// warning C4522: multiple assignment operators specified
#endif
// This is a container class for either an oop* or a narrowOop*.
// Both are pushed onto a task queue and the consumer will test is_narrow()
// to determine which should be processed.
class StarTask {
void* _holder; // either union oop* or narrowOop*
enum { COMPRESSED_OOP_MASK = 1 };
public:
}
_holder = (void*)p;
}
operator narrowOop*() {
}
return *this;
}
return *this;
}
bool is_narrow() const {
}
};
class ObjArrayTask
{
public:
}
ObjArrayTask& operator =(const ObjArrayTask& t) {
return *this;
}
volatile ObjArrayTask&
operator =(const volatile ObjArrayTask& t) volatile {
return *this;
}
private:
int _index;
};
#ifdef _MSC_VER
#endif
#endif // SHARE_VM_UTILITIES_TASKQUEUE_HPP