PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/util/ReadWriteLock.java

#
Java | 205 lines | 108 code | 19 blank | 78 comment | 28 complexity | f21203a929b58f223e816c309c686347 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * ReadWriteLock.java
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001 Peter Graves
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.util;
  23. import java.util.Vector;
  24. /**
  25. * Implements consumer/producer locking scemantics.
  26. * @author Peter Graves
  27. * @version $Id: ReadWriteLock.java 4970 2004-02-14 19:02:49Z spestov $
  28. * The lock tries to be re-entrant when called from the same thread in some
  29. * cases.
  30. *
  31. * The following is ok:
  32. * read lock
  33. * read lock
  34. * read unlock
  35. * read unlock
  36. *
  37. * write lock
  38. * read lock
  39. * read unlock
  40. * write unlock
  41. *
  42. * The following is not ok:
  43. *
  44. * read lock
  45. * write lock
  46. * write unlock
  47. * read unlock
  48. *
  49. * write lock
  50. * write lock
  51. * write unlock
  52. * write unlock
  53. */
  54. public class ReadWriteLock
  55. {
  56. //{{{ readLock() method
  57. public synchronized void readLock()
  58. {
  59. // this seems to make nested readLock() calls work okay.
  60. // but I have no idea if it actually fixes things or not.
  61. if (activeReaders != 0 || allowRead())
  62. {
  63. ++activeReaders;
  64. //readers.addElement(Thread.currentThread());
  65. return;
  66. }
  67. ++waitingReaders;
  68. while (!allowRead())
  69. {
  70. try
  71. {
  72. wait();
  73. }
  74. catch (InterruptedException e)
  75. {
  76. --waitingReaders; // Roll back state.
  77. Log.log(Log.ERROR,this,e);
  78. return;
  79. }
  80. }
  81. --waitingReaders;
  82. ++activeReaders;
  83. readers.addElement(Thread.currentThread());
  84. } //}}}
  85. //{{{ readUnlock() method
  86. public synchronized void readUnlock()
  87. {
  88. if(activeReaders == 0)
  89. throw new InternalError("Unbalanced readLock()/readUnlock() calls");
  90. --activeReaders;
  91. //readers.removeElement(Thread.currentThread());
  92. notifyAll();
  93. } //}}}
  94. //{{{ writeLock() method
  95. public synchronized void writeLock()
  96. {
  97. if (writerThread != null)
  98. {
  99. // Write in progress.
  100. if (Thread.currentThread() == writerThread)
  101. {
  102. // Same thread.
  103. ++lockCount;
  104. return;
  105. }
  106. }
  107. if (allowWrite())
  108. {
  109. claimWriteLock();
  110. return;
  111. }
  112. ++waitingWriters;
  113. while (!allowWrite())
  114. {
  115. try
  116. {
  117. wait();
  118. }
  119. catch (InterruptedException e)
  120. {
  121. --waitingWriters;
  122. Log.log(Log.ERROR,this,e);
  123. return;
  124. }
  125. }
  126. --waitingWriters;
  127. claimWriteLock();
  128. } //}}}
  129. //{{{ writeUnlock() method
  130. public synchronized void writeUnlock()
  131. {
  132. if(activeWriters != 1 || lockCount <= 0)
  133. throw new InternalError("Unbalanced writeLock()/writeUnlock() calls");
  134. if(Thread.currentThread() != writerThread)
  135. throw new InternalError("writeUnlock() from wrong thread");
  136. if (--lockCount == 0)
  137. {
  138. --activeWriters;
  139. writerThread = null;
  140. notifyAll();
  141. }
  142. } //}}}
  143. //{{{ isWriteLocked() method
  144. public synchronized boolean isWriteLocked()
  145. {
  146. //Debug.assert(activeWriters == 0 || activeWriters == 1);
  147. return activeWriters == 1;
  148. } //}}}
  149. //{{{ Private members
  150. //{{{ Instance variables
  151. private int activeReaders;
  152. private int activeWriters;
  153. private int waitingReaders;
  154. private int waitingWriters;
  155. private Vector readers = new Vector();
  156. private Thread writerThread;
  157. private int lockCount;
  158. //}}}
  159. //{{{ allowRead() method
  160. private final boolean allowRead()
  161. {
  162. return (Thread.currentThread() == writerThread)
  163. || (waitingWriters == 0 && activeWriters == 0);
  164. } //}}}
  165. //{{{ allowWrite() method
  166. private final boolean allowWrite()
  167. {
  168. /*Thread current = Thread.currentThread();
  169. for(int i = 0; i < readers.size(); i++)
  170. {
  171. if(readers.elementAt(i) == current)
  172. throw new InternalError("Cannot nest writeLock() inside readLock()");
  173. }*/
  174. return activeReaders == 0 && activeWriters == 0;
  175. } //}}}
  176. //{{{ claimWriteLock() method
  177. private void claimWriteLock()
  178. {
  179. ++activeWriters;
  180. //Debug.assert(writerThread == null);
  181. writerThread = Thread.currentThread();
  182. //Debug.assert(lockCount == 0);
  183. lockCount = 1;
  184. } //}}}
  185. //}}}
  186. }