Search in sources :

Example 41 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project voltdb by VoltDB.

the class ForkJoinTask method clearExceptionalCompletion.

/**
     * Removes exception node and clears status.
     */
private void clearExceptionalCompletion() {
    int h = System.identityHashCode(this);
    final ReentrantLock lock = exceptionTableLock;
    lock.lock();
    try {
        ExceptionNode[] t = exceptionTable;
        int i = h & (t.length - 1);
        ExceptionNode e = t[i];
        ExceptionNode pred = null;
        while (e != null) {
            ExceptionNode next = e.next;
            if (e.get() == this) {
                if (pred == null)
                    t[i] = next;
                else
                    pred.next = next;
                break;
            }
            pred = e;
            e = next;
        }
        expungeStaleExceptions();
        status = 0;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 42 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project voltdb by VoltDB.

the class StateMachineSnipits method testLockingExample.

@Test
public void testLockingExample() {
    // Create a state machine manager operating in the LOCK_TESTER domain that will run on ZooKeeper0.
    // This manager is named manager1 and it expects a single state machine instance.
    SynchronizedStatesManager ssm1 = addStateMachineManagerFor(0, "LOCK_TESTER", "manager1", 1);
    // Create a state machine manager operating in the LOCK_TESTER domain that will run on ZooKeeper1.
    // This manager is named manager2 and it expects a single state machine instance.
    SynchronizedStatesManager ssm2 = addStateMachineManagerFor(1, "LOCK_TESTER", "manager2", 1);
    // This is a little convoluted, but because StateMachineInstances are a nested class of
    // SynchronizedStatesManager we must specify the correct SynchronizedStatesManager in the
    // constructor. The second parameter is the state machine name shared by all instances wishing
    // to participate in the same shared machine across Manager instances using the same
    // domain (LOCK_TESTER).
    DistributedLock distributedLock1 = new DistributedLock(ssm1, "lockingStateMachine");
    try {
        distributedLock1.start();
    } catch (InterruptedException e1) {
        fail();
    }
    DistributedLock distributedLock2 = new DistributedLock(ssm2, "lockingStateMachine");
    try {
        distributedLock2.start();
    } catch (InterruptedException e1) {
        fail();
    }
    while (!distributedLock1.isInitialized()) {
        Thread.yield();
    }
    while (!distributedLock2.isInitialized()) {
        Thread.yield();
    }
    distributedLock1.acquireDistriutedLock();
    Lock waiterForLock2 = new ReentrantLock();
    distributedLock2.acquireDistributedLockAsync(waiterForLock2);
    try {
        ssm2 = null;
        failSite(1);
        distributedLock1.releaseDistributedLock();
    } catch (Exception e) {
        fail();
    }
    // Create a state machine manager operating in the LOCK_TESTER domain that will run on ZooKeeper0.
    // This manager is named manager3 and it expects a single state machine instance.
    SynchronizedStatesManager ssm3 = addStateMachineManagerFor(0, "LOCK_TESTER", "manager3", 1);
    DistributedLock distributedLock3 = new DistributedLock(ssm3, "lockingStateMachine");
    try {
        distributedLock3.start();
    } catch (InterruptedException e1) {
        fail();
    }
    try {
        // release their locks.
        while (!distributedLock3.isInitialized()) {
            Thread.yield();
        }
        Lock waiterForLock3 = new ReentrantLock();
        distributedLock3.acquireDistributedLockAsync(waiterForLock3);
        synchronized (waiterForLock3) {
            while (!distributedLock3.m_locked) waiterForLock3.wait();
        }
        waiterForLock3 = null;
        distributedLock3.releaseDistributedLock();
        tearDownZK();
    } catch (Exception e) {
        fail();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Example 43 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project android_frameworks_base by ResurrectionRemix.

the class RenderAction method release.

/**
     * Cleans up the scene after an action.
     */
public void release() {
    ReentrantLock lock = Bridge.getLock();
    // not throw IllegalMonitorStateException.
    if (lock.isHeldByCurrentThread()) {
        tearDown();
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 44 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project android_frameworks_base by ResurrectionRemix.

the class CaptureCollector method failNextJpeg.

/**
     * Called to alert the {@link CaptureCollector} that the next pending jpeg capture has failed.
     */
public void failNextJpeg() {
    final ReentrantLock lock = this.mLock;
    lock.lock();
    try {
        CaptureHolder h1 = mJpegCaptureQueue.peek();
        CaptureHolder h2 = mJpegProduceQueue.peek();
        // Find the request with the lowest frame number.
        CaptureHolder h = (h1 == null) ? h2 : ((h2 == null) ? h1 : ((h1.compareTo(h2) <= 0) ? h1 : h2));
        if (h != null) {
            mJpegCaptureQueue.remove(h);
            mJpegProduceQueue.remove(h);
            mActiveRequests.remove(h);
            h.setJpegFailed();
        }
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 45 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project android_frameworks_base by ResurrectionRemix.

the class CaptureCollector method waitForEmpty.

/**
     * Wait all queued requests to complete.
     *
     * @param timeout a timeout to use for this call.
     * @param unit the units to use for the timeout.
     * @return {@code false} if this method timed out.
     * @throws InterruptedException if this thread is interrupted.
     */
public boolean waitForEmpty(long timeout, TimeUnit unit) throws InterruptedException {
    long nanos = unit.toNanos(timeout);
    final ReentrantLock lock = this.mLock;
    lock.lock();
    try {
        while (mInFlight > 0) {
            if (nanos <= 0) {
                return false;
            }
            nanos = mIsEmpty.awaitNanos(nanos);
        }
        return true;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Aggregations

ReentrantLock (java.util.concurrent.locks.ReentrantLock)1524 Lock (java.util.concurrent.locks.Lock)125 Test (org.junit.Test)78 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)74 Condition (java.util.concurrent.locks.Condition)57 ArrayList (java.util.ArrayList)45 IOException (java.io.IOException)42 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)27 File (java.io.File)22 TimeoutException (java.util.concurrent.TimeoutException)19 Before (org.junit.Before)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)17 HashMap (java.util.HashMap)16 CountDownLatch (java.util.concurrent.CountDownLatch)16 ExecutionException (java.util.concurrent.ExecutionException)15 List (java.util.List)14 AtomicLong (java.util.concurrent.atomic.AtomicLong)14 Map (java.util.Map)11 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)11 Pair (android.util.Pair)10