Contiki-NG
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  * Setup the stack frame for a thread that is being started.
54  *
55  * This function is called by the mt_start() function in order to set
56  * up the architecture specific stack of the thread to be started.
57  *
58  * \param thread A pointer to a struct mtarch_thread for the thread to
59  * be started.
60  *
61  * \param function A pointer to the function that the thread will
62  * start executing the first time it is scheduled to run.
63  *
64  * \param data A pointer to the argument that the function should be
65  * passed.
66  */
67 void cooja_mtarch_start(struct cooja_mtarch_thread *thread,
68  void (* function)(void *data),
69  void *data);
70 
71 /**
72  * Yield the processor.
73  *
74  * This function is called by the mt_yield() function, which is called
75  * from the running thread in order to give up the processor.
76  *
77  */
78 void cooja_mtarch_yield(void);
79 
80 /**
81  * Start executing a thread.
82  *
83  * This function is called from mt_exec() and the purpose of the
84  * function is to start execution of the thread. The function should
85  * switch in the stack of the thread, and does not return until the
86  * thread has explicitly yielded (using mt_yield()) or until it is
87  * preempted.
88  *
89  */
90 void cooja_mtarch_exec(struct cooja_mtarch_thread *thread);
91 
92 
93 /** @} */
94 
95 
96 #include "cooja_mtarch.h"
97 
98 struct cooja_mt_thread {
99  int state;
100  process_event_t *evptr;
101  process_data_t *dataptr;
102  struct cooja_mtarch_thread thread;
103 };
104 
105 /**
106  * No error.
107  *
108  * \hideinitializer
109  */
110 #define MT_OK 1
111 
112 /**
113  * Starts a multithreading thread.
114  *
115  * \param thread Pointer to an mt_thread struct that must have been
116  * previously allocated by the caller.
117  *
118  * \param function A pointer to the entry function of the thread that is
119  * to be set up.
120  *
121  * \param data A pointer that will be passed to the entry function.
122  *
123  */
124 void cooja_mt_start(struct cooja_mt_thread *thread, void (* function)(void *), void *data);
125 
126 /**
127  * Execute parts of a thread.
128  *
129  * This function is called by a Contiki process and runs a
130  * thread. The function does not return until the thread has yielded,
131  * or is preempted.
132  *
133  * \note The thread must first be initialized with the mt_init() function.
134  *
135  * \param thread A pointer to a struct mt_thread block that must be
136  * allocated by the caller.
137  *
138  */
139 void cooja_mt_exec(struct cooja_mt_thread *thread);
140 
141 /**
142  * Post an event to a thread.
143  *
144  * This function posts an event to a thread. The thread will be
145  * scheduled if the thread currently is waiting for the posted event
146  * number. If the thread is not waiting for the event, this function
147  * does nothing.
148  *
149  * \note The thread must first be initialized with the mt_init() function.
150  *
151  * \param thread A pointer to a struct mt_thread block that must be
152  * allocated by the caller.
153  *
154  * \param s The event that is posted to the thread.
155  *
156  * \param data An opaque pointer to a user specified structure
157  * containing additonal information, or NULL if no additional
158  * information is needed.
159  */
160 /*void mt_exec_event(struct mt_thread *thread, process_event_t s,
161  process_data_t data);*/
162 
163 /**
164  * Voluntarily give up the processor.
165  *
166  * This function is called by a running thread in order to give up
167  * control of the CPU.
168  *
169  */
170 void cooja_mt_yield(void);
171 
172 /** @} */
173 /** @} */
174 #endif /* MT_H_ */