Class used to manage timeout handlers across multiple threads.
Timeout handlers should be managed by using the class methods which are synchronized.
id = TimeoutHandler.register(10, Timeout::Error)
begin
sleep 20
puts 'foo'
ensure
TimeoutHandler.cancel(id)
end
will raise Timeout::Error
id = TimeoutHandler.register(10, Timeout::Error)
begin
sleep 5
puts 'foo'
ensure
TimeoutHandler.cancel(id)
end
will print 'foo'
Namespace
Methods
Included Modules
Constants
| TimeoutMutex | = | Mutex.new # :nodoc: |
Mutex used to synchronize access across threads |
||
Class Public methods
cancel(id)
Link
Cancels the timeout handler id
new()
Link
Creates a new TimeoutHandler. You should use ::register and ::cancel instead of creating the timeout handler directly.
# File lib/webrick/utils.rb, line 151 def initialize TimeoutMutex.synchronize{ @timeout_info = Hash.new } @queue = Queue.new @watcher = Thread.start{ to_interrupt = [] while true now = Time.now wakeup = nil to_interrupt.clear TimeoutMutex.synchronize{ @timeout_info.each {|thread, ary| next unless ary ary.each{|info| time, exception = *info if time < now to_interrupt.push [thread, info.object_id, exception] elsif !wakeup || time < wakeup wakeup = time end } } } to_interrupt.each {|arg| interrupt(*arg)} if !wakeup @queue.pop elsif (wakeup -= now) > 0 begin (th = Thread.start {@queue.pop}).join(wakeup) ensure th&.kill&.join end end @queue.clear end } end
register(seconds, exception)
Link
Registers a new timeout handler
Instance Public methods
cancel(thread, id)
Link
Cancels the timeout handler id
interrupt(thread, id, exception)
Link
Interrupts the timeout handler id and raises
exception
register(thread, time, exception)
Link
Registers a new timeout handler