/** Check if lock is in correct state (i.e. not broken in non-failoversafe mode), * if not throw {@linkplain IgniteInterruptedException} */ private void validate(final boolean throwInterrupt) { // Interrupted flag shouldn't be always cleared // (e.g. lock() method doesn't throw exception and doesn't clear interrupted) // but should be cleared if this method is called after lock breakage or node stop. // If interruptAll is set, exception is thrown anyway. boolean interrupted = Thread.currentThread().isInterrupted(); // Clear interrupt flag. if (throwInterrupt || interruptAll) Thread.interrupted(); if (interruptAll) throw new IgniteException("Lock broken (possible reason: node stopped" + " or node owning lock failed while in non-failoversafe mode)."); // Global queue should be synchronized only if interrupted exception should be thrown. if (fair && (throwInterrupt && interrupted) && !interruptAll) { synchronizeQueue(true, Thread.currentThread()); throw new IgniteInterruptedException("Lock is interrupted."); } }
synchronizeQueue(false, current);
/** {@inheritDoc} */ @Override public boolean tryLock(long timeout, TimeUnit unit) throws IgniteInterruptedException { ctx.kernalContext().gateway().readLock(); try{ initializeReentrantLock(); boolean result = sync.tryAcquireNanos(1, unit.toNanos(timeout)); sync.validate(true); return result; } catch (IgniteCheckedException e) { throw U.convertException(e); } catch (InterruptedException e) { if (sync.fair) sync.synchronizeQueue(true, Thread.currentThread()); throw new IgniteInterruptedException(e); } finally { ctx.kernalContext().gateway().readUnlock(); } }
synchronizeQueue(false, current);
/** {@inheritDoc} */ @Override public boolean tryLock(long timeout, TimeUnit unit) throws IgniteInterruptedException { ctx.kernalContext().gateway().readLock(); try{ initializeReentrantLock(); boolean result = sync.tryAcquireNanos(1, unit.toNanos(timeout)); sync.validate(true); return result; } catch (IgniteCheckedException e) { throw U.convertException(e); } catch (InterruptedException e) { if (sync.fair) sync.synchronizeQueue(true, Thread.currentThread()); throw new IgniteInterruptedException(e); } finally { ctx.kernalContext().gateway().readUnlock(); } }
/** {@inheritDoc} */ @Override public void lockInterruptibly() throws IgniteInterruptedException { ctx.kernalContext().gateway().readLock(); try { initializeReentrantLock(); sync.acquireInterruptibly(1); sync.validate(true); } catch (IgniteCheckedException e) { throw U.convertException(e); } catch (InterruptedException e) { if (sync.fair) sync.synchronizeQueue(true, Thread.currentThread()); throw new IgniteInterruptedException(e); } finally { ctx.kernalContext().gateway().readUnlock(); } }
/** Check if lock is in correct state (i.e. not broken in non-failoversafe mode), * if not throw {@linkplain IgniteInterruptedException} */ private void validate(final boolean throwInterrupt) { // Interrupted flag shouldn't be always cleared // (e.g. lock() method doesn't throw exception and doesn't clear interrupted) // but should be cleared if this method is called after lock breakage or node stop. // If interruptAll is set, exception is thrown anyway. boolean interrupted = Thread.currentThread().isInterrupted(); // Clear interrupt flag. if (throwInterrupt || interruptAll) Thread.interrupted(); if (interruptAll) throw new IgniteException("Lock broken (possible reason: node stopped" + " or node owning lock failed while in non-failoversafe mode)."); // Global queue should be synchronized only if interrupted exception should be thrown. if (fair && (throwInterrupt && interrupted) && !interruptAll) { synchronizeQueue(true, Thread.currentThread()); throw new IgniteInterruptedException("Lock is interrupted."); } }
/** {@inheritDoc} */ @Override public void lockInterruptibly() throws IgniteInterruptedException { ctx.kernalContext().gateway().readLock(); try { initializeReentrantLock(); sync.acquireInterruptibly(1); sync.validate(true); } catch (IgniteCheckedException e) { throw U.convertException(e); } catch (InterruptedException e) { if (sync.fair) sync.synchronizeQueue(true, Thread.currentThread()); throw new IgniteInterruptedException(e); } finally { ctx.kernalContext().gateway().readUnlock(); } }