PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 180 lines | 108 code | 19 blank | 53 comment | 28 complexity | adeed918a85070ef7aa8968806cce598 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 4455 2003-01-31 04:49:31Z spestov $
  28. */
  29. public class ReadWriteLock
  30. {
  31. //{{{ readLock() method
  32. public synchronized void readLock()
  33. {
  34. // this seems to make nested readLock() calls work okay.
  35. // but I have no idea if it actually fixes things or not.
  36. if (activeReaders != 0 || allowRead())
  37. {
  38. ++activeReaders;
  39. //readers.addElement(Thread.currentThread());
  40. return;
  41. }
  42. ++waitingReaders;
  43. while (!allowRead())
  44. {
  45. try
  46. {
  47. wait();
  48. }
  49. catch (InterruptedException e)
  50. {
  51. --waitingReaders; // Roll back state.
  52. Log.log(Log.ERROR,this,e);
  53. return;
  54. }
  55. }
  56. --waitingReaders;
  57. ++activeReaders;
  58. readers.addElement(Thread.currentThread());
  59. } //}}}
  60. //{{{ readUnlock() method
  61. public synchronized void readUnlock()
  62. {
  63. if(activeReaders == 0)
  64. throw new InternalError("Unbalanced readLock()/readUnlock() calls");
  65. --activeReaders;
  66. //readers.removeElement(Thread.currentThread());
  67. notifyAll();
  68. } //}}}
  69. //{{{ writeLock() method
  70. public synchronized void writeLock()
  71. {
  72. if (writerThread != null)
  73. {
  74. // Write in progress.
  75. if (Thread.currentThread() == writerThread)
  76. {
  77. // Same thread.
  78. ++lockCount;
  79. return;
  80. }
  81. }
  82. if (allowWrite())
  83. {
  84. claimWriteLock();
  85. return;
  86. }
  87. ++waitingWriters;
  88. while (!allowWrite())
  89. {
  90. try
  91. {
  92. wait();
  93. }
  94. catch (InterruptedException e)
  95. {
  96. --waitingWriters;
  97. Log.log(Log.ERROR,this,e);
  98. return;
  99. }
  100. }
  101. --waitingWriters;
  102. claimWriteLock();
  103. } //}}}
  104. //{{{ writeUnlock() method
  105. public synchronized void writeUnlock()
  106. {
  107. if(activeWriters != 1 || lockCount <= 0)
  108. throw new InternalError("Unbalanced writeLock()/writeUnlock() calls");
  109. if(Thread.currentThread() != writerThread)
  110. throw new InternalError("writeUnlock() from wrong thread");
  111. if (--lockCount == 0)
  112. {
  113. --activeWriters;
  114. writerThread = null;
  115. notifyAll();
  116. }
  117. } //}}}
  118. //{{{ isWriteLocked() method
  119. public synchronized boolean isWriteLocked()
  120. {
  121. //Debug.assert(activeWriters == 0 || activeWriters == 1);
  122. return activeWriters == 1;
  123. } //}}}
  124. //{{{ Private members
  125. //{{{ Instance variables
  126. private int activeReaders;
  127. private int activeWriters;
  128. private int waitingReaders;
  129. private int waitingWriters;
  130. private Vector readers = new Vector();
  131. private Thread writerThread;
  132. private int lockCount;
  133. //}}}
  134. //{{{ allowRead() method
  135. private final boolean allowRead()
  136. {
  137. return (Thread.currentThread() == writerThread)
  138. || (waitingWriters == 0 && activeWriters == 0);
  139. } //}}}
  140. //{{{ allowWrite() method
  141. private final boolean allowWrite()
  142. {
  143. /*Thread current = Thread.currentThread();
  144. for(int i = 0; i < readers.size(); i++)
  145. {
  146. if(readers.elementAt(i) == current)
  147. throw new InternalError("Cannot nest writeLock() inside readLock()");
  148. }*/
  149. return activeReaders == 0 && activeWriters == 0;
  150. } //}}}
  151. //{{{ claimWriteLock() method
  152. private void claimWriteLock()
  153. {
  154. ++activeWriters;
  155. //Debug.assert(writerThread == null);
  156. writerThread = Thread.currentThread();
  157. //Debug.assert(lockCount == 0);
  158. lockCount = 1;
  159. } //}}}
  160. //}}}
  161. }