35 for (
size_t i = 0; i < num_cores; ++i) {
42 for (
auto& core :
cores_) {
43 core.interrupt_enabled =
true;
44 core.page_directory = 0;
45 core.paging_enabled =
false;
46 core.switch_history.clear();
54 std::lock_guard<std::mutex> lock(map_mutex_);
56 throw std::out_of_range(
"Invalid core_id: " + std::to_string(
core_id));
70 throw std::out_of_range(
"Invalid core_id: " + std::to_string(
core_id));
77 std::lock_guard<std::mutex> lock(map_mutex_);
78 auto it = thread_to_core_map_.find(tid);
79 if (it != thread_to_core_map_.end()) {
84 if (next_core_id_ >= cores_.size()) {
87 cores_.emplace_back(0);
92 size_t new_id = next_core_id_;
93 thread_to_core_map_[tid] = new_id;
94 cores_[new_id].thread_id = tid;
95 next_core_id_ = (next_core_id_ + 1) % cores_.size();
100 auto tid = std::this_thread::get_id();
101 size_t core_id = GetCoreIdForThread(tid);
102 std::lock_guard<std::mutex> lock(map_mutex_);
119 std::lock_guard<std::mutex> lock(map_mutex_);
120 auto it = context_to_task_map_.find(context_ptr);
121 if (it != context_to_task_map_.end()) {
129 std::cout <<
"\n=== Test Environment State Dump ===" << std::endl;
130 std::cout <<
"Total cores: " <<
cores_.size() << std::endl;
132 for (
const auto& core :
cores_) {
133 std::cout <<
"\nCore " << core.core_id <<
":" << std::endl;
134 std::cout <<
" Interrupt enabled: " << core.interrupt_enabled << std::endl;
135 std::cout <<
" Page directory: 0x" << std::hex << core.page_directory
136 << std::dec << std::endl;
137 std::cout <<
" Paging enabled: " << core.paging_enabled << std::endl;
138 std::cout <<
" Switch history size: " << core.switch_history.size()
141 std::cout <<
"==================================\n" << std::endl;
147 std::vector<CoreEnvironment::SwitchEvent> all_events;
149 for (
const auto& core :
cores_) {
150 all_events.insert(all_events.end(), core.switch_history.begin(),
151 core.switch_history.end());
156 all_events.begin(), all_events.end(),
157 [](
const auto& a,
const auto& b) { return a.timestamp < b.timestamp; });
164 for (
auto& core :
cores_) {
165 core.switch_history.clear();
void ClearCurrentThreadEnvironment()
清除当前线程的环境实例指针
void UnregisterTaskContext(void *context_ptr)
注销任务上下文
auto FindTaskByContext(void *context_ptr) -> TaskControlBlock *
通过上下文指针查找任务
auto GetCore(size_t core_id) -> CoreEnvironment &
获取指定核心的环境
std::unordered_map< std::thread::id, size_t > thread_to_core_map_
void BindThreadToCore(std::thread::id tid, size_t core_id)
将指定线程绑定到核心
void InitializeCores(size_t num_cores)
初始化指定数量的核心
auto GetAllSwitchHistory() const -> std::vector< CoreEnvironment::SwitchEvent >
获取所有核心的切换历史,按时间戳排序
static auto GetCurrentThreadEnvironment() -> TestEnvironmentState *
获取当前线程的环境实例指针(供 Mock 层调用)
void ClearSwitchHistory()
清空所有核心的切换历史
void DumpAllCoreStates() const
打印所有核心的状态信息
void ResetAllCores()
重置所有核心状态(中断、页表、历史记录)
std::unordered_map< void *, TaskControlBlock * > context_to_task_map_
auto GetCurrentCoreEnv() -> CoreEnvironment &
获取当前线程的核心环境
auto GetCoreIdForThread(std::thread::id tid) -> size_t
获取线程对应的核心 ID
auto GetCoreCount() const -> size_t
获取核心数量
void SetCurrentThreadEnvironment()
设置当前线程的环境实例指针
void RegisterTaskContext(void *context_ptr, TaskControlBlock *task)
注册任务上下文,建立上下文指针到任务的映射
std::vector< CoreEnvironment > cores_
thread_local TestEnvironmentState * current_thread_env