Use std::unique_ptr in threadpoolman (#2374)

This commit is contained in:
Andrew Gaul 2023-11-26 01:49:17 +09:00 committed by GitHub
parent b671fa7a9c
commit 2b57e74330
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 20 deletions

View File

@ -449,15 +449,14 @@ bool PseudoFdInfo::ParallelMultipartUpload(const char* path, const mp_part_list_
thargs->petag = petag; thargs->petag = petag;
// make parameter for thread pool // make parameter for thread pool
thpoolman_param* ppoolparam = new thpoolman_param; std::unique_ptr<thpoolman_param> ppoolparam(new thpoolman_param);
ppoolparam->args = thargs; ppoolparam->args = thargs;
ppoolparam->psem = &uploaded_sem; ppoolparam->psem = &uploaded_sem;
ppoolparam->pfunc = PseudoFdInfo::MultipartUploadThreadWorker; ppoolparam->pfunc = PseudoFdInfo::MultipartUploadThreadWorker;
// setup instruction // setup instruction
if(!ThreadPoolMan::Instruct(ppoolparam)){ if(!ThreadPoolMan::Instruct(std::move(ppoolparam))){
S3FS_PRN_ERR("failed setup instruction for uploading."); S3FS_PRN_ERR("failed setup instruction for uploading.");
delete ppoolparam;
delete thargs; delete thargs;
return false; return false;
} }

View File

@ -53,13 +53,13 @@ void ThreadPoolMan::Destroy()
} }
} }
bool ThreadPoolMan::Instruct(thpoolman_param* pparam) bool ThreadPoolMan::Instruct(std::unique_ptr<thpoolman_param> pparam)
{ {
if(!ThreadPoolMan::singleton){ if(!ThreadPoolMan::singleton){
S3FS_PRN_WARN("The singleton object is not initialized yet."); S3FS_PRN_WARN("The singleton object is not initialized yet.");
return false; return false;
} }
return ThreadPoolMan::singleton->SetInstruction(pparam); return ThreadPoolMan::singleton->SetInstruction(std::move(pparam));
} }
// //
@ -84,12 +84,12 @@ void* ThreadPoolMan::Worker(void* arg)
} }
// get instruction // get instruction
thpoolman_param* pparam; std::unique_ptr<thpoolman_param> pparam;
{ {
AutoLock auto_lock(&(psingleton->thread_list_lock)); AutoLock auto_lock(&(psingleton->thread_list_lock));
if(!psingleton->instruction_list.empty()){ if(!psingleton->instruction_list.empty()){
pparam = psingleton->instruction_list.front(); pparam = std::move(psingleton->instruction_list.front());
psingleton->instruction_list.pop_front(); psingleton->instruction_list.pop_front();
if(!pparam){ if(!pparam){
S3FS_PRN_WARN("Got a semaphore, but the instruction is empty."); S3FS_PRN_WARN("Got a semaphore, but the instruction is empty.");
@ -108,7 +108,6 @@ void* ThreadPoolMan::Worker(void* arg)
if(pparam->psem){ if(pparam->psem){
pparam->psem->post(); pparam->psem->post();
} }
delete pparam;
} }
} }
@ -218,13 +217,6 @@ bool ThreadPoolMan::StopThreads()
while(thpoolman_sem.try_wait()){ while(thpoolman_sem.try_wait()){
} }
// clear instructions
for(thpoolman_params_t::const_iterator iter = instruction_list.begin(); iter != instruction_list.end(); ++iter){
thpoolman_param* pparam = *iter;
delete pparam;
}
instruction_list.clear();
return true; return true;
} }
@ -259,7 +251,7 @@ bool ThreadPoolMan::StartThreads(int count)
return true; return true;
} }
bool ThreadPoolMan::SetInstruction(thpoolman_param* pparam) bool ThreadPoolMan::SetInstruction(std::unique_ptr<thpoolman_param> pparam)
{ {
if(!pparam){ if(!pparam){
S3FS_PRN_ERR("The parameter value is nullptr."); S3FS_PRN_ERR("The parameter value is nullptr.");
@ -269,7 +261,7 @@ bool ThreadPoolMan::SetInstruction(thpoolman_param* pparam)
// set parameter to list // set parameter to list
{ {
AutoLock auto_lock(&thread_list_lock); AutoLock auto_lock(&thread_list_lock);
instruction_list.push_back(pparam); instruction_list.push_back(std::move(pparam));
} }
// run thread // run thread

View File

@ -22,6 +22,7 @@
#define S3FS_THREADPOOLMAN_H_ #define S3FS_THREADPOOLMAN_H_
#include <list> #include <list>
#include <memory>
#include <vector> #include <vector>
#include "psemaphore.h" #include "psemaphore.h"
@ -51,7 +52,7 @@ struct thpoolman_param
thpoolman_param() : args(nullptr), psem(nullptr), pfunc(nullptr) {} thpoolman_param() : args(nullptr), psem(nullptr), pfunc(nullptr) {}
}; };
typedef std::list<thpoolman_param*> thpoolman_params_t; typedef std::list<std::unique_ptr<thpoolman_param>> thpoolman_params_t;
typedef std::vector<pthread_t> thread_list_t; typedef std::vector<pthread_t> thread_list_t;
@ -90,12 +91,12 @@ class ThreadPoolMan
bool StopThreads(); bool StopThreads();
bool StartThreads(int count); bool StartThreads(int count);
bool SetInstruction(thpoolman_param* pparam); bool SetInstruction(std::unique_ptr<thpoolman_param> pparam);
public: public:
static bool Initialize(int count); static bool Initialize(int count);
static void Destroy(); static void Destroy();
static bool Instruct(thpoolman_param* pparam); static bool Instruct(std::unique_ptr<thpoolman_param> pparam);
}; };
#endif // S3FS_THREADPOOLMAN_H_ #endif // S3FS_THREADPOOLMAN_H_