Make sure now_us returns a value bigger than the previous one
On some platforms clock_gettime may sometimes return a current
time that is earlier than a previous time. While it rarely
happens, we need cover the case anyway.
Bug: 18154020
Change-Id: I3846487aa45d0b11aeb63fa68af8826b864248fa
diff --git a/gki/ulinux/gki_ulinux.c b/gki/ulinux/gki_ulinux.c
index 0c22d23..c2db835 100644
--- a/gki/ulinux/gki_ulinux.c
+++ b/gki/ulinux/gki_ulinux.c
@@ -153,9 +153,18 @@
/** Callback from Java thread after alarm from AlarmService fires. */
static void bt_alarm_cb(void *data)
{
+ UINT32 ticks_taken = 0;
+
alarm_service.timer_last_expired_us = now_us();
- UINT32 ticks_taken = GKI_MS_TO_TICKS((alarm_service.timer_last_expired_us
- - alarm_service.timer_started_us) / 1000);
+ if (alarm_service.timer_last_expired_us > alarm_service.timer_started_us)
+ {
+ ticks_taken = GKI_MS_TO_TICKS((alarm_service.timer_last_expired_us
+ - alarm_service.timer_started_us) / 1000);
+ } else {
+ // this could happen on some platform
+ ALOGE("%s now_us %lld less than %lld", __func__, alarm_service.timer_last_expired_us,
+ alarm_service.timer_started_us);
+ }
GKI_timer_update(ticks_taken > alarm_service.ticks_scheduled
? ticks_taken : alarm_service.ticks_scheduled);