Skip to content

Commit

Permalink
De-duplicate pop from list
Browse files Browse the repository at this point in the history
  • Loading branch information
sloretz committed May 15, 2018
1 parent 05ae2b1 commit 210346b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
37 changes: 17 additions & 20 deletions rmw_fastrtps_cpp/include/rmw_fastrtps_cpp/custom_client_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ typedef struct CustomClientResponse
{
eprosima::fastrtps::rtps::SampleIdentity sample_identity_;
std::unique_ptr<eprosima::fastcdr::FastBuffer> buffer_;

CustomClientResponse()
: buffer_(nullptr) {}
} CustomClientResponse;

class ClientListener : public eprosima::fastrtps::SubscriberListener
Expand All @@ -65,7 +62,8 @@ class ClientListener : public eprosima::fastrtps::SubscriberListener
assert(sub);

CustomClientResponse response;
response.buffer_.reset(new eprosima::fastcdr::FastBuffer());
// Todo(sloretz) eliminate heap allocation pending eprosima/Fast-CDR#19
response.buffer_.reset(new eprosima::fastcdr::FastBuffer);
eprosima::fastrtps::SampleInfo_t sinfo;

if (sub->takeNextData(response.buffer_.get(), &sinfo)) {
Expand Down Expand Up @@ -93,28 +91,27 @@ class ClientListener : public eprosima::fastrtps::SubscriberListener
}
}

CustomClientResponse
getResponse()
bool
getResponse(CustomClientResponse & response)
{
std::lock_guard<std::mutex> lock(internalMutex_);
CustomClientResponse response;

auto pop_response = [this](CustomClientResponse & response) -> bool
{
if (!list.empty()) {
response = std::move(list.front());
list.pop_front();
list_has_data_.store(!list.empty());
return true;
}
return false;
};

if (conditionMutex_ != nullptr) {
std::unique_lock<std::mutex> clock(*conditionMutex_);
if (!list.empty()) {
response = std::move(list.front());
list.pop_front();
list_has_data_.store(!list.empty());
}
} else {
if (!list.empty()) {
response = std::move(list.front());
list.pop_front();
list_has_data_.store(!list.empty());
}
return pop_response(response);
}

return std::move(response);
return pop_response(response);
}

void
Expand Down
4 changes: 2 additions & 2 deletions rmw_fastrtps_cpp/src/rmw_response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ rmw_take_response(
auto info = static_cast<CustomClientInfo *>(client->data);
assert(info);

CustomClientResponse response = info->listener_->getResponse();
CustomClientResponse response;

if (response.buffer_ != nullptr) {
if (info->listener_->getResponse(response)) {
_deserialize_ros_message(response.buffer_.get(), ros_response, info->response_type_support_,
info->typesupport_identifier_);

Expand Down

0 comments on commit 210346b

Please sign in to comment.