Async::IO SourceAsyncIOTrap

class Trap

A cross-reactor/process notification pipe.

Definitions

def ignore!

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

Implementation

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

def install!

Install the trap into the current process. Thread safe.

Implementation

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

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

Wait until the signal occurs. If a block is given, execute in a loop.

Implementation

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

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

In order to avoid blocking the reactor, specify transient: true as an option.

Implementation

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

def trigger(signal_number = nil)

Signal all waiting tasks that the trap occurred.

Implementation

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