Jump to content

Will SC_THREAD create a real thread?


milesma

Recommended Posts


Hi Guys,

It is my first day to read SystemC tutorial, forgive me if the questions are silly.

I debugged following code in Visual Studio 2008 (64 bit) by setting break point at the start of do_test1 and do_test2,

to my suprise, the code is running in the same thread of the sc_main function.

I didn't debug in Linux environment. However, by searching the source code, I found the "pthread.h" was included by some SystemC library source code.

Question 1: In Windows, will the SC_THREAD create a real thread?

Or it is always in the same thread of sc_main? If this is the case, may I say SC_THREAD is creating a "fake" thread?

Question 2: In Linux, since "pthread.h" is included, will SC_THREAD create a new thread?

Question 3: In Windows and Linux, did I miss some settings to enable the real thread?

========================================

Following code is from this website:

http://www.asic-world.com/systemc/systemc_time4.html#Example_:_sc_event

    #include <systemc.h>

    SC_MODULE (events) {

      sc_in<bool> clock;

      sc_event  e1;

      sc_event  e2;

      void do_test1() {

        

        while (true) {

          // Wait for posedge of clock

          wait();

          cout << "@" << sc_time_stamp() <<" Starting test"<<endl;

          // Wait for posedge of clock

          wait();

          cout << "@" << sc_time_stamp() <<" Triggering e1"<<endl;

          // Trigger event e1

          e1.notify(5,SC_NS);

          // Wait for posedge of clock

          wait();

          // Wait for event e2

          wait(e2);

          cout << "@" << sc_time_stamp() <<" Got Trigger e2"<<endl;

          // Wait for posedge of clock

          wait();

          cout<<"Terminating Simulation"<<endl;

          sc_stop(); // sc_stop triggers end of simulation

        }

      }

      void do_test2() {

        while (true) {

          // Wait for event e2

          wait(e1);

          cout << "@" << sc_time_stamp() <<" Got Trigger e1"<<endl;

          // Wait for 3 posedge of clock

          wait(3);

          cout << "@" << sc_time_stamp() <<" Triggering e2"<<endl;

          // Trigger event e2

          e2.notify();

        }

      }

      SC_CTOR(events) {

        SC_CTHREAD(do_test1,clock.pos());

        SC_CTHREAD(do_test2,clock.pos());

      }

    };

    int sc_main (int argc, char* argv[]) {

      sc_clock clock ("my_clock",1,0.5);

      events  object("events");

        object.clock (clock);

      sc_start();  // Run the simulation till sc_stop is encountered

      return 0;// Terminate simulation

    }

Link to comment
Share on other sites

See IEEE 1666-2011, Section 4.2.12 (emphasis mine):

Since process instances execute without interruption, only a single process instance can be running at any one time, and no other process instance can execute until the currently executing process instance has yielded control to the kernel. A process shall not pre-empt or interrupt the execution of another process. This is known as co-routine semantics or co-operative multitasking.
[...]
An implementation running on a machine that provides hardware support for concurrent processes may permit two or more processes to run concurrently, provided that the behavior appears identical to the co-routine semantics defined in this subclause. In other words, the implementation would be obliged to analyze any dependencies between processes and to constrain their execution to match the co-routine semantics.



In other words, even if an implementation chooses to use "real" OS threads to implement SC_[C]THREAD processes, the usual way to guarantee the above is to ensure mutual exclusion between the thread activations.

Greetings from Oldenburg,
Philipp

Link to comment
Share on other sites

  • 1 year later...
Hi Guys,It is my first day to read SystemC tutorial, forgive me if the questions are silly.I debugged following code in Visual Studio 2008 (64 bit) by setting break point at the start of do_test1 and do_test2,to my suprise, the code is running in the same thread of the sc_main function.I didn't debug in Linux environment. However, by searching the source code, I found the "pthread.h" was included by some SystemC library source code.Question 1: In Windows, will the SC_THREAD create a real thread?             Or it is always in the same thread of sc_main? If this is the case, may I say SC_THREAD is creating a "fake" thread?Question 2: In Linux, since "pthread.h" is included, will SC_THREAD create a new thread?Question 3: In Windows and Linux, did I miss some settings to enable the real thread?========================================Following code is from this website:http://www.asic-world.com/systemc/systemc_time4.html#Example_:_sc_event    #include <systemc.h>    SC_MODULE (events) {      sc_in<bool> clock;      sc_event  e1;      sc_event  e2;      void do_test1() {                while (true) {          // Wait for posedge of clock          wait();          cout << "@" << sc_time_stamp() <<" Starting test"<<endl;          // Wait for posedge of clock          wait();          cout << "@" << sc_time_stamp() <<" Triggering e1"<<endl;          // Trigger event e1          e1.notify(5,SC_NS);          // Wait for posedge of clock          wait();          // Wait for event e2          wait(e2);          cout << "@" << sc_time_stamp() <<" Got Trigger e2"<<endl;          // Wait for posedge of clock          wait();          cout<<"Terminating Simulation"<<endl;          sc_stop(); // sc_stop triggers end of simulation        }      }      void do_test2() {        while (true) {          // Wait for event e2          wait(e1);          cout << "@" << sc_time_stamp() <<" Got Trigger e1"<<endl;          // Wait for 3 posedge of clock          wait(3);          cout << "@" << sc_time_stamp() <<" Triggering e2"<<endl;          // Trigger event e2          e2.notify();        }      }      SC_CTOR(events) {        SC_CTHREAD(do_test1,clock.pos());        SC_CTHREAD(do_test2,clock.pos());      }    };    int sc_main (int argc, char* argv[]) {      sc_clock clock ("my_clock",1,0.5);      events  object("events");        object.clock (clock);      sc_start();  // Run the simulation till sc_stop is encountered      return 0;// Terminate simulation    }

Hello Sir,

First of all, please do not rely too much on the contents of the ASIC

world Web site. The examples were created/tested on SystemC 2.0,

and the author  has unfortunately not upgraded to SystemC 2.3.1 -

new features have been added, older ones deprecated -- e.g., the

SC_THREAD can be used both for clock and non-clock signals,

making SC_CTHREAD redundant.. There are a number of new books

on this subject out there -- use one of these. Also, SystemC kernel

uses its own thread creation/scheduling scheme and moreover this

is a simulation anyway, so it does not matter much if real threads

are created/executed or not. So long the model behaviour confirms

to the expected one, nothing else matters.

Link to comment
Share on other sites

  • 8 months later...

Hi Arya,

 

As far as the your TLM platform  is concerned, there will be no issue. But I did not understand the problem you mentioned about RTL simulation. If two masters try to access the bus at same clock cycle, the arbiter within the bus IP must arbitrate between these two requests and send grant to only one master. Why does it result in simulation error? what is the multi layer matrix ? Did you mean some sort of arbitration only? 

 

If you have used LT coding style(blocking transport), then you may not need any arbitration as it is based on first come first serve. It simply blocks the bus till a response is returned from slave. But it you have used AT coding style, then there is provision to do arbitration if you wish. 

 

Regards,

Dinesh.

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...