src/share/vm/utilities/taskqueue.hpp

Thu, 19 Mar 2015 19:53:34 +0100

author
zmajo
date
Thu, 19 Mar 2015 19:53:34 +0100
changeset 7638
aefa2e84b424
parent 6911
ce8f6bb717c9
child 7535
7ae4e26cb1e0
child 8611
a753c8401458
permissions
-rw-r--r--

8074869: C2 code generator can replace -0.0f with +0.0f on Linux
Summary: Instead of 'fpclass', use cast float->int and double->long to check if value is +0.0f and +0.0d, respectively.
Reviewed-by: kvn, simonis, dlong

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

mercurial