|
SuperTinyKernel™ RTOS 1.08.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 | |
| struct | TimerInfo |
| Consistent snapshot of a timer, see QuerySync(). More... | |
| struct | Config |
| Optional configuration of the timer tasks, see Initialize(). More... | |
| class | TimerWorkerTask |
| Internal kernel task used by TimerHost for both the tick task and handler tasks. More... | |
| struct | Completion |
| Completion point of a synchronous command, lives on the stack of the waiting caller. 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) } |
| enum | EResult : int8_t { RES_CANCELLED = -5 , RES_INVALID_PARAM = -4 , RES_QUEUE_FULL = -3 , RES_NOT_READY = -2 , RES_FAIL = -1 , RES_OK = 0 , RES_ACTIVE = 1 , RES_INACTIVE = 2 , RES_NOT_PERIODIC = 3 , RES_QUEUED = 4 } |
| Outcome of a command: returned by the asynchronous API (Start(), Stop(), ...) and by the synchronous API (StartSync() and the other *Sync() functions), see "Result codes" in TimerHost. More... | |
| typedef ::stk::time::Timer | Timer |
| Alias of stk::time::Timer, keeps the former nested name TimerHost::Timer valid. | |
Public Member Functions | |
| TimerHost () | |
| Default constructor. Zero-initializes all internal state. | |
| ~TimerHost ()=default | |
| Destructor. | |
| EAddResult | Initialize (IKernel *kernel, EAccessMode mode, const Config &cfg=Config()) |
| Initialize timer host instance. | |
| void | InitializePolled () |
| Initialize timer host instance in polled mode (no kernel tasks are created). | |
| Timeout | Process () |
| Run the host once (polled mode only): execute pending commands, evaluate deadlines and invoke OnExpired() of every expired timer inline. | |
| EResult | Start (Timer &tmr, uint32_t delay, uint32_t period=0) |
| Start timer. | |
| EResult | Stop (Timer &tmr) |
| Stop running timer. | |
| EResult | Reset (Timer &tmr) |
| Reset periodic timer's deadline. | |
| EResult | Restart (Timer &tmr, uint32_t delay, uint32_t period=0) |
| Atomically stop and re-start timer. | |
| EResult | StartOrReset (Timer &tmr, uint32_t delay, uint32_t period=0) |
| Start timer if inactive, or reset its deadline if already active and periodic. | |
| EResult | SetPeriod (Timer &tmr, uint32_t period) |
| Change the period of a running periodic timer without affecting its current deadline. | |
| EResult | StartSync (Timer &tmr, uint32_t delay, uint32_t period=0) |
| Start timer and wait for the result (see "Synchronous API" in TimerHost). | |
| EResult | ActivateSync (Timer &tmr) |
| Start timer with the delay and period stored in it and wait for the result. | |
| EResult | StopSync (Timer &tmr) |
| Stop timer and wait for the result. | |
| EResult | RestartSync (Timer &tmr, uint32_t delay, uint32_t period=0) |
| Atomically stop and re-start timer and wait for the result. | |
| EResult | ResetSync (Timer &tmr) |
| Reset periodic timer's deadline and wait for the result. | |
| EResult | SetPeriodSync (Timer &tmr, uint32_t period) |
| Change the period of a running periodic timer without affecting its current deadline and wait for the result. | |
| EResult | ChangeSync (Timer &tmr, uint32_t delay, uint32_t period) |
| Change the delay and period of an inactive timer and wait for the result. | |
| EResult | QuerySync (Timer &tmr, TimerInfo &info) |
| Get a consistent snapshot of the timer (evaluated by the tick task). | |
| bool | IsHostTid (TId tid) |
| Check whether a task id belongs to a task of this host (tick or handler task). | |
| bool | IsEmpty () const |
| Return true if no timers are currently active. | |
| size_t | GetSize () const |
| Return number of currently active timers. | |
| EResult | 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 detail::DListHead< Timer::ReadyNode, false > | ReadyList |
| Intrusive list of the expired timers (via Timer::m_ready_node) handed over from the tick task to the handler tasks. Protected by a critical section. | |
| 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. | |
| Timeout | ScanTimers (Ticks now) |
| Evaluate all active timers against the given time. | |
| void | ProcessTimers () |
| Handler task body: dequeues expired timers from the ready list and invokes their OnExpired() callbacks. | |
| bool | ProcessCommands (Timeout next_sleep) |
| Drain the command queue and execute each pending command. | |
| EResult | PushCommand (TimerCommand cmd, Timeout wait=NO_WAIT) |
| Enqueue a command for the tick task. | |
| bool | CanWait () const |
| Check whether the calling context can wait for the completion of a command. | |
| void | CancelPending (Timer &tmr) |
| Cancel a pending (expired, not yet handled) expiration of the timer. | |
| void | WaitCallbackDone (Timer &tmr) |
| Wait until no handler task is executing the callback of the timer. | |
| EResult | Execute (TimerCommand::EId id, Timer &tmr, uint32_t delay, uint32_t period, TimerInfo *info) |
| Execute a command synchronously, or queue it if the caller can not wait. | |
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 | |
| detail::DListHead< Timer, false > | m_active |
| active timers (tick task only) | |
| ReadyList | m_ready_list |
| expired timers waiting for a handler task (access only inside a critical section) | |
| sync::Event | m_ready_event |
| auto-reset event which wakes a handler task when m_ready_list gets an entry or on shutdown | |
| CommandQueue | m_commands |
| command queue | |
| Ticks | m_now |
| last known current time (ticks) | |
| bool | m_polled |
| true in polled mode (InitializePolled()), no tasks, callbacks run inline from Process() | |
| volatile bool | m_ready |
| true while the host has a consumer of the command queue (initialized and not shut down) | |
| volatile bool | m_shutdown |
| true once CMD_SHUTDOWN was received (task mode): handlers exit after draining m_ready_list | |
| Timer *volatile | m_running [1U] |
| timers whose callback is being executed by a handler task (one slot per handler: set inside the claim critical section, cleared and polled lock-free), see WaitCallbackDone() | |
| IKernel * | m_kernel |
| kernel of the task mode (nullptr in polled mode) | |
| volatile TId | m_tick_tid |
| id of the tick task (TID_NONE until it runs), commands must not be waited for from it | |
Software timer multiplexer that manages multiple Timer instances on top of a small fixed set of kernel tasks.
TimerHost has two operating modes:
In task mode 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). It is recommended that the tick task is at least as urgent as the handler tasks, otherwise a long OnExpired() callback delays evaluation of all other deadlines.
Configuration examples (pick the one matching the strategy the kernel is built with, a kernel using sched::SwitchStrategyRoundRobin, sched::SwitchStrategyEDF or sched::SwitchStrategyMonotonic needs no Config):
Process() returns the number of ticks until the earliest deadline, a non-HRT application can use it to sleep only as long as needed (commands issued meanwhile are picked up when the sleep ends, hence the cap):
if(host.Start(...)) which was written for the former bool return value means "failed" now, compare with RES_OK explicitly.Definition at line 433 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 992 of file stk_time_timer.h.
|
private |
Intrusive list of the expired timers (via Timer::m_ready_node) handed over from the tick task to the handler tasks. Protected by a critical section.
Definition at line 987 of file stk_time_timer.h.
Stack memory type for the single tick task.
Definition at line 976 of file stk_time_timer.h.
Alias of stk::time::Timer, keeps the former nested name TimerHost::Timer valid.
Definition at line 485 of file stk_time_timer.h.
|
private |
Timer task function prototype.
Definition at line 775 of file stk_time_timer.h.
Stack memory type for each handler task.
Definition at line 981 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 436 of file stk_time_timer.h.
| enum stk::time::TimerHost::EResult : int8_t |
Outcome of a command: returned by the asynchronous API (Start(), Stop(), ...) and by the synchronous API (StartSync() and the other *Sync() functions), see "Result codes" in TimerHost.
| Enumerator | |
|---|---|
| RES_CANCELLED | The wait for a free command queue slot was cancelled (IKernel::CancelTaskWait()), nothing is queued. |
| RES_INVALID_PARAM | An argument is out of range (e.g. zero period for SetPeriod()), nothing is queued. |
| RES_QUEUE_FULL | The command queue is full and the caller can not wait (ISR, kernel not started, polled mode, tick task), nothing is queued. |
| RES_NOT_READY | Host is not initialized (or its initialization failed), or it has been shut down. |
| RES_FAIL | Unspecified failure. |
| RES_OK | Sync: the command was executed. Async: the command was accepted into the command queue. |
| RES_ACTIVE | The timer is active, but the command requires it to be inactive (start, activate, change). |
| RES_INACTIVE | The timer is not active, but the command requires it to be active (stop, reset). |
| RES_NOT_PERIODIC | The timer is not periodic, but the command requires it to be. |
| RES_QUEUED | The calling context can not wait: the command was queued asynchronously, its outcome is unknown. |
Definition at line 454 of file stk_time_timer.h.
|
inlineexplicit |
Default constructor. Zero-initializes all internal state.
Definition at line 522 of file stk_time_timer.h.
References m_active, m_kernel, m_now, m_polled, m_ready, m_ready_event, m_ready_list, m_running, m_shutdown, m_task_handler_memory, m_task_process, m_task_tick, m_task_tick_memory, m_tick_tid, and stk::TID_NONE.
Referenced by Initialize(), stk::time::TimerHost::TimerWorkerTask::Initialize(), and STK_NONCOPYABLE_CLASS().
|
default |
Destructor.
|
inline |
Start timer with the delay and period stored in it and wait for the result.
| [in] | tmr | Timer instance. Must not be active. |
Definition at line 2080 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_ACTIVATE, and Execute().
Referenced by stk_timer_activate_sync().
|
inlineprivate |
Cancel a pending (expired, not yet handled) expiration of the timer.
| [in] | tmr | Timer instance. |
Clears Timer::m_pending and, in task mode, unlinks the timer's node from the ready list, so no handler task can dequeue it afterwards and the host holds no queued reference to tmr. Atomic with the dequeue done by a handler task: if the handler got there first, the callback is already claimed and runs (it is not interrupted).
Definition at line 1928 of file stk_time_timer.h.
References stk::detail::DListEntry< T, TClosedLoop >::IsLinked(), stk::time::Timer::m_pending, m_polled, m_ready_list, and stk::time::Timer::m_ready_node.
Referenced by ProcessCommands().
|
inlineprivate |
Check whether the calling context can wait for the completion of a command.
Definition at line 1919 of file stk_time_timer.h.
References stk::GetTid(), stk::hw::IsInsideISR(), stk::IKernel::KSTATE_RUNNING, m_kernel, m_polled, m_ready, and m_tick_tid.
Referenced by Execute(), and PushCommand().
|
inline |
Change the delay and period of an inactive timer and wait for the result.
| [in] | tmr | Timer instance. Must not be active. |
| [in] | delay | New initial delay in ticks. |
| [in] | period | New reload period in ticks (0 is one-shot timer). |
Definition at line 2112 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_CHANGE, and Execute().
Referenced by stk_timer_change_sync().
|
inlineprivate |
Execute a command synchronously, or queue it if the caller can not wait.
| [in] | id | Command id. |
| [in] | tmr | Target timer. |
| [in] | delay | Delay argument of the command. |
| [in] | period | Period argument of the command. |
| [out] | info | Snapshot for CMD_QUERY, otherwise nullptr. |
Definition at line 1982 of file stk_time_timer.h.
References stk::time::TimerHost::TimerInfo::active, CanWait(), stk::time::TimerHost::TimerCommand::CMD_QUERY, stk::time::TimerHost::TimerCommand::CMD_STOP, stk::time::TimerHost::TimerInfo::delay, stk::time::TimerHost::Completion::done, stk::time::Timer::GetRemainingTicks(), stk::GetTicks(), stk::time::TimerHost::Completion::info, stk::time::Timer::m_active, stk::time::Timer::m_delay, stk::time::Timer::m_period, m_ready, stk::time::TimerHost::TimerInfo::period, PushCommand(), stk::time::TimerHost::TimerInfo::remaining, RES_INACTIVE, RES_NOT_READY, RES_OK, RES_QUEUED, stk::time::TimerHost::Completion::result, stk::Sleep(), STK_ASSERT, stk::WAIT_INFINITE, stk::WAIT_RESULT_FAIL, stk::WAIT_RESULT_SIGNAL, WaitCallbackDone(), and stk::sync::Event::WaitEx().
Referenced by ActivateSync(), ChangeSync(), QuerySync(), ResetSync(), RestartSync(), SetPeriodSync(), StartSync(), and StopSync().
|
inline |
Return number of currently active timers.
Definition at line 755 of file stk_time_timer.h.
References m_active.
Referenced by stk_timerhost_get_size().
|
inline |
Get current time.
Definition at line 767 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. The access mode of the tick task is Config::tick_mode (ACCESS_USER by default). |
| [in] | cfg | Weights, access mode of the tick task and (optionally) stack buffers of the tick and handler tasks, by default DEFAULT_WEIGHT, ACCESS_USER and the internal stacks are used (see Config). |
Definition at line 1039 of file stk_time_timer.h.
References stk::ADD_OK, stk::IKernel::AddTask(), stk::time::TimerHost::Config::handler_stack, stk::time::TimerHost::Config::handler_stack_size, stk::time::TimerHost::Config::handler_weight, m_kernel, m_polled, m_ready, m_shutdown, m_task_handler_memory, m_task_process, m_task_tick, m_task_tick_memory, stk::NO_WEIGHT, STK_ASSERT, STK_STACK_SIZE_MIN, STK_TIMER_THREADS_COUNT, TASK_HANDLER_STACK_SIZE, TASK_TICK_MEMORY_SIZE, stk::time::TimerHost::Config::tick_mode, stk::time::TimerHost::Config::tick_stack, stk::time::TimerHost::Config::tick_stack_size, stk::time::TimerHost::Config::tick_weight, and TimerHost().
Referenced by cy_rtos_stk_kernel_init(), stk::interop_c_helper::InitializeTimerHost(), stk::interop_c_helper::InitializeTimerHostEx(), and RunExample().
|
inline |
Initialize timer host instance in polled mode (no kernel tasks are created).
The application drives the host by calling Process() periodically. Expired timers are handled inline, from the context which calls Process(), see "Polled mode and HRT kernels" in TimerHost. This is the mode to use with a stk::KERNEL_HRT kernel.
Definition at line 1420 of file stk_time_timer.h.
References m_kernel, m_polled, and m_ready.
Referenced by stk_timerhost_init_polled().
|
inline |
Return true if no timers are currently active.
Definition at line 749 of file stk_time_timer.h.
References m_active.
Referenced by stk_timerhost_is_empty().
|
inline |
Check whether a task id belongs to a task of this host (tick or handler task).
Definition at line 728 of file stk_time_timer.h.
References stk::GetTidFromUserTask(), m_polled, m_task_process, m_task_tick, STK_TIMER_THREADS_COUNT, and stk::TID_NONE.
Referenced by WaitCallbackDone().
|
inline |
Run the host once (polled mode only): execute pending commands, evaluate deadlines and invoke OnExpired() of every expired timer inline.
Definition at line 1431 of file stk_time_timer.h.
References stk::GetTicks(), m_polled, stk::NO_WAIT, ProcessCommands(), ScanTimers(), STK_ASSERT, and STK_UNUSED.
Referenced by stk_timerhost_process().
|
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 (task mode only, polled mode never blocks). |
Definition at line 1540 of file stk_time_timer.h.
References stk::time::TimerHost::TimerInfo::active, CancelPending(), stk::time::TimerHost::TimerCommand::cmd, stk::time::TimerHost::TimerCommand::CMD_ACTIVATE, stk::time::TimerHost::TimerCommand::CMD_CHANGE, stk::time::TimerHost::TimerCommand::CMD_QUERY, 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::completion, stk::time::TimerHost::TimerCommand::delay, stk::time::TimerHost::TimerInfo::delay, stk::time::TimerHost::Completion::done, stk::detail::DListEntry< T, TClosedLoop >::GetHead(), stk::GetTicks(), stk::time::TimerHost::Completion::info, stk::detail::DListEntry< T, TClosedLoop >::IsLinked(), stk::detail::DListCast::ListEntryToParent(), stk::time::Timer::m_active, m_active, m_commands, stk::time::Timer::m_deadline, stk::time::Timer::m_delay, stk::time::Timer::m_pending, stk::time::Timer::m_period, m_polled, m_ready_event, stk::time::Timer::m_rearming, m_shutdown, stk::NO_WAIT, stk::time::TimerHost::TimerCommand::period, stk::time::TimerHost::TimerInfo::period, stk::time::TimerHost::TimerInfo::remaining, RES_ACTIVE, RES_FAIL, RES_INACTIVE, RES_NOT_PERIODIC, RES_OK, stk::time::TimerHost::Completion::result, stk::sync::Event::Set(), STK_ASSERT, STK_UNUSED, stk::time::TimerHost::TimerCommand::timer, stk::time::TimerHost::TimerCommand::timestamp, and stk::WAIT_INFINITE.
Referenced by Process(), and UpdateTime().
|
inlineprivate |
Handler task body: dequeues expired timers from the ready list and invokes their OnExpired() callbacks.
Definition at line 1445 of file stk_time_timer.h.
References stk::detail::DListCast::ListEntryToParent(), stk::time::Timer::m_pending, m_ready_event, m_ready_list, m_running, m_shutdown, stk::time::Timer::OnExpired(), stk::Sleep(), STK_ASSERT, STK_TIMER_THREADS_COUNT, STK_UNUSED, stk::WAIT_INFINITE, and stk::WAIT_RESULT_FAIL.
|
inlineprivate |
Enqueue a command for the tick task.
| [in] | cmd | Fully initialized TimerCommand to push. |
| [in] | wait | Maximum ticks to wait for a free queue slot: NO_WAIT (default) does not wait unless the queue is full and the caller can wait (see CanWait()), WAIT_INFINITE blocks until the tick task has drained at least one command. |
Definition at line 1849 of file stk_time_timer.h.
References CanWait(), stk::time::TimerHost::TimerCommand::cmd, stk::time::TimerHost::TimerCommand::CMD_RESTART, stk::time::TimerHost::TimerCommand::CMD_START_OR_RESET, stk::time::TimerHost::TimerCommand::completion, m_commands, m_ready, stk::time::Timer::m_rearming, stk::NO_WAIT, RES_CANCELLED, RES_NOT_READY, RES_OK, RES_QUEUE_FULL, stk::time::TimerHost::TimerCommand::timer, and stk::WAIT_INFINITE.
Referenced by Execute(), Reset(), Restart(), SetPeriod(), Shutdown(), Start(), StartOrReset(), and Stop().
|
inline |
Get a consistent snapshot of the timer (evaluated by the tick task).
| [in] | tmr | Timer instance. |
| [out] | info | Snapshot, valid if the result is RES_OK. |
Definition at line 2117 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_QUERY, and Execute().
Referenced by cy_rtos_timer_is_running(), and stk_timer_query_sync().
|
inline |
Reset periodic timer's deadline.
| [in] | tmr | Timer instance. Must be active and periodic. |
Definition at line 1162 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_RESET, stk::GetTicks(), stk::time::Timer::m_active, stk::time::Timer::m_period, PushCommand(), RES_INACTIVE, and RES_NOT_PERIODIC.
Referenced by stk_timer_reset().
|
inline |
Reset periodic timer's deadline and wait for the result.
| [in] | tmr | Timer instance. Must be active and periodic. |
Definition at line 2095 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_RESET, and Execute().
Referenced by stk_timer_reset_sync().
|
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 1194 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 |
Atomically stop and re-start timer and wait for the result.
| [in] | tmr | Timer instance (active or inactive). |
| [in] | delay | Initial delay in ticks before first expiration, stored in the timer. |
| [in] | period | Reload period in ticks (0 is one-shot timer), stored in the timer. |
Definition at line 2090 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_RESTART, and Execute().
Referenced by cy_rtos_timer_start(), osThreadFeedWatchdog(), osTimerStart(), and stk_timer_restart_sync().
Evaluate all active timers against the given time.
| [in] | now | Current time (ticks). |
Definition at line 1311 of file stk_time_timer.h.
References stk::detail::DListEntry< T, TClosedLoop >::GetNext(), stk::detail::DListEntry< T, TClosedLoop >::IsLinked(), stk::detail::DListCast::ListEntryToParent(), stk::time::Timer::m_active, m_active, stk::time::Timer::m_deadline, m_now, stk::time::Timer::m_pending, stk::time::Timer::m_period, m_polled, m_ready_event, m_ready_list, stk::time::Timer::m_ready_node, stk::time::Timer::m_timestamp, stk::NO_WAIT, stk::time::Timer::OnExpired(), STK_ASSERT, STK_UNUSED, stk::WAIT_INFINITE, and stk::hw::WriteVolatile64().
Referenced by Process(), and UpdateTime().
|
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 1232 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_SET_PERIOD, stk::time::Timer::m_active, stk::time::Timer::m_period, PushCommand(), RES_INACTIVE, RES_INVALID_PARAM, RES_NOT_PERIODIC, and stk::WAIT_INFINITE.
Referenced by stk_timer_set_period().
|
inline |
Change the period of a running periodic timer without affecting its current deadline and wait for the result.
| [in] | tmr | Timer instance. Must be active and periodic. |
| [in] | period | New reload period in ticks. Must be non-zero. |
Definition at line 2100 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_SET_PERIOD, Execute(), RES_INVALID_PARAM, and stk::WAIT_INFINITE.
Referenced by stk_timer_set_period_sync().
|
inline |
Shutdown host instance. All timers are stopped and removed from the host.
Definition at line 1269 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_SHUTDOWN, and PushCommand().
Referenced by ShutdownTimer::OnExpired(), and 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 1103 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_START, stk::GetTicks(), stk::time::Timer::m_active, PushCommand(), RES_ACTIVE, STK_ASSERT, and stk::WAIT_INFINITE.
Referenced by RunExample(), and 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 1213 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().
|
inline |
Start timer and wait for the result (see "Synchronous API" in TimerHost).
| [in] | tmr | Timer instance. Must not be active. |
| [in] | delay | Initial delay in ticks before first expiration, stored in the timer. |
| [in] | period | Reload period in ticks (0 is one-shot timer), stored in the timer. |
Definition at line 2075 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_START, and Execute().
Referenced by stk_timer_start_sync().
|
private |
|
inline |
Stop running timer.
| [in] | tmr | Timer instance. Must be active. |
Definition at line 1134 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_STOP, stk::time::Timer::m_active, PushCommand(), and RES_INACTIVE.
Referenced by stk_timer_stop().
|
inline |
Stop timer and wait for the result.
| [in] | tmr | Timer instance. |
Definition at line 2085 of file stk_time_timer.h.
References stk::time::TimerHost::TimerCommand::CMD_STOP, and Execute().
Referenced by cy_rtos_timer_deinit(), cy_rtos_timer_stop(), osThreadFeedWatchdog(), osTimerDelete(), osTimerStop(), and stk_timer_stop_sync().
|
inlineprivate |
Tick task body: drives the timer list and dispatches expired timers.
Definition at line 1285 of file stk_time_timer.h.
References stk::GetTicks(), stk::GetTid(), m_active, m_ready, m_tick_tid, stk::NO_WAIT, ProcessCommands(), and ScanTimers().
|
inlineprivate |
Wait until no handler task is executing the callback of the timer.
| [in] | tmr | Timer instance, already stopped (CancelPending() done by the tick task), so no new callback of it can start and the wait is bounded by the callback which is running now. |
Polls the running slots every tick (Sleep(1)), lock-free: it takes no critical section.
Definition at line 1950 of file stk_time_timer.h.
References stk::GetTid(), IsHostTid(), m_running, stk::Sleep(), and STK_TIMER_THREADS_COUNT.
Referenced by Execute().
|
private |
active timers (tick task only)
Definition at line 998 of file stk_time_timer.h.
Referenced by GetSize(), IsEmpty(), ProcessCommands(), ScanTimers(), TimerHost(), and UpdateTime().
|
private |
command queue
Definition at line 1001 of file stk_time_timer.h.
Referenced by ProcessCommands(), and PushCommand().
|
private |
kernel of the task mode (nullptr in polled mode)
Definition at line 1007 of file stk_time_timer.h.
Referenced by CanWait(), Initialize(), InitializePolled(), and TimerHost().
|
private |
last known current time (ticks)
Definition at line 1002 of file stk_time_timer.h.
Referenced by GetTimeNow(), ScanTimers(), and TimerHost().
|
private |
true in polled mode (InitializePolled()), no tasks, callbacks run inline from Process()
Definition at line 1003 of file stk_time_timer.h.
Referenced by CancelPending(), CanWait(), Initialize(), InitializePolled(), IsHostTid(), Process(), ProcessCommands(), ScanTimers(), and TimerHost().
|
private |
true while the host has a consumer of the command queue (initialized and not shut down)
Definition at line 1004 of file stk_time_timer.h.
Referenced by CanWait(), Execute(), Initialize(), InitializePolled(), PushCommand(), TimerHost(), and UpdateTime().
|
private |
auto-reset event which wakes a handler task when m_ready_list gets an entry or on shutdown
Definition at line 1000 of file stk_time_timer.h.
Referenced by ProcessCommands(), ProcessTimers(), ScanTimers(), and TimerHost().
|
private |
expired timers waiting for a handler task (access only inside a critical section)
Definition at line 999 of file stk_time_timer.h.
Referenced by CancelPending(), ProcessTimers(), ScanTimers(), and TimerHost().
|
private |
timers whose callback is being executed by a handler task (one slot per handler: set inside the claim critical section, cleared and polled lock-free), see WaitCallbackDone()
Definition at line 1006 of file stk_time_timer.h.
Referenced by ProcessTimers(), TimerHost(), and WaitCallbackDone().
|
private |
true once CMD_SHUTDOWN was received (task mode): handlers exit after draining m_ready_list
Definition at line 1005 of file stk_time_timer.h.
Referenced by Initialize(), ProcessCommands(), ProcessTimers(), and TimerHost().
|
private |
handler task memory
Definition at line 995 of file stk_time_timer.h.
Referenced by Initialize(), and TimerHost().
|
private |
handler tasks
Definition at line 997 of file stk_time_timer.h.
Referenced by Initialize(), IsHostTid(), and TimerHost().
|
private |
timer task
Definition at line 996 of file stk_time_timer.h.
Referenced by Initialize(), IsHostTid(), and TimerHost().
|
private |
tick task memory
Definition at line 994 of file stk_time_timer.h.
Referenced by Initialize(), and TimerHost().
|
private |
id of the tick task (TID_NONE until it runs), commands must not be waited for from it
Definition at line 1008 of file stk_time_timer.h.
Referenced by CanWait(), TimerHost(), and UpdateTime().