| 212 | } |
| 213 | |
| 214 | void test_thread_exit(void* /*arg*/) { |
| 215 | klog::Info("=== Thread Exit Test ==="); |
| 216 | |
| 217 | g_exit_test_counter = 0; |
| 218 | |
| 219 | // 创建线程组主线程(不加入调度,entry 为 nullptr) |
| 220 | auto leader_uptr = |
| 221 | kstd::make_unique<TaskControlBlock>("ThreadLeader", 10, nullptr, nullptr); |
| 222 | auto* leader = leader_uptr.get(); |
| 223 | leader->pid = local_pid_counter.fetch_add(1); |
| 224 | leader->aux->tgid = leader->pid; |
| 225 | leader->aux->parent_pid = 1; |
| 226 | |
| 227 | // 创建子线程 |
| 228 | auto thread1 = kstd::make_unique<TaskControlBlock>( |
| 229 | "Thread1", 10, child_thread_exit_work, reinterpret_cast<void*>(1)); |
| 230 | thread1->aux->tgid = leader->pid; |
| 231 | thread1->JoinThreadGroup(leader); |
| 232 | |
| 233 | TaskManagerSingleton::instance().AddTask(std::move(thread1)); |
| 234 | |
| 235 | auto thread2 = kstd::make_unique<TaskControlBlock>( |
| 236 | "Thread2", 10, child_thread_exit_work, reinterpret_cast<void*>(2)); |
| 237 | thread2->aux->tgid = leader->pid; |
| 238 | thread2->JoinThreadGroup(leader); |
| 239 | |
| 240 | TaskManagerSingleton::instance().AddTask(std::move(thread2)); |
| 241 | |
| 242 | klog::Info("Created thread group with leader (pid={}) and 2 threads", |
| 243 | leader->pid); |
| 244 | |
| 245 | // 等待线程运行并退出 |
| 246 | (void)sys_sleep(200); |
| 247 | |
| 248 | klog::Info("Exit test counter: {} (expected >= 6)", |
| 249 | g_exit_test_counter.load()); |
| 250 | |
| 251 | bool passed = (g_exit_test_counter.load() >= 6); |
| 252 | |
| 253 | if (passed) { |
| 254 | klog::Info("Thread Exit Test: PASSED"); |
| 255 | } else { |
| 256 | klog::Err("Thread Exit Test: FAILED (counter={}, expected >= 6)", |
| 257 | g_exit_test_counter.load()); |
| 258 | g_tests_failed++; |
| 259 | } |
| 260 | |
| 261 | g_tests_completed++; |
| 262 | sys_exit(passed ? 0 : 1); |
| 263 | } |
| 264 | |
| 265 | // --------------------------------------------------------------------------- |
| 266 | // test_orphan_exit |