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 <atomic>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
@ -205,15 +206,13 @@ static int s3fs_removexattr(const char* path, const char* name);
class MpStatFlag
{
private:
mutable pthread_mutex_t flag_lock;
bool is_lock_init = false;
bool has_mp_stat = false;
std::atomic<bool> has_mp_stat;
public:
MpStatFlag();
MpStatFlag() = default;
MpStatFlag(const MpStatFlag&) = delete;
MpStatFlag(MpStatFlag&&) = delete;
~MpStatFlag();
~MpStatFlag() = default;
MpStatFlag& operator=(const MpStatFlag&) = delete;
MpStatFlag& operator=(MpStatFlag&&) = delete;
@ -221,46 +220,14 @@ class MpStatFlag
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()
{
AutoLock auto_lock(&flag_lock);
return has_mp_stat;
}
bool MpStatFlag::Set(bool flag)
{
AutoLock auto_lock(&flag_lock);
bool old = has_mp_stat;
has_mp_stat = flag;
return old;
return has_mp_stat.exchange(flag);
}
// whether the stat information file for mount point exists

View File

@ -117,7 +117,7 @@ void* ThreadPoolMan::Worker(void* arg)
//------------------------------------------------
// 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){
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;
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
if(!StartThreads(count)){
S3FS_PRN_ERR("Failed starting threads at initializing.");
@ -166,25 +160,15 @@ ThreadPoolMan::~ThreadPoolMan()
}
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
{
AutoLock auto_lock(&thread_exit_flag_lock);
return is_exit;
}
void ThreadPoolMan::SetExitFlag(bool exit_flag)
{
AutoLock auto_lock(&thread_exit_flag_lock);
is_exit = exit_flag;
}

View File

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