[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: atexit in modules
From: |
Mike Gran |
Subject: |
Re: atexit in modules |
Date: |
Sat, 15 Nov 2008 19:26:14 -0800 (PST) |
> 2008/11/14 Mike Gran :
> >
> > So I'm back to the idea of creating a module-level variable that exists
> > while
> > the module is in memory, and then calling library_end() when that variable
> > is
> > GC'd. But I can make it more schemey by using guardians and after-gc-hook
> > as
> >you suggested.
>
> But are modules ever unloaded? I thought not.
Everything I said above is nonsense. I tried four things, and the last one
works fine.
Here's a review. I had a scheme module that wrapped a C library that required
calls to a library_init() and library_end() function pair that allocated and
freed global data.
Idea 1 was to wrap as library_init() as a scheme function that gets called when
the scheme module is loaded, and then wrap library_end() as a scheme function
that gets called when the module is unloaded. But, modules are never unloaded.
Idea 2 was to wrap library_init() as above, and then wrap library_end() as a
scheme function that gets called when Guile exits. But, there is no such hook
as far as I know for non-REPL Guile scripts.
Idea 3 was some nonsense idea that I could catch a module variable as it was
being garbage collected and use this as the trigger to call library_end().
Ignore everything I said about that.
Idea 4: So ultimately I went with this. The C function library_init() gets
called in init_foo() in the call to (load-extension "libfoo.so" "init_foo").
The C function library_end() gets registered using the libc atexit() during the
same call to init_foo(). That works fine.
-Mike Gran