summaryrefslogtreecommitdiff
path: root/src/script/cpp_api/s_base.cpp
diff options
context:
space:
mode:
authorkwolekr <kwolekr@minetest.net>2015-10-31 16:31:43 -0400
committerkwolekr <kwolekr@minetest.net>2015-11-01 11:32:05 -0500
commit52e5b513ed9dc143c967c733423fe751e1b663d1 (patch)
tree524462147e28889f655cb2ae867ebb412d9c75e3 /src/script/cpp_api/s_base.cpp
parentd198e420ec54be47fe2285b3205953282ec06742 (diff)
downloadminetest-52e5b513ed9dc143c967c733423fe751e1b663d1.tar.gz
minetest-52e5b513ed9dc143c967c733423fe751e1b663d1.tar.bz2
minetest-52e5b513ed9dc143c967c733423fe751e1b663d1.zip
Fix Lua scripting synchronization
For several years now, the lua script lock has been completely broken. This commit fixes the main issue (creation of a temporary rather than scoped object), and fixes a subsequent deadlock issue caused by nested script API calls by adding support for recursive mutexes.
Diffstat (limited to 'src/script/cpp_api/s_base.cpp')
-rw-r--r--src/script/cpp_api/s_base.cpp10
1 files changed, 8 insertions, 2 deletions
diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp
index b40d31533..71369e3d7 100644
--- a/src/script/cpp_api/s_base.cpp
+++ b/src/script/cpp_api/s_base.cpp
@@ -67,10 +67,11 @@ public:
ScriptApiBase
*/
-ScriptApiBase::ScriptApiBase()
+ScriptApiBase::ScriptApiBase() :
+ m_luastackmutex(true)
{
#ifdef SCRIPTAPI_LOCK_DEBUG
- m_locked = false;
+ m_lock_recursion_count = 0;
#endif
m_luastack = luaL_newstate();
@@ -157,9 +158,14 @@ void ScriptApiBase::loadScript(const std::string &script_path)
// - runs the callbacks
// - replaces the table and arguments with the return value,
// computed depending on mode
+// This function must only be called with scriptlock held (i.e. inside of a
+// code block with SCRIPTAPI_PRECHECKHEADER declared)
void ScriptApiBase::runCallbacksRaw(int nargs,
RunCallbacksMode mode, const char *fxn)
{
+#ifdef SCRIPTAPI_LOCK_DEBUG
+ assert(m_lock_recursion_count > 0);
+#endif
lua_State *L = getStack();
FATAL_ERROR_IF(lua_gettop(L) < nargs + 1, "Not enough arguments");