Skip to content

Commit

Permalink
Add support for Scheduler#fiber_interrupt.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jan 16, 2025
1 parent d7b87fe commit 8cc5060
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/async/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,32 @@ def io_write(io, buffer, length, offset = 0)
end
end

# Used to defer stopping the current task until later.
class RaiseException
# Create a new stop later operation.
#
# @parameter task [Task] The task to stop later.
def initialize(fiber, exception)
@fiber = fiber
@exception = exception
end

# @returns [Boolean] Whether the task is alive.
def alive?
@fiber.alive?
end

# Transfer control to the operation - this will stop the task.
def transfer
@fiber.raise(@exception)
end
end

# Raise an exception on the specified fiber, waking up the event loop if necessary.
def fiber_interrupt(fiber, exception)
unblock(nil, RaiseException.new(fiber, exception))
end

# Wait for the specified process ID to exit.
#
# @public Since *Async v2*.
Expand Down

0 comments on commit 8cc5060

Please sign in to comment.