Lua 5.1 userstate stuff

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua 5.1 userstate stuff

Diego Nehab-3
Hi,

If you take a look at lua_close...

     LUA_API void lua_close (lua_State *L) {
       L = G(L)->mainthread;  /* only the main thread can be closed */
       luai_userstateclose(L);
       lua_lock(L);
       luaF_close(L, L->stack);  /* close all upvalues for this thread */
       luaC_separateudata(L, 1);  /* separate udata that have GC metamethods */
       L->errfunc = 0;  /* no error function during GC metamethods */
       do {  /* repeat until no more errors */
         L->ci = L->base_ci;
         L->base = L->top = L->ci->base;
         L->nCcalls = 0;
       } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);
       lua_assert(G(L)->tmudata == NULL);
       close_state(L);
     }

... you notice luai_userstateclose is called right before a
lua_lock.  Now, lua_lock probably uses stuff in the user
state that was allocated and initialized during a call to
luai_userstateopen. Isn't this stuff supposed to freed by a
call to luai_userstateclose? And if so, isn't calling
lua_lock right after it A Very Bad Thing (TM)?

What is the rationale for this? Why not call
luai_userstateclose only at the very end?

Regards,
Diego.
Reply | Threaded
Open this post in threaded view
|

Re: Lua 5.1 userstate stuff

Mike Pall-4-2
Hi,

Diego Nehab wrote:
> What is the rationale for this? Why not call
> luai_userstateclose only at the very end?

I guess the best location would be after luaC_freeall() in
close_state(). Because then all GC methods have been run (these
may depend on the userstate) and all threads have been freed
(luaC_freeall() -> luaE_freethread() -> luai_userstatefree()).

Of course the macro must deal with the fact that the main thread
is locked at this point (which makes sense).

A slight complication is that close_state() may be called for an
unsuccessful lua_newstate(), too. And luai_userstateopen() has
not been called in this case.

IMHO something for 5.1.1.

Bye,
     Mike