Skip to content

Commit

Permalink
Adapt API style of lock_memory to match the one of the other functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennart Nachtigall committed Nov 28, 2024
1 parent d976477 commit b5642d7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
5 changes: 3 additions & 2 deletions include/realtime_tools/realtime_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool, std::string> lock_memory();

/**
* Configure the caller thread affinity - Tell the scheduler to prefer a certain
Expand Down
10 changes: 5 additions & 5 deletions src/realtime_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ bool configure_sched_fifo(int priority)
#endif
}

bool lock_memory(std::string & message)
std::pair<bool, std::string> 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;
Expand All @@ -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!";
Expand All @@ -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
}
Expand Down

0 comments on commit b5642d7

Please sign in to comment.