FIX: fixed deadlock when participant connects another one

This commit is contained in:
Dominik Meyer 2022-02-19 16:06:49 +01:00
parent c2a609c913
commit 92975e8349
No known key found for this signature in database
GPG Key ID: B4C312B600606B64
2 changed files with 39 additions and 12 deletions

View File

@ -78,6 +78,12 @@
/// the id for the next participant connecting /// the id for the next participant connecting
std::uint32_t nextParticipantID_; std::uint32_t nextParticipantID_;
/// queue for connection request so connecting is done in manager context
std::queue<std::shared_ptr<EventManager::Participant>> connectionQueue_;
/// mutex to protect connectionQueue_
std::mutex mutexConnectionQueue_;
/* /*
* all private methods * all private methods
*/ */
@ -97,6 +103,9 @@
*/ */
void processEvent(const std::shared_ptr<EventManager::Event> event); void processEvent(const std::shared_ptr<EventManager::Event> event);
void processConnections_();
/** /**
* @brief start the main thread * @brief start the main thread
*/ */

View File

@ -198,6 +198,8 @@
} }
} }
mutexSchedulingParticipants_.unlock(); mutexSchedulingParticipants_.unlock();
processConnections_();
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
} }
@ -341,18 +343,34 @@
} }
} }
void EventManager::Manager::processConnections_()
{
std::lock_guard<std::mutex> guard(mutexParticipants_);
std::lock_guard<std::mutex> lockGuard(mutexConnectionQueue_);
while(!connectionQueue_.empty())
{
std::shared_ptr<EventManager::Participant> participant = connectionQueue_.front();
connectionQueue_.pop();
particpants_.push_back(participant);
participant->init();
}
}
void EventManager::Manager::connect(std::shared_ptr<EventManager::Participant> participant) void EventManager::Manager::connect(std::shared_ptr<EventManager::Participant> participant)
{ {
std::lock_guard<std::mutex> guard(mutexParticipants_); std::lock_guard<std::mutex> guard(mutexParticipants_);
particpants_.push_back(participant);
participant->setManager(shared_from_this()); participant->setManager(shared_from_this());
// we can set and increment here because only one participant is in this critical // we can set and increment here because only one participant is in this
// section in any moment // critical section in any moment
participant->setID(nextParticipantID_); participant->setID(nextParticipantID_);
nextParticipantID_++; nextParticipantID_++;
participant->init(); connectionQueue_.push(participant);
} }
void EventManager::Manager::disconnect(std::shared_ptr<EventManager::Participant> participant) void EventManager::Manager::disconnect(std::shared_ptr<EventManager::Participant> participant)