Class: Async::IO::Trap

Inherits:
Object
  • Object
show all
Defined in:
lib/async/io/trap.rb

Overview

A cross-reactor/process notification pipe.

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Trap

Returns a new instance of Trap.



27
28
29
30
# File 'lib/async/io/trap.rb', line 27

def initialize(name)
	@name = name
	@notifications = []
end

Instance Method Details

#install!Object



32
33
34
35
36
# File 'lib/async/io/trap.rb', line 32

def install!
	Signal.trap(@name, &self.method(:trigger))
	
	return self
end

#trapObject

Block the calling task until the signal occurs.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/async/io/trap.rb', line 39

def trap
	task = Task.current
	task.annotate("waiting for signal #{@name}")
	
	notification = Notification.new
	@notifications << notification
	
	while true
		notification.wait
		yield
	end
ensure
	if notification
		notification.close
		@notifications.delete(notification)
	end
end

#trigger(signal_number = nil) ⇒ void

This method returns an undefined value.

Signal all waiting tasks that the trap occurred.



59
60
61
# File 'lib/async/io/trap.rb', line 59

def trigger(signal_number = nil)
	@notifications.each(&:signal)
end