remove unused headers
This commit is contained in:
parent
615d2eacb0
commit
7d696cfbd9
@ -76,9 +76,9 @@ target_sources(nf7
|
||||
common/generic_memento.hh
|
||||
common/generic_type_info.hh
|
||||
common/generic_watcher.hh
|
||||
common/gui_dnd.hh
|
||||
common/gui_config.hh
|
||||
common/gui_context.hh
|
||||
common/gui_dnd.hh
|
||||
common/gui_file.hh
|
||||
common/gui_file.cc
|
||||
common/gui_node.hh
|
||||
@ -107,7 +107,6 @@ target_sources(nf7
|
||||
common/nfile_watcher.hh
|
||||
common/node.hh
|
||||
common/node_link_store.hh
|
||||
common/node_root_lambda.hh
|
||||
common/node_root_select_lambda.hh
|
||||
common/ptr_selector.hh
|
||||
common/queue.hh
|
||||
|
@ -138,8 +138,7 @@ static void PushMeta(lua_State* L) noexcept {
|
||||
th->ExecResume(L, 0);
|
||||
}
|
||||
});
|
||||
th->ExpectYield(L);
|
||||
return lua_yield(L, 0);
|
||||
return th->Yield(L, 0);
|
||||
});
|
||||
lua_setfield(L, -2, "resolve");
|
||||
|
||||
@ -174,8 +173,7 @@ static void PushMeta(lua_State* L) noexcept {
|
||||
th->ExecResume(L, nullptr, e.msg());
|
||||
}
|
||||
});
|
||||
th->ExpectYield(L);
|
||||
return lua_yield(L, 0);
|
||||
return th->Yield(L, 0);
|
||||
});
|
||||
lua_setfield(L, -2, "query");
|
||||
|
||||
@ -188,8 +186,7 @@ static void PushMeta(lua_State* L) noexcept {
|
||||
std::chrono::milliseconds(static_cast<uint64_t>(sec*1000));
|
||||
th->ljq()->Push(th->ctx(), [th, L](auto) { th->ExecResume(L); }, time);
|
||||
|
||||
th->ExpectYield(L);
|
||||
return lua_yield(L, 0);
|
||||
return th->Yield(L, 0);
|
||||
});
|
||||
lua_setfield(L, -2, "sleep");
|
||||
|
||||
@ -240,8 +237,7 @@ static void PushMeta(lua_State* L) noexcept {
|
||||
}).template Catch<nf7::Exception>(nullptr, [L, th](nf7::Exception&) {
|
||||
th->ExecResume(L);
|
||||
});
|
||||
th->ExpectYield(L);
|
||||
return lua_yield(L, 0);
|
||||
return th->Yield(L, 0);
|
||||
}
|
||||
});
|
||||
lua_setfield(L, -2, "recv");
|
||||
|
@ -37,12 +37,12 @@ class Thread final : public std::enable_shared_from_this<Thread> {
|
||||
using nf7::Exception::Exception;
|
||||
};
|
||||
|
||||
// Creates a handler to finalize a promise.
|
||||
// Creates a handler that finalizes a promise.
|
||||
template <typename T>
|
||||
static inline Handler CreatePromiseHandler(
|
||||
nf7::Future<T>::Promise& pro, std::function<T(lua_State*)>&&) noexcept;
|
||||
|
||||
// Creates a handler to emit yielded value to Node::Lambda.
|
||||
// Creates a handler that emits yielded value to Node::Lambda.
|
||||
static Handler CreateNodeLambdaHandler(
|
||||
const std::shared_ptr<nf7::Node::Lambda>& caller,
|
||||
const std::shared_ptr<nf7::Node::Lambda>& callee) noexcept;
|
||||
@ -78,9 +78,10 @@ class Thread final : public std::enable_shared_from_this<Thread> {
|
||||
void Resume(lua_State* L, int narg) noexcept;
|
||||
|
||||
// must be called on luajit thread
|
||||
// handler_ won't be called on next yielding
|
||||
void ExpectYield(lua_State*) noexcept {
|
||||
// handler_ won't be called on this yielding
|
||||
int Yield(lua_State* L, int narg) {
|
||||
skip_handle_ = true;
|
||||
return lua_yield(L, narg);
|
||||
}
|
||||
|
||||
// must be called on luajit thread
|
||||
|
@ -1,197 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/luajit_thread.hh"
|
||||
|
||||
#include <algorithm>
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "common/luajit.hh"
|
||||
#include "common/node.hh"
|
||||
|
||||
|
||||
namespace nf7::luajit {
|
||||
|
||||
class Thread::Lambda final : public Thread::RegistryItem,
|
||||
public std::enable_shared_from_this<Thread::Lambda> {
|
||||
public:
|
||||
static constexpr const char* kTypeName = "nf7::luajit::Thread::Lambda";
|
||||
|
||||
static void CreateAndPush(
|
||||
lua_State* L, const std::shared_ptr<Thread>& th, nf7::File& f) {
|
||||
auto la = std::make_shared<Thread::Lambda>(th, f.interfaceOrThrow<nf7::Node>());
|
||||
th->ljq()->Push(th->ctx(), [L, th, la](auto) {
|
||||
th->Register(L, la);
|
||||
la->Push(L);
|
||||
th->Resume(L, 1);
|
||||
});
|
||||
}
|
||||
|
||||
static std::shared_ptr<Thread::Lambda> GetPtr(lua_State* L, int idx) {
|
||||
auto self = luajit::CheckWeakPtr<Thread::Lambda>(L, idx, kTypeName);
|
||||
self->GetThread(L)->EnsureActive(L);
|
||||
return self;
|
||||
}
|
||||
|
||||
// must be created on main thread
|
||||
explicit Lambda(const std::shared_ptr<Thread>& th, nf7::Node& n) noexcept;
|
||||
|
||||
void Push(lua_State* L) noexcept {
|
||||
luajit::PushWeakPtr<Thread::Lambda>(L, shared_from_this());
|
||||
PushMeta(L);
|
||||
lua_setmetatable(L, -2);
|
||||
}
|
||||
|
||||
private:
|
||||
std::weak_ptr<Thread> th_;
|
||||
|
||||
class Receiver;
|
||||
std::shared_ptr<Receiver> recv_;
|
||||
std::shared_ptr<Node::Lambda> la_;
|
||||
|
||||
|
||||
std::shared_ptr<Thread> GetThread(lua_State* L) {
|
||||
if (auto th = th_.lock()) {
|
||||
return th;
|
||||
} else {
|
||||
luaL_error(L, "thread expired");
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void PushMeta(lua_State* L) noexcept;
|
||||
};
|
||||
|
||||
|
||||
// Receives an output from targetted lambda and Resumes the Thread.
|
||||
class Thread::Lambda::Receiver final : public Node::Lambda,
|
||||
public std::enable_shared_from_this<Thread::Lambda::Receiver> {
|
||||
public:
|
||||
static constexpr size_t kMaxQueue = 1024;
|
||||
|
||||
Receiver() = delete;
|
||||
Receiver(nf7::Env& env, nf7::File::Id id) noexcept :
|
||||
Node::Lambda(env, id, nullptr) {
|
||||
}
|
||||
|
||||
void Handle(std::string_view name, const nf7::Value& v,
|
||||
const std::shared_ptr<Node::Lambda>&) noexcept override {
|
||||
values_.emplace_back(name, v);
|
||||
if (values_.size() > kMaxQueue) {
|
||||
values_.pop_front();
|
||||
}
|
||||
std::unique_lock<std::mutex> k(mtx_);
|
||||
ResumeIf();
|
||||
}
|
||||
|
||||
// must be called on luajit thread
|
||||
// Returns true and pushes results to Lua stack when a value is already queued.
|
||||
bool Select(lua_State* L,const std::shared_ptr<Thread>& th, std::vector<std::string>&& names) noexcept {
|
||||
std::unique_lock<std::mutex> k(mtx_);
|
||||
L_ = L;
|
||||
th_ = th;
|
||||
waiting_ = std::move(names);
|
||||
return ResumeIf(false);
|
||||
}
|
||||
|
||||
private:
|
||||
std::deque<std::pair<std::string, Value>> values_;
|
||||
|
||||
std::mutex mtx_;
|
||||
lua_State* L_;
|
||||
std::shared_ptr<Thread> th_;
|
||||
std::vector<std::string> waiting_;
|
||||
|
||||
|
||||
// don't forget to lock mtx_
|
||||
bool ResumeIf(bool yielded = true) noexcept;
|
||||
};
|
||||
|
||||
|
||||
Thread::Lambda::Lambda(const std::shared_ptr<Thread>& th, nf7::Node& n) noexcept :
|
||||
th_(th),
|
||||
recv_(new Receiver {th->env(), th->ctx()->initiator()}),
|
||||
la_(n.CreateLambda(recv_)) {
|
||||
}
|
||||
void Thread::Lambda::PushMeta(lua_State* L) noexcept {
|
||||
if (luaL_newmetatable(L, kTypeName)) {
|
||||
lua_createtable(L, 0, 0);
|
||||
|
||||
// Lambda:send(name or idx, value)
|
||||
lua_pushcfunction(L, [](auto L) {
|
||||
auto self = GetPtr(L, 1);
|
||||
|
||||
auto name = lua_tostring(L, 2);;
|
||||
auto val = luajit::CheckValue(L, 3);
|
||||
|
||||
auto th = self->GetThread(L);
|
||||
th->env().ExecSub(th->ctx(), [self, th, L, name = std::move(name), val = std::move(val)]() mutable {
|
||||
self->la_->Handle(name, std::move(val), self->recv_);
|
||||
th->ExecResume(L);
|
||||
});
|
||||
|
||||
th->ExpectYield(L);
|
||||
return lua_yield(L, 0);
|
||||
});
|
||||
lua_setfield(L, -2, "send");
|
||||
|
||||
// Lambda:recv(handler)
|
||||
lua_pushcfunction(L, [](auto L) {
|
||||
auto self = GetPtr(L, 1);
|
||||
|
||||
std::vector<std::string> names = {};
|
||||
if (lua_istable(L, 2)) {
|
||||
names.resize(lua_objlen(L, 2));
|
||||
for (size_t i = 0; i < names.size(); ++i) {
|
||||
lua_rawgeti(L, 2, static_cast<int>(i+1));
|
||||
names[i] = lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
} else {
|
||||
names.push_back(lua_tostring(L, 2));
|
||||
}
|
||||
|
||||
auto th = self->GetThread(L);
|
||||
if (self->recv_->Select(L, th, std::move(names))) {
|
||||
return 2;
|
||||
} else {
|
||||
th->ExpectYield(L);
|
||||
return lua_yield(L, 0);
|
||||
}
|
||||
});
|
||||
lua_setfield(L, -2, "recv");
|
||||
|
||||
lua_setfield(L, -2, "__index");
|
||||
|
||||
PushWeakPtrDeleter<Thread::Lambda>(L);
|
||||
lua_setfield(L, -2, "__gc");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Thread::Lambda::Receiver::ResumeIf(bool yielded) noexcept {
|
||||
if (!th_) return false;
|
||||
|
||||
for (auto p = values_.begin(); p < values_.end(); ++p) {
|
||||
auto itr = std::find(waiting_.begin(), waiting_.end(), p->first);
|
||||
if (itr == waiting_.end()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (yielded) {
|
||||
th_->ExecResume(L_, *itr, p->second);
|
||||
} else {
|
||||
luajit::PushAll(L_, *itr, p->second);
|
||||
}
|
||||
values_.erase(p);
|
||||
waiting_ = {};
|
||||
th_ = nullptr;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace nf7::luajit
|
@ -1,116 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "nf7.hh"
|
||||
|
||||
#include "common/future.hh"
|
||||
#include "common/value.hh"
|
||||
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
class NodeRootLambda final : public nf7::Node::Lambda,
|
||||
public std::enable_shared_from_this<NodeRootLambda> {
|
||||
public:
|
||||
struct Builder;
|
||||
|
||||
NodeRootLambda(const NodeRootLambda&) = delete;
|
||||
NodeRootLambda(NodeRootLambda&&) = delete;
|
||||
NodeRootLambda& operator=(const NodeRootLambda&) = delete;
|
||||
NodeRootLambda& operator=(NodeRootLambda&&) = delete;
|
||||
~NodeRootLambda() noexcept {
|
||||
target_ = nullptr;
|
||||
for (auto& pro : pro_) {
|
||||
pro.second.Throw(std::make_exception_ptr(
|
||||
nf7::Exception {"output was never satisified"}));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<nf7::Node::Lambda> target_;
|
||||
|
||||
std::unordered_map<std::string, nf7::Future<nf7::Value>::Promise> pro_;
|
||||
std::unordered_map<std::string, std::function<void(const nf7::Value&)>> handler_;
|
||||
|
||||
|
||||
using nf7::Node::Lambda::Lambda;
|
||||
void Handle(std::string_view name, const nf7::Value& v,
|
||||
const std::shared_ptr<nf7::Node::Lambda>&) noexcept override {
|
||||
const auto sname = std::string {name};
|
||||
auto pitr = pro_.find(sname);
|
||||
if (pitr != pro_.end()) {
|
||||
pitr->second.Return(nf7::Value {v});
|
||||
pro_.erase(pitr);
|
||||
}
|
||||
auto hitr = handler_.find(sname);
|
||||
if (hitr != handler_.end()) {
|
||||
hitr->second(v);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct NodeRootLambda::Builder final {
|
||||
public:
|
||||
Builder() = delete;
|
||||
Builder(nf7::File& f, nf7::Node& n,
|
||||
const std::shared_ptr<nf7::Context>& ctx = nullptr) noexcept :
|
||||
prod_(new NodeRootLambda {f, ctx}), target_(n.CreateLambda(prod_)), node_(&n) {
|
||||
prod_->target_ = target_;
|
||||
}
|
||||
|
||||
void CheckOutput(std::string_view name) const {
|
||||
auto out = node_->GetOutputs();
|
||||
if (out.end() == std::find(out.begin(), out.end(), name)) {
|
||||
throw nf7::Exception {"required output is missing: "+std::string {name}};
|
||||
}
|
||||
}
|
||||
void CheckInput(std::string_view name) const {
|
||||
auto in = node_->GetInputs();
|
||||
if (in.end() == std::find(in.begin(), in.end(), name)) {
|
||||
throw nf7::Exception {"required input is missing: "+std::string {name}};
|
||||
}
|
||||
}
|
||||
|
||||
nf7::Future<nf7::Value> Receive(const std::string& name) {
|
||||
assert(!built_);
|
||||
CheckOutput(name);
|
||||
auto [itr, added] =
|
||||
prod_->pro_.try_emplace(name, nf7::Future<nf7::Value>::Promise {});
|
||||
assert(added);
|
||||
return itr->second.future();
|
||||
}
|
||||
void Listen(const std::string& name, std::function<void(const nf7::Value&)>&& f) {
|
||||
assert(!built_);
|
||||
CheckOutput(name);
|
||||
prod_->handler_[name] = std::move(f);
|
||||
}
|
||||
|
||||
std::shared_ptr<NodeRootLambda> Build() noexcept {
|
||||
assert(!built_);
|
||||
built_ = true;
|
||||
return prod_;
|
||||
}
|
||||
|
||||
void Send(std::string_view name, const nf7::Value& v) {
|
||||
assert(built_);
|
||||
CheckInput(name);
|
||||
target_->Handle(name, v, prod_);
|
||||
}
|
||||
|
||||
private:
|
||||
bool built_ = false;
|
||||
|
||||
std::shared_ptr<NodeRootLambda> prod_;
|
||||
|
||||
std::shared_ptr<nf7::Node::Lambda> target_;
|
||||
|
||||
nf7::Node* const node_;
|
||||
};
|
||||
|
||||
} // namespace nf7
|
Loading…
x
Reference in New Issue
Block a user