Hello,
My patches for SGThread are now complete. I use them on Linux with no
problems and some people use them on Windows since a few weeks with no
problems either.
Patch for simgear:
- Replace SGMutex with OpenThreads::Mutex.
- Replace SGGuard with OpenThreads::ScopedLock (which acts exactly like
SGGuard, see include/OpenThreads/ScopedLock).
- Use global variables for mutex where needed to be thread safe (as
pointed by Tim).
Patch for flightgear:
- Replace SGThread with OpenThreads::Thread.
- Replace SGGuard with OpenThreads::ScopedLock.
- Replace SGMutex with OpenThreads::Mutex.
- Replace SGPthreadCond by OpenThreads::Condition.
I did not touch configuration files, so to enable threads pthread still
needs to be detected (but will not be used). With these patches, only
SGQueue.hxx is used in simgear/threads (which is cross-platform since it
only uses OpenThreads).
Best regards,
Benoît
Index: src/Environment/environment_ctrl.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Environment/environment_ctrl.cxx,v
retrieving revision 1.50
diff -u -r1.50 environment_ctrl.cxx
--- src/Environment/environment_ctrl.cxx 11 Oct 2007 07:53:17 -0000 1.50
+++ src/Environment/environment_ctrl.cxx 11 May 2008 10:48:59 -0000
@@ -356,7 +356,8 @@
{
#if defined(ENABLE_THREADS)
thread = new MetarThread(this);
- thread->start( 1 );
+ thread->setProcessorAffinity(1);
+ thread->start();
#endif // ENABLE_THREADS
}
Index: src/Environment/environment_ctrl.hxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Environment/environment_ctrl.hxx,v
retrieving revision 1.27
diff -u -r1.27 environment_ctrl.hxx
--- src/Environment/environment_ctrl.hxx 15 Aug 2007 15:22:44 -0000 1.27
+++ src/Environment/environment_ctrl.hxx 11 May 2008 10:48:59 -0000
@@ -32,7 +32,7 @@
#include <simgear/environment/metar.hxx>
#if defined(ENABLE_THREADS)
-# include <simgear/threads/SGThread.hxx>
+# include <OpenThreads/Thread>
# include <simgear/threads/SGQueue.hxx>
#endif
@@ -233,7 +233,7 @@
* This class represents the thread of execution responsible for
* fetching the metar data.
*/
- class MetarThread : public SGThread
+ class MetarThread : public OpenThreads::Thread
{
public:
MetarThread( FGMetarEnvironmentCtrl* f ) : fetcher(f) {}
Index: src/Sound/voice.cxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Sound/voice.cxx,v
retrieving revision 1.8
diff -u -r1.8 voice.cxx
--- src/Sound/voice.cxx 23 Mar 2007 15:53:58 -0000 1.8
+++ src/Sound/voice.cxx 11 May 2008 10:48:59 -0000
@@ -71,7 +71,8 @@
}
#if defined(ENABLE_THREADS)
- _thread->start(1);
+ _thread->setProcessorAffinity(1);
+ _thread->start();
#endif
}
Index: src/Sound/voice.hxx
===================================================================
RCS file: /var/cvs/FlightGear-0.9/source/src/Sound/voice.hxx,v
retrieving revision 1.3
diff -u -r1.3 voice.hxx
--- src/Sound/voice.hxx 31 Mar 2006 10:12:00 -0000 1.3
+++ src/Sound/voice.hxx 11 May 2008 10:48:59 -0000
@@ -37,7 +37,10 @@
#include <Main/fg_props.hxx>
#if defined(ENABLE_THREADS)
-# include <simgear/threads/SGThread.hxx>
+# include <OpenThreads/Thread>
+# include <OpenThreads/Mutex>
+# include <OpenThreads/ScopedLock>
+# include <OpenThreads/Condition>
# include <simgear/threads/SGQueue.hxx>
#else
# include <queue>
@@ -74,16 +77,16 @@
#if defined(ENABLE_THREADS)
-class FGVoiceMgr::FGVoiceThread : public SGThread {
+class FGVoiceMgr::FGVoiceThread : public OpenThreads::Thread {
public:
FGVoiceThread(FGVoiceMgr *mgr) : _mgr(mgr) {}
void run();
void wake_up() { _jobs.signal(); }
private:
- void wait_for_jobs() { SGGuard<SGMutex> g(_mutex); _jobs.wait(_mutex); }
- SGPthreadCond _jobs;
- SGMutex _mutex;
+ void wait_for_jobs() { OpenThreads::ScopedLock<OpenThreads::Mutex> g(_mutex); _jobs.wait(&_mutex); }
+ OpenThreads::Condition _jobs;
+ OpenThreads::Mutex _mutex;
FGVoiceMgr *_mgr;
};
#endif
Index: simgear/scene/model/shadanim.cxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/scene/model/shadanim.cxx,v
retrieving revision 1.13
diff -u -r1.13 shadanim.cxx
--- simgear/scene/model/shadanim.cxx 4 Dec 2007 22:38:41 -0000 1.13
+++ simgear/scene/model/shadanim.cxx 11 May 2008 10:52:53 -0000
@@ -36,9 +36,10 @@
#include <osg/Texture1D>
#include <osgUtil/HighlightMapGenerator>
+#include <OpenThreads/Mutex>
+#include <OpenThreads/ScopedLock>
+
#include <simgear/scene/util/SGUpdateVisitor.hxx>
-#include <simgear/threads/SGThread.hxx>
-#include <simgear/threads/SGGuard.hxx>
#include <simgear/props/condition.hxx>
#include <simgear/props/props.hxx>
@@ -125,6 +126,8 @@
SGVec4f _lastLightColor;
};
+static OpenThreads::Mutex cubeMutex;
+
static osg::TextureCubeMap*
getOrCreateTextureCubeMap()
{
@@ -132,8 +135,7 @@
if (textureCubeMap.get())
return textureCubeMap.get();
- static SGMutex mutex;
- SGGuard<SGMutex> locker(mutex);
+ OpenThreads::ScopedLock<OpenThreads::Mutex> lock(cubeMutex);
if (textureCubeMap.get())
return textureCubeMap.get();
@@ -212,13 +214,14 @@
StateSetMap;
}
+static OpenThreads::Mutex chromeMutex;
+
// The chrome effect is mixed by the alpha channel of the texture
// on the model, which will be attached to a node lower in the scene
// graph: 0 -> completely chrome, 1 -> completely model texture.
static void create_chrome(osg::Group* group, osg::Texture2D* texture)
{
- static SGMutex mutex;
- SGGuard<SGMutex> locker(mutex);
+ OpenThreads::ScopedLock<OpenThreads::Mutex> lock(chromeMutex);
static StateSetMap chromeMap;
osg::StateSet *stateSet;
StateSetMap::iterator iterator = chromeMap.find(texture);
Index: simgear/scene/tgdb/obj.cxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/scene/tgdb/obj.cxx,v
retrieving revision 1.34
diff -u -r1.34 obj.cxx
--- simgear/scene/tgdb/obj.cxx 1 May 2008 12:21:52 -0000 1.34
+++ simgear/scene/tgdb/obj.cxx 11 May 2008 10:52:53 -0000
@@ -50,8 +50,6 @@
#include <simgear/scene/util/SGUpdateVisitor.hxx>
#include <simgear/scene/util/SGNodeMasks.hxx>
#include <simgear/scene/util/QuadTreeBuilder.hxx>
-#include <simgear/threads/SGThread.hxx>
-#include <simgear/threads/SGGuard.hxx>
#include "SGTexturedTriangleBin.hxx"
#include "SGLightBin.hxx"
Index: simgear/scene/tgdb/pt_lights.cxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/scene/tgdb/pt_lights.cxx,v
retrieving revision 1.14
diff -u -r1.14 pt_lights.cxx
--- simgear/scene/tgdb/pt_lights.cxx 21 Dec 2007 06:24:53 -0000 1.14
+++ simgear/scene/tgdb/pt_lights.cxx 11 May 2008 10:52:53 -0000
@@ -50,10 +50,11 @@
#include <osgUtil/CullVisitor>
+#include <OpenThreads/Mutex>
+#include <OpenThreads/ScopedLock>
+
#include <simgear/math/sg_random.h>
#include <simgear/debug/logstream.hxx>
-#include <simgear/threads/SGThread.hxx>
-#include <simgear/threads/SGGuard.hxx>
#include <simgear/scene/util/RenderConstants.hxx>
#include <simgear/scene/util/SGEnlargeBoundingBox.hxx>
@@ -123,6 +124,8 @@
return image;
}
+static OpenThreads::Mutex lightMutex;
+
static osg::Texture2D*
gen_standard_light_sprite(void)
{
@@ -131,8 +134,7 @@
if (texture.valid())
return texture.get();
- static SGMutex mutex;
- SGGuard<SGMutex> guard(mutex);
+ OpenThreads::ScopedLock<OpenThreads::Mutex> lock(lightMutex);
if (texture.valid())
return texture.get();
Index: simgear/scene/util/SGSceneFeatures.cxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/scene/util/SGSceneFeatures.cxx,v
retrieving revision 1.3
diff -u -r1.3 SGSceneFeatures.cxx
--- simgear/scene/util/SGSceneFeatures.cxx 8 Jun 2007 06:50:16 -0000 1.3
+++ simgear/scene/util/SGSceneFeatures.cxx 11 May 2008 10:52:53 -0000
@@ -31,9 +31,10 @@
#include <osg/PointSprite>
#include <osg/Texture>
+#include <OpenThreads/Mutex>
+#include <OpenThreads/ScopedLock>
+
#include <simgear/structure/SGSharedPtr.hxx>
-#include <simgear/threads/SGThread.hxx>
-#include <simgear/threads/SGGuard.hxx>
SGSceneFeatures::SGSceneFeatures() :
_textureCompression(UseARBCompression),
@@ -44,14 +45,15 @@
{
}
+OpenThreads::Mutex SGSceneFeatures::_instanceMutex;
+
SGSceneFeatures*
SGSceneFeatures::instance()
{
static SGSharedPtr<SGSceneFeatures> sceneFeatures;
if (sceneFeatures)
return sceneFeatures;
- static SGMutex mutex;
- SGGuard<SGMutex> guard(mutex);
+ OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_instanceMutex);
if (sceneFeatures)
return sceneFeatures;
sceneFeatures = new SGSceneFeatures;
Index: simgear/scene/util/SGSceneFeatures.hxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/scene/util/SGSceneFeatures.hxx,v
retrieving revision 1.3
diff -u -r1.3 SGSceneFeatures.hxx
--- simgear/scene/util/SGSceneFeatures.hxx 8 Jun 2007 06:50:16 -0000 1.3
+++ simgear/scene/util/SGSceneFeatures.hxx 11 May 2008 10:52:53 -0000
@@ -22,6 +22,8 @@
#ifndef SG_SCENE_FEATURES_HXX
#define SG_SCENE_FEATURES_HXX
+#include <OpenThreads/Mutex>
+
#include <simgear/structure/SGReferenced.hxx>
namespace osg { class Texture; }
@@ -94,6 +96,8 @@
bool _pointSpriteLights;
bool _distanceAttenuationLights;
int _textureFilter;
+
+ static OpenThreads::Mutex _instanceMutex;
};
#endif
Index: simgear/structure/SGAtomic.hxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/structure/SGAtomic.hxx,v
retrieving revision 1.3
diff -u -r1.3 SGAtomic.hxx
--- simgear/structure/SGAtomic.hxx 15 Oct 2007 18:49:51 -0000 1.3
+++ simgear/structure/SGAtomic.hxx 11 May 2008 10:52:53 -0000
@@ -33,8 +33,8 @@
# define SGATOMIC_USE_WIN32_INTERLOCKED
#else
// The sledge hammer ...
-# include <simgear/threads/SGThread.hxx>
-# include <simgear/threads/SGGuard.hxx>
+# include <OpenThreads/Mutex>
+# include <OpenThreads/ScopedLock>
#endif
class SGAtomic {
@@ -50,7 +50,7 @@
#elif defined(SGATOMIC_USE_WIN32_INTERLOCKED)
return InterlockedIncrement(reinterpret_cast<long volatile*>(&mValue));
#else
- SGGuard<SGMutex> lock(mMutex);
+ OpenThreads::ScopedLock<OpenThreads::Mutex> lock(mMutex);
return ++mValue;
#endif
}
@@ -63,7 +63,7 @@
#elif defined(SGATOMIC_USE_WIN32_INTERLOCKED)
return InterlockedDecrement(reinterpret_cast<long volatile*>(&mValue));
#else
- SGGuard<SGMutex> lock(mMutex);
+ OpenThreads::ScopedLock<OpenThreads::Mutex> lock(mMutex);
return --mValue;
#endif
}
@@ -78,7 +78,7 @@
#elif defined(SGATOMIC_USE_WIN32_INTERLOCKED)
return static_cast<unsigned const volatile &>(mValue);
#else
- SGGuard<SGMutex> lock(mMutex);
+ OpenThreads::ScopedLock<OpenThreads::Mutex> lock(mMutex);
return mValue;
#endif
}
@@ -90,7 +90,7 @@
#if !defined(SGATOMIC_USE_GCC4_BUILTINS) \
&& !defined(SGATOMIC_USE_MIPOSPRO_BUILTINS) \
&& !defined(SGATOMIC_USE_WIN32_INTERLOCKED)
- mutable SGMutex mMutex;
+ mutable OpenThreads::Mutex mMutex;
#endif
#ifdef SGATOMIC_USE_WIN32_INTERLOCKED
__declspec(align(32))
Index: simgear/structure/commands.cxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/structure/commands.cxx,v
retrieving revision 1.2
diff -u -r1.2 commands.cxx
--- simgear/structure/commands.cxx 4 Jan 2007 12:47:12 -0000 1.2
+++ simgear/structure/commands.cxx 11 May 2008 10:52:53 -0000
@@ -6,8 +6,9 @@
#include <memory>
#include <simgear/props/props_io.hxx>
-#include <simgear/threads/SGThread.hxx>
-#include <simgear/threads/SGGuard.hxx>
+
+#include <OpenThreads/Mutex>
+#include <OpenThreads/ScopedLock>
#include "commands.hxx"
@@ -28,6 +29,8 @@
// no-op
}
+OpenThreads::Mutex SGCommandMgr::_instanceMutex;
+
SGCommandMgr*
SGCommandMgr::instance()
{
@@ -35,8 +38,7 @@
if (mgr.get())
return mgr.get();
- static SGMutex lock;
- SGGuard<SGMutex> guard(lock);
+ OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_instanceMutex);
if (mgr.get())
return mgr.get();
Index: simgear/structure/commands.hxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/structure/commands.hxx,v
retrieving revision 1.2
diff -u -r1.2 commands.hxx
--- simgear/structure/commands.hxx 4 Jan 2007 12:47:12 -0000 1.2
+++ simgear/structure/commands.hxx 11 May 2008 10:52:53 -0000
@@ -17,6 +17,8 @@
#include <map>
#include <vector>
+#include <OpenThreads/Mutex>
+
#include <simgear/props/props.hxx>
SG_USING_STD(string);
@@ -109,6 +111,8 @@
typedef map<string,command_t> command_map;
command_map _commands;
+ static OpenThreads::Mutex _instanceMutex;
+
};
#endif // __COMMANDS_HXX
Index: simgear/threads/SGQueue.hxx
===================================================================
RCS file: /var/cvs/SimGear-0.3/source/simgear/threads/SGQueue.hxx,v
retrieving revision 1.8
diff -u -r1.8 SGQueue.hxx
--- simgear/threads/SGQueue.hxx 21 Feb 2006 12:59:31 -0000 1.8
+++ simgear/threads/SGQueue.hxx 11 May 2008 10:52:54 -0000
@@ -10,8 +10,9 @@
#endif
#include <queue>
-#include "SGThread.hxx"
-#include "SGGuard.hxx"
+#include <OpenThreads/Mutex>
+#include <OpenThreads/ScopedLock>
+#include <OpenThreads/Condition>
/**
* SGQueue defines an interface for a FIFO.
@@ -78,7 +79,7 @@
/**
* A simple thread safe queue. All access functions are guarded with a mutex.
*/
-template<class T, class SGLOCK=SGMutex>
+template<class T, class SGLOCK=OpenThreads::Mutex>
class SGLockedQueue : public SGQueue<T>
{
public:
@@ -99,7 +100,7 @@
* @return bool True if queue is empty, otherwisr false.
*/
virtual bool empty() {
- SGGuard<SGLOCK> g(mutex);
+ OpenThreads::ScopedLock<SGLOCK> g(mutex);
return this->fifo.empty();
}
@@ -109,7 +110,7 @@
* @param T object to add.
*/
virtual void push( const T& item ) {
- SGGuard<SGLOCK> g(mutex);
+ OpenThreads::ScopedLock<SGLOCK> g(mutex);
this->fifo.push( item );
}
@@ -119,7 +120,7 @@
* @return T next available object.
*/
virtual T front() {
- SGGuard<SGLOCK> g(mutex);
+ OpenThreads::ScopedLock<SGLOCK> g(mutex);
assert( ! this->fifo.empty() );
T item = this->fifo.front();
return item;
@@ -131,7 +132,7 @@
* @return T next available object.
*/
virtual T pop() {
- SGGuard<SGLOCK> g(mutex);
+ OpenThreads::ScopedLock<SGLOCK> g(mutex);
//if (fifo.empty()) throw NoSuchElementException();
assert( ! this->fifo.empty() );
// if (fifo.empty())
@@ -150,7 +151,7 @@
* @return size_t size of queue.
*/
virtual size_t size() {
- SGGuard<SGLOCK> g(mutex);
+ OpenThreads::ScopedLock<SGLOCK> g(mutex);
return this->fifo.size();
}
@@ -189,7 +190,7 @@
*
*/
virtual bool empty() {
- SGGuard<SGMutex> g(mutex);
+ OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
return this->fifo.empty();
}
@@ -199,7 +200,7 @@
* @param T object to add.
*/
virtual void push( const T& item ) {
- SGGuard<SGMutex> g(mutex);
+ OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
this->fifo.push( item );
not_empty.signal();
}
@@ -211,7 +212,7 @@
* @return T next available object.
*/
virtual T front() {
- SGGuard<SGMutex> g(mutex);
+ OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
assert(this->fifo.empty() != true);
//if (fifo.empty()) throw ??
@@ -227,10 +228,10 @@
* @return T next available object.
*/
virtual T pop() {
- SGGuard<SGMutex> g(mutex);
+ OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
while (this->fifo.empty())
- not_empty.wait(mutex);
+ not_empty.wait(&mutex);
assert(this->fifo.empty() != true);
//if (fifo.empty()) throw ??
@@ -246,7 +247,7 @@
* @return size_t size of queue.
*/
virtual size_t size() {
- SGGuard<SGMutex> g(mutex);
+ OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
return this->fifo.size();
}
@@ -255,12 +256,12 @@
/**
* Mutex to serialise access.
*/
- SGMutex mutex;
+ OpenThreads::Mutex mutex;
/**
* Condition to signal when queue not empty.
*/
- SGPthreadCond not_empty;
+ OpenThreads::Condition not_empty;
private:
// Prevent copying.
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Flightgear-devel mailing list
Flightgear-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/flightgear-devel