Skip to content

Add Fiber::ExecutionContext::ThreadPool#15885

Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom
ysbaddaden:feature/execution-context-thread-pool
Jan 23, 2026
Merged

Add Fiber::ExecutionContext::ThreadPool#15885
straight-shoota merged 2 commits intocrystal-lang:masterfrom
ysbaddaden:feature/execution-context-thread-pool

Conversation

@ysbaddaden
Copy link
Collaborator

@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. 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 rename SingleThreaded to Concurrent and MultiThreaded to Parallel. Done.

Extracted from #15871

@crysbot
Copy link
Collaborator

crysbot commented Aug 12, 2025

This pull request has been mentioned on Crystal Forum. There might be relevant details there:

https://forum.crystal-lang.org/t/how-to-add-command-line-options-to-specify-the-number-of-threads/6286/12

@ysbaddaden
Copy link
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).
@ysbaddaden ysbaddaden force-pushed the feature/execution-context-thread-pool branch from e811e22 to 5370dd5 Compare January 13, 2026 10:14
@ysbaddaden ysbaddaden marked this pull request as ready for review January 13, 2026 10:14
@ysbaddaden
Copy link
Collaborator Author

ysbaddaden commented Jan 13, 2026

Rebased again to remove the extracted bits (#16420, #16421) and ready for review.

@straight-shoota straight-shoota moved this from Review to Approved in Multi-threading Jan 21, 2026
@straight-shoota straight-shoota added this to the 1.20.0 milestone Jan 21, 2026
@straight-shoota straight-shoota merged commit 73a70fa into crystal-lang:master Jan 23, 2026
50 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in Multi-threading Jan 23, 2026
@ysbaddaden ysbaddaden deleted the feature/execution-context-thread-pool branch January 23, 2026 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants