Skip to content

Commit

Permalink
iox-#1150 Protect findService with a mutex
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Killat <[email protected]>
  • Loading branch information
MatthiasKillat committed Feb 23, 2022
1 parent 1592b46 commit 1879568
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
15 changes: 8 additions & 7 deletions iceoryx_posh/include/iceoryx_posh/runtime/service_discovery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "iceoryx_posh/runtime/posh_runtime.hpp"

#include <memory>
#include <mutex>

namespace iox
{
Expand All @@ -43,10 +44,7 @@ enum class ServiceDiscoveryEvent : popo::EventEnumIdentifier
class ServiceDiscovery
{
public:
ServiceDiscovery()
{
}

ServiceDiscovery() = default;
ServiceDiscovery(const ServiceDiscovery&) = delete;
ServiceDiscovery& operator=(const ServiceDiscovery&) = delete;
ServiceDiscovery(ServiceDiscovery&&) = delete;
Expand All @@ -73,13 +71,16 @@ class ServiceDiscovery
iox::popo::WaitSetIsConditionSatisfiedCallback
getCallbackForIsStateConditionSatisfied(const popo::SubscriberState state);

// use dynamic memory to reduce stack usage,
/// @todo improve solution to avoid stack usage without using dynamic memory
std::unique_ptr<roudi::ServiceRegistry> m_serviceRegistry{new roudi::ServiceRegistry};
// use dynamic memory to reduce stack usage
/// @todo #1155 improve solution to avoid stack usage without using dynamic memory
std::unique_ptr<roudi::ServiceRegistry> m_serviceRegistry{new iox::roudi::ServiceRegistry};
std::mutex m_serviceRegistryMutex;

popo::Subscriber<roudi::ServiceRegistry> m_serviceRegistrySubscriber{
{SERVICE_DISCOVERY_SERVICE_NAME, SERVICE_DISCOVERY_INSTANCE_NAME, SERVICE_DISCOVERY_EVENT_NAME},
{1U, 1U, iox::NodeName_t("Service Registry"), true}};

void update();
};

} // namespace runtime
Expand Down
13 changes: 10 additions & 3 deletions iceoryx_posh/source/runtime/service_discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ namespace iox
{
namespace runtime
{
void ServiceDiscovery::update()
{
// allows us to use update and hence findService concurrently
std::lock_guard<std::mutex> lock(m_serviceRegistryMutex);
m_serviceRegistrySubscriber.take().and_then([&](popo::Sample<const roudi::ServiceRegistry>& serviceRegistrySample) {
*m_serviceRegistry = *serviceRegistrySample;
});
}

void ServiceDiscovery::findService(const cxx::optional<capro::IdString_t>& service,
const cxx::optional<capro::IdString_t>& instance,
const cxx::optional<capro::IdString_t>& event,
Expand All @@ -32,9 +41,7 @@ void ServiceDiscovery::findService(const cxx::optional<capro::IdString_t>& servi
return;
}

m_serviceRegistrySubscriber.take().and_then([&](popo::Sample<const roudi::ServiceRegistry>& serviceRegistrySample) {
*m_serviceRegistry = *serviceRegistrySample;
});
update();

switch (pattern)
{
Expand Down
7 changes: 7 additions & 0 deletions iceoryx_posh/test/integrationtests/test_service_discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ TYPED_TEST(ServiceDiscovery_test, FindSameServiceMultipleTimesReturnsSingleInsta
SERVICE_DESCRIPTION.getEventIDString());
ASSERT_THAT(serviceContainer.size(), Eq(1U));
EXPECT_THAT(*serviceContainer.begin(), Eq(SERVICE_DESCRIPTION));

this->findService(SERVICE_DESCRIPTION.getServiceIDString(),
SERVICE_DESCRIPTION.getInstanceIDString(),
SERVICE_DESCRIPTION.getEventIDString());

ASSERT_THAT(serviceContainer.size(), Eq(1U));
EXPECT_THAT(*serviceContainer.begin(), Eq(SERVICE_DESCRIPTION));
}

TYPED_TEST(ServiceDiscovery_test, OfferDifferentServicesWithSameInstanceAndEvent)
Expand Down
2 changes: 0 additions & 2 deletions iceoryx_posh/test/moduletests/test_roudi_service_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,6 @@ TYPED_TEST(ServiceRegistry_test, SearchInFullRegistryWorks)
ASSERT_EQ(this->searchResult.size(), 1);
}

using Entry = iox::roudi::ServiceRegistry::ServiceDescriptionEntry;

TYPED_TEST(ServiceRegistry_test, FunctionIsAppliedToAllEntriesInSearchResult)
{
::testing::Test::RecordProperty("TEST_ID", "b7828085-d879-43b7-9fee-e5e88cf36995");
Expand Down

0 comments on commit 1879568

Please sign in to comment.