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();
}
}
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();
}
}
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();
}
}
}
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();
}
}
}
}
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();
}
}
}
Aggregations