systemc

Event generation in systemc


I am trying to understand event generation in systemc. I found that it depends on the order of thread registration in constructor.

#include <systemc.h>                                                                                                   
    SC_MODULE(EventTest){                                                  
        sc_event ev1;
        void p1(){
           ev1.notify();
           ev1.notify(0, SC_NS);                                                                                    
        }

    void p2(){
        wait(ev1);
        cout<<"ev1 is activated at "<<sc_time_stamp()<<endl;           
        wait(ev1);
        cout<<"ev1-2 is activated at "<<sc_time_stamp()<<endl;         
    }
    SC_CTOR(EventTest){                                                
        SC_THREAD(p1);
        SC_THREAD(p2);                                       
    }   
};      

int sc_main(int argc, char *argv[]){                                   
    EventTest d("d");                                               
    sc_start();
    return 1;
}

output : ev1 is activated at 0 s

if i change the in SC_CTOR to >

SC_THREAD(p2);
 SC_THREAD(p1);

then output is >

ev1 is activated at 0 s
ev1-2 is activated at 0 s

Please someone tell how does the order of registration affect event generation?


Solution

  • Your code is composed of two SystemC processes (SystemC threads or methods are called processes), which are both SystemC threads : p1 and p2. They will be executed both during the first delta cycle of the simulation. However, SystemC standard has no guarantee about the running order of processes in a same delta cycle.

    Finally, in your two cases, p1 is always executed after p2 with the SystemC implementation you used. With another implementation of SystemC, it could be the reverse. You should consider they're executed in parallel at the same simulation time. In that case, both orders are right.

    In the end, it means that your code can lead to a non deterministic behaviour.