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