| 50 | } // namespace |
| 51 | |
| 52 | auto yield_test() -> bool { |
| 53 | klog::Info("yield_test: start"); |
| 54 | auto& tm = TaskManagerSingleton::instance(); |
| 55 | |
| 56 | // Test 1: basic yield returns 0 |
| 57 | g_yield_ret = -1; |
| 58 | auto t1 = kstd::make_unique<TaskControlBlock>("YieldBasic", 10, |
| 59 | yield_basic_work, nullptr); |
| 60 | tm.AddTask(std::move(t1)); |
| 61 | |
| 62 | int timeout = 100; |
| 63 | while (timeout-- > 0 && g_yield_ret.load() == -1) { |
| 64 | (void)sys_sleep(50); |
| 65 | } |
| 66 | EXPECT_EQ(g_yield_ret.load(), 0, "sys_yield should return 0"); |
| 67 | |
| 68 | // Test 2: yield fairness — both tasks make progress |
| 69 | g_a_count = 0; |
| 70 | g_b_count = 0; |
| 71 | g_fairness_done = false; |
| 72 | |
| 73 | auto ta = |
| 74 | kstd::make_unique<TaskControlBlock>("YieldA", 10, yield_task_a, nullptr); |
| 75 | auto tb = |
| 76 | kstd::make_unique<TaskControlBlock>("YieldB", 10, yield_task_b, nullptr); |
| 77 | tm.AddTask(std::move(ta)); |
| 78 | tm.AddTask(std::move(tb)); |
| 79 | |
| 80 | timeout = 100; |
| 81 | while (timeout-- > 0 && !g_fairness_done.load()) { |
| 82 | (void)sys_sleep(50); |
| 83 | } |
| 84 | EXPECT_EQ(g_a_count.load(), 10, "Task A should complete 10 iterations"); |
| 85 | EXPECT_EQ(g_b_count.load(), 10, "Task B should complete 10 iterations"); |
| 86 | |
| 87 | klog::Info("yield_test: PASS"); |
| 88 | return true; |
| 89 | } |