Skip to content

Commit

Permalink
create TimersManager class, implement spin() variants for EventsExecu…
Browse files Browse the repository at this point in the history
…tor and fix unit tests

Signed-off-by: Alberto Soragna <alberto.soragna@gmail.com>
  • Loading branch information
alsora committed Oct 12, 2020
1 parent c9d395e commit 10f4074
Show file tree
Hide file tree
Showing 13 changed files with 751 additions and 443 deletions.
1 change: 1 addition & 0 deletions rclcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ set(${PROJECT_NAME}_SRCS
src/rclcpp/executors/single_threaded_executor.cpp
src/rclcpp/executors/static_executor_entities_collector.cpp
src/rclcpp/executors/static_single_threaded_executor.cpp
src/rclcpp/executors/timers_manager.cpp
src/rclcpp/future_return_code.cpp
src/rclcpp/graph_listener.cpp
src/rclcpp/guard_condition.cpp
Expand Down
2 changes: 1 addition & 1 deletion rclcpp/include/rclcpp/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ class Executor
RCLCPP_DISABLE_COPY(Executor)

RCLCPP_PUBLIC
void
virtual void
spin_once_impl(std::chrono::nanoseconds timeout);

typedef std::map<rclcpp::node_interfaces::NodeBaseInterface::WeakPtr,
Expand Down
1 change: 1 addition & 0 deletions rclcpp/include/rclcpp/executors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <future>
#include <memory>

#include "rclcpp/executors/events_executor.hpp"
#include "rclcpp/executors/multi_threaded_executor.hpp"
#include "rclcpp/executors/single_threaded_executor.hpp"
#include "rclcpp/executors/static_single_threaded_executor.hpp"
Expand Down
43 changes: 30 additions & 13 deletions rclcpp/include/rclcpp/executors/events_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
#ifndef RCLCPP__EXECUTORS__EVENTS_EXECUTOR_HPP_
#define RCLCPP__EXECUTORS__EVENTS_EXECUTOR_HPP_

#include <queue>
#include <chrono>
#include <memory>
#include <queue>

#include "rcutils/event_types.h"

#include "rclcpp/executor.hpp"
#include "rclcpp/executors/events_executor_entities_collector.hpp"
#include "rclcpp/executors/timers_heap.hpp"
#include "rclcpp/executors/timers_manager.hpp"
#include "rclcpp/node.hpp"

#include "rcutils/event_types.h"

namespace rclcpp
{
namespace executors
Expand All @@ -46,6 +47,9 @@ class EventsExecutor : public rclcpp::Executor
RCLCPP_SMART_PTR_DEFINITIONS(EventsExecutor)

/// Default constructor. See the default constructor for Executor.
/**
* \param[in] options Options used to configure the executor.
*/
RCLCPP_PUBLIC
explicit EventsExecutor(
const rclcpp::ExecutorOptions & options = rclcpp::ExecutorOptions());
Expand Down Expand Up @@ -75,6 +79,10 @@ class EventsExecutor : public rclcpp::Executor
void
spin_some(std::chrono::nanoseconds max_duration) override;

RCLCPP_PUBLIC
void
spin_all(std::chrono::nanoseconds max_duration) override;

/// Add a node to the executor.
/**
* \sa rclcpp::Executor::add_node
Expand Down Expand Up @@ -112,21 +120,30 @@ class EventsExecutor : public rclcpp::Executor
remove_node(std::shared_ptr<rclcpp::Node> node_ptr, bool notify = true) override;

protected:
/// Execute timers when ready
/// Waits for events and then executes them
RCLCPP_PUBLIC
void
spin_timers(bool spin_once);
handle_events();

/// Execute events in the queue until is empty
RCLCPP_PUBLIC
void
execute_events();
spin_once_impl(std::chrono::nanoseconds timeout) override;

private:
RCLCPP_DISABLE_COPY(EventsExecutor)

EventsExecutorEntitiesCollector::SharedPtr entities_collector_;

/// Extract and execute events from the queue until it is empty
RCLCPP_PUBLIC
void
consume_all_events(std::queue<EventQ> &queue);

// Execute a single event
RCLCPP_PUBLIC
void
execute_event(const EventQ &event);

// Executor callback: Push new events into the queue and trigger cv.
// This function is called by the DDS entities when an event happened,
// like a subscription receiving a message.
Expand All @@ -141,19 +158,19 @@ class EventsExecutor : public rclcpp::Executor
{
std::unique_lock<std::mutex> lock(this_executor->event_queue_mutex_);

this_executor->event_queue.push(event);
this_executor->event_queue_.push(event);
}
// Notify that the event queue has some events in it.
this_executor->event_queue_cv.notify_one();
this_executor->event_queue_cv_.notify_one();
}

// Event queue members
std::queue<EventQ> event_queue;
std::queue<EventQ> event_queue_;
std::mutex event_queue_mutex_;
std::condition_variable event_queue_cv;
std::condition_variable event_queue_cv_;

// Timers heap manager
TimersHeap timers;
std::shared_ptr<TimersManager> timers_manager_;
};

} // namespace executors
Expand Down
225 changes: 0 additions & 225 deletions rclcpp/include/rclcpp/executors/timers_heap.hpp

This file was deleted.

Loading

0 comments on commit 10f4074

Please sign in to comment.