| 245 | } |
| 246 | |
| 247 | void test_mutex_ordering(void* /*arg*/) { |
| 248 | klog::Info("=== Mutex Ordering Test ==="); |
| 249 | |
| 250 | Mutex mtx("ordering_test"); |
| 251 | std::atomic<int> sequence{0}; |
| 252 | bool passed = true; |
| 253 | |
| 254 | OrderingArgs ctx_first; |
| 255 | ctx_first.mtx = &mtx; |
| 256 | ctx_first.sequence = &sequence; |
| 257 | ctx_first.task_id = 1; |
| 258 | |
| 259 | OrderingArgs ctx_second; |
| 260 | ctx_second.mtx = &mtx; |
| 261 | ctx_second.sequence = &sequence; |
| 262 | ctx_second.task_id = 2; |
| 263 | |
| 264 | auto first_task = kstd::make_unique<TaskControlBlock>( |
| 265 | "OrderFirst", 10, ordering_first, reinterpret_cast<void*>(&ctx_first)); |
| 266 | auto second_task = kstd::make_unique<TaskControlBlock>( |
| 267 | "OrderSecond", 10, ordering_second, reinterpret_cast<void*>(&ctx_second)); |
| 268 | |
| 269 | TaskManagerSingleton::instance().AddTask(std::move(first_task)); |
| 270 | TaskManagerSingleton::instance().AddTask(std::move(second_task)); |
| 271 | |
| 272 | int timeout = 100; |
| 273 | while (timeout > 0 && sequence.load() != 2) { |
| 274 | (void)sys_sleep(50); |
| 275 | timeout--; |
| 276 | } |
| 277 | |
| 278 | if (sequence.load() != 2) { |
| 279 | klog::Err("test_mutex_ordering: FAIL — final sequence={}, expected 2", |
| 280 | sequence.load()); |
| 281 | passed = false; |
| 282 | } |
| 283 | |
| 284 | if (passed) { |
| 285 | klog::Info("Mutex Ordering Test: PASSED"); |
| 286 | } else { |
| 287 | klog::Err("Mutex Ordering Test: FAILED"); |
| 288 | g_tests_failed++; |
| 289 | } |
| 290 | |
| 291 | g_tests_completed++; |
| 292 | sys_exit(0); |
| 293 | } |
| 294 | |
| 295 | } // namespace |
| 296 | |