From 3b5344b0f7f75413f871fefb8de48b4c15ade2f9 Mon Sep 17 00:00:00 2001 From: Andre Puschmann Date: Fri, 30 Jul 2021 12:10:50 +0200 Subject: [PATCH] thread_pool: add optional ID to thread pool this allows to prepend an ID to each thread pool to better differentiate workers from different pools --- lib/include/srsran/common/thread_pool.h | 26 +++++++++++++------------ lib/src/common/thread_pool.cc | 11 ++++++++--- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/lib/include/srsran/common/thread_pool.h b/lib/include/srsran/common/thread_pool.h index 0c3eb1674..bc0973d64 100644 --- a/lib/include/srsran/common/thread_pool.h +++ b/lib/include/srsran/common/thread_pool.h @@ -54,8 +54,8 @@ public: virtual void work_imp() = 0; private: - uint32_t my_id = 0; - thread_pool* my_parent = nullptr; + uint32_t my_id = 0; + thread_pool* my_parent = nullptr; std::atomic running = {true}; void run_thread(); @@ -64,22 +64,24 @@ public: bool is_stopped() const; }; - thread_pool(uint32_t nof_workers); - void init_worker(uint32_t id, worker*, uint32_t prio = 0, uint32_t mask = 255); - void stop(); - worker* wait_worker_id(uint32_t id); - worker* wait_worker(uint32_t tti); - worker* wait_worker_nb(uint32_t tti); - void start_worker(worker*); - void start_worker(uint32_t id); - worker* get_worker(uint32_t id); - uint32_t get_nof_workers(); + thread_pool(uint32_t nof_workers_, std::string id_ = ""); + void init_worker(uint32_t id, worker*, uint32_t prio = 0, uint32_t mask = 255); + void stop(); + worker* wait_worker_id(uint32_t id); + worker* wait_worker(uint32_t tti); + worker* wait_worker_nb(uint32_t tti); + void start_worker(worker*); + void start_worker(uint32_t id); + worker* get_worker(uint32_t id); + uint32_t get_nof_workers(); + std::string get_id(); private: bool find_finished_worker(uint32_t tti, uint32_t* id); typedef enum { STOP, IDLE, START_WORK, WORKER_READY, WORKING } worker_status; + std::string id; // id is prepended to every worker std::vector workers = {}; uint32_t nof_workers = 0; uint32_t max_workers = 0; diff --git a/lib/src/common/thread_pool.cc b/lib/src/common/thread_pool.cc index e74111d6b..f01327c75 100644 --- a/lib/src/common/thread_pool.cc +++ b/lib/src/common/thread_pool.cc @@ -41,7 +41,7 @@ void thread_pool::worker::setup(uint32_t id, thread_pool* parent, uint32_t prio, void thread_pool::worker::run_thread() { - set_name(std::string("WORKER") + std::to_string(my_id)); + set_name(my_parent->get_id() + std::string("WORKER") + std::to_string(my_id)); while (running.load(std::memory_order_relaxed)) { wait_to_start(); if (running.load(std::memory_order_relaxed)) { @@ -61,9 +61,9 @@ uint32_t thread_pool::worker::get_id() return my_id; } -thread_pool::thread_pool(uint32_t max_workers_) : workers(max_workers_), status(max_workers_), cvar_worker(max_workers_) +thread_pool::thread_pool(uint32_t max_workers_, std::string id_) : + workers(max_workers_), max_workers(max_workers_), status(max_workers_), cvar_worker(max_workers_), id(id_) { - max_workers = max_workers_; for (uint32_t i = 0; i < max_workers; i++) { workers[i] = NULL; status[i] = IDLE; @@ -253,6 +253,11 @@ uint32_t thread_pool::get_nof_workers() return nof_workers; } +std::string thread_pool::get_id() +{ + return id; +} + /************************************************************************** * task_thread_pool - uses a queue to enqueue callables, that start * once a worker is available