SuperTinyKernel™ RTOS 1.05.3
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk::test::spinlock::TryLockContendedTask< _AccessMode > Class Template Reference

Tests TryLock() returns false immediately when the lock is held by another task. More...

Inheritance diagram for stk::test::spinlock::TryLockContendedTask< _AccessMode >:
Collaboration diagram for stk::test::spinlock::TryLockContendedTask< _AccessMode >:

Public Types

enum  

Public Member Functions

 TryLockContendedTask (uint8_t task_id, int32_t)
WordGetStack () const
 Get pointer to the stack memory.
size_t GetStackSize () const
 Get number of elements of the stack memory array.
size_t GetStackSizeBytes () const
 Get size of the memory in bytes.
EAccessMode GetAccessMode () const
 Get hardware access mode of the user task.
virtual void OnDeadlineMissed (uint32_t duration)
 Default no-op handler. Override in subclass to log or handle missed deadlines.
virtual int32_t GetWeight () const
 Default weight of 1. Override in subclass if custom scheduling weight is needed.
virtual TId GetId () const
 Get object's own address as its Id. Unique per task instance, requires no manual assignment.
virtual const char * GetTraceName () const
 Override in subclass to supply a name for SEGGER SystemView tracing. Returns NULL by default.

Private Member Functions

void Run ()
 Entry point of the user task.

Private Attributes

uint8_t m_task_id
StackMemoryDef< _StackSize >::Type m_stack
 Stack memory region, 16-byte aligned.

Detailed Description

template<EAccessMode _AccessMode>
class stk::test::spinlock::TryLockContendedTask< _AccessMode >

Tests TryLock() returns false immediately when the lock is held by another task.

Note
Task 0 holds the lock for an extended period; task 1 calls TryLock() and verifies it returns false quickly without blocking.

Definition at line 186 of file test_spinlock.cpp.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
inherited

Definition at line 52 of file stk_helper.h.

Partial implementation of the user task.
Definition stk_helper.h:50

Constructor & Destructor Documentation

◆ TryLockContendedTask()

template<EAccessMode _AccessMode>
stk::test::spinlock::TryLockContendedTask< _AccessMode >::TryLockContendedTask ( uint8_t task_id,
int32_t  )
inline

Definition at line 191 of file test_spinlock.cpp.

192 {}
Tests TryLock() returns false immediately when the lock is held by another task.

References m_task_id.

Member Function Documentation

◆ GetAccessMode()

EAccessMode stk::Task< _StackSize, _AccessMode >::GetAccessMode ( ) const
inlinevirtualinherited

Get hardware access mode of the user task.

Implements stk::ITask.

Definition at line 57 of file stk_helper.h.

57{ return _AccessMode; }

◆ GetId()

virtual TId stk::Task< _StackSize, _AccessMode >::GetId ( ) const
inlinevirtualinherited

Get object's own address as its Id. Unique per task instance, requires no manual assignment.

Implements stk::ITask.

Definition at line 72 of file stk_helper.h.

72{ return hw::PtrToWord(this); }
__stk_forceinline Word PtrToWord(T *ptr) noexcept
Cast a pointer to a CPU register-width integer.
Definition stk_arch.h:94

◆ GetStack()

Word * stk::Task< _StackSize, _AccessMode >::GetStack ( ) const
inlinevirtualinherited

Get pointer to the stack memory.

Implements stk::IStackMemory.

Definition at line 54 of file stk_helper.h.

54{ return const_cast<Word *>(m_stack); }

◆ GetStackSize()

size_t stk::Task< _StackSize, _AccessMode >::GetStackSize ( ) const
inlinevirtualinherited

Get number of elements of the stack memory array.

Implements stk::IStackMemory.

Definition at line 55 of file stk_helper.h.

55{ return _StackSize; }

◆ GetStackSizeBytes()

size_t stk::Task< _StackSize, _AccessMode >::GetStackSizeBytes ( ) const
inlinevirtualinherited

Get size of the memory in bytes.

Implements stk::IStackMemory.

Definition at line 56 of file stk_helper.h.

56{ return _StackSize * sizeof(Word); }

◆ GetTraceName()

virtual const char * stk::Task< _StackSize, _AccessMode >::GetTraceName ( ) const
inlinevirtualinherited

Override in subclass to supply a name for SEGGER SystemView tracing. Returns NULL by default.

Implements stk::ITask.

Definition at line 76 of file stk_helper.h.

76{ return nullptr; }

◆ GetWeight()

virtual int32_t stk::Task< _StackSize, _AccessMode >::GetWeight ( ) const
inlinevirtualinherited

Default weight of 1. Override in subclass if custom scheduling weight is needed.

Note
Only relevant when using SwitchStrategySmoothWeightedRoundRobin. Prefer TaskW for compile-time weight assignment.

Implements stk::ITask.

Definition at line 68 of file stk_helper.h.

68{ return 1; }

◆ OnDeadlineMissed()

virtual void stk::Task< _StackSize, _AccessMode >::OnDeadlineMissed ( uint32_t duration)
inlinevirtualinherited

Default no-op handler. Override in subclass to log or handle missed deadlines.

Note
HRT deadline misses are only possible when the kernel is started with KERNEL_HRT.

Implements stk::ITask.

Definition at line 62 of file stk_helper.h.

62{ (void)duration; }

◆ Run()

template<EAccessMode _AccessMode>
void stk::test::spinlock::TryLockContendedTask< _AccessMode >::Run ( )
inlineprivatevirtual

Entry point of the user task.

Note
Called by the Kernel when the task is scheduled for execution. Implement this method with the task's main logic.
Warning
If Kernel is configured as KERNEL_STATIC, the body must contain an infinite loop.
void Run()
{
while (true)
{
// task logic here
}
}
void Run()
Entry point of the user task.

Implements stk::ITask.

Definition at line 195 of file test_spinlock.cpp.

196 {
197 if (m_task_id == 0)
198 {
199 // Acquire and hold; signal task 1 that the lock is held
200 g_TestSpinLock.Lock();
201 g_SharedCounter = 1;
203 g_TestSpinLock.Unlock();
204 }
205 else
206 if (m_task_id == 1)
207 {
208 // Wait until task 0 signals it has acquired the lock
209 while (g_SharedCounter == 0)
210 stk::Yield();
211
213 bool acquired = g_TestSpinLock.TryLock();
215
216 // Must fail immediately: task 0 holds the lock
218 ++g_SharedCounter; // 2: correctly returned false immediately
219
220 if (acquired)
221 g_TestSpinLock.Unlock();
222 }
223 else
224 if (m_task_id == 2)
225 {
227
228 printf("try-lock contended: counter=%d (expected 2)\n", (int)g_SharedCounter);
229
230 if (g_SharedCounter == 2)
231 g_TestResult = 1;
232 }
233
235 }
static int64_t GetTimeNowMs()
Get current time in milliseconds since kernel start.
Definition stk_helper.h:281
void Sleep(uint32_t ticks)
Put calling process into a sleep state.
Definition stk_helper.h:298
void Yield()
Notify scheduler to switch to the next runnable task.
Definition stk_helper.h:331

References _STK_SL_TEST_LONG_SLEEP, _STK_SL_TEST_SHORT_SLEEP, stk::test::spinlock::g_InstancesDone, stk::test::spinlock::g_SharedCounter, stk::test::spinlock::g_TestResult, stk::test::spinlock::g_TestSpinLock, stk::GetTimeNowMs(), m_task_id, stk::Sleep(), and stk::Yield().

Here is the call graph for this function:

Member Data Documentation

◆ m_stack

StackMemoryDef<_StackSize>::Type stk::Task< _StackSize, _AccessMode >::m_stack
privateinherited

Stack memory region, 16-byte aligned.

Definition at line 98 of file stk_helper.h.

◆ m_task_id

template<EAccessMode _AccessMode>
uint8_t stk::test::spinlock::TryLockContendedTask< _AccessMode >::m_task_id
private

Definition at line 188 of file test_spinlock.cpp.

Referenced by Run(), and TryLockContendedTask().


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