diff --git a/cmake/Config.h.cmake b/cmake/Config.h.cmake index f0eced7cc..eaf629201 100644 --- a/cmake/Config.h.cmake +++ b/cmake/Config.h.cmake @@ -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 diff --git a/cmake/TestPThreadName.cpp b/cmake/TestPThreadName.cpp new file mode 100644 index 000000000..947fd41ee --- /dev/null +++ b/cmake/TestPThreadName.cpp @@ -0,0 +1,11 @@ +#include +#include + +#include + +int main() +{ + static_assert(std::is_same::value, "std::thread::native_handle_type have to be pthread_t"); + + return pthread_setname_np(pthread_self(), "Test") == 0; +} diff --git a/cmake/features.cmake b/cmake/features.cmake index 5fb120cd2..3c3bb3f52 100644 --- a/cmake/features.cmake +++ b/cmake/features.cmake @@ -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}" diff --git a/libosmscout-map/src/osmscoutmap/MapService.cpp b/libosmscout-map/src/osmscoutmap/MapService.cpp index 684c3b27f..e6792947a 100644 --- a/libosmscout-map/src/osmscoutmap/MapService.cpp +++ b/libosmscout-map/src/osmscoutmap/MapService.cpp @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -613,6 +614,8 @@ namespace osmscout { void MapService::NodeWorkerLoop() { + SetThreadName("NodeLoader"); + std::packaged_task task; while (nodeWorkerQueue.PopTask(task)) { @@ -622,6 +625,8 @@ namespace osmscout { void MapService::WayWorkerLoop() { + SetThreadName("WayLoader"); + std::packaged_task task; while (wayWorkerQueue.PopTask(task)) { @@ -631,6 +636,8 @@ namespace osmscout { void MapService::WayLowZoomWorkerLoop() { + SetThreadName("WayLowZoomLoader"); + std::packaged_task task; while (wayLowZoomWorkerQueue.PopTask(task)) { @@ -640,6 +647,8 @@ namespace osmscout { void MapService::AreaWorkerLoop() { + SetThreadName("AreaLoader"); + std::packaged_task task; while (areaWorkerQueue.PopTask(task)) { @@ -649,6 +658,8 @@ namespace osmscout { void MapService::AreaLowZoomWorkerLoop() { + SetThreadName("AreaLowZoomLoader"); + std::packaged_task task; while (areaLowZoomWorkerQueue.PopTask(task)) { @@ -658,6 +669,8 @@ namespace osmscout { void MapService::RouteWorkerLoop() { + SetThreadName("RouteLoader"); + std::packaged_task task; while (routeWorkerQueue.PopTask(task)) { diff --git a/libosmscout/CMakeLists.txt b/libosmscout/CMakeLists.txt index 2ec1b203e..47edfd211 100644 --- a/libosmscout/CMakeLists.txt +++ b/libosmscout/CMakeLists.txt @@ -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 @@ -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 @@ -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 diff --git a/libosmscout/include/meson.build b/libosmscout/include/meson.build index aff701d76..598e1b8d0 100644 --- a/libosmscout/include/meson.build +++ b/libosmscout/include/meson.build @@ -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', diff --git a/libosmscout/include/osmscout/system/Thread.h b/libosmscout/include/osmscout/system/Thread.h new file mode 100644 index 000000000..2effb606b --- /dev/null +++ b/libosmscout/include/osmscout/system/Thread.h @@ -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 + +#include +#include + +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 diff --git a/libosmscout/src/meson.build b/libosmscout/src/meson.build index 4074704ab..bc4aba7cc 100644 --- a/libosmscout/src/meson.build +++ b/libosmscout/src/meson.build @@ -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', diff --git a/libosmscout/src/osmscout/system/Thread.cpp b/libosmscout/src/osmscout/system/Thread.cpp new file mode 100644 index 000000000..bc745ccf9 --- /dev/null +++ b/libosmscout/src/osmscout/system/Thread.cpp @@ -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 +#include + +#ifdef OSMSCOUT_PTHREAD_NAME +#include + +#include + +static_assert(std::is_same::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 +} + +}