Added use of Mutex into osg::Stats to better handle multi-threaded usage

This commit is contained in:
Robert Osfield
2007-02-14 16:24:49 +00:00
parent 02ff109746
commit 810e7291fc
2 changed files with 48 additions and 10 deletions

View File

@@ -31,6 +31,8 @@ Stats::Stats(const std::string& name, unsigned int numberOfFrames):
void Stats::allocate(unsigned int numberOfFrames)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
_baseFrameNumber = 0;
_latestFrameNumber = 0;
_attributeMapList.clear();
@@ -41,6 +43,9 @@ void Stats::allocate(unsigned int numberOfFrames)
bool Stats::setAttribute(int frameNumber, const std::string& attributeName, double value)
{
if (frameNumber<getEarliestFrameNumber()) return false;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
if (frameNumber>_latestFrameNumber)
{
// need to advance
@@ -74,7 +79,7 @@ bool Stats::setAttribute(int frameNumber, const std::string& attributeName, doub
return true;
}
bool Stats::getAttribute(int frameNumber, const std::string& attributeName, double& value) const
bool Stats::getAttributeNoMutex(int frameNumber, const std::string& attributeName, double& value) const
{
int index = getIndex(frameNumber);
if (index<0) return false;
@@ -98,13 +103,15 @@ bool Stats::getAveragedAttribute(int startFrameNumber, int endFrameNumber, const
{
std::swap(endFrameNumber, startFrameNumber);
}
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
double total = 0.0;
double numValidSamples = 0.0;
for(int i = startFrameNumber; i<=endFrameNumber; ++i)
{
double v = 0.0;
if (getAttribute(i,attributeName,v))
if (getAttributeNoMutex(i,attributeName,v))
{
if (averageInInverseSpace) total += 1.0/v;
else total += v;
@@ -120,7 +127,7 @@ bool Stats::getAveragedAttribute(int startFrameNumber, int endFrameNumber, const
else return false;
}
Stats::AttributeMap& Stats::getAttributeMap(int frameNumber)
Stats::AttributeMap& Stats::getAttributeMapNoMutex(int frameNumber)
{
int index = getIndex(frameNumber);
if (index<0) return _invalidAttributeMap;
@@ -128,7 +135,7 @@ Stats::AttributeMap& Stats::getAttributeMap(int frameNumber)
return _attributeMapList[index];
}
const Stats::AttributeMap& Stats::getAttributeMap(int frameNumber) const
const Stats::AttributeMap& Stats::getAttributeMapNoMutex(int frameNumber) const
{
int index = getIndex(frameNumber);
if (index<0) return _invalidAttributeMap;
@@ -138,12 +145,14 @@ const Stats::AttributeMap& Stats::getAttributeMap(int frameNumber) const
void Stats::report(std::ostream& out, const char* indent) const
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
if (indent) out<<indent;
out<<"Stats "<<_name<<std::endl;
for(int i = getEarliestFrameNumber(); i<= getLatestFrameNumber(); ++i)
{
out<<" FrameNumber "<<i<<std::endl;
const osg::Stats::AttributeMap& attributes = getAttributeMap(i);
const osg::Stats::AttributeMap& attributes = getAttributeMapNoMutex(i);
for(osg::Stats::AttributeMap::const_iterator itr = attributes.begin();
itr != attributes.end();
++itr)
@@ -157,9 +166,11 @@ void Stats::report(std::ostream& out, const char* indent) const
void Stats::report(std::ostream& out, unsigned int frameNumber, const char* indent) const
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
if (indent) out<<indent;
out<<"Stats "<<_name<<" FrameNumber "<<frameNumber<<std::endl;
const osg::Stats::AttributeMap& attributes = getAttributeMap(frameNumber);
const osg::Stats::AttributeMap& attributes = getAttributeMapNoMutex(frameNumber);
for(osg::Stats::AttributeMap::const_iterator itr = attributes.begin();
itr != attributes.end();
++itr)