Contiki 2.5
process.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005, 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  * @(#)$Id: process.h,v 1.17 2010/09/14 18:55:04 dak664 Exp $
32  */
33 
34 /**
35  * \addtogroup sys
36  * @{
37  */
38 
39 /**
40  * \defgroup process Contiki processes
41  *
42  * A process in Contiki consists of a single \ref pt "protothread".
43  *
44  * @{
45  */
46 
47 /**
48  * \file
49  * Header file for the Contiki process interface.
50  * \author
51  * Adam Dunkels <adam@sics.se>
52  *
53  */
54 #ifndef __PROCESS_H__
55 #define __PROCESS_H__
56 
57 #include "sys/pt.h"
58 #include "sys/cc.h"
59 
60 typedef unsigned char process_event_t;
61 typedef void * process_data_t;
62 typedef unsigned char process_num_events_t;
63 
64 /**
65  * \name Return values
66  * @{
67  */
68 
69 /**
70  * \brief Return value indicating that an operation was successful.
71  *
72  * This value is returned to indicate that an operation
73  * was successful.
74  */
75 #define PROCESS_ERR_OK 0
76 /**
77  * \brief Return value indicating that the event queue was full.
78  *
79  * This value is returned from process_post() to indicate
80  * that the event queue was full and that an event could
81  * not be posted.
82  */
83 #define PROCESS_ERR_FULL 1
84 /* @} */
85 
86 #define PROCESS_NONE NULL
87 
88 #ifndef PROCESS_CONF_NUMEVENTS
89 #define PROCESS_CONF_NUMEVENTS 32
90 #endif /* PROCESS_CONF_NUMEVENTS */
91 
92 #define PROCESS_EVENT_NONE 0x80
93 #define PROCESS_EVENT_INIT 0x81
94 #define PROCESS_EVENT_POLL 0x82
95 #define PROCESS_EVENT_EXIT 0x83
96 #define PROCESS_EVENT_SERVICE_REMOVED 0x84
97 #define PROCESS_EVENT_CONTINUE 0x85
98 #define PROCESS_EVENT_MSG 0x86
99 #define PROCESS_EVENT_EXITED 0x87
100 #define PROCESS_EVENT_TIMER 0x88
101 #define PROCESS_EVENT_COM 0x89
102 #define PROCESS_EVENT_MAX 0x8a
103 
104 #define PROCESS_BROADCAST NULL
105 #define PROCESS_ZOMBIE ((struct process *)0x1)
106 
107 /**
108  * \name Process protothread functions
109  * @{
110  */
111 
112 /**
113  * Define the beginning of a process.
114  *
115  * This macro defines the beginning of a process, and must always
116  * appear in a PROCESS_THREAD() definition. The PROCESS_END() macro
117  * must come at the end of the process.
118  *
119  * \hideinitializer
120  */
121 #define PROCESS_BEGIN() PT_BEGIN(process_pt)
122 
123 /**
124  * Define the end of a process.
125  *
126  * This macro defines the end of a process. It must appear in a
127  * PROCESS_THREAD() definition and must always be included. The
128  * process exits when the PROCESS_END() macro is reached.
129  *
130  * \hideinitializer
131  */
132 #define PROCESS_END() PT_END(process_pt)
133 
134 /**
135  * Wait for an event to be posted to the process.
136  *
137  * This macro blocks the currently running process until the process
138  * receives an event.
139  *
140  * \hideinitializer
141  */
142 #define PROCESS_WAIT_EVENT() PROCESS_YIELD()
143 
144 /**
145  * Wait for an event to be posted to the process, with an extra
146  * condition.
147  *
148  * This macro is similar to PROCESS_WAIT_EVENT() in that it blocks the
149  * currently running process until the process receives an event. But
150  * PROCESS_WAIT_EVENT_UNTIL() takes an extra condition which must be
151  * true for the process to continue.
152  *
153  * \param c The condition that must be true for the process to continue.
154  * \sa PT_WAIT_UNTIL()
155  *
156  * \hideinitializer
157  */
158 #define PROCESS_WAIT_EVENT_UNTIL(c) PROCESS_YIELD_UNTIL(c)
159 
160 /**
161  * Yield the currently running process.
162  *
163  * \hideinitializer
164  */
165 #define PROCESS_YIELD() PT_YIELD(process_pt)
166 
167 /**
168  * Yield the currently running process until a condition occurs.
169  *
170  * This macro is different from PROCESS_WAIT_UNTIL() in that
171  * PROCESS_YIELD_UNTIL() is guaranteed to always yield at least
172  * once. This ensures that the process does not end up in an infinite
173  * loop and monopolizing the CPU.
174  *
175  * \param c The condition to wait for.
176  *
177  * \hideinitializer
178  */
179 #define PROCESS_YIELD_UNTIL(c) PT_YIELD_UNTIL(process_pt, c)
180 
181 /**
182  * Wait for a condition to occur.
183  *
184  * This macro does not guarantee that the process yields, and should
185  * therefore be used with care. In most cases, PROCESS_WAIT_EVENT(),
186  * PROCESS_WAIT_EVENT_UNTIL(), PROCESS_YIELD() or
187  * PROCESS_YIELD_UNTIL() should be used instead.
188  *
189  * \param c The condition to wait for.
190  *
191  * \hideinitializer
192  */
193 #define PROCESS_WAIT_UNTIL(c) PT_WAIT_UNTIL(process_pt, c)
194 #define PROCESS_WAIT_WHILE(c) PT_WAIT_WHILE(process_pt, c)
195 
196 /**
197  * Exit the currently running process.
198  *
199  * \hideinitializer
200  */
201 #define PROCESS_EXIT() PT_EXIT(process_pt)
202 
203 /**
204  * Spawn a protothread from the process.
205  *
206  * \param pt The protothread state (struct pt) for the new protothread
207  * \param thread The call to the protothread function.
208  * \sa PT_SPAWN()
209  *
210  * \hideinitializer
211  */
212 #define PROCESS_PT_SPAWN(pt, thread) PT_SPAWN(process_pt, pt, thread)
213 
214 /**
215  * Yield the process for a short while.
216  *
217  * This macro yields the currently running process for a short while,
218  * thus letting other processes run before the process continues.
219  *
220  * \hideinitializer
221  */
222 #define PROCESS_PAUSE() do { \
223  process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, NULL); \
224  PROCESS_WAIT_EVENT(); \
225 } while(0)
226 
227 /** @} end of protothread functions */
228 
229 /**
230  * \name Poll and exit handlers
231  * @{
232  */
233 /**
234  * Specify an action when a process is polled.
235  *
236  * \note This declaration must come immediately before the
237  * PROCESS_BEGIN() macro.
238  *
239  * \param handler The action to be performed.
240  *
241  * \hideinitializer
242  */
243 #define PROCESS_POLLHANDLER(handler) if(ev == PROCESS_EVENT_POLL) { handler; }
244 
245 /**
246  * Specify an action when a process exits.
247  *
248  * \note This declaration must come immediately before the
249  * PROCESS_BEGIN() macro.
250  *
251  * \param handler The action to be performed.
252  *
253  * \hideinitializer
254  */
255 #define PROCESS_EXITHANDLER(handler) if(ev == PROCESS_EVENT_EXIT) { handler; }
256 
257 /** @} */
258 
259 /**
260  * \name Process declaration and definition
261  * @{
262  */
263 
264 /**
265  * Define the body of a process.
266  *
267  * This macro is used to define the body (protothread) of a
268  * process. The process is called whenever an event occurs in the
269  * system, A process always start with the PROCESS_BEGIN() macro and
270  * end with the PROCESS_END() macro.
271  *
272  * \hideinitializer
273  */
274 #define PROCESS_THREAD(name, ev, data) \
275 static PT_THREAD(process_thread_##name(struct pt *process_pt, \
276  process_event_t ev, \
277  process_data_t data))
278 
279 /**
280  * Declare the name of a process.
281  *
282  * This macro is typically used in header files to declare the name of
283  * a process that is implemented in the C file.
284  *
285  * \hideinitializer
286  */
287 #define PROCESS_NAME(name) extern struct process name
288 
289 /**
290  * Declare a process.
291  *
292  * This macro declares a process. The process has two names: the
293  * variable of the process structure, which is used by the C program,
294  * and a human readable string name, which is used when debugging.
295  * A configuration option allows removal of the readable name to save RAM.
296  *
297  * \param name The variable name of the process structure.
298  * \param strname The string representation of the process' name.
299  *
300  * \hideinitializer
301  */
302 #if PROCESS_CONF_NO_PROCESS_NAMES
303 #define PROCESS(name, strname) \
304  PROCESS_THREAD(name, ev, data); \
305  struct process name = { NULL, \
306  process_thread_##name }
307 #else
308 #define PROCESS(name, strname) \
309  PROCESS_THREAD(name, ev, data); \
310  struct process name = { NULL, strname, \
311  process_thread_##name }
312 #endif
313 
314 /** @} */
315 
316 struct process {
317  struct process *next;
318 #if PROCESS_CONF_NO_PROCESS_NAMES
319 #define PROCESS_NAME_STRING(process) ""
320 #else
321  const char *name;
322 #define PROCESS_NAME_STRING(process) (process)->name
323 #endif
324  PT_THREAD((* thread)(struct pt *, process_event_t, process_data_t));
325  struct pt pt;
326  unsigned char state, needspoll;
327 };
328 
329 /**
330  * \name Functions called from application programs
331  * @{
332  */
333 
334 /**
335  * Start a process.
336  *
337  * \param p A pointer to a process structure.
338  *
339  * \param arg An argument pointer that can be passed to the new
340  * process
341  *
342  */
343 CCIF void process_start(struct process *p, const char *arg);
344 
345 /**
346  * Post an asynchronous event.
347  *
348  * This function posts an asynchronous event to one or more
349  * processes. The handing of the event is deferred until the target
350  * process is scheduled by the kernel. An event can be broadcast to
351  * all processes, in which case all processes in the system will be
352  * scheduled to handle the event.
353  *
354  * \param ev The event to be posted.
355  *
356  * \param data The auxiliary data to be sent with the event
357  *
358  * \param p The process to which the event should be posted, or
359  * PROCESS_BROADCAST if the event should be posted to all processes.
360  *
361  * \retval PROCESS_ERR_OK The event could be posted.
362  *
363  * \retval PROCESS_ERR_FULL The event queue was full and the event could
364  * not be posted.
365  */
366 CCIF int process_post(struct process *p, process_event_t ev, void* data);
367 
368 /**
369  * Post a synchronous event to a process.
370  *
371  * \param p A pointer to the process' process structure.
372  *
373  * \param ev The event to be posted.
374  *
375  * \param data A pointer to additional data that is posted together
376  * with the event.
377  */
378 CCIF void process_post_synch(struct process *p,
379  process_event_t ev, void* data);
380 
381 /**
382  * \brief Cause a process to exit
383  * \param p The process that is to be exited
384  *
385  * This function causes a process to exit. The process can
386  * either be the currently executing process, or another
387  * process that is currently running.
388  *
389  * \sa PROCESS_CURRENT()
390  */
391 CCIF void process_exit(struct process *p);
392 
393 
394 /**
395  * Get a pointer to the currently running process.
396  *
397  * This macro get a pointer to the currently running
398  * process. Typically, this macro is used to post an event to the
399  * current process with process_post().
400  *
401  * \hideinitializer
402  */
403 #define PROCESS_CURRENT() process_current
404 CCIF extern struct process *process_current;
405 
406 /**
407  * Switch context to another process
408  *
409  * This function switch context to the specified process and executes
410  * the code as if run by that process. Typical use of this function is
411  * to switch context in services, called by other processes. Each
412  * PROCESS_CONTEXT_BEGIN() must be followed by the
413  * PROCESS_CONTEXT_END() macro to end the context switch.
414  *
415  * Example:
416  \code
417  PROCESS_CONTEXT_BEGIN(&test_process);
418  etimer_set(&timer, CLOCK_SECOND);
419  PROCESS_CONTEXT_END(&test_process);
420  \endcode
421  *
422  * \param p The process to use as context
423  *
424  * \sa PROCESS_CONTEXT_END()
425  * \sa PROCESS_CURRENT()
426  */
427 #define PROCESS_CONTEXT_BEGIN(p) {\
428 struct process *tmp_current = PROCESS_CURRENT();\
429 process_current = p
430 
431 /**
432  * End a context switch
433  *
434  * This function ends a context switch and changes back to the
435  * previous process.
436  *
437  * \param p The process used in the context switch
438  *
439  * \sa PROCESS_CONTEXT_START()
440  */
441 #define PROCESS_CONTEXT_END(p) process_current = tmp_current; }
442 
443 /**
444  * \brief Allocate a global event number.
445  * \return The allocated event number
446  *
447  * In Contiki, event numbers above 128 are global and may
448  * be posted from one process to another. This function
449  * allocates one such event number.
450  *
451  * \note There currently is no way to deallocate an allocated event
452  * number.
453  */
454 CCIF process_event_t process_alloc_event(void);
455 
456 /** @} */
457 
458 /**
459  * \name Functions called from device drivers
460  * @{
461  */
462 
463 /**
464  * Request a process to be polled.
465  *
466  * This function typically is called from an interrupt handler to
467  * cause a process to be polled.
468  *
469  * \param p A pointer to the process' process structure.
470  */
471 CCIF void process_poll(struct process *p);
472 
473 /** @} */
474 
475 /**
476  * \name Functions called by the system and boot-up code
477  * @{
478  */
479 
480 /**
481  * \brief Initialize the process module.
482  *
483  * This function initializes the process module and should
484  * be called by the system boot-up code.
485  */
486 void process_init(void);
487 
488 /**
489  * Run the system once - call poll handlers and process one event.
490  *
491  * This function should be called repeatedly from the main() program
492  * to actually run the Contiki system. It calls the necessary poll
493  * handlers, and processes one event. The function returns the number
494  * of events that are waiting in the event queue so that the caller
495  * may choose to put the CPU to sleep when there are no pending
496  * events.
497  *
498  * \return The number of events that are currently waiting in the
499  * event queue.
500  */
501 int process_run(void);
502 
503 
504 /**
505  * Check if a process is running.
506  *
507  * This function checks if a specific process is running.
508  *
509  * \param p The process.
510  * \retval Non-zero if the process is running.
511  * \retval Zero if the process is not running.
512  */
513 CCIF int process_is_running(struct process *p);
514 
515 /**
516  * Number of events waiting to be processed.
517  *
518  * \return The number of events that are currently waiting to be
519  * processed.
520  */
521 int process_nevents(void);
522 
523 /** @} */
524 
525 CCIF extern struct process *process_list;
526 
527 #define PROCESS_LIST() process_list
528 
529 #endif /* __PROCESS_H__ */
530 
531 /** @} */
532 /** @} */