Contiki 2.5
cooja_mt.h
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: cooja_mt.h,v 1.1 2006/09/29 14:32:38 fros4943 Exp $
34  */
35 /*
36  * This file is ripped from mt.h of the Contiki Multi-threading library.
37  * Fredrik Osterlind <fros@sics.se>
38  */
39 #ifndef __COOJA_MT_H__
40 #define __COOJA_MT_H__
41 
42 #include "contiki.h"
43 
44 
45 /**
46  * An opaque structure that is used for holding the state of a thread.
47  *
48  * The structure should be defined in the "mtarch.h" file. This
49  * structure typically holds the entire stack for the thread.
50  */
51 struct cooja_mtarch_thread;
52 
53 /**
54  * Initialize the architecture specific support functions for the
55  * multi-thread library.
56  *
57  * This function is implemented by the architecture specific functions
58  * for the multi-thread library and is called by the mt_init()
59  * function as part of the initialization of the library. The
60  * mtarch_init() function can be used for, e.g., starting preemtion
61  * timers or other architecture specific mechanisms required for the
62  * operation of the library.
63  */
64 void cooja_mtarch_init(void);
65 
66 /**
67  * Uninstall library and clean up.
68  *
69  */
70 void cooja_mtarch_remove(void);
71 
72 /**
73  * Setup the stack frame for a thread that is being started.
74  *
75  * This function is called by the mt_start() function in order to set
76  * up the architecture specific stack of the thread to be started.
77  *
78  * \param thread A pointer to a struct mtarch_thread for the thread to
79  * be started.
80  *
81  * \param function A pointer to the function that the thread will
82  * start executing the first time it is scheduled to run.
83  *
84  * \param data A pointer to the argument that the function should be
85  * passed.
86  */
87 void cooja_mtarch_start(struct cooja_mtarch_thread *thread,
88  void (* function)(void *data),
89  void *data);
90 
91 /**
92  * Yield the processor.
93  *
94  * This function is called by the mt_yield() function, which is called
95  * from the running thread in order to give up the processor.
96  *
97  */
98 void cooja_mtarch_yield(void);
99 
100 /**
101  * Start executing a thread.
102  *
103  * This function is called from mt_exec() and the purpose of the
104  * function is to start execution of the thread. The function should
105  * switch in the stack of the thread, and does not return until the
106  * thread has explicitly yielded (using mt_yield()) or until it is
107  * preempted.
108  *
109  */
110 void cooja_mtarch_exec(struct cooja_mtarch_thread *thread);
111 
112 
113 /** @} */
114 
115 
116 #include "cooja_mtarch.h"
117 
118 struct cooja_mt_thread {
119  int state;
120  process_event_t *evptr;
121  process_data_t *dataptr;
122  struct cooja_mtarch_thread thread;
123 };
124 
125 /**
126  * No error.
127  *
128  * \hideinitializer
129  */
130 #define MT_OK 1
131 
132 /**
133  * Initializes the multithreading library.
134  *
135  */
136 void cooja_mt_init(void);
137 
138 /**
139  * Uninstalls library and cleans up.
140  *
141  */
142 void cooja_mt_remove(void);
143 
144 
145 /**
146  * Starts a multithreading thread.
147  *
148  * \param thread Pointer to an mt_thread struct that must have been
149  * previously allocated by the caller.
150  *
151  * \param function A pointer to the entry function of the thread that is
152  * to be set up.
153  *
154  * \param data A pointer that will be passed to the entry function.
155  *
156  */
157 void cooja_mt_start(struct cooja_mt_thread *thread, void (* function)(void *), void *data);
158 
159 /**
160  * Execute parts of a thread.
161  *
162  * This function is called by a Contiki process and runs a
163  * thread. The function does not return until the thread has yielded,
164  * or is preempted.
165  *
166  * \note The thread must first be initialized with the mt_init() function.
167  *
168  * \param thread A pointer to a struct mt_thread block that must be
169  * allocated by the caller.
170  *
171  */
172 void cooja_mt_exec(struct cooja_mt_thread *thread);
173 
174 /**
175  * Post an event to a thread.
176  *
177  * This function posts an event to a thread. The thread will be
178  * scheduled if the thread currently is waiting for the posted event
179  * number. If the thread is not waiting for the event, this function
180  * does nothing.
181  *
182  * \note The thread must first be initialized with the mt_init() function.
183  *
184  * \param thread A pointer to a struct mt_thread block that must be
185  * allocated by the caller.
186  *
187  * \param s The event that is posted to the thread.
188  *
189  * \param data An opaque pointer to a user specified structure
190  * containing additonal information, or NULL if no additional
191  * information is needed.
192  */
193 /*void mt_exec_event(struct mt_thread *thread, process_event_t s,
194  process_data_t data);*/
195 
196 /**
197  * Voluntarily give up the processor.
198  *
199  * This function is called by a running thread in order to give up
200  * control of the CPU.
201  *
202  */
203 void cooja_mt_yield(void);
204 
205 /**
206  * Post an event to another process.
207  *
208  * This function is called by a running thread and will emit a signal
209  * to another Contiki process. This will cause the currently executing
210  * thread to yield.
211  *
212  * \param p The process receiving the signal, or PROCESS_BROADCAST
213  * for a broadcast event.
214  *
215  * \param ev The event to be posted.
216  *
217  * \param data A pointer to a message that is to be delivered together
218  * with the signal.
219  *
220  */
221 /*void mt_post(struct process *p, process_event_t ev, process_data_t data);*/
222 
223 /**
224  * Block and wait for an event to occur.
225  *
226  * This function can be called by a running thread in order to block
227  * and wait for an event. The function returns when an event has
228  * occured. The event number and the associated data are placed in the
229  * variables pointed to by the function arguments.
230  *
231  * \param ev A pointer to a process_event_t variable. The variable
232  * will be filled with the number event that woke the thread.
233  *
234  * \param data A pointer to a process_data_t variable. The variable
235  * will be filled with the data associated with the event that woke
236  * the thread.
237  *
238  */
239 /*void mt_wait(process_event_t *ev, process_data_t *data);*/
240 
241 /**
242  * Exit a thread.
243  *
244  * This function is called from within an executing thread in order to
245  * exit the thread. The function never returns.
246  *
247  */
248 void cooja_mt_exit(void);
249 
250 /** @} */
251 /** @} */
252 #endif /* __MT_H__ */