Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
cases.h
Go to the documentation of this file.
1// Copyright 2026 The Action Engine Authors.
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// http://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#ifndef THREAD_FIBER_CASES_H_
16#define THREAD_FIBER_CASES_H_
17
18#include <cstddef>
19#include <type_traits>
20
21#include <absl/base/nullability.h>
22#include <absl/container/inlined_vector.h>
23#include <absl/log/check.h>
24#include <absl/log/log.h>
25
26#include "thread/boost_primitives.h"
27
28namespace thread {
29namespace internal {
30template <typename T>
31concept IsPointer = std::is_pointer_v<T>;
32
33template <typename T>
34concept IsNonConstPointer =
35 IsPointer<T> && !std::is_const_v<std::remove_pointer_t<T>>;
36
37template <typename T>
38concept IsConstPointer =
39 IsPointer<T> && std::is_const_v<std::remove_pointer_t<T>>;
40
41struct Selector {
42 static constexpr int kNonePicked = -1;
43
44 bool TryPick(int case_index) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu) {
45 if (picked_case_index != kNonePicked) {
46 return false; // Already picked.
47 }
48
49 picked_case_index = case_index;
50 cv.Signal();
51 return true;
52 }
53
54 // Returns true iff a case was picked before the deadline.
55 bool WaitForPickUntil(absl::Time deadline) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu) {
56 while (picked_case_index == internal::Selector::kNonePicked) {
57 if (cv.WaitWithDeadline(&mu, deadline) &&
58 picked_case_index == internal::Selector::kNonePicked) {
59 return false;
60 }
61 }
62 return true;
63 }
64
65 // kNonePicked until a case is picked, or the index of picked case
67 int picked_case_index ABSL_GUARDED_BY(mu) = kNonePicked;
68 thread::CondVar cv ABSL_GUARDED_BY(mu);
69};
70
71class Selectable;
72} // namespace internal
73
83struct [[nodiscard]] Case {
84 internal::Selectable* absl_nonnull selectable;
85 absl::InlinedVector<void* absl_nullable, 2> arguments;
86
87 Case(internal::Selectable* absl_nonnull s) : selectable(s) {}
88
89 Case(internal::Selectable* absl_nullable s, internal::IsPointer auto... args)
90 : selectable(s) {
91 (arguments.push_back(const_cast<void*>(static_cast<const void*>(args))),
92 ...);
93 }
94
95 Case(const Case&) = default;
96 Case& operator=(const Case&) = default;
97
98 // // Disallow casting from bool
99 // // ReSharper disable once CppNonExplicitConvertingConstructor
100 template <typename... Args>
101 Case(bool, Args... args) = delete;
102
103 template <typename T>
104 void AddArg(const T* absl_nonnull arg) {
105 arguments.push_back(const_cast<void*>(static_cast<const void*>(arg)));
106 }
107
108 template <typename T>
109 void AddArg(T* absl_nonnull arg) {
110 arguments.push_back(static_cast<void*>(arg));
111 }
112
113 [[nodiscard]] void* absl_nullable GetArgPtr(int index) const {
114 if (index < 0 || static_cast<size_t>(index) >= arguments.size()) {
115 LOG(FATAL) << "Case::GetArgOrDie: index out of bounds: " << index
116 << ", arguments.size() = " << arguments.size();
117 ABSL_ASSUME(false);
118 }
119 return arguments[static_cast<size_t>(index)];
120 }
121
122 template <typename T>
123 [[nodiscard]] T* absl_nullable GetArgPtr(int index) const {
124 return static_cast<T*>(GetArgPtr(index));
125 }
126
127 [[nodiscard]] size_t GetNumArgs() const { return arguments.size(); }
128};
129
130// An array of cases; the type supplied to Select. Must be initializer list
131// compatible.
132typedef absl::InlinedVector<Case, 4> CaseArray;
133
134namespace internal {
135struct CaseInSelectClause {
136 const Case* absl_nullable case_ptr = nullptr; // Initialized by Select().
137 int index = -1; // Provided by Select(): index in parameter list.
138 internal::Selector* absl_nonnull selector =
139 nullptr; // Provided by Select(): owning selector.
140 CaseInSelectClause* absl_nullable prev =
141 nullptr; // Initialized by Select(), nullptr -> not on list.
142 CaseInSelectClause* absl_nullable next = nullptr;
143
144 [[nodiscard]] const Case* absl_nullable GetCase() const { return case_ptr; }
145
146 // Attempt to cause the owning Selector to choose this case.
147 bool TryPick() ABSL_EXCLUSIVE_LOCKS_REQUIRED(selector->mu) {
148 return selector->TryPick(index);
149 }
150
151 bool Handle(bool enqueue);
152 void Unregister();
153};
154
155using CaseStateArray = absl::InlinedVector<CaseInSelectClause, 4>;
156
157// The interface implemented by objects that can be used with Select().
158class Selectable {
159 public:
160 virtual ~Selectable() = default;
161
162 // If this selectable is ready to be picked up by c's Select, call
163 // c->TryPick() (which may or may not pick this selectable), and return true.
164 virtual bool Handle(CaseInSelectClause* absl_nonnull case_state,
165 bool enqueue) = 0;
166
167 // Unregister a case against future transitions for this Selectable.
168 virtual void Unregister(CaseInSelectClause* absl_nonnull case_state) = 0;
169};
170
171inline bool CaseInSelectClause::Handle(bool enqueue) {
172 return GetCase()->selectable->Handle(this, enqueue);
173}
174
175inline void CaseInSelectClause::Unregister() {
176 GetCase()->selectable->Unregister(this);
177}
178
179// Shared linked list code. Implements a doubly-linked list where the list head
180// is a pointer to the oldest element added to the list.
181inline void PushBack(CaseInSelectClause* absl_nonnull* absl_nonnull head,
182 CaseInSelectClause* absl_nonnull element) {
183 CHECK_EQ(element->prev, nullptr)
184 << "Attempted to enqueue a CaseInSelectClause that is already on a list";
185
186 if (*head == nullptr) {
187 // Queue is empty; make singleton queue.
188 element->next = element;
189 element->prev = element;
190 *head = element;
191 } else {
192 CHECK_NE((*head)->prev, nullptr)
193 << "Waiter list head is corrupt: non-null head has null prev";
194
195 // Add just before the oldest element (*head).
196 element->next = *head;
197 element->prev = element->next->prev;
198 element->prev->next = element;
199 element->next->prev = element;
200 }
201}
202
203inline void UnlinkFromList(CaseInSelectClause* absl_nonnull* absl_nonnull head,
204 CaseInSelectClause* absl_nonnull element) {
205 if (element->next == element) {
206 // Single entry; clear list
207 *head = nullptr;
208 } else {
209 // Remove from list
210 element->next->prev = element->prev;
211 element->prev->next = element->next;
212 if (*head == element) {
213 *head = element->next;
214 }
215 }
216 // Maintaining this state in "prev" allows the safe removal of the current
217 // element while iterating forwards.
218 element->prev = nullptr;
219}
220} // namespace internal
221} // namespace thread
222
223#endif // THREAD_FIBER_CASES_H_
Definition boost_primitives.h:113
Definition boost_primitives.h:36
void UnlinkFromList(CaseInSelectClause *absl_nullable *absl_nullable head, CaseInSelectClause *absl_nonnull element)
Definition cases.h:155
static bool reader selector mu
Definition channel_waiter_state.cc:62
absl::InlinedVector< CaseInSelectClause, 4 > CaseStateArray
Definition cases.h:120
void PushBack(CaseInSelectClause *absl_nullable *absl_nullable head, CaseInSelectClause *absl_nonnull element)
Definition cases.h:138
Definition channel.h:29
absl::InlinedVector< Case, 4 > CaseArray
Definition cases.h:102
A Case represents a selectable case in a Select statement.
Definition cases.h:57
Case(internal::Selectable *absl_nullable s, internal::IsPointer auto... args)
Definition cases.h:89
Case(internal::Selectable *absl_nonnull s)
Definition cases.h:87
void AddArg(T *absl_nonnull arg)
Definition cases.h:109
size_t GetNumArgs() const
Definition cases.h:127
void AddArg(const T *absl_nonnull arg)
Definition cases.h:104
Case(const Case &)=default
void *absl_nullable GetArgPtr(int index) const
Definition cases.h:113
Case & operator=(const Case &)=default
Case(bool, Args... args)=delete
T *absl_nullable GetArgPtr(int index) const
Definition cases.h:123