Search in sources :

Example 36 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project XobotOS by xamarin.

the class LinkedBlockingQueue method peek.

public E peek() {
    if (count.get() == 0)
        return null;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        Node<E> first = head.next;
        if (first == null)
            return null;
        else
            return first.item;
    } finally {
        takeLock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 37 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project XobotOS by xamarin.

the class LinkedBlockingQueue method drainTo.

/**
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws ClassCastException            {@inheritDoc}
     * @throws NullPointerException          {@inheritDoc}
     * @throws IllegalArgumentException      {@inheritDoc}
     */
public int drainTo(Collection<? super E> c, int maxElements) {
    if (c == null)
        throw new NullPointerException();
    if (c == this)
        throw new IllegalArgumentException();
    boolean signalNotFull = false;
    final ReentrantLock takeLock = this.takeLock;
    takeLock.lock();
    try {
        int n = Math.min(maxElements, count.get());
        // count.get provides visibility to first n Nodes
        Node<E> h = head;
        int i = 0;
        try {
            while (i < n) {
                Node<E> p = h.next;
                c.add(p.item);
                p.item = null;
                h.next = h;
                h = p;
                ++i;
            }
            return n;
        } finally {
            // Restore invariants even if c.add() threw
            if (i > 0) {
                // assert h.item == null;
                head = h;
                signalNotFull = (count.getAndAdd(-i) == capacity);
            }
        }
    } finally {
        takeLock.unlock();
        if (signalNotFull)
            signalNotFull();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 38 with ReentrantLock

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

the class Monitor method enterWhen.

/**
   * Enters this monitor when the guard is satisfied. Blocks indefinitely, but may be interrupted.
   *
   * @throws InterruptedException if interrupted while waiting
   */
public void enterWhen(Guard guard) throws InterruptedException {
    if (guard.monitor != this) {
        throw new IllegalMonitorStateException();
    }
    final ReentrantLock lock = this.lock;
    boolean signalBeforeWaiting = lock.isHeldByCurrentThread();
    lock.lockInterruptibly();
    boolean satisfied = false;
    try {
        if (!guard.isSatisfied()) {
            await(guard, signalBeforeWaiting);
        }
        satisfied = true;
    } finally {
        if (!satisfied) {
            leave();
        }
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 39 with ReentrantLock

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

the class Monitor method enterWhen.

/**
   * Enters this monitor when the guard is satisfied. Blocks at most the given time, including both
   * the time to acquire the lock and the time to wait for the guard to be satisfied, and may be
   * interrupted.
   *
   * @return whether the monitor was entered, which guarantees that the guard is now satisfied
   * @throws InterruptedException if interrupted while waiting
   */
public boolean enterWhen(Guard guard, long time, TimeUnit unit) throws InterruptedException {
    final long timeoutNanos = toSafeNanos(time, unit);
    if (guard.monitor != this) {
        throw new IllegalMonitorStateException();
    }
    final ReentrantLock lock = this.lock;
    boolean reentrant = lock.isHeldByCurrentThread();
    long startTime = 0L;
    locked: {
        if (!fair) {
            // Check interrupt status to get behavior consistent with fair case.
            if (Thread.interrupted()) {
                throw new InterruptedException();
            }
            if (lock.tryLock()) {
                break locked;
            }
        }
        startTime = initNanoTime(timeoutNanos);
        if (!lock.tryLock(time, unit)) {
            return false;
        }
    }
    boolean satisfied = false;
    boolean threw = true;
    try {
        satisfied = guard.isSatisfied() || awaitNanos(guard, (startTime == 0L) ? timeoutNanos : remainingNanos(startTime, timeoutNanos), reentrant);
        threw = false;
        return satisfied;
    } finally {
        if (!satisfied) {
            try {
                // Don't need to signal if timed out, but do if interrupted
                if (threw && !reentrant) {
                    signalNextWaiter();
                }
            } finally {
                lock.unlock();
            }
        }
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 40 with ReentrantLock

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

the class Monitor method enterWhenUninterruptibly.

/**
   * Enters this monitor when the guard is satisfied. Blocks indefinitely.
   */
public void enterWhenUninterruptibly(Guard guard) {
    if (guard.monitor != this) {
        throw new IllegalMonitorStateException();
    }
    final ReentrantLock lock = this.lock;
    boolean signalBeforeWaiting = lock.isHeldByCurrentThread();
    lock.lock();
    boolean satisfied = false;
    try {
        if (!guard.isSatisfied()) {
            awaitUninterruptibly(guard, signalBeforeWaiting);
        }
        satisfied = true;
    } finally {
        if (!satisfied) {
            leave();
        }
    }
}
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