class Async::IO::Trap

A cross-reactor/process notification pipe.

def async(parent: Task.current, &block)

def async(parent: Task.current, &block)
	parent.async do |task|
		self.trap(task: task, &block)
	end
end

def default!

def default!
	Signal.trap(@name, :DEFAULT)
end

def ignore!

Ignore the trap within the current process. Can be invoked before forking and/or invoking `install!` to assert default behaviour.
def ignore!
	Signal.trap(@name, :IGNORE)
end

def initialize(name)

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

def install!

Returns:
  • (Boolean) - whether the trap was installed or not. If the trap was already installed, returns nil.
def install!
	return if @installed
	
	@mutex.synchronize do
		return if @installed
		
		Signal.trap(@name, &self.method(:trigger))
		
		@installed = true
	end
	
	return true
end

def trigger(signal_number = nil)

Returns:
  • (void) -
def trigger(signal_number = nil)
	@notifications.each(&:signal)
end

def wait(task: Task.current, &block)

Other tags:
    Yield: - the current task.
def wait(task: Task.current, &block)
	task.annotate("waiting for signal #{@name}")
	
	notification = Notification.new
	@notifications << notification
	
	if block_given?
		while true
			notification.wait
			yield task
		end
	else
		notification.wait
	end
ensure
	if notification
		notification.close
		@notifications.delete(notification)
	end
end