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