Class: FiberScheduler::Selector
- Inherits:
-
Object
- Object
- FiberScheduler::Selector
- Defined in:
- lib/fiber_scheduler/selector.rb
Defined Under Namespace
Classes: Waiter
Constant Summary collapse
- EAGAIN =
Errno::EAGAIN::Errno
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(fiber) ⇒ Selector
constructor
A new instance of Selector.
- #io_read(fiber, io, buffer, length) ⇒ Object
- #io_wait(fiber, io, events) ⇒ Object
- #io_write(fiber, io, buffer, length) ⇒ Object
- #process_wait(fiber, pid, flags) ⇒ Object
- #push(fiber) ⇒ Object
- #select(duration = nil) ⇒ Object
- #transfer ⇒ Object
Constructor Details
#initialize(fiber) ⇒ Selector
Returns a new instance of Selector.
62 63 64 65 66 67 |
# File 'lib/fiber_scheduler/selector.rb', line 62 def initialize(fiber) @fiber = fiber @waiting = {}.compare_by_identity @ready = [] end |
Instance Method Details
#close ⇒ Object
69 70 71 72 73 |
# File 'lib/fiber_scheduler/selector.rb', line 69 def close @fiber = nil @waiting = nil @ready = nil end |
#io_read(fiber, io, buffer, length) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/fiber_scheduler/selector.rb', line 91 def io_read(fiber, io, buffer, length) offset = 0 loop do maximum_size = buffer.size - offset result = Fiber.new(blocking: true) { io.read_nonblock(maximum_size, exception: false) }.resume case result when :wait_readable if length > 0 io_wait(fiber, io, IO::READABLE) else return -EAGAIN end when :wait_writable if length > 0 io_wait(fiber, io, IO::WRITABLE) else return -EAGAIN end when nil break else buffer.set_string(result, offset) size = result.bytesize offset += size break if size >= length length -= size end end offset end |
#io_wait(fiber, io, events) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/fiber_scheduler/selector.rb', line 83 def io_wait(fiber, io, events) waiter = @waiting[io] = Waiter.new(fiber, events, @waiting[io]) @fiber.transfer ensure waiter&.invalidate end |
#io_write(fiber, io, buffer, length) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/fiber_scheduler/selector.rb', line 129 def io_write(fiber, io, buffer, length) offset = 0 loop do maximum_size = buffer.size - offset chunk = buffer.get_string(offset, maximum_size) result = Fiber.new(blocking: true) { io.write_nonblock(chunk, exception: false) }.resume case result when :wait_readable if length > 0 io_wait(fiber, io, IO::READABLE) else return -EAGAIN end when :wait_writable if length > 0 io_wait(fiber, io, IO::WRITABLE) else return -EAGAIN end else offset += result break if result >= length length -= result end end offset end |
#process_wait(fiber, pid, flags) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/fiber_scheduler/selector.rb', line 163 def process_wait(fiber, pid, flags) reader, writer = IO.pipe thread = Thread.new do Process::Status.wait(pid, flags) ensure writer.close end io_wait(fiber, reader, IO::READABLE) thread.value ensure reader.close writer.close thread&.kill end |
#push(fiber) ⇒ Object
79 80 81 |
# File 'lib/fiber_scheduler/selector.rb', line 79 def push(fiber) @ready.push(fiber) end |
#select(duration = nil) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/fiber_scheduler/selector.rb', line 181 def select(duration = nil) if @ready.any? # If we have popped items from the ready list, they may influence the # duration calculation, so we don't delay the event loop: duration = 0 count = @ready.size count.times do fiber = @ready.shift fiber.transfer if fiber.alive? end end readable = [] writable = [] @waiting.each do |io, waiter| waiter.each do |fiber, events| if (events & IO::READABLE) > 0 readable << io end if (events & IO::WRITABLE) > 0 writable << io end end end duration = 0 if @ready.any? readable, writable, _ = IO.select(readable, writable, nil, duration) ready = Hash.new(0) readable&.each do |io| ready[io] |= IO::READABLE end writable&.each do |io| ready[io] |= IO::WRITABLE end ready.each do |io, events| waiter = @waiting.delete(io) waiter.transfer(events) end ready.size end |
#transfer ⇒ Object
75 76 77 |
# File 'lib/fiber_scheduler/selector.rb', line 75 def transfer @fiber.transfer end |