2016-09-07 23:39:41 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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;
|
|
|
|
enum class cv_status;
|
|
|
|
}
|
2016-09-07 23:39:41 +02:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
//
|
|
|
|
// a dock is a condition variable which has no requirement for locking because
|
|
|
|
// the context system does not require mutual exclusion for coherence, however
|
|
|
|
// we have to create our own queue here rather than piggyback a mutex's.
|
|
|
|
//
|
|
|
|
class ircd::ctx::dock
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
|
|
|
std::deque<ctx *> q;
|
|
|
|
|
|
|
|
void remove_self();
|
2017-03-27 23:56:05 +02:00
|
|
|
void notify(ctx &) noexcept;
|
2016-09-07 23:39:41 +02:00
|
|
|
|
|
|
|
public:
|
2017-09-12 18:37:44 +02:00
|
|
|
size_t size() const;
|
2016-09-07 23:39:41 +02:00
|
|
|
|
|
|
|
template<class time_point, class predicate> bool wait_until(time_point&& tp, predicate&& pred);
|
|
|
|
template<class time_point> cv_status wait_until(time_point&& tp);
|
|
|
|
|
|
|
|
template<class duration, class predicate> bool wait_for(const duration &dur, predicate&& pred);
|
|
|
|
template<class duration> cv_status wait_for(const duration &dur);
|
|
|
|
|
|
|
|
template<class predicate> void wait(predicate&& pred);
|
|
|
|
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
|
|
|
|
|
|
|
dock() = default;
|
|
|
|
~dock() noexcept;
|
|
|
|
};
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
enum class ircd::ctx::cv_status
|
|
|
|
{
|
|
|
|
no_timeout, timeout
|
|
|
|
};
|
|
|
|
|
2016-09-07 23:39:41 +02:00
|
|
|
inline
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::dock::~dock()
|
2016-09-07 23:39:41 +02:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
assert(q.empty());
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Wake up the next context waiting on the dock
|
|
|
|
///
|
|
|
|
/// Unlike notify_one(), the next context in the queue is repositioned in the
|
|
|
|
/// back before being woken up for better fairness.
|
2016-11-01 11:54:52 +01:00
|
|
|
inline void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::dock::notify()
|
2016-11-01 11:54:52 +01:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(q.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto c(q.front());
|
|
|
|
q.pop_front();
|
|
|
|
q.emplace_back(c);
|
2017-03-27 23:56:05 +02:00
|
|
|
notify(*c);
|
2016-11-01 11:54:52 +01:00
|
|
|
}
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Wake up the next context waiting on the dock
|
2016-09-07 23:39:41 +02:00
|
|
|
inline void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::dock::notify_one()
|
2016-09-07 23:39:41 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2016-09-10 07:23:07 +02:00
|
|
|
if(q.empty())
|
|
|
|
return;
|
2016-09-07 23:39:41 +02:00
|
|
|
|
2017-03-27 23:56:05 +02:00
|
|
|
notify(*q.front());
|
2016-09-07 23:39:41 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Wake up all contexts waiting on the dock.
|
|
|
|
///
|
|
|
|
/// We copy the queue and post all notifications without requesting direct context
|
|
|
|
/// switches. This ensures everyone gets notified in a single transaction without
|
|
|
|
/// any interleaving during this process.
|
2016-09-07 23:39:41 +02:00
|
|
|
inline void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::dock::notify_all()
|
2016-09-07 23:39:41 +02:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
const auto copy(q);
|
|
|
|
for(const auto &c : copy)
|
2016-11-01 11:54:52 +01:00
|
|
|
ircd::ctx::notify(*c);
|
2016-09-07 23:39:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::dock::wait()
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
|
|
|
const scope remove(std::bind(&dock::remove_self, this));
|
|
|
|
q.emplace_back(&cur());
|
|
|
|
ircd::ctx::wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class predicate>
|
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::dock::wait(predicate&& pred)
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
|
|
|
if(pred())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const scope remove(std::bind(&dock::remove_self, this));
|
|
|
|
q.emplace_back(&cur()); do
|
|
|
|
{
|
|
|
|
ircd::ctx::wait();
|
|
|
|
}
|
|
|
|
while(!pred());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class duration>
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::cv_status
|
|
|
|
ircd::ctx::dock::wait_for(const duration &dur)
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
|
|
|
static const duration zero(0);
|
|
|
|
|
|
|
|
const scope remove(std::bind(&dock::remove_self, this));
|
|
|
|
q.emplace_back(&cur());
|
|
|
|
|
|
|
|
return ircd::ctx::wait<std::nothrow_t>(dur) > zero? cv_status::no_timeout:
|
|
|
|
cv_status::timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class duration,
|
|
|
|
class predicate>
|
|
|
|
bool
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::dock::wait_for(const duration &dur,
|
2017-09-12 18:37:44 +02:00
|
|
|
predicate&& pred)
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
|
|
|
static const duration zero(0);
|
|
|
|
|
|
|
|
if(pred())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const scope remove(std::bind(&dock::remove_self, this));
|
|
|
|
q.emplace_back(&cur()); do
|
|
|
|
{
|
|
|
|
const bool expired(ircd::ctx::wait<std::nothrow_t>(dur) <= zero);
|
|
|
|
|
|
|
|
if(pred())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if(expired)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
while(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class time_point>
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::cv_status
|
|
|
|
ircd::ctx::dock::wait_until(time_point&& tp)
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
2017-09-12 18:37:44 +02:00
|
|
|
const scope remove
|
|
|
|
{
|
|
|
|
std::bind(&dock::remove_self, this)
|
|
|
|
};
|
2016-09-07 23:39:41 +02:00
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
q.emplace_back(&cur());
|
2016-09-07 23:39:41 +02:00
|
|
|
return ircd::ctx::wait_until<std::nothrow_t>(tp)? cv_status::timeout:
|
|
|
|
cv_status::no_timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class time_point,
|
|
|
|
class predicate>
|
|
|
|
bool
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::dock::wait_until(time_point&& tp,
|
2017-09-12 18:37:44 +02:00
|
|
|
predicate&& pred)
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
|
|
|
if(pred())
|
|
|
|
return true;
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
const scope remove
|
|
|
|
{
|
|
|
|
std::bind(&dock::remove_self, this)
|
|
|
|
};
|
|
|
|
|
2016-09-07 23:39:41 +02:00
|
|
|
q.emplace_back(&cur()); do
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// The number of contexts waiting in the queue.
|
|
|
|
inline size_t
|
|
|
|
ircd::ctx::dock::size()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return q.size();
|
|
|
|
}
|
|
|
|
|
2017-03-27 23:56:05 +02:00
|
|
|
inline void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::dock::notify(ctx &ctx)
|
2017-03-27 23:56:05 +02:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
// This branch handles dock.notify() being called from outside the context system.
|
|
|
|
// If a context is currently running we can make a direct context-switch with
|
|
|
|
// yield(ctx), otherwise notify(ctx) enqueues the context.
|
|
|
|
|
|
|
|
if(current)
|
|
|
|
ircd::ctx::yield(ctx);
|
|
|
|
else
|
|
|
|
ircd::ctx::notify(ctx);
|
|
|
|
}
|
|
|
|
|
2016-09-07 23:39:41 +02:00
|
|
|
inline void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::dock::remove_self()
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
2016-09-10 07:23:07 +02:00
|
|
|
const auto it(std::find(begin(q), end(q), &cur()));
|
|
|
|
assert(it != end(q));
|
|
|
|
q.erase(it);
|
2016-09-07 23:39:41 +02:00
|
|
|
}
|