Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
parallel.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2// Licensed under the Apache License, Version 2.0 (the "License");
3// http://www.apache.org/licenses/LICENSE-2.0
4//
5// Stackless guest adaptation of A11 cpp/a11/concurrency/parallel.h at
6// fcccb8cb6e1e67d7ba0822ac14cece9ee4c7091b. A11's AwaitAll waits for
7// every input, even after an error, preserving input order. This form returns
8// a Future so the event thread can observe the same result without blocking.
9
10#ifndef SYMBIAN_CONCURRENCY_PARALLEL_H_
11#define SYMBIAN_CONCURRENCY_PARALLEL_H_
12
13#include <cstddef>
14#include <memory>
15#include <optional>
16#include <utility>
17#include <vector>
18
20
21namespace symbian::concurrency {
22
23template <typename T>
25 struct Collector {
26 explicit Collector(size_t count) : slots(count), remaining(count) {}
27
29 std::vector<std::optional<absl::StatusOr<T>>> slots;
30 size_t remaining;
32 };
33
34 auto collector = std::make_shared<Collector>(inputs.size());
35 auto joined = collector->promise.future();
36 collector->promise.SetCancellationCallback([inputs] {
37 for (const auto& input : inputs) {
38 input.Cancel();
39 }
40 });
41 if (inputs.empty()) {
42 collector->promise.SetValue({});
43 return joined;
44 }
45 for (size_t index = 0; index < inputs.size(); ++index) {
46 inputs[index].OnReady([collector, index](const absl::StatusOr<T>& result) {
47 bool complete = false;
48 std::vector<absl::StatusOr<T>> results;
49 {
51 collector->slots[index] = result;
52 complete = --collector->remaining == 0;
53 if (complete) {
54 results.reserve(collector->slots.size());
55 for (auto& slot : collector->slots) {
56 results.push_back(std::move(*slot));
57 }
58 }
59 }
60 if (complete) {
61 collector->promise.SetValue(std::move(results));
62 }
63 });
64 }
65 return joined;
66}
67
68} // namespace symbian::concurrency
69
70#endif // SYMBIAN_CONCURRENCY_PARALLEL_H_
Definition bounded_channel.h:38
Definition boost_primitives.h:95
Definition boost_primitives.h:34
Definition bounded_channel.h:32
Future< std::vector< absl::StatusOr< T > > > JoinAll(std::vector< Future< T > > inputs)
Definition parallel.h:24