Class: Async::IO::Trap
- Inherits:
-
Object
- Object
- Async::IO::Trap
- Defined in:
- lib/async/io/trap.rb
Overview
A cross-reactor/process notification pipe.
Instance Method Summary collapse
- #async(parent: Task.current, &block) ⇒ Object
- #default! ⇒ Object
-
#ignore! ⇒ Object
Ignore the trap within the current process.
-
#initialize(name) ⇒ Trap
constructor
A new instance of Trap.
-
#install! ⇒ Boolean
Install the trap into the current process.
-
#trigger(signal_number = nil) ⇒ void
Signal all waiting tasks that the trap occurred.
-
#wait(task: Task.current) {|Async::Task| ... } ⇒ Object
(also: #trap)
Wait until the signal occurs.
Constructor Details
#initialize(name) ⇒ Trap
Returns a new instance of Trap.
31 32 33 34 35 36 37 |
# File 'lib/async/io/trap.rb', line 31 def initialize(name) @name = name @notifications = [] @installed = false @mutex = Mutex.new end |
Instance Method Details
#async(parent: Task.current, &block) ⇒ Object
90 91 92 93 94 |
# File 'lib/async/io/trap.rb', line 90 def async(parent: Task.current, &block) parent.async do |task| self.trap(task: task, &block) end end |
#default! ⇒ Object
44 45 46 |
# File 'lib/async/io/trap.rb', line 44 def default! Signal.trap(@name, :DEFAULT) end |
#ignore! ⇒ Object
Ignore the trap within the current process. Can be invoked before forking and/or invoking install!
to assert default behaviour.
40 41 42 |
# File 'lib/async/io/trap.rb', line 40 def ignore! Signal.trap(@name, :IGNORE) end |
#install! ⇒ Boolean
Install the trap into the current process. Thread safe.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/async/io/trap.rb', line 50 def install! return if @installed @mutex.synchronize do return if @installed Signal.trap(@name, &self.method(:trigger)) @installed = true end return true end |
#trigger(signal_number = nil) ⇒ void
This method returns an undefined value.
Signal all waiting tasks that the trap occurred.
98 99 100 |
# File 'lib/async/io/trap.rb', line 98 def trigger(signal_number = nil) @notifications.each(&:signal) end |
#wait(task: Task.current) {|Async::Task| ... } ⇒ Object Also known as: trap
Wait until the signal occurs. If a block is given, execute in a loop.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/async/io/trap.rb', line 66 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 |