fiber_scheduler 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fiber_scheduler/compatibility.rb +49 -0
- data/lib/fiber_scheduler/version.rb +1 -1
- data/lib/fiber_scheduler.rb +40 -25
- metadata +3 -3
- data/lib/fiber/scheduler.rb +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f52317e155b7fcae9a70ec8c93471ce583e818e00bc239581f52db1943aea579
|
4
|
+
data.tar.gz: 592b146c29b9448f8948201b94f0f3468973b19dfc7c75b015a7a2167179824f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40c9a3f67136992e766b6a9424922061debfbf09d07759396c783e53cccfd4adb63d0027cce0a6fd5922427f59c3ad02e93980b3cf7b4cf8fac5e473257dd42c
|
7
|
+
data.tar.gz: 51d94ee5f1431bafe2921a12a20238049d51f6415010eace40407e5a373c4e35cf4500848196489b9c2534dc1dab0b8efbe3c7f780c49bcd1e9c1c6f1f6bd0c2
|
@@ -0,0 +1,49 @@
|
|
1
|
+
class FiberScheduler
|
2
|
+
module Compatibility
|
3
|
+
def fiber(*args, **opts, &block)
|
4
|
+
return super unless Compatibility.internal?
|
5
|
+
|
6
|
+
# This is `Fiber.schedule` call inside `FiberScheduler { ... }` block.
|
7
|
+
if opts[:blocking]
|
8
|
+
Fiber.new(blocking: true) {
|
9
|
+
Compatibility.set_internal!
|
10
|
+
yield
|
11
|
+
}.tap(&:resume)
|
12
|
+
|
13
|
+
elsif opts[:waiting]
|
14
|
+
parent = Fiber.current
|
15
|
+
finished = false # prevents races
|
16
|
+
blocking = false # prevents #unblock-ing a fiber that never blocked
|
17
|
+
|
18
|
+
# Don't pass *args and **opts to an unknown fiber scheduler class.
|
19
|
+
super() do
|
20
|
+
Compatibility.set_internal!
|
21
|
+
yield
|
22
|
+
ensure
|
23
|
+
finished = true
|
24
|
+
unblock(nil, parent) if blocking
|
25
|
+
end
|
26
|
+
|
27
|
+
unless finished
|
28
|
+
blocking = true
|
29
|
+
block(nil, nil)
|
30
|
+
end
|
31
|
+
|
32
|
+
else
|
33
|
+
# Don't pass *args and **opts to an unknown fiber scheduler class.
|
34
|
+
super() do
|
35
|
+
Compatibility.set_internal!
|
36
|
+
yield
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.set_internal!
|
42
|
+
Thread.current[:_fiber_scheduler] = true # Sets a FIBER local var!
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.internal?
|
46
|
+
Thread.current[:_fiber_scheduler]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/fiber_scheduler.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "resolv"
|
2
|
+
require_relative "fiber_scheduler/compatibility"
|
2
3
|
require_relative "fiber_scheduler/selector"
|
3
4
|
require_relative "fiber_scheduler/timeouts"
|
4
5
|
|
@@ -31,33 +32,47 @@ module Kernel
|
|
31
32
|
|
32
33
|
# Unknown fiber scheduler class; can't just pass options to
|
33
34
|
# Fiber.schedule, handle each option separately.
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
35
|
+
else
|
36
|
+
scheduler.singleton_class.prepend(FiberScheduler::Compatibility)
|
37
|
+
|
38
|
+
if blocking
|
39
|
+
fiber = Fiber.new(blocking: true) do
|
40
|
+
FiberScheduler::Compatibility.set_internal!
|
41
|
+
yield
|
42
|
+
end
|
43
|
+
fiber.tap(&:resume)
|
44
|
+
|
45
|
+
elsif waiting
|
46
|
+
parent = Fiber.current
|
47
|
+
finished = false # prevents races
|
48
|
+
blocking = false # prevents #unblock-ing a fiber that never blocked
|
49
|
+
|
50
|
+
Fiber.schedule do
|
51
|
+
FiberScheduler::Compatibility.set_internal!
|
52
|
+
yield
|
53
|
+
ensure
|
54
|
+
finished = true
|
55
|
+
scheduler.unblock(nil, parent) if blocking
|
56
|
+
end
|
47
57
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
58
|
+
if Fiber.blocking?
|
59
|
+
# In a blocking fiber, which is potentially also a loop fiber so
|
60
|
+
# there's nothing we can transfer to. Run other fibers (or just
|
61
|
+
# block) until waiting fiber finishes.
|
62
|
+
until finished
|
63
|
+
scheduler.run_once
|
64
|
+
end
|
65
|
+
elsif !finished
|
66
|
+
blocking = true
|
67
|
+
scheduler.block(nil, nil)
|
54
68
|
end
|
69
|
+
|
55
70
|
else
|
56
|
-
|
71
|
+
Fiber.schedule do
|
72
|
+
FiberScheduler::Compatibility.set_internal!
|
73
|
+
yield
|
74
|
+
end
|
57
75
|
end
|
58
|
-
|
59
|
-
else
|
60
|
-
Fiber.schedule(&block)
|
61
76
|
end
|
62
77
|
end
|
63
78
|
end
|
@@ -181,8 +196,8 @@ class FiberScheduler
|
|
181
196
|
until finished
|
182
197
|
run_once
|
183
198
|
end
|
184
|
-
|
185
|
-
@selector.transfer
|
199
|
+
elsif !finished
|
200
|
+
@selector.transfer
|
186
201
|
end
|
187
202
|
|
188
203
|
fiber
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fiber_scheduler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno Sutic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-02-
|
11
|
+
date: 2022-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: async
|
@@ -72,8 +72,8 @@ executables: []
|
|
72
72
|
extensions: []
|
73
73
|
extra_rdoc_files: []
|
74
74
|
files:
|
75
|
-
- lib/fiber/scheduler.rb
|
76
75
|
- lib/fiber_scheduler.rb
|
76
|
+
- lib/fiber_scheduler/compatibility.rb
|
77
77
|
- lib/fiber_scheduler/selector.rb
|
78
78
|
- lib/fiber_scheduler/timeout.rb
|
79
79
|
- lib/fiber_scheduler/timeouts.rb
|
data/lib/fiber/scheduler.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require_relative "../fiber_scheduler"
|