cortex 0.0.1
Loading...
Searching...
No Matches
fiber.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <cstdint>
8#include <exception>
9#include <memory>
10#include <vector>
11
12#include <function2/function2.hpp>
13
15
16class Scheduler;
17
18// Internal fiber states
19enum class FiberState : std::uint8_t {
20 Ready, // In ready queue, waiting to run
21 Running, // Currently executing
22 Suspended, // Waiting for something (Future, Mutex, CondVar)
23 Finished // Completed execution
24};
25
26// Internal fiber representation.
27// Inherits from BaseCoroutine to get proper coroutine lifecycle management.
28// The user's function is stored and called from Continuation().
29class Fiber final : public BaseCoroutine {
30private:
31 struct PrivateTag {};
32
33public:
34 using Id = std::uint64_t;
35 using Body = fu2::unique_function<void()>;
36
37 // Create a new fiber with the given body, stack size, and memory resource.
38 static std::unique_ptr<Fiber> Make(Body body, std::size_t stack_size, MemoryResourceSharedPtr resource);
39
40 // Constructor is public but requires PrivateTag (only Make can call it)
41 Fiber(PrivateTag, Id id, Body body, std::size_t stack_size, MemoryResourceSharedPtr resource);
42
43 ~Fiber() override = default;
44
45 Fiber(const Fiber&) = delete;
46 Fiber& operator=(const Fiber&) = delete;
47 Fiber(Fiber&&) = delete;
48 Fiber& operator=(Fiber&&) = delete;
49
50 [[nodiscard]] Id GetId() const noexcept {
51 return id_;
52 }
53
54 [[nodiscard]] bool IsSuspended() const noexcept {
55 return state_ == FiberState::Suspended;
56 }
57
58 [[nodiscard]] bool HasException() const noexcept {
59 return exception_ != nullptr;
60 }
61
62 [[nodiscard]] std::exception_ptr GetException() const noexcept {
63 return exception_;
64 }
65
66 void SetException(std::exception_ptr ex) noexcept {
67 exception_ = std::move(ex);
68 }
69
70 // Run this fiber (Ready -> Running, resumes coroutine execution)
71 void Run();
72
73 // Yield control back to the scheduler (Running -> Ready, suspends)
74 void Yield();
75
76 // Park until woken by another fiber (Running -> Suspended, suspends)
77 void Park();
78
79 // Wake a parked fiber (Suspended -> Ready)
80 void Wake();
81
82 // Mark fiber as finished and return its waiters (Running -> Finished)
83 std::vector<Fiber*> Complete();
84
85 // Add a fiber that is waiting for this fiber to finish
86 void AddWaiter(Fiber* waiter);
87
88private:
89 void Continuation(CoroutineSuspendContext& ctx) override;
90
91 // Suspend the coroutine (yields control back to the resumer)
92 void Suspend();
93
94private:
95 Id id_;
97 Body body_;
98 CoroutineSuspendContext* suspend_ctx_ {nullptr};
99 std::exception_ptr exception_ {nullptr};
100 std::vector<Fiber*> waiters_;
101};
102
103} // namespace cortex::tiny_fiber::detail
Base class for object-oriented coroutines.
An abstract base class for creating object-oriented coroutines.
Definition base_coroutine.hpp:24
Provides a mechanism for a coroutine to suspend itself.
Definition coroutine_suspend_context.hpp:17
Manages cooperative execution of fibers.
Definition scheduler.hpp:28
Definition fiber.hpp:29
void SetException(std::exception_ptr ex) noexcept
Definition fiber.hpp:66
Fiber(const Fiber &)=delete
Fiber & operator=(Fiber &&)=delete
bool HasException() const noexcept
Definition fiber.hpp:58
bool IsSuspended() const noexcept
Definition fiber.hpp:54
Fiber & operator=(const Fiber &)=delete
fu2::unique_function< void()> Body
Definition fiber.hpp:35
std::vector< Fiber * > Complete()
static std::unique_ptr< Fiber > Make(Body body, std::size_t stack_size, MemoryResourceSharedPtr resource)
Id GetId() const noexcept
Definition fiber.hpp:50
std::uint64_t Id
Definition fiber.hpp:34
std::exception_ptr GetException() const noexcept
Definition fiber.hpp:62
Fiber(PrivateTag, Id id, Body body, std::size_t stack_size, MemoryResourceSharedPtr resource)
Context for suspending coroutine execution.
Definition fiber.hpp:14
FiberState
Definition fiber.hpp:19
std::shared_ptr< MemoryResource > MemoryResourceSharedPtr
Definition memory_resource.hpp:30