Source code: com/flexstor/common/threadmgr/BooleanLock.java
1 /*
2 * BooleanLock.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:35 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.threadmgr;
12
13 /**
14 * Useful class to hide away all the implementation of a locking mechanism using
15 * wait()/notifyAll()
16 * This class makes missed notifications impossible and provides an interruptible,
17 * timeout-capable, technique for providing exclusive access to a block of code.
18 */
19 public class BooleanLock
20 {
21 private boolean value;
22
23 /**
24 * Sets the value of the lock to an initial state.
25 */
26 public BooleanLock(boolean initialValue)
27 {
28 value = initialValue;
29 }
30
31 /**
32 * Sets the initial value of the lock to false.
33 */
34 public BooleanLock()
35 {
36 this(false);
37 }
38
39 /**
40 * If current lock's value is different from newValue, sets it to newValue
41 * and notifies other threads of this change.
42 */
43 public synchronized void setValue(boolean newValue)
44 {
45 if ( newValue != value )
46 {
47 value = newValue;
48 notifyAll();
49 }
50 }
51
52 /**
53 * Waits until it can set the lock to true.
54 * It first waits until it has exclusive access and the lock is false.
55 *
56 * @return true after lock is set to true
57 * @throws InterruptedException if interrupted while waiting
58 */
59 public synchronized boolean waitToSetTrue()
60 throws InterruptedException
61 {
62 return waitToSetTrue(0);
63 }
64
65 /**
66 * Waits up to msTimeOut milliseconds to set the lock to true.
67 * It first waits until it has exclusive access and the lock is false.
68 *
69 * @param msTimeOut The max amount in milliseconds to wait for the lock to be false,
70 * or zero to wait indefinitely
71 * @return true if lock is set to true, false if timeout expires
72 * @throws InterruptedException if interrupted while waiting
73 */
74 public synchronized boolean waitToSetTrue(long msTimeout)
75 throws InterruptedException
76 {
77
78 boolean success = waitUntilFalse(msTimeout);
79 if ( success )
80 setValue(true);
81
82 return success;
83 }
84
85 /**
86 * Waits until it can set the lock to false.
87 * It first waits until it has exclusive access and the lock is true.
88 *
89 * @return true after lock is set to false
90 * @throws InterruptedException if interrupted while waiting
91 */
92 public synchronized boolean waitToSetFalse()
93 throws InterruptedException
94 {
95 return waitToSetFalse(0);
96 }
97
98 /**
99 * Waits up to msTimeOut milliseconds to set the lock to false.
100 * It first waits until it has exclusive access and the lock is true.
101 *
102 * @param msTimeOut The max amount in milliseconds to wait for the lock to be true,
103 * or zero to wait indefinitely
104 * @return true if lock is set to false, false if timeout expires
105 * @throws InterruptedException if interrupted while waiting
106 */
107 public synchronized boolean waitToSetFalse(long msTimeout)
108 throws InterruptedException
109 {
110 boolean success = waitUntilTrue(msTimeout);
111 if ( success )
112 setValue(false);
113
114 return success;
115 }
116
117 /**
118 * Returns true if lock is set to true, false, otherwise.
119 */
120 public synchronized boolean isTrue()
121 {
122 return value;
123 }
124
125 /**
126 * Returns true if lock is set to false, false otherwise.
127 */
128 public synchronized boolean isFalse()
129 {
130 return !value;
131 }
132
133 /**
134 * Waits until lock is true.
135 *
136 * @return true when lock becomes true
137 * @throws InterruptedException if interrupted while waiting
138 */
139 public synchronized boolean waitUntilTrue()
140 throws InterruptedException
141 {
142 return waitUntilStateIs(true, 0);
143 }
144
145 /**
146 * Waits up to msTimeOut milliseconds until another thread sets the lock to true.
147 *
148 * @param msTimeOut The max amount in milliseconds to wait for the lock to be true,
149 * or zero to wait indefinitely
150 * @return true if lock is set to true, false if timeout expires
151 * @throws InterruptedException if interrupted while waiting
152 */
153 public synchronized boolean waitUntilTrue(long msTimeout)
154 throws InterruptedException
155 {
156 return waitUntilStateIs(true, msTimeout);
157 }
158
159 /**
160 * Waits until lock is false.
161 *
162 * @return true when lock becomes false
163 * @throws InterruptedException if interrupted while waiting
164 */
165 public synchronized boolean waitUntilFalse()
166 throws InterruptedException
167 {
168 return waitUntilStateIs(false, 0);
169 }
170
171 /**
172 * Waits up to msTimeOut milliseconds until another thread sets the lock to false.
173 *
174 * @param msTimeOut The max amount in milliseconds to wait for the lock to be false,
175 * or zero to wait indefinitely
176 * @return true if lock is set to false, false if timeout expires
177 * @throws InterruptedException if interrupted while waiting
178 */
179 public synchronized boolean waitUntilFalse(long msTimeout)
180 throws InterruptedException
181 {
182 return waitUntilStateIs(false, msTimeout);
183 }
184
185 /**
186 * Waits until lock is set to state by another thread.
187 *
188 * @param state The state to check for
189 * @return true when lock becomes equal to state
190 * @throws InterruptedException if interrupted while waiting
191 */
192 public synchronized boolean waitUntilStateIs( boolean state )
193 throws InterruptedException
194 {
195 return waitUntilStateIs( state );
196 }
197
198 /**
199 * Waits up to msTimeOut milliseconds until another thread sets the lock to the state
200 * being checked.
201 *
202 * @param state The state to check for
203 * @param msTimeOut The max amount in milliseconds to wait for the lock to be true,
204 * or zero to wait indefinitely
205 * @return true if lock is set to state, false if timeout expires
206 * @throws InterruptedException if interrupted while waiting
207 */
208 public synchronized boolean waitUntilStateIs( boolean state, long msTimeout )
209 throws InterruptedException
210 {
211 if ( msTimeout == 0L ) {
212 while ( value != state )
213 wait(); // wait indefinitely until notified
214
215 // condition has finally been met
216 return true;
217 }
218
219 // only wait for the specified amount of time
220 long endTime = System.currentTimeMillis() + msTimeout;
221 long msRemaining = msTimeout;
222
223 while ( ( value != state ) && ( msRemaining > 0L ) )
224 {
225 wait(msRemaining);
226 msRemaining = endTime - System.currentTimeMillis();
227 }
228
229 // May have timed out, or may have met value,
230 // calculate return value.
231 return ( value == state );
232 }
233 }