-
Notifications
You must be signed in to change notification settings - Fork 774
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Platform implementations for set_name_to_current_thread (#3823)
* 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
1 parent
4d6f001
commit b10f4fb
Showing
5 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |