module Kernel

def Async(...)

@asynchronous May block until given block completes executing.
@public Since `stable-v1`.

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

running.
the block as an asynchronous task. Will block until the reactor finishes
- When invoked at the top level, will create and run a reactor, and invoke
asynchronously. Will return the task once it has been scheduled.
- When invoked within an existing reactor task, it will run the given block

The preferred method to invoke asynchronous behavior at the top level.

Run the given block of code in a task, asynchronously, creating a reactor if necessary.
def Async(...)
	if current = ::Async::Task.current?
		return current.async(...)
	else
		# This calls Fiber.set_scheduler(self):
		reactor = ::Async::Reactor.new
		
		begin
			return reactor.run(...)
		ensure
			Fiber.set_scheduler(nil)
		end
	end
end

def Sync(&block)

@asynchronous Will block until given block completes executing.
@public Since `stable-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(&block)
	if task = ::Async::Task.current?
		yield task
	else
		# This calls Fiber.set_scheduler(self):
		reactor = Async::Reactor.new
		
		begin
			return reactor.run(finished: ::Async::Condition.new, &block).wait
		ensure
			Fiber.set_scheduler(nil)
		end
	end
end