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
stktest_sync.cpp
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
10#include "stktest.h"
11
12namespace stk {
13namespace test {
14
15// ============================================================================ //
16// ================================ Sync ====================================== //
17// ============================================================================ //
18
20{
21 void setup() {}
22 void teardown() {}
23};
24
25TEST(Sync, CriticalSection)
26{
27 CHECK_EQUAL(0, test::g_CriticalSectionState);
28 {
30 CHECK_EQUAL(1, test::g_CriticalSectionState);
31 }
32 CHECK_EQUAL(0, test::g_CriticalSectionState);
33}
34
35TEST(Sync, IMutex_ScopedLock)
36{
38 {
40 CHECK_TRUE(mutex.m_locked);
41 }
42 CHECK_FALSE(mutex.m_locked);
43}
44
46{
48 {
49 wake_all = false;
50 counter = 0;
51 platform = NULL;
52 sobj = NULL;
53 }
54
56 uint32_t counter;
59
60 void Process()
61 {
62 platform->ProcessTick();
63 ++counter;
64
65 if (!wake_all && (counter >= 5))
66 sobj->WakeOne();
67 else
68 if (wake_all && (counter >= 7))
69 sobj->WakeAll();
70 }
71}
73
75{
77}
78
79TEST(Sync, SyncWait_Wake)
80{
82 PlatformTestMock *platform = static_cast<PlatformTestMock *>(kernel.GetPlatform());
84
86 SyncObjectMock sobj;
87
88 g_SyncWaitWakeRelaxCpuContext.platform = platform;
91
92 kernel.Initialize();
93 kernel.AddTask(&task);
94 kernel.Start();
95
97
98 // short wait in order to get wait object for a Wake testing
100 CHECK_TRUE(wo != nullptr); // expect wait object in return after timeout
101
102 // woken by WakeOne() on 5th tick, became active on 6th (see SyncWaitWakeRelaxCpuContext::Process)
103 CHECK_EQUAL(6, g_SyncWaitWakeRelaxCpuContext.counter);
104 CHECK_FALSE(wo->IsTimeout()); // expect no timeout
105 CHECK_EQUAL(true, mutex.m_locked); // expect locked mutex after Wait return
106
108 g_SyncWaitWakeRelaxCpuContext.wake_all = true;
109
110 // repeat for WakeAll()
111 wo = IKernelService::GetInstance()->Wait(&sobj, &mutex, 10);
112 CHECK_TRUE(wo != nullptr); // expect wait object in return after timeout
113
114 // woken by WakeAll() on 7th tick, became active on 8th (see SyncWaitWakeRelaxCpuContext::Process)
115 CHECK_EQUAL(8, g_SyncWaitWakeRelaxCpuContext.counter);
116 CHECK_FALSE(wo->IsTimeout()); // expect no timeout
117 CHECK_EQUAL(true, mutex.m_locked); // expect locked mutex after Wait return
118}
119
120} // namespace stk
121} // namespace test
void(* g_RelaxCpuHandler)()
__stk_relax_cpu handler.
Definition stktest.cpp:17
Namespace of STK package.
Namespace of the test inventory.
int32_t g_CriticalSectionState
Critical section state.
Definition stktest.cpp:19
static struct stk::test::SyncWaitWakeRelaxCpuContext g_SyncWaitWakeRelaxCpuContext
TEST_GROUP(Kernel)
static void SyncWaitWakeRelaxCpu()
TEST(Kernel, MaxTasks)
Namespace of Mutex test.
Concrete implementation of IKernel.
Definition stk.h:83
void Initialize(uint32_t resolution_us=PERIODICITY_DEFAULT)
Prepare kernel for use: reset state, configure the platform, and register the service singleton.
Definition stk.h:805
void Start()
Start the scheduler. This call does not return until all tasks have exited (KERNEL_DYNAMIC mode) or i...
Definition stk.h:921
IPlatform * GetPlatform()
Get platform driver instance owned by this kernel.
Definition stk.h:954
void AddTask(ITask *user_task)
Register task for a soft real-time (SRT) scheduling.
Definition stk.h:832
Wait object.
Definition stk_common.h:212
virtual bool IsTimeout() const =0
Check if task woke up due to a timeout.
Locks bound mutex within a scope of execution. Ensures the mutex is always unlocked when leaving the ...
Definition stk_common.h:389
static IKernelService * GetInstance()
Get CPU-local instance of the kernel service.
Definition stktest.cpp:69
virtual IWaitObject * Wait(ISyncObject *sobj, IMutex *mutex, Timeout timeout)=0
Put calling process into a waiting state until synchronization object is signaled or timeout occurs.
RAII-style low-level synchronization primitive for atomic code execution. Used as building brick for ...
Definition stk_sync_cs.h:54
IPlatform mock.
Definition stktest.h:75