2020-05-14 06:10:55 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-09-16 05:36:39 -04:00
|
|
|
# This is an example and simplified scheduler for test purposes.
|
|
|
|
# It is not efficient for a large number of file descriptors as it uses IO.select().
|
|
|
|
# Production Fiber schedulers should use epoll/kqueue/etc.
|
|
|
|
|
2020-05-14 06:10:55 -04:00
|
|
|
require 'fiber'
|
2020-07-15 23:19:47 -04:00
|
|
|
require 'socket'
|
2020-05-14 06:10:55 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
require 'io/nonblock'
|
|
|
|
rescue LoadError
|
|
|
|
# Ignore.
|
|
|
|
end
|
|
|
|
|
|
|
|
class Scheduler
|
|
|
|
def initialize
|
|
|
|
@readable = {}
|
|
|
|
@writable = {}
|
|
|
|
@waiting = {}
|
2020-09-05 00:26:24 -04:00
|
|
|
|
2020-09-19 19:34:02 -04:00
|
|
|
@closed = false
|
|
|
|
|
2021-06-28 10:01:53 -04:00
|
|
|
@lock = Thread::Mutex.new
|
2020-09-20 17:54:08 -04:00
|
|
|
@blocking = 0
|
2020-09-17 06:55:17 -04:00
|
|
|
@ready = []
|
2020-09-24 06:29:54 -04:00
|
|
|
|
2020-11-08 01:55:27 -05:00
|
|
|
@urgent = IO.pipe
|
2020-05-14 06:10:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
attr :readable
|
|
|
|
attr :writable
|
|
|
|
attr :waiting
|
|
|
|
|
|
|
|
def next_timeout
|
2020-05-14 12:21:12 -04:00
|
|
|
_fiber, timeout = @waiting.min_by{|key, value| value}
|
2020-05-14 06:10:55 -04:00
|
|
|
|
|
|
|
if timeout
|
|
|
|
offset = timeout - current_time
|
|
|
|
|
|
|
|
if offset < 0
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return offset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2021-02-09 01:39:56 -05:00
|
|
|
# $stderr.puts [__method__, Fiber.current].inspect
|
|
|
|
|
2020-09-20 17:54:08 -04:00
|
|
|
while @readable.any? or @writable.any? or @waiting.any? or @blocking.positive?
|
2020-05-14 06:10:55 -04:00
|
|
|
# Can only handle file descriptors up to 1024...
|
2020-09-05 00:26:24 -04:00
|
|
|
readable, writable = IO.select(@readable.keys + [@urgent.first], @writable.keys, [], next_timeout)
|
2020-05-14 06:10:55 -04:00
|
|
|
|
|
|
|
# puts "readable: #{readable}" if readable&.any?
|
|
|
|
# puts "writable: #{writable}" if writable&.any?
|
|
|
|
|
2021-02-09 01:39:56 -05:00
|
|
|
selected = {}
|
|
|
|
|
2020-05-14 06:10:55 -04:00
|
|
|
readable&.each do |io|
|
2020-09-05 22:48:52 -04:00
|
|
|
if fiber = @readable.delete(io)
|
2021-08-25 06:03:35 -04:00
|
|
|
@writable.delete(io) if @writable[io] == fiber
|
2021-02-09 01:39:56 -05:00
|
|
|
selected[fiber] = IO::READABLE
|
2020-09-05 22:48:52 -04:00
|
|
|
elsif io == @urgent.first
|
|
|
|
@urgent.first.read_nonblock(1024)
|
|
|
|
end
|
2020-05-14 06:10:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
writable&.each do |io|
|
2020-09-05 22:48:52 -04:00
|
|
|
if fiber = @writable.delete(io)
|
2021-08-25 06:03:35 -04:00
|
|
|
@readable.delete(io) if @readable[io] == fiber
|
|
|
|
selected[fiber] = selected.fetch(fiber, 0) | IO::WRITABLE
|
2020-09-05 22:48:52 -04:00
|
|
|
end
|
2020-05-14 06:10:55 -04:00
|
|
|
end
|
|
|
|
|
2021-02-09 01:39:56 -05:00
|
|
|
selected.each do |fiber, events|
|
|
|
|
fiber.resume(events)
|
|
|
|
end
|
|
|
|
|
2020-05-14 06:10:55 -04:00
|
|
|
if @waiting.any?
|
|
|
|
time = current_time
|
2020-09-17 06:55:17 -04:00
|
|
|
waiting, @waiting = @waiting, {}
|
2020-05-14 06:10:55 -04:00
|
|
|
|
|
|
|
waiting.each do |fiber, timeout|
|
2021-02-11 01:17:54 -05:00
|
|
|
if fiber.alive?
|
|
|
|
if timeout <= time
|
|
|
|
fiber.resume
|
|
|
|
else
|
|
|
|
@waiting[fiber] = timeout
|
|
|
|
end
|
2020-05-14 06:10:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-05 00:26:24 -04:00
|
|
|
|
|
|
|
if @ready.any?
|
|
|
|
ready = nil
|
|
|
|
|
|
|
|
@lock.synchronize do
|
2020-09-17 06:55:17 -04:00
|
|
|
ready, @ready = @ready, []
|
2020-09-05 00:26:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
ready.each do |fiber|
|
|
|
|
fiber.resume
|
|
|
|
end
|
|
|
|
end
|
2020-05-14 06:10:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-19 19:34:02 -04:00
|
|
|
def close
|
2021-02-09 01:39:56 -05:00
|
|
|
# $stderr.puts [__method__, Fiber.current].inspect
|
|
|
|
|
2020-09-30 20:43:16 -04:00
|
|
|
raise "Scheduler already closed!" if @closed
|
|
|
|
|
2020-09-19 19:34:02 -04:00
|
|
|
self.run
|
|
|
|
ensure
|
2021-07-28 03:55:55 -04:00
|
|
|
if @urgent
|
|
|
|
@urgent.each(&:close)
|
|
|
|
@urgent = nil
|
|
|
|
end
|
2020-11-08 01:55:27 -05:00
|
|
|
|
2020-09-19 19:34:02 -04:00
|
|
|
@closed = true
|
2020-09-21 10:04:20 -04:00
|
|
|
|
|
|
|
# We freeze to detect any unintended modifications after the scheduler is closed:
|
2020-09-19 19:34:02 -04:00
|
|
|
self.freeze
|
|
|
|
end
|
|
|
|
|
|
|
|
def closed?
|
|
|
|
@closed
|
|
|
|
end
|
|
|
|
|
2020-05-14 06:10:55 -04:00
|
|
|
def current_time
|
|
|
|
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
|
|
end
|
|
|
|
|
2020-12-26 04:09:49 -05:00
|
|
|
def timeout_after(duration, klass, message, &block)
|
2021-02-11 01:17:54 -05:00
|
|
|
fiber = Fiber.current
|
|
|
|
|
|
|
|
self.fiber do
|
|
|
|
sleep(duration)
|
|
|
|
|
|
|
|
if fiber&.alive?
|
|
|
|
fiber.raise(klass, message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
yield(duration)
|
|
|
|
ensure
|
|
|
|
fiber = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-07 15:29:09 -05:00
|
|
|
def process_wait(pid, flags)
|
2021-02-09 01:39:56 -05:00
|
|
|
# $stderr.puts [__method__, pid, flags, Fiber.current].inspect
|
|
|
|
|
2020-12-07 15:29:09 -05:00
|
|
|
# This is a very simple way to implement a non-blocking wait:
|
|
|
|
Thread.new do
|
|
|
|
Process::Status.wait(pid, flags)
|
|
|
|
end.value
|
|
|
|
end
|
|
|
|
|
2020-08-19 21:51:45 -04:00
|
|
|
def io_wait(io, events, duration)
|
2021-02-09 01:39:56 -05:00
|
|
|
# $stderr.puts [__method__, io, events, duration, Fiber.current].inspect
|
|
|
|
|
2020-08-19 21:49:09 -04:00
|
|
|
unless (events & IO::READABLE).zero?
|
2020-05-14 06:10:55 -04:00
|
|
|
@readable[io] = Fiber.current
|
|
|
|
end
|
|
|
|
|
2020-08-19 21:49:09 -04:00
|
|
|
unless (events & IO::WRITABLE).zero?
|
2020-05-14 06:10:55 -04:00
|
|
|
@writable[io] = Fiber.current
|
|
|
|
end
|
|
|
|
|
|
|
|
Fiber.yield
|
|
|
|
end
|
|
|
|
|
2021-06-28 10:01:53 -04:00
|
|
|
# Used for Kernel#sleep and Thread::Mutex#sleep
|
2020-09-17 08:30:40 -04:00
|
|
|
def kernel_sleep(duration = nil)
|
2021-02-09 01:39:56 -05:00
|
|
|
# $stderr.puts [__method__, duration, Fiber.current].inspect
|
|
|
|
|
2020-11-07 22:19:39 -05:00
|
|
|
self.block(:sleep, duration)
|
2020-09-17 08:30:40 -04:00
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2021-06-28 10:01:53 -04:00
|
|
|
# Used when blocking on synchronization (Thread::Mutex#lock,
|
|
|
|
# Thread::Queue#pop, Thread::SizedQueue#push, ...)
|
2020-09-20 17:54:08 -04:00
|
|
|
def block(blocker, timeout = nil)
|
2020-11-08 01:55:27 -05:00
|
|
|
# $stderr.puts [__method__, blocker, timeout].inspect
|
|
|
|
|
2020-09-20 17:54:08 -04:00
|
|
|
if timeout
|
|
|
|
@waiting[Fiber.current] = current_time + timeout
|
2020-09-21 10:04:20 -04:00
|
|
|
begin
|
|
|
|
Fiber.yield
|
|
|
|
ensure
|
|
|
|
# Remove from @waiting in the case #unblock was called before the timeout expired:
|
|
|
|
@waiting.delete(Fiber.current)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@blocking += 1
|
|
|
|
begin
|
|
|
|
Fiber.yield
|
|
|
|
ensure
|
|
|
|
@blocking -= 1
|
|
|
|
end
|
2020-09-20 17:54:08 -04:00
|
|
|
end
|
2020-09-05 00:26:24 -04:00
|
|
|
end
|
|
|
|
|
2021-06-28 10:01:53 -04:00
|
|
|
# Used when synchronization wakes up a previously-blocked fiber
|
|
|
|
# (Thread::Mutex#unlock, Thread::Queue#push, ...).
|
2020-09-17 11:26:52 -04:00
|
|
|
# This might be called from another thread.
|
2020-09-17 08:30:40 -04:00
|
|
|
def unblock(blocker, fiber)
|
2020-11-08 01:55:27 -05:00
|
|
|
# $stderr.puts [__method__, blocker, fiber].inspect
|
2021-02-09 01:39:56 -05:00
|
|
|
# $stderr.puts blocker.backtrace.inspect
|
|
|
|
# $stderr.puts fiber.backtrace.inspect
|
2020-11-08 01:55:27 -05:00
|
|
|
|
2020-09-05 00:26:24 -04:00
|
|
|
@lock.synchronize do
|
|
|
|
@ready << fiber
|
2020-09-05 22:48:52 -04:00
|
|
|
end
|
2020-09-05 00:26:24 -04:00
|
|
|
|
2020-11-08 01:55:27 -05:00
|
|
|
io = @urgent.last
|
|
|
|
io.write_nonblock('.')
|
2020-09-05 00:26:24 -04:00
|
|
|
end
|
|
|
|
|
2020-05-14 06:10:55 -04:00
|
|
|
def fiber(&block)
|
|
|
|
fiber = Fiber.new(blocking: false, &block)
|
|
|
|
|
|
|
|
fiber.resume
|
|
|
|
|
|
|
|
return fiber
|
|
|
|
end
|
2021-06-14 00:21:08 -04:00
|
|
|
|
|
|
|
def address_resolve(hostname)
|
|
|
|
Thread.new do
|
|
|
|
Addrinfo.getaddrinfo(hostname, nil).map(&:ip_address).uniq
|
|
|
|
end.value
|
|
|
|
end
|
2020-05-14 06:10:55 -04:00
|
|
|
end
|
2021-06-14 01:56:53 -04:00
|
|
|
|
|
|
|
class BrokenUnblockScheduler < Scheduler
|
|
|
|
def unblock(blocker, fiber)
|
|
|
|
super
|
|
|
|
|
|
|
|
raise "Broken unblock!"
|
|
|
|
end
|
|
|
|
end
|
2021-07-28 03:55:55 -04:00
|
|
|
|
|
|
|
class SleepingUnblockScheduler < Scheduler
|
|
|
|
# This method is invoked when the thread is exiting.
|
|
|
|
def unblock(blocker, fiber)
|
|
|
|
super
|
|
|
|
|
|
|
|
# This changes the current thread state to `THREAD_RUNNING` which causes `thread_join_sleep` to hang.
|
|
|
|
sleep(0.1)
|
|
|
|
end
|
|
|
|
end
|