Skip to content
Merged
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
56 changes: 47 additions & 9 deletions src/hxcoro/dispatchers/LuvDispatcher.cpp.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package hxcoro.dispatchers;

import cpp.luv.Luv;
import cpp.luv.Work;
import haxe.Exception;
import haxe.atomic.AtomicInt;
import haxe.coro.dispatchers.Dispatcher;
import haxe.coro.dispatchers.IDispatchObject;
import haxe.coro.schedulers.IScheduler;
import haxe.ds.Option;
import hxcoro.concurrent.BackOff;
import hxcoro.schedulers.LuvScheduler;

/**
Expand All @@ -17,17 +20,20 @@ import hxcoro.schedulers.LuvScheduler;
class LuvDispatcher extends Dispatcher
{
final loop:LuvLoop;
final workQueue:AsyncDeque<()->Void>;
final workQueue:AsyncDeque<IDispatchObject>;
final s:IScheduler;
// Only set if we create it
final luvScheduler:Option<LuvScheduler>;
// Objects that were dispatched but have not finished running yet, or -1 once shut down
final numPending:AtomicInt;

function get_scheduler():IScheduler {
return s;
}

public function new(loop:LuvLoop, ?scheduler:IScheduler) {
this.loop = loop;
numPending = new AtomicInt(0);

workQueue = new AsyncDeque(loop, @:nullSafety(Off) loopWork);
if (scheduler == null) {
Expand All @@ -40,27 +46,59 @@ class LuvDispatcher extends Dispatcher
}
}

/**
Runs `obj` on the libuv thread pool. Throws if this dispatcher has been shut down.
**/
public function dispatch(obj:IDispatchObject) {
workQueue.add(obj.onDispatch);
// Increment and check in one step so that shutDown can't close the queue in between.
while (true) {
final old = numPending.load();
if (old < 0) {
throw new Exception("LuvDispatcher has been shut down");
}
if (numPending.compareExchange(old, old + 1) == old) {
break;
}
BackOff.backOff();
}
workQueue.add(obj);
}

function loopWork() {
consumeDeque(workQueue, event -> {
Work.queue(loop, event);
consumeDeque(workQueue, obj -> {
Work.queue(loop, () -> {
obj.onDispatch();
numPending.sub(1);
});
});
}

/**
Shuts down the scheduler if it is owned by this dispatcher, queues any pending work and
closes the internal libuv handle.
Runs `loop` until all dispatched objects have finished, including objects that are
dispatched while doing so. Then closes the internal libuv handle and shuts down the
scheduler if it is owned by this dispatcher.

Must be called from the thread that runs `loop`.
**/
public function shutDown() {
// Switching from 0 to -1 atomically means that no dispatch can sneak in after the check.
while (true) {
switch (numPending.compareExchange(0, -1)) {
case 0:
break;
case -1:
// Already shut down
return;
case _:
// Each finished work item wakes the loop up, so this does not miss the last one.
Luv.runLoop(loop, Once);
}
}
workQueue.close();
switch (luvScheduler) {
case Some(s):
s.shutDown();
case None:
}
loopWork();
workQueue.close();
}
}
}
17 changes: 10 additions & 7 deletions src/hxcoro/run/Setup.hx
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ class Setup {
final scheduler = new hxcoro.schedulers.LuvScheduler(loop);
final dispatcher = createDispatcher(loop, scheduler);
function finalize() {
scheduler.shutDown();
// The dispatcher goes first: work that is still running might use the scheduler.
shutDownDispatcher(dispatcher);
cpp.luv.Luv.stopLoop(loop);
scheduler.shutDown();
cpp.luv.Luv.shutdownLoop(loop);
Comment thread
Simn marked this conversation as resolved.
Resolved
cpp.luv.Luv.freeLoop(loop);
}
return new LoopSetup(scheduler, dispatcher, finalize);
Expand All @@ -111,7 +112,7 @@ class Setup {
final pool = new hxcoro.thread.FixedThreadPool(10);
return createLuvGen(
(uvLoop, loop) -> new hxcoro.dispatchers.ThreadPoolDispatcher(loop, pool),
dispatcher -> pool.shutDown()
dispatcher -> pool.shutDown(true)
);
}

Expand Down Expand Up @@ -153,13 +154,15 @@ class Setup {
#end

/**
Creates the default setup for the current target, as used by `CoroRun.runWith`: a
thread pool of 10 threads on JVM, C++ and eval (combined with libuv on C++ with
`hxcpp_luv_io`), and `createTrampoline` otherwise.
Creates the default setup for the current target, as used by `CoroRun.runWith`:

- C++ with `hxcpp_luv_io`: `createLuv()`
- JVM, C++ without `hxcpp_luv_io` and eval: `createThreadPool(10)`
- Other targets: `createTrampoline()`
**/
static public function createDefault() {
#if (cpp && hxcpp_luv_io)
return createLuvThreadPool();
return createLuv();
#elseif (jvm || cpp || interp)
return createThreadPool(10);
#else
Expand Down
2 changes: 1 addition & 1 deletion tests/src/run/TestEntrypoints.hx
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class TestEntrypoints extends atest.Test {
final setup = Setup.createLuvGen(
(uvLoop, loop) -> new ThreadPoolDispatcher(loop, pool),
dispatcher -> {
pool.shutDown();
pool.shutDown(true);
}
);
final context = setup.createContext();
Expand Down
Loading