Skip to content

Commit

Permalink
refine default libpath luailbcpath implement
Browse files Browse the repository at this point in the history
always include dir(os.argv[0])/lualib/?.lua and
dir(os.argv[0])/luaclib/?.so
  • Loading branch information
findstr committed May 15, 2024
1 parent 3e78826 commit 644ccf2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 46 deletions.
1 change: 1 addition & 0 deletions silly-src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ int main(int argc, char *argv[])
struct silly_config config;
config.argc = argc;
config.argv = argv;
config.selfpath = argv[0];
config.selfname = selfname(argv[0]);
if (argc < 2) {
print_help(config.selfname);
Expand Down
1 change: 1 addition & 0 deletions silly-src/silly.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct silly_config {
int timeraffinity;
int argc;
char **argv;
const char *selfpath;
const char *selfname;
char bootstrap[PATH_MAX];
char lualib_path[PATH_MAX];
Expand Down
9 changes: 0 additions & 9 deletions silly-src/silly_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@
#define LUA_GC_STEP (100) //KiB
#endif

#ifndef DEFAULT_LUA_PATH
#define DEFAULT_LUA_PATH "lualib/?.lua"
#endif

#ifndef DEFAULT_LUA_CPATH
#define DEFAULT_LUA_CPATH "luaclib/?.so"
#endif


//(1 << 16) = 65536
#define SOCKET_MAX_EXP (16)
#define TIMER_RESOLUTION (10) //ms
Expand Down
6 changes: 3 additions & 3 deletions silly-src/silly_run.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ silly_run(const struct silly_config *config)
silly_worker_init();
silly_monitor_init();
srand(time(NULL));
thread_create(&pid[0], thread_socket, NULL, config->socketaffinity);
thread_create(&pid[1], thread_timer, NULL, config->timeraffinity);
thread_create(&pid[2], thread_worker, (void *)config, config->workeraffinity);
silly_log_info("%s %s is running ...\n", config->selfname, SILLY_RELEASE);
silly_log_info("cpu affinity setting, timer:%d, socket:%d, worker:%d\n",
config->timeraffinity, config->socketaffinity, config->workeraffinity);
thread_create(&pid[0], thread_socket, NULL, config->socketaffinity);
thread_create(&pid[1], thread_timer, NULL, config->timeraffinity);
thread_create(&pid[2], thread_worker, (void *)config, config->workeraffinity);
monitor_check();
for (i = 0; i < 3; i++)
pthread_join(pid[i], NULL);
Expand Down
64 changes: 30 additions & 34 deletions silly-src/silly_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "silly_monitor.h"
#include "silly_worker.h"

#define max(a, b) ((a) > (b) ? (a) : (b))
#define WARNING_THRESHOLD (64)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define WARNING_THRESHOLD (64)

struct silly_worker {
int argc;
Expand Down Expand Up @@ -91,38 +91,26 @@ silly_worker_callback(void (*callback)(struct lua_State *L, struct silly_message
return ;
}

static int
setlibpath(lua_State *L, const char *libpath, const char *clibpath)
static void
setlibpath(lua_State *L, const char *pathname, const char *libpath)
{
const char *path;
const char *cpath;
size_t sz1 = strlen(libpath);
size_t sz2 = strlen(clibpath);
size_t sz3;
size_t sz4;
size_t sz1;
size_t sz2 = strlen(libpath);
size_t need_sz;

const char *path;
if (sz2 == 0)
return ;
lua_getglobal(L, "package");
lua_getfield(L, -1, "path");
path = luaL_checklstring(L, -1, &sz3);

lua_getfield(L, -2, "cpath");
cpath = luaL_checklstring(L, -1, &sz4);

need_sz = max(sz1, sz2) + max(sz3, sz4) + 1;
lua_getfield(L, -1, pathname);
path = luaL_checklstring(L, -1, &sz1);
need_sz = sz2 + sz1 + 1;
char new_path[need_sz];

snprintf(new_path, need_sz, "%s;%s", libpath, path);
lua_pushstring(L, new_path);
lua_setfield(L, -4, "path");

snprintf(new_path, need_sz, "%s;%s", clibpath, cpath);
lua_pushstring(L, new_path);
lua_setfield(L, -4, "cpath");

lua_setfield(L, -3, pathname);
//clear the stack
lua_settop(L, 0);
return 0;
return ;
}

static void *
Expand Down Expand Up @@ -164,6 +152,8 @@ void
silly_worker_start(const struct silly_config *config)
{
int err;
int dir_len;
int lib_len;
lua_State *L = lua_newstate(lua_alloc, NULL);
luaL_openlibs(L);
W->argc = config->argc;
Expand All @@ -173,14 +163,20 @@ silly_worker_start(const struct silly_config *config)
#else
lua_gc(L, LUA_GCGEN, 0, 0);
#endif
setlibpath(L, DEFAULT_LUA_PATH, DEFAULT_LUA_CPATH);
err = setlibpath(L, config->lualib_path, config->lualib_cpath);
if (unlikely(err != 0)) {
silly_log_error("[worker] set lua libpath fail,%s\n",
lua_tostring(L, -1));
lua_close(L);
exit(-1);
}
//set load path
lib_len = max(sizeof("lualib/?.lua"), sizeof("luaclib/?.so"));
dir_len = config->selfname - config->selfpath;
char buf[dir_len + lib_len];
setlibpath(L, "path", config->lualib_path);
setlibpath(L, "cpath", config->lualib_cpath);
setlibpath(L, "path", "./lualib/?.lua");
setlibpath(L, "cpath", "./luaclib/?.so");
memcpy(buf, config->selfpath, dir_len);
memcpy(buf + dir_len, "lualib/?.lua", sizeof("lualib/?.lua"));
setlibpath(L, "path", buf);
memcpy(buf + dir_len, "luaclib/?.so", sizeof("luaclib/?.so"));
setlibpath(L, "cpath", buf);
//exec core.start()
lua_pushcfunction(L, ltraceback);
fetch_core_start(L);
err = luaL_loadfile(L, config->bootstrap);
Expand Down

0 comments on commit 644ccf2

Please sign in to comment.