[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: c module, using static variables
- From: Graham Wakefield <wakefield@...>
- Date: Sun, 15 Apr 2012 17:05:56 -0700
There is in 5.2, but not in 5.1, unless you dig into the internals (not portable).
One workaround is to assume that the L used in your luaopen_xxx is the master state, and store it in the registry. But that still doesn't help you, since the registry isn't necessarily safe to access from a separate thread.
Is there really no way to attach the structure to the callback itself? Most callback-oriented APIs allow the attachment of a userdata pointer to avoid using globals.
On Apr 15, 2012, at 1:49 PM, Thijs Schreijer wrote:
> OK, thanks, looked it up in the ref manual; lua_newthread. So L basically identifies ‘independent execution stacks’
> So, as I understand it, I need to be able to identify the ‘master’ state to fix my issue, as my static c variables should be shared amongst all the child states, but only within one ‘master’ state. The L of the ‘master’ state should then be my unique identifier.
>
> So is there a way to get the ‘master’ L from an L representing a coroutine/childstate?
>
> Thijs
>
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On Behalf Of wesley.hoke@gmail.com
> Sent: zondag 15 april 2012 21:21
> To: Lua mailing list
> Cc: Lua mailing list
> Subject: Re: c module, using static variables
>
> The L pointer isn't necessarily related to the master Lua state. It can have a different value if a coroutine calls into your function.
>
> wes
>
> On Apr 15, 2012, at 12:00 PM, "Thijs Schreijer" <thijs@thijsschreijer.nl> wrote:
>
> I’m sort of stumbling along in my quest to learn c to create lua modules and every time I think I got it all figured out, something new pops up.
>
> So now I’m creating a C module and have defined some static variables. Now I read in PiL somewhere (couldn’t find it when I went looking for it) that static variables are a bad idea, this related to multiple lua states.
>
> So my understanding is now, that if I create a c module ‘mymodule’ with a static variable, then if an application creates 2 lua states that each require ‘mymodule’, the c code of my module will be loaded only once, and be executed against the two different lua states.
> This implies that the two states would then share the same static variable in my c module. Correct?
>
> Because I’m using async callbacks (on another OS thread), when one of the callbacks arrives, I cannot call into the lua state to collect the values from the registry or from an upvalue safely. This prevents me from putting all my static variables in a big struct and store that as a userdata in the registry or as an upvalue.
>
> If I would incorporate L (from lua_State *L from a function call) into my data structures to identify to which lua state that structure belongs, would that be safe? Is L only valid during a single function call, or does it remain valid (and unchanged) for as long as that lua state exists?
>
> Whats the best approach? Any help is appreciated.
>
> Thijs