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.
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
#async(parent: Task.current, &block) ⇒ Object
88 89 90 91 92 |
# File 'lib/async/io/trap.rb', line 88 def async(parent: Task.current, &block) parent.async do |task| self.trap(task: task, &block) end end |
#default! ⇒ Object
42 43 44 |
# File 'lib/async/io/trap.rb', line 42 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.
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.
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/async/io/trap.rb', line 48 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.
96 97 98 |
# File 'lib/async/io/trap.rb', line 96 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.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/async/io/trap.rb', line 64 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 |