/** * Internal lock routine. * * @param canInterrupt Whether to acquire the lock interruptibly. * @throws InterruptedException If interrupted. */ private void lock0(boolean canInterrupt) throws InterruptedException { int i = 0; try { for (; i < locks.length; i++) { if (canInterrupt) locks[i].writeLock().lockInterruptibly(); else locks[i].writeLock().lock(); } } catch (InterruptedException e) { unlock0(i - 1); throw e; } }
/** * Internal lock routine. * * @param canInterrupt Whether to acquire the lock interruptibly. * @throws InterruptedException If interrupted. */ private void lock0(boolean canInterrupt) throws InterruptedException { int i = 0; try { for (; i < locks.length; i++) { if (canInterrupt) locks[i].writeLock().lockInterruptibly(); else locks[i].writeLock().lock(); } } catch (InterruptedException e) { unlock0(i - 1); throw e; } }
/** {@inheritDoc} */ @SuppressWarnings("NullableProblems") @Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { int i = 0; long end = unit.toNanos(time) + System.nanoTime(); try { for (; i < locks.length && System.nanoTime() < end; i++) { if (!locks[i].writeLock().tryLock(time, unit)) break; } } finally { if (0 < i && i < locks.length) unlock0(i - 1); } return i == locks.length; }
/** {@inheritDoc} */ @Override public boolean tryLock() { int i = 0; try { for (; i < locks.length; i++) { if (!locks[i].writeLock().tryLock()) break; } } finally { if (0 < i && i < locks.length) unlock0(i - 1); } return i == locks.length; }
/** * Creates a new instance with given concurrency level. * * @param concurrencyLvl Number of internal read locks. */ public StripedCompositeReadWriteLock(int concurrencyLvl) { locks = new ReadLock[concurrencyLvl]; for (int i = 0; i < concurrencyLvl; i++) locks[i] = new ReadLock(); writeLock = new WriteLock(); }
/** {@inheritDoc} */ @Override public void lockInterruptibly() throws InterruptedException { lock0(true); }
/** {@inheritDoc} */ @Override public void unlock() { unlock0(locks.length - 1); }
/** {@inheritDoc} */ @Override public void lock() { try { lock0(false); } catch (InterruptedException ignore) { assert false : "Should never happen"; } }
/** {@inheritDoc} */ @Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { int i = 0; long end = unit.toNanos(time) + System.nanoTime(); try { for (; i < locks.length && System.nanoTime() < end; i++) { if (!locks[i].writeLock().tryLock(time, unit)) break; } } finally { if (0 < i && i < locks.length) unlock0(i - 1); } return i == locks.length; }
/** {@inheritDoc} */ @Override public boolean tryLock() { int i = 0; try { for (; i < locks.length; i++) { if (!locks[i].writeLock().tryLock()) break; } } finally { if (0 < i && i < locks.length) unlock0(i - 1); } return i == locks.length; }
/** * Creates a new instance with given concurrency level. * * @param concurrencyLvl Number of internal read locks. */ public StripedCompositeReadWriteLock(int concurrencyLvl) { locks = new ReadLock[concurrencyLvl]; for (int i = 0; i < concurrencyLvl; i++) locks[i] = new ReadLock(); writeLock = new WriteLock(); }
/** {@inheritDoc} */ @Override public void lock() { try { lock0(false); } catch (InterruptedException ignore) { assert false : "Should never happen"; } }
/** {@inheritDoc} */ @Override public void unlock() { unlock0(locks.length - 1); }
/** {@inheritDoc} */ @Override public void lockInterruptibly() throws InterruptedException { lock0(true); }