Avoid a race in MatModels loading

Add a mutex to ensure an SGMatModels only loads its models once. Caught
be ASan, hurrah.
This commit is contained in:
James Turner
2020-08-26 10:37:54 +01:00
committed by Automatic Release Builder
parent 6167159795
commit fd191b51ce
2 changed files with 36 additions and 32 deletions
+34 -32
View File
@@ -28,7 +28,7 @@
#include <simgear/compiler.h> #include <simgear/compiler.h>
#include <map> #include <map>
#include <mutex>
#include <osg/AlphaFunc> #include <osg/AlphaFunc>
#include <osg/Group> #include <osg/Group>
@@ -103,37 +103,39 @@ SGMatModel::get_model_count( SGPropertyNode *prop_root )
inline void inline void
SGMatModel::load_models( SGPropertyNode *prop_root ) SGMatModel::load_models( SGPropertyNode *prop_root )
{ {
// Load model only on demand std::lock_guard<std::mutex> g(_loadMutex);
if (!_models_loaded) {
for (unsigned int i = 0; i < _paths.size(); i++) { // Load model only on demand
osg::Node *entity = SGModelLib::loadModel(_paths[i], prop_root); if (!_models_loaded) {
if (entity != 0) { for (unsigned int i = 0; i < _paths.size(); i++) {
// FIXME: this stuff can be handled osg::Node* entity = SGModelLib::loadModel(_paths[i], prop_root);
// in the XML wrapper as well (at least, if (entity != 0) {
// the billboarding should be handled // FIXME: this stuff can be handled
// there). // in the XML wrapper as well (at least,
// the billboarding should be handled
if (_heading_type == HEADING_BILLBOARD) { // there).
// if the model is a billboard, it is likely :
// 1. a branch with only leaves, if (_heading_type == HEADING_BILLBOARD) {
// 2. a tree or a non rectangular shape faked by transparency // if the model is a billboard, it is likely :
// We add alpha clamp then // 1. a branch with only leaves,
osg::StateSet* stateSet = entity->getOrCreateStateSet(); // 2. a tree or a non rectangular shape faked by transparency
osg::AlphaFunc* alphaFunc = // We add alpha clamp then
new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.01f); osg::StateSet* stateSet = entity->getOrCreateStateSet();
stateSet->setAttributeAndModes(alphaFunc, osg::AlphaFunc* alphaFunc =
osg::StateAttribute::OVERRIDE); new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0.01f);
stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); stateSet->setAttributeAndModes(alphaFunc,
} osg::StateAttribute::OVERRIDE);
stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
_models.push_back(entity); }
} else { _models.push_back(entity);
SG_LOG(SG_INPUT, SG_ALERT, "Failed to load object " << _paths[i]);
// Ensure the vector contains something, otherwise get_random_model below fails } else {
_models.push_back(new osg::Node()); SG_LOG(SG_INPUT, SG_ALERT, "Failed to load object " << _paths[i]);
} // Ensure the vector contains something, otherwise get_random_model below fails
} _models.push_back(new osg::Node());
}
}
} }
_models_loaded = true; _models_loaded = true;
} }
+2
View File
@@ -149,6 +149,8 @@ private:
double _spacing_m; double _spacing_m;
double _range_m; double _range_m;
HeadingType _heading_type; HeadingType _heading_type;
std::mutex _loadMutex;
}; };