A11 (C++ runtime)
Native C++ implementation of the A11 action and streaming runtime
Loading...
Searching...
No Matches
callback_scheduler.h
Go to the documentation of this file.
1// Copyright 2026 The A11 Authors.
2
3#ifndef A11_CONCURRENCY_CALLBACK_SCHEDULER_H_
4#define A11_CONCURRENCY_CALLBACK_SCHEDULER_H_
5
6#include <cstddef>
7#include <deque>
8
9#include <absl/functional/any_invocable.h>
10
11#include "thread/boost_primitives.h"
12
13namespace a11::internal {
14
15// A fair, stackless callback pump. Instances must have process lifetime because
16// posted callbacks retain a raw pointer to the scheduler, while queued work
17// owns the state it operates on.
18class CallbackScheduler {
19 public:
20 explicit CallbackScheduler(size_t max_callbacks_per_turn = 64)
21 : max_callbacks_per_turn_(max_callbacks_per_turn) {}
22
23 CallbackScheduler(const CallbackScheduler&) = delete;
24 CallbackScheduler& operator=(const CallbackScheduler&) = delete;
25
26 void Schedule(absl::AnyInvocable<void() &&> callback);
27
28 private:
29 void Run();
30
31 const size_t max_callbacks_per_turn_;
32 thread::Mutex mu_;
33 std::deque<absl::AnyInvocable<void() &&>> callbacks_ ABSL_GUARDED_BY(mu_);
34 bool scheduled_ ABSL_GUARDED_BY(mu_) = false;
35};
36
37} // namespace a11::internal
38
39#endif // A11_CONCURRENCY_CALLBACK_SCHEDULER_H_
Definition callback_scheduler.cc:12
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