Jump to content

Problem with immediate notification of event


Recommended Posts

Hi team,

I have a small piece of code inked below. 

#include <systemc.h>
#include <iostream>

using namespace std;

class Test: public sc_module
{
private:
    sc_event e1;
public:
    SC_HAS_PROCESS(Test);
    Test(sc_module_name nm): sc_module(nm), SC_NAMED(e1)
    {
        SC_THREAD(th1);

        SC_THREAD(th2);
        SC_THREAD(th3);
    }

    void th1()
    {
        wait(e1);
        cout << "Test::th1() --> event e1 notified" << endl;
    }

    void th2()
    {
        e1.notify();
        cout << "Test::th2() --> event has been notified" << endl;
    }

    void th3()
    {
        cout << "Test::th3() --> waiting for event e1" << endl;
        wait(e1);
        cout << "Test::th3() --> event e1 notified" << endl;
    }

};

int sc_main(int argc, char* argv[])
{
  Test t("test");
  sc_start(10, SC_NS);
}

The above code prints

Test::th2() --> event has been notified
Test::th3() --> waiting for event e1
Test::th1() --> event e1 notified

I understand that using immediate notification is not encouraged in SystemC (as there are chances that some processes might miss the notified event). But from the above output when event in th2 is notified the SystemC scheduler schedules th3 (after return statement of th2); the statement before wait(e1) is displayed but not after the wait. 

Also if you observe , the statement after wait in th1 is displayed correctly and th1 is scheduled first when simulation starts.

So, my understanding is - Since th1 thread is waiting for e1 before e1.notify() (in th2); th1 is considered to schedule in the same evaluation phase and th3 has missed this event; to resolve this I should replace e1.notify() with e1.notify(SC_ZERO_TIME). 

If my understanding is correct, What is reason behind this behaviour?

Link to comment
Share on other sites

SystemC is a discrete event driven simulator using an approach similar to Verilog, SystemVerilog and VHDL.The coding approach assumes cooperative multitasking. This is very time efficient and more importantly simplifies the modeling aspect if you really understand it, and makes it easier to interoperate with other simulators.

Immediate notification is a unique feature of SystemC that works for some models, but not all. The reasoning behind it is simple efficiency. Delayed notification (i.e., SC_ZERO_TIME) is the safest approach if you are not certain that other processes are designed to work with immediate notification. Both potentially (likely) result in additional delta cycles.

I have an old presentation (with code) that graphically single steps the simulator to make its behavior easier to understand under https://github.com/dcblack/SystemC-Engine/.

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.

 Share

×
×
  • Create New...