[m-dev.] thread.spawn_native

Peter Wang novalazy at gmail.com
Fri Jun 6 14:03:32 AEST 2014


Hi,

I propose to add a predicate that always spawns a "native" thread,
where "native" means OS thread or whatever is provided by the
environment we are running in.

:- pred thread.spawn_native(pred(io, io)::in(pred(di, uo) is cc_multi),
    maybe_error::out, io::di, io::uo) is cc_multi.

It would allow low-level C .par grades to spawn OS threads.
This is important for applications which use blocking calls, or have
resources bound to OS threads and not Mercury threads.  These
applications are currently restricted to hlc.par.gc for no good reason.

On low-level C non-.par grades, thread.spawn_native would return an
error.

For all other backends, thread.spawn is currently equivalent to
thread.spawn_native except that the former has no provision to fail.
If someone was so inclined, they could make thread.spawn create
user-mode threads in other grades, too.


As a related proposal, we may add a thread handle type and thread.join
so that the user can be certain when a thread is terminated and its
resources cleaned up.

:- type thread.

:- pred thread.spawn_native(
    pred(thread, io, io)::in(pred(in, di, uo) is cc_multi),
    maybe_error(thread)::out, io::di, io::uo) is cc_multi.

:- pred thread.join(thread::in, io::di, io::uo) is det.

I am not sure whether thread.join should be mandatory to call.

The thread handle would give us a convenient place to hang a thread
identifier, useful for logging and debugging.

The thread handle could have a slot to hold a return value for
thread.join.  Not strictly necessary.

Peter



More information about the developers mailing list