Re: [m-users.] Calling exit(3) from a foreign predicate
Volker Wysk <[email protected]>
| Newsgroups | gmane.comp.lang.mercury.general |
|---|---|
| Message-ID | <[email protected]> |
Am Donnerstag, dem 20.07.2023 um 20:31 +1000 schrieb Julien Fischer:
> 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.
I mean, is it allowed to?
> > 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.
That's how I did it until now.
This leads me to a simple idea. What about introducing some type
:- type shutdown ---> shutdown(int).
in io.m, and a predicate
:- pred exit(int::in) is erroneous.
exit(ExitCode) :-
throw(shutdown(ExitCode)).
The Mercury runtime would catch shutdown/1 exceptions and exit with the
supplied exit code. After doing all shutdown and cleanup. The program would
still be able to catch such shutdown/1 exceptions, if that is desired.
Cheers,
Volker
_______________________________________________
users mailing list
[email protected]
https://lists.mercurylang.org/listinfo/users
signature.asc
(application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE6QXGh82Ov3+2nrxp+K4ydFOsHoUFAmS5FlUACgkQ+K4ydFOs HoV/Hw//T6kpBih7ZgONWm+z03m0aqF0Ts/G/8u5rysCk1k7Qx9Y3PJ/L+9JThAO F/xIXbRG00SRxn1NZAaoV3SSKMixbJojQB/o8fLVifeGWcP0BYr7EUyUxXqwtTHk kZjyk9fIsRpSUnw5u5CjL1ONlHuF4OfO9hlw7sQTHxyoKj9kZTSsvdiv7+5AVvlW 8PBmXNhaZ9t6cUmmSCRjIm7RDA0Xt6Anxfdo3lckpy0DjTiXIIBCDv63lWgiri7/ 1potu24kcHWhq1J9xJL/EbfzErKfvPQVWNTJ4upfQs9NfpHXZrTtvwFY5N0LSyrg rpTNUBA/CqoILwkymxGhc70Ifnf649ybzSMjvtvlUF9hUaGBO647m5zf6X/RqkTT R/0ekXldnLm6F1Q7IugANnSCzbo57KuQ14J5Nj1wXU2d6nn4Zke5zCSve54ggzCc 8wvfodB9OCE+vQqwoJGbuzIhep14YNACuRAnjInyyOPkWxQiEFNh/mPRj1UWoO8w LWqaMDEMiX+vohPsjdP6E5yolq6xnUYa/dQ1yZTfRuUkBiNeXB5ltzve2uK4fetl PZDKmkfB6Rv+rCWxDHnyowYXTpNeLJcw+cjtrnZiVcMvvY0cZge74EochW688VSX CEjkpcASQfQfkgmlKXYjlZQlYswQUg6YRSE0WaqWahkir1SX/fY= =nPL7 -----END PGP SIGNATURE-----