Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
boost_primitives.h
Go to the documentation of this file.
1// Copyright 2026 The Action Engine Authors.
2// Licensed under the Apache License, Version 2.0 (the "License");
3// http://www.apache.org/licenses/LICENSE-2.0
4//
5// Guest ARM-fiber adaptation of cpp/thread/thread/boost_primitives.h at
6// fcccb8cb6e1e67d7ba0822ac14cece9ee4c7091b. Only the creating event
7// thread pumps guest fibers. OS-thread callers retain normal blocking locks.
8
9#ifndef THREAD_BOOST_PRIMITIVES_H_
10#define THREAD_BOOST_PRIMITIVES_H_
11
12#include <algorithm>
13#include <atomic>
14#include <chrono>
15#include <condition_variable>
16#include <cstdint>
17#include <deque>
18#include <mutex>
19#include <thread>
20
21#include <absl/base/nullability.h>
22
23#include "absl/base/thread_annotations.h"
24#include "absl/time/clock.h"
25#include "absl/time/time.h"
26#include "thread/fiber.h"
27
28namespace thread {
29
30inline constexpr bool kCanParkAwait = true;
31
32inline bool CanParkAwait() noexcept {
33 return Fiber::Current() != nullptr;
34}
35
36class ABSL_LOCKABLE Mutex {
37 public:
38 Mutex() = default;
39 Mutex(const Mutex&) = delete;
40 Mutex& operator=(const Mutex&) = delete;
41
42 void Lock() noexcept ABSL_EXCLUSIVE_LOCK_FUNCTION() {
43 Fiber* absl_nullable fiber = Fiber::Current();
44 if (fiber == nullptr) {
45 mu_.lock();
46 return;
47 }
48 while (!mu_.try_lock()) {
49 fiber->scheduler_.PreparePark(
50 fiber, std::chrono::steady_clock::time_point::max());
51 {
52 std::lock_guard guard(waiters_mu_);
53 waiters_.push_back(fiber);
54 // Unlock may have preceded registration. Retry under the waiter
55 // guard so the fiber cannot miss the only wakeup.
56 if (mu_.try_lock()) {
57 waiters_.pop_back();
58 fiber->scheduler_.CancelPark(fiber);
59 return;
60 }
61 }
62 fiber->scheduler_.Suspend(fiber);
63 std::lock_guard guard(waiters_mu_);
64 if (auto it = std::find(waiters_.begin(), waiters_.end(), fiber);
65 it != waiters_.end()) {
66 waiters_.erase(it);
67 }
68 }
69 }
70
71 void Unlock() noexcept ABSL_UNLOCK_FUNCTION() {
72 Fiber* absl_nullable fiber = nullptr;
73 {
74 std::lock_guard guard(waiters_mu_);
75 mu_.unlock();
76 if (!waiters_.empty()) {
77 fiber = waiters_.front();
78 waiters_.pop_front();
79 }
80 }
81 if (fiber != nullptr) {
82 fiber->scheduler_.Wake(fiber);
83 }
84 }
85
86 void lock() noexcept ABSL_EXCLUSIVE_LOCK_FUNCTION() { Lock(); }
87
88 void unlock() noexcept ABSL_UNLOCK_FUNCTION() { Unlock(); }
89
90 private:
91 friend class CondVar;
92 std::mutex mu_;
93 std::mutex waiters_mu_;
94 std::deque<Fiber* absl_nonnull> waiters_;
95};
96
97class ABSL_SCOPED_LOCKABLE MutexLock {
98 public:
99 explicit MutexLock(Mutex* absl_nonnull mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
100 : mu_(mu) {
101 mu_->Lock();
102 }
103
104 MutexLock(const MutexLock&) = delete;
105 MutexLock& operator=(const MutexLock&) = delete;
106
107 ~MutexLock() ABSL_UNLOCK_FUNCTION() { mu_->Unlock(); }
108
109 private:
110 Mutex* absl_nonnull mu_;
111};
112
113class CondVar {
114 public:
115 CondVar() = default;
116 CondVar(const CondVar&) = delete;
117 CondVar& operator=(const CondVar&) = delete;
118
119 void Wait(Mutex* absl_nonnull mu) noexcept {
120 if (Fiber::Current() != nullptr) {
121 WaitWithTimeout(mu, absl::InfiniteDuration());
122 return;
123 }
124 std::unique_lock<std::mutex> lock(mu->mu_, std::adopt_lock);
125 cv_.wait(lock);
126 lock.release();
127 }
128
129 bool WaitWithDeadline(Mutex* absl_nonnull mu, absl::Time deadline) noexcept {
130 if (deadline == absl::InfiniteFuture()) {
131 Wait(mu);
132 return false;
133 }
134 return WaitWithTimeout(mu, deadline - absl::Now());
135 }
136
137 bool WaitWithTimeout(Mutex* absl_nonnull mu,
138 absl::Duration remaining) noexcept {
139 if (Fiber* absl_nullable fiber = Fiber::Current()) {
140 if (remaining <= absl::ZeroDuration()) {
141 return true;
142 }
143 const bool infinite = remaining == absl::InfiniteDuration();
144 while (infinite || remaining > absl::ZeroDuration()) {
145 const auto start = std::chrono::steady_clock::now();
146 auto deadline = std::chrono::steady_clock::time_point::max();
147 if (!infinite) {
148 const absl::Duration safe = remaining < absl::Hours(24 * 365)
149 ? remaining
150 : absl::Hours(24 * 365);
151 deadline =
152 start + std::chrono::nanoseconds(absl::ToInt64Nanoseconds(safe));
153 }
154 Waiter waiter{.fiber = fiber, .signalled = false};
155 {
156 std::lock_guard guard(waiters_mu_);
157 fiber->scheduler_.PreparePark(fiber, deadline);
158 waiters_.push_back(&waiter);
159 }
160 mu->Unlock();
161 fiber->scheduler_.Suspend(fiber);
162 bool signalled = false;
163 {
164 std::lock_guard guard(waiters_mu_);
165 if (auto it = std::find(waiters_.begin(), waiters_.end(), &waiter);
166 it != waiters_.end()) {
167 waiters_.erase(it);
168 }
169 signalled = waiter.signalled;
170 }
171 mu->Lock();
172 if (signalled) {
173 return false;
174 }
175 if (!infinite) {
176 const auto elapsed =
177 std::chrono::duration_cast<std::chrono::nanoseconds>(
178 std::chrono::steady_clock::now() - start);
179 remaining -= absl::Nanoseconds(elapsed.count());
180 }
181 }
182 return true;
183 }
184 if (remaining == absl::InfiniteDuration()) {
185 Wait(mu);
186 return false;
187 }
188 if (remaining <= absl::ZeroDuration()) {
189 return true;
190 }
191 const std::uint32_t observed = generation_.load(std::memory_order_acquire);
192 std::unique_lock<std::mutex> lock(mu->mu_, std::adopt_lock);
193 bool signalled = false;
194 while (remaining > absl::ZeroDuration()) {
195 // Convert the accepted wall deadline once. Short steady-clock slices
196 // avoid overflowing libc++/pthread timer ranges on distant deadlines.
197 const absl::Duration slice =
198 remaining < absl::Hours(1) ? remaining : absl::Hours(1);
199 const auto start = std::chrono::steady_clock::now();
200 cv_.wait_for(lock,
201 std::chrono::nanoseconds(absl::ToInt64Nanoseconds(slice)));
202 if (generation_.load(std::memory_order_acquire) != observed) {
203 signalled = true;
204 break;
205 }
206 const auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(
207 std::chrono::steady_clock::now() - start);
208 remaining -= absl::Nanoseconds(elapsed.count());
209 }
210 lock.release();
211 return !signalled;
212 }
213
214 void Signal() noexcept {
215 generation_.fetch_add(1, std::memory_order_release);
216 Scheduler* absl_nullable scheduler = nullptr;
217 bool notify = false;
218 {
219 std::lock_guard guard(waiters_mu_);
220 if (!waiters_.empty()) {
221 Waiter* absl_nonnull waiter = waiters_.front();
222 waiters_.pop_front();
223 waiter->signalled = true;
224 scheduler = &waiter->fiber->scheduler_;
225 notify = scheduler->WakeWithoutNotify(waiter->fiber);
226 }
227 }
228 if (notify) {
229 scheduler->NotifyReady();
230 }
231 cv_.notify_one();
232 }
233
234 void SignalAll() noexcept {
235 generation_.fetch_add(1, std::memory_order_release);
236 std::deque<Scheduler* absl_nonnull> schedulers;
237 {
238 std::lock_guard guard(waiters_mu_);
239 while (!waiters_.empty()) {
240 Waiter* absl_nonnull waiter = waiters_.front();
241 waiters_.pop_front();
242 waiter->signalled = true;
243 Scheduler* absl_nonnull scheduler = &waiter->fiber->scheduler_;
244 if (scheduler->WakeWithoutNotify(waiter->fiber)) {
245 schedulers.push_back(scheduler);
246 }
247 }
248 }
249 for (Scheduler* absl_nonnull scheduler : schedulers) {
250 scheduler->NotifyReady();
251 }
252 cv_.notify_all();
253 }
254
255 private:
256 struct Waiter {
257 Fiber* absl_nonnull fiber;
258 bool signalled;
259 };
260
261 std::mutex waiters_mu_;
262 std::deque<Waiter* absl_nonnull> waiters_;
263 std::atomic<std::uint32_t> generation_{0};
264 std::condition_variable cv_;
265};
266
267inline void SleepFor(absl::Duration duration) {
268 if (Fiber::Current() != nullptr) {
269 Fiber::SleepFor(duration);
270 return;
271 }
272 if (duration <= absl::ZeroDuration()) {
273 std::this_thread::yield();
274 return;
275 }
276 while (duration > absl::ZeroDuration()) {
277 const absl::Duration slice =
278 duration < absl::Hours(1) ? duration : absl::Hours(1);
279 std::this_thread::sleep_for(
280 std::chrono::nanoseconds(absl::ToInt64Nanoseconds(slice)));
281 duration -= slice;
282 }
283}
284
285} // namespace thread
286
287#endif // THREAD_BOOST_PRIMITIVES_H_
const std::function< absl::Status()> & start
Definition active_service.cc:15
Definition boost_primitives.h:113
void Signal() noexcept
Definition boost_primitives.h:214
CondVar()=default
Definition boost_primitives.cc:56
void Wait(Mutex *absl_nonnull mu) noexcept
Definition boost_primitives.h:119
bool WaitWithDeadline(Mutex *absl_nonnull mu, absl::Time deadline) noexcept
Definition boost_primitives.h:129
CondVar(const CondVar &)=delete
bool WaitWithTimeout(Mutex *absl_nonnull mu, absl::Duration remaining) noexcept
Definition boost_primitives.h:137
void SignalAll() noexcept
Definition boost_primitives.h:234
CondVar & operator=(const CondVar &)=delete
Definition fiber.h:89
static Fiber *absl_nullable Current() noexcept
Definition fiber.cc:272
static void SleepFor(absl::Duration duration)
Definition fiber.cc:313
Definition boost_primitives.h:97
~MutexLock() ABSL_UNLOCK_FUNCTION()
Definition boost_primitives.h:107
MutexLock(Mutex *absl_nonnull mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
Definition boost_primitives.h:99
MutexLock(const MutexLock &)=delete
MutexLock & operator=(const MutexLock &)=delete
Definition boost_primitives.h:36
void lock() noexcept ABSL_EXCLUSIVE_LOCK_FUNCTION()
Definition boost_primitives.h:86
void Lock() noexcept ABSL_EXCLUSIVE_LOCK_FUNCTION()
Definition boost_primitives.h:42
Mutex & operator=(const Mutex &)=delete
void Unlock() noexcept ABSL_UNLOCK_FUNCTION()
Definition boost_primitives.h:71
void unlock() noexcept ABSL_UNLOCK_FUNCTION()
Definition boost_primitives.h:88
Mutex(const Mutex &)=delete
Definition fiber.h:53
Definition channel.h:29
void SleepFor(absl::Duration duration)
Definition boost_primitives.h:267
constexpr bool kCanParkAwait
Definition boost_primitives.h:30
bool CanParkAwait() noexcept
Definition boost_primitives.h:32