Module: Syntropy::SideRun

Defined in:
lib/syntropy/side_run.rb

Overview

SideRun implements running an operation on a separate thread.

Class Method Summary collapse

Class Method Details

.call(machine, &block) ⇒ any

Runs the given block on a separate thread, using UringMachine to wait for the operation to complete. If the operation results in a raised exception, that exception will be reraised in the context of the waiting fiber.

Parameters:

  • machine (UringMachine)

    machine instance

Returns:

  • (any)

    operation return value



16
17
18
19
20
21
22
23
24
# File 'lib/syntropy/side_run.rb', line 16

def call(machine, &block)
  setup if !@queue

  # TODO: share mailboxes, acquire them with e.g. with_mailbox { |mbox| ... }
  mailbox = Thread.current[:fiber_mailbox] ||= UM::Queue.new
  machine.push(@queue, [mailbox, block])
  result = machine.shift(mailbox)
  result.is_a?(Exception) ? (raise result) : result
end