From ec03ba2a3bc4dc2328b3278c61006a957fbdb245 Mon Sep 17 00:00:00 2001 From: William Lash Date: Sun, 30 Jun 2019 01:08:41 +0000 Subject: [PATCH] Make sure to set the maxwait before each select call On linux at least select will update the timeout value if it returns before the timeout is up. It seems that simply moving the ````maxwait.tv_usec=10000;```` statement inside the ````while(1)```` loop significantly reduced CPU usage. We'll keep the timeout at 10 msec until the delay becomes noticable, which so far isn't. --- src/curses/ux_input.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/curses/ux_input.c b/src/curses/ux_input.c index 62047b9..c90ed92 100644 --- a/src/curses/ux_input.c +++ b/src/curses/ux_input.c @@ -178,11 +178,6 @@ static int unix_read_char(int extkeys) fd_set rsel; struct timeval tval, *t_left, maxwait; - /* - * If the timeout is 0, we still want to call os_tick once per second - */ - maxwait.tv_sec=0; - maxwait.tv_usec=1000; while(1) { /* Wait with select so that we get interrupted on SIGWINCH. */ @@ -190,11 +185,21 @@ static int unix_read_char(int extkeys) FD_SET(fd, &rsel); os_tick(); refresh(); + + /* + * If the timeout is 0, we still want to call os_tick periodically + * + * Based on experimentation, 10 msec seems to strike a balance + * between cpu usage and not having pauses between sounds + */ + maxwait.tv_sec=0; + maxwait.tv_usec=10000; + t_left = timeout_left(&tval) ? &tval : NULL; /* * if the timeout is zero, we wait forever for input, but if * we are playing a sequence of sounds, we need to periodically - * call os_tick(). So if the timeout is zero, wait up to a second + * call os_tick(). So if the timeout is zero, wait up to maxwait * for input, but if we get no input continue the while loop. */ if (t_left) -- 2.34.1