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_chain.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 <stk_config.h>
11#include <stk.h>
12#include <assert.h>
13
14#include "stktest_context.h"
15
16using namespace stk;
17using namespace stk::test;
18
20
21#define _STK_CHAIN_TEST_TASKS_MAX 3
22#define _STK_CHAIN_TEST_DELAY_TIME 100
23
24namespace stk {
25namespace test {
26
30namespace chain {
31
32static volatile uint8_t g_TaskSwitch = 0;
33
38template <EAccessMode _AccessMode>
39class TestTask : public Task<256, _AccessMode>
40{
41 uint8_t m_task_id;
42
43public:
44 TestTask(uint8_t task_id) : m_task_id(task_id)
45 {}
46
47private:
48 void Run();
49};
50
53
56
58static int64_t g_Time[_STK_CHAIN_TEST_TASKS_MAX] = {};
59
61{
62 uint8_t task_id = m_task_id;
63
64 g_Time[task_id] = stk::GetTimeNowMs();
65
66 printf("id=%d time=%d\n", task_id, (int)g_Time[task_id]);
67
69
70 // activate next task and exit
71 g_TaskSwitch = (task_id + 1) % 3;
72 if (g_TaskSwitch == 1)
73 kernel.AddTask(&task2);
74 else
75 if (g_TaskSwitch == 2)
76 kernel.AddTask(&task3);
77}
78
79} // namespace chain
80} // namespace test
81} // namespace stk
82
86int main(int argc, char **argv)
87{
88 (void)argc;
89 (void)argv;
90
92
93 using namespace stk;
94 using namespace stk::test;
95 using namespace stk::test::chain;
96
97 kernel.Initialize();
98 kernel.AddTask(&task1);
99 kernel.Start();
100
101 int32_t result = TestContext::SUCCESS_EXIT_CODE;
102 for (int32_t i = 0; i < _STK_CHAIN_TEST_TASKS_MAX; ++i)
103 {
104 int32_t diff = g_Time[i] - (i * _STK_CHAIN_TEST_DELAY_TIME);
105
106 if (diff < 0)
107 diff = -diff;
108
109 // check if time difference for every task is not more than 15 ms
110 if (diff > 25)
111 {
112 printf("failed time: id=%d diff=%d (>25)\n", (int)i, (int)diff);
114 }
115 }
116
118 return result;
119}
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
int main(int argc, char **argv)
#define _STK_CHAIN_TEST_DELAY_TIME
#define _STK_CHAIN_TEST_TASKS_MAX
#define STK_TEST_DECL_ASSERT
Declare assertion redirector in the source file.
Namespace of STK package.
static int64_t GetTimeNowMs()
Get current time in milliseconds since kernel start.
Definition stk_helper.h:281
void Delay(uint32_t ticks)
Delay calling process by busy-waiting until the deadline expires.
Definition stk_helper.h:342
Namespace of the test inventory.
Namespace of Chain test.
static Kernel< KERNEL_DYNAMIC, 3, SwitchStrategyRoundRobin, PlatformDefault > kernel
Kernel.
static TestTask< ACCESS_PRIVILEGED > task3(2)
static TestTask< ACCESS_PRIVILEGED > task1(0)
Tasks (threads).
static TestTask< ACCESS_PRIVILEGED > task2(1)
static int64_t g_Time[3]
Execution time of the task.
static volatile uint8_t g_TaskSwitch
Concrete implementation of IKernel.
Definition stk.h:83
Task(const Task &)=delete
void Run()
Entry point of the user task.
static void ShowTestSuitePrologue()
Show text string as prologue before tests start.
@ DEFAULT_FAILURE_EXIT_CODE
default exit code for exit() to denote failure of the test
@ SUCCESS_EXIT_CODE
exit code for exit() to denote the success of the test
static void ShowTestSuiteEpilogue(int32_t result)
Show text string as epilogue after tests end.