// Matrix Construct // // Copyright (C) Matrix Construct Developers, Authors & Contributors // Copyright (C) 2016-2018 Jason Volk // // 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. #pragma once #define HAVE_IRCD_CTX_SHARED_STATE_H namespace ircd::ctx { struct shared_state_base; template struct shared_state; template<> struct shared_state; template struct promise; template<> struct promise; template bool invalid(const shared_state &); template bool pending(const shared_state &); template bool retrieved(const shared_state &); template bool ready(const shared_state &); template void notify(shared_state &); template void set_retrieved(shared_state &); template void set_ready(shared_state &); template void set_observed(shared_state &); template<> void set_observed(shared_state &); } struct ircd::ctx::shared_state_base { mutable dock cond; std::exception_ptr eptr; }; template struct ircd::ctx::shared_state :shared_state_base { using value_type = T; using pointer_type = T *; using reference_type = T &; std::function &)> then; promise *p {nullptr}; T val; }; template<> struct ircd::ctx::shared_state :shared_state_base { using value_type = void; std::function &)> then; promise *p {nullptr}; }; template void ircd::ctx::notify(shared_state &st) { st.cond.notify_all(); if(!st.then) return; if(!current) return st.then(st); ircd::post([&st] { st.then(st); }); } template void ircd::ctx::set_observed(shared_state &st) { set_ready(st); } template<> inline void ircd::ctx::set_observed(shared_state &st) { set_retrieved(st); } template void ircd::ctx::set_ready(shared_state &st) { st.p = reinterpret_cast *>(uintptr_t(0x42)); } template void ircd::ctx::set_retrieved(shared_state &st) { st.p = reinterpret_cast *>(uintptr_t(0x84)); } template bool ircd::ctx::ready(const shared_state &st) { return st.p == reinterpret_cast *>(uintptr_t(0x42)); } template bool ircd::ctx::retrieved(const shared_state &st) { return st.p == reinterpret_cast *>(uintptr_t(0x84)); } template bool ircd::ctx::pending(const shared_state &st) { return st.p > reinterpret_cast *>(uintptr_t(0x1000)); } template bool ircd::ctx::invalid(const shared_state &st) { return st.p == nullptr; }