1. /*
  2. File: Mutex.java
  3. Originally written by Doug Lea and released into the public domain.
  4. This may be used for any purposes whatsoever without acknowledgment.
  5. Thanks for the assistance and support of Sun Microsystems Labs,
  6. and everyone contributing, testing, and using this code.
  7. History:
  8. Date Who What
  9. 11Jun1998 dl Create public version
  10. */
  11. package com.sun.corba.se.impl.orbutil.concurrent;
  12. /**
  13. * A simple non-reentrant mutual exclusion lock.
  14. * The lock is free upon construction. Each acquire gets the
  15. * lock, and each release frees it. Releasing a lock that
  16. * is already free has no effect.
  17. * <p>
  18. * This implementation makes no attempt to provide any fairness
  19. * or ordering guarantees. If you need them, consider using one of
  20. * the Semaphore implementations as a locking mechanism.
  21. * <p>
  22. * <b>Sample usage</b><br>
  23. * <p>
  24. * Mutex can be useful in constructions that cannot be
  25. * expressed using java synchronized blocks because the
  26. * acquire/release pairs do not occur in the same method or
  27. * code block. For example, you can use them for hand-over-hand
  28. * locking across the nodes of a linked list. This allows
  29. * extremely fine-grained locking, and so increases
  30. * potential concurrency, at the cost of additional complexity and
  31. * overhead that would normally make this worthwhile only in cases of
  32. * extreme contention.
  33. * <pre>
  34. * class Node {
  35. * Object item;
  36. * Node next;
  37. * Mutex lock = new Mutex(); // each node keeps its own lock
  38. *
  39. * Node(Object x, Node n) { item = x; next = n; }
  40. * }
  41. *
  42. * class List {
  43. * protected Node head; // pointer to first node of list
  44. *
  45. * // Use plain java synchronization to protect head field.
  46. * // (We could instead use a Mutex here too but there is no
  47. * // reason to do so.)
  48. * protected synchronized Node getHead() { return head; }
  49. *
  50. * boolean search(Object x) throws InterruptedException {
  51. * Node p = getHead();
  52. * if (p == null) return false;
  53. *
  54. * // (This could be made more compact, but for clarity of illustration,
  55. * // all of the cases that can arise are handled separately.)
  56. *
  57. * p.lock.acquire(); // Prime loop by acquiring first lock.
  58. * // (If the acquire fails due to
  59. * // interrupt, the method will throw
  60. * // InterruptedException now,
  61. * // so there is no need for any
  62. * // further cleanup.)
  63. * for (;;) {
  64. * if (x.equals(p.item)) {
  65. * p.lock.release(); // release current before return
  66. * return true;
  67. * }
  68. * else {
  69. * Node nextp = p.next;
  70. * if (nextp == null) {
  71. * p.lock.release(); // release final lock that was held
  72. * return false;
  73. * }
  74. * else {
  75. * try {
  76. * nextp.lock.acquire(); // get next lock before releasing current
  77. * }
  78. * catch (InterruptedException ex) {
  79. * p.lock.release(); // also release current if acquire fails
  80. * throw ex;
  81. * }
  82. * p.lock.release(); // release old lock now that new one held
  83. * p = nextp;
  84. * }
  85. * }
  86. * }
  87. * }
  88. *
  89. * synchronized void add(Object x) { // simple prepend
  90. * // The use of `synchronized' here protects only head field.
  91. * // The method does not need to wait out other traversers
  92. * // who have already made it past head.
  93. *
  94. * head = new Node(x, head);
  95. * }
  96. *
  97. * // ... other similar traversal and update methods ...
  98. * }
  99. * </pre>
  100. * <p>
  101. * <p>This version adds some debugging capability: it will detect an attempt by a thread
  102. * that holds the lock to acquire it for a second time, and also an attempt by a thread that
  103. * does not hold the mutex to release it.
  104. * @see Semaphore
  105. * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
  106. **/
  107. import org.omg.CORBA.INTERNAL ;
  108. public class DebugMutex implements Sync {
  109. /** The lock status **/
  110. protected boolean inuse_ = false;
  111. protected Thread holder_ = null;
  112. public void acquire() throws InterruptedException {
  113. if (Thread.interrupted()) throw new InterruptedException();
  114. synchronized(this) {
  115. Thread thr = Thread.currentThread();
  116. if (holder_ == thr)
  117. throw new INTERNAL(
  118. "Attempt to acquire Mutex by thread holding the Mutex" ) ;
  119. try {
  120. while (inuse_) wait();
  121. inuse_ = true;
  122. holder_ = Thread.currentThread();
  123. }
  124. catch (InterruptedException ex) {
  125. notify();
  126. throw ex;
  127. }
  128. }
  129. }
  130. public synchronized void release() {
  131. Thread thr = Thread.currentThread();
  132. if (thr != holder_)
  133. throw new INTERNAL(
  134. "Attempt to release Mutex by thread not holding the Mutex" ) ;
  135. holder_ = null;
  136. inuse_ = false;
  137. notify();
  138. }
  139. public boolean attempt(long msecs) throws InterruptedException {
  140. if (Thread.interrupted()) throw new InterruptedException();
  141. synchronized(this) {
  142. Thread thr = Thread.currentThread() ;
  143. if (!inuse_) {
  144. inuse_ = true;
  145. holder_ = thr;
  146. return true;
  147. } else if (msecs <= 0)
  148. return false;
  149. else {
  150. long waitTime = msecs;
  151. long start = System.currentTimeMillis();
  152. try {
  153. for (;;) {
  154. wait(waitTime);
  155. if (!inuse_) {
  156. inuse_ = true;
  157. holder_ = thr;
  158. return true;
  159. }
  160. else {
  161. waitTime = msecs - (System.currentTimeMillis() - start);
  162. if (waitTime <= 0)
  163. return false;
  164. }
  165. }
  166. }
  167. catch (InterruptedException ex) {
  168. notify();
  169. throw ex;
  170. }
  171. }
  172. }
  173. }
  174. }