Skip to content

Commit d645a50

Browse files
committed
Use atomic to store pending orphaned objects size
To avoid locking a mutex when the container is empty.
1 parent 8890897 commit d645a50

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

include/osg/BufferObject

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ class OSG_EXPORT GLBufferObjectSet : public Referenced
323323
BufferObjectProfile _profile;
324324
unsigned int _numOfGLBufferObjects;
325325
GLBufferObjectList _orphanedGLBufferObjects;
326+
OpenThreads::Atomic _pendingOrphanedGLBufferObjectsSize;
326327
GLBufferObjectVector _pendingOrphanedGLBufferObjects;
327328

328329
GLBufferObject* _head;

include/osg/Texture

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,7 @@ protected:
11771177
Texture::TextureProfile _profile;
11781178
unsigned int _numOfTextureObjects;
11791179
Texture::TextureObjectList _orphanedTextureObjects;
1180+
OpenThreads::Atomic _pendingOrphanedTextureObjectsSize;
11801181
TextureObjectVector _pendingOrphanedTextureObjects;
11811182

11821183
Texture::TextureObject* _head;

src/osg/BufferObject.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ void GLBufferObjectSet::handlePendingOrphandedGLBufferObjects()
382382
_parent->getNumberActiveGLBufferObjects() -= numOrphaned;
383383

384384
_pendingOrphanedGLBufferObjects.clear();
385+
_pendingOrphanedGLBufferObjectsSize.exchange(0);
385386

386387
CHECK_CONSISTENCY
387388
}
@@ -390,6 +391,7 @@ void GLBufferObjectSet::deleteAllGLBufferObjects()
390391
{
391392
// OSG_NOTICE<<"GLBufferObjectSet::deleteAllGLBufferObjects()"<<std::endl;
392393

394+
if (_pendingOrphanedGLBufferObjectsSize != 0)
393395
{
394396
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
395397
// OSG_NOTICE<<"GLBufferObjectSet::flushDeletedGLBufferObjects(..) handling orphans"<<std::endl;
@@ -466,6 +468,7 @@ void GLBufferObjectSet::discardAllGLBufferObjects()
466468

467469
void GLBufferObjectSet::flushAllDeletedGLBufferObjects()
468470
{
471+
if (_pendingOrphanedGLBufferObjectsSize != 0)
469472
{
470473
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
471474
// OSG_NOTICE<<"GLBufferObjectSet::flushDeletedGLBufferObjects(..) handling orphans"<<std::endl;
@@ -495,6 +498,7 @@ void GLBufferObjectSet::discardAllDeletedGLBufferObjects()
495498
// OSG_NOTICE<<"GLBufferObjectSet::discardAllDeletedGLBufferObjects()"<<std::endl;
496499

497500
// clean up the pending orphans.
501+
if (_pendingOrphanedGLBufferObjectsSize != 0)
498502
{
499503
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
500504
// OSG_NOTICE<<"GLBufferObjectSet::flushDeletedGLBufferObjects(..) handling orphans"<<std::endl;
@@ -519,6 +523,7 @@ void GLBufferObjectSet::discardAllDeletedGLBufferObjects()
519523

520524
void GLBufferObjectSet::flushDeletedGLBufferObjects(double /*currentTime*/, double& availableTime)
521525
{
526+
if (_pendingOrphanedGLBufferObjectsSize != 0)
522527
{
523528
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
524529
// OSG_NOTICE<<"GLBufferObjectSet::flushDeletedGLBufferObjects(..) handling orphans"<<std::endl;
@@ -794,6 +799,7 @@ void GLBufferObjectSet::orphan(GLBufferObject* to)
794799
// list. This double buffered approach to handling orphaned TO's is used
795800
// to avoid having to mutex the process of appling active TO's.
796801
_pendingOrphanedGLBufferObjects.push_back(to);
802+
++_pendingOrphanedGLBufferObjectsSize;
797803
}
798804

799805
void GLBufferObjectSet::remove(GLBufferObject* to)

src/osg/Texture.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ void TextureObjectSet::handlePendingOrphandedTextureObjects()
547547
_parent->getNumberActiveTextureObjects() -= numOrphaned;
548548

549549
_pendingOrphanedTextureObjects.clear();
550+
_pendingOrphanedTextureObjectsSize.exchange(0);
550551

551552
CHECK_CONSISTENCY
552553
}
@@ -556,6 +557,7 @@ void TextureObjectSet::deleteAllTextureObjects()
556557
{
557558
// OSG_NOTICE<<"TextureObjectSet::deleteAllTextureObjects()"<<std::endl;
558559

560+
if (_pendingOrphanedTextureObjectsSize != 0)
559561
{
560562
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
561563
// OSG_NOTICE<<"TextureObjectSet::flushDeletedTextureObjects(..) handling orphans"<<std::endl;
@@ -619,6 +621,7 @@ void TextureObjectSet::discardAllTextureObjects()
619621
_tail = 0;
620622

621623
_pendingOrphanedTextureObjects.clear();
624+
_pendingOrphanedTextureObjectsSize.exchange(0);
622625
_orphanedTextureObjects.clear();
623626

624627
unsigned int numDeleted = _numOfTextureObjects;
@@ -633,6 +636,7 @@ void TextureObjectSet::discardAllTextureObjects()
633636
void TextureObjectSet::flushAllDeletedTextureObjects()
634637
{
635638
// OSG_NOTICE<<"TextureObjectSet::flushAllDeletedTextureObjects()"<<std::endl;
639+
if (_pendingOrphanedTextureObjectsSize != 0)
636640
{
637641
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
638642
// OSG_NOTICE<<"TextureObjectSet::flushDeletedTextureObjects(..) handling orphans"<<std::endl;
@@ -666,6 +670,7 @@ void TextureObjectSet::discardAllDeletedTextureObjects()
666670
// OSG_NOTICE<<"TextureObjectSet::discardAllDeletedTextureObjects()"<<std::endl;
667671

668672
// clean up the pending orphans.
673+
if (_pendingOrphanedTextureObjectsSize != 0)
669674
{
670675
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
671676
// OSG_NOTICE<<"TextureObjectSet::flushDeletedTextureObjects(..) handling orphans"<<std::endl;
@@ -691,6 +696,7 @@ void TextureObjectSet::flushDeletedTextureObjects(double /*currentTime*/, double
691696
{
692697
// OSG_NOTICE<<"TextureObjectSet::flushDeletedTextureObjects(..)"<<std::endl;
693698

699+
if (_pendingOrphanedTextureObjectsSize != 0)
694700
{
695701
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
696702
// OSG_NOTICE<<"TextureObjectSet::flushDeletedTextureObjects(..) handling orphans"<<std::endl;
@@ -756,6 +762,7 @@ void TextureObjectSet::flushDeletedTextureObjects(double /*currentTime*/, double
756762

757763
bool TextureObjectSet::makeSpace(unsigned int& size)
758764
{
765+
if (_pendingOrphanedTextureObjectsSize != 0)
759766
{
760767
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
761768
// OSG_NOTICE<<"TextureObjectSet::TextureObjectSet::makeSpace(..) handling orphans"<<std::endl;
@@ -980,6 +987,7 @@ void TextureObjectSet::orphan(Texture::TextureObject* to)
980987
// list. This double buffered approach to handling orphaned TO's is used
981988
// to avoid having to mutex the process of appling active TO's.
982989
_pendingOrphanedTextureObjects.push_back(to);
990+
++_pendingOrphanedTextureObjectsSize;
983991

984992
#if 0
985993
OSG_NOTICE<<"TextureObjectSet::orphan("<<to<<") _pendingOrphanedTextureObjects.size()="<<_pendingOrphanedTextureObjects.size()<<std::endl;

0 commit comments

Comments
 (0)