Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
event_executor.h
Go to the documentation of this file.
1// Copyright 2026 The Symbian SDK Authors.
2// Licensed under the Apache License, Version 2.0.
3
4#ifndef SYMBIAN_CONCURRENCY_EVENT_EXECUTOR_H_
5#define SYMBIAN_CONCURRENCY_EVENT_EXECUTOR_H_
6
7#include <chrono>
8#include <cstddef>
9#include <functional>
10#include <memory>
11#include <mutex>
12#include <utility>
13
14#include <absl/status/status_macros.h>
15#include <absl/base/nullability.h>
16
17#include "absl/status/status.h"
18#include "absl/status/statusor.h"
23#include "thread/fiber.h"
24
25namespace symbian::concurrency {
26
27// The event thread's sole native request-semaphore consumer. Window Server
28// statuses remain owned by the caller and must be inspected on every turn.
29// Native adapters remove completed requests before invoking inline callbacks.
31 public:
32 EventExecutor() : scheduler_(policy_.Borrow()) {}
33
34 EventExecutor(const EventExecutor&) = delete;
36
38
39 absl::Status Open() {
40 if (opened_ || closed_) {
41 return absl::FailedPreconditionError("Event executor already opened");
42 }
43 if (const int result = timers_.Open(); result != 0) {
44 return symbian::StatusFromNativeError(result, "Event executor wake");
45 }
46 auto wake = timers_.WakeCallback();
47 policy_.SetWake(wake);
48 auto mailbox = std::make_shared<EventMailbox>(std::move(wake));
49 {
50 std::lock_guard lock(dispatch_mu_);
51 mailbox_ = std::move(mailbox);
52 opened_ = true;
53 }
54 return absl::OkStatus();
55 }
56
57 // Explicit event affinity. A11 Post/PostAt retain their worker-pool meaning.
58 absl::Status DispatchToEvent(std::function<void()> callback) {
59 std::shared_ptr<EventMailbox> mailbox;
60 {
61 std::lock_guard lock(dispatch_mu_);
62 if (!opened_ || closed_) {
63 return absl::FailedPreconditionError("Event executor is closed");
64 }
65 mailbox = mailbox_;
66 }
67 return mailbox->Enqueue(std::move(callback));
68 }
69
70 Task ScheduleAfter(absl::Duration delay) {
71 return timers_.ScheduleAfter(delay);
72 }
73
74 Task ScheduleAt(absl::Time deadline) { return timers_.ScheduleAt(deadline); }
75
76 absl::Status OpenProperty(int category, unsigned int key) {
77 if (!opened_ || closed_ || property_) {
78 return absl::FailedPreconditionError("Property owner unavailable");
79 }
80 auto property = std::make_unique<PropertyWatch>(timers_.WakeCallback());
81 absl::Status status = property->Open(category, key);
82 if (status.ok()) {
83 property_ = std::move(property);
84 }
85 return status;
86 }
87
89 if (!property_ || closed_) {
90 return FailedFuture<int>(
91 absl::FailedPreconditionError("Property owner is closed"));
92 }
93 return property_->Next();
94 }
95
96 absl::Status SetProperty(int value) {
97 if (!property_ || closed_) {
98 return absl::FailedPreconditionError("Property owner is closed");
99 }
100 return property_->Set(value);
101 }
102
103 thread::Scheduler& fibers() { return scheduler_; }
104
105 // Lazily create one shared worker for explicit compute placement. The
106 // event thread calls this during setup; ordinary DispatchReady turns never
107 // create an OS thread or offload callbacks implicitly.
108 absl::StatusOr<WorkerExecutor* absl_nonnull> workers() {
109 std::lock_guard lock(dispatch_mu_);
110 if (!opened_ || closed_) {
111 return absl::FailedPreconditionError("Event executor is closed");
112 }
113 if (!worker_) {
114 worker_ = std::make_unique<WorkerExecutor>();
115 }
116 return worker_.get();
117 }
118
119 // Bound each source independently. Call again when HasReady() is true;
120 // native and mailbox adapters resignal when a turn leaves work behind.
121 absl::Status DispatchReady(std::size_t budget = 64) {
122 if (!opened_ || closed_) {
123 return absl::FailedPreconditionError("Event executor is closed");
124 }
125 if (budget == 0) {
126 return absl::OkStatus();
127 }
128 if (property_) {
129 property_->DispatchReady();
130 }
131 timers_.DispatchReady(budget);
132 mailbox_->DispatchReady(budget);
134 return RearmFiberDeadline();
135 }
136
137 bool HasReady() const {
138 return opened_ && !closed_ &&
139 (scheduler_.HasReady() || mailbox_->Pending() != 0 ||
140 (fiber_alarm_.valid() && fiber_alarm_.IsReady()));
141 }
142
143 // The caller checks Window Server statuses before this call. The request
144 // semaphore preserves completion signals arriving after that check.
145 void Park() const {
146 if (opened_ && !closed_ && !HasReady()) {
147 timers_.Park();
148 }
149 }
150
151 void Close() {
152 {
153 std::lock_guard lock(dispatch_mu_);
154 if (closed_) {
155 return;
156 }
157 closed_ = true;
158 }
159 if (property_) {
160 property_->Close();
161 property_.reset();
162 }
163 if (worker_) {
164 worker_->Close();
165 }
166 if (fiber_alarm_.valid()) {
167 fiber_alarm_.Cancel();
168 }
169 timers_.Close();
170 if (mailbox_) {
171 mailbox_->Close();
172 mailbox_.reset();
173 }
174 policy_.SetWake({});
175 }
176
177 private:
178 class WakePolicy final {
179 public:
180 WakePolicy()
181 : callbacks_{.pick_next =
182 [](std::span<thread::Fiber* absl_nonnull const>) {
183 return std::size_t{0};
184 },
185 .notify_ready = [this] { NotifyReady(); }} {}
186
187 thread::SchedulerPolicy* absl_nonnull Borrow() { return &callbacks_; }
188
189 void NotifyReady() noexcept {
190 std::function<void()> callback;
191 {
192 std::lock_guard lock(mu_);
193 callback = wake_;
194 }
195 if (callback) {
196 callback();
197 }
198 }
199
200 void SetWake(std::function<void()> wake) {
201 std::lock_guard lock(mu_);
202 wake_ = std::move(wake);
203 }
204
205 private:
206 thread::SchedulerPolicy callbacks_;
207 std::mutex mu_;
208 std::function<void()> wake_;
209 };
210
211 absl::Status RearmFiberDeadline() {
212 const auto next = scheduler_.NextDeadline();
213 if (next == fiber_deadline_ &&
214 (!fiber_alarm_.valid() || !fiber_alarm_.IsReady())) {
215 return absl::OkStatus();
216 }
217 if (fiber_alarm_.valid() && !fiber_alarm_.IsReady()) {
218 fiber_alarm_.Cancel();
219 }
220 fiber_alarm_ = {};
221 fiber_deadline_ = next;
222 if (next == std::chrono::steady_clock::time_point::max()) {
223 return absl::OkStatus();
224 }
225 const auto now = std::chrono::steady_clock::now();
226 const auto delay =
227 next <= now ? std::chrono::nanoseconds::zero() : next - now;
228 fiber_alarm_ = timers_.ScheduleAfter(absl::Nanoseconds(delay.count()));
229 if (auto result = fiber_alarm_.ResultIfReady(); result && !result->ok()) {
230 return result->status();
231 }
232 return absl::OkStatus();
233 }
234
235 TimerPump timers_;
236 WakePolicy policy_;
237 thread::Scheduler scheduler_;
238 std::mutex dispatch_mu_;
239 std::shared_ptr<EventMailbox> mailbox_;
240 std::unique_ptr<PropertyWatch> property_;
241 std::unique_ptr<WorkerExecutor> worker_;
242 Task fiber_alarm_;
243 std::chrono::steady_clock::time_point fiber_deadline_ =
244 std::chrono::steady_clock::time_point::max();
245 bool opened_ = false;
246 bool closed_ = false;
247};
248
249} // namespace symbian::concurrency
250
251#endif // SYMBIAN_CONCURRENCY_EVENT_EXECUTOR_H_
absl::Status status
Definition camera_stream.cc:48
Definition bounded_channel.h:41
Definition event_executor.h:30
absl::StatusOr< WorkerExecutor *absl_nonnull > workers()
Definition event_executor.h:108
EventExecutor(const EventExecutor &)=delete
bool HasReady() const
Definition event_executor.h:137
absl::Status SetProperty(int value)
Definition event_executor.h:96
void Park() const
Definition event_executor.h:145
EventExecutor & operator=(const EventExecutor &)=delete
absl::Status Open()
Definition event_executor.h:39
Task ScheduleAfter(absl::Duration delay)
Definition event_executor.h:70
absl::Status DispatchToEvent(std::function< void()> callback)
Definition event_executor.h:58
EventExecutor()
Definition event_executor.h:32
~EventExecutor()
Definition event_executor.h:37
absl::Status DispatchReady(std::size_t budget=64)
Definition event_executor.h:121
void Close()
Definition event_executor.h:151
Task ScheduleAt(absl::Time deadline)
Definition event_executor.h:74
Future< int > NextProperty()
Definition event_executor.h:88
thread::Scheduler & fibers()
Definition event_executor.h:103
absl::Status OpenProperty(int category, unsigned int key)
Definition event_executor.h:76
bool IsReady() const
Definition future.h:138
bool Cancel() const
Definition future.h:148
bool valid() const
Definition future.h:136
Task ScheduleAt(absl::Time deadline)
Definition timer_pump.h:94
std::size_t DispatchReady(std::size_t budget=64)
Definition timer_pump.h:151
void Close()
Definition timer_pump.h:225
void Park() const
Definition timer_pump.h:219
std::function< void()> WakeCallback() const
Definition timer_pump.h:84
Task ScheduleAfter(absl::Duration delay)
Definition timer_pump.h:105
int Open()
Definition timer_pump.h:70
Definition fiber.h:53
bool HasReady() const
Definition fiber.cc:143
absl::Status RunReady(std::size_t max_turns)
Definition fiber.cc:168
Definition native_camera_stream.cc:19
Definition bounded_channel.h:35
Future< Unit > Task
Definition future.h:273
absl::Status StatusFromNativeError(int native_code, absl::string_view operation)
Definition native_status.h:44
Definition channel.h:29
Definition fiber.h:39
libusb_context *absl_nullable value
Definition usb.cc:37