class Async::Barrier

@public Since ‘stable-v1`.
A general purpose synchronisation primitive, which allows one task to wait for a number of other tasks to complete. It can be used in conjunction with {Semaphore}.

def async(*arguments, parent: (@parent or Task.current), **options, &block)

@asynchronous Executes the given block concurrently.
Execute a child task and add it to the barrier.
def async(*arguments, parent: (@parent or Task.current), **options, &block)
	task = parent.async(*arguments, **options, &block)
	
	@tasks.append(TaskNode.new(task))
	
	return task
end

def empty?

@returns [Boolean]
Whether there are any tasks being held by the barrier.
def empty?
	@tasks.empty?
end

def initialize(parent: nil)

@public Since `stable-v1`.
@parameter parent [Task | Semaphore | Nil] The parent for holding any children tasks.
Initialize the barrier.
def initialize(parent: nil)
	@tasks = List.new
	
	@parent = parent
end

def size

Number of tasks being held by the barrier.
def size
	@tasks.size
end

def stop

@asynchronous May wait for tasks to finish executing.
Stop all tasks held by the barrier.
def stop
	@tasks.each do |waiting|
		waiting.task.stop
	end
end

def wait

@asynchronous Will wait for tasks to finish executing.
Wait for all tasks to complete by invoking {Task#wait} on each waiting task, which may raise an error. As long as the task has completed, it will be removed from the barrier.
def wait
	@tasks.each do |waiting|
		task = waiting.task
		begin
			task.wait
		ensure
			@tasks.remove?(waiting) unless task.alive?
		end
	end
end