summaryrefslogtreecommitdiff
path: root/java/JACE/netsvcs/Token/MutexHandler.java
blob: 82f79fe5a76cea563c6d06bea5f430fc58a5a59d (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
package JACE.netsvcs.Token;

import JACE.Concurrency.*;

/**
 * LockHandler implementation for a mutex lock.  
 * <P>
 * Currently, this uses JACE.Concurrency.Token as the actual lock since
 * it supports nested acquires.
 *
 *@see LockHandler
 */
public class MutexHandler extends LockHandlerAdapter
{
  // Uses token since it supports nested acquires.
  static class ExtendedMutex extends Token
  {
    // This is so that we don't make any assumptions about previous
    // implementations of LockAdapter, and enable owner checking with
    // the client ID from TokenRequest.  The thread name is set in
    // handleRequest.
    protected Object accessorID ()
    {
      return Thread.currentThread().getName();
    }
  }

  /**
   * Default constructor.
   */
  public MutexHandler ()
  {
    super (new ExtendedMutex ());
  }

  public TokenReply handleRequest (TokenRequestHandler caller,
				   TokenRequest request)
  {
    Thread.currentThread().setName (request.clientID ());
    
    return super.handleRequest (caller, request);
  }

  public void abandonLock (String clientID)
  {
    Thread.currentThread().setName (clientID);

    super.abandonLock (clientID);
  }
}