A11 (C++ runtime)
Native C++ implementation of the A11 action and streaming runtime
Loading...
Searching...
No Matches
executor.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2
3#ifndef A11_CONCURRENCY_EXECUTOR_H_
4#define A11_CONCURRENCY_EXECUTOR_H_
5
6#include <exception>
7#include <functional>
8#include <utility>
9
10#include <absl/functional/any_invocable.h>
11#include <absl/status/status.h>
12#include <absl/status/status_macros.h>
13#include <absl/status/statusor.h>
14
16
17namespace a11 {
18
21void Schedule(absl::AnyInvocable<void() &&> work,
22 thread::TreeOptions tree_options = {});
23
26std::function<void()> ScheduleCancelable(absl::AnyInvocable<void() &&> work,
27 thread::TreeOptions tree_options = {});
28
29template <typename T>
31 absl::AnyInvocable<absl::StatusOr<T>() &&> work,
32 std::function<void()> cancellation_hook, thread::TreeOptions tree_options) {
33 Promise<T> promise;
34 Future<T> future = promise.future();
35 std::function<void()> cancel = ScheduleCancelable(
36 [promise = std::move(promise), work = std::move(work)]() mutable {
37 absl::StatusOr<T> result;
38 if (thread::Cancelled()) {
39 result = absl::CancelledError("Task cancelled before it started");
40 } else
41 try {
42 result = std::move(work)();
43 } catch (const std::exception& error) {
44 result = absl::UnknownError(error.what());
45 } catch (...) {
46 result = absl::UnknownError("task raised a non-standard exception");
47 }
48 const absl::Status completion = promise.SetResult(std::move(result));
49 (void)completion;
50 },
51 std::move(tree_options));
52 // The promise has moved into the task, but both handles share its state.
53 // Install cancellation through a temporary handle recovered from Future.
54 future.SetCancellationCallbackForExecutor(
55 [cancel = std::move(cancel),
56 cancellation_hook = std::move(cancellation_hook)]() {
57 if (cancellation_hook != nullptr)
58 cancellation_hook();
59 cancel();
60 });
61 return future;
62}
63
65template <typename T>
66Future<T> Submit(absl::AnyInvocable<absl::StatusOr<T>() &&> work,
67 thread::TreeOptions tree_options) {
68 return SubmitWithCancellationHook<T>(std::move(work), {},
69 std::move(tree_options));
70}
71
73inline Task SubmitTask(absl::AnyInvocable<absl::Status() &&> work,
74 thread::TreeOptions tree_options = {}) {
75 return Submit<Unit>(
76 [work = std::move(work)]() mutable -> absl::StatusOr<Unit> {
77 ABSL_RETURN_IF_ERROR(std::move(work)());
78 return Unit{};
79 },
80 std::move(tree_options));
81}
82
83} // namespace a11
84
85#endif // A11_CONCURRENCY_EXECUTOR_H_
Shared handle to one asynchronous result.
Definition future.h:110
Move-only producer for a Future result.
Definition future.h:274
absl::Status SetResult(absl::StatusOr< T > result)
Complete with either a value or an error, waking every observer.
Definition future.h:328
Future< T > future() const
Return a consumer handle sharing this promise's completion state.
Definition future.h:296
Completion values used by every asynchronous A11 operation.
Definition action.cc:46
std::function< void()> ScheduleCancelable(absl::AnyInvocable< void() && > work, thread::TreeOptions tree_options)
Schedule work and return an idempotent cooperative-cancellation function.
Definition executor.cc:48
Task SubmitTask(absl::AnyInvocable< absl::Status() && > work, thread::TreeOptions tree_options={})
Run an operation that returns only a completion status.
Definition executor.h:73
void Schedule(absl::AnyInvocable< void() && > work, thread::TreeOptions tree_options)
Schedule work on A11's fiber pool without returning a completion handle.
Definition executor.cc:35
Future< T > Submit(absl::AnyInvocable< absl::StatusOr< T >() && > work, thread::TreeOptions tree_options)
Run status-returning work on the fiber pool and expose its Future.
Definition executor.h:66
Future< T > SubmitWithCancellationHook(absl::AnyInvocable< absl::StatusOr< T >() && > work, std::function< void()> cancellation_hook, thread::TreeOptions tree_options)
Run work on A11's fiber pool with application-specific cancellation.
Definition executor.h:30