diff --git a/include/lua.h b/include/lua.h index 5248f72..769d472 100644 --- a/include/lua.h +++ b/include/lua.h @@ -262,6 +262,7 @@ LUA_API void (lua_halt) (lua_State *L); /* forcefully halts the Lua state speci warning: this will leave the state in an invalid state; do not use the state after calling this - close it immediately */ LUA_API void (lua_externalerror) (lua_State *L, const char * message); /* throws an error into a running state - meant to be run from a different thread */ +LUA_API void (lua_setlockstate) (lua_State *L, int enabled); /* enables/disables lua_lock */ diff --git a/src/lapi.c b/src/lapi.c index d9db268..39bf61b 100644 --- a/src/lapi.c +++ b/src/lapi.c @@ -1166,3 +1166,13 @@ LUA_API void lua_externalerror(lua_State *L, const char * message) { lua_unlock(L); } +LUA_API void lua_setlockstate(lua_State *L, int enabled) { + lua_lock(L); + if (!enabled) { + G(L)->lockstate = G(L)->lockstate >= 2 ? 2 : 3; + } else { + G(L)->lockstate = G(L)->lockstate >= 2 ? 0 : 1; + } + lua_unlock(L); +} + diff --git a/src/lcode.c b/src/lcode.c index 3bdfd63..ce9f6e5 100644 --- a/src/lcode.c +++ b/src/lcode.c @@ -626,7 +626,8 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { static lua_Number luai_nummod(lua_Number a, lua_Number b) { lua_Number q = fmod(a, b); - if ((a < 0) != (b < 0) && q != 0) return q + b; + if (q == 0) return 0; + else if ((a < 0) != (b < 0)) return q + b; return q; } diff --git a/src/llock.cpp b/src/llock.cpp index 0c215a4..18c6f93 100644 --- a/src/llock.cpp +++ b/src/llock.cpp @@ -8,16 +8,17 @@ extern "C" { extern "C" { void _lua_lock(lua_State *L) { + if (G(L)->lockstate == 2) return; ((std::mutex*)G(L)->lock)->lock(); G(L)->lockstate = 1; } void _lua_unlock(lua_State *L) { - if (!G(L)->lockstate) { + if (G(L)->lockstate != 1 && G(L)->lockstate != 3) { //fprintf(stderr, "Attempted to unlock a thread twice!\n"); return; } - G(L)->lockstate = 0; + G(L)->lockstate--; ((std::mutex*)G(L)->lock)->unlock(); } diff --git a/src/lua.h b/src/lua.h index d77f31a..8759ada 100644 --- a/src/lua.h +++ b/src/lua.h @@ -262,6 +262,7 @@ LUA_API void (lua_halt) (lua_State *L); /* forcefully halts the Lua state speci warning: this will leave the state in an invalid state; do not use the state after calling this - close it immediately */ LUA_API void (lua_externalerror) (lua_State *L, const char * message); /* throws an error into a running state - meant to be run from a different thread */ +LUA_API void (lua_setlockstate) (lua_State *L, int enabled); /* enables/disables lua_lock */ /* diff --git a/src/lvm.c b/src/lvm.c index 517bb3a..bc3e3d6 100644 --- a/src/lvm.c +++ b/src/lvm.c @@ -319,7 +319,8 @@ void luaV_concat (lua_State *L, int total, int last) { static lua_Number luai_nummod(lua_Number a, lua_Number b) { lua_Number q = fmod(a, b); - if ((a < 0) != (b < 0) && q != 0) return q + b; + if (q == 0) return 0; + else if ((a < 0) != (b < 0)) return q + b; return q; }