Skip to content

Commit

Permalink
Platform implementations for set_name_to_current_thread (#3823)
Browse files Browse the repository at this point in the history
* Refs #17492. Added pthread implementation for set_name_to_current_thread

Signed-off-by: Miguel Company <[email protected]>

* Refs #17492. Added implementation for Windows.

Signed-off-by: Miguel Company <[email protected]>

* Refs #17492. Added implementation for Mac.

Signed-off-by: Miguel Company <[email protected]>

* Refs #17492. Added doxygen to threading.hpp

Signed-off-by: Miguel Company <[email protected]>

* Refs #17492. Using pthread implementation for Android.

Signed-off-by: Miguel Company <[email protected]>

* Refs #17492. Fix build error on snprintf.

Signed-off-by: Miguel Company <[email protected]>

---------

Signed-off-by: Miguel Company <[email protected]>
  • Loading branch information
MiguelCompany authored and EduPonz committed Nov 20, 2023
1 parent 1e3dbb3 commit 50c0787
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cpp/utils/SystemInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,12 @@ std::string SystemInfo::environment_file_;
} // eprosima

// threading.hpp implementations
#ifdef _WIN32
#include "threading/threading_win32.ipp"
#elif defined(__APPLE__)
#include "threading/threading_osx.ipp"
#elif defined(_POSIX_SOURCE) || defined(__QNXNTO__) || defined(__ANDROID__)
#include "threading/threading_pthread.ipp"
#else
#include "threading/threading_empty.ipp"
#endif // Platform selection
26 changes: 26 additions & 0 deletions src/cpp/utils/threading.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,39 @@

namespace eprosima {

/**
* @brief Give a name to the thread calling this function.
*
* @param[in] name A null-terminated string with the name to give to the calling thread.
* The implementation for certain platforms may truncate the final thread
* name if there is a limit on the length of the name of a thread.
*/
void set_name_to_current_thread(
const char* name);

/**
* @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] arg Single variadic argument passed to the formatting function.
*/
void set_name_to_current_thread(
const char* fmt,
uint32_t arg);

/**
* @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(
const char* fmt,
uint32_t arg1,
Expand Down
51 changes: 51 additions & 0 deletions src/cpp/utils/threading/threading_osx.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.

#include <pthread.h>
#include <string.h>
#include <stdio.h>

namespace eprosima {

template<typename... Args>
static void set_name_to_current_thread_impl(
const char* fmt, Args... args)
{
char thread_name[16]{};
snprintf(thread_name, 16, fmt, args...);
pthread_setname_np(thread_name);
}

void set_name_to_current_thread(
const char* name)
{
set_name_to_current_thread_impl("%s", name);
}

void set_name_to_current_thread(
const char* fmt,
uint32_t arg)
{
set_name_to_current_thread_impl(fmt, arg);
}

void set_name_to_current_thread(
const char* fmt,
uint32_t arg1,
uint32_t arg2)
{
set_name_to_current_thread_impl(fmt, arg1, arg2);
}

} // namespace eprosima
52 changes: 52 additions & 0 deletions src/cpp/utils/threading/threading_pthread.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.

#include <pthread.h>
#include <string.h>
#include <stdio.h>

namespace eprosima {

template<typename... Args>
static void set_name_to_current_thread_impl(
const char* fmt, Args... args)
{
char thread_name[16]{};
snprintf(thread_name, 16, fmt, args...);
auto id = pthread_self();
pthread_setname_np(id, thread_name);
}

void set_name_to_current_thread(
const char* name)
{
set_name_to_current_thread_impl("%s", name);
}

void set_name_to_current_thread(
const char* fmt,
uint32_t arg)
{
set_name_to_current_thread_impl(fmt, arg);
}

void set_name_to_current_thread(
const char* fmt,
uint32_t arg1,
uint32_t arg2)
{
set_name_to_current_thread_impl(fmt, arg1, arg2);
}

} // namespace eprosima
56 changes: 56 additions & 0 deletions src/cpp/utils/threading/threading_win32.ipp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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.

#include <sstream>
#include <string>
#include <processthreadsapi.h>

namespace eprosima {

template<typename... Args>
static void set_name_to_current_thread_impl(
const char* fmt, Args... args)
{
char thread_name[16]{};
snprintf(thread_name, 16, fmt, args...);

std::wstringstream stream;
stream << thread_name;
std::wstring w_thread_name = stream.str();

SetThreadDescription(GetCurrentThread(), w_thread_name.c_str());
}

void set_name_to_current_thread(
const char* name)
{
set_name_to_current_thread_impl("%s", name);
}

void set_name_to_current_thread(
const char* fmt,
uint32_t arg)
{
set_name_to_current_thread_impl(fmt, arg);
}

void set_name_to_current_thread(
const char* fmt,
uint32_t arg1,
uint32_t arg2)
{
set_name_to_current_thread_impl(fmt, arg1, arg2);
}

} // namespace eprosima

0 comments on commit 50c0787

Please sign in to comment.