cortex 0.0.1
Loading...
Searching...
No Matches
scheduler.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <deque>
7#include <memory>
8#include <unordered_map>
9
15namespace cortex::tiny_fiber {
16
28class Scheduler {
29public:
38
46 template <typename F>
47 static void Run(F&& entry);
48
55 template <typename F>
56 static void Run(F&& entry, Config config);
57
67 template <typename F>
68 static std::unique_ptr<Scheduler> Create(F&& entry);
69
73 template <typename F>
74 static std::unique_ptr<Scheduler> Create(F&& entry, Config config);
75
84 static Scheduler& Current();
85
86 Scheduler(const Scheduler&) = delete;
87 Scheduler& operator=(const Scheduler&) = delete;
88 Scheduler(Scheduler&&) = delete;
91
100 bool Step();
101
106 return ready_queue_.empty() && !current_fiber_;
107 }
108
113 return config_.default_stack_size;
114 }
115
122
127 return running_;
128 }
129
136 return stopping_;
137 }
138
145 void Stop();
146
150
151private:
152 friend class detail::Fiber;
153 template <typename T>
154 friend class Future;
155 friend class Mutex;
156 friend class ConditionVariable;
157 friend void Yield();
158 friend bool YieldIfOthersReady();
159
160 explicit Scheduler(Config config);
161
162 void RunLoop();
163
164 // Get fiber by ID
165 detail::Fiber* GetFiber(detail::Fiber::Id id);
166
167 // Get currently running fiber
168 detail::Fiber* GetCurrentFiber() {
169 return current_fiber_;
170 }
171
172 // Wake a suspended fiber and enqueue it to run
173 void Schedule(detail::Fiber* fiber);
174
175 // Suspend current fiber
176 void SuspendCurrent();
177
178 // Yield current fiber (put back in ready queue)
179 void YieldCurrent();
180
181 // Check if there are other ready fibers
182 bool HasOtherReadyFibers() const;
183
184 // Process pending fiber cleanup
185 void ProcessPendingCleanup();
186
187private:
188 Config config_;
189 bool running_ {false};
190 bool stopping_ {false};
191 detail::Fiber* current_fiber_ {nullptr};
192 std::deque<detail::Fiber*> ready_queue_;
193 std::unordered_map<detail::Fiber::Id, std::unique_ptr<detail::Fiber>> fibers_;
194 std::vector<detail::Fiber::Id> pending_cleanup_;
195};
196
197// Template implementations
198template <typename F>
200 Run(std::forward<F>(entry), Config {});
201}
202
203template <typename F>
205 Scheduler scheduler(std::move(config));
206 scheduler.SpawnFiberInternal(std::forward<F>(entry), scheduler.config_.default_stack_size);
207 scheduler.RunLoop();
208}
209
210template <typename F>
211std::unique_ptr<Scheduler> Scheduler::Create(F&& entry) {
212 return Create(std::forward<F>(entry), Config {});
213}
214
215template <typename F>
216std::unique_ptr<Scheduler> Scheduler::Create(F&& entry, Config config) {
217 std::unique_ptr<Scheduler> scheduler(new Scheduler(std::move(config)));
218 scheduler->SpawnFiberInternal(std::forward<F>(entry), scheduler->config_.default_stack_size);
219 return scheduler;
220}
221
222} // namespace cortex::tiny_fiber
static constexpr std::size_t kDefaultStackSizeBytes
Definition coroutine.hpp:30
A cooperative condition variable.
Definition condition_variable.hpp:21
Handle to a spawned fiber that returns a value.
Definition future.hpp:46
A cooperative mutex that yields instead of blocking.
Definition mutex.hpp:21
Manages cooperative execution of fibers.
Definition scheduler.hpp:28
static std::unique_ptr< Scheduler > Create(F &&entry)
Create a scheduler for manual stepping (WASM/async integration).
Definition scheduler.hpp:211
MemoryResourceSharedPtr GetMemoryResource() const noexcept
Get the memory resource used by this scheduler.
Definition scheduler.hpp:119
Scheduler(Scheduler &&)=delete
void Stop()
Signal all fibers to stop and wake suspended ones.
Scheduler & operator=(Scheduler &&)=delete
bool IsDone() const noexcept
Check if all fibers have completed.
Definition scheduler.hpp:105
friend bool YieldIfOthersReady()
Yield only if there are other ready fibers.
static Scheduler & Current()
Get the current scheduler.
bool Step()
Run one step of the scheduler.
static void Run(F &&entry)
Run the scheduler with an initial fiber.
Definition scheduler.hpp:199
bool IsStopping() const noexcept
Check if the scheduler is stopping (being destroyed).
Definition scheduler.hpp:135
bool IsRunning() const noexcept
Check if the scheduler is currently running.
Definition scheduler.hpp:126
friend class Future
Definition scheduler.hpp:154
std::size_t GetDefaultStackSize() const noexcept
Get the default stack size for new fibers.
Definition scheduler.hpp:112
Scheduler(const Scheduler &)=delete
Scheduler & operator=(const Scheduler &)=delete
friend void Yield()
Yield control to other ready fibers.
Definition fiber.hpp:29
fu2::unique_function< void()> Body
Definition fiber.hpp:35
std::uint64_t Id
Definition fiber.hpp:34
Cooperative multitasking primitives built on cortex::Coroutine.
Definition condition_variable.hpp:13
MemoryResourceSharedPtr GetDefaultMemoryResource()
std::shared_ptr< MemoryResource > MemoryResourceSharedPtr
Definition memory_resource.hpp:30
Configuration options for the scheduler.
Definition scheduler.hpp:34
std::size_t default_stack_size
Definition scheduler.hpp:35
MemoryResourceSharedPtr memory_resource
Definition scheduler.hpp:36