summaryrefslogtreecommitdiff
path: root/java/src/Token.java
blob: c112acdb6531990579a35e68e309821e19f5d36f (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*************************************************
 *
 * = PACKAGE
 *    JACE.Concurrency
 *
 * = FILENAME
 *    Token.java
 *
 *@author Prashant Jain
 *
 *************************************************/
package JACE.Concurrency;

import java.util.*;
import JACE.ASX.*;

class WaitObject extends TimedWait
{
  public boolean condition ()
  {
    return this.condition_;
  }
  
  public void condition (boolean c)
  {
    this.condition_ = c;
  }

  private boolean condition_ = false;
}

/**
 * <hr>
 * <h2>SYNOPSIS</h2>
 *<blockquote>
 *     Class that acquires, renews, and releases a synchronization
 *     token that is serviced in strict FIFO ordering.
 *
 *</blockquote>
 *
 * <h2>DESCRIPTION</h2>
 *<blockquote>
 *     This is a general-purpose synchronization mechanism that offers
 *     several benefits. For example, it implements "recursive mutex"
 *     semantics, where a thread that owns the token can reacquire it
 *     without deadlocking.  In addition, threads that are blocked
 *     awaiting the token are serviced in strict FIFO order as other
 *     threads release the token. The solution makes use of the
 *     Specific Notification pattern presented by Tom Cargill in
 *     "Specific Notification for Java Thread Synchronization," PLoP96.
 *</blockquote>
 */
public class Token
{

  /**
   * Acquire the token. Note that this will block. The method uses
   * synchronized blocks internally to avoid race conditions.  It
   * ignores thread interrupts.
   *@return 0 if acquires without calling <sleepHook>
   * 1 if <sleepHook> is called.
   * -1 if failure occurs (should never happen)
   */
  public int acquire ()
    {
      try
	{
	    return this.acquire (null);
	}
      catch (TimeoutException e)
	{
	  // This really shouldn't happen since we are supposed to
	  // block.
	  return -1;
	}
    }

  /**
   * Acquire the token.  Returns failure   
   * Throws a TimeoutException if the token isn't acquired before the
   * given absolute time timeout.
   *@param timeout time (TimeValue) to wait until before throwing a
   * TimeoutException (unless the token is acquired before that).
   * Performs a blocking acquire if the given timeout is null.
   *@return 0 if acquires without calling <sleepHook>
   * 1 if <sleepHook> is called.
   * -1 if failure occurs (timeout)
   */
  public int acquire (TimeValue timeout) throws TimeoutException
    {
      int result = 0;
      WaitObject snl = new WaitObject ();
      boolean mustWait;
      synchronized (snl)
	{
	  synchronized (this)
	    {
	      mustWait = !this.snq_.isEmpty ();

	      if (mustWait && isOwner ())
		{
		  // I am the one who has the token. So just increment
		  // the nesting level
		  this.nestingLevel_++;
		  return 0;
		}
	      // Add local lock to the queue
	      this.snq_.addElement (snl);
	    }
	  if (mustWait)
	  {
	      result = 1;
	      sleepHook();

	      while (mustWait) { 
		  try {
		      snl.timedWait(timeout);
		      mustWait = false;
		  } catch (InterruptedException e) {
		      // must keep waiting
		  }
	      }
	  }

	  // Set the owner of the token
	  setOwner();
	}
      return result;
    }

  /**
   * Try to acquire the token. Implements a non-blocking acquire.
   *@return 0 if acquires without calling <sleepHook>
   * -1 if failure occurs
   */
  public synchronized int tryAcquire ()
    {
      int result = 0;

      if (this.snq_.isEmpty ())
	{
	  // No one has the token, so acquire it
	  this.snq_.addElement (new WaitObject ());

      setOwner();
	}
      else if (isOwner())
	{
	  this.nestingLevel_++;
	}
      // Someone else has the token.
      else
	{
      // Would have to block to acquire the token, so return
      // failure.
	  result = -1;
	}
      return result;
    }

  /**
   * 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 before acquire() goes to sleep.
   * By default, this is a no-op.
   */
  public void sleepHook ()
  {
  }

  /**
   * 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.  This blocks until it
   * can regain the token.
   *@param requeuePosition Position in the queue where to insert the
   * lock. If requeuePosition == -1 and there are other threads
   * waiting to obtain the token we are queued at the end of the list
   * of waiters.  If requeuePosition > -1 then it indicates how many
   * entries to skip over before inserting our thread into the list of
   * waiters (e.g.,requeuePosition == 0 means "insert at front of the
   * queue"). 
   */
  public void renew (int requeuePosition)
  {
    try
      {
	this.renew (requeuePosition, null);
      }
    catch (TimeoutException e)
      {
	// This really shouldn't happen since we are supposed to
	// block.
      }
  }

  /**
   * 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.  If the given TimeValue
   * is null, it's the same as calling renew(int requeuePosition).
   *@param requeuePosition Position in the queue where to insert the
   * lock. If requeuePosition == -1 and there are other threads
   * waiting to obtain the token we are queued at the end of the list
   * of waiters.  If requeuePosition > -1 then it indicates how many
   * entries to skip over before inserting our thread into the list of
   * waiters (e.g.,requeuePosition == 0 means "insert at front of the
   * queue").
   *@param timeout Throw a TimeoutException if the token isn't renewed
   * before this absolute time timeout.
   *@exception TimeoutException exception if timeout occurs
   */
  public void renew (int requeuePosition, TimeValue timeout) 
    throws TimeoutException
  {
    WaitObject snl = null;
    int saveNestingLevel = 0;

    synchronized (this)
    {
        // Check if there is a thread waiting to acquire the token. If
        // not or if requeuePosition == 0, then we don't do anything
        // and we simply keep the token. 
        if (this.snq_.size () > 1 && requeuePosition != 0)
        {
            // Save the nesting level
            saveNestingLevel = this.nestingLevel_;
            this.nestingLevel_ = 0;
            
            // Reinsert ourselves at requeuePosition in the queue
            snl = (WaitObject) this.snq_.firstElement ();
            this.snq_.removeElementAt (0);
            
            if (requeuePosition < 0)
                this.snq_.addElement (snl);  // Insert at end
            else
                this.snq_.insertElementAt (snl, Math.min(requeuePosition,
                                                         this.snq_.size()));
            
            synchronized (this.snq_.firstElement ())
            {
                // Notify the first waiting thread in the queue
                WaitObject obj = (WaitObject) this.snq_.firstElement ();
                // Set its condition to be true so that it falls out
                // of the for loop
                obj.condition (true);
                // Now signal the thread
                obj.signal ();
            }    
        }
    }
    
    // Check if we reinserted the lock in the queue and therefore need
    // to do a wait 
    if (snl != null)
    {
        synchronized (snl)
        {
            // Set the condition to be false so that we can begin the
            // wait 
            snl.condition (false);
	    // Wait until the given absolute time (or until notified
	    // if the timeout is null)
	    boolean mustWait = true;
	    while (mustWait) {
		try {
		    snl.timedWait (timeout);
		    mustWait = false;
		} catch (InterruptedException e) { 
		    // must keep waiting
		}
	    }
        }
        // Restore the nesting level and current owner of the lock
        this.nestingLevel_ = saveNestingLevel;

        // Set the owner of the token
        setOwner();
    }
  }

  /**
   * Release the token.  It is safe for non-owners to call
   * this.
   */
  public synchronized void release ()
  {
    if (!isOwner())
        return;

    // Check if nestingLevel > 0 and if so, decrement it
    if (this.nestingLevel_ > 0)
        this.nestingLevel_--;
    else
        {
            this.snq_.removeElementAt (0);
            if (!this.snq_.isEmpty ())
            {
                synchronized (this.snq_.firstElement ())
                {
                    // Notify the first waiting thread in the queue
                    WaitObject obj = (WaitObject) this.snq_.firstElement ();
                    // Set its condition to be true so that it falls out
                    // of the for loop
                    obj.condition (true);
                    // Now signal the thread
                    obj.signal ();
                }
            }
        }
  }

  // The next two methods allow subclasses to change the behavior of the
  // checking and setting the Object owner_ member variable.  The default
  // is to use the current Thread's toString() as the Object.
  protected void setOwner() {
      this.owner_ = Thread.currentThread().toString();
  }

  protected boolean isOwner() {
      return Thread.currentThread().toString().equals(this.owner_);
  }

  private Vector snq_ = new Vector ();
  // Vector of lock objects

  private int nestingLevel_ = 0;
  // Current Nesting Level

  private Object owner_ = null;
  // Current owner of the token.  The setOwner() and isOwner()
  // methods provide subclasses with the ability to change the
  // behavior.  The default is to use the Thread.toString().
}