Update method name and add documentation.
This commit is contained in:
parent
09c865d541
commit
511acba4ae
Notes:
git
2021-03-30 14:39:03 +09:00
12
doc/fiber.md
12
doc/fiber.md
@ -76,10 +76,20 @@ class Scheduler
|
|||||||
|
|
||||||
# Sleep the current task for the specified duration, or forever if not
|
# Sleep the current task for the specified duration, or forever if not
|
||||||
# specified.
|
# specified.
|
||||||
# @param duration [Numeric] The amount of time to sleep in seconds.
|
# @parameter duration [Numeric] The amount of time to sleep in seconds.
|
||||||
def kernel_sleep(duration = nil)
|
def kernel_sleep(duration = nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Execute the given block. If the block execution exceeds the given timeout,
|
||||||
|
# the specified exception `klass` will be raised. Typically, only non-blocking
|
||||||
|
# methods which enter the scheduler will raise such exceptions.
|
||||||
|
# @parameter duration [Integer] The amount of time to wait, after which an exception will be raised.
|
||||||
|
# @parameter klass [Class] The exception class to raise.
|
||||||
|
# @parameter *arguments [Array] The arguments to send to the constructor of the exception.
|
||||||
|
# @yields {...} The user code to execute.
|
||||||
|
def timeout_after(duration, klass, *arguments, &block)
|
||||||
|
end
|
||||||
|
|
||||||
# Block the calling fiber.
|
# Block the calling fiber.
|
||||||
# @parameter blocker [Object] What we are waiting on, informational only.
|
# @parameter blocker [Object] What we are waiting on, informational only.
|
||||||
# @parameter timeout [Numeric | Nil] The amount of time to wait for in seconds.
|
# @parameter timeout [Numeric | Nil] The amount of time to wait for in seconds.
|
||||||
|
@ -73,6 +73,9 @@ module Timeout
|
|||||||
# ensure to prevent the handling of the exception. For that reason, this
|
# ensure to prevent the handling of the exception. For that reason, this
|
||||||
# method cannot be relied on to enforce timeouts for untrusted blocks.
|
# method cannot be relied on to enforce timeouts for untrusted blocks.
|
||||||
#
|
#
|
||||||
|
# If a scheduler is defined, it will be used to handle the timeout by invoking
|
||||||
|
# Scheduler#timeout_after.
|
||||||
|
#
|
||||||
# Note that this is both a method of module Timeout, so you can <tt>include
|
# Note that this is both a method of module Timeout, so you can <tt>include
|
||||||
# Timeout</tt> into your classes so they have a #timeout method, as well as
|
# Timeout</tt> into your classes so they have a #timeout method, as well as
|
||||||
# a module method, so you can call it directly as Timeout.timeout().
|
# a module method, so you can call it directly as Timeout.timeout().
|
||||||
@ -81,8 +84,8 @@ module Timeout
|
|||||||
|
|
||||||
message ||= "execution expired".freeze
|
message ||= "execution expired".freeze
|
||||||
|
|
||||||
if (scheduler = Fiber.scheduler)&.respond_to?(:timeout_raise)
|
if (scheduler = Fiber.scheduler)&.respond_to?(:timeout_after)
|
||||||
return scheduler.timeout_raise(sec, klass || Error, message, &block)
|
return scheduler.timeout_after(sec, klass || Error, message, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
from = "from #{caller_locations(1, 1)[0]}" if $DEBUG
|
from = "from #{caller_locations(1, 1)[0]}" if $DEBUG
|
||||||
|
13
scheduler.c
13
scheduler.c
@ -17,7 +17,7 @@ static ID id_close;
|
|||||||
static ID id_block;
|
static ID id_block;
|
||||||
static ID id_unblock;
|
static ID id_unblock;
|
||||||
|
|
||||||
static ID id_timeout_raise;
|
static ID id_timeout_after;
|
||||||
static ID id_kernel_sleep;
|
static ID id_kernel_sleep;
|
||||||
static ID id_process_wait;
|
static ID id_process_wait;
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ Init_Fiber_Scheduler(void)
|
|||||||
id_block = rb_intern_const("block");
|
id_block = rb_intern_const("block");
|
||||||
id_unblock = rb_intern_const("unblock");
|
id_unblock = rb_intern_const("unblock");
|
||||||
|
|
||||||
id_timeout_raise = rb_intern_const("timeout_raise");
|
id_timeout_after = rb_intern_const("timeout_after");
|
||||||
id_kernel_sleep = rb_intern_const("kernel_sleep");
|
id_kernel_sleep = rb_intern_const("kernel_sleep");
|
||||||
id_process_wait = rb_intern_const("process_wait");
|
id_process_wait = rb_intern_const("process_wait");
|
||||||
|
|
||||||
@ -110,19 +110,20 @@ rb_fiber_scheduler_make_timeout(struct timeval *timeout)
|
|||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
|
|
||||||
VALUE rb_fiber_scheduler_timeout_raise(VALUE scheduler, VALUE timeout, VALUE exception, VALUE message)
|
VALUE
|
||||||
|
rb_fiber_scheduler_timeout_after(VALUE scheduler, VALUE timeout, VALUE exception, VALUE message)
|
||||||
{
|
{
|
||||||
VALUE arguments[] = {
|
VALUE arguments[] = {
|
||||||
timeout, exception, message
|
timeout, exception, message
|
||||||
};
|
};
|
||||||
|
|
||||||
return rb_check_funcall(scheduler, id_timeout_raise, 3, arguments);
|
return rb_check_funcall(scheduler, id_timeout_after, 3, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
rb_fiber_scheduler_timeout_raisev(VALUE scheduler, int argc, VALUE * argv)
|
rb_fiber_scheduler_timeout_afterv(VALUE scheduler, int argc, VALUE * argv)
|
||||||
{
|
{
|
||||||
return rb_check_funcall(scheduler, id_timeout_raise, argc, argv);
|
return rb_check_funcall(scheduler, id_timeout_after, argc, argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
|
@ -129,7 +129,7 @@ class Scheduler
|
|||||||
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||||
end
|
end
|
||||||
|
|
||||||
def timeout_raise(duration, klass, message, &block)
|
def timeout_after(duration, klass, message, &block)
|
||||||
fiber = Fiber.current
|
fiber = Fiber.current
|
||||||
|
|
||||||
self.fiber do
|
self.fiber do
|
||||||
|
@ -5,7 +5,7 @@ require_relative 'scheduler'
|
|||||||
require 'timeout'
|
require 'timeout'
|
||||||
|
|
||||||
class TestFiberTimeout < Test::Unit::TestCase
|
class TestFiberTimeout < Test::Unit::TestCase
|
||||||
def test_timeout_raise
|
def test_timeout_after
|
||||||
error = nil
|
error = nil
|
||||||
|
|
||||||
thread = Thread.new do
|
thread = Thread.new do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user