From 6311f5fbe5c108a85babba8a392122752101ddfd Mon Sep 17 00:00:00 2001 From: liujialiang Date: Tue, 6 Sep 2022 19:27:40 +0800 Subject: [PATCH 1/3] Add a new feature that the name user gives to class "ThreadPool" will be the real name of thread the pool create. Signed-off-by: liujialiang Change-Id: Ia0e7bab9a15f08509a07ed2f12928bcdf140b26b --- base/include/thread_pool.h | 5 +++ base/src/thread_pool.cpp | 10 ++++- .../common/utils_thread_pool_test.cpp | 40 +++++++++++++++++-- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/base/include/thread_pool.h b/base/include/thread_pool.h index 07caa4f..e2d1838 100644 --- a/base/include/thread_pool.h +++ b/base/include/thread_pool.h @@ -31,6 +31,11 @@ class ThreadPool : public NoCopyable { public: typedef std::function Task; + // The name(args here) will be set as a part the real name of threads in pool. + // The real name of threads in pool will be like: myName_ + no. + // The thread name is a meaningful C language string, whose length + // is restricted to 16 characters, including the terminating null byte ('\0'). + // Please pay attention to the length of name(args here). explicit ThreadPool(const std::string &name = std::string()); ~ThreadPool(); diff --git a/base/src/thread_pool.cpp b/base/src/thread_pool.cpp index 0eea411..9951929 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/test/unittest/common/utils_thread_pool_test.cpp b/base/test/unittest/common/utils_thread_pool_test.cpp index b8e62a8..6030b03 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(); +} -- Gitee From 83b1f1149488a83a8ddb42b2c407b851071b8ca6 Mon Sep 17 00:00:00 2001 From: liujialiang Date: Thu, 22 Sep 2022 21:56:07 +0800 Subject: [PATCH 2/3] Put "timers_" under lock. Signed-off-by: liujialiang --- base/src/timer.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/src/timer.cpp b/base/src/timer.cpp index 8302ace..cbc1e9d 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]; } -- Gitee From 2b8c456ce7356be02d65e29e5b15c37aa3bea3a2 Mon Sep 17 00:00:00 2001 From: JokerXD_liu Date: Mon, 26 Sep 2022 02:28:03 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20base?= =?UTF-8?q?/include/thread=5Fpool.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/include/thread_pool.h | 73 -------------------------------------- 1 file changed, 73 deletions(-) delete mode 100644 base/include/thread_pool.h diff --git a/base/include/thread_pool.h b/base/include/thread_pool.h deleted file mode 100644 index e2d1838..0000000 --- a/base/include/thread_pool.h +++ /dev/null @@ -1,73 +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; - - // The name(args here) will be set as a part the real name of threads in pool. - // The real name of threads in pool will be like: myName_ + no. - // The thread name is a meaningful C language string, whose length - // is restricted to 16 characters, including the terminating null byte ('\0'). - // Please pay attention to the length of name(args here). - 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 - -- Gitee