Commit 9849df8c authored by Carles Cufi's avatar Carles Cufi Committed by Andrew Boie
Browse files

kernel: Disable interrupts after tick calculation in k_sleep()



To guarantee that the compiler does not reorder the execution of
irq_lock() with preceding operations, a volatile qualifier is
placed before the declaration of the ticks variable, which then
ensures that irq_lock() is executed after the tick calculation but
before accessing the ready and timeout queues.
Without the volatile keyword interrupts will be disabled during the
calculation of the ticks, which increases interrupt latency
significantly.

Change-Id: I2da82a1282e344f3b8d69e9457b36a4cb1d9ec18
Signed-off-by: default avatarCarles Cufi <carles.cufi@nordicsemi.no>
parent 30277538
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -295,6 +295,12 @@ void k_yield(void)

void k_sleep(int32_t duration)
{
	/* volatile to guarantee that irq_lock() is executed after ticks is
	 * populated
	 */
	volatile int32_t ticks;
	unsigned int key;

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

@@ -306,8 +312,8 @@ void k_sleep(int32_t duration)
		return;
	}

	int32_t ticks = _TICK_ALIGN + _ms_to_ticks(duration);
	int key = irq_lock();
	ticks = _TICK_ALIGN + _ms_to_ticks(duration);
	key = irq_lock();

	_remove_thread_from_ready_q(_current);
	_add_thread_timeout(_current, NULL, ticks);