Search the Community
Showing results for tags 'semaphore channel'.
-
Hi, I have some questions on systemC syntax. 1. sc_event_queue. I found a lot of examples for sc_event_queue on some systemC coursewares (even threads on this forum), like below, SC_MODULE(somemod){ sc_event_queue eq; ... void process1(){ while(true){ ... wait(eq); // here got a message: cannot convert from 'sc_core::sc_event_queue' to 'const sc_core::sc_event' , ... } } void process2(){ while(true){ ... eq.notify(5,SC_NS); eq.notify(8,SC_NS); ... } } SC_CTOR(somemod){ ... } }; If I change "wait(eq)" into "wait(eq.default_event())" or just use "wait()" meanwhile add eq into the sensitive list, it will work. Is it correct? 2. sc_semaphore channel. I see an example from the book systemc from the ground up like this, SC_MODULE(gas_station) { sc_semaphore pump(12); // here seems define a function which will return a sc_semaphore instance void customer1_thread { for(;; ) { // wait till tank empty … // find an available gas pump pump.wait(); // fill tank & pay } }; But this seems that it cannot work at all. Then if I use a new form like below it will work, SC_MODULE(gas_station) { sc_semaphore pump; void customer1_thread { for(;; ) { // wait till tank empty … // find an available gas pump pump.wait(); // fill tank & pay } SC_CTOR: pump(12){ ... } }; So if I want to use the form of sc_semaphore pump(12) to the module, what should I do on the constructor? Another question for the semaphore channel: does the sc_semaphore value have a upper limit or what is the size of this channel? Thanks a lot! Wayne
-
Hi everyone, Recently, I am studying systemc and have a problem on semaphore channel. Actually, I found an example of semaphore channel on asic world (http://www.asic-world.com/systemc/channels3.html). This example provides 3 processes (SC_CTHREAD): bus_semaphore(), do_read() and do_write(). like below, ------------------------------------------------------------------------------------------------------- #include <systemc.h> SC_MODULE (sc_semaphore_example) { sc_in<bool> clock; sc_semaphore bus; int cnt; void bus_semaphore() { while (true) { wait(); cout << "@" << sc_time_stamp() <<" Check if semaphore is 0 " << endl; if (bus.get_value() == 0) { cout << "@" << sc_time_stamp() <<" Posting 2 to semaphore " << endl; bus.post(); bus.post(); if (cnt >= 3) { sc_stop(); // sc_stop triggers end of simulation } cnt ++; } } } void do_read() { while (true) { wait(); cout << "@" << sc_time_stamp() <<" Checking semaphore for intance 0"<<endl; // Check if semaphore is available if (bus.trywait() != -1) { cout << "@" << sc_time_stamp() <<" Got semaphore for intance 0"<<endl; wait(2); } } } void do_write() { while (true) { wait(); cout << "@" << sc_time_stamp() <<" Checking semaphore for intance 1"<<endl; // Wait till semaphore is available bus.wait(); cout << "@" << sc_time_stamp() <<" Got semaphore for intance 1"<<endl; wait(3); } } SC_CTOR(sc_semaphore_example) : bus(0){ cnt = 0; SC_CTHREAD(do_read,clock.pos()); SC_CTHREAD(do_write,clock.pos()); SC_CTHREAD(bus_semaphore,clock.pos()); } }; int sc_main (int argc, char* argv[]) { sc_clock clock ("my_clock",1,0.5); sc_semaphore_example object("semaphore"); object.clock (clock.signal()); sc_start(0); // First time called will init schedular sc_start(); // Run the simulation till sc_stop is encountered return 0;// Terminate simulation } ===================== simulation result given ==================== @1 ns Check if semaphore is 0 @1 ns Posting 2 to semaphore @1 ns Checking semaphore for intance 1 @1 ns Got semaphore for intance 1 @1 ns Checking semaphore for intance 0 @1 ns Got semaphore for intance 0 @2 ns Check if semaphore is 0 @2 ns Posting 2 to semaphore @3 ns Check if semaphore is 0 @4 ns Check if semaphore is 0 @4 ns Checking semaphore for intance 0 @4 ns Got semaphore for intance 0 @5 ns Check if semaphore is 0 @5 ns Checking semaphore for intance 1 @5 ns Got semaphore for intance 1 @6 ns Check if semaphore is 0 @6 ns Posting 2 to semaphore @7 ns Check if semaphore is 0 @7 ns Checking semaphore for intance 0 @7 ns Got semaphore for intance 0 @8 ns Check if semaphore is 0 @9 ns Check if semaphore is 0 @9 ns Checking semaphore for intance 1 @9 ns Got semaphore for intance 1 @10 ns Check if semaphore is 0 @10 ns Posting 2 to semaphore @10 ns Checking semaphore for intance 0 @10 ns Got semaphore for intance 0 --------------------------------------------------------------------------------------------------------- There might be a problem about the former two threads. At the first 1ns for this example, the first process bus_semaphore() works and can print out all of the two lines like "@1 ns ....". At the same time in this thread then, the semaphore value (bus) changes into 2 (bus.post()). Then this thread will wait for the next clock posedge. So far everything is good. For the second process, do_read(), also at the 1ns, the first "@" line can be printed out normally, but then what about the expression trywait() in the next if-statement? The first and second process should start to work at the same time (all have wait()at the beginning), that is to say we cannot determine whether the trywait() (in the second process) executes before or after the bus.post() statement (in the first process), so we don't know if the second "@" line of the second process will be printed out. But the simulation result shows that the trywait() will execute after the bus.post() executes such that the second "@..." statement in the second process will be printed out. My question is how can I be sure that the trywait() will execute after the bus.post()'s execution? Shouldn't they execute simultaneously? Thanks a lot! Wayne