KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > lockmanager > impl > ServerThreadContext


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.objectserver.lockmanager.impl;
5
6 import com.tc.net.protocol.tcm.ChannelID;
7 import com.tc.object.lockmanager.api.ServerThreadID;
8 import com.tc.object.lockmanager.api.ThreadID;
9 import com.tc.util.Assert;
10
11 import java.util.HashSet JavaDoc;
12 import java.util.Set JavaDoc;
13
14 /**
15  * This class represents a "open" transaction (ie. one that holds locks). It's only purpose is to hold the proper
16  * bookkeeping to do reasonably fast deadlock detection. Some day this might get merged with some of form of higher
17  * level server transaction class, but that really doesn't exist yet
18  */

19 class ServerThreadContext {
20   static final ServerThreadContext NULL_CONTEXT = new ServerThreadContext(ServerThreadID.NULL_ID);
21
22   private final static Lock[] EMPTY_LOCK_ARRAY = new Lock[] {};
23   private final boolean isNull;
24   private final Set JavaDoc locksHeld = new HashSet JavaDoc();
25   private final ServerThreadID id;
26   private ServerThreadContext cycle;
27   private Lock waitingOn = null;
28   private final int hashcode;
29
30   ServerThreadContext(ChannelID channelID, ThreadID threadID) {
31     this(new ServerThreadID(channelID, threadID));
32   }
33
34   ServerThreadContext(ServerThreadID id2) {
35     Assert.assertNotNull(id2);
36     this.id = id2;
37     this.isNull = ServerThreadID.NULL_ID.equals(id2);
38     this.hashcode = this.id.hashCode();
39   }
40
41   public String JavaDoc toString() {
42     return "ServerThreadContext@" + System.identityHashCode(this) + "[" + id + "](HELD-LOCKS={" + locksHeld
43            + "}, WAITING-ON={ " + waitingOn + "})";
44   }
45
46   public int hashCode() {
47     return this.hashcode;
48   }
49
50   public boolean equals(Object JavaDoc obj) {
51     if (obj instanceof ServerThreadContext) {
52       ServerThreadContext other = (ServerThreadContext) obj;
53       return this.id.equals(other.id);
54     }
55     return false;
56   }
57
58   public ServerThreadID getId() {
59     return this.id;
60   }
61
62   synchronized void addLock(Lock lock) {
63     boolean added = locksHeld.add(lock);
64     Assert.assertTrue(added);
65     clearWaitingOn();
66   }
67
68   synchronized boolean removeLock(Lock lock) {
69     boolean removed = locksHeld.remove(lock);
70     if (!removed) { throw new AssertionError JavaDoc(lock
71                                              + " : This lock is not held in this ServerThreadContext ! Locks Held = "
72                                              + locksHeld); }
73     return isClear();
74   }
75
76   synchronized boolean isWaiting() {
77     return this.waitingOn != null;
78   }
79
80   synchronized void setWaitingOn(Lock lock) {
81     if (!(this.waitingOn == null || !this.waitingOn.equals(lock))) { throw new AssertionError JavaDoc("Assert Failed : "
82                                                                                               + toString()
83                                                                                               + " : old = " + waitingOn
84                                                                                               + " : new = " + lock); }
85     this.waitingOn = lock;
86   }
87
88   synchronized boolean clearWaitingOn() {
89     this.waitingOn = null;
90     return isClear();
91   }
92
93   synchronized boolean isClear() {
94     return this.waitingOn == null && this.locksHeld.isEmpty();
95   }
96
97   synchronized Lock[] getLocksHeld() {
98     return (Lock[]) this.locksHeld.toArray(EMPTY_LOCK_ARRAY);
99   }
100
101   synchronized Lock getWaitingOn() {
102     return this.waitingOn;
103   }
104
105   // Deadlock cycle stuff. These methods not syncrhonized, only one thread will ever set/read
106
void setCycle(ServerThreadContext other) {
107     this.cycle = other;
108   }
109
110   // Deadlock cycle stuff. These methods not syncrhonized, only one thread will ever set/read
111
ServerThreadContext getCycle() {
112     return this.cycle;
113   }
114
115   public boolean isNull() {
116     return isNull;
117   }
118
119 }
120
Popular Tags