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