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(*arguments) ⇒ 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.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/async/wrapper.rb', line 63 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
.
98 99 100 |
# File 'lib/async/wrapper.rb', line 98 def io @io end |
#monitor ⇒ Object (readonly)
The monitor for this wrapper, if any.
104 105 106 |
# File 'lib/async/wrapper.rb', line 104 def monitor @monitor end |
#reactor ⇒ Object
The reactor this wrapper is associated with, if any.
101 102 103 |
# File 'lib/async/wrapper.rb', line 101 def reactor @reactor end |
Instance Method Details
#close ⇒ Object
Close the io and monitor.
163 164 165 166 167 |
# File 'lib/async/wrapper.rb', line 163 def close cancel_monitor @io.close end |
#closed? ⇒ Boolean
169 170 171 |
# File 'lib/async/wrapper.rb', line 169 def closed? @io.closed? end |
#dup ⇒ Object
74 75 76 |
# File 'lib/async/wrapper.rb', line 74 def dup self.class.new(@io.dup, @reactor) end |
#resume(*arguments) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/async/wrapper.rb', line 78 def resume(*arguments) # 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(*arguments) end if @writable and (readiness == :w or readiness == :rw) @writable.resume(*arguments) end if @any @any.resume(*arguments) end end |
#wait_any(timeout = nil) ⇒ Object
Wait fo the io to become either readable or writable.
148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/async/wrapper.rb', line 148 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.
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/async/wrapper.rb', line 117 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.
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/async/wrapper.rb', line 132 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 |