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

Volker Wysk post at volker-wysk.de
Fri Jan 26 00:31:03 AEDT 2024


Am Donnerstag, dem 25.01.2024 um 13:31 +1100 schrieb Mark Brown:
> On Thu, Jan 25, 2024 at 1:40 AM Volker Wysk <post at volker-wysk.de> wrote:
> > Calling exit(3), without calling mercury_runtime_terminate() before, works.
> > All thread are terminated and the program exits. It's only
> > mercury_runtime_terminate() that blocks. I've read somewhere that it waits
> > for all threads to terminate. Is there another function which doesn't? I'd
> > like to properly shut down the Mercury runtime, if possible.
> 
> How was it started in the first place? If you entered via main/2 then
> in principle the "proper" way to leave would be by exiting main/2. If
> you used the standalone interface then mercury_terminate() is
> documented right after mercury_init() in the User's Guide.

My master thread (the main program) spawns some worker threads, which begin
in the workers' main predicate. Which they leave after they have received an
"end" message (from a channel). The workers don't get started in main/2. 

And I'm not using the standalone interface.

It's clear to me, that the threads should terminate by leaving the
predicate, which they have started in, as specified for the thread.spawn/3
predicate.


> Do you have a situation (as in the GLUT example) where the existing
> interfaces don't work?

I'll answer that in the reply for Sean's message.

> Mark
> 
> > 
> > Volker
> > 
> > Am Dienstag, dem 23.01.2024 um 19:11 +0100 schrieb Volker Wysk:
> > > 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.
> > > > 
> > > > > 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());
> > > 
> > > I have multiple threads, and the mercury_runtime_terminate() call hangs. It
> > > doesn't hang if there aren't any (additional) threads. I also get this
> > > sometimes:
> > > 
> > > fis: mercury_context.c:2571: action_shutdown_ws_engine: Assertion `engine_id
> > > != 0' failed.
> > > 
> > > Do you know a way to kill all threads? I've searched the web for a long time
> > > and couldn't find any. (There's a page from IBM, but that seems to apply
> > > only to AIX. It doesn't work that way in Linux.) I've also looked at
> > > mercury_wrapper.h, mercury_thread.h and mercury_threadscope.h.
> > > 
> > > > 
> > > > (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.
> > > 
> > > I've tried this. It looks like the program doesn't exit while there are
> > > still running threads. Those can be long-running, so I need to kill them
> > > asynchronously.
> > > 
> > > Cheers,
> > > Volker
> > > _______________________________________________
> > > users mailing list
> > > users at lists.mercurylang.org
> > > https://lists.mercurylang.org/listinfo/users
> > 
> > _______________________________________________
> > users mailing list
> > users at lists.mercurylang.org
> > https://lists.mercurylang.org/listinfo/users



More information about the users mailing list