-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow channels to be closed on blocking select #8243
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
|
@@ -32,6 +32,7 @@ class Channel(T) | |
abstract def lock_object_id | ||
abstract def lock | ||
abstract def unlock | ||
abstract def closed? : Bool | ||
|
||
def create_context_and_wait(state_ptr) | ||
context = SelectContext.new(state_ptr, self) | ||
|
@@ -286,42 +287,72 @@ class Channel(T) | |
.uniq(&.lock_object_id) | ||
.sort_by(&.lock_object_id) | ||
|
||
ops_locks.each &.lock | ||
while true | ||
ops_locks.each &.lock | ||
|
||
ops.each_with_index do |op, index| | ||
ignore = false | ||
result = op.execute | ||
ops.each_with_index do |op, index| | ||
result = op.execute | ||
|
||
unless result.is_a?(NotReady) | ||
unless result.is_a?(NotReady) | ||
ops_locks.each &.unlock | ||
return index, result | ||
end | ||
end | ||
|
||
if has_else | ||
ops_locks.each &.unlock | ||
return index, result | ||
return ops.size, NotReady | ||
end | ||
end | ||
|
||
if has_else | ||
ops_locks.each &.unlock | ||
return ops.size, NotReady | ||
end | ||
state = Atomic(SelectState).new(SelectState::Active) | ||
all_closed = true | ||
contexts = ops.map_with_index do |op, index| | ||
# Avoid waiting over a closed channel | ||
if op.closed? | ||
nil | ||
else | ||
all_closed = false | ||
op.create_context_and_wait(pointerof(state)) | ||
# Closing one of the channels will wakeup this path | ||
end | ||
end | ||
|
||
state = Atomic(SelectState).new(SelectState::Active) | ||
contexts = ops.map &.create_context_and_wait(pointerof(state)) | ||
if all_closed | ||
ops_locks.each &.unlock | ||
|
||
ops_locks.each &.unlock | ||
Crystal::Scheduler.reschedule | ||
# If channels are closed, there is no need to reschedule | ||
# if there is no has_else an exception is more accurate probable | ||
if has_else | ||
return ops.size, NotReady | ||
else | ||
raise "All channels are closed on a blocking select" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should ideally this would be designed to literally call |
||
end | ||
end | ||
|
||
ops.each do |op| | ||
op.lock | ||
op.unwait | ||
op.unlock | ||
end | ||
ops_locks.each &.unlock | ||
Crystal::Scheduler.reschedule | ||
|
||
contexts.each_with_index do |context, index| | ||
if context.activated? | ||
return index, context.action.result | ||
ops.each do |op| | ||
op.lock | ||
op.unwait | ||
op.unlock | ||
end | ||
|
||
contexts.each_with_index do |context, index| | ||
if context && context.activated? | ||
return index, context.action.result | ||
end | ||
end | ||
end | ||
|
||
raise "BUG: Fiber was awaken from select but no action was activated" | ||
# if this point is reached !all_closed and no context was activated | ||
# so the wakeup was due to a close of some channels. | ||
# has_else indicated a non-blocking select, so we can stop the execution | ||
# right away. We were waiting for channels, we got a signal of some channel closing | ||
# but no data is ready. | ||
if has_else | ||
return ops.size, NotReady | ||
end | ||
end | ||
end | ||
|
||
# :nodoc: | ||
|
@@ -372,6 +403,10 @@ class Channel(T) | |
def unlock | ||
@[email protected] | ||
end | ||
|
||
def closed? : Bool | ||
@channel.closed? | ||
end | ||
end | ||
|
||
# :nodoc: | ||
|
@@ -409,5 +444,9 @@ class Channel(T) | |
def unlock | ||
@[email protected] | ||
end | ||
|
||
def closed? : Bool | ||
@channel.closed? | ||
end | ||
end | ||
end |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The index is never used?