future.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef dap_future_h
  15. #define dap_future_h
  16. #include <condition_variable>
  17. #include <memory>
  18. #include <mutex>
  19. namespace dap {
  20. // internal functionality
  21. namespace detail {
  22. template <typename T>
  23. struct promise_state {
  24. T val;
  25. std::mutex mutex;
  26. std::condition_variable cv;
  27. bool hasVal = false;
  28. };
  29. } // namespace detail
  30. // forward declaration
  31. template <typename T>
  32. class promise;
  33. // future_status is the enumeration returned by future::wait_for and
  34. // future::wait_until.
  35. enum class future_status {
  36. ready,
  37. timeout,
  38. };
  39. // future is a minimal reimplementation of std::future, that does not suffer
  40. // from TSAN false positives. See:
  41. // https://gcc.gnu.org/bugzilla//show_bug.cgi?id=69204
  42. template <typename T>
  43. class future {
  44. public:
  45. using State = detail::promise_state<T>;
  46. // constructors
  47. inline future() = default;
  48. inline future(future&&) = default;
  49. // valid() returns true if the future has an internal state.
  50. bool valid() const;
  51. // get() blocks until the future has a valid result, and returns it.
  52. // The future must have a valid internal state to call this method.
  53. inline T get();
  54. // wait() blocks until the future has a valid result.
  55. // The future must have a valid internal state to call this method.
  56. void wait() const;
  57. // wait_for() blocks until the future has a valid result, or the timeout is
  58. // reached.
  59. // The future must have a valid internal state to call this method.
  60. template <class Rep, class Period>
  61. future_status wait_for(
  62. const std::chrono::duration<Rep, Period>& timeout) const;
  63. // wait_until() blocks until the future has a valid result, or the timeout is
  64. // reached.
  65. // The future must have a valid internal state to call this method.
  66. template <class Clock, class Duration>
  67. future_status wait_until(
  68. const std::chrono::time_point<Clock, Duration>& timeout) const;
  69. private:
  70. friend promise<T>;
  71. future(const future&) = delete;
  72. inline future(const std::shared_ptr<State>& state);
  73. std::shared_ptr<State> state = std::make_shared<State>();
  74. };
  75. template <typename T>
  76. future<T>::future(const std::shared_ptr<State>& s) : state(s) {}
  77. template <typename T>
  78. bool future<T>::valid() const {
  79. return static_cast<bool>(state);
  80. }
  81. template <typename T>
  82. T future<T>::get() {
  83. std::unique_lock<std::mutex> lock(state->mutex);
  84. state->cv.wait(lock, [&] { return state->hasVal; });
  85. return state->val;
  86. }
  87. template <typename T>
  88. void future<T>::wait() const {
  89. std::unique_lock<std::mutex> lock(state->mutex);
  90. state->cv.wait(lock, [&] { return state->hasVal; });
  91. }
  92. template <typename T>
  93. template <class Rep, class Period>
  94. future_status future<T>::wait_for(
  95. const std::chrono::duration<Rep, Period>& timeout) const {
  96. std::unique_lock<std::mutex> lock(state->mutex);
  97. return state->cv.wait_for(lock, timeout, [&] { return state->hasVal; })
  98. ? future_status::ready
  99. : future_status::timeout;
  100. }
  101. template <typename T>
  102. template <class Clock, class Duration>
  103. future_status future<T>::wait_until(
  104. const std::chrono::time_point<Clock, Duration>& timeout) const {
  105. std::unique_lock<std::mutex> lock(state->mutex);
  106. return state->cv.wait_until(lock, timeout, [&] { return state->hasVal; })
  107. ? future_status::ready
  108. : future_status::timeout;
  109. }
  110. // promise is a minimal reimplementation of std::promise, that does not suffer
  111. // from TSAN false positives. See:
  112. // https://gcc.gnu.org/bugzilla//show_bug.cgi?id=69204
  113. template <typename T>
  114. class promise {
  115. public:
  116. // constructors
  117. inline promise() = default;
  118. inline promise(promise&& other) = default;
  119. inline promise(const promise& other) = default;
  120. // set_value() stores value to the shared state.
  121. // set_value() must only be called once.
  122. inline void set_value(const T& value) const;
  123. inline void set_value(T&& value) const;
  124. // get_future() returns a future sharing this promise's state.
  125. future<T> get_future();
  126. private:
  127. using State = detail::promise_state<T>;
  128. std::shared_ptr<State> state = std::make_shared<State>();
  129. };
  130. template <typename T>
  131. future<T> promise<T>::get_future() {
  132. return future<T>(state);
  133. }
  134. template <typename T>
  135. void promise<T>::set_value(const T& value) const {
  136. std::unique_lock<std::mutex> lock(state->mutex);
  137. state->val = value;
  138. state->hasVal = true;
  139. state->cv.notify_all();
  140. }
  141. template <typename T>
  142. void promise<T>::set_value(T&& value) const {
  143. std::unique_lock<std::mutex> lock(state->mutex);
  144. state->val = std::move(value);
  145. state->hasVal = true;
  146. state->cv.notify_all();
  147. }
  148. } // namespace dap
  149. #endif // dap_future_h