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 #include <stdint.h>
44 
45 /**
46  * An opaque structure that is used for holding the state of a thread.
47  *
48  * This 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 #ifndef COOJA_MTARCH_STACKSIZE
97 #define COOJA_MTARCH_STACKSIZE 1024
98 #endif /* COOJA_MTARCH_STACKSIZE */
99 
100 struct cooja_mtarch_thread {
101  uintptr_t sp; /* Note: stack pointer must be first var in struct! */
102  uintptr_t stack[COOJA_MTARCH_STACKSIZE];
103 } __attribute__ ((aligned (16)));
104 
105 struct cooja_mt_thread {
106  int state;
107  struct cooja_mtarch_thread thread;
108 };
109 
110 /**
111  * No error.
112  *
113  * \hideinitializer
114  */
115 #define MT_OK 1
116 
117 /**
118  * Starts a multithreading thread.
119  *
120  * \param thread Pointer to an mt_thread struct that must have been
121  * previously allocated by the caller.
122  *
123  * \param function A pointer to the entry function of the thread that is
124  * to be set up.
125  *
126  * \param data A pointer that will be passed to the entry function.
127  *
128  */
129 void cooja_mt_start(struct cooja_mt_thread *thread, void (* function)(void *), void *data);
130 
131 /**
132  * Execute parts of a thread.
133  *
134  * This function is called by a Contiki process and runs a
135  * thread. The function does not return until the thread has yielded,
136  * or is preempted.
137  *
138  * \note The thread must first be initialized with the mt_init() function.
139  *
140  * \param thread A pointer to a struct mt_thread block that must be
141  * allocated by the caller.
142  *
143  */
144 void cooja_mt_exec(struct cooja_mt_thread *thread);
145 
146 /**
147  * Voluntarily give up the processor.
148  *
149  * This function is called by a running thread in order to give up
150  * control of the CPU.
151  *
152  */
153 void cooja_mt_yield(void);
154 
155 /** @} */
156 /** @} */
157 #endif /* MT_H_ */