use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.
the class IgniteLockAbstractSelfTest method testConditionAwaitUninterruptibly.
/**
* @throws Exception If failed.
*/
private void testConditionAwaitUninterruptibly(final boolean fair) throws Exception {
final IgniteLock lock0 = grid(0).reentrantLock("lock", true, fair, true);
assertEquals(0, lock0.getHoldCount());
assertFalse(lock0.hasQueuedThreads());
final int totalThreads = 2;
final Set<Thread> startedThreads = new GridConcurrentHashSet<>();
IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
assertFalse(lock0.isHeldByCurrentThread());
startedThreads.add(Thread.currentThread());
boolean isInterrupted = false;
lock0.lock();
IgniteCondition cond = lock0.getOrCreateCondition("cond");
try {
cond.awaitUninterruptibly();
} catch (IgniteInterruptedException ignored) {
isInterrupted = true;
} finally {
// Assert that thread was not interrupted.
assertFalse(isInterrupted);
// Assert that lock is still locked.
assertTrue(lock0.isLocked());
// Assert that this thread does own the lock.
assertTrue(lock0.isHeldByCurrentThread());
// Clear interrupt flag.
assertTrue(Thread.interrupted());
// Release lock.
if (lock0.isHeldByCurrentThread())
lock0.unlock();
}
return null;
}
}, totalThreads);
// Wait for all threads to attempt to acquire lock.
while (startedThreads.size() != totalThreads) {
Thread.sleep(500);
}
lock0.lock();
for (Thread t : startedThreads) {
t.interrupt();
lock0.getOrCreateCondition("cond").signal();
}
lock0.unlock();
fut.get();
assertFalse(lock0.isLocked());
for (Thread t : startedThreads) assertFalse(lock0.hasQueuedThread(t));
lock0.close();
}
use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.
the class GridCacheQueueAdapter method offer.
/** {@inheritDoc} */
@Override
public boolean offer(T item, long timeout, TimeUnit unit) throws IgniteException {
A.notNull(item, "item");
A.ensure(timeout >= 0, "Timeout cannot be negative: " + timeout);
if (!bounded()) {
boolean offer = offer(item);
assert offer;
return true;
}
long end = U.currentTimeMillis() + MILLISECONDS.convert(timeout, unit);
while (U.currentTimeMillis() < end) {
boolean retVal = false;
try {
if (writeSem.tryAcquire(end - U.currentTimeMillis(), MILLISECONDS)) {
checkStopping();
retVal = offer(item);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IgniteInterruptedException("Queue put interrupted.", e);
}
if (retVal)
return true;
}
return false;
}
use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.
the class GridCacheQueueAdapter method take.
/** {@inheritDoc} */
@Nullable
@Override
public T take() throws IgniteException {
while (true) {
try {
readSem.acquire();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IgniteInterruptedException("Queue take interrupted.", e);
}
checkStopping();
T e = poll();
if (e != null)
return e;
}
}
use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.
the class GridCacheSemaphoreImpl method tryAcquire.
/** {@inheritDoc} */
@Override
public boolean tryAcquire() {
ctx.kernalContext().gateway().readLock();
try {
initializeSemaphore();
boolean res = sync.nonfairTryAcquireShared(1) >= 0;
if (isBroken()) {
// Clear interrupt flag.
Thread.interrupted();
throw new InterruptedException();
}
return res;
} catch (IgniteCheckedException e) {
throw U.convertException(e);
} catch (InterruptedException e) {
throw new IgniteInterruptedException(e);
} finally {
ctx.kernalContext().gateway().readUnlock();
}
}
use of org.apache.ignite.IgniteInterruptedException in project ignite by apache.
the class GridCacheSemaphoreImpl method acquire.
/** {@inheritDoc} */
@Override
public void acquire(int permits) throws IgniteInterruptedException {
ctx.kernalContext().gateway().readLock();
A.ensure(permits >= 0, "Number of permits must be non-negative.");
try {
initializeSemaphore();
sync.acquireSharedInterruptibly(permits);
if (isBroken()) {
// Clear interrupt flag.
Thread.interrupted();
throw new InterruptedException();
}
} catch (IgniteCheckedException e) {
throw U.convertException(e);
} catch (InterruptedException e) {
throw new IgniteInterruptedException(e);
} finally {
ctx.kernalContext().gateway().readUnlock();
}
}
Aggregations