![]() |
SuperTinyKernel™ RTOS 1.06.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
|
Software timer multiplexer that manages multiple Timer instances on top of a small fixed set of kernel tasks. More...
#include <stk_time_timer.h>
Classes | |
| class | Timer |
| Abstract base class for a timer managed by TimerHost. More... | |
| class | TimerWorkerTask |
| Internal kernel task used by TimerHost for both the tick task and handler tasks. More... | |
| struct | TimerCommand |
| POD command record passed from the public API methods to the tick task via the command queue. More... | |
Public Types | |
| enum | EConsts : size_t { TASK_COUNT = (1U + 1U ) , TASK_TICK_MEMORY_SIZE = Max<size_t>(256U, STK_STACK_SIZE_MIN) , TASK_HANDLER_STACK_SIZE = Max<size_t>( 256U , STK_STACK_SIZE_MIN) } |
Public Member Functions | |
| TimerHost () | |
| Default constructor. Zero-initializes all internal state. | |
| ~TimerHost ()=default | |
| Destructor. | |
| void | Initialize (IKernel *kernel, EAccessMode mode) |
| Initialize timer host instance. | |
| bool | Start (Timer &tmr, uint32_t delay, uint32_t period=0) |
| Start timer. | |
| bool | Stop (Timer &tmr) |
| Stop running timer. | |
| bool | Reset (Timer &tmr) |
| Reset periodic timer's deadline. | |
| bool | Restart (Timer &tmr, uint32_t delay, uint32_t period=0) |
| Atomically stop and re-start timer. | |
| bool | StartOrReset (Timer &tmr, uint32_t delay, uint32_t period=0) |
| Start timer if inactive, or reset its deadline if already active and periodic. | |
| bool | SetPeriod (Timer &tmr, uint32_t period) |
| Change the period of a running periodic timer without affecting its current deadline. | |
| bool | IsEmpty () const |
| Return true if no timers are currently active. | |
| size_t | GetSize () const |
| Return number of currently active timers. | |
| bool | Shutdown () |
| Shutdown host instance. All timers are stopped and removed from the host. | |
| Ticks | GetTimeNow () const |
| Get current time. | |
Private Types | |
| typedef void(* | TimerFuncType) (TimerHost *host) |
| Timer task function prototype. | |
| typedef StackMemoryDef< TASK_TICK_MEMORY_SIZE >::Type | TaskTickMemory |
| Stack memory type for the single tick task. | |
| typedef StackMemoryDef< TASK_HANDLER_STACK_SIZE >::Type | TimerHostMemory |
| Stack memory type for each handler task. | |
| typedef sync::PipeT< Timer *, 32U > | ReadyQueue |
| Lock-free pipe used to transfer expired timer pointers from the tick task to handler tasks. | |
| typedef sync::PipeT< TimerCommand, 32U > | CommandQueue |
| Lock-free pipe used to send TimerCommand records from API callers to the tick task. | |
Private Member Functions | |
| STK_NONCOPYABLE_CLASS (TimerHost) | |
| void | UpdateTime () |
| Tick task body: drives the timer list and dispatches expired timers. | |
| void | ProcessTimers () |
| Handler task body: dequeues expired timers and invokes their OnExpired() callbacks. | |
| bool | ProcessCommands (Timeout next_sleep) |
| Drain the command queue and execute each pending command. | |
| bool | PushCommand (TimerCommand cmd) |
| Enqueue a command for the tick task. | |
Private Attributes | |
| TaskTickMemory | m_task_tick_memory |
| tick task memory | |
| TimerHostMemory | m_task_handler_memory [1U] |
| handler task memory | |
| TimerWorkerTask | m_task_tick |
| timer task | |
| TimerWorkerTask | m_task_process [1U] |
| handler tasks | |
| util::DListHead< Timer, false > | m_active |
| active timers (tick task only) | |
| ReadyQueue | m_queue |
| queue of timers ready for handling | |
| CommandQueue | m_commands |
| command queue | |
| Ticks | m_now |
| last known current time (ticks) | |
Software timer multiplexer that manages multiple Timer instances on top of a small fixed set of kernel tasks.
TimerHost internally runs two categories of tasks:
All timers share the same tick and handler tasks, so the total kernel task overhead is constant regardless of how many timers are active.
Two timer modes are supported:
uint32_t (~49 days at 1 ms resolution).Definition at line 111 of file stk_time_timer.h.
|
private |
Lock-free pipe used to send TimerCommand records from API callers to the tick task.
Definition at line 462 of file stk_time_timer.h.
|
private |
Lock-free pipe used to transfer expired timer pointers from the tick task to handler tasks.
Definition at line 457 of file stk_time_timer.h.
Stack memory type for the single tick task.
Definition at line 447 of file stk_time_timer.h.
|
private |
Timer task function prototype.
Definition at line 318 of file stk_time_timer.h.
Stack memory type for each handler task.
Definition at line 452 of file stk_time_timer.h.
| enum stk::time::TimerHost::EConsts : size_t |
| Enumerator | |
|---|---|
| TASK_COUNT | total number of tasks serving this instance stack memory size of the tick task |
| TASK_TICK_MEMORY_SIZE | stack memory size of the timer handler task |
| TASK_HANDLER_STACK_SIZE | |
Definition at line 114 of file stk_time_timer.h.
|
inlineexplicit |
Default constructor. Zero-initializes all internal state.
Definition at line 215 of file stk_time_timer.h.
References m_active, m_now, m_task_handler_memory, m_task_process, m_task_tick, and m_task_tick_memory.
Referenced by Initialize(), stk::time::TimerHost::TimerWorkerTask::Initialize(), and STK_NONCOPYABLE_CLASS().
|
default |
Destructor.
|
inline |
Return number of currently active timers.
Definition at line 300 of file stk_time_timer.h.
References m_active.
Referenced by stk_timerhost_get_size().
|
inline |
Get current time.
Definition at line 310 of file stk_time_timer.h.
References m_now, and stk::hw::ReadVolatile64().
Referenced by stk_timerhost_get_time_now().
|
inline |
Initialize timer host instance.
| [in] | kernel | Kernel to which instance will be bound. |
| [in] | mode | Access mode for the timer handling tasks which call expired timers. |
Definition at line 502 of file stk_time_timer.h.
References stk::ACCESS_USER, stk::IKernel::AddTask(), m_task_handler_memory, m_task_process, m_task_tick, m_task_tick_memory, ProcessTimers(), STK_TIMER_THREADS_COUNT, TASK_HANDLER_STACK_SIZE, TASK_TICK_MEMORY_SIZE, TimerHost(), and UpdateTime().
Referenced by stk::interop_c_helper::InitializeTimerHost().
|
inline |
Return true if no timers are currently active.
Definition at line 294 of file stk_time_timer.h.
References m_active.
Referenced by stk_timerhost_is_empty().
|
inlineprivate |
Drain the command queue and execute each pending command.
| [in] | next_sleep | Maximum ticks to block waiting for the first command when the active list is non-empty. Overridden to WAIT_INFINITE when empty. |
Definition at line 814 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::cmd, stk::time::TimerHost::TimerCommand::CMD_RESET, stk::time::TimerHost::TimerCommand::CMD_RESTART, stk::time::TimerHost::TimerCommand::CMD_SET_PERIOD, stk::time::TimerHost::TimerCommand::CMD_SHUTDOWN, stk::time::TimerHost::TimerCommand::CMD_START, stk::time::TimerHost::TimerCommand::CMD_START_OR_RESET, stk::time::TimerHost::TimerCommand::CMD_STOP, stk::time::TimerHost::TimerCommand::delay, stk::util::DListEntry< T, TClosedLoop >::GetHead(), stk::util::DListEntry< T, TClosedLoop >::IsLinked(), m_active, stk::time::TimerHost::Timer::m_active, m_commands, stk::time::TimerHost::Timer::m_deadline, stk::time::TimerHost::Timer::m_pending, stk::time::TimerHost::Timer::m_period, m_queue, stk::time::TimerHost::Timer::m_rearming, stk::NO_WAIT, stk::time::TimerHost::TimerCommand::period, STK_ASSERT, STK_TIMER_THREADS_COUNT, STK_UNUSED, stk::time::TimerHost::TimerCommand::timer, stk::time::TimerHost::TimerCommand::timestamp, and stk::WAIT_INFINITE.
Referenced by UpdateTime().
|
inlineprivate |
Handler task body: dequeues expired timers and invokes their OnExpired() callbacks.
Definition at line 781 of file stk_time_timer.h.
References stk::time::TimerHost::Timer::m_pending, m_queue, and stk::time::TimerHost::Timer::OnExpired().
Referenced by Initialize().
|
inlineprivate |
Enqueue a command for the tick task.
| [in] | cmd | Fully initialized TimerCommand to push. |
Definition at line 1000 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::cmd, stk::time::TimerHost::TimerCommand::CMD_RESTART, stk::time::TimerHost::TimerCommand::CMD_START_OR_RESET, m_commands, stk::time::TimerHost::Timer::m_rearming, stk::NO_WAIT, STK_ASSERT, and stk::time::TimerHost::TimerCommand::timer.
Referenced by Reset(), Restart(), SetPeriod(), Shutdown(), Start(), StartOrReset(), and Stop().
|
inline |
Reset periodic timer's deadline.
| [in] | tmr | Timer instance. Must be active and periodic. |
Definition at line 583 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_RESET, stk::GetTicks(), stk::time::TimerHost::Timer::m_active, stk::time::TimerHost::Timer::m_period, and PushCommand().
Referenced by stk_timer_reset().
|
inline |
Atomically stop and re-start timer.
| [in] | tmr | Timer instance (active or inactive). |
| [in] | delay | Initial delay in ticks before first expiration. |
| [in] | period | Reload period in ticks (0 is one-shot timer). |
Definition at line 610 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_RESTART, stk::GetTicks(), PushCommand(), STK_ASSERT, and stk::WAIT_INFINITE.
Referenced by stk_timer_restart().
|
inline |
Change the period of a running periodic timer without affecting its current deadline.
| [in] | tmr | Timer instance. Must be active and periodic. |
| [in] | period | New reload period in ticks. Must be non-zero. |
Definition at line 646 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_SET_PERIOD, stk::time::TimerHost::Timer::m_active, stk::time::TimerHost::Timer::m_period, PushCommand(), and stk::WAIT_INFINITE.
Referenced by stk_timer_set_period().
|
inline |
Shutdown host instance. All timers are stopped and removed from the host.
Definition at line 675 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_SHUTDOWN, and PushCommand().
Referenced by stk_timerhost_shutdown().
|
inline |
Start timer.
| [in] | tmr | Timer instance. Must not already be active. |
| [in] | delay | Initial delay in ticks before first expiration. |
| [in] | period | Reload period in ticks (0 is one-shot timer). |
Definition at line 524 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_START, stk::GetTicks(), stk::time::TimerHost::Timer::m_active, PushCommand(), STK_ASSERT, and stk::WAIT_INFINITE.
Referenced by stk_timer_start().
|
inline |
Start timer if inactive, or reset its deadline if already active and periodic.
| [in] | tmr | Timer instance (active or inactive). |
| [in] | delay | Initial delay in ticks (used only when starting). |
| [in] | period | Reload period in ticks (used only when starting, 0 is one-shot). |
Definition at line 628 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_START_OR_RESET, stk::GetTicks(), PushCommand(), STK_ASSERT, and stk::WAIT_INFINITE.
Referenced by stk_timer_start_or_reset().
|
private |
|
inline |
Stop running timer.
| [in] | tmr | Timer instance. Must be active. |
Definition at line 555 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_STOP, stk::time::TimerHost::Timer::m_active, and PushCommand().
Referenced by FrtosPendDrainer::OnExpired(), and stk_timer_stop().
|
inlineprivate |
Tick task body: drives the timer list and dispatches expired timers.
Definition at line 690 of file stk_time_timer.h.
References stk::util::DListEntry< T, TClosedLoop >::GetNext(), stk::GetTicks(), stk::util::DListCast::ListEntryToParent(), m_active, stk::time::TimerHost::Timer::m_active, stk::time::TimerHost::Timer::m_deadline, m_now, stk::time::TimerHost::Timer::m_pending, stk::time::TimerHost::Timer::m_period, m_queue, stk::time::TimerHost::Timer::m_timestamp, stk::NO_WAIT, ProcessCommands(), STK_ASSERT, STK_UNUSED, stk::WAIT_INFINITE, and stk::hw::WriteVolatile64().
Referenced by Initialize().
|
private |
active timers (tick task only)
Definition at line 468 of file stk_time_timer.h.
Referenced by GetSize(), IsEmpty(), ProcessCommands(), TimerHost(), and UpdateTime().
|
private |
command queue
Definition at line 470 of file stk_time_timer.h.
Referenced by ProcessCommands(), and PushCommand().
|
private |
last known current time (ticks)
Definition at line 471 of file stk_time_timer.h.
Referenced by GetTimeNow(), TimerHost(), and UpdateTime().
|
private |
queue of timers ready for handling
Definition at line 469 of file stk_time_timer.h.
Referenced by ProcessCommands(), ProcessTimers(), and UpdateTime().
|
private |
handler task memory
Definition at line 465 of file stk_time_timer.h.
Referenced by Initialize(), and TimerHost().
|
private |
handler tasks
Definition at line 467 of file stk_time_timer.h.
Referenced by Initialize(), and TimerHost().
|
private |
timer task
Definition at line 466 of file stk_time_timer.h.
Referenced by Initialize(), and TimerHost().
|
private |
tick task memory
Definition at line 464 of file stk_time_timer.h.
Referenced by Initialize(), and TimerHost().