Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental for Linux, change thread priority to execute timer. #1382

Closed
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
5 changes: 5 additions & 0 deletions rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <thread>
#include <unordered_map>

#include <pthread.h>

#include "rclcpp/executor.hpp"
#include "rclcpp/macros.hpp"
#include "rclcpp/memory_strategies.hpp"
Expand Down Expand Up @@ -87,6 +89,9 @@ class MultiThreadedExecutor : public rclcpp::Executor
std::chrono::nanoseconds next_exec_timeout_;

std::set<TimerBase::SharedPtr> scheduled_timers_;

struct sched_param sch_org, sch_mod;
int policy;
};

} // namespace executors
Expand Down
15 changes: 11 additions & 4 deletions rclcpp/src/rclcpp/executors/multi_threaded_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ MultiThreadedExecutor::MultiThreadedExecutor(
if (number_of_threads_ == 0) {
number_of_threads_ = 1;
}

pthread_getschedparam(pthread_self(), &policy, &sch_org);
sch_mod.sched_priority = 1; // set lowest priority in SCHED_RR
}

MultiThreadedExecutor::~MultiThreadedExecutor() {}
Expand Down Expand Up @@ -94,6 +97,7 @@ MultiThreadedExecutor::run(size_t)
continue;
}
scheduled_timers_.insert(any_exec.timer);
pthread_setschedparam(pthread_self(), SCHED_RR, &sch_mod);
}
}
if (yield_before_execute_) {
Expand All @@ -103,11 +107,14 @@ MultiThreadedExecutor::run(size_t)
execute_any_executable(any_exec);

if (any_exec.timer) {
std::lock_guard<std::mutex> wait_lock(wait_mutex_);
auto it = scheduled_timers_.find(any_exec.timer);
if (it != scheduled_timers_.end()) {
scheduled_timers_.erase(it);
{
std::lock_guard<std::mutex> wait_lock(wait_mutex_);
auto it = scheduled_timers_.find(any_exec.timer);
if (it != scheduled_timers_.end()) {
scheduled_timers_.erase(it);
}
}
pthread_setschedparam(pthread_self(), policy, &sch_org);
}
// Clear the callback_group to prevent the AnyExecutable destructor from
// resetting the callback group `can_be_taken_from`
Expand Down