4#include "include/types.hpp"
5#include "include/timer.hpp"
6#include "include/macro_overload.hpp"
27 const state BEGINNING = 0;
39 state pos{ BEGINNING };
42 state operator()(
void)
const
59 handle( handle
const& ) =
delete;
60 void operator=( handle
const& ) =
delete;
84 explicit operator bool() const noexcept {
85 return (
nullptr != ctx );
87 friend class co::coContext;
91 class semaphore final {
94 void signal(
void )
noexcept;
95 bool tryLock(
void )
noexcept;
96 semaphore( semaphore
const& ) =
delete;
97 void operator=( semaphore
const& ) =
delete;
116 void set(
size_t val )
noexcept;
117 friend class co::coContext;
122 class coContext final {
124 coContext( coContext
const& ) =
delete;
125 void operator=( coContext
const& ) =
delete;
129 coContext() =
default;
130 inline void saveHandle(
co::handle& h )
noexcept
134 inline static void saveHandle(
void )
noexcept {}
135 inline static void semSignal( semaphore& s )
noexcept
139 inline static bool semTrylock( semaphore& s )
noexcept
143 inline coContext& operator=( co::state l )
148 inline co::state operator()(
void )
const
156 inline void operator()( coAction action )
158 if ( coAction::CO_TIMEOUT_DISARM == action ) {
162 inline bool operator==(
const co::state l )
const
166 inline bool operator!=(
const co::state l )
const
170 inline bool timeout(
void )
const
178 inline void crNOP(
void )
noexcept {}
220 inline void yield(
void )
noexcept {}
240 inline void waitUntil(
bool condition )
noexcept { Q_UNUSED(condition); }
364 inline void until(
bool condition )
noexcept { Q_UNUSED(condition); }
372#define q_co_label ( __LINE__ )
375#define reenter_0() q_co_reenter( Q_NONE )
376#define reenter_1(h) q_co_reenter( h )
377#define reenter(...) MACRO_OVERLOAD( reenter_ , __VA_ARGS__ )
381#define q_co_reenter( h ) \
383static qOS::co::coContext co_ctx; \
384co_ctx.saveHandle( h ); \
385for ( ; co_ctx != qOS::co::SUSPENDED ; co_ctx = qOS::co::SUSPENDED ) \
387 goto q_co_continue; \
397 switch ( co_ctx() ) \
401#define q_co_SaveRestore( label, init_action, pos_label_action ) \
403for ( co_ctx = (label) ;; ) \
410 else goto q_co_break \
414#define q_co_cond( c ) \
420#define q_co_t_cond( c ) \
421if ( !( (c) || co_ctx.timeout() ) ) { \
425#define yield() q_co_yield( q_co_label )
426#define q_co_yield(label) \
428q_co_SaveRestore( label, qOS::co::crNOP(), Q_NONE ) \
431#define delay( t ) q_co_delay( q_co_label , t)
432#define q_co_delay( label, t ) \
434q_co_SaveRestore( label, co_ctx(t) , q_co_t_cond(0) ) \
437#define q_co_wu_1( c ) q_co_waitUntil( q_co_label , c )
438#define q_co_waitUntil( label, c ) \
440q_co_SaveRestore( label, qOS::co::crNOP(), q_co_cond(c) ) \
442#define q_co_wu_2( c, t ) q_co_timedWaitUntil( q_co_label , c, t )
443#define q_co_timedWaitUntil( label, c, t ) \
445q_co_SaveRestore( label, qOS::co::crNOP(), q_co_t_cond(c) ) \
447#define waitUntil(...) MACRO_OVERLOAD( q_co_wu_ , __VA_ARGS__ )
449#define timeoutExpired() timeoutExpired() || co_ctx.timeout()
452#define restart() q_co_restart
453#define q_co_restart \
455co_ctx = qOS::co::BEGINNING; \
459#define semWait( sem ) \
461q_co_SaveRestore( q_co_label, qOS::co::crNOP(), q_co_cond( co_ctx.semTrylock( sem )) ) \
464#define semSignal( sem ) \
466co_ctx.semSignal( sem ) \
469#define getPosition( var ) q_co_get_pos( var, q_co_label )
470#define q_co_get_pos( var, label ) \
473case ( label ) : qOS::co::crNOP() \
476#define setPosition( var ) co_res_pos( var, q_co_label )
477#define co_res_pos( var, label ) \
483#define perform_0() q_co_perform( co::coAction::CO_TIMEOUT_DISARM )
484#define perform_1( t ) q_co_perform( t )
485#define perform(...) MACRO_OVERLOAD( perform_ , __VA_ARGS__ )
487#define q_co_perform( t ) \
489q_co_SaveRestore( q_co_label, co_ctx(t), Q_NONE ); \
494q_co_cond( ( c ) || co_ctx.timeout() ) \
A Co-Routine handle.
Definition coroutine.hpp:55
void try_restart(void) noexcept
Try to execute the co::restart() statement externally.
void try_resume(void) noexcept
Try to resume the coroutine execution externally after a suspend operation.
void try_suspend(void) noexcept
Try to suspend the coroutine execution externally.
friend class co::coContext
Definition coroutine.hpp:87
void try_set(co::state p) noexcept
Try to execute co::setPosition() statement externally.
A placeholder for the Co-Routine current position or progress.
Definition coroutine.hpp:38
A Co-Routine Semaphore.
Definition coroutine.hpp:91
semaphore(size_t init)
Initializes a coroutine semaphore with a value for the counter. Internally, the semaphores use an siz...
Definition coroutine.hpp:107
void set(size_t val) noexcept
Set the coroutine semaphore with a value for the counter. Internally, the semaphores use an size_t to...
A non-blocking Timer object.
Definition timer.hpp:26
bool set(const qOS::duration_t tTime) noexcept
Set the expiration time for a timer. On success, the timer gets armed immediately.
bool expired(void) const noexcept
Non-Blocking timer check.
void disarm(void) noexcept
Disarms the timer object.
timeCount_t duration_t
The typedef that specified an time quantity, usually expressed in milliseconds.
Definition clock.hpp:18
bool timeoutExpired(void) noexcept
Check if the internal Co-routine timeout expires.
Definition coroutine.hpp:271
void restart(void) noexcept
This statement cause the running Coroutine to restart its execution at the place of the co::reenter()...
Definition coroutine.hpp:281
void delay(qOS::duration_t t) noexcept
Delay a coroutine for a given number of time.
Definition coroutine.hpp:226
base_t state
The intrinsic type of co::position to hold a coroutine progress.
Definition coroutine.hpp:21
void perform(void) noexcept
This statement start a blocking Job segment.
Definition coroutine.hpp:331
void semWait(co::semaphore &sem) noexcept
Carries out the "wait" operation on the semaphore. The wait operation causes the Co-routine to block ...
Definition coroutine.hpp:290
void until(bool condition) noexcept
This statement ends a blocking Job segment starting with the co::perform() statement.
Definition coroutine.hpp:364
void yield(void) noexcept
This statement is only allowed inside a Coroutine segment. co::yield return the CPU control back to t...
Definition coroutine.hpp:220
void reenter(void) noexcept
Defines a Coroutine segment. The co::reenter() statement is used to declare the starting point of a C...
Definition coroutine.hpp:194
void getPosition(co::position &var) noexcept
Labels the current position and saves it to var so it can be later restored by co::setPosition()
Definition coroutine.hpp:308
void setPosition(co::position &var) noexcept
Restores the Co-Routine position saved in var.
Definition coroutine.hpp:316
void waitUntil(bool condition) noexcept
Yields until the logical condition is met.
Definition coroutine.hpp:240
void semSignal(co::semaphore &sem) noexcept
Carries out the "signal" operation on the semaphore. The signal operation increments the counter insi...
Definition coroutine.hpp:299
@ SUSPENDED
Definition task.hpp:90
@ UNDEFINED
Definition task.hpp:87
int base_t
A type to instantiate a integer-base variable. This size of this type is implementation-defined.
Definition types.hpp:62
CoRoutines interfaces.
Definition coroutine.hpp:13
OS/Kernel interfaces.
Definition bytebuffer.hpp:7