Moved OpenThreads directly into OpenSceneGraph/trunk rather than being introduced via svn:externals.
This change has been done to make it easier for OpenSceneGraph users to check out the svn via https without any conflicts introduced with a http externals.
This commit is contained in:
100
include/OpenThreads/Barrier
Normal file
100
include/OpenThreads/Barrier
Normal file
@@ -0,0 +1,100 @@
|
||||
/* -*-c++-*- OpenThreads library, Copyright (C) 2002 - 2007 The Open Thread Group
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library 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
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
|
||||
//
|
||||
// Barrier - C++ barrier class
|
||||
// ~~~~~~~
|
||||
//
|
||||
|
||||
#ifndef _OPENTHREADS_BARRIER_
|
||||
#define _OPENTHREADS_BARRIER_
|
||||
|
||||
#include <OpenThreads/Exports>
|
||||
|
||||
namespace OpenThreads {
|
||||
|
||||
|
||||
/**
|
||||
* @class Barrier
|
||||
* @brief This class provides an object-oriented thread barrier interface
|
||||
*
|
||||
* @warning It is unwise to use the construct "Barrier barrier" in the
|
||||
* global namespace on sgi's. The object "barrier"
|
||||
* will confilict with the c-library sproc function "barrier" and
|
||||
* unpredictable results may occur. You have been warned.
|
||||
*/
|
||||
class OPENTHREAD_EXPORT_DIRECTIVE Barrier {
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
Barrier(int numThreads=0);
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
virtual ~Barrier();
|
||||
|
||||
/**
|
||||
* Reset the barrier to it's original state.
|
||||
*/
|
||||
virtual void reset();
|
||||
|
||||
/**
|
||||
* Block until numThreads threads have entered the barrier.
|
||||
*/
|
||||
virtual void block(unsigned int numThreads=0);
|
||||
|
||||
/**
|
||||
* Release the barrier, now.
|
||||
*/
|
||||
virtual void release();
|
||||
|
||||
/**
|
||||
* Return the number of threads currently blocked in the barrier,
|
||||
* Return -1 if error.
|
||||
*/
|
||||
virtual int numThreadsCurrentlyBlocked();
|
||||
|
||||
|
||||
void invalidate();
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Private copy constructor, to prevent tampering.
|
||||
*/
|
||||
Barrier(const Barrier &/*b*/) {};
|
||||
|
||||
/**
|
||||
* Private copy assignment, to prevent tampering.
|
||||
*/
|
||||
Barrier &operator=(const Barrier &/*b*/) {return *(this);};
|
||||
|
||||
/**
|
||||
* Implementation-specific private data.
|
||||
*/
|
||||
void *_prvData;
|
||||
|
||||
|
||||
bool _valid;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // !_OPENTHREADS_BARRIER_
|
||||
|
||||
177
include/OpenThreads/Block
Normal file
177
include/OpenThreads/Block
Normal file
@@ -0,0 +1,177 @@
|
||||
/* -*-c++-*- OpenThreads - Copyright (C) 1998-2007 Robert Osfield
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library 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
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef _OPENTHREADS_BLOCK_
|
||||
#define _OPENTHREADS_BLOCK_
|
||||
|
||||
#include <OpenThreads/Thread>
|
||||
#include <OpenThreads/Barrier>
|
||||
#include <OpenThreads/Condition>
|
||||
#include <OpenThreads/ScopedLock>
|
||||
|
||||
namespace OpenThreads {
|
||||
|
||||
/** Block is a block that can be used to halt a thread that is waiting another thread to release it.*/
|
||||
class Block
|
||||
{
|
||||
public:
|
||||
|
||||
Block():
|
||||
_released(false) {}
|
||||
|
||||
~Block()
|
||||
{
|
||||
release();
|
||||
}
|
||||
|
||||
inline bool block()
|
||||
{
|
||||
ScopedLock<OpenThreads::Mutex> mutlock(_mut);
|
||||
if( !_released )
|
||||
{
|
||||
return _cond.wait(&_mut)==0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool block(unsigned long timeout)
|
||||
{
|
||||
ScopedLock<OpenThreads::Mutex> mutlock(_mut);
|
||||
if( !_released )
|
||||
{
|
||||
return _cond.wait(&_mut, timeout)==0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
inline void release()
|
||||
{
|
||||
ScopedLock<OpenThreads::Mutex> mutlock(_mut);
|
||||
if (!_released)
|
||||
{
|
||||
_released = true;
|
||||
_cond.broadcast();
|
||||
}
|
||||
}
|
||||
|
||||
inline void reset()
|
||||
{
|
||||
ScopedLock<OpenThreads::Mutex> mutlock(_mut);
|
||||
_released = false;
|
||||
}
|
||||
|
||||
inline void set(bool doRelease)
|
||||
{
|
||||
if (doRelease!=_released)
|
||||
{
|
||||
if (doRelease) release();
|
||||
else reset();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
Mutex _mut;
|
||||
Condition _cond;
|
||||
bool _released;
|
||||
|
||||
private:
|
||||
|
||||
Block(const Block&) {}
|
||||
};
|
||||
|
||||
/** BlockCount is a block that can be used to halt a thread that is waiting for a specified number of operations to be completed.*/
|
||||
class BlockCount
|
||||
{
|
||||
public:
|
||||
|
||||
BlockCount(unsigned int blockCount):
|
||||
_blockCount(blockCount),
|
||||
_currentCount(0) {}
|
||||
|
||||
~BlockCount()
|
||||
{
|
||||
_blockCount = 0;
|
||||
release();
|
||||
}
|
||||
|
||||
inline void completed()
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
|
||||
if (_currentCount>0)
|
||||
{
|
||||
--_currentCount;
|
||||
|
||||
if (_currentCount==0)
|
||||
{
|
||||
// osg::notify(osg::NOTICE)<<"Released"<<std::endl;
|
||||
_cond.broadcast();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void block()
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
|
||||
if (_currentCount)
|
||||
_cond.wait(&_mut);
|
||||
}
|
||||
|
||||
inline void reset()
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
|
||||
if (_currentCount!=_blockCount)
|
||||
{
|
||||
if (_blockCount==0) _cond.broadcast();
|
||||
_currentCount = _blockCount;
|
||||
}
|
||||
}
|
||||
|
||||
inline void release()
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
|
||||
if (_currentCount)
|
||||
{
|
||||
_currentCount = 0;
|
||||
_cond.broadcast();
|
||||
}
|
||||
}
|
||||
|
||||
inline void setBlockCount(unsigned int blockCount) { _blockCount = blockCount; }
|
||||
|
||||
inline unsigned int getBlockCount() const { return _blockCount; }
|
||||
|
||||
inline unsigned int getCurrentCount() const { return _currentCount; }
|
||||
|
||||
protected:
|
||||
|
||||
OpenThreads::Mutex _mut;
|
||||
OpenThreads::Condition _cond;
|
||||
unsigned int _blockCount;
|
||||
unsigned int _currentCount;
|
||||
|
||||
private:
|
||||
|
||||
BlockCount(const BlockCount&) {}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
93
include/OpenThreads/Condition
Normal file
93
include/OpenThreads/Condition
Normal file
@@ -0,0 +1,93 @@
|
||||
/* -*-c++-*- OpenThreads library, Copyright (C) 2002 - 2007 The Open Thread Group
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library 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
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
|
||||
//
|
||||
// Condition - C++ condition class
|
||||
// ~~~~~~~~~
|
||||
//
|
||||
|
||||
#ifndef _OPENTHREADS_CONDITION_
|
||||
#define _OPENTHREADS_CONDITION_
|
||||
|
||||
#include <OpenThreads/Exports>
|
||||
#include <OpenThreads/Mutex>
|
||||
|
||||
namespace OpenThreads {
|
||||
|
||||
/**
|
||||
* @class Condition
|
||||
* @brief This class provides an object-oriented thread condition interface.
|
||||
*/
|
||||
class OPENTHREAD_EXPORT_DIRECTIVE Condition {
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
Condition();
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
virtual ~Condition();
|
||||
|
||||
/**
|
||||
* Wait on a mutex.
|
||||
*/
|
||||
virtual int wait(Mutex *mutex);
|
||||
|
||||
/**
|
||||
* Wait on a mutex for a given amount of time (ms)
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
virtual int wait(Mutex *mutex, unsigned long int ms);
|
||||
|
||||
/**
|
||||
* Signal a SINGLE thread to wake if it's waiting.
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
virtual int signal();
|
||||
|
||||
/**
|
||||
* Wake all threads waiting on this condition.
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
virtual int broadcast();
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Private copy constructor, to prevent tampering.
|
||||
*/
|
||||
Condition(const Condition &/*c*/) {};
|
||||
|
||||
/**
|
||||
* Private copy assignment, to prevent tampering.
|
||||
*/
|
||||
Condition &operator=(const Condition &/*c*/) {return *(this);};
|
||||
|
||||
/**
|
||||
* Implementation-specific data
|
||||
*/
|
||||
void *_prvData;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // !_OPENTHREADS_CONDITION_
|
||||
43
include/OpenThreads/Exports
Normal file
43
include/OpenThreads/Exports
Normal file
@@ -0,0 +1,43 @@
|
||||
/* -*-c++-*- OpenThreads library, Copyright (C) 2002 - 2007 The Open Thread Group
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library 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
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef _OPENTHREAD_EXPORTS_H_
|
||||
#define _OPENTHREAD_EXPORTS_H_
|
||||
|
||||
|
||||
#ifndef WIN32
|
||||
#define OPENTHREAD_EXPORT_DIRECTIVE
|
||||
#else
|
||||
#if defined( OT_LIBRARY_STATIC )
|
||||
#define OPENTHREAD_EXPORT_DIRECTIVE
|
||||
#elif defined( OPENTHREADS_EXPORTS )
|
||||
#define OPENTHREAD_EXPORT_DIRECTIVE __declspec(dllexport)
|
||||
#else
|
||||
#define OPENTHREAD_EXPORT_DIRECTIVE __declspec(dllimport)
|
||||
|
||||
#if 0 // Commented out for now
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#ifdef _DEBUG
|
||||
#pragma comment(lib ,"OpenThreadsWin32d")
|
||||
#else
|
||||
#pragma comment(lib, "OpenThreadsWin32")
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
89
include/OpenThreads/Mutex
Normal file
89
include/OpenThreads/Mutex
Normal file
@@ -0,0 +1,89 @@
|
||||
/* -*-c++-*- OpenThreads library, Copyright (C) 2002 - 2007 The Open Thread Group
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library 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
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
|
||||
//
|
||||
// Mutex - C++ mutex class
|
||||
// ~~~~~
|
||||
//
|
||||
|
||||
#ifndef _OPENTHREADS_MUTEX_
|
||||
#define _OPENTHREADS_MUTEX_
|
||||
|
||||
#include <OpenThreads/Exports>
|
||||
|
||||
namespace OpenThreads {
|
||||
|
||||
/**
|
||||
* @class Mutex
|
||||
* @brief This class provides an object-oriented thread mutex interface.
|
||||
*/
|
||||
class OPENTHREAD_EXPORT_DIRECTIVE Mutex {
|
||||
|
||||
friend class Condition;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
Mutex();
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
virtual ~Mutex();
|
||||
|
||||
/**
|
||||
* Lock the mutex
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
virtual int lock();
|
||||
|
||||
/**
|
||||
* Unlock the mutex
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
virtual int unlock();
|
||||
|
||||
/**
|
||||
* Test if mutex can be locked.
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
virtual int trylock();
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Private copy constructor, to prevent tampering.
|
||||
*/
|
||||
Mutex(const Mutex &/*m*/) {};
|
||||
|
||||
/**
|
||||
* Private copy assignment, to prevent tampering.
|
||||
*/
|
||||
Mutex &operator=(const Mutex &/*m*/) {return *(this);};
|
||||
|
||||
/**
|
||||
* Implementation-specific private data.
|
||||
*/
|
||||
void *_prvData;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // _OPENTHREADS_MUTEX_
|
||||
110
include/OpenThreads/ReadWriteMutex
Normal file
110
include/OpenThreads/ReadWriteMutex
Normal file
@@ -0,0 +1,110 @@
|
||||
/* -*-c++-*- OpenThreads - Copyright (C) 1998-2007 Robert Osfield
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library 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
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef _OPENTHREADS_READWRITEMUTEX_
|
||||
#define _OPENTHREADS_READWRITEMUTEX_
|
||||
|
||||
#include <OpenThreads/Thread>
|
||||
#include <OpenThreads/ReentrantMutex>
|
||||
|
||||
namespace OpenThreads {
|
||||
|
||||
class ReadWriteMutex
|
||||
{
|
||||
public:
|
||||
|
||||
ReadWriteMutex():
|
||||
_readCount(0) {}
|
||||
|
||||
virtual ~ReadWriteMutex() {}
|
||||
|
||||
virtual int readLock()
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_readCountMutex);
|
||||
int result = 0;
|
||||
if (_readCount==0)
|
||||
{
|
||||
result = _readWriteMutex.lock();
|
||||
}
|
||||
++_readCount;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
virtual int readUnlock()
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_readCountMutex);
|
||||
int result = 0;
|
||||
if (_readCount>0)
|
||||
{
|
||||
--_readCount;
|
||||
if (_readCount==0)
|
||||
{
|
||||
result = _readWriteMutex.unlock();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
virtual int writeLock()
|
||||
{
|
||||
return _readWriteMutex.lock();
|
||||
}
|
||||
|
||||
virtual int writeUnlock()
|
||||
{
|
||||
return _readWriteMutex.unlock();
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
ReadWriteMutex(const ReadWriteMutex&) {}
|
||||
ReadWriteMutex& operator = (const ReadWriteMutex&) { return *(this); }
|
||||
|
||||
#if 0
|
||||
ReentrantMutex _readWriteMutex;
|
||||
ReentrantMutex _readCountMutex;
|
||||
#else
|
||||
OpenThreads::Mutex _readWriteMutex;
|
||||
OpenThreads::Mutex _readCountMutex;
|
||||
#endif
|
||||
unsigned int _readCount;
|
||||
|
||||
};
|
||||
|
||||
class ScopedReadLock
|
||||
{
|
||||
public:
|
||||
|
||||
ScopedReadLock(ReadWriteMutex& mutex):_mutex(mutex) { _mutex.readLock(); }
|
||||
~ScopedReadLock() { _mutex.readUnlock(); }
|
||||
|
||||
protected:
|
||||
ReadWriteMutex& _mutex;
|
||||
};
|
||||
|
||||
|
||||
class ScopedWriteLock
|
||||
{
|
||||
public:
|
||||
|
||||
ScopedWriteLock(ReadWriteMutex& mutex):_mutex(mutex) { _mutex.writeLock(); }
|
||||
~ScopedWriteLock() { _mutex.writeUnlock(); }
|
||||
|
||||
protected:
|
||||
ReadWriteMutex& _mutex;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
125
include/OpenThreads/ReentrantMutex
Normal file
125
include/OpenThreads/ReentrantMutex
Normal file
@@ -0,0 +1,125 @@
|
||||
/* -*-c++-*- OpenThreads - Copyright (C) 1998-2007 Robert Osfield
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library 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
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef _OPENTHREADS_REENTRANTMUTEX_
|
||||
#define _OPENTHREADS_REENTRANTMUTEX_
|
||||
|
||||
#include <OpenThreads/Thread>
|
||||
#include <OpenThreads/Mutex>
|
||||
#include <OpenThreads/ScopedLock>
|
||||
|
||||
namespace OpenThreads {
|
||||
|
||||
class ReentrantMutex : public OpenThreads::Mutex
|
||||
{
|
||||
public:
|
||||
|
||||
ReentrantMutex():
|
||||
_threadHoldingMutex(0),
|
||||
_lockCount(0) {}
|
||||
|
||||
virtual ~ReentrantMutex() {}
|
||||
|
||||
virtual int lock()
|
||||
{
|
||||
if (_threadHoldingMutex==OpenThreads::Thread::CurrentThread() && _lockCount>0)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_lockCountMutex);
|
||||
++_lockCount;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
int result = Mutex::lock();
|
||||
if (result==0)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_lockCountMutex);
|
||||
|
||||
_threadHoldingMutex = OpenThreads::Thread::CurrentThread();
|
||||
_lockCount = 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
virtual int unlock()
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_lockCountMutex);
|
||||
#if 0
|
||||
if (_threadHoldingMutex==OpenThreads::Thread::CurrentThread() && _lockCount>0)
|
||||
{
|
||||
--_lockCount;
|
||||
if (_lockCount<=0)
|
||||
{
|
||||
_threadHoldingMutex = 0;
|
||||
return Mutex::unlock();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<"Error: ReentrantMutex::unlock() - unlocking from the wrong thread."<<std::endl;
|
||||
}
|
||||
#else
|
||||
if (_lockCount>0)
|
||||
{
|
||||
--_lockCount;
|
||||
if (_lockCount<=0)
|
||||
{
|
||||
_threadHoldingMutex = 0;
|
||||
return Mutex::unlock();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
virtual int trylock()
|
||||
{
|
||||
if (_threadHoldingMutex==OpenThreads::Thread::CurrentThread() && _lockCount>0)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_lockCountMutex);
|
||||
++_lockCount;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
int result = Mutex::trylock();
|
||||
if (result==0)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_lockCountMutex);
|
||||
|
||||
_threadHoldingMutex = OpenThreads::Thread::CurrentThread();
|
||||
_lockCount = 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
ReentrantMutex(const ReentrantMutex&):OpenThreads::Mutex() {}
|
||||
|
||||
ReentrantMutex& operator =(const ReentrantMutex&) { return *(this); }
|
||||
|
||||
OpenThreads::Thread* _threadHoldingMutex;
|
||||
|
||||
OpenThreads::Mutex _lockCountMutex;
|
||||
unsigned int _lockCount;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
47
include/OpenThreads/ScopedLock
Normal file
47
include/OpenThreads/ScopedLock
Normal file
@@ -0,0 +1,47 @@
|
||||
/* -*-c++-*- OpenThreads library, Copyright (C) 2002 - 2007 The Open Thread Group
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library 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
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
|
||||
//
|
||||
// ScopedLock and ReverseScopedLock templates
|
||||
// ~~~~~~~
|
||||
//
|
||||
#ifndef _ScopedLock_
|
||||
#define _ScopedLock_
|
||||
|
||||
namespace OpenThreads{
|
||||
|
||||
template <class M> class ScopedLock
|
||||
{
|
||||
private:
|
||||
M& m_lock;
|
||||
ScopedLock(const ScopedLock&); // prevent copy
|
||||
ScopedLock& operator=(const ScopedLock&); // prevent assign
|
||||
public:
|
||||
explicit ScopedLock(M& m):m_lock(m) {m_lock.lock();}
|
||||
~ScopedLock(){m_lock.unlock();}
|
||||
};
|
||||
|
||||
template <class M> class ReverseScopedLock
|
||||
{
|
||||
private:
|
||||
M& m_lock;
|
||||
ReverseScopedLock(const ReverseScopedLock&); // prevent copy
|
||||
ReverseScopedLock& operator=(const ReverseScopedLock&); // prevent assign
|
||||
public:
|
||||
explicit ReverseScopedLock(M& m):m_lock(m) {m_lock.unlock();}
|
||||
~ReverseScopedLock(){m_lock.lock();}
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
389
include/OpenThreads/Thread
Normal file
389
include/OpenThreads/Thread
Normal file
@@ -0,0 +1,389 @@
|
||||
/* -*-c++-*- OpenThreads library, Copyright (C) 2002 - 2007 The Open Thread Group
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library 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
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
|
||||
//
|
||||
// Thread - C++ Thread class
|
||||
// ~~~~~~~~
|
||||
//
|
||||
|
||||
#ifndef _OPENTHREADS_THREAD_
|
||||
#define _OPENTHREADS_THREAD_
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <OpenThreads/Mutex>
|
||||
|
||||
namespace OpenThreads {
|
||||
|
||||
/**
|
||||
* Get the number of processors.
|
||||
*
|
||||
* Note, systems where no support exists for querrying the number of processors, 1 is returned.
|
||||
*
|
||||
*/
|
||||
extern OPENTHREAD_EXPORT_DIRECTIVE int GetNumberOfProcessors();
|
||||
|
||||
/**
|
||||
* Set the processor affinity of current thread.
|
||||
*
|
||||
* Note, systems where no support exists no affinity will be set, and -1 will be returned.
|
||||
*
|
||||
*/
|
||||
extern OPENTHREAD_EXPORT_DIRECTIVE int SetProcessorAffinityOfCurrentThread(unsigned int cpunum);
|
||||
|
||||
/**
|
||||
* @class Thread
|
||||
* @brief This class provides an object-oriented thread interface.
|
||||
*/
|
||||
class OPENTHREAD_EXPORT_DIRECTIVE Thread {
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Set the concurrency level for a running application. This method
|
||||
* only has effect if the pthreads thread model is being used, and
|
||||
* then only when that model is many-to-one (eg. irix).
|
||||
* in other cases it is ignored. The concurrency level is only a
|
||||
* *hint* as to the number of execution vehicles to use, the actual
|
||||
* implementation may do anything it wants. Setting the value
|
||||
* to 0 returns things to their default state.
|
||||
*
|
||||
* @return previous concurrency level, -1 indicates no-op.
|
||||
*/
|
||||
static int SetConcurrency(int concurrencyLevel);
|
||||
|
||||
/**
|
||||
* Get the concurrency level for a running application. In this
|
||||
* case, a return code of 0 means that the application is in default
|
||||
* mode. A return code of -1 means that the application is incapable
|
||||
* of setting an arbitrary concurrency, because it is a one-to-one
|
||||
* execution model (sprocs, linuxThreads)
|
||||
*/
|
||||
static int GetConcurrency();
|
||||
|
||||
/**
|
||||
* Enumerated Type for thread priority
|
||||
*/
|
||||
enum ThreadPriority {
|
||||
|
||||
THREAD_PRIORITY_MAX, /**< The maximum possible priority */
|
||||
THREAD_PRIORITY_HIGH, /**< A high (but not max) setting */
|
||||
THREAD_PRIORITY_NOMINAL, /**< An average priority */
|
||||
THREAD_PRIORITY_LOW, /**< A low (but not min) setting */
|
||||
THREAD_PRIORITY_MIN, /**< The miniumum possible priority */
|
||||
THREAD_PRIORITY_DEFAULT /**< Priority scheduling default */
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Enumerated Type for thread scheduling policy
|
||||
*/
|
||||
enum ThreadPolicy {
|
||||
|
||||
THREAD_SCHEDULE_FIFO, /**< First in, First out scheduling */
|
||||
THREAD_SCHEDULE_ROUND_ROBIN, /**< Round-robin scheduling (LINUX_DEFAULT) */
|
||||
THREAD_SCHEDULE_TIME_SHARE, /**< Time-share scheduling (IRIX DEFAULT) */
|
||||
THREAD_SCHEDULE_DEFAULT /**< Default scheduling */
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
Thread();
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
virtual ~Thread();
|
||||
|
||||
|
||||
/**
|
||||
* Return a pointer to the current running thread
|
||||
*/
|
||||
static Thread *CurrentThread();
|
||||
|
||||
|
||||
/**
|
||||
* Initialize Threading in a program. This method must be called before
|
||||
* you can do any threading in a program.
|
||||
*/
|
||||
static void Init();
|
||||
|
||||
/**
|
||||
* Yield the processor.
|
||||
*
|
||||
* @note This method operates on the calling process. And is
|
||||
* equivalent to calling sched_yield().
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
static int YieldCurrentThread();
|
||||
|
||||
/**
|
||||
* This method will return the ThreadPriority of the master process.
|
||||
* (ie, the one calling the thread->start() methods for the first time)
|
||||
* The method will almost certainly return
|
||||
* Thread::THREAD_PRIORITY_DEFAULT if
|
||||
* Init() has not been called.
|
||||
*
|
||||
* @return the Thread::ThreadPriority of the master thread.
|
||||
*/
|
||||
static ThreadPriority GetMasterPriority() {return s_masterThreadPriority;};
|
||||
|
||||
|
||||
/**
|
||||
* Get a unique thread id. This id is monotonically increasing.
|
||||
*
|
||||
* @return a unique thread identifier
|
||||
*/
|
||||
int getThreadId();
|
||||
|
||||
/**
|
||||
* Get the thread's process id. This is the pthread_t or pid_t value
|
||||
* depending on the threading model being used.
|
||||
*
|
||||
* @return thread process id.
|
||||
*/
|
||||
size_t getProcessId();
|
||||
|
||||
/**
|
||||
* Start the thread. This method will configure the thread, set
|
||||
* it's priority, and spawn it.
|
||||
*
|
||||
* @note if the stack size specified setStackSize is smaller than the
|
||||
* smallest allowable stack size, the threads stack size will be set to
|
||||
* the minimum allowed, and may be retrieved via the getStackSize()
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
int start();
|
||||
int startThread();
|
||||
|
||||
/**
|
||||
* Test the cancel state of the thread. If the thread has been canceled
|
||||
* this method will cause the thread to exit now. This method operates
|
||||
* on the calling thread.
|
||||
*
|
||||
* Returns 0 if normal, -1 if called from a thread other that this.
|
||||
*/
|
||||
int testCancel();
|
||||
|
||||
|
||||
/**
|
||||
* Cancel the thread. Equivalent to SIGKILL.
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
virtual int cancel();
|
||||
|
||||
/**
|
||||
* Set the thread's schedule priority. This is a complex method.
|
||||
* Beware of thread priorities when using a many-to-many kernel
|
||||
* entity implemenation (such as IRIX pthreads). If one is not carefull
|
||||
* to manage the thread priorities, a priority inversion deadlock can
|
||||
* easily occur (Although the OpenThreads::Mutex & OpenThreads::Barrier
|
||||
* constructs have been designed with this senario in mind). Unless
|
||||
* you have explicit need to set the schedule pirorites for a given
|
||||
* task, it is best to leave them alone.
|
||||
*
|
||||
* @note some implementations (notably LinuxThreads and IRIX Sprocs)
|
||||
* only alow you to decrease thread priorities dynamically. Thus,
|
||||
* a lower priority thread will not allow it's priority to be raised
|
||||
* on the fly.
|
||||
*
|
||||
* @note seting the environment variable OUTPUT_THREADLIB_SCHEDULING_INFO
|
||||
* will output scheduling information for each thread to stdout.
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
int setSchedulePriority(ThreadPriority priority);
|
||||
|
||||
/**
|
||||
* Get the thread's schedule priority (if able)
|
||||
*
|
||||
* @note seting the environment variable OUTPUT_THREADLIB_SCHEDULING_INFO
|
||||
* will output scheduling information for each thread to stdout.
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
int getSchedulePriority();
|
||||
|
||||
/**
|
||||
* Set the thread's scheduling policy (if able)
|
||||
*
|
||||
* @note On some implementations (notably IRIX Sprocs & LinuxThreads)
|
||||
* The policy may prohibit the use of SCHEDULE_ROUND_ROBIN and
|
||||
* SCHEDULE_FIFO policies - due to their real-time nature, and
|
||||
* the danger of deadlocking the machine when used as super-user.
|
||||
* In such cases, the command is a no-op.
|
||||
*
|
||||
* @note seting the environment variable OUTPUT_THREADLIB_SCHEDULING_INFO
|
||||
* will output scheduling information for each thread to stdout.
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
int setSchedulePolicy(ThreadPolicy policy);
|
||||
|
||||
/**
|
||||
* Get the thread's policy (if able)
|
||||
*
|
||||
* @note seting the environment variable OUTPUT_THREADLIB_SCHEDULING_INFO
|
||||
* will output scheduling information for each thread to stdout.
|
||||
*
|
||||
* @return policy if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
int getSchedulePolicy();
|
||||
|
||||
/**
|
||||
* Set the thread's desired stack size (in bytes).
|
||||
* This method is an attribute of the thread and must be called
|
||||
* *before* the start() method is invoked.
|
||||
*
|
||||
* @note a return code of 13 (EACESS) means that the thread stack
|
||||
* size can no longer be changed.
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
int setStackSize(size_t size);
|
||||
|
||||
/**
|
||||
* Get the thread's desired stack size.
|
||||
*
|
||||
* @return the thread's stack size. 0 indicates that the stack size
|
||||
* has either not yet been initialized, or not yet been specified by
|
||||
* the application.
|
||||
*/
|
||||
size_t getStackSize();
|
||||
|
||||
/**
|
||||
* Print the thread's scheduling information to stdout.
|
||||
*/
|
||||
void printSchedulingInfo();
|
||||
|
||||
/**
|
||||
* Detach the thread from the calling process.
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
int detach();
|
||||
|
||||
/**
|
||||
* Join the calling process with the thread
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
int join();
|
||||
|
||||
/**
|
||||
* Disable thread cancelation altogether. Thread::cancel() has no effect.
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
int setCancelModeDisable();
|
||||
|
||||
/**
|
||||
* Mark the thread to cancel aysncronously on Thread::cancel().
|
||||
* (May not be available with process-level implementations).
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
int setCancelModeAsynchronous();
|
||||
|
||||
/**
|
||||
* Mark the thread to cancel at the earliest convenience on
|
||||
* Thread::cancel() (This is the default)
|
||||
*
|
||||
* @return 0 if normal, -1 if errno set, errno code otherwise.
|
||||
*/
|
||||
int setCancelModeDeferred();
|
||||
|
||||
/**
|
||||
* Query the thread's running status
|
||||
*
|
||||
* @return true if running, false if not.
|
||||
*/
|
||||
bool isRunning();
|
||||
|
||||
/**
|
||||
* Thread's run method. Must be implemented by derived classes.
|
||||
* This is where the action happens.
|
||||
*/
|
||||
virtual void run() = 0;
|
||||
|
||||
/**
|
||||
* Thread's cancel cleanup routine, called upon cancel(), after the
|
||||
* cancelation has taken place, but before the thread exits completely.
|
||||
* This method should be used to repair parts of the thread's data
|
||||
* that may have been damaged by a pre-mature cancel. No-op by default.
|
||||
*/
|
||||
virtual void cancelCleanup() {};
|
||||
|
||||
void* getImplementation(){ return _prvData; };
|
||||
|
||||
/** Thread's processor affinity method. This binds a thread to a
|
||||
* processor whenever possible. This call must be made before
|
||||
* start() or startThread() and has no effect after the thread
|
||||
* has been running. In the pthreads implementation, this is only
|
||||
* implemented on sgi, through a pthread extension. On other pthread
|
||||
* platforms this is ignored. Returns 0 on success, implementation's
|
||||
* error on failure, or -1 if ignored.
|
||||
*/
|
||||
int setProcessorAffinity( unsigned int cpunum );
|
||||
|
||||
/** microSleep method, equivilant to the posix usleep(microsec).
|
||||
* This is not strictly thread API but is used
|
||||
* so often with threads. It's basically UNIX usleep. Parameter is
|
||||
* number of microseconds we current thread to sleep. Returns 0 on
|
||||
* succes, non-zero on failure (UNIX errno or GetLastError() will give
|
||||
* detailed description.
|
||||
*/
|
||||
static int microSleep( unsigned int microsec);
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* The Private Actions class is allowed to operate on private data.
|
||||
*/
|
||||
friend class ThreadPrivateActions;
|
||||
|
||||
/**
|
||||
* Private copy constructor, to prevent tampering.
|
||||
*/
|
||||
Thread(const Thread &/*t*/) {};
|
||||
|
||||
/**
|
||||
* Private copy assignment, to prevent tampering.
|
||||
*/
|
||||
Thread &operator=(const Thread &/*t*/) {return *(this);};
|
||||
|
||||
/**
|
||||
* Implementation-specific data
|
||||
*/
|
||||
void * _prvData;
|
||||
|
||||
/**
|
||||
* Master thread's priority, set by Thread::Init.
|
||||
*/
|
||||
static ThreadPriority s_masterThreadPriority;
|
||||
|
||||
/**
|
||||
* Is initialized flag
|
||||
*/
|
||||
static bool s_isInitialized;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // !_OPENTHREADS_THREAD_
|
||||
38
include/OpenThreads/Version
Normal file
38
include/OpenThreads/Version
Normal file
@@ -0,0 +1,38 @@
|
||||
/* -*-c++-*- OpenThreads library, Copyright (C) 2002 - 2007 The Open Thread Group
|
||||
*
|
||||
* This library is open source and may be redistributed and/or modified under
|
||||
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
||||
* (at your option) any later version. The full license is in LICENSE file
|
||||
* included with this distribution, and on the openscenegraph.org website.
|
||||
*
|
||||
* This library 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
|
||||
* OpenSceneGraph Public License for more details.
|
||||
*/
|
||||
|
||||
#ifndef OPENTHREADS_VERSION
|
||||
#define OPENTHREADS_VERSION 1
|
||||
|
||||
#include <OpenThreads/Exports>
|
||||
|
||||
extern "C" {
|
||||
|
||||
#define OPENTHREADS_MAJOR_VERSION 2
|
||||
#define OPENTHREADS_MINOR_VERSION 2
|
||||
#define OPENTHREADS_PATCH_VERSION 0
|
||||
#define OPENTHREADS_SOVERSION 9
|
||||
|
||||
/** OpenThreadsGetVersion() returns the library version number.
|
||||
* Numbering convention : OpenThreads-1.0 will return 1.0 from OpenThreadsGetVersion. */
|
||||
extern OPENTHREAD_EXPORT_DIRECTIVE const char* OpenThreadsGetVersion();
|
||||
|
||||
/** The OpenThreadsGetSOVersion() method returns the OpenSceneGraph soversion number. */
|
||||
extern OPENTHREAD_EXPORT_DIRECTIVE const char* OpenThreadsGetSOVersion();
|
||||
|
||||
/** The OpenThreadsGetLibraryName() method returns the library name in human-friendly form. */
|
||||
extern OPENTHREAD_EXPORT_DIRECTIVE const char* OpenThreadsGetLibraryName();
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user