osgEarth 2.1.1
Public Member Functions | Protected Member Functions | Private Attributes

osgEarth::Threading::ReadWriteMutex Class Reference

Collaboration diagram for osgEarth::Threading::ReadWriteMutex:

List of all members.

Public Member Functions

 ReadWriteMutex ()
void readLock ()
void readUnlock ()
void writeLock ()
void writeUnlock ()

Protected Member Functions

void incrementReaderCount ()
void decrementReaderCount ()

Private Attributes

int _readerCount
OpenThreads::Mutex _lockWriterMutex
OpenThreads::Mutex _readerCountMutex
Event _noWriterEvent
Event _noReadersEvent

Detailed Description

Custom read/write lock. The read/write lock in OSG can unlock mutexes from a different thread than the one that locked them - this can hang the thread in Windows.

Adapted from: http://www.codeproject.com/KB/threads/ReadWriteLock.aspx

Definition at line 156 of file ThreadingUtils.


Constructor & Destructor Documentation

osgEarth::Threading::ReadWriteMutex::ReadWriteMutex ( ) [inline]

Definition at line 165 of file ThreadingUtils.

Here is the call graph for this function:


Member Function Documentation

void osgEarth::Threading::ReadWriteMutex::decrementReaderCount ( ) [inline, protected]

Definition at line 255 of file ThreadingUtils.

        {
            OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _readerCountMutex );
            _readerCount--;               // remove a reader
            if ( _readerCount <= 0 )      // if that was the last one, signal that writers are now allowed
                _noReadersEvent.set();
        }

Here is the call graph for this function:

Here is the caller graph for this function:

void osgEarth::Threading::ReadWriteMutex::incrementReaderCount ( ) [inline, protected]

Definition at line 248 of file ThreadingUtils.

        {
            OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _readerCountMutex );
            _readerCount++;            // add a reader
            _noReadersEvent.reset();   // there's at least one reader now so clear the flag
        }

Here is the call graph for this function:

Here is the caller graph for this function:

void osgEarth::Threading::ReadWriteMutex::readLock ( ) [inline]

Definition at line 172 of file ThreadingUtils.

        {

#ifdef TRACE_THREADS
            {
                OpenThreads::ScopedLock<OpenThreads::Mutex> ttLock(_traceMutex);
                if( _trace.find(OpenThreads::Thread::CurrentThread()) != _trace.end() )
                    OE_WARN << "TRACE: tried to double-lock" << std::endl;
            }
#endif
            for( ; ; )
            {
                _noWriterEvent.wait();             // wait for a writer to quit if there is one
                incrementReaderCount();            // register this reader
                if ( !_noWriterEvent.isSet() )     // double lock check, in case a writer snuck in while inrementing
                    decrementReaderCount();        // if it did, undo the registration and try again
                else
                    break;                         // otherwise, we're in
            }

#ifdef TRACE_THREADS
            {
                OpenThreads::ScopedLock<OpenThreads::Mutex> ttLock(_traceMutex);
                _trace.insert(OpenThreads::Thread::CurrentThread());
            }
#endif
        }

Here is the call graph for this function:

Here is the caller graph for this function:

void osgEarth::Threading::ReadWriteMutex::readUnlock ( ) [inline]

Definition at line 200 of file ThreadingUtils.

        {
            decrementReaderCount();                // unregister this reader
            
#ifdef TRACE_THREADS
            {
                OpenThreads::ScopedLock<OpenThreads::Mutex> ttLock(_traceMutex);
                _trace.erase(OpenThreads::Thread::CurrentThread());
            }
#endif
        }

Here is the call graph for this function:

Here is the caller graph for this function:

void osgEarth::Threading::ReadWriteMutex::writeLock ( ) [inline]

Definition at line 212 of file ThreadingUtils.

        {
#ifdef TRACE_THREADS
            {
                OpenThreads::ScopedLock<OpenThreads::Mutex> ttLock(_traceMutex);
                if( _trace.find(OpenThreads::Thread::CurrentThread()) != _trace.end() )
                    OE_WARN << "TRACE: tried to double-lock" << std::endl;
            }
#endif
            OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _lockWriterMutex ); // one at a time please
            _noWriterEvent.wait();    // wait for a writer to quit if there is one
            _noWriterEvent.reset();   // prevent further writers from joining
            _noReadersEvent.wait();   // wait for all readers to quit

#ifdef TRACE_THREADS
            {
                OpenThreads::ScopedLock<OpenThreads::Mutex> ttLock(_traceMutex);
                _trace.insert(OpenThreads::Thread::CurrentThread());
            }
#endif
        }

Here is the call graph for this function:

Here is the caller graph for this function:

void osgEarth::Threading::ReadWriteMutex::writeUnlock ( ) [inline]

Definition at line 234 of file ThreadingUtils.

        {
            _noWriterEvent.set();

#ifdef TRACE_THREADS
            {
                OpenThreads::ScopedLock<OpenThreads::Mutex> ttLock(_traceMutex);
                _trace.erase(OpenThreads::Thread::CurrentThread());
            }
#endif
        }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 265 of file ThreadingUtils.

Definition at line 268 of file ThreadingUtils.

Definition at line 267 of file ThreadingUtils.

Definition at line 264 of file ThreadingUtils.

Definition at line 266 of file ThreadingUtils.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines