[m-users.] Calling exit(3) from a foreign predicate

Julien Fischer jfischer at opturion.com
Thu Jul 20 20:31:56 AEST 2023


On Thu, 20 Jul 2023, Volker Wysk wrote:

> Just a short question:
>
> May a foreign predicate call the exit(3) function?

A foreign predicate can do that, there's nothing the Mercury compiler
can do to stop it.

> Or does Mercury need a proper shutdown?

Calling exit() directly will bypass the usual shutdown of the Mercury
runtime. Among the effects of that will be:

- Finalizers will not be run.
- The Mercury exit status will not be returned to the OS.

If for some reason, you do want to call exit() from a foreign predicate,
I would suggest something like the following:

      exit(mercury_runtime_terminate());

(mercury_runtime_terminate() is declared in runtime/mercury_wrapper.h)

The only time I've wanted to something similar was from within the
event loop of the old GLUT library, where there that library provided
no way of getting out of the event loop. (See the quit/2 predicate
in extras/graphics/mercury_glut/glut.m.)

In general, given the choice, I would simply throw an exception that is
caught be a top-level exception handler in main/2 and exit (normally)
that way.

Julien.


More information about the users mailing list