Skip to content

Commit

Permalink
Merge pull request #1454 from Karry/thread-name
Browse files Browse the repository at this point in the history
add utility for renaming threads for simpler debugging
  • Loading branch information
Framstag committed Jun 19, 2023
2 parents 42194dc + 725f1ac commit d09ca7a
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cmake/Config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,9 @@
#cmakedefine OSMSCOUT_MAP_SVG_HAVE_LIB_PANGO 1
#endif

#ifndef OSMSCOUT_PTHREAD_NAME
/* Threads are pthreads and non-posix setname is available */
#cmakedefine OSMSCOUT_PTHREAD_NAME
#endif

#endif // @OSMSCOUT_PRIVATE_CONFIG_HEADER_NAME@_PRIVATE_CONFIG_H
11 changes: 11 additions & 0 deletions cmake/TestPThreadName.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <type_traits>
#include <thread>

#include <pthread.h>

int main()
{
static_assert(std::is_same<std::thread::native_handle_type, pthread_t>::value, "std::thread::native_handle_type have to be pthread_t");

return pthread_setname_np(pthread_self(), "Test") == 0;
}
7 changes: 7 additions & 0 deletions cmake/features.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ if(THREADS_HAVE_PTHREAD_ARG)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${THREADS_PTHREAD_ARG}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${THREADS_PTHREAD_ARG}")
endif()

try_compile(PTHREAD_NAME_OK "${PROJECT_BINARY_DIR}"
"${PROJECT_SOURCE_DIR}/cmake/TestPThreadName.cpp")
if(PTHREAD_NAME_OK)
set(OSMSCOUT_PTHREAD_NAME TRUE)
endif()

find_package(TBB QUIET)
if (TBB_FOUND)
try_compile(TBB_HAS_SCHEDULER_INIT "${PROJECT_BINARY_DIR}"
Expand Down
13 changes: 13 additions & 0 deletions libosmscout-map/src/osmscoutmap/MapService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <osmscout/system/Assert.h>
#include <osmscout/system/Math.h>
#include <osmscout/system/Thread.h>

#include <osmscout/util/Geometry.h>
#include <osmscout/util/Logger.h>
Expand Down Expand Up @@ -613,6 +614,8 @@ namespace osmscout {

void MapService::NodeWorkerLoop()
{
SetThreadName("NodeLoader");

std::packaged_task<bool()> task;

while (nodeWorkerQueue.PopTask(task)) {
Expand All @@ -622,6 +625,8 @@ namespace osmscout {

void MapService::WayWorkerLoop()
{
SetThreadName("WayLoader");

std::packaged_task<bool()> task;

while (wayWorkerQueue.PopTask(task)) {
Expand All @@ -631,6 +636,8 @@ namespace osmscout {

void MapService::WayLowZoomWorkerLoop()
{
SetThreadName("WayLowZoomLoader");

std::packaged_task<bool()> task;

while (wayLowZoomWorkerQueue.PopTask(task)) {
Expand All @@ -640,6 +647,8 @@ namespace osmscout {

void MapService::AreaWorkerLoop()
{
SetThreadName("AreaLoader");

std::packaged_task<bool()> task;

while (areaWorkerQueue.PopTask(task)) {
Expand All @@ -649,6 +658,8 @@ namespace osmscout {

void MapService::AreaLowZoomWorkerLoop()
{
SetThreadName("AreaLowZoomLoader");

std::packaged_task<bool()> task;

while (areaLowZoomWorkerQueue.PopTask(task)) {
Expand All @@ -658,6 +669,8 @@ namespace osmscout {

void MapService::RouteWorkerLoop()
{
SetThreadName("RouteLoader");

std::packaged_task<bool()> task;

while (routeWorkerQueue.PopTask(task)) {
Expand Down
6 changes: 4 additions & 2 deletions libosmscout/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ set(HEADER_FILES_SYSTEM
include/osmscout/system/Math.h
include/osmscout/system/SSEMath.h
include/osmscout/system/SSEMathPublic.h
include/osmscout/system/SystemTypes.h)
include/osmscout/system/SystemTypes.h
include/osmscout/system/Thread.h)

set(HEADER_FILES_UTIL
include/osmscout/util/Base64.h
Expand Down Expand Up @@ -259,6 +260,7 @@ set(SOURCE_FILES
src/osmscout/routing/RouteDescriptionPostprocessor.cpp
src/osmscout/routing/RouteDataFile.cpp
src/osmscout/system/SSEMath.cpp
src/osmscout/system/Thread.cpp
src/osmscout/util/Bearing.cpp
src/osmscout/util/Breaker.cpp
src/osmscout/util/Cache.cpp
Expand Down Expand Up @@ -326,7 +328,7 @@ set(SOURCE_FILES
src/osmscout/GroundTile.cpp
src/osmscout/Intersection.cpp
src/osmscout/Node.cpp
src/osmscout/io/NumericIndex.cpp
src/osmscout/io/NumericIndex.cpp
src/osmscout/ObjectRef.cpp
src/osmscout/Path.cpp
src/osmscout/Pixel.cpp
Expand Down
1 change: 1 addition & 0 deletions libosmscout/include/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ osmscoutHeader = [
'osmscout/system/SSEMath.h',
'osmscout/system/SSEMathPublic.h',
'osmscout/system/SystemTypes.h',
'osmscout/system/Thread.h',
'osmscout/util/Base64.h',
'osmscout/util/Bearing.h',
'osmscout/util/Breaker.h',
Expand Down
46 changes: 46 additions & 0 deletions libosmscout/include/osmscout/system/Thread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef LIBOSMSCOUT_THREAD_H
#define LIBOSMSCOUT_THREAD_H

/*
This source is part of the libosmscout library
Copyright (C) 2023 Lukas Karas
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include <osmscout/CoreImportExport.h>

#include <string>
#include <thread>

namespace osmscout {

/** Try to set current thread name.
*
* @param name
* @return true if supported and successful, else otherwise
*/
extern OSMSCOUT_API bool SetThreadName(const std::string &name);

/** Try to set thread name
*
* @param thread
* @param name
* @return true if supported and successful, else otherwise
*/
extern OSMSCOUT_API bool SetThreadName(std::thread &thread, const std::string &name);
}

#endif //LIBOSMSCOUT_THREAD_H
1 change: 1 addition & 0 deletions libosmscout/src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ osmscoutSrc = [
'src/osmscout/routing/TurnRestriction.cpp',
'src/osmscout/routing/MultiDBRoutingState.cpp',
'src/osmscout/system/SSEMath.cpp',
'src/osmscout/system/Thread.cpp',
'src/osmscout/util/Breaker.cpp',
'src/osmscout/util/Bearing.cpp',
'src/osmscout/util/Cache.cpp',
Expand Down
51 changes: 51 additions & 0 deletions libosmscout/src/osmscout/system/Thread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
This source is part of the libosmscout library
Copyright (C) 2023 Lukas Karas
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include <osmscout/private/Config.h>
#include <osmscout/system/Thread.h>

#ifdef OSMSCOUT_PTHREAD_NAME
#include <type_traits>

#include <pthread.h>

static_assert(std::is_same<std::thread::native_handle_type, pthread_t>::value, "std::thread::native_handle_type have to be pthread_t");
#endif

namespace osmscout {

bool SetThreadName([[maybe_unused]] const std::string &name)
{
#ifdef OSMSCOUT_PTHREAD_NAME
return pthread_setname_np(pthread_self(), name.c_str()) == 0;
#else
return false;
#endif
}

extern OSMSCOUT_API bool SetThreadName(std::thread &thread, const std::string &name)
{
#ifdef OSMSCOUT_PTHREAD_NAME
return pthread_setname_np(thread.native_handle(), name.c_str()) == 0;
#else
return false;
#endif
}

}

0 comments on commit d09ca7a

Please sign in to comment.