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_mutex

sc_mutex is a predefined primitive channel intended to model the behavior of a mutual exclusion lock as used to control access to a resource shared by concurrent processes. A mutex will be in one of two exclusive states: unlocked or locked. Only one process can lock a given mutex at one time. A mutex can only be unlocked by the particular process that locked the mutex, but may be locked subsequently by a different process.

   

space.gif

sc_mutex class comes with pre-defined methods as below.

   

space.gif

  • int lock() : Lock the mutex if it is free, else wait till mutex gets free.
  • int unlock() : Unlock the mutex
  • int trylock() : Check if mutex is free, if free then lock it else return -1.
  • char* kind() : Return string "sc_mutex"
   

space.gif

   

space.gif

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

space.gif


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

space.gif

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

space.gif

 @1 ns Checking mutex intance 1
 @1 ns Got mutex for intance 1
 @1 ns Bus access by instance 1
 @1 ns Checking mutex intance 0
 @2 ns Checking mutex intance 0
 @3 ns Checking mutex intance 0
 @4 ns Checking mutex intance 0
 @4 ns Got mutex for intance 0
 @4 ns Bus access by instance 0
 @5 ns Checking mutex intance 1
 @7 ns Got mutex for intance 1
 @7 ns Bus access by instance 1
 @7 ns Checking mutex intance 0
 @8 ns Checking mutex intance 0
 @9 ns Checking mutex intance 0
 @10 ns Checking mutex intance 0
 @10 ns Got mutex for intance 0
 @10 ns Bus access by instance 0
 @11 ns Checking mutex intance 1
 @13 ns Got mutex for intance 1
 @13 ns Bus access by instance 1
 @13 ns Checking mutex intance 0
 @14 ns Checking mutex intance 0
 @15 ns Checking mutex intance 0
 @16 ns Checking mutex intance 0
 @16 ns Got mutex for intance 0
 @16 ns Bus access by instance 0
 @17 ns Checking mutex intance 1
 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