Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
etorth committed Aug 12, 2023
1 parent fe302a7 commit e85be35
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 43 deletions.
41 changes: 19 additions & 22 deletions server/src/serverluacoroutinerunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,34 @@

extern MonoServer *g_monoServer;

LuaCoopResumer::LuaCoopResumer(ServerLuaCoroutineRunner *luaRunner, sol::function callback, const LuaCoopCallDoneFlag &doneFlag)
: m_luaRunner(luaRunner)
, m_currRunner(luaRunner->m_currRunner)
, m_callback(std::move(callback))
, m_doneFlag(doneFlag)
{}

LuaCoopResumer::LuaCoopResumer(const LuaCoopResumer & resumer)
: LuaCoopResumer(resumer.m_luaRunner, resumer.m_callback, resumer.m_doneFlag)
{}

LuaCoopResumer::LuaCoopResumer(LuaCoopResumer && resumer)
: LuaCoopResumer(resumer.m_luaRunner, std::move(resumer.m_callback), resumer.m_doneFlag /* always copy doneFlag */)
{}

void LuaCoopResumer::pushOnClose(std::function<void()> fnOnClose) const
{
m_luaRunner->pushOnClose(m_threadKey, m_threadSeqID, std::move(fnOnClose));
static_cast<ServerLuaCoroutineRunner::LuaThreadHandle *>(m_currRunner)->onClose.push(std::move(fnOnClose));
}

void LuaCoopResumer::popOnClose() const
{
m_luaRunner->popOnClose(m_threadKey, m_threadSeqID);
static_cast<ServerLuaCoroutineRunner::LuaThreadHandle *>(m_currRunner)->onClose.pop();
}

void LuaCoopResumer::resumeRunner(ServerLuaCoroutineRunner *luaRunner, uint64_t threadKey, uint64_t threadSeqID)
void LuaCoopResumer::resumeRunner(ServerLuaCoroutineRunner *luaRunner, void *currRunner)
{
luaRunner->resume(threadKey, threadSeqID);
luaRunner->resumeRunner(m_currRunner, static_cast<ServerLuaCoroutineRunner::LuaThreadHandle *>(currRunner));
}

ServerLuaCoroutineRunner::ServerLuaCoroutineRunner(ActorPod *podPtr)
Expand Down Expand Up @@ -460,24 +475,6 @@ ServerLuaCoroutineRunner::LuaThreadHandle *ServerLuaCoroutineRunner::hasKey(uint
return nullptr;
}

void ServerLuaCoroutineRunner::pushOnClose(uint64_t key, uint64_t seqID, std::function<void()> onClose)
{
fflassert(onClose);
auto p = hasKey(key, seqID);

fflassert(p, key, seqID);
p->onClose.push(std::move(onClose));
}

void ServerLuaCoroutineRunner::popOnClose(uint64_t key, uint64_t seqID)
{
auto p = hasKey(key, seqID);

fflassert(p, key, seqID);
fflassert(!p->onClose.empty());
p->onClose.pop();
}

void ServerLuaCoroutineRunner::close(uint64_t key, uint64_t seqID)
{
if(auto eqr = m_runnerList.equal_range(key); eqr.first != eqr.second){
Expand Down
29 changes: 11 additions & 18 deletions server/src/serverluacoroutinerunner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,21 @@ class LuaCoopResumer final
private:
ServerLuaCoroutineRunner * const m_luaRunner;

private:
void * const m_currRunner;

private:
sol::function m_callback;

private:
const LuaCoopCallDoneFlag m_doneFlag;

public:
LuaCoopResumer(ServerLuaCoroutineRunner *luaRunner, sol::function callback, const LuaCoopCallDoneFlag &doneFlag)
: m_luaRunner(luaRunner)
, m_callback(std::move(callback))
, m_doneFlag(doneFlag)
{}
LuaCoopResumer(ServerLuaCoroutineRunner *, sol::function, const LuaCoopCallDoneFlag &);

public:
LuaCoopResumer(const LuaCoopResumer & resumer)
: LuaCoopResumer(resumer.m_luaRunner, resumer.m_callback, resumer.m_doneFlag)
{}

LuaCoopResumer(LuaCoopResumer && resumer)
: LuaCoopResumer(resumer.m_luaRunner, std::move(resumer.m_callback), resumer.m_doneFlag /* always copy doneFlag */)
{}
LuaCoopResumer(const LuaCoopResumer & );
LuaCoopResumer( LuaCoopResumer &&);

public:
~LuaCoopResumer() = default;
Expand All @@ -82,7 +76,7 @@ class LuaCoopResumer final
{
m_callback(std::forward<Args>(args)...);
if(m_doneFlag){
resumeRunner(m_luaRunner);
resumeRunner(m_luaRunner, m_currRunner);
}
}

Expand All @@ -91,7 +85,7 @@ class LuaCoopResumer final
void popOnClose() const;

private:
static void resumeRunner(ServerLuaCoroutineRunner *, uint64_t, uint64_t); // resolve dependency
static void resumeRunner(ServerLuaCoroutineRunner *, void *); // resolve dependency
};

class LuaCoopState final
Expand Down Expand Up @@ -131,6 +125,9 @@ class LuaCoopVargs final
class ActorPod;
class ServerLuaCoroutineRunner: public ServerLuaModule
{
protected:
friend class LuaCoopResumer;

protected:
struct LuaThreadHandle
{
Expand Down Expand Up @@ -294,10 +291,6 @@ class ServerLuaCoroutineRunner: public ServerLuaModule
public:
LuaThreadHandle *hasKey(uint64_t, uint64_t = 0);

public:
void pushOnClose(uint64_t, uint64_t, std::function<void()>);
void popOnClose(uint64_t, uint64_t);

private:
void resumeRunner(LuaThreadHandle *, std::optional<std::pair<std::string, luaf::luaVar>> = {});

Expand Down
4 changes: 1 addition & 3 deletions server/src/serverluacoroutinerunner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,8 @@ function _RSVD_NAME_callFuncCoop(funcName, ...)
local args = table.pack(...)

args[args.n + 1] = onDone
args[args.n + 2] = getTLSTable().threadKey
args[args.n + 3] = getTLSTable().threadSeqID

_G[string.format('_RSVD_NAME_%s%s', funcName, SYS_COOP)](table.unpack(args, 1, args.n + 3))
_G[string.format('_RSVD_NAME_%s%s', funcName, SYS_COOP)](table.unpack(args, 1, args.n + 1))

-- onDone can get ran immedately in _RSVD_NAME_funcCoop
-- in this situation we shall not yield
Expand Down

0 comments on commit e85be35

Please sign in to comment.