Class: Thrifty::Signals
- Inherits:
-
Object
- Object
- Thrifty::Signals
- Includes:
- Singleton
- Defined in:
- lib/thrifty/signals.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Signals
constructor
A new instance of Signals.
- #install ⇒ Object
- #register(fn) ⇒ Object
- #shutdown ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize ⇒ Signals
Returns a new instance of Signals.
14 15 16 17 18 |
# File 'lib/thrifty/signals.rb', line 14 def initialize @handlers = [] @mutex = Mutex.new @resource = ConditionVariable.new end |
Class Method Details
.register(fn) ⇒ Object
9 10 11 |
# File 'lib/thrifty/signals.rb', line 9 def register(fn) instance.register(fn) end |
Instance Method Details
#install ⇒ Object
20 21 22 23 24 |
# File 'lib/thrifty/signals.rb', line 20 def install %w{INT TERM}.each do |sig| trap sig, &method(:shutdown) end end |
#register(fn) ⇒ Object
32 33 34 |
# File 'lib/thrifty/signals.rb', line 32 def register(fn) @handlers << fn end |
#shutdown ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/thrifty/signals.rb', line 36 def shutdown @handlers.each {|fn| fn.call } @mutex.synchronize do @resource.signal end end |
#wait ⇒ Object
26 27 28 29 30 |
# File 'lib/thrifty/signals.rb', line 26 def wait @mutex.synchronize do @resource.wait(@mutex) end end |