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_MAX 0x8a
104
105#define PROCESS_BROADCAST NULL
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_UNTIL(ev == PROCESS_EVENT_CONTINUE); \
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) \
275static 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, {0}, 0, 0 }
307#else
308#define PROCESS(name, strname) \
309 PROCESS_THREAD(name, ev, data); \
310 struct process name = { NULL, strname, \
311 process_thread_##name, {0}, 0, 0 }
312#endif
313
314/** @} */
315
316struct 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) != NULL ? (process)->name : "")
323#endif
324 PT_THREAD((* thread)(struct pt *, process_event_t, process_data_t));
325 struct pt pt;
326 uint8_t state;
327 bool needspoll;
328};
329
330/**
331 * \name Functions called from application programs
332 * @{
333 */
334
335/**
336 * Start a process.
337 *
338 * \param p A pointer to a process structure.
339 *
340 * \param data An argument pointer that can be passed to the new
341 * process
342 *
343 */
344void process_start(struct process *p, process_data_t data);
345
346/**
347 * Post an asynchronous event.
348 *
349 * This function posts an asynchronous event to one or more
350 * processes. The handing of the event is deferred until the target
351 * process is scheduled by the kernel. An event can be broadcast to
352 * all processes, in which case all processes in the system will be
353 * scheduled to handle the event.
354 *
355 * \param ev The event to be posted.
356 *
357 * \param data The auxiliary data to be sent with the event
358 *
359 * \param p The process to which the event should be posted, or
360 * PROCESS_BROADCAST if the event should be posted to all processes.
361 *
362 * \retval PROCESS_ERR_OK The event could be posted.
363 *
364 * \retval PROCESS_ERR_FULL The event queue was full and the event could
365 * not be posted.
366 */
367int process_post(struct process *p, process_event_t ev, process_data_t data);
368
369/**
370 * Post a synchronous event to a process.
371 *
372 * \param p A pointer to the process' process structure.
373 *
374 * \param ev The event to be posted.
375 *
376 * \param data A pointer to additional data that is posted together
377 * with the event.
378 */
379void process_post_synch(struct process *p,
380 process_event_t ev, process_data_t data);
381
382/**
383 * \brief Cause a process to exit
384 * \param p The process that is to be exited
385 *
386 * This function causes a process to exit. The process can
387 * either be the currently executing process, or another
388 * process that is currently running.
389 *
390 * \sa PROCESS_CURRENT()
391 */
392void process_exit(struct process *p);
393
394
395/**
396 * Get a pointer to the currently running process.
397 *
398 * This macro get a pointer to the currently running
399 * process. Typically, this macro is used to post an event to the
400 * current process with process_post().
401 *
402 * \hideinitializer
403 */
404#define PROCESS_CURRENT() process_current
405extern struct process *process_current;
406
407/**
408 * Switch context to another process
409 *
410 * This function switch context to the specified process and executes
411 * the code as if run by that process. Typical use of this function is
412 * to switch context in services, called by other processes. Each
413 * PROCESS_CONTEXT_BEGIN() must be followed by the
414 * PROCESS_CONTEXT_END() macro to end the context switch.
415 *
416 * Example:
417 \code
418 PROCESS_CONTEXT_BEGIN(&test_process);
419 etimer_set(&timer, CLOCK_SECOND);
420 PROCESS_CONTEXT_END(&test_process);
421 \endcode
422 *
423 * \param p The process to use as context
424 *
425 * \sa PROCESS_CONTEXT_END()
426 * \sa PROCESS_CURRENT()
427 */
428#define PROCESS_CONTEXT_BEGIN(p) {\
429struct process *tmp_current = PROCESS_CURRENT();\
430process_current = p
431
432/**
433 * End a context switch
434 *
435 * This function ends a context switch and changes back to the
436 * previous process.
437 *
438 * \param p The process used in the context switch
439 *
440 * \sa PROCESS_CONTEXT_START()
441 */
442#define PROCESS_CONTEXT_END(p) process_current = tmp_current; }
443
444/**
445 * \brief Allocate a global event number.
446 * \return The allocated event number on success,
447 * or PROCESS_EVENT_NONE on failure.
448 *
449 * In Contiki, event numbers above 128 are global and may
450 * be posted from one process to another. This function
451 * allocates one such event number.
452 *
453 * \note There currently is no way to deallocate an allocated event
454 * number.
455
456 * \note For local events within a module, there is no need to
457 * allocate the event number using this function. One can
458 * simply use an arbitrary event number in the range of
459 * 0 - 127 for this purpose.
460 */
461process_event_t process_alloc_event(void);
462
463/** @} */
464
465/**
466 * \name Functions called from device drivers
467 * @{
468 */
469
470/**
471 * Request a process to be polled.
472 *
473 * This function typically is called from an interrupt handler to
474 * cause a process to be polled.
475 *
476 * \param p A pointer to the process' process structure.
477 */
478void process_poll(struct process *p);
479
480/** @} */
481
482/**
483 * \name Functions called by the system and boot-up code
484 * @{
485 */
486
487/**
488 * \brief Initialize the process module.
489 *
490 * This function initializes the process module and should
491 * be called by the system boot-up code.
492 */
493void process_init(void);
494
495/**
496 * Run the system once - call poll handlers and process one event.
497 *
498 * This function should be called repeatedly from the main() program
499 * to actually run the Contiki system. It calls the necessary poll
500 * handlers, and processes one event. The function returns the number
501 * of events that are waiting in the event queue so that the caller
502 * may choose to put the CPU to sleep when there are no pending
503 * events.
504 *
505 * \return The number of events that are currently waiting in the
506 * event queue.
507 */
508process_num_events_t process_run(void);
509
510
511/**
512 * Check if a process is running.
513 *
514 * This function checks if a specific process is running.
515 *
516 * \param p The process.
517 * \retval true if the process is running.
518 * \retval false if the process is not running.
519 */
520bool process_is_running(struct process *p);
521
522/**
523 * Number of events waiting to be processed.
524 *
525 * \return The number of events that are currently waiting to be
526 * processed.
527 */
528process_num_events_t process_nevents(void);
529
530/** @} */
531
532extern struct process *process_list;
533
534#define PROCESS_LIST() process_list
535
536#endif /* PROCESS_H_ */
537
538/** @} */
539/** @} */
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.