Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/hxcoro/Coro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hxcoro;

import haxe.coro.IContinuation;
import haxe.coro.SuspensionResult;
import haxe.coro.context.ExceptionHandler;
import haxe.coro.dispatchers.Dispatcher;
import haxe.exceptions.ArgumentException;
import haxe.exceptions.CancellationException;
Expand All @@ -16,27 +15,25 @@ import hxcoro.task.NodeLambda;
private typedef SuspendCancellableFunc<T> = IContinuation<T> -> Null<(CancellationException -> Void)>;

class Coro {
@:coroutine(transformed, outcome = { noThrow: true, noReturn: true })
@:coroutine(transformed)
public static function suspend<T>(completion:IContinuation<T>, func:IContinuation<T>->Void):SuspensionResult<T> {
var safe = new RacingContinuation(completion);
func(safe);
safe.resolve();
return cast SuspensionResult.suspended;
return safe.resolve();
}

/**
* Suspends a coroutine which will be automatically resumed with a `haxe.exceptions.CancellationException` when cancelled.
* If `func` returns a callback, it is registered to be invoked on cancellation allowing the easy cleanup of resources.
*/
@:coroutine(transformed, outcome = { noThrow: true, noReturn: true })
@:coroutine(transformed)
public static function suspendCancellable<T>(completion:IContinuation<T>, func:SuspendCancellableFunc<T>):SuspensionResult<T> {
var safe = new CancellingContinuation(completion);
final onCancellationRequested = func(safe);
if (onCancellationRequested != null) {
safe.onCancellationRequested = onCancellationRequested;
}
safe.resolve();
return cast SuspensionResult.suspended;
return safe.resolve();
}

static function delayImpl<T>(ms:Int, cont:IContinuation<T>) {
Expand All @@ -48,12 +45,12 @@ class Coro {
}
}

@:coroutine(outcome = { noThrow: true, noReturn: true }, assert = { numStates: 1})
@:coroutine(assert = { numStates: 1})
public static function delay(ms:Int):Void {
suspendCancellable(cont -> delayImpl(ms, cont));
}

@:coroutine(outcome = { noThrow: true, noReturn: true }, assert = { numStates: 1})
@:coroutine(assert = { numStates: 1})
public static function yield():Void {
suspendCancellable(cont -> delayImpl(0, cont));
}
Expand Down
15 changes: 6 additions & 9 deletions src/hxcoro/continuations/CancellingContinuation.hx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package hxcoro.continuations;

import haxe.Exception;
import haxe.coro.IContinuation;
import haxe.coro.SuspensionResult;
import haxe.coro.cancellation.CancellationToken;
import haxe.coro.cancellation.ICancellationCallback;
import haxe.coro.cancellation.ICancellationHandle;
import haxe.coro.cancellation.ICancellationToken;
import haxe.coro.dispatchers.Dispatcher;
import haxe.coro.dispatchers.IDispatchObject;
import haxe.exceptions.CancellationException;
import hxcoro.concurrent.AtomicInt;
Expand Down Expand Up @@ -103,16 +102,14 @@ class CancellingContinuation<T> extends StackFrameContinuation<T> implements ICa
}
}

public function resolve():Void {
if (resumeState.compareExchange(Active, Resolved) != Active) {
public function resolve():SuspensionResult<T> {
if (resumeState.compareExchange(Active, Resolved) == Active) {
return cast SuspensionResult.suspended;
} else {
while (resumeState.load() == Completing) {
BackOff.backOff();
}
// Resume (or cancellation) beat resolve(). Dispatch so that onDispatch() →
// cont.resume() is always called, regardless of whether the caller was a
// BaseContinuation state machine or a plain lambda (where an inline Returned
// result would be silently discarded on multi-threaded targets).
context.dispatchOrCall(this);
return this;
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/hxcoro/continuations/RacingContinuation.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package hxcoro.continuations;

import haxe.Exception;
import haxe.coro.IContinuation;
import haxe.coro.dispatchers.Dispatcher;
import haxe.coro.SuspensionResult;
import haxe.coro.dispatchers.IDispatchObject;
import hxcoro.concurrent.AtomicInt;

Expand All @@ -29,11 +29,11 @@ class RacingContinuation<T> extends StackFrameContinuation<T> implements IDispat
}
}

public function resolve():Void {
public function resolve():SuspensionResult<T> {
if (resumeState.compareExchange(Active, Resolved) == Active) {
state = Pending;
return cast SuspensionResult.suspended;
} else {
context.dispatchOrCall(this);
return this;
}
}

Expand Down