55 struct process *process_list =
NULL;
56 struct process *process_current =
NULL;
58 static process_event_t lastevent;
69 static process_num_events_t nevents, fevent;
70 static struct event_data events[PROCESS_CONF_NUMEVENTS];
72 #if PROCESS_CONF_STATS
73 process_num_events_t process_maxevents;
76 static volatile unsigned char poll_requested;
78 #define PROCESS_STATE_NONE 0
79 #define PROCESS_STATE_RUNNING 1
80 #define PROCESS_STATE_CALLED 2
82 static void call_process(
struct process *p, process_event_t ev, process_data_t data);
87 #define PRINTF(...) printf(__VA_ARGS__)
106 for(q = process_list; q != p && q !=
NULL; q = q->next);
113 p->next = process_list;
115 p->state = PROCESS_STATE_RUNNING;
118 PRINTF(
"process: starting '%s'\n", PROCESS_NAME_STRING(p));
125 exit_process(
struct process *p,
struct process *fromprocess)
127 register struct process *q;
128 struct process *old_current = process_current;
130 PRINTF(
"process: exit_process '%s'\n", PROCESS_NAME_STRING(p));
134 for(q = process_list; q != p && q !=
NULL; q = q->next);
141 p->state = PROCESS_STATE_NONE;
148 for(q = process_list; q !=
NULL; q = q->next) {
150 call_process(q, PROCESS_EVENT_EXITED, (process_data_t)p);
154 if(p->thread !=
NULL && p != fromprocess) {
157 p->thread(&p->pt, PROCESS_EVENT_EXIT,
NULL);
161 if(p == process_list) {
162 process_list = process_list->next;
164 for(q = process_list; q !=
NULL; q = q->next) {
172 process_current = old_current;
176 call_process(
struct process *p, process_event_t ev, process_data_t data)
181 if(p->state == PROCESS_STATE_CALLED) {
182 printf(
"process: process '%s' called again with event %d\n", PROCESS_NAME_STRING(p), ev);
186 if((p->state & PROCESS_STATE_RUNNING) &&
188 PRINTF(
"process: calling process '%s' with event %d\n", PROCESS_NAME_STRING(p), ev);
190 p->state = PROCESS_STATE_CALLED;
191 ret = p->thread(&p->pt, ev, data);
192 if(ret == PT_EXITED ||
194 ev == PROCESS_EVENT_EXIT) {
197 p->state = PROCESS_STATE_RUNNING;
211 lastevent = PROCESS_EVENT_MAX;
213 nevents = fevent = 0;
214 #if PROCESS_CONF_STATS
215 process_maxevents = 0;
218 process_current = process_list =
NULL;
232 for(p = process_list; p !=
NULL; p = p->next) {
234 p->state = PROCESS_STATE_RUNNING;
236 call_process(p, PROCESS_EVENT_POLL,
NULL);
249 static process_event_t ev;
250 static process_data_t data;
251 static struct process *receiver;
252 static struct process *p;
265 ev = events[fevent].ev;
267 data = events[fevent].data;
268 receiver = events[fevent].p;
272 fevent = (fevent + 1) % PROCESS_CONF_NUMEVENTS;
277 if(receiver == PROCESS_BROADCAST) {
278 for(p = process_list; p !=
NULL; p = p->next) {
285 call_process(p, ev, data);
292 if(ev == PROCESS_EVENT_INIT) {
293 receiver->state = PROCESS_STATE_RUNNING;
297 call_process(receiver, ev, data);
313 return nevents + poll_requested;
319 return nevents + poll_requested;
323 process_post(
struct process *p, process_event_t ev, process_data_t data)
325 static process_num_events_t snum;
327 PRINTF(
"process_post: NULL process posts event %d to process '%s', nevents %d\n",
328 ev,PROCESS_NAME_STRING(p), nevents);
330 PRINTF(
"process_post: Process '%s' posts event %d to process '%s', nevents %d\n",
332 p == PROCESS_BROADCAST?
"<broadcast>": PROCESS_NAME_STRING(p), nevents);
335 if(nevents == PROCESS_CONF_NUMEVENTS) {
337 if(p == PROCESS_BROADCAST) {
338 printf(
"soft panic: event queue is full when broadcast event %d was posted from %s %u\n", ev, PROCESS_NAME_STRING(process_current),nevents);
340 printf(
"soft panic: event queue is full when event %d was posted to %s frpm %s %u\n", ev, PROCESS_NAME_STRING(p), PROCESS_NAME_STRING(process_current),nevents);
346 snum = (process_num_events_t)(fevent + nevents) % PROCESS_CONF_NUMEVENTS;
347 events[snum].ev = ev;
348 events[snum].data = data;
352 #if PROCESS_CONF_STATS
353 if(nevents > process_maxevents) {
354 process_maxevents = nevents;
364 struct process *caller = process_current;
366 call_process(p, ev, data);
367 process_current = caller;
374 if(p->state == PROCESS_STATE_RUNNING ||
375 p->state == PROCESS_STATE_CALLED) {
385 return p->state != PROCESS_STATE_NONE;