Skip to content

Commit

Permalink
mt: Fix preemption
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bthebaudeau committed Nov 24, 2016
1 parent be0a815 commit c6111ce
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
9 changes: 2 additions & 7 deletions mt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -87,22 +86,18 @@ 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
mt_exit(void)
{
mtarch_pstop();
current->state = MT_STATE_EXITED;
current = NULL;
mtarch_yield();
}
/*--------------------------------------------------------------------------*/
Expand Down
5 changes: 2 additions & 3 deletions mt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c6111ce

Please sign in to comment.