From cf415d32929af79f89f84f2d0f30f7971883174e Mon Sep 17 00:00:00 2001 From: Mario Dominguez Date: Mon, 6 May 2024 15:50:14 +0200 Subject: [PATCH] Refs 20862: Apply Miguel suggestions Signed-off-by: Mario Dominguez --- src/cpp/utils/threading.hpp | 27 +++++-------------- src/cpp/utils/threading/threading_empty.ipp | 7 ----- src/cpp/utils/threading/threading_osx.ipp | 24 ++++++----------- src/cpp/utils/threading/threading_pthread.ipp | 24 ++++++----------- src/cpp/utils/threading/threading_win32.ipp | 24 ++++++----------- 5 files changed, 31 insertions(+), 75 deletions(-) diff --git a/src/cpp/utils/threading.hpp b/src/cpp/utils/threading.hpp index cfa969fffd3..d73720509f1 100644 --- a/src/cpp/utils/threading.hpp +++ b/src/cpp/utils/threading.hpp @@ -15,6 +15,8 @@ #ifndef UTILS__THREADING_HPP_ #define UTILS__THREADING_HPP_ +#include + #include "./thread.hpp" namespace eprosima { @@ -35,7 +37,7 @@ struct ThreadSettings; * name if there is a limit on the length of the name of a thread. */ void set_name_to_current_thread( - char* thread_name_buffer, + std::array& thread_name_buffer, const char* name); /** @@ -49,7 +51,7 @@ void set_name_to_current_thread( * @param[in] arg Single variadic argument passed to the formatting function. */ void set_name_to_current_thread( - char* thread_name_buffer, + std::array& thread_name_buffer, const char* fmt, uint32_t arg); @@ -78,22 +80,7 @@ void set_name_to_current_thread( * @param[in] arg2 Second variadic argument passed to the formatting function. */ void set_name_to_current_thread( - char* thread_name_buffer, - const char* fmt, - uint32_t arg1, - uint32_t arg2); - -/** - * @brief Give a name to the thread calling this function. - * - * @param[in] fmt A null-terminated string to be used as the format argument of - * a `snprintf` like function, in order to accomodate the restrictions of - * the OS. Those restrictions may truncate the final thread name if there - * is a limit on the length of the name of a thread. - * @param[in] arg1 First variadic argument passed to the formatting function. - * @param[in] arg2 Second variadic argument passed to the formatting function. - */ -void set_name_to_current_thread( + std::array& thread_name_buffer, const char* fmt, uint32_t arg1, uint32_t arg2); @@ -129,9 +116,9 @@ eprosima::thread create_thread( { return eprosima::thread(settings.stack_size, [=]() { - char thread_name_buffer[16]{}; + std::array thread_name_buffer; set_name_to_current_thread(thread_name_buffer, name, args ...); - apply_thread_settings_to_current_thread(thread_name_buffer, settings); + apply_thread_settings_to_current_thread(thread_name_buffer.begin(), settings); func(); }); } diff --git a/src/cpp/utils/threading/threading_empty.ipp b/src/cpp/utils/threading/threading_empty.ipp index deab638a6d1..30edd7fb260 100644 --- a/src/cpp/utils/threading/threading_empty.ipp +++ b/src/cpp/utils/threading/threading_empty.ipp @@ -49,13 +49,6 @@ void set_name_to_current_thread( { } -void set_name_to_current_thread( - const char* /* fmt */, - uint32_t /* arg1 */, - uint32_t /* arg2 */) -{ -} - void apply_thread_settings_to_current_thread( const char* /* thread_name */, const fastdds::rtps::ThreadSettings& /*settings*/) diff --git a/src/cpp/utils/threading/threading_osx.ipp b/src/cpp/utils/threading/threading_osx.ipp index 8ccd3cff4b0..6a4d04c25ae 100644 --- a/src/cpp/utils/threading/threading_osx.ipp +++ b/src/cpp/utils/threading/threading_osx.ipp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include #include @@ -26,23 +27,23 @@ namespace eprosima { template static void set_name_to_current_thread_impl( - char* thread_name_buffer, + std::array& thread_name_buffer, const char* fmt, Args... args) { - snprintf(thread_name_buffer, 16, fmt, args ...); - pthread_setname_np(thread_name_buffer); + snprintf(thread_name_buffer.begin(), 16, fmt, args ...); + pthread_setname_np(thread_name_buffer.begin()); } void set_name_to_current_thread( - char* thread_name_buffer, + std::array& thread_name_buffer, const char* name) { set_name_to_current_thread_impl(thread_name_buffer, "%s", name); } void set_name_to_current_thread( - char* thread_name_buffer, + std::array& thread_name_buffer, const char* fmt, uint32_t arg) { @@ -53,12 +54,12 @@ void set_name_to_current_thread( const char* fmt, uint32_t arg) { - char thread_name_buffer[16]; + std::array thread_name_buffer; set_name_to_current_thread(thread_name_buffer, fmt, arg); } void set_name_to_current_thread( - char* thread_name_buffer, + std::array& thread_name_buffer, const char* fmt, uint32_t arg1, uint32_t arg2) @@ -66,15 +67,6 @@ void set_name_to_current_thread( set_name_to_current_thread_impl(thread_name_buffer, fmt, arg1, arg2); } -void set_name_to_current_thread( - const char* fmt, - uint32_t arg1, - uint32_t arg2) -{ - char thread_name_buffer[16]; - set_name_to_current_thread(thread_name_buffer, fmt, arg1, arg2); -} - static void configure_current_thread_scheduler( const char* thread_name, int sched_class, diff --git a/src/cpp/utils/threading/threading_pthread.ipp b/src/cpp/utils/threading/threading_pthread.ipp index ed45e18f540..585415dd814 100644 --- a/src/cpp/utils/threading/threading_pthread.ipp +++ b/src/cpp/utils/threading/threading_pthread.ipp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include #include @@ -29,24 +30,24 @@ namespace eprosima { template static void set_name_to_current_thread_impl( - char* thread_name_buffer, + std::array& thread_name_buffer, const char* fmt, Args... args) { - snprintf(thread_name_buffer, 16, fmt, args ...); + snprintf(thread_name_buffer.begin(), 16, fmt, args ...); auto id = pthread_self(); - pthread_setname_np(id, thread_name_buffer); + pthread_setname_np(id, thread_name_buffer.begin()); } void set_name_to_current_thread( - char* thread_name_buffer, + std::array& thread_name_buffer, const char* name) { set_name_to_current_thread_impl(thread_name_buffer, "%s", name); } void set_name_to_current_thread( - char* thread_name_buffer, + std::array& thread_name_buffer, const char* fmt, uint32_t arg) { @@ -57,12 +58,12 @@ void set_name_to_current_thread( const char* fmt, uint32_t arg) { - char thread_name_buffer[16]; + std::array thread_name_buffer; set_name_to_current_thread(thread_name_buffer, fmt, arg); } void set_name_to_current_thread( - char* thread_name_buffer, + std::array& thread_name_buffer, const char* fmt, uint32_t arg1, uint32_t arg2) @@ -70,15 +71,6 @@ void set_name_to_current_thread( set_name_to_current_thread_impl(thread_name_buffer, fmt, arg1, arg2); } -void set_name_to_current_thread( - const char* fmt, - uint32_t arg1, - uint32_t arg2) -{ - char thread_name_buffer[16]; - set_name_to_current_thread(thread_name_buffer, fmt, arg1, arg2); -} - static void configure_current_thread_scheduler( const char* thread_name, int sched_class, diff --git a/src/cpp/utils/threading/threading_win32.ipp b/src/cpp/utils/threading/threading_win32.ipp index 9d94f9c57dd..e7af52666e1 100644 --- a/src/cpp/utils/threading/threading_win32.ipp +++ b/src/cpp/utils/threading/threading_win32.ipp @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include #include @@ -23,28 +24,28 @@ namespace eprosima { template static void set_name_to_current_thread_impl( - char* thread_name_buffer, + std::array& thread_name_buffer, const char* fmt, Args... args) { - snprintf(thread_name_buffer, 16, fmt, args ...); + snprintf(thread_name_buffer.begin(), 16, fmt, args ...); std::wstringstream stream; - stream << thread_name_buffer; + stream << thread_name_buffer.begin(); std::wstring w_thread_name = stream.str(); SetThreadDescription(GetCurrentThread(), w_thread_name.c_str()); } void set_name_to_current_thread( - char* thread_name_buffer, + std::array& thread_name_buffer, const char* name) { set_name_to_current_thread_impl(thread_name_buffer, "%s", name); } void set_name_to_current_thread( - char* thread_name_buffer, + std::array& thread_name_buffer, const char* fmt, uint32_t arg) { @@ -55,12 +56,12 @@ void set_name_to_current_thread( const char* fmt, uint32_t arg) { - char thread_name_buffer[16]; + std::array thread_name_buffer; set_name_to_current_thread(thread_name_buffer, fmt, arg); } void set_name_to_current_thread( - char* thread_name_buffer, + std::array& thread_name_buffer, const char* fmt, uint32_t arg1, uint32_t arg2) @@ -68,15 +69,6 @@ void set_name_to_current_thread( set_name_to_current_thread_impl(thread_name_buffer, fmt, arg1, arg2); } -void set_name_to_current_thread( - const char* fmt, - uint32_t arg1, - uint32_t arg2) -{ - char thread_name_buffer[16]; - set_name_to_current_thread(thread_name_buffer, fmt, arg1, arg2); -} - static void configure_current_thread_priority( const char* thread_name, int32_t priority)