summaryrefslogtreecommitdiff
path: root/java/JACE/Concurrency/RWMutex.java
blob: abb30ce3bc85e02adb301aa409a3843f54f60b92 (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
package JACE.Concurrency;

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

/**
 * A read/write lock allows multiple
 * readers or a single writer to access the guarded element.
 * <P>
 * <EM>This class does not support recursive semantics.</EM>
 */
public class RWMutex extends LockAdapter
{
  public synchronized int tryAcquire () 
  {
    if (referenceCount_ == 0) {
      referenceCount_ = -1;
      setOwner ();
      return AbstractLock.SUCCESS;
    } else
      return AbstractLock.FAILURE;
  }

  public synchronized int tryAcquireRead () 
  {
    if (referenceCount_ > -1 && waiters_.size () == 0) {
      referenceCount_++;
      setOwner ();
      return AbstractLock.SUCCESS;
    } else
      return AbstractLock.FAILURE;
  }

  public int acquire(TimeValue timeout) 
    throws TimeoutException, InterruptedException
  {
    return acquireWrite(timeout);
  }

  public void waitUntilIsOwner (RWWaitObject waitObj, TimeValue timeout)
    throws TimeoutException, InterruptedException
  {
    boolean exceptionOccured = true;
    try {
      sleepHook ();
      synchronized (waitObj) {
	waitObj.timedWait (timeout);
      }
      exceptionOccured = false;
    } finally {
      
      synchronized (this) {

	if (exceptionOccured) {
	  if (!waiters_.removeElement (waitObj)) {  
	    setOwner ();
	    release ();
	  } 
	} else
	  setOwner();
      }
    }
  }
  
  public int acquireRead(TimeValue timeout)
    throws TimeoutException, InterruptedException
  {
    RWWaitObject waitObj = null;

    synchronized (this) {

      if (referenceCount_ > -1 && waiters_.size () == 0) {
	referenceCount_++;
	setOwner ();
	return AbstractLock.SUCCESS;
      }

      waitObj = new RWWaitObject (true);

      waiters_.addElement (waitObj);
    }

    waitUntilIsOwner (waitObj, timeout);

    return AbstractLock.SLEEPHOOK;
  }
  
  public int acquireWrite(TimeValue timeout)
    throws TimeoutException, InterruptedException
  {
    RWWaitObject waitObj = null;

    synchronized (this) {
      
      if (referenceCount_ == 0) {
	referenceCount_ = -1;
	setOwner ();
	return AbstractLock.SUCCESS;
      }
      
      waitObj = new RWWaitObject (false);

      waiters_.addElement (waitObj);
    }

    waitUntilIsOwner (waitObj, timeout);

    // When the writer gets here, it has been cleared to go by
    // whatever thread specifically gave control to this writer in
    // release.  The referenceCount_ and numberOfWaitingWriters_
    // variables are also adjusted by the releasing thread since
    // it already has a synchronization lock.  Not doing that,
    // and then having another synchronized (this) block in here
    // could lead to a situation in which another thread sneaks
    // in inbetween when this thread leaves timedWait and goes to
    // adjust them.

    return AbstractLock.SLEEPHOOK;
  }


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

    clearOwner ();

    // Releasing a reader.
    if (referenceCount_ > 0) {
      referenceCount_--;
      
      if (referenceCount_ != 0) 
	return AbstractLock.SUCCESS;

    } else {
      // releasing a writer
      referenceCount_ = 0;
    }
    
    if (waiters_.size () == 0)
      return AbstractLock.SUCCESS;

    if (releaseFirstReaders () == 0) {
      RWWaitObject waitObj = (RWWaitObject)waiters_.firstElement ();
      waiters_.removeElementAt (0);
      
      referenceCount_ = -1;
      
      waitObj.condition (true);
      synchronized (waitObj) {
	waitObj.signal ();
      }
    }

    return AbstractLock.SUCCESS;
  }
   
  // Releases all waiting readers up to the first waiting writer
  // or the end of the queue.  Returns the number of readers
  // released.
  protected int releaseFirstReaders ()
  {
    int releasedReaders = 0;
    
    do {
      
      RWWaitObject waitObj = (RWWaitObject)waiters_.firstElement ();
      if (!waitObj.isReader ())
	break;

      waiters_.removeElementAt (0);

      referenceCount_++;
      releasedReaders++;
      
      waitObj.condition (true);
      synchronized (waitObj) {
	waitObj.signal ();
      }

    } while (waiters_.size () > 0);

    return releasedReaders;
  }

  public int renew (int requeuePosition,
		    JACE.ASX.TimeValue timeout)
    throws InterruptedException,
	   TimeoutException
  {
    RWWaitObject waitObj = null;

    synchronized (this) {

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

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

      waitObj = new RWWaitObject (referenceCount_ > 0);

      if (requeuePosition < 0 || requeuePosition > waiters_.size ()) {
	requeuePosition = waiters_.size ();
      }

      waiters_.insertElementAt (waitObj, requeuePosition);

      release ();
    }
    
    waitUntilIsOwner (waitObj, timeout);

    // When the writer gets here, it has been cleared to go by
    // whatever thread specifically gave control to this writer in
    // release.  The referenceCount_ and numberOfWaitingWriters_
    // variables are also adjusted by the releasing thread since
    // it already has a synchronization lock.  Not doing that,
    // and then having another synchronized (this) block in here
    // could lead to a situation in which another thread sneaks
    // in inbetween when this thread leaves timedWait and goes to
    // adjust them.

    return AbstractLock.SUCCESS;
  }

  static class RWWaitObject extends WaitObject
  {
    public RWWaitObject (boolean isReader)
    {
      isReader_ = isReader;
    }

    public boolean isReader ()
    {
      return isReader_;
    }

    private boolean isReader_ = false;
  }

  protected boolean isOwner ()
  {
    return owners_.containsKey (accessorID());
  }

  protected void setOwner ()
  {
    owners_.put (accessorID(), this);
  }

  protected void clearOwner ()
  {
    owners_.remove (accessorID());
  }

  private Vector waiters_ = new Vector ();

  private int referenceCount_ = 0;
  // Value is -1 if writer has the lock, else this keeps track of the
  // number of readers holding the lock.  

  private Hashtable owners_ = new Hashtable ();

  private int nestingLevel_ = 0;
}