Tue, 11 May 2010 14:35:43 -0700
6931180: Migration to recent versions of MS Platform SDK
6951582: Build problems on win64
Summary: Changes to enable building JDK7 with Microsoft Visual Studio 2010
Reviewed-by: ohair, art, ccheung, dcubed
duke@435 | 1 | /* |
xdono@1014 | 2 | * Copyright 2001-2009 Sun Microsystems, Inc. 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 | * |
duke@435 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@435 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@435 | 21 | * have any questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
jcoomes@1746 | 25 | template <unsigned int N> |
duke@435 | 26 | class TaskQueueSuper: public CHeapObj { |
duke@435 | 27 | protected: |
jcoomes@1342 | 28 | // Internal type for indexing the queue; also used for the tag. |
jcoomes@1342 | 29 | typedef NOT_LP64(uint16_t) LP64_ONLY(uint32_t) idx_t; |
jcoomes@1342 | 30 | |
jcoomes@1342 | 31 | // The first free element after the last one pushed (mod N). |
ysr@976 | 32 | volatile uint _bottom; |
duke@435 | 33 | |
jcoomes@1746 | 34 | enum { MOD_N_MASK = N - 1 }; |
duke@435 | 35 | |
jcoomes@1342 | 36 | class Age { |
jcoomes@1342 | 37 | public: |
jcoomes@1342 | 38 | Age(size_t data = 0) { _data = data; } |
jcoomes@1342 | 39 | Age(const Age& age) { _data = age._data; } |
jcoomes@1342 | 40 | Age(idx_t top, idx_t tag) { _fields._top = top; _fields._tag = tag; } |
duke@435 | 41 | |
jcoomes@1342 | 42 | Age get() const volatile { return _data; } |
jcoomes@1342 | 43 | void set(Age age) volatile { _data = age._data; } |
duke@435 | 44 | |
jcoomes@1342 | 45 | idx_t top() const volatile { return _fields._top; } |
jcoomes@1342 | 46 | idx_t tag() const volatile { return _fields._tag; } |
duke@435 | 47 | |
jcoomes@1342 | 48 | // Increment top; if it wraps, increment tag also. |
jcoomes@1342 | 49 | void increment() { |
jcoomes@1342 | 50 | _fields._top = increment_index(_fields._top); |
jcoomes@1342 | 51 | if (_fields._top == 0) ++_fields._tag; |
jcoomes@1342 | 52 | } |
duke@435 | 53 | |
jcoomes@1342 | 54 | Age cmpxchg(const Age new_age, const Age old_age) volatile { |
jcoomes@1342 | 55 | return (size_t) Atomic::cmpxchg_ptr((intptr_t)new_age._data, |
jcoomes@1342 | 56 | (volatile intptr_t *)&_data, |
jcoomes@1342 | 57 | (intptr_t)old_age._data); |
duke@435 | 58 | } |
jcoomes@1342 | 59 | |
jcoomes@1342 | 60 | bool operator ==(const Age& other) const { return _data == other._data; } |
jcoomes@1342 | 61 | |
jcoomes@1342 | 62 | private: |
jcoomes@1342 | 63 | struct fields { |
jcoomes@1342 | 64 | idx_t _top; |
jcoomes@1342 | 65 | idx_t _tag; |
jcoomes@1342 | 66 | }; |
jcoomes@1342 | 67 | union { |
jcoomes@1342 | 68 | size_t _data; |
jcoomes@1342 | 69 | fields _fields; |
jcoomes@1342 | 70 | }; |
duke@435 | 71 | }; |
jcoomes@1342 | 72 | |
jcoomes@1342 | 73 | volatile Age _age; |
jcoomes@1342 | 74 | |
jcoomes@1342 | 75 | // These both operate mod N. |
jcoomes@1342 | 76 | static uint increment_index(uint ind) { |
jcoomes@1342 | 77 | return (ind + 1) & MOD_N_MASK; |
duke@435 | 78 | } |
jcoomes@1342 | 79 | static uint decrement_index(uint ind) { |
jcoomes@1342 | 80 | return (ind - 1) & MOD_N_MASK; |
duke@435 | 81 | } |
duke@435 | 82 | |
jcoomes@1342 | 83 | // Returns a number in the range [0..N). If the result is "N-1", it should be |
jcoomes@1342 | 84 | // interpreted as 0. |
jcoomes@1746 | 85 | uint dirty_size(uint bot, uint top) const { |
jcoomes@1342 | 86 | return (bot - top) & MOD_N_MASK; |
duke@435 | 87 | } |
duke@435 | 88 | |
duke@435 | 89 | // Returns the size corresponding to the given "bot" and "top". |
jcoomes@1746 | 90 | uint size(uint bot, uint top) const { |
ysr@976 | 91 | uint sz = dirty_size(bot, top); |
jcoomes@1342 | 92 | // Has the queue "wrapped", so that bottom is less than top? There's a |
jcoomes@1342 | 93 | // complicated special case here. A pair of threads could perform pop_local |
jcoomes@1342 | 94 | // and pop_global operations concurrently, starting from a state in which |
jcoomes@1342 | 95 | // _bottom == _top+1. The pop_local could succeed in decrementing _bottom, |
jcoomes@1342 | 96 | // and the pop_global in incrementing _top (in which case the pop_global |
jcoomes@1342 | 97 | // will be awarded the contested queue element.) The resulting state must |
jcoomes@1342 | 98 | // be interpreted as an empty queue. (We only need to worry about one such |
jcoomes@1342 | 99 | // event: only the queue owner performs pop_local's, and several concurrent |
jcoomes@1342 | 100 | // threads attempting to perform the pop_global will all perform the same |
jcoomes@1342 | 101 | // CAS, and only one can succeed.) Any stealing thread that reads after |
jcoomes@1342 | 102 | // either the increment or decrement will see an empty queue, and will not |
jcoomes@1342 | 103 | // join the competitors. The "sz == -1 || sz == N-1" state will not be |
jcoomes@1342 | 104 | // modified by concurrent queues, so the owner thread can reset the state to |
jcoomes@1342 | 105 | // _bottom == top so subsequent pushes will be performed normally. |
jcoomes@1342 | 106 | return (sz == N - 1) ? 0 : sz; |
duke@435 | 107 | } |
duke@435 | 108 | |
duke@435 | 109 | public: |
duke@435 | 110 | TaskQueueSuper() : _bottom(0), _age() {} |
duke@435 | 111 | |
jcoomes@1746 | 112 | // Return true if the TaskQueue contains any tasks. |
jcoomes@1746 | 113 | bool peek() { return _bottom != _age.top(); } |
duke@435 | 114 | |
duke@435 | 115 | // Return an estimate of the number of elements in the queue. |
duke@435 | 116 | // The "careful" version admits the possibility of pop_local/pop_global |
duke@435 | 117 | // races. |
jcoomes@1746 | 118 | uint size() const { |
jcoomes@1342 | 119 | return size(_bottom, _age.top()); |
duke@435 | 120 | } |
duke@435 | 121 | |
jcoomes@1746 | 122 | uint dirty_size() const { |
jcoomes@1342 | 123 | return dirty_size(_bottom, _age.top()); |
duke@435 | 124 | } |
duke@435 | 125 | |
ysr@777 | 126 | void set_empty() { |
ysr@777 | 127 | _bottom = 0; |
jcoomes@1342 | 128 | _age.set(0); |
ysr@777 | 129 | } |
ysr@777 | 130 | |
duke@435 | 131 | // Maximum number of elements allowed in the queue. This is two less |
duke@435 | 132 | // than the actual queue size, for somewhat complicated reasons. |
jcoomes@1746 | 133 | uint max_elems() const { return N - 2; } |
jmasa@1719 | 134 | |
jmasa@1719 | 135 | // Total size of queue. |
jmasa@1719 | 136 | static const uint total_size() { return N; } |
duke@435 | 137 | }; |
duke@435 | 138 | |
jcoomes@1746 | 139 | template<class E, unsigned int N = TASKQUEUE_SIZE> |
jcoomes@1746 | 140 | class GenericTaskQueue: public TaskQueueSuper<N> { |
jcoomes@1746 | 141 | protected: |
jcoomes@1746 | 142 | typedef typename TaskQueueSuper<N>::Age Age; |
jcoomes@1746 | 143 | typedef typename TaskQueueSuper<N>::idx_t idx_t; |
jcoomes@1746 | 144 | |
jcoomes@1746 | 145 | using TaskQueueSuper<N>::_bottom; |
jcoomes@1746 | 146 | using TaskQueueSuper<N>::_age; |
jcoomes@1746 | 147 | using TaskQueueSuper<N>::increment_index; |
jcoomes@1746 | 148 | using TaskQueueSuper<N>::decrement_index; |
jcoomes@1746 | 149 | using TaskQueueSuper<N>::dirty_size; |
jcoomes@1746 | 150 | |
jcoomes@1746 | 151 | public: |
jcoomes@1746 | 152 | using TaskQueueSuper<N>::max_elems; |
jcoomes@1746 | 153 | using TaskQueueSuper<N>::size; |
jcoomes@1746 | 154 | |
duke@435 | 155 | private: |
duke@435 | 156 | // Slow paths for push, pop_local. (pop_global has no fast path.) |
ysr@976 | 157 | bool push_slow(E t, uint dirty_n_elems); |
ysr@976 | 158 | bool pop_local_slow(uint localBot, Age oldAge); |
duke@435 | 159 | |
duke@435 | 160 | public: |
jcoomes@1746 | 161 | typedef E element_type; |
jcoomes@1746 | 162 | |
duke@435 | 163 | // Initializes the queue to empty. |
duke@435 | 164 | GenericTaskQueue(); |
duke@435 | 165 | |
duke@435 | 166 | void initialize(); |
duke@435 | 167 | |
duke@435 | 168 | // Push the task "t" on the queue. Returns "false" iff the queue is |
duke@435 | 169 | // full. |
duke@435 | 170 | inline bool push(E t); |
duke@435 | 171 | |
duke@435 | 172 | // If succeeds in claiming a task (from the 'local' end, that is, the |
duke@435 | 173 | // most recently pushed task), returns "true" and sets "t" to that task. |
duke@435 | 174 | // Otherwise, the queue is empty and returns false. |
duke@435 | 175 | inline bool pop_local(E& t); |
duke@435 | 176 | |
duke@435 | 177 | // If succeeds in claiming a task (from the 'global' end, that is, the |
duke@435 | 178 | // least recently pushed task), returns "true" and sets "t" to that task. |
duke@435 | 179 | // Otherwise, the queue is empty and returns false. |
duke@435 | 180 | bool pop_global(E& t); |
duke@435 | 181 | |
duke@435 | 182 | // Delete any resource associated with the queue. |
duke@435 | 183 | ~GenericTaskQueue(); |
duke@435 | 184 | |
ysr@777 | 185 | // apply the closure to all elements in the task queue |
ysr@777 | 186 | void oops_do(OopClosure* f); |
ysr@777 | 187 | |
duke@435 | 188 | private: |
duke@435 | 189 | // Element array. |
duke@435 | 190 | volatile E* _elems; |
duke@435 | 191 | }; |
duke@435 | 192 | |
jcoomes@1746 | 193 | template<class E, unsigned int N> |
jcoomes@1746 | 194 | GenericTaskQueue<E, N>::GenericTaskQueue() { |
jcoomes@1342 | 195 | assert(sizeof(Age) == sizeof(size_t), "Depends on this."); |
duke@435 | 196 | } |
duke@435 | 197 | |
jcoomes@1746 | 198 | template<class E, unsigned int N> |
jcoomes@1746 | 199 | void GenericTaskQueue<E, N>::initialize() { |
jcoomes@1342 | 200 | _elems = NEW_C_HEAP_ARRAY(E, N); |
duke@435 | 201 | guarantee(_elems != NULL, "Allocation failed."); |
duke@435 | 202 | } |
duke@435 | 203 | |
jcoomes@1746 | 204 | template<class E, unsigned int N> |
jcoomes@1746 | 205 | void GenericTaskQueue<E, N>::oops_do(OopClosure* f) { |
ysr@777 | 206 | // tty->print_cr("START OopTaskQueue::oops_do"); |
ysr@976 | 207 | uint iters = size(); |
ysr@976 | 208 | uint index = _bottom; |
ysr@976 | 209 | for (uint i = 0; i < iters; ++i) { |
ysr@777 | 210 | index = decrement_index(index); |
ysr@777 | 211 | // tty->print_cr(" doing entry %d," INTPTR_T " -> " INTPTR_T, |
ysr@777 | 212 | // index, &_elems[index], _elems[index]); |
ysr@777 | 213 | E* t = (E*)&_elems[index]; // cast away volatility |
ysr@777 | 214 | oop* p = (oop*)t; |
ysr@777 | 215 | assert((*t)->is_oop_or_null(), "Not an oop or null"); |
ysr@777 | 216 | f->do_oop(p); |
ysr@777 | 217 | } |
ysr@777 | 218 | // tty->print_cr("END OopTaskQueue::oops_do"); |
ysr@777 | 219 | } |
ysr@777 | 220 | |
jcoomes@1746 | 221 | template<class E, unsigned int N> |
jcoomes@1746 | 222 | bool GenericTaskQueue<E, N>::push_slow(E t, uint dirty_n_elems) { |
jcoomes@1342 | 223 | if (dirty_n_elems == N - 1) { |
duke@435 | 224 | // Actually means 0, so do the push. |
ysr@976 | 225 | uint localBot = _bottom; |
jcoomes@1746 | 226 | // g++ complains if the volatile result of the assignment is unused. |
jcoomes@1746 | 227 | const_cast<E&>(_elems[localBot] = t); |
bobv@1459 | 228 | OrderAccess::release_store(&_bottom, increment_index(localBot)); |
duke@435 | 229 | return true; |
jcoomes@1342 | 230 | } |
jcoomes@1342 | 231 | return false; |
duke@435 | 232 | } |
duke@435 | 233 | |
jcoomes@1746 | 234 | template<class E, unsigned int N> |
jcoomes@1746 | 235 | bool GenericTaskQueue<E, N>:: |
ysr@976 | 236 | pop_local_slow(uint localBot, Age oldAge) { |
duke@435 | 237 | // This queue was observed to contain exactly one element; either this |
duke@435 | 238 | // thread will claim it, or a competing "pop_global". In either case, |
duke@435 | 239 | // the queue will be logically empty afterwards. Create a new Age value |
duke@435 | 240 | // that represents the empty queue for the given value of "_bottom". (We |
duke@435 | 241 | // must also increment "tag" because of the case where "bottom == 1", |
duke@435 | 242 | // "top == 0". A pop_global could read the queue element in that case, |
duke@435 | 243 | // then have the owner thread do a pop followed by another push. Without |
duke@435 | 244 | // the incrementing of "tag", the pop_global's CAS could succeed, |
duke@435 | 245 | // allowing it to believe it has claimed the stale element.) |
jcoomes@1342 | 246 | Age newAge((idx_t)localBot, oldAge.tag() + 1); |
duke@435 | 247 | // Perhaps a competing pop_global has already incremented "top", in which |
duke@435 | 248 | // case it wins the element. |
duke@435 | 249 | if (localBot == oldAge.top()) { |
duke@435 | 250 | // No competing pop_global has yet incremented "top"; we'll try to |
duke@435 | 251 | // install new_age, thus claiming the element. |
jcoomes@1342 | 252 | Age tempAge = _age.cmpxchg(newAge, oldAge); |
duke@435 | 253 | if (tempAge == oldAge) { |
duke@435 | 254 | // We win. |
jcoomes@1342 | 255 | assert(dirty_size(localBot, _age.top()) != N - 1, "sanity"); |
duke@435 | 256 | return true; |
duke@435 | 257 | } |
duke@435 | 258 | } |
jcoomes@1342 | 259 | // We lose; a completing pop_global gets the element. But the queue is empty |
jcoomes@1342 | 260 | // and top is greater than bottom. Fix this representation of the empty queue |
jcoomes@1342 | 261 | // to become the canonical one. |
jcoomes@1342 | 262 | _age.set(newAge); |
jcoomes@1342 | 263 | assert(dirty_size(localBot, _age.top()) != N - 1, "sanity"); |
duke@435 | 264 | return false; |
duke@435 | 265 | } |
duke@435 | 266 | |
jcoomes@1746 | 267 | template<class E, unsigned int N> |
jcoomes@1746 | 268 | bool GenericTaskQueue<E, N>::pop_global(E& t) { |
jcoomes@1342 | 269 | Age oldAge = _age.get(); |
ysr@976 | 270 | uint localBot = _bottom; |
ysr@976 | 271 | uint n_elems = size(localBot, oldAge.top()); |
duke@435 | 272 | if (n_elems == 0) { |
duke@435 | 273 | return false; |
duke@435 | 274 | } |
jcoomes@1342 | 275 | |
jcoomes@1746 | 276 | const_cast<E&>(t = _elems[oldAge.top()]); |
jcoomes@1342 | 277 | Age newAge(oldAge); |
jcoomes@1342 | 278 | newAge.increment(); |
jcoomes@1342 | 279 | Age resAge = _age.cmpxchg(newAge, oldAge); |
jcoomes@1342 | 280 | |
duke@435 | 281 | // Note that using "_bottom" here might fail, since a pop_local might |
duke@435 | 282 | // have decremented it. |
jcoomes@1342 | 283 | assert(dirty_size(localBot, newAge.top()) != N - 1, "sanity"); |
jcoomes@1342 | 284 | return resAge == oldAge; |
duke@435 | 285 | } |
duke@435 | 286 | |
jcoomes@1746 | 287 | template<class E, unsigned int N> |
jcoomes@1746 | 288 | GenericTaskQueue<E, N>::~GenericTaskQueue() { |
duke@435 | 289 | FREE_C_HEAP_ARRAY(E, _elems); |
duke@435 | 290 | } |
duke@435 | 291 | |
duke@435 | 292 | // Inherits the typedef of "Task" from above. |
duke@435 | 293 | class TaskQueueSetSuper: public CHeapObj { |
duke@435 | 294 | protected: |
duke@435 | 295 | static int randomParkAndMiller(int* seed0); |
duke@435 | 296 | public: |
duke@435 | 297 | // Returns "true" if some TaskQueue in the set contains a task. |
duke@435 | 298 | virtual bool peek() = 0; |
duke@435 | 299 | }; |
duke@435 | 300 | |
jcoomes@1746 | 301 | template<class T> |
jcoomes@1746 | 302 | class GenericTaskQueueSet: public TaskQueueSetSuper { |
duke@435 | 303 | private: |
ysr@976 | 304 | uint _n; |
jcoomes@1746 | 305 | T** _queues; |
duke@435 | 306 | |
duke@435 | 307 | public: |
jcoomes@1746 | 308 | typedef typename T::element_type E; |
jcoomes@1746 | 309 | |
duke@435 | 310 | GenericTaskQueueSet(int n) : _n(n) { |
jcoomes@1746 | 311 | typedef T* GenericTaskQueuePtr; |
duke@435 | 312 | _queues = NEW_C_HEAP_ARRAY(GenericTaskQueuePtr, n); |
duke@435 | 313 | for (int i = 0; i < n; i++) { |
duke@435 | 314 | _queues[i] = NULL; |
duke@435 | 315 | } |
duke@435 | 316 | } |
duke@435 | 317 | |
ysr@976 | 318 | bool steal_1_random(uint queue_num, int* seed, E& t); |
ysr@976 | 319 | bool steal_best_of_2(uint queue_num, int* seed, E& t); |
ysr@976 | 320 | bool steal_best_of_all(uint queue_num, int* seed, E& t); |
duke@435 | 321 | |
jcoomes@1746 | 322 | void register_queue(uint i, T* q); |
duke@435 | 323 | |
jcoomes@1746 | 324 | T* queue(uint n); |
duke@435 | 325 | |
duke@435 | 326 | // The thread with queue number "queue_num" (and whose random number seed |
duke@435 | 327 | // is at "seed") is trying to steal a task from some other queue. (It |
duke@435 | 328 | // may try several queues, according to some configuration parameter.) |
duke@435 | 329 | // If some steal succeeds, returns "true" and sets "t" the stolen task, |
duke@435 | 330 | // otherwise returns false. |
ysr@976 | 331 | bool steal(uint queue_num, int* seed, E& t); |
duke@435 | 332 | |
duke@435 | 333 | bool peek(); |
duke@435 | 334 | }; |
duke@435 | 335 | |
jcoomes@1746 | 336 | template<class T> void |
jcoomes@1746 | 337 | GenericTaskQueueSet<T>::register_queue(uint i, T* q) { |
ysr@976 | 338 | assert(i < _n, "index out of range."); |
duke@435 | 339 | _queues[i] = q; |
duke@435 | 340 | } |
duke@435 | 341 | |
jcoomes@1746 | 342 | template<class T> T* |
jcoomes@1746 | 343 | GenericTaskQueueSet<T>::queue(uint i) { |
duke@435 | 344 | return _queues[i]; |
duke@435 | 345 | } |
duke@435 | 346 | |
jcoomes@1746 | 347 | template<class T> bool |
jcoomes@1746 | 348 | GenericTaskQueueSet<T>::steal(uint queue_num, int* seed, E& t) { |
ysr@976 | 349 | for (uint i = 0; i < 2 * _n; i++) |
duke@435 | 350 | if (steal_best_of_2(queue_num, seed, t)) |
duke@435 | 351 | return true; |
duke@435 | 352 | return false; |
duke@435 | 353 | } |
duke@435 | 354 | |
jcoomes@1746 | 355 | template<class T> bool |
jcoomes@1746 | 356 | GenericTaskQueueSet<T>::steal_best_of_all(uint queue_num, int* seed, E& t) { |
duke@435 | 357 | if (_n > 2) { |
duke@435 | 358 | int best_k; |
ysr@976 | 359 | uint best_sz = 0; |
ysr@976 | 360 | for (uint k = 0; k < _n; k++) { |
duke@435 | 361 | if (k == queue_num) continue; |
ysr@976 | 362 | uint sz = _queues[k]->size(); |
duke@435 | 363 | if (sz > best_sz) { |
duke@435 | 364 | best_sz = sz; |
duke@435 | 365 | best_k = k; |
duke@435 | 366 | } |
duke@435 | 367 | } |
duke@435 | 368 | return best_sz > 0 && _queues[best_k]->pop_global(t); |
duke@435 | 369 | } else if (_n == 2) { |
duke@435 | 370 | // Just try the other one. |
duke@435 | 371 | int k = (queue_num + 1) % 2; |
duke@435 | 372 | return _queues[k]->pop_global(t); |
duke@435 | 373 | } else { |
duke@435 | 374 | assert(_n == 1, "can't be zero."); |
duke@435 | 375 | return false; |
duke@435 | 376 | } |
duke@435 | 377 | } |
duke@435 | 378 | |
jcoomes@1746 | 379 | template<class T> bool |
jcoomes@1746 | 380 | GenericTaskQueueSet<T>::steal_1_random(uint queue_num, int* seed, E& t) { |
duke@435 | 381 | if (_n > 2) { |
ysr@976 | 382 | uint k = queue_num; |
duke@435 | 383 | while (k == queue_num) k = randomParkAndMiller(seed) % _n; |
duke@435 | 384 | return _queues[2]->pop_global(t); |
duke@435 | 385 | } else if (_n == 2) { |
duke@435 | 386 | // Just try the other one. |
duke@435 | 387 | int k = (queue_num + 1) % 2; |
duke@435 | 388 | return _queues[k]->pop_global(t); |
duke@435 | 389 | } else { |
duke@435 | 390 | assert(_n == 1, "can't be zero."); |
duke@435 | 391 | return false; |
duke@435 | 392 | } |
duke@435 | 393 | } |
duke@435 | 394 | |
jcoomes@1746 | 395 | template<class T> bool |
jcoomes@1746 | 396 | GenericTaskQueueSet<T>::steal_best_of_2(uint queue_num, int* seed, E& t) { |
duke@435 | 397 | if (_n > 2) { |
ysr@976 | 398 | uint k1 = queue_num; |
duke@435 | 399 | while (k1 == queue_num) k1 = randomParkAndMiller(seed) % _n; |
ysr@976 | 400 | uint k2 = queue_num; |
duke@435 | 401 | while (k2 == queue_num || k2 == k1) k2 = randomParkAndMiller(seed) % _n; |
duke@435 | 402 | // Sample both and try the larger. |
ysr@976 | 403 | uint sz1 = _queues[k1]->size(); |
ysr@976 | 404 | uint sz2 = _queues[k2]->size(); |
duke@435 | 405 | if (sz2 > sz1) return _queues[k2]->pop_global(t); |
duke@435 | 406 | else return _queues[k1]->pop_global(t); |
duke@435 | 407 | } else if (_n == 2) { |
duke@435 | 408 | // Just try the other one. |
ysr@976 | 409 | uint k = (queue_num + 1) % 2; |
duke@435 | 410 | return _queues[k]->pop_global(t); |
duke@435 | 411 | } else { |
duke@435 | 412 | assert(_n == 1, "can't be zero."); |
duke@435 | 413 | return false; |
duke@435 | 414 | } |
duke@435 | 415 | } |
duke@435 | 416 | |
jcoomes@1746 | 417 | template<class T> |
jcoomes@1746 | 418 | bool GenericTaskQueueSet<T>::peek() { |
duke@435 | 419 | // Try all the queues. |
ysr@976 | 420 | for (uint j = 0; j < _n; j++) { |
duke@435 | 421 | if (_queues[j]->peek()) |
duke@435 | 422 | return true; |
duke@435 | 423 | } |
duke@435 | 424 | return false; |
duke@435 | 425 | } |
duke@435 | 426 | |
ysr@777 | 427 | // When to terminate from the termination protocol. |
ysr@777 | 428 | class TerminatorTerminator: public CHeapObj { |
ysr@777 | 429 | public: |
ysr@777 | 430 | virtual bool should_exit_termination() = 0; |
ysr@777 | 431 | }; |
ysr@777 | 432 | |
duke@435 | 433 | // A class to aid in the termination of a set of parallel tasks using |
duke@435 | 434 | // TaskQueueSet's for work stealing. |
duke@435 | 435 | |
jmasa@981 | 436 | #undef TRACESPINNING |
jmasa@981 | 437 | |
duke@435 | 438 | class ParallelTaskTerminator: public StackObj { |
duke@435 | 439 | private: |
duke@435 | 440 | int _n_threads; |
duke@435 | 441 | TaskQueueSetSuper* _queue_set; |
ysr@976 | 442 | int _offered_termination; |
duke@435 | 443 | |
jmasa@981 | 444 | #ifdef TRACESPINNING |
jmasa@981 | 445 | static uint _total_yields; |
jmasa@981 | 446 | static uint _total_spins; |
jmasa@981 | 447 | static uint _total_peeks; |
jmasa@981 | 448 | #endif |
jmasa@981 | 449 | |
duke@435 | 450 | bool peek_in_queue_set(); |
duke@435 | 451 | protected: |
duke@435 | 452 | virtual void yield(); |
duke@435 | 453 | void sleep(uint millis); |
duke@435 | 454 | |
duke@435 | 455 | public: |
duke@435 | 456 | |
duke@435 | 457 | // "n_threads" is the number of threads to be terminated. "queue_set" is a |
duke@435 | 458 | // queue sets of work queues of other threads. |
duke@435 | 459 | ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set); |
duke@435 | 460 | |
duke@435 | 461 | // The current thread has no work, and is ready to terminate if everyone |
duke@435 | 462 | // else is. If returns "true", all threads are terminated. If returns |
duke@435 | 463 | // "false", available work has been observed in one of the task queues, |
duke@435 | 464 | // so the global task is not complete. |
ysr@777 | 465 | bool offer_termination() { |
ysr@777 | 466 | return offer_termination(NULL); |
ysr@777 | 467 | } |
ysr@777 | 468 | |
jcoomes@1342 | 469 | // As above, but it also terminates if the should_exit_termination() |
ysr@777 | 470 | // method of the terminator parameter returns true. If terminator is |
ysr@777 | 471 | // NULL, then it is ignored. |
ysr@777 | 472 | bool offer_termination(TerminatorTerminator* terminator); |
duke@435 | 473 | |
duke@435 | 474 | // Reset the terminator, so that it may be reused again. |
duke@435 | 475 | // The caller is responsible for ensuring that this is done |
duke@435 | 476 | // in an MT-safe manner, once the previous round of use of |
duke@435 | 477 | // the terminator is finished. |
duke@435 | 478 | void reset_for_reuse(); |
duke@435 | 479 | |
jmasa@981 | 480 | #ifdef TRACESPINNING |
jmasa@981 | 481 | static uint total_yields() { return _total_yields; } |
jmasa@981 | 482 | static uint total_spins() { return _total_spins; } |
jmasa@981 | 483 | static uint total_peeks() { return _total_peeks; } |
jmasa@981 | 484 | static void print_termination_counts(); |
jmasa@981 | 485 | #endif |
duke@435 | 486 | }; |
duke@435 | 487 | |
jcoomes@1746 | 488 | template<class E, unsigned int N> inline bool |
jcoomes@1746 | 489 | GenericTaskQueue<E, N>::push(E t) { |
ysr@976 | 490 | uint localBot = _bottom; |
jcoomes@1342 | 491 | assert((localBot >= 0) && (localBot < N), "_bottom out of range."); |
jcoomes@1342 | 492 | idx_t top = _age.top(); |
ysr@976 | 493 | uint dirty_n_elems = dirty_size(localBot, top); |
jcoomes@1746 | 494 | assert(dirty_n_elems < N, "n_elems out of range."); |
duke@435 | 495 | if (dirty_n_elems < max_elems()) { |
jcoomes@1746 | 496 | // g++ complains if the volatile result of the assignment is unused. |
jcoomes@1746 | 497 | const_cast<E&>(_elems[localBot] = t); |
bobv@1459 | 498 | OrderAccess::release_store(&_bottom, increment_index(localBot)); |
duke@435 | 499 | return true; |
duke@435 | 500 | } else { |
duke@435 | 501 | return push_slow(t, dirty_n_elems); |
duke@435 | 502 | } |
duke@435 | 503 | } |
duke@435 | 504 | |
jcoomes@1746 | 505 | template<class E, unsigned int N> inline bool |
jcoomes@1746 | 506 | GenericTaskQueue<E, N>::pop_local(E& t) { |
ysr@976 | 507 | uint localBot = _bottom; |
jcoomes@1342 | 508 | // This value cannot be N-1. That can only occur as a result of |
duke@435 | 509 | // the assignment to bottom in this method. If it does, this method |
duke@435 | 510 | // resets the size( to 0 before the next call (which is sequential, |
duke@435 | 511 | // since this is pop_local.) |
jcoomes@1342 | 512 | uint dirty_n_elems = dirty_size(localBot, _age.top()); |
jcoomes@1342 | 513 | assert(dirty_n_elems != N - 1, "Shouldn't be possible..."); |
duke@435 | 514 | if (dirty_n_elems == 0) return false; |
duke@435 | 515 | localBot = decrement_index(localBot); |
duke@435 | 516 | _bottom = localBot; |
duke@435 | 517 | // This is necessary to prevent any read below from being reordered |
duke@435 | 518 | // before the store just above. |
duke@435 | 519 | OrderAccess::fence(); |
jcoomes@1746 | 520 | const_cast<E&>(t = _elems[localBot]); |
duke@435 | 521 | // This is a second read of "age"; the "size()" above is the first. |
duke@435 | 522 | // If there's still at least one element in the queue, based on the |
duke@435 | 523 | // "_bottom" and "age" we've read, then there can be no interference with |
duke@435 | 524 | // a "pop_global" operation, and we're done. |
jcoomes@1342 | 525 | idx_t tp = _age.top(); // XXX |
duke@435 | 526 | if (size(localBot, tp) > 0) { |
jcoomes@1342 | 527 | assert(dirty_size(localBot, tp) != N - 1, "sanity"); |
duke@435 | 528 | return true; |
duke@435 | 529 | } else { |
duke@435 | 530 | // Otherwise, the queue contained exactly one element; we take the slow |
duke@435 | 531 | // path. |
jcoomes@1342 | 532 | return pop_local_slow(localBot, _age.get()); |
duke@435 | 533 | } |
duke@435 | 534 | } |
duke@435 | 535 | |
duke@435 | 536 | typedef oop Task; |
jcoomes@1746 | 537 | typedef GenericTaskQueue<Task> OopTaskQueue; |
jcoomes@1746 | 538 | typedef GenericTaskQueueSet<OopTaskQueue> OopTaskQueueSet; |
duke@435 | 539 | |
jcoomes@1746 | 540 | #ifdef _MSC_VER |
jcoomes@1746 | 541 | #pragma warning(push) |
jcoomes@1746 | 542 | // warning C4522: multiple assignment operators specified |
jcoomes@1746 | 543 | #pragma warning(disable:4522) |
jcoomes@1746 | 544 | #endif |
coleenp@548 | 545 | |
coleenp@548 | 546 | // This is a container class for either an oop* or a narrowOop*. |
coleenp@548 | 547 | // Both are pushed onto a task queue and the consumer will test is_narrow() |
coleenp@548 | 548 | // to determine which should be processed. |
coleenp@548 | 549 | class StarTask { |
coleenp@548 | 550 | void* _holder; // either union oop* or narrowOop* |
jcoomes@1746 | 551 | |
jcoomes@1746 | 552 | enum { COMPRESSED_OOP_MASK = 1 }; |
jcoomes@1746 | 553 | |
coleenp@548 | 554 | public: |
ysr@1280 | 555 | StarTask(narrowOop* p) { |
ysr@1280 | 556 | assert(((uintptr_t)p & COMPRESSED_OOP_MASK) == 0, "Information loss!"); |
ysr@1280 | 557 | _holder = (void *)((uintptr_t)p | COMPRESSED_OOP_MASK); |
ysr@1280 | 558 | } |
ysr@1280 | 559 | StarTask(oop* p) { |
ysr@1280 | 560 | assert(((uintptr_t)p & COMPRESSED_OOP_MASK) == 0, "Information loss!"); |
ysr@1280 | 561 | _holder = (void*)p; |
ysr@1280 | 562 | } |
coleenp@548 | 563 | StarTask() { _holder = NULL; } |
coleenp@548 | 564 | operator oop*() { return (oop*)_holder; } |
coleenp@548 | 565 | operator narrowOop*() { |
coleenp@548 | 566 | return (narrowOop*)((uintptr_t)_holder & ~COMPRESSED_OOP_MASK); |
coleenp@548 | 567 | } |
coleenp@548 | 568 | |
jcoomes@1746 | 569 | StarTask& operator=(const StarTask& t) { |
jcoomes@1746 | 570 | _holder = t._holder; |
jcoomes@1746 | 571 | return *this; |
jcoomes@1746 | 572 | } |
jcoomes@1746 | 573 | volatile StarTask& operator=(const volatile StarTask& t) volatile { |
jcoomes@1746 | 574 | _holder = t._holder; |
jcoomes@1746 | 575 | return *this; |
jcoomes@1746 | 576 | } |
coleenp@548 | 577 | |
coleenp@548 | 578 | bool is_narrow() const { |
coleenp@548 | 579 | return (((uintptr_t)_holder & COMPRESSED_OOP_MASK) != 0); |
coleenp@548 | 580 | } |
coleenp@548 | 581 | }; |
coleenp@548 | 582 | |
jcoomes@1746 | 583 | class ObjArrayTask |
jcoomes@1746 | 584 | { |
jcoomes@1746 | 585 | public: |
jcoomes@1746 | 586 | ObjArrayTask(oop o = NULL, int idx = 0): _obj(o), _index(idx) { } |
jcoomes@1746 | 587 | ObjArrayTask(oop o, size_t idx): _obj(o), _index(int(idx)) { |
jcoomes@1746 | 588 | assert(idx <= size_t(max_jint), "too big"); |
jcoomes@1746 | 589 | } |
jcoomes@1746 | 590 | ObjArrayTask(const ObjArrayTask& t): _obj(t._obj), _index(t._index) { } |
jcoomes@1746 | 591 | |
jcoomes@1746 | 592 | ObjArrayTask& operator =(const ObjArrayTask& t) { |
jcoomes@1746 | 593 | _obj = t._obj; |
jcoomes@1746 | 594 | _index = t._index; |
jcoomes@1746 | 595 | return *this; |
jcoomes@1746 | 596 | } |
jcoomes@1746 | 597 | volatile ObjArrayTask& |
jcoomes@1746 | 598 | operator =(const volatile ObjArrayTask& t) volatile { |
jcoomes@1746 | 599 | _obj = t._obj; |
jcoomes@1746 | 600 | _index = t._index; |
jcoomes@1746 | 601 | return *this; |
jcoomes@1746 | 602 | } |
jcoomes@1746 | 603 | |
jcoomes@1746 | 604 | inline oop obj() const { return _obj; } |
jcoomes@1746 | 605 | inline int index() const { return _index; } |
jcoomes@1746 | 606 | |
jcoomes@1746 | 607 | DEBUG_ONLY(bool is_valid() const); // Tasks to be pushed/popped must be valid. |
jcoomes@1746 | 608 | |
jcoomes@1746 | 609 | private: |
jcoomes@1746 | 610 | oop _obj; |
jcoomes@1746 | 611 | int _index; |
jcoomes@1746 | 612 | }; |
jcoomes@1746 | 613 | |
jcoomes@1746 | 614 | #ifdef _MSC_VER |
jcoomes@1746 | 615 | #pragma warning(pop) |
jcoomes@1746 | 616 | #endif |
jcoomes@1746 | 617 | |
jcoomes@1746 | 618 | typedef GenericTaskQueue<StarTask> OopStarTaskQueue; |
jcoomes@1746 | 619 | typedef GenericTaskQueueSet<OopStarTaskQueue> OopStarTaskQueueSet; |
duke@435 | 620 | |
jcoomes@810 | 621 | typedef size_t RegionTask; // index for region |
jcoomes@1746 | 622 | typedef GenericTaskQueue<RegionTask> RegionTaskQueue; |
jcoomes@1746 | 623 | typedef GenericTaskQueueSet<RegionTaskQueue> RegionTaskQueueSet; |
duke@435 | 624 | |
jcoomes@810 | 625 | class RegionTaskQueueWithOverflow: public CHeapObj { |
duke@435 | 626 | protected: |
jcoomes@810 | 627 | RegionTaskQueue _region_queue; |
jcoomes@810 | 628 | GrowableArray<RegionTask>* _overflow_stack; |
duke@435 | 629 | |
duke@435 | 630 | public: |
jcoomes@810 | 631 | RegionTaskQueueWithOverflow() : _overflow_stack(NULL) {} |
duke@435 | 632 | // Initialize both stealable queue and overflow |
duke@435 | 633 | void initialize(); |
duke@435 | 634 | // Save first to stealable queue and then to overflow |
jcoomes@810 | 635 | void save(RegionTask t); |
duke@435 | 636 | // Retrieve first from overflow and then from stealable queue |
jcoomes@810 | 637 | bool retrieve(RegionTask& region_index); |
duke@435 | 638 | // Retrieve from stealable queue |
jcoomes@810 | 639 | bool retrieve_from_stealable_queue(RegionTask& region_index); |
duke@435 | 640 | // Retrieve from overflow |
jcoomes@810 | 641 | bool retrieve_from_overflow(RegionTask& region_index); |
duke@435 | 642 | bool is_empty(); |
duke@435 | 643 | bool stealable_is_empty(); |
duke@435 | 644 | bool overflow_is_empty(); |
ysr@976 | 645 | uint stealable_size() { return _region_queue.size(); } |
jcoomes@810 | 646 | RegionTaskQueue* task_queue() { return &_region_queue; } |
duke@435 | 647 | }; |
duke@435 | 648 | |
jcoomes@810 | 649 | #define USE_RegionTaskQueueWithOverflow |