Skip to content

Commit 8cc5060

Browse files
committed
Add support for Scheduler#fiber_interrupt.
1 parent d7b87fe commit 8cc5060

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
@@ -347,6 +347,32 @@ def io_write(io, buffer, length, offset = 0)
347347
end
348348
end
349349

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

0 commit comments

Comments
 (0)