Contiki 3.x
mt.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Adam Dunkels <adam@sics.se>
32  *
33  */
34 
35 /**
36  * \file
37  * Implementation of the archtecture agnostic parts of the preemptive
38  * multithreading library for Contiki.
39  *
40  * \author
41  * Adam Dunkels <adam@sics.se>
42  *
43  */
44 
45 #include "contiki.h"
46 #include "sys/mt.h"
47 #include "sys/cc.h"
48 
49 static struct mt_thread *current;
50 
51 /*--------------------------------------------------------------------------*/
52 void
53 mt_init(void)
54 {
55  mtarch_init();
56 }
57 /*--------------------------------------------------------------------------*/
58 void
59 mt_remove(void)
60 {
61  mtarch_remove();
62 }
63 /*--------------------------------------------------------------------------*/
64 void
65 mt_start(struct mt_thread *thread, void (* function)(void *), void *data)
66 {
67  /* Call the architecture dependant function to set up the processor
68  stack with the correct parameters. */
69  mtarch_start(&thread->thread, function, data);
70 
71  thread->state = MT_STATE_READY;
72 }
73 /*--------------------------------------------------------------------------*/
74 void
75 mt_exec(struct mt_thread *thread)
76 {
77  if(thread->state == MT_STATE_READY) {
78  thread->state = MT_STATE_RUNNING;
79  current = thread;
80  /* Switch context to the thread. The function call will not return
81  until the the thread has yielded, or is preempted. */
82  mtarch_exec(&thread->thread);
83  }
84 }
85 /*--------------------------------------------------------------------------*/
86 void
87 mt_yield(void)
88 {
89  mtarch_pstop();
90  current->state = MT_STATE_READY;
91  current = NULL;
92  /* This function is called from the running thread, and we call the
93  switch function in order to switch the thread to the main Contiki
94  program instead. For us, the switch function will not return
95  until the next time we are scheduled to run. */
96  mtarch_yield();
97 
98 }
99 /*--------------------------------------------------------------------------*/
100 void
101 mt_exit(void)
102 {
103  current->state = MT_STATE_EXITED;
104  current = NULL;
105  mtarch_yield();
106 }
107 /*--------------------------------------------------------------------------*/
108 void
109 mt_stop(struct mt_thread *thread)
110 {
111  mtarch_stop(&thread->thread);
112 }
113 /*--------------------------------------------------------------------------*/
void mtarch_init(void)
Initialize the architecture specific support functions for the multi-thread library.
Definition: mtarch.c:49
void mtarch_yield(void)
Yield the processor.
Definition: mtarch.c:89
Header file for the preemptive multitasking library for Contiki.
void mtarch_remove(void)
Uninstall library and clean up.
Definition: mtarch.c:54
void mtarch_start(struct mtarch_thread *thread, void(*function)(void *data), void *data)
Setup the stack frame for a thread that is being started.
Definition: mtarch.c:59
#define NULL
The null pointer.
void mt_start(struct mt_thread *thread, void(*function)(void *), void *data)
Starts a multithreading thread.
Definition: mt.c:65
Default definitions of C compiler quirk work-arounds.
void mt_remove(void)
Uninstalls library and cleans up.
Definition: mt.c:59
void mt_yield(void)
Voluntarily give up the processor.
Definition: mt.c:87
void mt_init(void)
Initializes the multithreading library.
Definition: mt.c:53
void mt_exec(struct mt_thread *thread)
Execute parts of a thread.
Definition: mt.c:75
void mtarch_exec(struct mtarch_thread *thread)
Start executing a thread.
Definition: mtarch.c:95
void mt_exit(void)
Exit a thread.
Definition: mt.c:101
void mtarch_stop(struct mtarch_thread *thread)
Clean up the stack of a thread.
Definition: mtarch.c:110
void mt_stop(struct mt_thread *thread)
Stop a thread.
Definition: mt.c:109