12

What is the point of lua_lock and lua_unlock?

The following implies it's important:

LUA_API void lua_gettable (lua_State *L, int idx) {
  StkId t;
  lua_lock(L);
  t = index2adr(L, idx);
  api_checkvalidindex(L, t);
  luaV_gettable(L, t, L->top - 1, L->top - 1);
  lua_unlock(L);
}


LUA_API void lua_getfield (lua_State *L, int idx, const char *k) {
  StkId t;
  TValue key;
  lua_lock(L);
  t = index2adr(L, idx);
  api_checkvalidindex(L, t);
  setsvalue(L, &key, luaS_new(L, k));
  luaV_gettable(L, t, &key, L->top);
  api_incr_top(L);
  lua_unlock(L);
}

The following implies it does nothing:

#define lua_lock(L)     ((void) 0) 
#define lua_unlock(L)   ((void) 0)

Please enlighten.

1 Answer 1

20

If you port Lua to another platform, you are "allowed" to overwrite lua_lock with your own definition; and this definition should essentially be a mutex, to disallow cross-thread operations on the same Lua objects. Essentially, when implemented, it should act similarly to Python's Global Interpreter Lock (GIL).

It's defined to a no-op in vanilla Lua, because vanilla Lua is 100% ANSI-C and runs in a single thread: there's no need for any locking mechanism to be implemented. However, the developers chose to put the lock statements in there for people who port Lua and implement threading in the interpreter.

Sources:

5
  • If this needs to be mutex-protected on other archs, why is it not necessary on 64-bit ubuntu on x86? [the system I'm running on]
    – anon
    Jun 10, 2010 at 1:02
  • If you allow the Lua API to be called for the same lua_State from multiple threads, then you must define an appropriate lua_lock and lua_unlock. The default build does not do this because it hurts performance for the most common single-thread case, so you cannot use a single lua_State from multiple threads.
    – RBerteig
    Jun 10, 2010 at 22:59
  • 3
    You still need to be careful if you implement multiple threads own your own. If two threads attempt to iterate over a table at the same time the lua_lock and lua_unlock won't be sufficient.
    – Mike M.
    Mar 30, 2012 at 2:54
  • 2
    The sane way to do multi-threading in Lua is have one Lua state per thread and communicate between them either explicitly or by using shared C data structures. E.g. feel free to take any of the existing concurrent data structures for C++11 or Facebook's folly, or Intel TBB and bind them for Lua (so easy with LuaJIT), or use any other C data structure and implement your own locking around it. You can even use lanes and pass actual Lua structures back and forth between states. Talk about flexible!
    – Eloff
    Apr 7, 2013 at 21:02
  • "One Lua state per thread" is not actually very sane. Because: for one thing merely associating threads with lua_State does not properly lock the "global state" (not actually truly global as Lua is fully re-entrant, but that's what it's called) of threads - you need to use lua_lock() for that if you do anything at all with calls to the Lua API from multiple threads. On the other hand, it's simultaneously /too/ restrictive - Lua threads (coroutines) use multiple lua_State even when they all exist in the same thread / serialized - you're essentially forbidding coroutines with this rule.
    – BadZen
    May 2, 2022 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.