mirror of
https://github.com/s3fs-fuse/s3fs-fuse.git
synced 2024-11-05 04:17:52 +00:00
Rewrite AutoLock
Previously AutoLock::Lock allowed subsequent callers to proceed without the lock. Further is_locked was not always protected by auto_mutex. Finally AutoLock eagerly released auto_mutex when recursively unlocking. s3fs does not need recursive locks so we rewrite and simplify AutoLock. Partially surfaced by Coverity.
This commit is contained in:
parent
2e344bb48f
commit
3f59b8da01
@ -425,51 +425,14 @@ void free_mvnodes(MVNODE *head)
|
|||||||
//-------------------------------------------------------------------
|
//-------------------------------------------------------------------
|
||||||
// Class AutoLock
|
// 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()
|
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);
|
pthread_mutex_unlock(auto_mutex);
|
||||||
is_locked = false;
|
|
||||||
}catch(exception& e){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------
|
//-------------------------------------------------------------------
|
||||||
|
@ -88,14 +88,10 @@ class AutoLock
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
pthread_mutex_t* auto_mutex;
|
pthread_mutex_t* auto_mutex;
|
||||||
bool is_locked;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit AutoLock(pthread_mutex_t* pmutex = NULL);
|
explicit AutoLock(pthread_mutex_t* pmutex);
|
||||||
~AutoLock();
|
~AutoLock();
|
||||||
|
|
||||||
bool Lock(void);
|
|
||||||
bool Unlock(void);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//-------------------------------------------------------------------
|
//-------------------------------------------------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user