// a read / write lock
final ReadWriteLockManager manager = new ReadWriteLockManager(sLogger,
TIMEOUT);
final RendezvousBarrier restart = new RendezvousBarrier("restart", 5, TIMEOUT, sLogger);
final TurnBarrier cb = new TurnBarrier("cb1", TIMEOUT, sLogger, 1);
for (int i = 0; i < CONCURRENT_TESTS; i++) {
System.out.print(".");
// thread that accesses lock of res1 just to cause interference and
// possibly detect concurrency problems
Thread jamThread1 = new Thread(new Runnable() {
public void run() {
try {
for (int i = 0; i < 10; i++) {
manager.readLock(jamowner1, res1);
Thread.sleep(10);
manager.releaseAll(jamowner1);
Thread.sleep(10);
manager.writeLock(jamowner1, res1);
Thread.sleep(10);
manager.releaseAll(jamowner1);
Thread.sleep(10);
}
} catch (LockException le) {
fail("Jam Thread should not fail");
} catch (InterruptedException ie) {
} finally {
manager.releaseAll(jamowner1);
synchronized (restart) {
try {
synchronized (restart) {
restart.meet();
restart.reset();
}
} catch (InterruptedException ie) {}
}
}
}
}, "Jam Thread #1");
jamThread1.start();
// thread that accesses lock of res1 just to cause interference and
// possibly detect concurrency problems
Thread jamThread2 = new Thread(new Runnable() {
public void run() {
try {
for (int i = 0; i < 10; i++) {
manager.writeLock(jamowner2, res1);
Thread.sleep(10);
manager.releaseAll(jamowner2);
Thread.sleep(10);
manager.readLock(jamowner2, res1);
Thread.sleep(10);
manager.releaseAll(jamowner2);
Thread.sleep(10);
}
} catch (LockException le) {
fail("Jam Thread should not fail");
} catch (InterruptedException ie) {
} finally {
manager.releaseAll(jamowner2);
synchronized (restart) {
try {
synchronized (restart) {
restart.meet();
restart.reset();
}
} catch (InterruptedException ie) {}
}
}
}
}, "Jam Thread #2");
jamThread2.start();
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
cb.waitForTurn(2);
manager.readLock(owner2, res2);
cb.signalTurn(3);
cb.waitForTurn(5);
synchronized (manager.getLock(res1)) {
cb.signalTurn(6);
manager.writeLock(owner2, res1);
}
} catch (LockException le) {
assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
deadlockCnt++;
} catch (InterruptedException ie) {
} finally {
manager.releaseAll(owner2);
synchronized (restart) {
try {
synchronized (restart) {
restart.meet();
restart.reset();
}
} catch (InterruptedException ie) {}
}
}
}
}, "Thread #1");
t1.start();
Thread t2 = new Thread(new Runnable() {
public void run() {
try {
cb.waitForTurn(3);
manager.readLock(owner3, res3);
synchronized (manager.getLock(res2)) {
cb.signalTurn(5);
manager.writeLock(owner3, res2);
}
} catch (LockException le) {
assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
deadlockCnt++;
} catch (InterruptedException ie) {
} finally {
manager.releaseAll(owner3);
synchronized (restart) {
try {
synchronized (restart) {
restart.meet();
restart.reset();
}
} catch (InterruptedException ie) {}
}
}
}
}, "Thread #2");
t2.start();
try {
cb.waitForTurn(1);
manager.readLock(owner1, res1);
cb.signalTurn(2);
cb.waitForTurn(6);
manager.writeLock(owner1, res3);
} catch (LockException le) {
assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
deadlockCnt++;
} catch (InterruptedException ie) {
} finally {
manager.releaseAll(owner1);
synchronized (restart) {
try {
synchronized (restart) {
restart.meet();
restart.reset();
}
} catch (InterruptedException ie) {
}
}
}