module Kernel

def Sync(annotation: nil, &block)

@asynchronous Will block until given block completes executing.
@public Since *Async v1*.

@parameter task [Async::Task] The task that is executing the given block.
@yields {|task| ...} The block that will execute asynchronously.

Run the given block of code synchronously, but within a reactor if not already in one.
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
		Fiber.blocking do
			# This calls Fiber.set_scheduler(self):
			reactor = Async::Reactor.new
			
			begin
				# Use finished: false to suppress warnings since we're handling exceptions explicitly
				return reactor.run(annotation: annotation, finished: false, &block).wait
			ensure
				Fiber.set_scheduler(nil)
			end
		end
	end
end