PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/util/ReadWriteLock.java

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