1 4 package com.tc.object.lockmanager.impl; 5 6 import com.tc.object.lockmanager.api.ClientLockManager; 7 import com.tc.object.lockmanager.api.LockID; 8 import com.tc.object.lockmanager.api.Notify; 9 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, getThreadID()); 39 } 40 41 public boolean isLocked(LockID lockID) { 42 return lockManager.isLocked(lockID, getThreadID()); 43 } 44 45 public void lock(LockID lockID, int lockLevel) { 46 lockManager.lock(lockID, getThreadID(), lockLevel); 47 } 48 49 public boolean tryLock(LockID lockID, int lockLevel) { 50 return lockManager.tryLock(lockID, getThreadID(), lockLevel); 51 } 52 53 public void wait(LockID lockID, WaitInvocation call, Object object, WaitListener waitListener) throws InterruptedException { 54 lockManager.wait(lockID, getThreadID(), call, object, waitListener); 55 } 56 57 public Notify notify(LockID lockID, boolean all) { 58 return lockManager.notify(lockID, getThreadID(), all); 61 } 62 63 public void unlock(LockID lockID) { 64 lockManager.unlock(lockID, getThreadID()); 65 } 66 67 private ThreadID getThreadID() { 68 ThreadID rv = (ThreadID) threadID.get(); 69 if (rv == null) { 70 rv = new ThreadID(nextThreadID()); 71 threadID.set(rv); 72 } 73 74 return rv; 75 } 76 77 private synchronized long nextThreadID() { 78 return ++threadIDSequence; 79 } 80 81 } 82 | Popular Tags |