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)
|
|
|
|
|
|
|
|
future_state state(const shared_state_base &);
|
|
|
|
bool is(const shared_state_base &, const future_state &);
|
|
|
|
void set(shared_state_base &, const future_state &);
|
|
|
|
size_t refcount(const shared_state_base &);
|
|
|
|
void invalidate(shared_state_base &);
|
|
|
|
void update(shared_state_base &);
|
|
|
|
void notify(shared_state_base &);
|
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.
|
2017-08-28 23:51:22 +02:00
|
|
|
struct ircd::ctx::shared_state_base
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
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;
|
2018-10-30 19:18:03 +01:00
|
|
|
union
|
|
|
|
{
|
|
|
|
promise_base *p {nullptr};
|
|
|
|
future_state st;
|
|
|
|
};
|
2018-08-29 03:56:41 +02:00
|
|
|
|
2018-10-30 19:18:03 +01:00
|
|
|
shared_state_base(promise_base &p);
|
|
|
|
shared_state_base() = default;
|
2018-08-29 03:56:41 +02:00
|
|
|
shared_state_base(shared_state_base &&) = default;
|
|
|
|
shared_state_base(const shared_state_base &) = delete;
|
|
|
|
shared_state_base &operator=(shared_state_base &&) = default;
|
|
|
|
shared_state_base &operator=(const shared_state_base &) = delete;
|
|
|
|
~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
|
|
|
};
|