author | dl |
Fri, 15 Jul 2016 13:51:43 -0700 | |
changeset 39722 | a0b0fa5763b1 |
parent 33674 | 566777f73c32 |
child 39780 | 18618975fbb6 |
permissions | -rw-r--r-- |
15648 | 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 |
|
6 |
* published by the Free Software Foundation. Oracle designates this |
|
7 |
* particular file as subject to the "Classpath" exception as provided |
|
8 |
* by Oracle in the LICENSE file that accompanied this code. |
|
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 |
* |
|
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. |
|
23 |
*/ |
|
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
34 |
*/ |
|
35 |
||
36 |
package java.util.concurrent.locks; |
|
37 |
||
39722 | 38 |
import java.lang.invoke.MethodHandles; |
39 |
import java.lang.invoke.VarHandle; |
|
15648 | 40 |
import java.util.concurrent.TimeUnit; |
39722 | 41 |
import jdk.internal.vm.annotation.ReservedStackAccess; |
15648 | 42 |
|
43 |
/** |
|
44 |
* A capability-based lock with three modes for controlling read/write |
|
45 |
* access. The state of a StampedLock consists of a version and mode. |
|
46 |
* Lock acquisition methods return a stamp that represents and |
|
47 |
* controls access with respect to a lock state; "try" versions of |
|
48 |
* these methods may instead return the special value zero to |
|
49 |
* represent failure to acquire access. Lock release and conversion |
|
50 |
* methods require stamps as arguments, and fail if they do not match |
|
51 |
* the state of the lock. The three modes are: |
|
52 |
* |
|
53 |
* <ul> |
|
54 |
* |
|
55 |
* <li><b>Writing.</b> Method {@link #writeLock} possibly blocks |
|
56 |
* waiting for exclusive access, returning a stamp that can be used |
|
57 |
* in method {@link #unlockWrite} to release the lock. Untimed and |
|
58 |
* timed versions of {@code tryWriteLock} are also provided. When |
|
59 |
* the lock is held in write mode, no read locks may be obtained, |
|
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
60 |
* and all optimistic read validations will fail. |
15648 | 61 |
* |
62 |
* <li><b>Reading.</b> Method {@link #readLock} possibly blocks |
|
63 |
* waiting for non-exclusive access, returning a stamp that can be |
|
64 |
* used in method {@link #unlockRead} to release the lock. Untimed |
|
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
65 |
* and timed versions of {@code tryReadLock} are also provided. |
15648 | 66 |
* |
67 |
* <li><b>Optimistic Reading.</b> Method {@link #tryOptimisticRead} |
|
68 |
* returns a non-zero stamp only if the lock is not currently held |
|
69 |
* in write mode. Method {@link #validate} returns true if the lock |
|
70 |
* has not been acquired in write mode since obtaining a given |
|
71 |
* stamp. This mode can be thought of as an extremely weak version |
|
72 |
* of a read-lock, that can be broken by a writer at any time. The |
|
73 |
* use of optimistic mode for short read-only code segments often |
|
74 |
* reduces contention and improves throughput. However, its use is |
|
75 |
* inherently fragile. Optimistic read sections should only read |
|
76 |
* fields and hold them in local variables for later use after |
|
77 |
* validation. Fields read while in optimistic mode may be wildly |
|
78 |
* inconsistent, so usage applies only when you are familiar enough |
|
79 |
* with data representations to check consistency and/or repeatedly |
|
80 |
* invoke method {@code validate()}. For example, such steps are |
|
81 |
* typically required when first reading an object or array |
|
82 |
* reference, and then accessing one of its fields, elements or |
|
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
83 |
* methods. |
15648 | 84 |
* |
85 |
* </ul> |
|
86 |
* |
|
87 |
* <p>This class also supports methods that conditionally provide |
|
88 |
* conversions across the three modes. For example, method {@link |
|
89 |
* #tryConvertToWriteLock} attempts to "upgrade" a mode, returning |
|
90 |
* a valid write stamp if (1) already in writing mode (2) in reading |
|
91 |
* mode and there are no other readers or (3) in optimistic mode and |
|
92 |
* the lock is available. The forms of these methods are designed to |
|
93 |
* help reduce some of the code bloat that otherwise occurs in |
|
94 |
* retry-based designs. |
|
95 |
* |
|
96 |
* <p>StampedLocks are designed for use as internal utilities in the |
|
97 |
* development of thread-safe components. Their use relies on |
|
98 |
* knowledge of the internal properties of the data, objects, and |
|
99 |
* methods they are protecting. They are not reentrant, so locked |
|
100 |
* bodies should not call other unknown methods that may try to |
|
101 |
* re-acquire locks (although you may pass a stamp to other methods |
|
102 |
* that can use or convert it). The use of read lock modes relies on |
|
103 |
* the associated code sections being side-effect-free. Unvalidated |
|
104 |
* optimistic read sections cannot call methods that are not known to |
|
105 |
* tolerate potential inconsistencies. Stamps use finite |
|
106 |
* representations, and are not cryptographically secure (i.e., a |
|
107 |
* valid stamp may be guessable). Stamp values may recycle after (no |
|
108 |
* sooner than) one year of continuous operation. A stamp held without |
|
109 |
* use or validation for longer than this period may fail to validate |
|
110 |
* correctly. StampedLocks are serializable, but always deserialize |
|
111 |
* into initial unlocked state, so they are not useful for remote |
|
112 |
* locking. |
|
113 |
* |
|
39722 | 114 |
* <p>Like {@link java.util.concurrent.Semaphore Semaphore}, but unlike most |
115 |
* {@link Lock} implementations, StampedLocks have no notion of ownership. |
|
116 |
* Locks acquired in one thread can be released or converted in another. |
|
117 |
* |
|
15648 | 118 |
* <p>The scheduling policy of StampedLock does not consistently |
119 |
* prefer readers over writers or vice versa. All "try" methods are |
|
120 |
* best-effort and do not necessarily conform to any scheduling or |
|
121 |
* fairness policy. A zero return from any "try" method for acquiring |
|
122 |
* or converting locks does not carry any information about the state |
|
123 |
* of the lock; a subsequent invocation may succeed. |
|
124 |
* |
|
125 |
* <p>Because it supports coordinated usage across multiple lock |
|
126 |
* modes, this class does not directly implement the {@link Lock} or |
|
127 |
* {@link ReadWriteLock} interfaces. However, a StampedLock may be |
|
128 |
* viewed {@link #asReadLock()}, {@link #asWriteLock()}, or {@link |
|
129 |
* #asReadWriteLock()} in applications requiring only the associated |
|
130 |
* set of functionality. |
|
131 |
* |
|
132 |
* <p><b>Sample Usage.</b> The following illustrates some usage idioms |
|
133 |
* in a class that maintains simple two-dimensional points. The sample |
|
134 |
* code illustrates some try/catch conventions even though they are |
|
135 |
* not strictly needed here because no exceptions can occur in their |
|
39722 | 136 |
* bodies. |
15648 | 137 |
* |
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
138 |
* <pre> {@code |
15648 | 139 |
* class Point { |
140 |
* private double x, y; |
|
141 |
* private final StampedLock sl = new StampedLock(); |
|
142 |
* |
|
143 |
* void move(double deltaX, double deltaY) { // an exclusively locked method |
|
144 |
* long stamp = sl.writeLock(); |
|
145 |
* try { |
|
146 |
* x += deltaX; |
|
147 |
* y += deltaY; |
|
148 |
* } finally { |
|
149 |
* sl.unlockWrite(stamp); |
|
150 |
* } |
|
151 |
* } |
|
152 |
* |
|
153 |
* double distanceFromOrigin() { // A read-only method |
|
154 |
* long stamp = sl.tryOptimisticRead(); |
|
155 |
* double currentX = x, currentY = y; |
|
156 |
* if (!sl.validate(stamp)) { |
|
157 |
* stamp = sl.readLock(); |
|
158 |
* try { |
|
159 |
* currentX = x; |
|
160 |
* currentY = y; |
|
161 |
* } finally { |
|
162 |
* sl.unlockRead(stamp); |
|
163 |
* } |
|
164 |
* } |
|
165 |
* return Math.sqrt(currentX * currentX + currentY * currentY); |
|
166 |
* } |
|
167 |
* |
|
168 |
* void moveIfAtOrigin(double newX, double newY) { // upgrade |
|
169 |
* // Could instead start with optimistic, not read mode |
|
170 |
* long stamp = sl.readLock(); |
|
171 |
* try { |
|
172 |
* while (x == 0.0 && y == 0.0) { |
|
173 |
* long ws = sl.tryConvertToWriteLock(stamp); |
|
174 |
* if (ws != 0L) { |
|
175 |
* stamp = ws; |
|
176 |
* x = newX; |
|
177 |
* y = newY; |
|
178 |
* break; |
|
179 |
* } |
|
180 |
* else { |
|
181 |
* sl.unlockRead(stamp); |
|
182 |
* stamp = sl.writeLock(); |
|
183 |
* } |
|
184 |
* } |
|
185 |
* } finally { |
|
186 |
* sl.unlock(stamp); |
|
187 |
* } |
|
188 |
* } |
|
189 |
* }}</pre> |
|
190 |
* |
|
191 |
* @since 1.8 |
|
192 |
* @author Doug Lea |
|
193 |
*/ |
|
194 |
public class StampedLock implements java.io.Serializable { |
|
195 |
/* |
|
196 |
* Algorithmic notes: |
|
197 |
* |
|
198 |
* The design employs elements of Sequence locks |
|
199 |
* (as used in linux kernels; see Lameter's |
|
200 |
* http://www.lameter.com/gelato2005.pdf |
|
201 |
* and elsewhere; see |
|
202 |
* Boehm's http://www.hpl.hp.com/techreports/2012/HPL-2012-68.html) |
|
203 |
* and Ordered RW locks (see Shirako et al |
|
204 |
* http://dl.acm.org/citation.cfm?id=2312015) |
|
205 |
* |
|
206 |
* Conceptually, the primary state of the lock includes a sequence |
|
207 |
* number that is odd when write-locked and even otherwise. |
|
208 |
* However, this is offset by a reader count that is non-zero when |
|
209 |
* read-locked. The read count is ignored when validating |
|
210 |
* "optimistic" seqlock-reader-style stamps. Because we must use |
|
211 |
* a small finite number of bits (currently 7) for readers, a |
|
212 |
* supplementary reader overflow word is used when the number of |
|
213 |
* readers exceeds the count field. We do this by treating the max |
|
214 |
* reader count value (RBITS) as a spinlock protecting overflow |
|
215 |
* updates. |
|
216 |
* |
|
217 |
* Waiters use a modified form of CLH lock used in |
|
218 |
* AbstractQueuedSynchronizer (see its internal documentation for |
|
219 |
* a fuller account), where each node is tagged (field mode) as |
|
220 |
* either a reader or writer. Sets of waiting readers are grouped |
|
221 |
* (linked) under a common node (field cowait) so act as a single |
|
222 |
* node with respect to most CLH mechanics. By virtue of the |
|
223 |
* queue structure, wait nodes need not actually carry sequence |
|
224 |
* numbers; we know each is greater than its predecessor. This |
|
225 |
* simplifies the scheduling policy to a mainly-FIFO scheme that |
|
226 |
* incorporates elements of Phase-Fair locks (see Brandenburg & |
|
227 |
* Anderson, especially http://www.cs.unc.edu/~bbb/diss/). In |
|
228 |
* particular, we use the phase-fair anti-barging rule: If an |
|
229 |
* incoming reader arrives while read lock is held but there is a |
|
230 |
* queued writer, this incoming reader is queued. (This rule is |
|
231 |
* responsible for some of the complexity of method acquireRead, |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
232 |
* but without it, the lock becomes highly unfair.) Method release |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
233 |
* does not (and sometimes cannot) itself wake up cowaiters. This |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
234 |
* is done by the primary thread, but helped by any other threads |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
235 |
* with nothing better to do in methods acquireRead and |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
236 |
* acquireWrite. |
15648 | 237 |
* |
238 |
* These rules apply to threads actually queued. All tryLock forms |
|
239 |
* opportunistically try to acquire locks regardless of preference |
|
240 |
* rules, and so may "barge" their way in. Randomized spinning is |
|
241 |
* used in the acquire methods to reduce (increasingly expensive) |
|
242 |
* context switching while also avoiding sustained memory |
|
243 |
* thrashing among many threads. We limit spins to the head of |
|
39722 | 244 |
* queue. If, upon wakening, a thread fails to obtain lock, and is |
15648 | 245 |
* still (or becomes) the first waiting thread (which indicates |
246 |
* that some other thread barged and obtained lock), it escalates |
|
247 |
* spins (up to MAX_HEAD_SPINS) to reduce the likelihood of |
|
248 |
* continually losing to barging threads. |
|
249 |
* |
|
250 |
* Nearly all of these mechanics are carried out in methods |
|
251 |
* acquireWrite and acquireRead, that, as typical of such code, |
|
252 |
* sprawl out because actions and retries rely on consistent sets |
|
253 |
* of locally cached reads. |
|
254 |
* |
|
255 |
* As noted in Boehm's paper (above), sequence validation (mainly |
|
256 |
* method validate()) requires stricter ordering rules than apply |
|
257 |
* to normal volatile reads (of "state"). To force orderings of |
|
258 |
* reads before a validation and the validation itself in those |
|
259 |
* cases where this is not already forced, we use |
|
39722 | 260 |
* VarHandle.acquireFence. |
15648 | 261 |
* |
262 |
* The memory layout keeps lock state and queue pointers together |
|
263 |
* (normally on the same cache line). This usually works well for |
|
264 |
* read-mostly loads. In most other cases, the natural tendency of |
|
265 |
* adaptive-spin CLH locks to reduce memory contention lessens |
|
266 |
* motivation to further spread out contended locations, but might |
|
267 |
* be subject to future improvements. |
|
268 |
*/ |
|
269 |
||
270 |
private static final long serialVersionUID = -6001602636862214147L; |
|
271 |
||
272 |
/** Number of processors, for spin control */ |
|
273 |
private static final int NCPU = Runtime.getRuntime().availableProcessors(); |
|
274 |
||
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
275 |
/** Maximum number of retries before enqueuing on acquisition */ |
15648 | 276 |
private static final int SPINS = (NCPU > 1) ? 1 << 6 : 0; |
277 |
||
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
278 |
/** Maximum number of retries before blocking at head on acquisition */ |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
279 |
private static final int HEAD_SPINS = (NCPU > 1) ? 1 << 10 : 0; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
280 |
|
15648 | 281 |
/** Maximum number of retries before re-blocking */ |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
282 |
private static final int MAX_HEAD_SPINS = (NCPU > 1) ? 1 << 16 : 0; |
15648 | 283 |
|
284 |
/** The period for yielding when waiting for overflow spinlock */ |
|
285 |
private static final int OVERFLOW_YIELD_RATE = 7; // must be power 2 - 1 |
|
286 |
||
287 |
/** The number of bits to use for reader count before overflowing */ |
|
288 |
private static final int LG_READERS = 7; |
|
289 |
||
290 |
// Values for lock state and stamp operations |
|
291 |
private static final long RUNIT = 1L; |
|
292 |
private static final long WBIT = 1L << LG_READERS; |
|
293 |
private static final long RBITS = WBIT - 1L; |
|
294 |
private static final long RFULL = RBITS - 1L; |
|
295 |
private static final long ABITS = RBITS | WBIT; |
|
296 |
private static final long SBITS = ~RBITS; // note overlap with ABITS |
|
297 |
||
39722 | 298 |
/* |
299 |
* 3 stamp modes can be distinguished by examining (m = stamp & ABITS): |
|
300 |
* write mode: m == WBIT |
|
301 |
* optimistic read mode: m == 0L (even when read lock is held) |
|
302 |
* read mode: m > 0L && m <= RFULL (the stamp is a copy of state, but the |
|
303 |
* read hold count in the stamp is unused other than to determine mode) |
|
304 |
* |
|
305 |
* This differs slightly from the encoding of state: |
|
306 |
* (state & ABITS) == 0L indicates the lock is currently unlocked. |
|
307 |
* (state & ABITS) == RBITS is a special transient value |
|
308 |
* indicating spin-locked to manipulate reader bits overflow. |
|
309 |
*/ |
|
310 |
||
311 |
/** Initial value for lock state; avoids failure value zero. */ |
|
15648 | 312 |
private static final long ORIGIN = WBIT << 1; |
313 |
||
314 |
// Special value from cancelled acquire methods so caller can throw IE |
|
315 |
private static final long INTERRUPTED = 1L; |
|
316 |
||
317 |
// Values for node status; order matters |
|
318 |
private static final int WAITING = -1; |
|
319 |
private static final int CANCELLED = 1; |
|
320 |
||
321 |
// Modes for nodes (int not boolean to allow arithmetic) |
|
322 |
private static final int RMODE = 0; |
|
323 |
private static final int WMODE = 1; |
|
324 |
||
325 |
/** Wait nodes */ |
|
326 |
static final class WNode { |
|
327 |
volatile WNode prev; |
|
328 |
volatile WNode next; |
|
329 |
volatile WNode cowait; // list of linked readers |
|
330 |
volatile Thread thread; // non-null while possibly parked |
|
331 |
volatile int status; // 0, WAITING, or CANCELLED |
|
332 |
final int mode; // RMODE or WMODE |
|
333 |
WNode(int m, WNode p) { mode = m; prev = p; } |
|
334 |
} |
|
335 |
||
336 |
/** Head of CLH queue */ |
|
337 |
private transient volatile WNode whead; |
|
338 |
/** Tail (last) of CLH queue */ |
|
339 |
private transient volatile WNode wtail; |
|
340 |
||
341 |
// views |
|
342 |
transient ReadLockView readLockView; |
|
343 |
transient WriteLockView writeLockView; |
|
344 |
transient ReadWriteLockView readWriteLockView; |
|
345 |
||
346 |
/** Lock sequence/state */ |
|
347 |
private transient volatile long state; |
|
348 |
/** extra reader count when state read count saturated */ |
|
349 |
private transient int readerOverflow; |
|
350 |
||
351 |
/** |
|
352 |
* Creates a new lock, initially in unlocked state. |
|
353 |
*/ |
|
354 |
public StampedLock() { |
|
355 |
state = ORIGIN; |
|
356 |
} |
|
357 |
||
358 |
/** |
|
359 |
* Exclusively acquires the lock, blocking if necessary |
|
360 |
* until available. |
|
361 |
* |
|
39722 | 362 |
* @return a write stamp that can be used to unlock or convert mode |
15648 | 363 |
*/ |
39722 | 364 |
@ReservedStackAccess |
15648 | 365 |
public long writeLock() { |
366 |
long s, next; // bypass acquireWrite in fully unlocked case only |
|
367 |
return ((((s = state) & ABITS) == 0L && |
|
39722 | 368 |
STATE.compareAndSet(this, s, next = s + WBIT)) ? |
15648 | 369 |
next : acquireWrite(false, 0L)); |
370 |
} |
|
371 |
||
372 |
/** |
|
373 |
* Exclusively acquires the lock if it is immediately available. |
|
374 |
* |
|
39722 | 375 |
* @return a write stamp that can be used to unlock or convert mode, |
15648 | 376 |
* or zero if the lock is not available |
377 |
*/ |
|
39722 | 378 |
@ReservedStackAccess |
15648 | 379 |
public long tryWriteLock() { |
380 |
long s, next; |
|
381 |
return ((((s = state) & ABITS) == 0L && |
|
39722 | 382 |
STATE.compareAndSet(this, s, next = s + WBIT)) ? |
15648 | 383 |
next : 0L); |
384 |
} |
|
385 |
||
386 |
/** |
|
387 |
* Exclusively acquires the lock if it is available within the |
|
388 |
* given time and the current thread has not been interrupted. |
|
389 |
* Behavior under timeout and interruption matches that specified |
|
390 |
* for method {@link Lock#tryLock(long,TimeUnit)}. |
|
391 |
* |
|
18576 | 392 |
* @param time the maximum time to wait for the lock |
393 |
* @param unit the time unit of the {@code time} argument |
|
39722 | 394 |
* @return a write stamp that can be used to unlock or convert mode, |
15648 | 395 |
* or zero if the lock is not available |
396 |
* @throws InterruptedException if the current thread is interrupted |
|
397 |
* before acquiring the lock |
|
398 |
*/ |
|
399 |
public long tryWriteLock(long time, TimeUnit unit) |
|
400 |
throws InterruptedException { |
|
401 |
long nanos = unit.toNanos(time); |
|
402 |
if (!Thread.interrupted()) { |
|
403 |
long next, deadline; |
|
404 |
if ((next = tryWriteLock()) != 0L) |
|
405 |
return next; |
|
406 |
if (nanos <= 0L) |
|
407 |
return 0L; |
|
408 |
if ((deadline = System.nanoTime() + nanos) == 0L) |
|
409 |
deadline = 1L; |
|
410 |
if ((next = acquireWrite(true, deadline)) != INTERRUPTED) |
|
411 |
return next; |
|
412 |
} |
|
413 |
throw new InterruptedException(); |
|
414 |
} |
|
415 |
||
416 |
/** |
|
417 |
* Exclusively acquires the lock, blocking if necessary |
|
418 |
* until available or the current thread is interrupted. |
|
419 |
* Behavior under interruption matches that specified |
|
420 |
* for method {@link Lock#lockInterruptibly()}. |
|
421 |
* |
|
39722 | 422 |
* @return a write stamp that can be used to unlock or convert mode |
15648 | 423 |
* @throws InterruptedException if the current thread is interrupted |
424 |
* before acquiring the lock |
|
425 |
*/ |
|
39722 | 426 |
@ReservedStackAccess |
15648 | 427 |
public long writeLockInterruptibly() throws InterruptedException { |
428 |
long next; |
|
429 |
if (!Thread.interrupted() && |
|
430 |
(next = acquireWrite(true, 0L)) != INTERRUPTED) |
|
431 |
return next; |
|
432 |
throw new InterruptedException(); |
|
433 |
} |
|
434 |
||
435 |
/** |
|
436 |
* Non-exclusively acquires the lock, blocking if necessary |
|
437 |
* until available. |
|
438 |
* |
|
39722 | 439 |
* @return a read stamp that can be used to unlock or convert mode |
15648 | 440 |
*/ |
39722 | 441 |
@ReservedStackAccess |
15648 | 442 |
public long readLock() { |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
443 |
long s = state, next; // bypass acquireRead on common uncontended case |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
444 |
return ((whead == wtail && (s & ABITS) < RFULL && |
39722 | 445 |
STATE.compareAndSet(this, s, next = s + RUNIT)) ? |
15648 | 446 |
next : acquireRead(false, 0L)); |
447 |
} |
|
448 |
||
449 |
/** |
|
450 |
* Non-exclusively acquires the lock if it is immediately available. |
|
451 |
* |
|
39722 | 452 |
* @return a read stamp that can be used to unlock or convert mode, |
15648 | 453 |
* or zero if the lock is not available |
454 |
*/ |
|
39722 | 455 |
@ReservedStackAccess |
15648 | 456 |
public long tryReadLock() { |
39722 | 457 |
long s, m, next; |
458 |
while ((m = (s = state) & ABITS) != WBIT) { |
|
459 |
if (m < RFULL) { |
|
460 |
if (STATE.compareAndSet(this, s, next = s + RUNIT)) |
|
15648 | 461 |
return next; |
462 |
} |
|
463 |
else if ((next = tryIncReaderOverflow(s)) != 0L) |
|
464 |
return next; |
|
465 |
} |
|
39722 | 466 |
return 0L; |
15648 | 467 |
} |
468 |
||
469 |
/** |
|
470 |
* Non-exclusively acquires the lock if it is available within the |
|
471 |
* given time and the current thread has not been interrupted. |
|
472 |
* Behavior under timeout and interruption matches that specified |
|
473 |
* for method {@link Lock#tryLock(long,TimeUnit)}. |
|
474 |
* |
|
18576 | 475 |
* @param time the maximum time to wait for the lock |
476 |
* @param unit the time unit of the {@code time} argument |
|
39722 | 477 |
* @return a read stamp that can be used to unlock or convert mode, |
15648 | 478 |
* or zero if the lock is not available |
479 |
* @throws InterruptedException if the current thread is interrupted |
|
480 |
* before acquiring the lock |
|
481 |
*/ |
|
39722 | 482 |
@ReservedStackAccess |
15648 | 483 |
public long tryReadLock(long time, TimeUnit unit) |
484 |
throws InterruptedException { |
|
485 |
long s, m, next, deadline; |
|
486 |
long nanos = unit.toNanos(time); |
|
487 |
if (!Thread.interrupted()) { |
|
488 |
if ((m = (s = state) & ABITS) != WBIT) { |
|
489 |
if (m < RFULL) { |
|
39722 | 490 |
if (STATE.compareAndSet(this, s, next = s + RUNIT)) |
15648 | 491 |
return next; |
492 |
} |
|
493 |
else if ((next = tryIncReaderOverflow(s)) != 0L) |
|
494 |
return next; |
|
495 |
} |
|
496 |
if (nanos <= 0L) |
|
497 |
return 0L; |
|
498 |
if ((deadline = System.nanoTime() + nanos) == 0L) |
|
499 |
deadline = 1L; |
|
500 |
if ((next = acquireRead(true, deadline)) != INTERRUPTED) |
|
501 |
return next; |
|
502 |
} |
|
503 |
throw new InterruptedException(); |
|
504 |
} |
|
505 |
||
506 |
/** |
|
507 |
* Non-exclusively acquires the lock, blocking if necessary |
|
508 |
* until available or the current thread is interrupted. |
|
509 |
* Behavior under interruption matches that specified |
|
510 |
* for method {@link Lock#lockInterruptibly()}. |
|
511 |
* |
|
39722 | 512 |
* @return a read stamp that can be used to unlock or convert mode |
15648 | 513 |
* @throws InterruptedException if the current thread is interrupted |
514 |
* before acquiring the lock |
|
515 |
*/ |
|
39722 | 516 |
@ReservedStackAccess |
15648 | 517 |
public long readLockInterruptibly() throws InterruptedException { |
518 |
long next; |
|
519 |
if (!Thread.interrupted() && |
|
520 |
(next = acquireRead(true, 0L)) != INTERRUPTED) |
|
521 |
return next; |
|
522 |
throw new InterruptedException(); |
|
523 |
} |
|
524 |
||
525 |
/** |
|
526 |
* Returns a stamp that can later be validated, or zero |
|
527 |
* if exclusively locked. |
|
528 |
* |
|
39722 | 529 |
* @return a valid optimistic read stamp, or zero if exclusively locked |
15648 | 530 |
*/ |
531 |
public long tryOptimisticRead() { |
|
532 |
long s; |
|
533 |
return (((s = state) & WBIT) == 0L) ? (s & SBITS) : 0L; |
|
534 |
} |
|
535 |
||
536 |
/** |
|
537 |
* Returns true if the lock has not been exclusively acquired |
|
538 |
* since issuance of the given stamp. Always returns false if the |
|
539 |
* stamp is zero. Always returns true if the stamp represents a |
|
540 |
* currently held lock. Invoking this method with a value not |
|
541 |
* obtained from {@link #tryOptimisticRead} or a locking method |
|
542 |
* for this lock has no defined effect or result. |
|
543 |
* |
|
18576 | 544 |
* @param stamp a stamp |
545 |
* @return {@code true} if the lock has not been exclusively acquired |
|
15648 | 546 |
* since issuance of the given stamp; else false |
547 |
*/ |
|
548 |
public boolean validate(long stamp) { |
|
39722 | 549 |
VarHandle.acquireFence(); |
15648 | 550 |
return (stamp & SBITS) == (state & SBITS); |
551 |
} |
|
552 |
||
553 |
/** |
|
39722 | 554 |
* Returns an unlocked state, incrementing the version and |
555 |
* avoiding special failure value 0L. |
|
556 |
* |
|
557 |
* @param s a write-locked state (or stamp) |
|
558 |
*/ |
|
559 |
private static long unlockWriteState(long s) { |
|
560 |
return ((s += WBIT) == 0L) ? ORIGIN : s; |
|
561 |
} |
|
562 |
||
563 |
private long unlockWriteInternal(long s) { |
|
564 |
long next; WNode h; |
|
565 |
STATE.setVolatile(this, next = unlockWriteState(s)); |
|
566 |
if ((h = whead) != null && h.status != 0) |
|
567 |
release(h); |
|
568 |
return next; |
|
569 |
} |
|
570 |
||
571 |
/** |
|
15648 | 572 |
* If the lock state matches the given stamp, releases the |
573 |
* exclusive lock. |
|
574 |
* |
|
575 |
* @param stamp a stamp returned by a write-lock operation |
|
576 |
* @throws IllegalMonitorStateException if the stamp does |
|
577 |
* not match the current state of this lock |
|
578 |
*/ |
|
39722 | 579 |
@ReservedStackAccess |
15648 | 580 |
public void unlockWrite(long stamp) { |
581 |
if (state != stamp || (stamp & WBIT) == 0L) |
|
582 |
throw new IllegalMonitorStateException(); |
|
39722 | 583 |
unlockWriteInternal(stamp); |
15648 | 584 |
} |
585 |
||
586 |
/** |
|
587 |
* If the lock state matches the given stamp, releases the |
|
588 |
* non-exclusive lock. |
|
589 |
* |
|
590 |
* @param stamp a stamp returned by a read-lock operation |
|
591 |
* @throws IllegalMonitorStateException if the stamp does |
|
592 |
* not match the current state of this lock |
|
593 |
*/ |
|
39722 | 594 |
@ReservedStackAccess |
15648 | 595 |
public void unlockRead(long stamp) { |
596 |
long s, m; WNode h; |
|
39722 | 597 |
while (((s = state) & SBITS) == (stamp & SBITS) |
598 |
&& (stamp & RBITS) > 0L |
|
599 |
&& ((m = s & RBITS) > 0L)) { |
|
15648 | 600 |
if (m < RFULL) { |
39722 | 601 |
if (STATE.compareAndSet(this, s, s - RUNIT)) { |
15648 | 602 |
if (m == RUNIT && (h = whead) != null && h.status != 0) |
603 |
release(h); |
|
39722 | 604 |
return; |
15648 | 605 |
} |
606 |
} |
|
607 |
else if (tryDecReaderOverflow(s) != 0L) |
|
39722 | 608 |
return; |
15648 | 609 |
} |
39722 | 610 |
throw new IllegalMonitorStateException(); |
15648 | 611 |
} |
612 |
||
613 |
/** |
|
614 |
* If the lock state matches the given stamp, releases the |
|
615 |
* corresponding mode of the lock. |
|
616 |
* |
|
617 |
* @param stamp a stamp returned by a lock operation |
|
618 |
* @throws IllegalMonitorStateException if the stamp does |
|
619 |
* not match the current state of this lock |
|
620 |
*/ |
|
39722 | 621 |
@ReservedStackAccess |
15648 | 622 |
public void unlock(long stamp) { |
39722 | 623 |
if ((stamp & WBIT) != 0) |
624 |
unlockWrite(stamp); |
|
625 |
else |
|
626 |
unlockRead(stamp); |
|
15648 | 627 |
} |
628 |
||
629 |
/** |
|
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
630 |
* If the lock state matches the given stamp, atomically performs one of |
15648 | 631 |
* the following actions. If the stamp represents holding a write |
632 |
* lock, returns it. Or, if a read lock, if the write lock is |
|
633 |
* available, releases the read lock and returns a write stamp. |
|
634 |
* Or, if an optimistic read, returns a write stamp only if |
|
635 |
* immediately available. This method returns zero in all other |
|
636 |
* cases. |
|
637 |
* |
|
638 |
* @param stamp a stamp |
|
639 |
* @return a valid write stamp, or zero on failure |
|
640 |
*/ |
|
641 |
public long tryConvertToWriteLock(long stamp) { |
|
642 |
long a = stamp & ABITS, m, s, next; |
|
643 |
while (((s = state) & SBITS) == (stamp & SBITS)) { |
|
644 |
if ((m = s & ABITS) == 0L) { |
|
645 |
if (a != 0L) |
|
646 |
break; |
|
39722 | 647 |
if (STATE.compareAndSet(this, s, next = s + WBIT)) |
15648 | 648 |
return next; |
649 |
} |
|
650 |
else if (m == WBIT) { |
|
651 |
if (a != m) |
|
652 |
break; |
|
653 |
return stamp; |
|
654 |
} |
|
655 |
else if (m == RUNIT && a != 0L) { |
|
39722 | 656 |
if (STATE.compareAndSet(this, s, next = s - RUNIT + WBIT)) |
15648 | 657 |
return next; |
658 |
} |
|
659 |
else |
|
660 |
break; |
|
661 |
} |
|
662 |
return 0L; |
|
663 |
} |
|
664 |
||
665 |
/** |
|
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
666 |
* If the lock state matches the given stamp, atomically performs one of |
15648 | 667 |
* the following actions. If the stamp represents holding a write |
668 |
* lock, releases it and obtains a read lock. Or, if a read lock, |
|
669 |
* returns it. Or, if an optimistic read, acquires a read lock and |
|
670 |
* returns a read stamp only if immediately available. This method |
|
671 |
* returns zero in all other cases. |
|
672 |
* |
|
673 |
* @param stamp a stamp |
|
674 |
* @return a valid read stamp, or zero on failure |
|
675 |
*/ |
|
676 |
public long tryConvertToReadLock(long stamp) { |
|
39722 | 677 |
long a, s, next; WNode h; |
15648 | 678 |
while (((s = state) & SBITS) == (stamp & SBITS)) { |
39722 | 679 |
if ((a = stamp & ABITS) >= WBIT) { |
680 |
// write stamp |
|
681 |
if (s != stamp) |
|
15648 | 682 |
break; |
39722 | 683 |
STATE.setVolatile(this, next = unlockWriteState(s) + RUNIT); |
684 |
if ((h = whead) != null && h.status != 0) |
|
685 |
release(h); |
|
686 |
return next; |
|
687 |
} |
|
688 |
else if (a == 0L) { |
|
689 |
// optimistic read stamp |
|
690 |
if ((s & ABITS) < RFULL) { |
|
691 |
if (STATE.compareAndSet(this, s, next = s + RUNIT)) |
|
15648 | 692 |
return next; |
693 |
} |
|
694 |
else if ((next = tryIncReaderOverflow(s)) != 0L) |
|
695 |
return next; |
|
696 |
} |
|
39722 | 697 |
else { |
698 |
// already a read stamp |
|
699 |
if ((s & ABITS) == 0L) |
|
15648 | 700 |
break; |
39722 | 701 |
return stamp; |
15648 | 702 |
} |
703 |
} |
|
704 |
return 0L; |
|
705 |
} |
|
706 |
||
707 |
/** |
|
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
708 |
* If the lock state matches the given stamp then, atomically, if the stamp |
15648 | 709 |
* represents holding a lock, releases it and returns an |
710 |
* observation stamp. Or, if an optimistic read, returns it if |
|
711 |
* validated. This method returns zero in all other cases, and so |
|
712 |
* may be useful as a form of "tryUnlock". |
|
713 |
* |
|
714 |
* @param stamp a stamp |
|
715 |
* @return a valid optimistic read stamp, or zero on failure |
|
716 |
*/ |
|
717 |
public long tryConvertToOptimisticRead(long stamp) { |
|
39722 | 718 |
long a, m, s, next; WNode h; |
719 |
VarHandle.acquireFence(); |
|
720 |
while (((s = state) & SBITS) == (stamp & SBITS)) { |
|
721 |
if ((a = stamp & ABITS) >= WBIT) { |
|
722 |
// write stamp |
|
723 |
if (s != stamp) |
|
15648 | 724 |
break; |
39722 | 725 |
return unlockWriteInternal(s); |
15648 | 726 |
} |
39722 | 727 |
else if (a == 0L) |
728 |
// already an optimistic read stamp |
|
729 |
return stamp; |
|
730 |
else if ((m = s & ABITS) == 0L) // invalid read stamp |
|
15648 | 731 |
break; |
732 |
else if (m < RFULL) { |
|
39722 | 733 |
if (STATE.compareAndSet(this, s, next = s - RUNIT)) { |
15648 | 734 |
if (m == RUNIT && (h = whead) != null && h.status != 0) |
735 |
release(h); |
|
736 |
return next & SBITS; |
|
737 |
} |
|
738 |
} |
|
739 |
else if ((next = tryDecReaderOverflow(s)) != 0L) |
|
740 |
return next & SBITS; |
|
741 |
} |
|
742 |
return 0L; |
|
743 |
} |
|
744 |
||
745 |
/** |
|
746 |
* Releases the write lock if it is held, without requiring a |
|
747 |
* stamp value. This method may be useful for recovery after |
|
748 |
* errors. |
|
749 |
* |
|
18576 | 750 |
* @return {@code true} if the lock was held, else false |
15648 | 751 |
*/ |
39722 | 752 |
@ReservedStackAccess |
15648 | 753 |
public boolean tryUnlockWrite() { |
39722 | 754 |
long s; |
15648 | 755 |
if (((s = state) & WBIT) != 0L) { |
39722 | 756 |
unlockWriteInternal(s); |
15648 | 757 |
return true; |
758 |
} |
|
759 |
return false; |
|
760 |
} |
|
761 |
||
762 |
/** |
|
763 |
* Releases one hold of the read lock if it is held, without |
|
764 |
* requiring a stamp value. This method may be useful for recovery |
|
765 |
* after errors. |
|
766 |
* |
|
18576 | 767 |
* @return {@code true} if the read lock was held, else false |
15648 | 768 |
*/ |
39722 | 769 |
@ReservedStackAccess |
15648 | 770 |
public boolean tryUnlockRead() { |
771 |
long s, m; WNode h; |
|
772 |
while ((m = (s = state) & ABITS) != 0L && m < WBIT) { |
|
773 |
if (m < RFULL) { |
|
39722 | 774 |
if (STATE.compareAndSet(this, s, s - RUNIT)) { |
15648 | 775 |
if (m == RUNIT && (h = whead) != null && h.status != 0) |
776 |
release(h); |
|
777 |
return true; |
|
778 |
} |
|
779 |
} |
|
780 |
else if (tryDecReaderOverflow(s) != 0L) |
|
781 |
return true; |
|
782 |
} |
|
783 |
return false; |
|
784 |
} |
|
785 |
||
786 |
// status monitoring methods |
|
787 |
||
788 |
/** |
|
789 |
* Returns combined state-held and overflow read count for given |
|
790 |
* state s. |
|
791 |
*/ |
|
792 |
private int getReadLockCount(long s) { |
|
793 |
long readers; |
|
794 |
if ((readers = s & RBITS) >= RFULL) |
|
795 |
readers = RFULL + readerOverflow; |
|
796 |
return (int) readers; |
|
797 |
} |
|
798 |
||
799 |
/** |
|
18576 | 800 |
* Returns {@code true} if the lock is currently held exclusively. |
15648 | 801 |
* |
18576 | 802 |
* @return {@code true} if the lock is currently held exclusively |
15648 | 803 |
*/ |
804 |
public boolean isWriteLocked() { |
|
805 |
return (state & WBIT) != 0L; |
|
806 |
} |
|
807 |
||
808 |
/** |
|
18576 | 809 |
* Returns {@code true} if the lock is currently held non-exclusively. |
15648 | 810 |
* |
18576 | 811 |
* @return {@code true} if the lock is currently held non-exclusively |
15648 | 812 |
*/ |
813 |
public boolean isReadLocked() { |
|
814 |
return (state & RBITS) != 0L; |
|
815 |
} |
|
816 |
||
817 |
/** |
|
818 |
* Queries the number of read locks held for this lock. This |
|
819 |
* method is designed for use in monitoring system state, not for |
|
820 |
* synchronization control. |
|
821 |
* @return the number of read locks held |
|
822 |
*/ |
|
823 |
public int getReadLockCount() { |
|
824 |
return getReadLockCount(state); |
|
825 |
} |
|
826 |
||
827 |
/** |
|
828 |
* Returns a string identifying this lock, as well as its lock |
|
829 |
* state. The state, in brackets, includes the String {@code |
|
830 |
* "Unlocked"} or the String {@code "Write-locked"} or the String |
|
831 |
* {@code "Read-locks:"} followed by the current number of |
|
832 |
* read-locks held. |
|
833 |
* |
|
834 |
* @return a string identifying this lock, as well as its lock state |
|
835 |
*/ |
|
836 |
public String toString() { |
|
837 |
long s = state; |
|
838 |
return super.toString() + |
|
839 |
((s & ABITS) == 0L ? "[Unlocked]" : |
|
840 |
(s & WBIT) != 0L ? "[Write-locked]" : |
|
841 |
"[Read-locks:" + getReadLockCount(s) + "]"); |
|
842 |
} |
|
843 |
||
844 |
// views |
|
845 |
||
846 |
/** |
|
847 |
* Returns a plain {@link Lock} view of this StampedLock in which |
|
848 |
* the {@link Lock#lock} method is mapped to {@link #readLock}, |
|
849 |
* and similarly for other methods. The returned Lock does not |
|
39722 | 850 |
* support a {@link Condition}; method {@link Lock#newCondition()} |
851 |
* throws {@code UnsupportedOperationException}. |
|
15648 | 852 |
* |
853 |
* @return the lock |
|
854 |
*/ |
|
855 |
public Lock asReadLock() { |
|
856 |
ReadLockView v; |
|
39722 | 857 |
if ((v = readLockView) != null) return v; |
858 |
return readLockView = new ReadLockView(); |
|
15648 | 859 |
} |
860 |
||
861 |
/** |
|
862 |
* Returns a plain {@link Lock} view of this StampedLock in which |
|
863 |
* the {@link Lock#lock} method is mapped to {@link #writeLock}, |
|
864 |
* and similarly for other methods. The returned Lock does not |
|
39722 | 865 |
* support a {@link Condition}; method {@link Lock#newCondition()} |
866 |
* throws {@code UnsupportedOperationException}. |
|
15648 | 867 |
* |
868 |
* @return the lock |
|
869 |
*/ |
|
870 |
public Lock asWriteLock() { |
|
871 |
WriteLockView v; |
|
39722 | 872 |
if ((v = writeLockView) != null) return v; |
873 |
return writeLockView = new WriteLockView(); |
|
15648 | 874 |
} |
875 |
||
876 |
/** |
|
877 |
* Returns a {@link ReadWriteLock} view of this StampedLock in |
|
878 |
* which the {@link ReadWriteLock#readLock()} method is mapped to |
|
879 |
* {@link #asReadLock()}, and {@link ReadWriteLock#writeLock()} to |
|
880 |
* {@link #asWriteLock()}. |
|
881 |
* |
|
882 |
* @return the lock |
|
883 |
*/ |
|
884 |
public ReadWriteLock asReadWriteLock() { |
|
885 |
ReadWriteLockView v; |
|
39722 | 886 |
if ((v = readWriteLockView) != null) return v; |
887 |
return readWriteLockView = new ReadWriteLockView(); |
|
15648 | 888 |
} |
889 |
||
890 |
// view classes |
|
891 |
||
892 |
final class ReadLockView implements Lock { |
|
893 |
public void lock() { readLock(); } |
|
894 |
public void lockInterruptibly() throws InterruptedException { |
|
895 |
readLockInterruptibly(); |
|
896 |
} |
|
897 |
public boolean tryLock() { return tryReadLock() != 0L; } |
|
898 |
public boolean tryLock(long time, TimeUnit unit) |
|
899 |
throws InterruptedException { |
|
900 |
return tryReadLock(time, unit) != 0L; |
|
901 |
} |
|
902 |
public void unlock() { unstampedUnlockRead(); } |
|
903 |
public Condition newCondition() { |
|
904 |
throw new UnsupportedOperationException(); |
|
905 |
} |
|
906 |
} |
|
907 |
||
908 |
final class WriteLockView implements Lock { |
|
909 |
public void lock() { writeLock(); } |
|
910 |
public void lockInterruptibly() throws InterruptedException { |
|
911 |
writeLockInterruptibly(); |
|
912 |
} |
|
913 |
public boolean tryLock() { return tryWriteLock() != 0L; } |
|
914 |
public boolean tryLock(long time, TimeUnit unit) |
|
915 |
throws InterruptedException { |
|
916 |
return tryWriteLock(time, unit) != 0L; |
|
917 |
} |
|
918 |
public void unlock() { unstampedUnlockWrite(); } |
|
919 |
public Condition newCondition() { |
|
920 |
throw new UnsupportedOperationException(); |
|
921 |
} |
|
922 |
} |
|
923 |
||
924 |
final class ReadWriteLockView implements ReadWriteLock { |
|
925 |
public Lock readLock() { return asReadLock(); } |
|
926 |
public Lock writeLock() { return asWriteLock(); } |
|
927 |
} |
|
928 |
||
929 |
// Unlock methods without stamp argument checks for view classes. |
|
930 |
// Needed because view-class lock methods throw away stamps. |
|
931 |
||
932 |
final void unstampedUnlockWrite() { |
|
39722 | 933 |
long s; |
15648 | 934 |
if (((s = state) & WBIT) == 0L) |
935 |
throw new IllegalMonitorStateException(); |
|
39722 | 936 |
unlockWriteInternal(s); |
15648 | 937 |
} |
938 |
||
939 |
final void unstampedUnlockRead() { |
|
39722 | 940 |
long s, m; WNode h; |
941 |
while ((m = (s = state) & RBITS) > 0L) { |
|
942 |
if (m < RFULL) { |
|
943 |
if (STATE.compareAndSet(this, s, s - RUNIT)) { |
|
15648 | 944 |
if (m == RUNIT && (h = whead) != null && h.status != 0) |
945 |
release(h); |
|
39722 | 946 |
return; |
15648 | 947 |
} |
948 |
} |
|
949 |
else if (tryDecReaderOverflow(s) != 0L) |
|
39722 | 950 |
return; |
15648 | 951 |
} |
39722 | 952 |
throw new IllegalMonitorStateException(); |
15648 | 953 |
} |
954 |
||
955 |
private void readObject(java.io.ObjectInputStream s) |
|
956 |
throws java.io.IOException, ClassNotFoundException { |
|
957 |
s.defaultReadObject(); |
|
39722 | 958 |
STATE.setVolatile(this, ORIGIN); // reset to unlocked state |
15648 | 959 |
} |
960 |
||
961 |
// internals |
|
962 |
||
963 |
/** |
|
964 |
* Tries to increment readerOverflow by first setting state |
|
965 |
* access bits value to RBITS, indicating hold of spinlock, |
|
966 |
* then updating, then releasing. |
|
967 |
* |
|
968 |
* @param s a reader overflow stamp: (s & ABITS) >= RFULL |
|
969 |
* @return new stamp on success, else zero |
|
970 |
*/ |
|
971 |
private long tryIncReaderOverflow(long s) { |
|
972 |
// assert (s & ABITS) >= RFULL; |
|
973 |
if ((s & ABITS) == RFULL) { |
|
39722 | 974 |
if (STATE.compareAndSet(this, s, s | RBITS)) { |
15648 | 975 |
++readerOverflow; |
39722 | 976 |
STATE.setVolatile(this, s); |
15648 | 977 |
return s; |
978 |
} |
|
979 |
} |
|
39722 | 980 |
else if ((LockSupport.nextSecondarySeed() & OVERFLOW_YIELD_RATE) == 0) |
15648 | 981 |
Thread.yield(); |
39722 | 982 |
else |
983 |
Thread.onSpinWait(); |
|
15648 | 984 |
return 0L; |
985 |
} |
|
986 |
||
987 |
/** |
|
988 |
* Tries to decrement readerOverflow. |
|
989 |
* |
|
990 |
* @param s a reader overflow stamp: (s & ABITS) >= RFULL |
|
991 |
* @return new stamp on success, else zero |
|
992 |
*/ |
|
993 |
private long tryDecReaderOverflow(long s) { |
|
994 |
// assert (s & ABITS) >= RFULL; |
|
995 |
if ((s & ABITS) == RFULL) { |
|
39722 | 996 |
if (STATE.compareAndSet(this, s, s | RBITS)) { |
15648 | 997 |
int r; long next; |
998 |
if ((r = readerOverflow) > 0) { |
|
999 |
readerOverflow = r - 1; |
|
1000 |
next = s; |
|
1001 |
} |
|
1002 |
else |
|
1003 |
next = s - RUNIT; |
|
39722 | 1004 |
STATE.setVolatile(this, next); |
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1005 |
return next; |
15648 | 1006 |
} |
1007 |
} |
|
39722 | 1008 |
else if ((LockSupport.nextSecondarySeed() & OVERFLOW_YIELD_RATE) == 0) |
15648 | 1009 |
Thread.yield(); |
39722 | 1010 |
else |
1011 |
Thread.onSpinWait(); |
|
15648 | 1012 |
return 0L; |
1013 |
} |
|
1014 |
||
1015 |
/** |
|
1016 |
* Wakes up the successor of h (normally whead). This is normally |
|
1017 |
* just h.next, but may require traversal from wtail if next |
|
1018 |
* pointers are lagging. This may fail to wake up an acquiring |
|
1019 |
* thread when one or more have been cancelled, but the cancel |
|
1020 |
* methods themselves provide extra safeguards to ensure liveness. |
|
1021 |
*/ |
|
1022 |
private void release(WNode h) { |
|
1023 |
if (h != null) { |
|
1024 |
WNode q; Thread w; |
|
39722 | 1025 |
WSTATUS.compareAndSet(h, WAITING, 0); |
15648 | 1026 |
if ((q = h.next) == null || q.status == CANCELLED) { |
1027 |
for (WNode t = wtail; t != null && t != h; t = t.prev) |
|
1028 |
if (t.status <= 0) |
|
1029 |
q = t; |
|
1030 |
} |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1031 |
if (q != null && (w = q.thread) != null) |
39722 | 1032 |
LockSupport.unpark(w); |
15648 | 1033 |
} |
1034 |
} |
|
1035 |
||
1036 |
/** |
|
1037 |
* See above for explanation. |
|
1038 |
* |
|
1039 |
* @param interruptible true if should check interrupts and if so |
|
1040 |
* return INTERRUPTED |
|
1041 |
* @param deadline if nonzero, the System.nanoTime value to timeout |
|
1042 |
* at (and return zero) |
|
1043 |
* @return next state, or INTERRUPTED |
|
1044 |
*/ |
|
1045 |
private long acquireWrite(boolean interruptible, long deadline) { |
|
1046 |
WNode node = null, p; |
|
1047 |
for (int spins = -1;;) { // spin while enqueuing |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1048 |
long m, s, ns; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1049 |
if ((m = (s = state) & ABITS) == 0L) { |
39722 | 1050 |
if (STATE.compareAndSet(this, s, ns = s + WBIT)) |
15648 | 1051 |
return ns; |
1052 |
} |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1053 |
else if (spins < 0) |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1054 |
spins = (m == WBIT && wtail == whead) ? SPINS : 0; |
15648 | 1055 |
else if (spins > 0) { |
39722 | 1056 |
--spins; |
1057 |
Thread.onSpinWait(); |
|
15648 | 1058 |
} |
1059 |
else if ((p = wtail) == null) { // initialize queue |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1060 |
WNode hd = new WNode(WMODE, null); |
39722 | 1061 |
if (WHEAD.weakCompareAndSetVolatile(this, null, hd)) |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1062 |
wtail = hd; |
15648 | 1063 |
} |
1064 |
else if (node == null) |
|
1065 |
node = new WNode(WMODE, p); |
|
1066 |
else if (node.prev != p) |
|
1067 |
node.prev = p; |
|
39722 | 1068 |
else if (WTAIL.weakCompareAndSetVolatile(this, p, node)) { |
15648 | 1069 |
p.next = node; |
1070 |
break; |
|
1071 |
} |
|
1072 |
} |
|
1073 |
||
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1074 |
boolean wasInterrupted = false; |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1075 |
for (int spins = -1;;) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1076 |
WNode h, np, pp; int ps; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1077 |
if ((h = whead) == p) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1078 |
if (spins < 0) |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1079 |
spins = HEAD_SPINS; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1080 |
else if (spins < MAX_HEAD_SPINS) |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1081 |
spins <<= 1; |
39722 | 1082 |
for (int k = spins; k > 0; --k) { // spin at head |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1083 |
long s, ns; |
15648 | 1084 |
if (((s = state) & ABITS) == 0L) { |
39722 | 1085 |
if (STATE.compareAndSet(this, s, ns = s + WBIT)) { |
15648 | 1086 |
whead = node; |
1087 |
node.prev = null; |
|
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1088 |
if (wasInterrupted) |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1089 |
Thread.currentThread().interrupt(); |
15648 | 1090 |
return ns; |
1091 |
} |
|
1092 |
} |
|
39722 | 1093 |
else |
1094 |
Thread.onSpinWait(); |
|
15648 | 1095 |
} |
1096 |
} |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1097 |
else if (h != null) { // help release stale waiters |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1098 |
WNode c; Thread w; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1099 |
while ((c = h.cowait) != null) { |
39722 | 1100 |
if (WCOWAIT.weakCompareAndSetVolatile(h, c, c.cowait) && |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1101 |
(w = c.thread) != null) |
39722 | 1102 |
LockSupport.unpark(w); |
15648 | 1103 |
} |
1104 |
} |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1105 |
if (whead == h) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1106 |
if ((np = node.prev) != p) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1107 |
if (np != null) |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1108 |
(p = np).next = node; // stale |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1109 |
} |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1110 |
else if ((ps = p.status) == 0) |
39722 | 1111 |
WSTATUS.compareAndSet(p, 0, WAITING); |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1112 |
else if (ps == CANCELLED) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1113 |
if ((pp = p.prev) != null) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1114 |
node.prev = pp; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1115 |
pp.next = node; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1116 |
} |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1117 |
} |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1118 |
else { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1119 |
long time; // 0 argument to park means no timeout |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1120 |
if (deadline == 0L) |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1121 |
time = 0L; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1122 |
else if ((time = deadline - System.nanoTime()) <= 0L) |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1123 |
return cancelWaiter(node, node, false); |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1124 |
Thread wt = Thread.currentThread(); |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1125 |
node.thread = wt; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1126 |
if (p.status < 0 && (p != h || (state & ABITS) != 0L) && |
39722 | 1127 |
whead == h && node.prev == p) { |
1128 |
if (time == 0L) |
|
1129 |
LockSupport.park(this); |
|
1130 |
else |
|
1131 |
LockSupport.parkNanos(this, time); |
|
1132 |
} |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1133 |
node.thread = null; |
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1134 |
if (Thread.interrupted()) { |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1135 |
if (interruptible) |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1136 |
return cancelWaiter(node, node, true); |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1137 |
wasInterrupted = true; |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1138 |
} |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1139 |
} |
15648 | 1140 |
} |
1141 |
} |
|
1142 |
} |
|
1143 |
||
1144 |
/** |
|
1145 |
* See above for explanation. |
|
1146 |
* |
|
1147 |
* @param interruptible true if should check interrupts and if so |
|
1148 |
* return INTERRUPTED |
|
1149 |
* @param deadline if nonzero, the System.nanoTime value to timeout |
|
1150 |
* at (and return zero) |
|
1151 |
* @return next state, or INTERRUPTED |
|
1152 |
*/ |
|
1153 |
private long acquireRead(boolean interruptible, long deadline) { |
|
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1154 |
boolean wasInterrupted = false; |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1155 |
WNode node = null, p; |
15648 | 1156 |
for (int spins = -1;;) { |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1157 |
WNode h; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1158 |
if ((h = whead) == (p = wtail)) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1159 |
for (long m, s, ns;;) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1160 |
if ((m = (s = state) & ABITS) < RFULL ? |
39722 | 1161 |
STATE.compareAndSet(this, s, ns = s + RUNIT) : |
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1162 |
(m < WBIT && (ns = tryIncReaderOverflow(s)) != 0L)) { |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1163 |
if (wasInterrupted) |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1164 |
Thread.currentThread().interrupt(); |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1165 |
return ns; |
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1166 |
} |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1167 |
else if (m >= WBIT) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1168 |
if (spins > 0) { |
39722 | 1169 |
--spins; |
1170 |
Thread.onSpinWait(); |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1171 |
} |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1172 |
else { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1173 |
if (spins == 0) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1174 |
WNode nh = whead, np = wtail; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1175 |
if ((nh == h && np == p) || (h = nh) != (p = np)) |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1176 |
break; |
15648 | 1177 |
} |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1178 |
spins = SPINS; |
15648 | 1179 |
} |
1180 |
} |
|
1181 |
} |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1182 |
} |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1183 |
if (p == null) { // initialize queue |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1184 |
WNode hd = new WNode(WMODE, null); |
39722 | 1185 |
if (WHEAD.weakCompareAndSetVolatile(this, null, hd)) |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1186 |
wtail = hd; |
15648 | 1187 |
} |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1188 |
else if (node == null) |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1189 |
node = new WNode(RMODE, p); |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1190 |
else if (h == p || p.mode != RMODE) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1191 |
if (node.prev != p) |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1192 |
node.prev = p; |
39722 | 1193 |
else if (WTAIL.weakCompareAndSetVolatile(this, p, node)) { |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1194 |
p.next = node; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1195 |
break; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1196 |
} |
15648 | 1197 |
} |
39722 | 1198 |
else if (!WCOWAIT.compareAndSet(p, node.cowait = p.cowait, node)) |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1199 |
node.cowait = null; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1200 |
else { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1201 |
for (;;) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1202 |
WNode pp, c; Thread w; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1203 |
if ((h = whead) != null && (c = h.cowait) != null && |
39722 | 1204 |
WCOWAIT.compareAndSet(h, c, c.cowait) && |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1205 |
(w = c.thread) != null) // help release |
39722 | 1206 |
LockSupport.unpark(w); |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1207 |
if (h == (pp = p.prev) || h == p || pp == null) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1208 |
long m, s, ns; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1209 |
do { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1210 |
if ((m = (s = state) & ABITS) < RFULL ? |
39722 | 1211 |
STATE.compareAndSet(this, s, ns = s + RUNIT) : |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1212 |
(m < WBIT && |
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1213 |
(ns = tryIncReaderOverflow(s)) != 0L)) { |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1214 |
if (wasInterrupted) |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1215 |
Thread.currentThread().interrupt(); |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1216 |
return ns; |
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1217 |
} |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1218 |
} while (m < WBIT); |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1219 |
} |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1220 |
if (whead == h && p.prev == pp) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1221 |
long time; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1222 |
if (pp == null || h == p || p.status > 0) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1223 |
node = null; // throw away |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1224 |
break; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1225 |
} |
15648 | 1226 |
if (deadline == 0L) |
1227 |
time = 0L; |
|
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1228 |
else if ((time = deadline - System.nanoTime()) <= 0L) { |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1229 |
if (wasInterrupted) |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1230 |
Thread.currentThread().interrupt(); |
15648 | 1231 |
return cancelWaiter(node, p, false); |
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1232 |
} |
15648 | 1233 |
Thread wt = Thread.currentThread(); |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1234 |
node.thread = wt; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1235 |
if ((h != pp || (state & ABITS) == WBIT) && |
39722 | 1236 |
whead == h && p.prev == pp) { |
1237 |
if (time == 0L) |
|
1238 |
LockSupport.park(this); |
|
1239 |
else |
|
1240 |
LockSupport.parkNanos(this, time); |
|
1241 |
} |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1242 |
node.thread = null; |
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1243 |
if (Thread.interrupted()) { |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1244 |
if (interruptible) |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1245 |
return cancelWaiter(node, p, true); |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1246 |
wasInterrupted = true; |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1247 |
} |
15648 | 1248 |
} |
1249 |
} |
|
1250 |
} |
|
1251 |
} |
|
1252 |
||
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1253 |
for (int spins = -1;;) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1254 |
WNode h, np, pp; int ps; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1255 |
if ((h = whead) == p) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1256 |
if (spins < 0) |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1257 |
spins = HEAD_SPINS; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1258 |
else if (spins < MAX_HEAD_SPINS) |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1259 |
spins <<= 1; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1260 |
for (int k = spins;;) { // spin at head |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1261 |
long m, s, ns; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1262 |
if ((m = (s = state) & ABITS) < RFULL ? |
39722 | 1263 |
STATE.compareAndSet(this, s, ns = s + RUNIT) : |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1264 |
(m < WBIT && (ns = tryIncReaderOverflow(s)) != 0L)) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1265 |
WNode c; Thread w; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1266 |
whead = node; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1267 |
node.prev = null; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1268 |
while ((c = node.cowait) != null) { |
39722 | 1269 |
if (WCOWAIT.compareAndSet(node, c, c.cowait) && |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1270 |
(w = c.thread) != null) |
39722 | 1271 |
LockSupport.unpark(w); |
15648 | 1272 |
} |
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1273 |
if (wasInterrupted) |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1274 |
Thread.currentThread().interrupt(); |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1275 |
return ns; |
15648 | 1276 |
} |
39722 | 1277 |
else if (m >= WBIT && --k <= 0) |
15648 | 1278 |
break; |
39722 | 1279 |
else |
1280 |
Thread.onSpinWait(); |
|
15648 | 1281 |
} |
1282 |
} |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1283 |
else if (h != null) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1284 |
WNode c; Thread w; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1285 |
while ((c = h.cowait) != null) { |
39722 | 1286 |
if (WCOWAIT.compareAndSet(h, c, c.cowait) && |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1287 |
(w = c.thread) != null) |
39722 | 1288 |
LockSupport.unpark(w); |
15648 | 1289 |
} |
1290 |
} |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1291 |
if (whead == h) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1292 |
if ((np = node.prev) != p) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1293 |
if (np != null) |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1294 |
(p = np).next = node; // stale |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1295 |
} |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1296 |
else if ((ps = p.status) == 0) |
39722 | 1297 |
WSTATUS.compareAndSet(p, 0, WAITING); |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1298 |
else if (ps == CANCELLED) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1299 |
if ((pp = p.prev) != null) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1300 |
node.prev = pp; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1301 |
pp.next = node; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1302 |
} |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1303 |
} |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1304 |
else { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1305 |
long time; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1306 |
if (deadline == 0L) |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1307 |
time = 0L; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1308 |
else if ((time = deadline - System.nanoTime()) <= 0L) |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1309 |
return cancelWaiter(node, node, false); |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1310 |
Thread wt = Thread.currentThread(); |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1311 |
node.thread = wt; |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1312 |
if (p.status < 0 && |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1313 |
(p != h || (state & ABITS) == WBIT) && |
39722 | 1314 |
whead == h && node.prev == p) { |
1315 |
if (time == 0L) |
|
1316 |
LockSupport.park(this); |
|
1317 |
else |
|
1318 |
LockSupport.parkNanos(this, time); |
|
1319 |
} |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1320 |
node.thread = null; |
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1321 |
if (Thread.interrupted()) { |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1322 |
if (interruptible) |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1323 |
return cancelWaiter(node, node, true); |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1324 |
wasInterrupted = true; |
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1325 |
} |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1326 |
} |
15648 | 1327 |
} |
1328 |
} |
|
1329 |
} |
|
1330 |
||
1331 |
/** |
|
1332 |
* If node non-null, forces cancel status and unsplices it from |
|
1333 |
* queue if possible and wakes up any cowaiters (of the node, or |
|
1334 |
* group, as applicable), and in any case helps release current |
|
1335 |
* first waiter if lock is free. (Calling with null arguments |
|
1336 |
* serves as a conditional form of release, which is not currently |
|
1337 |
* needed but may be needed under possible future cancellation |
|
1338 |
* policies). This is a variant of cancellation methods in |
|
1339 |
* AbstractQueuedSynchronizer (see its detailed explanation in AQS |
|
1340 |
* internal documentation). |
|
1341 |
* |
|
39722 | 1342 |
* @param node if non-null, the waiter |
15648 | 1343 |
* @param group either node or the group node is cowaiting with |
1344 |
* @param interrupted if already interrupted |
|
1345 |
* @return INTERRUPTED if interrupted or Thread.interrupted, else zero |
|
1346 |
*/ |
|
1347 |
private long cancelWaiter(WNode node, WNode group, boolean interrupted) { |
|
1348 |
if (node != null && group != null) { |
|
1349 |
Thread w; |
|
1350 |
node.status = CANCELLED; |
|
1351 |
// unsplice cancelled nodes from group |
|
1352 |
for (WNode p = group, q; (q = p.cowait) != null;) { |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1353 |
if (q.status == CANCELLED) { |
39722 | 1354 |
WCOWAIT.compareAndSet(p, q, q.cowait); |
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1355 |
p = group; // restart |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1356 |
} |
15648 | 1357 |
else |
1358 |
p = q; |
|
1359 |
} |
|
1360 |
if (group == node) { |
|
19589
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1361 |
for (WNode r = group.cowait; r != null; r = r.cowait) { |
459db4bb1df0
8023234: StampedLock serializes readers on writer unlock
shade
parents:
18576
diff
changeset
|
1362 |
if ((w = r.thread) != null) |
39722 | 1363 |
LockSupport.unpark(w); // wake up uncancelled co-waiters |
15648 | 1364 |
} |
1365 |
for (WNode pred = node.prev; pred != null; ) { // unsplice |
|
1366 |
WNode succ, pp; // find valid successor |
|
1367 |
while ((succ = node.next) == null || |
|
1368 |
succ.status == CANCELLED) { |
|
1369 |
WNode q = null; // find successor the slow way |
|
1370 |
for (WNode t = wtail; t != null && t != node; t = t.prev) |
|
1371 |
if (t.status != CANCELLED) |
|
1372 |
q = t; // don't link if succ cancelled |
|
1373 |
if (succ == q || // ensure accurate successor |
|
39722 | 1374 |
WNEXT.compareAndSet(node, succ, succ = q)) { |
15648 | 1375 |
if (succ == null && node == wtail) |
39722 | 1376 |
WTAIL.compareAndSet(this, node, pred); |
15648 | 1377 |
break; |
1378 |
} |
|
1379 |
} |
|
1380 |
if (pred.next == node) // unsplice pred link |
|
39722 | 1381 |
WNEXT.compareAndSet(pred, node, succ); |
15648 | 1382 |
if (succ != null && (w = succ.thread) != null) { |
39722 | 1383 |
// wake up succ to observe new pred |
15648 | 1384 |
succ.thread = null; |
39722 | 1385 |
LockSupport.unpark(w); |
15648 | 1386 |
} |
1387 |
if (pred.status != CANCELLED || (pp = pred.prev) == null) |
|
1388 |
break; |
|
1389 |
node.prev = pp; // repeat if new pred wrong/cancelled |
|
39722 | 1390 |
WNEXT.compareAndSet(pp, pred, succ); |
15648 | 1391 |
pred = pp; |
1392 |
} |
|
1393 |
} |
|
1394 |
} |
|
1395 |
WNode h; // Possibly release first waiter |
|
1396 |
while ((h = whead) != null) { |
|
1397 |
long s; WNode q; // similar to release() but check eligibility |
|
1398 |
if ((q = h.next) == null || q.status == CANCELLED) { |
|
1399 |
for (WNode t = wtail; t != null && t != h; t = t.prev) |
|
1400 |
if (t.status <= 0) |
|
1401 |
q = t; |
|
1402 |
} |
|
1403 |
if (h == whead) { |
|
1404 |
if (q != null && h.status == 0 && |
|
1405 |
((s = state) & ABITS) != WBIT && // waiter is eligible |
|
1406 |
(s == 0L || q.mode == RMODE)) |
|
1407 |
release(h); |
|
1408 |
break; |
|
1409 |
} |
|
1410 |
} |
|
1411 |
return (interrupted || Thread.interrupted()) ? INTERRUPTED : 0L; |
|
1412 |
} |
|
1413 |
||
39722 | 1414 |
// VarHandle mechanics |
1415 |
private static final VarHandle STATE; |
|
1416 |
private static final VarHandle WHEAD; |
|
1417 |
private static final VarHandle WTAIL; |
|
1418 |
private static final VarHandle WNEXT; |
|
1419 |
private static final VarHandle WSTATUS; |
|
1420 |
private static final VarHandle WCOWAIT; |
|
15648 | 1421 |
static { |
1422 |
try { |
|
39722 | 1423 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
1424 |
STATE = l.findVarHandle(StampedLock.class, "state", long.class); |
|
1425 |
WHEAD = l.findVarHandle(StampedLock.class, "whead", WNode.class); |
|
1426 |
WTAIL = l.findVarHandle(StampedLock.class, "wtail", WNode.class); |
|
1427 |
WSTATUS = l.findVarHandle(WNode.class, "status", int.class); |
|
1428 |
WNEXT = l.findVarHandle(WNode.class, "next", WNode.class); |
|
1429 |
WCOWAIT = l.findVarHandle(WNode.class, "cowait", WNode.class); |
|
32990
299a81977f48
8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents:
25859
diff
changeset
|
1430 |
} catch (ReflectiveOperationException e) { |
15648 | 1431 |
throw new Error(e); |
1432 |
} |
|
1433 |
} |
|
1434 |
} |