RFC 2: Add Fiber::ExecutionContext::Isolated#15513
RFC 2: Add Fiber::ExecutionContext::Isolated#15513straight-shoota merged 15 commits intocrystal-lang:masterfrom
Fiber::ExecutionContext::Isolated#15513Conversation
Introduces the last EC scheduler that runs a single fiber in a single thread. Contrary to the other schedulers, concurrency is disabled. Like the ST scheduler, the scheduler doesn't need to actively park the thread and merely waits on the event loop.
The point is to avoid having fiber being spawn in an unknown context from the isolated fiber. While we could default to `nil` it would delay the understanding of the behavior to a runtime error, while having a compilation error will bring up the issue sooner, and makes the intent more explicit. We'll decide later if we want to have a default value, and what (nil? current context? default context?)
| protected getter thread : Thread | ||
| @main_fiber : Fiber | ||
|
|
||
| getter event_loop : Crystal::EventLoop = Crystal::EventLoop.create |
There was a problem hiding this comment.
Optimization: the discussion on the spawn context made me realize that an isolated fiber may not need an event loop. It should be lazily initialized when needed, though it means that the thread will need to be able to park itself (#enqueue and #reschedule must take care of this case) 🤔
There was a problem hiding this comment.
I recall that I dismissed the following but I can never recall why: there could be a blocking event loop that would merely do blocking calls.
And I just recalled: sockets are nonblocking on UNIX (O_NONBLOCK) and the syscalls wouldn't be blocking.
There was a problem hiding this comment.
The Wasi event loop is also blocking only. There are no threads there, though.
|
For what it's worth, I've been using the EC shard's Thank you for your hard work 🫡 |
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
Fiber::ExecutionContext::Isolated
Fiber::ExecutionContext::IsolatedFiber::ExecutionContext::Isolated
Aka bubble wrap a thread.
The
Fiber::ExecutionContext::Isolatedcontext is a special scheduler: it starts a single thread to execute a block a code inside its main fiber. Concurrency is disabled, no other fiber can run on this thread. The fiber owns the thread and can do any number of blocking operations as it needs to without blocking any other fiber.But you can still:
Channel,MutexorWaitGroup);spawnfibers, they will be spawned into another context (for example the default context).NOTE: this PR alone isn't enough to enable execution contexts. It misses either the ST or MT context.
refs #15342