From 371d0076f1ddbbbb9d939d9902c6e1e8048a732e Mon Sep 17 00:00:00 2001 From: Yangosoft Date: Wed, 22 Jan 2025 20:50:10 +0100 Subject: [PATCH] Test priority management --- src/CMakeLists.txt | 3 +- src/include/cpputils2/cpputils2.hpp | 13 +++ src/include/cpputils2/linux/thread/thread.hpp | 88 +++++++++++++++++++ src/test/cpputils2_tests.cpp | 12 +++ 4 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 src/include/cpputils2/cpputils2.hpp create mode 100644 src/include/cpputils2/linux/thread/thread.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 51bbaae..837a798 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,7 +7,7 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") endif() -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 23) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Pthread @@ -47,6 +47,7 @@ else() include/cpputils2/linux/shm/shm.hpp include/cpputils2/linux/futex/futex.hpp include/cpputils2/linux/futex/shared_futex.hpp + include/cpputils2/linux/thread/thread.hpp ) endif() diff --git a/src/include/cpputils2/cpputils2.hpp b/src/include/cpputils2/cpputils2.hpp new file mode 100644 index 0000000..58dd247 --- /dev/null +++ b/src/include/cpputils2/cpputils2.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "cpputils2/common/types.hpp" + +#include +#include + +#define CPPUTILS2_VERSION "1.0.0" + +namespace CppUtils2 +{ + const std::string VERSION{CPPUTILS2_VERSION}; +} diff --git a/src/include/cpputils2/linux/thread/thread.hpp b/src/include/cpputils2/linux/thread/thread.hpp new file mode 100644 index 0000000..01cda07 --- /dev/null +++ b/src/include/cpputils2/linux/thread/thread.hpp @@ -0,0 +1,88 @@ +/** + * @file thread.hpp + * @brief Various thread utilities + * + * + */ + +#pragma once + +#include "cpputils2/common/types.hpp" + +#include +#include +#include +#include + +#include +#include +#include + +namespace CppUtils2 +{ + struct ThreadConfig + { + int policy; + int priority; + }; + + struct RealTimeThreadConfig : public ThreadConfig + { + RealTimeThreadConfig() + { + policy = SCHED_FIFO; + priority = 99; + } + }; + + struct BestEffortThreadConfig : public ThreadConfig + { + BestEffortThreadConfig() + { + policy = SCHED_OTHER; + priority = 0; + } + }; + + Result set_thread_sched_policy(std::thread &thread, const int policy, const int priority) + { + struct sched_param param; + param.sched_priority = priority; + + // ensure that native_handler() is a pthread_t + assert(typeid(thread.native_handle()) == typeid(pthread_t)); + + int ret = pthread_setschedparam(thread.native_handle(), policy, ¶m); + if (ret != 0) + { + return Result::RET_ERROR; + } + + return Result::RET_OK; + } + + Result set_thread_sched_policy(std::thread &thread, const ThreadConfig &config) + { + return set_thread_sched_policy(thread, config.policy, config.priority); + } + + std::expected get_thread_sched_policy(std::thread &thread) + { + int policy; + struct sched_param param; + + // ensure that native_handler() is a pthread_t + assert(typeid(thread.native_handle()) == typeid(pthread_t)); + ThreadConfig config; + int ret = pthread_getschedparam(thread.native_handle(), &config.policy, ¶m); + config.priority = param.sched_priority; + Result ret_result = Result::RET_ERROR; + if (ret != 0) + { + return std::unexpected(ret_result); + } + + return config; + } + +} \ No newline at end of file diff --git a/src/test/cpputils2_tests.cpp b/src/test/cpputils2_tests.cpp index d20903a..2a3107f 100644 --- a/src/test/cpputils2_tests.cpp +++ b/src/test/cpputils2_tests.cpp @@ -15,6 +15,7 @@ #include "cpputils2/linux/net/socket/udsclient.hpp" #include "cpputils2/linux/net/socket/udsserver.hpp" #include "cpputils2/linux/shm/shm.hpp" +#include "cpputils2/linux/thread/thread.hpp" #endif #ifdef _WIN32 @@ -143,6 +144,17 @@ namespace EXPECT_TRUE(true); } + TEST(ThreadMng, ThreadMng) + { + CppUtils2::BestEffortThreadConfig config; + std::thread t([&config]() { + + }); + auto ret = CppUtils2::set_thread_sched_policy(t, config); + EXPECT_EQ(ret, CppUtils2::Result::RET_OK); + t.join(); + } + #endif #ifdef _WIN32