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