* @brief 测试线程组的基本功能 */
| 50 | * @brief 测试线程组的基本功能 |
| 51 | */ |
| 52 | void test_thread_group_basic(void* /*arg*/) { |
| 53 | klog::Info("=== Thread Group Basic Test ==="); |
| 54 | |
| 55 | g_thread_counter = 0; |
| 56 | g_thread_completed = 0; |
| 57 | |
| 58 | auto leader_holder = kstd::make_unique<TaskControlBlock>( |
| 59 | "ThreadGroupLeader", 10, nullptr, nullptr); |
| 60 | auto* leader = leader_holder.get(); |
| 61 | leader->pid = g_local_pid_counter.fetch_add(1); |
| 62 | leader->aux->tgid = leader->pid; |
| 63 | |
| 64 | // 创建并加入线程组的线程 |
| 65 | auto thread1 = kstd::make_unique<TaskControlBlock>( |
| 66 | "Thread1", 10, thread_increment, reinterpret_cast<void*>(1)); |
| 67 | auto* thread1_raw = thread1.get(); |
| 68 | thread1->JoinThreadGroup(leader); |
| 69 | |
| 70 | auto thread2 = kstd::make_unique<TaskControlBlock>( |
| 71 | "Thread2", 10, thread_increment, reinterpret_cast<void*>(2)); |
| 72 | auto* thread2_raw = thread2.get(); |
| 73 | thread2->JoinThreadGroup(leader); |
| 74 | |
| 75 | auto thread3 = kstd::make_unique<TaskControlBlock>( |
| 76 | "Thread3", 10, thread_increment, reinterpret_cast<void*>(3)); |
| 77 | auto* thread3_raw = thread3.get(); |
| 78 | thread3->JoinThreadGroup(leader); |
| 79 | |
| 80 | // 验证线程组大小 |
| 81 | size_t group_size = leader->GetThreadGroupSize(); |
| 82 | klog::Info("Thread group size: {} (expected 4)", group_size); |
| 83 | |
| 84 | // 验证所有线程在同一线程组 |
| 85 | if (leader->InSameThreadGroup(thread1.get()) && |
| 86 | leader->InSameThreadGroup(thread2.get()) && |
| 87 | leader->InSameThreadGroup(thread3.get())) { |
| 88 | klog::Info("All threads are in the same thread group: PASS"); |
| 89 | } else { |
| 90 | klog::Err("Thread group membership check failed: FAIL"); |
| 91 | } |
| 92 | |
| 93 | // 添加到调度器 |
| 94 | auto& task_mgr = TaskManagerSingleton::instance(); |
| 95 | task_mgr.AddTask(std::move(thread1)); |
| 96 | task_mgr.AddTask(std::move(thread2)); |
| 97 | task_mgr.AddTask(std::move(thread3)); |
| 98 | |
| 99 | // 等待线程完成 |
| 100 | for (int i = 0; i < 200 && g_thread_completed < 3; ++i) { |
| 101 | (void)sys_sleep(50); |
| 102 | } |
| 103 | |
| 104 | klog::Info("Thread completed count: {} (expected 3)", |
| 105 | g_thread_completed.load()); |
| 106 | klog::Info("Final counter value: {} (expected 30)", g_thread_counter.load()); |
| 107 | |
| 108 | bool passed = (g_thread_completed == 3 && g_thread_counter >= 30); |
| 109 | if (passed) { |
nothing calls this directly
no test coverage detected