Search in sources :

Example 16 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project CorfuDB by CorfuDB.

the class MultiReadWriteLock method acquireWriteLock.

/**
     * Acquire a write lock. The recommended use of this method is in try-with-resources statement.
     * @param address id of the lock to acquire.
     * @return A closable that will free the allocations for this lock if necessary
     */
public AutoCloseableLock acquireWriteLock(final Long address) {
    registerLockReference(address, true);
    ReentrantReadWriteLock lock = constructLockFor(address);
    final ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
    writeLock.lock();
    AtomicBoolean closed = new AtomicBoolean(false);
    return () -> {
        if (!closed.getAndSet(true)) {
            try {
                writeLock.unlock();
                clearEventuallyLockFor(address);
            } finally {
                deregisterLockReference(address, true);
            }
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 17 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project jgnash by ccavanaugh.

the class SecurityNode method postLoad.

@PostLoad
private void postLoad() {
    lock = new ReentrantReadWriteLock(true);
    // load the cache list
    sortedHistoryNodeCache = new ArrayList<>(historyNodes);
    // JPA will be naturally sorted, but XML files will not
    Collections.sort(sortedHistoryNodeCache);
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) PostLoad(javax.persistence.PostLoad)

Example 18 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project jgnash by ccavanaugh.

the class Account method postLoad.

@PostLoad
private void postLoad() {
    transactionLock = new ReentrantReadWriteLock(true);
    childLock = new ReentrantReadWriteLock(true);
    securitiesLock = new ReentrantReadWriteLock(true);
    attributesLock = new ReentrantReadWriteLock(true);
    cachedSortedChildren = new ArrayList<>(children);
    // JPA will be naturally sorted, but XML files will not
    Collections.sort(cachedSortedChildren);
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) PostLoad(javax.persistence.PostLoad)

Example 19 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project hbase by apache.

the class TestBucketCache method testMemoryLeak.

@Test
public void testMemoryLeak() throws Exception {
    final BlockCacheKey cacheKey = new BlockCacheKey("dummy", 1L);
    cacheAndWaitUntilFlushedToBucket(cache, cacheKey, new CacheTestUtils.ByteArrayCacheable(new byte[10]));
    long lockId = cache.backingMap.get(cacheKey).offset();
    ReentrantReadWriteLock lock = cache.offsetLock.getLock(lockId);
    lock.writeLock().lock();
    Thread evictThread = new Thread("evict-block") {

        @Override
        public void run() {
            cache.evictBlock(cacheKey);
        }
    };
    evictThread.start();
    cache.offsetLock.waitForWaiters(lockId, 1);
    cache.blockEvicted(cacheKey, cache.backingMap.remove(cacheKey), true);
    cacheAndWaitUntilFlushedToBucket(cache, cacheKey, new CacheTestUtils.ByteArrayCacheable(new byte[10]));
    lock.writeLock().unlock();
    evictThread.join();
    assertEquals(1L, cache.getBlockCount());
    assertTrue(cache.getCurrentSize() > 0L);
    assertTrue("We should have a block!", cache.iterator().hasNext());
}
Also used : CacheTestUtils(org.apache.hadoop.hbase.io.hfile.CacheTestUtils) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) BlockCacheKey(org.apache.hadoop.hbase.io.hfile.BlockCacheKey) Test(org.junit.Test)

Example 20 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project hbase by apache.

the class IdReadWriteLock method getLock.

/**
   * Get the ReentrantReadWriteLock corresponding to the given id
   * @param id an arbitrary number to identify the lock
   */
public ReentrantReadWriteLock getLock(long id) {
    lockPool.purge();
    ReentrantReadWriteLock readWriteLock = lockPool.get(id);
    return readWriteLock;
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Aggregations

ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)52 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)17 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)6 Lock (java.util.concurrent.locks.Lock)5 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)5 HashMap (java.util.HashMap)4 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)4 Nullable (org.jetbrains.annotations.Nullable)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 IOException (java.io.IOException)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 PostLoad (javax.persistence.PostLoad)2 Configuration (org.apache.hadoop.conf.Configuration)2 Path (org.apache.hadoop.fs.Path)2