Java Code Examples for java.util.concurrent.locks.StampedLock#unlockRead()

The following examples show how to use java.util.concurrent.locks.StampedLock#unlockRead() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Multiple threads can hold a read lock when not write-locked
 */
public void testMultipleReadLocks() {
    final StampedLock lock = new StampedLock();
    final long s = lock.readLock();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            long s2 = lock.tryReadLock();
            assertValid(lock, s2);
            lock.unlockRead(s2);
            long s3 = lock.tryReadLock(LONG_DELAY_MS, MILLISECONDS);
            assertValid(lock, s3);
            lock.unlockRead(s3);
            long s4 = lock.readLock();
            assertValid(lock, s4);
            lock.unlockRead(s4);
            lock.asReadLock().lock();
            lock.asReadLock().unlock();
            lock.asReadLock().lockInterruptibly();
            lock.asReadLock().unlock();
            lock.asReadLock().tryLock(Long.MIN_VALUE, DAYS);
            lock.asReadLock().unlock();
        }});

    awaitTermination(t);
    lock.unlockRead(s);
}
 
Example 2
Source File: RegionRouteTable.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the list of regions to which the keys belongs.
 */
public Map<Region, List<byte[]>> findRegionsByKeys(final List<byte[]> keys) {
    Requires.requireNonNull(keys, "keys");
    final Map<Region, List<byte[]>> regionMap = Maps.newHashMap();
    final StampedLock stampedLock = this.stampedLock;
    final long stamp = stampedLock.readLock();
    try {
        for (final byte[] key : keys) {
            final Region region = findRegionByKeyWithoutLock(key);
            regionMap.computeIfAbsent(region, k -> Lists.newArrayList()).add(key);
        }
        return regionMap;
    } finally {
        stampedLock.unlockRead(stamp);
    }
}
 
Example 3
Source File: RegionRouteTable.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the list of regions to which the keys belongs.
 */
public Map<Region, List<KVEntry>> findRegionsByKvEntries(final List<KVEntry> kvEntries) {
    Requires.requireNonNull(kvEntries, "kvEntries");
    final Map<Region, List<KVEntry>> regionMap = Maps.newHashMap();
    final StampedLock stampedLock = this.stampedLock;
    final long stamp = stampedLock.readLock();
    try {
        for (final KVEntry kvEntry : kvEntries) {
            final Region region = findRegionByKeyWithoutLock(kvEntry.getKey());
            regionMap.computeIfAbsent(region, k -> Lists.newArrayList()).add(kvEntry);
        }
        return regionMap;
    } finally {
        stampedLock.unlockRead(stamp);
    }
}
 
Example 4
Source File: RegionRouteTable.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the startKey of next region.
 */
public byte[] findStartKeyOfNextRegion(final byte[] key) {
    Requires.requireNonNull(key, "key");
    final StampedLock stampedLock = this.stampedLock;
    long stamp = stampedLock.tryOptimisticRead();
    // get the least key strictly greater than the given key
    byte[] nextStartKey = this.rangeTable.higherKey(key);
    if (!stampedLock.validate(stamp)) {
        stamp = stampedLock.readLock();
        try {
            // get the least key strictly greater than the given key
            nextStartKey = this.rangeTable.higherKey(key);
        } finally {
            stampedLock.unlockRead(stamp);
        }
    }
    return nextStartKey;
}
 
Example 5
Source File: AbstractRegistryService.java    From Jupiter with Apache License 2.0 6 votes vote down vote up
@Override
public Map<ServiceMeta, Integer> consumers() {
    Map<ServiceMeta, Integer> result = Maps.newHashMap();
    for (Map.Entry<RegisterMeta.ServiceMeta, RegisterValue> entry : registries.entrySet()) {
        RegisterValue value = entry.getValue();
        final StampedLock stampedLock = value.lock;
        long stamp = stampedLock.tryOptimisticRead();
        int optimisticVal = value.metaSet.size();
        if (stampedLock.validate(stamp)) {
            result.put(entry.getKey(), optimisticVal);
            continue;
        }
        stamp = stampedLock.readLock();
        try {
            result.put(entry.getKey(), value.metaSet.size());
        } finally {
            stampedLock.unlockRead(stamp);
        }
    }
    return result;
}
 
Example 6
Source File: Basic.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
static Reader interruptibleReader(final StampedLock sl,
                                  final long timeout,
                                  final TimeUnit unit,
                                  final Phaser gate) {
    return new Reader("InterruptibleReader") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        try {
            if (timeout < 0)
                stamp(sl.readLockInterruptibly());
            else
                stamp(sl.tryReadLock(timeout, unit));
            check(sl.validate(stamp()));
            check(sl.isReadLocked());
            check(!sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) sl.unlockRead(stamp()); } }};
}
 
Example 7
Source File: Basic.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
static Reader interruptibleReader(final StampedLock sl,
                                  final long timeout,
                                  final TimeUnit unit,
                                  final Phaser gate) {
    return new Reader("InterruptibleReader") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        try {
            if (timeout < 0)
                stamp(sl.readLockInterruptibly());
            else
                stamp(sl.tryReadLock(timeout, unit));
            check(sl.validate(stamp()));
            check(sl.isReadLocked());
            check(!sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) sl.unlockRead(stamp()); } }};
}
 
Example 8
Source File: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * readLock() succeed only after a writing thread unlocks
 */
public void testReadAfterWriteLock() {
    final StampedLock lock = new StampedLock();
    final CountDownLatch threadsStarted = new CountDownLatch(2);
    final long s = lock.writeLock();
    final Runnable acquireReleaseReadLock = new CheckedRunnable() {
        public void realRun() {
            threadsStarted.countDown();
            long rs = lock.readLock();
            assertTrue(lock.isReadLocked());
            assertFalse(lock.isWriteLocked());
            lock.unlockRead(rs);
        }};
    Thread t1 = newStartedThread(acquireReleaseReadLock);
    Thread t2 = newStartedThread(acquireReleaseReadLock);

    await(threadsStarted);
    waitForThreadToEnterWaitState(t1);
    waitForThreadToEnterWaitState(t2);
    assertTrue(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    releaseWriteLock(lock, s);
    awaitTermination(t1);
    awaitTermination(t2);
    assertUnlocked(lock);
}
 
Example 9
Source File: Basic.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static Reader interruptibleReader(final StampedLock sl,
                                  final long timeout,
                                  final TimeUnit unit,
                                  final Phaser gate) {
    return new Reader("InterruptibleReader") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        try {
            if (timeout < 0)
                stamp(sl.readLockInterruptibly());
            else
                stamp(sl.tryReadLock(timeout, unit));
            check(sl.validate(stamp()));
            check(sl.isReadLocked());
            check(!sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) sl.unlockRead(stamp()); } }};
}
 
Example 10
Source File: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * writeLock() succeeds only after a reading thread unlocks
 */
public void testWriteAfterReadLock() throws InterruptedException {
    final CountDownLatch aboutToLock = new CountDownLatch(1);
    final StampedLock lock = new StampedLock();
    long rs = lock.readLock();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            aboutToLock.countDown();
            long s = lock.writeLock();
            assertTrue(lock.isWriteLocked());
            assertFalse(lock.isReadLocked());
            lock.unlockWrite(s);
        }});

    aboutToLock.await();
    waitForThreadToEnterWaitState(t);
    assertFalse(lock.isWriteLocked());
    assertTrue(lock.isReadLocked());
    lock.unlockRead(rs);
    awaitTermination(t);
    assertUnlocked(lock);
}
 
Example 11
Source File: Basic.java    From native-obfuscator with GNU General Public License v3.0 6 votes vote down vote up
static Reader interruptibleReader(final StampedLock sl,
                                  final long timeout,
                                  final TimeUnit unit,
                                  final Phaser gate) {
    return new Reader("InterruptibleReader") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        try {
            if (timeout < 0)
                stamp(sl.readLockInterruptibly());
            else
                stamp(sl.tryReadLock(timeout, unit));
            check(sl.validate(stamp()));
            check(sl.isReadLocked());
            check(!sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) sl.unlockRead(stamp()); } }};
}
 
Example 12
Source File: Basic.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static Reader reader(final StampedLock sl, final Phaser gate) {
    return new Reader() { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        stamp(sl.readLock());
        try {
            check(sl.validate(stamp()));
            check(sl.isReadLocked());
            check(!sl.isWriteLocked());
        } finally { sl.unlockRead(stamp()); } }};
}
 
Example 13
Source File: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Releases read lock, checking isReadLocked before and after
 */
void releaseReadLock(StampedLock lock, long stamp) {
    assertTrue(lock.isReadLocked());
    assertValid(lock, stamp);
    lock.unlockRead(stamp);
    assertFalse(lock.isReadLocked());
    assertTrue(lock.validate(stamp));
}
 
Example 14
Source File: Basic.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
static Reader reader(final StampedLock sl, final Phaser gate) {
    return new Reader() { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        stamp(sl.readLock());
        try {
            check(sl.validate(stamp()));
            check(sl.isReadLocked());
            check(!sl.isWriteLocked());
        } finally { sl.unlockRead(stamp()); } }};
}
 
Example 15
Source File: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * tryWriteLock fails when read locked
 */
public void testTryWriteLockWhenReadLocked() {
    final StampedLock lock = new StampedLock();
    long s = lock.readLock();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            threadAssertEquals(0L, lock.tryWriteLock());
        }});

    awaitTermination(t);
    lock.unlockRead(s);
}
 
Example 16
Source File: Basic.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static Reader reader(final StampedLock sl, final Phaser gate) {
    return new Reader() { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        stamp(sl.readLock());
        try {
            check(sl.validate(stamp()));
            check(sl.isReadLocked());
            check(!sl.isWriteLocked());
        } finally { sl.unlockRead(stamp()); } }};
}
 
Example 17
Source File: Basic.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
static Reader reader(final StampedLock sl, final Phaser gate) {
    return new Reader() { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        stamp(sl.readLock());
        try {
            check(sl.validate(stamp()));
            check(sl.isReadLocked());
            check(!sl.isWriteLocked());
        } finally { sl.unlockRead(stamp()); } }};
}
 
Example 18
Source File: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * tryReadLock succeeds if read locked but not write locked
 */
public void testTryLockWhenReadLocked() {
    final StampedLock lock = new StampedLock();
    long s = lock.readLock();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            long rs = lock.tryReadLock();
            assertValid(lock, rs);
            lock.unlockRead(rs);
        }});

    awaitTermination(t);
    lock.unlockRead(s);
}
 
Example 19
Source File: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * readLockInterruptibly succeeds if lock free
 */
public void testReadLockInterruptibly() throws InterruptedException {
    final StampedLock lock = new StampedLock();

    long s = assertValid(lock, lock.readLockInterruptibly());
    assertTrue(lock.isReadLocked());
    lock.unlockRead(s);

    lock.asReadLock().lockInterruptibly();
    assertTrue(lock.isReadLocked());
    lock.asReadLock().unlock();
}
 
Example 20
Source File: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * tryConvertToReadLock succeeds for valid stamps
 */
public void testTryConvertToReadLock() throws InterruptedException {
    StampedLock lock = new StampedLock();
    long s, p;

    assertEquals(0L, lock.tryConvertToReadLock(0L));

    s = assertValid(lock, lock.tryOptimisticRead());
    p = assertValid(lock, lock.tryConvertToReadLock(s));
    assertTrue(lock.isReadLocked());
    assertEquals(1, lock.getReadLockCount());
    assertTrue(lock.validate(s));
    lock.unlockRead(p);

    s = assertValid(lock, lock.tryOptimisticRead());
    lock.readLock();
    p = assertValid(lock, lock.tryConvertToReadLock(s));
    assertTrue(lock.isReadLocked());
    assertEquals(2, lock.getReadLockCount());
    lock.unlockRead(p);
    lock.unlockRead(p);
    assertUnlocked(lock);

    for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
        for (Function<StampedLock, Long> writeLocker : writeLockers()) {
            s = assertValid(lock, writeLocker.apply(lock));
            p = assertValid(lock, lock.tryConvertToReadLock(s));
            assertFalse(lock.validate(s));
            assertTrue(lock.isReadLocked());
            assertEquals(1, lock.getReadLockCount());
            readUnlocker.accept(lock, p);
        }

        for (Function<StampedLock, Long> readLocker : readLockers()) {
            s = assertValid(lock, readLocker.apply(lock));
            assertEquals(s, lock.tryConvertToReadLock(s));
            assertTrue(lock.validate(s));
            assertTrue(lock.isReadLocked());
            assertEquals(1, lock.getReadLockCount());
            readUnlocker.accept(lock, s);
        }
    }
}