Introduced mutex into Terrain node to manage the tile system data structures

This commit is contained in:
Robert Osfield 2008-03-28 15:31:46 +00:00
parent 49accb1635
commit 3f81a994b7
3 changed files with 27 additions and 24 deletions

View File

@ -36,17 +36,6 @@ class OSGTERRAIN_EXPORT Terrain : public osg::Group
virtual void traverse(osg::NodeVisitor& nv);
/** Set the TerrainTechnique that is used as the prototype for all Terrain below the Terrain. */
void setTerrainTechnique(osgTerrain::TerrainTechnique* TerrainTechnique);
/** Get the TerrainTechnique*/
TerrainTechnique* getTerrainTechnique() { return _terrainTechnique.get(); }
/** Get the const TerrainTechnique*/
const TerrainTechnique* getTerrainTechnique() const { return _terrainTechnique.get(); }
/** Get the TerrainTile for a given TileID.*/
TerrainTile* getTile(const TileID& tileID);
@ -65,9 +54,7 @@ class OSGTERRAIN_EXPORT Terrain : public osg::Group
typedef std::map< TileID, TerrainTile* > TerrainTileMap;
typedef std::set< TerrainTile* > TerrainTileSet;
osg::ref_ptr<TerrainTechnique> _terrainTechnique;
mutable OpenThreads::Mutex _mutex;
TerrainTileSet _terrainTileSet;
TerrainTileMap _terrainTileMap;

View File

@ -11,8 +11,8 @@
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGTERRAIN_terrainTileTECHNIQUE
#define OSGTERRAIN_terrainTileTECHNIQUE 1
#ifndef OSGTERRAIN_TERRAINTECHNIQUE
#define OSGTERRAIN_TERRAINTECHNIQUE 1
#include <osg/Object>

View File

@ -12,6 +12,7 @@
*/
#include <osgTerrain/Terrain>
#include <OpenThreads/ScopedLock>
using namespace osg;
using namespace osgTerrain;
@ -20,8 +21,16 @@ Terrain::Terrain()
{
}
Terrain::Terrain(const Terrain& ts, const osg::CopyOp& copyop):
osg::Group(ts,copyop)
{
}
Terrain::~Terrain()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
for(TerrainTileSet::iterator itr = _terrainTileSet.begin();
itr != _terrainTileSet.end();
++itr)
@ -33,10 +42,6 @@ Terrain::~Terrain()
_terrainTileMap.clear();
}
Terrain::Terrain(const Terrain& ts, const osg::CopyOp& copyop)
{
}
void Terrain::traverse(osg::NodeVisitor& nv)
{
Group::traverse(nv);
@ -44,6 +49,8 @@ void Terrain::traverse(osg::NodeVisitor& nv)
TerrainTile* Terrain::getTile(const TileID& tileID)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
TerrainTileMap::iterator itr = _terrainTileMap.find(tileID);
if (itr != _terrainTileMap.end()) return 0;
@ -52,16 +59,21 @@ TerrainTile* Terrain::getTile(const TileID& tileID)
const TerrainTile* Terrain::getTile(const TileID& tileID) const
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
TerrainTileMap::const_iterator itr = _terrainTileMap.find(tileID);
if (itr != _terrainTileMap.end()) return 0;
return itr->second;
}
static unsigned int s_maxNumTiles = 0;
void Terrain::registerTerrainTile(TerrainTile* tile)
{
if (!tile) return;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
if (tile->getTileID().valid())
{
_terrainTileMap[tile->getTileID()] = tile;
@ -69,13 +81,17 @@ void Terrain::registerTerrainTile(TerrainTile* tile)
_terrainTileSet.insert(tile);
osg::notify(osg::INFO)<<"Terrain::registerTerrainTile "<<tile<<" total number of tile "<<_terrainTileSet.size()<<std::endl;
if (_terrainTileSet.size() > s_maxNumTiles) s_maxNumTiles = _terrainTileSet.size();
// osg::notify(osg::NOTICE)<<"Terrain::registerTerrainTile "<<tile<<" total number of tile "<<_terrainTileSet.size()<<" max = "<<s_maxNumTiles<<std::endl;
}
void Terrain::unregisterTerrainTile(TerrainTile* tile)
{
if (!tile) return;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
if (tile->getTileID().valid())
{
_terrainTileMap.erase(tile->getTileID());
@ -83,5 +99,5 @@ void Terrain::unregisterTerrainTile(TerrainTile* tile)
_terrainTileSet.erase(tile);
osg::notify(osg::INFO)<<"Terrain::unregisterTerrainTile "<<tile<<" total number of tile "<<_terrainTileSet.size()<<std::endl;
// osg::notify(osg::NOTICE)<<"Terrain::unregisterTerrainTile "<<tile<<" total number of tile "<<_terrainTileSet.size()<<" max = "<<s_maxNumTiles<<std::endl;
}