lua-users home
lua-l archive

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


I have more questions with respect to cleaning up my co routines.

In all places i call resume, i check the status and decide if i need to clean it up.
Lets say I've referenced the coroutine somewhere.

lua_getfield(MainState, LUA_GLOBALSINDEX, "ThreadTable");
lua_State *thread = lua_newthread(MainState);
int refID = luaL_ref(MainState, -2);
lua_pop(MainState,1);

The question is where should i store my refID so that i can clean up when a resume returns and it needs to clean up the co routine.
What i did was put it on the stack of my new thread, which worked like a charm until there is an error in lua which leaves the stack in a bad state, and i don't know how to get my refID.

What would you guys suggest?

-Chris


On Wed, Oct 7, 2009 at 8:28 AM, Jerome Vuarand <jerome.vuarand@gmail.com> wrote:
2009/10/7 Chris Gagnon <cgagnon@zindagigames.com>:
>
>> Sorry if I misunderstand, but isn't this all you want?
>>
>> lua_getfield(MainState, LUA_GLOBALSINDEX, "ThreadTable");
>> lua_pushnil(MainState);
>> lua_rawseti(MainState, -2, refID);
>> lua_pop(MainState, 1); /* ThreadTable */
>
> I asked the question poorly, the code to release the coroutine isn't the
> problem.
> The placement of that code is, i have multiple entry point back to my co
> routines from different systems that utilize yeild/resume to implement
> functionality.
> Do you need to check the state of the thread after ever lua_resume call? and
> then do code like you suggest?
> Or is there a way to do it more generically then that, I'm leaning towards
> the latter.
>
> Sorry for the confusing question, and thank you for the replies.

You have to check for the return value of lua_resume. If it's anything
other than LUA_YIELD, the coroutine is no longer resumable. Anyway you
need to check that value to make sure you don't call lua_resume on it
anymore, so in that check you can also remove the reference you have
in your ThreadTable (and actually if you only resume coroutines from
that table, that's the only thing you need to do).