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