Class: FiberRecycling::RecycledFiber
- Inherits:
-
Object
- Object
- FiberRecycling::RecycledFiber
- Defined in:
- lib/fiber_recycling/recycled_fiber.rb
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #execution_loop ⇒ Object
-
#initialize ⇒ RecycledFiber
constructor
A new instance of RecycledFiber.
- #resume(*args) ⇒ Object
- #run(*args, &block) ⇒ Object
- #transfer(*args) ⇒ Object
Constructor Details
#initialize ⇒ RecycledFiber
Returns a new instance of RecycledFiber.
8 9 10 11 12 |
# File 'lib/fiber_recycling/recycled_fiber.rb', line 8 def initialize @native_fiber = ::Fiber.new { execution_loop } @state = :initialized start end |
Class Method Details
.yield(*args) ⇒ Object
4 5 6 |
# File 'lib/fiber_recycling/recycled_fiber.rb', line 4 def self.yield(*args) ::Fiber.yield(*args) end |
Instance Method Details
#close ⇒ Object
14 15 16 17 18 |
# File 'lib/fiber_recycling/recycled_fiber.rb', line 14 def close raise 'can not close, currently running a block' unless @state == :waiting_for_instruction @native_fiber.resume(:close) nil end |
#execution_loop ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/fiber_recycling/recycled_fiber.rb', line 28 def execution_loop last_reutrn_value = nil loop do @state = :waiting_for_instruction instruction = ::Fiber.yield(last_reutrn_value) case instruction when :run @state = :executing_block args = ::Fiber.yield proc = ::Fiber.yield last_reutrn_value = proc.call(*args) when :close @state = :closed break else raise 'invalid instruction' end end end |
#resume(*args) ⇒ Object
48 49 50 51 |
# File 'lib/fiber_recycling/recycled_fiber.rb', line 48 def resume(*args) raise 'can not resume, not running a block' unless @state == :executing_block @native_fiber.resume(*args) end |
#run(*args, &block) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/fiber_recycling/recycled_fiber.rb', line 20 def run(*args, &block) raise 'can not run, already running a block' unless @state == :waiting_for_instruction @native_fiber.resume(:run) @native_fiber.resume(args) block_reutrn_value = @native_fiber.resume(block) block_reutrn_value end |
#transfer(*args) ⇒ Object
53 54 55 56 |
# File 'lib/fiber_recycling/recycled_fiber.rb', line 53 def transfer(*args) raise 'can not transfer, not running a block' unless @state == :executing_block @native_fiber.transfer(*args) end |