Class: RedditGet::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/scheduler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScheduler

Returns a new instance of Scheduler.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/scheduler.rb', line 18

def initialize
  @readable = {}
  @writable = {}
  @waiting = {}

  @closed = false

  @lock = Mutex.new
  @blocking = 0
  @ready = []

  @urgent = IO.pipe
end

Instance Attribute Details

#readableObject (readonly)

Returns the value of attribute readable.



32
33
34
# File 'lib/scheduler.rb', line 32

def readable
  @readable
end

#waitingObject (readonly)

Returns the value of attribute waiting.



32
33
34
# File 'lib/scheduler.rb', line 32

def waiting
  @waiting
end

#writableObject (readonly)

Returns the value of attribute writable.



32
33
34
# File 'lib/scheduler.rb', line 32

def writable
  @writable
end

Instance Method Details

#block(_blocker, timeout = nil) ⇒ Object

Used when blocking on synchronization (Mutex#lock, Queue#pop, SizedQueue#push, ...)



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/scheduler.rb', line 144

def block(_blocker, timeout = nil)
  # $stderr.puts [__method__, blocker, timeout].inspect

  if timeout
    @waiting[Fiber.current] = current_time + timeout
    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
  end
end

#closeObject



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/scheduler.rb', line 98

def close
  raise 'Scheduler already closed!' if @closed

  run
ensure
  @urgent.each(&:close)
  @urgent = nil

  @closed = true

  # We freeze to detect any unintended modifications after the scheduler is closed:
  freeze
end

#closed?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/scheduler.rb', line 112

def closed?
  @closed
end

#current_timeObject



116
117
118
# File 'lib/scheduler.rb', line 116

def current_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

#fiber(&block) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/scheduler.rb', line 178

def fiber(&block)
  fiber = Fiber.new(blocking: false, &block)

  fiber.resume

  fiber
end

#io_wait(io, events, _duration) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/scheduler.rb', line 127

def io_wait(io, events, _duration)
  @readable[io] = Fiber.current unless (events & IO::READABLE).zero?

  @writable[io] = Fiber.current unless (events & IO::WRITABLE).zero?

  Fiber.yield
  events
end

#kernel_sleep(duration = nil) ⇒ Object

Used for Kernel#sleep and Mutex#sleep



137
138
139
140
141
# File 'lib/scheduler.rb', line 137

def kernel_sleep(duration = nil)
  block(:sleep, duration)

  true
end

#next_timeoutObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/scheduler.rb', line 34

def next_timeout
  _fiber, timeout = @waiting.min_by { |_key, value| value }

  if timeout
    offset = timeout - current_time

    if offset.negative?
      0
    else
      offset
    end
  end
end

#process_wait(pid, flags) ⇒ Object



120
121
122
123
124
125
# File 'lib/scheduler.rb', line 120

def process_wait(pid, flags)
  # This is a very simple way to implement a non-blocking wait:
  Thread.new do
    Process::Status.wait(pid, flags)
  end.value
end

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/scheduler.rb', line 48

def run
  while @readable.any? || @writable.any? || @waiting.any? || @blocking.positive?
    # Can only handle file descriptors up to 1024...
    readable, writable = IO.select(@readable.keys + [@urgent.first], @writable.keys, [],
                                   next_timeout)

    # puts "readable: #{readable}" if readable&.any?
    # puts "writable: #{writable}" if writable&.any?

    readable&.each do |io|
      if fiber = @readable.delete(io)
        fiber.resume
      elsif io == @urgent.first
        @urgent.first.read_nonblock(1024)
      end
    end

    writable&.each do |io|
      if fiber = @writable.delete(io)
        fiber.resume
      end
    end

    if @waiting.any?
      time = current_time
      waiting = @waiting
      @waiting = {}

      waiting.each do |fiber, timeout|
        if timeout <= time
          fiber.resume
        else
          @waiting[fiber] = timeout
        end
      end
    end

    next unless @ready.any?

    ready = nil

    @lock.synchronize do
      ready = @ready
      @ready = []
    end

    ready.each(&:resume)
  end
end

#unblock(_blocker, fiber) ⇒ Object

Used when synchronization wakes up a previously-blocked fiber (Mutex#unlock, Queue#push, ...). This might be called from another thread.



167
168
169
170
171
172
173
174
175
176
# File 'lib/scheduler.rb', line 167

def unblock(_blocker, fiber)
  # $stderr.puts [__method__, blocker, fiber].inspect

  @lock.synchronize do
    @ready << fiber
  end

  io = @urgent.last
  io.write_nonblock('.')
end