5#include <MPMCQueue.hpp>
17#include "kstd_cstring"
30std::atomic<uint64_t> global_counter{0};
33auto task1_func(
void* arg) ->
void {
35 static_cast<uint64_t
>(
reinterpret_cast<uintptr_t
>(arg)));
36 for (
int i = 0; i < 5; ++i) {
45auto task2_func(
void* arg) ->
void {
47 static_cast<uint64_t
>(
reinterpret_cast<uintptr_t
>(arg)));
58auto task3_func(
void* arg) ->
void {
60 static_cast<uint64_t
>(
reinterpret_cast<uintptr_t
>(arg)));
62 uint64_t old_value = global_counter.fetch_add(1, std::memory_order_relaxed);
63 klog::Info(
"Task3: global_counter {} -> {}", old_value, old_value + 1);
69auto task4_func(
void* arg) ->
void {
71 static_cast<uint64_t
>(
reinterpret_cast<uintptr_t
>(arg)));
72 uint64_t iteration = 0;
75 auto start_tick = cpu_sched->local_tick;
76 klog::Info(
"Task4: sleeping for 4s (iteration {})", iteration++);
78 auto end_tick = cpu_sched->local_tick;
79 klog::Info(
"Task4: woke up (slept ~{} ticks)", end_tick - start_tick);
84auto create_test_tasks() ->
void {
86 auto& tm = TaskManagerSingleton::instance();
88 auto task1 = kstd::make_unique<TaskControlBlock>(
89 "Task1-Exit", 10, task1_func,
reinterpret_cast<void*
>(0x1111));
90 auto task2 = kstd::make_unique<TaskControlBlock>(
91 "Task2-Yield", 10, task2_func,
reinterpret_cast<void*
>(0x2222));
92 auto task3 = kstd::make_unique<TaskControlBlock>(
93 "Task3-Sync", 10, task3_func,
reinterpret_cast<void*
>(0x3333));
94 auto task4 = kstd::make_unique<TaskControlBlock>(
95 "Task4-Sleep", 10, task4_func,
reinterpret_cast<void*
>(0x4444));
98 task1->aux->cpu_affinity = (1UL <<
core_id);
99 task2->aux->cpu_affinity = (1UL <<
core_id);
100 task3->aux->cpu_affinity = (1UL <<
core_id);
101 task4->aux->cpu_affinity = (1UL <<
core_id);
103 tm.AddTask(std::move(task1));
104 tm.AddTask(std::move(task2));
105 tm.AddTask(std::move(task3));
106 tm.AddTask(std::move(task4));
112auto main_smp(
int argc,
const char** argv) ->
int {
117 TaskManagerSingleton::instance().InitCurrentCore();
126 TaskManagerSingleton::instance().Schedule();
129 __builtin_unreachable();
134auto _start(
int argc,
const char** argv) ->
void {
135 if (argv !=
nullptr) {
139 main_smp(argc, argv);
147auto main(
int argc,
const char** argv) ->
int {
149 per_cpu::PerCpuArraySingleton::create();
163 TaskManagerSingleton::create();
164 TaskManagerSingleton::instance().InitCurrentCore();
183 TaskManagerSingleton::instance().Schedule();
186 __builtin_unreachable();
auto WakeUpOtherCores() -> void
唤醒其余 core
auto ArchInit(int argc, const char **argv) -> void
体系结构相关初始化
auto ArchInitSMP(int argc, const char **argv) -> void
从核的体系结构相关初始化
auto DumpStack() -> void
打印调用栈
auto InterruptInit(int, const char **) -> void
体系结构相关中断初始化
auto InterruptInitSMP(int, const char **) -> void
从核的体系结构相关中断初始化
auto TimerInitSMP() -> void
从核的定时器初始化
auto TimerInit() -> void
初始化定时器
auto DeviceInit() -> void
设备子系统初始化入口
auto FileSystemInit() -> void
文件系统子系统初始化入口
auto MemoryInit() -> void
内存子系统初始化
auto MemoryInitSMP() -> void
多核内存子系统初始化
auto CppInit() -> void
构造 c++ 全局对象
auto GetCurrentCoreId() -> size_t
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
static __always_inline auto GetCurrentCore() -> PerCpu &
获取当前核心的 PerCpu 数据
auto _start(int argc, const char **argv) -> void
负责 crtbegin 的工作
auto sys_sleep(uint64_t ms) -> int
休眠指定毫秒数
auto sys_exit(int code) -> int
退出当前进程或线程
auto sys_yield() -> int
主动放弃CPU,让出时间片