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

process.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005, 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  * @(#)$Id: process.h,v 1.8 2005/07/04 14:57:05 adam Exp $
00032  */
00033 /**
00034  * \defgroup process Contiki processes
00035  *
00036  * A process in Contiki consists of a single \ref pt protothread.
00037  *
00038  * @{
00039  */
00040 
00041 /**
00042  * \file
00043  * Header file for the Contiki process interface.
00044  * \author
00045  * Adam Dunkels <adam@sics.se>
00046  *
00047  */
00048 #ifndef __PROCESS_H__
00049 #define __PROCESS_H__
00050 
00051 #include "pt.h"
00052 #include "cc.h"
00053 
00054 typedef unsigned char process_event_t;
00055 typedef void *        process_data_t;
00056 typedef unsigned char process_num_events_t;
00057 
00058 #define PROCESS_ERR_OK        0
00059 #define PROCESS_ERR_FULL      1
00060 #define PROCESS_ERR_NOTUNIQUE 2
00061 #define PROCESS_ERR_NOTFOUND  3
00062 
00063 #define PROCESS_NONE          NULL
00064 
00065 #define PROCESS_CONF_NUMEVENTS 32
00066 
00067 #define PROCESS_EVENT_NONE            0x80
00068 #define PROCESS_EVENT_INIT            0x81
00069 #define PROCESS_EVENT_POLL            0x82
00070 #define PROCESS_EVENT_EXIT            0x83
00071 #define PROCESS_EVENT_SERVICE_REMOVED 0x84
00072 #define PROCESS_EVENT_CONTINUE        0x85
00073 #define PROCESS_EVENT_MSG             0x86
00074 #define PROCESS_EVENT_EXITED          0x87
00075 #define PROCESS_EVENT_TIMER           0x88
00076 #define PROCESS_EVENT_MAX             0x89
00077 
00078 #define PROCESS_BROADCAST NULL
00079 
00080 /**
00081  * Define the beginning of a process.
00082  *
00083  * This macro defines the beginning of a process, and must always
00084  * appear in a PROCESS_THREAD() definition. The PROCESS_END() macro
00085  * must come at the end of the process.
00086  *
00087  * \hideinitializer
00088  */
00089 #define PROCESS_BEGIN()             PT_BEGIN(process_pt)
00090 
00091 /**
00092  * Define the end of a process.
00093  *
00094  * This macro defines the end of a process. It must appear in a
00095  * PROCESS_THREAD() definition and must always be included. The
00096  * process exits when the PROCESS_END() macro is reached.
00097  *
00098  * \hideinitializer
00099  */
00100 #define PROCESS_END()               PT_END(process_pt)
00101 
00102 /**
00103  * Wait for an event to be posted to the process.
00104  *
00105  * This macro blocks the currently running process until the process
00106  * receives an event.
00107  *
00108  * \hideinitializer
00109  */
00110 #define PROCESS_WAIT_EVENT()        PROCESS_YIELD()
00111 
00112 /**
00113  * Wait for an event to be posted to the process, with an extra
00114  * condition.
00115  *
00116  * This macro is similar to PROCESS_WAIT_EVENT() in that it blocks the
00117  * currently running process until the process receives an event. But
00118  * PROCESS_WAIT_EVENT_UNTIL() takes an extra condition which must be
00119  * true for the process to continue.
00120  *
00121  * \param c The condition that must be true for the process to continue.
00122  * \sa PT_WAIT_UNTIL()
00123  *
00124  * \hideinitializer
00125  */
00126 #define PROCESS_WAIT_EVENT_UNTIL(c) PROCESS_YIELD_UNTIL(c)
00127 
00128 /**
00129  * Yield the currently running process.
00130  *
00131  * \hideinitializer
00132  */
00133 #define PROCESS_YIELD()             PT_YIELD(process_pt)
00134 
00135 /**
00136  * Yield the currently running process until a condition occurs.
00137  *
00138  * This macro is different from PROCESS_WAIT_UNTIL() in that
00139  * PROCESS_YIELD_UNTIL() is guaranteed to always yield at least
00140  * once. This ensures that the process does not end up in an infinite
00141  * loop and monopolizing the CPU.
00142  *
00143  * \param c The condition to wait for.
00144  *
00145  * \hideinitializer
00146  */
00147 #define PROCESS_YIELD_UNTIL(c)      PT_YIELD_UNTIL(process_pt, c)
00148 
00149 /**
00150  * Wait for a condition to occur.
00151  *
00152  * This macro does not guarantee that the process yields, and should
00153  * therefore be used with care. In most cases, PROCESS_WAIT_EVENT(),
00154  * PROCESS_WAIT_EVENT_UNTIL(), PROCESS_YIELD() or
00155  * PROCESS_YIELD_UNTIL() should be used instead.
00156  *
00157  * \param c The condition to wait for.
00158  *
00159  * \hideinitializer
00160  */
00161 #define PROCESS_WAIT_UNTIL(c)       PT_WAIT_UNTIL(process_pt, c)
00162 
00163 /**
00164  * Exit the currently running process.
00165  *
00166  * \hideinitializer
00167  */
00168 #define PROCESS_EXIT()              PT_EXIT(process_pt)
00169 
00170 /**
00171  * Spawn a protothread from the process.
00172  *
00173  * \param pt The protothread state (struct pt) for the new protothread
00174  * \param thread The call to the protothread function.
00175  * \sa PT_SPAWN()
00176  * 
00177  * \hideinitializer
00178  */
00179 #define PROCESS_SPAWN(pt, thread)   PT_SPAWN(process_pt, pt, thread)
00180 
00181 /**
00182  * Yield the process for a short while.
00183  *
00184  * This macro yields the currently running process for a short while,
00185  * thus letting other processes run before the process continues.
00186  *
00187  * \hideinitializer
00188  */
00189 #define PROCESS_PAUSE()             do {                                \
00190   process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, NULL);        \
00191   PROCESS_WAIT_EVENT();                                                 \
00192 } while(0)
00193 
00194 
00195 /**
00196  * Define the body of a process.
00197  *
00198  * This macro is used to define the body (protothread) of a
00199  * process. The process is called whenever an event occurs in the
00200  * system, A process always start with the PROCESS_BEGIN() macro and
00201  * end with the PROCESS_END() macro.
00202  */
00203 #define PROCESS_THREAD(name, ev, data)                          \
00204 static PT_THREAD(process_thread_##name(struct pt *process_pt,   \
00205                                        process_event_t ev,      \
00206                                        process_data_t data))
00207 
00208 #if PROCESS_LOADABLE
00209 #define PROCESS_LOAD(name) const struct process *process_load = &name;
00210 #else  /* PROCESS_LOADABLE */
00211 #define PROCESS_LOAD(name)
00212 #endif /* PROCESS_LOADABLE */
00213 
00214 /**
00215  * Declare the name of a process.
00216  *
00217  * This macro is typically used in header files to declare the name of
00218  * a process that is implemented in the C file.
00219  *
00220  * \hideinitializer
00221  */
00222 #define PROCESS_NAME(name) extern struct process name
00223 
00224 /**
00225  * Declare a process.
00226  *
00227  * This macro declares a process with the variable name of the
00228  * process, and a textual repressentation of the process (used mainly
00229  * for debugging).
00230  *
00231  * \hideinitializer
00232  */
00233 #if PROCESS_LOADABLE
00234 #define PROCESS(name, strname)                          \
00235   PROCESS_THREAD(name, ev, data);                       \
00236   static struct process name = { NULL, strname,         \
00237                           process_thread_##name };      \
00238   PROCESS_LOAD(name)
00239 #else
00240 #define PROCESS(name, strname)                          \
00241   PROCESS_THREAD(name, ev, data);                       \
00242   struct process name = { NULL, strname,                \
00243                           process_thread_##name };      \
00244   PROCESS_LOAD(name)
00245 #endif
00246 
00247 struct process {
00248   struct process *next;
00249   const char *name;
00250   PT_THREAD((* thread)(struct pt *, process_event_t, process_data_t));
00251   struct pt pt;
00252   unsigned char state;
00253 };
00254 
00255 
00256 void process_start(struct process *p, char *arg);
00257 int  process_post(struct process *p, process_event_t ev, process_data_t data);
00258 void process_post_synch(struct process *p,
00259                         process_event_t ev, process_data_t data);
00260 
00261 void process_poll(struct process *p);
00262 
00263 void process_exit(struct process *p);
00264 
00265 
00266 /**
00267  * Get a pointer to the currently running process.
00268  *
00269  * This macro get a pointer to the currently running
00270  * process. Typically, this macro is used to post an event to the
00271  * current process with process_post().
00272  *
00273  * \hideinitializer
00274  */
00275 #define PROCESS_CURRENT() process_current
00276 extern struct process *process_current;
00277 
00278 #define PROCESS_SET_FLAGS(flags)
00279 #define PROCESS_NO_BROADCAST
00280 
00281 process_event_t process_alloc_event(void);
00282 
00283 void process_init(void);
00284 
00285 /**
00286  * Process one event from the event queue.
00287  *
00288  * This function is called from the system initialization code (the
00289  * main() function) and should never be called from user programs. The
00290  * function processes one event from the kernel's internal event
00291  * queue. The function returns the number of events that are left to
00292  * be processed.
00293  *
00294  * If no events are left to be processed, the CPU can be put into a
00295  * sleep mode.
00296  *
00297  */
00298 int process_run(void);
00299 
00300 extern struct process *process_list;
00301 
00302 #define PROCESS_LIST() process_list
00303 
00304 #endif /* __PROCESS_H__ */
00305 
00306 /** @} */

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