| 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 |
|
| 15 |
#pragma once |
| 16 |
|
| 17 |
#include <condition_variable> |
| 18 |
#include <memory> |
| 19 |
#include <mutex> |
| 20 |
|
| 21 |
namespace lsp |
| 22 |
{ |
| 23 |
|
| 24 |
// internal functionality |
| 25 |
namespace detail |
| 26 |
{ |
| 27 |
template<typename T> |
| 28 |
struct promise_state |
| 29 |
{ |
| 30 |
T val; |
| 31 |
std::mutex mutex; |
| 32 |
std::condition_variable cv; |
| 33 |
bool hasVal = false; |
| 34 |
}; |
| 35 |
} // namespace detail |
| 36 |
|
| 37 |
// forward declaration |
| 38 |
template<typename T> |
| 39 |
class promise; |
| 40 |
|
| 41 |
// future_status is the enumeration returned by future::wait_for and |
| 42 |
// future::wait_until. |
| 43 |
enum class future_status |
| 44 |
{ |
| 45 |
ready, |
| 46 |
timeout, |
| 47 |
}; |
| 48 |
|
| 49 |
// future is a minimal reimplementation of std::future, that does not suffer |
| 50 |
// from TSAN false positives. See: |
| 51 |
// https://gcc.gnu.org/bugzilla//show_bug.cgi?id=69204 |
| 52 |
template<typename T> |
| 53 |
class future |
| 54 |
{ |
| 55 |
public: |
| 56 |
using State = detail::promise_state<T>; |
| 57 |
|
| 58 |
// constructors |
| 59 |
inline future() = default; |
| 60 |
inline future(future&&) = default; |
| 61 |
|
| 62 |
// valid() returns true if the future has an internal state. |
| 63 |
bool valid() const; |
| 64 |
|
| 65 |
// get() blocks until the future has a valid result, and returns it. |
| 66 |
// The future must have a valid internal state to call this method. |
| 67 |
inline T get(); |
| 68 |
|
| 69 |
// wait() blocks until the future has a valid result. |
| 70 |
// The future must have a valid internal state to call this method. |
| 71 |
void wait() const; |
| 72 |
|
| 73 |
// wait_for() blocks until the future has a valid result, or the timeout is |
| 74 |
// reached. |
| 75 |
// The future must have a valid internal state to call this method. |
| 76 |
template<class Rep, class Period> |
| 77 |
future_status wait_for(std::chrono::duration<Rep, Period> const& timeout) const; |
| 78 |
|
| 79 |
// wait_until() blocks until the future has a valid result, or the timeout is |
| 80 |
// reached. |
| 81 |
// The future must have a valid internal state to call this method. |
| 82 |
template<class Clock, class Duration> |
| 83 |
future_status wait_until(std::chrono::time_point<Clock, Duration> const& timeout) const; |
| 84 |
|
| 85 |
private: |
| 86 |
friend promise<T>; |
| 87 |
future(future const&) = delete; |
| 88 |
inline future(std::shared_ptr<State> const& state); |
| 89 |
|
| 90 |
std::shared_ptr<State> state = std::make_shared<State>(); |
| 91 |
}; |
| 92 |
|
| 93 |
template<typename T> |
| 94 |
future<T>::future(std::shared_ptr<State> const& s) : state(s) |
| 95 |
{ |
| 96 |
} |
| 97 |
|
| 98 |
template<typename T> |
| 99 |
bool future<T>::valid() const |
| 100 |
{ |
| 101 |
return static_cast<bool>(state); |
| 102 |
} |
| 103 |
|
| 104 |
template<typename T> |
| 105 |
T future<T>::get() |
| 106 |
{ |
| 107 |
std::unique_lock<std::mutex> lock(state->mutex); |
| 108 |
state->cv.wait(lock, [&] { return state->hasVal; }); |
| 109 |
return state->val; |
| 110 |
} |
| 111 |
|
| 112 |
template<typename T> |
| 113 |
void future<T>::wait() const |
| 114 |
{ |
| 115 |
std::unique_lock<std::mutex> lock(state->mutex); |
| 116 |
state->cv.wait(lock, [&] { return state->hasVal; }); |
| 117 |
} |
| 118 |
|
| 119 |
template<typename T> |
| 120 |
template<class Rep, class Period> |
| 121 |
future_status future<T>::wait_for(std::chrono::duration<Rep, Period> const& timeout) const |
| 122 |
{ |
| 123 |
std::unique_lock<std::mutex> lock(state->mutex); |
| 124 |
return state->cv.wait_for(lock, timeout, [&] { return state->hasVal; }) ? future_status::ready |
| 125 |
: future_status::timeout; |
| 126 |
} |
| 127 |
|
| 128 |
template<typename T> |
| 129 |
template<class Clock, class Duration> |
| 130 |
future_status future<T>::wait_until(std::chrono::time_point<Clock, Duration> const& timeout) const |
| 131 |
{ |
| 132 |
std::unique_lock<std::mutex> lock(state->mutex); |
| 133 |
return state->cv.wait_until(lock, timeout, [&] { return state->hasVal; }) ? future_status::ready |
| 134 |
: future_status::timeout; |
| 135 |
} |
| 136 |
|
| 137 |
// promise is a minimal reimplementation of std::promise, that does not suffer |
| 138 |
// from TSAN false positives. See: |
| 139 |
// https://gcc.gnu.org/bugzilla//show_bug.cgi?id=69204 |
| 140 |
template<typename T> |
| 141 |
class promise |
| 142 |
{ |
| 143 |
public: |
| 144 |
// constructors |
| 145 |
inline promise() = default; |
| 146 |
inline promise(promise&& other) = default; |
| 147 |
inline promise(promise const& other) = default; |
| 148 |
|
| 149 |
// set_value() stores value to the shared state. |
| 150 |
// set_value() must only be called once. |
| 151 |
inline void set_value(T const& value) const; |
| 152 |
inline void set_value(T&& value) const; |
| 153 |
|
| 154 |
// get_future() returns a future sharing this promise's state. |
| 155 |
future<T> get_future(); |
| 156 |
|
| 157 |
private: |
| 158 |
using State = detail::promise_state<T>; |
| 159 |
std::shared_ptr<State> state = std::make_shared<State>(); |
| 160 |
}; |
| 161 |
|
| 162 |
template<typename T> |
| 163 |
future<T> promise<T>::get_future() |
| 164 |
{ |
| 165 |
return future<T>(state); |
| 166 |
} |
| 167 |
|
| 168 |
template<typename T> |
| 169 |
void promise<T>::set_value(T const& value) const |
| 170 |
{ |
| 171 |
std::unique_lock<std::mutex> lock(state->mutex); |
| 172 |
state->val = value; |
| 173 |
state->hasVal = true; |
| 174 |
state->cv.notify_all(); |
| 175 |
} |
| 176 |
|
| 177 |
template<typename T> |
| 178 |
void promise<T>::set_value(T&& value) const |
| 179 |
{ |
| 180 |
std::unique_lock<std::mutex> lock(state->mutex); |
| 181 |
state->val = std::move(value); |
| 182 |
state->hasVal = true; |
| 183 |
state->cv.notify_all(); |
| 184 |
} |
| 185 |
|
| 186 |
} // namespace lsp |