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_switch.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_SWITCH_TEST_TASKS_MAX 3
22#define _STK_SWITCH_TEST_CYCLES_MAX 3
23
24namespace stk {
25namespace test {
26
30namespace switch_ {
31
32static volatile uint8_t g_TaskSwitch = 0;
33static volatile uint8_t g_Cycles[_STK_SWITCH_TEST_TASKS_MAX] = {};
34
39template <EAccessMode _AccessMode>
40class TestTask : public Task<256, _AccessMode>
41{
42 uint8_t m_task_id;
43
44public:
45 TestTask(uint8_t task_id) : m_task_id(task_id)
46 {}
47
48private:
49 void Run()
50 {
51 uint8_t task_id = m_task_id;
52 volatile float count_skip = 0;
53
54 while (true)
55 {
56 if (g_TaskSwitch != task_id)
57 {
58 ++count_skip;
59 continue;
60 }
61
62 ++g_Cycles[task_id];
63 printf("id=%d c=%d\n", task_id, g_Cycles[task_id]);
64
65 // count total workload of all tasks
66 uint32_t total = 0;
67 for (int32_t i = 0; i < _STK_SWITCH_TEST_TASKS_MAX; ++i)
68 {
69 total += g_Cycles[i];
70 }
71
72 // check if it is a time to evaluate workload counters
74 {
76
77 // check if workload is spread equally between all tasks
78 for (int32_t i = 0; i < _STK_SWITCH_TEST_TASKS_MAX; ++i)
79 {
81 }
82
83 // success, exit process
85 break;
86 }
87
88 stk::Delay(100);
89
90 g_TaskSwitch = (task_id + 1) % 3;
91 }
92 }
93};
94
97
100
101} // namespace switch_
102} // namespace test
103} // namespace stk
104
108int main(int argc, char **argv)
109{
110 (void)argc;
111 (void)argv;
112
114
115 using namespace stk;
116 using namespace stk::test;
117 using namespace stk::test::switch_;
118
119 kernel.Initialize();
120
121 kernel.AddTask(&task1);
122 kernel.AddTask(&task2);
123 kernel.AddTask(&task3);
124
125 kernel.Start();
126
128}
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
#define STK_TEST_DECL_ASSERT
Declare assertion redirector in the source file.
#define STK_TEST_CHECK_EQUAL(expected, actual)
Compare values for equality.
#define _STK_SWITCH_TEST_TASKS_MAX
int main(int argc, char **argv)
#define _STK_SWITCH_TEST_CYCLES_MAX
Namespace of STK package.
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 Switch test.
static TestTask< ACCESS_PRIVILEGED > task1(0)
Tasks (threads).
static TestTask< ACCESS_PRIVILEGED > task3(2)
static Kernel< KERNEL_STATIC, 3, SwitchStrategyRoundRobin, PlatformDefault > kernel
Kernel.
static TestTask< ACCESS_PRIVILEGED > task2(1)
static volatile uint8_t g_TaskSwitch
static volatile uint8_t g_Cycles[3]
Concrete implementation of IKernel.
Definition stk.h:83
Task(const Task &)=delete
static void ShowTestSuitePrologue()
Show text string as prologue before tests start.
static void ForceExitTestSuite(int32_t result)
Exit test suite process forcibly.
@ 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
void Run()
Entry point of the user task.