[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Proposed C API function: lua_getmainthread
- From: Eric Scouten <scouten@...>
- Date: Fri, 10 Mar 2006 15:35:22 -0800
We have something like this in Lightroom.
When we create the first lua_State, we push a pointer to that state
into the state's registry table. Roughly:
lua_pushlightuserdata( L, &some_constant ); // (see comment below)
lua_pushlightuserdata( L, L );
lua_settable( L, LUA_REGISTRYINDEX );
(We use the address of the function in which the above snippet
appears as the constant. It really doesn't matter. The main thing is
that you use the same address in the snippet below.)
Later, when we want to get back to the root state, we go back to the
registry as follows:
lua_pushlightuserdata( L, &some_constant );
lua_gettable( L, LUA_REGISTRYINDEX );
lua_State* universe = (lua_State*) luaL_checklightuserdata( L, -1 );
lua_pop( L, 1 );
Note: Ours is actually a little more complicated, as we're storing
some other information along with the "universe." I hand-edited this
to remove that extra detail, and I may have bollixed it up a bit in
so doing. Hopefully, this is close enough to be helpful.
On 10 Mar 2006, at 14:50, Matt Campbell wrote:
I think it would be useful to have a C API function called
lua_getmainthread which returns the lua_State of the given state's
main thread. I can think of two uses for this function. It allows
C code to determine whether two lua_State pointers belong to the
same global state. More importantly, C code which needs to receive
a lua_State from Lua and store it long-term for use by callbacks
can use this function to make sure it has a lua_State that isn't
about to go away. Without this measure, C functions called from a
short-lived coroutine get a lua_State pointer that will soon become
This lua_getmainthread function would be very easy to implement; it
only needs to return the mainthread member of the state structure.
I'll probably go ahead and implement it in my copy of Lua (5.1
final). I'm posting this suggestion because I figure it would be
generally useful. Any thoughts?
"The Accessibility Anywhere People"