use of java.util.concurrent.locks.StampedLock in project jdk8u_jdk by JetBrains.
the class Basic method realMain.
private static void realMain(String[] args) throws Throwable {
Thread.currentThread().setName("mainThread");
//----------------------------------------------------------------
try {
final StampedLock sl = new StampedLock();
check(!sl.isReadLocked());
check(!sl.isWriteLocked());
long stamp = sl.tryOptimisticRead();
check(stamp != 0L);
check(sl.validate(stamp));
check(!sl.validate(0));
stamp = sl.writeLock();
try {
check(sl.validate(stamp));
check(!sl.isReadLocked());
check(sl.isWriteLocked());
check(sl.tryReadLock() == 0L);
check(sl.tryReadLock(100, MILLISECONDS) == 0L);
check(sl.tryOptimisticRead() == 0L);
check(sl.tryWriteLock() == 0L);
check(sl.tryWriteLock(100, MILLISECONDS) == 0L);
check(!sl.tryUnlockRead());
check(sl.tryConvertToWriteLock(stamp) == stamp);
try {
sl.unlockRead(stamp);
fail("Expected unlockRead to throw when not holding read lock");
} catch (IllegalMonitorStateException x) {
pass();
}
check(sl.validate(stamp));
} finally {
sl.unlockWrite(stamp);
}
check(!sl.isWriteLocked());
stamp = sl.readLock();
try {
check(sl.validate(stamp));
check(sl.isReadLocked());
check(!sl.isWriteLocked());
check(sl.tryOptimisticRead() != 0L);
check(sl.tryWriteLock() == 0L);
check(sl.tryWriteLock(100, MILLISECONDS) == 0L);
check(!sl.tryUnlockWrite());
check(sl.tryConvertToReadLock(stamp) == stamp);
try {
sl.unlockWrite(stamp);
fail("Expected unlockWrite to throw when not holding read lock");
} catch (IllegalMonitorStateException x) {
pass();
}
check(sl.validate(stamp));
} finally {
sl.unlockRead(stamp);
}
check(!sl.isReadLocked());
stamp = sl.tryReadLock(100, MILLISECONDS);
try {
check(stamp != 0L);
} finally {
sl.unlockRead(stamp);
}
} catch (Throwable t) {
unexpected(t);
}
//----------------------------------------------------------------
try {
StampedLock sl = new StampedLock();
Phaser gate = new Phaser(102);
Iterator<Writer> writers = writerIterator(sl, gate);
Iterator<Reader> readers = readerIterator(sl, gate);
for (int i = 0; i < 10; i++) {
check(!sl.isReadLocked());
check(!sl.isWriteLocked());
check(!sl.tryUnlockRead());
check(!sl.tryUnlockWrite());
check(sl.tryOptimisticRead() != 0L);
Locker[] wThreads = new Locker[100];
;
for (int j = 0; j < 100; j++) wThreads[j] = writers.next();
for (int j = 0; j < 100; j++) wThreads[j].start();
Reader reader = readers.next();
reader.start();
toTheStartingGate(gate);
reader.join();
for (int j = 0; j < 100; j++) wThreads[j].join();
for (int j = 0; j < 100; j++) checkResult(wThreads[j], null);
checkResult(reader, null);
}
} catch (Throwable t) {
unexpected(t);
}
//----------------------------------------------------------------
try {
StampedLock sl = new StampedLock();
Phaser gate = new Phaser(102);
Iterator<Writer> writers = writerIterator(sl, gate);
Iterator<Reader> readers = readerIterator(sl, gate);
for (int i = 0; i < 10; i++) {
check(!sl.isReadLocked());
check(!sl.isWriteLocked());
check(!sl.tryUnlockRead());
check(!sl.tryUnlockWrite());
check(sl.tryOptimisticRead() != 0L);
Locker[] rThreads = new Locker[100];
;
for (int j = 0; j < 100; j++) rThreads[j] = readers.next();
for (int j = 0; j < 100; j++) rThreads[j].start();
Writer writer = writers.next();
writer.start();
toTheStartingGate(gate);
writer.join();
for (int j = 0; j < 100; j++) rThreads[j].join();
for (int j = 0; j < 100; j++) checkResult(rThreads[j], null);
checkResult(writer, null);
}
} catch (Throwable t) {
unexpected(t);
}
//----------------------------------------------------------------
try {
boolean view = false;
StampedLock sl = new StampedLock();
for (long timeout : new long[] { -1L, 30L, -1L, 30L }) {
long stamp = sl.writeLock();
try {
Reader r = interruptibleReader(sl, timeout, SECONDS, null, view);
r.start();
// allow r to block
Thread.sleep(2000);
r.interrupt();
r.join();
checkResult(r, InterruptedException.class);
} finally {
sl.unlockWrite(stamp);
}
stamp = sl.readLock();
try {
Writer w = interruptibleWriter(sl, timeout, SECONDS, null, view);
w.start();
// allow w to block
Thread.sleep(2000);
w.interrupt();
w.join();
checkResult(w, InterruptedException.class);
} finally {
sl.unlockRead(stamp);
}
check(!sl.isReadLocked());
check(!sl.isWriteLocked());
check(!sl.tryUnlockRead());
check(!sl.tryUnlockWrite());
check(sl.tryOptimisticRead() != 0L);
if (timeout == 30L)
view = true;
}
} catch (Throwable t) {
unexpected(t);
}
//----------------------------------------------------------------
try {
StampedLock sl = new StampedLock();
for (long timeout : new long[] { 0L, 5L }) {
long stamp = sl.writeLock();
try {
check(sl.tryReadLock(timeout, SECONDS) == 0L);
} finally {
sl.unlockWrite(stamp);
}
stamp = sl.readLock();
try {
check(sl.tryWriteLock(timeout, SECONDS) == 0L);
} finally {
sl.unlockRead(stamp);
}
check(!sl.isReadLocked());
check(!sl.isWriteLocked());
check(!sl.tryUnlockRead());
check(!sl.tryUnlockWrite());
check(sl.tryOptimisticRead() != 0L);
}
} catch (Throwable t) {
unexpected(t);
}
//----------------------------------------------------------------
try {
StampedLock sl = new StampedLock();
Iterator<Writer> writers = writerIterator(sl, null);
Iterator<Reader> readers = readerIterator(sl, null);
for (int i = 0; i < 10; i++) {
check(!sl.isReadLocked());
check(!sl.isWriteLocked());
check(!sl.tryUnlockRead());
check(!sl.tryUnlockWrite());
long stamp = sl.tryOptimisticRead();
check(stamp != 0L);
check(sl.tryConvertToOptimisticRead(stamp) == stamp);
Reader r = readers.next();
r.start();
r.join();
checkResult(r, null);
check(sl.validate(stamp));
check(sl.tryConvertToOptimisticRead(stamp) == stamp);
Writer w = writers.next();
w.start();
w.join();
checkResult(w, null);
check(sl.validate(stamp) == false);
}
} catch (Throwable t) {
unexpected(t);
}
//----------------------------------------------------------------
try {
StampedLock sl = new StampedLock();
for (int i = 0; i < 2; i++) {
check(!sl.isReadLocked());
check(!sl.isWriteLocked());
check(!sl.tryUnlockRead());
check(!sl.tryUnlockWrite());
long stamp = sl.tryOptimisticRead();
check(stamp != 0L);
check((stamp = sl.tryConvertToReadLock(stamp)) != 0L);
check(sl.validate(stamp));
check(sl.isReadLocked());
check(sl.tryWriteLock() == 0L);
check(sl.tryWriteLock(1L, SECONDS) == 0L);
check((stamp = sl.tryConvertToWriteLock(stamp)) != 0L);
check(sl.validate(stamp));
check(!sl.isReadLocked());
check(sl.isWriteLocked());
check(sl.tryReadLock(1L, SECONDS) == 0L);
if (i != 0) {
sl.unlockWrite(stamp);
continue;
}
// convert down
check((stamp = sl.tryConvertToReadLock(stamp)) != 0L);
check(sl.validate(stamp));
check(sl.isReadLocked());
check(!sl.isWriteLocked());
check(sl.tryWriteLock() == 0L);
check(sl.tryWriteLock(1L, SECONDS) == 0L);
check((stamp = sl.tryConvertToOptimisticRead(stamp)) != 0L);
check(sl.validate(stamp));
check(!sl.isReadLocked());
check(!sl.isWriteLocked());
check(sl.validate(stamp));
}
} catch (Throwable t) {
unexpected(t);
}
//----------------------------------------------------------------
try {
StampedLock sl = new StampedLock();
Lock rl = sl.asReadLock();
Lock wl = sl.asWriteLock();
for (int i = 0; i < 2; i++) {
rl.lock();
try {
check(sl.isReadLocked());
check(!sl.isWriteLocked());
check(sl.tryWriteLock() == 0L);
check(sl.tryWriteLock(1L, SECONDS) == 0L);
} finally {
rl.unlock();
}
check(!sl.isReadLocked());
check(!sl.isWriteLocked());
wl.lock();
try {
check(!sl.isReadLocked());
check(sl.isWriteLocked());
check(sl.tryWriteLock() == 0L);
check(sl.tryWriteLock(1L, SECONDS) == 0L);
} finally {
wl.unlock();
}
check(!sl.isReadLocked());
check(!sl.isWriteLocked());
ReadWriteLock rwl = sl.asReadWriteLock();
rl = rwl.readLock();
wl = rwl.writeLock();
}
} catch (Throwable t) {
unexpected(t);
}
}
use of java.util.concurrent.locks.StampedLock in project jdk8u_jdk by JetBrains.
the class Basic method interruptibleReaderView.
static Reader interruptibleReaderView(final StampedLock sl, final long timeout, final TimeUnit unit, final Phaser gate) {
return new Reader("InterruptibleReaderView") {
public void run() {
if (gate != null)
toTheStartingGate(gate);
final Lock rl = sl.asReadLock();
try {
if (timeout < 0)
rl.lockInterruptibly();
else
rl.tryLock(timeout, unit);
// got the lock
stamp(1L);
check(sl.isReadLocked());
check(!sl.isWriteLocked());
} catch (Throwable x) {
thrown(x);
} finally {
if (stamp() != 0L)
rl.unlock();
}
}
};
}
use of java.util.concurrent.locks.StampedLock in project jdk8u_jdk by JetBrains.
the class Basic method interruptibleWriterView.
static Writer interruptibleWriterView(final StampedLock sl, final long timeout, final TimeUnit unit, final Phaser gate) {
return new Writer("InterruptibleWriterView") {
public void run() {
if (gate != null)
toTheStartingGate(gate);
Lock wl = sl.asWriteLock();
try {
if (timeout < 0)
wl.lockInterruptibly();
else
wl.tryLock(timeout, unit);
// got the lock
stamp(1L);
check(!sl.isReadLocked());
check(sl.isWriteLocked());
} catch (Throwable x) {
thrown(x);
} finally {
if (stamp() != 0L)
wl.unlock();
}
}
};
}
Aggregations