Class: SyncThread

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/lockstep/sync_thread.rb

Defined Under Namespace

Classes: Finished, Interrupted, Started

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSyncThread

Returns a new instance of SyncThread.



13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



10
11
12
# File 'lib/lockstep/sync_thread.rb', line 10

def status
  @status
end

Class Method Details

.interrupt(source, name, *args, &block) ⇒ Object



6
7
8
# File 'lib/lockstep/sync_thread.rb', line 6

def self.interrupt(source, name, *args, &block)
  Fiber.yield(:interrupt, source, name, args, block)
end

Instance Method Details

#finishObject



41
42
43
44
# File 'lib/lockstep/sync_thread.rb', line 41

def finish
  return @status if @status.finished?
  resume(ignore: true)
end

#last_return_valueObject



46
47
48
# File 'lib/lockstep/sync_thread.rb', line 46

def last_return_value
  status.return_value
end

#resume(options = {}) ⇒ Object



33
34
35
36
37
38
39
# 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) ⇒ Object



25
26
27
28
29
30
31
# 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