]> git.mxchange.org Git - simgear.git/commitdiff
Use observer_ptr::lock for thread-safe pointer retrieval.
authorThorstenB <brehmt@gmail.com>
Mon, 2 Apr 2012 18:09:02 +0000 (20:09 +0200)
committerThorstenB <brehmt@gmail.com>
Mon, 2 Apr 2012 18:47:27 +0000 (20:47 +0200)
Also revert to using ref_ptr for the top-level EffectMap, since it holds
elements no one else references (and don't affect memory much).

simgear/scene/material/TextureBuilder.cxx
simgear/scene/material/makeEffect.cxx
simgear/scene/material/mat.cxx
simgear/scene/sky/newcloud.cxx
simgear/scene/tgdb/TreeBin.cxx
simgear/scene/tgdb/pt_lights.cxx

index 679d67e3215f27f67f57e98ccc84fdd7e4f55b73..c06883aab0f5b0eda332e3269eb4ec472c44cf78 100644 (file)
@@ -291,20 +291,20 @@ Texture* TexBuilder<T>::build(Effect* effect, Pass* pass, const SGPropertyNode*
     TexTuple attrs = makeTexTuple(effect, props, options, _type);
     typename TexMap::iterator itr = texMap.find(attrs);
 
-    if (itr != texMap.end())
+    ref_ptr<T> tex;
+    if ((itr != texMap.end())&&
+        (itr->second.lock(tex)))
     {
-        T* tex = itr->second.get();
-        if (tex)
-            return tex;
+        return tex.release();
     }
 
-    T* tex = new T;
+    tex = new T;
     setAttrs(attrs, tex, options);
     if (itr == texMap.end())
         texMap.insert(make_pair(attrs, tex));
     else
         itr->second = tex; // update existing, but empty observer
-    return tex;
+    return tex.release();
 }
 
 
index 4ca288b87f5603dbcf8a9c11a50f0a53b7719e5b..06bf47b229b1d8d118816f85a3c60a2f63e41e53 100644 (file)
@@ -44,7 +44,7 @@ using namespace osg;
 using namespace effect;
 
 typedef vector<const SGPropertyNode*> RawPropVector;
-typedef map<const string, observer_ptr<Effect> > EffectMap;
+typedef map<const string, ref_ptr<Effect> > EffectMap;
 
 namespace
 {
@@ -206,10 +206,10 @@ Effect* makeEffect(SGPropertyNode* prop,
                     lock(effectMutex);
                 cache = parent->getCache();
                 itr = cache->find(key);
-                if (itr != cache->end()) {
-                    effect = itr->second.get();
-                    if (effect.valid())
-                        effect->generator = parent->generator;  // Copy the generators
+                if ((itr != cache->end())&&
+                    itr->second.lock(effect))
+                {
+                    effect->generator = parent->generator;  // Copy the generators
                 }
             }
             if (!effect.valid()) {
@@ -222,8 +222,8 @@ Effect* makeEffect(SGPropertyNode* prop,
                 pair<Effect::Cache::iterator, bool> irslt
                     = cache->insert(make_pair(key, effect));
                 if (!irslt.second) {
-                    ref_ptr<Effect> old = irslt.first->second.get();
-                    if (old.valid())
+                    ref_ptr<Effect> old;
+                    if (irslt.first->second.lock(old))
                         effect = old; // Another thread beat us in creating it! Discard our own...
                     else
                         irslt.first->second = effect; // update existing, but empty observer
index 582571f3096d734e58558fb077520530c44f32b6..ec5164bd5c17711e770c12c9d30302875b2e1f5c 100644 (file)
@@ -378,6 +378,8 @@ SGMaterial::init ()
 Effect* SGMaterial::get_effect(int i)
 {    
     if(!_status[i].effect_realized) {
+        if (!_status[i].effect.valid())
+            return 0;
         _status[i].effect->realizeTechniques(_status[i].options.get());
         _status[i].effect_realized = true;
     }
@@ -459,7 +461,8 @@ void SGMaterial::buildEffectProperties(const SGReaderWriterOptions* options)
         makeChild(effectParamProp, "light-coverage")->setDoubleValue(light_coverage);
 
         matState.effect = makeEffect(effectProp, false, options);
-        matState.effect->setUserData(user.get());
+        if (matState.effect.valid())
+            matState.effect->setUserData(user.get());
     }
 }
 
index 02f04c197f10a41d8d7ce75f64c7a938286c8c79..2f24298c5c3188603e65371af71dccc313869c70 100644 (file)
@@ -103,11 +103,8 @@ SGNewCloud::SGNewCloud(const SGPath &texture_root, const SGPropertyNode *cld_def
     // Create a new Effect for the texture, if required.
     EffectMap::iterator iter = effectMap.find(texture);
 
-    if (iter != effectMap.end()) {
-        effect = iter->second.get();
-    }
-
-    if (!effect.valid())
+    if ((iter == effectMap.end())||
+        (!iter->second.lock(effect)))
     {
         SGPropertyNode_ptr pcloudEffect = new SGPropertyNode;
         makeChild(pcloudEffect, "inherits-from")->setValue("Effects/cloud");
index a0203ebce561f91d25696e3cf55302a32ce25b2b..b0ca2e02b31488ee7b7e453061cbb878af653ade 100644 (file)
@@ -302,10 +302,8 @@ osg::Group* createForest(SGTreeBinList& forestList, const osg::Matrix& transform
         ref_ptr<Effect> effect;
         EffectMap::iterator iter = treeEffectMap.find(forest->texture);
 
-        if (iter != treeEffectMap.end())
-            effect = iter->second.get();
-
-        if (!effect.valid())
+        if ((iter == treeEffectMap.end())||
+            (!iter->second.lock(effect)))
         {
             SGPropertyNode_ptr effectProp = new SGPropertyNode;
             makeChild(effectProp, "inherits-from")->setStringValue("Effects/tree");
index e4ec0d5995ae7de00936ffd9a3f603e121666ca2..2aa6606acbdc56c0cca96914621771ea2e641c47 100644 (file)
@@ -174,9 +174,12 @@ Effect* getLightEffect(float size, const Vec3& attenuation,
     PointParams pointParams(size, attenuation, minSize, maxSize, directional);
     ScopedLock<Mutex> lock(lightMutex);
     EffectMap::iterator eitr = effectMap.find(pointParams);
-    if ((eitr != effectMap.end())&&
-        eitr->second.valid())
-        return eitr->second.get();
+    if (eitr != effectMap.end())
+    {
+        ref_ptr<Effect> effect;
+        if (eitr->second.lock(effect))
+            return effect.release();
+    }
     // Basic stuff; no sprite or attenuation support
     Pass *basicPass = new Pass;
     basicPass->setRenderBinDetails(POINT_LIGHTS_BIN, "DepthSortedBin");