0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-17 23:40:57 +01:00

ircd::ctx: Add sleep() suite, or a wait() which ignores notification.

This commit is contained in:
Jason Volk 2016-09-18 22:22:22 -07:00
parent 1a367399d3
commit e2d115a97a
3 changed files with 27 additions and 1 deletions

View file

@ -29,7 +29,6 @@
namespace ircd { namespace ircd {
namespace ctx { namespace ctx {
using std::chrono::microseconds;
using std::chrono::steady_clock; using std::chrono::steady_clock;
using time_point = steady_clock::time_point; using time_point = steady_clock::time_point;
@ -75,6 +74,11 @@ bool wait_until(const time_point &tp, const std::nothrow_t &);
template<class E> nothrow_overload<E, bool> wait_until(const time_point &tp); template<class E> nothrow_overload<E, bool> wait_until(const time_point &tp);
template<class E = timeout> throw_overload<E> wait_until(const time_point &tp); template<class E = timeout> throw_overload<E> wait_until(const time_point &tp);
// Ignores notes. Throws if interrupted.
void sleep_until(const time_point &tp);
template<class duration> void sleep(const duration &);
void sleep(const int &secs);
class context class context
{ {
std::unique_ptr<ctx> c; std::unique_ptr<ctx> c;
@ -108,6 +112,19 @@ noexcept
std::swap(a.c, b.c); std::swap(a.c, b.c);
} }
inline void
sleep(const int &secs)
{
sleep(seconds(secs));
}
template<class duration>
void
sleep(const duration &d)
{
sleep_until(steady_clock::now() + d);
}
template<class E> template<class E>
throw_overload<E> throw_overload<E>
wait_until(const time_point &tp) wait_until(const time_point &tp)

View file

@ -44,6 +44,9 @@ namespace ircd
using std::begin; using std::begin;
using std::end; using std::end;
using std::get; using std::get;
using std::chrono::seconds;
using std::chrono::milliseconds;
using std::chrono::microseconds;
using ostream = std::ostream; using ostream = std::ostream;
namespace ph = std::placeholders; namespace ph = std::placeholders;
using namespace std::string_literals; using namespace std::string_literals;

View file

@ -29,6 +29,12 @@ using namespace ircd;
// //
__thread ctx::ctx *ctx::current; __thread ctx::ctx *ctx::current;
void
ctx::sleep_until(const std::chrono::steady_clock::time_point &tp)
{
while(!wait_until(tp, std::nothrow));
}
bool bool
ctx::wait_until(const std::chrono::steady_clock::time_point &tp, ctx::wait_until(const std::chrono::steady_clock::time_point &tp,
const std::nothrow_t &) const std::nothrow_t &)