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
|
|
|
|
2017-03-27 23:56:05 +02:00
|
|
|
void notify(ctx &) noexcept;
|
2016-09-07 23:39:41 +02:00
|
|
|
|
|
|
|
public:
|
2017-09-20 04:01:37 +02:00
|
|
|
bool empty() const;
|
2017-09-12 18:37:44 +02:00
|
|
|
size_t size() const;
|
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)
|
2019-03-12 02:18:58 +01:00
|
|
|
try
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
|
|
|
static const duration zero(0);
|
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
assert(current);
|
|
|
|
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
|
|
|
}
|
2019-03-12 02:18:58 +01:00
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
notify_one();
|
|
|
|
throw;
|
|
|
|
}
|
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)
|
2019-03-12 02:18:58 +01:00
|
|
|
try
|
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);
|
|
|
|
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);
|
|
|
|
}
|
2019-03-12 02:18:58 +01:00
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
notify_one();
|
|
|
|
throw;
|
|
|
|
}
|
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 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)
|
2019-03-12 02:18:58 +01:00
|
|
|
try
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
2018-01-14 08:14:23 +01:00
|
|
|
assert(current);
|
|
|
|
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
|
|
|
}
|
2019-03-12 02:18:58 +01:00
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
notify_one();
|
|
|
|
throw;
|
|
|
|
}
|
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)
|
2019-03-12 02:18:58 +01:00
|
|
|
try
|
2016-09-07 23:39:41 +02:00
|
|
|
{
|
|
|
|
if(pred())
|
|
|
|
return true;
|
|
|
|
|
2018-01-14 08:14:23 +01:00
|
|
|
assert(current);
|
|
|
|
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-03-12 02:18:58 +01:00
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
notify_one();
|
|
|
|
throw;
|
|
|
|
}
|