From b5642d7ad978595b677fc15d28406aebc8809928 Mon Sep 17 00:00:00 2001 From: Lennart Nachtigall Date: Thu, 28 Nov 2024 08:32:52 +0100 Subject: [PATCH 1/3] Adapt API style of lock_memory to match the one of the other functions --- include/realtime_tools/realtime_helpers.hpp | 5 +++-- src/realtime_helpers.cpp | 10 +++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/realtime_tools/realtime_helpers.hpp b/include/realtime_tools/realtime_helpers.hpp index 371f9643..bffdfbb3 100644 --- a/include/realtime_tools/realtime_helpers.hpp +++ b/include/realtime_tools/realtime_helpers.hpp @@ -62,9 +62,10 @@ bool configure_sched_fifo(int priority); * will not swap out the pages to disk i.e., the pages are guaranteed to stay in * RAM until later unlocked - which is important for realtime applications. * \param[out] message a message describing the result of the operation - * \returns true if memory locking succeeded, false otherwise + * \returns a pair of a boolean indicating whether the operation succeeded or not + * and a message describing the result of the operation */ -bool lock_memory(std::string & message); +std::pair lock_memory(); /** * Configure the caller thread affinity - Tell the scheduler to prefer a certain diff --git a/src/realtime_helpers.cpp b/src/realtime_helpers.cpp index a9ce1a73..d6daf30c 100644 --- a/src/realtime_helpers.cpp +++ b/src/realtime_helpers.cpp @@ -64,11 +64,10 @@ bool configure_sched_fifo(int priority) #endif } -bool lock_memory(std::string & message) +std::pair lock_memory() { #ifdef _WIN32 - message = "Memory locking is not supported on Windows."; - return false; + return {false, "Memory locking is not supported on Windows."}; #else auto is_capable = [](cap_value_t v) -> bool { bool rc = false; @@ -86,6 +85,7 @@ bool lock_memory(std::string & message) return rc; }; + std::string message; if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { if (!is_capable(CAP_IPC_LOCK)) { message = "No proper privileges to lock the memory!"; @@ -105,10 +105,10 @@ bool lock_memory(std::string & message) } else { message = "Unknown error occurred!"; } - return false; + return {false, message}; } else { message = "Memory locked successfully!"; - return true; + return {true, message}; } #endif } From ed2fbfe9a5ee1f5b81e345665eacb3631a045305 Mon Sep 17 00:00:00 2001 From: Lennart Nachtigall Date: Thu, 28 Nov 2024 08:54:38 +0100 Subject: [PATCH 2/3] propery deprecate the old style --- include/realtime_tools/realtime_helpers.hpp | 13 +++++++++++++ src/realtime_helpers.cpp | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/include/realtime_tools/realtime_helpers.hpp b/include/realtime_tools/realtime_helpers.hpp index bffdfbb3..f9ed663b 100644 --- a/include/realtime_tools/realtime_helpers.hpp +++ b/include/realtime_tools/realtime_helpers.hpp @@ -55,6 +55,19 @@ bool has_realtime_kernel(); */ bool configure_sched_fifo(int priority); +/** + * Locks the memory pages of the calling thread to prevent page faults. + * By calling this method, the programs locks all pages mapped into the address + * space of the calling process and future mappings. This means that the kernel + * will not swap out the pages to disk i.e., the pages are guaranteed to stay in + * RAM until later unlocked - which is important for realtime applications. + * \param[out] message a message describing the result of the operation + * \returns a pair of a boolean indicating whether the operation succeeded or not. +*/ + +[[deprecated("Use std::pair lock_memory() instead.")]] +bool lock_memory(std::string & message); + /** * Locks the memory pages of the calling thread to prevent page faults. * By calling this method, the programs locks all pages mapped into the address diff --git a/src/realtime_helpers.cpp b/src/realtime_helpers.cpp index d6daf30c..fb00c39d 100644 --- a/src/realtime_helpers.cpp +++ b/src/realtime_helpers.cpp @@ -64,6 +64,13 @@ bool configure_sched_fifo(int priority) #endif } +bool lock_memory(std::string & message) +{ + const auto lock_result = lock_memory(); + message = lock_result.second; + return lock_result.first; +} + std::pair lock_memory() { #ifdef _WIN32 From bfb11f74d6d3c316a6887f348d46f870f34ecea6 Mon Sep 17 00:00:00 2001 From: Lennart Nachtigall Date: Thu, 28 Nov 2024 08:55:56 +0100 Subject: [PATCH 3/3] fix comment --- include/realtime_tools/realtime_helpers.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/realtime_tools/realtime_helpers.hpp b/include/realtime_tools/realtime_helpers.hpp index f9ed663b..2ee43e15 100644 --- a/include/realtime_tools/realtime_helpers.hpp +++ b/include/realtime_tools/realtime_helpers.hpp @@ -62,7 +62,7 @@ bool configure_sched_fifo(int priority); * will not swap out the pages to disk i.e., the pages are guaranteed to stay in * RAM until later unlocked - which is important for realtime applications. * \param[out] message a message describing the result of the operation - * \returns a pair of a boolean indicating whether the operation succeeded or not. + * \returns true if memory locking succeeded, false otherwise. */ [[deprecated("Use std::pair lock_memory() instead.")]]