Contiki 3.x
mt.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004, 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 /** \addtogroup sys
36  * @{
37  */
38 
39 /**
40  * \defgroup mt Multi-threading library
41  *
42  * The event driven Contiki kernel does not provide multi-threading
43  * by itself - instead, preemptive multi-threading is implemented
44  * as a library that optionally can be linked with applications. This
45  * library consists of two parts: a platform independent part, which is
46  * the same for all platforms on which Contiki runs, and a platform
47  * specific part, which must be implemented specifically for the
48  * platform that the multi-threading library should run.
49  *
50  * @{
51  */
52 
53 /**
54  * \defgroup mtarch Architecture support for multi-threading
55  * @{
56  *
57  * The Contiki multi-threading library requires some architecture
58  * specific support for setting up and switching stacks. This support
59  * requires four stack manipulation functions to be implemented:
60  * mtarch_start(), which sets up the stack frame for a new thread,
61  * mtarch_exec(), which switches in the stack of a thread,
62  * mtarch_yield(), which restores the kernel stack from a thread's
63  * stack and mtarch_stop(), which cleans up the stack of a thread.
64  * Additionally, two functions for controlling the preemption
65  * (if any) must be implemented: mtarch_pstart() and mtarch_pstop().
66  * If no preemption is used, these functions can be implemented as
67  * empty functions. Finally, the function mtarch_init() is called by
68  * mt_init(), and can be used for initialization of timer interrupts,
69  * or any other mechanisms required for correct operation of the
70  * architecture specific support functions while mtarch_remove() is
71  * called by mt_remove() to clean up those resources.
72  *
73  */
74 
75 /**
76  * \file
77  * Header file for the preemptive multitasking library for Contiki.
78  * \author
79  * Adam Dunkels <adam@sics.se>
80  *
81  */
82 #ifndef MT_H_
83 #define MT_H_
84 
85 #include "contiki.h"
86 
87 #define MT_STATE_READY 1
88 #define MT_STATE_RUNNING 2
89 #define MT_STATE_EXITED 5
90 
91 /**
92  * An opaque structure that is used for holding the state of a thread.
93  *
94  * The structure should be defined in the "mtarch.h" file. This
95  * structure typically holds the entire stack for the thread.
96  */
97 struct mtarch_thread;
98 
99 /**
100  * Initialize the architecture specific support functions for the
101  * multi-thread library.
102  *
103  * This function is implemented by the architecture specific functions
104  * for the multi-thread library and is called by the mt_init()
105  * function as part of the initialization of the library. The
106  * mtarch_init() function can be used for, e.g., starting preemption
107  * timers or other architecture specific mechanisms required for the
108  * operation of the library.
109  */
110 void mtarch_init(void);
111 
112 /**
113  * Uninstall library and clean up.
114  *
115  */
116 void mtarch_remove(void);
117 
118 /**
119  * Setup the stack frame for a thread that is being started.
120  *
121  * This function is called by the mt_start() function in order to set
122  * up the architecture specific stack of the thread to be started.
123  *
124  * \param thread A pointer to a struct mtarch_thread for the thread to
125  * be started.
126  *
127  * \param function A pointer to the function that the thread will
128  * start executing the first time it is scheduled to run.
129  *
130  * \param data A pointer to the argument that the function should be
131  * passed.
132  */
133 void mtarch_start(struct mtarch_thread *thread,
134  void (* function)(void *data),
135  void *data);
136 
137 /**
138  * Start executing a thread.
139  *
140  * This function is called from mt_exec() and the purpose of the
141  * function is to start execution of the thread. The function should
142  * switch in the stack of the thread, and does not return until the
143  * thread has explicitly yielded (using mt_yield()) or until it is
144  * preempted.
145  *
146  * \param thread A pointer to a struct mtarch_thread for the thread to
147  * be executed.
148  *
149  */
150 void mtarch_exec(struct mtarch_thread *thread);
151 
152 /**
153  * Yield the processor.
154  *
155  * This function is called by the mt_yield() function, which is called
156  * from the running thread in order to give up the processor.
157  *
158  */
159 void mtarch_yield(void);
160 
161 /**
162  * Clean up the stack of a thread.
163  *
164  * This function is called by the mt_stop() function in order to clean
165  * up the architecture specific stack of the thread to be stopped.
166  *
167  * \note If the stack is wholly contained in struct mtarch_thread this
168  * function may very well be empty.
169  *
170  * \param thread A pointer to a struct mtarch_thread for the thread to
171  * be stopped.
172  *
173  */
174 void mtarch_stop(struct mtarch_thread *thread);
175 
176 void mtarch_pstart(void);
177 void mtarch_pstop(void);
178 
179 /** @} */
180 
181 
182 #include "mtarch.h"
183 
184 struct mt_thread {
185  int state;
186  process_event_t *evptr;
187  process_data_t *dataptr;
188  struct mtarch_thread thread;
189 };
190 
191 /**
192  * No error.
193  *
194  * \hideinitializer
195  */
196 #define MT_OK 1
197 
198 /**
199  * Initializes the multithreading library.
200  *
201  */
202 void mt_init(void);
203 
204 /**
205  * Uninstalls library and cleans up.
206  *
207  */
208 void mt_remove(void);
209 
210 
211 /**
212  * Starts a multithreading thread.
213  *
214  * \param thread Pointer to an mt_thread struct that must have been
215  * previously allocated by the caller.
216  *
217  * \param function A pointer to the entry function of the thread that is
218  * to be set up.
219  *
220  * \param data A pointer that will be passed to the entry function.
221  *
222  */
223 void mt_start(struct mt_thread *thread, void (* function)(void *), void *data);
224 
225 /**
226  * Execute parts of a thread.
227  *
228  * This function is called by a Contiki process and runs a
229  * thread. The function does not return until the thread has yielded,
230  * or is preempted.
231  *
232  * \note The thread library must first be initialized with the mt_init()
233  * function.
234  *
235  * \param thread A pointer to a struct mt_thread block that must be
236  * allocated by the caller.
237  *
238  */
239 void mt_exec(struct mt_thread *thread);
240 
241 /**
242  * Voluntarily give up the processor.
243  *
244  * This function is called by a running thread in order to give up
245  * control of the CPU.
246  *
247  */
248 void mt_yield(void);
249 
250 /**
251  * Exit a thread.
252  *
253  * This function is called from within an executing thread in order to
254  * exit the thread. The function never returns.
255  *
256  */
257 void mt_exit(void);
258 
259 /**
260  * Stop a thread.
261  *
262  * This function is called by a Contiki process in order to clean up a
263  * thread. The struct mt_thread block may then be discarded by the caller.
264  *
265  * \param thread A pointer to a struct mt_thread block that must be
266  * allocated by the caller.
267  *
268  */
269 void mt_stop(struct mt_thread *thread);
270 
271 /** @} */
272 /** @} */
273 #endif /* MT_H_ */
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
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
void mt_start(struct mt_thread *thread, void(*function)(void *), void *data)
Starts a multithreading thread.
Definition: mt.c:65
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
Copyright (c) 2014, Analog Devices, Inc.
Definition: mtarch.h:42
void mt_stop(struct mt_thread *thread)
Stop a thread.
Definition: mt.c:109