author | dl |
Tue, 10 Apr 2018 11:29:37 -0700 | |
changeset 49563 | 79d2c9da2c26 |
parent 47216 | 71c04702a3d5 |
child 52957 | c7c285b0b640 |
permissions | -rw-r--r-- |
4110 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
4110 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
4110 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
4110 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
8765
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
4110 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
37 |
||
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
38 |
import java.lang.Thread.UncaughtExceptionHandler; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
39 |
import java.lang.invoke.MethodHandles; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
40 |
import java.lang.invoke.VarHandle; |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41131
diff
changeset
|
41 |
import java.security.AccessController; |
32988 | 42 |
import java.security.AccessControlContext; |
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
43 |
import java.security.Permission; |
32988 | 44 |
import java.security.Permissions; |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41131
diff
changeset
|
45 |
import java.security.PrivilegedAction; |
32988 | 46 |
import java.security.ProtectionDomain; |
4110 | 47 |
import java.util.ArrayList; |
48 |
import java.util.Collection; |
|
49 |
import java.util.Collections; |
|
50 |
import java.util.List; |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
51 |
import java.util.function.Predicate; |
32988 | 52 |
import java.util.concurrent.locks.LockSupport; |
4110 | 53 |
|
54 |
/** |
|
55 |
* An {@link ExecutorService} for running {@link ForkJoinTask}s. |
|
56 |
* A {@code ForkJoinPool} provides the entry point for submissions |
|
6543 | 57 |
* from non-{@code ForkJoinTask} clients, as well as management and |
4110 | 58 |
* monitoring operations. |
59 |
* |
|
60 |
* <p>A {@code ForkJoinPool} differs from other kinds of {@link |
|
61 |
* ExecutorService} mainly by virtue of employing |
|
62 |
* <em>work-stealing</em>: all threads in the pool attempt to find and |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
63 |
* execute tasks submitted to the pool and/or created by other active |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
64 |
* tasks (eventually blocking waiting for work if none exist). This |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
65 |
* enables efficient processing when most tasks spawn other subtasks |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
66 |
* (as do most {@code ForkJoinTask}s), as well as when many small |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
67 |
* tasks are submitted to the pool from external clients. Especially |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
68 |
* when setting <em>asyncMode</em> to true in constructors, {@code |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
69 |
* ForkJoinPool}s may also be appropriate for use with event-style |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41131
diff
changeset
|
70 |
* tasks that are never joined. All worker threads are initialized |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41131
diff
changeset
|
71 |
* with {@link Thread#isDaemon} set {@code true}. |
4110 | 72 |
* |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
73 |
* <p>A static {@link #commonPool()} is available and appropriate for |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
74 |
* most applications. The common pool is used by any ForkJoinTask that |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
75 |
* is not explicitly submitted to a specified pool. Using the common |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
76 |
* pool normally reduces resource usage (its threads are slowly |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
77 |
* reclaimed during periods of non-use, and reinstated upon subsequent |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
78 |
* use). |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
79 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
80 |
* <p>For applications that require separate or custom pools, a {@code |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
81 |
* ForkJoinPool} may be constructed with a given target parallelism |
26448 | 82 |
* level; by default, equal to the number of available processors. |
83 |
* The pool attempts to maintain enough active (or available) threads |
|
84 |
* by dynamically adding, suspending, or resuming internal worker |
|
18790 | 85 |
* threads, even if some tasks are stalled waiting to join others. |
86 |
* However, no such adjustments are guaranteed in the face of blocked |
|
87 |
* I/O or other unmanaged synchronization. The nested {@link |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
88 |
* ManagedBlocker} interface enables extension of the kinds of |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
89 |
* synchronization accommodated. The default policies may be |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
90 |
* overridden using a constructor with parameters corresponding to |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
91 |
* those documented in class {@link ThreadPoolExecutor}. |
4110 | 92 |
* |
93 |
* <p>In addition to execution and lifecycle control methods, this |
|
94 |
* class provides status check methods (for example |
|
95 |
* {@link #getStealCount}) that are intended to aid in developing, |
|
96 |
* tuning, and monitoring fork/join applications. Also, method |
|
97 |
* {@link #toString} returns indications of pool state in a |
|
98 |
* convenient form for informal monitoring. |
|
99 |
* |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
100 |
* <p>As is the case with other ExecutorServices, there are three |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
101 |
* main task execution methods summarized in the following table. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
102 |
* These are designed to be used primarily by clients not already |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
103 |
* engaged in fork/join computations in the current pool. The main |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
104 |
* forms of these methods accept instances of {@code ForkJoinTask}, |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
105 |
* but overloaded forms also allow mixed execution of plain {@code |
6543 | 106 |
* Runnable}- or {@code Callable}- based activities as well. However, |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
107 |
* tasks that are already executing in a pool should normally instead |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
108 |
* use the within-computation forms listed in the table unless using |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
109 |
* async event-style tasks that are not usually joined, in which case |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
110 |
* there is little difference among choice of methods. |
6543 | 111 |
* |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
112 |
* <table class="plain"> |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
113 |
* <caption>Summary of task execution methods</caption> |
6543 | 114 |
* <tr> |
115 |
* <td></td> |
|
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
45184
diff
changeset
|
116 |
* <th scope="col"> Call from non-fork/join clients</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
45184
diff
changeset
|
117 |
* <th scope="col"> Call from within fork/join computations</th> |
6543 | 118 |
* </tr> |
119 |
* <tr> |
|
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
45184
diff
changeset
|
120 |
* <th scope="row" style="text-align:left"> Arrange async execution</th> |
6543 | 121 |
* <td> {@link #execute(ForkJoinTask)}</td> |
122 |
* <td> {@link ForkJoinTask#fork}</td> |
|
123 |
* </tr> |
|
124 |
* <tr> |
|
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
45184
diff
changeset
|
125 |
* <th scope="row" style="text-align:left"> Await and obtain result</th> |
6543 | 126 |
* <td> {@link #invoke(ForkJoinTask)}</td> |
127 |
* <td> {@link ForkJoinTask#invoke}</td> |
|
128 |
* </tr> |
|
129 |
* <tr> |
|
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
45184
diff
changeset
|
130 |
* <th scope="row" style="text-align:left"> Arrange exec and obtain Future</th> |
6543 | 131 |
* <td> {@link #submit(ForkJoinTask)}</td> |
132 |
* <td> {@link ForkJoinTask#fork} (ForkJoinTasks <em>are</em> Futures)</td> |
|
133 |
* </tr> |
|
134 |
* </table> |
|
135 |
* |
|
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
136 |
* <p>The parameters used to construct the common pool may be controlled by |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
137 |
* setting the following {@linkplain System#getProperty system properties}: |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
138 |
* <ul> |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
139 |
* <li>{@code java.util.concurrent.ForkJoinPool.common.parallelism} |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
140 |
* - the parallelism level, a non-negative integer |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
141 |
* <li>{@code java.util.concurrent.ForkJoinPool.common.threadFactory} |
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
142 |
* - the class name of a {@link ForkJoinWorkerThreadFactory}. |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
143 |
* The {@linkplain ClassLoader#getSystemClassLoader() system class loader} |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
144 |
* is used to load this class. |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
145 |
* <li>{@code java.util.concurrent.ForkJoinPool.common.exceptionHandler} |
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
146 |
* - the class name of a {@link UncaughtExceptionHandler}. |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
147 |
* The {@linkplain ClassLoader#getSystemClassLoader() system class loader} |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
148 |
* is used to load this class. |
26448 | 149 |
* <li>{@code java.util.concurrent.ForkJoinPool.common.maximumSpares} |
150 |
* - the maximum number of allowed extra threads to maintain target |
|
151 |
* parallelism (default 256). |
|
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
152 |
* </ul> |
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
153 |
* If no thread factory is supplied via a system property, then the |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
154 |
* common pool uses a factory that uses the system class loader as the |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
155 |
* {@linkplain Thread#getContextClassLoader() thread context class loader}. |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
156 |
* In addition, if a {@link SecurityManager} is present, then |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
157 |
* the common pool uses a factory supplying threads that have no |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
158 |
* {@link Permissions} enabled. |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
159 |
* |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
160 |
* Upon any error in establishing these settings, default parameters |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
161 |
* are used. It is possible to disable or limit the use of threads in |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
162 |
* the common pool by setting the parallelism property to zero, and/or |
19412 | 163 |
* using a factory that may return {@code null}. However doing so may |
164 |
* cause unjoined tasks to never be executed. |
|
4110 | 165 |
* |
166 |
* <p><b>Implementation notes</b>: This implementation restricts the |
|
167 |
* maximum number of running threads to 32767. Attempts to create |
|
168 |
* pools with greater than the maximum number result in |
|
169 |
* {@code IllegalArgumentException}. |
|
170 |
* |
|
171 |
* <p>This implementation rejects submitted tasks (that is, by throwing |
|
6543 | 172 |
* {@link RejectedExecutionException}) only when the pool is shut down |
173 |
* or internal resources have been exhausted. |
|
4110 | 174 |
* |
175 |
* @since 1.7 |
|
176 |
* @author Doug Lea |
|
177 |
*/ |
|
178 |
public class ForkJoinPool extends AbstractExecutorService { |
|
179 |
||
180 |
/* |
|
6543 | 181 |
* Implementation Overview |
182 |
* |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
183 |
* This class and its nested classes provide the main |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
184 |
* functionality and control for a set of worker threads: |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
185 |
* Submissions from non-FJ threads enter into submission queues. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
186 |
* Workers take these tasks and typically split them into subtasks |
49563 | 187 |
* that may be stolen by other workers. Work-stealing based on |
188 |
* randomized scans generally leads to better throughput than |
|
189 |
* "work dealing" in which producers assign tasks to idle threads, |
|
190 |
* in part because threads that have finished other tasks before |
|
191 |
* the signalled thread wakes up (which can be a long time) can |
|
192 |
* take the task instead. Preference rules give first priority to |
|
193 |
* processing tasks from their own queues (LIFO or FIFO, depending |
|
194 |
* on mode), then to randomized FIFO steals of tasks in other |
|
195 |
* queues. This framework began as vehicle for supporting |
|
196 |
* tree-structured parallelism using work-stealing. Over time, |
|
197 |
* its scalability advantages led to extensions and changes to |
|
198 |
* better support more diverse usage contexts. Because most |
|
199 |
* internal methods and nested classes are interrelated, their |
|
200 |
* main rationale and descriptions are presented here; individual |
|
201 |
* methods and nested classes contain only brief comments about |
|
202 |
* details. |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
203 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
204 |
* WorkQueues |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
205 |
* ========== |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
206 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
207 |
* Most operations occur within work-stealing queues (in nested |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
208 |
* class WorkQueue). These are special forms of Deques that |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
209 |
* support only three of the four possible end-operations -- push, |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
210 |
* pop, and poll (aka steal), under the further constraints that |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
211 |
* push and pop are called only from the owning thread (or, as |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
212 |
* extended here, under a lock), while poll may be called from |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
213 |
* other threads. (If you are unfamiliar with them, you probably |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
214 |
* want to read Herlihy and Shavit's book "The Art of |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
215 |
* Multiprocessor programming", chapter 16 describing these in |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
216 |
* more detail before proceeding.) The main work-stealing queue |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
217 |
* design is roughly similar to those in the papers "Dynamic |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
218 |
* Circular Work-Stealing Deque" by Chase and Lev, SPAA 2005 |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
219 |
* (http://research.sun.com/scalable/pubs/index.html) and |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
220 |
* "Idempotent work stealing" by Michael, Saraswat, and Vechev, |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
221 |
* PPoPP 2009 (http://portal.acm.org/citation.cfm?id=1504186). |
26448 | 222 |
* The main differences ultimately stem from GC requirements that |
223 |
* we null out taken slots as soon as we can, to maintain as small |
|
224 |
* a footprint as possible even in programs generating huge |
|
225 |
* numbers of tasks. To accomplish this, we shift the CAS |
|
226 |
* arbitrating pop vs poll (steal) from being on the indices |
|
227 |
* ("base" and "top") to the slots themselves. |
|
228 |
* |
|
32988 | 229 |
* Adding tasks then takes the form of a classic array push(task) |
230 |
* in a circular buffer: |
|
231 |
* q.array[q.top++ % length] = task; |
|
26448 | 232 |
* |
233 |
* (The actual code needs to null-check and size-check the array, |
|
32988 | 234 |
* uses masking, not mod, for indexing a power-of-two-sized array, |
49563 | 235 |
* adds a release fence for publication, and possibly signals |
236 |
* waiting workers to start scanning -- see below.) Both a |
|
237 |
* successful pop and poll mainly entail a CAS of a slot from |
|
238 |
* non-null to null. |
|
26448 | 239 |
* |
240 |
* The pop operation (always performed by owner) is: |
|
32988 | 241 |
* if ((the task at top slot is not null) and |
26448 | 242 |
* (CAS slot to null)) |
243 |
* decrement top and return task; |
|
244 |
* |
|
245 |
* And the poll operation (usually by a stealer) is |
|
32988 | 246 |
* if ((the task at base slot is not null) and |
26448 | 247 |
* (CAS slot to null)) |
248 |
* increment base and return task; |
|
249 |
* |
|
49563 | 250 |
* There are several variants of each of these. Most uses occur |
251 |
* within operations that also interleave contention or emptiness |
|
252 |
* tracking or inspection of elements before extracting them, so |
|
253 |
* must interleave these with the above code. When performed by |
|
254 |
* owner, getAndSet is used instead of CAS (see for example method |
|
255 |
* nextLocalTask) which is usually more efficient, and possible |
|
256 |
* because the top index cannot independently change during the |
|
257 |
* operation. |
|
32988 | 258 |
* |
259 |
* Memory ordering. See "Correct and Efficient Work-Stealing for |
|
260 |
* Weak Memory Models" by Le, Pop, Cohen, and Nardelli, PPoPP 2013 |
|
261 |
* (http://www.di.ens.fr/~zappa/readings/ppopp13.pdf) for an |
|
262 |
* analysis of memory ordering requirements in work-stealing |
|
263 |
* algorithms similar to (but different than) the one used here. |
|
264 |
* Extracting tasks in array slots via (fully fenced) CAS provides |
|
265 |
* primary synchronization. The base and top indices imprecisely |
|
49563 | 266 |
* guide where to extract from. We do not usually require strict |
267 |
* orderings of array and index updates. Many index accesses use |
|
268 |
* plain mode, with ordering constrained by surrounding context |
|
269 |
* (usually with respect to element CASes or the two WorkQueue |
|
270 |
* volatile fields source and phase). When not otherwise already |
|
271 |
* constrained, reads of "base" by queue owners use acquire-mode, |
|
272 |
* and some externally callable methods preface accesses with |
|
273 |
* acquire fences. Additionally, to ensure that index update |
|
274 |
* writes are not coalesced or postponed in loops etc, "opaque" |
|
275 |
* mode is used in a few cases where timely writes are not |
|
276 |
* otherwise ensured. The "locked" versions of push- and pop- |
|
277 |
* based methods for shared queues differ from owned versions |
|
278 |
* because locking already forces some of the ordering. |
|
279 |
* |
|
280 |
* Because indices and slot contents cannot always be consistent, |
|
281 |
* a check that base == top indicates (momentary) emptiness, but |
|
282 |
* otherwise may err on the side of possibly making the queue |
|
283 |
* appear nonempty when a push, pop, or poll have not fully |
|
284 |
* committed, or making it appear empty when an update of top has |
|
285 |
* not yet been visibly written. (Method isEmpty() checks the |
|
286 |
* case of a partially completed removal of the last element.) |
|
287 |
* Because of this, the poll operation, considered individually, |
|
288 |
* is not wait-free. One thief cannot successfully continue until |
|
289 |
* another in-progress one (or, if previously empty, a push) |
|
290 |
* visibly completes. This can stall threads when required to |
|
291 |
* consume from a given queue (see method poll()). However, in |
|
292 |
* the aggregate, we ensure at least probabilistic |
|
32988 | 293 |
* non-blockingness. If an attempted steal fails, a scanning |
294 |
* thief chooses a different random victim target to try next. So, |
|
295 |
* in order for one thief to progress, it suffices for any |
|
49563 | 296 |
* in-progress poll or new push on any empty queue to complete. |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
297 |
* |
26448 | 298 |
* This approach also enables support of a user mode in which |
299 |
* local task processing is in FIFO, not LIFO order, simply by |
|
300 |
* using poll rather than pop. This can be useful in |
|
301 |
* message-passing frameworks in which tasks are never joined. |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
302 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
303 |
* WorkQueues are also used in a similar way for tasks submitted |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
304 |
* to the pool. We cannot mix these tasks in the same queues used |
26448 | 305 |
* by workers. Instead, we randomly associate submission queues |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
306 |
* with submitting threads, using a form of hashing. The |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
307 |
* ThreadLocalRandom probe value serves as a hash code for |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
308 |
* choosing existing queues, and may be randomly repositioned upon |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
309 |
* contention with other submitters. In essence, submitters act |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
310 |
* like workers except that they are restricted to executing local |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
311 |
* tasks that they submitted. Insertion of tasks in shared mode |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
312 |
* requires a lock but we use only a simple spinlock (using field |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
313 |
* phase), because submitters encountering a busy queue move to a |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
314 |
* different position to use or create other queues -- they block |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
315 |
* only when creating and registering new queues. Because it is |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
316 |
* used only as a spinlock, unlocking requires only a "releasing" |
49563 | 317 |
* store (using setRelease) unless otherwise signalling. |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
318 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
319 |
* Management |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
320 |
* ========== |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
321 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
322 |
* The main throughput advantages of work-stealing stem from |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
323 |
* decentralized control -- workers mostly take tasks from |
26448 | 324 |
* themselves or each other, at rates that can exceed a billion |
325 |
* per second. The pool itself creates, activates (enables |
|
326 |
* scanning for and running tasks), deactivates, blocks, and |
|
327 |
* terminates threads, all with minimal central information. |
|
328 |
* There are only a few properties that we can globally track or |
|
329 |
* maintain, so we pack them into a small number of variables, |
|
330 |
* often maintaining atomicity without blocking or locking. |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
331 |
* Nearly all essentially atomic control state is held in a few |
26448 | 332 |
* volatile variables that are by far most often read (not |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
333 |
* written) as status and consistency checks. We pack as much |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
334 |
* information into them as we can. |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
335 |
* |
26448 | 336 |
* Field "ctl" contains 64 bits holding information needed to |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
337 |
* atomically decide to add, enqueue (on an event queue), and |
49563 | 338 |
* dequeue and release workers. To enable this packing, we |
339 |
* restrict maximum parallelism to (1<<15)-1 (which is far in |
|
340 |
* excess of normal operating range) to allow ids, counts, and |
|
341 |
* their negations (used for thresholding) to fit into 16bit |
|
26448 | 342 |
* subfields. |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
343 |
* |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
344 |
* Field "mode" holds configuration parameters as well as lifetime |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
345 |
* status, atomically and monotonically setting SHUTDOWN, STOP, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
346 |
* and finally TERMINATED bits. |
26448 | 347 |
* |
32988 | 348 |
* Field "workQueues" holds references to WorkQueues. It is |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
349 |
* updated (only during worker creation and termination) under |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
350 |
* lock (using field workerNamePrefix as lock), but is otherwise |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
351 |
* concurrently readable, and accessed directly. We also ensure |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
352 |
* that uses of the array reference itself never become too stale |
49563 | 353 |
* in case of resizing, by arranging that (re-)reads are separated |
354 |
* by at least one acquiring read access. To simplify index-based |
|
355 |
* operations, the array size is always a power of two, and all |
|
356 |
* readers must tolerate null slots. Worker queues are at odd |
|
357 |
* indices. Shared (submission) queues are at even indices, up to |
|
358 |
* a maximum of 64 slots, to limit growth even if the array needs |
|
359 |
* to expand to add more workers. Grouping them together in this |
|
360 |
* way simplifies and speeds up task scanning. |
|
6543 | 361 |
* |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
362 |
* All worker thread creation is on-demand, triggered by task |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
363 |
* submissions, replacement of terminated workers, and/or |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
364 |
* compensation for blocked workers. However, all other support |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
365 |
* code is set up to work with other policies. To ensure that we |
32988 | 366 |
* do not hold on to worker references that would prevent GC, all |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
367 |
* accesses to workQueues are via indices into the workQueues |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
368 |
* array (which is one source of some of the messy code |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
369 |
* constructions here). In essence, the workQueues array serves as |
26448 | 370 |
* a weak reference mechanism. Thus for example the stack top |
371 |
* subfield of ctl stores indices, not references. |
|
372 |
* |
|
373 |
* Queuing Idle Workers. Unlike HPC work-stealing frameworks, we |
|
374 |
* cannot let workers spin indefinitely scanning for tasks when |
|
375 |
* none can be found immediately, and we cannot start/resume |
|
376 |
* workers unless there appear to be tasks available. On the |
|
377 |
* other hand, we must quickly prod them into action when new |
|
378 |
* tasks are submitted or generated. In many usages, ramp-up time |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
379 |
* is the main limiting factor in overall performance, which is |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
380 |
* compounded at program start-up by JIT compilation and |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
381 |
* allocation. So we streamline this as much as possible. |
26448 | 382 |
* |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
383 |
* The "ctl" field atomically maintains total worker and |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
384 |
* "released" worker counts, plus the head of the available worker |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
385 |
* queue (actually stack, represented by the lower 32bit subfield |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
386 |
* of ctl). Released workers are those known to be scanning for |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
387 |
* and/or running tasks. Unreleased ("available") workers are |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
388 |
* recorded in the ctl stack. These workers are made available for |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
389 |
* signalling by enqueuing in ctl (see method runWorker). The |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
390 |
* "queue" is a form of Treiber stack. This is ideal for |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
391 |
* activating threads in most-recently used order, and improves |
26448 | 392 |
* performance and locality, outweighing the disadvantages of |
393 |
* being prone to contention and inability to release a worker |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
394 |
* unless it is topmost on stack. To avoid missed signal problems |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
395 |
* inherent in any wait/signal design, available workers rescan |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
396 |
* for (and if found run) tasks after enqueuing. Normally their |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
397 |
* release status will be updated while doing so, but the released |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
398 |
* worker ctl count may underestimate the number of active |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
399 |
* threads. (However, it is still possible to determine quiescence |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
400 |
* via a validation traversal -- see isQuiescent). After an |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
401 |
* unsuccessful rescan, available workers are blocked until |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
402 |
* signalled (see signalWork). The top stack state holds the |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
403 |
* value of the "phase" field of the worker: its index and status, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
404 |
* plus a version counter that, in addition to the count subfields |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
405 |
* (also serving as version stamps) provide protection against |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
406 |
* Treiber stack ABA effects. |
26448 | 407 |
* |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
408 |
* Creating workers. To create a worker, we pre-increment counts |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
409 |
* (serving as a reservation), and attempt to construct a |
26448 | 410 |
* ForkJoinWorkerThread via its factory. Upon construction, the |
411 |
* new thread invokes registerWorker, where it constructs a |
|
412 |
* WorkQueue and is assigned an index in the workQueues array |
|
32988 | 413 |
* (expanding the array if necessary). The thread is then started. |
414 |
* Upon any exception across these steps, or null return from |
|
415 |
* factory, deregisterWorker adjusts counts and records |
|
26448 | 416 |
* accordingly. If a null return, the pool continues running with |
417 |
* fewer than the target number workers. If exceptional, the |
|
418 |
* exception is propagated, generally to some external caller. |
|
419 |
* Worker index assignment avoids the bias in scanning that would |
|
420 |
* occur if entries were sequentially packed starting at the front |
|
421 |
* of the workQueues array. We treat the array as a simple |
|
422 |
* power-of-two hash table, expanding as needed. The seedIndex |
|
423 |
* increment ensures no collisions until a resize is needed or a |
|
424 |
* worker is deregistered and replaced, and thereafter keeps |
|
425 |
* probability of collision low. We cannot use |
|
426 |
* ThreadLocalRandom.getProbe() for similar purposes here because |
|
427 |
* the thread has not started yet, but do so for creating |
|
32988 | 428 |
* submission queues for existing external threads (see |
429 |
* externalPush). |
|
430 |
* |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
431 |
* WorkQueue field "phase" is used by both workers and the pool to |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
432 |
* manage and track whether a worker is UNSIGNALLED (possibly |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
433 |
* blocked waiting for a signal). When a worker is enqueued its |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
434 |
* phase field is set. Note that phase field updates lag queue CAS |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
435 |
* releases so usage requires care -- seeing a negative phase does |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
436 |
* not guarantee that the worker is available. When queued, the |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
437 |
* lower 16 bits of scanState must hold its pool index. So we |
49563 | 438 |
* place the index there upon initialization and otherwise keep it |
439 |
* there or restore it when necessary. |
|
32988 | 440 |
* |
441 |
* The ctl field also serves as the basis for memory |
|
442 |
* synchronization surrounding activation. This uses a more |
|
443 |
* efficient version of a Dekker-like rule that task producers and |
|
444 |
* consumers sync with each other by both writing/CASing ctl (even |
|
445 |
* if to its current value). This would be extremely costly. So |
|
446 |
* we relax it in several ways: (1) Producers only signal when |
|
49563 | 447 |
* their queue is possibly empty at some point during a push |
448 |
* operation (which requires conservatively checking size zero or |
|
449 |
* one to cover races). (2) Other workers propagate this signal |
|
450 |
* when they find tasks in a queue with size greater than one. (3) |
|
451 |
* Workers only enqueue after scanning (see below) and not finding |
|
452 |
* any tasks. (4) Rather than CASing ctl to its current value in |
|
453 |
* the common case where no action is required, we reduce write |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
454 |
* contention by equivalently prefacing signalWork when called by |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
455 |
* an external task producer using a memory access with |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
456 |
* full-volatile semantics or a "fullFence". |
32988 | 457 |
* |
49563 | 458 |
* Almost always, too many signals are issued, in part because a |
459 |
* task producer cannot tell if some existing worker is in the |
|
460 |
* midst of finishing one task (or already scanning) and ready to |
|
461 |
* take another without being signalled. So the producer might |
|
462 |
* instead activate a different worker that does not find any |
|
463 |
* work, and then inactivates. This scarcely matters in |
|
464 |
* steady-state computations involving all workers, but can create |
|
465 |
* contention and bookkeeping bottlenecks during ramp-up, |
|
466 |
* ramp-down, and small computations involving only a few workers. |
|
32988 | 467 |
* |
49563 | 468 |
* Scanning. Method scan (from runWorker) performs top-level |
469 |
* scanning for tasks. (Similar scans appear in helpQuiesce and |
|
470 |
* pollScan.) Each scan traverses and tries to poll from each |
|
471 |
* queue starting at a random index. Scans are not performed in |
|
472 |
* ideal random permutation order, to reduce cacheline |
|
473 |
* contention. The pseudorandom generator need not have |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
474 |
* high-quality statistical properties in the long term, but just |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
475 |
* within computations; We use Marsaglia XorShifts (often via |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
476 |
* ThreadLocalRandom.nextSecondarySeed), which are cheap and |
49563 | 477 |
* suffice. Scanning also includes contention reduction: When |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
478 |
* scanning workers fail to extract an apparently existing task, |
49563 | 479 |
* they soon restart at a different pseudorandom index. This form |
480 |
* of backoff improves throughput when many threads are trying to |
|
481 |
* take tasks from few queues, which can be common in some usages. |
|
482 |
* Scans do not otherwise explicitly take into account core |
|
483 |
* affinities, loads, cache localities, etc, However, they do |
|
484 |
* exploit temporal locality (which usually approximates these) by |
|
485 |
* preferring to re-poll from the same queue after a successful |
|
486 |
* poll before trying others (see method topLevelExec). However |
|
487 |
* this preference is bounded (see TOP_BOUND_SHIFT) as a safeguard |
|
488 |
* against infinitely unfair looping under unbounded user task |
|
489 |
* recursion, and also to reduce long-term contention when many |
|
490 |
* threads poll few queues holding many small tasks. The bound is |
|
491 |
* high enough to avoid much impact on locality and scheduling |
|
492 |
* overhead. |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
493 |
* |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
494 |
* Trimming workers. To release resources after periods of lack of |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
495 |
* use, a worker starting to wait when the pool is quiescent will |
49563 | 496 |
* time out and terminate (see method runWorker) if the pool has |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
497 |
* remained quiescent for period given by field keepAlive. |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
498 |
* |
26448 | 499 |
* Shutdown and Termination. A call to shutdownNow invokes |
500 |
* tryTerminate to atomically set a runState bit. The calling |
|
501 |
* thread, as well as every other worker thereafter terminating, |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
502 |
* helps terminate others by cancelling their unprocessed tasks, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
503 |
* and waking them up, doing so repeatedly until stable. Calls to |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
504 |
* non-abrupt shutdown() preface this by checking whether |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
505 |
* termination should commence by sweeping through queues (until |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
506 |
* stable) to ensure lack of in-flight submissions and workers |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
507 |
* about to process them before triggering the "STOP" phase of |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
508 |
* termination. |
26448 | 509 |
* |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
510 |
* Joining Tasks |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
511 |
* ============= |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
512 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
513 |
* Any of several actions may be taken when one worker is waiting |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
514 |
* to join a task stolen (or always held) by another. Because we |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
515 |
* are multiplexing many tasks on to a pool of workers, we can't |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
516 |
* always just let them block (as in Thread.join). We also cannot |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
517 |
* just reassign the joiner's run-time stack with another and |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
518 |
* replace it later, which would be a form of "continuation", that |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
519 |
* even if possible is not necessarily a good idea since we may |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
520 |
* need both an unblocked task and its continuation to progress. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
521 |
* Instead we combine two tactics: |
6543 | 522 |
* |
523 |
* Helping: Arranging for the joiner to execute some task that it |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
524 |
* would be running if the steal had not occurred. |
6543 | 525 |
* |
526 |
* Compensating: Unless there are already enough live threads, |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
527 |
* method tryCompensate() may create or re-activate a spare |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
528 |
* thread to compensate for blocked joiners until they unblock. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
529 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
530 |
* A third form (implemented in tryRemoveAndExec) amounts to |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
531 |
* helping a hypothetical compensator: If we can readily tell that |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
532 |
* a possible action of a compensator is to steal and execute the |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
533 |
* task being joined, the joining thread can do so directly, |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
534 |
* without the need for a compensation thread. |
6543 | 535 |
* |
536 |
* The ManagedBlocker extension API can't use helping so relies |
|
537 |
* only on compensation in method awaitBlocker. |
|
538 |
* |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
539 |
* The algorithm in awaitJoin entails a form of "linear helping". |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
540 |
* Each worker records (in field source) the id of the queue from |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
541 |
* which it last stole a task. The scan in method awaitJoin uses |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
542 |
* these markers to try to find a worker to help (i.e., steal back |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
543 |
* a task from and execute it) that could hasten completion of the |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
544 |
* actively joined task. Thus, the joiner executes a task that |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
545 |
* would be on its own local deque if the to-be-joined task had |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
546 |
* not been stolen. This is a conservative variant of the approach |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
547 |
* described in Wagner & Calder "Leapfrogging: a portable |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
548 |
* technique for implementing efficient futures" SIGPLAN Notices, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
549 |
* 1993 (http://portal.acm.org/citation.cfm?id=155354). It differs |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
550 |
* mainly in that we only record queue ids, not full dependency |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
551 |
* links. This requires a linear scan of the workQueues array to |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
552 |
* locate stealers, but isolates cost to when it is needed, rather |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
553 |
* than adding to per-task overhead. Searches can fail to locate |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
554 |
* stealers GC stalls and the like delay recording sources. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
555 |
* Further, even when accurately identified, stealers might not |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
556 |
* ever produce a task that the joiner can in turn help with. So, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
557 |
* compensation is tried upon failure to find tasks to run. |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
558 |
* |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
559 |
* Compensation does not by default aim to keep exactly the target |
26448 | 560 |
* parallelism number of unblocked threads running at any given |
561 |
* time. Some previous versions of this class employed immediate |
|
562 |
* compensations for any blocked join. However, in practice, the |
|
563 |
* vast majority of blockages are transient byproducts of GC and |
|
49563 | 564 |
* other JVM or OS activities that are made worse by replacement |
565 |
* when they cause longer-term oversubscription. Rather than |
|
566 |
* impose arbitrary policies, we allow users to override the |
|
567 |
* default of only adding threads upon apparent starvation. The |
|
568 |
* compensation mechanism may also be bounded. Bounds for the |
|
569 |
* commonPool (see COMMON_MAX_SPARES) better enable JVMs to cope |
|
570 |
* with programming errors and abuse before running out of |
|
571 |
* resources to do so. |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
572 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
573 |
* Common Pool |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
574 |
* =========== |
6543 | 575 |
* |
18790 | 576 |
* The static common pool always exists after static |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
577 |
* initialization. Since it (or any other created pool) need |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
578 |
* never be used, we minimize initial construction overhead and |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
579 |
* footprint to the setup of about a dozen fields. |
6543 | 580 |
* |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
581 |
* When external threads submit to the common pool, they can |
26448 | 582 |
* perform subtask processing (see externalHelpComplete and |
583 |
* related methods) upon joins. This caller-helps policy makes it |
|
584 |
* sensible to set common pool parallelism level to one (or more) |
|
585 |
* less than the total number of available cores, or even zero for |
|
586 |
* pure caller-runs. We do not need to record whether external |
|
587 |
* submissions are to the common pool -- if not, external help |
|
588 |
* methods return quickly. These submitters would otherwise be |
|
589 |
* blocked waiting for completion, so the extra effort (with |
|
590 |
* liberally sprinkled task status checks) in inapplicable cases |
|
591 |
* amounts to an odd form of limited spin-wait before blocking in |
|
592 |
* ForkJoinTask.join. |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
593 |
* |
22070 | 594 |
* As a more appropriate default in managed environments, unless |
595 |
* overridden by system properties, we use workers of subclass |
|
596 |
* InnocuousForkJoinWorkerThread when there is a SecurityManager |
|
597 |
* present. These workers have no permissions set, do not belong |
|
598 |
* to any user-defined ThreadGroup, and erase all ThreadLocals |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
599 |
* after executing any top-level task (see |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
600 |
* WorkQueue.afterTopLevelExec). The associated mechanics (mainly |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
601 |
* in ForkJoinWorkerThread) may be JVM-dependent and must access |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
602 |
* particular Thread class fields to achieve this effect. |
22070 | 603 |
* |
49563 | 604 |
* Memory placement |
605 |
* ================ |
|
606 |
* |
|
607 |
* Performance can be very sensitive to placement of instances of |
|
608 |
* ForkJoinPool and WorkQueues and their queue arrays. To reduce |
|
609 |
* false-sharing impact, the @Contended annotation isolates |
|
610 |
* adjacent WorkQueue instances, as well as the ForkJoinPool.ctl |
|
611 |
* field. WorkQueue arrays are allocated (by their threads) with |
|
612 |
* larger initial sizes than most ever need, mostly to reduce |
|
613 |
* false sharing with current garbage collectors that use cardmark |
|
614 |
* tables. |
|
615 |
* |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
616 |
* Style notes |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
617 |
* =========== |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
618 |
* |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
619 |
* Memory ordering relies mainly on VarHandles. This can be |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
620 |
* awkward and ugly, but also reflects the need to control |
26448 | 621 |
* outcomes across the unusual cases that arise in very racy code |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
622 |
* with very few invariants. All fields are read into locals |
49563 | 623 |
* before use, and null-checked if they are references. Array |
624 |
* accesses using masked indices include checks (that are always |
|
625 |
* true) that the array length is non-zero to avoid compilers |
|
626 |
* inserting more expensive traps. This is usually done in a |
|
627 |
* "C"-like style of listing declarations at the heads of methods |
|
628 |
* or blocks, and using inline assignments on first encounter. |
|
629 |
* Nearly all explicit checks lead to bypass/return, not exception |
|
630 |
* throws, because they may legitimately arise due to |
|
631 |
* cancellation/revocation during shutdown. |
|
26448 | 632 |
* |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
633 |
* There is a lot of representation-level coupling among classes |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
634 |
* ForkJoinPool, ForkJoinWorkerThread, and ForkJoinTask. The |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
635 |
* fields of WorkQueue maintain data structures managed by |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
636 |
* ForkJoinPool, so are directly accessed. There is little point |
6543 | 637 |
* trying to reduce this, since any associated future changes in |
638 |
* representations will need to be accompanied by algorithmic |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
639 |
* changes anyway. Several methods intrinsically sprawl because |
26448 | 640 |
* they must accumulate sets of consistent reads of fields held in |
49563 | 641 |
* local variables. Some others are artificially broken up to |
642 |
* reduce producer/consumer imbalances due to dynamic compilation. |
|
643 |
* There are also other coding oddities (including several |
|
644 |
* unnecessary-looking hoisted null checks) that help some methods |
|
645 |
* perform reasonably even when interpreted (not compiled). |
|
6543 | 646 |
* |
26448 | 647 |
* The order of declarations in this file is (with a few exceptions): |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
648 |
* (1) Static utility functions |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
649 |
* (2) Nested (static) classes |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
650 |
* (3) Static fields |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
651 |
* (4) Fields, along with constants used when unpacking some of them |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
652 |
* (5) Internal control methods |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
653 |
* (6) Callbacks and other support for ForkJoinTask methods |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
654 |
* (7) Exported methods |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
655 |
* (8) Static block initializing statics in minimally dependent order |
4110 | 656 |
*/ |
657 |
||
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
658 |
// Static utilities |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
659 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
660 |
/** |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
661 |
* If there is a security manager, makes sure caller has |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
662 |
* permission to modify threads. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
663 |
*/ |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
664 |
private static void checkPermission() { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
665 |
SecurityManager security = System.getSecurityManager(); |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
666 |
if (security != null) |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
667 |
security.checkPermission(modifyThreadPermission); |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
668 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
669 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
670 |
// Nested classes |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
671 |
|
4110 | 672 |
/** |
673 |
* Factory for creating new {@link ForkJoinWorkerThread}s. |
|
674 |
* A {@code ForkJoinWorkerThreadFactory} must be defined and used |
|
675 |
* for {@code ForkJoinWorkerThread} subclasses that extend base |
|
676 |
* functionality or initialize threads with different contexts. |
|
677 |
*/ |
|
678 |
public static interface ForkJoinWorkerThreadFactory { |
|
679 |
/** |
|
680 |
* Returns a new worker thread operating in the given pool. |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
681 |
* Returning null or throwing an exception may result in tasks |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
682 |
* never being executed. If this method throws an exception, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
683 |
* it is relayed to the caller of the method (for example |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
684 |
* {@code execute}) causing attempted thread creation. If this |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
685 |
* method returns null or throws an exception, it is not |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
686 |
* retried until the next attempted creation (for example |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
687 |
* another call to {@code execute}). |
4110 | 688 |
* |
689 |
* @param pool the pool this thread works in |
|
34341
423909a8c2a6
8143086: Document that ForkJoinWorkerThreadFactory.newThread can return null to reject request
dl
parents:
33674
diff
changeset
|
690 |
* @return the new worker thread, or {@code null} if the request |
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
691 |
* to create a thread is rejected |
4110 | 692 |
* @throws NullPointerException if the pool is null |
693 |
*/ |
|
694 |
public ForkJoinWorkerThread newThread(ForkJoinPool pool); |
|
695 |
} |
|
696 |
||
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
697 |
static AccessControlContext contextWithPermissions(Permission ... perms) { |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
698 |
Permissions permissions = new Permissions(); |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
699 |
for (Permission perm : perms) |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
700 |
permissions.add(perm); |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
701 |
return new AccessControlContext( |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
702 |
new ProtectionDomain[] { new ProtectionDomain(null, permissions) }); |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
703 |
} |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
704 |
|
4110 | 705 |
/** |
706 |
* Default ForkJoinWorkerThreadFactory implementation; creates a |
|
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
707 |
* new ForkJoinWorkerThread using the system class loader as the |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
708 |
* thread context class loader. |
4110 | 709 |
*/ |
32988 | 710 |
private static final class DefaultForkJoinWorkerThreadFactory |
4110 | 711 |
implements ForkJoinWorkerThreadFactory { |
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
712 |
private static final AccessControlContext ACC = contextWithPermissions( |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
713 |
new RuntimePermission("getClassLoader"), |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
714 |
new RuntimePermission("setContextClassLoader")); |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
715 |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
716 |
public final ForkJoinWorkerThread newThread(ForkJoinPool pool) { |
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
717 |
return AccessController.doPrivileged( |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
718 |
new PrivilegedAction<>() { |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
719 |
public ForkJoinWorkerThread run() { |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
720 |
return new ForkJoinWorkerThread( |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
721 |
pool, ClassLoader.getSystemClassLoader()); }}, |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
722 |
ACC); |
4110 | 723 |
} |
724 |
} |
|
725 |
||
26448 | 726 |
// Constants shared across ForkJoinPool and WorkQueue |
727 |
||
728 |
// Bounds |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
729 |
static final int SWIDTH = 16; // width of short |
26448 | 730 |
static final int SMASK = 0xffff; // short bits == max index |
731 |
static final int MAX_CAP = 0x7fff; // max #workers - 1 |
|
732 |
static final int SQMASK = 0x007e; // max 64 (even) slots |
|
733 |
||
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
734 |
// Masks and units for WorkQueue.phase and ctl sp subfield |
32988 | 735 |
static final int UNSIGNALLED = 1 << 31; // must be negative |
26448 | 736 |
static final int SS_SEQ = 1 << 16; // version count |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
737 |
static final int QLOCK = 1; // must be 1 |
26448 | 738 |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
739 |
// Mode bits and sentinels, some also used in WorkQueue id and.source fields |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
740 |
static final int OWNED = 1; // queue has owner thread |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
741 |
static final int FIFO = 1 << 16; // fifo queue or access mode |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
742 |
static final int SHUTDOWN = 1 << 18; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
743 |
static final int TERMINATED = 1 << 19; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
744 |
static final int STOP = 1 << 31; // must be negative |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
745 |
static final int QUIET = 1 << 30; // not scanning or working |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
746 |
static final int DORMANT = QUIET | UNSIGNALLED; |
32988 | 747 |
|
748 |
/** |
|
49563 | 749 |
* Initial capacity of work-stealing queue array. |
750 |
* Must be a power of two, at least 2. |
|
32988 | 751 |
*/ |
49563 | 752 |
static final int INITIAL_QUEUE_CAPACITY = 1 << 13; |
753 |
||
754 |
/** |
|
755 |
* Maximum capacity for queue arrays. Must be a power of two less |
|
756 |
* than or equal to 1 << (31 - width of array entry) to ensure |
|
757 |
* lack of wraparound of index calculations, but defined to a |
|
758 |
* value a bit less than this to help users trap runaway programs |
|
759 |
* before saturating systems. |
|
760 |
*/ |
|
761 |
static final int MAXIMUM_QUEUE_CAPACITY = 1 << 26; // 64M |
|
762 |
||
763 |
/** |
|
764 |
* The maximum number of top-level polls per worker before |
|
765 |
* checking other queues, expressed as a bit shift to, in effect, |
|
766 |
* multiply by pool size, and then use as random value mask, so |
|
767 |
* average bound is about poolSize*(1<<TOP_BOUND_SHIFT). See |
|
768 |
* above for rationale. |
|
769 |
*/ |
|
770 |
static final int TOP_BOUND_SHIFT = 10; |
|
26448 | 771 |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
772 |
/** |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
773 |
* Queues supporting work-stealing as well as external task |
26448 | 774 |
* submission. See above for descriptions and algorithms. |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
775 |
*/ |
34369
b6df4cc80001
8140687: Move @Contended to the jdk.internal.vm.annotation package
chegar
parents:
33674
diff
changeset
|
776 |
@jdk.internal.vm.annotation.Contended |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
777 |
static final class WorkQueue { |
49563 | 778 |
volatile int source; // source queue id, or sentinel |
779 |
int id; // pool index, mode, tag |
|
780 |
int base; // index of next slot for poll |
|
781 |
int top; // index of next slot for push |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
782 |
volatile int phase; // versioned, negative: queued, 1: locked |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
783 |
int stackPred; // pool stack (ctl) predecessor link |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
784 |
int nsteals; // number of steals |
49563 | 785 |
ForkJoinTask<?>[] array; // the queued tasks; power of 2 size |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
786 |
final ForkJoinPool pool; // the containing pool (may be null) |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
787 |
final ForkJoinWorkerThread owner; // owning thread or null if shared |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
788 |
|
26448 | 789 |
WorkQueue(ForkJoinPool pool, ForkJoinWorkerThread owner) { |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
790 |
this.pool = pool; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
791 |
this.owner = owner; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
792 |
// Place indices in the center of array (that is not yet allocated) |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
793 |
base = top = INITIAL_QUEUE_CAPACITY >>> 1; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
794 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
795 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
796 |
/** |
49563 | 797 |
* Tries to lock shared queue by CASing phase field. |
798 |
*/ |
|
799 |
final boolean tryLockPhase() { |
|
800 |
return PHASE.compareAndSet(this, 0, 1); |
|
801 |
} |
|
802 |
||
803 |
final void releasePhaseLock() { |
|
804 |
PHASE.setRelease(this, 0); |
|
805 |
} |
|
806 |
||
807 |
/** |
|
26448 | 808 |
* Returns an exportable index (used by ForkJoinWorkerThread). |
809 |
*/ |
|
810 |
final int getPoolIndex() { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
811 |
return (id & 0xffff) >>> 1; // ignore odd/even tag bit |
26448 | 812 |
} |
813 |
||
814 |
/** |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
815 |
* Returns the approximate number of tasks in the queue. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
816 |
*/ |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
817 |
final int queueSize() { |
49563 | 818 |
int n = (int)BASE.getAcquire(this) - top; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
819 |
return (n >= 0) ? 0 : -n; // ignore transient negative |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
820 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
821 |
|
18790 | 822 |
/** |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
823 |
* Provides a more accurate estimate of whether this queue has |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
824 |
* any tasks than does queueSize, by checking whether a |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
825 |
* near-empty queue has at least one unclaimed task. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
826 |
*/ |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
827 |
final boolean isEmpty() { |
49563 | 828 |
ForkJoinTask<?>[] a; int n, cap, b; |
829 |
VarHandle.acquireFence(); // needed by external callers |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
830 |
return ((n = (b = base) - top) >= 0 || // possibly one task |
32988 | 831 |
(n == -1 && ((a = array) == null || |
49563 | 832 |
(cap = a.length) == 0 || |
833 |
a[(cap - 1) & b] == null))); |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
834 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
835 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
836 |
/** |
32988 | 837 |
* Pushes a task. Call only by owner in unshared queues. |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
838 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
839 |
* @param task the task. Caller must ensure non-null. |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
840 |
* @throws RejectedExecutionException if array cannot be resized |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
841 |
*/ |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
842 |
final void push(ForkJoinTask<?> task) { |
49563 | 843 |
ForkJoinTask<?>[] a; |
844 |
int s = top, d, cap, m; |
|
845 |
ForkJoinPool p = pool; |
|
846 |
if ((a = array) != null && (cap = a.length) > 0) { |
|
847 |
QA.setRelease(a, (m = cap - 1) & s, task); |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
848 |
top = s + 1; |
49563 | 849 |
if (((d = s - (int)BASE.getAcquire(this)) & ~1) == 0 && |
850 |
p != null) { // size 0 or 1 |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
851 |
VarHandle.fullFence(); |
32988 | 852 |
p.signalWork(); |
26448 | 853 |
} |
49563 | 854 |
else if (d == m) |
855 |
growArray(false); |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
856 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
857 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
858 |
|
18790 | 859 |
/** |
49563 | 860 |
* Version of push for shared queues. Call only with phase lock held. |
861 |
* @return true if should signal work |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
862 |
*/ |
49563 | 863 |
final boolean lockedPush(ForkJoinTask<?> task) { |
864 |
ForkJoinTask<?>[] a; |
|
865 |
boolean signal = false; |
|
866 |
int s = top, b = base, cap, d; |
|
867 |
if ((a = array) != null && (cap = a.length) > 0) { |
|
868 |
a[(cap - 1) & s] = task; |
|
869 |
top = s + 1; |
|
870 |
if (b - s + cap - 1 == 0) |
|
871 |
growArray(true); |
|
872 |
else { |
|
873 |
phase = 0; // full volatile unlock |
|
874 |
if (((s - base) & ~1) == 0) // size 0 or 1 |
|
875 |
signal = true; |
|
876 |
} |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
877 |
} |
49563 | 878 |
return signal; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
879 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
880 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
881 |
/** |
49563 | 882 |
* Doubles the capacity of array. Call either by owner or with |
883 |
* lock held -- it is OK for base, but not top, to move while |
|
884 |
* resizings are in progress. |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
885 |
*/ |
49563 | 886 |
final void growArray(boolean locked) { |
887 |
ForkJoinTask<?>[] newA = null; |
|
888 |
try { |
|
889 |
ForkJoinTask<?>[] oldA; int oldSize, newSize; |
|
890 |
if ((oldA = array) != null && (oldSize = oldA.length) > 0 && |
|
891 |
(newSize = oldSize << 1) <= MAXIMUM_QUEUE_CAPACITY && |
|
892 |
newSize > 0) { |
|
893 |
try { |
|
894 |
newA = new ForkJoinTask<?>[newSize]; |
|
895 |
} catch (OutOfMemoryError ex) { |
|
896 |
} |
|
897 |
if (newA != null) { // poll from old array, push to new |
|
898 |
int oldMask = oldSize - 1, newMask = newSize - 1; |
|
899 |
for (int s = top - 1, k = oldMask; k >= 0; --k) { |
|
900 |
ForkJoinTask<?> x = (ForkJoinTask<?>) |
|
901 |
QA.getAndSet(oldA, s & oldMask, null); |
|
902 |
if (x != null) |
|
903 |
newA[s-- & newMask] = x; |
|
904 |
else |
|
905 |
break; |
|
906 |
} |
|
907 |
array = newA; |
|
908 |
VarHandle.releaseFence(); |
|
909 |
} |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
910 |
} |
49563 | 911 |
} finally { |
912 |
if (locked) |
|
913 |
phase = 0; |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
914 |
} |
49563 | 915 |
if (newA == null) |
916 |
throw new RejectedExecutionException("Queue capacity exceeded"); |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
917 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
918 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
919 |
/** |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
920 |
* Takes next task, if one exists, in FIFO order. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
921 |
*/ |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
922 |
final ForkJoinTask<?> poll() { |
49563 | 923 |
int b, k, cap; ForkJoinTask<?>[] a; |
924 |
while ((a = array) != null && (cap = a.length) > 0 && |
|
925 |
top - (b = base) > 0) { |
|
926 |
ForkJoinTask<?> t = (ForkJoinTask<?>) |
|
927 |
QA.getAcquire(a, k = (cap - 1) & b); |
|
928 |
if (base == b++) { |
|
929 |
if (t == null) |
|
930 |
Thread.yield(); // await index advance |
|
931 |
else if (QA.compareAndSet(a, k, t, null)) { |
|
932 |
BASE.setOpaque(this, b); |
|
933 |
return t; |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
934 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
935 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
936 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
937 |
return null; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
938 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
939 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
940 |
/** |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
941 |
* Takes next task, if one exists, in order specified by mode. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
942 |
*/ |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
943 |
final ForkJoinTask<?> nextLocalTask() { |
49563 | 944 |
ForkJoinTask<?> t = null; |
945 |
int md = id, b, s, d, cap; ForkJoinTask<?>[] a; |
|
946 |
if ((a = array) != null && (cap = a.length) > 0 && |
|
947 |
(d = (s = top) - (b = base)) > 0) { |
|
948 |
if ((md & FIFO) == 0 || d == 1) { |
|
949 |
if ((t = (ForkJoinTask<?>) |
|
950 |
QA.getAndSet(a, (cap - 1) & --s, null)) != null) |
|
951 |
TOP.setOpaque(this, s); |
|
952 |
} |
|
953 |
else if ((t = (ForkJoinTask<?>) |
|
954 |
QA.getAndSet(a, (cap - 1) & b++, null)) != null) { |
|
955 |
BASE.setOpaque(this, b); |
|
956 |
} |
|
957 |
else // on contention in FIFO mode, use regular poll |
|
958 |
t = poll(); |
|
959 |
} |
|
960 |
return t; |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
961 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
962 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
963 |
/** |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
964 |
* Returns next task, if one exists, in order specified by mode. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
965 |
*/ |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
966 |
final ForkJoinTask<?> peek() { |
49563 | 967 |
int cap; ForkJoinTask<?>[] a; |
968 |
return ((a = array) != null && (cap = a.length) > 0) ? |
|
969 |
a[(cap - 1) & ((id & FIFO) != 0 ? base : top - 1)] : null; |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
970 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
971 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
972 |
/** |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
973 |
* Pops the given task only if it is at the current top. |
32988 | 974 |
*/ |
975 |
final boolean tryUnpush(ForkJoinTask<?> task) { |
|
49563 | 976 |
boolean popped = false; |
977 |
int s, cap; ForkJoinTask<?>[] a; |
|
978 |
if ((a = array) != null && (cap = a.length) > 0 && |
|
979 |
(s = top) != base && |
|
980 |
(popped = QA.compareAndSet(a, (cap - 1) & --s, task, null))) |
|
981 |
TOP.setOpaque(this, s); |
|
982 |
return popped; |
|
983 |
} |
|
984 |
||
985 |
/** |
|
986 |
* Shared version of tryUnpush. |
|
987 |
*/ |
|
988 |
final boolean tryLockedUnpush(ForkJoinTask<?> task) { |
|
989 |
boolean popped = false; |
|
990 |
int s = top - 1, k, cap; ForkJoinTask<?>[] a; |
|
991 |
if ((a = array) != null && (cap = a.length) > 0 && |
|
992 |
a[k = (cap - 1) & s] == task && tryLockPhase()) { |
|
993 |
if (top == s + 1 && array == a && |
|
994 |
(popped = QA.compareAndSet(a, k, task, null))) |
|
32988 | 995 |
top = s; |
49563 | 996 |
releasePhaseLock(); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
997 |
} |
49563 | 998 |
return popped; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
999 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1000 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1001 |
/** |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1002 |
* Removes and cancels all known tasks, ignoring any exceptions. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1003 |
*/ |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1004 |
final void cancelAll() { |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1005 |
for (ForkJoinTask<?> t; (t = poll()) != null; ) |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1006 |
ForkJoinTask.cancelIgnoringExceptions(t); |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1007 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1008 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1009 |
// Specialized execution methods |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1010 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1011 |
/** |
49563 | 1012 |
* Runs the given (stolen) task if nonnull, as well as |
1013 |
* remaining local tasks and others available from the given |
|
1014 |
* queue, up to bound n (to avoid infinite unfairness). |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1015 |
*/ |
49563 | 1016 |
final void topLevelExec(ForkJoinTask<?> t, WorkQueue q, int n) { |
1017 |
if (t != null && q != null) { // hoist checks |
|
1018 |
int nstolen = 1; |
|
1019 |
for (;;) { |
|
1020 |
t.doExec(); |
|
1021 |
if (n-- < 0) |
|
1022 |
break; |
|
1023 |
else if ((t = nextLocalTask()) == null) { |
|
1024 |
if ((t = q.poll()) == null) |
|
32988 | 1025 |
break; |
49563 | 1026 |
else |
1027 |
++nstolen; |
|
32988 | 1028 |
} |
1029 |
} |
|
49563 | 1030 |
ForkJoinWorkerThread thread = owner; |
1031 |
nsteals += nstolen; |
|
1032 |
source = 0; |
|
1033 |
if (thread != null) |
|
1034 |
thread.afterTopLevelExec(); |
|
26448 | 1035 |
} |
1036 |
} |
|
1037 |
||
1038 |
/** |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1039 |
* If present, removes task from queue and executes it. |
18790 | 1040 |
*/ |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1041 |
final void tryRemoveAndExec(ForkJoinTask<?> task) { |
49563 | 1042 |
ForkJoinTask<?>[] a; int s, cap; |
1043 |
if ((a = array) != null && (cap = a.length) > 0 && |
|
1044 |
(s = top) - base > 0) { // traverse from top |
|
1045 |
for (int m = cap - 1, ns = s - 1, i = ns; ; --i) { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1046 |
int index = i & m; |
49563 | 1047 |
ForkJoinTask<?> t = (ForkJoinTask<?>)QA.get(a, index); |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1048 |
if (t == null) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1049 |
break; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1050 |
else if (t == task) { |
49563 | 1051 |
if (QA.compareAndSet(a, index, t, null)) { |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1052 |
top = ns; // safely shift down |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1053 |
for (int j = i; j != ns; ++j) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1054 |
ForkJoinTask<?> f; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1055 |
int pindex = (j + 1) & m; |
49563 | 1056 |
f = (ForkJoinTask<?>)QA.get(a, pindex); |
1057 |
QA.setVolatile(a, pindex, null); |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1058 |
int jindex = j & m; |
49563 | 1059 |
QA.setRelease(a, jindex, f); |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1060 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1061 |
VarHandle.releaseFence(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1062 |
t.doExec(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1063 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1064 |
break; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1065 |
} |
32988 | 1066 |
} |
26448 | 1067 |
} |
1068 |
} |
|
1069 |
||
1070 |
/** |
|
49563 | 1071 |
* Tries to pop and run tasks within the target's computation |
1072 |
* until done, not found, or limit exceeded. |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1073 |
* |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1074 |
* @param task root of CountedCompleter computation |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1075 |
* @param limit max runs, or zero for no limit |
49563 | 1076 |
* @param shared true if must lock to extract task |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1077 |
* @return task status on exit |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1078 |
*/ |
49563 | 1079 |
final int helpCC(CountedCompleter<?> task, int limit, boolean shared) { |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1080 |
int status = 0; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1081 |
if (task != null && (status = task.status) >= 0) { |
49563 | 1082 |
int s, k, cap; ForkJoinTask<?>[] a; |
1083 |
while ((a = array) != null && (cap = a.length) > 0 && |
|
1084 |
(s = top) - base > 0) { |
|
1085 |
CountedCompleter<?> v = null; |
|
1086 |
ForkJoinTask<?> o = a[k = (cap - 1) & (s - 1)]; |
|
1087 |
if (o instanceof CountedCompleter) { |
|
1088 |
CountedCompleter<?> t = (CountedCompleter<?>)o; |
|
1089 |
for (CountedCompleter<?> f = t;;) { |
|
1090 |
if (f != task) { |
|
1091 |
if ((f = f.completer) == null) |
|
1092 |
break; |
|
1093 |
} |
|
1094 |
else if (shared) { |
|
1095 |
if (tryLockPhase()) { |
|
1096 |
if (top == s && array == a && |
|
1097 |
QA.compareAndSet(a, k, t, null)) { |
|
1098 |
top = s - 1; |
|
1099 |
v = t; |
|
1100 |
} |
|
1101 |
releasePhaseLock(); |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1102 |
} |
49563 | 1103 |
break; |
1104 |
} |
|
1105 |
else { |
|
1106 |
if (QA.compareAndSet(a, k, t, null)) { |
|
1107 |
top = s - 1; |
|
1108 |
v = t; |
|
26448 | 1109 |
} |
49563 | 1110 |
break; |
26448 | 1111 |
} |
32988 | 1112 |
} |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1113 |
} |
49563 | 1114 |
if (v != null) |
1115 |
v.doExec(); |
|
1116 |
if ((status = task.status) < 0 || v == null || |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1117 |
(limit != 0 && --limit == 0)) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1118 |
break; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1119 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1120 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1121 |
return status; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1122 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1123 |
|
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1124 |
/** |
49563 | 1125 |
* Tries to poll and run AsynchronousCompletionTasks until |
1126 |
* none found or blocker is released |
|
1127 |
* |
|
1128 |
* @param blocker the blocker |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1129 |
*/ |
49563 | 1130 |
final void helpAsyncBlocker(ManagedBlocker blocker) { |
1131 |
if (blocker != null) { |
|
1132 |
int b, k, cap; ForkJoinTask<?>[] a; ForkJoinTask<?> t; |
|
1133 |
while ((a = array) != null && (cap = a.length) > 0 && |
|
1134 |
top - (b = base) > 0) { |
|
1135 |
t = (ForkJoinTask<?>)QA.getAcquire(a, k = (cap - 1) & b); |
|
1136 |
if (blocker.isReleasable()) |
|
1137 |
break; |
|
1138 |
else if (base == b++ && t != null) { |
|
1139 |
if (!(t instanceof CompletableFuture. |
|
1140 |
AsynchronousCompletionTask)) |
|
1141 |
break; |
|
1142 |
else if (QA.compareAndSet(a, k, t, null)) { |
|
1143 |
BASE.setOpaque(this, b); |
|
1144 |
t.doExec(); |
|
1145 |
} |
|
18790 | 1146 |
} |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1147 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1148 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1149 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1150 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1151 |
/** |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1152 |
* Returns true if owned and not known to be blocked. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1153 |
*/ |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1154 |
final boolean isApparentlyUnblocked() { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1155 |
Thread wt; Thread.State s; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1156 |
return ((wt = owner) != null && |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1157 |
(s = wt.getState()) != Thread.State.BLOCKED && |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1158 |
s != Thread.State.WAITING && |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1159 |
s != Thread.State.TIMED_WAITING); |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1160 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1161 |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1162 |
// VarHandle mechanics. |
49563 | 1163 |
static final VarHandle PHASE; |
1164 |
static final VarHandle BASE; |
|
1165 |
static final VarHandle TOP; |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1166 |
static { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1167 |
try { |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1168 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1169 |
PHASE = l.findVarHandle(WorkQueue.class, "phase", int.class); |
49563 | 1170 |
BASE = l.findVarHandle(WorkQueue.class, "base", int.class); |
1171 |
TOP = l.findVarHandle(WorkQueue.class, "top", int.class); |
|
32988 | 1172 |
} catch (ReflectiveOperationException e) { |
49563 | 1173 |
throw new ExceptionInInitializerError(e); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1174 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1175 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1176 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1177 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1178 |
// static fields (initialized in static initializer below) |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1179 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1180 |
/** |
4110 | 1181 |
* Creates a new ForkJoinWorkerThread. This factory is used unless |
1182 |
* overridden in ForkJoinPool constructors. |
|
1183 |
*/ |
|
1184 |
public static final ForkJoinWorkerThreadFactory |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1185 |
defaultForkJoinWorkerThreadFactory; |
4110 | 1186 |
|
1187 |
/** |
|
1188 |
* Permission required for callers of methods that may start or |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1189 |
* kill threads. |
4110 | 1190 |
*/ |
32988 | 1191 |
static final RuntimePermission modifyThreadPermission; |
4110 | 1192 |
|
1193 |
/** |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1194 |
* Common (static) pool. Non-null for public use unless a static |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1195 |
* construction exception, but internal usages null-check on use |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1196 |
* to paranoically avoid potential initialization circularities |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1197 |
* as well as to simplify generated code. |
4110 | 1198 |
*/ |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
1199 |
static final ForkJoinPool common; |
4110 | 1200 |
|
1201 |
/** |
|
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
1202 |
* Common pool parallelism. To allow simpler use and management |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
1203 |
* when common pool threads are disabled, we allow the underlying |
18790 | 1204 |
* common.parallelism field to be zero, but in that case still report |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
1205 |
* parallelism as 1 to reflect resulting caller-runs mechanics. |
4110 | 1206 |
*/ |
32988 | 1207 |
static final int COMMON_PARALLELISM; |
4110 | 1208 |
|
1209 |
/** |
|
26448 | 1210 |
* Limit on spare thread construction in tryCompensate. |
1211 |
*/ |
|
32988 | 1212 |
private static final int COMMON_MAX_SPARES; |
26448 | 1213 |
|
1214 |
/** |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1215 |
* Sequence number for creating workerNamePrefix. |
4110 | 1216 |
*/ |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1217 |
private static int poolNumberSequence; |
4110 | 1218 |
|
1219 |
/** |
|
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
1220 |
* Returns the next sequence number. We don't expect this to |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
1221 |
* ever contend, so use simple builtin sync. |
4110 | 1222 |
*/ |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1223 |
private static final synchronized int nextPoolId() { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1224 |
return ++poolNumberSequence; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1225 |
} |
4110 | 1226 |
|
26448 | 1227 |
// static configuration constants |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1228 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1229 |
/** |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1230 |
* Default idle timeout value (in milliseconds) for the thread |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1231 |
* triggering quiescence to park waiting for new work |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1232 |
*/ |
41131
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
1233 |
private static final long DEFAULT_KEEPALIVE = 60_000L; |
6543 | 1234 |
|
1235 |
/** |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1236 |
* Undershoot tolerance for idle timeouts |
6543 | 1237 |
*/ |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1238 |
private static final long TIMEOUT_SLOP = 20L; |
26448 | 1239 |
|
1240 |
/** |
|
32988 | 1241 |
* The default value for COMMON_MAX_SPARES. Overridable using the |
1242 |
* "java.util.concurrent.ForkJoinPool.common.maximumSpares" system |
|
1243 |
* property. The default value is far in excess of normal |
|
1244 |
* requirements, but also far short of MAX_CAP and typical OS |
|
1245 |
* thread limits, so allows JVMs to catch misuse/abuse before |
|
1246 |
* running out of resources needed to do so. |
|
26448 | 1247 |
*/ |
32988 | 1248 |
private static final int DEFAULT_COMMON_MAX_SPARES = 256; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1249 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1250 |
/** |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1251 |
* Increment for seed generators. See class ThreadLocal for |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1252 |
* explanation. |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1253 |
*/ |
22070 | 1254 |
private static final int SEED_INCREMENT = 0x9e3779b9; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1255 |
|
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
1256 |
/* |
26448 | 1257 |
* Bits and masks for field ctl, packed with 4 16 bit subfields: |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1258 |
* RC: Number of released (unqueued) workers minus target parallelism |
26448 | 1259 |
* TC: Number of total workers minus target parallelism |
1260 |
* SS: version count and status of top waiting thread |
|
1261 |
* ID: poolIndex of top of Treiber stack of waiters |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1262 |
* |
26448 | 1263 |
* When convenient, we can extract the lower 32 stack top bits |
1264 |
* (including version bits) as sp=(int)ctl. The offsets of counts |
|
1265 |
* by the target parallelism and the positionings of fields makes |
|
1266 |
* it possible to perform the most common checks via sign tests of |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1267 |
* fields: When ac is negative, there are not enough unqueued |
26448 | 1268 |
* workers, when tc is negative, there are not enough total |
1269 |
* workers. When sp is non-zero, there are waiting workers. To |
|
1270 |
* deal with possibly negative fields, we use casts in and out of |
|
1271 |
* "short" and/or signed shifts to maintain signedness. |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1272 |
* |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1273 |
* Because it occupies uppermost bits, we can add one release count |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1274 |
* using getAndAddLong of RC_UNIT, rather than CAS, when returning |
26448 | 1275 |
* from a blocked join. Other updates entail multiple subfields |
1276 |
* and masking, requiring CAS. |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1277 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1278 |
* The limits packed in field "bounds" are also offset by the |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1279 |
* parallelism level to make them comparable to the ctl rc and tc |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1280 |
* fields. |
6543 | 1281 |
*/ |
4110 | 1282 |
|
26448 | 1283 |
// Lower and upper word masks |
1284 |
private static final long SP_MASK = 0xffffffffL; |
|
1285 |
private static final long UC_MASK = ~SP_MASK; |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1286 |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1287 |
// Release counts |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1288 |
private static final int RC_SHIFT = 48; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1289 |
private static final long RC_UNIT = 0x0001L << RC_SHIFT; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1290 |
private static final long RC_MASK = 0xffffL << RC_SHIFT; |
6543 | 1291 |
|
26448 | 1292 |
// Total counts |
1293 |
private static final int TC_SHIFT = 32; |
|
1294 |
private static final long TC_UNIT = 0x0001L << TC_SHIFT; |
|
1295 |
private static final long TC_MASK = 0xffffL << TC_SHIFT; |
|
1296 |
private static final long ADD_WORKER = 0x0001L << (TC_SHIFT + 15); // sign |
|
6543 | 1297 |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1298 |
// Instance fields |
6543 | 1299 |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1300 |
volatile long stealCount; // collects worker nsteals |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1301 |
final long keepAlive; // milliseconds before dropping if idle |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1302 |
int indexSeed; // next worker index |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1303 |
final int bounds; // min, max threads packed as shorts |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1304 |
volatile int mode; // parallelism, runstate, queue mode |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1305 |
WorkQueue[] workQueues; // main registry |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1306 |
final String workerNamePrefix; // for worker thread string; sync lock |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1307 |
final ForkJoinWorkerThreadFactory factory; |
26448 | 1308 |
final UncaughtExceptionHandler ueh; // per-worker UEH |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1309 |
final Predicate<? super ForkJoinPool> saturate; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1310 |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1311 |
@jdk.internal.vm.annotation.Contended("fjpctl") // segregate |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1312 |
volatile long ctl; // main pool control |
26448 | 1313 |
|
1314 |
// Creating, registering and deregistering workers |
|
1315 |
||
1316 |
/** |
|
1317 |
* Tries to construct and start one worker. Assumes that total |
|
1318 |
* count has already been incremented as a reservation. Invokes |
|
1319 |
* deregisterWorker on any failure. |
|
1320 |
* |
|
1321 |
* @return true if successful |
|
1322 |
*/ |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1323 |
private boolean createWorker() { |
26448 | 1324 |
ForkJoinWorkerThreadFactory fac = factory; |
1325 |
Throwable ex = null; |
|
1326 |
ForkJoinWorkerThread wt = null; |
|
1327 |
try { |
|
1328 |
if (fac != null && (wt = fac.newThread(this)) != null) { |
|
1329 |
wt.start(); |
|
1330 |
return true; |
|
1331 |
} |
|
1332 |
} catch (Throwable rex) { |
|
1333 |
ex = rex; |
|
1334 |
} |
|
1335 |
deregisterWorker(wt, ex); |
|
1336 |
return false; |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1337 |
} |
4110 | 1338 |
|
1339 |
/** |
|
26448 | 1340 |
* Tries to add one worker, incrementing ctl counts before doing |
1341 |
* so, relying on createWorker to back out on failure. |
|
1342 |
* |
|
1343 |
* @param c incoming ctl value, with total count negative and no |
|
1344 |
* idle workers. On CAS failure, c is refreshed and retried if |
|
1345 |
* this holds (otherwise, a new worker is not needed). |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1346 |
*/ |
26448 | 1347 |
private void tryAddWorker(long c) { |
1348 |
do { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1349 |
long nc = ((RC_MASK & (c + RC_UNIT)) | |
26448 | 1350 |
(TC_MASK & (c + TC_UNIT))); |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1351 |
if (ctl == c && CTL.compareAndSet(this, c, nc)) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1352 |
createWorker(); |
32988 | 1353 |
break; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1354 |
} |
26448 | 1355 |
} while (((c = ctl) & ADD_WORKER) != 0L && (int)c == 0); |
4110 | 1356 |
} |
1357 |
||
1358 |
/** |
|
26448 | 1359 |
* Callback from ForkJoinWorkerThread constructor to establish and |
1360 |
* record its WorkQueue. |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1361 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1362 |
* @param wt the worker thread |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1363 |
* @return the worker's queue |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1364 |
*/ |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1365 |
final WorkQueue registerWorker(ForkJoinWorkerThread wt) { |
26448 | 1366 |
UncaughtExceptionHandler handler; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1367 |
wt.setDaemon(true); // configure thread |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1368 |
if ((handler = ueh) != null) |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1369 |
wt.setUncaughtExceptionHandler(handler); |
49563 | 1370 |
int tid = 0; // for thread name |
1371 |
int idbits = mode & FIFO; |
|
1372 |
String prefix = workerNamePrefix; |
|
26448 | 1373 |
WorkQueue w = new WorkQueue(this, wt); |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1374 |
if (prefix != null) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1375 |
synchronized (prefix) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1376 |
WorkQueue[] ws = workQueues; int n; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1377 |
int s = indexSeed += SEED_INCREMENT; |
49563 | 1378 |
idbits |= (s & ~(SMASK | FIFO | DORMANT)); |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1379 |
if (ws != null && (n = ws.length) > 1) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1380 |
int m = n - 1; |
49563 | 1381 |
tid = m & ((s << 1) | 1); // odd-numbered indices |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1382 |
for (int probes = n >>> 1;;) { // find empty slot |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1383 |
WorkQueue q; |
49563 | 1384 |
if ((q = ws[tid]) == null || q.phase == QUIET) |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1385 |
break; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1386 |
else if (--probes == 0) { |
49563 | 1387 |
tid = n | 1; // resize below |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1388 |
break; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1389 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1390 |
else |
49563 | 1391 |
tid = (tid + 2) & m; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1392 |
} |
49563 | 1393 |
w.phase = w.id = tid | idbits; // now publishable |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1394 |
|
49563 | 1395 |
if (tid < n) |
1396 |
ws[tid] = w; |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1397 |
else { // expand array |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1398 |
int an = n << 1; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1399 |
WorkQueue[] as = new WorkQueue[an]; |
49563 | 1400 |
as[tid] = w; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1401 |
int am = an - 1; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1402 |
for (int j = 0; j < n; ++j) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1403 |
WorkQueue v; // copy external queue |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1404 |
if ((v = ws[j]) != null) // position may change |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1405 |
as[v.id & am & SQMASK] = v; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1406 |
if (++j >= n) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1407 |
break; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1408 |
as[j] = ws[j]; // copy worker |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1409 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1410 |
workQueues = as; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1411 |
} |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1412 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1413 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1414 |
wt.setName(prefix.concat(Integer.toString(tid))); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1415 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1416 |
return w; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1417 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1418 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1419 |
/** |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1420 |
* Final callback from terminating worker, as well as upon failure |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1421 |
* to construct or start a worker. Removes record of worker from |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1422 |
* array, and adjusts counts. If pool is shutting down, tries to |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1423 |
* complete termination. |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1424 |
* |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
1425 |
* @param wt the worker thread, or null if construction failed |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1426 |
* @param ex the exception causing failure, or null if none |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1427 |
*/ |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1428 |
final void deregisterWorker(ForkJoinWorkerThread wt, Throwable ex) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1429 |
WorkQueue w = null; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1430 |
int phase = 0; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1431 |
if (wt != null && (w = wt.workQueue) != null) { |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1432 |
Object lock = workerNamePrefix; |
49563 | 1433 |
int wid = w.id; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1434 |
long ns = (long)w.nsteals & 0xffffffffL; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1435 |
if (lock != null) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1436 |
synchronized (lock) { |
49563 | 1437 |
WorkQueue[] ws; int n, i; // remove index from array |
1438 |
if ((ws = workQueues) != null && (n = ws.length) > 0 && |
|
1439 |
ws[i = wid & (n - 1)] == w) |
|
1440 |
ws[i] = null; |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1441 |
stealCount += ns; |
32988 | 1442 |
} |
1443 |
} |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1444 |
phase = w.phase; |
26448 | 1445 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1446 |
if (phase != QUIET) { // else pre-adjusted |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1447 |
long c; // decrement counts |
40734
48879ea67e2a
8162108: Rename weakCompareAndSetVolatile to weakCompareAndSet
psandoz
parents:
39723
diff
changeset
|
1448 |
do {} while (!CTL.weakCompareAndSet |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1449 |
(this, c = ctl, ((RC_MASK & (c - RC_UNIT)) | |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1450 |
(TC_MASK & (c - TC_UNIT)) | |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1451 |
(SP_MASK & c)))); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1452 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1453 |
if (w != null) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1454 |
w.cancelAll(); // cancel remaining tasks |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1455 |
|
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1456 |
if (!tryTerminate(false, false) && // possibly replace worker |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1457 |
w != null && w.array != null) // avoid repeated failures |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1458 |
signalWork(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1459 |
|
26448 | 1460 |
if (ex == null) // help clean on way out |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1461 |
ForkJoinTask.helpExpungeStaleExceptions(); |
26448 | 1462 |
else // rethrow |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1463 |
ForkJoinTask.rethrow(ex); |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1464 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1465 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1466 |
/** |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1467 |
* Tries to create or release a worker if too few are running. |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1468 |
*/ |
32988 | 1469 |
final void signalWork() { |
1470 |
for (;;) { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1471 |
long c; int sp; WorkQueue[] ws; int i; WorkQueue v; |
32988 | 1472 |
if ((c = ctl) >= 0L) // enough workers |
1473 |
break; |
|
1474 |
else if ((sp = (int)c) == 0) { // no idle workers |
|
1475 |
if ((c & ADD_WORKER) != 0L) // too few workers |
|
26448 | 1476 |
tryAddWorker(c); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1477 |
break; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1478 |
} |
32988 | 1479 |
else if ((ws = workQueues) == null) |
1480 |
break; // unstarted/terminated |
|
1481 |
else if (ws.length <= (i = sp & SMASK)) |
|
1482 |
break; // terminated |
|
1483 |
else if ((v = ws[i]) == null) |
|
1484 |
break; // terminating |
|
1485 |
else { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1486 |
int np = sp & ~UNSIGNALLED; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1487 |
int vp = v.phase; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1488 |
long nc = (v.stackPred & SP_MASK) | (UC_MASK & (c + RC_UNIT)); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1489 |
Thread vt = v.owner; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1490 |
if (sp == vp && CTL.compareAndSet(this, c, nc)) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1491 |
v.phase = np; |
49563 | 1492 |
if (vt != null && v.source < 0) |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1493 |
LockSupport.unpark(vt); |
32988 | 1494 |
break; |
1495 |
} |
|
18790 | 1496 |
} |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1497 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1498 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1499 |
|
26448 | 1500 |
/** |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1501 |
* Tries to decrement counts (sometimes implicitly) and possibly |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1502 |
* arrange for a compensating worker in preparation for blocking: |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1503 |
* If not all core workers yet exist, creates one, else if any are |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1504 |
* unreleased (possibly including caller) releases one, else if |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1505 |
* fewer than the minimum allowed number of workers running, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1506 |
* checks to see that they are all active, and if so creates an |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1507 |
* extra worker unless over maximum limit and policy is to |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1508 |
* saturate. Most of these steps can fail due to interference, in |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1509 |
* which case 0 is returned so caller will retry. A negative |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1510 |
* return value indicates that the caller doesn't need to |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1511 |
* re-adjust counts when later unblocked. |
26448 | 1512 |
* |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1513 |
* @return 1: block then adjust, -1: block without adjust, 0 : retry |
32988 | 1514 |
*/ |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1515 |
private int tryCompensate(WorkQueue w) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1516 |
int t, n, sp; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1517 |
long c = ctl; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1518 |
WorkQueue[] ws = workQueues; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1519 |
if ((t = (short)(c >>> TC_SHIFT)) >= 0) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1520 |
if (ws == null || (n = ws.length) <= 0 || w == null) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1521 |
return 0; // disabled |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1522 |
else if ((sp = (int)c) != 0) { // replace or release |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1523 |
WorkQueue v = ws[sp & (n - 1)]; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1524 |
int wp = w.phase; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1525 |
long uc = UC_MASK & ((wp < 0) ? c + RC_UNIT : c); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1526 |
int np = sp & ~UNSIGNALLED; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1527 |
if (v != null) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1528 |
int vp = v.phase; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1529 |
Thread vt = v.owner; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1530 |
long nc = ((long)v.stackPred & SP_MASK) | uc; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1531 |
if (vp == sp && CTL.compareAndSet(this, c, nc)) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1532 |
v.phase = np; |
49563 | 1533 |
if (vt != null && v.source < 0) |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1534 |
LockSupport.unpark(vt); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1535 |
return (wp < 0) ? -1 : 1; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1536 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1537 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1538 |
return 0; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1539 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1540 |
else if ((int)(c >> RC_SHIFT) - // reduce parallelism |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1541 |
(short)(bounds & SMASK) > 0) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1542 |
long nc = ((RC_MASK & (c - RC_UNIT)) | (~RC_MASK & c)); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1543 |
return CTL.compareAndSet(this, c, nc) ? 1 : 0; |
32988 | 1544 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1545 |
else { // validate |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1546 |
int md = mode, pc = md & SMASK, tc = pc + t, bc = 0; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1547 |
boolean unstable = false; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1548 |
for (int i = 1; i < n; i += 2) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1549 |
WorkQueue q; Thread wt; Thread.State ts; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1550 |
if ((q = ws[i]) != null) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1551 |
if (q.source == 0) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1552 |
unstable = true; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1553 |
break; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1554 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1555 |
else { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1556 |
--tc; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1557 |
if ((wt = q.owner) != null && |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1558 |
((ts = wt.getState()) == Thread.State.BLOCKED || |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1559 |
ts == Thread.State.WAITING)) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1560 |
++bc; // worker is blocking |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1561 |
} |
32988 | 1562 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1563 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1564 |
if (unstable || tc != 0 || ctl != c) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1565 |
return 0; // inconsistent |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1566 |
else if (t + pc >= MAX_CAP || t >= (bounds >>> SWIDTH)) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1567 |
Predicate<? super ForkJoinPool> sat; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1568 |
if ((sat = saturate) != null && sat.test(this)) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1569 |
return -1; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1570 |
else if (bc < pc) { // lagging |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1571 |
Thread.yield(); // for retry spins |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1572 |
return 0; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1573 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1574 |
else |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1575 |
throw new RejectedExecutionException( |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1576 |
"Thread limit exceeded replacing blocked worker"); |
32988 | 1577 |
} |
1578 |
} |
|
1579 |
} |
|
1580 |
||
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1581 |
long nc = ((c + TC_UNIT) & TC_MASK) | (c & ~TC_MASK); // expand pool |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1582 |
return CTL.compareAndSet(this, c, nc) && createWorker() ? 1 : 0; |
32988 | 1583 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1584 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1585 |
/** |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1586 |
* Top-level runloop for workers, called by ForkJoinWorkerThread.run. |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1587 |
* See above for explanation. |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
1588 |
*/ |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1589 |
final void runWorker(WorkQueue w) { |
49563 | 1590 |
int r = (w.id ^ ThreadLocalRandom.nextSecondarySeed()) | FIFO; // rng |
1591 |
w.array = new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY]; // initialize |
|
1592 |
for (;;) { |
|
1593 |
int phase; |
|
1594 |
if (scan(w, r)) { // scan until apparently empty |
|
1595 |
r ^= r << 13; r ^= r >>> 17; r ^= r << 5; // move (xorshift) |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1596 |
} |
49563 | 1597 |
else if ((phase = w.phase) >= 0) { // enqueue, then rescan |
1598 |
long np = (w.phase = (phase + SS_SEQ) | UNSIGNALLED) & SP_MASK; |
|
1599 |
long c, nc; |
|
1600 |
do { |
|
1601 |
w.stackPred = (int)(c = ctl); |
|
1602 |
nc = ((c - RC_UNIT) & UC_MASK) | np; |
|
1603 |
} while (!CTL.weakCompareAndSet(this, c, nc)); |
|
18790 | 1604 |
} |
49563 | 1605 |
else { // already queued |
1606 |
int pred = w.stackPred; |
|
1607 |
Thread.interrupted(); // clear before park |
|
1608 |
w.source = DORMANT; // enable signal |
|
1609 |
long c = ctl; |
|
1610 |
int md = mode, rc = (md & SMASK) + (int)(c >> RC_SHIFT); |
|
1611 |
if (md < 0) // terminating |
|
1612 |
break; |
|
1613 |
else if (rc <= 0 && (md & SHUTDOWN) != 0 && |
|
1614 |
tryTerminate(false, false)) |
|
1615 |
break; // quiescent shutdown |
|
1616 |
else if (rc <= 0 && pred != 0 && phase == (int)c) { |
|
1617 |
long nc = (UC_MASK & (c - TC_UNIT)) | (SP_MASK & pred); |
|
1618 |
long d = keepAlive + System.currentTimeMillis(); |
|
1619 |
LockSupport.parkUntil(this, d); |
|
1620 |
if (ctl == c && // drop on timeout if all idle |
|
1621 |
d - System.currentTimeMillis() <= TIMEOUT_SLOP && |
|
1622 |
CTL.compareAndSet(this, c, nc)) { |
|
1623 |
w.phase = QUIET; |
|
1624 |
break; |
|
6543 | 1625 |
} |
1626 |
} |
|
49563 | 1627 |
else if (w.phase < 0) |
1628 |
LockSupport.park(this); // OK if spuriously woken |
|
1629 |
w.source = 0; // disable signal |
|
32988 | 1630 |
} |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1631 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1632 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1633 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1634 |
/** |
49563 | 1635 |
* Scans for and if found executes one or more top-level tasks from a queue. |
1636 |
* |
|
1637 |
* @return true if found an apparently non-empty queue, and |
|
1638 |
* possibly ran task(s). |
|
1639 |
*/ |
|
1640 |
private boolean scan(WorkQueue w, int r) { |
|
1641 |
WorkQueue[] ws; int n; |
|
1642 |
if ((ws = workQueues) != null && (n = ws.length) > 0 && w != null) { |
|
1643 |
for (int m = n - 1, j = r & m;;) { |
|
1644 |
WorkQueue q; int b; |
|
1645 |
if ((q = ws[j]) != null && q.top != (b = q.base)) { |
|
1646 |
int qid = q.id; |
|
1647 |
ForkJoinTask<?>[] a; int cap, k; ForkJoinTask<?> t; |
|
1648 |
if ((a = q.array) != null && (cap = a.length) > 0) { |
|
1649 |
t = (ForkJoinTask<?>)QA.getAcquire(a, k = (cap - 1) & b); |
|
1650 |
if (q.base == b++ && t != null && |
|
1651 |
QA.compareAndSet(a, k, t, null)) { |
|
1652 |
q.base = b; |
|
1653 |
w.source = qid; |
|
1654 |
if (q.top - b > 0) |
|
1655 |
signalWork(); |
|
1656 |
w.topLevelExec(t, q, // random fairness bound |
|
1657 |
r & ((n << TOP_BOUND_SHIFT) - 1)); |
|
1658 |
} |
|
1659 |
} |
|
1660 |
return true; |
|
1661 |
} |
|
1662 |
else if (--n > 0) |
|
1663 |
j = (j + 1) & m; |
|
1664 |
else |
|
1665 |
break; |
|
1666 |
} |
|
1667 |
} |
|
1668 |
return false; |
|
1669 |
} |
|
1670 |
||
1671 |
/** |
|
26448 | 1672 |
* Helps and/or blocks until the given task is done or timeout. |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1673 |
* First tries locally helping, then scans other queues for a task |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1674 |
* produced by one of w's stealers; compensating and blocking if |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1675 |
* none are found (rescanning if tryCompensate fails). |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1676 |
* |
26448 | 1677 |
* @param w caller |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1678 |
* @param task the task |
26448 | 1679 |
* @param deadline for timed waits, if nonzero |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1680 |
* @return task status on exit |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1681 |
*/ |
26448 | 1682 |
final int awaitJoin(WorkQueue w, ForkJoinTask<?> task, long deadline) { |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1683 |
int s = 0; |
49563 | 1684 |
int seed = ThreadLocalRandom.nextSecondarySeed(); |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1685 |
if (w != null && task != null && |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1686 |
(!(task instanceof CountedCompleter) || |
49563 | 1687 |
(s = w.helpCC((CountedCompleter<?>)task, 0, false)) >= 0)) { |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1688 |
w.tryRemoveAndExec(task); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1689 |
int src = w.source, id = w.id; |
49563 | 1690 |
int r = (seed >>> 16) | 1, step = (seed & ~1) | 2; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1691 |
s = task.status; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1692 |
while (s >= 0) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1693 |
WorkQueue[] ws; |
49563 | 1694 |
int n = (ws = workQueues) == null ? 0 : ws.length, m = n - 1; |
1695 |
while (n > 0) { |
|
1696 |
WorkQueue q; int b; |
|
1697 |
if ((q = ws[r & m]) != null && q.source == id && |
|
1698 |
q.top != (b = q.base)) { |
|
1699 |
ForkJoinTask<?>[] a; int cap, k; |
|
1700 |
int qid = q.id; |
|
1701 |
if ((a = q.array) != null && (cap = a.length) > 0) { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1702 |
ForkJoinTask<?> t = (ForkJoinTask<?>) |
49563 | 1703 |
QA.getAcquire(a, k = (cap - 1) & b); |
1704 |
if (q.source == id && q.base == b++ && |
|
1705 |
t != null && QA.compareAndSet(a, k, t, null)) { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1706 |
q.base = b; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1707 |
w.source = qid; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1708 |
t.doExec(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1709 |
w.source = src; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1710 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1711 |
} |
49563 | 1712 |
break; |
1713 |
} |
|
1714 |
else { |
|
1715 |
r += step; |
|
1716 |
--n; |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1717 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1718 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1719 |
if ((s = task.status) < 0) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1720 |
break; |
49563 | 1721 |
else if (n == 0) { // empty scan |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1722 |
long ms, ns; int block; |
32988 | 1723 |
if (deadline == 0L) |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1724 |
ms = 0L; // untimed |
32988 | 1725 |
else if ((ns = deadline - System.nanoTime()) <= 0L) |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1726 |
break; // timeout |
32988 | 1727 |
else if ((ms = TimeUnit.NANOSECONDS.toMillis(ns)) <= 0L) |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1728 |
ms = 1L; // avoid 0 for timed wait |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1729 |
if ((block = tryCompensate(w)) != 0) { |
32988 | 1730 |
task.internalWait(ms); |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1731 |
CTL.getAndAdd(this, (block > 0) ? RC_UNIT : 0L); |
32988 | 1732 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1733 |
s = task.status; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1734 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1735 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1736 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1737 |
return s; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1738 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1739 |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1740 |
/** |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1741 |
* Runs tasks until {@code isQuiescent()}. Rather than blocking |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1742 |
* when tasks cannot be found, rescans until all others cannot |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1743 |
* find tasks either. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1744 |
*/ |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1745 |
final void helpQuiescePool(WorkQueue w) { |
49563 | 1746 |
int prevSrc = w.source; |
1747 |
int seed = ThreadLocalRandom.nextSecondarySeed(); |
|
1748 |
int r = seed >>> 16, step = r | 1; |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1749 |
for (int source = prevSrc, released = -1;;) { // -1 until known |
49563 | 1750 |
ForkJoinTask<?> localTask; WorkQueue[] ws; |
1751 |
while ((localTask = w.nextLocalTask()) != null) |
|
1752 |
localTask.doExec(); |
|
1753 |
if (w.phase >= 0 && released == -1) |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1754 |
released = 1; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1755 |
boolean quiet = true, empty = true; |
49563 | 1756 |
int n = (ws = workQueues) == null ? 0 : ws.length; |
1757 |
for (int m = n - 1; n > 0; r += step, --n) { |
|
1758 |
WorkQueue q; int b; |
|
1759 |
if ((q = ws[r & m]) != null) { |
|
1760 |
int qs = q.source; |
|
1761 |
if (q.top != (b = q.base)) { |
|
1762 |
quiet = empty = false; |
|
1763 |
ForkJoinTask<?>[] a; int cap, k; |
|
1764 |
int qid = q.id; |
|
1765 |
if ((a = q.array) != null && (cap = a.length) > 0) { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1766 |
if (released == 0) { // increment |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1767 |
released = 1; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1768 |
CTL.getAndAdd(this, RC_UNIT); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1769 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1770 |
ForkJoinTask<?> t = (ForkJoinTask<?>) |
49563 | 1771 |
QA.getAcquire(a, k = (cap - 1) & b); |
1772 |
if (q.base == b++ && t != null && |
|
1773 |
QA.compareAndSet(a, k, t, null)) { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1774 |
q.base = b; |
49563 | 1775 |
w.source = qid; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1776 |
t.doExec(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1777 |
w.source = source = prevSrc; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1778 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1779 |
} |
49563 | 1780 |
break; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1781 |
} |
49563 | 1782 |
else if ((qs & QUIET) == 0) |
1783 |
quiet = false; |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1784 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1785 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1786 |
if (quiet) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1787 |
if (released == 0) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1788 |
CTL.getAndAdd(this, RC_UNIT); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1789 |
w.source = prevSrc; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1790 |
break; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1791 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1792 |
else if (empty) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1793 |
if (source != QUIET) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1794 |
w.source = source = QUIET; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1795 |
if (released == 1) { // decrement |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1796 |
released = 0; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1797 |
CTL.getAndAdd(this, RC_MASK & -RC_UNIT); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1798 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1799 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1800 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1801 |
} |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1802 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1803 |
/** |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1804 |
* Scans for and returns a polled task, if available. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1805 |
* Used only for untracked polls. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1806 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1807 |
* @param submissionsOnly if true, only scan submission queues |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1808 |
*/ |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1809 |
private ForkJoinTask<?> pollScan(boolean submissionsOnly) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1810 |
WorkQueue[] ws; int n; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1811 |
rescan: while ((mode & STOP) == 0 && (ws = workQueues) != null && |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1812 |
(n = ws.length) > 0) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1813 |
int m = n - 1; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1814 |
int r = ThreadLocalRandom.nextSecondarySeed(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1815 |
int h = r >>> 16; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1816 |
int origin, step; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1817 |
if (submissionsOnly) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1818 |
origin = (r & ~1) & m; // even indices and steps |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1819 |
step = (h & ~1) | 2; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1820 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1821 |
else { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1822 |
origin = r & m; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1823 |
step = h | 1; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1824 |
} |
49563 | 1825 |
boolean nonempty = false; |
1826 |
for (int i = origin, oldSum = 0, checkSum = 0;;) { |
|
1827 |
WorkQueue q; |
|
1828 |
if ((q = ws[i]) != null) { |
|
1829 |
int b; ForkJoinTask<?> t; |
|
1830 |
if (q.top - (b = q.base) > 0) { |
|
1831 |
nonempty = true; |
|
1832 |
if ((t = q.poll()) != null) |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1833 |
return t; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1834 |
} |
49563 | 1835 |
else |
1836 |
checkSum += b + q.id; |
|
26448 | 1837 |
} |
49563 | 1838 |
if ((i = (i + step) & m) == origin) { |
1839 |
if (!nonempty && oldSum == (oldSum = checkSum)) |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1840 |
break rescan; |
26448 | 1841 |
checkSum = 0; |
49563 | 1842 |
nonempty = false; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1843 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1844 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1845 |
} |
26448 | 1846 |
return null; |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1847 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1848 |
|
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
1849 |
/** |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1850 |
* Gets and removes a local or stolen task for the given worker. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1851 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1852 |
* @return a task, if available |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1853 |
*/ |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1854 |
final ForkJoinTask<?> nextTaskFor(WorkQueue w) { |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1855 |
ForkJoinTask<?> t; |
49563 | 1856 |
if (w == null || (t = w.nextLocalTask()) == null) |
1857 |
t = pollScan(false); |
|
1858 |
return t; |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1859 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1860 |
|
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1861 |
// External operations |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1862 |
|
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1863 |
/** |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1864 |
* Adds the given task to a submission queue at submitter's |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1865 |
* current queue, creating one if null or contended. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1866 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1867 |
* @param task the task. Caller must ensure non-null. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1868 |
*/ |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1869 |
final void externalPush(ForkJoinTask<?> task) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1870 |
int r; // initialize caller's probe |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1871 |
if ((r = ThreadLocalRandom.getProbe()) == 0) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1872 |
ThreadLocalRandom.localInit(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1873 |
r = ThreadLocalRandom.getProbe(); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1874 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1875 |
for (;;) { |
49563 | 1876 |
WorkQueue q; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1877 |
int md = mode, n; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1878 |
WorkQueue[] ws = workQueues; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1879 |
if ((md & SHUTDOWN) != 0 || ws == null || (n = ws.length) <= 0) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1880 |
throw new RejectedExecutionException(); |
49563 | 1881 |
else if ((q = ws[(n - 1) & r & SQMASK]) == null) { // add queue |
1882 |
int qid = (r | QUIET) & ~(FIFO | OWNED); |
|
1883 |
Object lock = workerNamePrefix; |
|
1884 |
ForkJoinTask<?>[] qa = |
|
1885 |
new ForkJoinTask<?>[INITIAL_QUEUE_CAPACITY]; |
|
1886 |
q = new WorkQueue(this, null); |
|
1887 |
q.array = qa; |
|
1888 |
q.id = qid; |
|
1889 |
q.source = QUIET; |
|
1890 |
if (lock != null) { // unless disabled, lock pool to install |
|
1891 |
synchronized (lock) { |
|
1892 |
WorkQueue[] vs; int i, vn; |
|
1893 |
if ((vs = workQueues) != null && (vn = vs.length) > 0 && |
|
1894 |
vs[i = qid & (vn - 1) & SQMASK] == null) |
|
1895 |
vs[i] = q; // else another thread already installed |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1896 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1897 |
} |
49563 | 1898 |
} |
1899 |
else if (!q.tryLockPhase()) // move if busy |
|
1900 |
r = ThreadLocalRandom.advanceProbe(r); |
|
1901 |
else { |
|
1902 |
if (q.lockedPush(task)) |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1903 |
signalWork(); |
49563 | 1904 |
return; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1905 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1906 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1907 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1908 |
|
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1909 |
/** |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1910 |
* Pushes a possibly-external submission. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1911 |
*/ |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1912 |
private <T> ForkJoinTask<T> externalSubmit(ForkJoinTask<T> task) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1913 |
Thread t; ForkJoinWorkerThread w; WorkQueue q; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1914 |
if (task == null) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1915 |
throw new NullPointerException(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1916 |
if (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) && |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1917 |
(w = (ForkJoinWorkerThread)t).pool == this && |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1918 |
(q = w.workQueue) != null) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1919 |
q.push(task); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1920 |
else |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1921 |
externalPush(task); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1922 |
return task; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1923 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1924 |
|
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1925 |
/** |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1926 |
* Returns common pool queue for an external thread. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1927 |
*/ |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1928 |
static WorkQueue commonSubmitterQueue() { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1929 |
ForkJoinPool p = common; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1930 |
int r = ThreadLocalRandom.getProbe(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1931 |
WorkQueue[] ws; int n; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1932 |
return (p != null && (ws = p.workQueues) != null && |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1933 |
(n = ws.length) > 0) ? |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1934 |
ws[(n - 1) & r & SQMASK] : null; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1935 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1936 |
|
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1937 |
/** |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1938 |
* Performs tryUnpush for an external submitter. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1939 |
*/ |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1940 |
final boolean tryExternalUnpush(ForkJoinTask<?> task) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1941 |
int r = ThreadLocalRandom.getProbe(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1942 |
WorkQueue[] ws; WorkQueue w; int n; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1943 |
return ((ws = workQueues) != null && |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1944 |
(n = ws.length) > 0 && |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1945 |
(w = ws[(n - 1) & r & SQMASK]) != null && |
49563 | 1946 |
w.tryLockedUnpush(task)); |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1947 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1948 |
|
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1949 |
/** |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1950 |
* Performs helpComplete for an external submitter. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1951 |
*/ |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1952 |
final int externalHelpComplete(CountedCompleter<?> task, int maxTasks) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1953 |
int r = ThreadLocalRandom.getProbe(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1954 |
WorkQueue[] ws; WorkQueue w; int n; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1955 |
return ((ws = workQueues) != null && (n = ws.length) > 0 && |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1956 |
(w = ws[(n - 1) & r & SQMASK]) != null) ? |
49563 | 1957 |
w.helpCC(task, maxTasks, true) : 0; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1958 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1959 |
|
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1960 |
/** |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1961 |
* Tries to steal and run tasks within the target's computation. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1962 |
* The maxTasks argument supports external usages; internal calls |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1963 |
* use zero, allowing unbounded steps (external calls trap |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1964 |
* non-positive values). |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1965 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1966 |
* @param w caller |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1967 |
* @param maxTasks if non-zero, the maximum number of other tasks to run |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1968 |
* @return task status on exit |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1969 |
*/ |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1970 |
final int helpComplete(WorkQueue w, CountedCompleter<?> task, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
1971 |
int maxTasks) { |
49563 | 1972 |
return (w == null) ? 0 : w.helpCC(task, maxTasks, false); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1973 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1974 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1975 |
/** |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1976 |
* Returns a cheap heuristic guide for task partitioning when |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1977 |
* programmers, frameworks, tools, or languages have little or no |
26448 | 1978 |
* idea about task granularity. In essence, by offering this |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1979 |
* method, we ask users only about tradeoffs in overhead vs |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1980 |
* expected throughput and its variance, rather than how finely to |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1981 |
* partition tasks. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1982 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1983 |
* In a steady state strict (tree-structured) computation, each |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1984 |
* thread makes available for stealing enough tasks for other |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1985 |
* threads to remain active. Inductively, if all threads play by |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1986 |
* the same rules, each thread should make available only a |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1987 |
* constant number of tasks. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1988 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1989 |
* The minimum useful constant is just 1. But using a value of 1 |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1990 |
* would require immediate replenishment upon each steal to |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1991 |
* maintain enough tasks, which is infeasible. Further, |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1992 |
* partitionings/granularities of offered tasks should minimize |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1993 |
* steal rates, which in general means that threads nearer the top |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1994 |
* of computation tree should generate more than those nearer the |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1995 |
* bottom. In perfect steady state, each thread is at |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1996 |
* approximately the same level of computation tree. However, |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1997 |
* producing extra tasks amortizes the uncertainty of progress and |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1998 |
* diffusion assumptions. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
1999 |
* |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2000 |
* So, users will want to use values larger (but not much larger) |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2001 |
* than 1 to both smooth over transient shortages and hedge |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2002 |
* against uneven progress; as traded off against the cost of |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2003 |
* extra task overhead. We leave the user to pick a threshold |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2004 |
* value to compare with the results of this call to guide |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2005 |
* decisions, but recommend values such as 3. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2006 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2007 |
* When all threads are active, it is on average OK to estimate |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2008 |
* surplus strictly locally. In steady-state, if one thread is |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2009 |
* maintaining say 2 surplus tasks, then so are others. So we can |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2010 |
* just use estimated queue length. However, this strategy alone |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2011 |
* leads to serious mis-estimates in some non-steady-state |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2012 |
* conditions (ramp-up, ramp-down, other stalls). We can detect |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2013 |
* many of these by further considering the number of "idle" |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2014 |
* threads, that are known to have zero queued tasks, so |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2015 |
* compensate by a factor of (#idle/#active) threads. |
6543 | 2016 |
*/ |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2017 |
static int getSurplusQueuedTaskCount() { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2018 |
Thread t; ForkJoinWorkerThread wt; ForkJoinPool pool; WorkQueue q; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2019 |
if (((t = Thread.currentThread()) instanceof ForkJoinWorkerThread) && |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2020 |
(pool = (wt = (ForkJoinWorkerThread)t).pool) != null && |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2021 |
(q = wt.workQueue) != null) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2022 |
int p = pool.mode & SMASK; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2023 |
int a = p + (int)(pool.ctl >> RC_SHIFT); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2024 |
int n = q.top - q.base; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2025 |
return n - (a > (p >>>= 1) ? 0 : |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2026 |
a > (p >>>= 1) ? 1 : |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2027 |
a > (p >>>= 1) ? 2 : |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2028 |
a > (p >>>= 1) ? 4 : |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2029 |
8); |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2030 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2031 |
return 0; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2032 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2033 |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2034 |
// Termination |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2035 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2036 |
/** |
26448 | 2037 |
* Possibly initiates and/or completes termination. |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2038 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2039 |
* @param now if true, unconditionally terminate, else only |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2040 |
* if no work and no active workers |
32988 | 2041 |
* @param enable if true, terminate when next possible |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2042 |
* @return true if terminating or terminated |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2043 |
*/ |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2044 |
private boolean tryTerminate(boolean now, boolean enable) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2045 |
int md; // 3 phases: try to set SHUTDOWN, then STOP, then TERMINATED |
32988 | 2046 |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2047 |
while (((md = mode) & SHUTDOWN) == 0) { |
32988 | 2048 |
if (!enable || this == common) // cannot shutdown |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2049 |
return false; |
32988 | 2050 |
else |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2051 |
MODE.compareAndSet(this, md, md | SHUTDOWN); |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2052 |
} |
26448 | 2053 |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2054 |
while (((md = mode) & STOP) == 0) { // try to initiate termination |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2055 |
if (!now) { // check if quiescent & empty |
26448 | 2056 |
for (long oldSum = 0L;;) { // repeat until stable |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2057 |
boolean running = false; |
26448 | 2058 |
long checkSum = ctl; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2059 |
WorkQueue[] ws = workQueues; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2060 |
if ((md & SMASK) + (int)(checkSum >> RC_SHIFT) > 0) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2061 |
running = true; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2062 |
else if (ws != null) { |
49563 | 2063 |
WorkQueue w; |
32988 | 2064 |
for (int i = 0; i < ws.length; ++i) { |
2065 |
if ((w = ws[i]) != null) { |
|
49563 | 2066 |
int s = w.source, p = w.phase; |
2067 |
int d = w.id, b = w.base; |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2068 |
if (b != w.top || |
49563 | 2069 |
((d & 1) == 1 && (s >= 0 || p >= 0))) { |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2070 |
running = true; |
49563 | 2071 |
break; // working, scanning, or have work |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2072 |
} |
49563 | 2073 |
checkSum += (((long)s << 48) + ((long)p << 32) + |
2074 |
((long)b << 16) + (long)d); |
|
26448 | 2075 |
} |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2076 |
} |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2077 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2078 |
if (((md = mode) & STOP) != 0) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2079 |
break; // already triggered |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2080 |
else if (running) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2081 |
return false; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2082 |
else if (workQueues == ws && oldSum == (oldSum = checkSum)) |
26448 | 2083 |
break; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2084 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2085 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2086 |
if ((md & STOP) == 0) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2087 |
MODE.compareAndSet(this, md, md | STOP); |
26448 | 2088 |
} |
2089 |
||
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2090 |
while (((md = mode) & TERMINATED) == 0) { // help terminate others |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2091 |
for (long oldSum = 0L;;) { // repeat until stable |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2092 |
WorkQueue[] ws; WorkQueue w; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2093 |
long checkSum = ctl; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2094 |
if ((ws = workQueues) != null) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2095 |
for (int i = 0; i < ws.length; ++i) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2096 |
if ((w = ws[i]) != null) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2097 |
ForkJoinWorkerThread wt = w.owner; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2098 |
w.cancelAll(); // clear queues |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2099 |
if (wt != null) { |
32988 | 2100 |
try { // unblock join or park |
26448 | 2101 |
wt.interrupt(); |
2102 |
} catch (Throwable ignore) { |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2103 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2104 |
} |
49563 | 2105 |
checkSum += ((long)w.phase << 32) + w.base; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2106 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2107 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2108 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2109 |
if (((md = mode) & TERMINATED) != 0 || |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2110 |
(workQueues == ws && oldSum == (oldSum = checkSum))) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2111 |
break; |
26448 | 2112 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2113 |
if ((md & TERMINATED) != 0) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2114 |
break; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2115 |
else if ((md & SMASK) + (short)(ctl >>> TC_SHIFT) > 0) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2116 |
break; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2117 |
else if (MODE.compareAndSet(this, md, md | TERMINATED)) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2118 |
synchronized (this) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2119 |
notifyAll(); // for awaitTermination |
32988 | 2120 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2121 |
break; |
32988 | 2122 |
} |
2123 |
} |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2124 |
return true; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2125 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2126 |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2127 |
// Exported methods |
4110 | 2128 |
|
2129 |
// Constructors |
|
2130 |
||
2131 |
/** |
|
2132 |
* Creates a {@code ForkJoinPool} with parallelism equal to {@link |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2133 |
* java.lang.Runtime#availableProcessors}, using defaults for all |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2134 |
* other parameters (see {@link #ForkJoinPool(int, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2135 |
* ForkJoinWorkerThreadFactory, UncaughtExceptionHandler, boolean, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2136 |
* int, int, int, Predicate, long, TimeUnit)}). |
4110 | 2137 |
* |
2138 |
* @throws SecurityException if a security manager exists and |
|
2139 |
* the caller is not permitted to modify threads |
|
2140 |
* because it does not hold {@link |
|
2141 |
* java.lang.RuntimePermission}{@code ("modifyThread")} |
|
2142 |
*/ |
|
2143 |
public ForkJoinPool() { |
|
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2144 |
this(Math.min(MAX_CAP, Runtime.getRuntime().availableProcessors()), |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2145 |
defaultForkJoinWorkerThreadFactory, null, false, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2146 |
0, MAX_CAP, 1, null, DEFAULT_KEEPALIVE, TimeUnit.MILLISECONDS); |
4110 | 2147 |
} |
2148 |
||
2149 |
/** |
|
2150 |
* Creates a {@code ForkJoinPool} with the indicated parallelism |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2151 |
* level, using defaults for all other parameters (see {@link |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2152 |
* #ForkJoinPool(int, ForkJoinWorkerThreadFactory, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2153 |
* UncaughtExceptionHandler, boolean, int, int, int, Predicate, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2154 |
* long, TimeUnit)}). |
4110 | 2155 |
* |
2156 |
* @param parallelism the parallelism level |
|
2157 |
* @throws IllegalArgumentException if parallelism less than or |
|
2158 |
* equal to zero, or greater than implementation limit |
|
2159 |
* @throws SecurityException if a security manager exists and |
|
2160 |
* the caller is not permitted to modify threads |
|
2161 |
* because it does not hold {@link |
|
2162 |
* java.lang.RuntimePermission}{@code ("modifyThread")} |
|
2163 |
*/ |
|
2164 |
public ForkJoinPool(int parallelism) { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2165 |
this(parallelism, defaultForkJoinWorkerThreadFactory, null, false, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2166 |
0, MAX_CAP, 1, null, DEFAULT_KEEPALIVE, TimeUnit.MILLISECONDS); |
4110 | 2167 |
} |
2168 |
||
2169 |
/** |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2170 |
* Creates a {@code ForkJoinPool} with the given parameters (using |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2171 |
* defaults for others -- see {@link #ForkJoinPool(int, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2172 |
* ForkJoinWorkerThreadFactory, UncaughtExceptionHandler, boolean, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2173 |
* int, int, int, Predicate, long, TimeUnit)}). |
4110 | 2174 |
* |
6543 | 2175 |
* @param parallelism the parallelism level. For default value, |
2176 |
* use {@link java.lang.Runtime#availableProcessors}. |
|
2177 |
* @param factory the factory for creating new threads. For default value, |
|
2178 |
* use {@link #defaultForkJoinWorkerThreadFactory}. |
|
2179 |
* @param handler the handler for internal worker threads that |
|
2180 |
* terminate due to unrecoverable errors encountered while executing |
|
2181 |
* tasks. For default value, use {@code null}. |
|
2182 |
* @param asyncMode if true, |
|
2183 |
* establishes local first-in-first-out scheduling mode for forked |
|
2184 |
* tasks that are never joined. This mode may be more appropriate |
|
2185 |
* than default locally stack-based mode in applications in which |
|
2186 |
* worker threads only process event-style asynchronous tasks. |
|
2187 |
* For default value, use {@code false}. |
|
4110 | 2188 |
* @throws IllegalArgumentException if parallelism less than or |
2189 |
* equal to zero, or greater than implementation limit |
|
2190 |
* @throws NullPointerException if the factory is null |
|
2191 |
* @throws SecurityException if a security manager exists and |
|
2192 |
* the caller is not permitted to modify threads |
|
2193 |
* because it does not hold {@link |
|
2194 |
* java.lang.RuntimePermission}{@code ("modifyThread")} |
|
2195 |
*/ |
|
6543 | 2196 |
public ForkJoinPool(int parallelism, |
2197 |
ForkJoinWorkerThreadFactory factory, |
|
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2198 |
UncaughtExceptionHandler handler, |
6543 | 2199 |
boolean asyncMode) { |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2200 |
this(parallelism, factory, handler, asyncMode, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2201 |
0, MAX_CAP, 1, null, DEFAULT_KEEPALIVE, TimeUnit.MILLISECONDS); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2202 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2203 |
|
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2204 |
/** |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2205 |
* Creates a {@code ForkJoinPool} with the given parameters. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2206 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2207 |
* @param parallelism the parallelism level. For default value, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2208 |
* use {@link java.lang.Runtime#availableProcessors}. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2209 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2210 |
* @param factory the factory for creating new threads. For |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2211 |
* default value, use {@link #defaultForkJoinWorkerThreadFactory}. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2212 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2213 |
* @param handler the handler for internal worker threads that |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2214 |
* terminate due to unrecoverable errors encountered while |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2215 |
* executing tasks. For default value, use {@code null}. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2216 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2217 |
* @param asyncMode if true, establishes local first-in-first-out |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2218 |
* scheduling mode for forked tasks that are never joined. This |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2219 |
* mode may be more appropriate than default locally stack-based |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2220 |
* mode in applications in which worker threads only process |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2221 |
* event-style asynchronous tasks. For default value, use {@code |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2222 |
* false}. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2223 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2224 |
* @param corePoolSize the number of threads to keep in the pool |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2225 |
* (unless timed out after an elapsed keep-alive). Normally (and |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2226 |
* by default) this is the same value as the parallelism level, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2227 |
* but may be set to a larger value to reduce dynamic overhead if |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2228 |
* tasks regularly block. Using a smaller value (for example |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2229 |
* {@code 0}) has the same effect as the default. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2230 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2231 |
* @param maximumPoolSize the maximum number of threads allowed. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2232 |
* When the maximum is reached, attempts to replace blocked |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2233 |
* threads fail. (However, because creation and termination of |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2234 |
* different threads may overlap, and may be managed by the given |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2235 |
* thread factory, this value may be transiently exceeded.) To |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2236 |
* arrange the same value as is used by default for the common |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2237 |
* pool, use {@code 256} plus the {@code parallelism} level. (By |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2238 |
* default, the common pool allows a maximum of 256 spare |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2239 |
* threads.) Using a value (for example {@code |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2240 |
* Integer.MAX_VALUE}) larger than the implementation's total |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2241 |
* thread limit has the same effect as using this limit (which is |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2242 |
* the default). |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2243 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2244 |
* @param minimumRunnable the minimum allowed number of core |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2245 |
* threads not blocked by a join or {@link ManagedBlocker}. To |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2246 |
* ensure progress, when too few unblocked threads exist and |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2247 |
* unexecuted tasks may exist, new threads are constructed, up to |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2248 |
* the given maximumPoolSize. For the default value, use {@code |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2249 |
* 1}, that ensures liveness. A larger value might improve |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2250 |
* throughput in the presence of blocked activities, but might |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2251 |
* not, due to increased overhead. A value of zero may be |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2252 |
* acceptable when submitted tasks cannot have dependencies |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2253 |
* requiring additional threads. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2254 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2255 |
* @param saturate if non-null, a predicate invoked upon attempts |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2256 |
* to create more than the maximum total allowed threads. By |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2257 |
* default, when a thread is about to block on a join or {@link |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2258 |
* ManagedBlocker}, but cannot be replaced because the |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2259 |
* maximumPoolSize would be exceeded, a {@link |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2260 |
* RejectedExecutionException} is thrown. But if this predicate |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2261 |
* returns {@code true}, then no exception is thrown, so the pool |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2262 |
* continues to operate with fewer than the target number of |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2263 |
* runnable threads, which might not ensure progress. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2264 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2265 |
* @param keepAliveTime the elapsed time since last use before |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2266 |
* a thread is terminated (and then later replaced if needed). |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2267 |
* For the default value, use {@code 60, TimeUnit.SECONDS}. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2268 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2269 |
* @param unit the time unit for the {@code keepAliveTime} argument |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2270 |
* |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2271 |
* @throws IllegalArgumentException if parallelism is less than or |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2272 |
* equal to zero, or is greater than implementation limit, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2273 |
* or if maximumPoolSize is less than parallelism, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2274 |
* of if the keepAliveTime is less than or equal to zero. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2275 |
* @throws NullPointerException if the factory is null |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2276 |
* @throws SecurityException if a security manager exists and |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2277 |
* the caller is not permitted to modify threads |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2278 |
* because it does not hold {@link |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2279 |
* java.lang.RuntimePermission}{@code ("modifyThread")} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2280 |
* @since 9 |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2281 |
*/ |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2282 |
public ForkJoinPool(int parallelism, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2283 |
ForkJoinWorkerThreadFactory factory, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2284 |
UncaughtExceptionHandler handler, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2285 |
boolean asyncMode, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2286 |
int corePoolSize, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2287 |
int maximumPoolSize, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2288 |
int minimumRunnable, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2289 |
Predicate<? super ForkJoinPool> saturate, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2290 |
long keepAliveTime, |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2291 |
TimeUnit unit) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2292 |
// check, encode, pack parameters |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2293 |
if (parallelism <= 0 || parallelism > MAX_CAP || |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2294 |
maximumPoolSize < parallelism || keepAliveTime <= 0L) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2295 |
throw new IllegalArgumentException(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2296 |
if (factory == null) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2297 |
throw new NullPointerException(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2298 |
long ms = Math.max(unit.toMillis(keepAliveTime), TIMEOUT_SLOP); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2299 |
|
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2300 |
int corep = Math.min(Math.max(corePoolSize, parallelism), MAX_CAP); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2301 |
long c = ((((long)(-corep) << TC_SHIFT) & TC_MASK) | |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2302 |
(((long)(-parallelism) << RC_SHIFT) & RC_MASK)); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2303 |
int m = parallelism | (asyncMode ? FIFO : 0); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2304 |
int maxSpares = Math.min(maximumPoolSize, MAX_CAP) - parallelism; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2305 |
int minAvail = Math.min(Math.max(minimumRunnable, 0), MAX_CAP); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2306 |
int b = ((minAvail - parallelism) & SMASK) | (maxSpares << SWIDTH); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2307 |
int n = (parallelism > 1) ? parallelism - 1 : 1; // at least 2 slots |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2308 |
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2309 |
n = (n + 1) << 1; // power of two, including space for submission queues |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2310 |
|
41131
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
2311 |
this.workerNamePrefix = "ForkJoinPool-" + nextPoolId() + "-worker-"; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2312 |
this.workQueues = new WorkQueue[n]; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2313 |
this.factory = factory; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2314 |
this.ueh = handler; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2315 |
this.saturate = saturate; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2316 |
this.keepAlive = ms; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2317 |
this.bounds = b; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2318 |
this.mode = m; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2319 |
this.ctl = c; |
6543 | 2320 |
checkPermission(); |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2321 |
} |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2322 |
|
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43545
diff
changeset
|
2323 |
private static Object newInstanceFromSystemProperty(String property) |
41131
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
2324 |
throws ReflectiveOperationException { |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
2325 |
String className = System.getProperty(property); |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
2326 |
return (className == null) |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
2327 |
? null |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
2328 |
: ClassLoader.getSystemClassLoader().loadClass(className) |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
2329 |
.getConstructor().newInstance(); |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
2330 |
} |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
2331 |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2332 |
/** |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2333 |
* Constructor for common pool using parameters possibly |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2334 |
* overridden by system properties |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2335 |
*/ |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2336 |
private ForkJoinPool(byte forCommonPoolOnly) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2337 |
int parallelism = -1; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2338 |
ForkJoinWorkerThreadFactory fac = null; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2339 |
UncaughtExceptionHandler handler = null; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2340 |
try { // ignore exceptions in accessing/parsing properties |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2341 |
String pp = System.getProperty |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2342 |
("java.util.concurrent.ForkJoinPool.common.parallelism"); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2343 |
if (pp != null) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2344 |
parallelism = Integer.parseInt(pp); |
41131
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
2345 |
fac = (ForkJoinWorkerThreadFactory) newInstanceFromSystemProperty( |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
2346 |
"java.util.concurrent.ForkJoinPool.common.threadFactory"); |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
2347 |
handler = (UncaughtExceptionHandler) newInstanceFromSystemProperty( |
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
2348 |
"java.util.concurrent.ForkJoinPool.common.exceptionHandler"); |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2349 |
} catch (Exception ignore) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2350 |
} |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2351 |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2352 |
if (fac == null) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2353 |
if (System.getSecurityManager() == null) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2354 |
fac = defaultForkJoinWorkerThreadFactory; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2355 |
else // use security-managed default |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2356 |
fac = new InnocuousForkJoinWorkerThreadFactory(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2357 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2358 |
if (parallelism < 0 && // default 1 less than #cores |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2359 |
(parallelism = Runtime.getRuntime().availableProcessors() - 1) <= 0) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2360 |
parallelism = 1; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2361 |
if (parallelism > MAX_CAP) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2362 |
parallelism = MAX_CAP; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2363 |
|
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2364 |
long c = ((((long)(-parallelism) << TC_SHIFT) & TC_MASK) | |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2365 |
(((long)(-parallelism) << RC_SHIFT) & RC_MASK)); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2366 |
int b = ((1 - parallelism) & SMASK) | (COMMON_MAX_SPARES << SWIDTH); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2367 |
int n = (parallelism > 1) ? parallelism - 1 : 1; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2368 |
n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2369 |
n = (n + 1) << 1; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2370 |
|
41131
87edc8451f8a
8165919: Miscellaneous changes imported from jsr166 CVS 2016-09-21
dl
parents:
40817
diff
changeset
|
2371 |
this.workerNamePrefix = "ForkJoinPool.commonPool-worker-"; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2372 |
this.workQueues = new WorkQueue[n]; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2373 |
this.factory = fac; |
6543 | 2374 |
this.ueh = handler; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2375 |
this.saturate = null; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2376 |
this.keepAlive = DEFAULT_KEEPALIVE; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2377 |
this.bounds = b; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2378 |
this.mode = parallelism; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2379 |
this.ctl = c; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2380 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2381 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2382 |
/** |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2383 |
* Returns the common pool instance. This pool is statically |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2384 |
* constructed; its run state is unaffected by attempts to {@link |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2385 |
* #shutdown} or {@link #shutdownNow}. However this pool and any |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2386 |
* ongoing processing are automatically terminated upon program |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2387 |
* {@link System#exit}. Any program that relies on asynchronous |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2388 |
* task processing to complete before program termination should |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2389 |
* invoke {@code commonPool().}{@link #awaitQuiescence awaitQuiescence}, |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2390 |
* before exit. |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2391 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2392 |
* @return the common pool instance |
15002
8ab8c2b2eee6
8005709: Add at since tags to new FJP getCommonPoolParallelism and commonPool
chegar
parents:
14914
diff
changeset
|
2393 |
* @since 1.8 |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2394 |
*/ |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2395 |
public static ForkJoinPool commonPool() { |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2396 |
// assert common != null : "static init error"; |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2397 |
return common; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2398 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2399 |
|
4110 | 2400 |
// Execution methods |
2401 |
||
2402 |
/** |
|
2403 |
* Performs the given task, returning its result upon completion. |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2404 |
* If the computation encounters an unchecked Exception or Error, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2405 |
* it is rethrown as the outcome of this invocation. Rethrown |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2406 |
* exceptions behave in the same way as regular exceptions, but, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2407 |
* when possible, contain stack traces (as displayed for example |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2408 |
* using {@code ex.printStackTrace()}) of both the current thread |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2409 |
* as well as the thread actually encountering the exception; |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2410 |
* minimally only the latter. |
4110 | 2411 |
* |
2412 |
* @param task the task |
|
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18790
diff
changeset
|
2413 |
* @param <T> the type of the task's result |
4110 | 2414 |
* @return the task's result |
2415 |
* @throws NullPointerException if the task is null |
|
2416 |
* @throws RejectedExecutionException if the task cannot be |
|
2417 |
* scheduled for execution |
|
2418 |
*/ |
|
2419 |
public <T> T invoke(ForkJoinTask<T> task) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
2420 |
if (task == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
2421 |
throw new NullPointerException(); |
32988 | 2422 |
externalSubmit(task); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2423 |
return task.join(); |
4110 | 2424 |
} |
2425 |
||
2426 |
/** |
|
2427 |
* Arranges for (asynchronous) execution of the given task. |
|
2428 |
* |
|
2429 |
* @param task the task |
|
2430 |
* @throws NullPointerException if the task is null |
|
2431 |
* @throws RejectedExecutionException if the task cannot be |
|
2432 |
* scheduled for execution |
|
2433 |
*/ |
|
2434 |
public void execute(ForkJoinTask<?> task) { |
|
32988 | 2435 |
externalSubmit(task); |
4110 | 2436 |
} |
2437 |
||
2438 |
// AbstractExecutorService methods |
|
2439 |
||
2440 |
/** |
|
2441 |
* @throws NullPointerException if the task is null |
|
2442 |
* @throws RejectedExecutionException if the task cannot be |
|
2443 |
* scheduled for execution |
|
2444 |
*/ |
|
2445 |
public void execute(Runnable task) { |
|
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
2446 |
if (task == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
2447 |
throw new NullPointerException(); |
4110 | 2448 |
ForkJoinTask<?> job; |
2449 |
if (task instanceof ForkJoinTask<?>) // avoid re-wrap |
|
2450 |
job = (ForkJoinTask<?>) task; |
|
2451 |
else |
|
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2452 |
job = new ForkJoinTask.RunnableExecuteAction(task); |
32988 | 2453 |
externalSubmit(job); |
4110 | 2454 |
} |
2455 |
||
2456 |
/** |
|
6543 | 2457 |
* Submits a ForkJoinTask for execution. |
2458 |
* |
|
2459 |
* @param task the task to submit |
|
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18790
diff
changeset
|
2460 |
* @param <T> the type of the task's result |
6543 | 2461 |
* @return the task |
2462 |
* @throws NullPointerException if the task is null |
|
2463 |
* @throws RejectedExecutionException if the task cannot be |
|
2464 |
* scheduled for execution |
|
2465 |
*/ |
|
2466 |
public <T> ForkJoinTask<T> submit(ForkJoinTask<T> task) { |
|
32988 | 2467 |
return externalSubmit(task); |
6543 | 2468 |
} |
2469 |
||
2470 |
/** |
|
4110 | 2471 |
* @throws NullPointerException if the task is null |
2472 |
* @throws RejectedExecutionException if the task cannot be |
|
2473 |
* scheduled for execution |
|
2474 |
*/ |
|
2475 |
public <T> ForkJoinTask<T> submit(Callable<T> task) { |
|
32988 | 2476 |
return externalSubmit(new ForkJoinTask.AdaptedCallable<T>(task)); |
4110 | 2477 |
} |
2478 |
||
2479 |
/** |
|
2480 |
* @throws NullPointerException if the task is null |
|
2481 |
* @throws RejectedExecutionException if the task cannot be |
|
2482 |
* scheduled for execution |
|
2483 |
*/ |
|
2484 |
public <T> ForkJoinTask<T> submit(Runnable task, T result) { |
|
32988 | 2485 |
return externalSubmit(new ForkJoinTask.AdaptedRunnable<T>(task, result)); |
4110 | 2486 |
} |
2487 |
||
2488 |
/** |
|
2489 |
* @throws NullPointerException if the task is null |
|
2490 |
* @throws RejectedExecutionException if the task cannot be |
|
2491 |
* scheduled for execution |
|
2492 |
*/ |
|
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43545
diff
changeset
|
2493 |
@SuppressWarnings("unchecked") |
4110 | 2494 |
public ForkJoinTask<?> submit(Runnable task) { |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
2495 |
if (task == null) |
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
2496 |
throw new NullPointerException(); |
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43545
diff
changeset
|
2497 |
return externalSubmit((task instanceof ForkJoinTask<?>) |
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43545
diff
changeset
|
2498 |
? (ForkJoinTask<Void>) task // avoid re-wrap |
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43545
diff
changeset
|
2499 |
: new ForkJoinTask.AdaptedRunnableAction(task)); |
4110 | 2500 |
} |
2501 |
||
2502 |
/** |
|
2503 |
* @throws NullPointerException {@inheritDoc} |
|
2504 |
* @throws RejectedExecutionException {@inheritDoc} |
|
2505 |
*/ |
|
2506 |
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) { |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2507 |
// In previous versions of this class, this method constructed |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2508 |
// a task to run ForkJoinTask.invokeAll, but now external |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2509 |
// invocation of multiple tasks is at least as efficient. |
26448 | 2510 |
ArrayList<Future<T>> futures = new ArrayList<>(tasks.size()); |
4110 | 2511 |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2512 |
try { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2513 |
for (Callable<T> t : tasks) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2514 |
ForkJoinTask<T> f = new ForkJoinTask.AdaptedCallable<T>(t); |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2515 |
futures.add(f); |
32988 | 2516 |
externalSubmit(f); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2517 |
} |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2518 |
for (int i = 0, size = futures.size(); i < size; i++) |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2519 |
((ForkJoinTask<?>)futures.get(i)).quietlyJoin(); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2520 |
return futures; |
32988 | 2521 |
} catch (Throwable t) { |
2522 |
for (int i = 0, size = futures.size(); i < size; i++) |
|
2523 |
futures.get(i).cancel(false); |
|
2524 |
throw t; |
|
4110 | 2525 |
} |
2526 |
} |
|
2527 |
||
2528 |
/** |
|
2529 |
* Returns the factory used for constructing new workers. |
|
2530 |
* |
|
2531 |
* @return the factory used for constructing new workers |
|
2532 |
*/ |
|
2533 |
public ForkJoinWorkerThreadFactory getFactory() { |
|
2534 |
return factory; |
|
2535 |
} |
|
2536 |
||
2537 |
/** |
|
2538 |
* Returns the handler for internal worker threads that terminate |
|
2539 |
* due to unrecoverable errors encountered while executing tasks. |
|
2540 |
* |
|
2541 |
* @return the handler, or {@code null} if none |
|
2542 |
*/ |
|
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2543 |
public UncaughtExceptionHandler getUncaughtExceptionHandler() { |
6543 | 2544 |
return ueh; |
4110 | 2545 |
} |
2546 |
||
2547 |
/** |
|
2548 |
* Returns the targeted parallelism level of this pool. |
|
2549 |
* |
|
2550 |
* @return the targeted parallelism level of this pool |
|
2551 |
*/ |
|
2552 |
public int getParallelism() { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2553 |
int par = mode & SMASK; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2554 |
return (par > 0) ? par : 1; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2555 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2556 |
|
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2557 |
/** |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2558 |
* Returns the targeted parallelism level of the common pool. |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2559 |
* |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2560 |
* @return the targeted parallelism level of the common pool |
15002
8ab8c2b2eee6
8005709: Add at since tags to new FJP getCommonPoolParallelism and commonPool
chegar
parents:
14914
diff
changeset
|
2561 |
* @since 1.8 |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2562 |
*/ |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2563 |
public static int getCommonPoolParallelism() { |
32988 | 2564 |
return COMMON_PARALLELISM; |
4110 | 2565 |
} |
2566 |
||
2567 |
/** |
|
2568 |
* Returns the number of worker threads that have started but not |
|
6543 | 2569 |
* yet terminated. The result returned by this method may differ |
4110 | 2570 |
* from {@link #getParallelism} when threads are created to |
2571 |
* maintain parallelism when others are cooperatively blocked. |
|
2572 |
* |
|
2573 |
* @return the number of worker threads |
|
2574 |
*/ |
|
2575 |
public int getPoolSize() { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2576 |
return ((mode & SMASK) + (short)(ctl >>> TC_SHIFT)); |
4110 | 2577 |
} |
2578 |
||
2579 |
/** |
|
2580 |
* Returns {@code true} if this pool uses local first-in-first-out |
|
2581 |
* scheduling mode for forked tasks that are never joined. |
|
2582 |
* |
|
2583 |
* @return {@code true} if this pool uses async mode |
|
2584 |
*/ |
|
2585 |
public boolean getAsyncMode() { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2586 |
return (mode & FIFO) != 0; |
4110 | 2587 |
} |
2588 |
||
2589 |
/** |
|
2590 |
* Returns an estimate of the number of worker threads that are |
|
2591 |
* not blocked waiting to join tasks or for other managed |
|
6543 | 2592 |
* synchronization. This method may overestimate the |
2593 |
* number of running threads. |
|
4110 | 2594 |
* |
2595 |
* @return the number of worker threads |
|
2596 |
*/ |
|
2597 |
public int getRunningThreadCount() { |
|
49563 | 2598 |
WorkQueue[] ws; WorkQueue w; |
2599 |
VarHandle.acquireFence(); |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2600 |
int rc = 0; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2601 |
if ((ws = workQueues) != null) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2602 |
for (int i = 1; i < ws.length; i += 2) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2603 |
if ((w = ws[i]) != null && w.isApparentlyUnblocked()) |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2604 |
++rc; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2605 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2606 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2607 |
return rc; |
4110 | 2608 |
} |
2609 |
||
2610 |
/** |
|
2611 |
* Returns an estimate of the number of threads that are currently |
|
2612 |
* stealing or executing tasks. This method may overestimate the |
|
2613 |
* number of active threads. |
|
2614 |
* |
|
2615 |
* @return the number of active threads |
|
2616 |
*/ |
|
2617 |
public int getActiveThreadCount() { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2618 |
int r = (mode & SMASK) + (int)(ctl >> RC_SHIFT); |
9278 | 2619 |
return (r <= 0) ? 0 : r; // suppress momentarily negative values |
4110 | 2620 |
} |
2621 |
||
2622 |
/** |
|
2623 |
* Returns {@code true} if all worker threads are currently idle. |
|
2624 |
* An idle worker is one that cannot obtain a task to execute |
|
2625 |
* because none are available to steal from other threads, and |
|
2626 |
* there are no pending submissions to the pool. This method is |
|
2627 |
* conservative; it might not return {@code true} immediately upon |
|
2628 |
* idleness of all threads, but will eventually become true if |
|
2629 |
* threads remain inactive. |
|
2630 |
* |
|
2631 |
* @return {@code true} if all threads are currently idle |
|
2632 |
*/ |
|
2633 |
public boolean isQuiescent() { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2634 |
for (;;) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2635 |
long c = ctl; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2636 |
int md = mode, pc = md & SMASK; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2637 |
int tc = pc + (short)(c >>> TC_SHIFT); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2638 |
int rc = pc + (int)(c >> RC_SHIFT); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2639 |
if ((md & (STOP | TERMINATED)) != 0) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2640 |
return true; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2641 |
else if (rc > 0) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2642 |
return false; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2643 |
else { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2644 |
WorkQueue[] ws; WorkQueue v; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2645 |
if ((ws = workQueues) != null) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2646 |
for (int i = 1; i < ws.length; i += 2) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2647 |
if ((v = ws[i]) != null) { |
49563 | 2648 |
if (v.source > 0) |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2649 |
return false; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2650 |
--tc; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2651 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2652 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2653 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2654 |
if (tc == 0 && ctl == c) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2655 |
return true; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2656 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2657 |
} |
4110 | 2658 |
} |
2659 |
||
2660 |
/** |
|
2661 |
* Returns an estimate of the total number of tasks stolen from |
|
2662 |
* one thread's work queue by another. The reported value |
|
2663 |
* underestimates the actual total number of steals when the pool |
|
2664 |
* is not quiescent. This value may be useful for monitoring and |
|
2665 |
* tuning fork/join programs: in general, steal counts should be |
|
2666 |
* high enough to keep threads busy, but low enough to avoid |
|
2667 |
* overhead and contention across threads. |
|
2668 |
* |
|
2669 |
* @return the number of steals |
|
2670 |
*/ |
|
2671 |
public long getStealCount() { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2672 |
long count = stealCount; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2673 |
WorkQueue[] ws; WorkQueue w; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2674 |
if ((ws = workQueues) != null) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2675 |
for (int i = 1; i < ws.length; i += 2) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2676 |
if ((w = ws[i]) != null) |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2677 |
count += (long)w.nsteals & 0xffffffffL; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2678 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2679 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2680 |
return count; |
4110 | 2681 |
} |
2682 |
||
2683 |
/** |
|
2684 |
* Returns an estimate of the total number of tasks currently held |
|
2685 |
* in queues by worker threads (but not including tasks submitted |
|
2686 |
* to the pool that have not begun executing). This value is only |
|
2687 |
* an approximation, obtained by iterating across all threads in |
|
2688 |
* the pool. This method may be useful for tuning task |
|
2689 |
* granularities. |
|
2690 |
* |
|
2691 |
* @return the number of queued tasks |
|
2692 |
*/ |
|
2693 |
public long getQueuedTaskCount() { |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2694 |
WorkQueue[] ws; WorkQueue w; |
49563 | 2695 |
VarHandle.acquireFence(); |
2696 |
int count = 0; |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2697 |
if ((ws = workQueues) != null) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2698 |
for (int i = 1; i < ws.length; i += 2) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2699 |
if ((w = ws[i]) != null) |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2700 |
count += w.queueSize(); |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2701 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2702 |
} |
4110 | 2703 |
return count; |
2704 |
} |
|
2705 |
||
2706 |
/** |
|
2707 |
* 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
|
2708 |
* pool that have not yet begun executing. This method may take |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2709 |
* time proportional to the number of submissions. |
4110 | 2710 |
* |
2711 |
* @return the number of queued submissions |
|
2712 |
*/ |
|
2713 |
public int getQueuedSubmissionCount() { |
|
49563 | 2714 |
WorkQueue[] ws; WorkQueue w; |
2715 |
VarHandle.acquireFence(); |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2716 |
int count = 0; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2717 |
if ((ws = workQueues) != null) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2718 |
for (int i = 0; i < ws.length; i += 2) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2719 |
if ((w = ws[i]) != null) |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2720 |
count += w.queueSize(); |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2721 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2722 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2723 |
return count; |
4110 | 2724 |
} |
2725 |
||
2726 |
/** |
|
2727 |
* Returns {@code true} if there are any tasks submitted to this |
|
2728 |
* pool that have not yet begun executing. |
|
2729 |
* |
|
2730 |
* @return {@code true} if there are any queued submissions |
|
2731 |
*/ |
|
2732 |
public boolean hasQueuedSubmissions() { |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2733 |
WorkQueue[] ws; WorkQueue w; |
49563 | 2734 |
VarHandle.acquireFence(); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2735 |
if ((ws = workQueues) != null) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2736 |
for (int i = 0; i < ws.length; i += 2) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2737 |
if ((w = ws[i]) != null && !w.isEmpty()) |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2738 |
return true; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2739 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2740 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2741 |
return false; |
4110 | 2742 |
} |
2743 |
||
2744 |
/** |
|
2745 |
* Removes and returns the next unexecuted submission if one is |
|
2746 |
* available. This method may be useful in extensions to this |
|
2747 |
* class that re-assign work in systems with multiple pools. |
|
2748 |
* |
|
2749 |
* @return the next submission, or {@code null} if none |
|
2750 |
*/ |
|
2751 |
protected ForkJoinTask<?> pollSubmission() { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2752 |
return pollScan(true); |
4110 | 2753 |
} |
2754 |
||
2755 |
/** |
|
2756 |
* Removes all available unexecuted submitted and forked tasks |
|
2757 |
* from scheduling queues and adds them to the given collection, |
|
2758 |
* without altering their execution status. These may include |
|
2759 |
* artificially generated or wrapped tasks. This method is |
|
2760 |
* designed to be invoked only when the pool is known to be |
|
2761 |
* quiescent. Invocations at other times may not remove all |
|
2762 |
* tasks. A failure encountered while attempting to add elements |
|
2763 |
* to collection {@code c} may result in elements being in |
|
2764 |
* neither, either or both collections when the associated |
|
2765 |
* exception is thrown. The behavior of this operation is |
|
2766 |
* undefined if the specified collection is modified while the |
|
2767 |
* operation is in progress. |
|
2768 |
* |
|
2769 |
* @param c the collection to transfer elements into |
|
2770 |
* @return the number of elements transferred |
|
2771 |
*/ |
|
2772 |
protected int drainTasksTo(Collection<? super ForkJoinTask<?>> c) { |
|
49563 | 2773 |
WorkQueue[] ws; WorkQueue w; ForkJoinTask<?> t; |
2774 |
VarHandle.acquireFence(); |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2775 |
int count = 0; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2776 |
if ((ws = workQueues) != null) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2777 |
for (int i = 0; i < ws.length; ++i) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2778 |
if ((w = ws[i]) != null) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2779 |
while ((t = w.poll()) != null) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2780 |
c.add(t); |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2781 |
++count; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2782 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2783 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2784 |
} |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2785 |
} |
6543 | 2786 |
return count; |
4110 | 2787 |
} |
2788 |
||
2789 |
/** |
|
2790 |
* Returns a string identifying this pool, as well as its state, |
|
2791 |
* including indications of run state, parallelism level, and |
|
2792 |
* worker and task counts. |
|
2793 |
* |
|
2794 |
* @return a string identifying this pool, as well as its state |
|
2795 |
*/ |
|
2796 |
public String toString() { |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2797 |
// Use a single pass through workQueues to collect counts |
49563 | 2798 |
int md = mode; // read volatile fields first |
2799 |
long c = ctl; |
|
2800 |
long st = stealCount; |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2801 |
long qt = 0L, qs = 0L; int rc = 0; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2802 |
WorkQueue[] ws; WorkQueue w; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2803 |
if ((ws = workQueues) != null) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2804 |
for (int i = 0; i < ws.length; ++i) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2805 |
if ((w = ws[i]) != null) { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2806 |
int size = w.queueSize(); |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2807 |
if ((i & 1) == 0) |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2808 |
qs += size; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2809 |
else { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2810 |
qt += size; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2811 |
st += (long)w.nsteals & 0xffffffffL; |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2812 |
if (w.isApparentlyUnblocked()) |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2813 |
++rc; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2814 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2815 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2816 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2817 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2818 |
|
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2819 |
int pc = (md & SMASK); |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2820 |
int tc = pc + (short)(c >>> TC_SHIFT); |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2821 |
int ac = pc + (int)(c >> RC_SHIFT); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2822 |
if (ac < 0) // ignore transient negative |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2823 |
ac = 0; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2824 |
String level = ((md & TERMINATED) != 0 ? "Terminated" : |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2825 |
(md & STOP) != 0 ? "Terminating" : |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2826 |
(md & SHUTDOWN) != 0 ? "Shutting down" : |
26448 | 2827 |
"Running"); |
4110 | 2828 |
return super.toString() + |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2829 |
"[" + level + |
6543 | 2830 |
", parallelism = " + pc + |
2831 |
", size = " + tc + |
|
2832 |
", active = " + ac + |
|
2833 |
", running = " + rc + |
|
4110 | 2834 |
", steals = " + st + |
2835 |
", tasks = " + qt + |
|
2836 |
", submissions = " + qs + |
|
2837 |
"]"; |
|
2838 |
} |
|
2839 |
||
2840 |
/** |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2841 |
* Possibly initiates an orderly shutdown in which previously |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2842 |
* submitted tasks are executed, but no new tasks will be |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2843 |
* accepted. Invocation has no effect on execution state if this |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2844 |
* is the {@link #commonPool()}, and no additional effect if |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2845 |
* already shut down. Tasks that are in the process of being |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2846 |
* submitted concurrently during the course of this method may or |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2847 |
* may not be rejected. |
4110 | 2848 |
* |
2849 |
* @throws SecurityException if a security manager exists and |
|
2850 |
* the caller is not permitted to modify threads |
|
2851 |
* because it does not hold {@link |
|
2852 |
* java.lang.RuntimePermission}{@code ("modifyThread")} |
|
2853 |
*/ |
|
2854 |
public void shutdown() { |
|
2855 |
checkPermission(); |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2856 |
tryTerminate(false, true); |
4110 | 2857 |
} |
2858 |
||
2859 |
/** |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2860 |
* Possibly attempts to cancel and/or stop all tasks, and reject |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2861 |
* all subsequently submitted tasks. Invocation has no effect on |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2862 |
* execution state if this is the {@link #commonPool()}, and no |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2863 |
* additional effect if already shut down. Otherwise, tasks that |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2864 |
* are in the process of being submitted or executed concurrently |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2865 |
* during the course of this method may or may not be |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2866 |
* rejected. This method cancels both existing and unexecuted |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2867 |
* tasks, in order to permit termination in the presence of task |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2868 |
* dependencies. So the method always returns an empty list |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2869 |
* (unlike the case for some other Executors). |
4110 | 2870 |
* |
2871 |
* @return an empty list |
|
2872 |
* @throws SecurityException if a security manager exists and |
|
2873 |
* the caller is not permitted to modify threads |
|
2874 |
* because it does not hold {@link |
|
2875 |
* java.lang.RuntimePermission}{@code ("modifyThread")} |
|
2876 |
*/ |
|
2877 |
public List<Runnable> shutdownNow() { |
|
2878 |
checkPermission(); |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2879 |
tryTerminate(true, true); |
4110 | 2880 |
return Collections.emptyList(); |
2881 |
} |
|
2882 |
||
2883 |
/** |
|
2884 |
* Returns {@code true} if all tasks have completed following shut down. |
|
2885 |
* |
|
2886 |
* @return {@code true} if all tasks have completed following shut down |
|
2887 |
*/ |
|
2888 |
public boolean isTerminated() { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2889 |
return (mode & TERMINATED) != 0; |
4110 | 2890 |
} |
2891 |
||
2892 |
/** |
|
2893 |
* Returns {@code true} if the process of termination has |
|
2894 |
* commenced but not yet completed. This method may be useful for |
|
2895 |
* debugging. A return of {@code true} reported a sufficient |
|
2896 |
* period after shutdown may indicate that submitted tasks have |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2897 |
* ignored or suppressed interruption, or are waiting for I/O, |
7976
f273c0d04215
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents:
6674
diff
changeset
|
2898 |
* 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
|
2899 |
* 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
|
2900 |
* 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
|
2901 |
* they do, they must abort them on interrupt.) |
4110 | 2902 |
* |
2903 |
* @return {@code true} if terminating but not yet terminated |
|
2904 |
*/ |
|
2905 |
public boolean isTerminating() { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2906 |
int md = mode; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2907 |
return (md & STOP) != 0 && (md & TERMINATED) == 0; |
6674 | 2908 |
} |
2909 |
||
2910 |
/** |
|
4110 | 2911 |
* Returns {@code true} if this pool has been shut down. |
2912 |
* |
|
2913 |
* @return {@code true} if this pool has been shut down |
|
2914 |
*/ |
|
2915 |
public boolean isShutdown() { |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2916 |
return (mode & SHUTDOWN) != 0; |
4110 | 2917 |
} |
2918 |
||
2919 |
/** |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2920 |
* Blocks until all tasks have completed execution after a |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2921 |
* shutdown request, or the timeout occurs, or the current thread |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2922 |
* is interrupted, whichever happens first. Because the {@link |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2923 |
* #commonPool()} never terminates until program shutdown, when |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2924 |
* applied to the common pool, this method is equivalent to {@link |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2925 |
* #awaitQuiescence(long, TimeUnit)} but always returns {@code false}. |
4110 | 2926 |
* |
2927 |
* @param timeout the maximum time to wait |
|
2928 |
* @param unit the time unit of the timeout argument |
|
2929 |
* @return {@code true} if this executor terminated and |
|
2930 |
* {@code false} if the timeout elapsed before termination |
|
2931 |
* @throws InterruptedException if interrupted while waiting |
|
2932 |
*/ |
|
2933 |
public boolean awaitTermination(long timeout, TimeUnit unit) |
|
2934 |
throws InterruptedException { |
|
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2935 |
if (Thread.interrupted()) |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2936 |
throw new InterruptedException(); |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2937 |
if (this == common) { |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2938 |
awaitQuiescence(timeout, unit); |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2939 |
return false; |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2940 |
} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2941 |
long nanos = unit.toNanos(timeout); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2942 |
if (isTerminated()) |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2943 |
return true; |
18790 | 2944 |
if (nanos <= 0L) |
2945 |
return false; |
|
2946 |
long deadline = System.nanoTime() + nanos; |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
2947 |
synchronized (this) { |
18790 | 2948 |
for (;;) { |
2949 |
if (isTerminated()) |
|
2950 |
return true; |
|
2951 |
if (nanos <= 0L) |
|
2952 |
return false; |
|
2953 |
long millis = TimeUnit.NANOSECONDS.toMillis(nanos); |
|
2954 |
wait(millis > 0L ? millis : 1L); |
|
2955 |
nanos = deadline - System.nanoTime(); |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
2956 |
} |
4110 | 2957 |
} |
2958 |
} |
|
2959 |
||
2960 |
/** |
|
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2961 |
* If called by a ForkJoinTask operating in this pool, equivalent |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2962 |
* in effect to {@link ForkJoinTask#helpQuiesce}. Otherwise, |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2963 |
* waits and/or attempts to assist performing tasks until this |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2964 |
* pool {@link #isQuiescent} or the indicated timeout elapses. |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2965 |
* |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2966 |
* @param timeout the maximum time to wait |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2967 |
* @param unit the time unit of the timeout argument |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2968 |
* @return {@code true} if quiescent; {@code false} if the |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2969 |
* timeout elapsed. |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2970 |
*/ |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2971 |
public boolean awaitQuiescence(long timeout, TimeUnit unit) { |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2972 |
long nanos = unit.toNanos(timeout); |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2973 |
ForkJoinWorkerThread wt; |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2974 |
Thread thread = Thread.currentThread(); |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2975 |
if ((thread instanceof ForkJoinWorkerThread) && |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2976 |
(wt = (ForkJoinWorkerThread)thread).pool == this) { |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2977 |
helpQuiescePool(wt.workQueue); |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2978 |
return true; |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2979 |
} |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2980 |
else { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2981 |
for (long startTime = System.nanoTime();;) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2982 |
ForkJoinTask<?> t; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2983 |
if ((t = pollScan(false)) != null) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2984 |
t.doExec(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2985 |
else if (isQuiescent()) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2986 |
return true; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2987 |
else if ((System.nanoTime() - startTime) > nanos) |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2988 |
return false; |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2989 |
else |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
2990 |
Thread.yield(); // cannot block |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2991 |
} |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2992 |
} |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2993 |
} |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2994 |
|
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2995 |
/** |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2996 |
* Waits and/or attempts to assist performing tasks indefinitely |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2997 |
* until the {@link #commonPool()} {@link #isQuiescent}. |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2998 |
*/ |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
2999 |
static void quiesceCommonPool() { |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
3000 |
common.awaitQuiescence(Long.MAX_VALUE, TimeUnit.NANOSECONDS); |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
3001 |
} |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
3002 |
|
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
3003 |
/** |
4110 | 3004 |
* Interface for extending managed parallelism for tasks running |
3005 |
* in {@link ForkJoinPool}s. |
|
3006 |
* |
|
6543 | 3007 |
* <p>A {@code ManagedBlocker} provides two methods. Method |
26448 | 3008 |
* {@link #isReleasable} must return {@code true} if blocking is |
3009 |
* not necessary. Method {@link #block} blocks the current thread |
|
6543 | 3010 |
* if necessary (perhaps internally invoking {@code isReleasable} |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
3011 |
* before actually blocking). These actions are performed by any |
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
3012 |
* thread invoking {@link ForkJoinPool#managedBlock(ManagedBlocker)}. |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
3013 |
* The unusual methods in this API accommodate synchronizers that |
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
3014 |
* may, but don't usually, block for long periods. Similarly, they |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
3015 |
* allow more efficient internal handling of cases in which |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
3016 |
* additional workers may be, but usually are not, needed to |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
3017 |
* ensure sufficient parallelism. Toward this end, |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
3018 |
* implementations of method {@code isReleasable} must be amenable |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
3019 |
* to repeated invocation. |
4110 | 3020 |
* |
3021 |
* <p>For example, here is a ManagedBlocker based on a |
|
3022 |
* ReentrantLock: |
|
32988 | 3023 |
* <pre> {@code |
4110 | 3024 |
* class ManagedLocker implements ManagedBlocker { |
3025 |
* final ReentrantLock lock; |
|
3026 |
* boolean hasLock = false; |
|
3027 |
* ManagedLocker(ReentrantLock lock) { this.lock = lock; } |
|
3028 |
* public boolean block() { |
|
3029 |
* if (!hasLock) |
|
3030 |
* lock.lock(); |
|
3031 |
* return true; |
|
3032 |
* } |
|
3033 |
* public boolean isReleasable() { |
|
3034 |
* return hasLock || (hasLock = lock.tryLock()); |
|
3035 |
* } |
|
3036 |
* }}</pre> |
|
6543 | 3037 |
* |
3038 |
* <p>Here is a class that possibly blocks waiting for an |
|
3039 |
* item on a given queue: |
|
32988 | 3040 |
* <pre> {@code |
6543 | 3041 |
* class QueueTaker<E> implements ManagedBlocker { |
3042 |
* final BlockingQueue<E> queue; |
|
3043 |
* volatile E item = null; |
|
3044 |
* QueueTaker(BlockingQueue<E> q) { this.queue = q; } |
|
3045 |
* public boolean block() throws InterruptedException { |
|
3046 |
* if (item == null) |
|
3047 |
* item = queue.take(); |
|
3048 |
* return true; |
|
3049 |
* } |
|
3050 |
* public boolean isReleasable() { |
|
3051 |
* return item != null || (item = queue.poll()) != null; |
|
3052 |
* } |
|
3053 |
* public E getItem() { // call after pool.managedBlock completes |
|
3054 |
* return item; |
|
3055 |
* } |
|
3056 |
* }}</pre> |
|
4110 | 3057 |
*/ |
3058 |
public static interface ManagedBlocker { |
|
3059 |
/** |
|
3060 |
* Possibly blocks the current thread, for example waiting for |
|
3061 |
* a lock or condition. |
|
3062 |
* |
|
3063 |
* @return {@code true} if no additional blocking is necessary |
|
3064 |
* (i.e., if isReleasable would return true) |
|
3065 |
* @throws InterruptedException if interrupted while waiting |
|
3066 |
* (the method is not required to do so, but is allowed to) |
|
3067 |
*/ |
|
3068 |
boolean block() throws InterruptedException; |
|
3069 |
||
3070 |
/** |
|
3071 |
* Returns {@code true} if blocking is unnecessary. |
|
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
3072 |
* @return {@code true} if blocking is unnecessary |
4110 | 3073 |
*/ |
3074 |
boolean isReleasable(); |
|
3075 |
} |
|
3076 |
||
3077 |
/** |
|
26448 | 3078 |
* Runs the given possibly blocking task. When {@linkplain |
3079 |
* ForkJoinTask#inForkJoinPool() running in a ForkJoinPool}, this |
|
3080 |
* method possibly arranges for a spare thread to be activated if |
|
3081 |
* necessary to ensure sufficient parallelism while the current |
|
3082 |
* thread is blocked in {@link ManagedBlocker#block blocker.block()}. |
|
4110 | 3083 |
* |
26448 | 3084 |
* <p>This method repeatedly calls {@code blocker.isReleasable()} and |
3085 |
* {@code blocker.block()} until either method returns {@code true}. |
|
3086 |
* Every call to {@code blocker.block()} is preceded by a call to |
|
3087 |
* {@code blocker.isReleasable()} that returned {@code false}. |
|
3088 |
* |
|
3089 |
* <p>If not running in a ForkJoinPool, this method is |
|
4110 | 3090 |
* behaviorally equivalent to |
32988 | 3091 |
* <pre> {@code |
4110 | 3092 |
* while (!blocker.isReleasable()) |
3093 |
* if (blocker.block()) |
|
26448 | 3094 |
* break;}</pre> |
4110 | 3095 |
* |
26448 | 3096 |
* If running in a ForkJoinPool, the pool may first be expanded to |
3097 |
* ensure sufficient parallelism available during the call to |
|
3098 |
* {@code blocker.block()}. |
|
4110 | 3099 |
* |
26448 | 3100 |
* @param blocker the blocker task |
3101 |
* @throws InterruptedException if {@code blocker.block()} did so |
|
4110 | 3102 |
*/ |
6543 | 3103 |
public static void managedBlock(ManagedBlocker blocker) |
4110 | 3104 |
throws InterruptedException { |
49563 | 3105 |
if (blocker == null) throw new NullPointerException(); |
26448 | 3106 |
ForkJoinPool p; |
3107 |
ForkJoinWorkerThread wt; |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3108 |
WorkQueue w; |
4110 | 3109 |
Thread t = Thread.currentThread(); |
26448 | 3110 |
if ((t instanceof ForkJoinWorkerThread) && |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3111 |
(p = (wt = (ForkJoinWorkerThread)t).pool) != null && |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3112 |
(w = wt.workQueue) != null) { |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3113 |
int block; |
18790 | 3114 |
while (!blocker.isReleasable()) { |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3115 |
if ((block = p.tryCompensate(w)) != 0) { |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3116 |
try { |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3117 |
do {} while (!blocker.isReleasable() && |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3118 |
!blocker.block()); |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3119 |
} finally { |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3120 |
CTL.getAndAdd(p, (block > 0) ? RC_UNIT : 0L); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3121 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3122 |
break; |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3123 |
} |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3124 |
} |
4110 | 3125 |
} |
6543 | 3126 |
else { |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3127 |
do {} while (!blocker.isReleasable() && |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3128 |
!blocker.block()); |
6543 | 3129 |
} |
4110 | 3130 |
} |
3131 |
||
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3132 |
/** |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3133 |
* If the given executor is a ForkJoinPool, poll and execute |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3134 |
* AsynchronousCompletionTasks from worker's queue until none are |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3135 |
* available or blocker is released. |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3136 |
*/ |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3137 |
static void helpAsyncBlocker(Executor e, ManagedBlocker blocker) { |
49563 | 3138 |
if (e instanceof ForkJoinPool) { |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3139 |
WorkQueue w; ForkJoinWorkerThread wt; WorkQueue[] ws; int r, n; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3140 |
ForkJoinPool p = (ForkJoinPool)e; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3141 |
Thread thread = Thread.currentThread(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3142 |
if (thread instanceof ForkJoinWorkerThread && |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3143 |
(wt = (ForkJoinWorkerThread)thread).pool == p) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3144 |
w = wt.workQueue; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3145 |
else if ((r = ThreadLocalRandom.getProbe()) != 0 && |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3146 |
(ws = p.workQueues) != null && (n = ws.length) > 0) |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3147 |
w = ws[(n - 1) & r & SQMASK]; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3148 |
else |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3149 |
w = null; |
49563 | 3150 |
if (w != null) |
3151 |
w.helpAsyncBlocker(blocker); |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3152 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3153 |
} |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3154 |
|
4110 | 3155 |
// AbstractExecutorService overrides. These rely on undocumented |
3156 |
// fact that ForkJoinTask.adapt returns ForkJoinTasks that also |
|
3157 |
// implement RunnableFuture. |
|
3158 |
||
3159 |
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) { |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3160 |
return new ForkJoinTask.AdaptedRunnable<T>(runnable, value); |
4110 | 3161 |
} |
3162 |
||
3163 |
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) { |
|
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3164 |
return new ForkJoinTask.AdaptedCallable<T>(callable); |
4110 | 3165 |
} |
3166 |
||
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3167 |
// VarHandle mechanics |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3168 |
private static final VarHandle CTL; |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3169 |
private static final VarHandle MODE; |
49563 | 3170 |
static final VarHandle QA; |
4110 | 3171 |
|
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
3172 |
static { |
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
3173 |
try { |
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3174 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3175 |
CTL = l.findVarHandle(ForkJoinPool.class, "ctl", long.class); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3176 |
MODE = l.findVarHandle(ForkJoinPool.class, "mode", int.class); |
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3177 |
QA = MethodHandles.arrayElementVarHandle(ForkJoinTask[].class); |
32988 | 3178 |
} catch (ReflectiveOperationException e) { |
49563 | 3179 |
throw new ExceptionInInitializerError(e); |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
3180 |
} |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3181 |
|
32988 | 3182 |
// Reduce the risk of rare disastrous classloading in first call to |
3183 |
// LockSupport.park: https://bugs.openjdk.java.net/browse/JDK-8074773 |
|
3184 |
Class<?> ensureLoaded = LockSupport.class; |
|
3185 |
||
3186 |
int commonMaxSpares = DEFAULT_COMMON_MAX_SPARES; |
|
3187 |
try { |
|
3188 |
String p = System.getProperty |
|
3189 |
("java.util.concurrent.ForkJoinPool.common.maximumSpares"); |
|
3190 |
if (p != null) |
|
3191 |
commonMaxSpares = Integer.parseInt(p); |
|
3192 |
} catch (Exception ignore) {} |
|
3193 |
COMMON_MAX_SPARES = commonMaxSpares; |
|
3194 |
||
16049
92a3a919d4dc
8008378: FJP.commonPool support parallelism 0, add awaitQuiescence
chegar
parents:
15002
diff
changeset
|
3195 |
defaultForkJoinWorkerThreadFactory = |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3196 |
new DefaultForkJoinWorkerThreadFactory(); |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3197 |
modifyThreadPermission = new RuntimePermission("modifyThread"); |
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3198 |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41131
diff
changeset
|
3199 |
common = AccessController.doPrivileged(new PrivilegedAction<>() { |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41131
diff
changeset
|
3200 |
public ForkJoinPool run() { |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41131
diff
changeset
|
3201 |
return new ForkJoinPool((byte)0); }}); |
14914
69bfd88e3ea1
8002356: Add ForkJoin common pool and CountedCompleter
dl
parents:
11279
diff
changeset
|
3202 |
|
39723
9aa34e4a0469
8157523: Various improvements to ForkJoin/SubmissionPublisher code
dl
parents:
37782
diff
changeset
|
3203 |
COMMON_PARALLELISM = Math.max(common.mode & SMASK, 1); |
8765
dfc2a131d08a
7023006: Reduce unnecessary thread activity in ForkJoinPool
dl
parents:
7976
diff
changeset
|
3204 |
} |
4110 | 3205 |
|
22070 | 3206 |
/** |
32988 | 3207 |
* Factory for innocuous worker threads. |
22070 | 3208 |
*/ |
32988 | 3209 |
private static final class InnocuousForkJoinWorkerThreadFactory |
22070 | 3210 |
implements ForkJoinWorkerThreadFactory { |
3211 |
||
3212 |
/** |
|
3213 |
* An ACC to restrict permissions for the factory itself. |
|
3214 |
* The constructed workers have no permissions set. |
|
3215 |
*/ |
|
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
3216 |
private static final AccessControlContext ACC = contextWithPermissions( |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
3217 |
modifyThreadPermission, |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
3218 |
new RuntimePermission("enableContextClassLoaderOverride"), |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
3219 |
new RuntimePermission("modifyThreadGroup"), |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
3220 |
new RuntimePermission("getClassLoader"), |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
3221 |
new RuntimePermission("setContextClassLoader")); |
22070 | 3222 |
|
3223 |
public final ForkJoinWorkerThread newThread(ForkJoinPool pool) { |
|
43545
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
3224 |
return AccessController.doPrivileged( |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
3225 |
new PrivilegedAction<>() { |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
3226 |
public ForkJoinWorkerThread run() { |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
3227 |
return new ForkJoinWorkerThread. |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
3228 |
InnocuousForkJoinWorkerThread(pool); }}, |
fdecd1d14d53
8172726: ForkJoin common pool retains a reference to the thread context class loader
dl
parents:
42322
diff
changeset
|
3229 |
ACC); |
22070 | 3230 |
} |
3231 |
} |
|
4110 | 3232 |
} |