Contiki event source

356 Views Asked by At

When writing multiple processes in Contiki, it is usual to poll a process and wait for its exit or a continue signal. However, if I need to wait for a specific processes to end and I have events being triggered by multiple processes, how can I get to the source process which created that event? For example, in the following code, I would like to know which process has just exited so that P3 can move ahead.

Here is a common case:

PROCESS_THREAD(&P1,ev,data){
     PROCESS_BEGIN();
     //Do Something
     PROCESS_END();//Posts an EXITED event
}

PROCESS_THREAD(&P2,ev,data){
     PROCESS_BEGIN();
     //Do Something
     PROCESS_END();//Also posts an EXITED event
}

PROCESS_THREAD(&P3,ev,data){
     PROCESS_BEGIN();
     if(ev==PROCESS_EXITED_EVENT){
     //Do Something only upon the exit of PROCESS 2
     //However this if block works at the exit of either P1 or P2 
     }
     PROCESS_END();
}

There are other ways, I can do a while loop until both process_is_running(&P1) and process_is_running(&P2) are false. But the ev comparison approach with a small addition to the Process handle would be far more elegant and readable.

I could not get any hints from the Contiki source code. Has any one tried an alternative like the one I hinted above?

2

There are 2 best solutions below

0
On

I believe the data argument is a pointer to the process that has exited. So this should work:

if(ev == PROCESS_EXITED_EVENT && data == &P2) {
  printf("process 2 exited\n");
}
0
On

I figured out one more approach. Contiki has a semaphore library, that can be used to wait for a signal on some mutex process. Here is the link.

The idea will be to basically initiate a semaphore at the beginning of P3, make P3 wait for it to be released. I can release it only in P2 and not in P1.

I shall post the code after I test out the solution.