C-style atexit functions?

For the Compleat Fan
Locked
ryuo
Posts: 43
Joined: Wed May 21, 2014 4:40 pm

C-style atexit functions?

Post by ryuo »

I've been wondering if you would be willing to add a feature like ISO C's atexit function. Basically, the ability to register functions to be executed when someone calls (exit) from within newLISP or similar. This could be useful for cleaning up global symbols inside a module context or program. I'm thinking stuff that was allocated by an imported function, which is not managed by newLISP.

If this is not a good idea for a builtin, then I could just emulate it with a wrapper to the exit function I suppose. But, I figured it would be better if it was handled by newLISP. Anyway, thanks.

ssqq
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

Re: C-style atexit functions?

Post by ssqq »

yes, When we import some module, imported symbols list is useful, when it is not useable, delete it from symbols table also is a good habbit.

ralph.ronnquist
Posts: 228
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: C-style atexit functions?

Post by ralph.ronnquist »

This may well be too platform dependent, but on Linux (Ubuntu 12.04) one can augment the unix.lsp module with an import of on_exit and then register a callback to be performed on exit. Example:

Code: Select all

(module "unix.lsp")
(import unix:library "on_exit")
(on_exit (callback 0 'on-exit) 0)
(define (on-exit) (println "Cheerio!"))


This, as it should, invokes the on-exit function when the process exits. And it works well togeter with signal handlers such as for instance:

Code: Select all

(define (handler x) (exit 1))
(signal 2 handler) ; QUIT
(signal 15 handler) ; TERM

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: C-style atexit functions?

Post by Lutz »

on Mac OS X and other BSDs use atexit(...):

Code: Select all

> (import "libc.dylib" "atexit")
atexit@7FFF833285B1
> (atexit (callback 0 'on-exit))
0
> (define (on-exit) (println "Cheerio!"))
(lambda () (println "Cheerio!"))
> (exit)
Cheerio!

ralph.ronnquist
Posts: 228
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: C-style atexit functions?

Post by ralph.ronnquist »

Yes. The Linux man pages also speak highly about using atexit in preference of on_exit, but for some reason I can't import it. By nm, the library symbol table has atexit as "text" item, but when trying to import it I get the complaint:
ERR: import function not found in function import : "/lib/i386-linux-gnu/libc.so.6: undefined symbol: atexit"
Peculiar. But on_exit imports without complaints, and apparently does the same thing.

Locked