| 1118 | } |
| 1119 | |
| 1120 | void cbm_watcher_free(cbm_watcher_t *w) { |
| 1121 | if (!w) { |
| 1122 | return; |
| 1123 | } |
| 1124 | /* Safety net: ensure stopped is set before draining pending_free. |
| 1125 | * In production the caller should cbm_watcher_stop() + join first. */ |
| 1126 | atomic_store(&w->stopped, 1); |
| 1127 | cbm_mutex_lock(&w->coordination_lock); |
| 1128 | cbm_mutex_lock(&w->projects_lock); |
| 1129 | cbm_ht_foreach(w->projects, free_state_entry, NULL); |
| 1130 | cbm_ht_free(w->projects); |
| 1131 | for (int i = 0; i < w->pending_free_count; i++) { |
| 1132 | state_free(w->pending_free[i]); |
| 1133 | } |
| 1134 | free(w->pending_free); |
| 1135 | cbm_mutex_unlock(&w->projects_lock); |
| 1136 | cbm_mutex_unlock(&w->coordination_lock); |
| 1137 | cbm_mutex_destroy(&w->projects_lock); |
| 1138 | cbm_mutex_destroy(&w->coordination_lock); |
| 1139 | free(w); |
| 1140 | } |
| 1141 | |
| 1142 | void cbm_watcher_set_project_mutation_guard(cbm_watcher_t *w, |
| 1143 | cbm_watcher_project_mutation_begin_fn begin, |