summaryrefslogtreecommitdiff
path: root/java/JACE/Concurrency/AbstractLock.java
blob: c8afaf789aefc06721456eb3d288ec18c65f58db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*************************************************
 *
 * = PACKAGE
 *    JACE.Concurrency
 *
 * = FILENAME
 *    Lock.java
 *
 *@author Everett Anderson
 *
 *************************************************/
package JACE.Concurrency;

import JACE.ASX.*;

/**
 *      Interface for any Java ACE synchronization mechanism.
 * <P>
 *      Defines the interface for Token, Mutex, RWMutex, Semaphore,
 *      and the RemoteLock proxies in the Token service, as well as
 *      the possible constant return values.
 * <P>
 *      Methods which take TimeValue timeouts can throw
 *      JACE.ASX.TimeoutExceptions.  The locks should continue to
 *      function properly after a thread times out or is interrupted.
 *      <em>Also note that the timeouts are absolute time-of-day
 *      values, not relative times.</em>
 * <P>
 *      An AbstractLock.FAILURE can be returned for an undefined type of
 *      failure.
 * <P>
 *      You can assume that 
 *      AbstractLock.FAILURE < AbstractLock.SUCCESS < AbstractLock.SLEEPHOOK
 * <P>
 *      Any method can throw a LockException, providing a way to return
 *      unusual error cases in future types of locks (such as the Token
 *      service).
 * <P>
 *      It is safe to call release () in a finally block, since it will
 *      return FAILURE if the accessing thread is not the owner.
 *
 */
public interface AbstractLock
{
  /**
   * Generic failure indication, used as a return value.
   */
  public static final int FAILURE = -1;

  /**
   * Success indication, used as a return value.
   */
  int SUCCESS = 0;
  
  /**
   * Success indication, but notes that the thread had to sleep
   * to complete it (and it called the sleep hook).  Used as a
   * return value.
   */
  int SLEEPHOOK = 1;

  
  /**
   * Acquire ownership of the lock, blocking indefinitely if necessary.  
   * <P>
   *@return    AbstractLock.FAILURE or AbstractLock.SUCCESS
   *@exception LockException special exception defined by a later 
   *           implementation
   *@exception InterruptedException indicates another thread has 
   *           interrupted this one during wait
   */
  public int acquire () throws LockException, InterruptedException;

  /**
   * Acquire ownership of the lock by the given absolute time time-out.
   * A value of null for the timeout parameter results in a blocking
   * acquire.
   * A value of TimeValue.zero throws a TimeoutException if the
   * acquire would block.
   * <P>
   *@param timeout  absolute time by which the lock must be acquired
   *@return         appropriate Lock return value (AbstractLock.FAILURE, 
   *                AbstractLock.SUCCESS or AbstractLock.SLEEPHOOK)
   *@exception      LockException special exception defined by a later
   *                implementation
   *@exception      JACE.ASX.TimeoutException thrown when the lock is not
   *                obtained by the desired time
   *@exception      InterruptedException indicates another thread has 
   *                interrupted this one during wait
   *@see AbstractLock#tryAcquire
   */
  public int acquire (TimeValue timeout)
    throws LockException, TimeoutException, InterruptedException;

  /**
   * Acquire a read lock, blocking indefinitely if necessary.  This can
   * be used to implement Reader-Writer locks in which multiple readers
   * may have simultaneous access.
   * <P>
   *@return    AbstractLock.FAILURE or AbstractLock.SUCCESS
   *@exception LockException special exception defined by a later 
   *           implementation
   *@exception InterruptedException indicates another thread has 
   *           interrupted this one during wait
   */
  public int acquireRead () throws LockException, InterruptedException;
  
  /**
   * Acquire a read lock by the given absolute time time-out.  This can 
   * be used to implement Reader-Writer locks in which multiple readers
   * may have simultaneous access.  
   * <P>
   *@param timeout  absolute time by which the lock must be acquired
   *@return         appropriate lock return value (AbstractLock.FAILURE, 
   *                AbstractLock.SUCCESS or AbstractLock.SLEEPHOOK)
   *@exception      LockException special exception defined by a later
   *                implementation
   *@exception      JACE.ASX.TimeoutException thrown when the lock is not
   *                obtained by the desired time
   *@exception      InterruptedException indicates another thread has 
   *                interrupted this one during wait
   *@see AbstractLock#tryAcquireRead
   */
  public int acquireRead (TimeValue timeout) 
    throws LockException, TimeoutException, InterruptedException;

  /**
   * Acquire a write lock, blocking indefinitely if necessary.  This can
   * be used to implement Reader-Writer locks in which a writer has 
   * exclusive access.  
   * <P>
   *@return    AbstractLock.FAILURE or AbstractLock.SUCCESS
   *@exception LockException special exception defined by a later 
   *           implementation
   *@exception InterruptedException indicates another thread has 
   *           interrupted this one during wait
   */
  public int acquireWrite () throws LockException, InterruptedException;

  /** 
   * Acquire a write lock by the given absolute time time-out.  This can
   * be used to implement Reader-Writer locks in which a writer has
   * exclusive access.  
   * <P>
   *@param timeout  absolute time by which the lock must be acquired
   *@return         appropriate AbstractLock return value (AbstractLock.FAILURE, 
   *                AbstractLock.SUCCESS or AbstractLock.SLEEPHOOK)
   *@exception      LockException special exception defined by a later
   *                implementation
   *@exception      JACE.ASX.TimeoutException thrown when the lock is not
   *                obtained by the desired time
   *@exception      InterruptedException indicates another thread has 
   *                interrupted this one during wait
   *@see AbstractLock#tryAcquireWrite
   */
  public int acquireWrite (TimeValue timeout) 
    throws LockException, TimeoutException, InterruptedException;
  
  /**
   * Give up the lock to some number of waiting threads (if any), then 
   * reacquire, blocking indefinitely if necessary.
   * <P>
   * An optimized method that efficiently reacquires the token if no
   * other threads are waiting.  This is useful for situations where
   * you don't want to degrade the quality of service if there are
   * other threads waiting to get the token. 
   * <P>
   *@param     requeuePosition position in the waiters queue to insert
   *           this thread.  If this value is -1 and there are other
   *           threads waiting to obtain the token, this thread is queued
   *           at the end.  If this value is greater than -1, then it
   *           indicates how many entries to skip over before inserting
   *           our thread into the queue.  (For example, if it is 0,
   *           this thread is put at the front of the queue.)  If this 
   *           value is greater than the number of waiters, this thread is
   *           simply put at the end of the current waiters queue.
   *@return    AbstractLock.FAILURE or AbstractLock.SUCCESS
   *@exception LockException special exception defined by a later 
   *           implementation
   *@exception InterruptedException indicates another thread has 
   *           interrupted this one during wait
   */
  public int renew (int requeuePosition) throws LockException,
        InterruptedException;

  /**
   * Give up the lock to some waiting threads (if any), then reacquire
   * by the given absolute time time-out.
   * <P>
   * An optimized method that efficiently reacquires the token if no
   * other threads are waiting.  This is useful for situations where
   * you don't want to degrade the quality of service if there are
   * other threads waiting to get the token. 
   * <P>
   * A value of null for the timeout should indicate a blocking renew.
   * <P>
   *@param     requeuePosition position in the waiters queue to insert
   *           this thread.  If this value is -1 and there are other
   *           threads waiting to obtain the token, this thread is queued
   *           at the end.  If this value is greater than -1, then it
   *           indicates how many entries to skip over before inserting
   *           our thread into the queue.  (For example, if it is 0,
   *           this thread is put at the front of the queue.)  If this 
   *           value is greater than the number of waiters, this thread is
   *           simply put at the end of the current waiters queue.
   * 
   *@param     timeout absolute time by which the lock must be reacquired
   *
   *@return         appropriate AbstractLock return value 
   *                (AbstractLock.FAILURE or AbstractLock.SUCCESS)
   *@exception      LockException special exception defined by a later
   *                implementation
   *@exception      JACE.ASX.TimeoutException thrown when the lock is not
   *                obtained by the desired time
   *@exception      InterruptedException indicates another thread has 
   *                interrupted this one during wait
   */
  public int renew (int requeuePosition, TimeValue timeout) 
    throws LockException, TimeoutException, InterruptedException;

  /** 
   * Try to acquire the lock without blocking.  
   * <P>
   *@return         appropriate AbstractLock return value 
   *                (AbstractLock.FAILURE or AbstractLock.SUCCESS)
   *@exception      LockException special exception defined by a later
   *                implementation
   */
  public int tryAcquire () throws LockException;

  /** 
   * Try to acquire a read lock without blocking.  
   * <P>
   *@see            #acquireRead
   *@return         appropriate AbstractLock return value 
   *                (AbstractLock.FAILURE or AbstractLock.SUCCESS)
   *@exception      LockException special exception defined by a later
   *                implementation
   */
  public int tryAcquireRead () throws LockException;

  /**
   * Try to acquire a write lock without blocking.  
   *<P>
   *@see            #acquireWrite
   *@return         appropriate AbstractLock return value 
   *                (AbstractLock.FAILURE or AbstractLock.SUCCESS)
   *@exception      LockException special exception defined by a later
   *                implementation
   */
  public int tryAcquireWrite () throws LockException;

  /**
   * Method that is called before a thread goes to sleep in an
   * acquire. This should be overridden by a subclass to define 
   * the appropriate behavior.
   */
  public void sleepHook ();

  /** 
   * Release ownership of this lock.  
   * <P>
   *@return    appropriate AbstractLock return value 
   *           (AbstractLock.FAILURE or AbstractLock.SUCCESS)
   *@exception LockException special exception defined by a later
   *           implementation
   */
  public int release () throws LockException;
}