2 *******************************************************************************
3 * Copyright (C) 2001-2006, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.impl;
9 // See Allan Holub's 1999 column in JavaWorld, and Doug Lea's code for RWLocks with writer preference.
13 * <p>A simple Reader/Writer lock. This assumes that there will
14 * be little writing contention. It also doesn't allow
15 * active readers to acquire and release a write lock, or
16 * deal with priority inversion issues.</p>
18 * <p>Access to the lock should be enclosed in a try/finally block
19 * in order to ensure that the lock is always released in case of
20 * exceptions:<br><pre>
23 * // use service protected by the lock
30 * <p>The lock provides utility methods getStats and clearStats
31 * to return statistics on the use of the lock.</p>
33 public class ICURWLock {
34 private Object writeLock = new Object();
35 private Object readLock = new Object();
36 private int wwc; // waiting writers
37 private int rc; // active readers, -1 if there's an active writer
38 private int wrc; // waiting readers
40 private Stats stats = new Stats(); // maybe don't init to start...
43 * Internal class used to gather statistics on the RWLock.
45 public final static class Stats {
47 * Number of times read access granted (read count).
52 * Number of times concurrent read access granted (multiple read count).
57 * Number of times blocked for read (waiting reader count).
59 public int _wrc; // wait for read
62 * Number of times write access granted (writer count).
67 * Number of times blocked for write (waiting writer count).
74 private Stats(int rc, int mrc, int wrc, int wc, int wwc) {
82 private Stats(Stats rhs) {
83 this(rhs._rc, rhs._mrc, rhs._wrc, rhs._wc, rhs._wwc);
87 * Return a string listing all the stats.
89 public String toString() {
90 return " rc: " + _rc +
99 * Reset the stats. Returns existing stats, if any.
101 public synchronized Stats resetStats() {
102 Stats result = stats;
108 * Clear the stats (stop collecting stats). Returns existing stats, if any.
110 public synchronized Stats clearStats() {
111 Stats result = stats;
117 * Return a snapshot of the current stats. This does not reset the stats.
119 public synchronized Stats getStats() {
120 return stats == null ? null : new Stats(stats);
125 private synchronized boolean gotRead() {
129 if (rc > 1) ++stats._mrc;
134 private synchronized boolean getRead() {
135 if (rc >= 0 && wwc == 0) {
142 private synchronized boolean retryRead() {
143 if (stats != null) ++stats._wrc;
144 if (rc >= 0 && wwc == 0) {
151 private synchronized boolean finishRead() {
153 return (0 == --rc && wwc > 0);
155 throw new IllegalStateException("no current reader to release");
158 private synchronized boolean gotWrite() {
166 private synchronized boolean getWrite() {
174 private synchronized boolean retryWrite() {
175 if (stats != null) ++stats._wwc;
183 private static final int NOTIFY_NONE = 0;
184 private static final int NOTIFY_WRITERS = 1;
185 private static final int NOTIFY_READERS = 2;
187 private synchronized int finishWrite() {
191 return NOTIFY_WRITERS;
192 } else if (wrc > 0) {
193 return NOTIFY_READERS;
198 throw new IllegalStateException("no current writer to release");
202 * <p>Acquire a read lock, blocking until a read lock is
203 * available. Multiple readers can concurrently hold the read
206 * <p>If there's a writer, or a waiting writer, increment the
207 * waiting reader count and block on this. Otherwise
208 * increment the active reader count and return. Caller must call
209 * releaseRead when done (for example, in a finally block).</p>
211 public void acquireRead() {
215 synchronized (readLock) {
222 catch (InterruptedException e) {
229 * <p>Release a read lock and return. An error will be thrown
230 * if a read lock is not currently held.</p>
232 * <p>If this is the last active reader, notify the oldest
233 * waiting writer. Call when finished with work
234 * controlled by acquireRead.</p>
236 public void releaseRead() {
238 synchronized (writeLock) {
245 * <p>Acquire the write lock, blocking until the write lock is
246 * available. Only one writer can acquire the write lock, and
247 * when held, no readers can acquire the read lock.</p>
249 * <p>If there are no readers and no waiting writers, mark as
250 * having an active writer and return. Otherwise, add a lock to the
251 * end of the waiting writer list, and block on it. Caller
252 * must call releaseWrite when done (for example, in a finally
255 public void acquireWrite() {
259 synchronized (writeLock) {
266 catch (InterruptedException e) {
273 * <p>Release the write lock and return. An error will be thrown
274 * if the write lock is not currently held.</p>
276 * <p>If there are waiting readers, make them all active and
277 * notify all of them. Otherwise, notify the oldest waiting
278 * writer, if any. Call when finished with work controlled by
281 public void releaseWrite() {
282 switch (finishWrite()) {
284 synchronized (writeLock) {
289 synchronized (readLock) {
290 readLock.notifyAll();