Skip to content

Commit

Permalink
Convert pthread_mutex to std::mutex (s3fs-fuse#2476)
Browse files Browse the repository at this point in the history
This simplifies resource management and improve Windows compatibility.
  • Loading branch information
gaul authored Jun 23, 2024
1 parent 86b3535 commit 622dc0a
Show file tree
Hide file tree
Showing 31 changed files with 219 additions and 781 deletions.
1 change: 0 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ s3fs_SOURCES = \
fdcache_untreated.cpp \
addhead.cpp \
sighandlers.cpp \
autolock.cpp \
threadpoolman.cpp \
common_auth.cpp
if USE_SSL_OPENSSL
Expand Down
78 changes: 0 additions & 78 deletions src/autolock.cpp

This file was deleted.

67 changes: 0 additions & 67 deletions src/autolock.h

This file was deleted.

38 changes: 11 additions & 27 deletions src/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "s3fs_logger.h"
#include "s3fs_util.h"
#include "cache.h"
#include "autolock.h"
#include "string_util.h"

//-------------------------------------------------------------------
Expand Down Expand Up @@ -118,7 +117,7 @@ struct sort_symlinkiterlist{
// Static
//-------------------------------------------------------------------
StatCache StatCache::singleton;
pthread_mutex_t StatCache::stat_cache_lock;
std::mutex StatCache::stat_cache_lock;

//-------------------------------------------------------------------
// Constructor/Destructor
Expand All @@ -127,16 +126,6 @@ StatCache::StatCache() : IsExpireTime(true), IsExpireIntervalType(false), Expire
{
if(this == StatCache::getStatCacheData()){
stat_cache.clear();
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
#if S3FS_PTHREAD_ERRORCHECK
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
#endif
int result;
if(0 != (result = pthread_mutex_init(&StatCache::stat_cache_lock, &attr))){
S3FS_PRN_CRIT("failed to init stat_cache_lock: %d", result);
abort();
}
}else{
abort();
}
Expand All @@ -146,11 +135,6 @@ StatCache::~StatCache()
{
if(this == StatCache::getStatCacheData()){
Clear();
int result = pthread_mutex_destroy(&StatCache::stat_cache_lock);
if(result != 0){
S3FS_PRN_CRIT("failed to destroy stat_cache_lock: %d", result);
abort();
}
}else{
abort();
}
Expand Down Expand Up @@ -203,7 +187,7 @@ bool StatCache::SetCacheNoObject(bool flag)

void StatCache::Clear()
{
AutoLock lock(&StatCache::stat_cache_lock);
const std::lock_guard<std::mutex> lock(StatCache::stat_cache_lock);

stat_cache.clear();
S3FS_MALLOCTRIM(0);
Expand All @@ -214,7 +198,7 @@ bool StatCache::GetStat(const std::string& key, struct stat* pst, headers_t* met
bool is_delete_cache = false;
std::string strpath = key;

AutoLock lock(&StatCache::stat_cache_lock);
const std::lock_guard<std::mutex> lock(StatCache::stat_cache_lock);

stat_cache_t::iterator iter = stat_cache.end();
if(overcheck && '/' != *strpath.rbegin()){
Expand Down Expand Up @@ -300,7 +284,7 @@ bool StatCache::IsNoObjectCache(const std::string& key, bool overcheck)
return false;
}

AutoLock lock(&StatCache::stat_cache_lock);
const std::lock_guard<std::mutex> lock(StatCache::stat_cache_lock);

stat_cache_t::iterator iter = stat_cache.end();
if(overcheck && '/' != *strpath.rbegin()){
Expand Down Expand Up @@ -339,7 +323,7 @@ bool StatCache::AddStat(const std::string& key, const headers_t& meta, bool forc
}
S3FS_PRN_INFO3("add stat cache entry[path=%s]", key.c_str());

AutoLock lock(&StatCache::stat_cache_lock);
const std::lock_guard<std::mutex> lock(StatCache::stat_cache_lock);

if(stat_cache.end() != stat_cache.find(key)){
// found cache
Expand Down Expand Up @@ -416,7 +400,7 @@ bool StatCache::UpdateMetaStats(const std::string& key, const headers_t& meta)
}
S3FS_PRN_INFO3("update stat cache entry[path=%s]", key.c_str());

AutoLock lock(&StatCache::stat_cache_lock);
const std::lock_guard<std::mutex> lock(StatCache::stat_cache_lock);
stat_cache_t::iterator iter = stat_cache.find(key);
if(stat_cache.end() == iter){
return true;
Expand Down Expand Up @@ -459,7 +443,7 @@ bool StatCache::AddNoObjectCache(const std::string& key)
}
S3FS_PRN_INFO3("add no object cache entry[path=%s]", key.c_str());

AutoLock lock(&StatCache::stat_cache_lock);
const std::lock_guard<std::mutex> lock(StatCache::stat_cache_lock);

if(stat_cache.end() != stat_cache.find(key)){
// found
Expand Down Expand Up @@ -496,7 +480,7 @@ bool StatCache::AddNoObjectCache(const std::string& key)

void StatCache::ChangeNoTruncateFlag(const std::string& key, bool no_truncate)
{
AutoLock lock(&StatCache::stat_cache_lock);
const std::lock_guard<std::mutex> lock(StatCache::stat_cache_lock);
stat_cache_t::iterator iter = stat_cache.find(key);

if(stat_cache.end() != iter){
Expand Down Expand Up @@ -608,7 +592,7 @@ bool StatCache::GetSymlink(const std::string& key, std::string& value)
bool is_delete_cache = false;
const std::string& strpath = key;

AutoLock lock(&StatCache::stat_cache_lock);
const std::lock_guard<std::mutex> lock(StatCache::stat_cache_lock);

symlink_cache_t::iterator iter = symlink_cache.find(strpath);
if(iter != symlink_cache.end()){
Expand Down Expand Up @@ -644,7 +628,7 @@ bool StatCache::AddSymlink(const std::string& key, const std::string& value)
}
S3FS_PRN_INFO3("add symbolic link cache entry[path=%s, value=%s]", key.c_str(), value.c_str());

AutoLock lock(&StatCache::stat_cache_lock);
const std::lock_guard<std::mutex> lock(StatCache::stat_cache_lock);

if(symlink_cache.end() != symlink_cache.find(key)){
// found
Expand Down Expand Up @@ -819,7 +803,7 @@ bool StatCache::GetNotruncateCache(const std::string& parentdir, notruncate_file
dirpath += '/';
}

AutoLock lock(&StatCache::stat_cache_lock);
const std::lock_guard<std::mutex> lock(StatCache::stat_cache_lock);

notruncate_dir_map_t::iterator iter = notruncate_file_cache.find(dirpath);
if(iter == notruncate_file_cache.end()){
Expand Down
9 changes: 5 additions & 4 deletions src/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@

#include <cstring>
#include <map>
#include <mutex>
#include <string>
#include <sys/stat.h>
#include <vector>

#include "autolock.h"
#include "common.h"
#include "metaheader.h"

//-------------------------------------------------------------------
Expand Down Expand Up @@ -81,7 +82,7 @@ class StatCache
{
private:
static StatCache singleton;
static pthread_mutex_t stat_cache_lock;
static std::mutex stat_cache_lock;
stat_cache_t stat_cache;
bool IsExpireTime;
bool IsExpireIntervalType; // if this flag is true, cache data is updated at last access time.
Expand Down Expand Up @@ -178,7 +179,7 @@ class StatCache
// Delete stat cache
bool DelStat(const std::string& key)
{
AutoLock lock(&StatCache::stat_cache_lock);
const std::lock_guard<std::mutex> lock(StatCache::stat_cache_lock);
return DelStatHasLock(key);
}
bool DelStatHasLock(const std::string& key) REQUIRES(StatCache::stat_cache_lock);
Expand All @@ -187,7 +188,7 @@ class StatCache
bool GetSymlink(const std::string& key, std::string& value);
bool AddSymlink(const std::string& key, const std::string& value);
bool DelSymlink(const std::string& key) {
AutoLock lock(&StatCache::stat_cache_lock);
const std::lock_guard<std::mutex> lock(StatCache::stat_cache_lock);
return DelSymlinkHasLock(key);
}
bool DelSymlinkHasLock(const std::string& key);
Expand Down
3 changes: 3 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ extern std::string instance_name;
//-------------------------------------------------------------------
#define S3FS_FUNCATTR_WEAK __attribute__ ((weak,unused))

// empty annotation to indicate lock requirement
#define REQUIRES(...)

#endif // S3FS_COMMON_H_

/*
Expand Down
Loading

0 comments on commit 622dc0a

Please sign in to comment.