Skip to content

Commit

Permalink
minimal impl
Browse files Browse the repository at this point in the history
  • Loading branch information
jrhee17 committed Sep 26, 2023
1 parent d8be51b commit af7537d
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public final class CancellationScheduler {
private static final AtomicLongFieldUpdater<CancellationScheduler> pendingTimeoutNanosUpdater =
AtomicLongFieldUpdater.newUpdater(CancellationScheduler.class, "pendingTimeoutNanos");

private static final AtomicLongFieldUpdater<CancellationScheduler> initializeTriggeredUpdater =
AtomicLongFieldUpdater.newUpdater(CancellationScheduler.class, "initializeTriggered");

private static final Runnable noopPendingTask = () -> {
};

Expand All @@ -86,6 +89,7 @@ public final class CancellationScheduler {
private volatile TimeoutFuture whenTimedOut;
@SuppressWarnings("FieldMayBeFinal")
private volatile long pendingTimeoutNanos;
private volatile long initializeTriggered;
private boolean server;
@Nullable
private Throwable cause;
Expand All @@ -99,6 +103,10 @@ public CancellationScheduler(long timeoutNanos) {
* Initializes this {@link CancellationScheduler}.
*/
public void init(EventExecutor eventLoop, CancellationTask task, long timeoutNanos, boolean server) {
if (!initializeTriggeredUpdater.compareAndSet(this, 0, 1)) {
// already cancelled or initialized
return;
}
if (!eventLoop.inEventLoop()) {
eventLoop.execute(() -> init0(eventLoop, task, timeoutNanos, server));
} else {
Expand Down Expand Up @@ -301,6 +309,15 @@ public void finishNow(@Nullable Throwable cause) {
if (isFinishing()) {
return;
}

if (initializeTriggeredUpdater.compareAndSet(this, 0, 1)) {
// finish immediately since init hasn't been called yet
state = State.FINISHED;
this.cause = cause;
((CancellationFuture) whenCancelled()).doComplete(cause);
return;
}

if (isInitialized()) {
if (eventLoop.inEventLoop()) {
finishNow0(cause);
Expand Down

0 comments on commit af7537d

Please sign in to comment.