osgEarth 2.1.1
|
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 |
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.
osgEarth::Threading::ReadWriteMutex::ReadWriteMutex | ( | ) | [inline] |
Definition at line 165 of file ThreadingUtils.
: _readerCount(0) { _noWriterEvent.set(); _noReadersEvent.set(); }
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(); }
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 }
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 }
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 }
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 }
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 }
OpenThreads::Mutex osgEarth::Threading::ReadWriteMutex::_lockWriterMutex [private] |
Definition at line 265 of file ThreadingUtils.
Definition at line 268 of file ThreadingUtils.
Definition at line 267 of file ThreadingUtils.
int osgEarth::Threading::ReadWriteMutex::_readerCount [private] |
Definition at line 264 of file ThreadingUtils.
OpenThreads::Mutex osgEarth::Threading::ReadWriteMutex::_readerCountMutex [private] |
Definition at line 266 of file ThreadingUtils.