[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: hooks & coroutines
- From: Jamie Webb <j@...>
- Date: Tue, 1 Jun 2004 00:26:50 +0100
On Monday 31 May 2004 22:36, Mike Pall wrote:
> > You could save the Lua thread value in the registry when you first create
> > it, keyed on the state pointer.
>
> This sentence does not make sense to me. The state pointer *IS* the thread
> value, so the key would be the same as the value.
No. The lua_newthread() function pushes onto the Lua stack a /Lua value/
representing the thread. This is quite distinct from the the state pointer
returned to the C caller.
> And upon retrieval you can only index a table (e.g. the registry) with a
> key that is on the stack. But you cannot put 'L' on the stack -- there is
> just no public API for that.
Yes there is. lua_pushlightuserdata(). This light userdata forms the key, and
the Lua thread object is the value.
> > > I see no way to get at the current thread from within Lua code. I've
> > > proposed coroutine.current() and an extension to debug.getinfo() to
> > > allow for that. See my previous message on this list.
> >
> > And given the above you could easily implement this yourself.
>
> Ok, maybe I'm missing something obvious. So, please show me:
Untested:
int create_thread(lua_State *L)
{
lua_State* thread = lua_newthread(L);
lua_pushlightuserdata(L, L);
lua_pushvalue(L, -2);
lua_rawset(L, LUA_REGISTRYINDEX)
/* ...do whatever it is you normally do with the thread... */
}
int get_current_thread(lua_State* L)
{
lua_pushlightuserdata(L, L);
lua_rawget(L, LUA_REGISTRYINDEX);
return 1;
}
I've never looked to see if threads have separate registries. If they do,
you'll need to use lua_xmove() and save the thread to the new registry.
-- Jamie Webb