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-19 08:31:56 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_CTX_FUTURE_H
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
2018-01-22 09:10:32 +01:00
|
|
|
IRCD_OVERLOAD(use_future)
|
2018-01-08 21:40:09 +01:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
template<class T = void> class future;
|
|
|
|
template<> class future<void>;
|
|
|
|
template<class... T> struct scoped_future;
|
|
|
|
enum class future_status;
|
2016-09-19 08:31:56 +02:00
|
|
|
|
2018-03-11 02:33:14 +01:00
|
|
|
template<class it> future<it> when_any(it first, const it &last);
|
|
|
|
template<class it> future<void> when_all(it first, const it &last);
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enum class ircd::ctx::future_status
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
|
|
|
ready,
|
|
|
|
timeout,
|
|
|
|
deferred,
|
|
|
|
};
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
template<class T>
|
2018-03-10 19:57:27 +01:00
|
|
|
struct ircd::ctx::future
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2018-03-11 02:32:54 +01:00
|
|
|
mutable shared_state<T> st;
|
2016-09-19 08:31:56 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
using value_type = typename shared_state<T>::value_type;
|
|
|
|
using pointer_type = typename shared_state<T>::pointer_type;
|
|
|
|
using reference_type = typename shared_state<T>::reference_type;
|
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
bool valid() const { return !invalid(st); }
|
2016-09-20 05:07:30 +02:00
|
|
|
bool operator!() const { return !valid(); }
|
|
|
|
operator bool() const { return valid(); }
|
2016-09-19 08:31:56 +02:00
|
|
|
|
2018-03-07 17:17:50 +01:00
|
|
|
template<class U, class time_point> friend future_status wait_until(const future<U> &, const time_point &, std::nothrow_t);
|
2016-09-23 23:50:24 +02:00
|
|
|
template<class U, class time_point> friend future_status wait_until(const future<U> &, const time_point &);
|
2018-03-07 17:17:50 +01:00
|
|
|
template<class time_point> future_status wait_until(const time_point &, std::nothrow_t) const;
|
2016-09-19 08:31:56 +02:00
|
|
|
template<class time_point> future_status wait_until(const time_point &) const;
|
2018-03-07 17:17:50 +01:00
|
|
|
template<class duration> future_status wait(const duration &d, std::nothrow_t) const;
|
2016-09-19 08:31:56 +02:00
|
|
|
template<class duration> future_status wait(const duration &d) const;
|
|
|
|
void wait() const;
|
|
|
|
|
|
|
|
T get();
|
2016-09-20 05:07:30 +02:00
|
|
|
operator T() { return get(); }
|
2016-09-19 08:31:56 +02:00
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
future() = default;
|
2016-09-19 08:31:56 +02:00
|
|
|
future(promise<T> &promise);
|
2018-03-10 19:57:27 +01:00
|
|
|
future(future &&) noexcept;
|
|
|
|
future(const future &) = delete;
|
|
|
|
future &operator=(future &&) noexcept;
|
|
|
|
future &operator=(const future &) = delete;
|
|
|
|
~future() noexcept;
|
2016-09-19 08:31:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2018-03-10 19:57:27 +01:00
|
|
|
struct ircd::ctx::future<void>
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2018-03-11 02:32:54 +01:00
|
|
|
mutable shared_state<void> st;
|
2016-09-19 08:31:56 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
using value_type = typename shared_state<void>::value_type;
|
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
bool valid() const { return !invalid(st); }
|
2016-09-20 05:07:30 +02:00
|
|
|
bool operator!() const { return !valid(); }
|
|
|
|
operator bool() const { return valid(); }
|
2016-09-19 08:31:56 +02:00
|
|
|
|
2018-03-07 17:17:50 +01:00
|
|
|
template<class U, class time_point> friend future_status wait_until(const future<U> &, const time_point &, std::nothrow_t);
|
2016-09-23 23:50:24 +02:00
|
|
|
template<class U, class time_point> friend future_status wait_until(const future<U> &, const time_point &);
|
2018-03-07 17:17:50 +01:00
|
|
|
template<class time_point> future_status wait_until(const time_point &, std::nothrow_t) const;
|
2016-09-19 08:31:56 +02:00
|
|
|
template<class time_point> future_status wait_until(const time_point &) const;
|
2018-03-07 17:17:50 +01:00
|
|
|
template<class duration> future_status wait(const duration &d, std::nothrow_t) const;
|
2016-09-19 08:31:56 +02:00
|
|
|
template<class duration> future_status wait(const duration &d) const;
|
|
|
|
void wait() const;
|
|
|
|
|
2018-03-11 21:39:13 +01:00
|
|
|
IRCD_OVERLOAD(already)
|
|
|
|
|
2016-09-19 08:31:56 +02:00
|
|
|
future(promise<void> &promise);
|
2018-03-11 21:39:13 +01:00
|
|
|
future(already_t); // construct in ready state
|
|
|
|
future() = default;
|
2018-03-10 19:57:27 +01:00
|
|
|
future(future &&) noexcept;
|
|
|
|
future(const future &) = delete;
|
|
|
|
future &operator=(future &&) noexcept;
|
|
|
|
future &operator=(const future &) = delete;
|
|
|
|
~future() noexcept;
|
2016-09-19 08:31:56 +02:00
|
|
|
};
|
|
|
|
|
2018-03-11 02:32:54 +01:00
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
|
|
|
template<class T,
|
|
|
|
class time_point>
|
|
|
|
future_status wait_until(const future<T> &, const time_point &, std::nothrow_t);
|
|
|
|
}
|
|
|
|
|
2016-09-19 08:31:56 +02:00
|
|
|
template<class... T>
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::ctx::scoped_future
|
|
|
|
:future<T...>
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
|
|
|
template<class... Args> scoped_future(Args&&... args);
|
|
|
|
~scoped_future() noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class... T>
|
|
|
|
template<class... Args>
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::scoped_future<T...>::scoped_future(Args&&... args)
|
|
|
|
:future<T...>{std::forward<Args>(args)...}
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class... T>
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::scoped_future<T...>::~scoped_future()
|
2016-09-19 08:31:56 +02:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(std::uncaught_exception())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(this->valid())
|
|
|
|
this->wait();
|
|
|
|
}
|
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
template<class T>
|
|
|
|
ircd::ctx::future<T>::future(promise<T> &promise)
|
|
|
|
{
|
|
|
|
assert(!promise.st);
|
|
|
|
st.p = &promise;
|
|
|
|
update(st);
|
|
|
|
assert(promise.st);
|
|
|
|
}
|
|
|
|
|
2016-09-19 08:31:56 +02:00
|
|
|
inline
|
2018-03-10 19:57:27 +01:00
|
|
|
ircd::ctx::future<void>::future(promise<void> &promise)
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2018-03-10 19:57:27 +01:00
|
|
|
assert(!promise.st);
|
|
|
|
st.p = &promise;
|
|
|
|
update(st);
|
|
|
|
assert(promise.st);
|
2016-09-19 08:31:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 21:39:13 +01:00
|
|
|
inline
|
|
|
|
ircd::ctx::future<void>::future(already_t)
|
|
|
|
{
|
|
|
|
set_ready(st);
|
|
|
|
}
|
|
|
|
|
2016-09-19 08:31:56 +02:00
|
|
|
template<class T>
|
2018-03-10 19:57:27 +01:00
|
|
|
ircd::ctx::future<T>::future(future<T> &&o)
|
|
|
|
noexcept
|
|
|
|
:st{std::move(o.st)}
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2018-03-10 19:57:27 +01:00
|
|
|
update(st);
|
|
|
|
o.st.p = nullptr;
|
2016-09-19 08:31:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2018-03-10 19:57:27 +01:00
|
|
|
ircd::ctx::future<void>::future(future<void> &&o)
|
|
|
|
noexcept
|
|
|
|
:st{std::move(o.st)}
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2018-03-10 19:57:27 +01:00
|
|
|
update(st);
|
|
|
|
o.st.p = nullptr;
|
2016-09-19 08:31:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2018-03-10 19:57:27 +01:00
|
|
|
ircd::ctx::future<T> &
|
|
|
|
ircd::ctx::future<T>::operator=(future<T> &&o)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
this->~future();
|
|
|
|
st = std::move(o.st);
|
|
|
|
update(st);
|
|
|
|
o.st.p = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::ctx::future<void> &
|
|
|
|
ircd::ctx::future<void>::operator=(future<void> &&o)
|
|
|
|
noexcept
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2018-03-10 19:57:27 +01:00
|
|
|
this->~future();
|
|
|
|
st = std::move(o.st);
|
|
|
|
update(st);
|
|
|
|
o.st.p = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
ircd::ctx::future<T>::~future()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
invalidate(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
|
|
|
ircd::ctx::future<void>::~future()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
invalidate(st);
|
2016-09-19 08:31:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::future<T>::get()
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
|
|
|
wait();
|
2018-03-11 02:32:54 +01:00
|
|
|
if(unlikely(retrieved(st)))
|
|
|
|
throw future_already_retrieved{};
|
2016-09-19 08:31:56 +02:00
|
|
|
|
2018-03-11 02:32:54 +01:00
|
|
|
set_retrieved(st);
|
|
|
|
if(bool(st.eptr))
|
2018-03-10 19:57:27 +01:00
|
|
|
std::rethrow_exception(st.eptr);
|
2016-09-19 08:31:56 +02:00
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
return st.val;
|
2016-09-19 08:31:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
template<class T>
|
|
|
|
void
|
|
|
|
ircd::ctx::future<T>::wait()
|
2016-09-19 08:31:56 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
this->wait_until(steady_clock::time_point::max());
|
|
|
|
}
|
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
inline void
|
|
|
|
ircd::ctx::future<void>::wait()
|
2016-09-19 08:31:56 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
this->wait_until(steady_clock::time_point::max());
|
|
|
|
}
|
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
template<class T>
|
2016-09-19 08:31:56 +02:00
|
|
|
template<class duration>
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::future_status
|
2018-03-10 19:57:27 +01:00
|
|
|
ircd::ctx::future<T>::wait(const duration &d)
|
2016-09-19 08:31:56 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return this->wait_until(steady_clock::now() + d);
|
|
|
|
}
|
|
|
|
|
2018-03-07 17:17:50 +01:00
|
|
|
template<class duration>
|
|
|
|
ircd::ctx::future_status
|
2018-03-10 19:57:27 +01:00
|
|
|
ircd::ctx::future<void>::wait(const duration &d)
|
2018-03-07 17:17:50 +01:00
|
|
|
const
|
|
|
|
{
|
2018-03-10 19:57:27 +01:00
|
|
|
return this->wait_until(steady_clock::now() + d);
|
2018-03-07 17:17:50 +01:00
|
|
|
}
|
|
|
|
|
2016-09-19 08:31:56 +02:00
|
|
|
template<class T>
|
|
|
|
template<class duration>
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::future_status
|
2018-03-10 19:57:27 +01:00
|
|
|
ircd::ctx::future<T>::wait(const duration &d,
|
|
|
|
std::nothrow_t)
|
2016-09-19 08:31:56 +02:00
|
|
|
const
|
|
|
|
{
|
2018-03-10 19:57:27 +01:00
|
|
|
return this->wait_until(steady_clock::now() + d, std::nothrow);
|
2016-09-19 08:31:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-07 17:17:50 +01:00
|
|
|
template<class duration>
|
|
|
|
ircd::ctx::future_status
|
2018-03-10 19:57:27 +01:00
|
|
|
ircd::ctx::future<void>::wait(const duration &d,
|
|
|
|
std::nothrow_t)
|
2018-03-07 17:17:50 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return this->wait_until(steady_clock::now() + d, std::nothrow);
|
|
|
|
}
|
|
|
|
|
2016-09-23 23:50:24 +02:00
|
|
|
template<class T>
|
2016-09-19 08:31:56 +02:00
|
|
|
template<class time_point>
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::future_status
|
|
|
|
ircd::ctx::future<T>::wait_until(const time_point &tp)
|
2016-09-19 08:31:56 +02:00
|
|
|
const
|
|
|
|
{
|
2016-09-23 23:50:24 +02:00
|
|
|
return ircd::ctx::wait_until(*this, tp);
|
2016-09-19 08:31:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-07 17:17:50 +01:00
|
|
|
template<class time_point>
|
|
|
|
ircd::ctx::future_status
|
2018-03-10 19:57:27 +01:00
|
|
|
ircd::ctx::future<void>::wait_until(const time_point &tp)
|
2018-03-07 17:17:50 +01:00
|
|
|
const
|
|
|
|
{
|
2018-03-11 02:32:54 +01:00
|
|
|
const auto status
|
|
|
|
{
|
|
|
|
this->wait_until(tp, std::nothrow)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(status == future_status::timeout)
|
|
|
|
throw timeout{};
|
|
|
|
|
|
|
|
return status;
|
2018-03-07 17:17:50 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
template<class T>
|
2016-09-19 08:31:56 +02:00
|
|
|
template<class time_point>
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::future_status
|
2018-03-10 19:57:27 +01:00
|
|
|
ircd::ctx::future<T>::wait_until(const time_point &tp,
|
|
|
|
std::nothrow_t)
|
2016-09-19 08:31:56 +02:00
|
|
|
const
|
|
|
|
{
|
2018-03-10 19:57:27 +01:00
|
|
|
return ircd::ctx::wait_until(*this, tp, std::nothrow);
|
2016-09-23 23:50:24 +02:00
|
|
|
}
|
|
|
|
|
2018-03-07 17:17:50 +01:00
|
|
|
template<class time_point>
|
|
|
|
ircd::ctx::future_status
|
|
|
|
ircd::ctx::future<void>::wait_until(const time_point &tp,
|
|
|
|
std::nothrow_t)
|
|
|
|
const
|
|
|
|
{
|
2018-03-11 02:32:54 +01:00
|
|
|
const auto status
|
|
|
|
{
|
|
|
|
ircd::ctx::wait_until(*this, tp, std::nothrow)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(status == future_status::ready)
|
|
|
|
{
|
|
|
|
set_retrieved(st);
|
|
|
|
if(bool(st.eptr))
|
|
|
|
std::rethrow_exception(st.eptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2018-03-07 17:17:50 +01:00
|
|
|
}
|
|
|
|
|
2016-09-23 23:50:24 +02:00
|
|
|
template<class T,
|
|
|
|
class time_point>
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::future_status
|
|
|
|
ircd::ctx::wait_until(const future<T> &f,
|
|
|
|
const time_point &tp)
|
2018-03-07 17:17:50 +01:00
|
|
|
{
|
|
|
|
const auto ret
|
|
|
|
{
|
|
|
|
wait_until(f, tp, std::nothrow)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(ret == future_status::timeout)
|
|
|
|
throw timeout{};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T,
|
|
|
|
class time_point>
|
|
|
|
ircd::ctx::future_status
|
|
|
|
ircd::ctx::wait_until(const future<T> &f,
|
|
|
|
const time_point &tp,
|
|
|
|
std::nothrow_t)
|
2016-09-23 23:50:24 +02:00
|
|
|
{
|
|
|
|
const auto wfun([&f]() -> bool
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2018-03-10 19:57:27 +01:00
|
|
|
return !pending(f.st);
|
2016-09-19 08:31:56 +02:00
|
|
|
});
|
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
if(unlikely(invalid(f.st)))
|
|
|
|
throw no_state{};
|
2016-09-19 08:31:56 +02:00
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
if(unlikely(!f.st.cond.wait_until(tp, wfun)))
|
2016-09-19 08:31:56 +02:00
|
|
|
return future_status::timeout;
|
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
return likely(wfun())?
|
|
|
|
future_status::ready:
|
|
|
|
future_status::deferred;
|
2016-09-19 08:31:56 +02:00
|
|
|
}
|
2018-03-11 02:33:14 +01:00
|
|
|
|
|
|
|
template<class it>
|
|
|
|
ircd::ctx::future<void>
|
|
|
|
ircd::ctx::when_all(it first,
|
|
|
|
const it &last)
|
|
|
|
{
|
|
|
|
promise<void> p;
|
|
|
|
future<void> ret(p);
|
|
|
|
for(; first != last; ++first)
|
|
|
|
if(pending(first->st))
|
|
|
|
first->st.then = [p](shared_state_base &) mutable
|
|
|
|
{
|
|
|
|
if(!p.valid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(refcount(*p.st) < 2)
|
|
|
|
return p.set_value();
|
|
|
|
|
|
|
|
return remove(*p.st, p);
|
|
|
|
};
|
|
|
|
|
|
|
|
if(refcount(*p.st) <= 1)
|
|
|
|
p.set_value();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class it>
|
|
|
|
ircd::ctx::future<it>
|
|
|
|
ircd::ctx::when_any(it first,
|
|
|
|
const it &last)
|
|
|
|
{
|
|
|
|
promise<it> p;
|
|
|
|
future<it> ret(p);
|
|
|
|
for(auto first_(first); first_ != last; ++first_)
|
|
|
|
if(ready(first_->st))
|
|
|
|
{
|
|
|
|
set_observed(first_->st);
|
|
|
|
p.set_value(first_);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(; first != last; ++first)
|
|
|
|
if(pending(first->st))
|
|
|
|
first->st.then = [p, first](shared_state_base &) mutable
|
|
|
|
{
|
|
|
|
if(!p.valid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
set_observed(first->st);
|
|
|
|
p.set_value(first);
|
|
|
|
};
|
|
|
|
|
|
|
|
if(refcount(*p.st) <= 1)
|
|
|
|
p.set_value(first);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|