Jump to content

Process Synchronisation issue


rahuljn

Recommended Posts

Hello experts

 

I have a tricky synchronisation to achieve in one model. Here I have two thread processes, The IInd one is waiting for an event which is notified by Ist thread. Here after the event notification from Ist process, I wait for one delta cycle to allow IInd one to come out of wait and do some stuff before Ist one proceed. But what I am abserving that after the wait in Ist process, in next delta cycle both process becomes ready to run and scheduler may pick them in any order but I want IInd process  to run before Ist one. I may use another wait but is there any another way to achieve this ?

 

Here is one simple examle to show this issue

 

In the below example, I am getting the following output

P3

P6

I am expecting

P3

P6

 

#include "systemc.h"

SC_MODULE(event_trial){
    public:
    sc_event se;
    SC_CTOR(event_trial) {
 SC_THREAD(process1);
 SC_THREAD(process2);
    }

    void process1(){
 wait(SC_ZERO_TIME);
 se.notify(SC_ZERO_TIME);
 wait(SC_ZERO_TIME);
 cout<<"P3"<<endl;
    }

    void process2(){
 wait(se);
 cout<<"P6"<<endl;
    }
};

int sc_main(int , char**){
    event_trial et("et");
    sc_start();
    return 0;
}

 

Can anyone suggest a recommended way to achieve the desired output

 

Thanks

Rahul

 

Link to comment
Share on other sites

Hello experts

 

I have a tricky synchronisation to achieve in one model. Here I have two thread processes, The IInd one is waiting for an event which is notified by Ist thread. Here after the event notification from Ist process, I wait for one delta cycle to allow IInd one to come out of wait and do some stuff before Ist one proceed. But what I am abserving that after the wait in Ist process, in next delta cycle both process becomes ready to run and scheduler may pick them in any order but I want IInd process  to run before Ist one. I may use another wait but is there any another way to achieve this ?

 

Here is one simple examle to show this issue

 

In the below example, I am getting the following output

P3

P6

I am expecting

P3

P6

 

#include "systemc.h"

SC_MODULE(event_trial){

    public:

    sc_event se;

    SC_CTOR(event_trial) {

 SC_THREAD(process1);

 SC_THREAD(process2);

    }

    void process1(){

 wait(SC_ZERO_TIME);

 se.notify(SC_ZERO_TIME);

 wait(SC_ZERO_TIME);

 cout<<"P3"<<endl;

    }

    void process2(){

 wait(se);

 cout<<"P6"<<endl;

    }

};

int sc_main(int , char**){

    event_trial et("et");

    sc_start();

    return 0;

}

 

Can anyone suggest a recommended way to achieve the desired output

 

Thanks

Rahul

Hello Sir,

Please look up the built-in SystemC classes sc_mutex, sc_semaphore,

and use them. These are designed for process synchronization and so

why re-invent the wheel ? Hope that helps.

Link to comment
Share on other sites

sc_event's are the way to guarantee ordering in SystemC. Most channels use events to establish ordering. One or more processes wait on an sc_event, while another generates the sc_event. If you want to guarantee ordering, use events. If you look under the hood of a sc_mutex, sc_semaphore, sc_signal, sc_fifo, or any other properly designed channel, you will find events and flags.

class Top_module {
  sc_event evt1, evt2;
  void thread1(void);
  void thread2(void);
  SC_CTOR(Top_module);
};

Top_module::Top_module(sc_module_name nm) {
  SC_HAS_PROCESS(Top_module);
  SC_THREAD(thread1);
  SC_THREAD(thread2);
}

void Top_module::thread1(void) {
  for(; {
     evt1.notify(SC_ZERO_TIME);
     wait(evt2);
     cout << "Got evt2" << endl;
  }
}

void Top_module::thread2(void) {
  for (int i=0; i!=10; ++i) {
    wait(evt1);
    cout << "Got evt1" << endl;
    evt2.notify(SC_ZERO_TIME);
  }
  sc_stop();
}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...