Class: Async::Wrapper
- Inherits:
-
Object
- Object
- Async::Wrapper
- Defined in:
- lib/async/wrapper.rb
Overview
Represents an asynchronous IO within a reactor.
Defined Under Namespace
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
The underlying native
io
. -
#monitor ⇒ Object
readonly
The monitor for this wrapper, if any.
-
#reactor ⇒ Object
The reactor this wrapper is associated with, if any.
Instance Method Summary collapse
-
#close ⇒ Object
Close the io and monitor.
- #closed? ⇒ Boolean
- #dup ⇒ Object
-
#initialize(io, reactor = nil) ⇒ Wrapper
constructor
A new instance of Wrapper.
- #resume(*args) ⇒ Object
-
#wait_any(timeout = nil) ⇒ Object
Wait fo the io to become either readable or writable.
-
#wait_readable(timeout = nil) ⇒ Object
Wait for the io to become readable.
-
#wait_writable(timeout = nil) ⇒ Object
Wait for the io to become writable.
Constructor Details
#initialize(io, reactor = nil) ⇒ Wrapper
Returns a new instance of Wrapper.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/async/wrapper.rb', line 61 def initialize(io, reactor = nil) @io = io @reactor = reactor @monitor = nil @readable = nil @writable = nil @any = nil end |
Instance Attribute Details
#io ⇒ Object (readonly)
The underlying native io
.
96 97 98 |
# File 'lib/async/wrapper.rb', line 96 def io @io end |
#monitor ⇒ Object (readonly)
The monitor for this wrapper, if any.
102 103 104 |
# File 'lib/async/wrapper.rb', line 102 def monitor @monitor end |
#reactor ⇒ Object
The reactor this wrapper is associated with, if any.
99 100 101 |
# File 'lib/async/wrapper.rb', line 99 def reactor @reactor end |
Instance Method Details
#close ⇒ Object
Close the io and monitor.
161 162 163 164 165 |
# File 'lib/async/wrapper.rb', line 161 def close cancel_monitor @io.close end |
#closed? ⇒ Boolean
167 168 169 |
# File 'lib/async/wrapper.rb', line 167 def closed? @io.closed? end |
#dup ⇒ Object
72 73 74 |
# File 'lib/async/wrapper.rb', line 72 def dup self.class.new(@io.dup, @reactor) end |
#resume(*args) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/async/wrapper.rb', line 76 def resume(*args) # It's possible that the monitor was closed before calling resume. return unless @monitor readiness = @monitor.readiness if @readable and (readiness == :r or readiness == :rw) @readable.resume(*args) end if @writable and (readiness == :w or readiness == :rw) @writable.resume(*args) end if @any @any.resume(*args) end end |
#wait_any(timeout = nil) ⇒ Object
Wait fo the io to become either readable or writable.
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/async/wrapper.rb', line 146 def wait_any(timeout = nil) raise WaitError if @any self.reactor = Task.current.reactor begin @any = Fiber.current wait_for(timeout) ensure @any = nil @monitor.interests = interests if @monitor end end |
#wait_readable(timeout = nil) ⇒ Object
Wait for the io to become readable.
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/async/wrapper.rb', line 115 def wait_readable(timeout = nil) raise WaitError if @readable self.reactor = Task.current.reactor begin @readable = Fiber.current wait_for(timeout) ensure @readable = nil @monitor.interests = interests if @monitor end end |
#wait_writable(timeout = nil) ⇒ Object
Wait for the io to become writable.
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/async/wrapper.rb', line 130 def wait_writable(timeout = nil) raise WaitError if @writable self.reactor = Task.current.reactor begin @writable = Fiber.current wait_for(timeout) ensure @writable = nil @monitor.interests = interests if @monitor end end |