Skip to content

Commit

Permalink
Refs 20862: Apply Miguel suggestions
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>
  • Loading branch information
Mario-DL committed May 6, 2024
1 parent e840ccd commit cf415d3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 75 deletions.
27 changes: 7 additions & 20 deletions src/cpp/utils/threading.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef UTILS__THREADING_HPP_
#define UTILS__THREADING_HPP_

#include <array>

#include "./thread.hpp"

namespace eprosima {
Expand All @@ -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<char, 16>& thread_name_buffer,
const char* name);

/**
Expand All @@ -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<char, 16>& thread_name_buffer,
const char* fmt,
uint32_t arg);

Expand Down Expand Up @@ -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<char, 16>& thread_name_buffer,
const char* fmt,
uint32_t arg1,
uint32_t arg2);
Expand Down Expand Up @@ -129,9 +116,9 @@ eprosima::thread create_thread(
{
return eprosima::thread(settings.stack_size, [=]()
{
char thread_name_buffer[16]{};
std::array<char, 16> 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();
});
}
Expand Down
7 changes: 0 additions & 7 deletions src/cpp/utils/threading/threading_empty.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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*/)
Expand Down
24 changes: 8 additions & 16 deletions src/cpp/utils/threading/threading_osx.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <array>
#include <cstdio>
#include <cstring>
#include <limits>
Expand All @@ -26,23 +27,23 @@ namespace eprosima {

template<typename ... Args>
static void set_name_to_current_thread_impl(
char* thread_name_buffer,
std::array<char, 16>& 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<char, 16>& 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<char, 16>& thread_name_buffer,
const char* fmt,
uint32_t arg)
{
Expand All @@ -53,28 +54,19 @@ void set_name_to_current_thread(
const char* fmt,
uint32_t arg)
{
char thread_name_buffer[16];
std::array<char, 16> 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<char, 16>& thread_name_buffer,
const char* fmt,
uint32_t arg1,
uint32_t arg2)
{
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,
Expand Down
24 changes: 8 additions & 16 deletions src/cpp/utils/threading/threading_pthread.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <array>
#include <cstdio>
#include <cstring>
#include <limits>
Expand All @@ -29,24 +30,24 @@ namespace eprosima {

template<typename ... Args>
static void set_name_to_current_thread_impl(
char* thread_name_buffer,
std::array<char, 16>& 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<char, 16>& 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<char, 16>& thread_name_buffer,
const char* fmt,
uint32_t arg)
{
Expand All @@ -57,28 +58,19 @@ void set_name_to_current_thread(
const char* fmt,
uint32_t arg)
{
char thread_name_buffer[16];
std::array<char, 16> 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<char, 16>& thread_name_buffer,
const char* fmt,
uint32_t arg1,
uint32_t arg2)
{
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,
Expand Down
24 changes: 8 additions & 16 deletions src/cpp/utils/threading/threading_win32.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <array>
#include <limits>
#include <sstream>
#include <string>
Expand All @@ -23,28 +24,28 @@ namespace eprosima {

template<typename ... Args>
static void set_name_to_current_thread_impl(
char* thread_name_buffer,
std::array<char, 16>& 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<char, 16>& 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<char, 16>& thread_name_buffer,
const char* fmt,
uint32_t arg)
{
Expand All @@ -55,28 +56,19 @@ void set_name_to_current_thread(
const char* fmt,
uint32_t arg)
{
char thread_name_buffer[16];
std::array<char, 16> 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<char, 16>& thread_name_buffer,
const char* fmt,
uint32_t arg1,
uint32_t arg2)
{
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)
Expand Down

0 comments on commit cf415d3

Please sign in to comment.