remove unused features to simplify the codes
This commit is contained in:
parent
cf2e165c10
commit
f3d9973a66
@ -79,7 +79,6 @@ target_sources(nf7
|
||||
common/native_file.hh
|
||||
common/node.hh
|
||||
common/node_link_store.hh
|
||||
common/proxy_env.hh
|
||||
common/ptr_selector.hh
|
||||
common/queue.hh
|
||||
common/task.hh
|
||||
@ -90,6 +89,7 @@ target_sources(nf7
|
||||
common/yas_imgui.hh
|
||||
common/yas_imnodes.hh
|
||||
common/yas_nf7.hh
|
||||
common/yas_std_atomic.hh
|
||||
common/yas_std_filesystem.hh
|
||||
|
||||
$<$<PLATFORM_ID:Linux>:common/native_file_unix.cc>
|
||||
|
@ -36,9 +36,6 @@ static void PushLock(
|
||||
const std::shared_ptr<nf7::Lock>&) noexcept;
|
||||
|
||||
|
||||
Thread::~Thread() noexcept {
|
||||
if (holder_) *holder_ = nullptr;
|
||||
}
|
||||
lua_State* Thread::Init(lua_State* L) noexcept {
|
||||
assert(state_ == kInitial);
|
||||
|
||||
@ -54,7 +51,6 @@ void Thread::Resume(lua_State* L, int narg) noexcept {
|
||||
std::unique_lock<std::mutex> k(mtx_);
|
||||
|
||||
if (state_ == kAborted) return;
|
||||
assert(holder_);
|
||||
assert(L == th_);
|
||||
assert(state_ == kPaused);
|
||||
(void) L;
|
||||
@ -79,14 +75,12 @@ void Thread::Resume(lua_State* L, int narg) noexcept {
|
||||
switch (ret) {
|
||||
case 0:
|
||||
state_ = kFinished;
|
||||
if (holder_) *holder_ = nullptr;
|
||||
break;
|
||||
case LUA_YIELD:
|
||||
state_ = kPaused;
|
||||
break;
|
||||
default:
|
||||
state_ = kAborted;
|
||||
if (holder_) *holder_ = nullptr;
|
||||
}
|
||||
if (!std::exchange(skip_handle_, false)) {
|
||||
handler_(*this, th_);
|
||||
@ -95,63 +89,6 @@ void Thread::Resume(lua_State* L, int narg) noexcept {
|
||||
void Thread::Abort() noexcept {
|
||||
std::unique_lock<std::mutex> k(mtx_);
|
||||
state_ = kAborted;
|
||||
if (holder_) *holder_ = nullptr;
|
||||
}
|
||||
|
||||
|
||||
void Thread::Holder::Handle(const nf7::File::Event& ev) noexcept {
|
||||
std::unique_lock<std::mutex> k(mtx_);
|
||||
|
||||
switch (ev.type) {
|
||||
case nf7::File::Event::kAdd:
|
||||
assert(isolated_);
|
||||
isolated_ = false;
|
||||
|
||||
if (th_) {
|
||||
th_->file_parent_ = owner_;
|
||||
if (auto& f = th_->file_) {
|
||||
assert(!f->parent());
|
||||
f->MoveUnder(*owner_, "file");
|
||||
}
|
||||
}
|
||||
return;
|
||||
case nf7::File::Event::kRemove:
|
||||
assert(!isolated_);
|
||||
isolated_ = true;
|
||||
|
||||
if (th_) {
|
||||
th_->file_parent_ = nullptr;
|
||||
if (auto& f = th_->file_) {
|
||||
assert(f->parent());
|
||||
f->Isolate();
|
||||
}
|
||||
}
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
void Thread::Holder::Assign(const std::shared_ptr<Thread>& th) noexcept {
|
||||
if (th_ == th) return;
|
||||
if (th_) {
|
||||
th_->holder_ = nullptr;
|
||||
if (!isolated_) {
|
||||
if (auto& f = th_->file_) {
|
||||
assert(f->parent());
|
||||
f->Isolate();
|
||||
}
|
||||
}
|
||||
}
|
||||
th_ = th;
|
||||
if (th_) {
|
||||
th_->holder_ = this;
|
||||
if (!isolated_) {
|
||||
if (auto& f = th_->file_) {
|
||||
assert(!f->parent());
|
||||
f->MoveUnder(*owner_, "file");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -17,17 +17,12 @@
|
||||
#include "common/logger_ref.hh"
|
||||
#include "common/luajit.hh"
|
||||
#include "common/luajit_ref.hh"
|
||||
#include "common/proxy_env.hh"
|
||||
|
||||
|
||||
namespace nf7::luajit {
|
||||
|
||||
// use with Thread::Holder or Thread::HolderSet to handle child files properly
|
||||
class Thread final : public std::enable_shared_from_this<Thread> {
|
||||
public:
|
||||
class Holder;
|
||||
template <size_t kMax> class HolderSet;
|
||||
|
||||
enum State { kInitial, kRunning, kPaused, kFinished, kAborted, };
|
||||
using Handler = std::function<void(Thread&, lua_State*)>;
|
||||
|
||||
@ -45,9 +40,8 @@ class Thread final : public std::enable_shared_from_this<Thread> {
|
||||
Thread(const std::shared_ptr<nf7::Context>& ctx,
|
||||
const std::shared_ptr<nf7::luajit::Queue>& ljq,
|
||||
Handler&& handler) noexcept :
|
||||
env_(ctx->env()), ctx_(ctx), ljq_(ljq), handler_(std::move(handler)) {
|
||||
ctx_(ctx), ljq_(ljq), handler_(std::move(handler)) {
|
||||
}
|
||||
~Thread() noexcept;
|
||||
Thread(const Thread&) = delete;
|
||||
Thread(Thread&&) = delete;
|
||||
Thread& operator=(const Thread&) = delete;
|
||||
@ -88,15 +82,7 @@ class Thread final : public std::enable_shared_from_this<Thread> {
|
||||
ljq_->Push(ctx_, [this, L, self = shared_from_this()](auto) { Resume(L, 0); });
|
||||
}
|
||||
|
||||
// Creates new file from typename and sets it as child.
|
||||
void EmplaceFile(std::string_view name) {
|
||||
file_ = nf7::File::registry(name).Create(env_);
|
||||
if (file_parent_) {
|
||||
file_->MoveUnder(*file_parent_, "file");
|
||||
}
|
||||
}
|
||||
|
||||
nf7::Env& env() noexcept { return env_; }
|
||||
nf7::Env& env() noexcept { return ctx_->env(); }
|
||||
const std::shared_ptr<nf7::Context>& ctx() const noexcept { return ctx_; }
|
||||
const std::shared_ptr<nf7::luajit::Queue>& ljq() const noexcept { return ljq_; }
|
||||
const std::shared_ptr<nf7::LoggerRef>& logger() const noexcept { return logger_; }
|
||||
@ -105,7 +91,6 @@ class Thread final : public std::enable_shared_from_this<Thread> {
|
||||
private:
|
||||
// initialized by constructor
|
||||
std::mutex mtx_;
|
||||
nf7::ProxyEnv env_;
|
||||
|
||||
std::shared_ptr<nf7::Context> ctx_;
|
||||
std::shared_ptr<nf7::luajit::Queue> ljq_;
|
||||
@ -124,117 +109,11 @@ class Thread final : public std::enable_shared_from_this<Thread> {
|
||||
|
||||
|
||||
// mutable params
|
||||
Holder* holder_ = nullptr;
|
||||
|
||||
File* file_parent_ = nullptr;
|
||||
std::unique_ptr<nf7::File> file_;
|
||||
|
||||
std::vector<std::shared_ptr<nf7::Lock>> locks_;
|
||||
|
||||
bool skip_handle_ = false;
|
||||
};
|
||||
|
||||
// Holder handles an event to maintain Thread.
|
||||
// The owner file must call Handle() when it received any events.
|
||||
class Thread::Holder final {
|
||||
public:
|
||||
Holder() = delete;
|
||||
Holder(File& owner) noexcept : owner_(&owner) {
|
||||
}
|
||||
~Holder() noexcept {
|
||||
assert(isolated_);
|
||||
*this = nullptr;
|
||||
}
|
||||
Holder(const Holder&) = delete;
|
||||
Holder(Holder&&) = delete;
|
||||
Holder& operator=(const Holder&) = delete;
|
||||
Holder& operator=(Holder&&) = delete;
|
||||
|
||||
// thread-safe
|
||||
Holder& operator=(const std::shared_ptr<Thread>& th) noexcept {
|
||||
std::unique_lock<std::mutex> k(mtx_);
|
||||
Assign(th);
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::shared_ptr<Thread> EmplaceIf(
|
||||
const std::shared_ptr<nf7::Context>& ctx,
|
||||
const std::shared_ptr<nf7::luajit::Queue>& ljq,
|
||||
Handler&& handler) noexcept {
|
||||
std::unique_lock<std::mutex> k(mtx_);
|
||||
if (th_) return nullptr;
|
||||
Assign(std::make_shared<Thread>(ctx, ljq, std::move(handler)));
|
||||
return th_;
|
||||
}
|
||||
std::shared_ptr<Thread> Emplace(
|
||||
const std::shared_ptr<nf7::Context>& ctx,
|
||||
const std::shared_ptr<nf7::luajit::Queue>& ljq,
|
||||
Handler&& handler) noexcept {
|
||||
std::unique_lock<std::mutex> k(mtx_);
|
||||
Assign(std::make_shared<Thread>(ctx, ljq, std::move(handler)));
|
||||
return th_;
|
||||
}
|
||||
|
||||
void Handle(const nf7::File::Event& ev) noexcept;
|
||||
|
||||
bool holding() const noexcept {
|
||||
std::unique_lock<std::mutex> k(mtx_);
|
||||
return !!th_;
|
||||
}
|
||||
nf7::File* child() const noexcept {
|
||||
std::unique_lock<std::mutex> k(mtx_);
|
||||
return th_? th_->file_.get(): nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::mutex mtx_;
|
||||
|
||||
nf7::File* const owner_;
|
||||
|
||||
bool isolated_ = true;
|
||||
std::shared_ptr<Thread> th_;
|
||||
|
||||
|
||||
void Assign(const std::shared_ptr<Thread>&) noexcept;
|
||||
};
|
||||
|
||||
// Holder handles an event to maintain multiple Threads.
|
||||
// The owner file must call Handle() when it received any events.
|
||||
template <size_t kMax>
|
||||
class Thread::HolderSet final {
|
||||
public:
|
||||
HolderSet() = delete;
|
||||
HolderSet(nf7::File& owner) noexcept {
|
||||
for (auto& h : items_) {
|
||||
h.emplace(owner);
|
||||
}
|
||||
}
|
||||
HolderSet(const HolderSet&) = delete;
|
||||
HolderSet(HolderSet&&) = delete;
|
||||
HolderSet& operator=(const HolderSet&) = delete;
|
||||
HolderSet& operator=(HolderSet&&) = delete;
|
||||
|
||||
std::shared_ptr<Thread> Add(
|
||||
const std::shared_ptr<nf7::Context>& ctx,
|
||||
const std::shared_ptr<nf7::luajit::Queue>& ljq,
|
||||
Handler&& handler) {
|
||||
for (auto& h : items_) {
|
||||
if (auto th = h->EmplaceIf(ctx, ljq, std::move(handler))) {
|
||||
return th;
|
||||
}
|
||||
}
|
||||
throw nf7::Exception("luajit thread overflow");
|
||||
}
|
||||
void Handle(const nf7::File::Event& ev) noexcept {
|
||||
for (auto& h : items_) {
|
||||
h->Handle(ev);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<std::optional<Holder>, kMax> items_;
|
||||
};
|
||||
|
||||
|
||||
template <typename T>
|
||||
Thread::Handler Thread::CreatePromiseHandler(
|
||||
|
@ -1,65 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "nf7.hh"
|
||||
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
class ProxyEnv : public nf7::Env {
|
||||
public:
|
||||
ProxyEnv(Env& parent, const std::filesystem::path& npath) noexcept :
|
||||
Env(npath), parent_(&parent) {
|
||||
}
|
||||
ProxyEnv(Env& parent) noexcept : ProxyEnv(parent, parent.npath()) {
|
||||
}
|
||||
|
||||
File* GetFile(File::Id id) const noexcept override {
|
||||
return parent_->GetFile(id);
|
||||
}
|
||||
|
||||
void ExecMain(const std::shared_ptr<Context>& ctx, Task&& task) noexcept override {
|
||||
parent_->ExecMain(ctx, std::move(task));
|
||||
}
|
||||
void ExecSub(const std::shared_ptr<Context>& ctx, Task&& task) noexcept override {
|
||||
parent_->ExecSub(ctx, std::move(task));
|
||||
}
|
||||
void ExecAsync(const std::shared_ptr<Context>& ctx, Task&& task) noexcept override {
|
||||
parent_->ExecAsync(ctx, std::move(task));
|
||||
}
|
||||
|
||||
void Handle(const File::Event& ev) noexcept override {
|
||||
parent_->Handle(ev);
|
||||
}
|
||||
void Save() noexcept override {
|
||||
parent_->Save();
|
||||
}
|
||||
|
||||
protected:
|
||||
File::Id AddFile(File& f) noexcept override {
|
||||
return parent_->AddFile(f);
|
||||
}
|
||||
void RemoveFile(File::Id id) noexcept override {
|
||||
parent_->RemoveFile(id);
|
||||
}
|
||||
|
||||
void AddContext(Context& ctx) noexcept override {
|
||||
parent_->AddContext(ctx);
|
||||
}
|
||||
void RemoveContext(Context& ctx) noexcept override {
|
||||
parent_->RemoveContext(ctx);
|
||||
}
|
||||
|
||||
void AddWatcher(File::Id id, Watcher& w) noexcept override {
|
||||
parent_->AddWatcher(id, w);
|
||||
}
|
||||
void RemoveWatcher(Watcher& w) noexcept override {
|
||||
parent_->RemoveWatcher(w);
|
||||
}
|
||||
|
||||
private:
|
||||
Env* const parent_;
|
||||
};
|
||||
|
||||
} // namespace nf7
|
@ -50,7 +50,7 @@ class Node final : public nf7::File, public nf7::DirItem, public nf7::Node {
|
||||
std::vector<std::string>&& out = {}) noexcept :
|
||||
File(kType, env),
|
||||
DirItem(DirItem::kMenu | DirItem::kTooltip | DirItem::kDragDropTarget),
|
||||
log_(std::make_shared<nf7::LoggerRef>()), th_(*this),
|
||||
log_(std::make_shared<nf7::LoggerRef>()),
|
||||
obj_(*this, std::move(path)), desc_(desc) {
|
||||
input_ = std::move(in);
|
||||
output_ = std::move(out);
|
||||
@ -101,8 +101,6 @@ class Node final : public nf7::File, public nf7::DirItem, public nf7::Node {
|
||||
std::shared_ptr<nf7::luajit::Ref> handler_;
|
||||
nf7::Task<std::shared_ptr<nf7::luajit::Ref>>::Holder fetch_;
|
||||
|
||||
nf7::luajit::Thread::HolderSet<32> th_;
|
||||
|
||||
const char* popup_ = nullptr;
|
||||
|
||||
// persistent params
|
||||
@ -192,7 +190,9 @@ class Node::Lambda final : public nf7::Context, public nf7::Lambda,
|
||||
}
|
||||
void Abort() noexcept override {
|
||||
for (auto& wth : th_) {
|
||||
if (auto th = wth.lock()) th->Abort();
|
||||
if (auto th = wth.lock()) {
|
||||
th->Abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -222,7 +222,7 @@ class Node::Lambda final : public nf7::Context, public nf7::Lambda,
|
||||
ljq_ = handler->ljq();
|
||||
|
||||
env().GetFileOrThrow(file_id_); // check if the owner is alive
|
||||
auto th = file_->th_.Add(
|
||||
auto th = std::make_shared<nf7::luajit::Thread>(
|
||||
self, ljq_, [self](auto& th, auto L) { self->HandleThread(th, L); });
|
||||
th->Install(log_);
|
||||
th_.emplace_back(th);
|
||||
@ -321,7 +321,6 @@ nf7::Future<std::shared_ptr<nf7::luajit::Ref>> Node::FetchHandler() noexcept {
|
||||
}
|
||||
|
||||
void Node::Handle(const Event& ev) noexcept {
|
||||
th_.Handle(ev);
|
||||
switch (ev.type) {
|
||||
case Event::kAdd:
|
||||
log_->SetUp(*this);
|
||||
|
@ -49,7 +49,7 @@ class Obj final : public nf7::File,
|
||||
Obj(Env& env, Path&& path = {}) noexcept :
|
||||
File(kType, env),
|
||||
DirItem(DirItem::kTooltip | DirItem::kMenu | DirItem::kDragDropTarget),
|
||||
log_(std::make_shared<nf7::LoggerRef>()), th_(*this),
|
||||
log_(std::make_shared<nf7::LoggerRef>()),
|
||||
src_(*this, std::move(path)) {
|
||||
}
|
||||
|
||||
@ -81,7 +81,6 @@ class Obj final : public nf7::File,
|
||||
std::optional<nf7::GenericWatcher> watcher_;
|
||||
std::shared_ptr<nf7::luajit::Ref> cache_;
|
||||
|
||||
nf7::luajit::Thread::Holder th_;
|
||||
nf7::Task<std::shared_ptr<nf7::luajit::Ref>>::Holder exec_;
|
||||
|
||||
const char* popup_ = nullptr;
|
||||
@ -156,7 +155,6 @@ class Obj::ExecTask final : public nf7::Task<std::shared_ptr<nf7::luajit::Ref>>
|
||||
}
|
||||
return luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
});
|
||||
auto th = target_->th_.Emplace(self(), ljq, std::move(handler));
|
||||
|
||||
// setup watcher
|
||||
try {
|
||||
@ -182,6 +180,8 @@ class Obj::ExecTask final : public nf7::Task<std::shared_ptr<nf7::luajit::Ref>>
|
||||
}
|
||||
|
||||
// queue task to trigger the thread
|
||||
auto th = std::make_shared<nf7::luajit::Thread>(self(), ljq, std::move(handler));
|
||||
th->Install(log_);
|
||||
ljq->Push(self(), [&](auto L) {
|
||||
try {
|
||||
auto thL = th->Init(L);
|
||||
@ -238,8 +238,6 @@ nf7::Future<std::shared_ptr<nf7::luajit::Ref>> Obj::Build() noexcept {
|
||||
return exec->fu();
|
||||
}
|
||||
void Obj::Handle(const Event& ev) noexcept {
|
||||
th_.Handle(ev);
|
||||
|
||||
switch (ev.type) {
|
||||
case Event::kAdd:
|
||||
log_->SetUp(*this);
|
||||
|
Loading…
x
Reference in New Issue
Block a user