From: Bart Van Assche Date: Wed, 9 Sep 2020 02:32:02 +0000 (-0700) Subject: helper_thread: Introduce the wait_for_action() function X-Git-Tag: fio-3.24~21^2~3 X-Git-Url: https://git.kernel.dk/?a=commitdiff_plain;h=335210df931c79d59f93424e64c0623e59b18754;p=fio.git helper_thread: Introduce the wait_for_action() function This patch makes helper_thread_main() easier to read. Signed-off-by: Bart Van Assche --- diff --git a/helper_thread.c b/helper_thread.c index 88937ade..40f99a5e 100644 --- a/helper_thread.c +++ b/helper_thread.c @@ -140,6 +140,38 @@ void helper_thread_exit(void) pthread_join(helper_data->thread, NULL); } +/* + * Waits for an action from fd during at least timeout_ms. `fd` must be in + * non-blocking mode. + */ +static uint8_t wait_for_action(int fd, unsigned int timeout_ms) +{ + struct timeval timeout = { + .tv_sec = timeout_ms / 1000, + .tv_usec = (timeout_ms % 1000) * 1000, + }; + fd_set rfds, efds; + uint8_t action = 0; + int res; + + res = read_from_pipe(fd, &action, sizeof(action)); + if (res > 0 || timeout_ms == 0) + return action; + FD_ZERO(&rfds); + FD_SET(fd, &rfds); + FD_ZERO(&efds); + FD_SET(fd, &efds); + res = select(fd + 1, &rfds, NULL, &efds, &timeout); + if (res < 0) { + log_err("fio: select() call in helper thread failed: %s", + strerror(errno)); + return A_EXIT; + } + if (FD_ISSET(fd, &rfds)) + read_from_pipe(fd, &action, sizeof(action)); + return action; +} + static unsigned int task_helper(struct timespec *last, struct timespec *now, unsigned int period, void do_task()) { unsigned int next, since; @@ -164,7 +196,6 @@ static void *helper_thread_main(void *data) unsigned int msec_to_next_event, next_log, next_si = status_interval; unsigned int next_ss = STEADYSTATE_MSEC; struct timespec ts, last_du, last_ss, last_si; - char action; int ret = 0; sk_out_assign(hd->sk_out); @@ -182,26 +213,11 @@ static void *helper_thread_main(void *data) msec_to_next_event = DISK_UTIL_MSEC; while (!ret && !hd->exit) { uint64_t since_du; - struct timeval timeout = { - .tv_sec = msec_to_next_event / 1000, - .tv_usec = (msec_to_next_event % 1000) * 1000, - }; - fd_set rfds, efds; - - if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) < 0) { - FD_ZERO(&rfds); - FD_SET(hd->pipe[0], &rfds); - FD_ZERO(&efds); - FD_SET(hd->pipe[0], &efds); - if (select(1, &rfds, NULL, &efds, &timeout) < 0) { - log_err("fio: select() call in helper thread failed: %s", - strerror(errno)); - ret = 1; - } - if (read_from_pipe(hd->pipe[0], &action, sizeof(action)) < - 0) - action = 0; - } + uint8_t action; + + action = wait_for_action(hd->pipe[0], msec_to_next_event); + if (action == A_EXIT) + break; fio_get_mono_time(&ts);