Skip to content

Commit

Permalink
Fix refactor error and fix the gate
Browse files Browse the repository at this point in the history
  • Loading branch information
gwaldron committed Feb 16, 2024
1 parent 9b798e5 commit c5a12ce
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 25 deletions.
4 changes: 1 addition & 3 deletions src/osgEarth/ElevationLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,7 @@ ElevationLayer::createHeightFieldInKeyProfile(const TileKey& key, ProgressCallba
// at the same time. This helps a lot with elevation data since
// the many queries cross tile boundaries (like calculating
// normal maps)
ScopedGate<TileKey> gate(_sentry, key, [&]() {
return _memCache.valid();
});
ScopedGate<TileKey> gate(_sentry, key, _memCache.valid());

// Check the memory cache first
bool fromMemCache = false;
Expand Down
4 changes: 1 addition & 3 deletions src/osgEarth/ImageLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,7 @@ ImageLayer::createImageInKeyProfile(const TileKey& key, ProgressCallback* progre
// Tile gate prevents two threads from requesting the same key
// at the same time, which would be unnecessary work. Only lock
// the gate if there is an L2 cache active
ScopedGate<TileKey> scopedGate(_sentry, key, [&]() {
return _memCache.valid();
});
ScopedGate<TileKey> scopedGate(_sentry, key, _memCache.valid());

GeoImage result;

Expand Down
1 change: 1 addition & 0 deletions src/osgEarth/ObjectIndex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <osg/Drawable>
#include <osg/Array>
#include <algorithm>
#include <unordered_map>

#define OSGEARTH_OBJECTID_EMPTY (ObjectID)0
#define OSGEARTH_OBJECTID_TERRAIN (ObjectID)1
Expand Down
14 changes: 6 additions & 8 deletions src/osgEarth/Threading
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
#pragma once
#include <osgEarth/Export>
#include <unordered_map>
#include <unordered_set>

// bring in weejobs in the jobs namespace
#define WEEJOBS_EXPORT OSGEARTH_EXPORT
Expand Down Expand Up @@ -156,12 +156,10 @@ namespace osgEarth

inline void lock(const T& key) {
std::unique_lock<std::mutex> lock(_m);
auto thread_id = std::this_thread::get_id();
for (;;) {
auto i = _keys.emplace(key, thread_id);
if (i.second)
auto i = _keys.emplace(key);
if (i.second == true) // insert successful
return;
OE_HARD_ASSERT(i.first->second != thread_id, "Recursive Gate access attempt");
_unlocked.wait(lock);
}
}
Expand All @@ -175,7 +173,7 @@ namespace osgEarth
private:
std::mutex _m;
std::condition_variable_any _unlocked;
std::unordered_map<T, std::thread::id> _keys;
std::unordered_set<T> _keys;
};

//! Gate the locks for the duration of this object's scope
Expand All @@ -194,10 +192,10 @@ namespace osgEarth

//! Lock a gate based on key "key" IFF the predicate is true,
//! else it's a nop.
ScopedGate(Gate<T>& gate, const T& key, std::function<bool()> pred) :
ScopedGate(Gate<T>& gate, const T& key, bool pred) :
_gate(gate),
_key(key),
_active(pred())
_active(pred)
{
if (_active)
_gate.lock(_key);
Expand Down
12 changes: 1 addition & 11 deletions src/osgEarthDrivers/engine_rex/TileNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ using namespace osgEarth::Util;

#define LC "[TileNode] "

// template to capture the result type of a function:
template<typename T>
struct result_type;

template<typename R, typename...A>
struct result_type<R(A...)>
{
typedef R type;
};

namespace
{
// Scale and bias matrices, one for each TileKey quadrant.
Expand Down Expand Up @@ -751,7 +741,7 @@ TileNode::createChildren()
CreateChildResult result;

osg::ref_ptr<TileNode> tile;
if (tile_weakptr.lock(tile) && state.canceled())
if (tile_weakptr.lock(tile) && !state.canceled())
result = tile->createChild(childkey, &state);

return result;
Expand Down

0 comments on commit c5a12ce

Please sign in to comment.