/** * {@inheritDoc} */ public Status getStatus() { return getState().getStatus(); }
/** * {@inheritDoc} */ public IOException getException() throws IllegalStateException { return getState().getException(); }
/** * {@inheritDoc} */ @SuppressWarnings({"unchecked"}) public T get() throws IOException, CancellationException { switch (await()) { case DONE: return getState().getResult(); case FAILED: throw getState().getException(); case CANCELLED: throw futureMsg.opCancelled(); default: throw new IllegalStateException(); } }
/** * {@inheritDoc} */ @SuppressWarnings({"unchecked"}) public T getInterruptibly() throws IOException, InterruptedException, CancellationException { switch (awaitInterruptibly()) { case DONE: return getState().getResult(); case FAILED: throw getState().getException(); case CANCELLED: throw futureMsg.opCancelled(); default: throw new IllegalStateException(); } }
/** * Acknowledge the cancellation of this operation. * * @return {@code false} if the operation was already completed, {@code true} otherwise */ protected boolean setCancelled() { State<T> oldState; oldState = getState(); if (oldState.getStatus() != Status.WAITING) { return false; } else { @SuppressWarnings("unchecked") State<T> newState = (State<T>) ST_CANCELLED; while (! compareAndSetState(oldState, newState)) { oldState = getState(); if (oldState.getStatus() != Status.WAITING) { return false; } } } oldState.notifyCancelled(this); return true; }
/** * {@inheritDoc} */ public <A> IoFuture<T> addNotifier(final Notifier<? super T, A> notifier, final A attachment) { State<T> oldState, newState; do { oldState = getState(); newState = oldState.withNotifier(getNotifierExecutor(), this, notifier, attachment); } while (! compareAndSetState(oldState, newState)); return this; }
State<T> state; for (;;) { state = getState(); if (state.getStatus() != Status.WAITING || duration == 0L) { return state.getStatus(); LockSupport.parkNanos(this, duration); if (Thread.interrupted()) throw new InterruptedException(); state = getState(); duration -= (tick = System.nanoTime()) - now; now = tick;
/** * Set the exception for this operation. Any threads blocking on this instance will be unblocked. * * @param exception the exception to set * @return {@code false} if the operation was already completed, {@code true} otherwise */ protected boolean setException(IOException exception) { State<T> oldState; oldState = getState(); if (oldState.getStatus() != Status.WAITING) { return false; } else { State<T> newState = new FailedState<T>(exception); while (! compareAndSetState(oldState, newState)) { oldState = getState(); if (oldState.getStatus() != Status.WAITING) { return false; } } } oldState.notifyFailed(this, exception); return true; }
/** * Set the result for this operation. Any threads blocking on this instance will be unblocked. * * @param result the result to set * @return {@code false} if the operation was already completed, {@code true} otherwise */ protected boolean setResult(T result) { State<T> oldState; oldState = getState(); if (oldState.getStatus() != Status.WAITING) { return false; } else { State<T> newState = new CompleteState<>(result); while (! compareAndSetState(oldState, newState)) { oldState = getState(); if (oldState.getStatus() != Status.WAITING) { return false; } } } oldState.notifyDone(this, result); return true; }
/** * {@inheritDoc} */ public Status awaitInterruptibly() throws InterruptedException { final Thread thread = Thread.currentThread(); State<T> state; for (;;) { state = getState(); if (state.getStatus() != Status.WAITING) { return state.getStatus(); } Xnio.checkBlockingAllowed(); if (Thread.interrupted()) throw new InterruptedException(); State<T> withWaiter = state.withWaiter(thread); if (compareAndSetState(state, withWaiter)) { do { LockSupport.park(this); if (Thread.interrupted()) throw new InterruptedException(); state = getState(); } while (state.getStatus() == Status.WAITING); return state.getStatus(); } // retry } }
/** * {@inheritDoc} */ public Status await() { final Thread thread = Thread.currentThread(); State<T> state; for (;;) { state = getState(); if (state.getStatus() != Status.WAITING) { return state.getStatus(); } Xnio.checkBlockingAllowed(); State<T> withWaiter = state.withWaiter(thread); if (compareAndSetState(state, withWaiter)) { boolean intr = Thread.interrupted(); try { do { LockSupport.park(this); if (Thread.interrupted()) intr = true; state = getState(); } while (state.getStatus() == Status.WAITING); return state.getStatus(); } finally { if (intr) thread.interrupt(); } } // retry } }
/** * Cancel an operation. The actual cancel may be synchronous or asynchronous. Implementers will use this method * to initiate the cancel; use the {@link #setCancelled()} method to indicate that the cancel was successful. The * default implementation calls any registered cancel handlers. * * @return this {@code IoFuture} instance */ public IoFuture<T> cancel() { State<T> state; do { state = getState(); if (state.getStatus() != Status.WAITING || state.cancelRequested()) return this; } while (! compareAndSetState(state, new CancelRequestedState<T>(state))); state.cancel(); return this; }
/** * Add a cancellation handler. The argument will be cancelled whenever this {@code IoFuture} is cancelled. If * the {@code IoFuture} is already cancelled when this method is called, the handler will be called directly. * * @param cancellable the cancel handler */ protected void addCancelHandler(final Cancellable cancellable) { State<T> oldState, newState; do { oldState = getState(); if (oldState.getStatus() != Status.WAITING || oldState.cancelRequested()) { try { cancellable.cancel(); } catch (Throwable ignored) { } return; } newState = oldState.withCancelHandler(cancellable); if (oldState == newState) return; } while (! compareAndSetState(oldState, newState)); }
/** * {@inheritDoc} */ public Status getStatus() { return getState().getStatus(); }
/** * {@inheritDoc} */ public IOException getException() throws IllegalStateException { return getState().getException(); }
/** * {@inheritDoc} */ @SuppressWarnings({"unchecked"}) public T get() throws IOException, CancellationException { switch (await()) { case DONE: return getState().getResult(); case FAILED: throw getState().getException(); case CANCELLED: throw futureMsg.opCancelled(); default: throw new IllegalStateException(); } }
/** * {@inheritDoc} */ @SuppressWarnings({"unchecked"}) public T getInterruptibly() throws IOException, InterruptedException, CancellationException { switch (awaitInterruptibly()) { case DONE: return getState().getResult(); case FAILED: throw getState().getException(); case CANCELLED: throw futureMsg.opCancelled(); default: throw new IllegalStateException(); } }
/** * {@inheritDoc} */ public <A> IoFuture<T> addNotifier(final Notifier<? super T, A> notifier, final A attachment) { State<T> oldState, newState; do { oldState = getState(); newState = oldState.withNotifier(getNotifierExecutor(), this, notifier, attachment); } while (! compareAndSetState(oldState, newState)); return this; }
/** * Cancel an operation. The actual cancel may be synchronous or asynchronous. Implementers will use this method * to initiate the cancel; use the {@link #setCancelled()} method to indicate that the cancel was successful. The * default implementation calls any registered cancel handlers. * * @return this {@code IoFuture} instance */ public IoFuture<T> cancel() { State<T> state; do { state = getState(); if (state.getStatus() != Status.WAITING || state.cancelRequested()) return this; } while (! compareAndSetState(state, new CancelRequestedState<T>(state))); state.cancel(); return this; }