Support dynamic creation of Mercury engines in low-level C parallel grades.

This change allows Mercury engines (each in a separate OS thread) to be
created and destroyed dynamically in low-level C grades.

We divide Mercury engines into two types:

    "Shared" engines may execute code from any Mercury thread.
    Shared engines may steal work from other shared engines, so are also
    called work-stealing engines; we do not have shared engines that
    refrain from work-stealing.

    "Exclusive" engines execute code only for a single Mercury thread.

Only exclusive engines may be created and destroyed dynamically so far.
This assumption could be lifted when and if the need should arise.

Exclusive engines are a means for the user to map a Mercury thread directly
to an OS thread.  Calls to blocking procedures on that thread will not block
progress in arbitrary other Mercury threads.  Foreign code which depends on
the OS thread-local state is usable when called from that thread.

We do not yet allow shared engines to steal parallel work from exclusive
engines.

runtime/mercury_wrapper.c:
runtime/mercury_wrapper.h:
	Rename MR_num_threads to MR_num_ws_engines.  It counts only
	work-stealing engines.  Move comment to the header file.

	Add MR_max_engines.  The default value is arbitrary.

	Add MERCURY_OPTIONS `--max-engines' option.

	Define MR_num_ws_engines and MR_max_engines only with
	MR_LL_PARALLEL_CONJ.

runtime/mercury_context.c:
runtime/mercury_context.h:
	Rename MR_num_idle_engines to MR_num_idle_ws_engines.
	It only counts idle work-stealing engines.

	Extend MR_spark_deques to MR_max_engines length.

	Extend engine_sleep_sync_data to MR_max_engines length.

	Add function to index engine_sleep_sync_data with optional bounds
	checking.

	Replace instances of MR_num_threads by MR_num_ws_engines or
	MR_max_engines as appropriate.

	Add MR_ctxt_exclusive_engine field.

	Rename existing MR_Context fields to remove the implication that the
	engine "owns" the context.  The new exclusive_engine field does
	imply a kind of ownership, hence potential confusion.

	Rename MR_SavedOwner, too.

	Make MR_find_ready_context respect MR_ctxt_exclusive_engine.

	Make MR_schedule_context respect MR_ctxt_exclusive_engine.

	Rename MR_try_wake_an_engine to MR_try_wake_ws_engine
	and restrict it to work-stealing engines.

	Rename MR_shutdown_all_engines to MR_shutdown_ws_engines
	and restrict it to work-stealing engines.

	Make try_wake_engine and try_notify_engine decrement
	MR_num_idle_ws_engines only for shared engines.

	In MR_do_idle, make exclusive engines bypass work-stealing
	and skip to the sleep state.

	In MR_do_sleep, make exclusive engines ignore work-stealing advice
	and abort the program if told to shut down.

	Assert that a context with an exclusive_engine really is only loaded
	by that engine.

	In MR_fork_new_child, make exclusive engines not attempt to wake
	work-stealing engines.  Its sparks cannot be stolen anyway.

	Make do_work_steal fail the attempt for exclusive engines.
	There is one call where this might happen.

	Add notes to MR_attempt_steal_spark.  Its behaviour is unchanged.

	Replace a call to MR_destroy_thread by MR_finalize_thread_engine.

	Delete MR_num_exited_engines.  It was unused.

runtime/mercury_thread.c:
runtime/mercury_thread.h:
	Delete MR_next_engine_id and MR_next_engine_id_lock.  We can no longer
	allocate engine ids by incrementing a counter.  Engine ids need to be
	reused as they act as indices into fixed-sized arrays.

	Extend MR_all_engine_bases to MR_max_engines entries.

	Add MR_all_engine_bases_lock to protect MR_all_engine_bases.

	Add MR_highest_engine_id.

	Add MR_EngineType with the two options described.

	Split the main part of MR_init_engine into a new function which
	accepts an engine type.  MR_init_engine is used by generated code so
	maintain the interface.

	Factor out setup/shutdown for thread support.

	Make MR_finalize_thread_engine call the shutdown function.

	Specialise MR_create_thread into MR_create_worksteal_thread.
	The generic form was unused.

	Move thread pinning into MR_create_worksteal_thread as other threads
	do not require it.

	Delete MR_destroy_thread.  Its one caller can use
	MR_finalize_thread_engine.

	Delete declaration for non-existent variable
	MR_init_engine_array_lock.

runtime/mercury_engine.c:
runtime/mercury_engine.h:
	Add MR_eng_type field.

	Make MR_eng_spark_deque a pointer to separately-allocated memory.
	The reason is given in MR_attempt_steal_spark.

	Add MR_ENGINE_ID_NONE, a dummy value for MR_ctxt_exclusive_engine.

	Delete MR_eng_owner_thread which was obsoleted by engine ids
	before.

	Delete misplaced declaration of MR_all_engine_bases.

runtime/mercury_memory_zones.c:
	Replace MR_num_threads by appropriate counters (I hope).

runtime/mercury_memory_handlers.c:
runtime/mercury_par_builtin.h:
	Conform to changes.

runtime/mercury_threadscope.c:
	Conform to renaming (but it might be wrong).

library/thread.m:
	Add hidden predicate `spawn_native' for testing.
	The interface is subject to change.

	Share much of the code with the high-level C backend.

library/par_builtin.m:
	Delete `num_os_threads' as it is unused.

doc/user_guide.texi:
	Document MERCURY_OPTIONS `--max-engines' option.
MR_release_context can drop thread-local mutables immediately.

runtime/mercury_context.c:
	Null out thread-local mutables in MR_release_context.
Add thread.spawn_native/4 and thread.spawn/4.

Most backends already mapped Mercury threads to "native" threads in spawn/3,
but it was and remains an implementation detail.  spawn_native provides
that behaviour as a documented feature for programs which require it,
including for the low-level C backend.

While we are at it, add a `thread' handle type.  It currently holds a
thread identifier (not yet formally exported), but it may also have
other uses such as a handle for a `thread.join' predicate, or a place to
hold result values or uncaught exceptions.

library/thread.m:
	Add abstract type `thread'.

	Add can_spawn_native.

	Add spawn_native/4.  It can report failure to start a thread,
	which was missing from the spawn/3 interface.

	Add spawn/4 to match spawn_native/4, without the native thread
	requirement.

	Make ML_create_exclusive_thread wait for a success code from
	the new thread before continuing.

	Reduce accessibility levels in C# and Java helper classes.

runtime/mercury_thread.c:
	Make MR_init_thread_inner and MR_setup_engine_for_threads
	return errors instead of aborting on failure.

tests/hard_coded/Mercury.options:
tests/hard_coded/Mmakefile:
tests/hard_coded/spawn_native.exp2:
tests/hard_coded/spawn_native.exp:
tests/hard_coded/spawn_native.m:
	Add test case.

NEWS:
	Announce change.