Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions src/include/cpputils2/cpputils2.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "cpputils2/common/types.hpp"

#include <cstdint>
#include <string>

#define CPPUTILS2_VERSION "1.0.0"

namespace CppUtils2
{
const std::string VERSION{CPPUTILS2_VERSION};
}
88 changes: 88 additions & 0 deletions src/include/cpputils2/linux/thread/thread.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @file thread.hpp
* @brief Various thread utilities
*
*
*/

#pragma once

#include "cpputils2/common/types.hpp"

#include <pthread.h>
#include <sched.h>
#include <sys/syscall.h>
#include <unistd.h>

#include <cassert>
#include <expected>
#include <thread>

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, &param);
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<ThreadConfig, Result> 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, &param);
config.priority = param.sched_priority;
Result ret_result = Result::RET_ERROR;
if (ret != 0)
{
return std::unexpected(ret_result);
}

return config;
}

}
12 changes: 12 additions & 0 deletions src/test/cpputils2_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading