| 1411 | } |
| 1412 | |
| 1413 | int cbm_watcher_run(cbm_watcher_t *w, int base_interval_ms) { |
| 1414 | if (!w) { |
| 1415 | return CBM_NOT_FOUND; |
| 1416 | } |
| 1417 | if (base_interval_ms <= 0) { |
| 1418 | base_interval_ms = POLL_BASE_MS; |
| 1419 | } |
| 1420 | |
| 1421 | cbm_log_info("watcher.start", "interval_ms", base_interval_ms > 999 ? "multi-sec" : "fast"); |
| 1422 | |
| 1423 | while (!atomic_load(&w->stopped)) { |
| 1424 | cbm_watcher_poll_once(w); |
| 1425 | |
| 1426 | /* Sleep in small increments to allow responsive shutdown */ |
| 1427 | int slept = 0; |
| 1428 | while (slept < base_interval_ms && !atomic_load(&w->stopped)) { |
| 1429 | int chunk = base_interval_ms - slept; |
| 1430 | if (chunk > SLEEP_CHUNK_MS) { |
| 1431 | chunk = SLEEP_CHUNK_MS; |
| 1432 | } |
| 1433 | cbm_usleep((unsigned)chunk * CBM_MSEC_PER_SEC); |
| 1434 | slept += chunk; |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | cbm_log_info("watcher.stop"); |
| 1439 | return 0; |
| 1440 | } |