Skip to content

Commit

Permalink
rewrite errlog
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Apr 29, 2024
1 parent 3ca2640 commit 9eeb418
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
33 changes: 15 additions & 18 deletions binding/lua_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ namespace bee::lua_thread {

class channelmgr {
public:
channelmgr() {
channels.emplace(std::make_pair("errlog", std::make_shared<channel>()));
}
bool create(zstring_view name) {
std::unique_lock<spinlock> lk(mutex);
std::string namestr { name.data(), name.size() };
Expand All @@ -92,14 +89,7 @@ namespace bee::lua_thread {
}
void clear() {
std::unique_lock<spinlock> lk(mutex);
auto it = channels.find("errlog");
if (it != channels.end()) {
auto errlog = it->second;
channels.clear();
channels.emplace(std::make_pair("errlog", std::move(errlog)));
} else {
channels.clear();
}
channels.clear();
}
boxchannel query(zstring_view name) {
std::unique_lock<spinlock> lk(mutex);
Expand All @@ -117,6 +107,7 @@ namespace bee::lua_thread {
};

static channelmgr g_channel;
static channel g_errlog;
static std::atomic<int> g_thread_id = -1;
static int THREADID;

Expand Down Expand Up @@ -246,13 +237,8 @@ namespace bee::lua_thread {
lua_pushcfunction(L, thread_luamain);
lua_pushlightuserdata(L, ud);
if (lua_pcall(L, 1, 0, 1) != LUA_OK) {
boxchannel errlog = g_channel.query("errlog");
if (errlog) {
void* errmsg = seri_pack(L, lua_gettop(L) - 1, NULL);
errlog->push(errmsg);
} else {
std::println(stdout, "thread error : {}", lua_tostring(L, -1));
}
void* errmsg = seri_pack(L, lua_gettop(L) - 1, NULL);
g_errlog.push(errmsg);
}
lua_close(L);
}
Expand All @@ -273,6 +259,16 @@ namespace bee::lua_thread {
return 1;
}

static int lerrlog(lua_State* L) {
void* data;
if (!g_errlog.pop(data)) {
lua_pushboolean(L, 0);
return 1;
}
lua_pushboolean(L, 1);
return 1 + seri_unpackptr(L, data);
}

static int lreset(lua_State* L) {
lua_rawgetp(L, LUA_REGISTRYINDEX, &THREADID);
int threadid = (int)lua_tointeger(L, -1);
Expand Down Expand Up @@ -311,6 +307,7 @@ namespace bee::lua_thread {
luaL_Reg lib[] = {
{ "sleep", lsleep },
{ "thread", lthread },
{ "errlog", lerrlog },
{ "newchannel", lnewchannel },
{ "channel", lchannel },
{ "reset", lreset },
Expand Down
3 changes: 1 addition & 2 deletions test/test_socket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ local socket = require "bee.socket"
local select = require "bee.select"
local thread = require "bee.thread"
local fs = require "bee.filesystem"
local errlog = thread.channel "errlog"

local function simple_select(fd, mode)
local s <close> = select.create()
Expand All @@ -26,7 +25,7 @@ local function simple_select(fd, mode)
end

local function assertNotThreadError()
local ok, msg = errlog:pop()
local ok, msg = thread.errlog()
if ok then
lt.failure(msg)
end
Expand Down
5 changes: 2 additions & 3 deletions test/test_thread.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ local lt = require "ltest"
local thread = require "bee.thread"
local fs = require "bee.filesystem"
local time = require "bee.time"
local err = thread.channel "errlog"

local function createThread(script, ...)
return thread.thread(script, ...)
end

local function assertNotThreadError()
lt.assertEquals(err:pop(), false)
lt.assertEquals(thread.errlog(), false)
end

local function assertHasThreadError(m)
local ok, msg = err:pop()
local ok, msg = thread.errlog()
lt.assertEquals(ok, true)
lt.assertEquals(not not string.find(msg, m, nil, true), true)
end
Expand Down

0 comments on commit 9eeb418

Please sign in to comment.