8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/neverblock/core/system/timeout.rb', line 8
def timeout(time, klass=Timeout::Error, &block)
return rb_timeout(time, klass,&block) unless NB.neverblocking?
if time.nil? || time <= 0
block.call
else
fiber = NB::Fiber.current
timer = NB.reactor.add_timer(time) do
fiber[:timeouts].last.each do |event|
if event.is_a? Reactor::Timer
event.cancel
else
NB.reactor.detach(event[0], event[1])
end
end
fiber.resume(klass.new)
end
(fiber[:timeouts] ||= []) << []
begin
block.call
rescue Exception => e
raise e
ensure
timer.cancel
fiber[:timeouts].pop
end
end
end
|