Skip to content

Commit

Permalink
Added function to disable locks, fixed -0
Browse files Browse the repository at this point in the history
  • Loading branch information
MCJack123 committed Apr 3, 2021
1 parent 2cdee55 commit 3ebc137
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */



Expand Down
10 changes: 10 additions & 0 deletions src/lapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

3 changes: 2 additions & 1 deletion src/lcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions src/llock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
1 change: 1 addition & 0 deletions src/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */


/*
Expand Down
3 changes: 2 additions & 1 deletion src/lvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 3ebc137

Please sign in to comment.