fsmonitor--daemon: prepare for adding health thread

Refactor daemon thread startup to make it easier to start a third thread class to monitor the health of the daemon. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Hostetler committed May 26, 2022 at 21:47 UTC 802aa31840bd2080f668d0efbaeea2021f4e2488
1 file changed +20 -7
builtin/fsmonitor--daemon.c
+20 -7
@@ -1174,6 +1174,8 @@ static int fsmonitor_run_daemon_1(struct fsmonitor_daemon_state *state)
1174 */
1175 .uds_disallow_chdir = 0
1176 };
1177 + int listener_started = 0;
1178 + int err = 0;
1179
1180 /*
1181 * Start the IPC thread pool before the we've started the file
@@ -1194,15 +1196,20 @@ static int fsmonitor_run_daemon_1(struct fsmonitor_daemon_state *state)
1196 if (pthread_create(&state->listener_thread, NULL,
1197 fsm_listen__thread_proc, state) < 0) {
1198 ipc_server_stop_async(state->ipc_server_data);
1197 - ipc_server_await(state->ipc_server_data);
1198 -
1199 - return error(_("could not start fsmonitor listener thread"));
1199 + err = error(_("could not start fsmonitor listener thread"));
1200 + goto cleanup;
1201 }
1202 + listener_started = 1;
1203
1204 /*
1205 * The daemon is now fully functional in background threads.
1206 + * Our primary thread should now just wait while the threads
1207 + * do all the work.
1208 + */
1209 +cleanup:
1210 + /*
1211 * Wait for the IPC thread pool to shutdown (whether by client
1205 - * request or from filesystem activity).
1212 + * request, from filesystem activity, or an error).
1213 */
1214 ipc_server_await(state->ipc_server_data);
1215
@@ -1211,10 +1218,16 @@ static int fsmonitor_run_daemon_1(struct fsmonitor_daemon_state *state)
1218 * event from the IPC thread pool, but it doesn't hurt to tell
1219 * it again. And wait for it to shutdown.
1220 */
1214 - fsm_listen__stop_async(state);
1215 - pthread_join(state->listener_thread, NULL);
1221 + if (listener_started) {
1222 + fsm_listen__stop_async(state);
1223 + pthread_join(state->listener_thread, NULL);
1224 + }
1225
1217 - return state->error_code;
1226 + if (err)
1227 + return err;
1228 + if (state->error_code)
1229 + return state->error_code;
1230 + return 0;
1231 }
1232
1233 static int fsmonitor_run_daemon(void)