Class: FiberScheduler
- Inherits:
-
Object
show all
- Defined in:
- lib/fiber_scheduler/selector.rb,
lib/fiber_scheduler.rb,
lib/fiber_scheduler/timeout.rb,
lib/fiber_scheduler/version.rb,
lib/fiber_scheduler/timeouts.rb,
lib/fiber_scheduler/compatibility.rb
Overview
The code in this class has been taken from io-event gem. It has been cleaned up and appropriated for use in this gem.
Defined Under Namespace
Modules: Compatibility
Classes: Selector, Timeout, Timeouts
Constant Summary
collapse
- Error =
Class.new(RuntimeError)
- VERSION =
"0.13.0".freeze
Instance Method Summary
collapse
-
#address_resolve(hostname) ⇒ Object
-
#block(blocker, duration = nil) ⇒ Object
-
#close ⇒ Object
Fiber::SchedulerInterface methods below.
-
#fiber(type = nil, &block) ⇒ Object
-
#initialize ⇒ FiberScheduler
constructor
A new instance of FiberScheduler.
-
#io_read(io, buffer, length) ⇒ Object
-
#io_wait(io, events, duration = nil) ⇒ Object
-
#io_write(io, buffer, length) ⇒ Object
-
#kernel_sleep(duration = nil) ⇒ Object
-
#process_wait(pid, flags) ⇒ Object
-
#run ⇒ Object
-
#run_once ⇒ Object
-
#timeout_after(duration, exception = Timeout::Error, message = "timeout", &block) ⇒ Object
-
#unblock(blocker, fiber) ⇒ Object
Constructor Details
Returns a new instance of FiberScheduler.
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/fiber_scheduler.rb', line 100
def initialize
@fiber = Fiber.current
@selector =
if defined?(IO::Event)
IO::Event::Selector.new(@fiber)
else
Selector.new(@fiber)
end
@timeouts = Timeouts.new
@count = 0
@nested = []
end
|
Instance Method Details
#address_resolve(hostname) ⇒ Object
163
164
165
|
# File 'lib/fiber_scheduler.rb', line 163
def address_resolve(hostname)
Resolv.getaddresses(hostname)
end
|
#block(blocker, duration = nil) ⇒ Object
145
146
147
148
149
150
151
|
# File 'lib/fiber_scheduler.rb', line 145
def block(blocker, duration = nil)
return @selector.transfer unless duration
@timeouts.timeout(duration, method: :transfer) do
@selector.transfer
end
end
|
#close ⇒ Object
Fiber::SchedulerInterface methods below
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/fiber_scheduler.rb', line 134
def close
return unless @selector
begin
run
ensure
@selector.close
@selector = nil
end
end
|
#fiber(type = nil, &block) ⇒ Object
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# File 'lib/fiber_scheduler.rb', line 191
def fiber(type = nil, &block)
current = Fiber.current
case type
when :blocking
Fiber.new(blocking: true, &block).tap(&:resume)
when :waiting
finished = false fiber = Fiber.new(blocking: false) do
@count += 1
block.call
ensure
@count -= 1
finished = true
current.transfer
end
fiber.transfer
if current == @fiber
until finished
run_once
end
elsif !finished
@selector.transfer
end
fiber
when :volatile
if current != @fiber
@nested << current
end
Fiber.new(blocking: false, &block).tap(&:transfer)
when nil
if current != @fiber
@nested << current
end
fiber = Fiber.new(blocking: false) do
@count += 1
block.call
ensure
@count -= 1
end
fiber.tap(&:transfer)
else
raise "Unknown type"
end
end
|
#io_read(io, buffer, length) ⇒ Object
175
176
177
|
# File 'lib/fiber_scheduler.rb', line 175
def io_read(io, buffer, length)
@selector.io_read(Fiber.current, io, buffer, length)
end
|
#io_wait(io, events, duration = nil) ⇒ Object
167
168
169
170
171
172
173
|
# File 'lib/fiber_scheduler.rb', line 167
def io_wait(io, events, duration = nil)
return @selector.io_wait(Fiber.current, io, events) unless duration
@timeouts.timeout(duration, method: :transfer) do
@selector.io_wait(Fiber.current, io, events)
end
end
|
#io_write(io, buffer, length) ⇒ Object
179
180
181
|
# File 'lib/fiber_scheduler.rb', line 179
def io_write(io, buffer, length)
@selector.io_write(Fiber.current, io, buffer, length)
end
|
#kernel_sleep(duration = nil) ⇒ Object
157
158
159
160
161
|
# File 'lib/fiber_scheduler.rb', line 157
def kernel_sleep(duration = nil)
return @selector.transfer unless duration
block(:sleep, duration)
end
|
#process_wait(pid, flags) ⇒ Object
183
184
185
|
# File 'lib/fiber_scheduler.rb', line 183
def process_wait(pid, flags)
@selector.process_wait(Fiber.current, pid, flags)
end
|
#run ⇒ Object
114
115
116
117
118
|
# File 'lib/fiber_scheduler.rb', line 114
def run
while @count > 0
run_once
end
end
|
#run_once ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/fiber_scheduler.rb', line 120
def run_once
if @nested.empty?
@selector.select(@timeouts.interval)
@timeouts.call
else
while @nested.any?
fiber = @nested.pop
fiber.transfer
end
end
end
|
#timeout_after(duration, exception = Timeout::Error, message = "timeout", &block) ⇒ Object
187
188
189
|
# File 'lib/fiber_scheduler.rb', line 187
def timeout_after(duration, exception = Timeout::Error, message = "timeout", &block)
@timeouts.timeout(duration, exception, message, &block)
end
|
#unblock(blocker, fiber) ⇒ Object
153
154
155
|
# File 'lib/fiber_scheduler.rb', line 153
def unblock(blocker, fiber)
@selector.push(fiber)
end
|