/usr/include/osgEarth/ThreadingUtils is in libosgearth-dev 2.7.0+dfsg-2+b3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | /* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
* Copyright 2015 Pelican Mapping
* http://osgearth.org
*
* osgEarth is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef OSGEARTH_THREADING_UTILS_H
#define OSGEARTH_THREADING_UTILS_H 1
#include <osgEarth/Common>
#include <OpenThreads/Condition>
#include <OpenThreads/Mutex>
#include <OpenThreads/Thread>
#include <osg/ref_ptr>
#include <set>
#include <map>
#define USE_CUSTOM_READ_WRITE_LOCK 1
//#ifdef _DEBUG
//# define TRACE_THREADS 1
//#endif
namespace osgEarth { namespace Threading
{
typedef OpenThreads::Mutex Mutex;
typedef OpenThreads::ScopedLock<OpenThreads::Mutex> ScopedMutexLock;
typedef OpenThreads::Thread Thread;
/**
* Gets the unique ID of the running thread. Use this instead of
* OpenThreads::Thread::CurrentThread, which only works reliably on
* threads created with the OpenThreads framework
*/
extern OSGEARTH_EXPORT unsigned getCurrentThreadId();
#ifdef USE_CUSTOM_READ_WRITE_LOCK
/**
* Event with a toggled signal state.
*/
class Event
{
public:
Event() : _set( false ) { }
~Event() {
reset();
for( int i=0; i<255; ++i ) // workaround buggy broadcast
_cond.signal();
}
inline bool wait() {
OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _m );
return _set ? true : (_cond.wait( &_m ) == 0);
}
/** waits on a signal, and then automatically resets it before returning. */
inline bool waitAndReset() {
OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _m );
if ( _set ) {
_set = false;
return true;
}
else {
bool value = _cond.wait( &_m ) == 0;
_set = false;
return value;
}
}
inline void set() {
OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _m );
if ( !_set ) {
_set = true;
_cond.broadcast(); // possible deadlock before OSG r10457 on windows
//_cond.signal();
}
}
inline void reset() {
OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _m );
_set = false;
}
inline bool isSet() const {
return _set;
}
protected:
OpenThreads::Mutex _m;
OpenThreads::Condition _cond;
bool _set;
};
/** Same as an Event, but waits on multiple notifications before releasing its wait. */
class MultiEvent
{
public:
MultiEvent( int num =1 ) : _set( num ), _num(num) { }
~MultiEvent() {
reset();
for( int i=0; i<255; ++i ) // workaround buggy broadcast
_cond.signal();
}
inline bool wait() {
OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _m );
while( _set > 0 )
if ( _cond.wait( &_m ) != 0 )
return false;
return true;
}
/** waits on a signal, and then automatically resets it before returning. */
inline bool waitAndReset() {
OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _m );
while( _set > 0 )
if ( _cond.wait( &_m ) != 0 )
return false;
_set = _num;
return true;
}
inline void notify() {
OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _m );
if ( _set > 0 )
--_set;
if ( _set == 0 )
_cond.broadcast(); // possible deadlock before OSG r10457 on windows
//_cond.signal();
}
inline void reset( int num =0 ) {
OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _m );
if ( num > 0 ) _num = num;
_set = _num;
}
protected:
OpenThreads::Mutex _m;
OpenThreads::Condition _cond;
int _set, _num;
};
/**
* 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
*/
class ReadWriteMutex
{
#ifdef TRACE_THREADS
typedef std::set<unsigned> TracedThreads;
TracedThreads _trace;
OpenThreads::Mutex _traceMutex;
#endif
public:
ReadWriteMutex() :
_readerCount(0)
{
_noWriterEvent.set();
_noReadersEvent.set();
}
void readLock()
{
#ifdef TRACE_THREADS
{
OpenThreads::ScopedLock<OpenThreads::Mutex> ttLock(_traceMutex);
if( _trace.find(getCurrentThreadId()) != _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(getCurrentThreadId());
}
#endif
}
void readUnlock()
{
decrementReaderCount(); // unregister this reader
#ifdef TRACE_THREADS
{
OpenThreads::ScopedLock<OpenThreads::Mutex> ttLock(_traceMutex);
_trace.erase(getCurrentThreadId());
}
#endif
}
void writeLock()
{
#ifdef TRACE_THREADS
{
OpenThreads::ScopedLock<OpenThreads::Mutex> ttLock(_traceMutex);
if( _trace.find(getCurrentThreadId()) != _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(getCurrentThreadId());
}
#endif
}
void writeUnlock()
{
_noWriterEvent.set();
#ifdef TRACE_THREADS
{
OpenThreads::ScopedLock<OpenThreads::Mutex> ttLock(_traceMutex);
_trace.erase(getCurrentThreadId());
}
#endif
}
protected:
void incrementReaderCount()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock( _readerCountMutex );
_readerCount++; // add a reader
_noReadersEvent.reset(); // there's at least one reader now so clear the flag
}
void decrementReaderCount()
{
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();
}
private:
int _readerCount;
Mutex _lockWriterMutex;
Mutex _readerCountMutex;
Event _noWriterEvent;
Event _noReadersEvent;
};
struct ScopedWriteLock
{
ScopedWriteLock( ReadWriteMutex& lock ) : _lock(lock) { _lock.writeLock(); }
~ScopedWriteLock() { _lock.writeUnlock(); }
protected:
ReadWriteMutex& _lock;
};
struct ScopedReadLock
{
ScopedReadLock( ReadWriteMutex& lock ) : _lock(lock) { _lock.readLock(); }
~ScopedReadLock() { _lock.readUnlock(); }
protected:
ReadWriteMutex& _lock;
};
#else
typedef OpenThreads::ReadWriteMutex ReadWriteMutex;
typedef OpenThreads::ScopedWriteLock ScopedWriteLock;
typedef OpenThreads::ScopedReadLock ScopedReadLock;
#endif
} } // namepsace osgEarth::Threading
#endif // OSGEARTH_THREADING_UTILS_H
|