class Fiber

Attributes

thread[R]

Public Class Methods

current() click to toggle source
# File lib/ext/fiber18.rb, line 49
def self.current
  Thread.current[:fiber] or raise FiberError, 'not inside a fiber'
end
new() { |*pop| ... } click to toggle source
# File lib/ext/fiber18.rb, line 10
def initialize
  raise ArgumentError, 'new Fiber requires a block' unless block_given?

  @yield = Queue.new
  @resume = Queue.new

  @thread = Thread.new{ @yield.push [ *yield(*@resume.pop) ] }
  @thread.abort_on_exception = true
  @thread[:fiber] = self
end
yield(*args) click to toggle source
# File lib/ext/fiber18.rb, line 44
def self.yield *args
  raise FiberError, "can't yield from root fiber" unless fiber = Thread.current[:fiber]
  fiber.yield(*args)
end

Public Instance Methods

alive?() click to toggle source
# File lib/ext/fiber18.rb, line 22
def alive?
  @thread.alive?
end
inspect() click to toggle source
# File lib/ext/fiber18.rb, line 53
def inspect
  "#<#{self.class}:0x#{self.object_id.to_s(16)}>"
end
resume(*args) click to toggle source
# File lib/ext/fiber18.rb, line 26
def resume *args
  raise FiberError, 'dead fiber called' unless @thread.alive?
  raise FiberError, 'double resume' if @thread == Thread.current
  @resume.push(args)
  result = @yield.pop
  result.size > 1 ? result : result.first
end
resume!() click to toggle source
# File lib/ext/fiber18.rb, line 34
def resume!
  @resume.push []
end
yield(*args) click to toggle source
# File lib/ext/fiber18.rb, line 38
def yield *args
  @yield.push(args)
  result = @resume.pop
  result.size > 1 ? result : result.first
end