diff options
| author | pxz <[email protected]> | 2020-08-27 16:25:11 +0800 |
|---|---|---|
| committer | pxz <[email protected]> | 2020-08-27 16:25:11 +0800 |
| commit | eb476a9d84ddf9d3fc5f1f55aa45c51c7d5dc235 (patch) | |
| tree | 421ce552a2b2b0902e42a29d12052e64680711d0 /src | |
| parent | 31dafcf2bf8b8db895e792e141b401a5b3e73ff5 (diff) | |
增加第三方库luajit,修复由jmp_buf导致的多线程性能问题
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | src/tsg_lua_func.cpp | 60 |
2 files changed, 32 insertions, 34 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e98e04d..16e2449 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,9 +1,9 @@ cmake_minimum_required(VERSION 3.5) SET(CMAKE_CXX_COMPILER /usr/bin/g++) aux_source_directory(. DIR_LIB_SRCS) -include_directories("/usr/local/include/luajit-2.1/") -link_directories("/usr/local/lib") -link_libraries("libluajit-5.1.a") +include_directories("../3rd/include/" "/usr/local/include/gperftools") +link_directories("../3rd/lib/") +link_libraries("libluajit.a") add_definitions(-g -W -Wall -O0 -DTSG_LUA_DEBUG) SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -shared -fPIC") diff --git a/src/tsg_lua_func.cpp b/src/tsg_lua_func.cpp index b9e0403..b89eb0b 100644 --- a/src/tsg_lua_func.cpp +++ b/src/tsg_lua_func.cpp @@ -111,7 +111,7 @@ typedef struct{ char buff[TSG_LUA_READER_BUFFSIZE]; }tsg_lua_clfactory_file_t; -static jmp_buf lua_exception[TSG_MAX_LUA_ID]; +//static jmp_buf lua_exception[TSG_MAX_LUA_ID]; static const char *syntax_error[] = { @@ -415,9 +415,9 @@ static int c_lua_atpanic(lua_State *L) char *s = NULL; char err[255]; size_t len = 0; - int *lua_id; + jmp_buf *lua_exception; - lua_id = (int *)lua_getexdata(L); + lua_exception = (jmp_buf *)lua_getexdata(L); if (lua_type(L, -1) == LUA_TSTRING) { s = (char *)lua_tolstring(L, -1, &len); @@ -430,7 +430,7 @@ static int c_lua_atpanic(lua_State *L) } sprintf(err, "lua atpanic:lua VM creashed, reason:%*s\n", (int)len, s); debuginfo(err, __FILE__, __LINE__); - longjmp(lua_exception[*lua_id], 1); + longjmp(*lua_exception, 1); } #if 0 int tsg_lua_memmem(lua_State *L) @@ -484,12 +484,20 @@ static void *mem_alloc(void *ud, void *ptr, size_t osize, size_t nsize) tsg_lua_handle tsg_lua_vm_create() { lua_State *L; - //const char *err; + jmp_buf *lua_exception = (jmp_buf *)tc_malloc(sizeof(jmp_buf)); + if (lua_exception == NULL) + { + return NULL; + } L = luaL_newstate(); //L = lua_newstate(mem_alloc, NULL); if (L == NULL) + { + tc_free(lua_exception); return NULL; + } + lua_setexdata(L, lua_exception); lua_atpanic(L, c_lua_atpanic); luaL_openlibs(L); lua_createtable(L, 0, 2); @@ -497,6 +505,7 @@ tsg_lua_handle tsg_lua_vm_create() //lua_setfield(L, -2, "memmem"); lua_setglobal(L, "tsg"); #if 0 + const char *err; const char *code = "local ffi = require(\"ffi\")\n"\ "ffi.cdef[[char *memmem(const char *haystack, size_t haystacklen, const char *needle, size_t needlelen);]]\n"\ "tsg.ffi = ffi\n"\ @@ -521,7 +530,7 @@ int tsg_lua_exec_file(tsg_lua_handle lua, const char *script, const char *in, si int i = 0; int sharp = 0; tsg_lua_clfactory_file_t lf; - int *lua_id; + jmp_buf *lua_exception; if (L == NULL) { @@ -546,7 +555,7 @@ int tsg_lua_exec_file(tsg_lua_handle lua, const char *script, const char *in, si return ERR_IN_LEN_INVAILD; } - lua_id = (int *)lua_getexdata(L); + lua_exception = (jmp_buf *)lua_getexdata(L); lf.extraline = 0; lf.sent_begin = 0; @@ -661,7 +670,7 @@ int tsg_lua_exec_file(tsg_lua_handle lua, const char *script, const char *in, si lua_setfield(L, -2, "data"); lua_settop(L, 1); - if (setjmp(lua_exception[*lua_id]) == 0) + if (setjmp(*lua_exception) == 0) { if (lua_pcall(L, 0, LUA_MULTRET, 0)) { @@ -734,7 +743,7 @@ int tsg_lua_exec(tsg_lua_handle lua, const char *script, size_t script_len, cons int sharp = 0; int status; tsg_lua_clfactory_buffer_t ls; - int *lua_id; + jmp_buf *lua_exception; if (L == NULL) { @@ -759,7 +768,7 @@ int tsg_lua_exec(tsg_lua_handle lua, const char *script, size_t script_len, cons return ERR_IN_LEN_INVAILD; } - lua_id = (int *)lua_getexdata(L); + lua_exception = (jmp_buf *)lua_getexdata(L); ls.s = script; ls.size = script_len; @@ -858,7 +867,7 @@ int tsg_lua_exec(tsg_lua_handle lua, const char *script, size_t script_len, cons lua_pushlstring(L, in, in_len); lua_setfield(L, -2, "data"); lua_settop(L, 1); - if (setjmp(lua_exception[*lua_id]) == 0) + if (setjmp(*lua_exception) == 0) { if (lua_pcall(L, 0, LUA_MULTRET, 0)) { @@ -1201,7 +1210,7 @@ int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, siz char err_buf[255]; int i = 0; int num = 0; - int *lua_id; + jmp_buf *lua_exception; if (L == NULL) { @@ -1225,7 +1234,7 @@ int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, siz } lua_settop(L, 0); - lua_id = (int *)lua_getexdata(L); + lua_exception = (jmp_buf *)lua_getexdata(L); lua_rawgeti(L, LUA_REGISTRYINDEX, script_id); lua_getglobal(L, "tsg"); @@ -1233,7 +1242,7 @@ int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, siz lua_setfield(L, -2, "data"); lua_settop(L, 1); - if (setjmp(lua_exception[*lua_id]) == 0) + if (setjmp(*lua_exception) == 0) { if (lua_pcall(L, 0, LUA_MULTRET, 0)) { @@ -1299,31 +1308,20 @@ int tsg_lua_cache_exec(tsg_lua_handle lua, size_t script_id, const char *in, siz int tsg_destory_lua(tsg_lua_handle lua) { lua_State *L = (lua_State *)lua; + jmp_buf *lua_exception = NULL; if (L == NULL) { debuginfo("lua VM is null.", __FILE__, __LINE__); return ERR_LUAVM_ISNULL; } - - lua_close(L); - return 0; -} - -int tsg_lua_identify(tsg_lua_handle lua, size_t *id) -{ - lua_State *L = (lua_State *)lua; - if (L == NULL) - { - debuginfo("lua VM is null.", __FILE__, __LINE__); - return ERR_LUAVM_ISNULL; - } - if (*id > TSG_MAX_LUA_ID) + lua_exception = (jmp_buf *)lua_getexdata(L); + if (lua_exception != NULL) { - return ERR_LUA_ID; + tc_free(lua_exception); } - lua_setexdata(L, id); - + lua_close(L); return 0; } + |
