C-style atexit functions?

For the Compleat Fan

C-style atexit functions?

Postby ryuo » Sun Nov 02, 2014 3:36 pm

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.
ryuo
 
Posts: 43
Joined: Wed May 21, 2014 4:40 pm

Re: C-style atexit functions?

Postby ssqq » Thu Jan 08, 2015 3:46 am

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.
ssqq
 
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

Re: C-style atexit functions?

Postby ralph.ronnquist » Tue Mar 03, 2015 9:52 pm

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
ralph.ronnquist
 
Posts: 178
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: C-style atexit functions?

Postby Lutz » Wed Mar 04, 2015 12:14 am

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!
Lutz
 
Posts: 5258
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: C-style atexit functions?

Postby ralph.ronnquist » Wed Mar 04, 2015 6:33 am

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.
ralph.ronnquist
 
Posts: 178
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia


Return to Anything else we might add?

Who is online

Users browsing this forum: No registered users and 3 guests

cron