Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
fiber.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_GUEST_THREAD_FIBER_H_
5#define SYMBIAN_GUEST_THREAD_FIBER_H_
6
7#include <atomic>
8#include <chrono>
9#include <cstddef>
10#include <cstdint>
11#include <deque>
12#include <functional>
13#include <memory>
14#include <mutex>
15#include <span>
16#include <thread>
17#include <type_traits>
18#include <utility>
19#include <vector>
20
21#include <absl/base/nullability.h>
22
23#include "absl/functional/any_invocable.h"
24#include "absl/status/status.h"
25#include "absl/time/time.h"
26
27namespace thread {
28
29class Fiber;
30class Mutex;
31class CondVar;
32class PermanentEvent;
33struct Case;
34void FiberEntry();
35
36// Application-provided ordering and event-loop wake integration. Both hooks
37// run outside Scheduler's internal lock. NotifyReady may run on a worker OS
38// thread, so implementations must coalesce and dispatch to the event thread.
40 std::function<std::size_t(std::span<Fiber* absl_nonnull const>)> pick_next;
41 std::function<void()> notify_ready;
42
43 std::size_t PickNext(std::span<Fiber* absl_nonnull const> ready) const {
44 return pick_next(ready);
45 }
46
47 void NotifyReady() const noexcept { notify_ready(); }
48};
49
50// One explicitly pumped executor, pinned to its creating OS thread. RunReady
51// never consumes the native request semaphore; the event owner calls it after
52// dispatching native completions and when its next fiber deadline expires.
53class Scheduler {
54 public:
55 explicit Scheduler(SchedulerPolicy* absl_nullable policy = nullptr);
56 Scheduler(const Scheduler&) = delete;
57 Scheduler& operator=(const Scheduler&) = delete;
58 ~Scheduler();
59
60 absl::Status RunReady(std::size_t max_turns);
61 bool HasReady() const;
62 std::chrono::steady_clock::time_point NextDeadline() const;
63 static Scheduler* absl_nullable Current() noexcept;
64
65 private:
66 friend class Fiber;
67 friend class Mutex;
68 friend class CondVar;
69 friend void FiberEntry();
70 void Add(Fiber* absl_nonnull fiber);
71 void Remove(Fiber* absl_nullable fiber);
72 void Wake(Fiber* absl_nonnull fiber);
73 bool WakeWithoutNotify(Fiber* absl_nullable fiber);
74 void NotifyReady() noexcept;
75 void PreparePark(Fiber* absl_nonnull fiber,
76 std::chrono::steady_clock::time_point deadline);
77 void CancelPark(Fiber* absl_nonnull fiber);
78 void Suspend(Fiber* absl_nonnull fiber);
79
80 const std::thread::id owner_;
81 SchedulerPolicy* absl_nullable const policy_;
82 mutable std::mutex mu_;
83 std::deque<Fiber* absl_nonnull> ready_;
84 std::vector<Fiber* absl_nonnull> fibers_;
85 Fiber* absl_nullable current_ = nullptr;
86 std::uintptr_t root_sp_ = 0;
87};
88
89class Fiber {
90 public:
91 using Work = absl::AnyInvocable<void() &&>;
92 static constexpr std::size_t kDefaultStackBytes = 16 * 1024;
93
94 Fiber(Scheduler* absl_nonnull scheduler, Work work,
95 std::size_t stack_bytes = kDefaultStackBytes);
96
97 template <typename F>
98 requires(std::is_invocable_r_v<void, std::decay_t<F>> &&
99 !std::is_same_v<std::decay_t<F>, Work>)
100 Fiber(Scheduler* absl_nonnull scheduler, F&& work,
101 std::size_t stack_bytes = kDefaultStackBytes)
102 : Fiber(scheduler, Work(std::forward<F>(work)), stack_bytes) {}
103
104 Fiber(const Fiber&) = delete;
105 Fiber& operator=(const Fiber&) = delete;
106 ~Fiber();
107
108 bool Finished() const noexcept {
109 std::lock_guard lock(scheduler_.mu_);
110 return finished_;
111 }
112
113 void Cancel();
114 bool Cancelled() const noexcept;
115 Case OnCancel() const;
116
117 static Fiber* absl_nullable Current() noexcept;
118 static void Yield();
119 static void SleepFor(absl::Duration duration);
120
121 private:
122 friend class Scheduler;
123 friend class Mutex;
124 friend class CondVar;
125 friend void FiberEntry();
126 Scheduler& scheduler_;
127 Work work_;
128 std::unique_ptr<PermanentEvent> cancellation_;
129 std::atomic<bool> cancel_requested_{false};
130 std::unique_ptr<std::uintptr_t[]> stack_;
131 std::uintptr_t stack_sp_ = 0;
132 bool queued_ = false; // guarded by Scheduler::mu_
133 bool waiting_ = false; // guarded by Scheduler::mu_
134 bool finished_ = false;
135 std::chrono::steady_clock::time_point deadline_ =
136 std::chrono::steady_clock::time_point::max();
137};
138
139bool Cancelled();
140Case OnCancel();
141
142} // namespace thread
143
144#endif // SYMBIAN_GUEST_THREAD_FIBER_H_
Definition boost_primitives.h:113
Definition fiber.h:89
Fiber(const Fiber &)=delete
Fiber(Scheduler *absl_nonnull scheduler, F &&work, std::size_t stack_bytes=kDefaultStackBytes)
Definition fiber.h:100
bool Finished() const noexcept
Definition fiber.h:108
absl::AnyInvocable< void() && > Work
Definition fiber.h:91
Fiber & operator=(const Fiber &)=delete
Definition boost_primitives.h:36
Definition selectables.h:17
Definition fiber.h:53
bool HasReady() const
Definition fiber.cc:143
std::chrono::steady_clock::time_point NextDeadline() const
Definition fiber.cc:157
~Scheduler()
Definition fiber.cc:47
absl::Status RunReady(std::size_t max_turns)
Definition fiber.cc:168
friend class Fiber
Definition fiber.h:66
friend void FiberEntry()
Definition fiber.cc:230
Scheduler & operator=(const Scheduler &)=delete
Scheduler(const Scheduler &)=delete
static Scheduler *absl_nullable Current() noexcept
Definition fiber.cc:57
Definition native_camera_stream.cc:19
Definition channel.h:29
bool Cancelled()
Definition fiber.cc:291
Case OnCancel()
Definition fiber.cc:296
void SleepFor(absl::Duration duration)
Definition boost_primitives.h:267
void FiberEntry()
Definition fiber.cc:230
A Case represents a selectable case in a Select statement.
Definition cases.h:57
Definition fiber.h:39
std::function< std::size_t(std::span< Fiber *absl_nonnull const >)> pick_next
Definition fiber.h:40
void NotifyReady() const noexcept
Definition fiber.h:47
std::size_t PickNext(std::span< Fiber *absl_nonnull const > ready) const
Definition fiber.h:43
std::function< void()> notify_ready
Definition fiber.h:41