Alex Bikfalvi
SimWorker Documentation
SimMutex.cpp
00001 #include "Headers.h" 00002 #include "SimMutex.h" 00003 #include "ExceptionMutex.h" 00004 00005 CSimMutex::CSimMutex() 00006 { 00007 #ifdef WIN32 00008 InitializeCriticalSection(&this->mutex); 00009 #elif POSIX 00010 if(pthread_mutex_init(&this->mutex, NULL)) throw CExceptionMutex("initialize mutex failed", __FILE__, __LINE__); 00011 #endif 00012 } 00013 00014 CSimMutex::~CSimMutex() 00015 { 00016 #ifdef WIN32 00017 DeleteCriticalSection(&this->mutex); 00018 #elif POSIX 00019 if(pthread_mutex_destroy(&this->mutex)) throw CExceptionMutex("destroy mutex failed", __FILE__, __LINE__); 00020 #endif 00021 } 00022 00023 void CSimMutex::Lock() 00024 { 00025 #ifdef WIN32 00026 EnterCriticalSection(&this->mutex); 00027 #elif POSIX 00028 if(pthread_mutex_lock(&this->mutex)) throw CExceptionMutex("lock mutex failed", __FILE__, __LINE__); 00029 #endif 00030 } 00031 00032 void CSimMutex::Unlock() 00033 { 00034 #ifdef WIN32 00035 LeaveCriticalSection(&this->mutex); 00036 #elif POSIX 00037 if(pthread_mutex_unlock(&this->mutex)) throw CExceptionMutex("unlock mutex failed", __FILE__, __LINE__); 00038 #endif 00039 } 00040 00041 bool CSimMutex::TryLock() 00042 { 00043 #ifdef WIN32 00044 return TryEnterCriticalSection(&this->mutex) != 0; 00045 #elif POSIX 00046 return pthread_mutex_trylock(&this->mutex) == 0; 00047 #endif 00048 }
Last updated: February 8, 2011