Method: Kernel#Sync

Defined in:
lib/kernel/sync.rb

#Sync(annotation: nil, &block) ⇒ Object

Run the given block of code synchronously, but within a reactor if not already in one.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kernel/sync.rb', line 19

def Sync(annotation: nil, &block)
  if task = ::Async::Task.current?
    if annotation
      task.annotate(annotation) {yield task}
    else
      yield task
    end
  elsif scheduler = Fiber.scheduler
    ::Async::Task.run(scheduler, &block).wait
  else
    # This calls Fiber.set_scheduler(self):
    reactor = Async::Reactor.new
    
    begin
      return reactor.run(annotation: annotation, finished: ::Async::Condition.new, &block).wait
    ensure
      Fiber.set_scheduler(nil)
    end
  end
end