cortex
0.0.1
Loading...
Searching...
No Matches
fiber.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <
cortex/base_coroutine.hpp
>
4
#include <
cortex/coroutine_suspend_context.hpp
>
5
#include <
cortex/memory_resource.hpp
>
6
7
#include <cstdint>
8
#include <exception>
9
#include <memory>
10
#include <vector>
11
12
#include <function2/function2.hpp>
13
14
namespace
cortex::tiny_fiber::detail
{
15
16
class
Scheduler
;
17
18
// Internal fiber states
19
enum 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().
29
class
Fiber
final :
public
BaseCoroutine
{
30
private
:
31
struct
PrivateTag {};
32
33
public
:
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
88
private
:
89
void
Continuation(
CoroutineSuspendContext
& ctx)
override
;
90
91
// Suspend the coroutine (yields control back to the resumer)
92
void
Suspend();
93
94
private
:
95
Id
id_;
96
FiberState
state_ {
FiberState::Ready
};
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_coroutine.hpp
Base class for object-oriented coroutines.
cortex::BaseCoroutine
An abstract base class for creating object-oriented coroutines.
Definition
base_coroutine.hpp:24
cortex::CoroutineSuspendContext
Provides a mechanism for a coroutine to suspend itself.
Definition
coroutine_suspend_context.hpp:17
cortex::tiny_fiber::Scheduler
Manages cooperative execution of fibers.
Definition
scheduler.hpp:28
cortex::tiny_fiber::detail::Fiber
Definition
fiber.hpp:29
cortex::tiny_fiber::detail::Fiber::SetException
void SetException(std::exception_ptr ex) noexcept
Definition
fiber.hpp:66
cortex::tiny_fiber::detail::Fiber::Fiber
Fiber(Fiber &&)=delete
cortex::tiny_fiber::detail::Fiber::Fiber
Fiber(const Fiber &)=delete
cortex::tiny_fiber::detail::Fiber::operator=
Fiber & operator=(Fiber &&)=delete
cortex::tiny_fiber::detail::Fiber::HasException
bool HasException() const noexcept
Definition
fiber.hpp:58
cortex::tiny_fiber::detail::Fiber::Park
void Park()
cortex::tiny_fiber::detail::Fiber::IsSuspended
bool IsSuspended() const noexcept
Definition
fiber.hpp:54
cortex::tiny_fiber::detail::Fiber::operator=
Fiber & operator=(const Fiber &)=delete
cortex::tiny_fiber::detail::Fiber::Body
fu2::unique_function< void()> Body
Definition
fiber.hpp:35
cortex::tiny_fiber::detail::Fiber::Complete
std::vector< Fiber * > Complete()
cortex::tiny_fiber::detail::Fiber::Run
void Run()
cortex::tiny_fiber::detail::Fiber::Make
static std::unique_ptr< Fiber > Make(Body body, std::size_t stack_size, MemoryResourceSharedPtr resource)
cortex::tiny_fiber::detail::Fiber::Yield
void Yield()
cortex::tiny_fiber::detail::Fiber::AddWaiter
void AddWaiter(Fiber *waiter)
cortex::tiny_fiber::detail::Fiber::GetId
Id GetId() const noexcept
Definition
fiber.hpp:50
cortex::tiny_fiber::detail::Fiber::Id
std::uint64_t Id
Definition
fiber.hpp:34
cortex::tiny_fiber::detail::Fiber::Wake
void Wake()
cortex::tiny_fiber::detail::Fiber::~Fiber
~Fiber() override=default
cortex::tiny_fiber::detail::Fiber::GetException
std::exception_ptr GetException() const noexcept
Definition
fiber.hpp:62
cortex::tiny_fiber::detail::Fiber::Fiber
Fiber(PrivateTag, Id id, Body body, std::size_t stack_size, MemoryResourceSharedPtr resource)
coroutine_suspend_context.hpp
Context for suspending coroutine execution.
memory_resource.hpp
cortex::tiny_fiber::detail
Definition
fiber.hpp:14
cortex::tiny_fiber::detail::FiberState
FiberState
Definition
fiber.hpp:19
cortex::tiny_fiber::detail::FiberState::Running
@ Running
cortex::tiny_fiber::detail::FiberState::Suspended
@ Suspended
cortex::tiny_fiber::detail::FiberState::Finished
@ Finished
cortex::tiny_fiber::detail::FiberState::Ready
@ Ready
cortex::MemoryResourceSharedPtr
std::shared_ptr< MemoryResource > MemoryResourceSharedPtr
Definition
memory_resource.hpp:30
include
cortex
tiny_fiber
detail
fiber.hpp
Generated by
1.9.8