Skip to content
Permalink
Browse files

kernel/sched.c: refactor k_sleep() implementation for varied timescales

Current z_impl_k_sleep() does double duty, converting between units
specified by the API and ticks, as well as implementing the sleeping
mechanism itself. This patch separates the API from the mechanism,
so that sleeps need not be tied to millisecond timescales.

Signed-off-by: Charles E. Youse <charles.youse@intel.com>
  • Loading branch information...
Charles E. Youse authored and nashif committed May 8, 2019
1 parent 2f126bb commit b186303cb641ff9c42e11f2828c7369c616f7ecb
Showing with 16 additions and 7 deletions.
  1. +16 −7 kernel/sched.c
@@ -912,24 +912,22 @@ void z_impl_k_yield(void)
Z_SYSCALL_HANDLER0_SIMPLE_VOID(k_yield);
#endif

s32_t z_impl_k_sleep(s32_t duration)
static s32_t z_tick_sleep(s32_t ticks)
{
#ifdef CONFIG_MULTITHREADING
u32_t expected_wakeup_time;
s32_t ticks;

__ASSERT(!z_is_in_isr(), "");
__ASSERT(duration != K_FOREVER, "");

K_DEBUG("thread %p for %d ns\n", _current, duration);
K_DEBUG("thread %p for %d ticks\n", _current, ticks);

/* wait of 0 ms is treated as a 'yield' */
if (duration == 0) {
if (ticks == 0) {
k_yield();
return 0;
}

ticks = _TICK_ALIGN + z_ms_to_ticks(duration);
ticks += _TICK_ALIGN;
expected_wakeup_time = ticks + z_tick_get_32();

/* Spinlock purely for local interrupt locking to prevent us
@@ -952,13 +950,24 @@ s32_t z_impl_k_sleep(s32_t duration)

ticks = expected_wakeup_time - z_tick_get_32();
if (ticks > 0) {
return __ticks_to_ms(ticks);
return ticks;
}
#endif

return 0;
}

s32_t z_impl_k_sleep(int ms)
{
s32_t ticks;

__ASSERT(ms != K_FOREVER, "");

ticks = z_ms_to_ticks(ms);
ticks = z_tick_sleep(ticks);
return __ticks_to_ms(ticks);
}

#ifdef CONFIG_USERSPACE
Z_SYSCALL_HANDLER(k_sleep, duration)
{

0 comments on commit b186303

Please sign in to comment.
You can’t perform that action at this time.