Skip to content

Commit

Permalink
Merge pull request ros2#23 from alsora/asoragna/cleanup-and-test
Browse files Browse the repository at this point in the history
Asoragna/cleanup and test
  • Loading branch information
iRobot ROS committed Oct 19, 2020
2 parents dc3b747 + e9e237a commit 8688d3b
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 113 deletions.
75 changes: 75 additions & 0 deletions rclcpp/include/rclcpp/executors/event_waitable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2020 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RCLCPP__EXECUTORS__EVENT_WAITABLE_HPP_
#define RCLCPP__EXECUTORS__EVENT_WAITABLE_HPP_

#include "rclcpp/waitable.hpp"

namespace rclcpp
{
namespace executors
{

/**
* @brief This class provides a wrapper around the waitable object, that is
* meant to be used with the EventsExecutor.
* The waitset related methods are stubbed out as they should not be called.
* Nodes who want to implement a custom EventWaitable, can derive from this class and override
* the execute function.
*/
class EventWaitable : public rclcpp::Waitable
{
public:
RCLCPP_SMART_PTR_DEFINITIONS(EventWaitable)

// Constructor
RCLCPP_PUBLIC
EventWaitable() = default;

// Destructor
RCLCPP_PUBLIC
virtual ~EventWaitable() = default;

// Executing an EventWaitable is a no-op.
// Derive from this class to implement execute function.
RCLCPP_PUBLIC
virtual void
execute() = 0;

// Stub API: not used by EventsExecutor
RCLCPP_PUBLIC
bool
is_ready(rcl_wait_set_t * wait_set) final
{
(void)wait_set;
throw std::runtime_error("EventWaitable can't be checked if it's ready");
return false;
}

// Stub API: not used by EventsExecutor
RCLCPP_PUBLIC
bool
add_to_wait_set(rcl_wait_set_t * wait_set) final
{
(void)wait_set;
throw std::runtime_error("EventWaitable can't be added to a wait_set");
return false;
}
};

} // namespace executors
} // namespace rclcpp

#endif // RCLCPP__EXECUTORS__EVENT_WAITABLE_HPP_
9 changes: 5 additions & 4 deletions rclcpp/include/rclcpp/executors/events_executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
#include <chrono>
#include <deque>
#include <memory>
#include <queue>
#include <vector>

#include "rcutils/executor_event_types.h"

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

Expand Down Expand Up @@ -57,7 +58,7 @@ class EventsExecutor : public rclcpp::Executor

/// Default destrcutor.
RCLCPP_PUBLIC
virtual ~EventsExecutor();
virtual ~EventsExecutor() = default;

/// Events executor implementation of spin.
/**
Expand Down Expand Up @@ -231,8 +232,6 @@ class EventsExecutor : public rclcpp::Executor
// facilitate the removal of events from expired entities.
using EventQueue = std::deque<ExecutorEvent>;

EventsExecutorEntitiesCollector::SharedPtr entities_collector_;

/// Extract and execute events from the queue until it is empty
RCLCPP_PUBLIC
void
Expand All @@ -247,6 +246,8 @@ class EventsExecutor : public rclcpp::Executor
EventQueue event_queue_;
EventQueue local_event_queue_;

EventsExecutorEntitiesCollector::SharedPtr entities_collector_;
EventsExecutorNotifyWaitable::SharedPtr executor_notifier_;
// Mutex to protect the insertion of events in the queue
std::mutex push_mutex_;
// Mutex to protect the execution of events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
#define RCLCPP__EXECUTORS__EVENTS_EXECUTOR_ENTITIES_COLLECTOR_HPP_

#include <list>
#include <map>
#include <memory>
#include <vector>

#include "rclcpp/executors/event_waitable.hpp"
#include "rclcpp/executors/timers_manager.hpp"
#include "rclcpp/node_interfaces/node_base_interface.hpp"
#include "rclcpp/waitable.hpp"

namespace rclcpp
{
Expand All @@ -46,7 +48,7 @@ class EventsExecutor;
* When this occurs, the execute API takes care of handling changes
* in the entities currently used by the executor.
*/
class EventsExecutorEntitiesCollector final : public rclcpp::Waitable
class EventsExecutorEntitiesCollector final : public EventWaitable
{
public:
RCLCPP_SMART_PTR_DEFINITIONS(EventsExecutorEntitiesCollector)
Expand Down Expand Up @@ -79,24 +81,6 @@ class EventsExecutorEntitiesCollector final : public rclcpp::Waitable
remove_node(
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr);

// Stub API: not used by EventsExecutor
RCLCPP_PUBLIC
bool
is_ready(rcl_wait_set_t * wait_set) override
{
(void)wait_set;
return false;
}

// Stub API: not used by EventsExecutor
RCLCPP_PUBLIC
bool
add_to_wait_set(rcl_wait_set_t * wait_set) override
{
(void)wait_set;
return false;
}

/// Add a callback group to the entities collector
/**
* \see rclcpp::Executor::add_callback_group
Expand Down Expand Up @@ -165,6 +149,12 @@ class EventsExecutorEntitiesCollector final : public rclcpp::Waitable
void
unset_callback_group_entities_callbacks(rclcpp::CallbackGroup::SharedPtr group);

void
set_guard_condition_callback(const rcl_guard_condition_t * guard_condition);

void
unset_guard_condition_callback(const rcl_guard_condition_t * guard_condition);

/// Return true if the node belongs to the collector
/**
* \param[in] group_ptr a node base interface shared pointer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2020 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RCLCPP__EXECUTORS__EVENTS_EXECUTOR_NOTIFY_WAITABLE_HPP_
#define RCLCPP__EXECUTORS__EVENTS_EXECUTOR_NOTIFY_WAITABLE_HPP_

#include <list>

#include "rcl/guard_condition.h"
#include "rclcpp/executors/event_waitable.hpp"

namespace rclcpp
{
namespace executors
{

/**
* @brief This class provides an EventWaitable that allows to
* wake up an EventsExecutor when a guard condition is notified.
*/
class EventsExecutorNotifyWaitable final : public EventWaitable
{
public:
RCLCPP_SMART_PTR_DEFINITIONS(EventsExecutorNotifyWaitable)

// Constructor
RCLCPP_PUBLIC
EventsExecutorNotifyWaitable() = default;

// Destructor
RCLCPP_PUBLIC
virtual ~EventsExecutorNotifyWaitable()
{
if (on_destruction_callback_) {
on_destruction_callback_(this);
}
}

// The function is a no-op, since we only care of waking up the executor
RCLCPP_PUBLIC
void
execute() override
{}

RCLCPP_PUBLIC
void
add_guard_condition(rcl_guard_condition_t * guard_condition)
{
notify_guard_conditions_.push_back(guard_condition);
}

RCLCPP_PUBLIC
void
set_events_executor_callback(
const rclcpp::executors::EventsExecutor * executor,
ExecutorEventCallback executor_callback) const override
{
for (auto gc : notify_guard_conditions_) {
rcl_ret_t ret = rcl_guard_condition_set_events_executor_callback(
executor,
executor_callback,
this,
gc,
false);

if (RCL_RET_OK != ret) {
throw std::runtime_error("Couldn't set guard condition callback");
}
}
}

private:
std::list<rcl_guard_condition_t *> notify_guard_conditions_;
};

} // namespace executors
} // namespace rclcpp

#endif // RCLCPP__EXECUTORS__EVENTS_EXECUTOR_NOTIFY_WAITABLE_HPP_
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SubscriptionIntraProcessBase : public rclcpp::Waitable
: topic_name_(topic_name), qos_profile_(qos_profile)
{}

virtual ~SubscriptionIntraProcessBase() = default;
virtual ~SubscriptionIntraProcessBase();

RCLCPP_PUBLIC
size_t
Expand Down
26 changes: 7 additions & 19 deletions rclcpp/include/rclcpp/qos_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ class QOSEventHandlerBase : public Waitable
bool
is_ready(rcl_wait_set_t * wait_set) override;

/// Set EventsExecutor's callback
RCLCPP_PUBLIC
void
set_events_executor_callback(
const rclcpp::executors::EventsExecutor * executor,
ExecutorEventCallback executor_callback) const override;

protected:
rcl_event_t event_handle_;
size_t wait_set_event_index_;
Expand Down Expand Up @@ -153,25 +160,6 @@ class QOSEventHandler : public QOSEventHandlerBase
event_callback_(callback_info);
}

/// Set EventsExecutor's callback
RCLCPP_PUBLIC
void
set_events_executor_callback(
const rclcpp::executors::EventsExecutor * executor,
ExecutorEventCallback executor_callback) const override
{
rcl_ret_t ret = rcl_event_set_events_executor_callback(
executor,
executor_callback,
this,
&event_handle_,
false /* Discard previous events */);

if (RCL_RET_OK != ret) {
throw std::runtime_error("Couldn't set EventsExecutor's callback to event");
}
}

private:
using EventCallbackInfoT = typename std::remove_reference<typename
rclcpp::function_traits::function_traits<EventCallbackT>::template argument_type<0>>::type;
Expand Down
6 changes: 4 additions & 2 deletions rclcpp/include/rclcpp/waitable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Waitable
RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(Waitable)

RCLCPP_PUBLIC
virtual ~Waitable();
virtual ~Waitable() = default;

/// Get the number of ready subscriptions
/**
Expand Down Expand Up @@ -182,8 +182,10 @@ class Waitable
void
set_on_destruction_callback(std::function<void(Waitable *)> on_destruction_callback);

private:
protected:
std::function<void(Waitable *)> on_destruction_callback_;

private:
std::atomic<bool> in_use_by_wait_set_{false};
}; // class Waitable

Expand Down
Loading

0 comments on commit 8688d3b

Please sign in to comment.