Contiki-NG
Loading...
Searching...
No Matches
process.c
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 process
35 * @{
36 */
37
38/**
39 * \file
40 * Implementation of the Contiki process kernel.
41 * \author
42 * Adam Dunkels <adam@sics.se>
43 *
44 */
45
46#include <stdio.h>
47
48#include "contiki.h"
49#include "sys/process.h"
50
51#include "sys/log.h"
52#define LOG_MODULE "Process"
53#define LOG_LEVEL LOG_LEVEL_SYS
54
55/*
56 * The process_num_events_t type is an uint8_t. It must be able to store
57 * the value of PROCESS_CONF_NUMEVENTS + 1, for the additional
58 * boolean value indicating whether a poll has been requested.
59 */
60static_assert(PROCESS_CONF_NUMEVENTS > 0 && PROCESS_CONF_NUMEVENTS <= 128,
61 "PROCESS_CONF_NUMEVENTS must be a positive value of at most 128.");
62
63/* Require that PROCESS_CONF_NUMEVENTS is a power of 2 to allow
64 optimization of modulo operations. */
65static_assert(!(PROCESS_CONF_NUMEVENTS & (PROCESS_CONF_NUMEVENTS - 1)),
66 "PROCESS_CONF_NUMEVENTS must be a power of 2.");
67
68/*
69 * A configurable function called after a process poll been requested.
70 */
71#ifdef PROCESS_CONF_POLL_REQUESTED
72#define PROCESS_POLL_REQUESTED PROCESS_CONF_POLL_REQUESTED
73void PROCESS_POLL_REQUESTED(void);
74#else
75#define PROCESS_POLL_REQUESTED()
76#endif
77
78/*
79 * Pointer to the currently running process structure.
80 */
81struct process *process_list;
82struct process *process_current;
83
84static process_event_t lastevent;
85
86/*
87 * Structure used for keeping the queue of active events.
88 */
89struct event_data {
90 process_data_t data;
91 struct process *p;
92 process_event_t ev;
93};
94
95static process_num_events_t nevents, fevent;
96static struct event_data events[PROCESS_CONF_NUMEVENTS];
97
98#if PROCESS_CONF_STATS
99process_num_events_t process_maxevents;
100#endif
101
102static volatile bool poll_requested;
103
104#define PROCESS_STATE_NONE 0
105#define PROCESS_STATE_RUNNING 1
106#define PROCESS_STATE_CALLED 2
107
108static void call_process(struct process *p, process_event_t ev, process_data_t data);
109/*---------------------------------------------------------------------------*/
110process_event_t
112{
113 if(lastevent == (process_event_t)~0U) {
114 LOG_WARN("Cannot allocate another event number\n");
115 return PROCESS_EVENT_NONE;
116 }
117 return lastevent++;
118}
119/*---------------------------------------------------------------------------*/
120void
121process_start(struct process *p, process_data_t data)
122{
123 struct process *q;
124
125 /* First make sure that we don't try to start a process that is
126 already running. */
127 for(q = process_list; q != p && q != NULL; q = q->next);
128
129 /* If we found the process on the process list, we bail out. */
130 if(q == p) {
131 return;
132 }
133 /* Put on the procs list.*/
134 p->next = process_list;
135 process_list = p;
136 p->state = PROCESS_STATE_RUNNING;
137 PT_INIT(&p->pt);
138
139 LOG_DBG("starting '%s'\n", PROCESS_NAME_STRING(p));
140
141 /* Post a synchronous initialization event to the process. */
142 process_post_synch(p, PROCESS_EVENT_INIT, data);
143}
144/*---------------------------------------------------------------------------*/
145static void
146exit_process(struct process *p, const struct process *fromprocess)
147{
148 register struct process *q;
149 struct process *old_current = process_current;
150
151 LOG_DBG("exit_process '%s'\n", PROCESS_NAME_STRING(p));
152
153 /* Make sure the process is in the process list before we try to
154 exit it. */
155 for(q = process_list; q != p && q != NULL; q = q->next);
156 if(q == NULL) {
157 return;
158 }
159
160 if(process_is_running(p)) {
161 /* Process was running */
162
163 if(p->thread != NULL && p != fromprocess) {
164 /* Post the exit event to the process that is about to exit. */
165 process_current = p;
166 p->thread(&p->pt, PROCESS_EVENT_EXIT, NULL);
167 }
168 }
169
170 if(p == process_list) {
171 process_list = process_list->next;
172 } else {
173 for(q = process_list; q != NULL; q = q->next) {
174 if(q->next == p) {
175 q->next = p->next;
176 break;
177 }
178 }
179 }
180
181 if(process_is_running(p)) {
182 /* Process was running */
183 p->state = PROCESS_STATE_NONE;
184
185 /*
186 * Post a synchronous event to all processes to inform them that
187 * this process is about to exit. This will allow services to
188 * deallocate state associated with this process.
189 */
190 for(q = process_list; q != NULL; q = q->next) {
191 call_process(q, PROCESS_EVENT_EXITED, (process_data_t)p);
192 }
193 }
194
195 process_current = old_current;
196}
197/*---------------------------------------------------------------------------*/
198static void
199call_process(struct process *p, process_event_t ev, process_data_t data)
200{
201 if(p->state == PROCESS_STATE_CALLED) {
202 LOG_DBG("process '%s' called again with event %d\n",
203 PROCESS_NAME_STRING(p), ev);
204 }
205
206 if((p->state & PROCESS_STATE_RUNNING) &&
207 p->thread != NULL) {
208 LOG_DBG("calling process '%s' with event %d\n",
209 PROCESS_NAME_STRING(p), ev);
210 process_current = p;
211 p->state = PROCESS_STATE_CALLED;
212 int ret = p->thread(&p->pt, ev, data);
213 if(ret == PT_EXITED || ret == PT_ENDED || ev == PROCESS_EVENT_EXIT) {
214 exit_process(p, p);
215 } else {
216 p->state = PROCESS_STATE_RUNNING;
217 }
218 }
219}
220/*---------------------------------------------------------------------------*/
221void
222process_exit(struct process *p)
223{
224 exit_process(p, PROCESS_CURRENT());
225}
226/*---------------------------------------------------------------------------*/
227void
229{
230 lastevent = PROCESS_EVENT_MAX;
231}
232/*---------------------------------------------------------------------------*/
233/*
234 * Call each process' poll handler.
235 */
236/*---------------------------------------------------------------------------*/
237static void
238do_poll(void)
239{
240 poll_requested = false;
241 /* Call the processes that needs to be polled. */
242 for(struct process *p = process_list; p != NULL; p = p->next) {
243 if(p->needspoll) {
244 p->state = PROCESS_STATE_RUNNING;
245 p->needspoll = false;
246 call_process(p, PROCESS_EVENT_POLL, NULL);
247 }
248 }
249}
250/*---------------------------------------------------------------------------*/
251/*
252 * Process the next event in the event queue and deliver it to
253 * listening processes.
254 */
255/*---------------------------------------------------------------------------*/
256static void
257do_event(void)
258{
259 /*
260 * If there are any events in the queue, take the first one and walk
261 * through the list of processes to see if the event should be
262 * delivered to any of them. If so, we call the event handler
263 * function for the process. We only process one event at a time and
264 * call the poll handlers inbetween.
265 */
266 if(nevents > 0) {
267
268 /* There are events that we should deliver. */
269 process_event_t ev = events[fevent].ev;
270 process_data_t data = events[fevent].data;
271 struct process *receiver = events[fevent].p;
272
273 /* Since we have seen the new event, we move pointer upwards
274 and decrease the number of events. */
275 fevent = (fevent + 1) % PROCESS_CONF_NUMEVENTS;
276 --nevents;
277
278 /* If this is a broadcast event, we deliver it to all events, in
279 order of their priority. */
280 if(receiver == PROCESS_BROADCAST) {
281 for(struct process *p = process_list; p != NULL; p = p->next) {
282 /* If we have been requested to poll a process, we do this in
283 between processing the broadcast event. */
284 if(poll_requested) {
285 do_poll();
286 }
287 call_process(p, ev, data);
288 }
289 } else {
290 /* This is not a broadcast event, so we deliver it to the
291 specified process. */
292 /* If the event was an INIT event, we should also update the
293 state of the process. */
294 if(ev == PROCESS_EVENT_INIT) {
295 receiver->state = PROCESS_STATE_RUNNING;
296 }
297
298 /* Make sure that the process actually is running. */
299 call_process(receiver, ev, data);
300 }
301 }
302}
303/*---------------------------------------------------------------------------*/
304process_num_events_t
306{
307 /* Process poll events. */
308 if(poll_requested) {
309 do_poll();
310 }
311
312 /* Process one event from the queue */
313 do_event();
314
315 return nevents + poll_requested;
316}
317/*---------------------------------------------------------------------------*/
318process_num_events_t
320{
321 return nevents + poll_requested;
322}
323/*---------------------------------------------------------------------------*/
324int
325process_post(struct process *p, process_event_t ev, process_data_t data)
326{
327 if(nevents == PROCESS_CONF_NUMEVENTS) {
328 LOG_WARN("Cannot post event %d to %s from %s because the queue is full\n",
329 ev,
330 p == PROCESS_BROADCAST ? "<broadcast>" : PROCESS_NAME_STRING(p),
331 PROCESS_NAME_STRING(process_current));
332 return PROCESS_ERR_FULL;
333 }
334
335 LOG_DBG("Process '%s' posts event %d to process '%s', nevents %d\n",
336 PROCESS_NAME_STRING(PROCESS_CURRENT()),
337 ev, p == PROCESS_BROADCAST ? "<broadcast>" : PROCESS_NAME_STRING(p),
338 nevents);
339
340 process_num_events_t snum =
341 (process_num_events_t)(fevent + nevents) % PROCESS_CONF_NUMEVENTS;
342 events[snum].ev = ev;
343 events[snum].data = data;
344 events[snum].p = p;
345 ++nevents;
346
347#if PROCESS_CONF_STATS
348 if(nevents > process_maxevents) {
349 process_maxevents = nevents;
350 }
351#endif /* PROCESS_CONF_STATS */
352
353 return PROCESS_ERR_OK;
354}
355/*---------------------------------------------------------------------------*/
356void
357process_post_synch(struct process *p, process_event_t ev, process_data_t data)
358{
359 struct process *caller = process_current;
360
361 call_process(p, ev, data);
362 process_current = caller;
363}
364/*---------------------------------------------------------------------------*/
365void
366process_poll(struct process *p)
367{
368 if(p != NULL &&
369 (p->state == PROCESS_STATE_RUNNING || p->state == PROCESS_STATE_CALLED)) {
370 p->needspoll = true;
371 poll_requested = true;
372 PROCESS_POLL_REQUESTED();
373 }
374}
375/*---------------------------------------------------------------------------*/
376bool
377process_is_running(struct process *p)
378{
379 return p->state != PROCESS_STATE_NONE;
380}
381/*---------------------------------------------------------------------------*/
382/** @} */
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
#define PROCESS_CURRENT()
Get a pointer to the currently running process.
Definition process.h:404
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
#define PROCESS_ERR_OK
Return value indicating that an operation was successful.
Definition process.h:76
process_num_events_t process_nevents(void)
Number of events waiting to be processed.
Definition process.c:319
#define PROCESS_ERR_FULL
Return value indicating that the event queue was full.
Definition process.h:84
void process_poll(struct process *p)
Request a process to be polled.
Definition process.c:366
#define PT_INIT(pt)
Initialize a protothread.
Definition pt.h:245
Header file for the logging system.
Header file for the Contiki process interface.