Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
worker_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_WORKER_EXECUTOR_H_
5#define SYMBIAN_CONCURRENCY_WORKER_EXECUTOR_H_
6
7#include <cstddef>
8#include <functional>
9#include <memory>
10#include <type_traits>
11#include <utility>
12
13#include <absl/base/nullability.h>
14
15#include "absl/functional/any_invocable.h"
16#include "absl/status/status.h"
18
19namespace symbian::concurrency {
20
30 private:
31 struct State;
32
33 public:
34 using Work = absl::AnyInvocable<void() &&>;
35
37 public:
39 absl::Status Post(Work work) const;
40
41 private:
42 friend class WorkerExecutor;
43
44 explicit DispatchHandle(std::weak_ptr<State> state)
45 : state_(std::move(state)) {}
46
47 std::weak_ptr<State> state_;
48 };
49
50 explicit WorkerExecutor(std::size_t max_outstanding = 128);
54
55 DispatchHandle handle() const { return DispatchHandle(state_); }
56
58 absl::Status Post(Work work) { return handle().Post(std::move(work)); }
59
67 Task PostFiber(Work work, std::size_t stack_bytes = 16 * 1024);
69 void Close();
71 Task Finish();
72
73 private:
74 std::shared_ptr<State> state_;
75};
76
77namespace internal {
78absl::StatusOr<WorkerExecutor* absl_nonnull> WorkerFor(
79 EventExecutor* absl_nonnull executor);
80} // namespace internal
81
82// The transform is posted as a stackless callback. The completing thread
83// only enqueues a cheap Future handle; the worker copies the result there.
84// Queue rejection completes the returned Future with an error. Then()
85// retains its A11 inline semantics.
86template <typename T, typename Fn>
87auto ThenOn(const Future<T>& future, WorkerExecutor* absl_nonnull worker,
88 Fn transform)
89 -> Future<typename std::invoke_result_t<
90 Fn, const absl::StatusOr<T>&>::value_type> {
91 using U =
92 typename std::invoke_result_t<Fn, const absl::StatusOr<T>&>::value_type;
93 auto promise = std::make_shared<Promise<U>>();
94 Future<U> continued = promise->future();
95 promise->SetCancellationCallback([future] { future.Cancel(); });
96 auto handle = worker->handle();
97 future.OnReady([promise = std::move(promise),
98 transform = std::move(transform), handle,
99 future](const absl::StatusOr<T>&) mutable {
100 if (absl::Status posted = handle.Post(
101 [promise, transform = std::move(transform), future]() mutable {
102 auto result = future.ResultIfReady();
103 if (!result) {
104 promise->SetError(absl::InternalError(
105 "Ready Future lost its result before worker dispatch"));
106 return;
107 }
108 promise->SetResult(transform(*result));
109 });
110 !posted.ok()) {
111 promise->SetError(std::move(posted));
112 }
113 });
114 return continued;
115}
116
117template <typename T>
118template <typename Fn>
119auto Future<T>::ThenOnWorker(EventExecutor* absl_nonnull executor,
120 Fn transform) const
121 -> Future<typename std::invoke_result_t<
122 Fn, const absl::StatusOr<T>&>::value_type> {
123 using U =
124 typename std::invoke_result_t<Fn, const absl::StatusOr<T>&>::value_type;
125 auto worker = internal::WorkerFor(executor);
126 if (!worker.ok()) {
127 return FailedFuture<U>(worker.status());
128 }
129 return ThenOn(*this, *worker, std::move(transform));
130}
131
132} // namespace symbian::concurrency
133
134#endif // SYMBIAN_CONCURRENCY_WORKER_EXECUTOR_H_
Definition bounded_channel.h:41
absl::Status Post(Work work) const
Enqueue stackless work or return a full/closed status.
Definition worker_executor.cc:169
Bounded work destination on one guest OS thread.
Definition worker_executor.h:29
Task Finish()
Close and return a task that completes after all jobs drain.
Definition worker_executor.cc:206
absl::Status Post(Work work)
Enqueue stackless work on the single worker thread.
Definition worker_executor.h:58
WorkerExecutor & operator=(const WorkerExecutor &)=delete
WorkerExecutor(const WorkerExecutor &)=delete
DispatchHandle handle() const
Definition worker_executor.h:55
Task PostFiber(Work work, std::size_t stack_bytes=16 *1024)
Run one job on a bounded guest fiber hosted by this worker.
Definition worker_executor.cc:177
~WorkerExecutor()
Definition worker_executor.cc:165
void Close()
Stop accepting work while queued jobs continue to drain.
Definition worker_executor.cc:194
absl::AnyInvocable< void() && > Work
Definition worker_executor.h:34
Definition native_camera_stream.cc:19
Definition bounded_channel.h:35
auto ThenOn(const Future< T > &future, WorkerExecutor *absl_nonnull worker, Fn transform) -> Future< typename std::invoke_result_t< Fn, const absl::StatusOr< T > & >::value_type >
Definition worker_executor.h:87
Definition worker_executor.cc:33
libusb_device_handle *absl_nullable handle
Definition usb.cc:81