module Async::Container

def self.best_container_class

@returns [Class]
Some platforms, including JRuby, don't support fork. Applications which just want a reasonable default can use this method.
Determins the best container class based on the underlying Ruby implementation.
def self.best_container_class
	if fork?
		return Forked
	else
		return Threaded
	end
end

def self.fork?

@returns [Boolean]
Whether the underlying process supports fork.
def self.fork?
	::Process.respond_to?(:fork) && ::Process.respond_to?(:setpgid)
end

def self.new(*arguments, **options)

@returns [Generic] Typically an instance of either {Forked} or {Threaded} containers.
Create an instance of the best container class.
def self.new(*arguments, **options)
	best_container_class.new(*arguments, **options)
end

def self.processor_count(env = ENV)

@raises [RuntimeError] If the process count is invalid.
@returns [Integer] The number of hardware processors which can run threads/processes simultaneously.
The processor count which may be used for the default number of container threads/processes. You can override the value provided by the system by specifying the `ASYNC_CONTAINER_PROCESSOR_COUNT` environment variable.
def self.processor_count(env = ENV)
	count = env.fetch(ASYNC_CONTAINER_PROCESSOR_COUNT) do
		Etc.nprocessors rescue 1
	end.to_i
	
	if count < 1
		raise RuntimeError, "Invalid processor count #{count}!"
	end
	
	return count
end