1 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 ; 12 import java.util.Set ; 13 14 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 locksHeld = new HashSet (); 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 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 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 (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 ("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 void setCycle(ServerThreadContext other) { 107 this.cycle = other; 108 } 109 110 ServerThreadContext getCycle() { 112 return this.cycle; 113 } 114 115 public boolean isNull() { 116 return isNull; 117 } 118 119 } 120 | Popular Tags |