Add Fiber::ExecutionContext::ThreadPool#15885
Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom Jan 23, 2026
Merged
Add Fiber::ExecutionContext::ThreadPool#15885straight-shoota merged 2 commits intocrystal-lang:masterfrom
Fiber::ExecutionContext::ThreadPool#15885straight-shoota merged 2 commits intocrystal-lang:masterfrom
Conversation
Collaborator
|
This pull request has been mentioned on Crystal Forum. There might be relevant details there: |
This was referenced Mar 25, 2025
433a1c4 to
e811e22
Compare
ysbaddaden
commented
Nov 25, 2025
ysbaddaden
commented
Nov 25, 2025
This was referenced Nov 25, 2025
Merged
Collaborator
Author
|
Rebased from master. I fixed some issues, squashed commits, and extracted a couple PRs. |
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).
e811e22 to
5370dd5
Compare
Collaborator
Author
straight-shoota
approved these changes
Jan 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.Done.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 renameDone.SingleThreadedtoConcurrentandMultiThreadedtoParallel.Extracted from #15871