1
0
Fork 0

take the control lock when running callbacks

This commit is contained in:
Kevin Matz 2022-12-10 11:30:23 -05:00
parent 8ce86353c9
commit 849da8cecb
2 changed files with 11 additions and 2 deletions

View File

@ -295,6 +295,7 @@ void Universe::rx_timeout_(bool add_now)
*/
void Universe::do_callbacks_(std::vector<std::weak_ptr<const std::function<void(Universe*)>>> & callbacks)
{
std::shared_lock lk_ctl(mtx_control);
for (auto it = callbacks.cbegin(); it != callbacks.cend();)
{
if (auto sp = it->lock())
@ -304,8 +305,11 @@ void Universe::do_callbacks_(std::vector<std::weak_ptr<const std::function<void(
}
else
{ // the owner has released the token
std::unique_lock lk(mtx_control);
lk_ctl.unlock(); // release the shared lock
mtx_control.lock(); // take a unique lock
it = callbacks.erase(it);
mtx_control.unlock(); // release the unique lock
lk_ctl.lock(); // retake the shared lock
}
}
}

View File

@ -183,6 +183,7 @@ std::shared_ptr<void> ArbitratingUniverse::onSourceListChange(std::function<void
*/
void ArbitratingUniverse::doListChangeCallbacks()
{
std::shared_lock lk_ctl(mtx_control);
for (auto it = cb_sourceListChange.cbegin(); it != cb_sourceListChange.cend();)
{
if (auto sp = it->lock())
@ -192,8 +193,12 @@ void ArbitratingUniverse::doListChangeCallbacks()
}
else
{ // or remove the callback
std::unique_lock lk_ctl(mtx_control);
lk_ctl.unlock(); // release the shared lock
mtx_control.lock(); // take a unique lock
it = cb_sourceListChange.erase(it);
mtx_control.unlock(); // release the unique lock
lk_ctl.lock(); // retake the shared lock
}
}
}