lib/polyphony/extensions/fiber.rb
# call-seq:
#   Fiber.await(*fibers) -> [*results]
#   Fiber.join(*fibers) -> [*results]
#
# Waits for all given fibers to terminate, then returns the respective
# return values for all terminated fibers. If any of the awaited fibers
# terminates with an uncaught exception, `Fiber.await` will await all the
# other fibers to terminate, then reraise the exception.
#
# @param *fibers [Array<Fiber>] fibers to wait for
# @return [Array<any>] return values of given fibers


Waits for all given fibers to terminate, then returns the respective
return values for all terminated fibers. If any of the awaited fibers
terminates with an uncaught exception, `Fiber.await` will await all the
other fibers to terminate, then reraise the exception.
Waits for all given fibers to terminate then returns the respective return values for all terminated fibers . If any of the awaited fibers terminates with an uncaught exception ` Fiber . await ` will await all the other fibers to terminate then reraise the exception .
def await(*fibers)
      return [] if fibers.empty?
      current_fiber = self.current
      mailbox = current_fiber.monitor_mailbox
      results = {}
      fibers.each do |f|
        results[f] = nil
        if f.dead?
          mailbox << [f, f.result]
        else
          f.monitor(current_fiber)
        end
      end
      exception = nil
      while !fibers.empty?
        (fiber, result) = mailbox.shift
        next unless fibers.include?(fiber)
        fibers.delete(fiber)
        current_fiber.remove_child(fiber) if fiber.parent == current_fiber
        if result.is_a?(Exception)
          exception ||= result
          fibers.each { |f| f.terminate }
        else
          results[fiber] = result
        end
      end
      raise exception if exception
      results.values
    end
def await ( * fibers ) return [ ] if fibers . empty? current_fiber = self . current mailbox = current_fiber . monitor_mailbox results = { } fibers . each do | f | results [ f ] = nil if f . dead? mailbox << [ f , f . result ] else f . monitor ( current_fiber ) end end exception = nil while ! fibers . empty? ( fiber , result ) = mailbox . shift next unless fibers . include? ( fiber ) fibers . delete ( fiber ) current_fiber . remove_child ( fiber ) if fiber . parent == current_fiber if result . is_a? ( Exception ) exception ||= result fibers . each { | f | f . terminate } else results [ fiber ] = result end end raise exception if exception results . values end
call-seq:
Fiber.await(*fibers) -> [*results]
Fiber.join(*fibers) -> [*results]
call - seq : Fiber . await ( * fibers ) - > [ * results ] Fiber . join ( * fibers ) - > [ * results ]
# fiber already terminated, so queue message
return values of given fibers
return values of given fibers
Array<any>
fibers
<not_specific>
*fibers
<not_specific>
fibers to wait for
fibers to wait for