2006-06-17 22:41:10 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2005, Swedish Institute of Computer Science
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \addtogroup process
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Implementation of the Contiki process kernel.
|
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2017-09-04 14:22:34 +00:00
|
|
|
#include "contiki.h"
|
2006-06-17 22:41:10 +00:00
|
|
|
#include "sys/process.h"
|
|
|
|
#include "sys/arg.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pointer to the currently running process structure.
|
|
|
|
*/
|
|
|
|
struct process *process_list = NULL;
|
|
|
|
struct process *process_current = NULL;
|
2017-09-04 14:22:34 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
static process_event_t lastevent;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Structure used for keeping the queue of active events.
|
|
|
|
*/
|
|
|
|
struct event_data {
|
|
|
|
process_event_t ev;
|
|
|
|
process_data_t data;
|
|
|
|
struct process *p;
|
|
|
|
};
|
|
|
|
|
|
|
|
static process_num_events_t nevents, fevent;
|
|
|
|
static struct event_data events[PROCESS_CONF_NUMEVENTS];
|
|
|
|
|
2008-01-23 15:18:06 +00:00
|
|
|
#if PROCESS_CONF_STATS
|
|
|
|
process_num_events_t process_maxevents;
|
|
|
|
#endif
|
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
static volatile unsigned char poll_requested;
|
|
|
|
|
|
|
|
#define PROCESS_STATE_NONE 0
|
2008-01-23 15:18:06 +00:00
|
|
|
#define PROCESS_STATE_RUNNING 1
|
|
|
|
#define PROCESS_STATE_CALLED 2
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
static void call_process(struct process *p, process_event_t ev, process_data_t data);
|
|
|
|
|
2007-03-25 17:16:57 +00:00
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
process_event_t
|
|
|
|
process_alloc_event(void)
|
|
|
|
{
|
|
|
|
return lastevent++;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2014-06-12 11:40:28 +00:00
|
|
|
process_start(struct process *p, process_data_t data)
|
2006-06-17 22:41:10 +00:00
|
|
|
{
|
|
|
|
struct process *q;
|
|
|
|
|
|
|
|
/* First make sure that we don't try to start a process that is
|
|
|
|
already running. */
|
|
|
|
for(q = process_list; q != p && q != NULL; q = q->next);
|
|
|
|
|
|
|
|
/* If we found the process on the process list, we bail out. */
|
|
|
|
if(q == p) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Put on the procs list.*/
|
|
|
|
p->next = process_list;
|
|
|
|
process_list = p;
|
2008-01-23 15:18:06 +00:00
|
|
|
p->state = PROCESS_STATE_RUNNING;
|
2006-06-17 22:41:10 +00:00
|
|
|
PT_INIT(&p->pt);
|
2008-01-23 15:18:06 +00:00
|
|
|
|
2010-09-14 18:55:04 +00:00
|
|
|
PRINTF("process: starting '%s'\n", PROCESS_NAME_STRING(p));
|
2008-01-23 15:18:06 +00:00
|
|
|
|
|
|
|
/* Post a synchronous initialization event to the process. */
|
2014-06-12 11:40:28 +00:00
|
|
|
process_post_synch(p, PROCESS_EVENT_INIT, data);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
exit_process(struct process *p, struct process *fromprocess)
|
|
|
|
{
|
|
|
|
register struct process *q;
|
|
|
|
struct process *old_current = process_current;
|
|
|
|
|
2010-09-14 18:55:04 +00:00
|
|
|
PRINTF("process: exit_process '%s'\n", PROCESS_NAME_STRING(p));
|
2008-01-23 15:18:06 +00:00
|
|
|
|
2010-10-20 22:24:46 +00:00
|
|
|
/* Make sure the process is in the process list before we try to
|
|
|
|
exit it. */
|
|
|
|
for(q = process_list; q != p && q != NULL; q = q->next);
|
|
|
|
if(q == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-23 15:18:06 +00:00
|
|
|
if(process_is_running(p)) {
|
2006-06-17 22:41:10 +00:00
|
|
|
/* Process was running */
|
|
|
|
p->state = PROCESS_STATE_NONE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Post a synchronous event to all processes to inform them that
|
|
|
|
* this process is about to exit. This will allow services to
|
|
|
|
* deallocate state associated with this process.
|
|
|
|
*/
|
|
|
|
for(q = process_list; q != NULL; q = q->next) {
|
|
|
|
if(p != q) {
|
|
|
|
call_process(q, PROCESS_EVENT_EXITED, (process_data_t)p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(p->thread != NULL && p != fromprocess) {
|
|
|
|
/* Post the exit event to the process that is about to exit. */
|
|
|
|
process_current = p;
|
|
|
|
p->thread(&p->pt, PROCESS_EVENT_EXIT, NULL);
|
|
|
|
}
|
|
|
|
}
|
2010-10-20 22:24:46 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
if(p == process_list) {
|
|
|
|
process_list = process_list->next;
|
|
|
|
} else {
|
|
|
|
for(q = process_list; q != NULL; q = q->next) {
|
|
|
|
if(q->next == p) {
|
|
|
|
q->next = p->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-20 21:24:17 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
process_current = old_current;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
call_process(struct process *p, process_event_t ev, process_data_t data)
|
|
|
|
{
|
|
|
|
int ret;
|
2008-01-23 15:18:06 +00:00
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
if(p->state == PROCESS_STATE_CALLED) {
|
2010-09-14 18:55:04 +00:00
|
|
|
printf("process: process '%s' called again with event %d\n", PROCESS_NAME_STRING(p), ev);
|
2008-01-23 15:18:06 +00:00
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
2017-09-04 14:22:34 +00:00
|
|
|
|
2008-01-23 15:18:06 +00:00
|
|
|
if((p->state & PROCESS_STATE_RUNNING) &&
|
2006-06-17 22:41:10 +00:00
|
|
|
p->thread != NULL) {
|
2010-09-14 18:55:04 +00:00
|
|
|
PRINTF("process: calling process '%s' with event %d\n", PROCESS_NAME_STRING(p), ev);
|
2006-06-17 22:41:10 +00:00
|
|
|
process_current = p;
|
2008-01-23 15:18:06 +00:00
|
|
|
p->state = PROCESS_STATE_CALLED;
|
2006-06-17 22:41:10 +00:00
|
|
|
ret = p->thread(&p->pt, ev, data);
|
|
|
|
if(ret == PT_EXITED ||
|
|
|
|
ret == PT_ENDED ||
|
|
|
|
ev == PROCESS_EVENT_EXIT) {
|
|
|
|
exit_process(p, p);
|
2008-01-23 15:18:06 +00:00
|
|
|
} else {
|
|
|
|
p->state = PROCESS_STATE_RUNNING;
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
process_exit(struct process *p)
|
|
|
|
{
|
|
|
|
exit_process(p, PROCESS_CURRENT());
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
process_init(void)
|
|
|
|
{
|
|
|
|
lastevent = PROCESS_EVENT_MAX;
|
|
|
|
|
|
|
|
nevents = fevent = 0;
|
2008-01-23 15:18:06 +00:00
|
|
|
#if PROCESS_CONF_STATS
|
|
|
|
process_maxevents = 0;
|
|
|
|
#endif /* PROCESS_CONF_STATS */
|
2006-06-17 22:41:10 +00:00
|
|
|
|
|
|
|
process_current = process_list = NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*
|
|
|
|
* Call each process' poll handler.
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
do_poll(void)
|
|
|
|
{
|
|
|
|
struct process *p;
|
|
|
|
|
2008-01-24 21:00:51 +00:00
|
|
|
poll_requested = 0;
|
|
|
|
/* Call the processes that needs to be polled. */
|
|
|
|
for(p = process_list; p != NULL; p = p->next) {
|
|
|
|
if(p->needspoll) {
|
|
|
|
p->state = PROCESS_STATE_RUNNING;
|
|
|
|
p->needspoll = 0;
|
|
|
|
call_process(p, PROCESS_EVENT_POLL, NULL);
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
2008-01-24 21:00:51 +00:00
|
|
|
}
|
2006-06-17 22:41:10 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*
|
|
|
|
* Process the next event in the event queue and deliver it to
|
|
|
|
* listening processes.
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
do_event(void)
|
|
|
|
{
|
2016-04-13 16:12:47 +00:00
|
|
|
process_event_t ev;
|
|
|
|
process_data_t data;
|
|
|
|
struct process *receiver;
|
|
|
|
struct process *p;
|
2017-09-04 14:22:34 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
/*
|
|
|
|
* If there are any events in the queue, take the first one and walk
|
|
|
|
* through the list of processes to see if the event should be
|
|
|
|
* delivered to any of them. If so, we call the event handler
|
|
|
|
* function for the process. We only process one event at a time and
|
|
|
|
* call the poll handlers inbetween.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if(nevents > 0) {
|
2017-09-04 14:22:34 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
/* There are events that we should deliver. */
|
|
|
|
ev = events[fevent].ev;
|
2017-09-04 14:22:34 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
data = events[fevent].data;
|
|
|
|
receiver = events[fevent].p;
|
|
|
|
|
|
|
|
/* Since we have seen the new event, we move pointer upwards
|
2015-02-15 16:35:29 +00:00
|
|
|
and decrease the number of events. */
|
2006-06-17 22:41:10 +00:00
|
|
|
fevent = (fevent + 1) % PROCESS_CONF_NUMEVENTS;
|
|
|
|
--nevents;
|
|
|
|
|
|
|
|
/* If this is a broadcast event, we deliver it to all events, in
|
|
|
|
order of their priority. */
|
|
|
|
if(receiver == PROCESS_BROADCAST) {
|
|
|
|
for(p = process_list; p != NULL; p = p->next) {
|
|
|
|
|
|
|
|
/* If we have been requested to poll a process, we do this in
|
|
|
|
between processing the broadcast event. */
|
|
|
|
if(poll_requested) {
|
|
|
|
do_poll();
|
|
|
|
}
|
|
|
|
call_process(p, ev, data);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* This is not a broadcast event, so we deliver it to the
|
|
|
|
specified process. */
|
|
|
|
/* If the event was an INIT event, we should also update the
|
|
|
|
state of the process. */
|
|
|
|
if(ev == PROCESS_EVENT_INIT) {
|
|
|
|
receiver->state = PROCESS_STATE_RUNNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the process actually is running. */
|
|
|
|
call_process(receiver, ev, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
process_run(void)
|
|
|
|
{
|
2008-01-23 15:18:06 +00:00
|
|
|
/* Process poll events. */
|
2006-06-17 22:41:10 +00:00
|
|
|
if(poll_requested) {
|
|
|
|
do_poll();
|
|
|
|
}
|
2008-01-23 15:18:06 +00:00
|
|
|
|
|
|
|
/* Process one event from the queue */
|
2006-06-17 22:41:10 +00:00
|
|
|
do_event();
|
|
|
|
|
|
|
|
return nevents + poll_requested;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2006-08-17 15:39:24 +00:00
|
|
|
process_nevents(void)
|
|
|
|
{
|
|
|
|
return nevents + poll_requested;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2006-06-17 22:41:10 +00:00
|
|
|
process_post(struct process *p, process_event_t ev, process_data_t data)
|
|
|
|
{
|
2016-04-13 16:12:47 +00:00
|
|
|
process_num_events_t snum;
|
2009-02-20 21:24:17 +00:00
|
|
|
|
|
|
|
if(PROCESS_CURRENT() == NULL) {
|
|
|
|
PRINTF("process_post: NULL process posts event %d to process '%s', nevents %d\n",
|
2010-09-14 18:55:04 +00:00
|
|
|
ev,PROCESS_NAME_STRING(p), nevents);
|
2009-02-20 21:24:17 +00:00
|
|
|
} else {
|
|
|
|
PRINTF("process_post: Process '%s' posts event %d to process '%s', nevents %d\n",
|
2010-09-14 18:55:04 +00:00
|
|
|
PROCESS_NAME_STRING(PROCESS_CURRENT()), ev,
|
|
|
|
p == PROCESS_BROADCAST? "<broadcast>": PROCESS_NAME_STRING(p), nevents);
|
2009-02-20 21:24:17 +00:00
|
|
|
}
|
2017-09-04 14:22:34 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
if(nevents == PROCESS_CONF_NUMEVENTS) {
|
2007-03-25 17:16:57 +00:00
|
|
|
#if DEBUG
|
|
|
|
if(p == PROCESS_BROADCAST) {
|
2010-09-14 18:55:04 +00:00
|
|
|
printf("soft panic: event queue is full when broadcast event %d was posted from %s\n", ev, PROCESS_NAME_STRING(process_current));
|
2007-03-25 17:16:57 +00:00
|
|
|
} else {
|
2015-02-15 16:35:29 +00:00
|
|
|
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));
|
2007-03-25 17:16:57 +00:00
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
2006-06-17 22:41:10 +00:00
|
|
|
return PROCESS_ERR_FULL;
|
|
|
|
}
|
2017-09-04 14:22:34 +00:00
|
|
|
|
2010-02-02 21:13:27 +00:00
|
|
|
snum = (process_num_events_t)(fevent + nevents) % PROCESS_CONF_NUMEVENTS;
|
2006-06-17 22:41:10 +00:00
|
|
|
events[snum].ev = ev;
|
|
|
|
events[snum].data = data;
|
|
|
|
events[snum].p = p;
|
|
|
|
++nevents;
|
|
|
|
|
2008-01-23 15:18:06 +00:00
|
|
|
#if PROCESS_CONF_STATS
|
|
|
|
if(nevents > process_maxevents) {
|
|
|
|
process_maxevents = nevents;
|
|
|
|
}
|
|
|
|
#endif /* PROCESS_CONF_STATS */
|
2017-09-04 14:22:34 +00:00
|
|
|
|
2006-06-17 22:41:10 +00:00
|
|
|
return PROCESS_ERR_OK;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
process_post_synch(struct process *p, process_event_t ev, process_data_t data)
|
|
|
|
{
|
|
|
|
struct process *caller = process_current;
|
|
|
|
|
|
|
|
call_process(p, ev, data);
|
|
|
|
process_current = caller;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
process_poll(struct process *p)
|
|
|
|
{
|
|
|
|
if(p != NULL) {
|
2008-01-23 15:18:06 +00:00
|
|
|
if(p->state == PROCESS_STATE_RUNNING ||
|
|
|
|
p->state == PROCESS_STATE_CALLED) {
|
|
|
|
p->needspoll = 1;
|
2006-06-17 22:41:10 +00:00
|
|
|
poll_requested = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-01-23 15:18:06 +00:00
|
|
|
int
|
|
|
|
process_is_running(struct process *p)
|
|
|
|
{
|
|
|
|
return p->state != PROCESS_STATE_NONE;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2006-06-17 22:41:10 +00:00
|
|
|
/** @} */
|