use stdatomic (#1317)

This commit is contained in:
云风
2021-01-11 10:54:34 +08:00
committed by GitHub
parent 284df5430b
commit b164e3a8a9
12 changed files with 201 additions and 138 deletions

View File

@@ -30,7 +30,7 @@ struct snlua {
size_t mem_report;
size_t mem_limit;
lua_State * activeL;
volatile int trap;
volatile ATOM_INT trap;
};
// LUA_CACHELIB may defined in patched lua for shared proto
@@ -67,8 +67,8 @@ signal_hook(lua_State *L, lua_Debug *ar) {
struct snlua *l = (struct snlua *)ud;
lua_sethook (L, NULL, 0, 0);
if (l->trap) {
l->trap = 0;
if (ATOM_LOAD(&l->trap)) {
ATOM_STORE(&l->trap , 0);
luaL_error(L, "signal 0");
}
}
@@ -76,7 +76,7 @@ signal_hook(lua_State *L, lua_Debug *ar) {
static void
switchL(lua_State *L, struct snlua *l) {
l->activeL = L;
if (l->trap) {
if (ATOM_LOAD(&l->trap)) {
lua_sethook(L, signal_hook, LUA_MASKCOUNT, 1);
}
}
@@ -88,9 +88,9 @@ lua_resumeX(lua_State *L, lua_State *from, int nargs, int *nresults) {
struct snlua *l = (struct snlua *)ud;
switchL(L, l);
int err = lua_resume(L, from, nargs, nresults);
if (l->trap) {
if (ATOM_LOAD(&l->trap)) {
// wait for lua_sethook. (l->trap == -1)
while (l->trap >= 0) ;
while (ATOM_LOAD(&l->trap) >= 0) ;
}
switchL(from, l);
return err;
@@ -506,7 +506,7 @@ snlua_create(void) {
l->mem_limit = 0;
l->L = lua_newstate(lalloc, l);
l->activeL = NULL;
l->trap = 0;
ATOM_INIT(&l->trap , 0);
return l;
}
@@ -520,13 +520,17 @@ void
snlua_signal(struct snlua *l, int signal) {
skynet_error(l->ctx, "recv a signal %d", signal);
if (signal == 0) {
if (l->trap == 0) {
if (ATOM_LOAD(&l->trap) == 0) {
ATOM_INT zero;
ATOM_INIT(&zero, 0);
// only one thread can set trap ( l->trap 0->1 )
if (!ATOM_CAS(&l->trap, 0, 1))
if (!ATOM_CAS(&l->trap, zero, 1))
return;
ATOM_INT one;
ATOM_INIT(&one, 1);
lua_sethook (l->activeL, signal_hook, LUA_MASKCOUNT, 1);
// finish set ( l->trap 1 -> -1 )
ATOM_CAS(&l->trap, 1, -1);
ATOM_CAS(&l->trap, one, -1);
}
} else if (signal == 1) {
skynet_error(l->ctx, "Current Memory %.3fK", (float)l->mem / 1024);