| 1725 | } |
| 1726 | |
| 1727 | int cbm_watcher_poll_once(cbm_watcher_t *w) { |
| 1728 | if (!w) { |
| 1729 | return 0; |
| 1730 | } |
| 1731 | |
| 1732 | /* Snapshot project pointers under lock, then poll without holding it. |
| 1733 | * This keeps the critical section small — poll_project does git I/O |
| 1734 | * and may invoke index_fn which runs the full pipeline. */ |
| 1735 | cbm_mutex_lock(&w->projects_lock); |
| 1736 | |
| 1737 | /* Free deferred entries from the previous cycle. */ |
| 1738 | for (int i = 0; i < w->pending_free_count; i++) { |
| 1739 | state_free(w->pending_free[i]); |
| 1740 | } |
| 1741 | w->pending_free_count = 0; |
| 1742 | |
| 1743 | int n = cbm_ht_count(w->projects); |
| 1744 | if (n == 0) { |
| 1745 | cbm_mutex_unlock(&w->projects_lock); |
| 1746 | return 0; |
| 1747 | } |
| 1748 | project_state_t **snap = malloc(n * sizeof(project_state_t *)); |
| 1749 | if (!snap) { |
| 1750 | cbm_mutex_unlock(&w->projects_lock); |
| 1751 | return 0; |
| 1752 | } |
| 1753 | snapshot_ctx_t sc = {.items = snap, .count = 0, .cap = n}; |
| 1754 | cbm_ht_foreach(w->projects, snapshot_project, &sc); |
| 1755 | cbm_mutex_unlock(&w->projects_lock); |
| 1756 | |
| 1757 | poll_ctx_t ctx = { |
| 1758 | .w = w, |
| 1759 | .now = now_ns(), |
| 1760 | .reindexed = 0, |
| 1761 | }; |
| 1762 | for (int i = 0; i < sc.count; i++) { |
| 1763 | poll_project(NULL, snap[i], &ctx); |
| 1764 | } |
| 1765 | free(snap); |
| 1766 | return ctx.reindexed; |
| 1767 | } |
| 1768 | |
| 1769 | /* ── Blocking run loop ──────────────────────────────────────────── */ |
| 1770 | |