Class: Thrifty::Signals

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/thrifty/signals.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSignals

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

#installObject



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

#shutdownObject



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

#waitObject



26
27
28
29
30
# File 'lib/thrifty/signals.rb', line 26

def wait
  @mutex.synchronize do
    @resource.wait(@mutex)
  end
end