Skip to content

Commit 486a87e

Browse files
authored
Merge pull request #5 from yangosoft/feature/thread-utils
Test priority management
2 parents a89f396 + 371d007 commit 486a87e

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
77
endif()
88

99

10-
set(CMAKE_CXX_STANDARD 17)
10+
set(CMAKE_CXX_STANDARD 23)
1111
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1212

1313
# Pthread
@@ -47,6 +47,7 @@ else()
4747
include/cpputils2/linux/shm/shm.hpp
4848
include/cpputils2/linux/futex/futex.hpp
4949
include/cpputils2/linux/futex/shared_futex.hpp
50+
include/cpputils2/linux/thread/thread.hpp
5051
)
5152

5253
endif()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include "cpputils2/common/types.hpp"
4+
5+
#include <cstdint>
6+
#include <string>
7+
8+
#define CPPUTILS2_VERSION "1.0.0"
9+
10+
namespace CppUtils2
11+
{
12+
const std::string VERSION{CPPUTILS2_VERSION};
13+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @file thread.hpp
3+
* @brief Various thread utilities
4+
*
5+
*
6+
*/
7+
8+
#pragma once
9+
10+
#include "cpputils2/common/types.hpp"
11+
12+
#include <pthread.h>
13+
#include <sched.h>
14+
#include <sys/syscall.h>
15+
#include <unistd.h>
16+
17+
#include <cassert>
18+
#include <expected>
19+
#include <thread>
20+
21+
namespace CppUtils2
22+
{
23+
struct ThreadConfig
24+
{
25+
int policy;
26+
int priority;
27+
};
28+
29+
struct RealTimeThreadConfig : public ThreadConfig
30+
{
31+
RealTimeThreadConfig()
32+
{
33+
policy = SCHED_FIFO;
34+
priority = 99;
35+
}
36+
};
37+
38+
struct BestEffortThreadConfig : public ThreadConfig
39+
{
40+
BestEffortThreadConfig()
41+
{
42+
policy = SCHED_OTHER;
43+
priority = 0;
44+
}
45+
};
46+
47+
Result set_thread_sched_policy(std::thread &thread, const int policy, const int priority)
48+
{
49+
struct sched_param param;
50+
param.sched_priority = priority;
51+
52+
// ensure that native_handler() is a pthread_t
53+
assert(typeid(thread.native_handle()) == typeid(pthread_t));
54+
55+
int ret = pthread_setschedparam(thread.native_handle(), policy, &param);
56+
if (ret != 0)
57+
{
58+
return Result::RET_ERROR;
59+
}
60+
61+
return Result::RET_OK;
62+
}
63+
64+
Result set_thread_sched_policy(std::thread &thread, const ThreadConfig &config)
65+
{
66+
return set_thread_sched_policy(thread, config.policy, config.priority);
67+
}
68+
69+
std::expected<ThreadConfig, Result> get_thread_sched_policy(std::thread &thread)
70+
{
71+
int policy;
72+
struct sched_param param;
73+
74+
// ensure that native_handler() is a pthread_t
75+
assert(typeid(thread.native_handle()) == typeid(pthread_t));
76+
ThreadConfig config;
77+
int ret = pthread_getschedparam(thread.native_handle(), &config.policy, &param);
78+
config.priority = param.sched_priority;
79+
Result ret_result = Result::RET_ERROR;
80+
if (ret != 0)
81+
{
82+
return std::unexpected(ret_result);
83+
}
84+
85+
return config;
86+
}
87+
88+
}

src/test/cpputils2_tests.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "cpputils2/linux/net/socket/udsclient.hpp"
1616
#include "cpputils2/linux/net/socket/udsserver.hpp"
1717
#include "cpputils2/linux/shm/shm.hpp"
18+
#include "cpputils2/linux/thread/thread.hpp"
1819
#endif
1920

2021
#ifdef _WIN32
@@ -143,6 +144,17 @@ namespace
143144
EXPECT_TRUE(true);
144145
}
145146

147+
TEST(ThreadMng, ThreadMng)
148+
{
149+
CppUtils2::BestEffortThreadConfig config;
150+
std::thread t([&config]() {
151+
152+
});
153+
auto ret = CppUtils2::set_thread_sched_policy(t, config);
154+
EXPECT_EQ(ret, CppUtils2::Result::RET_OK);
155+
t.join();
156+
}
157+
146158
#endif
147159

148160
#ifdef _WIN32

0 commit comments

Comments
 (0)