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