01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.objectserver.lockmanager.impl;
06:
07: import com.tc.async.api.Sink;
08: import com.tc.net.groups.NodeID;
09: import com.tc.object.lockmanager.api.LockID;
10: import com.tc.object.lockmanager.api.LockLevel;
11: import com.tc.object.lockmanager.api.ThreadID;
12: import com.tc.objectserver.lockmanager.api.LockAwardContext;
13: import com.tc.util.Assert;
14:
15: public class Holder implements LockAwardContext {
16: private final ServerThreadContext threadContext;
17: private final LockID lockID;
18: private final NodeID nodeID;
19: private final ThreadID threadID;
20: private final long timeout;
21: private final long timestamp;
22: private int lockLevel;
23: private Sink sink;
24:
25: public Holder(LockID lockID, ServerThreadContext txn, long timeout) {
26: this .timestamp = System.currentTimeMillis();
27: this .lockID = lockID;
28: this .threadContext = txn;
29: this .nodeID = txn.getId().getNodeID();
30: this .threadID = txn.getId().getClientThreadID();
31: this .timeout = timeout;
32: this .lockLevel = LockLevel.NIL_LOCK_LEVEL;
33: }
34:
35: synchronized int addLockLevel(int level) {
36: if (LockLevel.isGreedy(level)) {
37: Assert.assertEquals(getThreadID(), ThreadID.VM_ID);
38: level = LockLevel.makeNotGreedy(level);
39: }
40: return this .lockLevel |= level;
41: }
42:
43: synchronized int removeLockLevel(int level) {
44: return this .lockLevel ^= level;
45: }
46:
47: public synchronized int getLockLevel() {
48: return this .lockLevel;
49: }
50:
51: public NodeID getNodeID() {
52: return nodeID;
53: }
54:
55: public LockID getLockID() {
56: return lockID;
57: }
58:
59: public long getTimeout() {
60: return this .timeout;
61: }
62:
63: public ThreadID getThreadID() {
64: return threadID;
65: }
66:
67: public boolean isGreedy() {
68: return (getThreadID().equals(ThreadID.VM_ID));
69: }
70:
71: public long getTimestamp() {
72: return timestamp;
73: }
74:
75: public String toString() {
76: return "Holder" + "@" + System.identityHashCode(this ) + "["
77: + nodeID + "," + threadID + ",level="
78: + LockLevel.toString(getLockLevel()) + ",timeout="
79: + timeout + "]";
80: }
81:
82: ServerThreadContext getThreadContext() {
83: return threadContext;
84: }
85:
86: public void setSink(Sink lockResponseSink) {
87: Assert.assertTrue(isGreedy());
88: sink = lockResponseSink;
89: }
90:
91: public Sink getSink() {
92: Assert.assertTrue(isGreedy());
93: return sink;
94: }
95: }
|