Skip to content

Commit

Permalink
Simplify locking with C++11 atomics (s3fs-fuse#2382)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaul authored Nov 26, 2023
1 parent feb0845 commit b139507
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 59 deletions.
43 changes: 5 additions & 38 deletions src/s3fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include <algorithm>
#include <atomic>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
Expand Down Expand Up @@ -205,62 +206,28 @@ 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;

bool Get();
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
Expand Down
18 changes: 1 addition & 17 deletions src/threadpoolman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.");
Expand All @@ -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;
}

Expand Down
6 changes: 2 additions & 4 deletions src/threadpoolman.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#ifndef S3FS_THREADPOOLMAN_H_
#define S3FS_THREADPOOLMAN_H_

#include <atomic>
#include <list>
#include <memory>
#include <vector>
Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand Down

0 comments on commit b139507

Please sign in to comment.