From 2b57e74330910e4c2a50e0fd160caeefda21e370 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 26 Nov 2023 01:49:17 +0900 Subject: [PATCH] Use std::unique_ptr in threadpoolman (#2374) --- src/fdcache_fdinfo.cpp | 5 ++--- src/threadpoolman.cpp | 20 ++++++-------------- src/threadpoolman.h | 7 ++++--- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/fdcache_fdinfo.cpp b/src/fdcache_fdinfo.cpp index c104ba2..bca631c 100644 --- a/src/fdcache_fdinfo.cpp +++ b/src/fdcache_fdinfo.cpp @@ -449,15 +449,14 @@ bool PseudoFdInfo::ParallelMultipartUpload(const char* path, const mp_part_list_ thargs->petag = petag; // make parameter for thread pool - thpoolman_param* ppoolparam = new thpoolman_param; + std::unique_ptr ppoolparam(new thpoolman_param); ppoolparam->args = thargs; ppoolparam->psem = &uploaded_sem; ppoolparam->pfunc = PseudoFdInfo::MultipartUploadThreadWorker; // setup instruction - if(!ThreadPoolMan::Instruct(ppoolparam)){ + if(!ThreadPoolMan::Instruct(std::move(ppoolparam))){ S3FS_PRN_ERR("failed setup instruction for uploading."); - delete ppoolparam; delete thargs; return false; } diff --git a/src/threadpoolman.cpp b/src/threadpoolman.cpp index a5be537..3bc2e2e 100644 --- a/src/threadpoolman.cpp +++ b/src/threadpoolman.cpp @@ -53,13 +53,13 @@ void ThreadPoolMan::Destroy() } } -bool ThreadPoolMan::Instruct(thpoolman_param* pparam) +bool ThreadPoolMan::Instruct(std::unique_ptr pparam) { if(!ThreadPoolMan::singleton){ S3FS_PRN_WARN("The singleton object is not initialized yet."); 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 - thpoolman_param* pparam; + std::unique_ptr pparam; { AutoLock auto_lock(&(psingleton->thread_list_lock)); if(!psingleton->instruction_list.empty()){ - pparam = psingleton->instruction_list.front(); + pparam = std::move(psingleton->instruction_list.front()); psingleton->instruction_list.pop_front(); if(!pparam){ S3FS_PRN_WARN("Got a semaphore, but the instruction is empty."); @@ -108,7 +108,6 @@ void* ThreadPoolMan::Worker(void* arg) if(pparam->psem){ pparam->psem->post(); } - delete pparam; } } @@ -218,13 +217,6 @@ bool ThreadPoolMan::StopThreads() 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; } @@ -259,7 +251,7 @@ bool ThreadPoolMan::StartThreads(int count) return true; } -bool ThreadPoolMan::SetInstruction(thpoolman_param* pparam) +bool ThreadPoolMan::SetInstruction(std::unique_ptr pparam) { if(!pparam){ S3FS_PRN_ERR("The parameter value is nullptr."); @@ -269,7 +261,7 @@ bool ThreadPoolMan::SetInstruction(thpoolman_param* pparam) // set parameter to list { AutoLock auto_lock(&thread_list_lock); - instruction_list.push_back(pparam); + instruction_list.push_back(std::move(pparam)); } // run thread diff --git a/src/threadpoolman.h b/src/threadpoolman.h index ee9a770..0a9769b 100644 --- a/src/threadpoolman.h +++ b/src/threadpoolman.h @@ -22,6 +22,7 @@ #define S3FS_THREADPOOLMAN_H_ #include +#include #include #include "psemaphore.h" @@ -51,7 +52,7 @@ struct thpoolman_param thpoolman_param() : args(nullptr), psem(nullptr), pfunc(nullptr) {} }; -typedef std::list thpoolman_params_t; +typedef std::list> thpoolman_params_t; typedef std::vector thread_list_t; @@ -90,12 +91,12 @@ class ThreadPoolMan bool StopThreads(); bool StartThreads(int count); - bool SetInstruction(thpoolman_param* pparam); + bool SetInstruction(std::unique_ptr pparam); public: static bool Initialize(int count); static void Destroy(); - static bool Instruct(thpoolman_param* pparam); + static bool Instruct(std::unique_ptr pparam); }; #endif // S3FS_THREADPOOLMAN_H_