diff --git a/include/binary_file_packer.h b/include/binary_file_packer.h index 38f9cf12fa0746a2840a636c1d58be15dc6ec757..1ee23c936e7dfa7a175e278c4a3ca675614edbf2 100644 --- a/include/binary_file_packer.h +++ b/include/binary_file_packer.h @@ -37,7 +37,6 @@ protected: virtual bool IsDuplicated(const std::unique_ptr &entry, std::string subPath); PackageParser packageParser_; std::string moduleName_; - ThreadPool threadPool_; std::vector> copyResults_; std::atomic stopCopy_{false}; std::mutex mutex_; diff --git a/include/resource_data.h b/include/resource_data.h index f51d640acbacfc54ace69a04e9bdb33a544176d2..c6b1ca7aced24bb7cf0a58af1999807b7ab49200 100644 --- a/include/resource_data.h +++ b/include/resource_data.h @@ -49,11 +49,10 @@ const static std::string SOLUTIONS_ARROW = "> "; const static std::string LONG_PATH_HEAD = "\\\\?\\"; const static int32_t VERSION_MAX_LEN = 128; const static int32_t INT_TO_BYTES = sizeof(uint32_t); -static const int8_t RESTOOL_VERSION[VERSION_MAX_LEN] = { "Restool 5.1.0.002" }; +static const int8_t RESTOOL_VERSION[VERSION_MAX_LEN] = { "Restool 5.1.0.003" }; const static int32_t TAG_LEN = 4; static std::set g_resourceSet; static std::set g_hapResourceSet; -const static int8_t THREAD_POOL_SIZE = 2; const static int8_t INVALID_ID = -1; enum class KeyType { diff --git a/include/thread_pool.h b/include/thread_pool.h index 0efc3df006c2b97ea1d9cbf51bea8c9a0cc4d0f2..04371550564b3af59e999bf71782955782bf3e73 100644 --- a/include/thread_pool.h +++ b/include/thread_pool.h @@ -30,11 +30,6 @@ namespace Global { namespace Restool { class ThreadPool { public: - /** - * @brief Creates a thread pool with specify thread count - * @param threadCount the count of threads to be created - */ - explicit ThreadPool(const size_t threadCount); ~ThreadPool(); /** @@ -55,7 +50,16 @@ public: template std::future::type> Enqueue(F &&f, Args &&...args); + static ThreadPool &GetInstance(); + private: + /** + * @brief Creates a thread pool with specify thread count + * @param threadCount the count of threads to be created + */ + explicit ThreadPool(const size_t threadCount); + ThreadPool(const ThreadPool &) = delete; + ThreadPool &operator=(const ThreadPool &) = delete; void WorkInThread(); std::vector workerThreads_; std::queue> tasks_; diff --git a/src/binary_file_packer.cpp b/src/binary_file_packer.cpp index dc4c3a15e5f0342bd6deadea160fa5d46b82b45e..8b0522c02e88e307ab9c7ec00f8efbb8fa726fc4 100644 --- a/src/binary_file_packer.cpp +++ b/src/binary_file_packer.cpp @@ -24,14 +24,12 @@ namespace Restool { using namespace std; BinaryFilePacker::BinaryFilePacker(const PackageParser &packageParser, const std::string &moduleName) - : packageParser_(packageParser), moduleName_(moduleName), threadPool_(ThreadPool(THREAD_POOL_SIZE)) + : packageParser_(packageParser), moduleName_(moduleName) { - threadPool_.Start(); } BinaryFilePacker::~BinaryFilePacker() { - threadPool_.Stop(); } void BinaryFilePacker::StopCopy() @@ -42,7 +40,7 @@ void BinaryFilePacker::StopCopy() std::future BinaryFilePacker::CopyBinaryFileAsync(const std::vector &inputs) { auto func = [this](const vector &inputs) { return this->CopyBinaryFile(inputs); }; - std::future res = threadPool_.Enqueue(func, inputs); + std::future res = ThreadPool::GetInstance().Enqueue(func, inputs); return res; } @@ -126,7 +124,7 @@ uint32_t BinaryFilePacker::CopyBinaryFileImpl(const string &src, const string &d string path = entry->GetFilePath().GetPath(); auto copyFunc = [this](const string path, string subPath) { return this->CopySingleFile(path, subPath); }; - std::future res = threadPool_.Enqueue(copyFunc, path, subPath); + std::future res = ThreadPool::GetInstance().Enqueue(copyFunc, path, subPath); copyResults_.push_back(std::move(res)); } return RESTOOL_SUCCESS; diff --git a/src/generic_compiler.cpp b/src/generic_compiler.cpp index 52056fa4890af7d501427f180ee8f5f474d52c8f..d954395916cc3b3ece9b8dd96643ab3b2d7218de 100644 --- a/src/generic_compiler.cpp +++ b/src/generic_compiler.cpp @@ -36,12 +36,10 @@ GenericCompiler::~GenericCompiler() uint32_t GenericCompiler::CompileFiles(const std::vector &fileInfos) { cout << "Info: GenericCompiler::CompileFiles" << endl; - ThreadPool pool(THREAD_POOL_SIZE); - pool.Start(); std::vector> results; for (const auto &fileInfo : fileInfos) { auto taskFunc = [this](const FileInfo &fileInfo) { return this->CompileSingleFile(fileInfo); }; - results.push_back(pool.Enqueue(taskFunc, fileInfo)); + results.push_back(ThreadPool::GetInstance().Enqueue(taskFunc, fileInfo)); } for (auto &ret : results) { if (ret.get() != RESTOOL_SUCCESS) { diff --git a/src/thread_pool.cpp b/src/thread_pool.cpp index 9cd88b2d6fd11185e7e7b536a951d78f1bb2df87..ab765c9ae4442a4ecc3a85f02baf0638b62c7dc3 100644 --- a/src/thread_pool.cpp +++ b/src/thread_pool.cpp @@ -23,14 +23,27 @@ namespace OHOS { namespace Global { namespace Restool { using namespace std; +constexpr int DEFAULT_POOL_SIZE = 8; ThreadPool::ThreadPool(size_t threadCount) : threadCount_(threadCount) -{} +{ + Start(); +} + +ThreadPool &ThreadPool::GetInstance() +{ + static ThreadPool pool(DEFAULT_POOL_SIZE); + return pool; +} uint32_t ThreadPool::Start() { - if (!workerThreads_.empty() || threadCount_ <= 0) { - cerr << "Error: ThreadPool start failed." << endl; + if (threadCount_ <= 0) { + cerr << "Error: ThreadPool start failed, the thread count must be greater than zero." << endl; + return RESTOOL_ERROR; + } + if (!workerThreads_.empty()) { + cout << "Warning: ThreadPool is already started." << endl; return RESTOOL_ERROR; } running_ = true;