lua-users home
lua-l archive

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


On Oct 9, 2012, at 12:34 PM, Glenn Schmottlach <gschmottlach@gmail.com> wrote:
> 1) Do I need to somehow anchor my Lua state (L) so that the future 'C' callback has a valid Lua state (and thread) to call back into? If I do need to anchor the Lua state, how do I do this?

int lua_pushthread (lua_State *L);

Pushes the thread represented by L onto the stack. Returns 1 if this thread is the main thread of its state.

You can then store this thread in a table maintained by the library for the purpose of preventing the thread from being garbage collected; this table can be in the library's environment (or be an upvalue of the library's functions) or may be in the registry

> 2) Do I need to be concerned with the Lua thread that the callback is executed on when I call it from 'C' via the lua_call() function? Do I need to explicitly (somehow) anchor this Lua thread/coroutine?
> 
> I hope someone can clear this mystery up for me. I've always had an uneasy feeling about this topic and I suspect other implementations I've looked at may have made some bad assumptions as well.

Callbacks to suspended threads are OK with certain caveats; see:
http://lua-users.org/lists/lua-l/2010-02/msg00447.html

In Lua 5.2 you can use LUA_RIDX_MAINTHREAD to get a handle on the main thread and use it for callbacks.

e