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.



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

def initialize(name)
	@name = name
	@notifications = []
	
	@installed = false
	@mutex = Mutex.new
end

Instance Method Details

#ignore!Object

Ignore the trap within the current process. Can be invoked before forking and/or invoking ‘install!` to assert default behaviour.



38
39
40
# File 'lib/async/io/trap.rb', line 38

def ignore!
	Signal.trap(@name, "IGNORE")
end

#install!Boolean

Install the trap into the current process. Thread safe.

Returns:

  • (Boolean)

    whether the trap was installed or not. If the trap was already installed, returns nil.



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

def install!
	return if @installed
	
	@mutex.synchronize do
		return if @installed
		
		Signal.trap(@name, &self.method(:trigger))
		
		@installed = true
	end
	
	return true
end

#trapObject

Block the calling task until the signal occurs.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/async/io/trap.rb', line 59

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.



79
80
81
# File 'lib/async/io/trap.rb', line 79

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