Skip to content

Commit d83a649

Browse files
edersondisouzanashif
authored andcommitted
lib/posix: Get uptime in ticks instead of cycles
__z_clock_nanosleep function was getting current time in cycles, via k_cycle_get_32(), to perform its time calculations. However, when calling k_sleep() to actually sleep, times are measured in ticks. This causes a problem when there's a big skew between the uptime measured in cycles vs uptime measured in ticks: in some platforms, the system clock maybe up for a long time already when Zephyr starts counting ticks, for instance, while downloading an image via PXE. In this case, the calculations done inside __z_clock_nanosleep end up measuring a much bigger current time than expected, thus sleeping too much, basically all the time since system clock initialization. This patch fixes that by avoiding the cycle trip: stick to ticks, instead. They start counting from Zephyr initialization instead, which is the expected uptime. Fixes #69608 Signed-off-by: Ederson de Souza <[email protected]>
1 parent f65770c commit d83a649

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

lib/posix/options/clock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static int __z_clock_nanosleep(clockid_t clock_id, int flags, const struct times
225225
ns = rqtp->tv_sec * NSEC_PER_SEC + rqtp->tv_nsec;
226226
}
227227

228-
uptime_ns = k_cyc_to_ns_ceil64(k_cycle_get_32());
228+
uptime_ns = k_ticks_to_ns_ceil64(sys_clock_tick_get());
229229

230230
if (flags & TIMER_ABSTIME && clock_id == CLOCK_REALTIME) {
231231
key = k_spin_lock(&rt_clock_base_lock);

0 commit comments

Comments
 (0)