47#define STK_SIZE( size ) \
48 ALIGNED_SIZE((size_t)( size ) + (OS_GUARD_SIZE), sizeof(stk_t))
50#define STK_OVER( size ) \
51 ALIGNED((size_t)( size ) + (OS_GUARD_SIZE), sizeof(stk_t))
53#define STK_CROP( base, size ) \
54 LIMITED((size_t)( base ) + (size_t)( size ), sizeof(stk_t))
143typedef struct __tsk tsk_id [];
163#define _TSK_INIT( _proc, _stack, _size ) \
164 { _OBJ_INIT(), _HDR_INIT(), _proc, NULL, 0, 0, 0, _stack, _size, \
165 NULL, 0, { NULL }, { { 0 } }, { _CTX_INIT() } }
183#define _TSK_STACK( _size ) (stk_t [STK_SIZE( _size )]) { 0 }
196#define _VA_STK( _size ) ((_size + 0) ? (_size + 0) : (OS_STACK_SIZE))
205#define OS_TSK_STACK( stk, ... ) \
206 stk_t stk[ STK_SIZE( _VA_STK(__VA_ARGS__) ) ] __STKALIGN
209#define static_TSK_STACK( stk, ... ) \
210 static stk_t stk[ STK_SIZE( _VA_STK(__VA_ARGS__) ) ] __STKALIGN
221#define OS_WRK( tsk, proc, size ) \
222 static stk_t tsk##__stk[STK_SIZE( size )] __STKALIGN; \
223 tsk_t tsk[] = { _TSK_INIT( proc, tsk##__stk, STK_OVER( size ) ) }
226#define static_WRK( tsk, proc, size ) \
227 static stk_t tsk##__stk[STK_SIZE( size )] __STKALIGN; \
228 static tsk_t tsk[] = { _TSK_INIT( proc, tsk##__stk, STK_OVER( size ) ) }
239#define OS_TSK( tsk, proc, ... ) \
240 OS_WRK( tsk, proc, _VA_STK(__VA_ARGS__) )
243#define static_TSK( tsk, proc, ... ) \
244 static_WRK( tsk, proc, _VA_STK(__VA_ARGS__) )
255#define OS_WRK_DEF( tsk, size ) \
256 static void tsk##__fun( void ); \
257 OS_WRK( tsk, tsk##__fun, size ); \
258 void tsk##__fun( void )
261#define static_WRK_DEF( tsk, size ) \
262 static void tsk##__fun( void ); \
263 static_WRK( tsk, tsk##__fun, size ); \
264 static void tsk##__fun( void )
275#define OS_TSK_DEF( tsk, ... ) \
276 OS_WRK_DEF( tsk, _VA_STK(__VA_ARGS__) )
279#define static_TSK_DEF( tsk, ... ) \
280 static_WRK_DEF( tsk, _VA_STK(__VA_ARGS__) )
294#define OS_WRK_START( tsk, size ) \
295 static void tsk##__fun( void ); \
296 OS_WRK( tsk, tsk##__fun, size ); \
297 __CONSTRUCTOR static void tsk##__run( void ) { tsk_start(tsk); } \
298 static void tsk##__fun( void )
303#define static_WRK_START( tsk, size ) \
304 static void tsk##__fun( void ); \
305 static_WRK( tsk, tsk##__fun, size ); \
306 __CONSTRUCTOR static void tsk##__run( void ) { tsk_start(tsk); } \
307 static void tsk##__fun( void )
322#define OS_TSK_START( tsk, ... ) \
323 OS_WRK_START( tsk, _VA_STK(__VA_ARGS__) )
328#define static_TSK_START( tsk, ... ) \
329 static_WRK_START( tsk, _VA_STK(__VA_ARGS__) )
345#define WRK_INIT( proc, size ) \
346 _TSK_INIT( proc, _TSK_STACK( size ), STK_OVER( size ) )
362#define WRK_CREATE( proc, size ) \
363 { WRK_INIT ( proc, size ) }
382#define TSK_INIT( proc, ... ) \
383 WRK_INIT( proc, _VA_STK(__VA_ARGS__) )
399#define TSK_CREATE( proc, ... ) \
400 WRK_CREATE( proc, _VA_STK(__VA_ARGS__) )
417tsk_t *
tsk_this(
void ) {
return System.cur; }
421tsk_t *
cur_task(
void ) {
return System.cur; }
424#define SELF ( System.cur )
438void wrk_init( tsk_t *tsk,
fun_t *proc, stk_t *stack,
size_t size );
451void tsk_init( tsk_t *tsk,
fun_t *proc, stk_t *stack,
size_t size );
519void tsk_yield(
void ) { core_ctx_switch(); }
523void tsk_pass (
void ) { core_ctx_switch(); }
619 assert_tsk_context();
620 return core_stk_space(System.cur);
632#if defined(__cplusplus)
648template<
size_t size_>
651 static_assert(size_>0,
"incorrect stack size");
652#if __cplusplus >= 201703L && !defined(__ICCARM__)
653 stk_t stack_[
STK_SIZE(size_)] __STKALIGN;
675struct baseTask :
public __tsk
677#if __cplusplus >= 201402L
679 baseTask( F&& _proc, stk_t *
const _stack,
const size_t _size ): __tsk _TSK_INIT(fun_, _stack, _size), fun{_proc} {}
681 baseTask(
fun_t *_proc, stk_t *
const _stack,
const size_t _size ): __tsk _TSK_INIT(_proc, _stack, _size) {}
685#if __cplusplus >= 201402L
687 void startFrom( F&& _proc )
689 if (__tsk::hdr.
id == ID_STOPPED)
703 operator bool ()
const {
return __tsk::hdr.id != ID_STOPPED; }
705 template<
class T = baseTask>
static
706 T * current () {
return static_cast<T *
>(
tsk_this()); }
708#if __cplusplus >= 201402L
710 void fun_ () { current()->fun(); }
727 void kill () {
tsk_kill (current()); }
732#if __cplusplus >= 201402L
733 template<
class F>
static
734 void flip ( F&& _proc ) { current()->fun = Fun_t(_proc);
740 template<
typename T>
static
742 template<
typename T>
static
744 template<
typename T>
static
748 template<
typename T>
static
755using thisTask = baseTask::Current;
756using this_task = baseTask::Current;
767template<
size_t size_>
768struct TaskT :
public baseTask,
public baseStack<size_>
773 baseTask{_proc, baseStack<size_>::stack_, sizeof(baseStack<size_>::stack_)} {}
775#if __cplusplus >= 202002L
777 template<
typename F,
typename... A>
778 TaskT( F&& _proc, A&&... _args ):
779 TaskT<size_>{[proc = std::forward<F>(_proc), ...args = std::forward<A>(_args)]()
mutable
780 { std::invoke(std::move(proc), std::unwrap_reference_t<A>(std::move(args))...); }} {}
782#if __cplusplus >= 201402L
784 template<
typename F,
typename... A>
785 TaskT( F&& _proc, A&&... _args ):
786 TaskT<size_>{std::bind(std::forward<F>(_proc), std::forward<A>(_args)...)} {}
790 ~TaskT() { assert(__tsk::hdr.
id == ID_STOPPED); }
792#if __cplusplus >= 201402L
795 baseTask{_src.fun, baseStack<size_>::stack_, sizeof(baseStack<size_>::stack_)}
799 baseTask{_src.proc, baseStack<size_>::stack_, sizeof(baseStack<size_>::stack_)}
802 assert(_src.hdr.id == ID_STOPPED);
812 template<
typename... A>
813 TaskT( Starter, A&&... _args ):
TaskT<size_>{
static_cast<A&&
>(_args)...} { this->start(); }
828 template<
class F>
static
834#if __cplusplus >= 201402L
836 template<
typename F,
typename... A>
static
839 return { std::forward<F>(_proc), std::forward<A>(_args)... };
855 template<
class F>
static
858 return { Starter{}, _proc };
861#if __cplusplus >= 201402L
863 template<
typename F,
typename... A>
static
866 return { Starter{}, std::forward<F>(_proc), std::forward<A>(_args)... };
877using Task = TaskT<OS_STACK_SIZE>;
void fun_a(void *)
timer/task procedure with argument
Definition osbase.h:143
#define INFINITE
infinite waiting
Definition osbase.h:250
void fun_t(void)
timer/task procedure
Definition osbase.h:142
char data[]
message data
Definition osmessagequeue.h:54
void tsk_startWith(tsk_t *tsk, fun_a *proc, void *arg)
start previously defined/created/stopped task object
void tsk_exit(void)
alias of tsk_stop()
Definition ostask.h:491
void tsk_start(tsk_t *tsk)
start previously defined/created/stopped task object
void tsk_reset(tsk_t *tsk)
stop execution of given task
int tsk_suspend(tsk_t *tsk)
delay indefinitely execution of given task
void tsk_flip(fun_t *proc)
restart current task with the new proc (task function)
void tsk_sleep(void)
delay indefinitely execution of current task
Definition ostask.h:577
void tsk_yield(void)
yield system control to the next task
Definition ostask.h:518
void tsk_sleepUntil(cnt_t time)
delay execution of current task until given timepoint
void tsk_init(tsk_t *tsk, fun_t *proc, stk_t *stack, size_t size)
initialize complete work area for task object and start the task, if proc != NULL
void tsk_delay(cnt_t delay)
alias of tsk_sleepFor()
Definition ostask.h:547
size_t tsk_stackSpace(void)
chack water mark of the stack of the current task
Definition ostask.h:615
tsk_t * cur_task(void)
alias of tsk_this()
Definition ostask.h:420
void wrk_init(tsk_t *tsk, fun_t *proc, stk_t *stack, size_t size)
initialize complete work area for task object
void tsk_pass(void)
alias of tsk_yield()
Definition ostask.h:522
void tsk_stop(void)
stop execution of current task
void tsk_sleepFor(cnt_t delay)
delay execution of current task for given duration of time
void tsk_startFrom(tsk_t *tsk, fun_t *proc)
start previously defined/created/stopped task object
int tsk_resume(tsk_t *tsk)
resume execution of given suspended task
tsk_t * tsk_this(void)
return current task object
Definition ostask.h:416
void tsk_kill(tsk_t *tsk)
alias of tsk_reset()
Definition ostask.h:511
void tsk_sleepNext(cnt_t delay)
delay execution of current task for given duration of time
#define STK_SIZE(size)
size of the task stack in stk_t units: the size in bytes (rounded up) with the guard area
Definition ostask.h:47
static constexpr rep until(const std::chrono::time_point< Clock, _Duration > &_time)
time point in ticks, rounded up to whole ticks (only a time point of this clock)
Definition osclock.h:109
static constexpr rep count(const std::chrono::duration< _Rep, _Period > &_delay)
delay in ticks, rounded up to whole ticks; negative delay: IMMEDIATE, delay out of range: INFINITE
Definition osclock.h:102
create and initialize complete work area for task object
Definition ostask.h:768
static TaskT< size_ > Start(F &&_proc)
Definition ostask.h:855
static TaskT< size_ > Make(F &&_proc)
Definition ostask.h:828
TaskT(F &&_proc)
constructor, see the constructor parameters of the class
Definition ostask.h:771