学海荡舟手机网
导航

主页 > 电子设备技术 > 电器电路基础 > > 详细内容

SystemC-sc_mutex

  学习过操作系统的人都知道,互斥(mutex)是用来保护共享资源的,以避免多个进程同时读写共享资源,导致系统行为的不确定性。互斥具有锁定和非锁定两种状态。如果有进程需要使用由互斥保护的资源,而这时互斥没有被锁定,则该进程就可以将互斥锁定,这时它就可以唯一的获得由该互斥保护的资源,并允许对资源进行任何的合法操作。当互斥已经由另外的进程锁定,这时申请互斥的进程就会被阻塞,直到锁定互斥的进程将互斥解锁。

      作为一个通道,互斥实现的接口为sc_mutex_if,如下面的代码:  

  1. class sc_mutex_if: virtual publ sc_interface  
  2. {  
  3. public:  
  4.     // the classical operations: k(), trylock(), and unlock()  
  5.     // blocks until mutex could be locked  
  6.     virtual int lock() = 0;  
  7.     // returns -1 if mutex could not be locked  
  8.     virtual int trylock() = 0;  
  9.     // returns -1 if mutex was not locked by caller  
  10.    virtual int unlock() = 0;  
  11. protected:  
  12.     // constructor  
  13.     sc_mutex_if()    {}  
  14. private:  
  15.     // db  
  16.    sc_mutex_if( const sc_mutex_if& );  
  17.     sc_mutex_if& operator = ( const sc_mutex_if& );  
  18. }; 

  通过lock()进程可以锁定互斥,如果互斥已经被锁定,这时申请锁定的进程就被阻塞直到互斥被解锁。通过trylock()进程可以查询互斥是否被锁定,以决定是否使用lock()锁定互斥从而避免进程被阻塞。通过unlock()函数进程可以解锁互斥。

      Syst2.2对sc_mutex的实现如下面的代码:

  1. class sc_mutex: public sc_mutex_if, public sc_prim_channel  
  2. {  
  3. public:  
  4.     // constructors  
  5.     sc_mutex();  
  6.     explicit sc_mutex( const char* name_ );  
  7.     // interface methods  
  8.     // blocks until mutex could be locked  
  9.     virtual int lock();  
  10.    // returns -1 if mutex could not be locked  
  11.    virtual int trylock();  
  12.     // returns -1 if mutex was not locked by caller  
  13.     virtual int unlock();  
  14.     static const charconst kind_string;  
  15.     virtual const char* kind() const 
  16.         { return kind_string; }  
  17.  
  18. protected:  
  19.     // support methods  
  20.     bool in_use() const { return ( m_owner != 0 ); }  
  21. protected:  
  22.     sc_process_b* m_owner;  
  23.     sc_event      m_free;  
  24. private:  
  25.     // disabled  
  26.    sc_mutex( const sc_mutex& );   sc_mutex& operator = ( const sc_mutex& );  
  27. };  
  28.  
  29. sc_mutex::sc_mutex()  
  30. : sc_prim_channel( sc_gen_unique_name( "mutex" ) ),  m_owner( 0 ){}  
  31.  
  32. sc_mutex::sc_mutex( const char* name_ ): sc_prim_channel( name_ ),  m_owner( 0 ){}  
  33.  
  34. // interface methods  
  35. // blocks until mutex could be locked  
  36. int sc_mutex::lock(){  
  37.     while( in_use() ) {    wait( m_free );  }  
  38.     m_owner = sc_get_curr_process_handle();  
  39.     return 0;  
  40. }  
  41. // returns -1 if mutex could not be locked  
  42. int sc_mutex::trylock(){  
  43.     if( in_use() ) {    return -1; }  
  44.     m_owner = sc_get_curr_process_handle();  
  45.    return 0;  
  46. }  
  47. // returns -1 if mutex was not locked by caller  
  48. int sc_mutex::unlock()  
  49. {   if( m_owner != sc_get_curr_process_handle() ) {    return -1; }  
  50.    m_owner = 0;  
  51.    m_free.notify();  
  52.     return 0;  

      我们在前面章节曾经给出过ss_mutex的一个例子,这里不再给出。

相关文章