module Polyphony::FiberLifeCycle
Fiber
life cycle methods
Public Instance Methods
dead?()
click to toggle source
# File lib/polyphony/extensions/fiber.rb, line 361 def dead? state == :dead end
finalize(result, uncaught_exception = false)
click to toggle source
# File lib/polyphony/extensions/fiber.rb, line 313 def finalize(result, uncaught_exception = false) result, uncaught_exception = finalize_children(result, uncaught_exception) Thread.backend.trace(:fiber_terminate, self, result) @result = result inform_monitors(result, uncaught_exception) @running = false ensure @parent&.remove_child(self) # Prevent fiber from being resumed after terminating @thread.fiber_unschedule(self) Thread.current.switch_fiber end
finalize_children(result, uncaught_exception)
click to toggle source
Shuts down all children of the current fiber. If any exception occurs while the children are shut down, it is returned along with the uncaught_exception flag set. Otherwise, it returns the given arguments.
# File lib/polyphony/extensions/fiber.rb, line 330 def finalize_children(result, uncaught_exception) shutdown_all_children [result, uncaught_exception] rescue Exception => e [e, true] end
inform_monitors(result, uncaught_exception)
click to toggle source
# File lib/polyphony/extensions/fiber.rb, line 337 def inform_monitors(result, uncaught_exception) if @monitors msg = [self, result] @monitors.each_key { |f| f.monitor_mailbox << msg } end if uncaught_exception && @parent parent_is_monitor = @monitors&.has_key?(@parent) @parent.schedule_with_priority(result) unless parent_is_monitor end end
monitor(fiber)
click to toggle source
# File lib/polyphony/extensions/fiber.rb, line 349 def monitor(fiber) (@monitors ||= {})[fiber] = true end
monitors()
click to toggle source
# File lib/polyphony/extensions/fiber.rb, line 357 def monitors @monitors&.keys || [] end
prepare(tag, block, caller, parent)
click to toggle source
# File lib/polyphony/extensions/fiber.rb, line 262 def prepare(tag, block, caller, parent) @thread = Thread.current @tag = tag @parent = parent @caller = caller @block = block Thread.backend.trace(:fiber_create, self) schedule end
restart_self(first_value)
click to toggle source
# File lib/polyphony/extensions/fiber.rb, line 308 def restart_self(first_value) @mailbox = nil run(first_value) end
run(first_value)
click to toggle source
# File lib/polyphony/extensions/fiber.rb, line 272 def run(first_value) setup first_value result = @block.(first_value) finalize result rescue Polyphony::Restart => e restart_self(e.value) rescue Polyphony::MoveOn, Polyphony::Terminate => e finalize e.value rescue Exception => e e.source_fiber = self finalize e, true end
setup(first_value)
click to toggle source
# File lib/polyphony/extensions/fiber.rb, line 285 def setup(first_value) Kernel.raise first_value if first_value.is_a?(Exception) @running = true end
setup_main_fiber()
click to toggle source
# File lib/polyphony/extensions/fiber.rb, line 300 def setup_main_fiber @main = true @tag = :main @thread = Thread.current @running = true @children&.clear end
setup_raw()
click to toggle source
Performs setup for a “raw” Fiber
created using Fiber.new
. Note that this fiber is an orphan fiber (has no parent), since we cannot control how the fiber terminates after it has already been created. Calling setup_raw
allows the fiber to be scheduled and to receive messages.
# File lib/polyphony/extensions/fiber.rb, line 295 def setup_raw @thread = Thread.current @running = true end
unmonitor(fiber)
click to toggle source
# File lib/polyphony/extensions/fiber.rb, line 353 def unmonitor(fiber) (@monitors ||= []).delete(fiber) end