lua-users home
lua-l archive

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


Hi list,

I'm currently writing a library that allow passing data between
several threads, each running a Lua interpreter. The shared data is
stored in a shared Lua state. The lua_lock mechanism ensures that my
two (or more) threads cannot corrupt the state while accessing it
concurrently. However some manipulations of the shared state imply
calling several functions of the Lua API, and since between each call
the state is unlocked another thread could have messed with the stack
state.

For that reason I call lua_lock in my library, and made sure recursive
calls to lua_lock were ok. However my initial implementation of
lua_lock was a fast macro accessing members of the lua_State
structure. Since the structure is not exposed by the Lua API, I cannot
use these macros outside of Lua source code, and had to create a
lua_lock function.

To keeps benefits of both the macro (very fast locking inside Lua
source code) and the function (ability to lock from outside Lua code),
I've modified Lua sources with the attached patch. It replaces all
calls to lua_lock (resp. lua_unlock) with calls to luai_lock (resp.
luai_unlock), which is defined as a no-op macro if not otherwise
defined (like the old lua_lock). Additionnally, I've added two
functions called lua_lock and lua_unlock, declared in lua.h and
implemented in lstate.c, which simply call luai_lock and luai_unlock
respectively.

It would be nice to incorporate such changes in the next version of Lua.

Attachment: luai-lock.patch
Description: Binary data