Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
43 changes: 34 additions & 9 deletions src/hxcoro/dispatchers/LuvDispatcher.cpp.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package hxcoro.dispatchers;

import cpp.luv.Luv;
import cpp.luv.Work;
import haxe.Exception;
import haxe.atomic.AtomicBool;
import haxe.atomic.AtomicInt;
import haxe.coro.dispatchers.Dispatcher;
import haxe.coro.dispatchers.IDispatchObject;
import haxe.coro.schedulers.IScheduler;
Expand All @@ -17,17 +20,22 @@ 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
final numPending:AtomicInt;
final isShutDown:AtomicBool;

function get_scheduler():IScheduler {
return s;
}

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

workQueue = new AsyncDeque(loop, @:nullSafety(Off) loopWork);
if (scheduler == null) {
Expand All @@ -40,27 +48,44 @@ 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);
if (isShutDown.load()) {
throw new Exception("LuvDispatcher has been shut down");
}
numPending.add(1);
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() {
while (numPending.load() > 0) {
// Each finished work item wakes the loop up, so this does not miss the last one.
Luv.runLoop(loop, Once);
}
isShutDown.store(true);
Comment thread
Simn marked this conversation as resolved.
Resolved
workQueue.close();
switch (luvScheduler) {
case Some(s):
s.shutDown();
case None:
}
loopWork();
workQueue.close();
}
}
}
15 changes: 8 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,13 @@ 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`:
`createLuv` on C++ with `hxcpp_luv_io`, a thread pool of 10 threads on JVM, C++ and
eval, and `createTrampoline` otherwise.
Comment thread
Simn marked this conversation as resolved.
Resolved
**/
static public function createDefault() {
#if (cpp && hxcpp_luv_io)
return createLuvThreadPool();
return createLuv();
#elseif (jvm || cpp || interp)
return createThreadPool(10);
#else
Expand Down
5 changes: 5 additions & 0 deletions tests/src/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ function main() {
atest.Macros.addCases(runner, "structured");

final passed = runner.run();
#if (cpp && hxcpp_luv_io)
// Join libuv's worker threads while the hxcpp runtime is still intact. Otherwise this
// happens in libuv's library destructor after static destructors have run.
cpp.luv.Luv.shutdown();
#end
Comment thread
Simn marked this conversation as resolved.
Resolved
#if sys
Sys.exit(passed ? 0 : 1);
#end
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