quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif sc_semaphore

sc_semaphore is a predefined primitive channel intended to model the behavior of a software semaphore as used to provide limited concurrent access to a shared resource. A semaphore has an integer value, the semaphore value, which is set to the permitted number of concurrent accesses when the semaphore is constructed.

   

space.gif

sc_semaphore has following predefined methods.

   

space.gif

  • int wait() : If the semaphore value is equal to 0, member function wait shall suspend until the semaphore value is incremented (by another process), at which point it shall resume and attempt to decrement the semaphore value.
  • int trywait() : If the semaphore value is equal to 0, member function trywait shall immediately return the value -1 without modifying the semaphore value.
  • int post(); Member function post shall increment the semaphore value. If processes exist that are suspended and are waiting for the semaphore value to be incremented, exactly one of these processes shall be permitted to decrement the semaphore value (the choice of process being non-deterministic) while the remaining processes shall suspend again.
  • int get_value() :Member function get_value shall return the semaphore value.
  • char* kind() : Return string "sc_semaphore"
   

space.gif

   

space.gif

  ../images/main/bullet_star_pink.gif Example : sc_semaphore
   

space.gif


  1 #include <systemc.h>
  2 
  3 SC_MODULE (sc_semaphore_example) {
  4   sc_in<bool> clock;
  5 
  6   sc_semaphore   bus;
  7   int     cnt;
  8 
  9   void bus_semaphore() {
 10     while (true) {
 11       wait();
 12       cout << "@" << sc_time_stamp() <<" Check if semaphore is 0 " << endl;
 13       if (bus.get_value() == 0) {
 14         cout << "@" << sc_time_stamp() <<" Posting 2 to semaphore " << endl;
 15         bus.post();
 16         bus.post();
 17         if (cnt >= 3) {
 18           sc_stop(); // sc_stop triggers end of simulation
 19         }
 20         cnt ++;
 21       }
 22     }
 23   }
 24 
 25   void do_read() {
 26     while (true) {
 27       wait();
 28       cout << "@" << sc_time_stamp() <<" Checking semaphore for intance 0"<<endl;
 29       // Check if semaphore is available
 30       if (bus.trywait()  ! = -1) {
 31         cout << "@" << sc_time_stamp() <<" Got semaphore for intance 0"<<endl;
 32         wait(2);
 33       }
 34     }
 35   }
 36 
 37   void do_write() {
 38     while (true) {
 39       wait();
 40       cout << "@" << sc_time_stamp() <<" Checking semaphore for intance 1"<<endl;
 41       // Wait till semaphore is available
 42       bus.wait();
 43       cout << "@" << sc_time_stamp() <<" Got semaphore for intance 1"<<endl;
 44       wait(3);
 45     }
 46   }
 47 
 48   SC_CTOR(sc_semaphore_example) : bus(0){
 49     cnt = 0;
 50     SC_CTHREAD(do_read,clock.pos());
 51     SC_CTHREAD(do_write,clock.pos());
 52     SC_CTHREAD(bus_semaphore,clock.pos());
 53   }
 54 }; 
 55 
 56 int sc_main (int argc, char* argv[]) {
 57   sc_clock clock ("my_clock",1,0.5);
 58 
 59   sc_semaphore_example  object("semaphore");
 60     object.clock (clock.signal());
 61 
 62   sc_start(0); // First time called will init schedular
 63   sc_start();  // Run the simulation till sc_stop is encountered
 64   return 0;// Terminate simulation
 65 }
You could download file sc_semaphore.cpp here
   

space.gif

  ../images/main/bullet_star_pink.gif Simulation Output : sc_semaphore
   

space.gif

 @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
 SystemC: simulation stopped by user.
   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com