[mercury-users] Way to exit immediately from a Mercury program?

Julien Fischer juliensf at cs.mu.OZ.AU
Thu Apr 27 15:47:21 AEST 2006


On Thu, 27 Apr 2006, Jonathan Morgan wrote:

> On 4/26/06, Julien Fischer <juliensf at cs.mu.oz.au> wrote:
> > That's what we do in the GLUT binding (see
> > extras/graphics/mercury_glut/glut.m), defining exit similar to the
> > following:
> >
> >         :- pragma foreign_proc("C",
> >                 exit(_IO0::di, _IO::uo),
> >                 [will_not_call_mercury, promise_pure],
> >         "
> >                 exit(mercury_runtime_terminate());
> >         ").
>
> Is it necessary to terminate the runtime?  What happens if you don't?

Finalizers may not be run, your program may return the wrong exit status
to the OS, trace counts or deep profiles may not be written etc.

> > It should be noted, among other things, that terminating a program this
> > way will stuff up the deep profiler (and most probably isn't that great
> > if you wish to use the debugger either).
>
> Does this mean that these tools can't be used usefully used with
> various GUI toolkits? (I don't think GLUT is the only one that is
> likely to need such treatment).

GLUT is something of a special case because glutMainLoop() never returns and
there isn't a quit event processing event or similar to break out of it
cleanly.  Throwing an exception from within one of the callbacks and catch it
at the top-level seems to work (at least on x86-linux) but I didn't do that
when I wrote it because:

(1) throwing exceptions across the C interface isn't recommended

(2) it wasn't clear how GLUT would react if you then tried to re-enter the
main loop (from the look of the source code it would probably be
fine).

FreeGLUT and the other GLUT extension (whose name escapes me at the moment)
have fixed this problem, so probably one of these days I'll extend the GLUT
binding to support them as well.

The debugger is limited with most GUI toolkits anyway because it cannot see
over the C interface, e.g. if you're in a (Mercury) callback the stack trace
will only go back as far as the thing the entry to the Mercury code.

Julien.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list