KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > CosTransactions > RWLock


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 package com.sun.jts.CosTransactions;
30 import java.lang.InterruptedException JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import com.sun.logging.LogDomains;
35
36
37 /**
38  * A <i>RWLock</i> provides concurrency control for multiple readers single writer
39  * access patterns. This lock can provide access to multiple reader threads simultaneously
40  * as long as there are no writer threads. Once a writer thread gains access to the
41  * instance locked by a RWLock, all the reader threads wait till the writer completes
42  * accessing the instance in question.
43  * <p>
44  * A RWLock is extremely useful in scenarios where there are lots more readers and
45  * very few writers to a data structure. Also if the read operation by the reader
46  * thread could take significant amount of time (binary search etc.)
47  * <p>
48  * The usage of Lock can be see as under:
49  * <p><hr><blockquote><pre>
50  * public class MyBTree {
51  * private RWLock lock = new Lock();
52  * .....
53  * .....
54  * public Object find(Object o) {
55  * try {
56  * lock.acquireReadLock();
57  * ....perform complex search to get the Object ...
58  * return result;
59  * } finally {
60  * lock.releaseReadLock();
61  * }
62  * }
63  *
64  * public void insert(Object o) {
65  * try {
66  * lock.acquireWriteLock();
67  * ....perform complex operation to insert object ...
68  * } finally {
69  * lock.releaseWriteLock();
70  * }
71  * }
72  * }
73  * </pre></blockquote><hr>
74  * <p>
75  * @author Dhiru Pandey 8/7/2000
76  */

77  
78  
79 public class RWLock {
80
81   int currentReaders;
82   int pendingReaders;
83   int currentWriters;
84   /*
85      Logger to log transaction messages
86   */

87   static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER);
88  
89   Queue writerQueue = new Queue();
90   /**
91    * This method is used to acquire a read lock. If there is already a writer thread
92    * accessing the object using the RWLock then the reader thread will wait until
93    * the writer completes its operation
94    */

95   public synchronized void acquireReadLock() {
96     if (currentWriters == 0 && writerQueue.size() == 0) {
97       ++currentReaders;
98     } else {
99       ++pendingReaders;
100       try {
101         wait();
102       } catch(InterruptedException JavaDoc ie) {
103         _logger.log(Level.FINE,"Error in acquireReadLock",ie);
104       }
105     }
106   }
107
108   /**
109    * This method is used to acquire a write lock. If there are already reader threads
110    * accessing the object using the RWLock, then the writer thread will wait till all
111    * the reader threads are finished with their operations.
112    */

113   public void acquireWriteLock() {
114     Object JavaDoc lock = new Object JavaDoc();
115
116     synchronized(lock) {
117       synchronized(this) {
118         if (writerQueue.size() == 0 && currentReaders == 0 && currentWriters == 0) {
119           ++currentWriters;
120           // Use logging facility if you need to log this
121
//_logger.log(Level.FINE," RW: incremented WriterLock count");
122
return;
123         }
124         writerQueue.enQueue(lock);
125         // Use logging facility if you need to log this
126
//_logger.log(Level.FINE," RW: Added WriterLock to queue");
127
}
128       try {
129         lock.wait();
130       } catch(InterruptedException JavaDoc ie) {
131         _logger.log(Level.FINE,"Error in acquireWriteLock",ie);
132       }
133     }
134   }
135
136   /**
137    * isWriteLocked
138    *
139    * returns true if the RWLock is in a write locked state.
140    *
141    */

142   public boolean isWriteLocked()
143   {
144       return currentWriters > 0 ;
145   }
146  
147   /**
148    * This method is used to release a read lock.
149    * It also notifies any waiting writer thread
150    * that it could now acquire a write lock.
151    */

152   public synchronized void releaseReadLock() {
153     if (--currentReaders == 0)
154       notifyWriters();
155   }
156  
157   /**
158    * This method is used to release a write lock. It also notifies any pending
159    * readers that they could now acquire the read lock. If there are no reader
160    * threads then it will try to notify any waiting writer thread that it could now
161    * acquire a write lock.
162    */

163   public synchronized void releaseWriteLock() {
164     --currentWriters;
165     if (pendingReaders > 0)
166       notifyReaders();
167     else
168       notifyWriters();
169   }
170   private void notifyReaders() {
171     currentReaders += pendingReaders;
172     pendingReaders = 0;
173     notifyAll();
174   }
175   
176   private void notifyWriters() {
177     if (writerQueue.size() > 0) {
178       Object JavaDoc lock = writerQueue.deQueueFirst();
179       ++currentWriters;
180       synchronized(lock) {
181         lock.notify();
182       }
183     }
184   }
185
186   class Queue extends LinkedList JavaDoc {
187
188     public Queue() {
189       super();
190     }
191
192     public void enQueue(Object JavaDoc o) {
193       super.addLast(o);
194     }
195
196     public Object JavaDoc deQueueFirst() {
197       return super.removeFirst();
198     }
199
200   }
201
202 }
203  
204
205
206
Popular Tags