lib/lightio/core/beam.rb in lightio-0.1.1 vs lib/lightio/core/beam.rb in lightio-0.2.0
- old
+ new
@@ -15,10 +15,20 @@
# b = Beam.new(){LightIO.sleep 3}
# b.join
# b.alive? # false
class Beam < LightFiber
+ # special class for simulate Thread#raise for Beam
+ class BeamError
+ attr_reader :error, :parent
+
+ def initialize(error)
+ @error = error
+ @parent = Beam.current
+ end
+ end
+
# Create a new beam
#
# Beam is light-weight executor, provide thread-like interface
#
# Beam.new("hello"){|hello| puts hello }
@@ -90,9 +100,17 @@
# @return [Beam]
def kill
dead
parent.transfer if self == Beam.current
self
+ end
+
+ # Fiber not provide raise method, so we have to simulate one
+ # @param [BeamError] error currently only support raise BeamError
+ def raise(error)
+ super unless error.is_a?(BeamError)
+ self.parent = error.parent if error.parent
+ raise error.error
end
class << self
# Schedule beams