adamdunkels | f339ec8 | 2004-03-18 22:13:27 +0000 | [diff] [blame^] | 1 | /** |
| 2 | * \file |
| 3 | * Header file for the preemptive multitasking library for Contiki. |
| 4 | * \author |
| 5 | * Adam Dunkels <adam@sics.se> |
| 6 | * |
| 7 | */ |
| 8 | #ifndef __MT_H__ |
| 9 | #define __MT_H__ |
| 10 | |
| 11 | /** |
| 12 | * \defgroup mt Contiki preemptive multi-threading library |
| 13 | * @{ |
| 14 | * |
| 15 | * The event driven Contiki kernel does not provide multi-threading |
| 16 | * by itself - instead, preemptive multi-threading is implemented |
| 17 | * as a library that optionally can be linked with applications. This |
| 18 | * library constists of two parts: a platform independent part, which is |
| 19 | * the same for all platforms on which Contiki runs, and a platform |
| 20 | * specific part, which must be implemented specifically for the |
| 21 | * platform that the multi-threading library should run. |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * \defgroup mtarch Architecture support for multi-threading |
| 27 | * @{ |
| 28 | * |
| 29 | * The Contiki multi-threading library requires some architecture |
| 30 | * specific support for seting up and switching stacks. This support |
| 31 | * requires three stack manipulation functions to be implemented: |
| 32 | * mtarch_start(), which sets up the stack frame for a new thread, |
| 33 | * mtarch_exec(), which switches in the stack of a thread, and |
| 34 | * mtarch_yield(), which restores the kernel stack from a thread's |
| 35 | * stack. Additionally, two functions for controlling the preemption |
| 36 | * (if any) must be implemented: mtarch_preemption_start() and |
| 37 | * mtarch_preemption_stop(). If no preemption is used, these functions |
| 38 | * can be implemented as empty functions. Finally, the function |
| 39 | * mtarch_init() is called by mt_init(), and can be used for |
| 40 | * initalization of timer interrupts, or any other mechanisms required |
| 41 | * for correct operation of the architecture specific support funcions. |
| 42 | * |
| 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 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 mtarch_init(void); |
| 65 | |
| 66 | /** |
| 67 | * Uninstall library and clean up. |
| 68 | * |
| 69 | */ |
| 70 | void 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 mtarch_start(struct 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 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 mtarch_exec(struct mtarch_thread *thread); |
| 111 | |
| 112 | |
| 113 | void mtarch_pstart(void); |
| 114 | void mtarch_pstop(void); |
| 115 | |
| 116 | /** @} */ |
| 117 | |
| 118 | |
| 119 | #include "mtarch.h" |
| 120 | #include "ek.h" |
| 121 | |
| 122 | struct mt_thread { |
| 123 | int state; |
| 124 | ek_signal_t s; |
| 125 | ek_data_t data; |
| 126 | struct mtarch_thread thread; |
| 127 | }; |
| 128 | |
| 129 | /** |
| 130 | * No error. |
| 131 | * |
| 132 | * \hideinitializer |
| 133 | */ |
| 134 | #define MT_OK 1 |
| 135 | |
| 136 | /** |
| 137 | * Initializes the multithreading library. |
| 138 | * |
| 139 | */ |
| 140 | void mt_init(void); |
| 141 | |
| 142 | /** |
| 143 | * Uninstalls library and cleans up. |
| 144 | * |
| 145 | */ |
| 146 | void mt_remove(void); |
| 147 | |
| 148 | |
| 149 | /** |
| 150 | * Starts a multithreading thread. |
| 151 | * |
| 152 | * \param thread Pointer to an mt_thread struct that must have been |
| 153 | * previously allocated by the caller. |
| 154 | * |
| 155 | * \param function A pointer to the entry function of the thread that is |
| 156 | * to be set up. |
| 157 | * |
| 158 | * \param data A pointer that will be passed to the entry function. |
| 159 | * |
| 160 | */ |
| 161 | void mt_start(struct mt_thread *thread, void (* function)(void *), void *data); |
| 162 | |
| 163 | /** |
| 164 | * Start executing a thread. |
| 165 | * |
| 166 | * This function is called by a Contiki process and starts running a |
| 167 | * thread. The function does not return until the thread has yielded, |
| 168 | * or is preempted. |
| 169 | * |
| 170 | * \note The thread must first be initialized with the mt_init() function. |
| 171 | * |
| 172 | * \param thread A pointer to a struct mt_thread block that must be |
| 173 | * allocated by the caller. |
| 174 | * |
| 175 | */ |
| 176 | void mt_exec(struct mt_thread *thread); |
| 177 | |
| 178 | /** |
| 179 | * Post an event to a thread. |
| 180 | * |
| 181 | * This function posts an event to a thread. The thread will be |
| 182 | * scheduled if the thread currently is waiting for the posted event |
| 183 | * number. If the thread is not waiting for the event, this function |
| 184 | * does nothing. |
| 185 | * |
| 186 | * \note The thread must first be initialized with the mt_init() function. |
| 187 | * |
| 188 | * \param thread A pointer to a struct mt_thread block that must be |
| 189 | * allocated by the caller. |
| 190 | * |
| 191 | * \param s The event that is posted to the thread. |
| 192 | */ |
| 193 | void mt_exec_event(struct mt_thread *thread, ek_signal_t s, ek_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 mt_yield(void); |
| 203 | |
| 204 | /** |
| 205 | * Emit a signal 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 s The signal to be emitted. |
| 212 | * \param data A pointer to a message that is to be delivered together with the signal. |
| 213 | * \param id The process ID of the receiver of the signal, or EK_ID_ALL for a broadcast signal. |
| 214 | */ |
| 215 | void mt_emit(ek_signal_t s, ek_data_t data, ek_id_t id); |
| 216 | |
| 217 | /** |
| 218 | * Block and wait for a specific signal. |
| 219 | * |
| 220 | * This function can be called by a running thread in order to block |
| 221 | * and wait for a specific event. |
| 222 | * |
| 223 | * \note The process must first have registered itself as a listener |
| 224 | * for the signal with mt_listen(). |
| 225 | * |
| 226 | * \param s The signal to wait for, or EK_ID_ALL to wait for any |
| 227 | * signal. |
| 228 | * |
| 229 | */ |
| 230 | ek_data_t mt_wait(ek_signal_t s); |
| 231 | |
| 232 | /** |
| 233 | * Register thread as a listener for a signal. |
| 234 | * |
| 235 | * This function can be called by a running thread in order to |
| 236 | * register the process in which the thread runs as a listener for the |
| 237 | * specified event. |
| 238 | * |
| 239 | * \note This function must be called prior to calling mt_wait(). |
| 240 | * |
| 241 | * \param s The signal to listen to. |
| 242 | */ |
| 243 | void mt_listen(ek_signal_t s); |
| 244 | |
| 245 | /** |
| 246 | * Exit a thread. |
| 247 | * |
| 248 | * This function is called from within an executing thread in order to |
| 249 | * exit the thread. The function never returns. |
| 250 | * |
| 251 | */ |
| 252 | void mt_exit(void); |
| 253 | |
| 254 | /** @} */ |
| 255 | #endif /* __MT_H__ */ |
| 256 | |