summaryrefslogtreecommitdiff
path: root/java/JACE/Concurrency/Mutex.java
blob: 856fdbd79eb40e702eb7d54814a9b04662dec86c (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
/*************************************************
 *
 * = PACKAGE
 *    JACE.Concurrency
 *
 * = FILENAME
 *    Mutex.java
 *
 *@author Prashant Jain
 *@author Everett Anderson
 *
 *************************************************/
package JACE.Concurrency;

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

/**
 *     Value added abstraction for mutex variable creation.
 *
 *     A mutex whose operations do not block forever and can time out.
 * <P>
 * <EM>This class does not support recursive semantics.</EM>
 */
public class Mutex extends LockAdapter
{
  /**
   * Acquire ownership of the lock, blocking indefinitely if necessary.  
   * <P>
   *@return    AbstractLock.FAILURE or AbstractLock.SUCCESS
   *@exception InterruptedException indicates another thread has 
   *           interrupted this one during wait
   */
  public synchronized int acquire () throws InterruptedException
  {
    if (this.monitor_.condition ()) {
      this.monitor_.condition (false);
      setOwner ();
      return AbstractLock.SUCCESS;
    }

    this.numberOfWaiters_++;
    try {
      sleepHook ();
      this.monitor_.timedWait ();
    } finally {
      this.numberOfWaiters_--;
    }
    this.monitor_.condition (false);
    setOwner();
  
    return AbstractLock.SLEEPHOOK;
  }

  public int renew (int requeuePosition, 
		    TimeValue timeout) 
    throws InterruptedException,
	   TimeoutException
  {
    RenewObject rwo;

    synchronized (this) {

      if (!this.isOwner ())
	return AbstractLock.FAILURE;

      if (numberOfWaiters_ == 0 || requeuePosition == 0)
	return AbstractLock.SUCCESS;

      if (requeuePosition < 0 || requeuePosition > numberOfWaiters_)
	requeuePosition = numberOfWaiters_;
      
      rwo = new RenewObject (requeuePosition);
      
      this.release ();
      this.renewers_.addElement (rwo);
    }

    // We can't have the method synchronized, or synchronize on (this)
    // in here because then the Thread that was woken up won't be able
    // to continue its acquire.
    //
    // Normally when an exception occurs in timedWait, this thread just
    // needs to remove itself from the renewers queue.
    //
    // However, the following situation exists:
    //   Thread A is the current owner, and is doing processing in release()
    //   This thread generates a timeout exception in timedWait
    //   Thread A signals this thread to wake up and take ownership, and
    //     removes it from the queue.
    //   This thread never takes ownership -- the exception keeps going up.
    //
    // This could lead to other renewers waiting in limbo forever.
    //
    // Solution: If this thread has an exception and it looks like it
    // has been proclaimed the owner, then it calls release and lets
    // the exception continue.

    boolean exceptionOccured = true;
    try {
      synchronized (rwo) {
	rwo.timedWait (timeout);

	exceptionOccured = false;
      }
    } finally {
      if (exceptionOccured) {
	synchronized (this) {
	  if (!renewers_.removeElement (rwo)) {
	    setOwner ();
	    release ();
	  }
	}
      }
    }

    synchronized (this) {
      setOwner ();
    }

    // By this point, we should know that we have the lock.  The condition
    // flag is never set to true in the release() call from the Thread
    // that gave us control.

    return AbstractLock.SUCCESS;
  }
    
  public synchronized int tryAcquire () {
    if (this.monitor_.condition ()) {
      this.monitor_.condition (false);
      setOwner();
      return AbstractLock.SUCCESS;
    } else
      return AbstractLock.FAILURE;
  }

  public synchronized int acquire (TimeValue tv) 
    throws TimeoutException, InterruptedException 
  {
    if (this.monitor_.condition ()) {
      this.monitor_.condition (false);
      setOwner ();
      return AbstractLock.SUCCESS;
    }

    this.numberOfWaiters_++;
    try {
      sleepHook ();
      this.monitor_.timedWait (tv);
    } finally {
      this.numberOfWaiters_--;
    }
    this.monitor_.condition (false);
    setOwner();
    
    return AbstractLock.SLEEPHOOK;
  }

  /**
   * Checks any objects in the renewers queue, giving one of them
   * the lock if it is appropriate.  Assumes the synchronization
   * lock is already held.
   *
   *@return true if a renewer was signaled, else false
   */
  protected boolean signalNextRenewer ()
  {
    // First find the renewer with the minimum yieldTo count, processing
    // all of them along the way.
    if (this.renewers_.size() > 0) {

      RenewObject renewer = (RenewObject)renewers_.
	elementAt (renewers_.size () - 1);
      
      renewer.decrementYieldTo ();

      for (int i = this.renewers_.size() - 2; i >=0; i--) {
	
	RenewObject rwo = (RenewObject)renewers_.elementAt (i);
	
	rwo.decrementYieldTo ();
	
	renewer = renewer.min (rwo);
      }
      
      // If the renewer with the minimum yieldTo count has yielded to
      // enough threads, or if there are no waiting threads, it should
      // be signaled (thus, it wakes up and obtains the lock again).
      
      if (renewer.condition () || numberOfWaiters_ == 0) {
	// Note that we leave monitor_.condition in the false state so
	// we are assured that only the renewer (and not another
	// Thread that does an acquire) will gain control.  This
	// is important since the renew method can't be synchronized
	// in its current implementation.
	renewers_.removeElement(renewer);

	synchronized (renewer) {
	  renewer.signal ();
	}
  
	return true;
      }
    }

    return false;
  }

  public synchronized int release ()
  {
    if (!isOwner()) 
      return AbstractLock.FAILURE;

    if (!signalNextRenewer ()) {
      // Do a normal release if there are no threads waiting to renew
      // or no such threads are ready to renew.
      this.monitor_.condition (true);
      this.monitor_.signal ();
    }
    
    return AbstractLock.SUCCESS;
  }

  /**
   * Monitor used to signal whether or not this Mutex is available.
   */
  protected WaitObject monitor_ = new WaitObject (true, this);
  // The monitor (adapter) to wait on

  /**
   * Queue of waiting renewers.
   */
  protected Vector renewers_ = new Vector ();

  /**
   * Number of waiting threads.
   */
  protected int numberOfWaiters_ = 0;
}