KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > concurrent > ReadWriteLock


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6  
7  Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11  
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14  
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18  
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24  
25  4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
26     must not be used to endorse or promote products derived from this software
27     without prior written permission. For written permission, please contact
28     apache@apache.org.
29  
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33  
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48  
49 */

50 package org.apache.avalon.excalibur.concurrent;
51
52 /**
53  * Class implementing a read/write lock. The lock has three states -
54  * unlocked, locked for reading and locked for writing. If the lock
55  * is unlocked, anyone can acquire a read or write lock. If the lock
56  * is locked for reading, anyone can acquire a read lock, but no one
57  * can acquire a write lock. If the lock is locked for writing, no one
58  * can quire any type of lock.
59  * <p>
60  * When the lock is released, those threads attempting to acquire a write lock
61  * will take priority over those trying to get a read lock.
62  *
63  * @deprecated use EDU.oswego.cs.dl.util.concurrent.ReadWriteLock instead
64  *
65  * @author <a HREF="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
66  * @version CVS $Revision: 1.8 $ $Date: 2003/04/05 19:39:33 $
67  * @since 4.0
68  */

69 public class ReadWriteLock
70 {
71     /**
72      * The number of read locks currently held.
73      */

74     private int m_numReadLocksHeld = 0;
75
76     /**
77      * The number of threads waiting for a write lock.
78      */

79     private int m_numWaitingForWrite = 0;
80
81     /**
82      * Synchronization primitive.
83      */

84     private Object JavaDoc m_lock = new Object JavaDoc();
85
86     /**
87      * Default constructor.
88      */

89     public ReadWriteLock()
90     {
91     }
92
93     /**
94      * Attempts to acquire a read lock. If no lock could be acquired
95      * the thread will wait until it can be obtained.
96      *
97      * @throws InterruptedException if the thread is interrupted while waiting for
98      * a lock.
99      */

100     public void acquireRead()
101         throws InterruptedException JavaDoc
102     {
103         synchronized( m_lock )
104         {
105             while( !( m_numReadLocksHeld != -1 && m_numWaitingForWrite == 0 ) )
106             {
107                 m_lock.wait();
108             }
109             m_numReadLocksHeld++;
110         }
111     }
112
113     /**
114      * @deprecated It's spelled <code>a<b>c</b>quire</code>...
115      *
116      * @throws InterruptedException if the thread is interrupted while waiting
117      * for a lock.
118      */

119     public void aquireRead()
120         throws InterruptedException JavaDoc
121     {
122         acquireRead ();
123     }
124
125
126     /**
127      * Attempts to acquire a write lock. If no lock could be acquired
128      * the thread will wait until it can be obtained.
129      *
130      * @throws InterruptedException if the thread is interrupted while waiting for
131      * a lock.
132      */

133     public void acquireWrite()
134         throws InterruptedException JavaDoc
135     {
136         synchronized( m_lock )
137         {
138             m_numWaitingForWrite++;
139             try
140             {
141                 while( m_numReadLocksHeld != 0 )
142                 {
143                     m_lock.wait();
144                 }
145                 m_numReadLocksHeld = -1;
146             }
147             finally
148             {
149                 m_numWaitingForWrite--;
150             }
151         }
152     }
153
154     /**
155      * @deprecated It's spelled <code>a<b>c</b>quire</code>...
156      *
157      * @throws InterruptedException if the thread is interrupted while waiting
158      * for a lock.
159      */

160     public void aquireWrite()
161         throws InterruptedException JavaDoc
162     {
163         acquireWrite ();
164     }
165
166     /**
167      * Releases a lock. This method will release both types of locks.
168      *
169      * @throws IllegalStateException when an attempt is made to release
170      * an unlocked lock.
171      */

172     public void release()
173     {
174         synchronized( m_lock )
175         {
176             if( m_numReadLocksHeld == 0 )
177             {
178                 throw new IllegalStateException JavaDoc( "Attempted to release an unlocked ReadWriteLock." );
179             }
180
181             if( m_numReadLocksHeld == -1 )
182             {
183                 m_numReadLocksHeld = 0;
184             }
185             else
186             {
187                 m_numReadLocksHeld--;
188             }
189
190             m_lock.notifyAll();
191         }
192     }
193
194     /**
195      * Attempts to acquire a read lock. This method returns immediately.
196      *
197      * @return <code>true</code> iff the lock was successfully obtained.
198      */

199     public boolean tryAcquireRead()
200     {
201         synchronized( m_lock )
202         {
203             if( m_numReadLocksHeld != -1 && m_numWaitingForWrite == 0 )
204             {
205                 m_numReadLocksHeld++;
206                 return true;
207             }
208             else
209             {
210                 return false;
211             }
212         }
213     }
214
215     /**
216      * @deprecated It's spelled <code>a<b>c</b>quire</code>...
217      *
218      * @return <code>true</code> iff the lock was successfully obtained.
219      */

220     public boolean tryAquireRead()
221     {
222         return tryAcquireRead();
223     }
224
225     /**
226      * Attempts to acquire a write lock. This method returns immediately.
227      *
228      * @return <code>true</code> iff the lock was successfully obtained.
229      */

230     public boolean tryAcquireWrite()
231     {
232         synchronized( m_lock )
233         {
234             if( m_numReadLocksHeld == 0 )
235             {
236                 m_numReadLocksHeld = -1;
237                 return true;
238             }
239             else
240             {
241                 return false;
242             }
243         }
244     }
245
246     /**
247      * @deprecated It's spelled <code>a<b>c</b>quire</code>...
248      *
249      * @return <code>true</code> iff the lock was successfully obtained.
250      */

251     public boolean tryAquireWrite()
252     {
253         return tryAcquireWrite();
254     }
255     
256     //
257
// Methods used for unit tests
258
//
259

260     /**
261      * Returns the number of read locks held.
262      */

263     protected synchronized int getNumReadLocksHeld() {
264         return m_numReadLocksHeld;
265     }
266     
267     /**
268      * Returns the number of write locks held.
269      */

270     protected synchronized int getNumWaitingForWrite() {
271         return m_numWaitingForWrite;
272     }
273 }
274
Popular Tags