C/C++ API Reference
Loading...
Searching...
No Matches
coro_task.h
1// Copyright 2026 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// 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, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <optional>
17
18#include "pw_async2/coro.h"
19#include "pw_async2/func_task.h"
20#include "pw_async2/task.h"
21
22namespace pw::async2 {
23
25
29template <typename T,
30 ReturnValuePolicy policy = std::is_void_v<T>
31 ? ReturnValuePolicy::kDiscard
32 : ReturnValuePolicy::kKeep>
33class CoroTask final : public Task {
34 public:
35 using value_type = T;
36
40 : Task(PW_ASYNC_TASK_NAME("CoroTask<T>")),
41 coro_(std::move(coro)),
42 return_value_(internal::CoroPollState::kPending) {}
43
44 CoroTask(const CoroTask&) = delete;
45 CoroTask& operator=(const CoroTask&) = delete;
46 CoroTask(CoroTask&&) = delete;
47 CoroTask& operator=(CoroTask&&) = delete;
48
49 ~CoroTask() override { Deregister(); }
50
55 [[nodiscard]] bool ok() const { return coro_.ok(); }
56
59 bool has_value() const { return return_value_.has_value(); }
60
64 value_type& value() { return return_value_.value(); }
65
67 const value_type& value() const { return return_value_.value(); }
68
70 value_type& Wait() {
71 Task::Join();
72 return *return_value_;
73 }
74
75 private:
76 Poll<> DoPend(Context& cx) final {
77 // Coro::Pend() asserts if allocation failed (!coro_.ok()).
78 return_value_ = coro_.Pend(cx);
79 switch (return_value_.state()) {
80 case internal::CoroPollState::kPending:
81 return Pending();
82 case internal::CoroPollState::kAborted:
83 internal::CrashDueToCoroutineAllocationFailure();
84 case internal::CoroPollState::kReady:
85 return Ready();
86 }
87 }
88
89 Coro<T> coro_;
91};
92
94template <typename T>
95class CoroTask<T, ReturnValuePolicy::kDiscard> final : public Task {
96 public:
102 : Task(PW_ASYNC_TASK_NAME("CoroTask")), coro_(std::move(coro)) {}
103
104 CoroTask(const CoroTask&) = delete;
105 CoroTask& operator=(const CoroTask&) = delete;
106 CoroTask(CoroTask&&) = delete;
107 CoroTask& operator=(CoroTask&&) = delete;
108
109 ~CoroTask() override { Deregister(); }
110
115 [[nodiscard]] bool ok() const { return coro_.ok(); }
116
117 private:
118 Poll<> DoPend(Context& cx) final {
119 // Coro::Pend() asserts if allocation failed (!coro_.ok()).
120 switch (coro_.Pend(cx).state()) {
121 case internal::CoroPollState::kPending:
122 return Pending();
123 case internal::CoroPollState::kAborted:
124 internal::CrashDueToCoroutineAllocationFailure();
125 case internal::CoroPollState::kReady:
126 return Ready();
127 }
128 }
129
130 Coro<T> coro_;
131};
132
133template <typename T>
134CoroTask(Coro<T>&&) -> CoroTask<T>;
135
137
138} // namespace pw::async2
Definition: context.h:46
Definition: coro.h:546
Poll DoPend(Context &cx) final
Definition: coro_task.h:118
CoroTask(Coro< T > &&coro)
Definition: coro_task.h:101
bool ok() const
Definition: coro_task.h:115
Definition: coro_task.h:33
value_type & value()
Definition: coro_task.h:64
const value_type & value() const
Definition: coro_task.h:67
bool ok() const
Definition: coro_task.h:55
value_type & Wait()
Blocks until the task completes and returns a reference its return value.
Definition: coro_task.h:70
bool has_value() const
Definition: coro_task.h:59
CoroTask(Coro< T > &&coro)
Definition: coro_task.h:39
Poll DoPend(Context &cx) final
Definition: coro_task.h:76
Definition: poll.h:138
Definition: task.h:78
Definition: optional.h:65
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:353
constexpr Poll Ready()
Returns a value indicating completion.
Definition: poll.h:337
ReturnValuePolicy
Whether to store or discard the function's return value in RunOnceTask.
Definition: func_task.h:62
#define PW_ASYNC_TASK_NAME(name)
Generates a token for use as a task name.
Definition: task.h:32