Nebula
Loading...
Searching...
No Matches
fibers.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
10//------------------------------------------------------------------------------
11#include "fiber.h"
12#include "ids/id.h"
13#include "ids/idallocator.h"
14#include "threading/thread.h"
15#include "threading/safequeue.h"
17
18namespace Fibers
19{
20
22{
24 volatile int* counter;
25};
26
28{
30 volatile int* counter;
31 int value;
32};
33
35{
37public:
38
42 virtual ~FiberThread();
43
45 void EmitWakeupSignal();
47 void DoWork();
49 bool HasWork();
50
52 void SleepFiber(volatile int* counter, int value);
54 void NewFiber();
56 void SwitchFiber();
57
58private:
61};
62
63thread_local extern FiberThread* currentThread;
64
70
72{
73public:
74
75 typedef void (*JobFunction)(void* context);
76 typedef void (*FiberFunction)();
77
85
87 FiberQueue();
89 virtual ~FiberQueue();
90
92 static void Setup(const FiberQueueCreateInfo& info);
94 static void Discard();
96 template <class T> static void Enqueue(JobFunction function, const Util::FixedArray<T*>& contexts, Threading::AtomicCounter* counter);
98 static void Free(uint id);
100 static void Sleep(FiberWaitContext context);
102 static bool WakeupFiber(Fibers::FiberContext& fiber);
104 static bool Dequeue(Fibers::FiberContext& fiber);
105
106private:
110
115
119};
120
121//------------------------------------------------------------------------------
124template <class T>
125inline void
126FiberQueue::Enqueue(JobFunction function, const Util::FixedArray<T*>& contexts, Threading::AtomicCounter* counter)
127{
128 Threading::Interlocked::Exchange(counter, contexts.Size());
129
130 for (uint i = 0; i < contexts.Size(); i++)
131 {
132 Job job;
133 job.function = function;
134 job.context = contexts[i];
135 job.counter = counter;
136 FiberQueue::PendingJobsQueue.Enqueue(job);
137 }
138}
139
140//------------------------------------------------------------------------------
143template <class T>
144inline void
146{
147 FiberQueue::Enqueue(function, contexts, counter);
148}
149
150//------------------------------------------------------------------------------
153inline void
154Wait(Threading::AtomicCounter* counter, int value)
155{
156 if (*counter != value)
157 {
158 currentThread->SleepFiber(counter, value);
159 }
160}
161
162//------------------------------------------------------------------------------
165inline void
166Lock(Threading::AtomicCounter* counter, int value)
167{
168 while (*counter != value) {};
169}
170
171} // namespace Fibers
Definition fiber.h:14
Definition fibers.h:72
void(*) FiberFunction()
Definition fibers.h:76
FiberQueue()
constructor
Definition fibers.cc:125
static void Discard()
discard job queue
Definition fibers.cc:162
static Threading::LockFreeQueue< Job > PendingJobsQueue
async queues
Definition fibers.h:108
void(*) JobFunction(void *context)
Definition fibers.h:75
static Util::FixedArray< FiberQueue::Job > FiberContexts
Definition fibers.h:113
static void Sleep(FiberWaitContext context)
sleep fiber
Definition fibers.cc:184
static Util::FixedArray< Ptr< FiberThread > > Threads
Definition fibers.h:114
virtual ~FiberQueue()
destructor
Definition fibers.cc:132
static Util::FixedArray< Fibers::Fiber > Fibers
storage
Definition fibers.h:112
static Threading::LockFreeQueue< uint > FiberIdQueue
Definition fibers.h:109
static void Enqueue(JobFunction function, const Util::FixedArray< T * > &contexts, Threading::AtomicCounter *counter)
enqueue job
Definition fibers.h:126
static void Setup(const FiberQueueCreateInfo &info)
setup job queue
Definition fibers.cc:140
static Util::Array< FiberWaitContext > SleepingFibers
Definition fibers.h:118
static Threading::CriticalSection SleepLock
sleeping fibers
Definition fibers.h:117
static bool Dequeue(Fibers::FiberContext &fiber)
dequeue job as a fiber
Definition fibers.cc:241
static bool WakeupFiber(Fibers::FiberContext &fiber)
wake up sleeping fibers
Definition fibers.cc:194
static void Free(uint id)
free a fiber
Definition fibers.cc:175
Definition fibers.h:35
void NewFiber()
switches the current fiber
Definition fibers.cc:87
bool HasWork()
returns true if thread has work
Definition fibers.cc:66
void SleepFiber(volatile int *counter, int value)
sleeps the current fiber and switches it for a new one
Definition fibers.cc:75
FiberThread()
constructor
Definition fibers.cc:18
__DeclareClass(FiberThread)
void EmitWakeupSignal()
called if thread needs a wakeup call before stopping
Definition fibers.cc:35
Fibers::FiberContext currentFiber
Definition fibers.h:59
void SwitchFiber()
switch back to original fiber
Definition fibers.cc:108
virtual ~FiberThread()
destructor
Definition fibers.cc:26
void DoWork()
this method runs in the thread context
Definition fibers.cc:43
Fibers::Fiber threadFiber
Definition fibers.h:60
Critical section objects are used to protect a portion of code from parallel execution.
Definition lockfreequeue.h:78
Nebula's dynamic array class.
Definition array.h:60
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
const SizeT Size() const
get number of elements
Definition fixedarray.h:532
Fiber implementation header.
Definition fiber.h:11
void Wait(Threading::AtomicCounter *counter, int value)
Definition fibers.h:154
thread_local FiberThread * currentThread
Definition fibers.cc:12
void Lock(Threading::AtomicCounter *counter, int value)
Definition fibers.h:166
void Enqueue(FiberQueue::JobFunction function, const Util::FixedArray< T * > &contexts, Threading::AtomicCounter *counter)
Definition fibers.h:145
int Exchange(int volatile *dest, int value)
interlocked exchange
Definition gccinterlocked.cc:94
volatile int AtomicCounter
Definition interlocked.h:19
Definition fibers.h:22
Fibers::Fiber * fiber
Definition fibers.h:23
volatile int * counter
Definition fibers.h:24
Definition fibers.h:79
Threading::AtomicCounter * counter
Definition fibers.h:83
uint id
Definition fibers.h:81
JobFunction function
Definition fibers.h:80
void * context
Definition fibers.h:82
Definition fibers.h:66
uint numFibers
Definition fibers.h:68
uint numThreads
Definition fibers.h:67
Definition fibers.h:28
int value
Definition fibers.h:31
Fibers::Fiber * fiber
Definition fibers.h:29
volatile int * counter
Definition fibers.h:30
unsigned int uint
Definition types.h:31