module SignalTrampoline

Constants

SIGNALS

Public Instance Methods

detach_handler_thread() click to toggle source
# File lib/raad_totem/signal_trampoline.rb, line 29
def detach_handler_thread
  Thread.new do
    Thread.current.abort_on_exception = true
    loop do
      s = @signal_q.pop
      @handlers[s].call if @handlers[s]
    end
  end
end
trap(signal, &block) click to toggle source

using threads to bounce signal using a thread-safe queue seem the most robust way to handle signals. it minimizes the code in the trap block and reissue the signal and its handling in the normal Ruby flow, within normal threads.

# File lib/raad_totem/signal_trampoline.rb, line 22
def trap(signal, &block)
  raise("unknown signal") unless SIGNALS.has_key?(signal)
  @handler_thread ||= detach_handler_thread
  @handlers[signal] = block
  Kernel.trap(signal) {Thread.new{@signal_q << signal}}
end