fiber_scheduler 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f52317e155b7fcae9a70ec8c93471ce583e818e00bc239581f52db1943aea579
4
- data.tar.gz: 592b146c29b9448f8948201b94f0f3468973b19dfc7c75b015a7a2167179824f
3
+ metadata.gz: 53d696318655c53901ad5236d6a2beb1ae14b915ce2f2c64f05ef1e3e40988a9
4
+ data.tar.gz: be844ddb1fb5010732ec84a01b5ef73256e60abe91e126d461daf4bbe5467012
5
5
  SHA512:
6
- metadata.gz: 40c9a3f67136992e766b6a9424922061debfbf09d07759396c783e53cccfd4adb63d0027cce0a6fd5922427f59c3ad02e93980b3cf7b4cf8fac5e473257dd42c
7
- data.tar.gz: 51d94ee5f1431bafe2921a12a20238049d51f6415010eace40407e5a373c4e35cf4500848196489b9c2534dc1dab0b8efbe3c7f780c49bcd1e9c1c6f1f6bd0c2
6
+ metadata.gz: e54d5897ea73e5a0dc8d753091c6700b3605458df9e101b90bff77b63e141054db8142720279d3556d9d5a02347b049ec83213462224e93f03055fac0a4af65c
7
+ data.tar.gz: 445b10119acd9d544e69999ea32eaa567976bc73409cd1db4919a19c881ffaab76bef770192d5ec11c41989ad6e9df3d470ec6697e8e8e430d510a595959311d
@@ -4,13 +4,15 @@ class FiberScheduler
4
4
  return super unless Compatibility.internal?
5
5
 
6
6
  # This is `Fiber.schedule` call inside `FiberScheduler { ... }` block.
7
- if opts[:blocking]
7
+ type = args.first
8
+ case type
9
+ when :blocking
8
10
  Fiber.new(blocking: true) {
9
11
  Compatibility.set_internal!
10
12
  yield
11
13
  }.tap(&:resume)
12
14
 
13
- elsif opts[:waiting]
15
+ when :waiting
14
16
  parent = Fiber.current
15
17
  finished = false # prevents races
16
18
  blocking = false # prevents #unblock-ing a fiber that never blocked
@@ -29,12 +31,25 @@ class FiberScheduler
29
31
  block(nil, nil)
30
32
  end
31
33
 
32
- else
34
+ when :fleeting
35
+ # Transfer to current fiber some time - after a fleeting fiber yields.
36
+ unblock(nil, Fiber.current)
37
+ # Alternative to #unblock: Fiber.scheduler.push(Fiber.current)
38
+
39
+ Fiber.new(blocking: false) {
40
+ Compatibility.set_internal!
41
+ yield
42
+ }.transfer
43
+
44
+ when nil
33
45
  # Don't pass *args and **opts to an unknown fiber scheduler class.
34
46
  super() do
35
47
  Compatibility.set_internal!
36
48
  yield
37
49
  end
50
+
51
+ else
52
+ raise "Unknown type"
38
53
  end
39
54
  end
40
55
 
@@ -1,3 +1,3 @@
1
1
  class FiberScheduler
2
- VERSION = "0.10.0".freeze
2
+ VERSION = "0.11.0".freeze
3
3
  end
@@ -10,7 +10,7 @@ rescue LoadError
10
10
  end
11
11
 
12
12
  module Kernel
13
- def FiberScheduler(blocking: false, waiting: true, &block)
13
+ def FiberScheduler(type = nil, &block)
14
14
  if Fiber.scheduler.nil?
15
15
  scheduler = FiberScheduler.new
16
16
  Fiber.set_scheduler(scheduler)
@@ -28,21 +28,22 @@ module Kernel
28
28
  if scheduler.is_a?(FiberScheduler)
29
29
  # The default waiting is 'true' as that is the most intuitive behavior
30
30
  # for a nested FiberScheduler call.
31
- Fiber.schedule(blocking: blocking, waiting: waiting, &block)
31
+ Fiber.schedule(type, &block)
32
32
 
33
33
  # Unknown fiber scheduler class; can't just pass options to
34
34
  # Fiber.schedule, handle each option separately.
35
35
  else
36
36
  scheduler.singleton_class.prepend(FiberScheduler::Compatibility)
37
37
 
38
- if blocking
38
+ case type
39
+ when :blocking
39
40
  fiber = Fiber.new(blocking: true) do
40
41
  FiberScheduler::Compatibility.set_internal!
41
42
  yield
42
43
  end
43
44
  fiber.tap(&:resume)
44
45
 
45
- elsif waiting
46
+ when :waiting
46
47
  parent = Fiber.current
47
48
  finished = false # prevents races
48
49
  blocking = false # prevents #unblock-ing a fiber that never blocked
@@ -66,12 +67,16 @@ module Kernel
66
67
  blocking = true
67
68
  scheduler.block(nil, nil)
68
69
  end
70
+ when :fleeting
69
71
 
70
- else
72
+ when nil
71
73
  Fiber.schedule do
72
74
  FiberScheduler::Compatibility.set_internal!
73
75
  yield
74
76
  end
77
+
78
+ else
79
+ raise "Unknown type"
75
80
  end
76
81
  end
77
82
  end
@@ -170,13 +175,13 @@ class FiberScheduler
170
175
  @timeouts.timeout(duration, exception, message, &block)
171
176
  end
172
177
 
173
- def fiber(blocking: false, waiting: false, &block)
178
+ def fiber(type = nil, &block)
174
179
  current = Fiber.current
175
180
 
176
- if blocking
177
- # All fibers wait on a blocking fiber, so 'waiting' option is ignored.
181
+ case type
182
+ when :blocking
178
183
  Fiber.new(blocking: true, &block).tap(&:resume)
179
- elsif waiting
184
+ when :waiting
180
185
  finished = false # prevents races
181
186
  fiber = Fiber.new(blocking: false) do
182
187
  @count += 1
@@ -201,7 +206,14 @@ class FiberScheduler
201
206
  end
202
207
 
203
208
  fiber
204
- else
209
+ when :fleeting
210
+ if current != @fiber
211
+ # nested Fiber.schedule
212
+ @nested << current
213
+ end
214
+
215
+ Fiber.new(blocking: false, &block).tap(&:transfer)
216
+ when nil
205
217
  if current != @fiber
206
218
  # nested Fiber.schedule
207
219
  @nested << current
@@ -214,6 +226,9 @@ class FiberScheduler
214
226
  @count -= 1
215
227
  end
216
228
  fiber.tap(&:transfer)
229
+
230
+ else
231
+ raise "Unknown type"
217
232
  end
218
233
  end
219
234
  end
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.10.0
4
+ version: 0.11.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-15 00:00:00.000000000 Z
11
+ date: 2022-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: fiber_scheduler_spec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement