0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-25 06:53:58 +01:00

ircd::ctx::dock: Deduplicate prologue/epilogue in wait() suite.

This commit is contained in:
Jason Volk 2023-02-19 17:39:32 -08:00
parent 17f0923eff
commit 5a35d63b64
2 changed files with 41 additions and 72 deletions

View file

@ -25,6 +25,7 @@ namespace ircd::ctx
/// ///
class ircd::ctx::dock class ircd::ctx::dock
{ {
struct continuation;
using predicate = std::function<bool ()>; using predicate = std::function<bool ()>;
list q; list q;
@ -56,6 +57,15 @@ namespace ircd::ctx
template<> bool dock::wait_for(const microseconds); template<> bool dock::wait_for(const microseconds);
} }
struct [[gnu::visibility("internal")]]
ircd::ctx::dock::continuation
{
dock *const d;
continuation(dock *);
~continuation() noexcept;
};
/// Wake up the next context waiting on the dock /// Wake up the next context waiting on the dock
inline void inline void
ircd::ctx::dock::notify_one() ircd::ctx::dock::notify_one()

View file

@ -2863,18 +2863,7 @@ noexcept
void void
ircd::ctx::dock::wait() ircd::ctx::dock::wait()
{ {
assert(current); const continuation c{this};
const unwind_exceptional renotify{[this]
{
notify_one();
}};
const unwind remove{[this]
{
q.remove(current);
}};
q.push_back(current);
this_ctx::wait(); this_ctx::wait();
} }
@ -2884,18 +2873,7 @@ ircd::ctx::dock::wait(const predicate &pred)
if(pred()) if(pred())
return; return;
assert(current); const continuation c{this}; do
const unwind_exceptional renotify{[this]
{
notify_one();
}};
const unwind remove{[this]
{
q.remove(current);
}};
q.push_back(current); do
{ {
this_ctx::wait(); this_ctx::wait();
} }
@ -2908,18 +2886,7 @@ ircd::ctx::dock::wait_for(const microseconds dur)
{ {
static const microseconds zero {0}; static const microseconds zero {0};
assert(current); const continuation c{this};
const unwind_exceptional renotify{[this]
{
notify_one();
}};
const unwind remove{[this]
{
q.remove(current);
}};
q.push_back(current);
return ircd::ctx::wait<std::nothrow_t>(dur) > zero; return ircd::ctx::wait<std::nothrow_t>(dur) > zero;
} }
@ -2933,18 +2900,7 @@ ircd::ctx::dock::wait_for(const microseconds dur,
if(pred()) if(pred())
return true; return true;
assert(current); const continuation c{this}; do
const unwind_exceptional renotify{[this]
{
notify_one();
}};
const unwind remove{[this]
{
q.remove(current);
}};
q.push_back(current); do
{ {
const bool expired const bool expired
{ {
@ -2963,18 +2919,7 @@ ircd::ctx::dock::wait_for(const microseconds dur,
bool bool
ircd::ctx::dock::wait_until(const system_point tp) ircd::ctx::dock::wait_until(const system_point tp)
{ {
assert(current); const continuation c{this};
const unwind_exceptional renotify{[this]
{
notify_one();
}};
const unwind remove{[this]
{
q.remove(current);
}};
q.push_back(current);
return !ircd::ctx::wait_until<std::nothrow_t>(tp); return !ircd::ctx::wait_until<std::nothrow_t>(tp);
} }
@ -2986,18 +2931,7 @@ ircd::ctx::dock::wait_until(const system_point tp,
if(pred()) if(pred())
return true; return true;
assert(current); const continuation c{this}; do
const unwind_exceptional renotify{[this]
{
notify_one();
}};
const unwind remove{[this]
{
q.remove(current);
}};
q.push_back(current); do
{ {
const bool expired const bool expired
{ {
@ -3027,6 +2961,31 @@ const noexcept
}}); }});
} }
//
// dock::continuation
//
ircd::ctx::dock::continuation::continuation(dock *const d)
:d{d}
{
assert(d);
assert(current);
d->q.push_back(current);
}
ircd::ctx::dock::continuation::~continuation()
noexcept
{
assert(d);
assert(current);
d->q.remove(current);
if(unlikely(std::uncaught_exceptions()))
d->notify_one();
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// //
// ctx_list.h // ctx_list.h