Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OsTimeInUs() bug on Linux ARM
09-01-2012, 08:44 PM
Post: #1
OsTimeInUs() bug on Linux ARM
The code in OsTimeInUs() doesn't work correctly on Linux ARM, and possibly other Linux platforms. This has serious consequences, with timer events either fired at the wrong time or lost completely.

To fix the problem, change line 242 in Os/Posix/Os.c from

return (uint64_t)((diff.tv_sec * 1000000) + diff.tv_usec);

to

return (uint64_t)diff.tv_sec * 1000000 + diff.tv_usec;

The first version is wrong because the tv_sec field is 32 bits on Linux ARM and the compiler therefore uses 32-bit arithmetic to evaluate the expression (diff.tv_sec * 1000000). For tv_sec values over 2147 (about 36 minutes), this causes 32-bit overflow which results in the wrong 64-bit number being returned.

The second version performs the entire calculation in 64-bit arithmetic which should give a correct result for times up to 9,223,372,036,854 seconds or about 292271 years. This is probably sufficient for most ohNet applications Smile

Simon
Find all posts by this user
10-01-2012, 11:40 AM (This post was last modified: 10-01-2012 11:40 AM by simonc.)
Post: #2
RE: OsTimeInUs() bug on Linux ARM
Oops, well spotted, thanks. This is fixed locally now so should be on github this evening
Find all posts by this user


Forum Jump: