01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.object.lockmanager.impl;
05:
06: import com.tc.object.lockmanager.api.ClientLockManager;
07: import com.tc.object.lockmanager.api.LockID;
08: import com.tc.object.lockmanager.api.Notify;
09: import com.tc.object.lockmanager.api.ThreadID;
10: import com.tc.object.lockmanager.api.ThreadLockManager;
11: import com.tc.object.lockmanager.api.WaitListener;
12: import com.tc.object.tx.WaitInvocation;
13:
14: public class ThreadLockManagerImpl implements ThreadLockManager {
15:
16: private final ClientLockManager lockManager;
17: private final ThreadLocal threadID;
18: private long threadIDSequence;
19:
20: public ThreadLockManagerImpl(ClientLockManager lockManager) {
21: this .lockManager = lockManager;
22: this .threadID = new ThreadLocal();
23: }
24:
25: public LockID lockIDFor(String lockName) {
26: return lockManager.lockIDFor(lockName);
27: }
28:
29: public int queueLength(LockID lockID) {
30: return lockManager.queueLength(lockID, getThreadID());
31: }
32:
33: public int waitLength(LockID lockID) {
34: return lockManager.waitLength(lockID, getThreadID());
35: }
36:
37: public int localHeldCount(LockID lockID, int lockLevel) {
38: return lockManager.localHeldCount(lockID, lockLevel,
39: getThreadID());
40: }
41:
42: public boolean isLocked(LockID lockID, int lockLevel) {
43: return lockManager.isLocked(lockID, getThreadID(), lockLevel);
44: }
45:
46: public void lock(LockID lockID, int lockLevel) {
47: lockManager.lock(lockID, getThreadID(), lockLevel);
48: }
49:
50: public boolean tryLock(LockID lockID, WaitInvocation timeout,
51: int lockLevel) {
52: return lockManager.tryLock(lockID, getThreadID(), timeout,
53: lockLevel);
54: }
55:
56: public void wait(LockID lockID, WaitInvocation call, Object object,
57: WaitListener waitListener) throws InterruptedException {
58: lockManager.wait(lockID, getThreadID(), call, object,
59: waitListener);
60: }
61:
62: public Notify notify(LockID lockID, boolean all) {
63: // XXX: HACK HACK HACK: this is here because notifies need to be attached to transactions.
64: // this needs to be refactored. --Orion (10/26/05)
65: return lockManager.notify(lockID, getThreadID(), all);
66: }
67:
68: public void unlock(LockID lockID) {
69: lockManager.unlock(lockID, getThreadID());
70: }
71:
72: private ThreadID getThreadID() {
73: ThreadID rv = (ThreadID) threadID.get();
74: if (rv == null) {
75: rv = new ThreadID(nextThreadID());
76: threadID.set(rv);
77: }
78:
79: return rv;
80: }
81:
82: private synchronized long nextThreadID() {
83: return ++threadIDSequence;
84: }
85:
86: }
|