Skip to content

Commit

Permalink
Merge pull request #125 from cactusdynamics/fair-dequeue-publisher
Browse files Browse the repository at this point in the history
Publisher dequeue changes
  • Loading branch information
shuhaowu authored Aug 21, 2024
2 parents dc53ca0 + 1042897 commit 93254fc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
4 changes: 4 additions & 0 deletions include/cactus_rt/ros2/ros2_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

namespace cactus_rt::ros2 {

class Ros2ExecutorThread;

class Ros2Adapter {
friend class Ros2ExecutorThread;

public:
struct Config {
/**
Expand Down
3 changes: 1 addition & 2 deletions src/cactus_rt/ros2/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ void Ros2ExecutorThread::Run() {
executor_->spin_once();
}

// Execute one more time to ensure everything is processed.
executor_->spin_once();
ros2_adapter_->DrainQueues();

executor_->remove_node(node_ptr);
}
Expand Down
16 changes: 9 additions & 7 deletions src/cactus_rt/ros2/ros2_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ void Ros2Adapter::TimerCallback() {
void Ros2Adapter::DrainQueues() {
const std::scoped_lock lock(mut_);

for (const auto& publisher : publishers_) {
// Hopefully the thread is not publishing so quickly that a single
// publisher monopolizes all resources. That said, if that happens the
// program is likely in bigger trouble anyway.
//
// TODO: make it so we dequeue once.
publisher->FullyDrainAndPublishToRos();
bool has_data = true;
while (has_data) {
has_data = false;

for (const auto& publisher : publishers_) {
if (publisher->DequeueAndPublishToRos()) {
has_data = true;
}
}
}
}

Expand Down

0 comments on commit 93254fc

Please sign in to comment.