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