taskqueue.hpp revision 0
/*
* Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved.
* 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
class TaskQueueSuper: public CHeapObj {
protected:
// The first free element after the last one pushed (mod _n).
// (For now we'll assume only 32-bit CAS).
// log2 of the size of the queue.
enum SomeProtectedConstants {
Log_n = 14
};
// Size of the queue.
// For computing "x mod n" efficiently.
struct Age {
}
};
// These make sure we do single atomic reads and writes.
}
}
}
// 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 seen 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.
if (sz == (n()-1)) return 0;
else return sz;
}
public:
// Return "true" if the TaskQueue contains any tasks.
bool peek();
// Return an estimate of the number of elements in the queue.
// The "careful" version admits the possibility of pop_local/pop_global
// races.
}
juint dirty_size() {
}
// Maximum number of elements allowed in the queue. This is two less
// than the actual queue size, for somewhat complicated reasons.
};
template<class E> class GenericTaskQueue: public TaskQueueSuper {
private:
// Slow paths for push, pop_local. (pop_global has no fast path.)
public:
// 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);
// If succeeds in claiming a task (from the 'local' end, that is, the
// most recently pushed task), returns "true" and sets "t" to that task.
// Otherwise, the queue is empty and returns false.
inline bool pop_local(E& t);
// If succeeds in claiming a task (from the 'global' end, that is, the
// least recently pushed task), returns "true" and sets "t" to that task.
// Otherwise, the queue is empty and returns false.
bool pop_global(E& t);
// Delete any resource associated with the queue.
~GenericTaskQueue();
private:
// Element array.
volatile E* _elems;
};
template<class E>
}
template<class E>
void GenericTaskQueue<E>::initialize() {
_elems = NEW_C_HEAP_ARRAY(E, n());
}
template<class E>
if (dirty_n_elems == n() - 1) {
// Actually means 0, so do the push.
return true;
} else
return false;
}
template<class E>
bool GenericTaskQueue<E>::
// 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.
"Assumption about CAS unit.");
// We win.
"Shouldn't be possible...");
return true;
}
}
// We fail; 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.
"Shouldn't be possible...");
return false;
}
template<class E>
bool GenericTaskQueue<E>::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.
"Shouldn't be possible...");
}
template<class E>
GenericTaskQueue<E>::~GenericTaskQueue() {
FREE_C_HEAP_ARRAY(E, _elems);
}
// Inherits the typedef of "Task" from above.
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 E> class GenericTaskQueueSet: public TaskQueueSetSuper {
private:
int _n;
GenericTaskQueue<E>** _queues;
public:
GenericTaskQueueSet(int n) : _n(n) {
typedef GenericTaskQueue<E>* GenericTaskQueuePtr;
for (int i = 0; i < n; i++) {
}
}
void register_queue(int i, GenericTaskQueue<E>* q);
GenericTaskQueue<E>* queue(int n);
// 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" the stolen task,
// otherwise returns false.
bool peek();
};
template<class E>
_queues[i] = q;
}
template<class E>
return _queues[i];
}
template<class E>
for (int i = 0; i < 2 * _n; i++)
return true;
return false;
}
template<class E>
if (_n > 2) {
int best_k;
for (int k = 0; k < _n; 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 E>
if (_n > 2) {
int k = queue_num;
} else if (_n == 2) {
// Just try the other one.
return _queues[k]->pop_global(t);
} else {
return false;
}
}
template<class E>
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 E>
bool GenericTaskQueueSet<E>::peek() {
// Try all the queues.
for (int j = 0; j < _n; j++) {
return true;
}
return false;
}
// 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;
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();
// 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();
};
#define SIMPLE_STACK 0
template<class E> inline bool GenericTaskQueue<E>::push(E t) {
#if SIMPLE_STACK
return true;
} else {
return false;
}
#else
"n_elems out of range.");
if (dirty_n_elems < max_elems()) {
return true;
} else {
return push_slow(t, dirty_n_elems);
}
#endif
}
template<class E> inline bool GenericTaskQueue<E>::pop_local(E& t) {
#if SIMPLE_STACK
localBot--;
return true;
#else
// 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.
"Shouldn't be possible...");
return true;
} else {
// Otherwise, the queue contained exactly one element; we take the slow
// path.
}
#endif
}
class ChunkTaskQueueWithOverflow: public CHeapObj {
protected:
public:
// Initialize both stealable queue and overflow
void initialize();
// Save first to stealable queue and then to overflow
// Retrieve first from overflow and then from stealable queue
// Retrieve from stealable queue
// Retrieve from overflow
bool is_empty();
bool stealable_is_empty();
bool overflow_is_empty();
};
#define USE_ChunkTaskQueueWithOverflow