class SyncThread

Constants

Finished
Interrupted

Attributes

status[R]

Public Class Methods

interrupt(source, name, *args, &block) click to toggle source
# File lib/lockstep/sync_thread.rb, line 6
def self.interrupt(source, name, *args, &block)
  Fiber.yield(:interrupt, source, name, args, block)
end
new() click to toggle source
# File lib/lockstep/sync_thread.rb, line 13
def initialize
  @status = Started.new
  @fiber = Fiber.new do
    return_value = nil
    loop do
      op = Fiber.yield(:finish, return_value)
      return_value = op.call
    end
  end
  @fiber.resume
end

Public Instance Methods

finish() click to toggle source
# File lib/lockstep/sync_thread.rb, line 41
def finish
  return @status if @status.finished?
  resume(ignore: true)
end
last_return_value() click to toggle source
# File lib/lockstep/sync_thread.rb, line 46
def last_return_value
  status.return_value
end
resume(options={}) click to toggle source
# File lib/lockstep/sync_thread.rb, line 33
def resume(options={})
  raise "Nothing to resume!" if @status.finished?
  execute(options) do
    @fiber.resume
  end
  @status
end
run(options={}, &op) click to toggle source
# File lib/lockstep/sync_thread.rb, line 25
def run(options={}, &op)
  raise "Not finished!" unless @status.finished?
  execute(options) do
    @fiber.resume(op)
  end
  @status
end

Private Instance Methods

execute(options={}) { || ... } click to toggle source
# File lib/lockstep/sync_thread.rb, line 52
def execute(options={})
  ignores = Array(options[:ignore])
  loop do
    status, *rest = yield
    case status
    when :finish
      @status = Finished.new(rest.first)
      break
    when :interrupt
      source, name, args, block = *rest
      @status = Interrupted.new(source, name, args, block)
      break unless ignores.include?(name) || ignores.include?(true)
    else
      raise "Should never get here"
    end
  end
end