author | dl |
Tue, 08 Mar 2011 18:16:14 +0000 | |
changeset 8765 | dfc2a131d08a |
parent 7976 | f273c0d04215 |
child 9242 | ef138d47df58 |
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 |
|
33 |
* http://creativecommons.org/licenses/publicdomain |
|
34 |
*/ |
|
35 |
||
36 |
package java.util.concurrent; |
|
37 |
||
38 |
import java.util.ArrayList; |
|
39 |
import java.util.Arrays; |
|
40 |
import java.util.Collection; |
|
41 |
import java.util.Collections; |
|
42 |
import java.util.List; |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
43 |
import java.util.Random; |
6543 | 44 |
import java.util.concurrent.AbstractExecutorService; |
45 |
import java.util.concurrent.Callable; |
|
46 |
import java.util.concurrent.ExecutorService; |
|
47 |
import java.util.concurrent.Future; |
|
48 |
import java.util.concurrent.RejectedExecutionException; |
|
49 |
import java.util.concurrent.RunnableFuture; |
|
50 |
import java.util.concurrent.TimeUnit; |
|
51 |
import java.util.concurrent.TimeoutException; |
|
52 |
import java.util.concurrent.atomic.AtomicInteger; |
|
4110 | 53 |
import java.util.concurrent.locks.LockSupport; |
54 |
import java.util.concurrent.locks.ReentrantLock; |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
55 |
import java.util.concurrent.locks.Condition; |
4110 | 56 |
|
57 |
/** |
|
58 |
* An {@link ExecutorService} for running {@link ForkJoinTask}s. |
|
59 |
* A {@code ForkJoinPool} provides the entry point for submissions |
|
6543 | 60 |
* from non-{@code ForkJoinTask} clients, as well as management and |
4110 | 61 |
* monitoring operations. |
62 |
* |
|
63 |
* <p>A {@code ForkJoinPool} differs from other kinds of {@link |
|
64 |
* ExecutorService} mainly by virtue of employing |
|
65 |
* <em>work-stealing</em>: all threads in the pool attempt to find and |
|
66 |
* execute subtasks created by other active tasks (eventually blocking |
|
67 |
* waiting for work if none exist). This enables efficient processing |
|
68 |
* when most tasks spawn other subtasks (as do most {@code |
|
6543 | 69 |
* ForkJoinTask}s). When setting <em>asyncMode</em> to true in |
70 |
* constructors, {@code ForkJoinPool}s may also be appropriate for use |
|
71 |
* with event-style tasks that are never joined. |
|
4110 | 72 |
* |
73 |
* <p>A {@code ForkJoinPool} is constructed with a given target |
|
74 |
* parallelism level; by default, equal to the number of available |
|
6543 | 75 |
* processors. The pool attempts to maintain enough active (or |
76 |
* available) threads by dynamically adding, suspending, or resuming |
|
77 |
* internal worker threads, even if some tasks are stalled waiting to |
|
78 |
* join others. However, no such adjustments are guaranteed in the |
|
79 |
* face of blocked IO or other unmanaged synchronization. The nested |
|
80 |
* {@link ManagedBlocker} interface enables extension of the kinds of |
|
81 |
* synchronization accommodated. |
|
4110 | 82 |
* |
83 |
* <p>In addition to execution and lifecycle control methods, this |
|
84 |
* class provides status check methods (for example |
|
85 |
* {@link #getStealCount}) that are intended to aid in developing, |
|
86 |
* tuning, and monitoring fork/join applications. Also, method |
|
87 |
* {@link #toString} returns indications of pool state in a |
|
88 |
* convenient form for informal monitoring. |
|
89 |
* |
|
6543 | 90 |
* <p> As is the case with other ExecutorServices, there are three |
91 |
* main task execution methods summarized in the following |
|
92 |
* table. These are designed to be used by clients not already engaged |
|
93 |
* in fork/join computations in the current pool. The main forms of |
|
94 |
* these methods accept instances of {@code ForkJoinTask}, but |
|
95 |
* overloaded forms also allow mixed execution of plain {@code |
|
96 |
* Runnable}- or {@code Callable}- based activities as well. However, |
|
97 |
* tasks that are already executing in a pool should normally |
|
98 |
* <em>NOT</em> use these pool execution methods, but instead use the |
|
99 |
* within-computation forms listed in the table. |
|
100 |
* |
|
101 |
* <table BORDER CELLPADDING=3 CELLSPACING=1> |
|
102 |
* <tr> |
|
103 |
* <td></td> |
|
104 |
* <td ALIGN=CENTER> <b>Call from non-fork/join clients</b></td> |
|
105 |
* <td ALIGN=CENTER> <b>Call from within fork/join computations</b></td> |
|
106 |
* </tr> |
|
107 |
* <tr> |
|
108 |
* <td> <b>Arrange async execution</td> |
|
109 |
* <td> {@link #execute(ForkJoinTask)}</td> |
|
110 |
* <td> {@link ForkJoinTask#fork}</td> |
|
111 |
* </tr> |
|
112 |
* <tr> |
|
113 |
* <td> <b>Await and obtain result</td> |
|
114 |
* <td> {@link #invoke(ForkJoinTask)}</td> |
|
115 |
* <td> {@link ForkJoinTask#invoke}</td> |
|
116 |
* </tr> |
|
117 |
* <tr> |
|
118 |
* <td> <b>Arrange exec and obtain Future</td> |
|
119 |
* <td> {@link #submit(ForkJoinTask)}</td> |
|
120 |
* <td> {@link ForkJoinTask#fork} (ForkJoinTasks <em>are</em> Futures)</td> |
|
121 |
* </tr> |
|
122 |
* </table> |
|
123 |
* |
|
4110 | 124 |
* <p><b>Sample Usage.</b> Normally a single {@code ForkJoinPool} is |
125 |
* used for all parallel task execution in a program or subsystem. |
|
126 |
* Otherwise, use would not usually outweigh the construction and |
|
127 |
* bookkeeping overhead of creating a large set of threads. For |
|
128 |
* example, a common pool could be used for the {@code SortTasks} |
|
129 |
* illustrated in {@link RecursiveAction}. Because {@code |
|
130 |
* ForkJoinPool} uses threads in {@linkplain java.lang.Thread#isDaemon |
|
131 |
* daemon} mode, there is typically no need to explicitly {@link |
|
132 |
* #shutdown} such a pool upon program exit. |
|
133 |
* |
|
134 |
* <pre> |
|
135 |
* static final ForkJoinPool mainPool = new ForkJoinPool(); |
|
136 |
* ... |
|
137 |
* public void sort(long[] array) { |
|
138 |
* mainPool.invoke(new SortTask(array, 0, array.length)); |
|
139 |
* } |
|
140 |
* </pre> |
|
141 |
* |
|
142 |
* <p><b>Implementation notes</b>: This implementation restricts the |
|
143 |
* maximum number of running threads to 32767. Attempts to create |
|
144 |
* pools with greater than the maximum number result in |
|
145 |
* {@code IllegalArgumentException}. |
|
146 |
* |
|
147 |
* <p>This implementation rejects submitted tasks (that is, by throwing |
|
6543 | 148 |
* {@link RejectedExecutionException}) only when the pool is shut down |
149 |
* or internal resources have been exhausted. |
|
4110 | 150 |
* |
151 |
* @since 1.7 |
|
152 |
* @author Doug Lea |
|
153 |
*/ |
|
154 |
public class ForkJoinPool extends AbstractExecutorService { |
|
155 |
||
156 |
/* |
|
6543 | 157 |
* Implementation Overview |
158 |
* |
|
159 |
* This class provides the central bookkeeping and control for a |
|
160 |
* set of worker threads: Submissions from non-FJ threads enter |
|
161 |
* into a submission queue. Workers take these tasks and typically |
|
162 |
* split them into subtasks that may be stolen by other workers. |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
163 |
* Preference rules give first priority to processing tasks from |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
164 |
* their own queues (LIFO or FIFO, depending on mode), then to |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
165 |
* randomized FIFO steals of tasks in other worker queues, and |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
166 |
* lastly to new submissions. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
167 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
168 |
* The main throughput advantages of work-stealing stem from |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
169 |
* decentralized control -- workers mostly take tasks from |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
170 |
* themselves or each other. We cannot negate this in the |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
171 |
* implementation of other management responsibilities. The main |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
172 |
* tactic for avoiding bottlenecks is packing nearly all |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
173 |
* essentially atomic control state into a single 64bit volatile |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
174 |
* variable ("ctl"). This variable is read on the order of 10-100 |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
175 |
* times as often as it is modified (always via CAS). (There is |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
176 |
* some additional control state, for example variable "shutdown" |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
177 |
* for which we can cope with uncoordinated updates.) This |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
178 |
* streamlines synchronization and control at the expense of messy |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
179 |
* constructions needed to repack status bits upon updates. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
180 |
* Updates tend not to contend with each other except during |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
181 |
* bursts while submitted tasks begin or end. In some cases when |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
182 |
* they do contend, threads can instead do something else |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
183 |
* (usually, scan for tasks) until contention subsides. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
184 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
185 |
* To enable packing, we restrict maximum parallelism to (1<<15)-1 |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
186 |
* (which is far in excess of normal operating range) to allow |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
187 |
* ids, counts, and their negations (used for thresholding) to fit |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
188 |
* into 16bit fields. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
189 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
190 |
* Recording Workers. Workers are recorded in the "workers" array |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
191 |
* that is created upon pool construction and expanded if (rarely) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
192 |
* necessary. This is an array as opposed to some other data |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
193 |
* structure to support index-based random steals by workers. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
194 |
* Updates to the array recording new workers and unrecording |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
195 |
* terminated ones are protected from each other by a seqLock |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
196 |
* (scanGuard) but the array is otherwise concurrently readable, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
197 |
* and accessed directly by workers. To simplify index-based |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
198 |
* operations, the array size is always a power of two, and all |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
199 |
* readers must tolerate null slots. To avoid flailing during |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
200 |
* start-up, the array is presized to hold twice #parallelism |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
201 |
* workers (which is unlikely to need further resizing during |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
202 |
* execution). But to avoid dealing with so many null slots, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
203 |
* variable scanGuard includes a mask for the nearest power of two |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
204 |
* that contains all current workers. All worker thread creation |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
205 |
* is on-demand, triggered by task submissions, replacement of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
206 |
* terminated workers, and/or compensation for blocked |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
207 |
* workers. However, all other support code is set up to work with |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
208 |
* other policies. To ensure that we do not hold on to worker |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
209 |
* references that would prevent GC, ALL accesses to workers are |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
210 |
* via indices into the workers array (which is one source of some |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
211 |
* of the messy code constructions here). In essence, the workers |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
212 |
* array serves as a weak reference mechanism. Thus for example |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
213 |
* the wait queue field of ctl stores worker indices, not worker |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
214 |
* references. Access to the workers in associated methods (for |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
215 |
* example signalWork) must both index-check and null-check the |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
216 |
* IDs. All such accesses ignore bad IDs by returning out early |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
217 |
* from what they are doing, since this can only be associated |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
218 |
* with termination, in which case it is OK to give up. |
6543 | 219 |
* |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
220 |
* All uses of the workers array, as well as queue arrays, check |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
221 |
* that the array is non-null (even if previously non-null). This |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
222 |
* allows nulling during termination, which is currently not |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
223 |
* necessary, but remains an option for resource-revocation-based |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
224 |
* shutdown schemes. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
225 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
226 |
* Wait Queuing. Unlike HPC work-stealing frameworks, we cannot |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
227 |
* let workers spin indefinitely scanning for tasks when none can |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
228 |
* be found immediately, and we cannot start/resume workers unless |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
229 |
* there appear to be tasks available. On the other hand, we must |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
230 |
* quickly prod them into action when new tasks are submitted or |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
231 |
* generated. We park/unpark workers after placing in an event |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
232 |
* wait queue when they cannot find work. This "queue" is actually |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
233 |
* a simple Treiber stack, headed by the "id" field of ctl, plus a |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
234 |
* 15bit counter value to both wake up waiters (by advancing their |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
235 |
* count) and avoid ABA effects. Successors are held in worker |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
236 |
* field "nextWait". Queuing deals with several intrinsic races, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
237 |
* mainly that a task-producing thread can miss seeing (and |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
238 |
* signalling) another thread that gave up looking for work but |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
239 |
* has not yet entered the wait queue. We solve this by requiring |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
240 |
* a full sweep of all workers both before (in scan()) and after |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
241 |
* (in tryAwaitWork()) a newly waiting worker is added to the wait |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
242 |
* queue. During a rescan, the worker might release some other |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
243 |
* queued worker rather than itself, which has the same net |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
244 |
* effect. Because enqueued workers may actually be rescanning |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
245 |
* rather than waiting, we set and clear the "parked" field of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
246 |
* ForkJoinWorkerThread to reduce unnecessary calls to unpark. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
247 |
* (Use of the parked field requires a secondary recheck to avoid |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
248 |
* missed signals.) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
249 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
250 |
* Signalling. We create or wake up workers only when there |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
251 |
* appears to be at least one task they might be able to find and |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
252 |
* execute. When a submission is added or another worker adds a |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
253 |
* task to a queue that previously had two or fewer tasks, they |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
254 |
* signal waiting workers (or trigger creation of new ones if |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
255 |
* fewer than the given parallelism level -- see signalWork). |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
256 |
* These primary signals are buttressed by signals during rescans |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
257 |
* as well as those performed when a worker steals a task and |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
258 |
* notices that there are more tasks too; together these cover the |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
259 |
* signals needed in cases when more than two tasks are pushed |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
260 |
* but untaken. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
261 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
262 |
* Trimming workers. To release resources after periods of lack of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
263 |
* use, a worker starting to wait when the pool is quiescent will |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
264 |
* time out and terminate if the pool has remained quiescent for |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
265 |
* SHRINK_RATE nanosecs. This will slowly propagate, eventually |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
266 |
* terminating all workers after long periods of non-use. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
267 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
268 |
* Submissions. External submissions are maintained in an |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
269 |
* array-based queue that is structured identically to |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
270 |
* ForkJoinWorkerThread queues except for the use of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
271 |
* submissionLock in method addSubmission. Unlike the case for |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
272 |
* worker queues, multiple external threads can add new |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
273 |
* submissions, so adding requires a lock. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
274 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
275 |
* Compensation. Beyond work-stealing support and lifecycle |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
276 |
* control, the main responsibility of this framework is to take |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
277 |
* actions when one worker is waiting to join a task stolen (or |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
278 |
* always held by) another. Because we are multiplexing many |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
279 |
* tasks on to a pool of workers, we can't just let them block (as |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
280 |
* in Thread.join). We also cannot just reassign the joiner's |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
281 |
* run-time stack with another and replace it later, which would |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
282 |
* be a form of "continuation", that even if possible is not |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
283 |
* necessarily a good idea since we sometimes need both an |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
284 |
* unblocked task and its continuation to progress. Instead we |
6543 | 285 |
* combine two tactics: |
286 |
* |
|
287 |
* Helping: Arranging for the joiner to execute some task that it |
|
288 |
* would be running if the steal had not occurred. Method |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
289 |
* ForkJoinWorkerThread.joinTask tracks joining->stealing |
6543 | 290 |
* links to try to find such a task. |
291 |
* |
|
292 |
* Compensating: Unless there are already enough live threads, |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
293 |
* method tryPreBlock() may create or re-activate a spare |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
294 |
* thread to compensate for blocked joiners until they |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
295 |
* unblock. |
6543 | 296 |
* |
297 |
* The ManagedBlocker extension API can't use helping so relies |
|
298 |
* only on compensation in method awaitBlocker. |
|
299 |
* |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
300 |
* It is impossible to keep exactly the target parallelism number |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
301 |
* of threads running at any given time. Determining the |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
302 |
* existence of conservatively safe helping targets, the |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
303 |
* availability of already-created spares, and the apparent need |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
304 |
* to create new spares are all racy and require heuristic |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
305 |
* guidance, so we rely on multiple retries of each. Currently, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
306 |
* in keeping with on-demand signalling policy, we compensate only |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
307 |
* if blocking would leave less than one active (non-waiting, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
308 |
* non-blocked) worker. Additionally, to avoid some false alarms |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
309 |
* due to GC, lagging counters, system activity, etc, compensated |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
310 |
* blocking for joins is only attempted after rechecks stabilize |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
311 |
* (retries are interspersed with Thread.yield, for good |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
312 |
* citizenship). The variable blockedCount, incremented before |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
313 |
* blocking and decremented after, is sometimes needed to |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
314 |
* distinguish cases of waiting for work vs blocking on joins or |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
315 |
* other managed sync. Both cases are equivalent for most pool |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
316 |
* control, so we can update non-atomically. (Additionally, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
317 |
* contention on blockedCount alleviates some contention on ctl). |
6543 | 318 |
* |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
319 |
* Shutdown and Termination. A call to shutdownNow atomically sets |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
320 |
* the ctl stop bit and then (non-atomically) sets each workers |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
321 |
* "terminate" status, cancels all unprocessed tasks, and wakes up |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
322 |
* all waiting workers. Detecting whether termination should |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
323 |
* commence after a non-abrupt shutdown() call requires more work |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
324 |
* and bookkeeping. We need consensus about quiesence (i.e., that |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
325 |
* there is no more work) which is reflected in active counts so |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
326 |
* long as there are no current blockers, as well as possible |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
327 |
* re-evaluations during independent changes in blocking or |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
328 |
* quiescing workers. |
6543 | 329 |
* |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
330 |
* Style notes: There is a lot of representation-level coupling |
6543 | 331 |
* among classes ForkJoinPool, ForkJoinWorkerThread, and |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
332 |
* ForkJoinTask. Most fields of ForkJoinWorkerThread maintain |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
333 |
* data structures managed by ForkJoinPool, so are directly |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
334 |
* accessed. Conversely we allow access to "workers" array by |
6543 | 335 |
* workers, and direct access to ForkJoinTask.status by both |
336 |
* ForkJoinPool and ForkJoinWorkerThread. There is little point |
|
337 |
* trying to reduce this, since any associated future changes in |
|
338 |
* representations will need to be accompanied by algorithmic |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
339 |
* changes anyway. All together, these low-level implementation |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
340 |
* choices produce as much as a factor of 4 performance |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
341 |
* improvement compared to naive implementations, and enable the |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
342 |
* processing of billions of tasks per second, at the expense of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
343 |
* some ugliness. |
6543 | 344 |
* |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
345 |
* Methods signalWork() and scan() are the main bottlenecks so are |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
346 |
* especially heavily micro-optimized/mangled. There are lots of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
347 |
* inline assignments (of form "while ((local = field) != 0)") |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
348 |
* which are usually the simplest way to ensure the required read |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
349 |
* orderings (which are sometimes critical). This leads to a |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
350 |
* "C"-like style of listing declarations of these locals at the |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
351 |
* heads of methods or blocks. There are several occurrences of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
352 |
* the unusual "do {} while (!cas...)" which is the simplest way |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
353 |
* to force an update of a CAS'ed variable. There are also other |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
354 |
* coding oddities that help some methods perform reasonably even |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
355 |
* when interpreted (not compiled). |
6543 | 356 |
* |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
357 |
* The order of declarations in this file is: (1) declarations of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
358 |
* statics (2) fields (along with constants used when unpacking |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
359 |
* some of them), listed in an order that tends to reduce |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
360 |
* contention among them a bit under most JVMs. (3) internal |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
361 |
* control methods (4) callbacks and other support for |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
362 |
* ForkJoinTask and ForkJoinWorkerThread classes, (5) exported |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
363 |
* methods (plus a few little helpers). (6) static block |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
364 |
* initializing all statics in a minimally dependent order. |
4110 | 365 |
*/ |
366 |
||
367 |
/** |
|
368 |
* Factory for creating new {@link ForkJoinWorkerThread}s. |
|
369 |
* A {@code ForkJoinWorkerThreadFactory} must be defined and used |
|
370 |
* for {@code ForkJoinWorkerThread} subclasses that extend base |
|
371 |
* functionality or initialize threads with different contexts. |
|
372 |
*/ |
|
373 |
public static interface ForkJoinWorkerThreadFactory { |
|
374 |
/** |
|
375 |
* Returns a new worker thread operating in the given pool. |
|
376 |
* |
|
377 |
* @param pool the pool this thread works in |
|
378 |
* @throws NullPointerException if the pool is null |
|
379 |
*/ |
|
380 |
public ForkJoinWorkerThread newThread(ForkJoinPool pool); |
|
381 |
} |
|
382 |
||
383 |
/** |
|
384 |
* Default ForkJoinWorkerThreadFactory implementation; creates a |
|
385 |
* new ForkJoinWorkerThread. |
|
386 |
*/ |
|
6543 | 387 |
static class DefaultForkJoinWorkerThreadFactory |
4110 | 388 |
implements ForkJoinWorkerThreadFactory { |
389 |
public ForkJoinWorkerThread newThread(ForkJoinPool pool) { |
|
6543 | 390 |
return new ForkJoinWorkerThread(pool); |
4110 | 391 |
} |
392 |
} |
|
393 |
||
394 |
/** |
|
395 |
* Creates a new ForkJoinWorkerThread. This factory is used unless |
|
396 |
* overridden in ForkJoinPool constructors. |
|
397 |
*/ |
|
398 |
public static final ForkJoinWorkerThreadFactory |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
399 |
defaultForkJoinWorkerThreadFactory; |
4110 | 400 |
|
401 |
/** |
|
402 |
* Permission required for callers of methods that may start or |
|
403 |
* kill threads. |
|
404 |
*/ |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
405 |
private static final RuntimePermission modifyThreadPermission; |
4110 | 406 |
|
407 |
/** |
|
408 |
* If there is a security manager, makes sure caller has |
|
409 |
* permission to modify threads. |
|
410 |
*/ |
|
411 |
private static void checkPermission() { |
|
412 |
SecurityManager security = System.getSecurityManager(); |
|
413 |
if (security != null) |
|
414 |
security.checkPermission(modifyThreadPermission); |
|
415 |
} |
|
416 |
||
417 |
/** |
|
418 |
* Generator for assigning sequence numbers as pool names. |
|
419 |
*/ |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
420 |
private static final AtomicInteger poolNumberGenerator; |
4110 | 421 |
|
422 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
423 |
* Generator for initial random seeds for worker victim |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
424 |
* selection. This is used only to create initial seeds. Random |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
425 |
* steals use a cheaper xorshift generator per steal attempt. We |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
426 |
* don't expect much contention on seedGenerator, so just use a |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
427 |
* plain Random. |
4110 | 428 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
429 |
static final Random workerSeedGenerator; |
4110 | 430 |
|
431 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
432 |
* Array holding all worker threads in the pool. Initialized upon |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
433 |
* construction. Array size must be a power of two. Updates and |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
434 |
* replacements are protected by scanGuard, but the array is |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
435 |
* always kept in a consistent enough state to be randomly |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
436 |
* accessed without locking by workers performing work-stealing, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
437 |
* as well as other traversal-based methods in this class, so long |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
438 |
* as reads memory-acquire by first reading ctl. All readers must |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
439 |
* tolerate that some array slots may be null. |
4110 | 440 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
441 |
ForkJoinWorkerThread[] workers; |
4110 | 442 |
|
443 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
444 |
* Initial size for submission queue array. Must be a power of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
445 |
* two. In many applications, these always stay small so we use a |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
446 |
* small initial cap. |
4110 | 447 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
448 |
private static final int INITIAL_QUEUE_CAPACITY = 8; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
449 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
450 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
451 |
* Maximum size for submission queue array. Must be a power of two |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
452 |
* 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
|
453 |
* ensure lack of index wraparound, but is capped at a lower |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
454 |
* value to help users trap runaway computations. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
455 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
456 |
private static final int MAXIMUM_QUEUE_CAPACITY = 1 << 24; // 16M |
4110 | 457 |
|
458 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
459 |
* Array serving as submission queue. Initialized upon construction. |
4110 | 460 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
461 |
private ForkJoinTask<?>[] submissionQueue; |
4110 | 462 |
|
463 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
464 |
* Lock protecting submissions array for addSubmission |
4110 | 465 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
466 |
private final ReentrantLock submissionLock; |
4110 | 467 |
|
468 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
469 |
* Condition for awaitTermination, using submissionLock for |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
470 |
* convenience. |
6543 | 471 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
472 |
private final Condition termination; |
6543 | 473 |
|
474 |
/** |
|
475 |
* Creation factory for worker threads. |
|
476 |
*/ |
|
477 |
private final ForkJoinWorkerThreadFactory factory; |
|
478 |
||
479 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
480 |
* The uncaught exception handler used when any worker abruptly |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
481 |
* terminates. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
482 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
483 |
final Thread.UncaughtExceptionHandler ueh; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
484 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
485 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
486 |
* Prefix for assigning names to worker threads |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
487 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
488 |
private final String workerNamePrefix; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
489 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
490 |
/** |
6543 | 491 |
* Sum of per-thread steal counts, updated only when threads are |
492 |
* idle or terminating. |
|
4110 | 493 |
*/ |
6543 | 494 |
private volatile long stealCount; |
495 |
||
496 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
497 |
* Main pool control -- a long packed with: |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
498 |
* AC: Number of active running workers minus target parallelism (16 bits) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
499 |
* TC: Number of total workers minus target parallelism (16bits) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
500 |
* ST: true if pool is terminating (1 bit) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
501 |
* EC: the wait count of top waiting thread (15 bits) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
502 |
* ID: ~poolIndex of top of Treiber stack of waiting threads (16 bits) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
503 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
504 |
* When convenient, we can extract the upper 32 bits of counts and |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
505 |
* the lower 32 bits of queue state, u = (int)(ctl >>> 32) and e = |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
506 |
* (int)ctl. The ec field is never accessed alone, but always |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
507 |
* together with id and st. The offsets of counts by the target |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
508 |
* parallelism and the positionings of fields makes it possible to |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
509 |
* perform the most common checks via sign tests of fields: When |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
510 |
* ac is negative, there are not enough active workers, when tc is |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
511 |
* negative, there are not enough total workers, when id is |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
512 |
* negative, there is at least one waiting worker, and when e is |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
513 |
* negative, the pool is terminating. To deal with these possibly |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
514 |
* negative fields, we use casts in and out of "short" and/or |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
515 |
* signed shifts to maintain signedness. |
6543 | 516 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
517 |
volatile long ctl; |
4110 | 518 |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
519 |
// bit positions/shifts for fields |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
520 |
private static final int AC_SHIFT = 48; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
521 |
private static final int TC_SHIFT = 32; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
522 |
private static final int ST_SHIFT = 31; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
523 |
private static final int EC_SHIFT = 16; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
524 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
525 |
// bounds |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
526 |
private static final int MAX_ID = 0x7fff; // max poolIndex |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
527 |
private static final int SMASK = 0xffff; // mask short bits |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
528 |
private static final int SHORT_SIGN = 1 << 15; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
529 |
private static final int INT_SIGN = 1 << 31; |
6543 | 530 |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
531 |
// masks |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
532 |
private static final long STOP_BIT = 0x0001L << ST_SHIFT; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
533 |
private static final long AC_MASK = ((long)SMASK) << AC_SHIFT; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
534 |
private static final long TC_MASK = ((long)SMASK) << TC_SHIFT; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
535 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
536 |
// units for incrementing and decrementing |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
537 |
private static final long TC_UNIT = 1L << TC_SHIFT; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
538 |
private static final long AC_UNIT = 1L << AC_SHIFT; |
6543 | 539 |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
540 |
// masks and units for dealing with u = (int)(ctl >>> 32) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
541 |
private static final int UAC_SHIFT = AC_SHIFT - 32; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
542 |
private static final int UTC_SHIFT = TC_SHIFT - 32; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
543 |
private static final int UAC_MASK = SMASK << UAC_SHIFT; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
544 |
private static final int UTC_MASK = SMASK << UTC_SHIFT; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
545 |
private static final int UAC_UNIT = 1 << UAC_SHIFT; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
546 |
private static final int UTC_UNIT = 1 << UTC_SHIFT; |
6543 | 547 |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
548 |
// masks and units for dealing with e = (int)ctl |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
549 |
private static final int E_MASK = 0x7fffffff; // no STOP_BIT |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
550 |
private static final int EC_UNIT = 1 << EC_SHIFT; |
6543 | 551 |
|
552 |
/** |
|
553 |
* The target parallelism level. |
|
554 |
*/ |
|
555 |
final int parallelism; |
|
556 |
||
557 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
558 |
* Index (mod submission queue length) of next element to take |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
559 |
* from submission queue. Usage is identical to that for |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
560 |
* per-worker queues -- see ForkJoinWorkerThread internal |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
561 |
* documentation. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
562 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
563 |
volatile int queueBase; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
564 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
565 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
566 |
* Index (mod submission queue length) of next element to add |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
567 |
* in submission queue. Usage is identical to that for |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
568 |
* per-worker queues -- see ForkJoinWorkerThread internal |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
569 |
* documentation. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
570 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
571 |
int queueTop; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
572 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
573 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
574 |
* True when shutdown() has been called. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
575 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
576 |
volatile boolean shutdown; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
577 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
578 |
/** |
6543 | 579 |
* True if use local fifo, not default lifo, for local polling |
580 |
* Read by, and replicated by ForkJoinWorkerThreads |
|
581 |
*/ |
|
582 |
final boolean locallyFifo; |
|
583 |
||
584 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
585 |
* The number of threads in ForkJoinWorkerThreads.helpQuiescePool. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
586 |
* When non-zero, suppresses automatic shutdown when active |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
587 |
* counts become zero. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
588 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
589 |
volatile int quiescerCount; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
590 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
591 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
592 |
* The number of threads blocked in join. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
593 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
594 |
volatile int blockedCount; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
595 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
596 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
597 |
* Counter for worker Thread names (unrelated to their poolIndex) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
598 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
599 |
private volatile int nextWorkerNumber; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
600 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
601 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
602 |
* The index for the next created worker. Accessed under scanGuard. |
6543 | 603 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
604 |
private int nextWorkerIndex; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
605 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
606 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
607 |
* SeqLock and index masking for updates to workers array. Locked |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
608 |
* when SG_UNIT is set. Unlocking clears bit by adding |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
609 |
* SG_UNIT. Staleness of read-only operations can be checked by |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
610 |
* comparing scanGuard to value before the reads. The low 16 bits |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
611 |
* (i.e, anding with SMASK) hold (the smallest power of two |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
612 |
* covering all worker indices, minus one, and is used to avoid |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
613 |
* dealing with large numbers of null slots when the workers array |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
614 |
* is overallocated. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
615 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
616 |
volatile int scanGuard; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
617 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
618 |
private static final int SG_UNIT = 1 << 16; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
619 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
620 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
621 |
* The wakeup interval (in nanoseconds) for a worker waiting for a |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
622 |
* task when the pool is quiescent to instead try to shrink the |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
623 |
* number of workers. The exact value does not matter too |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
624 |
* much. It must be short enough to release resources during |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
625 |
* sustained periods of idleness, but not so short that threads |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
626 |
* are continually re-created. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
627 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
628 |
private static final long SHRINK_RATE = |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
629 |
4L * 1000L * 1000L * 1000L; // 4 seconds |
4110 | 630 |
|
631 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
632 |
* Top-level loop for worker threads: On each step: if the |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
633 |
* previous step swept through all queues and found no tasks, or |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
634 |
* there are excess threads, then possibly blocks. Otherwise, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
635 |
* scans for and, if found, executes a task. Returns when pool |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
636 |
* and/or worker terminate. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
637 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
638 |
* @param w the worker |
4110 | 639 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
640 |
final void work(ForkJoinWorkerThread w) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
641 |
boolean swept = false; // true on empty scans |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
642 |
long c; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
643 |
while (!w.terminate && (int)(c = ctl) >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
644 |
int a; // active count |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
645 |
if (!swept && (a = (int)(c >> AC_SHIFT)) <= 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
646 |
swept = scan(w, a); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
647 |
else if (tryAwaitWork(w, c)) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
648 |
swept = false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
649 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
650 |
} |
4110 | 651 |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
652 |
// Signalling |
4110 | 653 |
|
654 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
655 |
* Wakes up or creates a worker. |
4110 | 656 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
657 |
final void signalWork() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
658 |
/* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
659 |
* The while condition is true if: (there is are too few total |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
660 |
* workers OR there is at least one waiter) AND (there are too |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
661 |
* few active workers OR the pool is terminating). The value |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
662 |
* of e distinguishes the remaining cases: zero (no waiters) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
663 |
* for create, negative if terminating (in which case do |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
664 |
* nothing), else release a waiter. The secondary checks for |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
665 |
* release (non-null array etc) can fail if the pool begins |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
666 |
* terminating after the test, and don't impose any added cost |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
667 |
* because JVMs must perform null and bounds checks anyway. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
668 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
669 |
long c; int e, u; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
670 |
while ((((e = (int)(c = ctl)) | (u = (int)(c >>> 32))) & |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
671 |
(INT_SIGN|SHORT_SIGN)) == (INT_SIGN|SHORT_SIGN) && e >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
672 |
if (e > 0) { // release a waiting worker |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
673 |
int i; ForkJoinWorkerThread w; ForkJoinWorkerThread[] ws; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
674 |
if ((ws = workers) == null || |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
675 |
(i = ~e & SMASK) >= ws.length || |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
676 |
(w = ws[i]) == null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
677 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
678 |
long nc = (((long)(w.nextWait & E_MASK)) | |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
679 |
((long)(u + UAC_UNIT) << 32)); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
680 |
if (w.eventCount == e && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
681 |
UNSAFE.compareAndSwapLong(this, ctlOffset, c, nc)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
682 |
w.eventCount = (e + EC_UNIT) & E_MASK; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
683 |
if (w.parked) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
684 |
UNSAFE.unpark(w); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
685 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
686 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
687 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
688 |
else if (UNSAFE.compareAndSwapLong |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
689 |
(this, ctlOffset, c, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
690 |
(long)(((u + UTC_UNIT) & UTC_MASK) | |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
691 |
((u + UAC_UNIT) & UAC_MASK)) << 32)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
692 |
addWorker(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
693 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
694 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
695 |
} |
4110 | 696 |
} |
697 |
||
698 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
699 |
* Variant of signalWork to help release waiters on rescans. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
700 |
* Tries once to release a waiter if active count < 0. |
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 |
* @return false if failed due to contention, else true |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
703 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
704 |
private boolean tryReleaseWaiter() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
705 |
long c; int e, i; ForkJoinWorkerThread w; ForkJoinWorkerThread[] ws; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
706 |
if ((e = (int)(c = ctl)) > 0 && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
707 |
(int)(c >> AC_SHIFT) < 0 && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
708 |
(ws = workers) != null && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
709 |
(i = ~e & SMASK) < ws.length && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
710 |
(w = ws[i]) != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
711 |
long nc = ((long)(w.nextWait & E_MASK) | |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
712 |
((c + AC_UNIT) & (AC_MASK|TC_MASK))); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
713 |
if (w.eventCount != e || |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
714 |
!UNSAFE.compareAndSwapLong(this, ctlOffset, c, nc)) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
715 |
return false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
716 |
w.eventCount = (e + EC_UNIT) & E_MASK; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
717 |
if (w.parked) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
718 |
UNSAFE.unpark(w); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
719 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
720 |
return true; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
721 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
722 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
723 |
// Scanning for tasks |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
724 |
|
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 |
* Scans for and, if found, executes one task. Scans start at a |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
727 |
* random index of workers array, and randomly select the first |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
728 |
* (2*#workers)-1 probes, and then, if all empty, resort to 2 |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
729 |
* circular sweeps, which is necessary to check quiescence. and |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
730 |
* taking a submission only if no stealable tasks were found. The |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
731 |
* steal code inside the loop is a specialized form of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
732 |
* ForkJoinWorkerThread.deqTask, followed bookkeeping to support |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
733 |
* helpJoinTask and signal propagation. The code for submission |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
734 |
* queues is almost identical. On each steal, the worker completes |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
735 |
* not only the task, but also all local tasks that this task may |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
736 |
* have generated. On detecting staleness or contention when |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
737 |
* trying to take a task, this method returns without finishing |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
738 |
* sweep, which allows global state rechecks before retry. |
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 |
* @param w the worker |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
741 |
* @param a the number of active workers |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
742 |
* @return true if swept all queues without finding a task |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
743 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
744 |
private boolean scan(ForkJoinWorkerThread w, int a) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
745 |
int g = scanGuard; // mask 0 avoids useless scans if only one active |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
746 |
int m = (parallelism == 1 - a && blockedCount == 0) ? 0 : g & SMASK; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
747 |
ForkJoinWorkerThread[] ws = workers; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
748 |
if (ws == null || ws.length <= m) // staleness check |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
749 |
return false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
750 |
for (int r = w.seed, k = r, j = -(m + m); j <= m + m; ++j) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
751 |
ForkJoinTask<?> t; ForkJoinTask<?>[] q; int b, i; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
752 |
ForkJoinWorkerThread v = ws[k & m]; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
753 |
if (v != null && (b = v.queueBase) != v.queueTop && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
754 |
(q = v.queue) != null && (i = (q.length - 1) & b) >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
755 |
long u = (i << ASHIFT) + ABASE; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
756 |
if ((t = q[i]) != null && v.queueBase == b && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
757 |
UNSAFE.compareAndSwapObject(q, u, t, null)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
758 |
int d = (v.queueBase = b + 1) - v.queueTop; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
759 |
v.stealHint = w.poolIndex; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
760 |
if (d != 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
761 |
signalWork(); // propagate if nonempty |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
762 |
w.execTask(t); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
763 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
764 |
r ^= r << 13; r ^= r >>> 17; w.seed = r ^ (r << 5); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
765 |
return false; // store next seed |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
766 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
767 |
else if (j < 0) { // xorshift |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
768 |
r ^= r << 13; r ^= r >>> 17; k = r ^= r << 5; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
769 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
770 |
else |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
771 |
++k; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
772 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
773 |
if (scanGuard != g) // staleness check |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
774 |
return false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
775 |
else { // try to take submission |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
776 |
ForkJoinTask<?> t; ForkJoinTask<?>[] q; int b, i; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
777 |
if ((b = queueBase) != queueTop && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
778 |
(q = submissionQueue) != null && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
779 |
(i = (q.length - 1) & b) >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
780 |
long u = (i << ASHIFT) + ABASE; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
781 |
if ((t = q[i]) != null && queueBase == b && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
782 |
UNSAFE.compareAndSwapObject(q, u, t, null)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
783 |
queueBase = b + 1; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
784 |
w.execTask(t); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
785 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
786 |
return false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
787 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
788 |
return true; // all queues empty |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
789 |
} |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
790 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
791 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
792 |
/** |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
793 |
* Tries to enqueue worker w in wait queue and await change in |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
794 |
* worker's eventCount. If the pool is quiescent, possibly |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
795 |
* terminates worker upon exit. Otherwise, before blocking, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
796 |
* rescans queues to avoid missed signals. Upon finding work, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
797 |
* releases at least one worker (which may be the current |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
798 |
* worker). Rescans restart upon detected staleness or failure to |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
799 |
* release due to contention. Note the unusual conventions about |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
800 |
* Thread.interrupt here and elsewhere: Because interrupts are |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
801 |
* used solely to alert threads to check termination, which is |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
802 |
* checked here anyway, we clear status (using Thread.interrupted) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
803 |
* before any call to park, so that park does not immediately |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
804 |
* return due to status being set via some other unrelated call to |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
805 |
* interrupt in user code. |
6543 | 806 |
* |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
807 |
* @param w the calling worker |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
808 |
* @param c the ctl value on entry |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
809 |
* @return true if waited or another thread was released upon enq |
4110 | 810 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
811 |
private boolean tryAwaitWork(ForkJoinWorkerThread w, long c) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
812 |
int v = w.eventCount; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
813 |
w.nextWait = (int)c; // w's successor record |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
814 |
long nc = (long)(v & E_MASK) | ((c - AC_UNIT) & (AC_MASK|TC_MASK)); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
815 |
if (ctl != c || !UNSAFE.compareAndSwapLong(this, ctlOffset, c, nc)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
816 |
long d = ctl; // return true if lost to a deq, to force scan |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
817 |
return (int)d != (int)c && ((d - c) & AC_MASK) >= 0L; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
818 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
819 |
for (int sc = w.stealCount; sc != 0;) { // accumulate stealCount |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
820 |
long s = stealCount; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
821 |
if (UNSAFE.compareAndSwapLong(this, stealCountOffset, s, s + sc)) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
822 |
sc = w.stealCount = 0; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
823 |
else if (w.eventCount != v) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
824 |
return true; // update next time |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
825 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
826 |
if (parallelism + (int)(nc >> AC_SHIFT) == 0 && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
827 |
blockedCount == 0 && quiescerCount == 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
828 |
idleAwaitWork(w, nc, c, v); // quiescent |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
829 |
for (boolean rescanned = false;;) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
830 |
if (w.eventCount != v) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
831 |
return true; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
832 |
if (!rescanned) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
833 |
int g = scanGuard, m = g & SMASK; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
834 |
ForkJoinWorkerThread[] ws = workers; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
835 |
if (ws != null && m < ws.length) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
836 |
rescanned = true; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
837 |
for (int i = 0; i <= m; ++i) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
838 |
ForkJoinWorkerThread u = ws[i]; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
839 |
if (u != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
840 |
if (u.queueBase != u.queueTop && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
841 |
!tryReleaseWaiter()) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
842 |
rescanned = false; // contended |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
843 |
if (w.eventCount != v) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
844 |
return true; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
845 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
846 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
847 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
848 |
if (scanGuard != g || // stale |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
849 |
(queueBase != queueTop && !tryReleaseWaiter())) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
850 |
rescanned = false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
851 |
if (!rescanned) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
852 |
Thread.yield(); // reduce contention |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
853 |
else |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
854 |
Thread.interrupted(); // clear before park |
6543 | 855 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
856 |
else { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
857 |
w.parked = true; // must recheck |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
858 |
if (w.eventCount != v) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
859 |
w.parked = false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
860 |
return true; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
861 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
862 |
LockSupport.park(this); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
863 |
rescanned = w.parked = false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
864 |
} |
6543 | 865 |
} |
4110 | 866 |
} |
867 |
||
868 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
869 |
* If inactivating worker w has caused pool to become |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
870 |
* quiescent, check for pool termination, and wait for event |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
871 |
* for up to SHRINK_RATE nanosecs (rescans are unnecessary in |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
872 |
* this case because quiescence reflects consensus about lack |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
873 |
* of work). On timeout, if ctl has not changed, terminate the |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
874 |
* worker. Upon its termination (see deregisterWorker), it may |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
875 |
* wake up another worker to possibly repeat this process. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
876 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
877 |
* @param w the calling worker |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
878 |
* @param currentCtl the ctl value after enqueuing w |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
879 |
* @param prevCtl the ctl value if w terminated |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
880 |
* @param v the eventCount w awaits change |
4110 | 881 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
882 |
private void idleAwaitWork(ForkJoinWorkerThread w, long currentCtl, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
883 |
long prevCtl, int v) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
884 |
if (w.eventCount == v) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
885 |
if (shutdown) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
886 |
tryTerminate(false); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
887 |
ForkJoinTask.helpExpungeStaleExceptions(); // help clean weak refs |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
888 |
while (ctl == currentCtl) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
889 |
long startTime = System.nanoTime(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
890 |
w.parked = true; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
891 |
if (w.eventCount == v) // must recheck |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
892 |
LockSupport.parkNanos(this, SHRINK_RATE); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
893 |
w.parked = false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
894 |
if (w.eventCount != v) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
895 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
896 |
else if (System.nanoTime() - startTime < SHRINK_RATE) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
897 |
Thread.interrupted(); // spurious wakeup |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
898 |
else if (UNSAFE.compareAndSwapLong(this, ctlOffset, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
899 |
currentCtl, prevCtl)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
900 |
w.terminate = true; // restore previous |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
901 |
w.eventCount = ((int)currentCtl + EC_UNIT) & E_MASK; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
902 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
903 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
904 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
905 |
} |
6543 | 906 |
} |
907 |
||
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
908 |
// Submissions |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
909 |
|
6543 | 910 |
/** |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
911 |
* Enqueues the given task in the submissionQueue. Same idea as |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
912 |
* ForkJoinWorkerThread.pushTask except for use of submissionLock. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
913 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
914 |
* @param t the task |
6543 | 915 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
916 |
private void addSubmission(ForkJoinTask<?> t) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
917 |
final ReentrantLock lock = this.submissionLock; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
918 |
lock.lock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
919 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
920 |
ForkJoinTask<?>[] q; int s, m; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
921 |
if ((q = submissionQueue) != null) { // ignore if queue removed |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
922 |
long u = (((s = queueTop) & (m = q.length-1)) << ASHIFT)+ABASE; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
923 |
UNSAFE.putOrderedObject(q, u, t); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
924 |
queueTop = s + 1; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
925 |
if (s - queueBase == m) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
926 |
growSubmissionQueue(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
927 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
928 |
} finally { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
929 |
lock.unlock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
930 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
931 |
signalWork(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
932 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
933 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
934 |
// (pollSubmission is defined below with exported methods) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
935 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
936 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
937 |
* Creates or doubles submissionQueue array. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
938 |
* Basically identical to ForkJoinWorkerThread version. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
939 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
940 |
private void growSubmissionQueue() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
941 |
ForkJoinTask<?>[] oldQ = submissionQueue; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
942 |
int size = oldQ != null ? oldQ.length << 1 : INITIAL_QUEUE_CAPACITY; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
943 |
if (size > MAXIMUM_QUEUE_CAPACITY) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
944 |
throw new RejectedExecutionException("Queue capacity exceeded"); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
945 |
if (size < INITIAL_QUEUE_CAPACITY) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
946 |
size = INITIAL_QUEUE_CAPACITY; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
947 |
ForkJoinTask<?>[] q = submissionQueue = new ForkJoinTask<?>[size]; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
948 |
int mask = size - 1; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
949 |
int top = queueTop; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
950 |
int oldMask; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
951 |
if (oldQ != null && (oldMask = oldQ.length - 1) >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
952 |
for (int b = queueBase; b != top; ++b) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
953 |
long u = ((b & oldMask) << ASHIFT) + ABASE; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
954 |
Object x = UNSAFE.getObjectVolatile(oldQ, u); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
955 |
if (x != null && UNSAFE.compareAndSwapObject(oldQ, u, x, null)) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
956 |
UNSAFE.putObjectVolatile |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
957 |
(q, ((b & mask) << ASHIFT) + ABASE, x); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
958 |
} |
6543 | 959 |
} |
960 |
} |
|
961 |
||
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
962 |
// Blocking support |
6543 | 963 |
|
964 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
965 |
* Tries to increment blockedCount, decrement active count |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
966 |
* (sometimes implicitly) and possibly release or create a |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
967 |
* compensating worker in preparation for blocking. Fails |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
968 |
* on contention or termination. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
969 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
970 |
* @return true if the caller can block, else should recheck and retry |
6543 | 971 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
972 |
private boolean tryPreBlock() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
973 |
int b = blockedCount; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
974 |
if (UNSAFE.compareAndSwapInt(this, blockedCountOffset, b, b + 1)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
975 |
int pc = parallelism; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
976 |
do { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
977 |
ForkJoinWorkerThread[] ws; ForkJoinWorkerThread w; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
978 |
int e, ac, tc, rc, i; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
979 |
long c = ctl; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
980 |
int u = (int)(c >>> 32); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
981 |
if ((e = (int)c) < 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
982 |
// skip -- terminating |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
983 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
984 |
else if ((ac = (u >> UAC_SHIFT)) <= 0 && e != 0 && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
985 |
(ws = workers) != null && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
986 |
(i = ~e & SMASK) < ws.length && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
987 |
(w = ws[i]) != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
988 |
long nc = ((long)(w.nextWait & E_MASK) | |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
989 |
(c & (AC_MASK|TC_MASK))); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
990 |
if (w.eventCount == e && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
991 |
UNSAFE.compareAndSwapLong(this, ctlOffset, c, nc)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
992 |
w.eventCount = (e + EC_UNIT) & E_MASK; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
993 |
if (w.parked) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
994 |
UNSAFE.unpark(w); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
995 |
return true; // release an idle worker |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
996 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
997 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
998 |
else if ((tc = (short)(u >>> UTC_SHIFT)) >= 0 && ac + pc > 1) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
999 |
long nc = ((c - AC_UNIT) & AC_MASK) | (c & ~AC_MASK); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1000 |
if (UNSAFE.compareAndSwapLong(this, ctlOffset, c, nc)) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1001 |
return true; // no compensation needed |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1002 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1003 |
else if (tc + pc < MAX_ID) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1004 |
long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1005 |
if (UNSAFE.compareAndSwapLong(this, ctlOffset, c, nc)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1006 |
addWorker(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1007 |
return true; // create a replacement |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1008 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1009 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1010 |
// try to back out on any failure and let caller retry |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1011 |
} while (!UNSAFE.compareAndSwapInt(this, blockedCountOffset, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1012 |
b = blockedCount, b - 1)); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1013 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1014 |
return false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1015 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1016 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1017 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1018 |
* Decrements blockedCount and increments active count |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1019 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1020 |
private void postBlock() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1021 |
long c; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1022 |
do {} while (!UNSAFE.compareAndSwapLong(this, ctlOffset, // no mask |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1023 |
c = ctl, c + AC_UNIT)); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1024 |
int b; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1025 |
do {} while(!UNSAFE.compareAndSwapInt(this, blockedCountOffset, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1026 |
b = blockedCount, b - 1)); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1027 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1028 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1029 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1030 |
* Possibly blocks waiting for the given task to complete, or |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1031 |
* cancels the task if terminating. Fails to wait if contended. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1032 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1033 |
* @param joinMe the task |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1034 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1035 |
final void tryAwaitJoin(ForkJoinTask<?> joinMe) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1036 |
int s; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1037 |
Thread.interrupted(); // clear interrupts before checking termination |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1038 |
if (joinMe.status >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1039 |
if (tryPreBlock()) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1040 |
joinMe.tryAwaitDone(0L); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1041 |
postBlock(); |
6543 | 1042 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1043 |
else if ((ctl & STOP_BIT) != 0L) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1044 |
joinMe.cancelIgnoringExceptions(); |
6543 | 1045 |
} |
1046 |
} |
|
1047 |
||
1048 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1049 |
* Possibly blocks the given worker waiting for joinMe to |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1050 |
* complete or timeout |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1051 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1052 |
* @param joinMe the task |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1053 |
* @param millis the wait time for underlying Object.wait |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1054 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1055 |
final void timedAwaitJoin(ForkJoinTask<?> joinMe, long nanos) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1056 |
while (joinMe.status >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1057 |
Thread.interrupted(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1058 |
if ((ctl & STOP_BIT) != 0L) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1059 |
joinMe.cancelIgnoringExceptions(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1060 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1061 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1062 |
if (tryPreBlock()) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1063 |
long last = System.nanoTime(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1064 |
while (joinMe.status >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1065 |
long millis = TimeUnit.NANOSECONDS.toMillis(nanos); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1066 |
if (millis <= 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1067 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1068 |
joinMe.tryAwaitDone(millis); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1069 |
if (joinMe.status < 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1070 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1071 |
if ((ctl & STOP_BIT) != 0L) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1072 |
joinMe.cancelIgnoringExceptions(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1073 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1074 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1075 |
long now = System.nanoTime(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1076 |
nanos -= now - last; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1077 |
last = now; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1078 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1079 |
postBlock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1080 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1081 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1082 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1083 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1084 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1085 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1086 |
* If necessary, compensates for blocker, and blocks |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1087 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1088 |
private void awaitBlocker(ManagedBlocker blocker) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1089 |
throws InterruptedException { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1090 |
while (!blocker.isReleasable()) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1091 |
if (tryPreBlock()) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1092 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1093 |
do {} while (!blocker.isReleasable() && !blocker.block()); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1094 |
} finally { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1095 |
postBlock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1096 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1097 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1098 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1099 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1100 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1101 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1102 |
// Creating, registering and deregistring workers |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1103 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1104 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1105 |
* Tries to create and start a worker; minimally rolls back counts |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1106 |
* on failure. |
6543 | 1107 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1108 |
private void addWorker() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1109 |
Throwable ex = null; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1110 |
ForkJoinWorkerThread t = null; |
6543 | 1111 |
try { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1112 |
t = factory.newThread(this); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1113 |
} catch (Throwable e) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1114 |
ex = e; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1115 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1116 |
if (t == null) { // null or exceptional factory return |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1117 |
long c; // adjust counts |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1118 |
do {} while (!UNSAFE.compareAndSwapLong |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1119 |
(this, ctlOffset, c = ctl, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1120 |
(((c - AC_UNIT) & AC_MASK) | |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1121 |
((c - TC_UNIT) & TC_MASK) | |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1122 |
(c & ~(AC_MASK|TC_MASK))))); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1123 |
// Propagate exception if originating from an external caller |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1124 |
if (!tryTerminate(false) && ex != null && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1125 |
!(Thread.currentThread() instanceof ForkJoinWorkerThread)) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1126 |
UNSAFE.throwException(ex); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1127 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1128 |
else |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1129 |
t.start(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1130 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1131 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1132 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1133 |
* Callback from ForkJoinWorkerThread constructor to assign a |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1134 |
* public name |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1135 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1136 |
final String nextWorkerName() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1137 |
for (int n;;) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1138 |
if (UNSAFE.compareAndSwapInt(this, nextWorkerNumberOffset, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1139 |
n = nextWorkerNumber, ++n)) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1140 |
return workerNamePrefix + n; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1141 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1142 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1143 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1144 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1145 |
* Callback from ForkJoinWorkerThread constructor to |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1146 |
* determine its poolIndex and record in workers array. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1147 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1148 |
* @param w the worker |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1149 |
* @return the worker's pool index |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1150 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1151 |
final int registerWorker(ForkJoinWorkerThread w) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1152 |
/* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1153 |
* In the typical case, a new worker acquires the lock, uses |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1154 |
* next available index and returns quickly. Since we should |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1155 |
* not block callers (ultimately from signalWork or |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1156 |
* tryPreBlock) waiting for the lock needed to do this, we |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1157 |
* instead help release other workers while waiting for the |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1158 |
* lock. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1159 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1160 |
for (int g;;) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1161 |
ForkJoinWorkerThread[] ws; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1162 |
if (((g = scanGuard) & SG_UNIT) == 0 && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1163 |
UNSAFE.compareAndSwapInt(this, scanGuardOffset, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1164 |
g, g | SG_UNIT)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1165 |
int k = nextWorkerIndex; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1166 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1167 |
if ((ws = workers) != null) { // ignore on shutdown |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1168 |
int n = ws.length; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1169 |
if (k < 0 || k >= n || ws[k] != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1170 |
for (k = 0; k < n && ws[k] != null; ++k) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1171 |
; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1172 |
if (k == n) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1173 |
ws = workers = Arrays.copyOf(ws, n << 1); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1174 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1175 |
ws[k] = w; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1176 |
nextWorkerIndex = k + 1; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1177 |
int m = g & SMASK; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1178 |
g = k >= m? ((m << 1) + 1) & SMASK : g + (SG_UNIT<<1); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1179 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1180 |
} finally { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1181 |
scanGuard = g; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1182 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1183 |
return k; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1184 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1185 |
else if ((ws = workers) != null) { // help release others |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1186 |
for (ForkJoinWorkerThread u : ws) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1187 |
if (u != null && u.queueBase != u.queueTop) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1188 |
if (tryReleaseWaiter()) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1189 |
break; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1190 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1191 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1192 |
} |
6543 | 1193 |
} |
1194 |
} |
|
1195 |
||
1196 |
/** |
|
1197 |
* Final callback from terminating worker. Removes record of |
|
1198 |
* worker from array, and adjusts counts. If pool is shutting |
|
1199 |
* down, tries to complete termination. |
|
1200 |
* |
|
1201 |
* @param w the worker |
|
1202 |
*/ |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1203 |
final void deregisterWorker(ForkJoinWorkerThread w, Throwable ex) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1204 |
int idx = w.poolIndex; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1205 |
int sc = w.stealCount; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1206 |
int steps = 0; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1207 |
// Remove from array, adjust worker counts and collect steal count. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1208 |
// We can intermix failed removes or adjusts with steal updates |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1209 |
do { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1210 |
long s, c; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1211 |
int g; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1212 |
if (steps == 0 && ((g = scanGuard) & SG_UNIT) == 0 && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1213 |
UNSAFE.compareAndSwapInt(this, scanGuardOffset, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1214 |
g, g |= SG_UNIT)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1215 |
ForkJoinWorkerThread[] ws = workers; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1216 |
if (ws != null && idx >= 0 && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1217 |
idx < ws.length && ws[idx] == w) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1218 |
ws[idx] = null; // verify |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1219 |
nextWorkerIndex = idx; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1220 |
scanGuard = g + SG_UNIT; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1221 |
steps = 1; |
6543 | 1222 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1223 |
if (steps == 1 && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1224 |
UNSAFE.compareAndSwapLong(this, ctlOffset, c = ctl, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1225 |
(((c - AC_UNIT) & AC_MASK) | |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1226 |
((c - TC_UNIT) & TC_MASK) | |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1227 |
(c & ~(AC_MASK|TC_MASK))))) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1228 |
steps = 2; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1229 |
if (sc != 0 && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1230 |
UNSAFE.compareAndSwapLong(this, stealCountOffset, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1231 |
s = stealCount, s + sc)) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1232 |
sc = 0; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1233 |
} while (steps != 2 || sc != 0); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1234 |
if (!tryTerminate(false)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1235 |
if (ex != null) // possibly replace if died abnormally |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1236 |
signalWork(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1237 |
else |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1238 |
tryReleaseWaiter(); |
6543 | 1239 |
} |
1240 |
} |
|
1241 |
||
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1242 |
// Shutdown and termination |
6543 | 1243 |
|
1244 |
/** |
|
1245 |
* Possibly initiates and/or completes termination. |
|
1246 |
* |
|
1247 |
* @param now if true, unconditionally terminate, else only |
|
1248 |
* if shutdown and empty queue and no active workers |
|
1249 |
* @return true if now terminating or terminated |
|
1250 |
*/ |
|
1251 |
private boolean tryTerminate(boolean now) { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1252 |
long c; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1253 |
while (((c = ctl) & STOP_BIT) == 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1254 |
if (!now) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1255 |
if ((int)(c >> AC_SHIFT) != -parallelism) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1256 |
return false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1257 |
if (!shutdown || blockedCount != 0 || quiescerCount != 0 || |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1258 |
queueBase != queueTop) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1259 |
if (ctl == c) // staleness check |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1260 |
return false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1261 |
continue; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1262 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1263 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1264 |
if (UNSAFE.compareAndSwapLong(this, ctlOffset, c, c | STOP_BIT)) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1265 |
startTerminating(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1266 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1267 |
if ((short)(c >>> TC_SHIFT) == -parallelism) { // signal when 0 workers |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1268 |
final ReentrantLock lock = this.submissionLock; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1269 |
lock.lock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1270 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1271 |
termination.signalAll(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1272 |
} finally { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1273 |
lock.unlock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1274 |
} |
6543 | 1275 |
} |
4110 | 1276 |
return true; |
1277 |
} |
|
1278 |
||
1279 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1280 |
* Runs up to three passes through workers: (0) Setting |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1281 |
* termination status for each worker, followed by wakeups up to |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1282 |
* queued workers; (1) helping cancel tasks; (2) interrupting |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1283 |
* lagging threads (likely in external tasks, but possibly also |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1284 |
* blocked in joins). Each pass repeats previous steps because of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1285 |
* potential lagging thread creation. |
4110 | 1286 |
*/ |
6543 | 1287 |
private void startTerminating() { |
1288 |
cancelSubmissions(); |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1289 |
for (int pass = 0; pass < 3; ++pass) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1290 |
ForkJoinWorkerThread[] ws = workers; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1291 |
if (ws != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1292 |
for (ForkJoinWorkerThread w : ws) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1293 |
if (w != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1294 |
w.terminate = true; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1295 |
if (pass > 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1296 |
w.cancelTasks(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1297 |
if (pass > 1 && !w.isInterrupted()) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1298 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1299 |
w.interrupt(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1300 |
} catch (SecurityException ignore) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1301 |
} |
6543 | 1302 |
} |
1303 |
} |
|
1304 |
} |
|
1305 |
} |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1306 |
terminateWaiters(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1307 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1308 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1309 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1310 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1311 |
/** |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1312 |
* Polls and cancels all submissions. Called only during termination. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1313 |
*/ |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1314 |
private void cancelSubmissions() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1315 |
while (queueBase != queueTop) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1316 |
ForkJoinTask<?> task = pollSubmission(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1317 |
if (task != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1318 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1319 |
task.cancel(false); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1320 |
} catch (Throwable ignore) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1321 |
} |
6543 | 1322 |
} |
4110 | 1323 |
} |
1324 |
} |
|
1325 |
||
1326 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1327 |
* Tries to set the termination status of waiting workers, and |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1328 |
* then wakes them up (after which they will terminate). |
6543 | 1329 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1330 |
private void terminateWaiters() { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1331 |
ForkJoinWorkerThread[] ws = workers; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1332 |
if (ws != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1333 |
ForkJoinWorkerThread w; long c; int i, e; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1334 |
int n = ws.length; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1335 |
while ((i = ~(e = (int)(c = ctl)) & SMASK) < n && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1336 |
(w = ws[i]) != null && w.eventCount == (e & E_MASK)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1337 |
if (UNSAFE.compareAndSwapLong(this, ctlOffset, c, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1338 |
(long)(w.nextWait & E_MASK) | |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1339 |
((c + AC_UNIT) & AC_MASK) | |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1340 |
(c & (TC_MASK|STOP_BIT)))) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1341 |
w.terminate = true; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1342 |
w.eventCount = e + EC_UNIT; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1343 |
if (w.parked) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1344 |
UNSAFE.unpark(w); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1345 |
} |
6543 | 1346 |
} |
1347 |
} |
|
1348 |
} |
|
1349 |
||
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1350 |
// misc ForkJoinWorkerThread support |
6543 | 1351 |
|
1352 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1353 |
* Increment or decrement quiescerCount. Needed only to prevent |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1354 |
* triggering shutdown if a worker is transiently inactive while |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1355 |
* checking quiescence. |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1356 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1357 |
* @param delta 1 for increment, -1 for decrement |
6543 | 1358 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1359 |
final void addQuiescerCount(int delta) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1360 |
int c; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1361 |
do {} while(!UNSAFE.compareAndSwapInt(this, quiescerCountOffset, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1362 |
c = quiescerCount, c + delta)); |
6543 | 1363 |
} |
1364 |
||
1365 |
/** |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1366 |
* Directly increment or decrement active count without |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1367 |
* queuing. This method is used to transiently assert inactivation |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1368 |
* while checking quiescence. |
6543 | 1369 |
* |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1370 |
* @param delta 1 for increment, -1 for decrement |
4110 | 1371 |
*/ |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1372 |
final void addActiveCount(int delta) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1373 |
long d = delta < 0 ? -AC_UNIT : AC_UNIT; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1374 |
long c; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1375 |
do {} while (!UNSAFE.compareAndSwapLong(this, ctlOffset, c = ctl, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1376 |
((c + d) & AC_MASK) | |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1377 |
(c & ~AC_MASK))); |
6543 | 1378 |
} |
1379 |
||
1380 |
/** |
|
1381 |
* Returns the approximate (non-atomic) number of idle threads per |
|
1382 |
* active thread. |
|
1383 |
*/ |
|
1384 |
final int idlePerActive() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1385 |
// Approximate at powers of two for small values, saturate past 4 |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1386 |
int p = parallelism; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1387 |
int a = p + (int)(ctl >> AC_SHIFT); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1388 |
return (a > (p >>>= 1) ? 0 : |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1389 |
a > (p >>>= 1) ? 1 : |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1390 |
a > (p >>>= 1) ? 2 : |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1391 |
a > (p >>>= 1) ? 4 : |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1392 |
8); |
6543 | 1393 |
} |
1394 |
||
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1395 |
// Exported methods |
4110 | 1396 |
|
1397 |
// Constructors |
|
1398 |
||
1399 |
/** |
|
1400 |
* Creates a {@code ForkJoinPool} with parallelism equal to {@link |
|
6543 | 1401 |
* java.lang.Runtime#availableProcessors}, using the {@linkplain |
1402 |
* #defaultForkJoinWorkerThreadFactory default thread factory}, |
|
1403 |
* no UncaughtExceptionHandler, and non-async LIFO processing mode. |
|
4110 | 1404 |
* |
1405 |
* @throws SecurityException if a security manager exists and |
|
1406 |
* the caller is not permitted to modify threads |
|
1407 |
* because it does not hold {@link |
|
1408 |
* java.lang.RuntimePermission}{@code ("modifyThread")} |
|
1409 |
*/ |
|
1410 |
public ForkJoinPool() { |
|
1411 |
this(Runtime.getRuntime().availableProcessors(), |
|
6543 | 1412 |
defaultForkJoinWorkerThreadFactory, null, false); |
4110 | 1413 |
} |
1414 |
||
1415 |
/** |
|
1416 |
* Creates a {@code ForkJoinPool} with the indicated parallelism |
|
6543 | 1417 |
* level, the {@linkplain |
1418 |
* #defaultForkJoinWorkerThreadFactory default thread factory}, |
|
1419 |
* no UncaughtExceptionHandler, and non-async LIFO processing mode. |
|
4110 | 1420 |
* |
1421 |
* @param parallelism the parallelism level |
|
1422 |
* @throws IllegalArgumentException if parallelism less than or |
|
1423 |
* equal to zero, or greater than implementation limit |
|
1424 |
* @throws SecurityException if a security manager exists and |
|
1425 |
* the caller is not permitted to modify threads |
|
1426 |
* because it does not hold {@link |
|
1427 |
* java.lang.RuntimePermission}{@code ("modifyThread")} |
|
1428 |
*/ |
|
1429 |
public ForkJoinPool(int parallelism) { |
|
6543 | 1430 |
this(parallelism, defaultForkJoinWorkerThreadFactory, null, false); |
4110 | 1431 |
} |
1432 |
||
1433 |
/** |
|
6543 | 1434 |
* Creates a {@code ForkJoinPool} with the given parameters. |
4110 | 1435 |
* |
6543 | 1436 |
* @param parallelism the parallelism level. For default value, |
1437 |
* use {@link java.lang.Runtime#availableProcessors}. |
|
1438 |
* @param factory the factory for creating new threads. For default value, |
|
1439 |
* use {@link #defaultForkJoinWorkerThreadFactory}. |
|
1440 |
* @param handler the handler for internal worker threads that |
|
1441 |
* terminate due to unrecoverable errors encountered while executing |
|
1442 |
* tasks. For default value, use {@code null}. |
|
1443 |
* @param asyncMode if true, |
|
1444 |
* establishes local first-in-first-out scheduling mode for forked |
|
1445 |
* tasks that are never joined. This mode may be more appropriate |
|
1446 |
* than default locally stack-based mode in applications in which |
|
1447 |
* worker threads only process event-style asynchronous tasks. |
|
1448 |
* For default value, use {@code false}. |
|
4110 | 1449 |
* @throws IllegalArgumentException if parallelism less than or |
1450 |
* equal to zero, or greater than implementation limit |
|
1451 |
* @throws NullPointerException if the factory is null |
|
1452 |
* @throws SecurityException if a security manager exists and |
|
1453 |
* the caller is not permitted to modify threads |
|
1454 |
* because it does not hold {@link |
|
1455 |
* java.lang.RuntimePermission}{@code ("modifyThread")} |
|
1456 |
*/ |
|
6543 | 1457 |
public ForkJoinPool(int parallelism, |
1458 |
ForkJoinWorkerThreadFactory factory, |
|
1459 |
Thread.UncaughtExceptionHandler handler, |
|
1460 |
boolean asyncMode) { |
|
1461 |
checkPermission(); |
|
4110 | 1462 |
if (factory == null) |
1463 |
throw new NullPointerException(); |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1464 |
if (parallelism <= 0 || parallelism > MAX_ID) |
6543 | 1465 |
throw new IllegalArgumentException(); |
4110 | 1466 |
this.parallelism = parallelism; |
6543 | 1467 |
this.factory = factory; |
1468 |
this.ueh = handler; |
|
1469 |
this.locallyFifo = asyncMode; |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1470 |
long np = (long)(-parallelism); // offset ctl counts |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1471 |
this.ctl = ((np << AC_SHIFT) & AC_MASK) | ((np << TC_SHIFT) & TC_MASK); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1472 |
this.submissionQueue = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY]; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1473 |
// initialize workers array with room for 2*parallelism if possible |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1474 |
int n = parallelism << 1; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1475 |
if (n >= MAX_ID) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1476 |
n = MAX_ID; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1477 |
else { // See Hackers Delight, sec 3.2, where n < (1 << 16) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1478 |
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1479 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1480 |
workers = new ForkJoinWorkerThread[n + 1]; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1481 |
this.submissionLock = new ReentrantLock(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1482 |
this.termination = submissionLock.newCondition(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1483 |
StringBuilder sb = new StringBuilder("ForkJoinPool-"); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1484 |
sb.append(poolNumberGenerator.incrementAndGet()); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1485 |
sb.append("-worker-"); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1486 |
this.workerNamePrefix = sb.toString(); |
4110 | 1487 |
} |
1488 |
||
1489 |
// Execution methods |
|
1490 |
||
1491 |
/** |
|
1492 |
* Performs the given task, returning its result upon completion. |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1493 |
* If the computation encounters an unchecked Exception or Error, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1494 |
* it is rethrown as the outcome of this invocation. Rethrown |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1495 |
* exceptions behave in the same way as regular exceptions, but, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1496 |
* when possible, contain stack traces (as displayed for example |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1497 |
* using {@code ex.printStackTrace()}) of both the current thread |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1498 |
* as well as the thread actually encountering the exception; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1499 |
* minimally only the latter. |
4110 | 1500 |
* |
1501 |
* @param task the task |
|
1502 |
* @return the task's result |
|
1503 |
* @throws NullPointerException if the task is null |
|
1504 |
* @throws RejectedExecutionException if the task cannot be |
|
1505 |
* scheduled for execution |
|
1506 |
*/ |
|
1507 |
public <T> T invoke(ForkJoinTask<T> task) { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1508 |
Thread t = Thread.currentThread(); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1509 |
if (task == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1510 |
throw new NullPointerException(); |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1511 |
if (shutdown) |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1512 |
throw new RejectedExecutionException(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1513 |
if ((t instanceof ForkJoinWorkerThread) && |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1514 |
((ForkJoinWorkerThread)t).pool == this) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1515 |
return task.invoke(); // bypass submit if in same pool |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1516 |
else { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1517 |
addSubmission(task); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1518 |
return task.join(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1519 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1520 |
} |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1521 |
|
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1522 |
/** |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1523 |
* Unless terminating, forks task if within an ongoing FJ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1524 |
* computation in the current pool, else submits as external task. |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1525 |
*/ |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1526 |
private <T> void forkOrSubmit(ForkJoinTask<T> task) { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1527 |
ForkJoinWorkerThread w; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1528 |
Thread t = Thread.currentThread(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1529 |
if (shutdown) |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1530 |
throw new RejectedExecutionException(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1531 |
if ((t instanceof ForkJoinWorkerThread) && |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1532 |
(w = (ForkJoinWorkerThread)t).pool == this) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1533 |
w.pushTask(task); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1534 |
else |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1535 |
addSubmission(task); |
4110 | 1536 |
} |
1537 |
||
1538 |
/** |
|
1539 |
* Arranges for (asynchronous) execution of the given task. |
|
1540 |
* |
|
1541 |
* @param task the task |
|
1542 |
* @throws NullPointerException if the task is null |
|
1543 |
* @throws RejectedExecutionException if the task cannot be |
|
1544 |
* scheduled for execution |
|
1545 |
*/ |
|
1546 |
public void execute(ForkJoinTask<?> task) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1547 |
if (task == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1548 |
throw new NullPointerException(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1549 |
forkOrSubmit(task); |
4110 | 1550 |
} |
1551 |
||
1552 |
// AbstractExecutorService methods |
|
1553 |
||
1554 |
/** |
|
1555 |
* @throws NullPointerException if the task is null |
|
1556 |
* @throws RejectedExecutionException if the task cannot be |
|
1557 |
* scheduled for execution |
|
1558 |
*/ |
|
1559 |
public void execute(Runnable task) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1560 |
if (task == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1561 |
throw new NullPointerException(); |
4110 | 1562 |
ForkJoinTask<?> job; |
1563 |
if (task instanceof ForkJoinTask<?>) // avoid re-wrap |
|
1564 |
job = (ForkJoinTask<?>) task; |
|
1565 |
else |
|
1566 |
job = ForkJoinTask.adapt(task, null); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1567 |
forkOrSubmit(job); |
4110 | 1568 |
} |
1569 |
||
1570 |
/** |
|
6543 | 1571 |
* Submits a ForkJoinTask for execution. |
1572 |
* |
|
1573 |
* @param task the task to submit |
|
1574 |
* @return the task |
|
1575 |
* @throws NullPointerException if the task is null |
|
1576 |
* @throws RejectedExecutionException if the task cannot be |
|
1577 |
* scheduled for execution |
|
1578 |
*/ |
|
1579 |
public <T> ForkJoinTask<T> submit(ForkJoinTask<T> task) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1580 |
if (task == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1581 |
throw new NullPointerException(); |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1582 |
forkOrSubmit(task); |
6543 | 1583 |
return task; |
1584 |
} |
|
1585 |
||
1586 |
/** |
|
4110 | 1587 |
* @throws NullPointerException if the task is null |
1588 |
* @throws RejectedExecutionException if the task cannot be |
|
1589 |
* scheduled for execution |
|
1590 |
*/ |
|
1591 |
public <T> ForkJoinTask<T> submit(Callable<T> task) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1592 |
if (task == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1593 |
throw new NullPointerException(); |
4110 | 1594 |
ForkJoinTask<T> job = ForkJoinTask.adapt(task); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1595 |
forkOrSubmit(job); |
4110 | 1596 |
return job; |
1597 |
} |
|
1598 |
||
1599 |
/** |
|
1600 |
* @throws NullPointerException if the task is null |
|
1601 |
* @throws RejectedExecutionException if the task cannot be |
|
1602 |
* scheduled for execution |
|
1603 |
*/ |
|
1604 |
public <T> ForkJoinTask<T> submit(Runnable task, T result) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1605 |
if (task == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1606 |
throw new NullPointerException(); |
4110 | 1607 |
ForkJoinTask<T> job = ForkJoinTask.adapt(task, result); |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1608 |
forkOrSubmit(job); |
4110 | 1609 |
return job; |
1610 |
} |
|
1611 |
||
1612 |
/** |
|
1613 |
* @throws NullPointerException if the task is null |
|
1614 |
* @throws RejectedExecutionException if the task cannot be |
|
1615 |
* scheduled for execution |
|
1616 |
*/ |
|
1617 |
public ForkJoinTask<?> submit(Runnable task) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1618 |
if (task == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1619 |
throw new NullPointerException(); |
4110 | 1620 |
ForkJoinTask<?> job; |
1621 |
if (task instanceof ForkJoinTask<?>) // avoid re-wrap |
|
1622 |
job = (ForkJoinTask<?>) task; |
|
1623 |
else |
|
1624 |
job = ForkJoinTask.adapt(task, null); |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1625 |
forkOrSubmit(job); |
4110 | 1626 |
return job; |
1627 |
} |
|
1628 |
||
1629 |
/** |
|
1630 |
* @throws NullPointerException {@inheritDoc} |
|
1631 |
* @throws RejectedExecutionException {@inheritDoc} |
|
1632 |
*/ |
|
1633 |
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) { |
|
1634 |
ArrayList<ForkJoinTask<T>> forkJoinTasks = |
|
1635 |
new ArrayList<ForkJoinTask<T>>(tasks.size()); |
|
1636 |
for (Callable<T> task : tasks) |
|
1637 |
forkJoinTasks.add(ForkJoinTask.adapt(task)); |
|
1638 |
invoke(new InvokeAll<T>(forkJoinTasks)); |
|
1639 |
||
1640 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
|
6543 | 1641 |
List<Future<T>> futures = (List<Future<T>>) (List) forkJoinTasks; |
4110 | 1642 |
return futures; |
1643 |
} |
|
1644 |
||
1645 |
static final class InvokeAll<T> extends RecursiveAction { |
|
1646 |
final ArrayList<ForkJoinTask<T>> tasks; |
|
1647 |
InvokeAll(ArrayList<ForkJoinTask<T>> tasks) { this.tasks = tasks; } |
|
1648 |
public void compute() { |
|
1649 |
try { invokeAll(tasks); } |
|
1650 |
catch (Exception ignore) {} |
|
1651 |
} |
|
1652 |
private static final long serialVersionUID = -7914297376763021607L; |
|
1653 |
} |
|
1654 |
||
1655 |
/** |
|
1656 |
* Returns the factory used for constructing new workers. |
|
1657 |
* |
|
1658 |
* @return the factory used for constructing new workers |
|
1659 |
*/ |
|
1660 |
public ForkJoinWorkerThreadFactory getFactory() { |
|
1661 |
return factory; |
|
1662 |
} |
|
1663 |
||
1664 |
/** |
|
1665 |
* Returns the handler for internal worker threads that terminate |
|
1666 |
* due to unrecoverable errors encountered while executing tasks. |
|
1667 |
* |
|
1668 |
* @return the handler, or {@code null} if none |
|
1669 |
*/ |
|
1670 |
public Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() { |
|
6543 | 1671 |
return ueh; |
4110 | 1672 |
} |
1673 |
||
1674 |
/** |
|
1675 |
* Returns the targeted parallelism level of this pool. |
|
1676 |
* |
|
1677 |
* @return the targeted parallelism level of this pool |
|
1678 |
*/ |
|
1679 |
public int getParallelism() { |
|
1680 |
return parallelism; |
|
1681 |
} |
|
1682 |
||
1683 |
/** |
|
1684 |
* Returns the number of worker threads that have started but not |
|
6543 | 1685 |
* yet terminated. The result returned by this method may differ |
4110 | 1686 |
* from {@link #getParallelism} when threads are created to |
1687 |
* maintain parallelism when others are cooperatively blocked. |
|
1688 |
* |
|
1689 |
* @return the number of worker threads |
|
1690 |
*/ |
|
1691 |
public int getPoolSize() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1692 |
return parallelism + (short)(ctl >>> TC_SHIFT); |
4110 | 1693 |
} |
1694 |
||
1695 |
/** |
|
1696 |
* Returns {@code true} if this pool uses local first-in-first-out |
|
1697 |
* scheduling mode for forked tasks that are never joined. |
|
1698 |
* |
|
1699 |
* @return {@code true} if this pool uses async mode |
|
1700 |
*/ |
|
1701 |
public boolean getAsyncMode() { |
|
1702 |
return locallyFifo; |
|
1703 |
} |
|
1704 |
||
1705 |
/** |
|
1706 |
* Returns an estimate of the number of worker threads that are |
|
1707 |
* not blocked waiting to join tasks or for other managed |
|
6543 | 1708 |
* synchronization. This method may overestimate the |
1709 |
* number of running threads. |
|
4110 | 1710 |
* |
1711 |
* @return the number of worker threads |
|
1712 |
*/ |
|
1713 |
public int getRunningThreadCount() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1714 |
int r = parallelism + (int)(ctl >> AC_SHIFT); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1715 |
return r <= 0? 0 : r; // suppress momentarily negative values |
4110 | 1716 |
} |
1717 |
||
1718 |
/** |
|
1719 |
* Returns an estimate of the number of threads that are currently |
|
1720 |
* stealing or executing tasks. This method may overestimate the |
|
1721 |
* number of active threads. |
|
1722 |
* |
|
1723 |
* @return the number of active threads |
|
1724 |
*/ |
|
1725 |
public int getActiveThreadCount() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1726 |
int r = parallelism + (int)(ctl >> AC_SHIFT) + blockedCount; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1727 |
return r <= 0? 0 : r; // suppress momentarily negative values |
4110 | 1728 |
} |
1729 |
||
1730 |
/** |
|
1731 |
* Returns {@code true} if all worker threads are currently idle. |
|
1732 |
* An idle worker is one that cannot obtain a task to execute |
|
1733 |
* because none are available to steal from other threads, and |
|
1734 |
* there are no pending submissions to the pool. This method is |
|
1735 |
* conservative; it might not return {@code true} immediately upon |
|
1736 |
* idleness of all threads, but will eventually become true if |
|
1737 |
* threads remain inactive. |
|
1738 |
* |
|
1739 |
* @return {@code true} if all threads are currently idle |
|
1740 |
*/ |
|
1741 |
public boolean isQuiescent() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1742 |
return parallelism + (int)(ctl >> AC_SHIFT) + blockedCount == 0; |
4110 | 1743 |
} |
1744 |
||
1745 |
/** |
|
1746 |
* Returns an estimate of the total number of tasks stolen from |
|
1747 |
* one thread's work queue by another. The reported value |
|
1748 |
* underestimates the actual total number of steals when the pool |
|
1749 |
* is not quiescent. This value may be useful for monitoring and |
|
1750 |
* tuning fork/join programs: in general, steal counts should be |
|
1751 |
* high enough to keep threads busy, but low enough to avoid |
|
1752 |
* overhead and contention across threads. |
|
1753 |
* |
|
1754 |
* @return the number of steals |
|
1755 |
*/ |
|
1756 |
public long getStealCount() { |
|
6543 | 1757 |
return stealCount; |
4110 | 1758 |
} |
1759 |
||
1760 |
/** |
|
1761 |
* Returns an estimate of the total number of tasks currently held |
|
1762 |
* in queues by worker threads (but not including tasks submitted |
|
1763 |
* to the pool that have not begun executing). This value is only |
|
1764 |
* an approximation, obtained by iterating across all threads in |
|
1765 |
* the pool. This method may be useful for tuning task |
|
1766 |
* granularities. |
|
1767 |
* |
|
1768 |
* @return the number of queued tasks |
|
1769 |
*/ |
|
1770 |
public long getQueuedTaskCount() { |
|
1771 |
long count = 0; |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1772 |
ForkJoinWorkerThread[] ws; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1773 |
if ((short)(ctl >>> TC_SHIFT) > -parallelism && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1774 |
(ws = workers) != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1775 |
for (ForkJoinWorkerThread w : ws) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1776 |
if (w != null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1777 |
count -= w.queueBase - w.queueTop; // must read base first |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1778 |
} |
4110 | 1779 |
return count; |
1780 |
} |
|
1781 |
||
1782 |
/** |
|
1783 |
* Returns an estimate of the number of tasks submitted to this |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1784 |
* pool that have not yet begun executing. This method may take |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1785 |
* time proportional to the number of submissions. |
4110 | 1786 |
* |
1787 |
* @return the number of queued submissions |
|
1788 |
*/ |
|
1789 |
public int getQueuedSubmissionCount() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1790 |
return -queueBase + queueTop; |
4110 | 1791 |
} |
1792 |
||
1793 |
/** |
|
1794 |
* Returns {@code true} if there are any tasks submitted to this |
|
1795 |
* pool that have not yet begun executing. |
|
1796 |
* |
|
1797 |
* @return {@code true} if there are any queued submissions |
|
1798 |
*/ |
|
1799 |
public boolean hasQueuedSubmissions() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1800 |
return queueBase != queueTop; |
4110 | 1801 |
} |
1802 |
||
1803 |
/** |
|
1804 |
* Removes and returns the next unexecuted submission if one is |
|
1805 |
* available. This method may be useful in extensions to this |
|
1806 |
* class that re-assign work in systems with multiple pools. |
|
1807 |
* |
|
1808 |
* @return the next submission, or {@code null} if none |
|
1809 |
*/ |
|
1810 |
protected ForkJoinTask<?> pollSubmission() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1811 |
ForkJoinTask<?> t; ForkJoinTask<?>[] q; int b, i; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1812 |
while ((b = queueBase) != queueTop && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1813 |
(q = submissionQueue) != null && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1814 |
(i = (q.length - 1) & b) >= 0) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1815 |
long u = (i << ASHIFT) + ABASE; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1816 |
if ((t = q[i]) != null && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1817 |
queueBase == b && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1818 |
UNSAFE.compareAndSwapObject(q, u, t, null)) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1819 |
queueBase = b + 1; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1820 |
return t; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1821 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1822 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1823 |
return null; |
4110 | 1824 |
} |
1825 |
||
1826 |
/** |
|
1827 |
* Removes all available unexecuted submitted and forked tasks |
|
1828 |
* from scheduling queues and adds them to the given collection, |
|
1829 |
* without altering their execution status. These may include |
|
1830 |
* artificially generated or wrapped tasks. This method is |
|
1831 |
* designed to be invoked only when the pool is known to be |
|
1832 |
* quiescent. Invocations at other times may not remove all |
|
1833 |
* tasks. A failure encountered while attempting to add elements |
|
1834 |
* to collection {@code c} may result in elements being in |
|
1835 |
* neither, either or both collections when the associated |
|
1836 |
* exception is thrown. The behavior of this operation is |
|
1837 |
* undefined if the specified collection is modified while the |
|
1838 |
* operation is in progress. |
|
1839 |
* |
|
1840 |
* @param c the collection to transfer elements into |
|
1841 |
* @return the number of elements transferred |
|
1842 |
*/ |
|
1843 |
protected int drainTasksTo(Collection<? super ForkJoinTask<?>> c) { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1844 |
int count = 0; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1845 |
while (queueBase != queueTop) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1846 |
ForkJoinTask<?> t = pollSubmission(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1847 |
if (t != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1848 |
c.add(t); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1849 |
++count; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1850 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1851 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1852 |
ForkJoinWorkerThread[] ws; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1853 |
if ((short)(ctl >>> TC_SHIFT) > -parallelism && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1854 |
(ws = workers) != null) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1855 |
for (ForkJoinWorkerThread w : ws) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1856 |
if (w != null) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1857 |
count += w.drainTasksTo(c); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1858 |
} |
6543 | 1859 |
return count; |
4110 | 1860 |
} |
1861 |
||
1862 |
/** |
|
1863 |
* Returns a string identifying this pool, as well as its state, |
|
1864 |
* including indications of run state, parallelism level, and |
|
1865 |
* worker and task counts. |
|
1866 |
* |
|
1867 |
* @return a string identifying this pool, as well as its state |
|
1868 |
*/ |
|
1869 |
public String toString() { |
|
1870 |
long st = getStealCount(); |
|
1871 |
long qt = getQueuedTaskCount(); |
|
1872 |
long qs = getQueuedSubmissionCount(); |
|
6543 | 1873 |
int pc = parallelism; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1874 |
long c = ctl; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1875 |
int tc = pc + (short)(c >>> TC_SHIFT); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1876 |
int rc = pc + (int)(c >> AC_SHIFT); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1877 |
if (rc < 0) // ignore transient negative |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1878 |
rc = 0; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1879 |
int ac = rc + blockedCount; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1880 |
String level; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1881 |
if ((c & STOP_BIT) != 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1882 |
level = (tc == 0)? "Terminated" : "Terminating"; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1883 |
else |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1884 |
level = shutdown? "Shutting down" : "Running"; |
4110 | 1885 |
return super.toString() + |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1886 |
"[" + level + |
6543 | 1887 |
", parallelism = " + pc + |
1888 |
", size = " + tc + |
|
1889 |
", active = " + ac + |
|
1890 |
", running = " + rc + |
|
4110 | 1891 |
", steals = " + st + |
1892 |
", tasks = " + qt + |
|
1893 |
", submissions = " + qs + |
|
1894 |
"]"; |
|
1895 |
} |
|
1896 |
||
1897 |
/** |
|
1898 |
* Initiates an orderly shutdown in which previously submitted |
|
1899 |
* tasks are executed, but no new tasks will be accepted. |
|
1900 |
* Invocation has no additional effect if already shut down. |
|
1901 |
* Tasks that are in the process of being submitted concurrently |
|
1902 |
* during the course of this method may or may not be rejected. |
|
1903 |
* |
|
1904 |
* @throws SecurityException if a security manager exists and |
|
1905 |
* the caller is not permitted to modify threads |
|
1906 |
* because it does not hold {@link |
|
1907 |
* java.lang.RuntimePermission}{@code ("modifyThread")} |
|
1908 |
*/ |
|
1909 |
public void shutdown() { |
|
1910 |
checkPermission(); |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1911 |
shutdown = true; |
6543 | 1912 |
tryTerminate(false); |
4110 | 1913 |
} |
1914 |
||
1915 |
/** |
|
1916 |
* Attempts to cancel and/or stop all tasks, and reject all |
|
1917 |
* subsequently submitted tasks. Tasks that are in the process of |
|
1918 |
* being submitted or executed concurrently during the course of |
|
1919 |
* this method may or may not be rejected. This method cancels |
|
1920 |
* both existing and unexecuted tasks, in order to permit |
|
1921 |
* termination in the presence of task dependencies. So the method |
|
1922 |
* always returns an empty list (unlike the case for some other |
|
1923 |
* Executors). |
|
1924 |
* |
|
1925 |
* @return an empty list |
|
1926 |
* @throws SecurityException if a security manager exists and |
|
1927 |
* the caller is not permitted to modify threads |
|
1928 |
* because it does not hold {@link |
|
1929 |
* java.lang.RuntimePermission}{@code ("modifyThread")} |
|
1930 |
*/ |
|
1931 |
public List<Runnable> shutdownNow() { |
|
1932 |
checkPermission(); |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1933 |
shutdown = true; |
6543 | 1934 |
tryTerminate(true); |
4110 | 1935 |
return Collections.emptyList(); |
1936 |
} |
|
1937 |
||
1938 |
/** |
|
1939 |
* Returns {@code true} if all tasks have completed following shut down. |
|
1940 |
* |
|
1941 |
* @return {@code true} if all tasks have completed following shut down |
|
1942 |
*/ |
|
1943 |
public boolean isTerminated() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1944 |
long c = ctl; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1945 |
return ((c & STOP_BIT) != 0L && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1946 |
(short)(c >>> TC_SHIFT) == -parallelism); |
4110 | 1947 |
} |
1948 |
||
1949 |
/** |
|
1950 |
* Returns {@code true} if the process of termination has |
|
1951 |
* commenced but not yet completed. This method may be useful for |
|
1952 |
* debugging. A return of {@code true} reported a sufficient |
|
1953 |
* period after shutdown may indicate that submitted tasks have |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1954 |
* ignored or suppressed interruption, or are waiting for IO, |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1955 |
* causing this executor not to properly terminate. (See the |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1956 |
* advisory notes for class {@link ForkJoinTask} stating that |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1957 |
* tasks should not normally entail blocking operations. But if |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1958 |
* they do, they must abort them on interrupt.) |
4110 | 1959 |
* |
1960 |
* @return {@code true} if terminating but not yet terminated |
|
1961 |
*/ |
|
1962 |
public boolean isTerminating() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1963 |
long c = ctl; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1964 |
return ((c & STOP_BIT) != 0L && |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1965 |
(short)(c >>> TC_SHIFT) != -parallelism); |
4110 | 1966 |
} |
1967 |
||
1968 |
/** |
|
6674 | 1969 |
* Returns true if terminating or terminated. Used by ForkJoinWorkerThread. |
1970 |
*/ |
|
1971 |
final boolean isAtLeastTerminating() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1972 |
return (ctl & STOP_BIT) != 0L; |
6674 | 1973 |
} |
1974 |
||
1975 |
/** |
|
4110 | 1976 |
* Returns {@code true} if this pool has been shut down. |
1977 |
* |
|
1978 |
* @return {@code true} if this pool has been shut down |
|
1979 |
*/ |
|
1980 |
public boolean isShutdown() { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1981 |
return shutdown; |
4110 | 1982 |
} |
1983 |
||
1984 |
/** |
|
1985 |
* Blocks until all tasks have completed execution after a shutdown |
|
1986 |
* request, or the timeout occurs, or the current thread is |
|
1987 |
* interrupted, whichever happens first. |
|
1988 |
* |
|
1989 |
* @param timeout the maximum time to wait |
|
1990 |
* @param unit the time unit of the timeout argument |
|
1991 |
* @return {@code true} if this executor terminated and |
|
1992 |
* {@code false} if the timeout elapsed before termination |
|
1993 |
* @throws InterruptedException if interrupted while waiting |
|
1994 |
*/ |
|
1995 |
public boolean awaitTermination(long timeout, TimeUnit unit) |
|
1996 |
throws InterruptedException { |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1997 |
long nanos = unit.toNanos(timeout); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1998 |
final ReentrantLock lock = this.submissionLock; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1999 |
lock.lock(); |
4110 | 2000 |
try { |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2001 |
for (;;) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2002 |
if (isTerminated()) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2003 |
return true; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2004 |
if (nanos <= 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2005 |
return false; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2006 |
nanos = termination.awaitNanos(nanos); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2007 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2008 |
} finally { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2009 |
lock.unlock(); |
4110 | 2010 |
} |
2011 |
} |
|
2012 |
||
2013 |
/** |
|
2014 |
* Interface for extending managed parallelism for tasks running |
|
2015 |
* in {@link ForkJoinPool}s. |
|
2016 |
* |
|
6543 | 2017 |
* <p>A {@code ManagedBlocker} provides two methods. Method |
2018 |
* {@code isReleasable} must return {@code true} if blocking is |
|
2019 |
* not necessary. Method {@code block} blocks the current thread |
|
2020 |
* if necessary (perhaps internally invoking {@code isReleasable} |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2021 |
* before actually blocking). These actions are performed by any |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2022 |
* thread invoking {@link ForkJoinPool#managedBlock}. The |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2023 |
* unusual methods in this API accommodate synchronizers that may, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2024 |
* but don't usually, block for long periods. Similarly, they |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2025 |
* allow more efficient internal handling of cases in which |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2026 |
* additional workers may be, but usually are not, needed to |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2027 |
* ensure sufficient parallelism. Toward this end, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2028 |
* implementations of method {@code isReleasable} must be amenable |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2029 |
* to repeated invocation. |
4110 | 2030 |
* |
2031 |
* <p>For example, here is a ManagedBlocker based on a |
|
2032 |
* ReentrantLock: |
|
2033 |
* <pre> {@code |
|
2034 |
* class ManagedLocker implements ManagedBlocker { |
|
2035 |
* final ReentrantLock lock; |
|
2036 |
* boolean hasLock = false; |
|
2037 |
* ManagedLocker(ReentrantLock lock) { this.lock = lock; } |
|
2038 |
* public boolean block() { |
|
2039 |
* if (!hasLock) |
|
2040 |
* lock.lock(); |
|
2041 |
* return true; |
|
2042 |
* } |
|
2043 |
* public boolean isReleasable() { |
|
2044 |
* return hasLock || (hasLock = lock.tryLock()); |
|
2045 |
* } |
|
2046 |
* }}</pre> |
|
6543 | 2047 |
* |
2048 |
* <p>Here is a class that possibly blocks waiting for an |
|
2049 |
* item on a given queue: |
|
2050 |
* <pre> {@code |
|
2051 |
* class QueueTaker<E> implements ManagedBlocker { |
|
2052 |
* final BlockingQueue<E> queue; |
|
2053 |
* volatile E item = null; |
|
2054 |
* QueueTaker(BlockingQueue<E> q) { this.queue = q; } |
|
2055 |
* public boolean block() throws InterruptedException { |
|
2056 |
* if (item == null) |
|
2057 |
* item = queue.take(); |
|
2058 |
* return true; |
|
2059 |
* } |
|
2060 |
* public boolean isReleasable() { |
|
2061 |
* return item != null || (item = queue.poll()) != null; |
|
2062 |
* } |
|
2063 |
* public E getItem() { // call after pool.managedBlock completes |
|
2064 |
* return item; |
|
2065 |
* } |
|
2066 |
* }}</pre> |
|
4110 | 2067 |
*/ |
2068 |
public static interface ManagedBlocker { |
|
2069 |
/** |
|
2070 |
* Possibly blocks the current thread, for example waiting for |
|
2071 |
* a lock or condition. |
|
2072 |
* |
|
2073 |
* @return {@code true} if no additional blocking is necessary |
|
2074 |
* (i.e., if isReleasable would return true) |
|
2075 |
* @throws InterruptedException if interrupted while waiting |
|
2076 |
* (the method is not required to do so, but is allowed to) |
|
2077 |
*/ |
|
2078 |
boolean block() throws InterruptedException; |
|
2079 |
||
2080 |
/** |
|
2081 |
* Returns {@code true} if blocking is unnecessary. |
|
2082 |
*/ |
|
2083 |
boolean isReleasable(); |
|
2084 |
} |
|
2085 |
||
2086 |
/** |
|
2087 |
* Blocks in accord with the given blocker. If the current thread |
|
2088 |
* is a {@link ForkJoinWorkerThread}, this method possibly |
|
2089 |
* arranges for a spare thread to be activated if necessary to |
|
6543 | 2090 |
* ensure sufficient parallelism while the current thread is blocked. |
4110 | 2091 |
* |
2092 |
* <p>If the caller is not a {@link ForkJoinTask}, this method is |
|
2093 |
* behaviorally equivalent to |
|
2094 |
* <pre> {@code |
|
2095 |
* while (!blocker.isReleasable()) |
|
2096 |
* if (blocker.block()) |
|
2097 |
* return; |
|
2098 |
* }</pre> |
|
2099 |
* |
|
2100 |
* If the caller is a {@code ForkJoinTask}, then the pool may |
|
2101 |
* first be expanded to ensure parallelism, and later adjusted. |
|
2102 |
* |
|
2103 |
* @param blocker the blocker |
|
2104 |
* @throws InterruptedException if blocker.block did so |
|
2105 |
*/ |
|
6543 | 2106 |
public static void managedBlock(ManagedBlocker blocker) |
4110 | 2107 |
throws InterruptedException { |
2108 |
Thread t = Thread.currentThread(); |
|
6543 | 2109 |
if (t instanceof ForkJoinWorkerThread) { |
2110 |
ForkJoinWorkerThread w = (ForkJoinWorkerThread) t; |
|
2111 |
w.pool.awaitBlocker(blocker); |
|
4110 | 2112 |
} |
6543 | 2113 |
else { |
2114 |
do {} while (!blocker.isReleasable() && !blocker.block()); |
|
2115 |
} |
|
4110 | 2116 |
} |
2117 |
||
2118 |
// AbstractExecutorService overrides. These rely on undocumented |
|
2119 |
// fact that ForkJoinTask.adapt returns ForkJoinTasks that also |
|
2120 |
// implement RunnableFuture. |
|
2121 |
||
2122 |
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { |
|
2123 |
return (RunnableFuture<T>) ForkJoinTask.adapt(runnable, value); |
|
2124 |
} |
|
2125 |
||
2126 |
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { |
|
2127 |
return (RunnableFuture<T>) ForkJoinTask.adapt(callable); |
|
2128 |
} |
|
2129 |
||
2130 |
// Unsafe mechanics |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2131 |
private static final sun.misc.Unsafe UNSAFE; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2132 |
private static final long ctlOffset; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2133 |
private static final long stealCountOffset; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2134 |
private static final long blockedCountOffset; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2135 |
private static final long quiescerCountOffset; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2136 |
private static final long scanGuardOffset; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2137 |
private static final long nextWorkerNumberOffset; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2138 |
private static final long ABASE; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2139 |
private static final int ASHIFT; |
4110 | 2140 |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2141 |
static { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2142 |
poolNumberGenerator = new AtomicInteger(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2143 |
workerSeedGenerator = new Random(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2144 |
modifyThreadPermission = new RuntimePermission("modifyThread"); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2145 |
defaultForkJoinWorkerThreadFactory = |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2146 |
new DefaultForkJoinWorkerThreadFactory(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2147 |
int s; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2148 |
try { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2149 |
UNSAFE = sun.misc.Unsafe.getUnsafe(); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2150 |
Class k = ForkJoinPool.class; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2151 |
ctlOffset = UNSAFE.objectFieldOffset |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2152 |
(k.getDeclaredField("ctl")); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2153 |
stealCountOffset = UNSAFE.objectFieldOffset |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2154 |
(k.getDeclaredField("stealCount")); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2155 |
blockedCountOffset = UNSAFE.objectFieldOffset |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2156 |
(k.getDeclaredField("blockedCount")); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2157 |
quiescerCountOffset = UNSAFE.objectFieldOffset |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2158 |
(k.getDeclaredField("quiescerCount")); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2159 |
scanGuardOffset = UNSAFE.objectFieldOffset |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2160 |
(k.getDeclaredField("scanGuard")); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2161 |
nextWorkerNumberOffset = UNSAFE.objectFieldOffset |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2162 |
(k.getDeclaredField("nextWorkerNumber")); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2163 |
Class a = ForkJoinTask[].class; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2164 |
ABASE = UNSAFE.arrayBaseOffset(a); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2165 |
s = UNSAFE.arrayIndexScale(a); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2166 |
} catch (Exception e) { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2167 |
throw new Error(e); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2168 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2169 |
if ((s & (s-1)) != 0) |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2170 |
throw new Error("data type scale not a power of two"); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2171 |
ASHIFT = 31 - Integer.numberOfLeadingZeros(s); |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2172 |
} |
4110 | 2173 |
|
2174 |
} |