/** * Acquires in shared mode, ignoring interrupts. Implemented by first invoking * at least once {@link #tryAcquireShared}, returning on success. Otherwise * the thread is queued, possibly repeatedly blocking and unblocking, invoking * {@link #tryAcquireShared} until success. * * @param arg * the acquire argument. This value is conveyed to * {@link #tryAcquireShared} but is otherwise uninterpreted and can * represent anything you like. */ public final void acquireShared(int arg, Object id) { if (tryAcquireShared(arg, id) < 0) doAcquireShared(arg, id); }
/** * Acquires in shared mode, aborting if interrupted. Implemented by first * checking interrupt status, then invoking at least once * {@link #tryAcquireShared}, returning on success. Otherwise the thread is * queued, possibly repeatedly blocking and unblocking, invoking * {@link #tryAcquireShared} until success or the thread is interrupted. * * @param arg * the acquire argument. This value is conveyed to * {@link #tryAcquireShared} but is otherwise uninterpreted and can * represent anything you like. * @throws InterruptedException * if the current thread is interrupted */ public final void acquireSharedInterruptibly(int arg, Object id) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); if (tryAcquireShared(arg, id) < 0) doAcquireSharedInterruptibly(arg, id); }
/** * Attempts to acquire in shared mode, aborting if interrupted, and failing if * the given timeout elapses. Implemented by first checking interrupt status, * then invoking at least once {@link #tryAcquireShared}, returning on * success. Otherwise, the thread is queued, possibly repeatedly blocking and * unblocking, invoking {@link #tryAcquireShared} until success or the thread * is interrupted or the timeout elapses. * * @param arg * the acquire argument. This value is conveyed to * {@link #tryAcquireShared} but is otherwise uninterpreted and can * represent anything you like. * @param nanosTimeout * the maximum number of nanoseconds to wait * @return {@code true} if acquired; {@code false} if timed out * @throws InterruptedException * if the current thread is interrupted */ public final boolean tryAcquireSharedNanos(int arg, Object id, long nanosTimeout) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); return tryAcquireShared(arg, id) >= 0 || doAcquireSharedNanos(arg, id, nanosTimeout); }
final Node p = node.predecessor(); if (p == head) { int r = tryAcquireShared(arg, id); if (r >= 0) { setHeadAndPropagate(node, r);
final Node p = node.predecessor(); if (p == head) { int r = tryAcquireShared(arg, id); if (r >= 0) { setHeadAndPropagate(node, r);
/** * Acquires in shared uninterruptible mode. * * @param arg * the acquire argument */ private void doAcquireShared(int arg, Object id) { final Node node = addWaiter(Node.SHARED); try { boolean interrupted = false; for (;;) { final Node p = node.predecessor(); if (p == head) { int r = tryAcquireShared(arg, id); if (r >= 0) { setHeadAndPropagate(node, r); p.next = null; // help GC if (interrupted) selfInterrupt(); return; } } if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) interrupted = true; } } catch (RuntimeException ex) { cancelAcquire(node); throw ex; } }