Repository navigation
Conversation
Adds a BlockingPool context element that Setup installs and shuts down, and Coro.blocking, which runs a function on that pool while the caller is suspended. The caller is resumed through the scheduler, so that a trampoline setup continues on its loop thread. See #256. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXFVBrzZpVWZvgWzbo9
|
Let's please outsource most of this jank in Coro.hx somewhere else. We should keep Also, this brings in a new context element for basically everything which feels very heavy. Is this really how the professionals do this? I'd be fine with a more general context element that is about "something with threads", but one specifically for blocking threads to be used specifically for this functionality feels wrong. |
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Va4FXFVBrzZpVWZvgWzbo9
|
Context element: no, a dedicated element for this is not how others do it. From memory of the other runtimes, not re-checked:
So the two options I see for hxcoro:
I'd go with 2. It keeps the ownership and shutdown that the issue wanted, and the context grows by one general slot instead of one per feature. If that is the direction you want, I'd rename the element ( Generated by Claude Code |
|
I find 2. appealing, but the name is crucial here because it determines the scope of this. I'm currently thinking it should be named something involving |
|
I think "Execution" makes more sense than a name about threads, for one concrete reason: an element about execution can exist on every target, while one about threads can't. With a thread-named element, The scope I'd give it: where a setup runs code that is not a coroutine. That deliberately leaves out the dispatcher and scheduler, which already have their place in the context, so the element doesn't turn into a second Two names that fit that scope differently:
I'd take // hxcoro.run.Executors, owned by Setup, installed in every context
class Executors implements IElement<Executors> {
/** Runs `func` where blocking is allowed, then resumes `cont` with its outcome. **/
public function runBlocking<T>(cont:IContinuation<T>, func:() -> T):Void;
public function shutDown():Void;
}
// Coro.hx
@:coroutine public static function blocking<T>(func:() -> T):T {
return suspend(cont -> cont.context.getOrRaise(Executors).runBlocking(cont, func));
}
Generated by Claude Code |
|
And I don't know, I kinda hate everything right now and don't feel very constructive. But this keeps coming up so we'll need some solution. I'll think about this some more. |
Prototype for #256: a way to call a blocking function from a coroutine without blocking the dispatcher.
What it adds
Coro.blocking(func)suspends the caller, runsfuncon a thread of the context's blocking pool, and resumes the caller with its result or exception.hxcoro.thread.BlockingPoolis a context element that wraps aFixedThreadPool(16 threads by default). The pool is created on first use, so a setup that never callsblockingstarts no extra threads.Setupowns aBlockingPool, installs it increateContext/adaptContext, and shuts it down inclose().funcis called directly.Decisions taken on the questions in #256
functo return and then throws theCancellationException; the result is dropped. A caller that is cancelled while its call is still queued never runsfunc. There is no option to abandon a running call.BlockingPool(n)into the context yourself.Resuming on a trampoline setup
TrampolineDispatcherruns what is dispatched to it on the dispatching thread. Resuming the caller from the pool thread would therefore continue the coroutine on that thread, concurrently with the loop thread.blockingresumes through the dispatcher's scheduler instead, which hands the caller over to the loop.testTrampolineCallerResumesOnLoopThreadcovers this and fails with a direct resume.On thread-pool setups this costs one extra hop through the loop thread.
Tested
Main suite and thread tests on eval, jvm and python, main suite on js, each with the default dispatcher and at least one other. CI covers the remaining targets (hl, cpp, neko, php, lua) and is green on all three platforms.
TODO in this PR
blockingon the libuv setups (createLuv,createLuvThreadPool). They use the same scheduler path, butLuvSchedulerwas not exercised.blockingdoes in a context without aBlockingPool, e.g. one built without aSetup. It currently throwsCoroutineException.Setupand its factory functions.CoroRun.promisecreates aSetupthat is never closed, so a blocking pool started from there is never shut down. Only matters if that path becomes available on a threaded target.TaskTraceoutput.-D coro.stack-traces, likescopedoes withstartPos.blockingand the location ofBlockingPool(hxcoro.thread).AGENTS.md.Not part of this PR: a pool that grows and shrinks on demand.
BlockingPoolonly depends onIThreadPool, so that can replaceFixedThreadPoolseparately.🤖 Generated with Claude Code
https://claude.ai/code/session_01Va4FXFVBrzZpVWZvgWzbo9