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