Simplify locking with C++11 atomics (#2382)

This commit is contained in:
Andrew Gaul 2023-11-27 01:12:49 +09:00 committed by GitHub
parent feb0845103
commit b139507ae6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 59 deletions

View File

@ -19,6 +19,7 @@
*/ */
#include <algorithm> #include <algorithm>
#include <atomic>
#include <cerrno> #include <cerrno>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
@ -205,15 +206,13 @@ static int s3fs_removexattr(const char* path, const char* name);
class MpStatFlag class MpStatFlag
{ {
private: private:
mutable pthread_mutex_t flag_lock; std::atomic<bool> has_mp_stat;
bool is_lock_init = false;
bool has_mp_stat = false;
public: public:
MpStatFlag(); MpStatFlag() = default;
MpStatFlag(const MpStatFlag&) = delete; MpStatFlag(const MpStatFlag&) = delete;
MpStatFlag(MpStatFlag&&) = delete; MpStatFlag(MpStatFlag&&) = delete;
~MpStatFlag(); ~MpStatFlag() = default;
MpStatFlag& operator=(const MpStatFlag&) = delete; MpStatFlag& operator=(const MpStatFlag&) = delete;
MpStatFlag& operator=(MpStatFlag&&) = delete; MpStatFlag& operator=(MpStatFlag&&) = delete;
@ -221,46 +220,14 @@ class MpStatFlag
bool Set(bool flag); bool Set(bool flag);
}; };
MpStatFlag::MpStatFlag()
{
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(&flag_lock, &attr))){
S3FS_PRN_CRIT("failed to init flag_lock: %d", result);
abort();
}
is_lock_init = true;
}
MpStatFlag::~MpStatFlag()
{
if(is_lock_init){
int result;
if(0 != (result = pthread_mutex_destroy(&flag_lock))){
S3FS_PRN_CRIT("failed to destroy flag_lock: %d", result);
abort();
}
is_lock_init = false;
}
}
bool MpStatFlag::Get() bool MpStatFlag::Get()
{ {
AutoLock auto_lock(&flag_lock);
return has_mp_stat; return has_mp_stat;
} }
bool MpStatFlag::Set(bool flag) bool MpStatFlag::Set(bool flag)
{ {
AutoLock auto_lock(&flag_lock); return has_mp_stat.exchange(flag);
bool old = has_mp_stat;
has_mp_stat = flag;
return old;
} }
// whether the stat information file for mount point exists // whether the stat information file for mount point exists

View File

@ -117,7 +117,7 @@ void* ThreadPoolMan::Worker(void* arg)
//------------------------------------------------ //------------------------------------------------
// ThreadPoolMan methods // ThreadPoolMan methods
//------------------------------------------------ //------------------------------------------------
ThreadPoolMan::ThreadPoolMan(int count) : is_exit(false), thpoolman_sem(0), is_lock_init(false), is_exit_flag_init(false) ThreadPoolMan::ThreadPoolMan(int count) : is_exit(false), thpoolman_sem(0), is_lock_init(false)
{ {
if(count < 1){ if(count < 1){
S3FS_PRN_CRIT("Failed to creating singleton for Thread Manager, because thread count(%d) is under 1.", count); S3FS_PRN_CRIT("Failed to creating singleton for Thread Manager, because thread count(%d) is under 1.", count);
@ -141,12 +141,6 @@ ThreadPoolMan::ThreadPoolMan(int count) : is_exit(false), thpoolman_sem(0), is_l
} }
is_lock_init = true; is_lock_init = true;
if(0 != (result = pthread_mutex_init(&thread_exit_flag_lock, &attr))){
S3FS_PRN_CRIT("failed to init thread_exit_flag_lock: %d", result);
abort();
}
is_exit_flag_init = true;
// create threads // create threads
if(!StartThreads(count)){ if(!StartThreads(count)){
S3FS_PRN_ERR("Failed starting threads at initializing."); S3FS_PRN_ERR("Failed starting threads at initializing.");
@ -166,25 +160,15 @@ ThreadPoolMan::~ThreadPoolMan()
} }
is_lock_init = false; is_lock_init = false;
} }
if(is_exit_flag_init ){
int result;
if(0 != (result = pthread_mutex_destroy(&thread_exit_flag_lock))){
S3FS_PRN_CRIT("failed to destroy thread_exit_flag_lock: %d", result);
abort();
}
is_exit_flag_init = false;
}
} }
bool ThreadPoolMan::IsExit() const bool ThreadPoolMan::IsExit() const
{ {
AutoLock auto_lock(&thread_exit_flag_lock);
return is_exit; return is_exit;
} }
void ThreadPoolMan::SetExitFlag(bool exit_flag) void ThreadPoolMan::SetExitFlag(bool exit_flag)
{ {
AutoLock auto_lock(&thread_exit_flag_lock);
is_exit = exit_flag; is_exit = exit_flag;
} }

View File

@ -21,6 +21,7 @@
#ifndef S3FS_THREADPOOLMAN_H_ #ifndef S3FS_THREADPOOLMAN_H_
#define S3FS_THREADPOOLMAN_H_ #define S3FS_THREADPOOLMAN_H_
#include <atomic>
#include <list> #include <list>
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -64,7 +65,7 @@ class ThreadPoolMan
private: private:
static ThreadPoolMan* singleton; static ThreadPoolMan* singleton;
bool is_exit; std::atomic<bool> is_exit;
Semaphore thpoolman_sem; Semaphore thpoolman_sem;
bool is_lock_init; bool is_lock_init;
@ -73,9 +74,6 @@ class ThreadPoolMan
thpoolman_params_t instruction_list; thpoolman_params_t instruction_list;
bool is_exit_flag_init;
mutable pthread_mutex_t thread_exit_flag_lock;
private: private:
static void* Worker(void* arg); static void* Worker(void* arg);