blob: badaa72258963b95c9ee2ccfef5a59a7867a32be (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
package jogamp.common.util.locks;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import com.jogamp.common.util.locks.RecursiveLock;
public class RecursiveLockImplJava5 implements RecursiveLock {
volatile Thread owner = null;
ReentrantLock lock;
public RecursiveLockImplJava5(final boolean fair) {
lock = new ReentrantLock(fair);
}
@Override
public void lock() {
try {
if(!tryLock(TIMEOUT)) {
throw new RuntimeException("Waited "+TIMEOUT+"ms for: "+threadName(owner)+" - "+threadName(Thread.currentThread())+", with count "+getHoldCount()+", lock: "+this);
}
} catch (final InterruptedException e) {
throw new RuntimeException("Interrupted", e);
}
owner = Thread.currentThread();
}
@Override
public boolean tryLock(final long timeout) throws InterruptedException {
if(lock.tryLock(timeout, TimeUnit.MILLISECONDS)) {
owner = Thread.currentThread();
return true;
}
return false;
}
@Override
public void unlock() throws RuntimeException {
unlock(null);
}
@Override
public void unlock(final Runnable taskAfterUnlockBeforeNotify) {
validateLocked();
owner = null;
if(null!=taskAfterUnlockBeforeNotify) {
taskAfterUnlockBeforeNotify.run();
}
lock.unlock();
}
@Override
public boolean isLocked() {
return lock.isLocked();
}
@Override
public Thread getOwner() {
return owner;
}
@Override
public boolean isLockedByOtherThread() {
return lock.isLocked() && !lock.isHeldByCurrentThread();
}
@Override
public boolean isOwner(final Thread thread) {
return lock.isLocked() && owner == thread;
}
@Override
public void validateLocked() throws RuntimeException {
if ( !lock.isHeldByCurrentThread() ) {
if ( !lock.isLocked() ) {
throw new RuntimeException(Thread.currentThread()+": Not locked");
} else {
throw new RuntimeException(Thread.currentThread()+": Not owner, owner is "+owner);
}
}
}
@Override
public int getHoldCount() {
return lock.getHoldCount();
}
@Override
public int getQueueLength() {
return lock.getQueueLength();
}
private String threadName(final Thread t) { return null!=t ? "<"+t.getName()+">" : "<NULL>" ; }
}
|