StateOS
Loading...
Searching...
No Matches
ostask.h
1/******************************************************************************
2
3 @file StateOS: ostask.h
4 @author Rajmund Szymanski
5 @date 18.11.2025
6 @brief This file contains definitions for StateOS.
7
8 ******************************************************************************
9
10 Copyright (c) 2018-2022 Rajmund Szymanski. All rights reserved.
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to
14 deal in the Software without restriction, including without limitation the
15 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 sell copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included
20 in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 IN THE SOFTWARE.
29
30 ******************************************************************************/
31
32#ifndef __STATEOS_TSK_H
33#define __STATEOS_TSK_H
34
35#include "oskernel.h"
36#include "osclock.h"
37#include "osmutex.h"
38#include "ostimer.h"
39#include "oscriticalsection.h"
40
46
47/* -------------------------------------------------------------------------- */
48
50#define STK_SIZE( size ) \
51 ALIGNED_SIZE((size_t)( size ) + (OS_GUARD_SIZE), sizeof(stk_t))
52
53#define STK_OVER( size ) \
54 ALIGNED((size_t)( size ) + (OS_GUARD_SIZE), sizeof(stk_t))
55
56#define STK_CROP( base, size ) \
57 LIMITED((size_t)( base ) + (size_t)( size ), sizeof(stk_t))
58
62
63union __tmp
64{
65
66 struct {
67 unsigned event;
68 } evt; // temporary data used by event object
69
70 struct {
71 unsigned sigset;
72 unsigned signo;
73 } sig; // temporary data used by signal object
74
75 struct {
76 unsigned flags;
77 unsigned mode;
78 } flg; // temporary data used by flag object
79
80 struct {
81 void * data;
82 } lst; // temporary data used by list / memory pool object
83
84 struct {
85 union {
86 const
87 char * out;
88 char * in;
89 } data;
90 size_t size;
91 } raw; // temporary data used by raw buffer object
92
93 struct {
94 union {
95 const
96 char * out;
97 char * in;
98 } data;
99 size_t size;
100 } msg; // temporary data used by message buffer object
101
102 struct {
103 union {
104 const
105 void * out;
106 void * in;
107 } data;
108 } box; // temporary data used by mailbox queue object
109
110 struct {
111 unsigned event;
112 } evq; // temporary data used by event queue object
113
114 struct {
115 fun_t * fun;
116 } job; // temporary data used by job queue object
117
118};
119
120struct __tsk
121{
122 obj_t obj;
123 hdr_t hdr;
124
125 fun_t * proc;
126 void * arg;
127 cnt_t start;
128 cnt_t delay;
129 cnt_t slice;
130
131 stk_t * stack;
132 size_t size;
133 void * sp;
134
135 unsigned basic;
136 unsigned prio;
137
138 tsk_t * owner;
139 tsk_t ** guard;
140 int event;
141
142 struct {
143 mtx_t * list;
144 mtx_t * tree;
145 } mtx;
146
147 struct {
148 unsigned sigset;
149 act_t * action;
150 unsigned run;
151 struct {
152 void * sp;
153 int event;
154 cnt_t start;
155 cnt_t delay;
156 union __tmp tmp;
157 } backup;
158 } sig;
159
160 union __tmp tmp;
161
162#ifndef _PORT_DATA_INIT
163#define _PORT_DATA_INIT()
164#else
165 _PORT_DATA();
166#endif
167};
168
169typedef struct __tsk tsk_id [];
170
171/******************************************************************************
172 *
173 * Name : _TSK_INIT
174 *
175 * Description : create and initialize a task object
176 *
177 * Parameters
178 * prio : initial task priority (any unsigned int value)
179 * proc : task proc (initial task function) doesn't have to be noreturn-type
180 * it will be executed into an infinite system-implemented loop
181 * stack : base of task's private stack storage
182 * size : size of task private stack (in bytes)
183 *
184 * Return : task object
185 *
186 * Note : for internal use
187 *
188 ******************************************************************************/
189
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() }
193
194/******************************************************************************
195 *
196 * Name : _TSK_STACK
197 *
198 * Description : create task's private stack storage
199 *
200 * Parameters
201 * size : size of task's private stack storage (in bytes)
202 *
203 * Return : base of task's private stack storage
204 *
205 * Note : for internal use
206 *
207 ******************************************************************************/
208
209#ifndef __cplusplus
210#define _TSK_STACK( _size ) (stk_t [STK_SIZE( _size )]) { 0 }
211#endif
212
213/******************************************************************************
214 *
215 * Name : _VA_STK
216 *
217 * Description : calculate stack size from optional parameter
218 *
219 * Note : for internal use
220 *
221 ******************************************************************************/
222
223#define _VA_STK( _size ) ((_size + 0) ? (_size + 0) : (OS_STACK_SIZE))
224
232#define OS_TSK_STACK( stk, ... ) \
233 stk_t stk[ STK_SIZE( _VA_STK(__VA_ARGS__) ) ] __STKALIGN
234
235/** static alias of OS_TSK_STACK() */
236#define static_TSK_STACK( stk, ... ) \
237 static stk_t stk[ STK_SIZE( _VA_STK(__VA_ARGS__) ) ] __STKALIGN
238
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 ) ) }
252
253/** static alias of OS_WRK() */
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 ) ) }
257
268#define OS_TSK( tsk, prio, proc, ... ) \
269 OS_WRK( tsk, prio, proc, _VA_STK(__VA_ARGS__) )
270
271/** static alias of OS_TSK() */
272#define static_TSK( tsk, prio, proc, ... ) \
273 static_WRK( tsk, prio, proc, _VA_STK(__VA_ARGS__) )
274
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 )
289
290/** static alias of OS_WRK_DEF() */
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 )
295
306#define OS_TSK_DEF( tsk, prio, ... ) \
307 OS_WRK_DEF( tsk, prio, _VA_STK(__VA_ARGS__) )
308
309/** static alias of OS_TSK_DEF() */
310#define static_TSK_DEF( tsk, prio, ... ) \
311 static_WRK_DEF( tsk, prio, _VA_STK(__VA_ARGS__) )
312
324
325#ifdef __CONSTRUCTOR
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 )
331#endif
332
333#ifdef __CONSTRUCTOR
334/** static alias of OS_WRK_START() */
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 )
340#endif
341
353
354#ifdef __CONSTRUCTOR
355#define OS_TSK_START( tsk, prio, ... ) \
356 OS_WRK_START( tsk, prio, _VA_STK(__VA_ARGS__) )
357#endif
358
359#ifdef __CONSTRUCTOR
360/** static alias of OS_TSK_START() */
361#define static_TSK_START( tsk, prio, ... ) \
362 static_WRK_START( tsk, prio, _VA_STK(__VA_ARGS__) )
363#endif
364
377
378#ifndef __cplusplus
379#define WRK_INIT( prio, proc, size ) \
380 _TSK_INIT( prio, proc, _TSK_STACK( size ), STK_OVER( size ) )
381#endif
382
395
396#ifndef __cplusplus
397#define WRK_CREATE( prio, proc, size ) \
398 { WRK_INIT ( prio, proc, size ) }
399/** alias of WRK_CREATE() */
400#define WRK_NEW \
401 WRK_CREATE
402#endif
403
416
417#ifndef __cplusplus
418#define TSK_INIT( prio, proc, ... ) \
419 WRK_INIT( prio, proc, _VA_STK(__VA_ARGS__) )
420#endif
421
434
435#ifndef __cplusplus
436#define TSK_CREATE( prio, proc, ... ) \
437 WRK_CREATE( prio, proc, _VA_STK(__VA_ARGS__) )
438/** alias of TSK_CREATE() */
439#define TSK_NEW \
440 TSK_CREATE
441#endif
442
443#ifdef __cplusplus
444extern "C" {
445#endif
446
454
455__STATIC_INLINE
456tsk_t *tsk_this( void ) { return System.cur; }
457
459__STATIC_INLINE
460tsk_t *cur_task( void ) { return System.cur; }
461
463#define SELF ( System.cur )
464
480void wrk_init( tsk_t *tsk, unsigned prio, fun_t *proc, stk_t *stack, size_t size );
481
496void tsk_init( tsk_t *tsk, unsigned prio, fun_t *proc, stk_t *stack, size_t size );
497
514tsk_t *wrk_create( unsigned prio, fun_t *proc, size_t size, bool detached, bool autostart );
515
517__STATIC_INLINE
518tsk_t *wrk_new( unsigned prio, fun_t *proc, size_t size, bool detached, bool autostart )
519{
520 return wrk_create(prio, proc, size, detached, autostart);
521}
522
539tsk_t *tsk_setup( unsigned prio, fun_a *proc, void *arg, size_t size );
540
554
555__STATIC_INLINE
556tsk_t *tsk_create( unsigned prio, fun_t *proc )
557{
558 return wrk_create(prio, proc, OS_STACK_SIZE, false, !!proc);
559}
560
562__STATIC_INLINE
563tsk_t *tsk_new( unsigned prio, fun_t *proc )
564{
565 return wrk_create(prio, proc, OS_STACK_SIZE, false, !!proc);
566}
567
581
582__STATIC_INLINE
583tsk_t *tsk_detached( unsigned prio, fun_t *proc )
584{
585 return wrk_create(prio, proc, OS_STACK_SIZE, true, !!proc);
586}
587
596void tsk_start( tsk_t *tsk );
597
608void tsk_startFrom( tsk_t *tsk, fun_t *proc );
609
621void tsk_startWith( tsk_t *tsk, fun_a *proc, void *arg );
622
634int tsk_detach( tsk_t *tsk );
635
649int tsk_join( tsk_t *tsk );
650
663
664__NO_RETURN
665void tsk_stop( void );
666
668__STATIC_INLINE
669void tsk_exit( void ) { tsk_stop(); }
670
688int tsk_reset( tsk_t *tsk );
689
691__STATIC_INLINE
692int tsk_kill( tsk_t *tsk ) { return tsk_reset(tsk); }
693
707int tsk_destroy( tsk_t *tsk );
708
710__STATIC_INLINE
711int tsk_delete( tsk_t *tsk ) { return tsk_destroy(tsk); }
712
721void tsk_yield( void );
722
724__STATIC_INLINE
725void tsk_pass ( void ) { tsk_yield(); }
726
736
737__NO_RETURN
738void tsk_flip( fun_t *proc );
739
748void tsk_setPrio( unsigned prio );
749
751__STATIC_INLINE
752void tsk_prio( unsigned prio ) { tsk_setPrio(prio); }
753
762unsigned tsk_getPrio( void );
763
774void tsk_sleepFor( cnt_t delay );
775
777__STATIC_INLINE
778void tsk_delay( cnt_t delay ) { tsk_sleepFor(delay); }
779
792void tsk_sleepNext( cnt_t delay );
793
803void tsk_sleepUntil( cnt_t time );
804
812
813__STATIC_INLINE
814void tsk_sleep( void ) { tsk_sleepFor(INFINITE); }
815
829int tsk_suspend( tsk_t *tsk );
830
845int tsk_resume( tsk_t *tsk );
846
848__STATIC_INLINE
849int tsk_resumeISR( tsk_t *tsk ) { return tsk_resume(tsk); }
850
869void tsk_give( tsk_t *tsk, unsigned signo );
870
872__STATIC_INLINE
873void tsk_signal( tsk_t *tsk, unsigned signo ) { tsk_give(tsk, signo); }
874
893void tsk_action( tsk_t *tsk, act_t *action );
894
901
902__STATIC_INLINE
903size_t tsk_stackSpace( void )
904{
905#ifdef DEBUG
906 return core_stk_space(System.cur);
907#else
908 return 0;
909#endif
910}
911
912#ifdef __cplusplus
913}
914#endif
915
916/* -------------------------------------------------------------------------- */
917
918#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
919namespace stateos {
920
921/******************************************************************************
922 *
923 * Class : baseStack
924 *
925 * Description : create base class for stack storage object
926 *
927 * Constructor parameters
928 * size : size of stack (in bytes)
929 *
930 * Note : for internal use
931 *
932 ******************************************************************************/
933
934template<size_t size_>
935struct baseStack
936{
937 static_assert(size_>sizeof(ctx_t), "incorrect stack size");
938#if __cplusplus >= 201703L && !defined(__ICCARM__)
939 stk_t stack_[STK_SIZE(size_)] __STKALIGN;
940#else
941 stk_t stack_[STK_SIZE(size_)];
942#endif
943};
944
945/******************************************************************************
946 *
947 * Class : baseTask
948 *
949 * Description : create and initialize base class for task objects
950 *
951 * Constructor parameters
952 * prio : initial task priority (any unsigned int value)
953 * proc : task proc (initial task function) doesn't have to be noreturn-type
954 * it will be executed into an infinite system-implemented loop
955 * stack : base of task's private stack storage
956 * size : size of task private stack (in bytes)
957 *
958 * Note : for internal use
959 *
960 ******************************************************************************/
961
962struct baseTask : public __tsk
963{
964#if __cplusplus >= 201402L
965 template<class F>
966 baseTask( const unsigned _prio, F&& _proc, stk_t * const _stack, const size_t _size ): __tsk _TSK_INIT(_prio, fun_, _stack, _size), fun{_proc} {}
967#else
968 baseTask( const unsigned _prio, fun_t * _proc, stk_t * const _stack, const size_t _size ): __tsk _TSK_INIT(_prio, _proc, _stack, _size) {}
969#endif
970
971 void start () { tsk_start (this); }
972#if __cplusplus >= 201402L
973 template<class F>
974 void startFrom( F&& _proc )
975 {
976 Fun_t tmp(_proc);
977 {
978 CriticalSection cs;
979 if (__tsk::hdr.id == ID_STOPPED) // active tasks cannot be started
980 {
981 fun.swap(tmp);
982 tsk_startFrom(this, fun_);
983 }
984 } // the replaced function is destroyed outside the critical section
985 }
986#else
987 void startFrom( fun_t * _proc ) { tsk_startFrom(this, _proc); }
988#endif
989 int detach () { return tsk_detach (this); }
990 int join () { return tsk_join (this); }
991 int reset () { return tsk_reset (this); }
992 int kill () { return tsk_kill (this); }
993 int destroy () { return tsk_destroy (this); }
994 unsigned prio () { return __tsk::basic; }
995 unsigned getPrio () { return __tsk::basic; }
996 int suspend () { return tsk_suspend (this); }
997 int resume () { return tsk_resume (this); }
998 int resumeISR() { return tsk_resumeISR(this); }
999 void give ( unsigned _signo ) { tsk_give (this, _signo); }
1000 void signal ( unsigned _signo ) { tsk_signal (this, _signo); }
1001#if __cplusplus >= 201402L
1002 template<class F>
1003 void action ( F&& _action )
1004 {
1005 Act_t tmp(_action);
1006 Act_t old;
1007 {
1008 CriticalSection cs;
1009 if (__tsk::sig.run == 0) // the action is not being executed:
1010 {
1011 act.swap(tmp); // replace it now
1012 nxt.swap(old); // and drop the pending one
1013 }
1014 else // the action is being executed (it can replace itself):
1015 nxt.swap(tmp); // replace it after the execution
1016 tsk_action(this, act_);
1017 } // the replaced actions are destroyed outside the critical section
1018 }
1019#else
1020 void action ( act_t * _action ) { tsk_action (this, _action); }
1021#endif
1022 explicit
1023 operator bool () const { return __tsk::hdr.id != ID_STOPPED; }
1024
1025 template<class T = baseTask> static
1026 T * current () { return static_cast<T *>(tsk_this()); }
1027
1028#if __cplusplus >= 201402L
1029 static
1030 void fun_ () { current()->fun(); }
1031 Fun_t fun;
1032 static
1033 void act_ ( unsigned _signo )
1034 {
1035 auto tsk = current();
1036 auto next = [tsk] // use the pending action
1037 {
1038 Act_t old;
1039 {
1040 CriticalSection cs;
1041 if (tsk->nxt && tsk->sig.run == 1) // no execution of the action is in progress (except the caller, outside of the action)
1042 {
1043 old.swap(tsk->act);
1044 tsk->act.swap(tsk->nxt);
1045 }
1046 } // the replaced action is destroyed outside the critical section
1047 };
1048 next(); // replaced during the previous execution (ended by tsk_flip, tsk_stop or tsk_reset)
1049 if (tsk->act)
1050 tsk->act(_signo);
1051 next(); // replaced during this execution
1052 }
1053 Act_t act;
1054 Act_t nxt; // the action replaced during its execution
1055#endif
1056
1060
1061 struct Current
1062 {
1063 static
1064 int detach () { return tsk_detach (current()); }
1065 static
1066 void stop () { tsk_stop (); }
1067 static
1068 void exit () { tsk_exit (); }
1069 static
1070 int reset () { return tsk_reset (current()); }
1071 static
1072 int kill () { return tsk_kill (current()); }
1073 static
1074 int destroy () { return tsk_destroy (current()); }
1075 static
1076 void yield () { tsk_yield (); }
1077 static
1078 void pass () { tsk_pass (); }
1079#if __cplusplus >= 201402L
1080 template<class F> static
1081 void flip ( F&& _proc ) { current()->fun = Fun_t(_proc);
1082 tsk_flip (fun_); }
1083#else
1084 static
1085 void flip ( fun_t * _proc ) { tsk_flip (_proc); }
1086#endif
1087 static
1088 void setPrio ( unsigned _prio ) { tsk_setPrio (_prio); }
1089 static
1090 void prio ( unsigned _prio ) { tsk_prio (_prio); }
1091 static
1092 unsigned getPrio () { return tsk_getPrio (); }
1093 static
1094 unsigned prio () { return tsk_getPrio (); }
1095 template<typename T> static
1096 void sleepFor ( const T& _delay ) { tsk_sleepFor (Clock::count(_delay)); }
1097 template<typename T> static
1098 void sleepNext ( const T& _delay ) { tsk_sleepNext (Clock::count(_delay)); }
1099 template<typename T> static
1100 void sleepUntil( const T& _time ) { tsk_sleepUntil(Clock::until(_time)); }
1101 static
1102 void sleep () { tsk_sleep (); }
1103 template<typename T> static
1104 void delay ( const T& _delay ) { tsk_delay (Clock::count(_delay)); }
1105 static
1106 void suspend () { tsk_suspend (current()); }
1107 static
1108 void give ( unsigned _signo ) { tsk_give (current(), _signo); }
1109 static
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)); }
1114#else
1115 static
1116 void action ( act_t * _action ) { tsk_action (current(), _action); }
1117#endif
1118 };
1119};
1120
1121using thisTask = baseTask::Current;
1122using this_task = baseTask::Current;
1123
1133
1134template<size_t size_>
1135struct TaskT : public baseTask, public baseStack<size_>
1136{
1138 template<class F>
1139 TaskT( const unsigned _prio, F&& _proc ):
1140 baseTask{_prio, _proc, baseStack<size_>::stack_, sizeof(baseStack<size_>::stack_)} {}
1141
1143 template<class F>
1144 TaskT( F&& _proc ):
1145 TaskT<size_>{OS_MAIN_PRIO, _proc} {}
1146
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)...)} {}
1152
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)...)} {}
1158#endif
1159#endif
1160
1161 ~TaskT() { assert(__tsk::hdr.id == ID_STOPPED); }
1162
1163#if __cplusplus >= 201402L
1164 /// move constructor
1165 TaskT( TaskT<size_>&& _src ):
1166 baseTask{_src.basic, _src.fun, baseStack<size_>::stack_, sizeof(baseStack<size_>::stack_)}
1167 {
1168 assert(_src.hdr.id == ID_STOPPED);
1169 act = _src.act; // the signal action set before the task was stopped
1170 nxt = _src.nxt;
1171 __tsk::sig.action = _src.sig.action;
1172 }
1173#else
1175 TaskT( TaskT<size_>&& _src ):
1176 baseTask{_src.basic, _src.proc, baseStack<size_>::stack_, sizeof(baseStack<size_>::stack_)}
1177 {
1178 assert(_src.hdr.id == ID_STOPPED);
1179 __tsk::sig.action = _src.sig.action; // the signal action set before the task was stopped
1180 }
1181#endif
1182
1183 TaskT( const TaskT<size_>& ) = delete;
1184 TaskT<size_>& operator=( TaskT<size_>&& ) = delete;
1185 TaskT<size_>& operator=( const TaskT<size_>& ) = delete;
1186
1187private:
1188 struct Starter {}; // Start functions return the task started in place: a started task must not be moved
1189 template<typename... A>
1190 TaskT( Starter, A&&... _args ): TaskT<size_>{static_cast<A&&>(_args)...} { this->start(); }
1191public:
1192
1205
1206 template<class F> static
1207 TaskT<size_> Make( const unsigned _prio, F&& _proc )
1208 {
1209 return { _prio, _proc };
1210 }
1211
1213 template<class F> static
1214 TaskT<size_> Make( F&& _proc )
1215 {
1216 return Make(OS_MAIN_PRIO, _proc);
1217 }
1218
1219#if __cplusplus >= 201402L
1221 template<typename F, typename... A> static
1222 TaskT<size_> Make( const unsigned _prio, F&& _proc, A&&... _args )
1223 {
1224 return Make(_prio, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1225 }
1226
1227#if __cplusplus >= 201703L && !defined(__ICCARM__)
1229 template<typename F, typename... A, typename = std::enable_if_t<std::is_invocable_v<F, A...>>> static
1230 TaskT<size_> Make( F&& _proc, A&&... _args )
1231 {
1232 return Make(std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1233 }
1234#endif
1235#endif
1236
1249
1250 template<class F> static
1251 TaskT<size_> Start( const unsigned _prio, F&& _proc )
1252 {
1253 return { Starter{}, _prio, _proc };
1254 }
1255
1257 template<class F> static
1258 TaskT<size_> Start( F&& _proc )
1259 {
1260 return Start(OS_MAIN_PRIO, _proc);
1261 }
1262
1263#if __cplusplus >= 201402L
1265 template<typename F, typename... A> static
1266 TaskT<size_> Start( const unsigned _prio, F&& _proc, A&&... _args )
1267 {
1268 return Start(_prio, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1269 }
1270
1271#if __cplusplus >= 201703L && !defined(__ICCARM__)
1273 template<typename F, typename... A, typename = std::enable_if_t<std::is_invocable_v<F, A...>>> static
1274 TaskT<size_> Start( F&& _proc, A&&... _args )
1275 {
1276 return Start(std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1277 }
1278#endif
1279#endif
1280
1281#if __cplusplus >= 201402L
1282 using Deleter = struct _Deleter { void operator()( void* ) const {} };
1283 using Ptr = std::unique_ptr<TaskT<size_>, Deleter>;
1284#else
1285 using Ptr = TaskT<size_> *;
1286#endif
1287
1304
1305 template<class F> static
1306 Ptr Create( const unsigned _prio, F&& _proc )
1307 {
1308 auto tsk = new (std::nothrow) TaskT<size_>(_prio, _proc);
1309 if (tsk != nullptr)
1310 {
1311 tsk->__tsk::obj.res = tsk;
1312 tsk->start();
1313 }
1314 return Ptr(tsk);
1315 }
1316
1318 template<class F> static
1319 Ptr Create( F&& _proc )
1320 {
1321 return Create(OS_MAIN_PRIO, _proc);
1322 }
1323
1324#if __cplusplus >= 201402L
1326 template<typename F, typename... A> static
1327 Ptr Create( const unsigned _prio, F&& _proc, A&&... _args )
1328 {
1329 return Create(_prio, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1330 }
1331
1332#if __cplusplus >= 201703L && !defined(__ICCARM__)
1334 template<typename F, typename... A, typename = std::enable_if_t<std::is_invocable_v<F, A...>>> static
1335 Ptr Create( F&& _proc, A&&... _args )
1336 {
1337 return Create(std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1338 }
1339#endif
1340#endif
1341
1358
1359 template<class F> static
1360 Ptr Detached( const unsigned _prio, F&& _proc )
1361 {
1362 auto tsk = new (std::nothrow) TaskT<size_>(_prio, _proc);
1363 if (tsk != nullptr)
1364 {
1365 tsk->__tsk::obj.res = tsk;
1366 tsk->__tsk::owner = tsk;
1367 tsk->start();
1368 }
1369 return Ptr(tsk);
1370 }
1371
1373 template<class F> static
1374 Ptr Detached( F&& _proc )
1375 {
1376 return Detached(OS_MAIN_PRIO, _proc);
1377 }
1378
1379#if __cplusplus >= 201402L
1381 template<typename F, typename... A> static
1382 Ptr Detached( const unsigned _prio, F&& _proc, A&&... _args )
1383 {
1384 return Detached(_prio, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1385 }
1386
1387#if __cplusplus >= 201703L && !defined(__ICCARM__)
1389 template<typename F, typename... A, typename = std::enable_if_t<std::is_invocable_v<F, A...>>> static
1390 Ptr Detached( F&& _proc, A&&... _args )
1391 {
1392 return Detached(std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
1393 }
1394#endif
1395#endif
1396};
1397
1403
1404using Task = TaskT<OS_STACK_SIZE>;
1405
1406} // namespace
1407#endif//__cplusplus
1408
1409/* -------------------------------------------------------------------------- */
1410
1412
1413#endif//__STATEOS_TSK_H
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