[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Co routine local data / best practices
- From: Chris Gagnon <cgagnon@...>
- Date: Mon, 30 Nov 2009 15:52:37 -0800
Since ref and the corresponding rawgeti are the fast methods for table access i use the following code to create myself a new co routine.
lua_getfield(MainState, LUA_GLOBALSINDEX, "ThreadTable");lua_State *thread = lua_newthread(MainState);
int refID = luaL_ref(MainState, -2);
So now the when i want to let GC clean up the coroutine i need to :
lua_getfield(MainState, LUA_GLOBALSINDEX, "ThreadTable");
luaL_unref(MainState, -1, refID );
The remaining piece is where do i get refID when i want to unref? or how do I store that refID with the co routine?
Here are some of the possibilities/issues i have run into.
- Environment table
I have multiple co routines sharing an environment which dosn't allow me to uniquely store this value,
without another table which is undesirable from a complexity/performance standpoint
Since the new stack is the only thing completely unshared when creating a co routine, I simply push the value onto the stack.
This works like a charm until an error case, errors leave the stack in the layout of the function that ran into the problem.
In this case i do not know how to recover the refID to properly clean up.
Thoughts about my specific issues?
Other suggestions/approaches that i haven't mentioned?
Performance is a big concern to me with that in mind I'm curious how you guys would solve/layout something like this.