2018-02-03 18:22:01 -08: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 14:39:41 -07:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_CTX_DOCK_H
|
|
|
|
|
2017-08-28 14:51:22 -07:00
|
|
|
namespace ircd::ctx
|
2016-09-07 14:39:41 -07:00
|
|
|
{
|
2017-08-28 14:51:22 -07:00
|
|
|
struct dock;
|
2020-02-19 16:16:53 -08:00
|
|
|
|
|
|
|
void terminate(dock &) noexcept;
|
|
|
|
void interrupt(dock &) noexcept;
|
|
|
|
void notify(dock &) noexcept;
|
2017-08-28 14:51:22 -07:00
|
|
|
}
|
2016-09-07 14:39:41 -07:00
|
|
|
|
2018-01-13 23:14:23 -08: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 14:51:22 -07:00
|
|
|
class ircd::ctx::dock
|
2016-09-07 14:39:41 -07:00
|
|
|
{
|
2019-01-13 13:57:34 -08:00
|
|
|
using predicate = std::function<bool ()>;
|
|
|
|
|
2018-01-13 23:14:23 -08:00
|
|
|
list q;
|
2016-09-07 14:39:41 -07:00
|
|
|
|
|
|
|
public:
|
2019-09-10 10:19:50 -07:00
|
|
|
bool empty() const noexcept;
|
|
|
|
size_t size() const noexcept;
|
2019-09-10 10:42:55 -07:00
|
|
|
bool waiting(const ctx &) const noexcept;
|
2016-09-07 14:39:41 -07:00
|
|
|
|
2022-06-29 13:03:04 -07:00
|
|
|
bool wait_until(const system_point, const predicate &);
|
|
|
|
bool wait_until(const system_point);
|
2016-09-07 14:39:41 -07:00
|
|
|
|
2022-06-29 13:03:04 -07:00
|
|
|
template<class duration> bool wait_for(const duration, const predicate &);
|
|
|
|
template<class duration> bool wait_for(const duration);
|
2016-09-07 14:39:41 -07:00
|
|
|
|
2019-01-13 13:57:34 -08:00
|
|
|
void wait(const predicate &);
|
2016-09-07 14:39:41 -07:00
|
|
|
void wait();
|
|
|
|
|
2020-02-19 16:16:53 -08:00
|
|
|
void terminate_all() noexcept;
|
|
|
|
void interrupt_all() noexcept;
|
2016-09-07 14:39:41 -07:00
|
|
|
void notify_all() noexcept;
|
|
|
|
void notify_one() noexcept;
|
2016-11-01 03:54:52 -07:00
|
|
|
void notify() noexcept;
|
2016-09-07 14:39:41 -07:00
|
|
|
};
|
|
|
|
|
2022-06-29 13:03:04 -07:00
|
|
|
namespace ircd::ctx
|
2020-05-01 16:20:16 -07:00
|
|
|
{
|
2022-06-29 13:03:04 -07:00
|
|
|
template<> bool dock::wait_for(const microseconds, const predicate &);
|
|
|
|
template<> bool dock::wait_for(const microseconds);
|
2020-05-01 16:20:16 -07: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());
|
|
|
|
}
|
|
|
|
|
2016-09-07 14:39:41 -07:00
|
|
|
template<class duration>
|
2020-07-11 15:29:13 -07:00
|
|
|
inline bool
|
2022-06-29 13:03:04 -07:00
|
|
|
ircd::ctx::dock::wait_for(const duration dur,
|
|
|
|
const predicate &pred)
|
2016-09-07 14:39:41 -07:00
|
|
|
{
|
2022-06-29 13:03:04 -07:00
|
|
|
static_assert(!std::is_same<duration, microseconds>());
|
|
|
|
return wait_for(duration_cast<microseconds>(dur), pred);
|
|
|
|
}
|
2016-09-07 14:39:41 -07:00
|
|
|
|
2022-06-29 13:03:04 -07:00
|
|
|
template<class duration>
|
|
|
|
inline bool
|
|
|
|
ircd::ctx::dock::wait_for(const duration dur)
|
|
|
|
{
|
|
|
|
static_assert(!std::is_same<duration, microseconds>());
|
|
|
|
return wait_for(duration_cast<microseconds>(dur));
|
|
|
|
}
|
2019-03-22 18:54:33 -07:00
|
|
|
|
2016-09-07 14:39:41 -07:00
|
|
|
|
2022-06-29 13:03:04 -07:00
|
|
|
/// The number of contexts waiting in the queue.
|
|
|
|
inline size_t
|
|
|
|
ircd::ctx::dock::size()
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
return q.size();
|
2016-09-07 14:39:41 -07:00
|
|
|
}
|
|
|
|
|
2022-06-29 13:03:04 -07:00
|
|
|
/// The number of contexts waiting in the queue.
|
2020-07-11 15:29:13 -07:00
|
|
|
inline bool
|
2022-06-29 13:03:04 -07:00
|
|
|
ircd::ctx::dock::empty()
|
|
|
|
const noexcept
|
2016-09-07 14:39:41 -07:00
|
|
|
{
|
2022-06-29 13:03:04 -07:00
|
|
|
return q.empty();
|
2016-09-07 14:39:41 -07:00
|
|
|
}
|
|
|
|
|
2022-06-29 13:03:04 -07:00
|
|
|
inline void
|
|
|
|
ircd::ctx::notify(dock &dock)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
dock.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ircd::ctx::interrupt(dock &dock)
|
|
|
|
noexcept
|
2016-09-07 14:39:41 -07:00
|
|
|
{
|
2022-06-29 13:03:04 -07:00
|
|
|
dock.interrupt_all();
|
2016-09-07 14:39:41 -07:00
|
|
|
}
|
|
|
|
|
2022-06-29 13:03:04 -07:00
|
|
|
inline void
|
|
|
|
ircd::ctx::terminate(dock &dock)
|
|
|
|
noexcept
|
2016-09-07 14:39:41 -07:00
|
|
|
{
|
2022-06-29 13:03:04 -07:00
|
|
|
dock.terminate_all();
|
2016-09-07 14:39:41 -07:00
|
|
|
}
|