author | dl |
Mon, 18 Apr 2011 15:50:18 +0100 | |
changeset 9278 | f4672926fe4c |
parent 9242 | ef138d47df58 |
child 11279 | d9dab5ec5044 |
permissions | -rw-r--r-- |
4110 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
4110 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
4110 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
4110 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
8765
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
4110 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
37 |
||
38 |
import java.util.Collection; |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
39 |
import java.util.concurrent.RejectedExecutionException; |
4110 | 40 |
|
41 |
/** |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
42 |
* A thread managed by a {@link ForkJoinPool}, which executes |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
43 |
* {@link ForkJoinTask}s. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
44 |
* This class is subclassable solely for the sake of adding |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
45 |
* functionality -- there are no overridable methods dealing with |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
46 |
* scheduling or execution. However, you can override initialization |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
47 |
* and termination methods surrounding the main task processing loop. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
48 |
* If you do create such a subclass, you will also need to supply a |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
49 |
* custom {@link ForkJoinPool.ForkJoinWorkerThreadFactory} to use it |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
50 |
* in a {@code ForkJoinPool}. |
4110 | 51 |
* |
52 |
* @since 1.7 |
|
53 |
* @author Doug Lea |
|
54 |
*/ |
|
55 |
public class ForkJoinWorkerThread extends Thread { |
|
56 |
/* |
|
6543 | 57 |
* Overview: |
58 |
* |
|
59 |
* ForkJoinWorkerThreads are managed by ForkJoinPools and perform |
|
60 |
* ForkJoinTasks. This class includes bookkeeping in support of |
|
61 |
* worker activation, suspension, and lifecycle control described |
|
62 |
* in more detail in the internal documentation of class |
|
63 |
* ForkJoinPool. And as described further below, this class also |
|
64 |
* includes special-cased support for some ForkJoinTask |
|
65 |
* methods. But the main mechanics involve work-stealing: |
|
4110 | 66 |
* |
6543 | 67 |
* Work-stealing queues are special forms of Deques that support |
68 |
* only three of the four possible end-operations -- push, pop, |
|
69 |
* and deq (aka steal), under the further constraints that push |
|
70 |
* and pop are called only from the owning thread, while deq may |
|
71 |
* be called from other threads. (If you are unfamiliar with |
|
72 |
* them, you probably want to read Herlihy and Shavit's book "The |
|
73 |
* Art of Multiprocessor programming", chapter 16 describing these |
|
74 |
* in more detail before proceeding.) The main work-stealing |
|
75 |
* queue design is roughly similar to those in the papers "Dynamic |
|
76 |
* Circular Work-Stealing Deque" by Chase and Lev, SPAA 2005 |
|
77 |
* (http://research.sun.com/scalable/pubs/index.html) and |
|
78 |
* "Idempotent work stealing" by Michael, Saraswat, and Vechev, |
|
79 |
* PPoPP 2009 (http://portal.acm.org/citation.cfm?id=1504186). |
|
80 |
* The main differences ultimately stem from gc requirements that |
|
81 |
* we null out taken slots as soon as we can, to maintain as small |
|
82 |
* a footprint as possible even in programs generating huge |
|
83 |
* numbers of tasks. To accomplish this, we shift the CAS |
|
84 |
* arbitrating pop vs deq (steal) from being on the indices |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
85 |
* ("queueBase" and "queueTop") to the slots themselves (mainly |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
86 |
* via method "casSlotNull()"). So, both a successful pop and deq |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
87 |
* mainly entail a CAS of a slot from non-null to null. Because |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
88 |
* we rely on CASes of references, we do not need tag bits on |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
89 |
* queueBase or queueTop. They are simple ints as used in any |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
90 |
* circular array-based queue (see for example ArrayDeque). |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
91 |
* Updates to the indices must still be ordered in a way that |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
92 |
* guarantees that queueTop == queueBase means the queue is empty, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
93 |
* but otherwise may err on the side of possibly making the queue |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
94 |
* appear nonempty when a push, pop, or deq have not fully |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
95 |
* committed. Note that this means that the deq operation, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
96 |
* considered individually, is not wait-free. One thief cannot |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
97 |
* successfully continue until another in-progress one (or, if |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
98 |
* previously empty, a push) completes. However, in the |
6543 | 99 |
* aggregate, we ensure at least probabilistic non-blockingness. |
100 |
* If an attempted steal fails, a thief always chooses a different |
|
101 |
* random victim target to try next. So, in order for one thief to |
|
102 |
* progress, it suffices for any in-progress deq or new push on |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
103 |
* any empty queue to complete. |
4110 | 104 |
* |
105 |
* This approach also enables support for "async mode" where local |
|
106 |
* task processing is in FIFO, not LIFO order; simply by using a |
|
107 |
* version of deq rather than pop when locallyFifo is true (as set |
|
108 |
* by the ForkJoinPool). This allows use in message-passing |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
109 |
* frameworks in which tasks are never joined. However neither |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
110 |
* mode considers affinities, loads, cache localities, etc, so |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
111 |
* rarely provide the best possible performance on a given |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
112 |
* machine, but portably provide good throughput by averaging over |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
113 |
* these factors. (Further, even if we did try to use such |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
114 |
* information, we do not usually have a basis for exploiting |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
115 |
* it. For example, some sets of tasks profit from cache |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
116 |
* affinities, but others are harmed by cache pollution effects.) |
4110 | 117 |
* |
6543 | 118 |
* When a worker would otherwise be blocked waiting to join a |
119 |
* task, it first tries a form of linear helping: Each worker |
|
120 |
* records (in field currentSteal) the most recent task it stole |
|
121 |
* from some other worker. Plus, it records (in field currentJoin) |
|
122 |
* the task it is currently actively joining. Method joinTask uses |
|
123 |
* these markers to try to find a worker to help (i.e., steal back |
|
124 |
* a task from and execute it) that could hasten completion of the |
|
125 |
* actively joined task. In essence, the joiner executes a task |
|
126 |
* that would be on its own local deque had the to-be-joined task |
|
127 |
* not been stolen. This may be seen as a conservative variant of |
|
128 |
* the approach in Wagner & Calder "Leapfrogging: a portable |
|
129 |
* technique for implementing efficient futures" SIGPLAN Notices, |
|
130 |
* 1993 (http://portal.acm.org/citation.cfm?id=155354). It differs |
|
131 |
* in that: (1) We only maintain dependency links across workers |
|
132 |
* upon steals, rather than use per-task bookkeeping. This may |
|
133 |
* require a linear scan of workers array to locate stealers, but |
|
134 |
* usually doesn't because stealers leave hints (that may become |
|
135 |
* stale/wrong) of where to locate them. This isolates cost to |
|
136 |
* when it is needed, rather than adding to per-task overhead. |
|
137 |
* (2) It is "shallow", ignoring nesting and potentially cyclic |
|
138 |
* mutual steals. (3) It is intentionally racy: field currentJoin |
|
139 |
* is updated only while actively joining, which means that we |
|
140 |
* miss links in the chain during long-lived tasks, GC stalls etc |
|
141 |
* (which is OK since blocking in such cases is usually a good |
|
142 |
* idea). (4) We bound the number of attempts to find work (see |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
143 |
* MAX_HELP) and fall back to suspending the worker and if |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
144 |
* necessary replacing it with another. |
6543 | 145 |
* |
146 |
* Efficient implementation of these algorithms currently relies |
|
147 |
* on an uncomfortable amount of "Unsafe" mechanics. To maintain |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
148 |
* correct orderings, reads and writes of variable queueBase |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
149 |
* require volatile ordering. Variable queueTop need not be |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
150 |
* volatile because non-local reads always follow those of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
151 |
* queueBase. Similarly, because they are protected by volatile |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
152 |
* queueBase reads, reads of the queue array and its slots by |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
153 |
* other threads do not need volatile load semantics, but writes |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
154 |
* (in push) require store order and CASes (in pop and deq) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
155 |
* require (volatile) CAS semantics. (Michael, Saraswat, and |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
156 |
* Vechev's algorithm has similar properties, but without support |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
157 |
* for nulling slots.) Since these combinations aren't supported |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
158 |
* using ordinary volatiles, the only way to accomplish these |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
159 |
* efficiently is to use direct Unsafe calls. (Using external |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
160 |
* AtomicIntegers and AtomicReferenceArrays for the indices and |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
161 |
* array is significantly slower because of memory locality and |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
162 |
* indirection effects.) |
4110 | 163 |
* |
164 |
* Further, performance on most platforms is very sensitive to |
|
165 |
* placement and sizing of the (resizable) queue array. Even |
|
166 |
* though these queues don't usually become all that big, the |
|
167 |
* initial size must be large enough to counteract cache |
|
168 |
* contention effects across multiple queues (especially in the |
|
169 |
* presence of GC cardmarking). Also, to improve thread-locality, |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
170 |
* queues are initialized after starting. |
4110 | 171 |
*/ |
172 |
||
173 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
174 |
* Mask for pool indices encoded as shorts |
6543 | 175 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
176 |
private static final int SMASK = 0xffff; |
6543 | 177 |
|
178 |
/** |
|
4110 | 179 |
* Capacity of work-stealing queue array upon initialization. |
6543 | 180 |
* Must be a power of two. Initial size must be at least 4, but is |
4110 | 181 |
* padded to minimize cache effects. |
182 |
*/ |
|
183 |
private static final int INITIAL_QUEUE_CAPACITY = 1 << 13; |
|
184 |
||
185 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
186 |
* Maximum size for queue array. Must be a power of two |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
187 |
* less than or equal to 1 << (31 - width of array entry) to |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
188 |
* ensure lack of index wraparound, but is capped at a lower |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
189 |
* value to help users trap runaway computations. |
4110 | 190 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
191 |
private static final int MAXIMUM_QUEUE_CAPACITY = 1 << 24; // 16M |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
192 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
193 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
194 |
* The work-stealing queue array. Size must be a power of two. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
195 |
* Initialized when started (as oposed to when constructed), to |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
196 |
* improve memory locality. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
197 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
198 |
ForkJoinTask<?>[] queue; |
4110 | 199 |
|
200 |
/** |
|
201 |
* The pool this thread works in. Accessed directly by ForkJoinTask. |
|
202 |
*/ |
|
203 |
final ForkJoinPool pool; |
|
204 |
||
205 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
206 |
* Index (mod queue.length) of next queue slot to push to or pop |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
207 |
* from. It is written only by owner thread, and accessed by other |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
208 |
* threads only after reading (volatile) queueBase. Both queueTop |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
209 |
* and queueBase are allowed to wrap around on overflow, but |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
210 |
* (queueTop - queueBase) still estimates size. |
4110 | 211 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
212 |
int queueTop; |
4110 | 213 |
|
214 |
/** |
|
215 |
* Index (mod queue.length) of least valid queue slot, which is |
|
216 |
* always the next position to steal from if nonempty. |
|
217 |
*/ |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
218 |
volatile int queueBase; |
6543 | 219 |
|
220 |
/** |
|
221 |
* The index of most recent stealer, used as a hint to avoid |
|
222 |
* traversal in method helpJoinTask. This is only a hint because a |
|
223 |
* worker might have had multiple steals and this only holds one |
|
224 |
* of them (usually the most current). Declared non-volatile, |
|
225 |
* relying on other prevailing sync to keep reasonably current. |
|
226 |
*/ |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
227 |
int stealHint; |
4110 | 228 |
|
229 |
/** |
|
230 |
* Index of this worker in pool array. Set once by pool before |
|
6543 | 231 |
* running, and accessed directly by pool to locate this worker in |
232 |
* its workers array. |
|
4110 | 233 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
234 |
final int poolIndex; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
235 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
236 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
237 |
* Encoded record for pool task waits. Usages are always |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
238 |
* surrounded by volatile reads/writes |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
239 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
240 |
int nextWait; |
4110 | 241 |
|
242 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
243 |
* Complement of poolIndex, offset by count of entries of task |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
244 |
* waits. Accessed by ForkJoinPool to manage event waiters. |
4110 | 245 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
246 |
volatile int eventCount; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
247 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
248 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
249 |
* Seed for random number generator for choosing steal victims. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
250 |
* Uses Marsaglia xorshift. Must be initialized as nonzero. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
251 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
252 |
int seed; |
6543 | 253 |
|
254 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
255 |
* Number of steals. Directly accessed (and reset) by pool when |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
256 |
* idle. |
6543 | 257 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
258 |
int stealCount; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
259 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
260 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
261 |
* True if this worker should or did terminate |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
262 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
263 |
volatile boolean terminate; |
6543 | 264 |
|
265 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
266 |
* Set to true before LockSupport.park; false on return |
6543 | 267 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
268 |
volatile boolean parked; |
4110 | 269 |
|
270 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
271 |
* True if use local fifo, not default lifo, for local polling. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
272 |
* Shadows value from ForkJoinPool. |
4110 | 273 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
274 |
final boolean locallyFifo; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
275 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
276 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
277 |
* The task most recently stolen from another worker (or |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
278 |
* submission queue). All uses are surrounded by enough volatile |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
279 |
* reads/writes to maintain as non-volatile. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
280 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
281 |
ForkJoinTask<?> currentSteal; |
6543 | 282 |
|
283 |
/** |
|
284 |
* The task currently being joined, set only when actively trying |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
285 |
* to help other stealers in helpJoinTask. All uses are surrounded |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
286 |
* by enough volatile reads/writes to maintain as non-volatile. |
6543 | 287 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
288 |
ForkJoinTask<?> currentJoin; |
4110 | 289 |
|
290 |
/** |
|
291 |
* Creates a ForkJoinWorkerThread operating in the given pool. |
|
292 |
* |
|
293 |
* @param pool the pool this thread works in |
|
294 |
* @throws NullPointerException if pool is null |
|
295 |
*/ |
|
296 |
protected ForkJoinWorkerThread(ForkJoinPool pool) { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
297 |
super(pool.nextWorkerName()); |
4110 | 298 |
this.pool = pool; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
299 |
int k = pool.registerWorker(this); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
300 |
poolIndex = k; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
301 |
eventCount = ~k & SMASK; // clear wait count |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
302 |
locallyFifo = pool.locallyFifo; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
303 |
Thread.UncaughtExceptionHandler ueh = pool.ueh; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
304 |
if (ueh != null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
305 |
setUncaughtExceptionHandler(ueh); |
6543 | 306 |
setDaemon(true); |
4110 | 307 |
} |
308 |
||
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
309 |
// Public methods |
4110 | 310 |
|
311 |
/** |
|
312 |
* Returns the pool hosting this thread. |
|
313 |
* |
|
314 |
* @return the pool |
|
315 |
*/ |
|
316 |
public ForkJoinPool getPool() { |
|
317 |
return pool; |
|
318 |
} |
|
319 |
||
320 |
/** |
|
321 |
* Returns the index number of this thread in its pool. The |
|
322 |
* returned value ranges from zero to the maximum number of |
|
323 |
* threads (minus one) that have ever been created in the pool. |
|
324 |
* This method may be useful for applications that track status or |
|
325 |
* collect results per-worker rather than per-task. |
|
326 |
* |
|
327 |
* @return the index number |
|
328 |
*/ |
|
329 |
public int getPoolIndex() { |
|
330 |
return poolIndex; |
|
331 |
} |
|
332 |
||
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
333 |
// Randomization |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
334 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
335 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
336 |
* Computes next value for random victim probes and backoffs. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
337 |
* Scans don't require a very high quality generator, but also not |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
338 |
* a crummy one. Marsaglia xor-shift is cheap and works well |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
339 |
* enough. Note: This is manually inlined in FJP.scan() to avoid |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
340 |
* writes inside busy loops. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
341 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
342 |
private int nextSeed() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
343 |
int r = seed; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
344 |
r ^= r << 13; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
345 |
r ^= r >>> 17; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
346 |
r ^= r << 5; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
347 |
return seed = r; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
348 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
349 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
350 |
// Run State management |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
351 |
|
4110 | 352 |
/** |
353 |
* Initializes internal state after construction but before |
|
354 |
* processing any tasks. If you override this method, you must |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
355 |
* invoke {@code super.onStart()} at the beginning of the method. |
4110 | 356 |
* Initialization requires care: Most fields must have legal |
357 |
* default values, to ensure that attempted accesses from other |
|
358 |
* threads work correctly even before this thread starts |
|
359 |
* processing tasks. |
|
360 |
*/ |
|
361 |
protected void onStart() { |
|
362 |
queue = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY]; |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
363 |
int r = pool.workerSeedGenerator.nextInt(); |
9278 | 364 |
seed = (r == 0) ? 1 : r; // must be nonzero |
4110 | 365 |
} |
366 |
||
367 |
/** |
|
368 |
* Performs cleanup associated with termination of this worker |
|
369 |
* thread. If you override this method, you must invoke |
|
370 |
* {@code super.onTermination} at the end of the overridden method. |
|
371 |
* |
|
372 |
* @param exception the exception causing this thread to abort due |
|
373 |
* to an unrecoverable error, or {@code null} if completed normally |
|
374 |
*/ |
|
375 |
protected void onTermination(Throwable exception) { |
|
6543 | 376 |
try { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
377 |
terminate = true; |
4110 | 378 |
cancelTasks(); |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
379 |
pool.deregisterWorker(this, exception); |
4110 | 380 |
} catch (Throwable ex) { // Shouldn't ever happen |
381 |
if (exception == null) // but if so, at least rethrown |
|
382 |
exception = ex; |
|
383 |
} finally { |
|
384 |
if (exception != null) |
|
6543 | 385 |
UNSAFE.throwException(exception); |
386 |
} |
|
387 |
} |
|
388 |
||
389 |
/** |
|
390 |
* This method is required to be public, but should never be |
|
391 |
* called explicitly. It performs the main run loop to execute |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
392 |
* {@link ForkJoinTask}s. |
6543 | 393 |
*/ |
394 |
public void run() { |
|
395 |
Throwable exception = null; |
|
396 |
try { |
|
397 |
onStart(); |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
398 |
pool.work(this); |
6543 | 399 |
} catch (Throwable ex) { |
400 |
exception = ex; |
|
401 |
} finally { |
|
402 |
onTermination(exception); |
|
4110 | 403 |
} |
404 |
} |
|
405 |
||
6543 | 406 |
/* |
407 |
* Intrinsics-based atomic writes for queue slots. These are |
|
408 |
* basically the same as methods in AtomicReferenceArray, but |
|
409 |
* specialized for (1) ForkJoinTask elements (2) requirement that |
|
410 |
* nullness and bounds checks have already been performed by |
|
411 |
* callers and (3) effective offsets are known not to overflow |
|
412 |
* from int to long (because of MAXIMUM_QUEUE_CAPACITY). We don't |
|
413 |
* need corresponding version for reads: plain array reads are OK |
|
414 |
* because they are protected by other volatile reads and are |
|
415 |
* confirmed by CASes. |
|
416 |
* |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
417 |
* Most uses don't actually call these methods, but instead |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
418 |
* contain inlined forms that enable more predictable |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
419 |
* optimization. We don't define the version of write used in |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
420 |
* pushTask at all, but instead inline there a store-fenced array |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
421 |
* slot write. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
422 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
423 |
* Also in most methods, as a performance (not correctness) issue, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
424 |
* we'd like to encourage compilers not to arbitrarily postpone |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
425 |
* setting queueTop after writing slot. Currently there is no |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
426 |
* intrinsic for arranging this, but using Unsafe putOrderedInt |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
427 |
* may be a preferable strategy on some compilers even though its |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
428 |
* main effect is a pre-, not post- fence. To simplify possible |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
429 |
* changes, the option is left in comments next to the associated |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
430 |
* assignments. |
6543 | 431 |
*/ |
4110 | 432 |
|
433 |
/** |
|
6543 | 434 |
* CASes slot i of array q from t to null. Caller must ensure q is |
435 |
* non-null and index is in range. |
|
436 |
*/ |
|
437 |
private static final boolean casSlotNull(ForkJoinTask<?>[] q, int i, |
|
438 |
ForkJoinTask<?> t) { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
439 |
return UNSAFE.compareAndSwapObject(q, (i << ASHIFT) + ABASE, t, null); |
6543 | 440 |
} |
441 |
||
442 |
/** |
|
443 |
* Performs a volatile write of the given task at given slot of |
|
444 |
* array q. Caller must ensure q is non-null and index is in |
|
445 |
* range. This method is used only during resets and backouts. |
|
446 |
*/ |
|
447 |
private static final void writeSlot(ForkJoinTask<?>[] q, int i, |
|
448 |
ForkJoinTask<?> t) { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
449 |
UNSAFE.putObjectVolatile(q, (i << ASHIFT) + ABASE, t); |
6543 | 450 |
} |
451 |
||
452 |
// queue methods |
|
453 |
||
454 |
/** |
|
455 |
* Pushes a task. Call only from this thread. |
|
4110 | 456 |
* |
457 |
* @param t the task. Caller must ensure non-null. |
|
458 |
*/ |
|
459 |
final void pushTask(ForkJoinTask<?> t) { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
460 |
ForkJoinTask<?>[] q; int s, m; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
461 |
if ((q = queue) != null) { // ignore if queue removed |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
462 |
long u = (((s = queueTop) & (m = q.length - 1)) << ASHIFT) + ABASE; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
463 |
UNSAFE.putOrderedObject(q, u, t); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
464 |
queueTop = s + 1; // or use putOrderedInt |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
465 |
if ((s -= queueBase) <= 2) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
466 |
pool.signalWork(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
467 |
else if (s == m) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
468 |
growQueue(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
469 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
470 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
471 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
472 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
473 |
* Creates or doubles queue array. Transfers elements by |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
474 |
* emulating steals (deqs) from old array and placing, oldest |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
475 |
* first, into new array. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
476 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
477 |
private void growQueue() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
478 |
ForkJoinTask<?>[] oldQ = queue; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
479 |
int size = oldQ != null ? oldQ.length << 1 : INITIAL_QUEUE_CAPACITY; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
480 |
if (size > MAXIMUM_QUEUE_CAPACITY) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
481 |
throw new RejectedExecutionException("Queue capacity exceeded"); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
482 |
if (size < INITIAL_QUEUE_CAPACITY) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
483 |
size = INITIAL_QUEUE_CAPACITY; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
484 |
ForkJoinTask<?>[] q = queue = new ForkJoinTask<?>[size]; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
485 |
int mask = size - 1; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
486 |
int top = queueTop; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
487 |
int oldMask; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
488 |
if (oldQ != null && (oldMask = oldQ.length - 1) >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
489 |
for (int b = queueBase; b != top; ++b) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
490 |
long u = ((b & oldMask) << ASHIFT) + ABASE; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
491 |
Object x = UNSAFE.getObjectVolatile(oldQ, u); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
492 |
if (x != null && UNSAFE.compareAndSwapObject(oldQ, u, x, null)) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
493 |
UNSAFE.putObjectVolatile |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
494 |
(q, ((b & mask) << ASHIFT) + ABASE, x); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
495 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
496 |
} |
4110 | 497 |
} |
498 |
||
499 |
/** |
|
500 |
* Tries to take a task from the base of the queue, failing if |
|
6543 | 501 |
* empty or contended. Note: Specializations of this code appear |
502 |
* in locallyDeqTask and elsewhere. |
|
4110 | 503 |
* |
504 |
* @return a task, or null if none or contended |
|
505 |
*/ |
|
506 |
final ForkJoinTask<?> deqTask() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
507 |
ForkJoinTask<?> t; ForkJoinTask<?>[] q; int b, i; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
508 |
if (queueTop != (b = queueBase) && |
4110 | 509 |
(q = queue) != null && // must read q after b |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
510 |
(i = (q.length - 1) & b) >= 0 && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
511 |
(t = q[i]) != null && queueBase == b && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
512 |
UNSAFE.compareAndSwapObject(q, (i << ASHIFT) + ABASE, t, null)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
513 |
queueBase = b + 1; |
4110 | 514 |
return t; |
515 |
} |
|
516 |
return null; |
|
517 |
} |
|
518 |
||
519 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
520 |
* Tries to take a task from the base of own queue. Called only |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
521 |
* by this thread. |
4110 | 522 |
* |
523 |
* @return a task, or null if none |
|
524 |
*/ |
|
525 |
final ForkJoinTask<?> locallyDeqTask() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
526 |
ForkJoinTask<?> t; int m, b, i; |
6543 | 527 |
ForkJoinTask<?>[] q = queue; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
528 |
if (q != null && (m = q.length - 1) >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
529 |
while (queueTop != (b = queueBase)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
530 |
if ((t = q[i = m & b]) != null && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
531 |
queueBase == b && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
532 |
UNSAFE.compareAndSwapObject(q, (i << ASHIFT) + ABASE, |
6543 | 533 |
t, null)) { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
534 |
queueBase = b + 1; |
4110 | 535 |
return t; |
536 |
} |
|
537 |
} |
|
538 |
} |
|
539 |
return null; |
|
540 |
} |
|
541 |
||
542 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
543 |
* Returns a popped task, or null if empty. |
6543 | 544 |
* Called only by this thread. |
4110 | 545 |
*/ |
6543 | 546 |
private ForkJoinTask<?> popTask() { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
547 |
int m; |
6543 | 548 |
ForkJoinTask<?>[] q = queue; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
549 |
if (q != null && (m = q.length - 1) >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
550 |
for (int s; (s = queueTop) != queueBase;) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
551 |
int i = m & --s; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
552 |
long u = (i << ASHIFT) + ABASE; // raw offset |
4110 | 553 |
ForkJoinTask<?> t = q[i]; |
6543 | 554 |
if (t == null) // lost to stealer |
4110 | 555 |
break; |
6543 | 556 |
if (UNSAFE.compareAndSwapObject(q, u, t, null)) { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
557 |
queueTop = s; // or putOrderedInt |
6543 | 558 |
return t; |
559 |
} |
|
4110 | 560 |
} |
561 |
} |
|
562 |
return null; |
|
563 |
} |
|
564 |
||
565 |
/** |
|
6543 | 566 |
* Specialized version of popTask to pop only if topmost element |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
567 |
* is the given task. Called only by this thread. |
4110 | 568 |
* |
569 |
* @param t the task. Caller must ensure non-null. |
|
570 |
*/ |
|
571 |
final boolean unpushTask(ForkJoinTask<?> t) { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
572 |
ForkJoinTask<?>[] q; |
6543 | 573 |
int s; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
574 |
if ((q = queue) != null && (s = queueTop) != queueBase && |
6543 | 575 |
UNSAFE.compareAndSwapObject |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
576 |
(q, (((q.length - 1) & --s) << ASHIFT) + ABASE, t, null)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
577 |
queueTop = s; // or putOrderedInt |
4110 | 578 |
return true; |
579 |
} |
|
580 |
return false; |
|
581 |
} |
|
582 |
||
583 |
/** |
|
6543 | 584 |
* Returns next task, or null if empty or contended. |
4110 | 585 |
*/ |
586 |
final ForkJoinTask<?> peekTask() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
587 |
int m; |
4110 | 588 |
ForkJoinTask<?>[] q = queue; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
589 |
if (q == null || (m = q.length - 1) < 0) |
4110 | 590 |
return null; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
591 |
int i = locallyFifo ? queueBase : (queueTop - 1); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
592 |
return q[i & m]; |
4110 | 593 |
} |
594 |
||
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
595 |
// Support methods for ForkJoinPool |
6543 | 596 |
|
597 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
598 |
* Runs the given task, plus any local tasks until queue is empty |
4110 | 599 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
600 |
final void execTask(ForkJoinTask<?> t) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
601 |
currentSteal = t; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
602 |
for (;;) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
603 |
if (t != null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
604 |
t.doExec(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
605 |
if (queueTop == queueBase) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
606 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
607 |
t = locallyFifo ? locallyDeqTask() : popTask(); |
6674 | 608 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
609 |
++stealCount; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
610 |
currentSteal = null; |
6543 | 611 |
} |
4110 | 612 |
|
613 |
/** |
|
614 |
* Removes and cancels all tasks in queue. Can be called from any |
|
615 |
* thread. |
|
616 |
*/ |
|
617 |
final void cancelTasks() { |
|
6543 | 618 |
ForkJoinTask<?> cj = currentJoin; // try to cancel ongoing tasks |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
619 |
if (cj != null && cj.status >= 0) |
6543 | 620 |
cj.cancelIgnoringExceptions(); |
621 |
ForkJoinTask<?> cs = currentSteal; |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
622 |
if (cs != null && cs.status >= 0) |
6543 | 623 |
cs.cancelIgnoringExceptions(); |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
624 |
while (queueBase != queueTop) { |
6543 | 625 |
ForkJoinTask<?> t = deqTask(); |
626 |
if (t != null) |
|
627 |
t.cancelIgnoringExceptions(); |
|
628 |
} |
|
4110 | 629 |
} |
630 |
||
631 |
/** |
|
632 |
* Drains tasks to given collection c. |
|
633 |
* |
|
634 |
* @return the number of tasks drained |
|
635 |
*/ |
|
636 |
final int drainTasksTo(Collection<? super ForkJoinTask<?>> c) { |
|
637 |
int n = 0; |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
638 |
while (queueBase != queueTop) { |
6543 | 639 |
ForkJoinTask<?> t = deqTask(); |
640 |
if (t != null) { |
|
641 |
c.add(t); |
|
642 |
++n; |
|
643 |
} |
|
4110 | 644 |
} |
645 |
return n; |
|
646 |
} |
|
647 |
||
648 |
// Support methods for ForkJoinTask |
|
649 |
||
650 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
651 |
* Returns an estimate of the number of tasks in the queue. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
652 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
653 |
final int getQueueSize() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
654 |
return queueTop - queueBase; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
655 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
656 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
657 |
/** |
6543 | 658 |
* Gets and removes a local task. |
659 |
* |
|
660 |
* @return a task, if available |
|
661 |
*/ |
|
662 |
final ForkJoinTask<?> pollLocalTask() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
663 |
return locallyFifo ? locallyDeqTask() : popTask(); |
6543 | 664 |
} |
665 |
||
666 |
/** |
|
667 |
* Gets and removes a local or stolen task. |
|
668 |
* |
|
669 |
* @return a task, if available |
|
670 |
*/ |
|
671 |
final ForkJoinTask<?> pollTask() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
672 |
ForkJoinWorkerThread[] ws; |
6543 | 673 |
ForkJoinTask<?> t = pollLocalTask(); |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
674 |
if (t != null || (ws = pool.workers) == null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
675 |
return t; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
676 |
int n = ws.length; // cheap version of FJP.scan |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
677 |
int steps = n << 1; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
678 |
int r = nextSeed(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
679 |
int i = 0; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
680 |
while (i < steps) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
681 |
ForkJoinWorkerThread w = ws[(i++ + r) & (n - 1)]; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
682 |
if (w != null && w.queueBase != w.queueTop && w.queue != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
683 |
if ((t = w.deqTask()) != null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
684 |
return t; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
685 |
i = 0; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
686 |
} |
6543 | 687 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
688 |
return null; |
6543 | 689 |
} |
690 |
||
691 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
692 |
* The maximum stolen->joining link depth allowed in helpJoinTask, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
693 |
* as well as the maximum number of retries (allowing on average |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
694 |
* one staleness retry per level) per attempt to instead try |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
695 |
* compensation. Depths for legitimate chains are unbounded, but |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
696 |
* we use a fixed constant to avoid (otherwise unchecked) cycles |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
697 |
* and bound staleness of traversal parameters at the expense of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
698 |
* sometimes blocking when we could be helping. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
699 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
700 |
private static final int MAX_HELP = 16; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
701 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
702 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
703 |
* Possibly runs some tasks and/or blocks, until joinMe is done. |
6543 | 704 |
* |
705 |
* @param joinMe the task to join |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
706 |
* @return completion status on exit |
4110 | 707 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
708 |
final int joinTask(ForkJoinTask<?> joinMe) { |
6543 | 709 |
ForkJoinTask<?> prevJoin = currentJoin; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
710 |
currentJoin = joinMe; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
711 |
for (int s, retries = MAX_HELP;;) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
712 |
if ((s = joinMe.status) < 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
713 |
currentJoin = prevJoin; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
714 |
return s; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
715 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
716 |
if (retries > 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
717 |
if (queueTop != queueBase) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
718 |
if (!localHelpJoinTask(joinMe)) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
719 |
retries = 0; // cannot help |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
720 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
721 |
else if (retries == MAX_HELP >>> 1) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
722 |
--retries; // check uncommon case |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
723 |
if (tryDeqAndExec(joinMe) >= 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
724 |
Thread.yield(); // for politeness |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
725 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
726 |
else |
9278 | 727 |
retries = helpJoinTask(joinMe) ? MAX_HELP : retries - 1; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
728 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
729 |
else { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
730 |
retries = MAX_HELP; // restart if not done |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
731 |
pool.tryAwaitJoin(joinMe); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
732 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
733 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
734 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
735 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
736 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
737 |
* If present, pops and executes the given task, or any other |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
738 |
* cancelled task |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
739 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
740 |
* @return false if any other non-cancelled task exists in local queue |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
741 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
742 |
private boolean localHelpJoinTask(ForkJoinTask<?> joinMe) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
743 |
int s, i; ForkJoinTask<?>[] q; ForkJoinTask<?> t; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
744 |
if ((s = queueTop) != queueBase && (q = queue) != null && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
745 |
(i = (q.length - 1) & --s) >= 0 && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
746 |
(t = q[i]) != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
747 |
if (t != joinMe && t.status >= 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
748 |
return false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
749 |
if (UNSAFE.compareAndSwapObject |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
750 |
(q, (i << ASHIFT) + ABASE, t, null)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
751 |
queueTop = s; // or putOrderedInt |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
752 |
t.doExec(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
753 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
754 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
755 |
return true; |
6543 | 756 |
} |
757 |
||
758 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
759 |
* Tries to locate and execute tasks for a stealer of the given |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
760 |
* task, or in turn one of its stealers, Traces |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
761 |
* currentSteal->currentJoin links looking for a thread working on |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
762 |
* a descendant of the given task and with a non-empty queue to |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
763 |
* steal back and execute tasks from. The implementation is very |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
764 |
* branchy to cope with potential inconsistencies or loops |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
765 |
* encountering chains that are stale, unknown, or of length |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
766 |
* greater than MAX_HELP links. All of these cases are dealt with |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
767 |
* by just retrying by caller. |
6543 | 768 |
* |
769 |
* @param joinMe the task to join |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
770 |
* @param canSteal true if local queue is empty |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
771 |
* @return true if ran a task |
4110 | 772 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
773 |
private boolean helpJoinTask(ForkJoinTask<?> joinMe) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
774 |
boolean helped = false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
775 |
int m = pool.scanGuard & SMASK; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
776 |
ForkJoinWorkerThread[] ws = pool.workers; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
777 |
if (ws != null && ws.length > m && joinMe.status >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
778 |
int levels = MAX_HELP; // remaining chain length |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
779 |
ForkJoinTask<?> task = joinMe; // base of chain |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
780 |
outer:for (ForkJoinWorkerThread thread = this;;) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
781 |
// Try to find v, the stealer of task, by first using hint |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
782 |
ForkJoinWorkerThread v = ws[thread.stealHint & m]; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
783 |
if (v == null || v.currentSteal != task) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
784 |
for (int j = 0; ;) { // search array |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
785 |
if ((v = ws[j]) != null && v.currentSteal == task) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
786 |
thread.stealHint = j; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
787 |
break; // save hint for next time |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
788 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
789 |
if (++j > m) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
790 |
break outer; // can't find stealer |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
791 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
792 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
793 |
// Try to help v, using specialized form of deqTask |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
794 |
for (;;) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
795 |
ForkJoinTask<?>[] q; int b, i; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
796 |
if (joinMe.status < 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
797 |
break outer; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
798 |
if ((b = v.queueBase) == v.queueTop || |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
799 |
(q = v.queue) == null || |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
800 |
(i = (q.length-1) & b) < 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
801 |
break; // empty |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
802 |
long u = (i << ASHIFT) + ABASE; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
803 |
ForkJoinTask<?> t = q[i]; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
804 |
if (task.status < 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
805 |
break outer; // stale |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
806 |
if (t != null && v.queueBase == b && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
807 |
UNSAFE.compareAndSwapObject(q, u, t, null)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
808 |
v.queueBase = b + 1; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
809 |
v.stealHint = poolIndex; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
810 |
ForkJoinTask<?> ps = currentSteal; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
811 |
currentSteal = t; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
812 |
t.doExec(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
813 |
currentSteal = ps; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
814 |
helped = true; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
815 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
816 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
817 |
// Try to descend to find v's stealer |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
818 |
ForkJoinTask<?> next = v.currentJoin; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
819 |
if (--levels > 0 && task.status >= 0 && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
820 |
next != null && next != task) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
821 |
task = next; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
822 |
thread = v; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
823 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
824 |
else |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
825 |
break; // max levels, stale, dead-end, or cyclic |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
826 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
827 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
828 |
return helped; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
829 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
830 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
831 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
832 |
* Performs an uncommon case for joinTask: If task t is at base of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
833 |
* some workers queue, steals and executes it. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
834 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
835 |
* @param t the task |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
836 |
* @return t's status |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
837 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
838 |
private int tryDeqAndExec(ForkJoinTask<?> t) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
839 |
int m = pool.scanGuard & SMASK; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
840 |
ForkJoinWorkerThread[] ws = pool.workers; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
841 |
if (ws != null && ws.length > m && t.status >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
842 |
for (int j = 0; j <= m; ++j) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
843 |
ForkJoinTask<?>[] q; int b, i; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
844 |
ForkJoinWorkerThread v = ws[j]; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
845 |
if (v != null && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
846 |
(b = v.queueBase) != v.queueTop && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
847 |
(q = v.queue) != null && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
848 |
(i = (q.length - 1) & b) >= 0 && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
849 |
q[i] == t) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
850 |
long u = (i << ASHIFT) + ABASE; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
851 |
if (v.queueBase == b && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
852 |
UNSAFE.compareAndSwapObject(q, u, t, null)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
853 |
v.queueBase = b + 1; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
854 |
v.stealHint = poolIndex; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
855 |
ForkJoinTask<?> ps = currentSteal; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
856 |
currentSteal = t; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
857 |
t.doExec(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
858 |
currentSteal = ps; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
859 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
860 |
break; |
6543 | 861 |
} |
862 |
} |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
863 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
864 |
return t.status; |
4110 | 865 |
} |
866 |
||
867 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
868 |
* Implements ForkJoinTask.getSurplusQueuedTaskCount(). Returns |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
869 |
* an estimate of the number of tasks, offset by a function of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
870 |
* number of idle workers. |
6543 | 871 |
* |
872 |
* This method provides a cheap heuristic guide for task |
|
873 |
* partitioning when programmers, frameworks, tools, or languages |
|
874 |
* have little or no idea about task granularity. In essence by |
|
875 |
* offering this method, we ask users only about tradeoffs in |
|
876 |
* overhead vs expected throughput and its variance, rather than |
|
877 |
* how finely to partition tasks. |
|
878 |
* |
|
879 |
* In a steady state strict (tree-structured) computation, each |
|
880 |
* thread makes available for stealing enough tasks for other |
|
881 |
* threads to remain active. Inductively, if all threads play by |
|
882 |
* the same rules, each thread should make available only a |
|
883 |
* constant number of tasks. |
|
884 |
* |
|
885 |
* The minimum useful constant is just 1. But using a value of 1 |
|
886 |
* would require immediate replenishment upon each steal to |
|
887 |
* maintain enough tasks, which is infeasible. Further, |
|
888 |
* partitionings/granularities of offered tasks should minimize |
|
889 |
* steal rates, which in general means that threads nearer the top |
|
890 |
* of computation tree should generate more than those nearer the |
|
891 |
* bottom. In perfect steady state, each thread is at |
|
892 |
* approximately the same level of computation tree. However, |
|
893 |
* producing extra tasks amortizes the uncertainty of progress and |
|
894 |
* diffusion assumptions. |
|
895 |
* |
|
896 |
* So, users will want to use values larger, but not much larger |
|
897 |
* than 1 to both smooth over transient shortages and hedge |
|
898 |
* against uneven progress; as traded off against the cost of |
|
899 |
* extra task overhead. We leave the user to pick a threshold |
|
900 |
* value to compare with the results of this call to guide |
|
901 |
* decisions, but recommend values such as 3. |
|
902 |
* |
|
903 |
* When all threads are active, it is on average OK to estimate |
|
904 |
* surplus strictly locally. In steady-state, if one thread is |
|
905 |
* maintaining say 2 surplus tasks, then so are others. So we can |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
906 |
* just use estimated queue length (although note that (queueTop - |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
907 |
* queueBase) can be an overestimate because of stealers lagging |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
908 |
* increments of queueBase). However, this strategy alone leads |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
909 |
* to serious mis-estimates in some non-steady-state conditions |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
910 |
* (ramp-up, ramp-down, other stalls). We can detect many of these |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
911 |
* by further considering the number of "idle" threads, that are |
6543 | 912 |
* known to have zero queued tasks, so compensate by a factor of |
913 |
* (#idle/#active) threads. |
|
4110 | 914 |
*/ |
6543 | 915 |
final int getEstimatedSurplusTaskCount() { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
916 |
return queueTop - queueBase - pool.idlePerActive(); |
4110 | 917 |
} |
918 |
||
919 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
920 |
* Runs tasks until {@code pool.isQuiescent()}. We piggyback on |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
921 |
* pool's active count ctl maintenance, but rather than blocking |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
922 |
* when tasks cannot be found, we rescan until all others cannot |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
923 |
* find tasks either. The bracketing by pool quiescerCounts |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
924 |
* updates suppresses pool auto-shutdown mechanics that could |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
925 |
* otherwise prematurely terminate the pool because all threads |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
926 |
* appear to be inactive. |
4110 | 927 |
*/ |
928 |
final void helpQuiescePool() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
929 |
boolean active = true; |
6543 | 930 |
ForkJoinTask<?> ps = currentSteal; // to restore below |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
931 |
ForkJoinPool p = pool; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
932 |
p.addQuiescerCount(1); |
4110 | 933 |
for (;;) { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
934 |
ForkJoinWorkerThread[] ws = p.workers; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
935 |
ForkJoinWorkerThread v = null; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
936 |
int n; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
937 |
if (queueTop != queueBase) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
938 |
v = this; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
939 |
else if (ws != null && (n = ws.length) > 1) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
940 |
ForkJoinWorkerThread w; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
941 |
int r = nextSeed(); // cheap version of FJP.scan |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
942 |
int steps = n << 1; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
943 |
for (int i = 0; i < steps; ++i) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
944 |
if ((w = ws[(i + r) & (n - 1)]) != null && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
945 |
w.queueBase != w.queueTop) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
946 |
v = w; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
947 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
948 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
949 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
950 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
951 |
if (v != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
952 |
ForkJoinTask<?> t; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
953 |
if (!active) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
954 |
active = true; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
955 |
p.addActiveCount(1); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
956 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
957 |
if ((t = (v != this) ? v.deqTask() : |
9278 | 958 |
locallyFifo ? locallyDeqTask() : popTask()) != null) { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
959 |
currentSteal = t; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
960 |
t.doExec(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
961 |
currentSteal = ps; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
962 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
963 |
} |
6543 | 964 |
else { |
965 |
if (active) { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
966 |
active = false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
967 |
p.addActiveCount(-1); |
6543 | 968 |
} |
969 |
if (p.isQuiescent()) { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
970 |
p.addActiveCount(1); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
971 |
p.addQuiescerCount(-1); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
972 |
break; |
6543 | 973 |
} |
974 |
} |
|
4110 | 975 |
} |
976 |
} |
|
977 |
||
978 |
// Unsafe mechanics |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
979 |
private static final sun.misc.Unsafe UNSAFE; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
980 |
private static final long ABASE; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
981 |
private static final int ASHIFT; |
4110 | 982 |
|
983 |
static { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
984 |
int s; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
985 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
986 |
UNSAFE = sun.misc.Unsafe.getUnsafe(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
987 |
Class a = ForkJoinTask[].class; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
988 |
ABASE = UNSAFE.arrayBaseOffset(a); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
989 |
s = UNSAFE.arrayIndexScale(a); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
990 |
} catch (Exception e) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
991 |
throw new Error(e); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
992 |
} |
4110 | 993 |
if ((s & (s-1)) != 0) |
994 |
throw new Error("data type scale not a power of two"); |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
995 |
ASHIFT = 31 - Integer.numberOfLeadingZeros(s); |
4110 | 996 |
} |
997 |
||
998 |
} |