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_SHARED_STATE_H
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
2018-10-30 19:18:03 +01:00
|
|
|
enum class future_state :uintptr_t;
|
2017-08-28 23:51:22 +02:00
|
|
|
struct shared_state_base;
|
2018-10-30 19:18:03 +01:00
|
|
|
struct promise_base;
|
|
|
|
|
|
|
|
template<class T> struct shared_state;
|
2017-08-28 23:51:22 +02:00
|
|
|
template<> struct shared_state<void>;
|
2018-03-10 19:57:27 +01:00
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
IRCD_EXCEPTION(ircd::ctx::error, future_error)
|
2019-09-01 08:05:06 +02:00
|
|
|
IRCD_OVERLOAD(already)
|
2018-10-30 19:18:03 +01:00
|
|
|
|
|
|
|
future_state state(const shared_state_base &);
|
|
|
|
bool is(const shared_state_base &, const future_state &);
|
|
|
|
void set(shared_state_base &, const future_state &);
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
2016-09-19 08:31:56 +02:00
|
|
|
|
2018-03-15 19:45:01 +01:00
|
|
|
/// Internal state enumeration for the promise / future / related. These can
|
|
|
|
/// all be observed through state() or is(); only some can be set(). This is
|
|
|
|
/// not for public manipulation.
|
|
|
|
enum class ircd::ctx::future_state
|
2018-08-29 03:56:41 +02:00
|
|
|
:uintptr_t
|
2018-03-15 19:45:01 +01:00
|
|
|
{
|
2018-10-30 19:18:03 +01:00
|
|
|
INVALID = 0, ///< Null.
|
|
|
|
PENDING = 1, ///< Promise is attached and busy.
|
|
|
|
READY = 2, ///< Result ready; promise is gone.
|
|
|
|
OBSERVED = 3, ///< Special case for when_*(); not a state; promise is gone.
|
|
|
|
RETRIEVED = 4, ///< User retrieved future value; promise is gone.
|
2018-03-15 19:45:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Internal Non-template base of the state object shared by promise and
|
|
|
|
/// future. It is extended by the appropriate template, and usually resides
|
|
|
|
/// in the future's instance, where the promise finds it.
|
2019-09-01 08:05:06 +02:00
|
|
|
///
|
|
|
|
/// There can be multiple promises and multiple futures all associated with
|
|
|
|
/// the same resolution event. All promises point to the first
|
|
|
|
/// shared_state_base (future) of the associated shared_state_base list. When
|
|
|
|
/// any promise in the list of associated promises sets a result, it copies
|
|
|
|
/// the result to all futures in the list; if only one future, it std::move()'s
|
|
|
|
/// the result; then the association of all promises and all futures and
|
|
|
|
/// respective lists are invalidated.
|
|
|
|
///
|
|
|
|
/// Note that the only way to traverse the list of shared_state_bases's is to
|
|
|
|
/// dereference the promise pointer (head promise) and follow the st->next
|
|
|
|
/// list. The only way to traverse the list of promises is to dereference a
|
|
|
|
/// shared_state_base with a valid *p in future_state::PENDING and chase the
|
|
|
|
/// p->next list.
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::ctx::shared_state_base
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
static const shared_state_base *head(const shared_state_base &);
|
|
|
|
static const shared_state_base *head(const promise_base &);
|
|
|
|
static size_t refcount(const shared_state_base &);
|
|
|
|
|
|
|
|
static shared_state_base *head(shared_state_base &);
|
|
|
|
static shared_state_base *head(promise_base &);
|
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
mutable dock cond;
|
2016-09-19 08:31:56 +02:00
|
|
|
std::exception_ptr eptr;
|
2018-03-14 19:53:25 +01:00
|
|
|
std::function<void (shared_state_base &)> then;
|
2019-09-04 07:07:50 +02:00
|
|
|
shared_state_base *next{nullptr}; // next sharing future
|
2018-10-30 19:18:03 +01:00
|
|
|
union
|
|
|
|
{
|
2019-09-01 08:05:06 +02:00
|
|
|
promise_base *p {nullptr}; // the head of all sharing promises
|
2018-10-30 19:18:03 +01:00
|
|
|
future_state st;
|
|
|
|
};
|
2018-08-29 03:56:41 +02:00
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
shared_state_base() = default;
|
2019-09-01 08:05:06 +02:00
|
|
|
shared_state_base(already_t);
|
|
|
|
shared_state_base(promise_base &);
|
|
|
|
shared_state_base(shared_state_base &&) noexcept;
|
|
|
|
shared_state_base(const shared_state_base &);
|
|
|
|
shared_state_base &operator=(promise_base &);
|
|
|
|
shared_state_base &operator=(shared_state_base &&) noexcept;
|
|
|
|
shared_state_base &operator=(const shared_state_base &);
|
2019-09-01 08:02:51 +02:00
|
|
|
~shared_state_base() noexcept;
|
2016-09-19 08:31:56 +02:00
|
|
|
};
|
|
|
|
|
2018-03-15 19:45:01 +01:00
|
|
|
/// Internal shared state between future and promise appropos a future value.
|
2017-08-28 23:51:22 +02:00
|
|
|
template<class T>
|
|
|
|
struct ircd::ctx::shared_state
|
2016-09-19 08:31:56 +02:00
|
|
|
:shared_state_base
|
|
|
|
{
|
2018-10-30 19:18:03 +01:00
|
|
|
using value_type = T;
|
|
|
|
using pointer_type = T *;
|
|
|
|
using reference_type = T &;
|
2016-09-19 08:31:56 +02:00
|
|
|
|
|
|
|
T val;
|
2018-03-14 19:53:25 +01:00
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
using shared_state_base::shared_state_base;
|
|
|
|
using shared_state_base::operator=;
|
2016-09-19 08:31:56 +02:00
|
|
|
};
|
|
|
|
|
2018-03-15 19:45:01 +01:00
|
|
|
/// Internal shared state between future and promise when there is no future
|
|
|
|
/// value, only a notification of completion or exception.
|
2016-09-19 08:31:56 +02:00
|
|
|
template<>
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::ctx::shared_state<void>
|
2016-09-19 08:31:56 +02:00
|
|
|
:shared_state_base
|
|
|
|
{
|
2018-10-30 19:18:03 +01:00
|
|
|
using value_type = void;
|
2016-09-19 08:31:56 +02:00
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
using shared_state_base::shared_state_base;
|
|
|
|
using shared_state_base::operator=;
|
2016-09-19 08:31:56 +02:00
|
|
|
};
|