From 4f87afdbf5d3fe927de6f18258e9d4a5f0b77c2b Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 3 Jan 2007 16:06:12 +0000 Subject: [PATCH] Improved the robustness of thread start and cancellation --- examples/osgcamera/osgcamera.cpp | 4 +-- include/osgViewer/GraphicsWindowX11 | 2 ++ src/osg/GraphicsContext.cpp | 10 ------ src/osg/GraphicsThread.cpp | 12 ++++--- src/osgDB/DatabasePager.cpp | 2 +- src/osgViewer/GraphicsWindowX11.cpp | 51 +++++++++++++++++++++++++++-- src/osgViewer/Viewer.cpp | 31 ++++++++++++++---- 7 files changed, 87 insertions(+), 25 deletions(-) diff --git a/examples/osgcamera/osgcamera.cpp b/examples/osgcamera/osgcamera.cpp index bbfc13a54..208f95b38 100644 --- a/examples/osgcamera/osgcamera.cpp +++ b/examples/osgcamera/osgcamera.cpp @@ -172,7 +172,7 @@ int main( int argc, char **argv ) else viewer.setCameraManipulator( new osgGA::TrackballManipulator() ); viewer.getCamera()->setClearColor(osg::Vec4f(0.6f,0.6f,0.8f,1.0f)); -#if 1 +#if 0 // singleWindowMultipleCameras(viewer); @@ -187,7 +187,7 @@ int main( int argc, char **argv ) viewer.realize(); - bool limitNumberOfFrames = false; + bool limitNumberOfFrames = true; unsigned int numFrames = 0; unsigned int maxFrames = 10; diff --git a/include/osgViewer/GraphicsWindowX11 b/include/osgViewer/GraphicsWindowX11 index 64ed652cc..3eb3ee703 100644 --- a/include/osgViewer/GraphicsWindowX11 +++ b/include/osgViewer/GraphicsWindowX11 @@ -106,6 +106,8 @@ class GraphicsWindowX11 : public osgViewer::GraphicsWindow protected: + ~GraphicsWindowX11(); + bool createVisualInfo(); void init(); diff --git a/src/osg/GraphicsContext.cpp b/src/osg/GraphicsContext.cpp index be8638ad5..35abb480e 100644 --- a/src/osg/GraphicsContext.cpp +++ b/src/osg/GraphicsContext.cpp @@ -141,10 +141,6 @@ bool GraphicsContext::realize() { if (realizeImplementation()) { - if (_graphicsThread.valid() && !_graphicsThread->isRunning()) - { - _graphicsThread->startThread(); - } return true; } else @@ -255,12 +251,6 @@ void GraphicsContext::setGraphicsThread(GraphicsThread* gt) if (_graphicsThread.valid()) { _graphicsThread->_graphicsContext = this; -#if 0 - if (!_graphicsThread->isRunning()) - { - _graphicsThread->startThread(); - } -#endif } } diff --git a/src/osg/GraphicsThread.cpp b/src/osg/GraphicsThread.cpp index e50af3f01..c65972a58 100644 --- a/src/osg/GraphicsThread.cpp +++ b/src/osg/GraphicsThread.cpp @@ -86,9 +86,11 @@ GraphicsThread::GraphicsThread(): GraphicsThread::~GraphicsThread() { - osg::notify(osg::INFO)<<"Destructing graphics thread"<mouseWarped(x,y); } +int X11ErrorHandling(Display* display, XErrorEvent* event) +{ + osg::notify(osg::NOTICE)<<"Got an X11ErrorHandling call"<getDatabasePager()->cancel(); _scene->setDatabasePager(0); } -#endif + + //osg::notify(osg::NOTICE)<<"finish Viewer::~Viewer()"< 1 && _threadingModel>=ThreadPerContext; + bool affinity = true; if (multiThreaded) { bool firstContextAsMainThread = _threadingModel==ThreadPerContext; unsigned int numThreadsIncludingMainThread = firstContextAsMainThread ? contexts.size() : contexts.size()+1; - osg::notify(osg::NOTICE)<<"numThreadsIncludingMainThread=="< swapOp = new osg::SwapBuffersOperation(); citr = contexts.begin(); - if (firstContextAsMainThread) ++citr; + unsigned int processNum = 0; + if (firstContextAsMainThread) + { + if (affinity) OpenThreads::SetProcessorAffinityOfCurrentThread(processNum % numProcessors); + ++processNum; + ++citr; + } + for(; citr != contexts.end(); - ++citr) + ++citr, + ++processNum) { osg::GraphicsContext* gc = (*citr); // create the a graphics thread for this context gc->createGraphicsThread(); + if (affinity) gc->getGraphicsThread()->setProcessorAffinity(processNum % numProcessors); + gc->getGraphicsThread()->add(new CompileOperation(getSceneData())); // add the startRenderingBarrier @@ -264,6 +278,8 @@ void Viewer::realize() if (multiThreaded) { + //osg::notify(osg::NOTICE)<<"Ready to start threads"<getGraphicsThread() && !gc->getGraphicsThread()->isRunning()) { + //osg::notify(osg::NOTICE)<<" gc->getGraphicsThread()->startThread() "<getGraphicsThread()<getGraphicsThread()->startThread(); OpenThreads::Thread::YieldCurrentThread(); } } + + //osg::notify(osg::NOTICE)<<"Started threads"<