Skip to content
Snippets Groups Projects
Commit 8e971a00 authored by Johannes Junggeburth's avatar Johannes Junggeburth :dog2:
Browse files

GeoDeDuplicator - Ensure thread safety & declare destructor to be virtual

parent 59d53ef5
No related branches found
No related tags found
1 merge request!335GeoDeDuplicator - Ensure thread safety & declare destructor to be virtual
Pipeline #7674856 failed
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
#include "GeoModelHelpers/TransformSorter.h" #include "GeoModelHelpers/TransformSorter.h"
#include <set> #include <set>
#include <mutex>
#include <thread>
/*** /***
* Helper class to deduplicate shapes, volumes and non-alignable transforms in the GeoModel tree that are equivalent * Helper class to deduplicate shapes, volumes and non-alignable transforms in the GeoModel tree that are equivalent
* but instantiated in different places. Every time when the cache function is invoked, it's tried to insert the object * but instantiated in different places. Every time when the cache function is invoked, it's tried to insert the object
...@@ -71,7 +73,7 @@ class GeoDeDuplicator { ...@@ -71,7 +73,7 @@ class GeoDeDuplicator {
using GeoPhysVolPtr = GeoIntrusivePtr<GeoPhysVol>; using GeoPhysVolPtr = GeoIntrusivePtr<GeoPhysVol>;
GeoDeDuplicator() = default; GeoDeDuplicator() = default;
~GeoDeDuplicator() = default; virtual ~GeoDeDuplicator() = default;
GeoPhysVolPtr cacheVolume(GeoPhysVolPtr vol) const; GeoPhysVolPtr cacheVolume(GeoPhysVolPtr vol) const;
GeoTrfPtr makeTransform(const GeoTrf::Transform3D& trf) const; GeoTrfPtr makeTransform(const GeoTrf::Transform3D& trf) const;
...@@ -98,6 +100,8 @@ class GeoDeDuplicator { ...@@ -98,6 +100,8 @@ class GeoDeDuplicator {
static TrfSet s_trfStore; static TrfSet s_trfStore;
static ShapeSet s_shapeStore; static ShapeSet s_shapeStore;
mutable std::mutex m_mutex{};
}; };
......
...@@ -6,7 +6,9 @@ ...@@ -6,7 +6,9 @@
GeoDeDuplicator::TrfSet GeoDeDuplicator::s_trfStore{}; GeoDeDuplicator::TrfSet GeoDeDuplicator::s_trfStore{};
GeoDeDuplicator::ShapeSet GeoDeDuplicator::s_shapeStore{}; GeoDeDuplicator::ShapeSet GeoDeDuplicator::s_shapeStore{};
namespace {
std::mutex s_mutex{};
}
void GeoDeDuplicator::setShapeDeDuplication(bool enable){ void GeoDeDuplicator::setShapeDeDuplication(bool enable){
m_deDuplicateShape = enable; m_deDuplicateShape = enable;
...@@ -23,15 +25,18 @@ void GeoDeDuplicator::setPhysVolDeDuplication(bool enable) { ...@@ -23,15 +25,18 @@ void GeoDeDuplicator::setPhysVolDeDuplication(bool enable) {
GeoDeDuplicator::GeoTrfPtr GeoDeDuplicator::GeoTrfPtr
GeoDeDuplicator::makeTransform(const GeoTrf::Transform3D& trf) const { GeoDeDuplicator::makeTransform(const GeoTrf::Transform3D& trf) const {
GeoTrfPtr trfNode{new GeoTransform(trf)}; GeoTrfPtr trfNode{make_intrusive<GeoTransform>(trf)};
if (!m_deDuplicateTransform) { if (!m_deDuplicateTransform) {
std::lock_guard guard{m_mutex};
m_genericCache.push_back(trfNode); m_genericCache.push_back(trfNode);
return trfNode; return trfNode;
} }
std::lock_guard guard{s_mutex};
return *s_trfStore.emplace(trfNode).first; return *s_trfStore.emplace(trfNode).first;
} }
GeoDeDuplicator::GeoPhysVolPtr GeoDeDuplicator::GeoPhysVolPtr
GeoDeDuplicator::cacheVolume(GeoPhysVolPtr vol) const { GeoDeDuplicator::cacheVolume(GeoPhysVolPtr vol) const {
std::lock_guard guard{m_mutex};
if (!m_deDuplicatePhysVol) { if (!m_deDuplicatePhysVol) {
m_genericCache.push_back(vol); m_genericCache.push_back(vol);
return vol; return vol;
...@@ -40,6 +45,7 @@ GeoDeDuplicator::GeoPhysVolPtr ...@@ -40,6 +45,7 @@ GeoDeDuplicator::GeoPhysVolPtr
} }
GeoDeDuplicator::GeoLogVolPtr GeoDeDuplicator::GeoLogVolPtr
GeoDeDuplicator::cacheVolume(GeoLogVolPtr vol) const { GeoDeDuplicator::cacheVolume(GeoLogVolPtr vol) const {
std::lock_guard guard{m_mutex};
if (!m_deDuplicateLogVol) { if (!m_deDuplicateLogVol) {
m_genericCache.push_back(vol); m_genericCache.push_back(vol);
return vol; return vol;
...@@ -49,8 +55,10 @@ GeoDeDuplicator::GeoLogVolPtr ...@@ -49,8 +55,10 @@ GeoDeDuplicator::GeoLogVolPtr
GeoDeDuplicator::GeoShapePtr GeoDeDuplicator::GeoShapePtr
GeoDeDuplicator::cacheShape(GeoShapePtr shape) const { GeoDeDuplicator::cacheShape(GeoShapePtr shape) const {
if (!m_deDuplicateShape) { if (!m_deDuplicateShape) {
std::lock_guard guard{m_mutex};
m_genericCache.push_back(shape); m_genericCache.push_back(shape);
return shape; return shape;
} }
std::lock_guard guard{s_mutex};
return *s_shapeStore.insert(shape).first; return *s_shapeStore.insert(shape).first;
} }
...@@ -22,6 +22,7 @@ class ElementProcessor: public GeoDeDuplicator { ...@@ -22,6 +22,7 @@ class ElementProcessor: public GeoDeDuplicator {
public: public:
ElementProcessor() = default; ElementProcessor() = default;
virtual ~ElementProcessor() = default;
virtual void process(const xercesc::DOMElement *element, GmxUtil &gmxUtil, virtual void process(const xercesc::DOMElement *element, GmxUtil &gmxUtil,
GeoNodeList &toBeAdded); GeoNodeList &toBeAdded);
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment