Skip to content

Commit a3e5af9

Browse files
peter-mitsisfabiobaltieri
authored andcommitted
kernel: Update k_sleep() and k_usleep() return values
Updates both the k_sleep() and k_usleep() return values so that if the thread was woken up prematurely, they will return the time left to sleep rounded up to the nearest millisecond (for k_sleep) or microsecond (for k_usleep) instead of rounding down. This removes ambiguity should there be a non-zero number of remaining ticks that correlate to a time of less than 1 millisecond or 1 microsecond. Signed-off-by: Peter Mitsis <[email protected]>
1 parent 093c7f4 commit a3e5af9

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

include/zephyr/kernel.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,9 @@ __syscall int k_thread_join(struct k_thread *thread, k_timeout_t timeout);
467467
*
468468
* @param timeout Desired duration of sleep.
469469
*
470-
* @return Zero if the requested time has elapsed or the number of milliseconds
471-
* left to sleep, if thread was woken up by \ref k_wakeup call.
470+
* @return Zero if the requested time has elapsed or if the thread was woken up
471+
* by the \ref k_wakeup call, the time left to sleep rounded up to the nearest
472+
* millisecond.
472473
*/
473474
__syscall int32_t k_sleep(k_timeout_t timeout);
474475

@@ -479,8 +480,9 @@ __syscall int32_t k_sleep(k_timeout_t timeout);
479480
*
480481
* @param ms Number of milliseconds to sleep.
481482
*
482-
* @return Zero if the requested time has elapsed or the number of milliseconds
483-
* left to sleep, if thread was woken up by \ref k_wakeup call.
483+
* @return Zero if the requested time has elapsed or if the thread was woken up
484+
* by the \ref k_wakeup call, the time left to sleep rounded up to the nearest
485+
* millisecond.
484486
*/
485487
static inline int32_t k_msleep(int32_t ms)
486488
{
@@ -499,8 +501,9 @@ static inline int32_t k_msleep(int32_t ms)
499501
*
500502
* @param us Number of microseconds to sleep.
501503
*
502-
* @return Zero if the requested time has elapsed or the number of microseconds
503-
* left to sleep, if thread was woken up by \ref k_wakeup call.
504+
* @return Zero if the requested time has elapsed or if the thread was woken up
505+
* by the \ref k_wakeup call, the time left to sleep rounded up to the nearest
506+
* microsecond.
504507
*/
505508
__syscall int32_t k_usleep(int32_t us);
506509

kernel/sched.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ int32_t z_impl_k_sleep(k_timeout_t timeout)
15901590

15911591
ticks = z_tick_sleep(ticks);
15921592

1593-
int32_t ret = k_ticks_to_ms_floor64(ticks);
1593+
int32_t ret = k_ticks_to_ms_ceil64(ticks);
15941594

15951595
SYS_PORT_TRACING_FUNC_EXIT(k_thread, sleep, timeout, ret);
15961596

@@ -1614,9 +1614,11 @@ int32_t z_impl_k_usleep(int us)
16141614
ticks = k_us_to_ticks_ceil64(us);
16151615
ticks = z_tick_sleep(ticks);
16161616

1617-
SYS_PORT_TRACING_FUNC_EXIT(k_thread, usleep, us, k_ticks_to_us_floor64(ticks));
1617+
int32_t ret = k_ticks_to_us_ceil64(ticks);
16181618

1619-
return k_ticks_to_us_floor64(ticks);
1619+
SYS_PORT_TRACING_FUNC_EXIT(k_thread, usleep, us, ret);
1620+
1621+
return ret;
16201622
}
16211623

16221624
#ifdef CONFIG_USERSPACE

0 commit comments

Comments
 (0)