SuperTinyKernel™ RTOS 1.06.0
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService Class Reference

Concrete implementation of IKernelService exposed to running tasks. More...

#include <stk.h>

Inheritance diagram for stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService:
Collaboration diagram for stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService:

Public Member Functions

TId GetTid () const
 Get thread Id of the currently running task.
Ticks GetTicks () const
 Get number of ticks elapsed since kernel start.
uint32_t GetTickResolution () const
 Get number of microseconds in one tick.
Cycles GetSysTimerCount () const
 Get system timer count value.
uint32_t GetSysTimerFrequency () const
 Get system timer frequency.
void Delay (Timeout ticks)
 Delay calling process.
void Sleep (Timeout ticks)
 Put calling process into a sleep state.
void SleepUntil (Ticks timestamp)
 Put calling process into a sleep state until the specified timestamp.
void SwitchToNext ()
 Notify scheduler to switch to the next task (yield).
IWaitObjectWait (ISyncObject *sobj, IMutex *mutex, Timeout ticks)
 Put calling process into a waiting state until synchronization object is signaled or timeout occurs.
Timeout Suspend ()
 Suspend scheduling.
void Resume (Timeout elapsed_ticks)
 Resume scheduling after a prior Suspend() call.

Static Public Member Functions

static IKernelServiceGetInstance ()
 Get CPU-local instance of the kernel service.

Private Member Functions

 KernelService ()
 Construct an uninitialized service instance (m_platform = null, m_ticks = 0).
 ~KernelService ()
 Destructor.
void Initialize (IPlatform *platform)
 Initialize instance.
void IncrementTicks (Ticks advance)
 Increment counter by value.

Private Attributes

TPlatform * m_platform
 Typed platform driver pointer, set at Initialize().
volatile Ticks m_ticks
 Global tick counter. Written via hw::WriteVolatile64() by IncrementTick() (ISR context); read via hw::ReadVolatile64() by GetTicks() (task context) for a lock-free consistent 64-bit read on 32-bit CPUs.

Friends

class Kernel

Detailed Description

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
class stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService

Concrete implementation of IKernelService exposed to running tasks.

Holds the global tick counter (m_ticks, updated atomically by IncrementTick() each SysTick) and a typed pointer to the platform driver. Tasks access this object via IKernelService::GetInstance() which returns the singleton registered at Initialize().

Definition at line 643 of file stk.h.

Constructor & Destructor Documentation

◆ KernelService()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::KernelService ( )
inlineexplicitprivate

Construct an uninitialized service instance (m_platform = null, m_ticks = 0).

Note
Fully initialized by Initialize(). Private; constructed only as a member of Kernel.

Definition at line 754 of file stk.h.

754 : m_platform(nullptr), m_ticks(0)
755 {}
volatile Ticks m_ticks
Global tick counter. Written via hw::WriteVolatile64() by IncrementTick() (ISR context); read via hw:...
Definition stk.h:783
TPlatform * m_platform
Typed platform driver pointer, set at Initialize().
Definition stk.h:782

References m_platform, and m_ticks.

◆ ~KernelService()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::~KernelService ( )
inlineprivate

Destructor.

Note
MISRA deviation: [STK-DEV-005] Rule 10-3-2.

Definition at line 760 of file stk.h.

761 {}

Member Function Documentation

◆ Delay()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::Delay ( Timeout ticks)
inlinevirtual

Delay calling process.

Note
Unlike Sleep this function delays code execution by spinning in a loop until deadline expiry.
Use with care in HRT mode to avoid missed deadline (see stk::KERNEL_HRT, ITask::OnDeadlineMissed).
Parameters
[in]ticksDelay time (ticks).
Warning
ISR-unsafe. Calling from an ISR context is not permitted and will trigger an assertion.
See also
Delay

Implements stk::IKernelService.

Definition at line 658 of file stk.h.

659 {
661 STK_ASSERT(ticks >= 0);
662
663 Ticks now = GetTicks();
664 const Ticks deadline = now + ticks;
666
667 for (; now < deadline; now = GetTicks())
668 {
670 }
671 }
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:330
Concrete implementation of IKernel.
Definition stk.h:81
Ticks GetTicks() const
Get number of ticks elapsed since kernel start.
Definition stk.h:650

References __stk_attr_noinline, GetTicks(), stk::hw::IsInsideISR(), and STK_ASSERT.

Here is the call graph for this function:

◆ GetInstance()

◆ GetSysTimerCount()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
Cycles stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::GetSysTimerCount ( ) const
inlinevirtual

Get system timer count value.

Note
ISR-safe.
Returns
64-bit count value.

Implements stk::IKernelService.

Definition at line 654 of file stk.h.

654{ return m_platform->GetSysTimerCount(); }

References m_platform.

◆ GetSysTimerFrequency()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
uint32_t stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::GetSysTimerFrequency ( ) const
inlinevirtual

Get system timer frequency.

Note
ISR-safe.
Returns
Frequency (Hz).

Implements stk::IKernelService.

Definition at line 656 of file stk.h.

656{ return m_platform->GetSysTimerFrequency(); }

References m_platform.

◆ GetTickResolution()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
uint32_t stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::GetTickResolution ( ) const
inlinevirtual

Get number of microseconds in one tick.

Note
Tick is a periodicity of the system timer expressed in microseconds.
ISR-safe.
Returns
Microseconds in one tick.

Implements stk::IKernelService.

Definition at line 652 of file stk.h.

652{ return m_platform->GetTickResolution(); }

References m_platform.

◆ GetTicks()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
Ticks stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::GetTicks ( ) const
inlinevirtual

Get number of ticks elapsed since kernel start.

Returns
Ticks.
Note
ISR-safe.

Implements stk::IKernelService.

Definition at line 650 of file stk.h.

650{ return hw::ReadVolatile64(&m_ticks); }
__stk_forceinline T ReadVolatile64(volatile const T *addr)
Atomically read a 64-bit volatile value.
Definition stk_arch.h:357

References m_ticks, and stk::hw::ReadVolatile64().

Referenced by Delay().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetTid()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
TId stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::GetTid ( ) const
inlinevirtual

Get thread Id of the currently running task.

Returns
Thread Id.
Warning
ISR-safe.
See also
TID_ISR_N, TID_NONE, IsIsrTid

Implements stk::IKernelService.

Definition at line 648 of file stk.h.

648{ return m_platform->GetTid(); }

References m_platform.

◆ IncrementTicks()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::IncrementTicks ( Ticks advance)
inlineprivate

Increment counter by value.

Parameters
[in]advanceNumber of ticks to add to the counter.

Definition at line 776 of file stk.h.

777 {
778 // using WriteVolatile64() to guarantee correct lockless reading order by ReadVolatile64
780 }
__stk_forceinline void WriteVolatile64(volatile T *addr, T value)
Atomically write a 64-bit volatile value.
Definition stk_arch.h:411

References m_ticks, and stk::hw::WriteVolatile64().

Here is the call graph for this function:

◆ Initialize()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::Initialize ( IPlatform * platform)
inlineprivate

Initialize instance.

Note
When call completes Singleton<IKernelService *> will start referencing this instance (see g_KernelService).
Parameters
[in]platformIPlatform instance.

Definition at line 768 of file stk.h.

769 {
770 m_platform = static_cast<TPlatform *>(platform);
771 }

References m_platform.

◆ Resume()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::Resume ( Timeout elapsed_ticks)
inlinevirtual

Resume scheduling after a prior Suspend() call.

Parameters
[in]elapsed_ticksNumber of ticks that elapsed during the suspended period. The kernel uses this value to advance internal time counters and wake tasks whose sleep deadlines have expired.
Note
ISR-safe.
See also
IKernel::EState::STATE_SUSPENDED

Implements stk::IKernelService.

Definition at line 738 of file stk.h.

739 {
740 if (IsTicklessMode())
741 {
742 return m_platform->Resume(elapsed_ticks);
743 }
744 else
745 {
746 STK_ASSERT(false);
747 }
748 }
static bool IsTicklessMode()
Definition stk.h:2035

References stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsTicklessMode(), m_platform, and STK_ASSERT.

Here is the call graph for this function:

◆ Sleep()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::Sleep ( Timeout ticks)
inlinevirtual

Put calling process into a sleep state.

Note
Unlike Delay this function does not waste CPU cycles and allows kernel to put CPU into a low-power state.
Unsupported in HRT mode (see stk::KERNEL_HRT); in HRT mode tasks sleep automatically according to their periodicity and workload.
Parameters
[in]ticksSleep time (ticks).
Warning
ISR-unsafe. Calling from an ISR context is not permitted and will trigger an assertion.

Implements stk::IKernelService.

Definition at line 673 of file stk.h.

674 {
676 STK_ASSERT(ticks >= 0);
677
678 if (!IsHrtMode())
679 {
680 m_platform->Sleep(ticks);
681 }
682 else
683 {
684 // sleeping is not supported in HRT mode, task will sleep according to its periodicity and workload
685 STK_ASSERT(false);
686 }
687 }
static bool IsHrtMode()
Definition stk.h:2033

References __stk_attr_noinline, stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsHrtMode(), stk::hw::IsInsideISR(), m_platform, and STK_ASSERT.

Here is the call graph for this function:

◆ SleepUntil()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::SleepUntil ( Ticks timestamp)
inlinevirtual

Put calling process into a sleep state until the specified timestamp.

Note
Unlike Delay this function does not waste CPU cycles and allows kernel to put CPU into a low-power state.
Unsupported in HRT mode (see stk::KERNEL_HRT); in HRT mode tasks sleep automatically according to their periodicity and workload.
Parameters
[in]timestampAbsolute timestamp (ticks).
Warning
ISR-unsafe. Calling from an ISR context is not permitted and will trigger an assertion.

Implements stk::IKernelService.

Definition at line 689 of file stk.h.

690 {
692 STK_ASSERT(timestamp >= 0);
693
694 if (!IsHrtMode())
695 {
696 m_platform->SleepUntil(timestamp);
697 }
698 else
699 {
700 // sleeping is not supported in HRT mode, task will sleep according to its periodicity and workload
701 STK_ASSERT(false);
702 }
703 }

References __stk_attr_noinline, stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsHrtMode(), stk::hw::IsInsideISR(), m_platform, and STK_ASSERT.

Here is the call graph for this function:

◆ Suspend()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
Timeout stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::Suspend ( )
inlinevirtual

Suspend scheduling.

Returns
Number of ticks available for the suspension period, as determined by the nearest pending wake-up. The caller may program a hardware timer with this value to avoid unnecessary wakeups (tickless idle).
Note
ISR-safe. Pair with Resume().
See also
IKernel::EState::STATE_SUSPENDED

Implements stk::IKernelService.

Definition at line 725 of file stk.h.

726 {
727 if (IsTicklessMode())
728 {
729 return m_platform->Suspend();
730 }
731 else
732 {
733 STK_ASSERT(false);
734 return 0;
735 }
736 }

References stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsTicklessMode(), m_platform, and STK_ASSERT.

Here is the call graph for this function:

◆ SwitchToNext()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
void stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::SwitchToNext ( )
inlinevirtual

Notify scheduler to switch to the next task (yield).

Note
A cooperation mechanism in HRT mode (see stk::KERNEL_HRT).
Warning
ISR-unsafe.

Implements stk::IKernelService.

Definition at line 705 of file stk.h.

706 {
708
709 m_platform->SwitchToNext();
710 }

References stk::hw::IsInsideISR(), m_platform, and STK_ASSERT.

Here is the call graph for this function:

◆ Wait()

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
IWaitObject * stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::Wait ( ISyncObject * sobj,
IMutex * mutex,
Timeout timeout )
inlinevirtual

Put calling process into a waiting state until synchronization object is signaled or timeout occurs.

Note
This function implements core blocking logic using the Monitor pattern to ensure atomicity between state check and suspension.
The kernel automatically unlocks the provided mutex before the task is suspended and re-locks it before this function returns.
Parameters
[in]sobjSynchronization object to wait on.
[in]mutexMutex protecting the state of the synchronization object.
[in]timeoutMaximum wait time (ticks). Use WAIT_INFINITE to block indefinitely, use NO_WAIT to poll without blocking.
Returns
Pointer to the wait object representing this wait operation (always non-NULL). The caller must check IWaitObject::IsTimeout() after this function returns to determine whether the wake was caused by a signal or by timeout expiry. The returned pointer is valid until the calling task re-enters a wait or the wait object is explicitly released by the kernel. The return value is guaranteed non nullptr and points to a valid IWaitObject.
Warning
ISR-unsafe.

Implements stk::IKernelService.

Definition at line 712 of file stk.h.

713 {
714 if (IsSyncMode())
715 {
716 return m_platform->Wait(sobj, mutex, ticks);
717 }
718 else
719 {
720 STK_ASSERT(false);
721 return nullptr;
722 }
723 }
static bool IsSyncMode()
Definition stk.h:2034

References stk::Kernel< TMode, TSize, TStrategy, TPlatform >::IsSyncMode(), m_platform, and STK_ASSERT.

Here is the call graph for this function:

◆ Kernel

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
friend class Kernel
friend

Definition at line 645 of file stk.h.

References Kernel.

Referenced by Kernel.

Member Data Documentation

◆ m_platform

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
TPlatform* stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::m_platform
private

◆ m_ticks

template<uint8_t TMode, uint32_t TSize, class TStrategy, class TPlatform>
volatile Ticks stk::Kernel< TMode, TSize, TStrategy, TPlatform >::KernelService::m_ticks
private

Global tick counter. Written via hw::WriteVolatile64() by IncrementTick() (ISR context); read via hw::ReadVolatile64() by GetTicks() (task context) for a lock-free consistent 64-bit read on 32-bit CPUs.

Definition at line 783 of file stk.h.

Referenced by GetTicks(), IncrementTicks(), and KernelService().


The documentation for this class was generated from the following file: