Call poll at a fixed interval.

The existing implementation blocks in select() for a fixed amount of
time. This change tracks when the next event (likely poll()) wants to be
run, and uses a shorter timeout in select() if necessary.

Also track all these timeouts using milliseconds as returned by
timeval_ms() instead of `struct timeval` to simplify the code.

This feature is helpful if poll() wants to do something like sample PCs
or memory values for basically the entire time that otherwise OpenOCD
would be hung in select(). See
https://github.com/riscv/riscv-openocd/pull/541 for an example of that.
The RISC-V code using this change will be upstreamed some day, too.

Signed-off-by: Tim Newsome <tim@sifive.com>
Change-Id: I67104a7cf69ed07c8399c14aa55963fc5116a67d
Reviewed-on: http://openocd.zylin.com/6363
Tested-by: jenkins
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
This commit is contained in:
Tim Newsome
2021-07-09 12:58:07 -07:00
committed by Antonio Borneo
parent beff3de2ce
commit db16b3dc5b
3 changed files with 42 additions and 15 deletions

View File

@@ -437,6 +437,8 @@ int server_loop(struct command_context *command_context)
/* used in accept() */
int retval;
int64_t next_event = timeval_ms() + polling_period;
#ifndef _WIN32
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
LOG_ERROR("couldn't set SIGPIPE to SIG_IGN");
@@ -478,7 +480,12 @@ int server_loop(struct command_context *command_context)
retval = socket_select(fd_max + 1, &read_fds, NULL, NULL, &tv);
} else {
/* Every 100ms, can be changed with "poll_period" command */
tv.tv_usec = polling_period * 1000;
int timeout_ms = next_event - timeval_ms();
if (timeout_ms < 0)
timeout_ms = 0;
else if (timeout_ms > polling_period)
timeout_ms = polling_period;
tv.tv_usec = timeout_ms * 1000;
/* Only while we're sleeping we'll let others run */
openocd_sleep_prelude();
kept_alive();
@@ -511,7 +518,8 @@ int server_loop(struct command_context *command_context)
if (retval == 0) {
/* We only execute these callbacks when there was nothing to do or we timed
*out */
target_call_timer_callbacks();
target_call_timer_callbacks_now();
next_event = target_timer_next_event();
process_jim_events(command_context);
FD_ZERO(&read_fds); /* eCos leaves read_fds unchanged in this case! */