From 81805129f86a68813fa38e81fb1c752e5030daf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Th=C3=A9baudeau?= Date: Wed, 10 Aug 2016 04:01:11 +0200 Subject: [PATCH] mt: Fix preemption MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Preemption was supposed to be supported, but it had no means of safely updating the state of a thread, so mt_exec() could fail to resume a preempted thread. mt_exec() is allowed to be called only from the main Contiki thread, so the mt threads passed to it may be only ready or exited, not running. Consequently, there is no need for a distinction between the ready and running states, so merge them as a started state, which avoids having to update the state of a thread upon preemption. Signed-off-by: Benoît Thébaudeau --- core/sys/mt.c | 9 ++------- core/sys/mt.h | 5 ++--- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/core/sys/mt.c b/core/sys/mt.c index b59d9463d..d5bc10398 100644 --- a/core/sys/mt.c +++ b/core/sys/mt.c @@ -68,14 +68,13 @@ mt_start(struct mt_thread *thread, void (* function)(void *), void *data) stack with the correct parameters. */ mtarch_start(&thread->thread, function, data); - thread->state = MT_STATE_READY; + thread->state = MT_STATE_STARTED; } /*--------------------------------------------------------------------------*/ void mt_exec(struct mt_thread *thread) { - if(thread->state == MT_STATE_READY) { - thread->state = MT_STATE_RUNNING; + if(thread->state == MT_STATE_STARTED) { current = thread; /* Switch context to the thread. The function call will not return until the the thread has yielded, or is preempted. */ @@ -87,14 +86,11 @@ void mt_yield(void) { mtarch_pstop(); - current->state = MT_STATE_READY; - current = NULL; /* This function is called from the running thread, and we call the switch function in order to switch the thread to the main Contiki program instead. For us, the switch function will not return until the next time we are scheduled to run. */ mtarch_yield(); - } /*--------------------------------------------------------------------------*/ void @@ -102,7 +98,6 @@ mt_exit(void) { mtarch_pstop(); current->state = MT_STATE_EXITED; - current = NULL; mtarch_yield(); } /*--------------------------------------------------------------------------*/ diff --git a/core/sys/mt.h b/core/sys/mt.h index 6db215084..b472b3197 100644 --- a/core/sys/mt.h +++ b/core/sys/mt.h @@ -84,9 +84,8 @@ #include "contiki.h" -#define MT_STATE_READY 1 -#define MT_STATE_RUNNING 2 -#define MT_STATE_EXITED 5 +#define MT_STATE_STARTED 1 +#define MT_STATE_EXITED 2 /** * An opaque structure that is used for holding the state of a thread.