-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
RFC 2: Skeleton for ExecutionContext #15350
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
Merged
straight-shoota
merged 16 commits into
crystal-lang:master
from
ysbaddaden:feature/execution-context-skeleton
Feb 22, 2025
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3e14951
Skeleton for ExecutionContext types as per RFC #0002
ysbaddaden 35da3f9
Add thread safety to Fiber::StackPool
ysbaddaden 3852ce4
Alt EventLoop#run(queue*, blocking) method
ysbaddaden 0683695
Fix: wrong documentation for ExecutionContext#enqueue
ysbaddaden 8abce65
Document Thread#dead_fiber (delayed fiber stack cleanup)
ysbaddaden 7f2d0d4
Fix: move require crystal/system/print_error to src/raise
ysbaddaden 094393c
Rename as Fiber::ExecutionContext + add :nodoc:
ysbaddaden 0e86c55
Fix: handle dead fiber stack out of swapcontext
ysbaddaden ce23a0e
Fix: compilation with Thread dead_fiber_stack
ysbaddaden 95718e6
Fixup: can't infer type of ivar declared in macro (stack pool lock)
ysbaddaden f6e11e2
fixup! Fixup: can't infer type of ivar declared in macro (stack pool …
ysbaddaden f15e6bc
Merge remote-tracking branch 'upstream/master' into feature/execution…
ysbaddaden b72f07e
Fix: explicit type for Fiber.new (not inferred because inside macro?)
ysbaddaden 2d6e474
fixup! Alt EventLoop#run(queue*, blocking) method
ysbaddaden d3af76b
Fix: dead fiber stack
ysbaddaden b5c0c8e
Fix: Fiber.new
ysbaddaden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,7 +115,11 @@ abstract class Crystal::EventLoop::Polling < Crystal::EventLoop | |
# NOTE: thread unsafe | ||
def run(blocking : Bool) : Bool | ||
system_run(blocking) do |fiber| | ||
Crystal::Scheduler.enqueue(fiber) | ||
{% if flag?(:execution_context) %} | ||
fiber.execution_context.enqueue(fiber) | ||
{% else %} | ||
Crystal::Scheduler.enqueue(fiber) | ||
{% end %} | ||
end | ||
true | ||
end | ||
|
@@ -303,13 +307,21 @@ abstract class Crystal::EventLoop::Polling < Crystal::EventLoop | |
Polling.arena.free(index) do |pd| | ||
[email protected]_all do |event| | ||
pd.value.@event_loop.try(&.unsafe_resume_io(event) do |fiber| | ||
Crystal::Scheduler.enqueue(fiber) | ||
{% if flag?(:execution_context) %} | ||
fiber.execution_context.enqueue(fiber) | ||
{% else %} | ||
Crystal::Scheduler.enqueue(fiber) | ||
{% end %} | ||
end) | ||
end | ||
|
||
[email protected]_all do |event| | ||
pd.value.@event_loop.try(&.unsafe_resume_io(event) do |fiber| | ||
Crystal::Scheduler.enqueue(fiber) | ||
{% if flag?(:execution_context) %} | ||
fiber.execution_context.enqueue(fiber) | ||
{% else %} | ||
Crystal::Scheduler.enqueue(fiber) | ||
{% end %} | ||
end) | ||
end | ||
|
||
|
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
require "../crystal/event_loop" | ||
require "../crystal/system/thread" | ||
require "../crystal/system/thread_linked_list" | ||
require "../fiber" | ||
require "../fiber/stack_pool" | ||
require "./scheduler" | ||
|
||
{% raise "ERROR: execution contexts require the `preview_mt` compilation flag" unless flag?(:preview_mt) %} | ||
|
||
module ExecutionContext | ||
ysbaddaden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@@default : ExecutionContext? | ||
|
||
@[AlwaysInline] | ||
def self.default : ExecutionContext | ||
@@default.not_nil!("expected default execution context to have been setup") | ||
end | ||
|
||
# :nodoc: | ||
def self.init_default_context : Nil | ||
raise NotImplementedError.new("No execution context implementations (yet)") | ||
end | ||
|
||
# Returns the default number of workers to start in the execution context. | ||
def self.default_workers_count : Int32 | ||
ENV["CRYSTAL_WORKERS"]?.try(&.to_i?) || Math.min(System.cpu_count.to_i, 32) | ||
end | ||
|
||
# :nodoc: | ||
protected class_getter(execution_contexts) { Thread::LinkedList(ExecutionContext).new } | ||
|
||
# :nodoc: | ||
property next : ExecutionContext? | ||
|
||
# :nodoc: | ||
property previous : ExecutionContext? | ||
|
||
# :nodoc: | ||
def self.unsafe_each(&) : Nil | ||
@@execution_contexts.try(&.unsafe_each { |execution_context| yield execution_context }) | ||
end | ||
|
||
def self.each(&) : Nil | ||
execution_contexts.each { |execution_context| yield execution_context } | ||
end | ||
|
||
@[AlwaysInline] | ||
def self.current : ExecutionContext | ||
Thread.current.execution_context | ||
end | ||
|
||
# Tells the current scheduler to suspend the current fiber and resume the | ||
# next runnable fiber. The current fiber will never be resumed; you're | ||
# responsible to reenqueue it. | ||
# | ||
# This method is safe as it only operates on the current `ExecutionContext` | ||
# and `Scheduler`. | ||
@[AlwaysInline] | ||
def self.reschedule : Nil | ||
Scheduler.current.reschedule | ||
end | ||
|
||
# Tells the current scheduler to suspend the current fiber and to resume | ||
# *fiber* instead. The current fiber will never be resumed; you're responsible | ||
# to reenqueue it. | ||
# | ||
# Raises `RuntimeError` if the fiber doesn't belong to the current execution | ||
# context. | ||
# | ||
# This method is safe as it only operates on the current `ExecutionContext` | ||
# and `Scheduler`. | ||
def self.resume(fiber : Fiber) : Nil | ||
if fiber.execution_context == current | ||
Scheduler.current.resume(fiber) | ||
else | ||
raise RuntimeError.new("Can't resume fiber from #{fiber.execution_context} into #{current}") | ||
end | ||
end | ||
|
||
# Creates a new fiber then calls `#enqueue` to add it to the execution | ||
# context. | ||
# | ||
# May be called from any `ExecutionContext` (i.e. must be thread-safe). | ||
def spawn(*, name : String? = nil, &block : ->) : Fiber | ||
Fiber.new(name, self, &block).tap { |fiber| enqueue(fiber) } | ||
end | ||
|
||
# Legacy support for the `same_thread` argument. Each execution context may | ||
# decide to support it or not (e.g. a single threaded context can accept it). | ||
abstract def spawn(*, name : String? = nil, same_thread : Bool, &block : ->) : Fiber | ||
|
||
abstract def stack_pool : Fiber::StackPool | ||
abstract def stack_pool? : Fiber::StackPool? | ||
|
||
abstract def event_loop : Crystal::EventLoop | ||
|
||
# Enqueues a fiber to be resumed inside the execution context. | ||
# | ||
# May be called from any ExecutionContext (i.e. must be thread-safe). | ||
abstract def enqueue(fiber : Fiber) : Nil | ||
end |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.