Main Page | Modules | Data Structures | File List | Data Fields | Globals | Examples

mt.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004, Swedish Institute of Computer Science.
00003  * All rights reserved. 
00004  *
00005  * Redistribution and use in source and binary forms, with or without 
00006  * modification, are permitted provided that the following conditions 
00007  * are met: 
00008  * 1. Redistributions of source code must retain the above copyright 
00009  *    notice, this list of conditions and the following disclaimer. 
00010  * 2. Redistributions in binary form must reproduce the above copyright 
00011  *    notice, this list of conditions and the following disclaimer in the 
00012  *    documentation and/or other materials provided with the distribution. 
00013  * 3. Neither the name of the Institute nor the names of its contributors 
00014  *    may be used to endorse or promote products derived from this software 
00015  *    without specific prior written permission. 
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
00027  * SUCH DAMAGE. 
00028  *
00029  * This file is part of the Contiki operating system.
00030  * 
00031  * Author: Adam Dunkels <adam@sics.se>
00032  *
00033  * $Id: mt.h,v 1.3 2005/07/04 13:20:33 adam Exp $
00034  */
00035 
00036 /**
00037  * \defgroup mt Multi-threading library
00038  * @{
00039  *
00040  * The event driven Contiki kernel does not provide multi-threading
00041  * by itself - instead, preemptive multi-threading is implemented
00042  * as a library that optionally can be linked with applications. This
00043  * library constists of two parts: a platform independent part, which is
00044  * the same for all platforms on which Contiki runs, and a platform
00045  * specific part, which must be implemented specifically for the
00046  * platform that the multi-threading library should run.
00047  */
00048 
00049 /**
00050  * \defgroup mtarch Architecture support for multi-threading
00051  * @{
00052  *
00053  * The Contiki multi-threading library requires some architecture
00054  * specific support for seting up and switching stacks. This support
00055  * requires three stack manipulation functions to be implemented:
00056  * mtarch_start(), which sets up the stack frame for a new thread,
00057  * mtarch_exec(), which switches in the stack of a thread, and
00058  * mtarch_yield(), which restores the kernel stack from a thread's
00059  * stack. Additionally, two functions for controlling the preemption
00060  * (if any) must be implemented: mtarch_preemption_start() and
00061  * mtarch_preemption_stop(). If no preemption is used, these functions
00062  * can be implemented as empty functions. Finally, the function
00063  * mtarch_init() is called by mt_init(), and can be used for
00064  * initalization of timer interrupts, or any other mechanisms required
00065  * for correct operation of the architecture specific support funcions.
00066  *
00067  */
00068 
00069 /**
00070  * \file
00071  * Header file for the preemptive multitasking library for Contiki.
00072  * \author
00073  * Adam Dunkels <adam@sics.se>
00074  *
00075  */
00076 #ifndef __MT_H__
00077 #define __MT_H__
00078 
00079 #include "ek.h"
00080 
00081 
00082 /**
00083  * An opaque structure that is used for holding the state of a thread.
00084  *
00085  * The structure should be defined in the "mtarch.h" file. This
00086  * structure typically holds the entire stack for the thread.
00087  */
00088 struct mtarch_thread;
00089 
00090 /**
00091  * Initialize the architecture specific support functions for the
00092  * multi-thread library.
00093  *
00094  * This function is implemented by the architecture specific functions
00095  * for the multi-thread library and is called by the mt_init()
00096  * function as part of the initialization of the library. The
00097  * mtarch_init() function can be used for, e.g., starting preemtion
00098  * timers or other architecture specific mechanisms required for the
00099  * operation of the library.
00100  */
00101 void mtarch_init(void);
00102 
00103 /**
00104  * Uninstall library and clean up.
00105  *
00106  */
00107 void mtarch_remove(void);
00108 
00109 /**
00110  * Setup the stack frame for a thread that is being started.
00111  *
00112  * This function is called by the mt_start() function in order to set
00113  * up the architecture specific stack of the thread to be started.
00114  *
00115  * \param thread A pointer to a struct mtarch_thread for the thread to
00116  * be started.
00117  *
00118  * \param function A pointer to the function that the thread will
00119  * start executing the first time it is scheduled to run.
00120  *
00121  * \param data A pointer to the argument that the function should be
00122  * passed.
00123  */
00124 void mtarch_start(struct mtarch_thread *thread,
00125                   void (* function)(void *data),
00126                   void *data);
00127 
00128 /**
00129  * Yield the processor.
00130  *
00131  * This function is called by the mt_yield() function, which is called
00132  * from the running thread in order to give up the processor.
00133  *
00134  */
00135 void mtarch_yield(void);
00136 
00137 /**
00138  * Start executing a thread.
00139  *
00140  * This function is called from mt_exec() and the purpose of the
00141  * function is to start execution of the thread. The function should
00142  * switch in the stack of the thread, and does not return until the
00143  * thread has explicitly yielded (using mt_yield()) or until it is
00144  * preempted.
00145  *
00146  */
00147 void mtarch_exec(struct mtarch_thread *thread);
00148 
00149 
00150 void mtarch_pstart(void);
00151 void mtarch_pstop(void);
00152 
00153 /** @} */
00154 
00155 
00156 #include "mtarch.h"
00157 #include "ek.h"
00158 
00159 struct mt_thread {
00160   int state;
00161   ek_event_t *evptr;
00162   ek_data_t *dataptr;
00163   struct mtarch_thread thread;
00164 };
00165 
00166 /**
00167  * No error.
00168  *
00169  * \hideinitializer
00170  */
00171 #define MT_OK 1
00172 
00173 /**
00174  * Initializes the multithreading library.
00175  *
00176  */
00177 void mt_init(void);
00178 
00179 /**
00180  * Uninstalls library and cleans up.
00181  *
00182  */
00183 void mt_remove(void);
00184 
00185 
00186 /**
00187  * Starts a multithreading thread.
00188  *
00189  * \param thread Pointer to an mt_thread struct that must have been
00190  * previously allocated by the caller.
00191  *
00192  * \param function A pointer to the entry function of the thread that is
00193  * to be set up.
00194  *
00195  * \param data A pointer that will be passed to the entry function.
00196  *
00197  */
00198 void mt_start(struct mt_thread *thread, void (* function)(void *), void *data);
00199 
00200 /**
00201  * Start executing a thread.
00202  *
00203  * This function is called by a Contiki process and starts running a
00204  * thread. The function does not return until the thread has yielded,
00205  * or is preempted.
00206  *
00207  * \note The thread must first be initialized with the mt_init() function.
00208  *
00209  * \param thread A pointer to a struct mt_thread block that must be
00210  * allocated by the caller.
00211  *
00212  */
00213 void mt_exec(struct mt_thread *thread);
00214 
00215 /**
00216  * Post an event to a thread.
00217  *
00218  * This function posts an event to a thread. The thread will be
00219  * scheduled if the thread currently is waiting for the posted event
00220  * number. If the thread is not waiting for the event, this function
00221  * does nothing.
00222  *
00223  * \note The thread must first be initialized with the mt_init() function.
00224  *
00225  * \param thread A pointer to a struct mt_thread block that must be
00226  * allocated by the caller.
00227  *
00228  * \param s The event that is posted to the thread.
00229  *
00230  * \param data An opaque pointer to a user specified structure
00231  * containing additonal information, or NULL if no additional
00232  * information is needed.
00233  */
00234 void mt_exec_event(struct mt_thread *thread, ek_event_t s, ek_data_t data);
00235 
00236 /**
00237  * Voluntarily give up the processor.
00238  *
00239  * This function is called by a running thread in order to give up
00240  * control of the CPU.
00241  *
00242  */
00243 void mt_yield(void);
00244 
00245 /**
00246  * Emit a signal to another process.
00247  *
00248  * This function is called by a running thread and will emit a signal
00249  * to another Contiki process. This will cause the currently executing
00250  * thread to yield.
00251  *
00252  * \param s The signal to be emitted.
00253  * \param data A pointer to a message that is to be delivered together with the signal.
00254  * \param id The process ID of the receiver of the signal, or EK_ID_ALL for a broadcast signal.
00255  */
00256 void mt_post(ek_id_t id, ek_event_t s, ek_data_t data);
00257 
00258 /**
00259  * Block and wait for an event to occur.
00260  *
00261  * This function can be called by a running thread in order to block
00262  * and wait for an event. The function returns when an event has
00263  * occured. The event number and the associated data are placed in the
00264  * variables pointed to by the function arguments.
00265  * 
00266  */
00267 void mt_wait(ek_event_t *s, ek_data_t *data);
00268 
00269 /**
00270  * Exit a thread.
00271  *
00272  * This function is called from within an executing thread in order to
00273  * exit the thread. The function never returns.
00274  *
00275  */
00276 void mt_exit(void);
00277 
00278 /**
00279  * \defgroup mtp Multi-threading library convenience functions
00280  * @{
00281  *
00282  * The Contiki multi-threading library has an interface that might be
00283  * hard to use. Therefore, the mtp module provides a simpler
00284  * interface.
00285  *
00286  * Example:
00287 \code
00288 static void
00289 example_thread_code(void *data)
00290 {
00291   while(1) {
00292     printf("Test\n");
00293     mt_yield();
00294   }
00295 } 
00296 MTP(example_thread, "Example thread", p1, t1, t1_idle);
00297 
00298 int
00299 main(int argc, char *argv[])
00300 {
00301   mtp_start(&example_thread, example_thread_code, NULL);
00302 }
00303 \endcode
00304 *
00305 */
00306 
00307 #include "mt.h"
00308 
00309 /**
00310  * Declare a thread.
00311  *
00312  * This macro is used to covneniently declare a thread, and the
00313  * process in which the thread should execute. The names of the
00314  * variables provided to the macro should be chosen to be unique
00315  * within the file that the thread is used.
00316  *
00317  * Example:
00318 \code
00319 MTP(example_thread, example_proc, "Example thread");
00320 \endcode
00321  *
00322  * \param thread The variable name of the thread.
00323  *
00324  * \param proc The variable name of the process containing the thread.
00325  *
00326  * \param name A string that specifies the user-visible name of the
00327  * process in which the thread will run.
00328  *
00329  * \hideinitializer
00330  */
00331 #define MTP(thread, proc, name) \
00332 extern struct mtp_thread thread; \
00333 EK_PROCESS(proc, name, EK_PRIO_NORMAL, mtp_eventhandler, \
00334            NULL, (void *)&thread); \
00335 static struct mtp_thread thread = {&proc}
00336 
00337 struct mtp_thread {
00338   struct ek_proc *p;
00339   struct mt_thread t;
00340 };
00341 
00342 /**
00343  * Start a thread.
00344  *
00345  * This function starts the process in which the thread is to run, and
00346  * also sets up the thread to run within the process. The function
00347  * should be passed variable names declared with the MTP() macro.
00348  *
00349  * Example:
00350 \code
00351 mtp_start(&t, example_thread_code, NULL);
00352 \endcode
00353  * \param t A pointer to a thread structure previously declared with MTP().
00354  *
00355  * \param function A pointer to the function that the thread should
00356  * start executing.
00357  *
00358  * \param data A pointer that the function should be passed when first
00359  * invocated.
00360  */
00361 void mtp_start(struct mtp_thread *t,
00362                void (* function)(void *), void *data);
00363 
00364 void mtp_exit(void);
00365 
00366 
00367 void mtp_eventhandler(ek_event_t ev, ek_data_t data);
00368 
00369 /** @} */
00370 /** @} */
00371 #endif /* __MT_H__ */
00372 

Generated on Wed Jul 6 01:18:59 2005 for Contiki/ESB by doxygen 1.3.6