KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > ReadWriteLock


1 /*
2  * ReadWriteLock.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: ReadWriteLock.java,v 1.3 2002/10/06 23:59:46 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 public final class ReadWriteLock
25 {
26     private int activeReaders;
27     private int activeWriters;
28     private int waitingReaders;
29     private int waitingWriters;
30     private Thread JavaDoc writerThread;
31     private int lockCount;
32
33     public synchronized void lockRead() throws InterruptedException JavaDoc
34     {
35         if (activeReaders != 0 || allowRead()) {
36             ++activeReaders;
37             return;
38         }
39         // Reaching here, either a write is in progress or waitingWriters > 0.
40
// If the current thread holds the write lock, we'll deadlock.
41
if (Thread.currentThread() == writerThread)
42             Debug.bug();
43         ++waitingReaders;
44         while (!allowRead()) {
45             try {
46                 wait();
47             }
48             catch (InterruptedException JavaDoc e) {
49                 --waitingReaders; // Roll back state.
50
throw e;
51             }
52         }
53         --waitingReaders;
54         ++activeReaders;
55     }
56
57     public synchronized void unlockRead()
58     {
59         Debug.assertTrue(activeReaders > 0);
60         --activeReaders;
61         notifyAll();
62     }
63
64     public synchronized void lockWrite() throws InterruptedException JavaDoc
65     {
66         if (writerThread != null) {
67             // Write in progress.
68
if (Thread.currentThread() == writerThread) {
69                 // Same thread.
70
++lockCount;
71                 return;
72             }
73         }
74         if (allowWrite()) {
75             claimWriteLock();
76             return;
77         }
78         ++waitingWriters;
79         while (!allowWrite()) {
80             try {
81                 wait();
82             }
83             catch (InterruptedException JavaDoc e) {
84                 --waitingWriters;
85                 throw e;
86             }
87         }
88         --waitingWriters;
89         claimWriteLock();
90     }
91
92     public synchronized void unlockWrite()
93     {
94         Debug.assertTrue(activeWriters == 1);
95         Debug.assertTrue(lockCount > 0);
96         Debug.assertTrue(Thread.currentThread() == writerThread);
97         if (--lockCount == 0) {
98             --activeWriters;
99             writerThread = null;
100             notifyAll();
101         }
102     }
103
104     public synchronized boolean isWriteLocked()
105     {
106         Debug.assertTrue(activeWriters == 0 || activeWriters == 1);
107         return activeWriters == 1;
108     }
109
110     private final boolean allowRead()
111     {
112         return waitingWriters == 0 && activeWriters == 0;
113     }
114
115     private final boolean allowWrite()
116     {
117         return activeReaders == 0 && activeWriters == 0;
118     }
119
120     private void claimWriteLock()
121     {
122         ++activeWriters;
123         Debug.assertTrue(writerThread == null);
124         writerThread = Thread.currentThread();
125         Debug.assertTrue(lockCount == 0);
126         lockCount = 1;
127     }
128 }
129
Popular Tags