diff options
author | Andreas Eversberg <jolly@eversberg.eu> | 2024-01-24 17:12:27 +0100 |
---|---|---|
committer | jolly <andreas@eversberg.eu> | 2024-01-30 12:06:58 +0000 |
commit | e8f05481ea1f5323b64f177d57f0f34cb0cdbee5 (patch) | |
tree | 4135c4b577316fa4aedb6fc09c664957112cec7f /src | |
parent | 22790c5d9792e5657457173b36fa04e165179672 (diff) |
Prevent poll() in select.c to timeout too early
Adjust osmo_timers_nearest_ms() to round up the remaining time.
Note that poll() has a granularity of 1 millisecond.
Previously, when rounding down the remaining time, osmo_select_main()
would return too early, before the nearest timer timed out.
Consequently, the main loop repeatedly called osmo_select_main() until
the timer actually timed out, resulting in excessive CPU usage.
By modifying osmo_timers_nearest_ms() to round up the remaining time,
we ensure accurate timeout calculations, preventing unnecessary CPU
consumption during the main loop.
The patch only applies to non-embedded version of libosmocore, because
the impact on embedded systems is not verified tested.
Related: OS#6339
Change-Id: I79de77c79af4d50d1eb9ca0c5417123ff760dca3
Diffstat (limited to 'src')
-rw-r--r-- | src/core/timer.c | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/src/core/timer.c b/src/core/timer.c index 20d87a05..067bd875 100644 --- a/src/core/timer.c +++ b/src/core/timer.c @@ -191,7 +191,16 @@ int osmo_timers_nearest_ms(void) return -1; nearest_ms = nearest_p->tv_sec * 1000; +#ifndef EMBEDDED + /* By adding 999 milliseconds, we ensure rounding up to the nearest + * whole millisecond. This approach prevents the return of 0 when the + * timer is still active, and it guarantees that the calling process + * does not wait for a duration shorter than the time remaining on the + * timer. */ + nearest_ms += (nearest_p->tv_usec + 999) / 1000; +#else nearest_ms += nearest_p->tv_usec / 1000; +#endif return nearest_ms; } |