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_PROMISE_H
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
2018-10-30 19:18:03 +01:00
|
|
|
struct promise_base;
|
2018-03-10 19:57:27 +01:00
|
|
|
template<class T = void> class promise;
|
|
|
|
template<> class promise<void>;
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
IRCD_EXCEPTION(future_error, no_state)
|
|
|
|
IRCD_EXCEPTION(future_error, broken_promise)
|
|
|
|
IRCD_EXCEPTION(future_error, promise_already_satisfied)
|
|
|
|
}
|
2016-09-19 08:31:56 +02:00
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
/// Abstract base type for ctx::promise. This dedupes most of the promissory
|
|
|
|
/// functionality with non-template definitions residing in ctx.cc.
|
|
|
|
///
|
|
|
|
/// In this system the promise is lightweight and maintains a pointer to the
|
|
|
|
/// shared_state object which generally resides within the future instance.
|
|
|
|
/// If the shared_state object moves or is destroyed the promise's pointer to
|
|
|
|
/// it must be updated. The shared_state object also has a pointer to the
|
|
|
|
/// promise; if the promise moves or is destroyed that pointer must be updated
|
|
|
|
/// as well. This is how the bi-directional relationship is maintained.
|
|
|
|
///
|
|
|
|
/// To further complicate things, this promise maintains a second pointer
|
|
|
|
/// to another instance of a promise implementing a linked-list semantic. All
|
|
|
|
/// of these promises are making the same promise to the same shared_state;
|
|
|
|
/// the list allows for copy semantics which are important for some callback
|
|
|
|
/// systems (like boost::asio). This solution is far more optimal than
|
2019-09-01 08:05:06 +02:00
|
|
|
/// allocating the promise in a shared_ptr and refcounting... Note that the
|
|
|
|
/// same semantic exists on the future side to implement shared futures. Both
|
|
|
|
/// parties maintain a pointer to the head of a singly linked list of the
|
|
|
|
/// other party, and a pointer to the next instance of their own party.
|
2018-10-30 19:18:03 +01:00
|
|
|
struct ircd::ctx::promise_base
|
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
static const promise_base *head(const shared_state_base &);
|
|
|
|
static const promise_base *head(const promise_base &);
|
2019-09-08 22:02:32 +02:00
|
|
|
static size_t refcount(const shared_state_base &);
|
2019-09-09 01:33:38 +02:00
|
|
|
static size_t refcount(const promise_base &);
|
2019-09-08 22:02:32 +02:00
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
static promise_base *head(shared_state_base &);
|
2019-09-07 22:24:02 +02:00
|
|
|
static promise_base *head(promise_base &);
|
2019-09-01 08:05:06 +02:00
|
|
|
|
2019-09-04 07:07:50 +02:00
|
|
|
shared_state_base *st {nullptr}; // the head of all sharing futures
|
|
|
|
promise_base *next {nullptr}; // next sharing promise
|
2018-10-30 19:18:03 +01:00
|
|
|
|
2019-08-14 05:43:46 +02:00
|
|
|
template<class T> const shared_state<T> &state() const noexcept;
|
|
|
|
template<class T> shared_state<T> &state() noexcept;
|
|
|
|
const shared_state_base &state() const noexcept;
|
|
|
|
shared_state_base &state() noexcept;
|
2018-10-30 19:18:03 +01:00
|
|
|
|
|
|
|
void check_pending() const;
|
|
|
|
void make_ready();
|
2019-09-01 08:05:06 +02:00
|
|
|
void remove();
|
2016-09-19 08:31:56 +02:00
|
|
|
|
|
|
|
public:
|
2019-08-14 05:43:46 +02:00
|
|
|
bool valid() const noexcept;
|
|
|
|
bool operator!() const noexcept;
|
|
|
|
explicit operator bool() const noexcept;
|
2016-09-19 08:31:56 +02:00
|
|
|
|
2019-09-08 22:02:32 +02:00
|
|
|
void set_exception(std::exception_ptr);
|
2018-03-14 19:53:25 +01:00
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
protected:
|
|
|
|
promise_base() = default;
|
|
|
|
promise_base(const promise_base &);
|
|
|
|
promise_base(promise_base &&) noexcept;
|
|
|
|
promise_base &operator=(const promise_base &);
|
|
|
|
promise_base &operator=(promise_base &&) noexcept;
|
2019-09-01 08:02:51 +02:00
|
|
|
~promise_base() noexcept;
|
2018-10-30 19:18:03 +01:00
|
|
|
};
|
2016-09-19 08:31:56 +02:00
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
/// Value-oriented promise. The user creates an instance of this promise in
|
|
|
|
/// order to send a value to a future. After creating an instance of this
|
|
|
|
/// promise the user should construct a future with the matching template type
|
|
|
|
/// from this. The two will then be linked.
|
|
|
|
///
|
|
|
|
/// Space for the value will reside within the future instance and not the
|
|
|
|
/// promise instance. When the value is set it will be copied (or movied) there.
|
|
|
|
///
|
|
|
|
/// Full object semantics for this promise are supported; including copy
|
|
|
|
/// semantics. All copies of a promise are making the same promise thus
|
|
|
|
/// setting a value or exception for one invalidates all the copies.
|
|
|
|
///
|
|
|
|
/// Instances of this promise can safely destruct at any time. When all copies
|
|
|
|
/// of a promise destruct without setting a value or exception the future is
|
|
|
|
/// notified with a broken_promise exception.
|
|
|
|
template<class T>
|
|
|
|
struct ircd::ctx::promise
|
|
|
|
:promise_base
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
shared_state<T> &state();
|
|
|
|
|
|
|
|
public:
|
2016-09-19 08:31:56 +02:00
|
|
|
void set_value(const T &val);
|
|
|
|
void set_value(T&& val);
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
using promise_base::promise_base;
|
|
|
|
using promise_base::operator=;
|
2016-09-19 08:31:56 +02:00
|
|
|
};
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
/// Valueless promise. The user creates an instance of this promise in
|
|
|
|
/// order to notify a future of a success or set an exception for failure.
|
|
|
|
///
|
|
|
|
/// See docs for promise<T> for more information.
|
2016-09-19 08:31:56 +02:00
|
|
|
template<>
|
2018-03-10 19:57:27 +01:00
|
|
|
struct ircd::ctx::promise<void>
|
2018-10-30 19:18:03 +01:00
|
|
|
:promise_base
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2018-10-30 19:18:03 +01:00
|
|
|
using value_type = typename shared_state<void>::value_type;
|
2016-09-19 08:31:56 +02:00
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
const shared_state<void> &state() const;
|
|
|
|
shared_state<void> &state();
|
2018-03-14 19:53:25 +01:00
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
public:
|
2016-09-19 08:31:56 +02:00
|
|
|
void set_value();
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
using promise_base::promise_base;
|
|
|
|
using promise_base::operator=;
|
2016-09-19 08:31:56 +02:00
|
|
|
};
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
//
|
|
|
|
// promise<T>
|
|
|
|
//
|
2016-09-19 08:31:56 +02:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::promise<T>::set_value(T&& val)
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2019-03-01 01:45:20 +01:00
|
|
|
if(!valid())
|
|
|
|
return;
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
check_pending();
|
2019-09-01 08:05:06 +02:00
|
|
|
auto *state
|
|
|
|
{
|
|
|
|
shared_state_base::head(*this)
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(state);
|
|
|
|
if(shared_state_base::refcount(*state) > 1) do
|
|
|
|
{
|
|
|
|
assert(is(*state, future_state::PENDING));
|
|
|
|
static_cast<shared_state<T> &>(*state).val = val;
|
|
|
|
}
|
|
|
|
while((state = state->next)); else
|
|
|
|
{
|
|
|
|
assert(is(this->state(), future_state::PENDING));
|
|
|
|
this->state().val = std::move(val);
|
|
|
|
}
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
make_ready();
|
2018-03-10 19:57:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void
|
|
|
|
ircd::ctx::promise<T>::set_value(const T &val)
|
|
|
|
{
|
2019-03-01 01:45:20 +01:00
|
|
|
if(!valid())
|
|
|
|
return;
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
check_pending();
|
2019-09-01 08:05:06 +02:00
|
|
|
auto *state(shared_state_base::head(*this)); do
|
|
|
|
{
|
|
|
|
assert(state);
|
|
|
|
assert(is(*state, future_state::PENDING));
|
|
|
|
static_cast<shared_state<T> &>(*state).val = val;
|
|
|
|
}
|
|
|
|
while((state = state->next));
|
2018-10-30 19:18:03 +01:00
|
|
|
make_ready();
|
2018-03-10 19:57:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2018-10-30 19:18:03 +01:00
|
|
|
ircd::ctx::shared_state<T> &
|
|
|
|
ircd::ctx::promise<T>::state()
|
2018-03-10 19:57:27 +01:00
|
|
|
{
|
2018-10-30 19:18:03 +01:00
|
|
|
return promise_base::state<T>();
|
2018-03-10 19:57:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2018-10-30 19:18:03 +01:00
|
|
|
const ircd::ctx::shared_state<T> &
|
|
|
|
ircd::ctx::promise<T>::state()
|
|
|
|
const
|
2018-03-10 19:57:27 +01:00
|
|
|
{
|
2018-10-30 19:18:03 +01:00
|
|
|
return promise_base::state<T>();
|
2018-03-10 19:57:27 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
//
|
|
|
|
// promise_base
|
|
|
|
//
|
2018-08-31 04:53:16 +02:00
|
|
|
|
2019-09-01 08:05:06 +02:00
|
|
|
inline void
|
|
|
|
ircd::ctx::promise_base::check_pending()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(valid());
|
|
|
|
if(unlikely(!is(state(), future_state::PENDING)))
|
|
|
|
throw promise_already_satisfied{};
|
|
|
|
}
|
|
|
|
|
2019-08-14 05:43:46 +02:00
|
|
|
inline bool
|
|
|
|
ircd::ctx::promise_base::operator!()
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
return !valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::ctx::promise_base::operator
|
|
|
|
bool()
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
return valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::ctx::promise_base::valid()
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
return bool(st);
|
|
|
|
}
|
|
|
|
|
2018-08-31 04:53:16 +02:00
|
|
|
template<class T>
|
|
|
|
ircd::ctx::shared_state<T> &
|
2018-10-30 19:18:03 +01:00
|
|
|
ircd::ctx::promise_base::state()
|
2019-08-14 05:43:46 +02:00
|
|
|
noexcept
|
2018-08-31 04:53:16 +02:00
|
|
|
{
|
2018-10-30 19:18:03 +01:00
|
|
|
return static_cast<shared_state<T> &>(state());
|
2018-08-31 04:53:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
const ircd::ctx::shared_state<T> &
|
2018-10-30 19:18:03 +01:00
|
|
|
ircd::ctx::promise_base::state()
|
2019-08-14 05:43:46 +02:00
|
|
|
const noexcept
|
2018-08-31 04:53:16 +02:00
|
|
|
{
|
2018-10-30 19:18:03 +01:00
|
|
|
return static_cast<const shared_state<T> &>(state());
|
2018-08-31 04:53:16 +02:00
|
|
|
}
|
2019-08-14 05:43:46 +02:00
|
|
|
|
|
|
|
inline ircd::ctx::shared_state_base &
|
|
|
|
ircd::ctx::promise_base::state()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
assert(valid());
|
|
|
|
return *st;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const ircd::ctx::shared_state_base &
|
|
|
|
ircd::ctx::promise_base::state()
|
|
|
|
const noexcept
|
|
|
|
{
|
|
|
|
assert(valid());
|
|
|
|
return *st;
|
|
|
|
}
|