Skip to content

Commit 17b5e6b

Browse files
committed
Add support for Scheduler#fiber_interrupt.
1 parent 8f48de0 commit 17b5e6b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

lib/async/scheduler.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,32 @@ def io_write(io, buffer, length, offset = 0)
348348
end
349349
end
350350

351+
# Used to defer stopping the current task until later.
352+
class RaiseException
353+
# Create a new stop later operation.
354+
#
355+
# @parameter task [Task] The task to stop later.
356+
def initialize(fiber, exception)
357+
@fiber = fiber
358+
@exception = exception
359+
end
360+
361+
# @returns [Boolean] Whether the task is alive.
362+
def alive?
363+
@fiber.alive?
364+
end
365+
366+
# Transfer control to the operation - this will stop the task.
367+
def transfer
368+
@fiber.raise(@exception)
369+
end
370+
end
371+
372+
# Raise an exception on the specified fiber, waking up the event loop if necessary.
373+
def fiber_interrupt(fiber, exception)
374+
unblock(nil, RaiseException.new(fiber, exception))
375+
end
376+
351377
# Wait for the specified process ID to exit.
352378
#
353379
# @public Since *Async v2*.

0 commit comments

Comments
 (0)