diff --git a/base/include/thread_pool.h b/base/include/thread_pool.h index 07caa4fff6901d6c36781fa77c98201a9e1e4e8a..e2d183810b02775c71b313b60c82b2599dbb3cb5 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 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(); +}