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
|
|
|
|
{
|
|
|
|
struct shared_state_base;
|
|
|
|
template<class T = void> struct shared_state;
|
|
|
|
template<> struct shared_state<void>;
|
2018-03-10 19:57:27 +01:00
|
|
|
template<class T> struct promise;
|
|
|
|
template<> struct promise<void>;
|
2018-03-15 19:45:01 +01:00
|
|
|
enum class future_state;
|
2018-03-10 19:57:27 +01:00
|
|
|
|
2018-03-15 19:45:01 +01:00
|
|
|
template<class T> future_state state(const shared_state<T> &);
|
|
|
|
template<class T> bool is(const shared_state<T> &, const future_state &);
|
|
|
|
template<class T> void set(shared_state<T> &, const future_state &);
|
|
|
|
template<> void set(shared_state<void> &, const future_state &);
|
2018-03-10 23:56:14 +01:00
|
|
|
template<class T> void notify(shared_state<T> &);
|
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
|
|
|
|
{
|
|
|
|
INVALID, ///< Null.
|
|
|
|
PENDING, ///< Promise is attached and busy.
|
|
|
|
READY, ///< Result ready; promise is gone.
|
|
|
|
OBSERVED, ///< Special case for when_*(); not a state; promise is gone.
|
|
|
|
RETRIEVED, ///< User retrieved future value; promise is gone.
|
|
|
|
};
|
|
|
|
|
|
|
|
/// 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;
|
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
|
|
|
|
{
|
|
|
|
using value_type = T;
|
|
|
|
using pointer_type = T *;
|
|
|
|
using reference_type = T &;
|
|
|
|
|
2018-07-06 09:23:50 +02:00
|
|
|
union
|
|
|
|
{
|
|
|
|
promise<T> *p {nullptr};
|
|
|
|
future_state st;
|
|
|
|
};
|
2016-09-19 08:31:56 +02:00
|
|
|
T val;
|
2018-03-14 19:53:25 +01:00
|
|
|
|
|
|
|
shared_state(promise<T> &p)
|
|
|
|
:p{&p}
|
|
|
|
{}
|
|
|
|
|
|
|
|
shared_state() = default;
|
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
|
|
|
|
{
|
|
|
|
using value_type = void;
|
|
|
|
|
2018-07-06 09:23:50 +02:00
|
|
|
union
|
|
|
|
{
|
|
|
|
promise<void> *p {nullptr};
|
|
|
|
future_state st;
|
|
|
|
};
|
2018-03-14 19:53:25 +01:00
|
|
|
|
|
|
|
shared_state(promise<void> &p)
|
|
|
|
:p{&p}
|
|
|
|
{}
|
|
|
|
|
|
|
|
shared_state() = default;
|
2016-09-19 08:31:56 +02:00
|
|
|
};
|
|
|
|
|
2018-03-15 19:45:01 +01:00
|
|
|
/// Internal use
|
2018-03-10 23:56:14 +01:00
|
|
|
template<class T>
|
|
|
|
void
|
|
|
|
ircd::ctx::notify(shared_state<T> &st)
|
|
|
|
{
|
|
|
|
if(!st.then)
|
2018-03-15 19:59:54 +01:00
|
|
|
{
|
|
|
|
st.cond.notify_all();
|
2018-03-10 23:56:14 +01:00
|
|
|
return;
|
2018-03-15 19:59:54 +01:00
|
|
|
}
|
2018-03-10 23:56:14 +01:00
|
|
|
|
|
|
|
if(!current)
|
2018-03-15 19:59:54 +01:00
|
|
|
{
|
|
|
|
st.cond.notify_all();
|
2018-04-28 04:05:34 +02:00
|
|
|
assert(bool(st.then));
|
2018-03-15 19:59:54 +01:00
|
|
|
st.then(st);
|
|
|
|
return;
|
|
|
|
}
|
2018-03-10 23:56:14 +01:00
|
|
|
|
2018-04-28 04:05:34 +02:00
|
|
|
const stack_usage_assertion sua;
|
|
|
|
st.cond.notify_all();
|
|
|
|
assert(bool(st.then));
|
|
|
|
st.then(st);
|
2018-03-10 23:56:14 +01:00
|
|
|
}
|
|
|
|
|
2018-03-15 19:45:01 +01:00
|
|
|
/// Internal use
|
2018-03-11 02:32:54 +01:00
|
|
|
template<class T>
|
|
|
|
void
|
2018-03-15 19:45:01 +01:00
|
|
|
ircd::ctx::set(shared_state<T> &st,
|
|
|
|
const future_state &state)
|
2018-03-11 02:32:54 +01:00
|
|
|
{
|
2018-03-15 19:45:01 +01:00
|
|
|
switch(state)
|
|
|
|
{
|
|
|
|
case future_state::INVALID: assert(0); return;
|
|
|
|
case future_state::PENDING: assert(0); return;
|
|
|
|
case future_state::OBSERVED:
|
|
|
|
case future_state::READY:
|
|
|
|
case future_state::RETRIEVED:
|
2018-07-06 09:23:50 +02:00
|
|
|
default:
|
|
|
|
st.st = state;
|
2018-03-15 19:45:01 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-03-11 02:32:54 +01:00
|
|
|
}
|
|
|
|
|
2018-03-15 19:45:01 +01:00
|
|
|
/// Internal use
|
2018-03-11 02:32:54 +01:00
|
|
|
template<>
|
|
|
|
inline void
|
2018-03-15 19:45:01 +01:00
|
|
|
ircd::ctx::set(shared_state<void> &st,
|
|
|
|
const future_state &state)
|
2018-03-11 02:32:54 +01:00
|
|
|
{
|
2018-03-15 19:45:01 +01:00
|
|
|
switch(state)
|
|
|
|
{
|
|
|
|
case future_state::INVALID: assert(0); return;
|
|
|
|
case future_state::PENDING: assert(0); return;
|
|
|
|
case future_state::READY:
|
|
|
|
case future_state::OBSERVED:
|
|
|
|
case future_state::RETRIEVED:
|
2018-07-06 09:23:50 +02:00
|
|
|
default:
|
|
|
|
st.st = state;
|
2018-03-15 19:45:01 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-03-11 02:32:54 +01:00
|
|
|
}
|
|
|
|
|
2018-03-15 19:45:01 +01:00
|
|
|
/// Internal; check if the current state is something; safe but unnecessary
|
|
|
|
/// for public use.
|
2018-03-10 19:57:27 +01:00
|
|
|
template<class T>
|
|
|
|
bool
|
2018-03-15 19:45:01 +01:00
|
|
|
ircd::ctx::is(const shared_state<T> &st,
|
|
|
|
const future_state &state_)
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2018-07-07 01:33:53 +02:00
|
|
|
switch(st.st)
|
|
|
|
{
|
|
|
|
case future_state::READY:
|
|
|
|
case future_state::OBSERVED:
|
|
|
|
case future_state::RETRIEVED:
|
|
|
|
return state_ == st.st;
|
|
|
|
|
|
|
|
default: switch(state_)
|
|
|
|
{
|
|
|
|
case future_state::INVALID:
|
|
|
|
return st.p == nullptr;
|
|
|
|
|
|
|
|
case future_state::PENDING:
|
|
|
|
return uintptr_t(st.p) >= 0x1000;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-09-19 08:31:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 19:45:01 +01:00
|
|
|
/// Internal; get the current state of the shared_state; safe but unnecessary
|
|
|
|
/// for public use.
|
2016-09-19 08:31:56 +02:00
|
|
|
template<class T>
|
2018-03-15 19:45:01 +01:00
|
|
|
ircd::ctx::future_state
|
|
|
|
ircd::ctx::state(const shared_state<T> &st)
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2018-07-06 09:23:50 +02:00
|
|
|
return uintptr_t(st.p) >= 0x1000?
|
|
|
|
future_state::PENDING:
|
|
|
|
st.st;
|
2016-09-19 08:31:56 +02:00
|
|
|
}
|