fix an issue that active LuaJIT thread prevents Nf7 from shutting down

This commit is contained in:
2022-11-15 15:02:07 +09:00
parent 5c74c5cc40
commit 3c67497229
9 changed files with 107 additions and 27 deletions

View File

@@ -117,6 +117,8 @@ class Node final : public nf7::FileBase,
std::filesystem::file_time_type last_build_ = {};
std::shared_ptr<nf7::luajit::NFileImporter> importer_;
nf7::ContextOwner la_owner_;
};
@@ -139,6 +141,10 @@ class Node::Lambda final : public nf7::Node::Lambda,
} catch (nf7::ExpiredException&) {
}
void Abort() noexcept override {
th_owner_.AbortAll();
}
private:
nf7::Life<Node>::Ref f_;
@@ -147,6 +153,8 @@ class Node::Lambda final : public nf7::Node::Lambda,
std::mutex mtx_;
std::optional<nf7::luajit::Ref> ctx_;
nf7::ContextOwner th_owner_;
void StartThread(const nf7::Node::Lambda::Msg& in,
const std::shared_ptr<nf7::luajit::Ref>& func) noexcept {
@@ -154,7 +162,7 @@ class Node::Lambda final : public nf7::Node::Lambda,
auto self = shared_from_this();
auto hndl = nf7::luajit::Thread::CreateNodeLambdaHandler(in.sender, self);
auto th = std::make_shared<nf7::luajit::Thread>(self, ljq, std::move(hndl));
auto th = th_owner_.Create<nf7::luajit::Thread>(self, ljq, std::move(hndl));
th->Install(log_);
th->Install(f_->importer_);
@@ -176,7 +184,7 @@ class Node::Lambda final : public nf7::Node::Lambda,
};
std::shared_ptr<nf7::Node::Lambda> Node::CreateLambda(
const std::shared_ptr<nf7::Node::Lambda>& parent) noexcept {
return std::make_shared<Lambda>(*this, parent);
return la_owner_.Create<Lambda>(*this, parent);
}

View File

@@ -120,6 +120,9 @@ class Singleton::SharedLambda final : public nf7::Node::Lambda,
public:
SharedLambda(Singleton& f) noexcept : nf7::Node::Lambda(f), f_(f.life_) {
}
~SharedLambda() noexcept {
Abort();
}
void SendToTarget(const nf7::Node::Lambda::Msg& in) noexcept
try {
@@ -135,10 +138,14 @@ class Singleton::SharedLambda final : public nf7::Node::Lambda,
} catch (nf7::ExpiredException&) {
} catch (nf7::Exception&) {
f_->log_.Error("failed to call target");
Abort();
}
void Drop() noexcept {
target_ = nullptr;
void Abort() noexcept override {
if (target_) {
target_->Abort();
target_ = nullptr;
}
}
std::string GetDescription() const noexcept override {
@@ -202,7 +209,7 @@ std::shared_ptr<nf7::Node::Lambda> Singleton::CreateLambda(
void Singleton::UpdateMenu() noexcept {
if (ImGui::MenuItem("drop current lambda")) {
shared_la_->Drop();
shared_la_->Abort();
}
}
void Singleton::UpdateTooltip() noexcept {