class Thrifty::Signals

Public Class Methods

new() click to toggle source
# File lib/thrifty/signals.rb, line 20
def initialize
  @handlers  = []
  @after     = []
  @mutex     = Mutex.new
  @resource  = ConditionVariable.new
  @installed = false
end
register(fn) click to toggle source
# File lib/thrifty/signals.rb, line 9
def register(fn)
  instance.install
  instance.register(fn)
end
register_after(fn) click to toggle source
# File lib/thrifty/signals.rb, line 14
def register_after(fn)
  instance.install
  instance.register_after(fn)
end

Public Instance Methods

install() click to toggle source
# File lib/thrifty/signals.rb, line 28
def install
  unless @installed
    %w{INT TERM}.each do |sig|
      trap sig do
        Thread.new{ shutdown }.join
      end
    end
    @installed = true
  end
end
register(fn) click to toggle source
# File lib/thrifty/signals.rb, line 49
def register(fn)
  @handlers << fn
end
register_after(fn) click to toggle source
# File lib/thrifty/signals.rb, line 53
def register_after(fn)
  @after << fn
end
shutdown(sig = nil) click to toggle source
# File lib/thrifty/signals.rb, line 57
def shutdown(sig = nil)
  @handlers.each {|fn| fn.call }
  @after.each {|fn| fn.call }

  @mutex.synchronize do
    @resource.signal
  end
end
wait() click to toggle source
# File lib/thrifty/signals.rb, line 39
def wait
  begin
    @mutex.synchronize do
      @resource.wait(@mutex)
    end
  rescue ::Interrupt
    shutdown
  end
end