diff --git a/fairmq/sdk/DDSSession.cxx b/fairmq/sdk/DDSSession.cxx index e4eb92a1..11edaca4 100644 --- a/fairmq/sdk/DDSSession.cxx +++ b/fairmq/sdk/DDSSession.cxx @@ -186,12 +186,12 @@ auto DDSSession::SubmitAgents(Quantity agents) -> void submitInfo.m_instances = agents; submitInfo.m_config = GetRMSConfig().string(); - tools::Semaphore blocker; + tools::SharedSemaphore blocker; auto submitRequest = SSubmitRequest::makeRequest(submitInfo); submitRequest->setMessageCallback([](const SMessageResponseData& message){ LOG(debug) << message.m_msg; }); - submitRequest->setDoneCallback([&]() { + submitRequest->setDoneCallback([agents, blocker]() mutable { LOG(debug) << agents << " Agents submitted"; blocker.Signal(); }); @@ -265,7 +265,7 @@ auto DDSSession::RequestCommanderInfo() -> CommanderInfo using namespace dds::tools_api; SCommanderInfoRequestData commanderInfo; - tools::Semaphore blocker; + tools::SharedSemaphore blocker; std::string error; auto commanderInfoRequest = SCommanderInfoRequest::makeRequest(commanderInfo); CommanderInfo info; @@ -281,7 +281,7 @@ auto DDSSession::RequestCommanderInfo() -> CommanderInfo LOG(debug) << _message.m_msg; } }); - commanderInfoRequest->setDoneCallback([&]() { blocker.Signal(); }); + commanderInfoRequest->setDoneCallback([blocker]() mutable { blocker.Signal(); }); fImpl->fSession->sendRequest(commanderInfoRequest); blocker.Wait(); @@ -322,12 +322,12 @@ auto DDSSession::ActivateTopology(DDSTopology topo) -> void topologyInfo.m_updateType = STopologyRequestData::EUpdateType::ACTIVATE; topologyInfo.m_topologyFile = topo.GetTopoFile().string(); - tools::Semaphore blocker; + tools::SharedSemaphore blocker; auto topologyRequest = STopologyRequest::makeRequest(topologyInfo); topologyRequest->setMessageCallback([](const SMessageResponseData& _message) { LOG(debug) << _message.m_msg; }); - topologyRequest->setDoneCallback([&]() { blocker.Signal(); }); + topologyRequest->setDoneCallback([blocker]() mutable { blocker.Signal(); }); fImpl->fSession->sendRequest(topologyRequest); blocker.Wait(); diff --git a/fairmq/sdk/Topology.h b/fairmq/sdk/Topology.h index 6020f82f..68d7e5c9 100644 --- a/fairmq/sdk/Topology.h +++ b/fairmq/sdk/Topology.h @@ -338,14 +338,15 @@ class BasicTopology : public AsioBase auto ChangeState(TopologyTransition transition, Duration timeout = Duration(0)) -> std::pair { - tools::Semaphore blocker; + tools::SharedSemaphore blocker; std::error_code ec; TopologyState state; - AsyncChangeState(transition, timeout, [&](std::error_code _ec, TopologyState _state) mutable { - ec = _ec; - state = _state; - blocker.Signal(); - }); + AsyncChangeState( + transition, timeout, [&, blocker](std::error_code _ec, TopologyState _state) mutable { + ec = _ec; + state = _state; + blocker.Signal(); + }); blocker.Wait(); return {ec, state}; } diff --git a/fairmq/tools/Semaphore.cxx b/fairmq/tools/Semaphore.cxx index 0d2018fb..fc5c424c 100644 --- a/fairmq/tools/Semaphore.cxx +++ b/fairmq/tools/Semaphore.cxx @@ -39,7 +39,7 @@ auto Semaphore::Signal() -> void fCv.notify_one(); } -auto Semaphore::GetCount() -> std::size_t +auto Semaphore::GetCount() const -> std::size_t { std::unique_lock lk(fMutex); return fCount; @@ -63,7 +63,7 @@ auto SharedSemaphore::Signal() -> void fSemaphore->Signal(); } -auto SharedSemaphore::GetCount() -> std::size_t +auto SharedSemaphore::GetCount() const -> std::size_t { return fSemaphore->GetCount(); } diff --git a/fairmq/tools/Semaphore.h b/fairmq/tools/Semaphore.h index ab36ff9f..2549b65e 100644 --- a/fairmq/tools/Semaphore.h +++ b/fairmq/tools/Semaphore.h @@ -30,11 +30,11 @@ struct Semaphore auto Wait() -> void; auto Signal() -> void; - auto GetCount() -> std::size_t; + auto GetCount() const -> std::size_t; private: std::size_t fCount; - std::mutex fMutex; + mutable std::mutex fMutex; std::condition_variable fCv; }; @@ -49,7 +49,7 @@ struct SharedSemaphore auto Wait() -> void; auto Signal() -> void; - auto GetCount() -> std::size_t; + auto GetCount() const -> std::size_t; private: std::shared_ptr fSemaphore; diff --git a/test/sdk/_topology.cxx b/test/sdk/_topology.cxx index 80055ab3..b8a72231 100644 --- a/test/sdk/_topology.cxx +++ b/test/sdk/_topology.cxx @@ -49,11 +49,11 @@ TEST_F(Topology, AsyncChangeState) { using namespace fair::mq; - tools::Semaphore blocker; + tools::SharedSemaphore blocker; sdk::Topology topo(mDDSTopo, mDDSSession); topo.AsyncChangeState( sdk::TopologyTransition::InitDevice, - [&](std::error_code ec, sdk::TopologyState) { + [=](std::error_code ec, sdk::TopologyState) mutable { LOG(info) << ec; EXPECT_EQ(ec, std::error_code()); blocker.Signal(); @@ -142,9 +142,9 @@ TEST_F(Topology, AsyncChangeStateConcurrent) using namespace fair::mq; sdk::Topology topo(mDDSTopo, mDDSSession); - tools::Semaphore blocker; + tools::SharedSemaphore blocker; topo.AsyncChangeState(sdk::TopologyTransition::InitDevice, - [&blocker](std::error_code ec, sdk::TopologyState) { + [blocker](std::error_code ec, sdk::TopologyState) mutable { LOG(info) << "result for valid ChangeState: " << ec; blocker.Signal(); });