[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Co routine local data / best practices
- From: Jim Pryor <lists+lua@...>
- Date: Wed, 2 Dec 2009 23:59:17 -0500
On Wed, Dec 02, 2009 at 05:16:33PM -0800, Chris Gagnon wrote:
> Your very close I'm just going to go ahead and explain it in code because
> the problem really is an implementation one.
>
> LuaManager::StartCoroutine(char * func)
> {
> lua_getfield(MainState, LUA_GLOBALSINDEX, "ThreadTable");
> lua_State *thread = lua_newthread(MainState);
> int refID = luaL_ref(MainState, -2);
>
> // start func on the new thread some setup removed for brevity
> int iRet = lua_resume(m_pLuaState, n);
> if (iRet == 0)
> {
> // clean up i have refID in this case all is good
> }
> else if (iRet == LUA_YIELD)
> {
> -> i have a refID what do i do with it?
> -> Attempting to avoid creating a map because I feel
> like I should be able to pack it with thread.
> }
> else
> {
> // error
> }
> }
>
> int SleepSystem::call_c_sleep_function(lua_State * L)
> {
> // save off L and an associated time
> return lua_yield(L);
> }
>
> int SleepSystem::process(...)
> {
> // realizes that L should be resumes since it's sleep time is up
> int iRet = lua_resume(L);
> if (iRet == 0)
> {
> // clean up i have refID in this case all is good
> }
> else if (iRet == LUA_YIELD)
> {
> -> i cannot clean up because i don't have refID to do
> luaL_unref(...)
> }
> else
> {
> // error
> }
> }
Sorry, none of this was obscure to me...it's what you're thinking will
go on in _between_ these functions that is obscure. Pretend that there
are calls lua_AssociateValue and lua_GetAssociatedValue that pack
whatever values you want together with the thread. Using them, can you fill in the
gaps some more so that I can see you start two threads (lets say simple
Lua functions), and have them each suspend by calling
SleepSystem::whatever, and then be resumed at the appropriate time, then
they stop. Maybe that will help me make a more useful suggestion about
how to implement lua_AssociateValue etc.
--
Jim Pryor
profjim@jimpryor.net