diff --git a/include/engine/data_watchdog.hpp b/include/engine/data_watchdog.hpp index b71ab080325..0f65ed10f65 100644 --- a/include/engine/data_watchdog.hpp +++ b/include/engine/data_watchdog.hpp @@ -56,11 +56,14 @@ class DataWatchdogImpl>( - std::make_shared( - std::vector{ - static_region.shm_key, updatable_region.shm_key})); + { + boost::unique_lock swap_lock(factory_mutex); + facade_factory = + DataFacadeFactory( + std::make_shared( + std::vector{ + static_region.shm_key, updatable_region.shm_key})); + } } watcher = std::thread(&DataWatchdogImpl::Run, this); @@ -75,21 +78,15 @@ class DataWatchdogImpl Get(const api::BaseParameters ¶ms) const { - // Ensure that an exchange of facade_factory while ->Get() is in progress - // doesn't access an object undergoing destruction from the Run() thread - // We increase the ref count on facade_factory, and it only gets decreased - // (possibly to zero) *after* this function returns - auto facade_factory_copy = facade_factory; - return facade_factory_copy->Get(params); + // make sure facade_factory stays stable while we call Get() + boost::shared_lock swap_lock(factory_mutex); + return facade_factory.Get(params); } std::shared_ptr Get(const api::TileParameters ¶ms) const { - // Ensure that an exchange of facade_factory while ->Get() is in progress - // doesn't access an object undergoing destruction from the Run() thread - // We increase the ref count on facade_factory, and it only gets decreased - // (possibly to zero) *after* this function returns - auto facade_factory_copy = facade_factory; - return facade_factory_copy->Get(params); + // make sure facade_factory stays stable while we call Get() + boost::shared_lock swap_lock(factory_mutex); + return facade_factory.Get(params); } private: @@ -121,16 +118,19 @@ class DataWatchdogImpl>( - std::make_shared( - std::vector{ - static_region.shm_key, updatable_region.shm_key})); + { + boost::unique_lock swap_lock(factory_mutex); + facade_factory =DataFacadeFactory( + std::make_shared( + std::vector{ + static_region.shm_key, updatable_region.shm_key})); + } } util::Log() << "DataWatchdog thread stopped"; } + mutable boost::shared_mutex factory_mutex; const std::string dataset_name; storage::SharedMonitor barrier; std::thread watcher; @@ -139,7 +139,7 @@ class DataWatchdogImpl> facade_factory; + DataFacadeFactory facade_factory; }; }