Skip to content
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

RFC 2: Skeleton for ExecutionContext #15350

Conversation

ysbaddaden
Copy link
Contributor

@ysbaddaden ysbaddaden commented Jan 16, 2025

Integrates the skeleton as per RFC #0002.

  • Add the ExecutionContext module;
  • Add the ExecutionContext::Scheduler module;
  • Add the execution_context compile-time flag.

When the execution_context flag is set:

  • Don't load Crystal::Scheduler;
  • Plug ExecutionContext instead of Crystal::Scheduler in spawn, Fiber, ...

This is only the skeleton: there are no implementations (yet). Trying to compile anything with -Dexecution_context will obviously fail until the follow up pull requests implementing at least the ST and/or MT context are merged.

TODO:

refs #15342
depends on #15345

- Add the `ExecutionContext` module;
- Add the `ExecutionContext::Scheduler` module;
- Add the `execution_context` compile-time flag.

When the `execution_context` flag is set:

- Don't load `Crystal::Scheduler`;
- Plug `ExecutionContext` instead of `Crystal::Scheduler` in `spawn`,
  `Fiber`, ...

This is only the skeleton: there are no implementations (yet). Trying to
compile anything with `-Dexecution_context` will obviously fail for the
time being.
The current ST and MT schedulers use a distinct pool per thread, which
means we only need the thread safety for execution contexts that will
share a single pool for a whole context.
The point is to avoid parallel enqueues while running the event loop, so
we get better control to where and how the runnable fibers are enqueued;
for example all at once instead of one by one (may not be as effective
as it sounds).

More importantly for Execution Contexts: it avoids parallel enqueues
while the eventloop is running which sometimes leads to confusing
behavior; for example when deciding to wake up a scheduler/thread we
musn't interryupt the event loop (obviously).

This is working correctly for the Polling (Epoll, Kqueue) and IOCP event
loop implementations. I'm less confident with the libevent one where the
external library executes arbitrary callbacks.
ysbaddaden and others added 3 commits January 17, 2025 14:09
We need this because we don't load crystal/scheduler anymore when
execution contexts are enabled.
@ysbaddaden
Copy link
Contributor Author

I added a couple TODO to be handled after #15409 and #15345 are merged.

@straight-shoota straight-shoota removed this from the 1.16.0 milestone Feb 5, 2025
@ysbaddaden
Copy link
Contributor Author

Merged with master to bring in #15409 + solved conflicts.

@ysbaddaden
Copy link
Contributor Author

ysbaddaden commented Feb 11, 2025

I solved more conflicts. Now I can run my branch with the ST and MT schedulers using #15345 and this branch 👍

ysbaddaden added a commit to ysbaddaden/crystal that referenced this pull request Feb 11, 2025
@ysbaddaden
Copy link
Contributor Author

As far as I can tell: there's nothing left to do here. Just waiting for a (final) review :)

@straight-shoota straight-shoota self-requested a review February 20, 2025 17:39
@straight-shoota straight-shoota added this to the 1.16.0 milestone Feb 21, 2025
@straight-shoota straight-shoota merged commit 0582376 into crystal-lang:master Feb 22, 2025
71 checks passed
@ysbaddaden ysbaddaden deleted the feature/execution-context-skeleton branch February 22, 2025 20:47
kojix2 pushed a commit to kojix2/crystal that referenced this pull request Feb 23, 2025
Integrates the skeleton as per crystal-lang/rfcs#2

- Add the `ExecutionContext` module;
- Add the `ExecutionContext::Scheduler` module;
- Add the `execution_context` compile-time flag.

When the `execution_context` flag is set:

- Don't load `Crystal::Scheduler`;
- Plug `ExecutionContext` instead of `Crystal::Scheduler` in `spawn`, `Fiber`, ...

This is only the skeleton: there are no implementations (yet). Trying to compile anything with `-Dexecution_context` will fail until the ST and/or MT context are implemented.

Co-authored-by: Johannes Müller <[email protected]>
ysbaddaden added a commit to crystal-lang/rfcs that referenced this pull request Feb 25, 2025
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