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
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
template<class T,
|
|
|
|
class time_point>
|
|
|
|
future_status wait_until(const future<T> &, const time_point &);
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
class ircd::ctx::future
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
|
|
|
std::shared_ptr<shared_state<T>> st;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-09-20 05:07:30 +02:00
|
|
|
bool valid() const { return bool(st); }
|
|
|
|
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-07 18:08:16 +01:00
|
|
|
void then(decltype(shared_state<T>::callback));
|
|
|
|
|
2016-09-19 08:31:56 +02:00
|
|
|
future();
|
|
|
|
future(promise<T> &promise);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2017-08-28 23:51:22 +02:00
|
|
|
class ircd::ctx::future<void>
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
|
|
|
std::shared_ptr<shared_state<void>> st;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using value_type = typename shared_state<void>::value_type;
|
|
|
|
|
2016-09-20 05:07:30 +02:00
|
|
|
bool valid() const { return bool(st); }
|
|
|
|
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-07 18:08:16 +01:00
|
|
|
void then(decltype(shared_state<void>::callback));
|
|
|
|
|
2016-09-19 08:31:56 +02:00
|
|
|
future();
|
|
|
|
future(promise<void> &promise);
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2017-12-18 23:06:14 +01:00
|
|
|
ircd::ctx::future<void>::future()
|
|
|
|
:st{nullptr}
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2017-12-18 23:06:14 +01:00
|
|
|
ircd::ctx::future<T>::future()
|
|
|
|
:st{nullptr}
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline
|
2017-12-18 23:06:14 +01:00
|
|
|
ircd::ctx::future<void>::future(promise<void> &promise)
|
|
|
|
:st{promise.get_state().share()}
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2017-12-18 23:06:14 +01:00
|
|
|
ircd::ctx::future<T>::future(promise<T> &promise)
|
|
|
|
:st{promise.get_state().share()}
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-07 18:08:16 +01:00
|
|
|
template<class T>
|
|
|
|
void
|
|
|
|
ircd::ctx::future<T>::then(decltype(shared_state<T>::callback) cb)
|
|
|
|
{
|
|
|
|
assert(valid());
|
|
|
|
st->callback = std::move(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
ircd::ctx::future<void>::then(decltype(shared_state<void>::callback) cb)
|
|
|
|
{
|
|
|
|
assert(valid());
|
|
|
|
st->callback = std::move(cb);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
if(unlikely(bool(st->eptr)))
|
|
|
|
std::rethrow_exception(st->eptr);
|
|
|
|
|
|
|
|
return st->val;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::future<void>::wait()
|
2016-09-19 08:31:56 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
this->wait_until(steady_clock::time_point::max());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::future<T>::wait()
|
2016-09-19 08:31:56 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
this->wait_until(steady_clock::time_point::max());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class duration>
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::future_status
|
|
|
|
ircd::ctx::future<void>::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
|
|
|
|
ircd::ctx::future<void>::wait(const duration &d,
|
|
|
|
std::nothrow_t)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return this->wait_until(steady_clock::now() + d, std::nothrow);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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 T>
|
|
|
|
template<class duration>
|
|
|
|
ircd::ctx::future_status
|
|
|
|
ircd::ctx::future<T>::wait(const duration &d,
|
|
|
|
std::nothrow_t)
|
|
|
|
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 T>
|
|
|
|
template<class time_point>
|
|
|
|
ircd::ctx::future_status
|
|
|
|
ircd::ctx::future<T>::wait_until(const time_point &tp,
|
|
|
|
std::nothrow_t)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return ircd::ctx::wait_until(*this, tp, std::nothrow);
|
|
|
|
}
|
|
|
|
|
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<void>::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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return ircd::ctx::wait_until(*this, tp, std::nothrow);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-09-23 23:50:24 +02:00
|
|
|
return f.st->finished;
|
2016-09-19 08:31:56 +02:00
|
|
|
});
|
|
|
|
|
2016-09-23 23:50:24 +02:00
|
|
|
if(unlikely(!f.valid()))
|
2016-09-19 08:31:56 +02:00
|
|
|
throw no_state();
|
|
|
|
|
2016-09-23 23:50:24 +02:00
|
|
|
if(unlikely(!f.st->cond.wait_until(tp, wfun)))
|
2016-09-19 08:31:56 +02:00
|
|
|
return future_status::timeout;
|
|
|
|
|
|
|
|
return likely(wfun())? future_status::ready:
|
|
|
|
future_status::deferred;
|
|
|
|
}
|