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_sleep.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_SLEEP_TEST_TASKS_MAX 3
22#define _STK_SLEEP_TEST_SLEEP_TIME 100
23
24namespace stk {
25namespace test {
26
30namespace sleep {
31
32static int32_t g_Time[_STK_SLEEP_TEST_TASKS_MAX] = {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 // task 0: sleep 100 ms
51 // task 1: sleep 200 ms
52 // task 2: sleep 300 ms
53
54 int64_t start = GetTimeNowMs();
55
57
58 int64_t diff = GetTimeNowMs() - start;
59
60 printf("id=%d time=%d\n", m_task_id, (int)diff);
61
62 g_Time[m_task_id] = diff;
63 }
64};
65
68
71
72} // namespace sleep
73} // namespace test
74} // namespace stk
75
79int main(int argc, char **argv)
80{
81 (void)argc;
82 (void)argv;
83
85
86 using namespace stk;
87 using namespace stk::test;
88 using namespace stk::test::sleep;
89
90 kernel.Initialize();
91
92 kernel.AddTask(&task1);
93 kernel.AddTask(&task2);
94 kernel.AddTask(&task3);
95
96 kernel.Start();
97
98 int32_t result = TestContext::SUCCESS_EXIT_CODE;
99 for (int32_t i = 0; i < _STK_SLEEP_TEST_TASKS_MAX; ++i)
100 {
101 int32_t diff = g_Time[i] - ((i + 1) * _STK_SLEEP_TEST_SLEEP_TIME);
102
103 if (diff < 0)
104 diff = -diff;
105
106 // check if time difference for every task is not more than 15 ms
107 if (diff > 20)
108 {
109 printf("failed time: id=%d diff=%d (>20)\n", (int)i, (int)diff);
111 }
112 }
113
115 return result;
116}
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
int main(int argc, char **argv)
#define _STK_SLEEP_TEST_TASKS_MAX
#define _STK_SLEEP_TEST_SLEEP_TIME
#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 Sleep(uint32_t ticks)
Put calling process into a sleep state.
Definition stk_helper.h:298
Namespace of the test inventory.
Namespace of Sleep test.
static Kernel< KERNEL_DYNAMIC, 3, SwitchStrategyRoundRobin, PlatformDefault > kernel
Kernel.
static TestTask< ACCESS_PRIVILEGED > task2(1)
static TestTask< ACCESS_PRIVILEGED > task3(2)
static int32_t g_Time[3]
static TestTask< ACCESS_PRIVILEGED > task1(0)
Tasks (threads).
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.