2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2016-09-07 23:39:41 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_CTX_DOCK_H
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::ctx
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
2017-08-28 23:51:22 +02:00
|
|
|
struct dock;
|
|
|
|
}
|
2016-09-07 23:39:41 +02:00
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
/// dock is a condition variable which has no requirement for locking because
|
|
|
|
/// the context system does not require mutual exclusion for coherence.
|
|
|
|
///
|
2017-08-28 23:51:22 +02:00
|
|
|
class ircd::ctx::dock
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
2019-01-13 22:57:34 +01:00
|
|
|
using predicate = std::function<bool ()>;
|
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
list q;
|
2016-09-07 23:39:41 +02:00
|
|
|
|
|
|
|
public:
|
2019-09-10 19:19:50 +02:00
|
|
|
bool empty() const noexcept;
|
|
|
|
size_t size() const noexcept;
|
2019-09-10 19:42:55 +02:00
|
|
|
bool waiting(const ctx &) const noexcept;
|
2016-09-07 23:39:41 +02:00
|
|
|
|
2019-01-13 22:57:34 +01:00
|
|
|
template<class time_point> bool wait_until(time_point&&, const predicate &);
|
|
|
|
template<class time_point> bool wait_until(time_point&&);
|
2016-09-07 23:39:41 +02:00
|
|
|
|
2019-01-13 22:57:34 +01:00
|
|
|
template<class duration> bool wait_for(const duration &, const predicate &);
|
|
|
|
template<class duration> bool wait_for(const duration &);
|
2016-09-07 23:39:41 +02:00
|
|
|
|
2019-01-13 22:57:34 +01:00
|
|
|
void wait(const predicate &);
|
2016-09-07 23:39:41 +02:00
|
|
|
void wait();
|
|
|
|
|
|
|
|
void notify_all() noexcept;
|
|
|
|
void notify_one() noexcept;
|
2016-11-01 11:54:52 +01:00
|
|
|
void notify() noexcept;
|
2016-09-07 23:39:41 +02:00
|
|
|
};
|
|
|
|
|
2018-03-27 01:52:14 +02:00
|
|
|
/// Returns true if notified; false if timed out
|
2016-09-07 23:39:41 +02:00
|
|
|
template<class duration>
|
2018-03-27 01:52:14 +02:00
|
|
|
bool
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::dock::wait_for(const duration &dur)
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
|
|
|
static const duration zero(0);
|
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
assert(current);
|
2019-03-23 02:54:33 +01:00
|
|
|
const unwind::exceptional renotify{[this]
|
|
|
|
{
|
|
|
|
notify_one();
|
|
|
|
}};
|
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
const unwind remove{[this]
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
2018-01-14 08:14:23 +01:00
|
|
|
q.remove(current);
|
|
|
|
}};
|
2016-09-07 23:39:41 +02:00
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
q.push_back(current);
|
2018-03-27 01:52:14 +02:00
|
|
|
return ircd::ctx::wait<std::nothrow_t>(dur) > zero;
|
2016-09-07 23:39:41 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 01:52:14 +02:00
|
|
|
/// Returns true if predicate passed; false if timed out
|
2019-01-13 22:57:34 +01:00
|
|
|
template<class duration>
|
2016-09-07 23:39:41 +02:00
|
|
|
bool
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::dock::wait_for(const duration &dur,
|
2019-01-13 22:57:34 +01:00
|
|
|
const predicate &pred)
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
|
|
|
static const duration zero(0);
|
|
|
|
|
|
|
|
if(pred())
|
|
|
|
return true;
|
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
assert(current);
|
2019-03-23 02:54:33 +01:00
|
|
|
const unwind::exceptional renotify{[this]
|
|
|
|
{
|
|
|
|
notify_one();
|
|
|
|
}};
|
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
const unwind remove{[this]
|
2017-09-20 04:01:37 +02:00
|
|
|
{
|
2018-01-14 08:14:23 +01:00
|
|
|
q.remove(current);
|
|
|
|
}};
|
2017-09-20 04:01:37 +02:00
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
q.push_back(current); do
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
2018-08-15 17:21:03 +02:00
|
|
|
const bool expired
|
|
|
|
{
|
|
|
|
ircd::ctx::wait<std::nothrow_t>(dur) <= zero
|
|
|
|
};
|
2016-09-07 23:39:41 +02:00
|
|
|
|
|
|
|
if(pred())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(expired)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while(1);
|
|
|
|
}
|
|
|
|
|
2018-03-27 01:52:14 +02:00
|
|
|
/// Returns true if notified; false if timed out
|
2016-09-07 23:39:41 +02:00
|
|
|
template<class time_point>
|
2018-03-27 01:52:14 +02:00
|
|
|
bool
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::dock::wait_until(time_point&& tp)
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
2018-01-14 08:14:23 +01:00
|
|
|
assert(current);
|
2019-03-23 02:54:33 +01:00
|
|
|
const unwind::exceptional renotify{[this]
|
|
|
|
{
|
|
|
|
notify_one();
|
|
|
|
}};
|
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
const unwind remove{[this]
|
2017-09-12 18:37:44 +02:00
|
|
|
{
|
2018-01-14 08:14:23 +01:00
|
|
|
q.remove(current);
|
|
|
|
}};
|
2016-09-07 23:39:41 +02:00
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
q.push_back(current);
|
2018-03-27 01:52:14 +02:00
|
|
|
return !ircd::ctx::wait_until<std::nothrow_t>(tp);
|
2016-09-07 23:39:41 +02:00
|
|
|
}
|
|
|
|
|
2018-03-27 01:52:14 +02:00
|
|
|
/// Returns true if predicate passed; false if timed out
|
2019-01-13 22:57:34 +01:00
|
|
|
template<class time_point>
|
2016-09-07 23:39:41 +02:00
|
|
|
bool
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::dock::wait_until(time_point&& tp,
|
2019-01-13 22:57:34 +01:00
|
|
|
const predicate &pred)
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
|
|
|
if(pred())
|
|
|
|
return true;
|
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
assert(current);
|
2019-03-23 02:54:33 +01:00
|
|
|
const unwind::exceptional renotify{[this]
|
|
|
|
{
|
|
|
|
notify_one();
|
|
|
|
}};
|
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
const unwind remove{[this]
|
2017-09-12 18:37:44 +02:00
|
|
|
{
|
2018-01-14 08:14:23 +01:00
|
|
|
q.remove(current);
|
|
|
|
}};
|
2017-09-12 18:37:44 +02:00
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
q.push_back(current); do
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
2017-09-12 18:37:44 +02:00
|
|
|
const bool expired
|
|
|
|
{
|
|
|
|
ircd::ctx::wait_until<std::nothrow_t>(tp)
|
|
|
|
};
|
2016-09-07 23:39:41 +02:00
|
|
|
|
|
|
|
if(pred())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(expired)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while(1);
|
|
|
|
}
|
2019-10-10 04:01:45 +02:00
|
|
|
|
|
|
|
/// Wake up the next context waiting on the dock
|
|
|
|
inline void
|
|
|
|
ircd::ctx::dock::notify_one()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(q.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ircd::ctx::notify(*q.front());
|
|
|
|
}
|