19 |
|
import junit.framework.Test; |
20 |
|
import junit.framework.TestSuite; |
21 |
|
|
22 |
+ |
@SuppressWarnings("WaitNotInLoop") // we implement spurious-wakeup freedom |
23 |
|
public class AbstractQueuedSynchronizerTest extends JSR166TestCase { |
24 |
|
public static void main(String[] args) { |
25 |
|
main(suite(), args); |
34 |
|
* methods/features of AbstractQueuedSynchronizer are tested via |
35 |
|
* other test classes, including those for ReentrantLock, |
36 |
|
* ReentrantReadWriteLock, and Semaphore. |
37 |
+ |
* |
38 |
+ |
* Unlike the javadoc sample, we don't track owner thread via |
39 |
+ |
* AbstractOwnableSynchronizer methods. |
40 |
|
*/ |
41 |
|
static class Mutex extends AbstractQueuedSynchronizer { |
42 |
|
/** An eccentric value for locked synchronizer state. */ |
44 |
|
|
45 |
|
static final int UNLOCKED = 0; |
46 |
|
|
47 |
+ |
/** Owner thread is untracked, so this is really just isLocked(). */ |
48 |
|
@Override public boolean isHeldExclusively() { |
49 |
|
int state = getState(); |
50 |
|
assertTrue(state == UNLOCKED || state == LOCKED); |
51 |
|
return state == LOCKED; |
52 |
|
} |
53 |
|
|
54 |
< |
@Override public boolean tryAcquire(int acquires) { |
54 |
> |
@Override protected boolean tryAcquire(int acquires) { |
55 |
|
assertEquals(LOCKED, acquires); |
56 |
|
return compareAndSetState(UNLOCKED, LOCKED); |
57 |
|
} |
58 |
|
|
59 |
< |
@Override public boolean tryRelease(int releases) { |
59 |
> |
@Override protected boolean tryRelease(int releases) { |
60 |
|
if (getState() != LOCKED) throw new IllegalMonitorStateException(); |
61 |
|
assertEquals(LOCKED, releases); |
62 |
|
setState(UNLOCKED); |
87 |
|
release(LOCKED); |
88 |
|
} |
89 |
|
|
90 |
+ |
/** Faux-Implements Lock.newCondition(). */ |
91 |
|
public ConditionObject newCondition() { |
92 |
|
return new ConditionObject(); |
93 |
|
} |
94 |
|
} |
95 |
|
|
96 |
|
/** |
97 |
< |
* A simple latch class, to test shared mode. |
97 |
> |
* A minimal latch class, to test shared mode. |
98 |
|
*/ |
99 |
|
static class BooleanLatch extends AbstractQueuedSynchronizer { |
100 |
|
public boolean isSignalled() { return getState() != 0; } |
1165 |
|
|
1166 |
|
waitForQueuedThread(l, t); |
1167 |
|
assertFalse(l.isSignalled()); |
1168 |
< |
assertThreadStaysAlive(t); |
1168 |
> |
assertThreadBlocks(t, Thread.State.TIMED_WAITING); |
1169 |
|
assertTrue(l.releaseShared(0)); |
1170 |
|
assertTrue(l.isSignalled()); |
1171 |
|
awaitTermination(t); |