lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> On Behalf Of Glenn Schmottlach
> Sent: dinsdag 9 oktober 2012 18:34
> 
> I have a question about providing Lua callbacks from a wrapped C
> library that seems to have never been fully answered (at least I can't
> find the thread). I am hoping someone can offer a recommended approach
> for this problem.
> 
> In general, when I've wrapped 'C' libraries with Lua there are
> invariably callback functions that need to be handled as well. For the
> sake of this discussion I am assuming the 'C' callback is delivered on
> the same OS thread on which the main Lua state has been initialized in.

>From your Lua code you call into C code, which executes something and
returns a callback. As long as you do not touch the luastate, the garbage
collector will not have a chance to run. Though this is more theoretical as
I assume the callback delivers some result you need to push on the stack,
and that is the very moment you touch the lua state and then GC might run.
But then still, the L reference you got represents the current active thread
in the Lua state, which hasn't continued as the code (the OS thread) was
waiting for the callback to occur. I doubt, but have no certainty, that the
GC will ever collect the currently active thread, even if not anchored.
Maybe someone else can confirm this.

Continuing the line of thought; if it could GC the active thread, then EVERY
piece of C code MUST always have its first statement anchor the thread
because it might be collected (which would be silly). So my logical
conclusion is that my statement above must be right.