"L. Spiro Engine"
|
00001 00027 #ifndef __LSH_CRITICALSECTION_H__ 00028 #define __LSH_CRITICALSECTION_H__ 00029 00030 #include "../LSHThreadLib.h" 00031 00032 00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00034 // MACROS 00035 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00040 //#define LSH_SINGLE_THREADED 00041 00042 00043 namespace lsh { 00044 00050 class CCriticalSection { 00051 public : 00052 // == Various constructors. 00053 LSE_INLINE LSE_CALL CCriticalSection(); 00054 LSE_INLINE LSE_CALL ~CCriticalSection(); 00055 00056 00057 // == Types. 00061 class CLocker { 00062 public : 00063 // == Various constructors. 00064 LSE_INLINE LSE_CALL CLocker( CCriticalSection &_csLockMe ) { 00065 m_pcsSection = &_csLockMe; 00066 m_pcsSection->Lock(); 00067 } 00068 LSE_INLINE LSE_CALL ~CLocker() { 00069 m_pcsSection->UnLock(); 00070 } 00071 00072 private : 00073 // == Members. 00077 CCriticalSection * m_pcsSection; 00078 }; 00079 00084 class CLockerS { 00085 public : 00086 // == Various constructors. 00087 LSE_INLINE LSE_CALL CLockerS( CCriticalSection &_csLockMe ) { 00088 #ifndef LSH_SINGLE_THREADED 00089 m_pcsSection = &_csLockMe; 00090 m_pcsSection->Lock(); 00091 #else // #ifndef LSH_SINGLE_THREADED 00092 // Remove warning C4100. 00093 static_cast<CCriticalSection &>(_csLockMe); 00094 #endif // #ifndef LSH_SINGLE_THREADED 00095 } 00096 LSE_INLINE LSE_CALL ~CLockerS() { 00097 #ifndef LSH_SINGLE_THREADED 00098 m_pcsSection->UnLock(); 00099 #endif // #ifndef LSH_SINGLE_THREADED 00100 } 00101 00102 private : 00103 #ifndef LSH_SINGLE_THREADED 00104 // == Members. 00105 // The section we lock and unlock. 00106 CCriticalSection * m_pcsSection; 00107 #endif // #ifndef LSH_SINGLE_THREADED 00108 }; 00109 00110 00111 // == Functions. 00112 // Lock the critical section. 00113 LSE_INLINE LSVOID LSE_CALL Lock(); 00114 00115 // Unlock the critical section. 00116 LSE_INLINE LSVOID LSE_CALL UnLock(); 00117 00118 protected : 00119 // == Members. 00120 #ifdef LSE_WIN32 00121 CRITICAL_SECTION m_csCrit; 00122 #elif defined( LSE_MAC ) 00123 pthread_mutex_t m_ptmCrit; 00124 #endif // #ifdef LSE_WIN32 00125 }; 00126 00127 00128 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00129 // DEFINITIONS 00130 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00131 // == Various constructors. 00132 LSE_INLINE LSE_CALL CCriticalSection::CCriticalSection() { 00133 #ifdef LSE_WIN32 00134 ::InitializeCriticalSection( &m_csCrit ); 00135 #elif defined( LSE_MAC ) 00136 pthread_mutexattr_t tmaAtt; 00137 ::pthread_mutexattr_init( &tmaAtt ); 00138 ::pthread_mutexattr_settype( &tmaAtt, PTHREAD_MUTEX_RECURSIVE ); 00139 ::pthread_mutex_init( &m_ptmCrit, &tmaAtt ); 00140 ::pthread_mutexattr_destroy( &tmaAtt ); 00141 #endif // #ifdef LSE_WIN32 00142 } 00143 LSE_INLINE LSE_CALL CCriticalSection::~CCriticalSection() { 00144 #ifdef LSE_WIN32 00145 ::DeleteCriticalSection( &m_csCrit ); 00146 #elif defined( LSE_MAC ) 00147 ::pthread_mutex_destroy( &m_ptmCrit ); 00148 #endif // #ifdef LSE_WIN32 00149 } 00150 00151 // == Functions. 00152 // Lock the critical section. 00153 LSE_INLINE LSVOID LSE_CALL CCriticalSection::Lock() { 00154 #ifdef LSE_WIN32 00155 ::EnterCriticalSection( &m_csCrit ); 00156 #elif defined( LSE_MAC ) 00157 ::pthread_mutex_lock( &m_ptmCrit ); 00158 #endif // #ifdef LSE_WIN32 00159 } 00160 00161 // Unlock the critical section. 00162 LSE_INLINE LSVOID LSE_CALL CCriticalSection::UnLock() { 00163 #ifdef LSE_WIN32 00164 ::LeaveCriticalSection( &m_csCrit ); 00165 #elif defined( LSE_MAC ) 00166 ::pthread_mutex_unlock( &m_ptmCrit ); 00167 #endif // #ifdef LSE_WIN32 00168 } 00169 00170 } // namespace lsh 00171 00172 #endif // __LSH_CRITICALSECTION_H__