Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
native_task_owner.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_NATIVE_TASK_OWNER_H_
5#define SYMBIAN_CONCURRENCY_NATIVE_TASK_OWNER_H_
6
7#include <atomic>
8#include <memory>
9#include <optional>
10
11#include <absl/base/nullability.h>
12#include <absl/status/status_macros.h>
13
14#include "absl/time/time.h"
17
18namespace symbian::concurrency {
19
20// Starts one timer and one real property subscription under a shared owner.
21// Close requests cancellation; Finish settles only after both native adapters
22// have published their drained results. Call Start and Close on the event
23// thread. A worker may cancel the returned Task.
25 public:
26 explicit NativeTaskOwner(EventExecutor* absl_nonnull executor)
27 : executor_(*executor) {}
28
31
33
34 Task Start(absl::Duration timer_delay,
35 absl::Time deadline = absl::InfiniteFuture()) {
36 if (closed_ || state_) {
37 return FailedTask(absl::FailedPreconditionError(
38 "Native task owner already started or closed"));
39 }
40 if (deadline != absl::InfiniteFuture() && deadline <= absl::Now()) {
41 return FailedTask(absl::DeadlineExceededError("Owner deadline expired"));
42 }
43 auto state = std::make_shared<State>();
44 state_ = state; // Establish owner state before either native submission.
45 state->completion.SetCancellationCallback(
46 [weak = std::weak_ptr<State>(state)] {
47 if (auto owned = weak.lock()) {
48 owned->RequestCancel();
49 }
50 });
51 state->timer = executor_.ScheduleAfter(timer_delay);
52 state->property = executor_.NextProperty();
53 TaskGroup children;
54 children.Add(state->timer);
55 children.Add(
56 Then(state->property,
57 [](const absl::StatusOr<int>& result) -> absl::StatusOr<Unit> {
58 ABSL_RETURN_IF_ERROR(result.status());
59 return Unit{};
60 }));
61 state->children = children.Finish();
62 if (deadline != absl::InfiniteFuture()) {
63 state->alarm = executor_.ScheduleAt(deadline);
64 }
65 state->timer.OnReady([weak = std::weak_ptr<State>(state)](
66 const absl::StatusOr<Unit>& result) {
67 if (!result.ok()) {
68 if (auto owned = weak.lock()) {
69 owned->property.Cancel();
70 }
71 }
72 });
73 state->property.OnReady([weak = std::weak_ptr<State>(state)](
74 const absl::StatusOr<int>& result) {
75 if (!result.ok()) {
76 if (auto owned = weak.lock()) {
77 owned->timer.Cancel();
78 }
79 }
80 });
81 if (state->alarm.valid()) {
82 state->alarm.OnReady([state](const absl::StatusOr<Unit>& result) {
83 state->alarm_result = result;
84 if (result.ok()) {
85 state->timed_out = true;
86 state->timer.Cancel();
87 state->property.Cancel();
88 } else if (result.status().code() != absl::StatusCode::kCancelled) {
89 state->timer.Cancel();
90 state->property.Cancel();
91 }
92 state->Check();
93 });
94 }
95 state->children.OnReady([state](const absl::StatusOr<Unit>& result) {
96 state->child_result = result;
97 if (state->alarm.valid() && !state->alarm.IsReady()) {
98 state->alarm.Cancel();
99 }
100 state->Check();
101 });
102 return state->joined;
103 }
104
106 return state_ ? state_->property : Future<int>();
107 }
108
109 Task Finish() const {
110 return state_ ? state_->joined
111 : FailedTask(absl::FailedPreconditionError(
112 "Native task owner was not started"));
113 }
114
115 void Close() {
116 if (closed_) {
117 return;
118 }
119 closed_ = true;
120 if (state_) {
121 state_->RequestCancel();
122 }
123 }
124
125 private:
126 struct State {
127 void RequestCancel() {
128 cancel_requested.store(true, std::memory_order_release);
129 timer.Cancel();
130 property.Cancel();
131 alarm.Cancel();
132 }
133
134 void Check() {
135 if (!child_result || (alarm.valid() && !alarm_result)) {
136 return;
137 }
138 if (timed_out) {
139 completion.SetError(
140 absl::DeadlineExceededError("Owner deadline expired"));
141 } else if (alarm_result && !alarm_result->ok() &&
142 alarm_result->status().code() !=
143 absl::StatusCode::kCancelled) {
144 completion.SetError(alarm_result->status());
145 } else if (!child_result->ok()) {
146 completion.SetError(child_result->status());
147 } else if (cancel_requested.load(std::memory_order_acquire)) {
148 completion.SetError(absl::CancelledError("Native task owner closed"));
149 } else {
150 completion.SetValue(Unit{});
151 }
152 }
153
154 Promise<Unit> completion;
155 Task joined = completion.future();
156 Task timer;
157 Future<int> property;
158 Task alarm;
159 Task children;
160 std::optional<absl::StatusOr<Unit>> child_result;
161 std::optional<absl::StatusOr<Unit>> alarm_result;
162 std::atomic<bool> cancel_requested{false};
163 bool timed_out = false;
164 };
165
166 EventExecutor& executor_;
167 std::shared_ptr<State> state_;
168 bool closed_ = false;
169};
170
171} // namespace symbian::concurrency
172
173#endif // SYMBIAN_CONCURRENCY_NATIVE_TASK_OWNER_H_
Definition bounded_channel.h:41
Definition event_executor.h:30
Task ScheduleAfter(absl::Duration delay)
Definition event_executor.h:70
Task ScheduleAt(absl::Time deadline)
Definition event_executor.h:74
Future< int > NextProperty()
Definition event_executor.h:88
void OnReady(std::function< void(const absl::StatusOr< T > &)> callback) const
Definition future.h:217
Definition native_task_owner.h:24
NativeTaskOwner(const NativeTaskOwner &)=delete
Task Start(absl::Duration timer_delay, absl::Time deadline=absl::InfiniteFuture())
Definition native_task_owner.h:34
~NativeTaskOwner()
Definition native_task_owner.h:32
NativeTaskOwner(EventExecutor *absl_nonnull executor)
Definition native_task_owner.h:26
Future< int > property_result() const
Definition native_task_owner.h:105
Task Finish() const
Definition native_task_owner.h:109
NativeTaskOwner & operator=(const NativeTaskOwner &)=delete
void Close()
Definition native_task_owner.h:115
Definition task_group.h:20
bool Add(Task child)
Definition task_group.h:34
Definition bounded_channel.h:35
Task FailedTask(absl::Status error)
Definition future.h:279
Future< Unit > Task
Definition future.h:273
auto Then(const Future< T > &future, Fn transform) -> Future< typename std::invoke_result_t< Fn, const absl::StatusOr< T > & >::value_type >
Definition future.h:284