Skip to content

Commit ef8e6b1

Browse files
jeffhostetlerdscho
authored andcommitted
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 <[email protected]>
1 parent a0525f9 commit ef8e6b1

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

builtin/fsmonitor--daemon.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,8 @@ static int fsmonitor_run_daemon_1(struct fsmonitor_daemon_state *state)
11621162
*/
11631163
.uds_disallow_chdir = 0
11641164
};
1165+
int listener_started = 0;
1166+
int err = 0;
11651167

11661168
/*
11671169
* Start the IPC thread pool before the we've started the file
@@ -1182,15 +1184,20 @@ static int fsmonitor_run_daemon_1(struct fsmonitor_daemon_state *state)
11821184
if (pthread_create(&state->listener_thread, NULL,
11831185
fsm_listen__thread_proc, state) < 0) {
11841186
ipc_server_stop_async(state->ipc_server_data);
1185-
ipc_server_await(state->ipc_server_data);
1186-
1187-
return error(_("could not start fsmonitor listener thread"));
1187+
err = error(_("could not start fsmonitor listener thread"));
1188+
goto cleanup;
11881189
}
1190+
listener_started = 1;
11891191

11901192
/*
11911193
* The daemon is now fully functional in background threads.
1194+
* Our primary thread should now just wait while the threads
1195+
* do all the work.
1196+
*/
1197+
cleanup:
1198+
/*
11921199
* Wait for the IPC thread pool to shutdown (whether by client
1193-
* request or from filesystem activity).
1200+
* request, from filesystem activity, or an error).
11941201
*/
11951202
ipc_server_await(state->ipc_server_data);
11961203

@@ -1199,10 +1206,16 @@ static int fsmonitor_run_daemon_1(struct fsmonitor_daemon_state *state)
11991206
* event from the IPC thread pool, but it doesn't hurt to tell
12001207
* it again. And wait for it to shutdown.
12011208
*/
1202-
fsm_listen__stop_async(state);
1203-
pthread_join(state->listener_thread, NULL);
1209+
if (listener_started) {
1210+
fsm_listen__stop_async(state);
1211+
pthread_join(state->listener_thread, NULL);
1212+
}
12041213

1205-
return state->error_code;
1214+
if (err)
1215+
return err;
1216+
if (state->error_code)
1217+
return state->error_code;
1218+
return 0;
12061219
}
12071220

12081221
static int fsmonitor_run_daemon(void)

0 commit comments

Comments
 (0)