From: Christopher Head Date: Wed, 24 Jan 2018 22:35:40 +0000 (-0800) Subject: Use timeval helpers X-Git-Tag: v0.11.0-rc1~1249 X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=commitdiff_plain;h=2428722a23d8a08b15331ab95884762e5f329a02 Use timeval helpers Some of these changes actually fix broken comparisons which could occasionally fail. Others just clean up the code and make it more clear. Change-Id: I6c398bdc45fa0d2716f48a74822457d1351f81a5 Signed-off-by: Christopher Head Reviewed-on: http://openocd.zylin.com/4380 Tested-by: jenkins Reviewed-by: Tomas Vanek --- diff --git a/src/jtag/drivers/presto.c b/src/jtag/drivers/presto.c index 49caa679f9..29bc811116 100644 --- a/src/jtag/drivers/presto.c +++ b/src/jtag/drivers/presto.c @@ -117,8 +117,7 @@ static int presto_read(uint8_t *buf, uint32_t size) ftbytes += presto->retval; gettimeofday(&now, NULL); - if ((now.tv_sec > timeout.tv_sec) || - ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec))) + if (timeval_compare(&now, &timeout) > 0) break; } diff --git a/src/target/cortex_m.c b/src/target/cortex_m.c index 24063a7e6a..79af632ac7 100644 --- a/src/target/cortex_m.c +++ b/src/target/cortex_m.c @@ -1787,8 +1787,7 @@ int cortex_m_profiling(struct target *target, uint32_t *samples, gettimeofday(&now, NULL); - if (sample_count >= max_num_samples || - (now.tv_sec >= timeout.tv_sec && now.tv_usec >= timeout.tv_usec)) { + if (sample_count >= max_num_samples || timeval_compare(&now, &timeout) > 0) { LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count); break; } diff --git a/src/target/embeddedice.c b/src/target/embeddedice.c index 09d6fc8a15..7232ef1e46 100644 --- a/src/target/embeddedice.c +++ b/src/target/embeddedice.c @@ -28,6 +28,7 @@ #include "embeddedice.h" #include "register.h" +#include /** * @file @@ -576,8 +577,8 @@ int embeddedice_handshake(struct arm_jtag *jtag_info, int hsbit, uint32_t timeou uint8_t field2_out[1]; int retval; uint32_t hsact; - struct timeval lap; struct timeval now; + struct timeval timeout_end; if (hsbit == EICE_COMM_CTRL_WBIT) hsact = 1; @@ -610,7 +611,8 @@ int embeddedice_handshake(struct arm_jtag *jtag_info, int hsbit, uint32_t timeou fields[2].in_value = NULL; jtag_add_dr_scan(jtag_info->tap, 3, fields, TAP_IDLE); - gettimeofday(&lap, NULL); + gettimeofday(&timeout_end, NULL); + timeval_add_time(&timeout_end, 0, timeout * 1000); do { jtag_add_dr_scan(jtag_info->tap, 3, fields, TAP_IDLE); retval = jtag_execute_queue(); @@ -621,8 +623,7 @@ int embeddedice_handshake(struct arm_jtag *jtag_info, int hsbit, uint32_t timeou return ERROR_OK; gettimeofday(&now, NULL); - } while ((uint32_t)((now.tv_sec - lap.tv_sec) * 1000 - + (now.tv_usec - lap.tv_usec) / 1000) <= timeout); + } while (timeval_compare(&now, &timeout_end) <= 0); LOG_ERROR("embeddedice handshake timeout"); return ERROR_TARGET_TIMEOUT; diff --git a/src/target/openrisc/or1k.c b/src/target/openrisc/or1k.c index 3895ddfaf5..bcb648c278 100644 --- a/src/target/openrisc/or1k.c +++ b/src/target/openrisc/or1k.c @@ -1248,8 +1248,7 @@ static int or1k_profiling(struct target *target, uint32_t *samples, samples[sample_count++] = reg_value; gettimeofday(&now, NULL); - if ((sample_count >= max_num_samples) || - ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))) { + if ((sample_count >= max_num_samples) || timeval_compare(&now, &timeout) > 0) { LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count); break; } diff --git a/src/target/target.c b/src/target/target.c index 52307dbf44..58d6d82682 100644 --- a/src/target/target.c +++ b/src/target/target.c @@ -1397,7 +1397,6 @@ int target_register_trace_callback(int (*callback)(struct target *target, int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv) { struct target_timer_callback **callbacks_p = &target_timer_callbacks; - struct timeval now; if (callback == NULL) return ERROR_COMMAND_SYNTAX_ERROR; @@ -1414,14 +1413,8 @@ int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int (*callbacks_p)->time_ms = time_ms; (*callbacks_p)->removed = false; - gettimeofday(&now, NULL); - (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000; - time_ms -= (time_ms % 1000); - (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000); - if ((*callbacks_p)->when.tv_usec > 1000000) { - (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000; - (*callbacks_p)->when.tv_sec += 1; - } + gettimeofday(&(*callbacks_p)->when, NULL); + timeval_add_time(&(*callbacks_p)->when, 0, time_ms * 1000); (*callbacks_p)->priv = priv; (*callbacks_p)->next = NULL; @@ -1556,14 +1549,8 @@ int target_call_trace_callbacks(struct target *target, size_t len, uint8_t *data static int target_timer_callback_periodic_restart( struct target_timer_callback *cb, struct timeval *now) { - int time_ms = cb->time_ms; - cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000; - time_ms -= (time_ms % 1000); - cb->when.tv_sec = now->tv_sec + time_ms / 1000; - if (cb->when.tv_usec > 1000000) { - cb->when.tv_usec = cb->when.tv_usec - 1000000; - cb->when.tv_sec += 1; - } + cb->when = *now; + timeval_add_time(&cb->when, 0, cb->time_ms * 1000L); return ERROR_OK; } @@ -1607,9 +1594,7 @@ static int target_call_timer_callbacks_check_time(int checktime) bool call_it = (*callback)->callback && ((!checktime && (*callback)->periodic) || - now.tv_sec > (*callback)->when.tv_sec || - (now.tv_sec == (*callback)->when.tv_sec && - now.tv_usec >= (*callback)->when.tv_usec)); + timeval_compare(&now, &(*callback)->when) >= 0); if (call_it) target_call_timer_callback(*callback, &now); @@ -2028,8 +2013,7 @@ static int target_profiling_default(struct target *target, uint32_t *samples, break; gettimeofday(&now, NULL); - if ((sample_count >= max_num_samples) || - ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))) { + if ((sample_count >= max_num_samples) || timeval_compare(&now, &timeout) >= 0) { LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count); break; } diff --git a/src/target/xscale.c b/src/target/xscale.c index 8fe8a2cb99..87a3d0f78d 100644 --- a/src/target/xscale.c +++ b/src/target/xscale.c @@ -404,8 +404,7 @@ static int xscale_read_tx(struct target *target, int consume) } gettimeofday(&now, NULL); - if ((now.tv_sec > timeout.tv_sec) || - ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec))) { + if (timeval_compare(&now, &timeout) > 0) { LOG_ERROR("time out reading TX register"); return ERROR_TARGET_TIMEOUT; }