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

Add Fiber.suspend #14560

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/channel.cr
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class Channel(T)
@senders.push pointerof(sender)
@lock.unlock

Crystal::Scheduler.reschedule
Fiber.suspend

case sender.state
in .delivered?
Expand Down Expand Up @@ -308,7 +308,7 @@ class Channel(T)
@receivers.push pointerof(receiver)
@lock.unlock

Crystal::Scheduler.reschedule
Fiber.suspend

case receiver.state
in .delivered?
Expand Down Expand Up @@ -473,7 +473,7 @@ class Channel(T)
contexts = ops.map &.create_context_and_wait(shared_state)

each_skip_duplicates(ops_locks, &.unlock)
Crystal::Scheduler.reschedule
Fiber.suspend

contexts.each_with_index do |context, index|
op = ops[index]
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/win32/process.cr
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
# stuck forever in that case?
# (https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_associate_completion_port)
@completion_key.fiber = ::Fiber.current
Crystal::Scheduler.reschedule
Fiber.suspend

Check failure on line 84 in src/crystal/system/win32/process.cr

View workflow job for this annotation

GitHub Actions / x86_64-windows / build

undefined method 'suspend' for Crystal::System::Fiber:Module

# If the IOCP notification is delivered before the process fully exits,
# wait for it
Expand Down
15 changes: 14 additions & 1 deletion src/fiber.cr
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class Fiber
{% unless flag?(:interpreted) %}
Crystal::Scheduler.stack_pool.release(@stack)
{% end %}
Crystal::Scheduler.reschedule
Fiber.suspend
end

# Returns the current fiber.
Expand Down Expand Up @@ -285,6 +285,19 @@ class Fiber
Crystal::Scheduler.yield
end

# Yields to the scheduler and tells it to swap execution to another waiting
# fibers. Unlike `Fiber.yield` the current fiber won't be automatically
# reenqueued and won't be resumed until it has been explicitely reenqueued.
#
# This is equivalent to `sleep` without a time.
#
# This method is particularly useful to stop the execution of the current
# fiber until something happens, for example an IO event, a message is ready
# in a channel, and so on.
ysbaddaden marked this conversation as resolved.
Show resolved Hide resolved
def self.suspend : Nil
Crystal::Scheduler.reschedule
end

def to_s(io : IO) : Nil
io << "#<" << self.class.name << ":0x"
object_id.to_s(io, 16)
Expand Down
4 changes: 2 additions & 2 deletions src/io/evented.cr
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module IO::Evented
readers = @readers.get { Deque(Fiber).new }
readers << Fiber.current
add_read_event(timeout)
Crystal::Scheduler.reschedule
Fiber.suspend

if @read_timed_out
@read_timed_out = false
Expand All @@ -115,7 +115,7 @@ module IO::Evented
writers = @writers.get { Deque(Fiber).new }
writers << Fiber.current
add_write_event(timeout)
Crystal::Scheduler.reschedule
Fiber.suspend

if @write_timed_out
@write_timed_out = false
Expand Down
2 changes: 1 addition & 1 deletion src/io/overlapped.cr
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ module IO::Overlapped
end
Crystal::Scheduler.event_loop.enqueue(timeout_event)

Crystal::Scheduler.reschedule
Fiber.suspend

Crystal::Scheduler.event_loop.dequeue(timeout_event)
end
Expand Down
3 changes: 2 additions & 1 deletion src/mutex.cr
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class Mutex

@queue.push Fiber.current
end
Crystal::Scheduler.reschedule

Fiber.suspend
end

@mutex_fiber = Fiber.current unless @protection.unchecked?
Expand Down
2 changes: 1 addition & 1 deletion src/wait_group.cr
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class WaitGroup
@waiting.push(pointerof(waiting))
end

Crystal::Scheduler.reschedule
Fiber.suspend

return if done?
raise RuntimeError.new("Positive WaitGroup counter (early wake up?)")
Expand Down
Loading