Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
bounded_channel.h
Go to the documentation of this file.
1// Copyright 2026 The Action Engine Authors and the Symbian SDK 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// SDK-owned fallible bounded queue, separate from A11
16// cpp/thread/thread/channel.h at fcccb8cb6e1e67d7ba0822ac14cece9ee4c7091b.
17// This queue serves nonblocking SDK mailboxes. It does not change the
18// signatures or closed-write contract of thread::Channel.
19
20#ifndef SYMBIAN_CONCURRENCY_BOUNDED_CHANNEL_H_
21#define SYMBIAN_CONCURRENCY_BOUNDED_CHANNEL_H_
22
23#include <cstddef>
24#include <deque>
25#include <type_traits>
26#include <utility>
27
28#include <absl/status/status_macros.h>
29#include <absl/base/nullability.h>
30
31#include "absl/status/status.h"
32#include "absl/status/statusor.h"
33#include "thread/boost_primitives.h"
34
36
37template <typename T>
38class BoundedChannel;
39
40template <typename T>
42 public:
43 explicit BoundedReader(BoundedChannel<T>* absl_nonnull channel)
44 : channel_(channel) {}
45
46 bool Read(T* absl_nonnull out) { return channel_->Read(out); }
47
48 absl::StatusOr<bool> TryRead(T* absl_nonnull out) {
49 return channel_->TryRead(out);
50 }
51
52 private:
53 BoundedChannel<T>* absl_nonnull channel_;
54};
55
56template <typename T>
58 public:
59 explicit BoundedWriter(BoundedChannel<T>* absl_nonnull channel)
60 : channel_(channel) {}
61
62 absl::Status Write(T&& item) { return channel_->Write(std::move(item)); }
63
64 absl::Status TryWrite(T&& item) {
65 return channel_->TryWrite(std::move(item));
66 }
67
68 absl::Status Write(const T& item) requires std::is_copy_constructible_v<T> {
69 return channel_->Write(item);
70 }
71
72 absl::Status TryWrite(
73 const T& item) requires std::is_copy_constructible_v<T> {
74 return channel_->TryWrite(item);
75 }
76
77 void Close() { channel_->Close(); }
78
79 private:
80 BoundedChannel<T>* absl_nonnull channel_;
81};
82
83// A bounded multi-producer, multi-consumer FIFO. Capacity must be positive.
84// Blocking calls park according to the selected thread primitive backend:
85// Boost fibers on hosts, guest ARM fibers on the event thread, or an OS thread
86// outside a fiber. A failed write
87// preserves an rvalue source. Closed writes return Status under the default
88// no-exceptions profile.
89template <typename T>
91 static_assert(std::is_move_assignable_v<T>);
92
93 public:
94 explicit BoundedChannel(std::size_t capacity)
95 : capacity_(capacity), reader_(this), writer_(this) {}
96
99
100 BoundedReader<T>* absl_nonnull reader() { return &reader_; }
101
102 BoundedWriter<T>* absl_nonnull writer() { return &writer_; }
103
104 absl::Status TryWrite(T&& item) { return TryWriteMoved(&item); }
105
106 absl::Status TryWrite(
107 const T& item) requires std::is_copy_constructible_v<T> {
108 {
109 thread::MutexLock lock(&mu_);
110 ABSL_RETURN_IF_ERROR(CheckWritable());
111 queue_.push_back(item);
112 }
113 readers_.Signal();
114 return absl::OkStatus();
115 }
116
117 absl::Status Write(T&& item) { return WriteMoved(&item); }
118
119 absl::Status Write(const T& item) requires std::is_copy_constructible_v<T> {
120 {
121 thread::MutexLock lock(&mu_);
122 if (capacity_ == 0) {
123 return absl::InvalidArgumentError(
124 "BoundedChannel capacity must be positive");
125 }
126 while (!closed_ && queue_.size() >= capacity_) {
127 writers_.Wait(&mu_);
128 }
129 if (closed_) {
130 return absl::FailedPreconditionError("BoundedChannel is closed");
131 }
132 queue_.push_back(item);
133 }
134 readers_.Signal();
135 return absl::OkStatus();
136 }
137
138 // True means one item was read; false means closed and drained. An open,
139 // empty channel returns Unavailable so pollers can distinguish the states.
140 absl::StatusOr<bool> TryRead(T* absl_nonnull out) {
141 {
142 thread::MutexLock lock(&mu_);
143 if (queue_.empty()) {
144 if (closed_) {
145 return false;
146 }
147 return absl::UnavailableError("BoundedChannel is empty");
148 }
149 *out = std::move(queue_.front());
150 queue_.pop_front();
151 }
152 writers_.Signal();
153 return true;
154 }
155
156 bool Read(T* absl_nonnull out) {
157 {
158 thread::MutexLock lock(&mu_);
159 while (!closed_ && queue_.empty()) {
160 readers_.Wait(&mu_);
161 }
162 if (queue_.empty()) {
163 return false;
164 }
165 *out = std::move(queue_.front());
166 queue_.pop_front();
167 }
168 writers_.Signal();
169 return true;
170 }
171
172 // Close rejects writes, wakes blocked readers/writers, and permits queued
173 // items to drain. Repeating Close is harmless for owner shutdown.
174 void Close() {
175 {
176 thread::MutexLock lock(&mu_);
177 closed_ = true;
178 }
179 readers_.SignalAll();
180 writers_.SignalAll();
181 }
182
183 // Discard closes and releases queued values outside the queue lock.
184 void Discard() {
185 std::deque<T> retired;
186 {
187 thread::MutexLock lock(&mu_);
188 closed_ = true;
189 retired.swap(queue_);
190 }
191 readers_.SignalAll();
192 writers_.SignalAll();
193 }
194
195 std::size_t Size() const {
196 thread::MutexLock lock(&mu_);
197 return queue_.size();
198 }
199
200 private:
201 absl::Status CheckWritable() const {
202 if (closed_) {
203 return absl::FailedPreconditionError("BoundedChannel is closed");
204 }
205 if (capacity_ == 0) {
206 return absl::InvalidArgumentError(
207 "BoundedChannel capacity must be positive");
208 }
209 if (queue_.size() >= capacity_) {
210 return absl::ResourceExhaustedError("BoundedChannel is full");
211 }
212 return absl::OkStatus();
213 }
214
215 absl::Status TryWriteMoved(T* absl_nonnull item) {
216 {
217 thread::MutexLock lock(&mu_);
218 ABSL_RETURN_IF_ERROR(CheckWritable());
219 queue_.push_back(std::move(*item));
220 }
221 readers_.Signal();
222 return absl::OkStatus();
223 }
224
225 absl::Status WriteMoved(T* absl_nonnull item) {
226 {
227 thread::MutexLock lock(&mu_);
228 if (capacity_ == 0) {
229 return absl::InvalidArgumentError(
230 "BoundedChannel capacity must be positive");
231 }
232 while (!closed_ && queue_.size() >= capacity_) {
233 writers_.Wait(&mu_);
234 }
235 if (closed_) {
236 return absl::FailedPreconditionError("BoundedChannel is closed");
237 }
238 queue_.push_back(std::move(*item));
239 }
240 readers_.Signal();
241 return absl::OkStatus();
242 }
243
244 const std::size_t capacity_;
245 mutable thread::Mutex mu_;
246 thread::CondVar readers_;
247 thread::CondVar writers_;
248 std::deque<T> queue_;
249 bool closed_ = false;
250 BoundedReader<T> reader_;
251 BoundedWriter<T> writer_;
252};
253
254} // namespace symbian::concurrency
255
256#endif // SYMBIAN_CONCURRENCY_BOUNDED_CHANNEL_H_
Definition bounded_channel.h:90
bool Read(T *absl_nonnull out)
Definition bounded_channel.h:156
BoundedChannel(std::size_t capacity)
Definition bounded_channel.h:94
BoundedChannel(const BoundedChannel &)=delete
BoundedReader< T > *absl_nonnull reader()
Definition bounded_channel.h:100
void Close()
Definition bounded_channel.h:174
absl::Status Write(const T &item)
Definition bounded_channel.h:119
absl::StatusOr< bool > TryRead(T *absl_nonnull out)
Definition bounded_channel.h:140
BoundedWriter< T > *absl_nonnull writer()
Definition bounded_channel.h:102
BoundedChannel & operator=(const BoundedChannel &)=delete
absl::Status TryWrite(T &&item)
Definition bounded_channel.h:104
std::size_t Size() const
Definition bounded_channel.h:195
absl::Status Write(T &&item)
Definition bounded_channel.h:117
void Discard()
Definition bounded_channel.h:184
absl::Status TryWrite(const T &item)
Definition bounded_channel.h:106
Definition bounded_channel.h:41
bool Read(T *absl_nonnull out)
Definition bounded_channel.h:46
absl::StatusOr< bool > TryRead(T *absl_nonnull out)
Definition bounded_channel.h:48
BoundedReader(BoundedChannel< T > *absl_nonnull channel)
Definition bounded_channel.h:43
Definition bounded_channel.h:57
BoundedWriter(BoundedChannel< T > *absl_nonnull channel)
Definition bounded_channel.h:59
absl::Status TryWrite(T &&item)
Definition bounded_channel.h:64
absl::Status Write(T &&item)
Definition bounded_channel.h:62
absl::Status TryWrite(const T &item)
Definition bounded_channel.h:72
void Close()
Definition bounded_channel.h:77
absl::Status Write(const T &item)
Definition bounded_channel.h:68
Definition boost_primitives.h:113
void Signal() noexcept
Definition boost_primitives.h:214
void Wait(Mutex *absl_nonnull mu) noexcept
Definition boost_primitives.h:119
void SignalAll() noexcept
Definition boost_primitives.h:234
Definition boost_primitives.h:97
Definition boost_primitives.h:36
Definition bounded_channel.h:35
uint8_t out
Definition usb.cc:198