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;
|
2020-02-20 01:16:53 +01:00
|
|
|
|
|
|
|
void terminate(dock &) noexcept;
|
|
|
|
void interrupt(dock &) noexcept;
|
|
|
|
void notify(dock &) noexcept;
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
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();
|
|
|
|
|
2020-02-20 01:16:53 +01:00
|
|
|
void terminate_all() noexcept;
|
|
|
|
void interrupt_all() noexcept;
|
2016-09-07 23:39:41 +02:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-05-02 01:20:16 +02:00
|
|
|
inline void
|
|
|
|
ircd::ctx::notify(dock &dock)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
dock.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ircd::ctx::interrupt(dock &dock)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
dock.interrupt_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ircd::ctx::terminate(dock &dock)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
dock.terminate_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// dock::dock
|
|
|
|
//
|
|
|
|
|
|
|
|
/// 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());
|
|
|
|
}
|
|
|
|
|
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-12-03 22:05:32 +01:00
|
|
|
const unwind_exceptional renotify{[this]
|
2019-03-23 02:54:33 +01:00
|
|
|
{
|
|
|
|
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-12-03 22:05:32 +01:00
|
|
|
const unwind_exceptional renotify{[this]
|
2019-03-23 02:54:33 +01:00
|
|
|
{
|
|
|
|
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-12-03 22:05:32 +01:00
|
|
|
const unwind_exceptional renotify{[this]
|
2019-03-23 02:54:33 +01:00
|
|
|
{
|
|
|
|
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-12-03 22:05:32 +01:00
|
|
|
const unwind_exceptional renotify{[this]
|
2019-03-23 02:54:33 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|