Skip to content

Add Fiber::ExecutionContext::ThreadPool #15885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

ysbaddaden
Copy link
Contributor

@ysbaddaden ysbaddaden commented Jun 6, 2025

Introduces a pool of threads for execution contexts.

This changes the runtime behavior of threads: we no longer start a thread to run a specific scheduler run loop and never terminate in practice (except for the isolated context). Each thread now has its own inner loop that switches to a scheduler loop fiber (the scheduler's main fiber) then switches back to its inner loop (the thread's main fiber) to sleep for a while, then eventually terminates.

The benefit of the global thread loop is that threads are kept around instead of being created and thrown away. This is for example helpful for #15871 that will allow to move a scheduler to another thread, as well as for applications that regularly start an isolated fiber. They can keep reusing a pending thread instead of having to create one every time.

Threads still eventually terminate after some configurable inactive time, except for the main thread because we need to keep the main fiber's stack alive.

A future improvement could park MT threads back into the thread pool, instead of keeping them tied to the MT context. They could be reused by any context that needs parallelism, or to boot a new isolated fiber or ST context, instead of sitting around.

NOTE: the thread pool makes the min/max range in MT contexts kinda moot; it sounds better to settle to a maximum number of schedulers instead.

NOTE: the more we decouple schedulers from threads, the more I'm convinced that using "thread" in the execution context names doesn't make much sense; I'm strongly leaning to rename SingleThreaded to Concurrent and MultiThreaded to Parallel.

Extracted from #15871

A global pool of thread to start new threads from, and return threads
to, so we don't start and stop threads all the time, and can wake an
existing thread instead of creating a new one from scratch.

The thread pool still eventually shuts down a thread after a
configurable keepalive is reached, but takes extra measures to never
shutdown the main thread, which would invalide the program's main fiber
stack (segfaults).
We can't join on a thread because it may not terminate anymore. We must
call the `#wait` method of the isolated context to know when the
isolated fiber has terminated. The method is conveniently aliased as
`#join` so we don't need to handle the type in most cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Review
Development

Successfully merging this pull request may close these issues.

1 participant