00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 #ifndef SC_HOST_MUTEX_H_INCLUDED_
00028 #define SC_HOST_MUTEX_H_INCLUDED_
00029 
00030 #ifndef SC_INCLUDE_WINDOWS_H
00031 #  define SC_INCLUDE_WINDOWS_H // include Windows.h, if needed
00032 #endif
00033 #include "sysc/kernel/sc_cmnhdr.h"
00034 #include "sysc/communication/sc_mutex_if.h"
00035 
00036 #if defined(WIN32) || defined(_WIN32)
00037 
00038 #define SC_MTX_TYPE_ CRITICAL_SECTION
00039 
00040 #define SC_MTX_INIT_( Mutex ) \
00041     InitializeCriticalSection( &(Mutex) )
00042 #define SC_MTX_LOCK_( Mutex ) \
00043     EnterCriticalSection( &(Mutex) )
00044 #define SC_MTX_UNLOCK_( Mutex ) \
00045     LeaveCriticalSection( &(Mutex) )
00046 #define SC_MTX_TRYLOCK_( Mutex ) \
00047     ( TryEnterCriticalSection( &(Mutex) ) != 0 )
00048 #define SC_MTX_DESTROY_( Mutex ) \
00049     DeleteCriticalSection( &(Mutex) )
00050 
00051 #else // use pthread mutex
00052 
00053 #include <pthread.h>
00054 #define SC_MTX_TYPE_ pthread_mutex_t
00055 
00056 #if defined(__hpux)
00057 #  define SC_PTHREAD_NULL_ cma_c_null
00058 #else // !defined(__hpux)
00059 #  define SC_PTHREAD_NULL_ NULL
00060 #endif
00061 
00062 #define SC_MTX_INIT_( Mutex ) \
00063     pthread_mutex_init( &(Mutex), SC_PTHREAD_NULL_ )
00064 #define SC_MTX_LOCK_( Mutex ) \
00065     pthread_mutex_lock( &(Mutex) )
00066 #define SC_MTX_UNLOCK_( Mutex ) \
00067     pthread_mutex_unlock( &(Mutex) )
00068 
00069 #ifdef _XOPEN_SOURCE
00070 #   define SC_MTX_TRYLOCK_( Mutex ) \
00071        ( pthread_mutex_trylock( &(Mutex) ) == 0 )
00072 #else // no try_lock available
00073 #   define SC_MTX_TRYLOCK_( Mutex ) \
00074        ( false ) 
00075 #endif
00076 
00077 #define SC_MTX_DESTROY_( Mutex ) \
00078     pthread_mutex_destroy( &(Mutex) )
00079 
00080 #endif
00081 
00082 namespace sc_core {
00083 
00084 
00091 class sc_host_mutex : public sc_mutex_if
00092 {
00093     typedef SC_MTX_TYPE_ underlying_type;
00094 public:
00095 
00096     
00097 
00098     sc_host_mutex()
00099         { SC_MTX_INIT_(m_mtx); }
00100     virtual ~sc_host_mutex()
00101         { SC_MTX_DESTROY_(m_mtx); }
00102 
00103 
00104     
00105 
00106     
00107 
00115     
00116     virtual int lock( int )
00117         { assert( 0 ); SC_MTX_LOCK_(m_mtx); return 0; }
00118 
00119     
00120     virtual int trylock()
00121         { return SC_MTX_TRYLOCK_(m_mtx) ? 0 : -1; }
00122 
00123     
00124     
00125     virtual int unlock()
00126         { SC_MTX_UNLOCK_(m_mtx); return 0; }
00127 
00128 private:
00129     underlying_type m_mtx;
00130 };
00131 
00132 } 
00133 
00134 #undef SC_MTX_TYPE_
00135 #undef SC_MTX_INIT_
00136 #undef SC_MTX_DESTROY_
00137 #undef SC_MTX_LOCK_
00138 #undef SC_MTX_TRYLOCK_
00139 #undef SC_MTX_UNLOCK_
00140 #undef SC_PTHREAD_NULL_
00141 
00142 
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 
00155 
00156 
00157 
00158 
00159 
00160 
00161 
00162 
00163 
00164 
00165 
00166 #endif
00167 
00168