lua-users home
lua-l archive

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

On Thursday 19 July 2012 14:43:31 Thijs Schreijer wrote:
> > 1) Determine if two threads belong to the same shared interpreter state:
> >
> >   bool is_same_state(lua_State *L1, lua_State *L2) { return L1->l_G ==
> L2->l_G; }
> >
> >   I used this for some assertions. I believe the only way around it
> >   involves using the registry, and thus writing to the stack of at
> >   least one of the states. This way is purely symmetrical, read-only
> >   and fast.
> I've had this one on my mind for a while, but I'm not convinced whether this
> is a good idea.
> Often multiple states are used with multiple threads (OS threads). Then your
> code in unsafe.

Yes, this is rather limited in usefulness. But the code itself
is safe under assumption that both state pointers are valid and
continue to be valid and allocated throughout the check: the field
being used never changes between initialization and deallocation
of the struct. Alternatively, you can also do L->l_G->mainthread to
get the root lua_State*, and compare them; if one of the pointers
is guaranteed to always be root, you don't even need to access it.

I compare the state given as a function argument to a global
variable holding a state that is never deallocated; and this is
only done in an assertion in any case.