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>;
|
|
|
|
|
|
|
|
template<class T> bool invalid(const shared_state<T> &);
|
|
|
|
template<class T> bool pending(const shared_state<T> &);
|
2018-03-11 02:32:54 +01:00
|
|
|
template<class T> bool retrieved(const shared_state<T> &);
|
2018-03-10 19:57:27 +01:00
|
|
|
template<class T> bool ready(const shared_state<T> &);
|
2018-03-10 23:56:14 +01:00
|
|
|
template<class T> void notify(shared_state<T> &);
|
2018-03-11 02:32:54 +01:00
|
|
|
template<class T> void set_retrieved(shared_state<T> &);
|
|
|
|
template<class T> void set_ready(shared_state<T> &);
|
|
|
|
template<class T> void set_observed(shared_state<T> &);
|
|
|
|
template<> void set_observed(shared_state<void> &);
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
2016-09-19 08:31:56 +02:00
|
|
|
|
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-10 23:56:14 +01:00
|
|
|
std::function<void (shared_state_base &)> then;
|
2016-09-19 08:31:56 +02:00
|
|
|
};
|
|
|
|
|
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-03-10 19:57:27 +01:00
|
|
|
promise<T> *p {nullptr};
|
2016-09-19 08:31:56 +02:00
|
|
|
T val;
|
|
|
|
};
|
|
|
|
|
|
|
|
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-03-10 19:57:27 +01:00
|
|
|
promise<void> *p {nullptr};
|
2016-09-19 08:31:56 +02:00
|
|
|
};
|
|
|
|
|
2018-03-10 23:56:14 +01:00
|
|
|
template<class T>
|
|
|
|
void
|
|
|
|
ircd::ctx::notify(shared_state<T> &st)
|
|
|
|
{
|
|
|
|
st.cond.notify_all();
|
|
|
|
|
|
|
|
if(!st.then)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!current)
|
|
|
|
return st.then(st);
|
|
|
|
|
|
|
|
ircd::post([&st]
|
|
|
|
{
|
|
|
|
st.then(st);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-11 02:32:54 +01:00
|
|
|
template<class T>
|
|
|
|
void
|
|
|
|
ircd::ctx::set_observed(shared_state<T> &st)
|
|
|
|
{
|
|
|
|
set_ready(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline void
|
|
|
|
ircd::ctx::set_observed(shared_state<void> &st)
|
|
|
|
{
|
|
|
|
set_retrieved(st);
|
|
|
|
}
|
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
template<class T>
|
|
|
|
void
|
|
|
|
ircd::ctx::set_ready(shared_state<T> &st)
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2018-03-10 19:57:27 +01:00
|
|
|
st.p = reinterpret_cast<promise<T> *>(uintptr_t(0x42));
|
2016-09-19 08:31:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 02:32:54 +01:00
|
|
|
template<class T>
|
|
|
|
void
|
|
|
|
ircd::ctx::set_retrieved(shared_state<T> &st)
|
|
|
|
{
|
|
|
|
st.p = reinterpret_cast<promise<T> *>(uintptr_t(0x84));
|
|
|
|
}
|
|
|
|
|
2016-09-19 08:31:56 +02:00
|
|
|
template<class T>
|
2018-03-10 19:57:27 +01:00
|
|
|
bool
|
|
|
|
ircd::ctx::ready(const shared_state<T> &st)
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2018-03-10 19:57:27 +01:00
|
|
|
return st.p == reinterpret_cast<const promise<T> *>(uintptr_t(0x42));
|
2016-09-19 08:31:56 +02:00
|
|
|
}
|
|
|
|
|
2018-03-11 02:32:54 +01:00
|
|
|
template<class T>
|
|
|
|
bool
|
|
|
|
ircd::ctx::retrieved(const shared_state<T> &st)
|
|
|
|
{
|
|
|
|
return st.p == reinterpret_cast<const promise<T> *>(uintptr_t(0x84));
|
|
|
|
}
|
|
|
|
|
2018-03-10 19:57:27 +01:00
|
|
|
template<class T>
|
|
|
|
bool
|
|
|
|
ircd::ctx::pending(const shared_state<T> &st)
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2018-03-11 02:32:54 +01:00
|
|
|
return st.p > reinterpret_cast<const promise<T> *>(uintptr_t(0x1000));
|
2016-09-19 08:31:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2018-03-10 19:57:27 +01:00
|
|
|
bool
|
|
|
|
ircd::ctx::invalid(const shared_state<T> &st)
|
2016-09-19 08:31:56 +02:00
|
|
|
{
|
2018-03-10 19:57:27 +01:00
|
|
|
return st.p == nullptr;
|
2016-09-19 08:31:56 +02:00
|
|
|
}
|