lib/lockstep/sync_thread.rb in lockstep-0.0.2 vs lib/lockstep/sync_thread.rb in lockstep-0.0.3
- old
+ new
@@ -1,10 +1,17 @@
+require 'forwardable'
+
class SyncThread
+ extend Forwardable
+
def self.interrupt(source, name, *args, &block)
Fiber.yield(:interrupt, source, name, args, block)
end
+ attr_reader :status
+ def_delegators :status, :interrupted?, :interrupted_by?, :finished?
+
def initialize
@status = Started.new
@fiber = Fiber.new do
return_value = nil
loop do
@@ -34,10 +41,14 @@
def finish
return @status if @status.finished?
resume(ignore: true)
end
+ def last_return_value
+ status.return_value
+ end
+
private
def execute(options={})
ignores = Array(options[:ignore])
loop do
@@ -56,16 +67,20 @@
end
end
class Started
def finished?; true; end
+ def interrupted?; false; end
+ def interrupted_by?(*); false; end
end
Finished = Struct.new(:return_value) do
def finished?; true; end
+ def interrupted?; false; end
def interrupted_by?(*); false; end
end
Interrupted = Struct.new(:source, :name, :arguments, :block) do
def finished?; false; end
+ def interrupted?; true; end
def interrupted_by?(source_or_message, message=nil, args=nil)
if args
return false unless args === arguments
end
if message