| 167 | } |
| 168 | |
| 169 | void test_mutex_contention(void* /*arg*/) { |
| 170 | klog::Info("=== Mutex Contention Test ==="); |
| 171 | |
| 172 | Mutex mtx("contention_test"); |
| 173 | std::atomic<int> counter{0}; |
| 174 | bool passed = true; |
| 175 | |
| 176 | ContentionArgs ctx_a; |
| 177 | ctx_a.mtx = &mtx; |
| 178 | ctx_a.counter = &counter; |
| 179 | |
| 180 | ContentionArgs ctx_b; |
| 181 | ctx_b.mtx = &mtx; |
| 182 | ctx_b.counter = &counter; |
| 183 | |
| 184 | auto task_a = kstd::make_unique<TaskControlBlock>( |
| 185 | "ContentionA", 10, contention_worker, reinterpret_cast<void*>(&ctx_a)); |
| 186 | auto task_b = kstd::make_unique<TaskControlBlock>( |
| 187 | "ContentionB", 10, contention_worker, reinterpret_cast<void*>(&ctx_b)); |
| 188 | |
| 189 | TaskManagerSingleton::instance().AddTask(std::move(task_a)); |
| 190 | TaskManagerSingleton::instance().AddTask(std::move(task_b)); |
| 191 | |
| 192 | int timeout = 100; |
| 193 | while (timeout > 0 && counter.load() < 10) { |
| 194 | (void)sys_sleep(50); |
| 195 | timeout--; |
| 196 | } |
| 197 | |
| 198 | if (counter.load() != 10) { |
| 199 | klog::Err("test_mutex_contention: FAIL — counter={}, expected 10", |
| 200 | counter.load()); |
| 201 | passed = false; |
| 202 | } |
| 203 | |
| 204 | if (passed) { |
| 205 | klog::Info("Mutex Contention Test: PASSED"); |
| 206 | } else { |
| 207 | klog::Err("Mutex Contention Test: FAILED"); |
| 208 | g_tests_failed++; |
| 209 | } |
| 210 | |
| 211 | g_tests_completed++; |
| 212 | sys_exit(0); |
| 213 | } |
| 214 | |
| 215 | // ========================================================================= |
| 216 | // test_mutex_ordering |