/
reactor.rb
46 lines (40 loc) · 1.02 KB
/
reactor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'async/reactor'
module Puma
# Runs an Async::Reactor in a separate thread.
class Reactor < Async::Reactor
def initialize
super
@stopping = false
@thread = Thread.new do
Puma.set_thread_name "reactor"
run {Async::Task.yield}
end
end
# Schedule an asynchronous task to run on the Reactor thread.
# Returns `true` if scheduling is successful, or `nil` if the
# Reactor has already been stopped.
def async(&block)
unless @stopping
later {super}
true
end
end
# Stop the Reactor and wait for the running thread to finish.
def stop(*args)
@stopping = true
later {super}
@thread.join
end
private
Later = Struct.new(:block) do
def alive?; true end
def resume; block.call end
end
# Schedules the provided block to be run on the Reactor thread
# on the next loop through the reactor.
def later(&block)
self << Later.new(block)
@selector.wakeup
end
end
end