0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-25 16:22:35 +01:00
construct/include/ircd/ctx/future.h

281 lines
7.3 KiB
C
Raw Normal View History

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.
#pragma once
#define HAVE_IRCD_CTX_FUTURE_H
namespace ircd::ctx
{
template<class T = void> class future;
template<> class future<void>;
template<class... T> struct scoped_future;
IRCD_EXCEPTION(future_error, future_already_retrieved)
IRCD_OVERLOAD(use_future)
2019-02-16 01:47:00 +01:00
template<class T> const shared_state<T> &state(const future<T> &);
template<class T> shared_state<T> &state(future<T> &);
template<class T,
class time_point>
2019-07-19 23:42:46 +02:00
bool _wait_until(const future<T> &, const time_point &, std::nothrow_t);
2019-02-16 01:47:00 +01:00
template<class T,
class time_point>
2019-07-19 23:42:46 +02:00
void _wait_until(const future<T> &, const time_point &);
}
template<class T>
struct ircd::ctx::future
:private shared_state<T>
{
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;
const shared_state<T> &state() const { return *this; }
shared_state<T> &state() { return *this; }
bool valid() const { return !is(state(), future_state::INVALID); }
2016-09-20 05:07:30 +02:00
bool operator!() const { return !valid(); }
explicit operator T() { return get(); }
template<class U, class time_point> friend bool wait_until(const future<U> &, const time_point &, std::nothrow_t);
template<class U, class time_point> friend void wait_until(const future<U> &, const time_point &);
template<class time_point> bool wait_until(const time_point &, std::nothrow_t) const;
template<class time_point> void wait_until(const time_point &) const;
template<class duration> bool wait(const duration &d, std::nothrow_t) const;
template<class duration> void wait(const duration &d) const;
void wait() const;
T get();
template<class duration> T get(const duration &d);
template<class time_point> T get_until(const time_point &);
2019-09-01 08:05:06 +02:00
using shared_state<T>::shared_state;
using shared_state<T>::operator=;
};
template<>
struct ircd::ctx::future<void>
:private shared_state<void>
{
using value_type = typename shared_state<void>::value_type;
const shared_state<void> &state() const { return *this; }
shared_state<void> &state() { return *this; }
bool valid() const { return !is(state(), future_state::INVALID); }
2016-09-20 05:07:30 +02:00
bool operator!() const { return !valid(); }
template<class U, class time_point> friend bool wait_until(const future<U> &, const time_point &, std::nothrow_t);
template<class U, class time_point> friend void wait_until(const future<U> &, const time_point &);
template<class time_point> bool wait_until(const time_point &, std::nothrow_t) const;
template<class time_point> void wait_until(const time_point &) const;
template<class duration> bool wait(const duration &d, std::nothrow_t) const;
template<class duration> void wait(const duration &d) const;
void wait() const;
2019-09-01 08:05:06 +02:00
using shared_state<void>::shared_state;
using shared_state<void>::operator=;
};
template<class... T>
struct ircd::ctx::scoped_future
:future<T...>
{
2019-09-01 08:05:06 +02:00
using future<T...>::future;
~scoped_future() noexcept;
};
template<class... T>
inline
ircd::ctx::scoped_future<T...>::~scoped_future()
noexcept
{
2019-09-01 08:05:06 +02:00
if(std::uncaught_exceptions() || !this->valid())
return;
2019-09-01 08:05:06 +02:00
this->wait();
}
template<class T>
template<class time_point>
inline T
ircd::ctx::future<T>::get_until(const time_point &tp)
{
this->wait_until(tp);
return this->get();
}
template<class T>
template<class duration>
inline T
ircd::ctx::future<T>::get(const duration &d)
{
this->wait(d);
return this->get();
}
template<class T>
inline T
ircd::ctx::future<T>::get()
{
wait();
if(unlikely(is(state(), future_state::RETRIEVED)))
throw future_already_retrieved{};
set(state(), future_state::RETRIEVED);
if(bool(state().eptr))
std::rethrow_exception(state().eptr);
return std::move(state().val);
}
template<class T>
inline void
ircd::ctx::future<T>::wait()
const
{
this->wait_until(system_point::max());
}
inline void
ircd::ctx::future<void>::wait()
const
{
this->wait_until(system_point::max());
}
template<class T>
template<class duration>
inline void
ircd::ctx::future<T>::wait(const duration &d)
const
{
this->wait_until(now<system_point>() + d);
}
template<class duration>
inline void
ircd::ctx::future<void>::wait(const duration &d)
const
{
this->wait_until(now<system_point>() + d);
}
template<class T>
template<class duration>
inline bool
ircd::ctx::future<T>::wait(const duration &d,
std::nothrow_t)
const
{
return this->wait_until(now<system_point>() + d, std::nothrow);
}
template<class duration>
inline bool
ircd::ctx::future<void>::wait(const duration &d,
std::nothrow_t)
const
{
return this->wait_until(now<system_point>() + d, std::nothrow);
}
template<class T>
template<class time_point>
inline void
ircd::ctx::future<T>::wait_until(const time_point &tp)
const
{
2019-07-20 03:08:37 +02:00
if(!this->wait_until(tp, std::nothrow))
throw timeout{};
}
template<class time_point>
inline void
ircd::ctx::future<void>::wait_until(const time_point &tp)
const
{
if(!this->wait_until(tp, std::nothrow))
throw timeout{};
}
template<class T>
template<class time_point>
inline bool
ircd::ctx::future<T>::wait_until(const time_point &tp,
std::nothrow_t)
const
{
2019-07-19 23:42:46 +02:00
return _wait_until(*this, tp, std::nothrow);
}
template<class time_point>
inline bool
ircd::ctx::future<void>::wait_until(const time_point &tp,
std::nothrow_t)
const
{
2019-07-19 23:42:46 +02:00
if(_wait_until(*this, tp, std::nothrow))
{
2019-09-01 08:05:06 +02:00
auto &state(mutable_cast(this->state()));
set(state, future_state::RETRIEVED);
if(bool(state.eptr))
std::rethrow_exception(state.eptr);
return true;
}
else return false;
}
template<class T,
class time_point>
inline void
ircd::ctx::wait_until(const future<T> &f,
const time_point &tp)
{
2019-07-19 23:42:46 +02:00
if(!_wait_until(f, tp, std::nothrow))
throw timeout{};
}
template<class T,
class time_point>
inline bool
2019-07-19 23:42:46 +02:00
ircd::ctx::_wait_until(const future<T> &f,
const time_point &tp,
std::nothrow_t)
{
2019-09-01 08:05:06 +02:00
auto &state(mutable_cast(f.state()));
if(unlikely(is(state, future_state::INVALID)))
throw no_state{};
return state.cond.wait_until(tp, [&state]() noexcept
2019-07-19 23:42:46 +02:00
{
return !is(state, future_state::PENDING);
});
}
template<class T>
inline ircd::ctx::shared_state<T> &
ircd::ctx::state(future<T> &future)
{
return future.state();
}
template<class T>
inline const ircd::ctx::shared_state<T> &
ircd::ctx::state(const future<T> &future)
{
return future.state();
}