Skip to content

Commit

Permalink
Merge branch 'master' into change/way_of_affinity
Browse files Browse the repository at this point in the history
  • Loading branch information
saikishor authored Nov 8, 2024
2 parents e8ad4ab + 45e93d7 commit 84e38c7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ cmake_minimum_required(VERSION 3.16)
project(realtime_tools LANGUAGES CXX)

if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
add_compile_options(-Wall -Wextra)
add_compile_options(-Wall -Wextra -Wpedantic -Werror=conversion -Werror=unused-but-set-variable
-Werror=return-type -Werror=shadow -Werror=format)
endif()

set(THIS_PACKAGE_INCLUDE_DEPENDS
Expand Down
2 changes: 1 addition & 1 deletion include/realtime_tools/realtime_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ std::pair<bool, std::string> set_current_thread_affinity(int core);
* \ref https://stackoverflow.com/a/150971
* \returns The number of processors currently online (available)
*/
int get_number_of_available_processors();
int64_t get_number_of_available_processors();

} // namespace realtime_tools

Expand Down
19 changes: 9 additions & 10 deletions src/realtime_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,27 +122,26 @@ std::pair<bool, std::string> set_thread_affinity(pthread_t thread, int core)
message = "Thread affinity is not supported on Windows.";
return std::make_pair(false, message);
#else
auto set_affinity_result_message = [](int result, std::string & message) -> bool {
auto set_affinity_result_message = [](int result, std::string & msg) -> bool {
if (result == 0) {
message = "Thread affinity set successfully!";
msg = "Thread affinity set successfully!";
return true;
}
switch (errno) {
case EFAULT:
message = "Call of sched_setaffinity with invalid cpuset!";
msg = "Call of sched_setaffinity with invalid cpuset!";
break;
case EINVAL:
message = "Call of sched_setaffinity with an invalid cpu core!";
msg = "Call of sched_setaffinity with an invalid cpu core!";
break;
case ESRCH:
message =
"Call of sched_setaffinity with a thread id/process id that is invalid or not found!";
msg = "Call of sched_setaffinity with a thread id/process id that is invalid or not found!";
break;
case EPERM:
message = "Call of sched_setaffinity with insufficient privileges!";
msg = "Call of sched_setaffinity with insufficient privileges!";
break;
default:
message = "Unknown error code: " + std::string(strerror(errno));
msg = "Unknown error code: " + std::string(strerror(errno));
}
return false;
};
Expand Down Expand Up @@ -194,12 +193,12 @@ std::pair<bool, std::string> set_current_thread_affinity(int core)
return set_thread_affinity(pthread_self(), core);
}

int get_number_of_available_processors()
int64_t get_number_of_available_processors()
{
#ifdef _WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
return static_cast<int64_t>(sysinfo.dwNumberOfProcessors);
#else
return sysconf(_SC_NPROCESSORS_ONLN);
#endif
Expand Down
9 changes: 6 additions & 3 deletions test/realtime_box_best_effort_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ TEST(RealtimeBoxBestEffort, non_default_constructable)
}
TEST(RealtimeBoxBestEffort, standard_get)
{
RealtimeBoxBestEffort<DefaultConstructable> box(DefaultConstructable{.a = 1000});
RealtimeBoxBestEffort<DefaultConstructable> box(DefaultConstructable{1000});

DefaultConstructable data;
box.get(data);
Expand Down Expand Up @@ -122,15 +122,18 @@ TEST(RealtimeBoxBestEffort, assignment_operator)
}
TEST(RealtimeBoxBestEffort, typecast_operator)
{
RealtimeBoxBestEffort box(DefaultConstructable{.a = 100, .str = ""});
DefaultConstructable data_construct;
data_construct.a = 100;
data_construct.str = "";
RealtimeBoxBestEffort box(data_construct);

// Use non RT access
DefaultConstructable data = box;

EXPECT_EQ(data.a, 100);

// Use RT access -> returns std::nullopt if the mutex could not be locked
std::optional<DefaultConstructable> rt_data_access = box;
std::optional<DefaultConstructable> rt_data_access = box.tryGet();

if (rt_data_access) {
EXPECT_EQ(rt_data_access->a, 100);
Expand Down
4 changes: 2 additions & 2 deletions test/test_async_function_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ TEST_F(AsyncFunctionHandlerTest, test_with_deactivate_and_activate_cycles)
async_class.deactivate();
async_class.get_handler().wait_for_trigger_cycle_to_finish();
for (int i = 0; i < 50; i++) {
const auto trigger_status = async_class.trigger();
ASSERT_FALSE(trigger_status.first);
const auto trigger_status_deactivated = async_class.trigger();
ASSERT_FALSE(trigger_status_deactivated.first);
ASSERT_EQ(async_class.get_counter(), total_cycles)
<< "The trigger should fail for any state different than ACTIVE";
}
Expand Down
2 changes: 1 addition & 1 deletion test/thread_priority_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ TEST(thread_priority, set_thread_affinity_valid)

TEST(thread_priority, set_thread_affinity_invalid_too_many_cores)
{
const auto count = realtime_tools::get_number_of_available_processors();
// create a basic thread for the test
std::thread t([]() { std::this_thread::sleep_for(std::chrono::milliseconds(100)); });
const int count = static_cast<int>(realtime_tools::get_number_of_available_processors());
// We should always have at least one core
EXPECT_FALSE(realtime_tools::set_thread_affinity(t.native_handle(), count + 10).first);
t.join();
Expand Down

0 comments on commit 84e38c7

Please sign in to comment.