polyphony 0.34 → 0.41
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +11 -2
- data/.gitignore +2 -2
- data/.rubocop.yml +30 -0
- data/CHANGELOG.md +34 -0
- data/Gemfile +0 -11
- data/Gemfile.lock +11 -10
- data/README.md +2 -1
- data/Rakefile +6 -2
- data/TODO.md +18 -95
- data/docs/_includes/head.html +40 -0
- data/docs/_includes/nav.html +5 -5
- data/docs/api-reference.md +1 -1
- data/docs/api-reference/fiber.md +18 -0
- data/docs/api-reference/gyro-async.md +57 -0
- data/docs/api-reference/gyro-child.md +29 -0
- data/docs/api-reference/gyro-queue.md +44 -0
- data/docs/api-reference/gyro-timer.md +51 -0
- data/docs/api-reference/gyro.md +25 -0
- data/docs/index.md +10 -7
- data/docs/main-concepts/design-principles.md +67 -9
- data/docs/main-concepts/extending.md +1 -1
- data/docs/main-concepts/fiber-scheduling.md +55 -72
- data/examples/core/xx-agent.rb +102 -0
- data/examples/core/xx-fork-cleanup.rb +22 -0
- data/examples/core/xx-sleeping.rb +14 -6
- data/examples/core/xx-timer-gc.rb +17 -0
- data/examples/io/tunnel.rb +48 -0
- data/examples/io/xx-irb.rb +1 -1
- data/examples/performance/thread-vs-fiber/polyphony_mt_server.rb +7 -6
- data/examples/performance/thread-vs-fiber/polyphony_server.rb +14 -25
- data/ext/{gyro → polyphony}/extconf.rb +2 -2
- data/ext/polyphony/fiber.c +112 -0
- data/ext/{gyro → polyphony}/libev.c +0 -0
- data/ext/{gyro → polyphony}/libev.h +0 -0
- data/ext/polyphony/libev_agent.c +503 -0
- data/ext/polyphony/libev_queue.c +214 -0
- data/ext/polyphony/polyphony.c +89 -0
- data/ext/{gyro/gyro.h → polyphony/polyphony.h} +49 -59
- data/ext/polyphony/polyphony_ext.c +23 -0
- data/ext/{gyro → polyphony}/socket.c +21 -19
- data/ext/{gyro → polyphony}/thread.c +55 -119
- data/ext/{gyro → polyphony}/tracing.c +1 -1
- data/lib/polyphony.rb +37 -44
- data/lib/polyphony/adapters/fs.rb +1 -4
- data/lib/polyphony/adapters/irb.rb +2 -2
- data/lib/polyphony/adapters/postgres.rb +6 -5
- data/lib/polyphony/adapters/process.rb +27 -23
- data/lib/polyphony/adapters/trace.rb +110 -105
- data/lib/polyphony/core/channel.rb +35 -35
- data/lib/polyphony/core/exceptions.rb +29 -29
- data/lib/polyphony/core/global_api.rb +94 -91
- data/lib/polyphony/core/resource_pool.rb +83 -83
- data/lib/polyphony/core/sync.rb +16 -16
- data/lib/polyphony/core/thread_pool.rb +49 -37
- data/lib/polyphony/core/throttler.rb +30 -23
- data/lib/polyphony/event.rb +27 -0
- data/lib/polyphony/extensions/core.rb +23 -14
- data/lib/polyphony/extensions/fiber.rb +269 -267
- data/lib/polyphony/extensions/io.rb +56 -26
- data/lib/polyphony/extensions/openssl.rb +5 -9
- data/lib/polyphony/extensions/socket.rb +29 -10
- data/lib/polyphony/extensions/thread.rb +19 -12
- data/lib/polyphony/net.rb +64 -60
- data/lib/polyphony/version.rb +1 -1
- data/polyphony.gemspec +3 -6
- data/test/helper.rb +14 -1
- data/test/stress.rb +17 -12
- data/test/test_agent.rb +77 -0
- data/test/{test_async.rb → test_event.rb} +17 -9
- data/test/test_ext.rb +25 -4
- data/test/test_fiber.rb +23 -14
- data/test/test_global_api.rb +5 -5
- data/test/test_io.rb +46 -24
- data/test/test_queue.rb +74 -0
- data/test/test_signal.rb +3 -40
- data/test/test_socket.rb +33 -0
- data/test/test_thread.rb +38 -16
- data/test/test_thread_pool.rb +3 -3
- data/test/test_throttler.rb +0 -1
- data/test/test_trace.rb +6 -5
- metadata +34 -39
- data/ext/gyro/async.c +0 -158
- data/ext/gyro/child.c +0 -117
- data/ext/gyro/gyro.c +0 -203
- data/ext/gyro/gyro_ext.c +0 -31
- data/ext/gyro/io.c +0 -447
- data/ext/gyro/queue.c +0 -142
- data/ext/gyro/selector.c +0 -183
- data/ext/gyro/signal.c +0 -108
- data/ext/gyro/timer.c +0 -154
- data/test/test_timer.rb +0 -56
data/lib/polyphony/core/sync.rb
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
module Polyphony
|
4
|
+
# Implements mutex lock for synchronizing access to a shared resource
|
5
|
+
class Mutex
|
6
|
+
def initialize
|
7
|
+
@waiting_fibers = Polyphony::Queue.new
|
8
|
+
end
|
4
9
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
yield
|
16
|
-
ensure
|
17
|
-
@waiting_fibers.delete(fiber)
|
18
|
-
@waiting_fibers.first&.schedule
|
19
|
-
snooze
|
10
|
+
def synchronize
|
11
|
+
fiber = Fiber.current
|
12
|
+
@waiting_fibers << fiber
|
13
|
+
suspend if @waiting_fibers.size > 1
|
14
|
+
yield
|
15
|
+
ensure
|
16
|
+
@waiting_fibers.delete(fiber)
|
17
|
+
@waiting_fibers.first&.schedule
|
18
|
+
snooze
|
19
|
+
end
|
20
20
|
end
|
21
21
|
end
|
@@ -1,52 +1,64 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
export_default :ThreadPool
|
4
|
-
|
5
3
|
require 'etc'
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
module Polyphony
|
6
|
+
# Implements a pool of threads
|
7
|
+
class ThreadPool
|
8
|
+
attr_reader :size
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
def self.process(&block)
|
11
|
+
@default_pool ||= new
|
12
|
+
@default_pool.process(&block)
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
@task_queue = ::Queue.new
|
19
|
-
@threads = (1..@size).map { Thread.new { thread_loop } }
|
20
|
-
end
|
15
|
+
def self.reset
|
16
|
+
return unless @default_pool
|
21
17
|
|
22
|
-
|
23
|
-
|
18
|
+
@default_pool.stop
|
19
|
+
@default_pool = nil
|
20
|
+
end
|
24
21
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
22
|
+
def initialize(size = Etc.nprocessors)
|
23
|
+
@size = size
|
24
|
+
@task_queue = Polyphony::Queue.new
|
25
|
+
@threads = (1..@size).map { Thread.new { thread_loop } }
|
26
|
+
end
|
29
27
|
|
30
|
-
|
31
|
-
|
28
|
+
def process(&block)
|
29
|
+
setup unless @task_queue
|
32
30
|
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
watcher = Fiber.current.auto_watcher
|
32
|
+
@task_queue << [block, watcher]
|
33
|
+
watcher.await
|
34
|
+
end
|
36
35
|
|
37
|
-
|
38
|
-
|
39
|
-
end
|
36
|
+
def cast(&block)
|
37
|
+
setup unless @task_queue
|
40
38
|
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
@task_queue << [block, nil]
|
40
|
+
self
|
41
|
+
end
|
42
|
+
|
43
|
+
def busy?
|
44
|
+
!@task_queue.empty?
|
45
|
+
end
|
46
|
+
|
47
|
+
def thread_loop
|
48
|
+
loop { run_queued_task }
|
49
|
+
end
|
50
|
+
|
51
|
+
def run_queued_task
|
52
|
+
(block, watcher) = @task_queue.pop
|
53
|
+
result = block.()
|
54
|
+
watcher&.signal(result)
|
55
|
+
rescue Exception => e
|
56
|
+
watcher ? watcher.signal(e) : raise(e)
|
57
|
+
end
|
44
58
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
rescue Exception => e
|
50
|
-
watcher ? watcher.signal!(e) : raise(e)
|
59
|
+
def stop
|
60
|
+
@threads.each(&:kill)
|
61
|
+
@threads.each(&:join)
|
62
|
+
end
|
51
63
|
end
|
52
64
|
end
|
@@ -1,34 +1,41 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
module Polyphony
|
4
|
+
# Implements general-purpose throttling
|
5
|
+
class Throttler
|
6
|
+
def initialize(rate)
|
7
|
+
@rate = rate_from_argument(rate)
|
8
|
+
@min_dt = 1.0 / @rate
|
9
|
+
@next_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
10
|
+
end
|
4
11
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
12
|
+
def call
|
13
|
+
now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
14
|
+
delta = @next_time - now
|
15
|
+
Thread.current.agent.sleep(delta) if delta > 0
|
16
|
+
yield self
|
11
17
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
+
loop do
|
19
|
+
@next_time += @min_dt
|
20
|
+
break if @next_time > now
|
21
|
+
end
|
22
|
+
end
|
23
|
+
alias_method :process, :call
|
18
24
|
|
19
|
-
|
20
|
-
|
21
|
-
|
25
|
+
def stop
|
26
|
+
@stop = true
|
27
|
+
end
|
22
28
|
|
23
|
-
|
29
|
+
private
|
24
30
|
|
25
|
-
|
26
|
-
|
31
|
+
def rate_from_argument(arg)
|
32
|
+
return arg if arg.is_a?(Numeric)
|
27
33
|
|
28
|
-
|
29
|
-
|
30
|
-
|
34
|
+
if arg.is_a?(Hash)
|
35
|
+
return 1.0 / arg[:interval] if arg[:interval]
|
36
|
+
return arg[:rate] if arg[:rate]
|
37
|
+
end
|
38
|
+
raise "Invalid rate argument #{arg.inspect}"
|
31
39
|
end
|
32
|
-
raise "Invalid rate argument #{arg.inspect}"
|
33
40
|
end
|
34
41
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Polyphony
|
4
|
+
# Event watcher for thread-safe synchronisation
|
5
|
+
class Event
|
6
|
+
def initialize
|
7
|
+
@i, @o = IO.pipe
|
8
|
+
end
|
9
|
+
|
10
|
+
def await
|
11
|
+
Thread.current.agent.read(@i, +'', 8192, false)
|
12
|
+
raise @value if @value.is_a?(Exception)
|
13
|
+
|
14
|
+
@value
|
15
|
+
end
|
16
|
+
|
17
|
+
def await_no_raise
|
18
|
+
Thread.current.agent.read(@i, +'', 8192, false)
|
19
|
+
@value
|
20
|
+
end
|
21
|
+
|
22
|
+
def signal(value = nil)
|
23
|
+
@value = value
|
24
|
+
Thread.current.agent.write(@o, '1')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -4,7 +4,7 @@ require 'fiber'
|
|
4
4
|
require 'timeout'
|
5
5
|
require 'open3'
|
6
6
|
|
7
|
-
|
7
|
+
require_relative '../core/exceptions'
|
8
8
|
|
9
9
|
# Exeption overrides
|
10
10
|
class ::Exception
|
@@ -22,7 +22,7 @@ class ::Exception
|
|
22
22
|
orig_initialize(*args)
|
23
23
|
end
|
24
24
|
|
25
|
-
|
25
|
+
alias_method :orig_backtrace, :backtrace
|
26
26
|
def backtrace
|
27
27
|
unless @first_backtrace_call || EXIT_EXCEPTION_CLASSES.include?(self.class)
|
28
28
|
@first_backtrace_call = true
|
@@ -52,10 +52,13 @@ end
|
|
52
52
|
|
53
53
|
# Overrides for Process
|
54
54
|
module ::Process
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
55
|
+
class << self
|
56
|
+
alias_method :orig_detach, :detach
|
57
|
+
def detach(pid)
|
58
|
+
fiber = spin { Thread.current.agent.waitpid(pid) }
|
59
|
+
fiber.define_singleton_method(:pid) { pid }
|
60
|
+
fiber
|
61
|
+
end
|
59
62
|
end
|
60
63
|
end
|
61
64
|
|
@@ -67,10 +70,9 @@ module ::Kernel
|
|
67
70
|
def `(cmd)
|
68
71
|
Open3.popen3(cmd) do |i, o, e, _t|
|
69
72
|
i.close
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
o.read
|
73
|
+
err = e.read
|
74
|
+
$stderr << err if err
|
75
|
+
o.read || ''
|
74
76
|
end
|
75
77
|
end
|
76
78
|
|
@@ -104,21 +106,28 @@ module ::Kernel
|
|
104
106
|
def system(*args)
|
105
107
|
Open3.popen2(*args) do |i, o, _t|
|
106
108
|
i.close
|
107
|
-
|
108
|
-
$stdout << l
|
109
|
-
end
|
109
|
+
pipe_to_eof(o, $stdout)
|
110
110
|
end
|
111
111
|
true
|
112
112
|
rescue SystemCallError
|
113
113
|
nil
|
114
114
|
end
|
115
|
+
|
116
|
+
def pipe_to_eof(src, dest)
|
117
|
+
loop do
|
118
|
+
data = src.readpartial(8192)
|
119
|
+
dest << data
|
120
|
+
rescue EOFError
|
121
|
+
break
|
122
|
+
end
|
123
|
+
end
|
115
124
|
end
|
116
125
|
|
117
126
|
# Override Timeout to use cancel scope
|
118
127
|
module ::Timeout
|
119
128
|
def self.timeout(sec, klass = nil, message = nil, &block)
|
120
129
|
cancel_after(sec, &block)
|
121
|
-
rescue
|
130
|
+
rescue Polyphony::Cancel => e
|
122
131
|
error = klass ? klass.new(message) : ::Timeout::Error.new
|
123
132
|
error.set_backtrace(e.backtrace)
|
124
133
|
raise error
|
@@ -2,340 +2,342 @@
|
|
2
2
|
|
3
3
|
require 'fiber'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
require_relative '../core/exceptions'
|
6
|
+
|
7
|
+
module Polyphony
|
8
|
+
# Fiber control API
|
9
|
+
module FiberControl
|
10
|
+
def await
|
11
|
+
if @running == false
|
12
|
+
return @result.is_a?(Exception) ? (Kernel.raise @result) : @result
|
13
|
+
end
|
14
|
+
|
15
|
+
fiber = Fiber.current
|
16
|
+
@waiting_fibers ||= {}
|
17
|
+
@waiting_fibers[fiber] = true
|
18
|
+
suspend
|
19
|
+
ensure
|
20
|
+
@waiting_fibers&.delete(fiber)
|
12
21
|
end
|
22
|
+
alias_method :join, :await
|
13
23
|
|
14
|
-
|
15
|
-
|
16
|
-
@waiting_fibers[fiber] = true
|
17
|
-
suspend
|
18
|
-
ensure
|
19
|
-
@waiting_fibers&.delete(fiber)
|
20
|
-
end
|
21
|
-
alias_method :join, :await
|
24
|
+
def interrupt(value = nil)
|
25
|
+
return if @running == false
|
22
26
|
|
23
|
-
|
24
|
-
|
27
|
+
schedule Polyphony::MoveOn.new(value)
|
28
|
+
end
|
29
|
+
alias_method :stop, :interrupt
|
25
30
|
|
26
|
-
|
27
|
-
|
28
|
-
alias_method :stop, :interrupt
|
31
|
+
def restart(value = nil)
|
32
|
+
raise "Can''t restart main fiber" if @main
|
29
33
|
|
30
|
-
|
31
|
-
|
34
|
+
if @running
|
35
|
+
schedule Polyphony::Restart.new(value)
|
36
|
+
return self
|
37
|
+
end
|
32
38
|
|
33
|
-
|
34
|
-
|
35
|
-
|
39
|
+
parent.spin(@tag, @caller, &@block).tap do |f|
|
40
|
+
f.schedule(value) unless value.nil?
|
41
|
+
end
|
36
42
|
end
|
43
|
+
alias_method :reset, :restart
|
37
44
|
|
38
|
-
|
39
|
-
|
40
|
-
end
|
41
|
-
end
|
42
|
-
alias_method :reset, :restart
|
43
|
-
|
44
|
-
def cancel!
|
45
|
-
return if @running == false
|
46
|
-
|
47
|
-
schedule Exceptions::Cancel.new
|
48
|
-
end
|
45
|
+
def cancel
|
46
|
+
return if @running == false
|
49
47
|
|
50
|
-
|
51
|
-
|
48
|
+
schedule Polyphony::Cancel.new
|
49
|
+
end
|
52
50
|
|
53
|
-
|
54
|
-
|
51
|
+
def terminate
|
52
|
+
return if @running == false
|
55
53
|
|
56
|
-
|
57
|
-
|
58
|
-
schedule(error)
|
59
|
-
end
|
54
|
+
schedule Polyphony::Terminate.new
|
55
|
+
end
|
60
56
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
when Class then arg.new(args.shift)
|
65
|
-
when Exception then arg
|
66
|
-
else RuntimeError.new
|
57
|
+
def raise(*args)
|
58
|
+
error = error_from_raise_args(args)
|
59
|
+
schedule(error)
|
67
60
|
end
|
68
|
-
end
|
69
|
-
end
|
70
61
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
62
|
+
def error_from_raise_args(args)
|
63
|
+
case (arg = args.shift)
|
64
|
+
when String then RuntimeError.new(arg)
|
65
|
+
when Class then arg.new(args.shift)
|
66
|
+
when Exception then arg
|
67
|
+
else RuntimeError.new
|
68
|
+
end
|
77
69
|
end
|
78
|
-
loop { supervise_perform(opts) }
|
79
|
-
ensure
|
80
|
-
@on_child_done = nil
|
81
70
|
end
|
82
71
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
72
|
+
# Fiber supervision
|
73
|
+
module FiberSupervision
|
74
|
+
def supervise(opts = {})
|
75
|
+
@counter = 0
|
76
|
+
@on_child_done = proc do |fiber, result|
|
77
|
+
self << fiber unless result.is_a?(Exception)
|
78
|
+
end
|
79
|
+
loop { supervise_perform(opts) }
|
80
|
+
ensure
|
81
|
+
@on_child_done = nil
|
82
|
+
end
|
90
83
|
|
91
|
-
|
92
|
-
|
84
|
+
def supervise_perform(opts)
|
85
|
+
fiber = receive
|
86
|
+
restart_fiber(fiber, opts) if fiber
|
87
|
+
rescue Polyphony::Restart
|
88
|
+
restart_all_children
|
89
|
+
rescue Exception => e
|
90
|
+
Kernel.raise e if e.source_fiber.nil? || e.source_fiber == self
|
93
91
|
|
94
|
-
|
95
|
-
opts[:watcher]&.send [:restart, fiber]
|
96
|
-
case opts[:restart]
|
97
|
-
when true
|
98
|
-
fiber.restart
|
99
|
-
when :one_for_all
|
100
|
-
@children.keys.each(&:restart)
|
92
|
+
restart_fiber(e.source_fiber, opts)
|
101
93
|
end
|
102
|
-
end
|
103
|
-
end
|
104
94
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
ensure
|
115
|
-
await_select_cleanup(state)
|
95
|
+
def restart_fiber(fiber, opts)
|
96
|
+
opts[:watcher]&.send [:restart, fiber]
|
97
|
+
case opts[:restart]
|
98
|
+
when true
|
99
|
+
fiber.restart
|
100
|
+
when :one_for_all
|
101
|
+
@children.keys.each(&:restart)
|
102
|
+
end
|
103
|
+
end
|
116
104
|
end
|
117
|
-
alias_method :join, :await
|
118
105
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
}
|
124
|
-
end
|
106
|
+
# Class methods for controlling fibers (namely await and select)
|
107
|
+
module FiberControlClassMethods
|
108
|
+
def await(*fibers)
|
109
|
+
return [] if fibers.empty?
|
125
110
|
|
126
|
-
|
127
|
-
|
128
|
-
|
111
|
+
state = setup_await_select_state(fibers)
|
112
|
+
await_setup_monitoring(fibers, state)
|
113
|
+
suspend
|
114
|
+
fibers.map(&:result)
|
115
|
+
ensure
|
116
|
+
await_select_cleanup(state)
|
129
117
|
end
|
130
|
-
|
118
|
+
alias_method :join, :await
|
131
119
|
|
132
|
-
|
133
|
-
|
120
|
+
def setup_await_select_state(fibers)
|
121
|
+
{
|
122
|
+
awaiter: Fiber.current,
|
123
|
+
pending: fibers.each_with_object({}) { |f, h| h[f] = true }
|
124
|
+
}
|
125
|
+
end
|
134
126
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
state[:done] = true
|
127
|
+
def await_setup_monitoring(fibers, state)
|
128
|
+
fibers.each do |f|
|
129
|
+
f.when_done { |r| await_fiber_done(f, r, state) }
|
130
|
+
end
|
140
131
|
end
|
141
|
-
end
|
142
132
|
|
143
|
-
|
144
|
-
|
133
|
+
def await_fiber_done(fiber, result, state)
|
134
|
+
state[:pending].delete(fiber)
|
145
135
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
136
|
+
if state[:cleanup]
|
137
|
+
state[:awaiter].schedule if state[:pending].empty?
|
138
|
+
elsif !state[:done] && (result.is_a?(Exception) || state[:pending].empty?)
|
139
|
+
state[:awaiter].schedule(result)
|
140
|
+
state[:done] = true
|
141
|
+
end
|
142
|
+
end
|
151
143
|
|
152
|
-
|
153
|
-
|
154
|
-
select_setup_monitoring(fibers, state)
|
155
|
-
suspend
|
156
|
-
ensure
|
157
|
-
await_select_cleanup(state)
|
158
|
-
end
|
144
|
+
def await_select_cleanup(state)
|
145
|
+
return if state[:pending].empty?
|
159
146
|
|
160
|
-
|
161
|
-
|
162
|
-
|
147
|
+
terminate = Polyphony::Terminate.new
|
148
|
+
state[:cleanup] = true
|
149
|
+
state[:pending].each_key { |f| f.schedule(terminate) }
|
150
|
+
suspend
|
163
151
|
end
|
164
|
-
end
|
165
152
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
# first fiber to complete, we schedule the result
|
173
|
-
state[:awaiter].schedule([fiber, result])
|
174
|
-
state[:selected] = true
|
153
|
+
def select(*fibers)
|
154
|
+
state = setup_await_select_state(fibers)
|
155
|
+
select_setup_monitoring(fibers, state)
|
156
|
+
suspend
|
157
|
+
ensure
|
158
|
+
await_select_cleanup(state)
|
175
159
|
end
|
176
|
-
end
|
177
|
-
end
|
178
160
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
alias_method :send, :<<
|
161
|
+
def select_setup_monitoring(fibers, state)
|
162
|
+
fibers.each do |f|
|
163
|
+
f.when_done { |r| select_fiber_done(f, r, state) }
|
164
|
+
end
|
165
|
+
end
|
185
166
|
|
186
|
-
|
187
|
-
|
167
|
+
def select_fiber_done(fiber, result, state)
|
168
|
+
state[:pending].delete(fiber)
|
169
|
+
if state[:cleanup]
|
170
|
+
# in cleanup mode the selector is resumed if no more pending fibers
|
171
|
+
state[:awaiter].schedule if state[:pending].empty?
|
172
|
+
elsif !state[:selected]
|
173
|
+
# first fiber to complete, we schedule the result
|
174
|
+
state[:awaiter].schedule([fiber, result])
|
175
|
+
state[:selected] = true
|
176
|
+
end
|
177
|
+
end
|
188
178
|
end
|
189
179
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
180
|
+
# Messaging functionality
|
181
|
+
module FiberMessaging
|
182
|
+
def <<(value)
|
183
|
+
@mailbox << value
|
184
|
+
end
|
185
|
+
alias_method :send, :<<
|
194
186
|
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
(@children ||= {}).keys
|
199
|
-
end
|
187
|
+
def receive
|
188
|
+
@mailbox.shift
|
189
|
+
end
|
200
190
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
(@children ||= {})[f] = true
|
205
|
-
f
|
191
|
+
def receive_pending
|
192
|
+
@mailbox.shift_each
|
193
|
+
end
|
206
194
|
end
|
207
195
|
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
196
|
+
# Methods for controlling child fibers
|
197
|
+
module ChildFiberControl
|
198
|
+
def children
|
199
|
+
(@children ||= {}).keys
|
200
|
+
end
|
212
201
|
|
213
|
-
|
214
|
-
|
202
|
+
def spin(tag = nil, orig_caller = Kernel.caller, &block)
|
203
|
+
f = Fiber.new { |v| f.run(v) }
|
204
|
+
f.prepare(tag, block, orig_caller, self)
|
205
|
+
(@children ||= {})[f] = true
|
206
|
+
f
|
207
|
+
end
|
215
208
|
|
216
|
-
|
217
|
-
|
218
|
-
|
209
|
+
def child_done(child_fiber, result)
|
210
|
+
@children.delete(child_fiber)
|
211
|
+
@on_child_done&.(child_fiber, result)
|
212
|
+
end
|
219
213
|
|
220
|
-
|
221
|
-
|
214
|
+
def terminate_all_children
|
215
|
+
return unless @children
|
222
216
|
|
223
|
-
|
224
|
-
|
217
|
+
e = Polyphony::Terminate.new
|
218
|
+
@children.each_key { |c| c.raise e }
|
219
|
+
end
|
225
220
|
|
226
|
-
|
227
|
-
|
228
|
-
await_all_children
|
229
|
-
end
|
230
|
-
end
|
221
|
+
def await_all_children
|
222
|
+
return unless @children && !@children.empty?
|
231
223
|
|
232
|
-
|
233
|
-
|
234
|
-
def prepare(tag, block, caller, parent)
|
235
|
-
@thread = Thread.current
|
236
|
-
@tag = tag
|
237
|
-
@parent = parent
|
238
|
-
@caller = caller
|
239
|
-
@block = block
|
240
|
-
@mailbox = Gyro::Queue.new
|
241
|
-
__fiber_trace__(:fiber_create, self)
|
242
|
-
schedule
|
243
|
-
end
|
224
|
+
Fiber.await(*@children.keys)
|
225
|
+
end
|
244
226
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
rescue Exceptions::Restart => e
|
250
|
-
restart_self(e.value)
|
251
|
-
rescue Exceptions::MoveOn, Exceptions::Terminate => e
|
252
|
-
finalize e.value
|
253
|
-
rescue Exception => e
|
254
|
-
e.source_fiber = self
|
255
|
-
finalize e, true
|
227
|
+
def shutdown_all_children
|
228
|
+
terminate_all_children
|
229
|
+
await_all_children
|
230
|
+
end
|
256
231
|
end
|
257
232
|
|
258
|
-
|
259
|
-
|
233
|
+
# Fiber life cycle methods
|
234
|
+
module FiberLifeCycle
|
235
|
+
def prepare(tag, block, caller, parent)
|
236
|
+
@thread = Thread.current
|
237
|
+
@tag = tag
|
238
|
+
@parent = parent
|
239
|
+
@caller = caller
|
240
|
+
@block = block
|
241
|
+
@mailbox = Polyphony::Queue.new
|
242
|
+
__fiber_trace__(:fiber_create, self)
|
243
|
+
schedule
|
244
|
+
end
|
260
245
|
|
261
|
-
|
262
|
-
|
246
|
+
def run(first_value)
|
247
|
+
setup first_value
|
248
|
+
result = @block.(first_value)
|
249
|
+
finalize result
|
250
|
+
rescue Polyphony::Restart => e
|
251
|
+
restart_self(e.value)
|
252
|
+
rescue Polyphony::MoveOn, Polyphony::Terminate => e
|
253
|
+
finalize e.value
|
254
|
+
rescue Exception => e
|
255
|
+
e.source_fiber = self
|
256
|
+
finalize e, true
|
257
|
+
end
|
263
258
|
|
264
|
-
|
265
|
-
|
266
|
-
# fiber terminates after it has already been created. Calling #setup_raw
|
267
|
-
# allows the fiber to be scheduled and to receive messages.
|
268
|
-
def setup_raw
|
269
|
-
@thread = Thread.current
|
270
|
-
@mailbox = Gyro::Queue.new
|
271
|
-
end
|
259
|
+
def setup(first_value)
|
260
|
+
Kernel.raise first_value if first_value.is_a?(Exception)
|
272
261
|
|
273
|
-
|
274
|
-
|
275
|
-
@tag = :main
|
276
|
-
@thread = Thread.current
|
277
|
-
@running = true
|
278
|
-
@children&.clear
|
279
|
-
@mailbox = Gyro::Queue.new
|
280
|
-
end
|
262
|
+
@running = true
|
263
|
+
end
|
281
264
|
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
265
|
+
# Performs setup for a "raw" Fiber created using Fiber.new. Note that this
|
266
|
+
# fiber is an orphan fiber (has no parent), since we cannot control how the
|
267
|
+
# fiber terminates after it has already been created. Calling #setup_raw
|
268
|
+
# allows the fiber to be scheduled and to receive messages.
|
269
|
+
def setup_raw
|
270
|
+
@thread = Thread.current
|
271
|
+
@mailbox = Polyphony::Queue.new
|
272
|
+
end
|
288
273
|
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
end
|
274
|
+
def setup_main_fiber
|
275
|
+
@main = true
|
276
|
+
@tag = :main
|
277
|
+
@thread = Thread.current
|
278
|
+
@running = true
|
279
|
+
@children&.clear
|
280
|
+
@mailbox = Polyphony::Queue.new
|
281
|
+
end
|
298
282
|
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
shutdown_all_children
|
305
|
-
rescue Exception => e
|
306
|
-
result = e
|
307
|
-
uncaught_exception = true
|
283
|
+
def restart_self(first_value)
|
284
|
+
@mailbox = Polyphony::Queue.new
|
285
|
+
@when_done_procs = nil
|
286
|
+
@waiting_fibers = nil
|
287
|
+
run(first_value)
|
308
288
|
end
|
309
|
-
[result, uncaught_exception]
|
310
|
-
end
|
311
289
|
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
290
|
+
def finalize(result, uncaught_exception = false)
|
291
|
+
result, uncaught_exception = finalize_children(result, uncaught_exception)
|
292
|
+
__fiber_trace__(:fiber_terminate, self, result)
|
293
|
+
@result = result
|
294
|
+
@running = false
|
295
|
+
inform_dependants(result, uncaught_exception)
|
296
|
+
ensure
|
297
|
+
Thread.current.switch_fiber
|
317
298
|
end
|
318
|
-
return unless uncaught_exception && !@waiting_fibers
|
319
299
|
|
320
|
-
#
|
321
|
-
|
322
|
-
|
300
|
+
# Shuts down all children of the current fiber. If any exception occurs while
|
301
|
+
# the children are shut down, it is returned along with the uncaught_exception
|
302
|
+
# flag set. Otherwise, it returns the given arguments.
|
303
|
+
def finalize_children(result, uncaught_exception)
|
304
|
+
begin
|
305
|
+
shutdown_all_children
|
306
|
+
rescue Exception => e
|
307
|
+
result = e
|
308
|
+
uncaught_exception = true
|
309
|
+
end
|
310
|
+
[result, uncaught_exception]
|
311
|
+
end
|
323
312
|
|
324
|
-
|
325
|
-
|
326
|
-
|
313
|
+
def inform_dependants(result, uncaught_exception)
|
314
|
+
@parent&.child_done(self, result)
|
315
|
+
@when_done_procs&.each { |p| p.(result) }
|
316
|
+
@waiting_fibers&.each_key do |f|
|
317
|
+
f.schedule(result)
|
318
|
+
end
|
319
|
+
return unless uncaught_exception && !@waiting_fibers
|
320
|
+
|
321
|
+
# propagate unaught exception to parent
|
322
|
+
@parent&.schedule(result)
|
323
|
+
end
|
324
|
+
|
325
|
+
def when_done(&block)
|
326
|
+
@when_done_procs ||= []
|
327
|
+
@when_done_procs << block
|
328
|
+
end
|
327
329
|
end
|
328
330
|
end
|
329
331
|
|
330
332
|
# Fiber extensions
|
331
333
|
class ::Fiber
|
332
|
-
prepend FiberControl
|
333
|
-
include FiberSupervision
|
334
|
-
include FiberMessaging
|
335
|
-
include ChildFiberControl
|
336
|
-
include FiberLifeCycle
|
334
|
+
prepend Polyphony::FiberControl
|
335
|
+
include Polyphony::FiberSupervision
|
336
|
+
include Polyphony::FiberMessaging
|
337
|
+
include Polyphony::ChildFiberControl
|
338
|
+
include Polyphony::FiberLifeCycle
|
337
339
|
|
338
|
-
extend FiberControlClassMethods
|
340
|
+
extend Polyphony::FiberControlClassMethods
|
339
341
|
|
340
342
|
attr_accessor :tag, :thread, :parent
|
341
343
|
attr_reader :result
|
@@ -377,6 +379,6 @@ orig_pid = Process.pid
|
|
377
379
|
at_exit do
|
378
380
|
next unless orig_pid == Process.pid
|
379
381
|
|
380
|
-
|
381
|
-
Fiber.current.
|
382
|
+
Polyphony.terminate_threads
|
383
|
+
Fiber.current.shutdown_all_children
|
382
384
|
end
|