Safe for concurrent callers: only publishes INITED after cbm_mutex_init() * has completed. Callers that lose the CAS race spin until init finishes. */
| 438 | /* Safe for concurrent callers: only publishes INITED after cbm_mutex_init() |
| 439 | * has completed. Callers that lose the CAS race spin until init finishes. */ |
| 440 | void cbm_ui_log_init(void) { |
| 441 | int state = atomic_load(&g_log_mutex_init); |
| 442 | if (state == CBM_LOG_MUTEX_INITED) |
| 443 | return; |
| 444 | |
| 445 | state = CBM_LOG_MUTEX_UNINIT; |
| 446 | if (atomic_compare_exchange_strong(&g_log_mutex_init, &state, CBM_LOG_MUTEX_INITING)) { |
| 447 | cbm_mutex_init(&g_log_mutex); |
| 448 | cbm_mutex_init(&g_ui_log_mutex); |
| 449 | atomic_store(&g_log_mutex_init, CBM_LOG_MUTEX_INITED); |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | /* Another thread is initializing — spin until done */ |
| 454 | while (atomic_load(&g_log_mutex_init) != CBM_LOG_MUTEX_INITED) { |
| 455 | cbm_usleep(1000); /* 1ms */ |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /* Called only under g_log_mutex. Cache-directory changes are used by isolated |
| 460 | * tests; production has one daemon-owned path for its lifetime. */ |
no test coverage detected