summaryrefslogtreecommitdiff
path: root/java/JACE/Concurrency/LockAdapter.java
blob: db2e9de05c73e3c5d3e46bb4d0f14a696b95d573 (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
/*************************************************
 *
 * = PACKAGE
 *    JACE.Concurrency
 *
 * = FILENAME
 *    Lock.java
 *
 *@author Everett Anderson
 *
 *************************************************/
package JACE.Concurrency;

import JACE.ASX.*;

/**
 *      Abstract adapter class which provides useful default implementations
 *      for several methods in the AbstractLock interface, as well as 
 *      protected helper functions for making sure only the owner
 *      can perform certain operations.
 *
 *@see  JACE.Concurrency.AbstractLock
 */
public abstract class LockAdapter implements AbstractLock
{
  /**
   * Default implementation that calls acquire (TimeValue) with a null
   * timeout.
   *
   *@see AbstractLock#acquire
   */
  public int acquire () throws InterruptedException
  {
    try {
      return acquire (null);
    } catch (TimeoutException e) {
      // This should never happen
      return AbstractLock.FAILURE;
    }
  }

  /**
   * 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      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 abstract int acquire (TimeValue timeout) 
    throws TimeoutException, InterruptedException;

  /**
   * Default implementation that calls acquireRead (TimeValue) with a
   * null timeout.
   *
   *@see AbstractLock#acquireRead
   */
  public int acquireRead () throws InterruptedException
  {
    try {
      return acquireRead (null);
    } catch (TimeoutException e) {
      // This should never happen
    }

    return AbstractLock.FAILURE;
  }

  /**
   * Default implementation that calls acquire (TimeValue).
   *
   *@see AbstractLock#acquireRead(TimeValue)
   */
  public int acquireRead (TimeValue timeout) 
    throws TimeoutException, InterruptedException
  {
    return acquire (timeout);
  }

  /**
   * Default implementation that calls acquire with a null
   * timeout.
   *
   *@see AbstractLock#acquireWrite
   */
  public int acquireWrite () throws InterruptedException
  {
    try {
      return acquire (null);
    } catch (TimeoutException e) {
      // This should never happen
    }

    return AbstractLock.FAILURE;
  }

  /**
   * Default implementation that calls acquire (TimeValue).
   *
   *@see AbstractLock#acquireWrite(TimeValue)
   */
  public int acquireWrite (TimeValue timeout) 
    throws TimeoutException, InterruptedException
  {
    return acquire (timeout);
  }
  
  /**
   * Default implementation that calls renew (int, TimeValue) with
   * a null timeout.
   *
   *@see AbstractLock#renew(int)
   */
  public int renew (int requeuePosition) throws InterruptedException
  {
    try 
      {
	return renew (requeuePosition, null);
      } catch (TimeoutException e) {
	// Note that this should never happen since we requested a
	// blocking acquire.
	return AbstractLock.FAILURE;
      }
  }

  /**
   * 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      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 abstract int renew (int requeuePosition,
			     TimeValue timeout)
			     throws TimeoutException,
				    InterruptedException;

  /**
   * Default implementation that calls tryAcquire ().
   *
   *@see AbstractLock#tryAcquireRead
   */
  public int tryAcquireRead ()
  {
    return tryAcquire ();
  }

  /**
   * Default implementation that calls tryAcquire ().
   *
   *@see AbstractLock#tryAcquireWrite
   */
  public int tryAcquireWrite ()
  {
    return tryAcquire ();
  }
  /** 
   * Try to acquire the lock without blocking.  
   * <P>
   *@return         appropriate AbstractLock return value 
   *                (AbstractLock.FAILURE or AbstractLock.SUCCESS)
   */
  public abstract int tryAcquire ();

  /**
   * Default implementation as a no-op.
   *
   *@see AbstractLock#sleepHook
   */
  public void sleepHook ()
  {
  }

  /** 
   * Release ownership of this lock.  
   * <P>
   *@return    appropriate AbstractLock return value 
   *           (AbstractLock.FAILURE or AbstractLock.SUCCESS)
   */
  public abstract int release ();

  /**
   * Obtains an Object which uniquely identifies the current accessor
   * (usually a thread).  This is used to make sure only an owner can
   * perform certain operations like release.  Subclasses can redefine
   * the behavior as necessary, such as in the Token service where it is
   * defined to be the client ID sent by the proxy.
   * <P>
   * When using Java 1.2 or later, it might be more efficient to use
   * ThreadLocal and an Integer for the ID.  The current default 
   * implementation returns the Thread.currentThread () reference.
   *
   *@return Object representing a unique ID for this accessor
   */
  protected Object accessorID ()
  {
    return Thread.currentThread();
  }

  /**
   * Check to see if the current accessor is the (or a) owner of this
   * lock.
   */
  protected boolean isOwner() 
  {
    return accessorID().equals(this.owner_);
  }

  /**
   * Set the current accessor to be the (or a) owner of this lock.
   */
  protected void setOwner()
  {
    this.owner_ = accessorID();
  }

  /**
   * Make sure that this accessor is no longer the (or a) owner of this
   * lock.
   */
  protected void clearOwner()
  {
    this.owner_ = null;
  }

  /**
   * Reference to the accessorID of the owner.
   */
  private Object owner_;
}