Merge pull request #231 from andrewgaul/autolock

Rewrite AutoLock
This commit is contained in:
Takeshi Nakatani 2015-08-20 00:24:05 +09:00
commit 036612dbb0
2 changed files with 4 additions and 45 deletions

View File

@ -425,51 +425,14 @@ void free_mvnodes(MVNODE *head)
//-------------------------------------------------------------------
// Class AutoLock
//-------------------------------------------------------------------
AutoLock::AutoLock(pthread_mutex_t* pmutex) : auto_mutex(pmutex), is_locked(false)
AutoLock::AutoLock(pthread_mutex_t* pmutex) : auto_mutex(pmutex)
{
Lock();
pthread_mutex_lock(auto_mutex);
}
AutoLock::~AutoLock()
{
Unlock();
}
bool AutoLock::Lock(void)
{
if(!auto_mutex){
return false;
}
if(is_locked){
// already locked
return true;
}
try{
pthread_mutex_lock(auto_mutex);
is_locked = true;
}catch(exception& e){
is_locked = false;
return false;
}
return true;
}
bool AutoLock::Unlock(void)
{
if(!auto_mutex){
return false;
}
if(!is_locked){
// already unlocked
return true;
}
try{
pthread_mutex_unlock(auto_mutex);
is_locked = false;
}catch(exception& e){
return false;
}
return true;
}
//-------------------------------------------------------------------

View File

@ -88,14 +88,10 @@ class AutoLock
{
private:
pthread_mutex_t* auto_mutex;
bool is_locked;
public:
explicit AutoLock(pthread_mutex_t* pmutex = NULL);
explicit AutoLock(pthread_mutex_t* pmutex);
~AutoLock();
bool Lock(void);
bool Unlock(void);
};
//-------------------------------------------------------------------