10#ifndef STK_TIME_TIMER_H_
11#define STK_TIME_TIMER_H_
27#ifndef STK_TIMER_THREADS_COUNT
28 #define STK_TIMER_THREADS_COUNT 1
34#ifndef STK_TIMER_HANDLER_STACK_SIZE
35 #define STK_TIMER_HANDLER_STACK_SIZE 256
41#ifndef STK_TIMER_COUNT_MAX
42 #define STK_TIMER_COUNT_MAX 32
198 bool Start(Timer &timer, uint32_t delay, uint32_t period = 0);
204 bool Stop(Timer &timer);
210 bool Reset(Timer &timer);
223 bool Restart(Timer &timer, uint32_t delay, uint32_t period = 0);
238 bool StartOrReset(Timer &timer, uint32_t delay, uint32_t period = 0);
249 bool SetPeriod(Timer &timer, uint32_t period);
379 return (remaining > 0 ?
static_cast<uint32_t
>(remaining) : 0);
554 while (timer !=
nullptr)
567 bool one_shot =
false;
596 __stk_full_memfence();
608 if ((next_deadline > 0) && (next_deadline < next_sleep))
609 next_sleep = next_deadline;
637 if (timer ==
nullptr)
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
#define STK_STACK_SIZE_MIN
Minimum stack size in elements of Word, shared by all stack allocation lower-bound checks.
Implementation of synchronization primitive: stk::sync::Event.
Implementation of synchronization primitive: stk::sync::Pipe.
#define STK_TIMER_THREADS_COUNT
Number of threads handling timers in TimerHost (default: 1).
#define STK_TIMER_HANDLER_STACK_SIZE
Stack size of the timer handler, increase if your timers consume more (default: 256).
Namespace of STK package.
uintptr_t Word
Native processor word type.
const Timeout WAIT_INFINITE
Timeout value: block indefinitely until the synchronization object is signaled.
Ticks GetTicks()
Get number of ticks elapsed since kernel start.
int64_t Ticks
Ticks value.
int32_t Timeout
Timeout time (ticks).
constexpr T Max(T a, T b) noexcept
Compile-time maximum of two values.
const Timeout NO_WAIT
Timeout value: return immediately if the synchronization object is not yet signaled (non-blocking pol...
EAccessMode
Hardware access mode by the user task.
@ ACCESS_USER
Unprivileged access mode (access to some hardware is restricted, see CPU manual for details).
__stk_forceinline void WriteVolatile64(volatile T *addr, T value)
Atomically write a 64-bit volatile value.
__stk_forceinline Word PtrToWord(T *ptr) noexcept
Cast a pointer to a CPU register-width integer.
__stk_forceinline T ReadVolatile64(volatile const T *addr)
Atomically read a 64-bit volatile value.
Word Type[_StackSize]
Stack memory type.
Interface for a user task.
Interface for the implementation of the kernel of the scheduler. It supports Soft and Hard Real-Time ...
virtual void AddTask(ITask *user_task)=0
Add user task.
Intrusive doubly-linked list container. Manages a collection of DListEntry nodes embedded in host obj...
Intrusive doubly-linked list node. Embed this as a base class in any object (T) that needs to partici...
DLHeadType * GetHead() const
Get the list head this entry currently belongs to.
bool IsLinked() const
Check whether this entry is currently a member of any list.
DLEntryType * GetNext() const
Get the next entry in the list.
DListEntry< Timer, _ClosedLoop > DLEntryType
Thread-safe FIFO communication pipe for inter-task data passing.
bool Reset(Timer &timer)
Reset periodic timer's deadline.
Ticks m_now
last known current time (ticks)
util::DListHead< Timer, false > m_active
active timers (tick task only)
bool SetPeriod(Timer &timer, uint32_t period)
Change the period of a running periodic timer without affecting its current deadline.
void Initialize(IKernel *kernel, EAccessMode mode)
Initialize timer host instance.
bool IsEmpty() const
Return true if no timers are currently active.
Ticks GetTimeNow() const
Get current time.
sync::Pipe< Timer *, 32 > ReadyQueue
TimerWorkerTask m_task_tick
timer task
TimerWorkerTask m_task_process[1]
handler tasks
size_t GetSize() const
Return number of currently active timers.
STK_NONCOPYABLE_CLASS(TimerHost)
TaskTickMemory m_task_tick_memory
tick task memory
void(* TimerFuncType)(TimerHost *host)
Timer task function prototype.
ReadyQueue m_queue
queue of timers ready for handling
bool Shutdown()
Shutdown host instance. All timers are stopped and removed from the host.
sync::Pipe< TimerCommand, 32 > CommandQueue
bool Restart(Timer &timer, uint32_t delay, uint32_t period=0)
Atomically stop and re-start timer.
bool StartOrReset(Timer &timer, uint32_t delay, uint32_t period=0)
Start timer if inactive, or reset its deadline if already active and periodic.
bool Stop(Timer &timer)
Stop running timer.
@ TASK_TICK_MEMORY_SIZE
stack memory size of the timer handler task
@ TASK_HANDLER_STACK_SIZE
@ TASK_COUNT
total number of tasks serving this instance
StackMemoryDef< TASK_TICK_MEMORY_SIZE >::Type TaskTickMemory
bool ProcessCommands(Timeout next_sleep)
bool Start(Timer &timer, uint32_t delay, uint32_t period=0)
Start timer.
bool PushCommand(TimerCommand cmd)
TimerHostMemory m_task_handler_memory[1]
handler task memory
CommandQueue m_commands
command queue
StackMemoryDef< TASK_HANDLER_STACK_SIZE >::Type TimerHostMemory
Abstract base class for a timer managed by TimerHost.
volatile bool m_active
true if active
Ticks m_deadline
absolute expiration time (ticks)
Ticks GetDeadline() const
Ticks m_timestamp
time at which timer expired (ticks), updated by TimerHost
uint32_t m_period
reload period in ticks (0 = one-shot)
STK_NONCOPYABLE_CLASS(Timer)
uint32_t GetRemainingTime() const
Get remaining ticks until the timer next expires.
uint32_t GetPeriod() const
volatile bool m_pending
true if pending to be handled
Ticks GetTimestamp() const
virtual void OnExpired(TimerHost *host)=0
The actual task that executes timer callback.
void Run()
Entry point of the user task.
void OnDeadlineMissed(uint32_t duration)
Called by the scheduler if deadline of the task is missed when Kernel is operating in Hard Real-Time ...
Word * GetStack() const
Get pointer to the stack memory.
void SetWeight(int32_t weight)
int32_t GetWeight() const
Get static base weight of the task.
const char * GetTraceName() const
Get task trace name set by application.
size_t GetStackSize() const
Get number of elements of the stack memory array.
void Initialize(TimerHost *host, Word *stack, size_t stack_size, EAccessMode mode, TimerFuncType func)
TId GetId() const
Get task Id set by application.
size_t GetStackSizeBytes() const
Get size of the memory in bytes.
EAccessMode GetAccessMode() const
Get hardware access mode of the user task.
@ CMD_SHUTDOWN
shutdown timer host
@ CMD_SET_PERIOD
change period of a running periodic timer
@ CMD_START_OR_RESET
start if inactive, reset deadline if active and periodic
@ CMD_RESTART
atomic stop + re-start
uint32_t period
reload period ticks (CMD_START / CMD_RESTART / CMD_START_OR_RESET / CMD_SET_PERIOD)
Timer * timer
timer instance
Ticks timestamp
hw tick count captured at call site
uint32_t delay
initial delay ticks (CMD_START / CMD_RESTART / CMD_START_OR_RESET)