A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
async_task.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef A11_WASM_ASYNC_TASK_H_
16#define A11_WASM_ASYNC_TASK_H_
17
18#include <atomic>
19#include <functional>
20#include <memory>
21#include <string>
22
23#include <absl/status/statusor.h>
24
25#include "bridge_core.h"
26
27namespace a11::wasm {
28
29class AsyncTask : public std::enable_shared_from_this<AsyncTask> {
30 public:
31 using Work = std::function<absl::StatusOr<std::string>()>;
32
33 static std::shared_ptr<AsyncTask> Start(Work work);
34 ~AsyncTask();
35 void Launch();
36 Result* Take();
37 void Cancel();
38
39 private:
40 void Publish(std::unique_ptr<Result> result);
41 void MaybeCancel();
42
43 std::atomic<Result*> result_{nullptr};
44 std::atomic<bool> consumed_{false};
45 std::atomic<bool> cancel_requested_{false};
46 std::atomic<bool> cancel_called_{false};
47 std::unique_ptr<std::function<void()>> cancel_owner_;
48 std::atomic<std::function<void()>*> cancel_{nullptr};
49 Work work_;
50};
51
52} // namespace a11::wasm
53
54#endif // A11_WASM_ASYNC_TASK_H_
a11::Promise< a11::Unit > result
Definition async_node.cc:56
Definition async_task.h:29
void Cancel()
Definition async_task.cc:96
Result * Take()
Definition async_task.cc:85
~AsyncTask()
Definition async_task.cc:35
std::function< absl::StatusOr< std::string >()> Work
Definition async_task.h:31
static std::shared_ptr< AsyncTask > Start(Work work)
Definition async_task.cc:29
void Launch()
Definition async_task.cc:65
Definition async_task.cc:27
Definition bridge_core.h:25