diff --git a/base/include/thread_pool.h b/base/include/thread_pool.h deleted file mode 100644 index 07caa4fff6901d6c36781fa77c98201a9e1e4e8a..0000000000000000000000000000000000000000 --- a/base/include/thread_pool.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef THREAD_POOL_H -#define THREAD_POOL_H - -#include "nocopyable.h" - -#include -#include -#include -#include -#include -#include -#include - -namespace OHOS { - -class ThreadPool : public NoCopyable { -public: - typedef std::function Task; - - explicit ThreadPool(const std::string &name = std::string()); - ~ThreadPool(); - - uint32_t Start(int threadsNum); - void Stop(); - void AddTask(const Task& f); - void SetMaxTaskNum(int maxSize) { maxTaskNum_ = maxSize; } - - // for testability - size_t GetMaxTaskNum() const { return maxTaskNum_; } - size_t GetCurTaskNum(); - size_t GetThreadsNum() const { return threads_.size(); } - std::string GetName() const { return myName_; } - -private: - // tasks in the queue reach the maximum set by maxQueueSize, means thread pool is full load. - bool Overloaded() const; - void WorkInThread(); // main function in each thread. - Task ScheduleTask(); // fetch a task from the queue and execute - -private: - std::string myName_; - std::mutex mutex_; - std::condition_variable hasTaskToDo_; - std::condition_variable acceptNewTask_; - std::vector threads_; - std::deque tasks_; - size_t maxTaskNum_; - bool running_; -}; - -} // namespace OHOS - -#endif - diff --git a/base/src/thread_pool.cpp b/base/src/thread_pool.cpp index 0eea4118dd3c70eb586562976f1193b26fbb6439..9951929fd05e0bcc4595d35df9dd49ad8cdab98c 100644 --- a/base/src/thread_pool.cpp +++ b/base/src/thread_pool.cpp @@ -15,8 +15,10 @@ #include "thread_pool.h" #include "errors.h" +#include "utils_log.h" #include +#include namespace OHOS { @@ -45,7 +47,13 @@ uint32_t ThreadPool::Start(int numThreads) threads_.reserve(numThreads); for (int i = 0; i < numThreads; ++i) { - threads_.push_back(std::thread(&ThreadPool::WorkInThread, this)); + std::thread t(&ThreadPool::WorkInThread, this); + // Give the name of ThreadPool to threads created by the ThreadPool. + int err = pthread_setname_np(t.native_handle(), (myName_ + std::to_string(i)).c_str()); + if (err != 0) { + UTILS_LOGW("Failed to set name to thread. %{public}s", strerror(err)); + } + threads_.push_back(std::move(t)); } return ERR_OK; } diff --git a/base/src/timer.cpp b/base/src/timer.cpp index 8302ace2fe6e7b4e5f0021cac4e9ede4a4498fd4..cbc1e9de3eda567091079bae746bd73db711ad8f 100644 --- a/base/src/timer.cpp +++ b/base/src/timer.cpp @@ -158,10 +158,11 @@ void Timer::DoUnregister(uint32_t interval) void Timer::OnTimer(int timerFd) { - uint32_t interval = timers_[timerFd]; + uint32_t interval; TimerEntryList entryList; { std::lock_guard lock(mutex_); + interval = timers_[timerFd]; entryList = intervalToTimers_[interval]; } diff --git a/base/test/unittest/common/utils_thread_pool_test.cpp b/base/test/unittest/common/utils_thread_pool_test.cpp index b8e62a8d640f769562c1df77cd0834964cbe6316..6030b03126fd268b9fab05a59c54b16d24ebab4b 100644 --- a/base/test/unittest/common/utils_thread_pool_test.cpp +++ b/base/test/unittest/common/utils_thread_pool_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -12,10 +12,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include -#include "thread_pool.h" + #include #include +#include +#include +#include "thread_pool.h" using namespace testing::ext; using namespace OHOS; @@ -269,3 +271,35 @@ HWTEST_F(UtilsThreadPoolTest, test_08, TestSize.Level0) pool.Stop(); } +void TestFuncGetName(const std::string& poolName) +{ + char name[16]; + prctl(PR_GET_NAME, name); + std::string nameStr(name); + size_t found = nameStr.find(poolName); + EXPECT_EQ(found, 0); +} + +/* + * Test_09 is used to verify the name set to ThreadPool will be set as the real name of threads in pool. + */ +HWTEST_F(UtilsThreadPoolTest, test_09, TestSize.Level0) +{ + std::string poolName("test_09_pool"); + ThreadPool pool(poolName); + pool.Start(5); + EXPECT_EQ(pool.GetName(), poolName); + EXPECT_EQ((int)pool.GetThreadsNum(), 5); + EXPECT_EQ((int)pool.GetCurTaskNum(), 0); + + for (int i = 0; i < 5; ++i) + { + auto task = std::bind(TestFuncGetName, poolName); + pool.AddTask(task); + } + + sleep(1); + // these tasks are endless Loop, 5 threads process 5 tasks, zero task remains in the task queue + EXPECT_EQ((int)pool.GetCurTaskNum(), 0); + pool.Stop(); +}