Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of lua_lock and lua_unlock?

Tags:

lua

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.

like image 690
anon Avatar asked Jun 10 '10 00:06

anon


1 Answers

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:

  • Proposal: fast lua_lock
  • Why write lua_unlock before lua_lock?
  • Mailing list
like image 87
Mark Rushakoff Avatar answered Dec 12 '22 15:12

Mark Rushakoff