CatapultServer  v0.5.0.1 (Elephant)
FutureSharedState.h
Go to the documentation of this file.
1 
21 #pragma once
23 #include "catapult/functions.h"
24 #include <future>
25 #include <memory>
26 #include <mutex>
27 
28 namespace catapult { namespace thread { namespace detail {
29 
31  template<typename T>
33  private:
35 
36  enum class future_status {
37  pending,
40  };
41 
42  public:
45  {}
46 
47  public:
49  bool is_ready() const {
51  }
52 
54  T get() {
56  std::unique_lock<std::mutex> lock(m_mutex);
57  m_condition.wait(lock, [&status = m_status]() { return status != future_status::pending; });
58  }
59 
61  std::rethrow_exception(m_pException);
62 
63  return std::move(m_value);
64  }
65 
66  public:
68  void set_value(T&& value) {
69  std::lock_guard<std::mutex> lock(m_mutex);
71  m_value = std::move(value);
73  }
74 
76  void set_exception(std::exception_ptr pException) {
77  std::lock_guard<std::mutex> lock(m_mutex);
79  m_pException = pException;
81  }
82 
84  void set_continuation(const ContinuationFunc& continuation) {
85  std::lock_guard<std::mutex> lock(m_mutex);
86  if (m_continuation)
87  throw std::logic_error("continuation already set");
88 
89  m_continuation = continuation;
90  if (is_ready())
92  }
93 
94  private:
95  void assert_pending() const {
96  if (is_ready())
97  throw std::future_error(std::future_errc::promise_already_satisfied);
98  }
99 
100  void signal(future_status status) {
101  m_status = status;
102  m_condition.notify_all();
103  if (!m_continuation)
104  return;
105 
107  }
108 
110  auto pStateCopy = std::make_shared<shared_state<T>>();
111  pStateCopy->m_status = m_status.load();
112  pStateCopy->m_value = std::move(m_value);
113  pStateCopy->m_pException = m_pException;
114  m_continuation(pStateCopy);
115  }
116 
117  private:
118  std::atomic<future_status> m_status;
120  std::exception_ptr m_pException;
122 
123  std::condition_variable m_condition;
124  std::mutex m_mutex;
125  };
126 }}}
catapult::thread::detail::shared_state
Shared state that is shared between a promise and a future.
Definition: FutureSharedState.h:32
catapult::thread::detail::shared_state::set_continuation
void set_continuation(const ContinuationFunc &continuation)
Configures continuation to run at the completion of this shared state.
Definition: FutureSharedState.h:84
catapult::thread::detail::shared_state::m_pException
std::exception_ptr m_pException
Definition: FutureSharedState.h:120
catapult::thread::detail::shared_state::shared_state
shared_state()
Creates an incomplete shared state.
Definition: FutureSharedState.h:44
catapult::thread::detail::shared_state::future_status
future_status
Definition: FutureSharedState.h:36
functions.h
catapult::thread::detail::shared_state::m_mutex
std::mutex m_mutex
Definition: FutureSharedState.h:124
catapult::thread::detail::shared_state::m_condition
std::condition_variable m_condition
Definition: FutureSharedState.h:123
catapult::thread::detail::shared_state::future_status::completed_error
catapult::thread::detail::shared_state::future_status::completed_success
catapult::thread::detail::shared_state::ContinuationFunc
consumer< const std::shared_ptr< shared_state< T > >> ContinuationFunc
Definition: FutureSharedState.h:34
catapult::thread::detail::shared_state::set_exception
void set_exception(std::exception_ptr pException)
Sets the result of this shared state to pException.
Definition: FutureSharedState.h:76
catapult::thread::detail::shared_state::is_ready
bool is_ready() const
Returns true if this shared state has completed and get will not block.
Definition: FutureSharedState.h:49
catapult::thread::detail::shared_state::signal
void signal(future_status status)
Definition: FutureSharedState.h:100
catapult::thread::detail::shared_state::invokeContinuation
void invokeContinuation()
Definition: FutureSharedState.h:109
catapult::thread::detail::shared_state::future_status::pending
catapult::thread::detail::shared_state::m_status
std::atomic< future_status > m_status
Definition: FutureSharedState.h:118
catapult::thread::detail::shared_state::get
T get()
Returns the result of this shared state and blocks until the result is available.
Definition: FutureSharedState.h:54
catapult::thread::detail::shared_state::set_value
void set_value(T &&value)
Sets the result of this shared state to value.
Definition: FutureSharedState.h:68
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::thread::detail::shared_state::m_continuation
ContinuationFunc m_continuation
Definition: FutureSharedState.h:121
catapult::utils::NonCopyable
A class that can neither be copied nor moved.
Definition: NonCopyable.h:26
catapult::consumer
std::function< void(TArgs...)> consumer
A consumer function.
Definition: functions.h:35
catapult::thread::detail::shared_state::m_value
T m_value
Definition: FutureSharedState.h:119
catapult::thread::detail::shared_state::assert_pending
void assert_pending() const
Definition: FutureSharedState.h:95
NonCopyable.h