Unloadable C libraries

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Unloadable C libraries

Matt Campbell-2
Hello:

Is it possible for a C library dynamically loaded into Lua to be
unloaded, complete with a call to FreeLibrary or dlclose, without
closing the Lua state?  If so, what requirements must such a C library
meet to be safely unloadable?  Thanks.

--
Matt Campbell
Lead Programmer
Serotek Corporation
www.freedombox.info
"The Accessibility Anywhere People"

Reply | Threaded
Open this post in threaded view
|

Re: Unloadable C libraries

Luiz Henrique de Figueiredo
> Is it possible for a C library dynamically loaded into Lua to be
> unloaded, complete with a call to FreeLibrary or dlclose, without
> closing the Lua state?  If so, what requirements must such a C library
> meet to be safely unloadable?  Thanks.

I think it must ensure that there are no references left to the C functions
it exports. The only way I know to do this is to add a fixed upvalue to each
exported function. In Lua 5.1 you can also arrange for the exported functions
to share the same (private) environment. In both cases, you then have to
set up a __gc metamethod for the upvalue or environment table. When this
metamethod is called, you do whatever clean up is needed by your library and
then you can unload it (though I'm not sure a library can be unloaded from
within itself). --lhf
Reply | Threaded
Open this post in threaded view
|

RE: Unloadable C libraries

Bilyk, Alex
In reply to this post by Matt Campbell-2
We've done it with C++ while evaluating the idea of using C++ as a
scripting language as opposed to using a high level language like Lua,
etc. for game scripting purposes. Worked fine in our tests. We
implemented a proxy function CallDLLMethod() that would do all DLL
management (rebuilding, reloading, etc) logic inside and the app doesn't
get to see any of the direct pointers to DLL code or data entries. The
app only gets to use the interface like

CallDLLMethod(dll_name, method_name, param1, ..., paramN).

So, I'd imagine the same protocol could be used to unload/reload lua
modules as well... in addition to how  Luiz is proposing to solve this
using upvalues.

AB

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Matt Campbell
Sent: Tuesday, May 02, 2006 12:47 PM
To: [hidden email]
Subject: Unloadable C libraries


Hello:

Is it possible for a C library dynamically loaded into Lua to be
unloaded, complete with a call to FreeLibrary or dlclose, without
closing the Lua state?  If so, what requirements must such a C library
meet to be safely unloadable?  Thanks.

--
Matt Campbell
Lead Programmer
Serotek Corporation
www.freedombox.info
"The Accessibility Anywhere People"

Reply | Threaded
Open this post in threaded view
|

Re: Unloadable C libraries

Rob Shaw-2
In reply to this post by Matt Campbell-2
 > (though I'm not sure a library can be unloaded from
 > within itself). --lhf

That's a bizarre enough concept that I had to try it.
The result was a segmentation fault.  Maybe you
could set a flag so that the library was unloaded "later".

Unloading dynamic libraries works fine in Mac OSX,
I just try to keep track of what's been unloaded so as
not to walk off a cliff, calling a function that's no longer there.

Though mostly during debug cycles I've been stepping
out of the interpreter and back in again, as reloading
dynamic libraries has generated even more ways for
me to outsmart myself.

rob