rt/emul/compact/src/main/java/java/util/concurrent/locks/ReentrantReadWriteLock.java
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
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
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/
36 package java.util.concurrent.locks;
37 import java.util.concurrent.*;
38 import java.util.concurrent.atomic.*;
42 * An implementation of {@link ReadWriteLock} supporting similar
43 * semantics to {@link ReentrantLock}.
44 * <p>This class has the following properties:
47 * <li><b>Acquisition order</b>
49 * <p> This class does not impose a reader or writer preference
50 * ordering for lock access. However, it does support an optional
51 * <em>fairness</em> policy.
54 * <dt><b><i>Non-fair mode (default)</i></b>
55 * <dd>When constructed as non-fair (the default), the order of entry
56 * to the read and write lock is unspecified, subject to reentrancy
57 * constraints. A nonfair lock that is continuously contended may
58 * indefinitely postpone one or more reader or writer threads, but
59 * will normally have higher throughput than a fair lock.
62 * <dt><b><i>Fair mode</i></b>
63 * <dd> When constructed as fair, threads contend for entry using an
64 * approximately arrival-order policy. When the currently held lock
65 * is released either the longest-waiting single writer thread will
66 * be assigned the write lock, or if there is a group of reader threads
67 * waiting longer than all waiting writer threads, that group will be
68 * assigned the read lock.
70 * <p>A thread that tries to acquire a fair read lock (non-reentrantly)
71 * will block if either the write lock is held, or there is a waiting
72 * writer thread. The thread will not acquire the read lock until
73 * after the oldest currently waiting writer thread has acquired and
74 * released the write lock. Of course, if a waiting writer abandons
75 * its wait, leaving one or more reader threads as the longest waiters
76 * in the queue with the write lock free, then those readers will be
77 * assigned the read lock.
79 * <p>A thread that tries to acquire a fair write lock (non-reentrantly)
80 * will block unless both the read lock and write lock are free (which
81 * implies there are no waiting threads). (Note that the non-blocking
82 * {@link ReadLock#tryLock()} and {@link WriteLock#tryLock()} methods
83 * do not honor this fair setting and will acquire the lock if it is
84 * possible, regardless of waiting threads.)
88 * <li><b>Reentrancy</b>
90 * <p>This lock allows both readers and writers to reacquire read or
91 * write locks in the style of a {@link ReentrantLock}. Non-reentrant
92 * readers are not allowed until all write locks held by the writing
93 * thread have been released.
95 * <p>Additionally, a writer can acquire the read lock, but not
96 * vice-versa. Among other applications, reentrancy can be useful
97 * when write locks are held during calls or callbacks to methods that
98 * perform reads under read locks. If a reader tries to acquire the
99 * write lock it will never succeed.
101 * <li><b>Lock downgrading</b>
102 * <p>Reentrancy also allows downgrading from the write lock to a read lock,
103 * by acquiring the write lock, then the read lock and then releasing the
104 * write lock. However, upgrading from a read lock to the write lock is
105 * <b>not</b> possible.
107 * <li><b>Interruption of lock acquisition</b>
108 * <p>The read lock and write lock both support interruption during lock
111 * <li><b>{@link Condition} support</b>
112 * <p>The write lock provides a {@link Condition} implementation that
113 * behaves in the same way, with respect to the write lock, as the
114 * {@link Condition} implementation provided by
115 * {@link ReentrantLock#newCondition} does for {@link ReentrantLock}.
116 * This {@link Condition} can, of course, only be used with the write lock.
118 * <p>The read lock does not support a {@link Condition} and
119 * {@code readLock().newCondition()} throws
120 * {@code UnsupportedOperationException}.
122 * <li><b>Instrumentation</b>
123 * <p>This class supports methods to determine whether locks
124 * are held or contended. These methods are designed for monitoring
125 * system state, not for synchronization control.
128 * <p>Serialization of this class behaves in the same way as built-in
129 * locks: a deserialized lock is in the unlocked state, regardless of
130 * its state when serialized.
132 * <p><b>Sample usages</b>. Here is a code sketch showing how to perform
133 * lock downgrading after updating a cache (exception handling is
134 * particularly tricky when handling multiple locks in a non-nested
140 * volatile boolean cacheValid;
141 * final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
143 * void processCachedData() {
144 * rwl.readLock().lock();
146 * // Must release read lock before acquiring write lock
147 * rwl.readLock().unlock();
148 * rwl.writeLock().lock();
150 * // Recheck state because another thread might have
151 * // acquired write lock and changed state before we did.
156 * // Downgrade by acquiring read lock before releasing write lock
157 * rwl.readLock().lock();
159 * rwl.writeLock().unlock(); // Unlock write, still hold read
166 * rwl.readLock().unlock();
171 * ReentrantReadWriteLocks can be used to improve concurrency in some
172 * uses of some kinds of Collections. This is typically worthwhile
173 * only when the collections are expected to be large, accessed by
174 * more reader threads than writer threads, and entail operations with
175 * overhead that outweighs synchronization overhead. For example, here
176 * is a class using a TreeMap that is expected to be large and
177 * concurrently accessed.
180 * class RWDictionary {
181 * private final Map<String, Data> m = new TreeMap<String, Data>();
182 * private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
183 * private final Lock r = rwl.readLock();
184 * private final Lock w = rwl.writeLock();
186 * public Data get(String key) {
188 * try { return m.get(key); }
189 * finally { r.unlock(); }
191 * public String[] allKeys() {
193 * try { return m.keySet().toArray(); }
194 * finally { r.unlock(); }
196 * public Data put(String key, Data value) {
198 * try { return m.put(key, value); }
199 * finally { w.unlock(); }
201 * public void clear() {
204 * finally { w.unlock(); }
208 * <h3>Implementation Notes</h3>
210 * <p>This lock supports a maximum of 65535 recursive write locks
211 * and 65535 read locks. Attempts to exceed these limits result in
212 * {@link Error} throws from locking methods.
218 public class ReentrantReadWriteLock
219 implements ReadWriteLock, java.io.Serializable {
220 private static final long serialVersionUID = -6992448646407690164L;
221 /** Inner class providing readlock */
222 private final ReentrantReadWriteLock.ReadLock readerLock;
223 /** Inner class providing writelock */
224 private final ReentrantReadWriteLock.WriteLock writerLock;
225 /** Performs all synchronization mechanics */
229 * Creates a new {@code ReentrantReadWriteLock} with
230 * default (nonfair) ordering properties.
232 public ReentrantReadWriteLock() {
237 * Creates a new {@code ReentrantReadWriteLock} with
238 * the given fairness policy.
240 * @param fair {@code true} if this lock should use a fair ordering policy
242 public ReentrantReadWriteLock(boolean fair) {
243 sync = fair ? new FairSync() : new NonfairSync();
244 readerLock = new ReadLock(this);
245 writerLock = new WriteLock(this);
248 public ReentrantReadWriteLock.WriteLock writeLock() { return writerLock; }
249 public ReentrantReadWriteLock.ReadLock readLock() { return readerLock; }
252 * Synchronization implementation for ReentrantReadWriteLock.
253 * Subclassed into fair and nonfair versions.
255 abstract static class Sync extends AbstractQueuedSynchronizer {
256 private static final long serialVersionUID = 6317671515068378041L;
259 * Read vs write count extraction constants and functions.
260 * Lock state is logically divided into two unsigned shorts:
261 * The lower one representing the exclusive (writer) lock hold count,
262 * and the upper the shared (reader) hold count.
265 static final int SHARED_SHIFT = 16;
266 static final int SHARED_UNIT = (1 << SHARED_SHIFT);
267 static final int MAX_COUNT = (1 << SHARED_SHIFT) - 1;
268 static final int EXCLUSIVE_MASK = (1 << SHARED_SHIFT) - 1;
270 /** Returns the number of shared holds represented in count */
271 static int sharedCount(int c) { return c >>> SHARED_SHIFT; }
272 /** Returns the number of exclusive holds represented in count */
273 static int exclusiveCount(int c) { return c & EXCLUSIVE_MASK; }
276 * A counter for per-thread read hold counts.
277 * Maintained as a ThreadLocal; cached in cachedHoldCounter
279 static final class HoldCounter {
281 // Use id, not reference, to avoid garbage retention
282 final long tid = Thread.currentThread().getId();
286 * ThreadLocal subclass. Easiest to explicitly define for sake
287 * of deserialization mechanics.
289 static final class ThreadLocalHoldCounter
290 extends ThreadLocal<HoldCounter> {
291 public HoldCounter initialValue() {
292 return new HoldCounter();
297 * The number of reentrant read locks held by current thread.
298 * Initialized only in constructor and readObject.
299 * Removed whenever a thread's read hold count drops to 0.
301 private transient ThreadLocalHoldCounter readHolds;
304 * The hold count of the last thread to successfully acquire
305 * readLock. This saves ThreadLocal lookup in the common case
306 * where the next thread to release is the last one to
307 * acquire. This is non-volatile since it is just used
308 * as a heuristic, and would be great for threads to cache.
310 * <p>Can outlive the Thread for which it is caching the read
311 * hold count, but avoids garbage retention by not retaining a
312 * reference to the Thread.
314 * <p>Accessed via a benign data race; relies on the memory
315 * model's final field and out-of-thin-air guarantees.
317 private transient HoldCounter cachedHoldCounter;
320 * firstReader is the first thread to have acquired the read lock.
321 * firstReaderHoldCount is firstReader's hold count.
323 * <p>More precisely, firstReader is the unique thread that last
324 * changed the shared count from 0 to 1, and has not released the
325 * read lock since then; null if there is no such thread.
327 * <p>Cannot cause garbage retention unless the thread terminated
328 * without relinquishing its read locks, since tryReleaseShared
331 * <p>Accessed via a benign data race; relies on the memory
332 * model's out-of-thin-air guarantees for references.
334 * <p>This allows tracking of read holds for uncontended read
335 * locks to be very cheap.
337 private transient Thread firstReader = null;
338 private transient int firstReaderHoldCount;
341 readHolds = new ThreadLocalHoldCounter();
342 setState(getState()); // ensures visibility of readHolds
346 * Acquires and releases use the same code for fair and
347 * nonfair locks, but differ in whether/how they allow barging
348 * when queues are non-empty.
352 * Returns true if the current thread, when trying to acquire
353 * the read lock, and otherwise eligible to do so, should block
354 * because of policy for overtaking other waiting threads.
356 abstract boolean readerShouldBlock();
359 * Returns true if the current thread, when trying to acquire
360 * the write lock, and otherwise eligible to do so, should block
361 * because of policy for overtaking other waiting threads.
363 abstract boolean writerShouldBlock();
366 * Note that tryRelease and tryAcquire can be called by
367 * Conditions. So it is possible that their arguments contain
368 * both read and write holds that are all released during a
369 * condition wait and re-established in tryAcquire.
372 protected final boolean tryRelease(int releases) {
373 if (!isHeldExclusively())
374 throw new IllegalMonitorStateException();
375 int nextc = getState() - releases;
376 boolean free = exclusiveCount(nextc) == 0;
378 setExclusiveOwnerThread(null);
383 protected final boolean tryAcquire(int acquires) {
386 * 1. If read count nonzero or write count nonzero
387 * and owner is a different thread, fail.
388 * 2. If count would saturate, fail. (This can only
389 * happen if count is already nonzero.)
390 * 3. Otherwise, this thread is eligible for lock if
391 * it is either a reentrant acquire or
392 * queue policy allows it. If so, update state
395 Thread current = Thread.currentThread();
397 int w = exclusiveCount(c);
399 // (Note: if c != 0 and w == 0 then shared count != 0)
400 if (w == 0 || current != getExclusiveOwnerThread())
402 if (w + exclusiveCount(acquires) > MAX_COUNT)
403 throw new Error("Maximum lock count exceeded");
405 setState(c + acquires);
408 if (writerShouldBlock() ||
409 !compareAndSetState(c, c + acquires))
411 setExclusiveOwnerThread(current);
415 protected final boolean tryReleaseShared(int unused) {
416 Thread current = Thread.currentThread();
417 if (firstReader == current) {
418 // assert firstReaderHoldCount > 0;
419 if (firstReaderHoldCount == 1)
422 firstReaderHoldCount--;
424 HoldCounter rh = cachedHoldCounter;
425 if (rh == null || rh.tid != current.getId())
426 rh = readHolds.get();
427 int count = rh.count;
431 throw unmatchedUnlockException();
437 int nextc = c - SHARED_UNIT;
438 if (compareAndSetState(c, nextc))
439 // Releasing the read lock has no effect on readers,
440 // but it may allow waiting writers to proceed if
441 // both read and write locks are now free.
446 private IllegalMonitorStateException unmatchedUnlockException() {
447 return new IllegalMonitorStateException(
448 "attempt to unlock read lock, not locked by current thread");
451 protected final int tryAcquireShared(int unused) {
454 * 1. If write lock held by another thread, fail.
455 * 2. Otherwise, this thread is eligible for
456 * lock wrt state, so ask if it should block
457 * because of queue policy. If not, try
458 * to grant by CASing state and updating count.
459 * Note that step does not check for reentrant
460 * acquires, which is postponed to full version
461 * to avoid having to check hold count in
462 * the more typical non-reentrant case.
463 * 3. If step 2 fails either because thread
464 * apparently not eligible or CAS fails or count
465 * saturated, chain to version with full retry loop.
467 Thread current = Thread.currentThread();
469 if (exclusiveCount(c) != 0 &&
470 getExclusiveOwnerThread() != current)
472 int r = sharedCount(c);
473 if (!readerShouldBlock() &&
475 compareAndSetState(c, c + SHARED_UNIT)) {
477 firstReader = current;
478 firstReaderHoldCount = 1;
479 } else if (firstReader == current) {
480 firstReaderHoldCount++;
482 HoldCounter rh = cachedHoldCounter;
483 if (rh == null || rh.tid != current.getId())
484 cachedHoldCounter = rh = readHolds.get();
485 else if (rh.count == 0)
491 return fullTryAcquireShared(current);
495 * Full version of acquire for reads, that handles CAS misses
496 * and reentrant reads not dealt with in tryAcquireShared.
498 final int fullTryAcquireShared(Thread current) {
500 * This code is in part redundant with that in
501 * tryAcquireShared but is simpler overall by not
502 * complicating tryAcquireShared with interactions between
503 * retries and lazily reading hold counts.
505 HoldCounter rh = null;
508 if (exclusiveCount(c) != 0) {
509 if (getExclusiveOwnerThread() != current)
511 // else we hold the exclusive lock; blocking here
512 // would cause deadlock.
513 } else if (readerShouldBlock()) {
514 // Make sure we're not acquiring read lock reentrantly
515 if (firstReader == current) {
516 // assert firstReaderHoldCount > 0;
519 rh = cachedHoldCounter;
520 if (rh == null || rh.tid != current.getId()) {
521 rh = readHolds.get();
530 if (sharedCount(c) == MAX_COUNT)
531 throw new Error("Maximum lock count exceeded");
532 if (compareAndSetState(c, c + SHARED_UNIT)) {
533 if (sharedCount(c) == 0) {
534 firstReader = current;
535 firstReaderHoldCount = 1;
536 } else if (firstReader == current) {
537 firstReaderHoldCount++;
540 rh = cachedHoldCounter;
541 if (rh == null || rh.tid != current.getId())
542 rh = readHolds.get();
543 else if (rh.count == 0)
546 cachedHoldCounter = rh; // cache for release
554 * Performs tryLock for write, enabling barging in both modes.
555 * This is identical in effect to tryAcquire except for lack
556 * of calls to writerShouldBlock.
558 final boolean tryWriteLock() {
559 Thread current = Thread.currentThread();
562 int w = exclusiveCount(c);
563 if (w == 0 || current != getExclusiveOwnerThread())
566 throw new Error("Maximum lock count exceeded");
568 if (!compareAndSetState(c, c + 1))
570 setExclusiveOwnerThread(current);
575 * Performs tryLock for read, enabling barging in both modes.
576 * This is identical in effect to tryAcquireShared except for
577 * lack of calls to readerShouldBlock.
579 final boolean tryReadLock() {
580 Thread current = Thread.currentThread();
583 if (exclusiveCount(c) != 0 &&
584 getExclusiveOwnerThread() != current)
586 int r = sharedCount(c);
588 throw new Error("Maximum lock count exceeded");
589 if (compareAndSetState(c, c + SHARED_UNIT)) {
591 firstReader = current;
592 firstReaderHoldCount = 1;
593 } else if (firstReader == current) {
594 firstReaderHoldCount++;
596 HoldCounter rh = cachedHoldCounter;
597 if (rh == null || rh.tid != current.getId())
598 cachedHoldCounter = rh = readHolds.get();
599 else if (rh.count == 0)
608 protected final boolean isHeldExclusively() {
609 // While we must in general read state before owner,
610 // we don't need to do so to check if current thread is owner
611 return getExclusiveOwnerThread() == Thread.currentThread();
614 // Methods relayed to outer class
616 final ConditionObject newCondition() {
617 return new ConditionObject();
620 final Thread getOwner() {
621 // Must read state before owner to ensure memory consistency
622 return ((exclusiveCount(getState()) == 0) ?
624 getExclusiveOwnerThread());
627 final int getReadLockCount() {
628 return sharedCount(getState());
631 final boolean isWriteLocked() {
632 return exclusiveCount(getState()) != 0;
635 final int getWriteHoldCount() {
636 return isHeldExclusively() ? exclusiveCount(getState()) : 0;
639 final int getReadHoldCount() {
640 if (getReadLockCount() == 0)
643 Thread current = Thread.currentThread();
644 if (firstReader == current)
645 return firstReaderHoldCount;
647 HoldCounter rh = cachedHoldCounter;
648 if (rh != null && rh.tid == current.getId())
651 int count = readHolds.get().count;
652 if (count == 0) readHolds.remove();
657 * Reconstitute this lock instance from a stream
658 * @param s the stream
660 private void readObject(java.io.ObjectInputStream s)
661 throws java.io.IOException, ClassNotFoundException {
662 s.defaultReadObject();
663 readHolds = new ThreadLocalHoldCounter();
664 setState(0); // reset to unlocked state
667 final int getCount() { return getState(); }
671 * Nonfair version of Sync
673 static final class NonfairSync extends Sync {
674 private static final long serialVersionUID = -8159625535654395037L;
675 final boolean writerShouldBlock() {
676 return false; // writers can always barge
678 final boolean readerShouldBlock() {
679 /* As a heuristic to avoid indefinite writer starvation,
680 * block if the thread that momentarily appears to be head
681 * of queue, if one exists, is a waiting writer. This is
682 * only a probabilistic effect since a new reader will not
683 * block if there is a waiting writer behind other enabled
684 * readers that have not yet drained from the queue.
686 return apparentlyFirstQueuedIsExclusive();
691 * Fair version of Sync
693 static final class FairSync extends Sync {
694 private static final long serialVersionUID = -2274990926593161451L;
695 final boolean writerShouldBlock() {
696 return hasQueuedPredecessors();
698 final boolean readerShouldBlock() {
699 return hasQueuedPredecessors();
704 * The lock returned by method {@link ReentrantReadWriteLock#readLock}.
706 public static class ReadLock implements Lock, java.io.Serializable {
707 private static final long serialVersionUID = -5992448646407690164L;
708 private final Sync sync;
711 * Constructor for use by subclasses
713 * @param lock the outer lock object
714 * @throws NullPointerException if the lock is null
716 protected ReadLock(ReentrantReadWriteLock lock) {
721 * Acquires the read lock.
723 * <p>Acquires the read lock if the write lock is not held by
724 * another thread and returns immediately.
726 * <p>If the write lock is held by another thread then
727 * the current thread becomes disabled for thread scheduling
728 * purposes and lies dormant until the read lock has been acquired.
731 sync.acquireShared(1);
735 * Acquires the read lock unless the current thread is
736 * {@linkplain Thread#interrupt interrupted}.
738 * <p>Acquires the read lock if the write lock is not held
739 * by another thread and returns immediately.
741 * <p>If the write lock is held by another thread then the
742 * current thread becomes disabled for thread scheduling
743 * purposes and lies dormant until one of two things happens:
747 * <li>The read lock is acquired by the current thread; or
749 * <li>Some other thread {@linkplain Thread#interrupt interrupts}
750 * the current thread.
754 * <p>If the current thread:
758 * <li>has its interrupted status set on entry to this method; or
760 * <li>is {@linkplain Thread#interrupt interrupted} while
761 * acquiring the read lock,
765 * then {@link InterruptedException} is thrown and the current
766 * thread's interrupted status is cleared.
768 * <p>In this implementation, as this method is an explicit
769 * interruption point, preference is given to responding to
770 * the interrupt over normal or reentrant acquisition of the
773 * @throws InterruptedException if the current thread is interrupted
775 public void lockInterruptibly() throws InterruptedException {
776 sync.acquireSharedInterruptibly(1);
780 * Acquires the read lock only if the write lock is not held by
781 * another thread at the time of invocation.
783 * <p>Acquires the read lock if the write lock is not held by
784 * another thread and returns immediately with the value
785 * {@code true}. Even when this lock has been set to use a
786 * fair ordering policy, a call to {@code tryLock()}
787 * <em>will</em> immediately acquire the read lock if it is
788 * available, whether or not other threads are currently
789 * waiting for the read lock. This "barging" behavior
790 * can be useful in certain circumstances, even though it
791 * breaks fairness. If you want to honor the fairness setting
792 * for this lock, then use {@link #tryLock(long, TimeUnit)
793 * tryLock(0, TimeUnit.SECONDS) } which is almost equivalent
794 * (it also detects interruption).
796 * <p>If the write lock is held by another thread then
797 * this method will return immediately with the value
800 * @return {@code true} if the read lock was acquired
802 public boolean tryLock() {
803 return sync.tryReadLock();
807 * Acquires the read lock if the write lock is not held by
808 * another thread within the given waiting time and the
809 * current thread has not been {@linkplain Thread#interrupt
812 * <p>Acquires the read lock if the write lock is not held by
813 * another thread and returns immediately with the value
814 * {@code true}. If this lock has been set to use a fair
815 * ordering policy then an available lock <em>will not</em> be
816 * acquired if any other threads are waiting for the
817 * lock. This is in contrast to the {@link #tryLock()}
818 * method. If you want a timed {@code tryLock} that does
819 * permit barging on a fair lock then combine the timed and
820 * un-timed forms together:
822 * <pre>if (lock.tryLock() || lock.tryLock(timeout, unit) ) { ... }
825 * <p>If the write lock is held by another thread then the
826 * current thread becomes disabled for thread scheduling
827 * purposes and lies dormant until one of three things happens:
831 * <li>The read lock is acquired by the current thread; or
833 * <li>Some other thread {@linkplain Thread#interrupt interrupts}
834 * the current thread; or
836 * <li>The specified waiting time elapses.
840 * <p>If the read lock is acquired then the value {@code true} is
843 * <p>If the current thread:
847 * <li>has its interrupted status set on entry to this method; or
849 * <li>is {@linkplain Thread#interrupt interrupted} while
850 * acquiring the read lock,
852 * </ul> then {@link InterruptedException} is thrown and the
853 * current thread's interrupted status is cleared.
855 * <p>If the specified waiting time elapses then the value
856 * {@code false} is returned. If the time is less than or
857 * equal to zero, the method will not wait at all.
859 * <p>In this implementation, as this method is an explicit
860 * interruption point, preference is given to responding to
861 * the interrupt over normal or reentrant acquisition of the
862 * lock, and over reporting the elapse of the waiting time.
864 * @param timeout the time to wait for the read lock
865 * @param unit the time unit of the timeout argument
866 * @return {@code true} if the read lock was acquired
867 * @throws InterruptedException if the current thread is interrupted
868 * @throws NullPointerException if the time unit is null
871 public boolean tryLock(long timeout, TimeUnit unit)
872 throws InterruptedException {
873 return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
877 * Attempts to release this lock.
879 * <p> If the number of readers is now zero then the lock
880 * is made available for write lock attempts.
882 public void unlock() {
883 sync.releaseShared(1);
887 * Throws {@code UnsupportedOperationException} because
888 * {@code ReadLocks} do not support conditions.
890 * @throws UnsupportedOperationException always
892 public Condition newCondition() {
893 throw new UnsupportedOperationException();
897 * Returns a string identifying this lock, as well as its lock state.
898 * The state, in brackets, includes the String {@code "Read locks ="}
899 * followed by the number of held read locks.
901 * @return a string identifying this lock, as well as its lock state
903 public String toString() {
904 int r = sync.getReadLockCount();
905 return super.toString() +
906 "[Read locks = " + r + "]";
911 * The lock returned by method {@link ReentrantReadWriteLock#writeLock}.
913 public static class WriteLock implements Lock, java.io.Serializable {
914 private static final long serialVersionUID = -4992448646407690164L;
915 private final Sync sync;
918 * Constructor for use by subclasses
920 * @param lock the outer lock object
921 * @throws NullPointerException if the lock is null
923 protected WriteLock(ReentrantReadWriteLock lock) {
928 * Acquires the write lock.
930 * <p>Acquires the write lock if neither the read nor write lock
931 * are held by another thread
932 * and returns immediately, setting the write lock hold count to
935 * <p>If the current thread already holds the write lock then the
936 * hold count is incremented by one and the method returns
939 * <p>If the lock is held by another thread then the current
940 * thread becomes disabled for thread scheduling purposes and
941 * lies dormant until the write lock has been acquired, at which
942 * time the write lock hold count is set to one.
949 * Acquires the write lock unless the current thread is
950 * {@linkplain Thread#interrupt interrupted}.
952 * <p>Acquires the write lock if neither the read nor write lock
953 * are held by another thread
954 * and returns immediately, setting the write lock hold count to
957 * <p>If the current thread already holds this lock then the
958 * hold count is incremented by one and the method returns
961 * <p>If the lock is held by another thread then the current
962 * thread becomes disabled for thread scheduling purposes and
963 * lies dormant until one of two things happens:
967 * <li>The write lock is acquired by the current thread; or
969 * <li>Some other thread {@linkplain Thread#interrupt interrupts}
970 * the current thread.
974 * <p>If the write lock is acquired by the current thread then the
975 * lock hold count is set to one.
977 * <p>If the current thread:
981 * <li>has its interrupted status set on entry to this method;
984 * <li>is {@linkplain Thread#interrupt interrupted} while
985 * acquiring the write lock,
989 * then {@link InterruptedException} is thrown and the current
990 * thread's interrupted status is cleared.
992 * <p>In this implementation, as this method is an explicit
993 * interruption point, preference is given to responding to
994 * the interrupt over normal or reentrant acquisition of the
997 * @throws InterruptedException if the current thread is interrupted
999 public void lockInterruptibly() throws InterruptedException {
1000 sync.acquireInterruptibly(1);
1004 * Acquires the write lock only if it is not held by another thread
1005 * at the time of invocation.
1007 * <p>Acquires the write lock if neither the read nor write lock
1008 * are held by another thread
1009 * and returns immediately with the value {@code true},
1010 * setting the write lock hold count to one. Even when this lock has
1011 * been set to use a fair ordering policy, a call to
1012 * {@code tryLock()} <em>will</em> immediately acquire the
1013 * lock if it is available, whether or not other threads are
1014 * currently waiting for the write lock. This "barging"
1015 * behavior can be useful in certain circumstances, even
1016 * though it breaks fairness. If you want to honor the
1017 * fairness setting for this lock, then use {@link
1018 * #tryLock(long, TimeUnit) tryLock(0, TimeUnit.SECONDS) }
1019 * which is almost equivalent (it also detects interruption).
1021 * <p> If the current thread already holds this lock then the
1022 * hold count is incremented by one and the method returns
1025 * <p>If the lock is held by another thread then this method
1026 * will return immediately with the value {@code false}.
1028 * @return {@code true} if the lock was free and was acquired
1029 * by the current thread, or the write lock was already held
1030 * by the current thread; and {@code false} otherwise.
1032 public boolean tryLock( ) {
1033 return sync.tryWriteLock();
1037 * Acquires the write lock if it is not held by another thread
1038 * within the given waiting time and the current thread has
1039 * not been {@linkplain Thread#interrupt interrupted}.
1041 * <p>Acquires the write lock if neither the read nor write lock
1042 * are held by another thread
1043 * and returns immediately with the value {@code true},
1044 * setting the write lock hold count to one. If this lock has been
1045 * set to use a fair ordering policy then an available lock
1046 * <em>will not</em> be acquired if any other threads are
1047 * waiting for the write lock. This is in contrast to the {@link
1048 * #tryLock()} method. If you want a timed {@code tryLock}
1049 * that does permit barging on a fair lock then combine the
1050 * timed and un-timed forms together:
1052 * <pre>if (lock.tryLock() || lock.tryLock(timeout, unit) ) { ... }
1055 * <p>If the current thread already holds this lock then the
1056 * hold count is incremented by one and the method returns
1059 * <p>If the lock is held by another thread then the current
1060 * thread becomes disabled for thread scheduling purposes and
1061 * lies dormant until one of three things happens:
1065 * <li>The write lock is acquired by the current thread; or
1067 * <li>Some other thread {@linkplain Thread#interrupt interrupts}
1068 * the current thread; or
1070 * <li>The specified waiting time elapses
1074 * <p>If the write lock is acquired then the value {@code true} is
1075 * returned and the write lock hold count is set to one.
1077 * <p>If the current thread:
1081 * <li>has its interrupted status set on entry to this method;
1084 * <li>is {@linkplain Thread#interrupt interrupted} while
1085 * acquiring the write lock,
1089 * then {@link InterruptedException} is thrown and the current
1090 * thread's interrupted status is cleared.
1092 * <p>If the specified waiting time elapses then the value
1093 * {@code false} is returned. If the time is less than or
1094 * equal to zero, the method will not wait at all.
1096 * <p>In this implementation, as this method is an explicit
1097 * interruption point, preference is given to responding to
1098 * the interrupt over normal or reentrant acquisition of the
1099 * lock, and over reporting the elapse of the waiting time.
1101 * @param timeout the time to wait for the write lock
1102 * @param unit the time unit of the timeout argument
1104 * @return {@code true} if the lock was free and was acquired
1105 * by the current thread, or the write lock was already held by the
1106 * current thread; and {@code false} if the waiting time
1107 * elapsed before the lock could be acquired.
1109 * @throws InterruptedException if the current thread is interrupted
1110 * @throws NullPointerException if the time unit is null
1113 public boolean tryLock(long timeout, TimeUnit unit)
1114 throws InterruptedException {
1115 return sync.tryAcquireNanos(1, unit.toNanos(timeout));
1119 * Attempts to release this lock.
1121 * <p>If the current thread is the holder of this lock then
1122 * the hold count is decremented. If the hold count is now
1123 * zero then the lock is released. If the current thread is
1124 * not the holder of this lock then {@link
1125 * IllegalMonitorStateException} is thrown.
1127 * @throws IllegalMonitorStateException if the current thread does not
1130 public void unlock() {
1135 * Returns a {@link Condition} instance for use with this
1136 * {@link Lock} instance.
1137 * <p>The returned {@link Condition} instance supports the same
1138 * usages as do the {@link Object} monitor methods ({@link
1139 * Object#wait() wait}, {@link Object#notify notify}, and {@link
1140 * Object#notifyAll notifyAll}) when used with the built-in
1145 * <li>If this write lock is not held when any {@link
1146 * Condition} method is called then an {@link
1147 * IllegalMonitorStateException} is thrown. (Read locks are
1148 * held independently of write locks, so are not checked or
1149 * affected. However it is essentially always an error to
1150 * invoke a condition waiting method when the current thread
1151 * has also acquired read locks, since other threads that
1152 * could unblock it will not be able to acquire the write
1155 * <li>When the condition {@linkplain Condition#await() waiting}
1156 * methods are called the write lock is released and, before
1157 * they return, the write lock is reacquired and the lock hold
1158 * count restored to what it was when the method was called.
1160 * <li>If a thread is {@linkplain Thread#interrupt interrupted} while
1161 * waiting then the wait will terminate, an {@link
1162 * InterruptedException} will be thrown, and the thread's
1163 * interrupted status will be cleared.
1165 * <li> Waiting threads are signalled in FIFO order.
1167 * <li>The ordering of lock reacquisition for threads returning
1168 * from waiting methods is the same as for threads initially
1169 * acquiring the lock, which is in the default case not specified,
1170 * but for <em>fair</em> locks favors those threads that have been
1171 * waiting the longest.
1175 * @return the Condition object
1177 public Condition newCondition() {
1178 return sync.newCondition();
1182 * Returns a string identifying this lock, as well as its lock
1183 * state. The state, in brackets includes either the String
1184 * {@code "Unlocked"} or the String {@code "Locked by"}
1185 * followed by the {@linkplain Thread#getName name} of the owning thread.
1187 * @return a string identifying this lock, as well as its lock state
1189 public String toString() {
1190 Thread o = sync.getOwner();
1191 return super.toString() + ((o == null) ?
1193 "[Locked by thread " + o.getName() + "]");
1197 * Queries if this write lock is held by the current thread.
1198 * Identical in effect to {@link
1199 * ReentrantReadWriteLock#isWriteLockedByCurrentThread}.
1201 * @return {@code true} if the current thread holds this lock and
1202 * {@code false} otherwise
1205 public boolean isHeldByCurrentThread() {
1206 return sync.isHeldExclusively();
1210 * Queries the number of holds on this write lock by the current
1211 * thread. A thread has a hold on a lock for each lock action
1212 * that is not matched by an unlock action. Identical in effect
1213 * to {@link ReentrantReadWriteLock#getWriteHoldCount}.
1215 * @return the number of holds on this lock by the current thread,
1216 * or zero if this lock is not held by the current thread
1219 public int getHoldCount() {
1220 return sync.getWriteHoldCount();
1224 // Instrumentation and status
1227 * Returns {@code true} if this lock has fairness set true.
1229 * @return {@code true} if this lock has fairness set true
1231 public final boolean isFair() {
1232 return sync instanceof FairSync;
1236 * Returns the thread that currently owns the write lock, or
1237 * {@code null} if not owned. When this method is called by a
1238 * thread that is not the owner, the return value reflects a
1239 * best-effort approximation of current lock status. For example,
1240 * the owner may be momentarily {@code null} even if there are
1241 * threads trying to acquire the lock but have not yet done so.
1242 * This method is designed to facilitate construction of
1243 * subclasses that provide more extensive lock monitoring
1246 * @return the owner, or {@code null} if not owned
1248 protected Thread getOwner() {
1249 return sync.getOwner();
1253 * Queries the number of read locks held for this lock. This
1254 * method is designed for use in monitoring system state, not for
1255 * synchronization control.
1256 * @return the number of read locks held.
1258 public int getReadLockCount() {
1259 return sync.getReadLockCount();
1263 * Queries if the write lock is held by any thread. This method is
1264 * designed for use in monitoring system state, not for
1265 * synchronization control.
1267 * @return {@code true} if any thread holds the write lock and
1268 * {@code false} otherwise
1270 public boolean isWriteLocked() {
1271 return sync.isWriteLocked();
1275 * Queries if the write lock is held by the current thread.
1277 * @return {@code true} if the current thread holds the write lock and
1278 * {@code false} otherwise
1280 public boolean isWriteLockedByCurrentThread() {
1281 return sync.isHeldExclusively();
1285 * Queries the number of reentrant write holds on this lock by the
1286 * current thread. A writer thread has a hold on a lock for
1287 * each lock action that is not matched by an unlock action.
1289 * @return the number of holds on the write lock by the current thread,
1290 * or zero if the write lock is not held by the current thread
1292 public int getWriteHoldCount() {
1293 return sync.getWriteHoldCount();
1297 * Queries the number of reentrant read holds on this lock by the
1298 * current thread. A reader thread has a hold on a lock for
1299 * each lock action that is not matched by an unlock action.
1301 * @return the number of holds on the read lock by the current thread,
1302 * or zero if the read lock is not held by the current thread
1305 public int getReadHoldCount() {
1306 return sync.getReadHoldCount();
1310 * Returns a collection containing threads that may be waiting to
1311 * acquire the write lock. Because the actual set of threads may
1312 * change dynamically while constructing this result, the returned
1313 * collection is only a best-effort estimate. The elements of the
1314 * returned collection are in no particular order. This method is
1315 * designed to facilitate construction of subclasses that provide
1316 * more extensive lock monitoring facilities.
1318 * @return the collection of threads
1320 protected Collection<Thread> getQueuedWriterThreads() {
1321 return sync.getExclusiveQueuedThreads();
1325 * Returns a collection containing threads that may be waiting to
1326 * acquire the read lock. Because the actual set of threads may
1327 * change dynamically while constructing this result, the returned
1328 * collection is only a best-effort estimate. The elements of the
1329 * returned collection are in no particular order. This method is
1330 * designed to facilitate construction of subclasses that provide
1331 * more extensive lock monitoring facilities.
1333 * @return the collection of threads
1335 protected Collection<Thread> getQueuedReaderThreads() {
1336 return sync.getSharedQueuedThreads();
1340 * Queries whether any threads are waiting to acquire the read or
1341 * write lock. Note that because cancellations may occur at any
1342 * time, a {@code true} return does not guarantee that any other
1343 * thread will ever acquire a lock. This method is designed
1344 * primarily for use in monitoring of the system state.
1346 * @return {@code true} if there may be other threads waiting to
1349 public final boolean hasQueuedThreads() {
1350 return sync.hasQueuedThreads();
1354 * Queries whether the given thread is waiting to acquire either
1355 * the read or write lock. Note that because cancellations may
1356 * occur at any time, a {@code true} return does not guarantee
1357 * that this thread will ever acquire a lock. This method is
1358 * designed primarily for use in monitoring of the system state.
1360 * @param thread the thread
1361 * @return {@code true} if the given thread is queued waiting for this lock
1362 * @throws NullPointerException if the thread is null
1364 public final boolean hasQueuedThread(Thread thread) {
1365 return sync.isQueued(thread);
1369 * Returns an estimate of the number of threads waiting to acquire
1370 * either the read or write lock. The value is only an estimate
1371 * because the number of threads may change dynamically while this
1372 * method traverses internal data structures. This method is
1373 * designed for use in monitoring of the system state, not for
1374 * synchronization control.
1376 * @return the estimated number of threads waiting for this lock
1378 public final int getQueueLength() {
1379 return sync.getQueueLength();
1383 * Returns a collection containing threads that may be waiting to
1384 * acquire either the read or write lock. Because the actual set
1385 * of threads may change dynamically while constructing this
1386 * result, the returned collection is only a best-effort estimate.
1387 * The elements of the returned collection are in no particular
1388 * order. This method is designed to facilitate construction of
1389 * subclasses that provide more extensive monitoring facilities.
1391 * @return the collection of threads
1393 protected Collection<Thread> getQueuedThreads() {
1394 return sync.getQueuedThreads();
1398 * Queries whether any threads are waiting on the given condition
1399 * associated with the write lock. Note that because timeouts and
1400 * interrupts may occur at any time, a {@code true} return does
1401 * not guarantee that a future {@code signal} will awaken any
1402 * threads. This method is designed primarily for use in
1403 * monitoring of the system state.
1405 * @param condition the condition
1406 * @return {@code true} if there are any waiting threads
1407 * @throws IllegalMonitorStateException if this lock is not held
1408 * @throws IllegalArgumentException if the given condition is
1409 * not associated with this lock
1410 * @throws NullPointerException if the condition is null
1412 public boolean hasWaiters(Condition condition) {
1413 if (condition == null)
1414 throw new NullPointerException();
1415 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
1416 throw new IllegalArgumentException("not owner");
1417 return sync.hasWaiters((AbstractQueuedSynchronizer.ConditionObject)condition);
1421 * Returns an estimate of the number of threads waiting on the
1422 * given condition associated with the write lock. Note that because
1423 * timeouts and interrupts may occur at any time, the estimate
1424 * serves only as an upper bound on the actual number of waiters.
1425 * This method is designed for use in monitoring of the system
1426 * state, not for synchronization control.
1428 * @param condition the condition
1429 * @return the estimated number of waiting threads
1430 * @throws IllegalMonitorStateException if this lock is not held
1431 * @throws IllegalArgumentException if the given condition is
1432 * not associated with this lock
1433 * @throws NullPointerException if the condition is null
1435 public int getWaitQueueLength(Condition condition) {
1436 if (condition == null)
1437 throw new NullPointerException();
1438 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
1439 throw new IllegalArgumentException("not owner");
1440 return sync.getWaitQueueLength((AbstractQueuedSynchronizer.ConditionObject)condition);
1444 * Returns a collection containing those threads that may be
1445 * waiting on the given condition associated with the write lock.
1446 * Because the actual set of threads may change dynamically while
1447 * constructing this result, the returned collection is only a
1448 * best-effort estimate. The elements of the returned collection
1449 * are in no particular order. This method is designed to
1450 * facilitate construction of subclasses that provide more
1451 * extensive condition monitoring facilities.
1453 * @param condition the condition
1454 * @return the collection of threads
1455 * @throws IllegalMonitorStateException if this lock is not held
1456 * @throws IllegalArgumentException if the given condition is
1457 * not associated with this lock
1458 * @throws NullPointerException if the condition is null
1460 protected Collection<Thread> getWaitingThreads(Condition condition) {
1461 if (condition == null)
1462 throw new NullPointerException();
1463 if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
1464 throw new IllegalArgumentException("not owner");
1465 return sync.getWaitingThreads((AbstractQueuedSynchronizer.ConditionObject)condition);
1469 * Returns a string identifying this lock, as well as its lock state.
1470 * The state, in brackets, includes the String {@code "Write locks ="}
1471 * followed by the number of reentrantly held write locks, and the
1472 * String {@code "Read locks ="} followed by the number of held
1475 * @return a string identifying this lock, as well as its lock state
1477 public String toString() {
1478 int c = sync.getCount();
1479 int w = Sync.exclusiveCount(c);
1480 int r = Sync.sharedCount(c);
1482 return super.toString() +
1483 "[Write locks = " + w + ", Read locks = " + r + "]";