18#include "pw_assert/assert.h"
19#include "pw_async2/future.h"
20#include "pw_polyfill/language_feature_macros.h"
21#include "pw_sync/interrupt_spin_lock.h"
26inline sync::InterruptSpinLock& ValueProviderLock() {
36class BroadcastValueProvider;
52 *
this = std::move(other);
58 std::lock_guard lock(internal::ValueProviderLock());
59 core_ = std::move(other.core_);
60 value_ = std::move(other.value_);
66 std::lock_guard lock(internal::ValueProviderLock());
72 template <
typename... Args>
74 return ValueFuture(std::in_place, std::forward<Args>(args)...);
84 std::lock_guard lock(internal::ValueProviderLock());
88 [[nodiscard]]
bool is_complete()
const {
89 std::lock_guard lock(internal::ValueProviderLock());
94 friend class FutureCore;
95 friend class ValueProvider<T>;
96 friend class BroadcastValueProvider<T>;
98 static constexpr char kWaitReason[] =
"ValueFuture";
100 template <
typename... Args>
101 explicit ValueFuture(std::in_place_t, Args&&... args)
102 : core_(FutureCore::kReadyForCompletion),
103 value_(std::in_place, std::forward<Args>(args)...) {}
107 Poll<T> DoPend(Context&)
113 return Ready(std::move(*value_));
116 template <
typename... Args>
117 void ResolveLocked(Args&&... args)
120 PW_DASSERT(!value_.has_value());
121 value_.emplace(std::forward<Args>(args)...);
125 FutureCore core_
PW_GUARDED_BY(internal::ValueProviderLock());
126 std::optional<T> value_
PW_GUARDED_BY(internal::ValueProviderLock());
141 std::lock_guard lock(internal::ValueProviderLock());
146 std::lock_guard lock(internal::ValueProviderLock());
150 [[nodiscard]]
bool is_complete()
const {
return core_.
is_complete(); }
153 return ValueFuture(FutureCore::kReadyForCompletion);
156 static constexpr char kWaitReason[] =
"ValueFuture<void>";
164 : core_(FutureCore::kReadyForCompletion) {}
202 std::lock_guard lock(internal::ValueProviderLock());
203 list_.Push(future.core_);
209 template <
typename U = T, std::enable_if_t<!std::is_
void_v<U>,
int> = 0>
211 std::lock_guard lock(internal::ValueProviderLock());
212 list_.ResolveAllWith(
218 template <
typename U = T, std::enable_if_t<std::is_
void_v<U>,
int> = 0>
220 std::lock_guard lock(internal::ValueProviderLock());
248 std::lock_guard lock(internal::ValueProviderLock());
249 list_.PushRequireEmpty(future);
261 std::lock_guard lock(internal::ValueProviderLock());
262 if (!list_.PushIfEmpty(future.core_)) {
271 std::lock_guard lock(internal::ValueProviderLock());
272 return !list_.empty();
277 template <
typename... Args,
279 std::enable_if_t<!std::is_void_v<U>,
int> = 0>
281 std::lock_guard lock(internal::ValueProviderLock());
282 if (
ValueFuture<T>* future = list_.PopIfAvailable(); future !=
nullptr) {
283 future->ResolveLocked(std::forward<Args>(args)...);
288 template <
typename U = T, std::enable_if_t<std::is_
void_v<U>,
int> = 0>
290 std::lock_guard lock(internal::ValueProviderLock());
291 list_.ResolveOneIfAvailable();
Definition: value_future.h:190
ValueFuture< T > Get()
Definition: value_future.h:199
void Resolve(const U &value)
Resolves every pending ValueFuture with a copy of the provided value.
Definition: value_future.h:210
void Resolve()
Resolves every pending ValueFuture.
Definition: value_future.h:219
Pending
Tag type to construct an active FutureCore. Pend may be called.
Definition: future.h:453
bool is_ready() const
Definition: future.h:482
void WakeAndMarkReady()
Definition: future.h:490
bool is_complete() const
Definition: future.h:476
ReadyForCompletion
Definition: future.h:450
void Unlist()
Removes this future from its list, if it is in one.
Definition: future.h:496
Definition: value_future.h:132
Definition: value_future.h:46
static ValueFuture Resolved(Args &&... args)
Definition: value_future.h:73
Definition: value_future.h:236
ValueFuture< T > Get()
Definition: value_future.h:245
void Resolve(Args &&... args)
Definition: value_future.h:280
bool has_future() const
Returns true if the provider stores a pending future.
Definition: value_future.h:270
void Resolve()
Resolves the pending ValueFuture.
Definition: value_future.h:289
std::optional< ValueFuture< T > > TryGet()
Definition: value_future.h:258
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:271
constexpr Poll Ready()
Returns a value indicating completion.
Definition: poll.h:255
#define PW_CONSTINIT
Definition: language_feature_macros.h:52
#define PW_GUARDED_BY(x)
Definition: lock_annotations.h:60
#define PW_NO_LOCK_SAFETY_ANALYSIS
Definition: lock_annotations.h:292
#define PW_EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: lock_annotations.h:146
#define PW_LOCKS_EXCLUDED(...)
Definition: lock_annotations.h:176