Convert thpoolman_param to value (#2430)
This simplifies memory management.
This commit is contained in:
parent
c97f7a2a13
commit
31676f6201
|
@ -449,13 +449,13 @@ 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
|
||||||
std::unique_ptr<thpoolman_param> ppoolparam(new thpoolman_param);
|
thpoolman_param ppoolparam;
|
||||||
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(std::move(ppoolparam))){
|
if(!ThreadPoolMan::Instruct(ppoolparam)){
|
||||||
S3FS_PRN_ERR("failed setup instruction for uploading.");
|
S3FS_PRN_ERR("failed setup instruction for uploading.");
|
||||||
delete thargs;
|
delete thargs;
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -53,13 +53,14 @@ void ThreadPoolMan::Destroy()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ThreadPoolMan::Instruct(std::unique_ptr<thpoolman_param> pparam)
|
bool ThreadPoolMan::Instruct(const thpoolman_param& param)
|
||||||
{
|
{
|
||||||
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(std::move(pparam));
|
ThreadPoolMan::singleton->SetInstruction(param);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -84,30 +85,25 @@ void* ThreadPoolMan::Worker(void* arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get instruction
|
// get instruction
|
||||||
std::unique_ptr<thpoolman_param> pparam;
|
thpoolman_param param;
|
||||||
{
|
{
|
||||||
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 = std::move(psingleton->instruction_list.front());
|
S3FS_PRN_DBG("Got a semaphore, but the instruction is empty.");
|
||||||
psingleton->instruction_list.pop_front();
|
continue;
|
||||||
if(!pparam){
|
|
||||||
S3FS_PRN_WARN("Got a semaphore, but the instruction is empty.");
|
|
||||||
}
|
|
||||||
}else{
|
}else{
|
||||||
S3FS_PRN_WARN("Got a semaphore, but there is no instruction.");
|
param = psingleton->instruction_list.front();
|
||||||
pparam = nullptr;
|
psingleton->instruction_list.pop_front();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(pparam){
|
void* retval = param.pfunc(param.args);
|
||||||
void* retval = pparam->pfunc(pparam->args);
|
if(nullptr != retval){
|
||||||
if(nullptr != retval){
|
S3FS_PRN_WARN("The instruction function returned with somthign error code(%ld).", reinterpret_cast<long>(retval));
|
||||||
S3FS_PRN_WARN("The instruction function returned with somthign error code(%ld).", reinterpret_cast<long>(retval));
|
}
|
||||||
}
|
if(param.psem){
|
||||||
if(pparam->psem){
|
param.psem->post();
|
||||||
pparam->psem->post();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -235,23 +231,16 @@ bool ThreadPoolMan::StartThreads(int count)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ThreadPoolMan::SetInstruction(std::unique_ptr<thpoolman_param> pparam)
|
void ThreadPoolMan::SetInstruction(const thpoolman_param& param)
|
||||||
{
|
{
|
||||||
if(!pparam){
|
|
||||||
S3FS_PRN_ERR("The parameter value is nullptr.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// set parameter to list
|
// set parameter to list
|
||||||
{
|
{
|
||||||
AutoLock auto_lock(&thread_list_lock);
|
AutoLock auto_lock(&thread_list_lock);
|
||||||
instruction_list.push_back(std::move(pparam));
|
instruction_list.push_back(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
// run thread
|
// run thread
|
||||||
thpoolman_sem.post();
|
thpoolman_sem.post();
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -23,7 +23,6 @@
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <memory>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "psemaphore.h"
|
#include "psemaphore.h"
|
||||||
|
@ -53,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<std::unique_ptr<thpoolman_param>> thpoolman_params_t;
|
typedef std::list<thpoolman_param> thpoolman_params_t;
|
||||||
|
|
||||||
typedef std::vector<pthread_t> thread_list_t;
|
typedef std::vector<pthread_t> thread_list_t;
|
||||||
|
|
||||||
|
@ -89,12 +88,12 @@ class ThreadPoolMan
|
||||||
|
|
||||||
bool StopThreads();
|
bool StopThreads();
|
||||||
bool StartThreads(int count);
|
bool StartThreads(int count);
|
||||||
bool SetInstruction(std::unique_ptr<thpoolman_param> pparam);
|
void SetInstruction(const thpoolman_param& pparam);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static bool Initialize(int count);
|
static bool Initialize(int count);
|
||||||
static void Destroy();
|
static void Destroy();
|
||||||
static bool Instruct(std::unique_ptr<thpoolman_param> pparam);
|
static bool Instruct(const thpoolman_param& pparam);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // S3FS_THREADPOOLMAN_H_
|
#endif // S3FS_THREADPOOLMAN_H_
|
||||||
|
|
Loading…
Reference in New Issue