From 982a4a5d98c7034004ec04bff7faea6e1dfcf131 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 12 Aug 2008 13:58:51 +0200 Subject: [PATCH] hwclock: use time limit for synchronization busy wait Signed-off-by: Karel Zak --- hwclock/clock.h | 1 + hwclock/hwclock.c | 2 +- hwclock/rtc.c | 23 ++++++++++++----------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/hwclock/clock.h b/hwclock/clock.h index 6e8a9b4c..cbdf9991 100644 --- a/hwclock/clock.h +++ b/hwclock/clock.h @@ -33,6 +33,7 @@ extern void outsyserr(char *msg, ...) #else ; #endif +extern double time_diff(struct timeval subtrahend, struct timeval subtractor); /* cmos.c */ extern void set_cmos_epoch(int ARCconsole, int SRM); extern void set_cmos_access(int Jensen, int funky_toy); diff --git a/hwclock/hwclock.c b/hwclock/hwclock.c index 82da273d..c08ae3f5 100644 --- a/hwclock/hwclock.c +++ b/hwclock/hwclock.c @@ -182,7 +182,7 @@ read_date_from_file (struct tm *tm) { write_date_to_file (tm); } -static double +double time_diff(struct timeval subtrahend, struct timeval subtractor) { /*--------------------------------------------------------------------------- The difference in seconds between two times in "timeval" format. diff --git a/hwclock/rtc.c b/hwclock/rtc.c index 3eb1f4eb..e59414fd 100644 --- a/hwclock/rtc.c +++ b/hwclock/rtc.c @@ -179,8 +179,8 @@ busywait_for_rtc_clock_tick(const int rtc_fd) { struct tm start_time; /* The time when we were called (and started waiting) */ struct tm nowtime; - int i; /* local loop index */ int rc; + struct timeval begin, now; if (debug) printf(_("Waiting in loop for time from %s to change\n"), @@ -191,25 +191,26 @@ busywait_for_rtc_clock_tick(const int rtc_fd) { return 1; /* Wait for change. Should be within a second, but in case something - weird happens, we have a limit on this loop to reduce the impact - of this failure. - */ - for (i = 0; - (rc = do_rtc_read_ioctl(rtc_fd, &nowtime)) == 0 - && start_time.tm_sec == nowtime.tm_sec; - i++) - if (i >= 1000000) { + * weird happens, we have a time limit (1.5s) on this loop to reduce the + * impact of this failure. + */ + gettimeofday(&begin, NULL); + do { + rc = do_rtc_read_ioctl(rtc_fd, &nowtime); + if (rc || start_time.tm_sec != nowtime.tm_sec) + break; + gettimeofday(&now, NULL); + if (time_diff(now, begin) > 1.5) { fprintf(stderr, _("Timed out waiting for time change.\n")); return 2; } + } while(1); if (rc) return 3; return 0; } - - static int synchronize_to_clock_tick_rtc(void) { /*---------------------------------------------------------------------------- -- 2.39.5