Module: FiberScheduler::Compatibility
- Defined in:
- lib/fiber_scheduler/compatibility.rb
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.internal? ⇒ Boolean
60 61 62 |
# File 'lib/fiber_scheduler/compatibility.rb', line 60 def self.internal? Thread.current[:_fiber_scheduler] end |
.set_internal! ⇒ Object
56 57 58 |
# File 'lib/fiber_scheduler/compatibility.rb', line 56 def self.set_internal! Thread.current[:_fiber_scheduler] = true # Sets a FIBER local var! end |
Instance Method Details
#fiber(*args, **opts, &block) ⇒ Object
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 47 48 49 50 51 52 53 54 |
# File 'lib/fiber_scheduler/compatibility.rb', line 3 def fiber(*args, **opts, &block) return super unless Compatibility.internal? # This is `Fiber.schedule` call inside `FiberScheduler { ... }` block. type = args.first case type when :blocking Fiber.new(blocking: true) { Compatibility.set_internal! yield }.tap(&:resume) when :waiting parent = Fiber.current finished = false # prevents races blocking = false # prevents #unblock-ing a fiber that never blocked # Don't pass *args and **opts to an unknown fiber scheduler class. super() do Compatibility.set_internal! yield ensure finished = true unblock(nil, parent) if blocking end unless finished blocking = true block(nil, nil) end when :fleeting # Transfer to current fiber some time - after a fleeting fiber yields. unblock(nil, Fiber.current) # Alternative to #unblock: Fiber.scheduler.push(Fiber.current) Fiber.new(blocking: false) { Compatibility.set_internal! yield }.transfer when nil # Don't pass *args and **opts to an unknown fiber scheduler class. super() do Compatibility.set_internal! yield end else raise "Unknown type" end end |