Class: PerfectQueue::SignalThread
- Inherits:
-
Thread
- Object
- Thread
- PerfectQueue::SignalThread
- Defined in:
- lib/perfectqueue/signal_thread.rb
Instance Method Summary collapse
- #handlers ⇒ Object
-
#initialize(&block) ⇒ SignalThread
constructor
A new instance of SignalThread.
- #stop ⇒ Object
- #trap(sig, command = nil, &block) ⇒ Object
Constructor Details
#initialize(&block) ⇒ SignalThread
Returns a new instance of SignalThread.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/perfectqueue/signal_thread.rb', line 21 def initialize(&block) require 'thread' @handlers = {} @mutex = Mutex.new @cond = ConditionVariable.new @queue = [] @finished = false block.call(self) if block super(&method(:main)) end |
Instance Method Details
#handlers ⇒ Object
56 57 58 |
# File 'lib/perfectqueue/signal_thread.rb', line 56 def handlers @handlers.dup end |
#stop ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/perfectqueue/signal_thread.rb', line 60 def stop @mutex.synchronize do @finished = true @cond.broadcast end self end |
#trap(sig, command = nil, &block) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/perfectqueue/signal_thread.rb', line 36 def trap(sig, command=nil, &block) # normalize signal names sig = sig.to_s.upcase if sig[0,3] == "SIG" sig = sig[3..-1] end sig = sig.to_sym old = @handlers[sig] if block Kernel.trap(sig) { signal_handler_main(sig) } @handlers[sig] = block else Kernel.trap(sig, command) @handlers.delete(sig) end old end |