StateOS
Loading...
Searching...
No Matches
ostimer.h
1/******************************************************************************
2
3 @file StateOS: ostimer.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_TMR_H
33#define __STATEOS_TMR_H
34
35#include "oskernel.h"
36#include "osclock.h"
37#include "oscriticalsection.h"
38
44
48
49struct __tmr
50{
51 obj_t obj;
52 hdr_t hdr;
53
54 fun_t * proc;
55 void * arg;
56 cnt_t start;
57 cnt_t delay;
58 cnt_t period;
59 cnt_t overrun;
60};
61
62typedef struct __tmr tmr_id [];
63
64/******************************************************************************
65 *
66 * Name : _TMR_INIT
67 *
68 * Description : create and initialize a timer object
69 *
70 * Parameters
71 * proc : callback procedure
72 * NULL: no callback
73 *
74 * Return : timer object
75 *
76 * Note : for internal use
77 *
78 ******************************************************************************/
79
80#define _TMR_INIT( _proc ) \
81 { _OBJ_INIT(), _HDR_INIT(), _proc, NULL, 0, 0, 0, 0 }
82
91#define OS_TMR( tmr, proc ) \
92 tmr_t tmr[] = { _TMR_INIT( proc ) }
93
94/** static alias of OS_TMR() */
95#define static_TMR( tmr, proc ) \
96 static tmr_t tmr[] = { _TMR_INIT( proc ) }
97
106#define OS_TMR_DEF( tmr ) \
107 static void tmr##__fun( void ); \
108 OS_TMR( tmr, tmr##__fun ); \
109 static void tmr##__fun( void )
110
111/** static alias of OS_TMR_DEF() */
112#define static_TMR_DEF( tmr ) \
113 static void tmr##__fun( void ); \
114 static_TMR( tmr, tmr##__fun ); \
115 static void tmr##__fun( void )
116
132
133#ifdef __CONSTRUCTOR
134#define OS_TMR_START( tmr, delay, period ) \
135 static void tmr##__fun( void ); \
136 OS_TMR( tmr, tmr##__fun ); \
137 __CONSTRUCTOR static void tmr##__run( void ) { sys_init(); tmr_start(tmr, delay, period); } \
138 static void tmr##__fun( void )
139#endif
140
141#ifdef __CONSTRUCTOR
142/** static alias of OS_TMR_START() */
143#define static_TMR_START( tmr, delay, period ) \
144 static void tmr##__fun( void ); \
145 static_TMR( tmr, tmr##__fun ); \
146 __CONSTRUCTOR static void tmr##__run( void ) { sys_init(); tmr_start(tmr, delay, period); } \
147 static void tmr##__fun( void )
148#endif
149
160
161#ifdef __CONSTRUCTOR
162#define OS_TMR_UNTIL( tmr, time ) \
163 static void tmr##__fun( void ); \
164 OS_TMR( tmr, tmr##__fun ); \
165 __CONSTRUCTOR static void tmr##__run( void ) { sys_init(); tmr_startUntil(tmr, time); } \
166 static void tmr##__fun( void )
167#endif
168
169#ifdef __CONSTRUCTOR
170/** static alias of OS_TMR_UNTIL() */
171#define static_TMR_UNTIL( tmr, time ) \
172 static void tmr##__fun( void ); \
173 static_TMR( tmr, tmr##__fun ); \
174 __CONSTRUCTOR static void tmr##__run( void ) { sys_init(); tmr_startUntil(tmr, time); } \
175 static void tmr##__fun( void )
176#endif
177
188
189#ifndef __cplusplus
190#define TMR_INIT( proc ) \
191 _TMR_INIT( proc )
192#endif
193
204
205#ifndef __cplusplus
206#define TMR_CREATE( proc ) \
207 { TMR_INIT ( proc ) }
208/** alias of TMR_CREATE() */
209#define TMR_NEW \
210 TMR_CREATE
211#endif
212
213#ifdef __cplusplus
214extern "C" {
215#endif
216
224
225__STATIC_INLINE
226tmr_t *tmr_thisISR( void ) { return (tmr_t *) WAIT.hdr.next; }
227
238void tmr_init( tmr_t *tmr, fun_t *proc );
239
252tmr_t *tmr_create( fun_t *proc );
253
255__STATIC_INLINE
256tmr_t *tmr_new( fun_t *proc ) { return tmr_create(proc); }
257
266void tmr_reset( tmr_t *tmr );
267
269__STATIC_INLINE
270void tmr_kill( tmr_t *tmr ) { tmr_reset(tmr); }
271
280void tmr_destroy( tmr_t *tmr );
281
283__STATIC_INLINE
284void tmr_delete( tmr_t *tmr ) { tmr_destroy(tmr); }
285
304void tmr_start( tmr_t *tmr, cnt_t delay, cnt_t period );
305
318
319__STATIC_INLINE
320void tmr_startFor( tmr_t *tmr, cnt_t delay ) { tmr_start(tmr, delay, 0); }
321
336
337__STATIC_INLINE
338void tmr_startPeriodic( tmr_t *tmr, cnt_t period ) { tmr_start(tmr, period, period); }
339
360void tmr_startFrom( tmr_t *tmr, cnt_t delay, cnt_t period, fun_t *proc );
361
375void tmr_startNext( tmr_t *tmr, cnt_t delay );
376
388void tmr_startUntil( tmr_t *tmr, cnt_t time );
389
397
398__STATIC_INLINE
399void tmr_stop( tmr_t *tmr ) { tmr_reset(tmr); }
400
414int tmr_take( tmr_t *tmr );
415
417__STATIC_INLINE
418int tmr_expired( tmr_t *tmr ) { return tmr_take(tmr); }
419
421__STATIC_INLINE
422int tmr_tryWait( tmr_t *tmr ) { return tmr_take(tmr); }
423
425__STATIC_INLINE
426int tmr_takeISR( tmr_t *tmr ) { return tmr_take(tmr); }
427
445int tmr_waitFor( tmr_t *tmr, cnt_t delay );
446
466int tmr_waitNext( tmr_t *tmr, cnt_t delay );
467
483int tmr_waitUntil( tmr_t *tmr, cnt_t time );
484
497
498__STATIC_INLINE
499int tmr_wait( tmr_t *tmr ) { return tmr_waitFor(tmr, INFINITE); }
500
509
510__STATIC_INLINE
511void tmr_flipISR( fun_t *proc ) { tmr_thisISR()->proc = proc; }
512
522
523__STATIC_INLINE
524void tmr_delayISR( cnt_t delay ) { tmr_thisISR()->delay = delay; }
525
535
536__STATIC_INLINE
537cnt_t tmr_overrunISR( void ) { return tmr_thisISR()->overrun; }
538
539#ifdef __cplusplus
540}
541#endif
542
543/* -------------------------------------------------------------------------- */
544
545#if defined(__cplusplus) && (__cplusplus >= 201103L) && !defined(__STATEOS_STDCXX)
546namespace stateos {
547
548/******************************************************************************
549 *
550 * Class : baseTimer
551 *
552 * Description : create and initialize base class for timer objects
553 *
554 * Constructor parameters
555 * proc : callback procedure
556 * nullptr: no callback
557 *
558 * Note : for internal use
559 *
560 ******************************************************************************/
561
562struct baseTimer : public __tmr
563{
564 baseTimer(): __tmr _TMR_INIT(nullptr) {}
565#if __cplusplus >= 201402L
566 template<class F>
567 baseTimer( F&& _proc ): __tmr _TMR_INIT(fun_), fun{_proc} {}
568#else
569 baseTimer( fun_t *_proc ): __tmr _TMR_INIT(_proc) {}
570#endif
571
572 void reset () { tmr_reset (this); }
573 void kill () { tmr_kill (this); }
574 void destroy () { tmr_destroy (this); }
575 template<typename T>
576 void start ( const T& _delay, const T& _period ) { tmr_start (this, Clock::count(_delay), Clock::count(_period)); }
577 template<typename T>
578 void startFor ( const T& _delay ) { tmr_startFor (this, Clock::count(_delay)); }
579 template<typename T>
580 void startPeriodic( const T& _period ) { tmr_startPeriodic(this, Clock::count(_period)); }
581 template<typename T>
582 void startNext ( const T& _delay ) { tmr_startNext (this, Clock::count(_delay)); }
583 template<typename T>
584 void startUntil ( const T& _time ) { tmr_startUntil (this, Clock::until(_time)); }
585#if __cplusplus >= 201402L
586 template<typename T>
587 void startFrom ( const T& _delay, const T& _period, std::nullptr_t ) { tmr_startFrom (this, Clock::count(_delay), Clock::count(_period), nullptr); }
588 template<typename T, class F>
589 void startFrom ( const T& _delay, const T& _period, F&& _proc )
590 {
591 Fun_t tmp(_proc);
592 {
593 CriticalSection cs; // the callback (handler mode) cannot run while the function is replaced
594 fun.swap(tmp);
595 tmr_startFrom(this, Clock::count(_delay), Clock::count(_period), fun_);
596 } // the replaced function is destroyed outside the critical section
597 }
598#else
599 template<typename T>
600 void startFrom ( const T& _delay, const T& _period, fun_t * _proc ) { tmr_startFrom (this, Clock::count(_delay), Clock::count(_period), _proc); }
601#endif
602 void stop () { tmr_stop (this); }
603 int take () { return tmr_take (this); }
604 int expired () { return tmr_expired (this); }
605 int tryWait () { return tmr_tryWait (this); }
606 int takeISR () { return tmr_takeISR (this); }
607 template<typename T>
608 int waitFor ( const T& _delay ) { return tmr_waitFor (this, Clock::count(_delay)); }
609 template<typename T>
610 int waitNext ( const T& _delay ) { return tmr_waitNext (this, Clock::count(_delay)); }
611 template<typename T>
612 int waitUntil ( const T& _time ) { return tmr_waitUntil (this, Clock::until(_time)); }
613 int wait () { return tmr_wait (this); }
614 explicit
615 operator bool () const { return __tmr::hdr.id != ID_STOPPED; }
616
617 template<class T = baseTimer> static
618 T * current () { return static_cast<T *>(tmr_thisISR()); }
619
620#if __cplusplus >= 201402L
621 static
622 void fun_ ()
623 {
624 auto tmr = current();
625 Fun_t run;
626 run.swap(tmr->fun); // the running function must live until the callback returns,
627 run(); // the callback can replace it (flipISR)
628 if (!tmr->fun)
629 tmr->fun.swap(run);
630 }
631 Fun_t fun;
632#endif
633
640
641 struct Current
642 {
643#if __cplusplus >= 201402L
644 static
645 void flipISR ( std::nullptr_t ) { tmr_flipISR (nullptr); }
646 template<class F> static
647 void flipISR ( F&& _proc ) { current()->fun = Fun_t(_proc);
648 tmr_flipISR (fun_); }
649 template<typename F, typename... A> static
650 void flipISR ( F&& _proc, A&&... _args ) { flipISR(std::bind(std::forward<F>(_proc), std::forward<A>(_args)...)); }
651#else
652 static
653 void flipISR ( fun_t * _proc ) { tmr_flipISR (_proc); }
654#endif
655 template<typename T> static
656 void delayISR( const T& _delay ) { tmr_delayISR(Clock::count(_delay)); }
657 static
658 cnt_t overrunISR() { return tmr_overrunISR(); }
659 };
660};
661
662using thisTimer = baseTimer::Current;
663using this_timer = baseTimer::Current;
664
673struct Timer : public baseTimer
674{
676 Timer(): baseTimer{} {}
678 template<class F>
679 Timer( F&& _proc ): baseTimer{_proc} {}
680#if __cplusplus >= 201402L
682 Timer( std::nullptr_t ): baseTimer{} {}
684 template<typename F, typename... A>
685 Timer( F&& _proc, A&&... _args ): baseTimer{std::bind(std::forward<F>(_proc), std::forward<A>(_args)...)} {}
686#endif
687
688 ~Timer() { assert(__tmr::hdr.id == ID_STOPPED); }
689
690 /// move constructor
691 Timer( Timer&& _src ): baseTimer(static_cast<baseTimer&&>(_src))
692 {
693 assert(_src.hdr.id == ID_STOPPED && _src.obj.queue == nullptr);
694 }
695
696 Timer( const Timer& ) = delete;
697 Timer& operator=( Timer&& ) = delete;
698 Timer& operator=( const Timer& ) = delete;
699
700private:
701 struct Starter {}; // Start* functions return the timer started in place: a started timer must not be moved
702 template<class S, typename... A>
703 Timer( Starter, S&& _start, A&&... _args ): Timer{static_cast<A&&>(_args)...} { _start(*this); }
704public:
705
716
717 static
718 Timer Make()
719 {
720 return {};
721 }
722
724 template<class F> static
725 Timer Make( F&& _proc )
726 {
727 return { _proc };
728 }
729
730#if __cplusplus >= 201402L
732 static
733 Timer Make( std::nullptr_t )
734 {
735 return Make();
736 }
737
739 template<typename F, typename... A> static
740 Timer Make( F&& _proc, A&&... _args )
741 {
742 return Make(std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
743 }
744#endif
745
767
768 template<typename T> static
769 Timer Start( const T& _delay, const T& _period )
770 {
771 return { Starter{}, [&]( Timer& _tmr ){ _tmr.start(_delay, _period); } };
772 }
773
775 template<typename T, class F> static
776 Timer Start( const T& _delay, const T& _period, F&& _proc )
777 {
778 return { Starter{}, [&]( Timer& _tmr ){ _tmr.start(_delay, _period); }, _proc };
779 }
780
781#if __cplusplus >= 201402L
783 template<typename T> static
784 Timer Start( const T& _delay, const T& _period, std::nullptr_t )
785 {
786 return Start(_delay, _period);
787 }
788
790 template<typename T, typename F, typename... A> static
791 Timer Start( const T& _delay, const T& _period, F&& _proc, A&&... _args )
792 {
793 return Start(_delay, _period, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
794 }
795#endif
796
813
814 template<typename T> static
815 Timer StartFor( const T& _delay )
816 {
817 return { Starter{}, [&]( Timer& _tmr ){ _tmr.startFor(_delay); } };
818 }
819
821 template<typename T, class F> static
822 Timer StartFor( const T& _delay, F&& _proc )
823 {
824 return { Starter{}, [&]( Timer& _tmr ){ _tmr.startFor(_delay); }, _proc };
825 }
826
827#if __cplusplus >= 201402L
829 template<typename T> static
830 Timer StartFor( const T& _delay, std::nullptr_t )
831 {
832 return StartFor(_delay);
833 }
834
836 template<typename T, typename F, typename... A> static
837 Timer StartFor( const T& _delay, F&& _proc, A&&... _args )
838 {
839 return StartFor(_delay, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
840 }
841#endif
842
861
862 template<typename T> static
863 Timer StartPeriodic( const T& _period )
864 {
865 return { Starter{}, [&]( Timer& _tmr ){ _tmr.startPeriodic(_period); } };
866 }
867
869 template<typename T, class F> static
870 Timer StartPeriodic( const T& _period, F&& _proc )
871 {
872 return { Starter{}, [&]( Timer& _tmr ){ _tmr.startPeriodic(_period); }, _proc };
873 }
874
875#if __cplusplus >= 201402L
877 template<typename T> static
878 Timer StartPeriodic( const T& _period, std::nullptr_t )
879 {
880 return StartPeriodic(_period);
881 }
882
884 template<typename T, typename F, typename... A> static
885 Timer StartPeriodic( const T& _period, F&& _proc, A&&... _args )
886 {
887 return StartPeriodic(_period, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
888 }
889#endif
890
905
906 template<typename T> static
907 Timer StartUntil( const T& _time )
908 {
909 return { Starter{}, [&]( Timer& _tmr ){ _tmr.startUntil(_time); } };
910 }
911
913 template<typename T, class F> static
914 Timer StartUntil( const T& _time, F&& _proc )
915 {
916 return { Starter{}, [&]( Timer& _tmr ){ _tmr.startUntil(_time); }, _proc };
917 }
918
919#if __cplusplus >= 201402L
921 template<typename T> static
922 Timer StartUntil( const T& _time, std::nullptr_t )
923 {
924 return StartUntil(_time);
925 }
926
928 template<typename T, typename F, typename... A> static
929 Timer StartUntil( const T& _time, F&& _proc, A&&... _args )
930 {
931 return StartUntil(_time, std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
932 }
933#endif
934
935#if __cplusplus >= 201402L
936 using Ptr = std::unique_ptr<Timer>;
937#else
938 using Ptr = Timer *;
939#endif
940
953
954 static
955 Ptr Create()
956 {
957 auto tmr = new (std::nothrow) Timer();
958 if (tmr != nullptr)
959 tmr->__tmr::obj.res = tmr;
960 return Ptr(tmr);
961 }
962
964 template<class F> static
965 Ptr Create( F&& _proc )
966 {
967 auto tmr = new (std::nothrow) Timer(_proc);
968 if (tmr != nullptr)
969 tmr->__tmr::obj.res = tmr;
970 return Ptr(tmr);
971 }
972
973#if __cplusplus >= 201402L
975 static
976 Ptr Create( std::nullptr_t )
977 {
978 return Create();
979 }
980
982 template<typename F, typename... A> static
983 Ptr Create( F&& _proc, A&&... _args )
984 {
985 return Create(std::bind(std::forward<F>(_proc), std::forward<A>(_args)...));
986 }
987#endif
988};
989
990} // namespace
991#endif//__cplusplus
992
993/* -------------------------------------------------------------------------- */
994
996
997#endif//__STATEOS_TMR_H
#define INFINITE
infinite waiting
Definition osbase.h:261
void fun_t(void)
timer/task procedure
Definition osbase.h:137
void tmr_delete(tmr_t *tmr)
alias of tmr_destroy()
Definition ostimer.h:283
int tmr_wait(tmr_t *tmr)
wait indefinitely until the timer finishes countdown
Definition ostimer.h:498
int tmr_takeISR(tmr_t *tmr)
ISR alias of tmr_take().
Definition ostimer.h:425
tmr_t * tmr_new(fun_t *proc)
alias of tmr_create()
Definition ostimer.h:255
void tmr_startPeriodic(tmr_t *tmr, cnt_t period)
start/restart periodic timer for given duration of time
Definition ostimer.h:337
void tmr_destroy(tmr_t *tmr)
reset the timer object, wake up all waiting tasks with 'E_DELETED' event value and free allocated res...
void tmr_reset(tmr_t *tmr)
reset the timer object and wake up all waiting tasks with 'E_STOPPED' event value
void tmr_stop(tmr_t *tmr)
stop timer
Definition ostimer.h:398
int tmr_tryWait(tmr_t *tmr)
alias of tmr_take()
Definition ostimer.h:421
tmr_t * tmr_thisISR(void)
return current timer object
Definition ostimer.h:225
void tmr_delayISR(cnt_t delay)
change delay time for current timer
Definition ostimer.h:523
int tmr_waitUntil(tmr_t *tmr, cnt_t time)
wait until given timepoint until the timer finishes countdown
void tmr_startUntil(tmr_t *tmr, cnt_t time)
start/restart one-shot timer until given timepoint
void tmr_init(tmr_t *tmr, fun_t *proc)
initialize a timer object
tmr_t * tmr_create(fun_t *proc)
create and initialize a new timer object
int tmr_take(tmr_t *tmr)
check if the timer finishes countdown
cnt_t tmr_overrunISR(void)
return the number of periods skipped by the current periodic timer
Definition ostimer.h:536
void tmr_kill(tmr_t *tmr)
alias of tmr_reset()
Definition ostimer.h:269
void tmr_startNext(tmr_t *tmr, cnt_t delay)
restart the timer for given duration of time from the end of the previous countdown
void tmr_flipISR(fun_t *proc)
change callback procedure for current periodic timer (available in next period)
Definition ostimer.h:510
int tmr_waitFor(tmr_t *tmr, cnt_t delay)
wait for given duration of time until the timer finishes countdown
int tmr_expired(tmr_t *tmr)
alias of tmr_take()
Definition ostimer.h:417
void tmr_startFor(tmr_t *tmr, cnt_t delay)
start/restart one-shot timer for given duration of time
Definition ostimer.h:319
void tmr_start(tmr_t *tmr, cnt_t delay, cnt_t period)
start/restart periodic timer for given duration of time
void tmr_startFrom(tmr_t *tmr, cnt_t delay, cnt_t period, fun_t *proc)
start/restart periodic timer for given duration of time
int tmr_waitNext(tmr_t *tmr, cnt_t delay)
wait for given duration of time from the end of the previous countdown
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 a timer object
Definition ostimer.h:673
Timer()
constructor, see the constructor parameters of the class
Definition ostimer.h:675
static Timer StartFor(const T &_delay)
create and initialize static timer object and start one-shot timer for given duration of time
Definition ostimer.h:814
std::unique_ptr< Timer > Ptr
result type of Create()
Definition ostimer.h:935
static Timer StartPeriodic(const T &_period)
create and initialize static timer object and start periodic timer for given duration of time
Definition ostimer.h:862
static Timer Start(const T &_delay, const T &_period)
create and initialize static timer object and start periodic timer for given duration of time
Definition ostimer.h:768
static Ptr Create()
create and initialize dynamic timer with manageable resources
Definition ostimer.h:954
static Timer StartUntil(const T &_time)
create and initialize static timer object and start one-shot timer until given timepoint
Definition ostimer.h:906
static Timer Make()
create and initialize static timer object
Definition ostimer.h:717