lua-users home
lua-l archive

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


> 
> On Feb 13, 2010, at 2:57 PM, M Joonas Pihlaja wrote:
> 
> > ...  I'm using a global lua_State.  My problem is that 
> > if the user actually set the Lua function callback to call from within 
> > a coroutine instead of the main Lua state, and that coroutine goes 
> > away before the callback gateway is called, then *boom* I've got a 
> > stale lua_State pointer.
> 
> After an off-list discussion with Grant Robinson, I realize I have been assuming that calling back from C to Lua is permitted on any Lua state that is live. In other words, a C callback function may store a Lua thread pointer (lua_State *) and use that pointer to push arguments, call a Lua function, and pop results as long as the thread has not been garbage collected. And that this is OK even if the thread is not the currently executing thread.
> 
> Is it documented anywhere that this is permitted, or not?

This is permitted (as it is no documented otherwise), but be aware of
side effects and interferences. You will have problems if the callback
function leaves the stack changed or if it resumes the same thread it is
using. If the thread is used only for callbacks, you should be safe.

-- Roberto