From e9f23cc33fec25241aa52b2bb7f86300a34c2be4 Mon Sep 17 00:00:00 2001 From: Mark Burton Date: Sun, 23 Feb 2014 18:07:06 +0000 Subject: [PATCH] Reorder assignments in cmn_systimer_get() to avoid race. Before, it was possible for the system timer counter to wrap from high to low after its value had been read but before the value of cmn_systimer_counter was read and so the resulting sum of those values would be too large. By reordering the assignments, we can now detect if cmn_systimer_counter has been advanced while reading the system timer counter value and, if so, repeat the assignments to ensure consistent values. --- src/common_tmr.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common_tmr.c b/src/common_tmr.c index 744a7752..b77d5095 100644 --- a/src/common_tmr.c +++ b/src/common_tmr.c @@ -425,12 +425,12 @@ timer_data_type cmn_systimer_get(void) { u64 tempsys, tempcnt, crtsys; - tempcnt = platform_timer_sys_raw_read(); tempsys = cmn_systimer_counter; + tempcnt = platform_timer_sys_raw_read(); while( ( crtsys = cmn_systimer_counter ) != tempsys ) { - tempcnt = platform_timer_sys_raw_read(); tempsys = crtsys; + tempcnt = platform_timer_sys_raw_read(); } crtsys += tempcnt / cmn_systimer_ticks_for_us; if( crtsys > PLATFORM_TIMER_SYS_MAX ) // timer overflow