Search in sources :

Example 46 with ReentrantReadWriteLock

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

the class ReadWriteLockMultiThreadedTest method testNotOwnedLockRelease.

/**
     *
     */
public void testNotOwnedLockRelease() {
    ReadWriteLock lock = new ReentrantReadWriteLock();
    lock.readLock().unlock();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 47 with ReentrantReadWriteLock

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

the class ReadWriteLockMultiThreadedTest method testReadThenWriteLockAcquire.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased" })
public void testReadThenWriteLockAcquire() throws Exception {
    ReadWriteLock lock = new ReentrantReadWriteLock();
    lock.readLock().lock();
    lock.writeLock().lock();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 48 with ReentrantReadWriteLock

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

the class ReadWriteLockMultiThreadedTest method testWriteLockAcquire.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased" })
public void testWriteLockAcquire() throws Exception {
    final ReadWriteLock lock = new ReentrantReadWriteLock();
    lock.readLock().lock();
    X.println("Read lock acquired.");
    IgniteInternalFuture fut1 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            X.println("Attempting to acquire write lock: " + lock);
            lock.writeLock().lock();
            try {
                X.println("Write lock acquired: " + lock);
                return null;
            } finally {
                lock.writeLock().unlock();
            }
        }
    }, 1, "write-lock");
    Thread.sleep(2000);
    IgniteInternalFuture fut2 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            X.println("Attempting to acquire read lock: " + lock);
            lock.readLock().lock();
            try {
                X.println("Read lock acquired: " + lock);
                return null;
            } finally {
                lock.readLock().unlock();
            }
        }
    }, 1, "read-lock");
    Thread.sleep(2000);
    X.println(">>> Dump threads now! <<<");
    Thread.sleep(15 * 1000);
    X.println("Read lock released: " + lock);
    lock.readLock().unlock();
    fut1.get();
    fut2.get();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Nullable(org.jetbrains.annotations.Nullable)

Example 49 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project jackrabbit-oak by apache.

the class DocumentNodeStoreTest method nonBlockingReset.

// OAK-2620
@Test
public void nonBlockingReset() throws Exception {
    final List<String> failure = Lists.newArrayList();
    final AtomicReference<ReentrantReadWriteLock> mergeLock = new AtomicReference<ReentrantReadWriteLock>();
    MemoryDocumentStore store = new MemoryDocumentStore() {

        @Override
        public <T extends Document> T findAndUpdate(Collection<T> collection, UpdateOp update) {
            for (Map.Entry<Key, Operation> entry : update.getChanges().entrySet()) {
                if (entry.getKey().getName().equals(NodeDocument.COLLISIONS)) {
                    ReentrantReadWriteLock rwLock = mergeLock.get();
                    if (rwLock.getReadHoldCount() > 0 || rwLock.getWriteHoldCount() > 0) {
                        failure.add("Branch reset still holds merge lock");
                        break;
                    }
                }
            }
            return super.findAndUpdate(collection, update);
        }
    };
    DocumentNodeStore ds = builderProvider.newBuilder().setDocumentStore(store).setAsyncDelay(0).getNodeStore();
    // do not retry merges
    ds.setMaxBackOffMillis(0);
    DocumentNodeState root = ds.getRoot();
    final DocumentNodeStoreBranch b = ds.createBranch(root);
    // branch state is now Unmodified
    assertTrue(b.getMergeLock() instanceof ReentrantReadWriteLock);
    mergeLock.set((ReentrantReadWriteLock) b.getMergeLock());
    NodeBuilder builder = root.builder();
    builder.child("foo");
    b.setRoot(builder.getNodeState());
    // branch state is now InMemory
    builder.child("bar");
    b.setRoot(builder.getNodeState());
    try {
        b.merge(new CommitHook() {

            @Nonnull
            @Override
            public NodeState processCommit(NodeState before, NodeState after, CommitInfo info) throws CommitFailedException {
                NodeBuilder foo = after.builder().child("foo");
                for (int i = 0; i <= DocumentMK.UPDATE_LIMIT; i++) {
                    foo.setProperty("prop", i);
                }
                throw new CommitFailedException("Fail", 0, "");
            }
        }, CommitInfo.EMPTY);
    } catch (CommitFailedException e) {
    // expected
    }
    for (String s : failure) {
        fail(s);
    }
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) MemoryDocumentStore(org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore) Nonnull(javax.annotation.Nonnull) CommitHook(org.apache.jackrabbit.oak.spi.commit.CommitHook) AtomicReference(java.util.concurrent.atomic.AtomicReference) Operation(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Operation) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder) CommitInfo(org.apache.jackrabbit.oak.spi.commit.CommitInfo) Map(java.util.Map) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) Key(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Key) Test(org.junit.Test)

Example 50 with ReentrantReadWriteLock

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

the class TestTDBInternal method exclusive_1.

@Test
public void exclusive_1() {
    DatasetGraph dsg = TDBFactory.createDatasetGraph();
    TransactionManager txnmgr = TDBInternal.getTransactionManager(dsg);
    checkTxnMgr(txnmgr, 0, 0);
    ReentrantReadWriteLock rwx = (ReentrantReadWriteLock) txnmgr.getExclusivityLock$();
    checkLock(rwx, 0, 0);
    dsg.begin(ReadWrite.WRITE);
    // Exclusivity reader lock.
    checkLock(rwx, 1, 0);
    checkTxnMgr(txnmgr, 0, 1);
    dsg.commit();
    // Exclusivity reader lock.
    checkLock(rwx, 0, 0);
    checkTxnMgr(txnmgr, 0, 0);
    dsg.end();
    // Exclusivity reader lock.
    checkLock(rwx, 0, 0);
    checkTxnMgr(txnmgr, 0, 0);
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

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