SimpleKernel 1.17.0
Loading...
Searching...
No Matches
kernel_task_test.cpp
Go to the documentation of this file.
1
5#include <cpu_io.h>
6
7#include <atomic>
8#include <cstddef>
9#include <cstdint>
10
11#include "arch.h"
12#include "basic_info.hpp"
13#include "kernel.h"
14#include "kstd_cstring"
15#include "kstd_libcxx.h"
16#include "kstd_memory"
17#include "sk_stdlib.h"
18#include "syscall.hpp"
19#include "system_test.h"
21#include "task_manager.hpp"
22
23namespace {
24
25std::atomic<int> g_task_a_counter{0};
26std::atomic<int> g_task_b_counter{0};
27
28void thread_func_a(void* arg) {
29 uint64_t id = (uint64_t)arg;
30 for (int i = 0; i < 5; ++i) {
31 klog::Info("Thread A: running, arg={}, iter={}", id, i);
32 g_task_a_counter++;
33 (void)sys_sleep(50);
34 }
35 klog::Info("Thread A: exit");
36 sys_exit(0);
37}
38
39void thread_func_b(void* arg) {
40 uint64_t id = (uint64_t)arg;
41 for (int i = 0; i < 5; ++i) {
42 klog::Info("Thread B: running, arg={}, iter={}", id, i);
43 g_task_b_counter++;
44 (void)sys_sleep(50);
45 }
46 klog::Info("Thread B: exit");
47 sys_exit(0);
48}
49} // namespace
50
51auto kernel_task_test() -> bool {
52 klog::Info("kernel_task_test: start");
53 g_task_a_counter = 0;
54 g_task_b_counter = 0;
55
56 // 创建线程 A
57 auto task_a = kstd::make_unique<TaskControlBlock>("Task A", 10, thread_func_a,
58 (void*)100);
59 TaskManagerSingleton::instance().AddTask(std::move(task_a));
60
61 // 创建线程 B
62 auto task_b = kstd::make_unique<TaskControlBlock>("Task B", 10, thread_func_b,
63 (void*)200);
64 TaskManagerSingleton::instance().AddTask(std::move(task_b));
65
66 klog::Info("Main: Waiting for tasks...");
67
68 // Wait for tasks to finish (or reach expected count)
69 int timeout = 200; // 200 * 50ms = 10s roughly
70 while (timeout > 0) {
71 (void)sys_sleep(50);
72 if (g_task_a_counter >= 5 && g_task_b_counter >= 5) {
73 break;
74 }
75 timeout--;
76 }
77
78 EXPECT_GT(timeout, 0, "Tasks should complete before timeout");
79 EXPECT_EQ(g_task_a_counter, 5, "Task A count");
80 EXPECT_EQ(g_task_b_counter, 5, "Task B count");
81
82 klog::Info("kernel_task_test: PASS");
83 return true;
84}
auto kernel_task_test() -> bool
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
auto sys_sleep(uint64_t ms) -> int
休眠指定毫秒数
Definition syscall.cpp:90
auto sys_exit(int code) -> int
退出当前进程或线程
Definition syscall.cpp:75
#define EXPECT_GT(val1, val2, msg)
#define EXPECT_EQ(val1, val2, msg)