Skip to content

Commit

Permalink
Wrap access to facade_factory in a shared lock so it doesn't get chan…
Browse files Browse the repository at this point in the history
…ged partway through access, leading to a crash.
  • Loading branch information
danpat committed Sep 30, 2020
1 parent de94cfe commit ba78fa8
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions include/engine/data_watchdog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacad
static_region = *static_shared_region;
updatable_region = *updatable_shared_region;

facade_factory =
std::make_shared<DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>>(
std::make_shared<datafacade::SharedMemoryAllocator>(
std::vector<storage::SharedRegionRegister::ShmKey>{
static_region.shm_key, updatable_region.shm_key}));
{
boost::unique_lock<boost::shared_mutex> swap_lock(factory_mutex);
facade_factory =
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>(
std::make_shared<datafacade::SharedMemoryAllocator>(
std::vector<storage::SharedRegionRegister::ShmKey>{
static_region.shm_key, updatable_region.shm_key}));
}
}

watcher = std::thread(&DataWatchdogImpl::Run, this);
Expand All @@ -75,21 +78,15 @@ class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacad

std::shared_ptr<const Facade> Get(const api::BaseParameters &params) 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<boost::shared_mutex> swap_lock(factory_mutex);
return facade_factory.Get(params);
}
std::shared_ptr<const Facade> Get(const api::TileParameters &params) 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<boost::shared_mutex> swap_lock(factory_mutex);
return facade_factory.Get(params);
}

private:
Expand Down Expand Up @@ -121,16 +118,19 @@ class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacad
<< (int)updatable_region.shm_key << " with timestamps "
<< static_region.timestamp << " and " << updatable_region.timestamp;

facade_factory =
std::make_shared<DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>>(
std::make_shared<datafacade::SharedMemoryAllocator>(
std::vector<storage::SharedRegionRegister::ShmKey>{
static_region.shm_key, updatable_region.shm_key}));
{
boost::unique_lock<boost::shared_mutex> swap_lock(factory_mutex);
facade_factory =DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>(
std::make_shared<datafacade::SharedMemoryAllocator>(
std::vector<storage::SharedRegionRegister::ShmKey>{
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<storage::SharedRegionRegister> barrier;
std::thread watcher;
Expand All @@ -139,7 +139,7 @@ class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacad
storage::SharedRegion updatable_region;
storage::SharedRegion *static_shared_region;
storage::SharedRegion *updatable_shared_region;
std::shared_ptr<DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>> facade_factory;
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT> facade_factory;
};
}

Expand Down

0 comments on commit ba78fa8

Please sign in to comment.