Contiki-Inga 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 
88 /**
89  * An opaque structure that is used for holding the state of a thread.
90  *
91  * The structure should be defined in the "mtarch.h" file. This
92  * structure typically holds the entire stack for the thread.
93  */
94 struct mtarch_thread;
95 
96 /**
97  * Initialize the architecture specific support functions for the
98  * multi-thread library.
99  *
100  * This function is implemented by the architecture specific functions
101  * for the multi-thread library and is called by the mt_init()
102  * function as part of the initialization of the library. The
103  * mtarch_init() function can be used for, e.g., starting preemption
104  * timers or other architecture specific mechanisms required for the
105  * operation of the library.
106  */
107 void mtarch_init(void);
108 
109 /**
110  * Uninstall library and clean up.
111  *
112  */
113 void mtarch_remove(void);
114 
115 /**
116  * Setup the stack frame for a thread that is being started.
117  *
118  * This function is called by the mt_start() function in order to set
119  * up the architecture specific stack of the thread to be started.
120  *
121  * \param thread A pointer to a struct mtarch_thread for the thread to
122  * be started.
123  *
124  * \param function A pointer to the function that the thread will
125  * start executing the first time it is scheduled to run.
126  *
127  * \param data A pointer to the argument that the function should be
128  * passed.
129  */
130 void mtarch_start(struct mtarch_thread *thread,
131  void (* function)(void *data),
132  void *data);
133 
134 /**
135  * Start executing a thread.
136  *
137  * This function is called from mt_exec() and the purpose of the
138  * function is to start execution of the thread. The function should
139  * switch in the stack of the thread, and does not return until the
140  * thread has explicitly yielded (using mt_yield()) or until it is
141  * preempted.
142  *
143  * \param thread A pointer to a struct mtarch_thread for the thread to
144  * be executed.
145  *
146  */
147 void mtarch_exec(struct mtarch_thread *thread);
148 
149 /**
150  * Yield the processor.
151  *
152  * This function is called by the mt_yield() function, which is called
153  * from the running thread in order to give up the processor.
154  *
155  */
156 void mtarch_yield(void);
157 
158 /**
159  * Clean up the stack of a thread.
160  *
161  * This function is called by the mt_stop() function in order to clean
162  * up the architecture specific stack of the thread to be stopped.
163  *
164  * \note If the stack is wholly contained in struct mtarch_thread this
165  * function may very well be empty.
166  *
167  * \param thread A pointer to a struct mtarch_thread for the thread to
168  * be stopped.
169  *
170  */
171 void mtarch_stop(struct mtarch_thread *thread);
172 
173 void mtarch_pstart(void);
174 void mtarch_pstop(void);
175 
176 /** @} */
177 
178 
179 #include "mtarch.h"
180 
181 struct mt_thread {
182  int state;
183  process_event_t *evptr;
184  process_data_t *dataptr;
185  struct mtarch_thread thread;
186 };
187 
188 /**
189  * No error.
190  *
191  * \hideinitializer
192  */
193 #define MT_OK 1
194 
195 /**
196  * Initializes the multithreading library.
197  *
198  */
199 void mt_init(void);
200 
201 /**
202  * Uninstalls library and cleans up.
203  *
204  */
205 void mt_remove(void);
206 
207 
208 /**
209  * Starts a multithreading thread.
210  *
211  * \param thread Pointer to an mt_thread struct that must have been
212  * previously allocated by the caller.
213  *
214  * \param function A pointer to the entry function of the thread that is
215  * to be set up.
216  *
217  * \param data A pointer that will be passed to the entry function.
218  *
219  */
220 void mt_start(struct mt_thread *thread, void (* function)(void *), void *data);
221 
222 /**
223  * Execute parts of a thread.
224  *
225  * This function is called by a Contiki process and runs a
226  * thread. The function does not return until the thread has yielded,
227  * or is preempted.
228  *
229  * \note The thread library must first be initialized with the mt_init()
230  * function.
231  *
232  * \param thread A pointer to a struct mt_thread block that must be
233  * allocated by the caller.
234  *
235  */
236 void mt_exec(struct mt_thread *thread);
237 
238 /**
239  * Voluntarily give up the processor.
240  *
241  * This function is called by a running thread in order to give up
242  * control of the CPU.
243  *
244  */
245 void mt_yield(void);
246 
247 /**
248  * Exit a thread.
249  *
250  * This function is called from within an executing thread in order to
251  * exit the thread. The function never returns.
252  *
253  */
254 void mt_exit(void);
255 
256 /**
257  * Stop a thread.
258  *
259  * This function is called by a Contiki process in order to clean up a
260  * thread. The struct mt_thread block may then be discarded by the caller.
261  *
262  * \param thread A pointer to a struct mt_thread block that must be
263  * allocated by the caller.
264  *
265  */
266 void mt_stop(struct mt_thread *thread);
267 
268 /** @} */
269 /** @} */
270 #endif /* MT_H_ */