Message ID | 1482852963-6887-5-git-send-email-yamada.masahiro@socionext.com |
---|---|
State | New |
Headers | show |
On 27 December 2016 at 08:36, Masahiro Yamada <yamada.masahiro@socionext.com> wrote: > It is not safe to compare timer values directly. > > On 32-bit systems, for example, timer_get_us() wraps around every > 72 min. (2 ^ 32 / 1000000 =~ 4295 sec =~ 72 min). Depending on > the get_ticks() implementation, it may wrap more frequently. > The 72 min might be possible on the use of U-Boot. > > Let's borrow time_after, time_before, and friends to solve the > wrap-around problem. > > These macros were copied from include/linux/jiffies.h of Linux 4.9. > > Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com> > --- > > Changes in v2: None > > include/time.h | 41 +++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 41 insertions(+) Reviewed-by: Simon Glass <sjg@chromium.org> _______________________________________________ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot
On Wed, Dec 28, 2016 at 12:36:02AM +0900, Masahiro Yamada wrote: > It is not safe to compare timer values directly. > > On 32-bit systems, for example, timer_get_us() wraps around every > 72 min. (2 ^ 32 / 1000000 =~ 4295 sec =~ 72 min). Depending on > the get_ticks() implementation, it may wrap more frequently. > The 72 min might be possible on the use of U-Boot. > > Let's borrow time_after, time_before, and friends to solve the > wrap-around problem. > > These macros were copied from include/linux/jiffies.h of Linux 4.9. > > Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com> > Reviewed-by: Simon Glass <sjg@chromium.org> Applied to u-boot/master, thanks! -- Tom _______________________________________________ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot
diff --git a/include/time.h b/include/time.h index 5ed021f..5746ad9 100644 --- a/include/time.h +++ b/include/time.h @@ -5,6 +5,8 @@ #ifndef _TIME_H #define _TIME_H +#include <linux/typecheck.h> + unsigned long get_timer(unsigned long base); /* @@ -13,4 +15,43 @@ unsigned long get_timer(unsigned long base); */ unsigned long timer_get_us(void); +/* + * These inlines deal with timer wrapping correctly. You are + * strongly encouraged to use them + * 1. Because people otherwise forget + * 2. Because if the timer wrap changes in future you won't have to + * alter your driver code. + * + * time_after(a,b) returns true if the time a is after time b. + * + * Do this with "<0" and ">=0" to only test the sign of the result. A + * good compiler would generate better code (and a really good compiler + * wouldn't care). Gcc is currently neither. + */ +#define time_after(a,b) \ + (typecheck(unsigned long, a) && \ + typecheck(unsigned long, b) && \ + ((long)((b) - (a)) < 0)) +#define time_before(a,b) time_after(b,a) + +#define time_after_eq(a,b) \ + (typecheck(unsigned long, a) && \ + typecheck(unsigned long, b) && \ + ((long)((a) - (b)) >= 0)) +#define time_before_eq(a,b) time_after_eq(b,a) + +/* + * Calculate whether a is in the range of [b, c]. + */ +#define time_in_range(a,b,c) \ + (time_after_eq(a,b) && \ + time_before_eq(a,c)) + +/* + * Calculate whether a is in the range of [b, c). + */ +#define time_in_range_open(a,b,c) \ + (time_after_eq(a,b) && \ + time_before(a,c)) + #endif /* _TIME_H */
It is not safe to compare timer values directly. On 32-bit systems, for example, timer_get_us() wraps around every 72 min. (2 ^ 32 / 1000000 =~ 4295 sec =~ 72 min). Depending on the get_ticks() implementation, it may wrap more frequently. The 72 min might be possible on the use of U-Boot. Let's borrow time_after, time_before, and friends to solve the wrap-around problem. These macros were copied from include/linux/jiffies.h of Linux 4.9. Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com> --- Changes in v2: None include/time.h | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) -- 2.7.4 _______________________________________________ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot