0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-02 18:18:56 +02:00

ircd::js: Fixes to async state related.

This commit is contained in:
Jason Volk 2016-11-12 18:37:54 -08:00
parent 411bed10b1
commit 254f5b921a
3 changed files with 29 additions and 12 deletions

View file

@ -44,6 +44,9 @@ struct contract
// The task is automatically found with task::get() in the overload without a task argument.
contract(struct task &, object::handle future);
contract(object::handle future);
contract(contract &&) = default;
contract(const contract &) = default;
~contract() noexcept;
};
} // namespace js

View file

@ -31,7 +31,7 @@ struct task
uint64_t pid; // unique process ID
uint64_t yid; // ID of current yield attempting unification
std::map<uint64_t, heap_object> complete; // futures waiting for yield unification
std::set<uint64_t> pending; // pending futures awaiting results
std::map<uint64_t, heap_object> pending; // pending futures awaiting results
std::shared_ptr<task> work; // references self when there is unfinished work
struct global global; // global / this / root scope object
heap_script main; // main generator wrapper script
@ -43,7 +43,7 @@ struct task
bool tasks_remove();
public:
bool pending_add(const uint64_t &id);
bool pending_add(const uint64_t &id, heap_object);
bool pending_del(const uint64_t &id);
task(const std::u16string &source);
@ -52,6 +52,7 @@ struct task
task(const task &) = delete;
~task() noexcept;
static bool enter(task &, const std::function<void (task &)> &closure);
static bool enter(const std::weak_ptr<task> &, const std::function<void (task &)> &closure);
static task &get(const object &global);
static task &get();

View file

@ -166,6 +166,11 @@ ircd::js::contract::contract(struct task &task,
{
}
ircd::js::contract::~contract()
noexcept
{
}
void
ircd::js::contract::operator()(const closure &func)
noexcept try
@ -286,14 +291,7 @@ ircd::js::task::enter(const std::weak_ptr<task> &ptr,
try
{
const life_guard<struct task> task(ptr);
const std::lock_guard<context> lock{*cx};
const compartment compartment(task->global);
run([&closure, &task]
{
closure(*task);
});
return true;
return enter(*task, closure);
}
catch(const std::bad_weak_ptr &e)
{
@ -303,6 +301,20 @@ catch(const std::bad_weak_ptr &e)
return false;
}
bool
ircd::js::task::enter(task &t,
const std::function<void (task &)> &closure)
{
const std::lock_guard<context> lock{*cx};
const compartment compartment(t.global);
run([&closure, &t]
{
closure(t);
});
return true;
}
bool
ircd::js::task::pending_del(const uint64_t &id)
{
@ -319,9 +331,10 @@ ircd::js::task::pending_del(const uint64_t &id)
}
bool
ircd::js::task::pending_add(const uint64_t &id)
ircd::js::task::pending_add(const uint64_t &id,
heap_object obj)
{
const auto iit(pending.emplace(id));
const auto iit(pending.emplace(id, std::move(obj)));
if(!iit.second)
return false;