32#ifndef __STATEOS_TSK_H
33#define __STATEOS_TSK_H
39#include "oscriticalsection.h"
50#define STK_SIZE( size ) \
51 ALIGNED_SIZE((size_t)( size ) + (OS_GUARD_SIZE), sizeof(stk_t))
53#define STK_OVER( size ) \
54 ALIGNED((size_t)( size ) + (OS_GUARD_SIZE), sizeof(stk_t))
56#define STK_CROP( base, size ) \
57 LIMITED((size_t)( base ) + (size_t)( size ), sizeof(stk_t))
162#ifndef _PORT_DATA_INIT
163#define _PORT_DATA_INIT()
169typedef struct __tsk tsk_id [];
190#define _TSK_INIT( _prio, _proc, _stack, _size ) \
191 { _OBJ_INIT(), _HDR_INIT(), _proc, NULL, 0, 0, 0, _stack, _size, NULL, _prio, _prio, NULL, NULL, 0, \
192 { NULL, NULL }, { 0, NULL, 0, { NULL, 0, 0, 0, { { 0 } } } }, { { 0 } }, _PORT_DATA_INIT() }
210#define _TSK_STACK( _size ) (stk_t [STK_SIZE( _size )]) { 0 }
223#define _VA_STK( _size ) ((_size + 0) ? (_size + 0) : (OS_STACK_SIZE))
232#define OS_TSK_STACK( stk, ... ) \
233 stk_t stk[ STK_SIZE( _VA_STK(__VA_ARGS__) ) ] __STKALIGN
236#define static_TSK_STACK( stk, ... ) \
237 static stk_t stk[ STK_SIZE( _VA_STK(__VA_ARGS__) ) ] __STKALIGN
249#define OS_WRK( tsk, prio, proc, size ) \
250 static stk_t tsk##__stk[STK_SIZE( size )] __STKALIGN; \
251 tsk_t tsk[] = { _TSK_INIT( prio, proc, tsk##__stk, STK_OVER( size ) ) }
254#define static_WRK( tsk, prio, proc, size ) \
255 static stk_t tsk##__stk[STK_SIZE( size )] __STKALIGN; \
256 static tsk_t tsk[] = { _TSK_INIT( prio, proc, tsk##__stk, STK_OVER( size ) ) }
268#define OS_TSK( tsk, prio, proc, ... ) \
269 OS_WRK( tsk, prio, proc, _VA_STK(__VA_ARGS__) )
272#define static_TSK( tsk, prio, proc, ... ) \
273 static_WRK( tsk, prio, proc, _VA_STK(__VA_ARGS__) )
285#define OS_WRK_DEF( tsk, prio, size ) \
286 static void tsk##__fun( void ); \
287 OS_WRK( tsk, prio, tsk##__fun, size ); \
288 static void tsk##__fun( void )
291#define static_WRK_DEF( tsk, prio, size ) \
292 static void tsk##__fun( void ); \
293 static_WRK( tsk, prio, tsk##__fun, size ); \
294 static void tsk##__fun( void )
306#define OS_TSK_DEF( tsk, prio, ... ) \
307 OS_WRK_DEF( tsk, prio, _VA_STK(__VA_ARGS__) )
310#define static_TSK_DEF( tsk, prio, ... ) \
311 static_WRK_DEF( tsk, prio, _VA_STK(__VA_ARGS__) )
326#define OS_WRK_START( tsk, prio, size ) \
327 static void tsk##__fun( void ); \
328 OS_WRK( tsk, prio, tsk##__fun, size ); \
329 __CONSTRUCTOR static void tsk##__run( void ) { sys_init(); tsk_start(tsk); } \
330 static void tsk##__fun( void )
335#define static_WRK_START( tsk, prio, size ) \
336 static void tsk##__fun( void ); \
337 static_WRK( tsk, prio, tsk##__fun, size ); \
338 __CONSTRUCTOR static void tsk##__run( void ) { sys_init(); tsk_start(tsk); } \
339 static void tsk##__fun( void )
355#define OS_TSK_START( tsk, prio, ... ) \
356 OS_WRK_START( tsk, prio, _VA_STK(__VA_ARGS__) )
361#define static_TSK_START( tsk, prio, ... ) \
362 static_WRK_START( tsk, prio, _VA_STK(__VA_ARGS__) )
379#define WRK_INIT( prio, proc, size ) \
380 _TSK_INIT( prio, proc, _TSK_STACK( size ), STK_OVER( size ) )
397#define WRK_CREATE( prio, proc, size ) \
398 { WRK_INIT ( prio, proc, size ) }
418#define TSK_INIT( prio, proc, ... ) \
419 WRK_INIT( prio, proc, _VA_STK(__VA_ARGS__) )
436#define TSK_CREATE( prio, proc, ... ) \
437 WRK_CREATE( prio, proc, _VA_STK(__VA_ARGS__) )
456tsk_t *
tsk_this(
void ) {
return System.cur; }
460tsk_t *
cur_task(
void ) {
return System.cur; }
463#define SELF ( System.cur )
480void wrk_init( tsk_t *tsk,
unsigned prio,
fun_t *proc, stk_t *stack,
size_t size );
496void tsk_init( tsk_t *tsk,
unsigned prio,
fun_t *proc, stk_t *stack,
size_t size );
514tsk_t *
wrk_create(
unsigned prio,
fun_t *proc,
size_t size,
bool detached,
bool autostart );
518tsk_t *
wrk_new(
unsigned prio,
fun_t *proc,
size_t size,
bool detached,
bool autostart )
520 return wrk_create(prio, proc, size, detached, autostart);
539tsk_t *
tsk_setup(
unsigned prio,
fun_a *proc,
void *arg,
size_t size );
558 return wrk_create(prio, proc, OS_STACK_SIZE,
false, !!proc);
565 return wrk_create(prio, proc, OS_STACK_SIZE,
false, !!proc);
585 return wrk_create(prio, proc, OS_STACK_SIZE,
true, !!proc);
869void tsk_give( tsk_t *tsk,
unsigned signo );
906 return core_stk_space(System.cur);
918#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
934template<
size_t size_>
937 static_assert(size_>
sizeof(ctx_t),
"incorrect stack size");
938#if __cplusplus >= 201703L && !defined(__ICCARM__)
939 stk_t stack_[
STK_SIZE(size_)] __STKALIGN;
962struct baseTask :
public __tsk
964#if __cplusplus >= 201402L
966 baseTask(
const unsigned _prio, F&& _proc, stk_t *
const _stack,
const size_t _size ): __tsk _TSK_INIT(_prio, fun_, _stack, _size), fun{_proc} {}
968 baseTask(
const unsigned _prio,
fun_t * _proc, stk_t *
const _stack,
const size_t _size ): __tsk _TSK_INIT(_prio, _proc, _stack, _size) {}
972#if __cplusplus >= 201402L
974 void startFrom( F&& _proc )
979 if (__tsk::hdr.
id == ID_STOPPED)
990 int join () {
return tsk_join (
this); }
991 int reset () {
return tsk_reset (
this); }
992 int kill () {
return tsk_kill (
this); }
994 unsigned prio () {
return __tsk::basic; }
995 unsigned getPrio () {
return __tsk::basic; }
999 void give (
unsigned _signo ) {
tsk_give (
this, _signo); }
1000 void signal (
unsigned _signo ) {
tsk_signal (
this, _signo); }
1001#if __cplusplus >= 201402L
1003 void action ( F&& _action )
1009 if (__tsk::sig.run == 0)
1023 operator bool ()
const {
return __tsk::hdr.id != ID_STOPPED; }
1025 template<
class T = baseTask>
static
1026 T * current () {
return static_cast<T *
>(
tsk_this()); }
1028#if __cplusplus >= 201402L
1030 void fun_ () { current()->fun(); }
1033 void act_ (
unsigned _signo )
1035 auto tsk = current();
1041 if (tsk->nxt && tsk->sig.run == 1)
1044 tsk->act.swap(tsk->nxt);
1064 int detach () {
return tsk_detach (current()); }
1070 int reset () {
return tsk_reset (current()); }
1072 int kill () {
return tsk_kill (current()); }
1074 int destroy () {
return tsk_destroy (current()); }
1079#if __cplusplus >= 201402L
1080 template<
class F>
static
1081 void flip ( F&& _proc ) { current()->fun = Fun_t(_proc);
1088 void setPrio (
unsigned _prio ) {
tsk_setPrio (_prio); }
1090 void prio (
unsigned _prio ) {
tsk_prio (_prio); }
1095 template<
typename T>
static
1097 template<
typename T>
static
1099 template<
typename T>
static
1103 template<
typename T>
static
1108 void give (
unsigned _signo ) {
tsk_give (current(), _signo); }
1110 void signal (
unsigned _signo ) {
tsk_signal (current(), _signo); }
1111#if __cplusplus >= 201402L
1112 template<
class F>
static
1113 void action ( F&& _action ) { current()->action(std::forward<F>(_action)); }
1121using thisTask = baseTask::Current;
1122using this_task = baseTask::Current;
1134template<
size_t size_>
1135struct TaskT :
public baseTask,
public baseStack<size_>
1139 TaskT(
const unsigned _prio, F&& _proc ):
1140 baseTask{_prio, _proc, baseStack<size_>::stack_, sizeof(baseStack<size_>::stack_)} {}
1145 TaskT<size_>{OS_MAIN_PRIO, _proc} {}
1147#if __cplusplus >= 201402L
1149 template<
typename F,
typename... A>
1150 TaskT(
const unsigned _prio, F&& _proc, A&&... _args ):
1151 TaskT<size_>{_prio, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...)} {}
1153#if __cplusplus >= 201703L && !defined(__ICCARM__)
1155 template<
typename F,
typename... A,
typename = std::enable_if_t<std::is_invocable_v<F, A...>>>
1156 TaskT( F&& _proc, A&&... _args ):
1157 TaskT<size_>{std::bind(std::forward<F>(_proc), std::forward<A>(_args)...)} {}
1161 ~TaskT() { assert(__tsk::hdr.
id == ID_STOPPED); }
1163#if __cplusplus >= 201402L
1166 baseTask{_src.basic, _src.fun, baseStack<size_>::stack_, sizeof(baseStack<size_>::stack_)}
1168 assert(_src.hdr.id == ID_STOPPED);
1171 __tsk::sig.action = _src.sig.action;
1176 baseTask{_src.basic, _src.proc, baseStack<size_>::stack_, sizeof(baseStack<size_>::stack_)}
1178 assert(_src.hdr.id == ID_STOPPED);
1179 __tsk::sig.action = _src.sig.action;
1189 template<
typename... A>
1190 TaskT( Starter, A&&... _args ):
TaskT<size_>{
static_cast<A&&
>(_args)...} { this->start(); }
1206 template<
class F>
static
1209 return { _prio, _proc };
1213 template<
class F>
static
1216 return Make(OS_MAIN_PRIO, _proc);
1219#if __cplusplus >= 201402L
1221 template<
typename F,
typename... A>
static
1224 return Make(_prio, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1227#if __cplusplus >= 201703L && !defined(__ICCARM__)
1229 template<
typename F,
typename... A,
typename = std::enable_if_t<std::is_invocable_v<F, A...>>>
static
1232 return Make(std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1250 template<
class F>
static
1253 return { Starter{}, _prio, _proc };
1257 template<
class F>
static
1260 return Start(OS_MAIN_PRIO, _proc);
1263#if __cplusplus >= 201402L
1265 template<
typename F,
typename... A>
static
1268 return Start(_prio, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1271#if __cplusplus >= 201703L && !defined(__ICCARM__)
1273 template<
typename F,
typename... A,
typename = std::enable_if_t<std::is_invocable_v<F, A...>>>
static
1276 return Start(std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1281#if __cplusplus >= 201402L
1282 using Deleter =
struct _Deleter {
void operator()(
void* )
const {} };
1283 using Ptr = std::unique_ptr<TaskT<size_>,
Deleter>;
1305 template<
class F>
static
1306 Ptr Create(
const unsigned _prio, F&& _proc )
1308 auto tsk =
new (std::nothrow)
TaskT<size_>(_prio, _proc);
1311 tsk->__tsk::obj.res = tsk;
1318 template<
class F>
static
1321 return Create(OS_MAIN_PRIO, _proc);
1324#if __cplusplus >= 201402L
1326 template<
typename F,
typename... A>
static
1327 Ptr Create(
const unsigned _prio, F&& _proc, A&&... _args )
1329 return Create(_prio, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1332#if __cplusplus >= 201703L && !defined(__ICCARM__)
1334 template<
typename F,
typename... A,
typename = std::enable_if_t<std::is_invocable_v<F, A...>>>
static
1337 return Create(std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1359 template<
class F>
static
1362 auto tsk =
new (std::nothrow)
TaskT<size_>(_prio, _proc);
1365 tsk->__tsk::obj.res = tsk;
1366 tsk->__tsk::owner = tsk;
1373 template<
class F>
static
1376 return Detached(OS_MAIN_PRIO, _proc);
1379#if __cplusplus >= 201402L
1381 template<
typename F,
typename... A>
static
1382 Ptr Detached(
const unsigned _prio, F&& _proc, A&&... _args )
1384 return Detached(_prio, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1387#if __cplusplus >= 201703L && !defined(__ICCARM__)
1389 template<
typename F,
typename... A,
typename = std::enable_if_t<std::is_invocable_v<F, A...>>>
static
1392 return Detached(std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1404using Task = TaskT<OS_STACK_SIZE>;
void fun_a(void *)
timer/task procedure with argument
Definition osbase.h:138
#define INFINITE
infinite waiting
Definition osbase.h:261
void fun_t(void)
timer/task procedure
Definition osbase.h:137
void act_t(unsigned)
signal action
Definition osbase.h:139
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:668
tsk_t * tsk_create(unsigned prio, fun_t *proc)
create and initialize a new task object with default stack size and start the task,...
Definition ostask.h:555
void tsk_give(tsk_t *tsk, unsigned signo)
send given signal to the task
void tsk_start(tsk_t *tsk)
start previously defined/created/stopped task object
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:813
void tsk_yield(void)
yield system control to the next task with the same priority in READY queue
void tsk_init(tsk_t *tsk, unsigned prio, fun_t *proc, stk_t *stack, size_t size)
initialize complete work area for task object and start the task, if proc != NULL
unsigned tsk_getPrio(void)
get current task priority
tsk_t * tsk_setup(unsigned prio, fun_a *proc, void *arg, size_t size)
create and initialize complete work area for task object and start the task, if proc !...
int tsk_kill(tsk_t *tsk)
alias of tsk_reset()
Definition ostask.h:691
int tsk_delete(tsk_t *tsk)
alias of tsk_destroy()
Definition ostask.h:710
tsk_t * wrk_create(unsigned prio, fun_t *proc, size_t size, bool detached, bool autostart)
create and initialize complete work area for task object
void tsk_sleepUntil(cnt_t time)
delay execution of current task until given timepoint
void tsk_delay(cnt_t delay)
alias of tsk_sleepFor()
Definition ostask.h:777
size_t tsk_stackSpace(void)
chack water mark of the stack of the current task
Definition ostask.h:902
tsk_t * cur_task(void)
alias of tsk_this()
Definition ostask.h:459
tsk_t * tsk_new(unsigned prio, fun_t *proc)
alias of tsk_create()
Definition ostask.h:562
tsk_t * tsk_detached(unsigned prio, fun_t *proc)
create and initialize a new detached task object with default stack size and start the task,...
Definition ostask.h:582
int tsk_resumeISR(tsk_t *tsk)
ISR alias of tsk_resume().
Definition ostask.h:848
void wrk_init(tsk_t *tsk, unsigned prio, fun_t *proc, stk_t *stack, size_t size)
initialize complete work area for task object
void tsk_prio(unsigned prio)
alias of tsk_setPrio()
Definition ostask.h:751
tsk_t * wrk_new(unsigned prio, fun_t *proc, size_t size, bool detached, bool autostart)
alias of wrk_create()
Definition ostask.h:517
void tsk_signal(tsk_t *tsk, unsigned signo)
alias of tsk_give()
Definition ostask.h:872
void tsk_pass(void)
alias of tsk_yield()
Definition ostask.h:724
void tsk_stop(void)
stop the current task and remove it from READY queue
void tsk_sleepFor(cnt_t delay)
delay execution of current task for given duration of time
void tsk_setPrio(unsigned prio)
set current task priority
void tsk_startFrom(tsk_t *tsk, fun_t *proc)
start previously defined/created/stopped task object
void tsk_action(tsk_t *tsk, act_t *action)
set given function as a signal handler
int tsk_detach(tsk_t *tsk)
detach given task
int tsk_reset(tsk_t *tsk)
reset the task object and remove it from READY/BLOCKED queue
int tsk_resume(tsk_t *tsk)
resume execution of given suspended task
tsk_t * tsk_this(void)
return current task object
Definition ostask.h:455
int tsk_destroy(tsk_t *tsk)
reset the task object and free allocated resources
void tsk_sleepNext(cnt_t delay)
delay execution of current task for given duration of time
int tsk_join(tsk_t *tsk)
delay execution of current task until termination of given task
#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:50
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:109
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:116
create and initialize complete work area for task object
Definition ostask.h:1135
static TaskT< size_ > Start(const unsigned _prio, F &&_proc)
Definition ostask.h:1250
static Ptr Create(const unsigned _prio, F &&_proc)
Definition ostask.h:1305
std::unique_ptr< TaskT< size_ >, Deleter > Ptr
result type of Create()
Definition ostask.h:1282
static Ptr Detached(const unsigned _prio, F &&_proc)
Definition ostask.h:1359
struct _Deleter { void operator()(void *) const {} } Deleter
deleter of Ptr (does not delete the task object)
Definition ostask.h:1281
static TaskT< size_ > Make(const unsigned _prio, F &&_proc)
Definition ostask.h:1206
TaskT(const unsigned _prio, F &&_proc)
constructor, see the constructor parameters of the class
Definition ostask.h:1138