/** * Tests whether the current strand has been interrupted. The * <i>interrupted status</i> of the strand is cleared by this method. In * other words, if this method were to be called twice in succession, the * second call would return {@code false} (unless the current strand were * interrupted again, after the first call had cleared its interrupted * status and before the second call had examined it). * * @return {@code true} if the current thread has been interrupted; {@code false} otherwise. */ public static boolean interrupted() { return Strand.interrupted(); }
/** * Convenience method to park and then check if interrupted * * @return {@code true} if interrupted */ private boolean parkAndCheckInterrupt() throws SuspendExecution { Strand.park(this); return Strand.interrupted(); }
/** * Checks for interrupt, returning THROW_IE if interrupted * before signalled, REINTERRUPT if after signalled, or * 0 if not interrupted. */ private int checkInterruptWhileWaiting(Node node) throws SuspendExecution { return Strand.interrupted() ? (transferAfterCancelledWait(node) ? THROW_IE : REINTERRUPT) : 0; }
/** * Convenience method to park and then check if interrupted * * @return {@code true} if interrupted */ private final boolean parkAndCheckInterrupt() throws SuspendExecution { Strand.park(this); return Strand.interrupted(); }
/** * Checks for interrupt, returning THROW_IE if interrupted * before signalled, REINTERRUPT if after signalled, or * 0 if not interrupted. */ private int checkInterruptWhileWaiting(Node node) throws SuspendExecution { return Strand.interrupted() ? (transferAfterCancelledWait(node) ? THROW_IE : REINTERRUPT) : 0; }
@Override public boolean send(Message message, long timeout, TimeUnit unit) throws SuspendExecution, InterruptedException { if (message == null) throw new IllegalArgumentException("message is null"); if (isSendClosed()) return true; if (xfer1(message, true, TIMED, unit.toNanos(timeout)) == null) return true; if (!Strand.interrupted()) return false; throw new InterruptedException(); }
public boolean isReleasable() { if (strand == null) return true; if (phaser.getPhase() != phase) { strand = null; return true; } if (Strand.interrupted()) wasInterrupted = true; if (wasInterrupted && interruptible) { strand = null; return true; } if (timed) { if (nanos > 0L) { long now = System.nanoTime(); nanos -= now - lastTime; lastTime = now; } if (nanos <= 0L) { strand = null; return true; } } return false; }
protected Message receiveInternal(long timeout, TimeUnit unit) throws SuspendExecution, InterruptedException { if (receiveClosed) return closeValue(); Object m = xfer1(null, false, TIMED, unit.toNanos(timeout)); if (m != null || !Strand.interrupted()) { if (m == CHANNEL_CLOSED) return closeValue(); return (Message) m; } throw new InterruptedException(); }
/** * Exclusively acquires the lock, blocking if necessary * until available or the current strand is interrupted. * Behavior under interruption matches that specified * for method {@link Lock#lockInterruptibly()}. * * @return a stamp that can be used to unlock or convert mode * @throws InterruptedException if the current strand is interrupted * before acquiring the lock */ @Suspendable public long writeLockInterruptibly() throws InterruptedException { try { long next; if (!Strand.interrupted() && (next = acquireWrite(true, 0L)) != INTERRUPTED) return next; throw new InterruptedException(); } catch (SuspendExecution e) { throw new AssertionError(); } }
/** * Non-exclusively acquires the lock, blocking if necessary * until available or the current strand is interrupted. * Behavior under interruption matches that specified * for method {@link Lock#lockInterruptibly()}. * * @return a stamp that can be used to unlock or convert mode * @throws InterruptedException if the current strand is interrupted * before acquiring the lock */ @Suspendable public long readLockInterruptibly() throws InterruptedException { try { long next; if (!Strand.interrupted() && (next = acquireRead(true, 0L)) != INTERRUPTED) return next; throw new InterruptedException(); } catch (SuspendExecution e) { throw new AssertionError(); } }
/** * Acquires in exclusive mode, aborting if interrupted. * Implemented by first checking interrupt status, then invoking * at least once {@link #tryAcquire}, returning on * success. Otherwise the strand is queued, possibly repeatedly * blocking and unblocking, invoking {@link #tryAcquire} * until success or the strand is interrupted. This method can be * used to implement method {@link Lock#lockInterruptibly}. * * @param arg the acquire argument. This value is conveyed to * {@link #tryAcquire} but is otherwise uninterpreted and * can represent anything you like. * @throws InterruptedException if the current strand is interrupted */ public final void acquireInterruptibly(long arg) throws InterruptedException, SuspendExecution { if (Strand.interrupted()) throw new InterruptedException(); if (!tryAcquire(arg)) doAcquireInterruptibly(arg); }
/** * Attempts to acquire in exclusive mode, aborting if interrupted, * and failing if the given timeout elapses. Implemented by first * checking interrupt status, then invoking at least once {@link * #tryAcquire}, returning on success. Otherwise, the strand is * queued, possibly repeatedly blocking and unblocking, invoking * {@link #tryAcquire} until success or the strand is interrupted * or the timeout elapses. This method can be used to implement * method {@link Lock#tryLock(long, TimeUnit)}. * * @param arg the acquire argument. This value is conveyed to * {@link #tryAcquire} 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 strand is interrupted */ public final boolean tryAcquireNanos(long arg, long nanosTimeout) throws InterruptedException, SuspendExecution { if (Strand.interrupted()) throw new InterruptedException(); return tryAcquire(arg) || doAcquireNanos(arg, nanosTimeout); }
/** * 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 * strand is queued, possibly repeatedly blocking and unblocking, * invoking {@link #tryAcquireShared} until success or the strand * 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 strand is interrupted */ @Suspendable public final boolean tryAcquireSharedNanos(long arg, long nanosTimeout) throws InterruptedException { try { if (Strand.interrupted()) throw new InterruptedException(); return tryAcquireShared(arg) >= 0 || doAcquireSharedNanos(arg, nanosTimeout); } catch (SuspendExecution e) { throw new AssertionError(); } }
/** * 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 * strand is queued, possibly repeatedly blocking and unblocking, * invoking {@link #tryAcquireShared} until success or the strand * 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 strand is interrupted */ @Suspendable public final void acquireSharedInterruptibly(int arg) throws InterruptedException { try { if (Strand.interrupted()) throw new InterruptedException(); if (tryAcquireShared(arg) < 0) doAcquireSharedInterruptibly(arg); } catch (SuspendExecution e) { throw new AssertionError(); } }
/** * Acquires in exclusive mode, aborting if interrupted. * Implemented by first checking interrupt status, then invoking * at least once {@link #tryAcquire}, returning on * success. Otherwise the strand is queued, possibly repeatedly * blocking and unblocking, invoking {@link #tryAcquire} * until success or the strand is interrupted. This method can be * used to implement method {@link Lock#lockInterruptibly}. * * @param arg the acquire argument. This value is conveyed to * {@link #tryAcquire} but is otherwise uninterpreted and * can represent anything you like. * @throws InterruptedException if the current strand is interrupted */ @Suspendable public final void acquireInterruptibly(int arg) throws InterruptedException { try { if (Strand.interrupted()) throw new InterruptedException(); if (!tryAcquire(arg)) doAcquireInterruptibly(arg); } catch (SuspendExecution e) { throw new AssertionError(); } }
/** * 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 * strand is queued, possibly repeatedly blocking and unblocking, * invoking {@link #tryAcquireShared} until success or the strand * 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 strand is interrupted */ @Suspendable public final void acquireSharedInterruptibly(long arg) throws InterruptedException { try { if (Strand.interrupted()) throw new InterruptedException(); if (tryAcquireShared(arg) < 0) doAcquireSharedInterruptibly(arg); } catch (SuspendExecution e) { throw new AssertionError(); } }
/** * Attempts to acquire in exclusive mode, aborting if interrupted, * and failing if the given timeout elapses. Implemented by first * checking interrupt status, then invoking at least once {@link * #tryAcquire}, returning on success. Otherwise, the strand is * queued, possibly repeatedly blocking and unblocking, invoking * {@link #tryAcquire} until success or the strand is interrupted * or the timeout elapses. This method can be used to implement * method {@link Lock#tryLock(long, TimeUnit)}. * * @param arg the acquire argument. This value is conveyed to * {@link #tryAcquire} 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 strand is interrupted */ @Suspendable public final boolean tryAcquireNanos(int arg, long nanosTimeout) throws InterruptedException { try { if (Strand.interrupted()) throw new InterruptedException(); return tryAcquire(arg) || doAcquireNanos(arg, nanosTimeout); } catch (SuspendExecution e) { throw new AssertionError(); } }
public void awaitNanos(int iter, long timeoutNanos) throws InterruptedException, SuspendExecution { final int spins; final Fiber<?> fib = Fiber.currentFiber(); if (fib != null) { spins = 0; if (iter > 0) fib.getMonitor().spuriousWakeup(); } else spins = SPINS - iter; if (spins > 0) { if (ThreadLocalRandom.current().nextInt(SPINS) == 0) Strand.yield(); } else { if (isRecording()) record("await", "%s parking", Strand.currentStrand()); Strand.parkNanos(owner, timeoutNanos); if (isRecording()) record("await", "%s awoke", Strand.currentStrand()); } if (Strand.interrupted()) throw new InterruptedException(); }
@Override public void await(int iter) throws InterruptedException, SuspendExecution { final int spins; final Fiber<?> fib = Fiber.currentFiber(); if (fib != null) { spins = 0; if (iter > 0) fib.getMonitor().spuriousWakeup(); } else spins = SPINS - iter; if (spins > 0) { if (ThreadLocalRandom.current().nextInt(SPINS) == 0) Strand.yield(); } else { if (isRecording()) record("await", "%s parking", Strand.currentStrand()); Strand.park(owner); if (isRecording()) record("await", "%s awoke", Strand.currentStrand()); } if (Strand.interrupted()) throw new InterruptedException(); }
/** * Implements uninterruptible condition wait. * <ol> * <li> Save lock state returned by {@link #getState}. * <li> Invoke {@link #release} with saved state as argument, * throwing IllegalMonitorStateException if it fails. * <li> Block until signalled. * <li> Reacquire by invoking specialized version of * {@link #acquire} with saved state as argument. * </ol> */ @Suspendable public final void awaitUninterruptibly() { try { Node node = addConditionWaiter(); long savedState = fullyRelease(node); boolean interrupted = false; while (!isOnSyncQueue(node)) { Strand.park(this); if (Strand.interrupted()) interrupted = true; } if (acquireQueued(node, savedState) || interrupted) selfInterrupt(); } catch (SuspendExecution e) { throw new AssertionError(); } }