0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-29 20:28:52 +02:00

ircd::ctx: Support future continuation.

This commit is contained in:
Jason Volk 2018-03-10 14:56:14 -08:00
parent e4d891abac
commit 30700b2ad8
2 changed files with 29 additions and 9 deletions

View file

@ -196,11 +196,11 @@ ircd::ctx::promise<T>::set_value(T&& val)
{
assert(valid());
assert(pending(*st));
st->val = std::move(val);
auto *const st{this->st};
invalidate(*st);
set_ready(*st);
st->val = std::move(val);
st->cond.notify_all();
notify(*st);
}
template<class T>
@ -209,11 +209,11 @@ ircd::ctx::promise<T>::set_value(const T &val)
{
assert(valid());
assert(pending(*st));
st->val = val;
auto *const st{this->st};
invalidate(*st);
set_ready(*st);
st->val = val;
st->cond.notify_all();
notify(*st);
}
inline void
@ -224,7 +224,7 @@ ircd::ctx::promise<void>::set_value()
auto *const st{this->st};
invalidate(*st);
set_ready(*st);
st->cond.notify_all();
notify(*st);
}
template<class T>
@ -233,11 +233,11 @@ ircd::ctx::promise<T>::set_exception(std::exception_ptr eptr)
{
assert(valid());
assert(pending(*st));
st->eptr = std::move(eptr);
auto *const st{this->st};
invalidate(*st);
set_ready(*st);
st->eptr = std::move(eptr);
st->cond.notify_all();
notify(*st);
}
inline void
@ -245,11 +245,11 @@ ircd::ctx::promise<void>::set_exception(std::exception_ptr eptr)
{
assert(valid());
assert(pending(*st));
st->eptr = std::move(eptr);
auto *const st{this->st};
invalidate(*st);
set_ready(*st);
st->eptr = std::move(eptr);
st->cond.notify_all();
notify(*st);
}
template<class T>

View file

@ -24,12 +24,14 @@ namespace ircd::ctx
template<class T> bool pending(const shared_state<T> &);
template<class T> bool ready(const shared_state<T> &);
template<class T> void set_ready(shared_state<T> &);
template<class T> void notify(shared_state<T> &);
}
struct ircd::ctx::shared_state_base
{
mutable dock cond;
std::exception_ptr eptr;
std::function<void (shared_state_base &)> then;
};
template<class T>
@ -53,6 +55,24 @@ struct ircd::ctx::shared_state<void>
promise<void> *p {nullptr};
};
template<class T>
void
ircd::ctx::notify(shared_state<T> &st)
{
st.cond.notify_all();
if(!st.then)
return;
if(!current)
return st.then(st);
ircd::post([&st]
{
st.then(st);
});
}
template<class T>
void
ircd::ctx::set_ready(shared_state<T> &st)