MaterialLib: make it thread-safe

This commit is contained in:
James Turner 2021-07-29 17:50:14 +01:00 committed by Automatic Release Builder
parent b79f03c96c
commit 5fe527a2d1
2 changed files with 18 additions and 6 deletions

View File

@ -83,6 +83,8 @@ bool SGMaterialLib::load( const SGPath &fg_root, const SGPath& mpath,
options->setObjectCacheHint(osgDB::Options::CACHE_ALL);
options->setDatabasePath(fg_root.utf8Str());
std::lock_guard<std::mutex> g(d->mutex);
simgear::PropertyList blocks = materialblocks.getChildren("region");
simgear::PropertyList::const_iterator block_iter = blocks.begin();
@ -153,14 +155,20 @@ bool SGMaterialLib::load( const SGPath &fg_root, const SGPath& mpath,
// find a material record by material name and tile center
SGMaterial *SGMaterialLib::find( const string& material, const SGVec2f center ) const
{
std::lock_guard<std::mutex> g(d->mutex);
return internalFind(material, center);
}
SGMaterial* SGMaterialLib::internalFind(const string& material, const SGVec2f center) const
{
SGMaterial *result = NULL;
const_material_map_iterator it = matlib.find( material );
if ( it != end() ) {
if (it != end()) {
// We now have a list of materials that match this
// name. Find the first one that matches.
// We start at the end of the list, as the materials
// list is ordered with the smallest regions at the end.
// We start at the end of the list, as the materials
// list is ordered with the smallest regions at the end.
material_list::const_reverse_iterator iter = it->second.rbegin();
while (iter != it->second.rend()) {
result = *iter;
@ -184,11 +192,12 @@ SGMaterial *SGMaterialLib::find( const string& material, const SGGeod& center )
SGMaterialCache *SGMaterialLib::generateMatCache(SGVec2f center)
{
SGMaterialCache* newCache = new SGMaterialCache();
std::lock_guard<std::mutex> g(d->mutex);
material_map::const_reverse_iterator it = matlib.rbegin();
for (; it != matlib.rend(); ++it) {
newCache->insert(it->first, find(it->first, center));
newCache->insert(it->first, internalFind(it->first, center));
}
return newCache;
}

View File

@ -81,7 +81,10 @@ private:
typedef material_map::const_iterator const_material_map_iterator;
material_map matlib;
SGMaterial* internalFind(const std::string& material, const SGVec2f center) const;
public:
// Constructor