001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package java.util.concurrent.locks;
006:
007: import com.tc.exception.TCNotSupportedMethodException;
008: import com.tc.exception.TCObjectNotSharableException;
009: import com.tc.object.bytecode.ManagerUtil;
010: import com.tc.object.lockmanager.api.LockLevel;
011: import com.tc.util.concurrent.locks.TCLock;
012: import com.tcclient.util.concurrent.locks.ConditionObject;
013:
014: import java.util.Collection;
015: import java.util.concurrent.TimeUnit;
016:
017: public class ReentrantLockTC extends ReentrantLock implements TCLock {
018:
019: public void lock() {
020: ManagerUtil.monitorEnter(this , LockLevel.WRITE);
021: super .lock();
022: }
023:
024: public void lockInterruptibly() throws InterruptedException {
025: if (Thread.interrupted()) {
026: throw new InterruptedException();
027: }
028:
029: try {
030: ManagerUtil.monitorEnter(this , LockLevel.WRITE);
031: super .lockInterruptibly();
032: } finally {
033: if (Thread.interrupted()) {
034: unlock();
035: throw new InterruptedException();
036: }
037: }
038: }
039:
040: public Condition newCondition() {
041: return new ConditionObject(this );
042: }
043:
044: public void unlock() {
045: super .unlock();
046: ManagerUtil.monitorExit(this );
047: }
048:
049: public int getHoldCount() {
050: return super .getHoldCount();
051: }
052:
053: protected Thread getOwner() {
054: if (ManagerUtil.isManaged(this )) {
055: throw new TCNotSupportedMethodException();
056: } else {
057: return super .getOwner();
058: }
059: }
060:
061: protected Collection<Thread> getQueuedThreads() {
062: if (ManagerUtil.isManaged(this )) {
063: throw new TCNotSupportedMethodException();
064: } else {
065: return super .getQueuedThreads();
066: }
067: }
068:
069: protected Collection<Thread> getWaitingThreads(Condition condition) {
070: if (ManagerUtil.isManaged(this )) {
071: throw new TCNotSupportedMethodException();
072: } else {
073: if (condition == null)
074: throw new NullPointerException();
075: if (!(condition instanceof ConditionObject))
076: throw new IllegalArgumentException("not owner");
077: return ((ConditionObject) condition)
078: .getWaitingThreads(this );
079: }
080: }
081:
082: public int getWaitQueueLength(Condition condition) {
083: if (condition == null)
084: throw new NullPointerException();
085: if (!(condition instanceof ConditionObject))
086: throw new IllegalArgumentException("not owner");
087: return ((ConditionObject) condition).getWaitQueueLength(this );
088: }
089:
090: public boolean hasWaiters(Condition condition) {
091: if (condition == null)
092: throw new NullPointerException();
093: if (!(condition instanceof ConditionObject))
094: throw new IllegalArgumentException("not owner");
095: return ((ConditionObject) condition).getWaitQueueLength(this ) > 0;
096: }
097:
098: public boolean isLocked() {
099: if (ManagerUtil.isManaged(this )) {
100: return isHeldByCurrentThread()
101: || ManagerUtil.isLocked(this , LockLevel.WRITE);
102: } else {
103: return super .isLocked();
104: }
105: }
106:
107: private String getLockState() {
108: return (isLocked() ? (isHeldByCurrentThread() ? "[Locally locked]"
109: : "[Remotelly locked]")
110: : "[Unlocked]");
111: }
112:
113: public String toString() {
114: if (ManagerUtil.isManaged(this )) {
115: return (new StringBuilder()).append(super .toString())
116: .append(getLockState()).toString();
117: } else {
118: return super .toString();
119: }
120: }
121:
122: private boolean dsoTryLock() {
123: if (ManagerUtil.isManaged(this )) {
124: return ManagerUtil
125: .tryMonitorEnter(this , 0, LockLevel.WRITE);
126: } else {
127: return true;
128: }
129: }
130:
131: public boolean dsoTryLock(long timeout, TimeUnit unit) {
132: if (ManagerUtil.isManaged(this )) {
133: long timeoutInNanos = TimeUnit.NANOSECONDS.convert(timeout,
134: unit);
135: return ManagerUtil.tryMonitorEnter(this , timeoutInNanos,
136: LockLevel.WRITE);
137: } else {
138: return true;
139: }
140: }
141:
142: public boolean tryLock() {
143: boolean isLocked = dsoTryLock();
144: if (isLocked || !ManagerUtil.isManaged(this )) {
145: isLocked = super .tryLock();
146: }
147: return isLocked;
148: }
149:
150: public boolean tryLock(long timeout, TimeUnit unit)
151: throws InterruptedException {
152: boolean isLocked = dsoTryLock(timeout, unit);
153: if (isLocked || !ManagerUtil.isManaged(this )) {
154: isLocked = super .tryLock(timeout, unit);
155: }
156: return isLocked;
157: }
158:
159: public Object getTCLockingObject() {
160: return this ;
161: }
162:
163: public int localHeldCount() {
164: return getHoldCount();
165: }
166:
167: public void validateInUnLockState() {
168: if (super .isLocked()) {
169: throw new TCObjectNotSharableException(
170: "You are attempting to share a ReentrantLock when it is in a locked state. Lock cannot be shared while locked.");
171: }
172: }
173: }
|