class Async::IO::Threads

def async(parent: (@parent or Task.current))

def async(parent: (@parent or Task.current))
	parent.async do
		thread = ::Thread.new do
			yield
		end
		
		thread.join
	rescue Stop
		if thread&.alive?
			thread.raise(Stop)
		end
		
		begin
			thread.join
		rescue Stop
			# Ignore.
		end
	end
end

def async(parent: (@parent or Task.current))

def async(parent: (@parent or Task.current))
	parent.async do |task|
		notification = Async::IO::Notification.new
		
		thread = ::Thread.new do
			yield
		ensure
			notification.signal
		end
		
		task.annotate "Waiting for thread to finish..."
		
		notification.wait
		
		thread.value
	ensure
		if thread&.alive?
			thread.raise(Stop)
			
			begin
				thread.join
			rescue Stop
				# Ignore.
			end
		end
		
		notification&.close
	end
end

def initialize(parent: nil)

def initialize(parent: nil)
	@parent = parent
end