Revision: 41957
          http://brlcad.svn.sourceforge.net/brlcad/?rev=41957&view=rev
Author:   davidloman
Date:     2011-01-05 20:49:05 +0000 (Wed, 05 Jan 2011)

Log Message:
-----------
Lots of clean up an optimization in ControlledThread.  Consolidated shutdown 
functionality.  Made shutdown work on thread->wait(timeout) instead of rolling 
our own solution.

Modified Paths:
--------------
    rt^3/trunk/include/ControlledThread.h
    rt^3/trunk/src/utility/ControlledThread.cxx

Modified: rt^3/trunk/include/ControlledThread.h
===================================================================
--- rt^3/trunk/include/ControlledThread.h       2011-01-05 19:24:58 UTC (rev 
41956)
+++ rt^3/trunk/include/ControlledThread.h       2011-01-05 20:49:05 UTC (rev 
41957)
@@ -38,9 +38,7 @@
        void start();
        void run();
 
-       void shutdown();
-       void terminate();
-       void terminate(bool block);
+       void shutdown(bool block = true);
        QString getThreadName();
 
        bool getRunStatus();
@@ -67,11 +65,13 @@
        QString threadName;
 
        QMutex runCmdLock;
-       bool runCmd;
+       volatile bool runCmd;
 
        QMutex runStatusLock;
-       bool runStatus;
+       volatile bool runStatus;
 
+       QMutex threadExitLock;
+
        /* Disable copy cstr and =operator */
        ControlledThread(ControlledThread const&){};
        ControlledThread& operator=(ControlledThread const&){};

Modified: rt^3/trunk/src/utility/ControlledThread.cxx
===================================================================
--- rt^3/trunk/src/utility/ControlledThread.cxx 2011-01-05 19:24:58 UTC (rev 
41956)
+++ rt^3/trunk/src/utility/ControlledThread.cxx 2011-01-05 20:49:05 UTC (rev 
41957)
@@ -31,56 +31,39 @@
        } else {
                this->threadName = threadName;
        }
-       this->setRunCmd(false);
-       this->setRunStatus(false);
+       this->runCmd = false;
+       this->runStatus = false;
 }
 
 ControlledThread::~ControlledThread() {}
 
 void ControlledThread::start() {
        bool preRetVal = this->preStartupHook();
-       this->setRunCmd(true);
        GSThread::start(); /* call super class start */
        bool postRetVal = this->postStartupHook();
+       GSThread::msleep(100);
 }
 
 void
-ControlledThread::shutdown() {
-       this->terminate();
-}
+ControlledThread::shutdown(bool block) {
 
-void
-ControlledThread::terminate() {
-       this->terminate(true);
-}
-
-void ControlledThread::terminate(bool block) {
-
        bool preRetVal = this->preShutdownHook();
        this->setRunCmd(false);
 
-       quint64 maxFailsafeTimeMS = 5 * 1000;
+       int maxFailsafeTimeMS = 5 * 1000;
 
-       if (block) {
-
-               quint64 startT = Logger::getCurrentTime();
-               quint64 stopT = 0;
-               while (this->getRunStatus()) {
-                       GSThread::msleep(100); //TODO need a failsafe here.
-                       stopT = Logger::getCurrentTime();
-
-                       if ((stopT - startT) >= maxFailsafeTimeMS) {
-                               
Logger::getInstance()->logWARNING("ControlledThread", "terminate(block=true) 
reached failsafe.  Forcefully stopping thread.");
-                               GSThread::terminate();
-                               break;
-                       }
-               }
+       /* Optionally block here until failsafe */
+       if (this->wait(maxFailsafeTimeMS) == false) {
+               std::cout << "Tripped " << this->threadName.toStdString() << 
"::terminate's failsafe." << std::endl;
        }
-
        bool postRetVal = this->postShutdownHook();
+       GSThread::msleep(10);
 }
 
-void ControlledThread::run() {
+void
+ControlledThread::run() {
+       QMutexLocker(&this->threadExitLock);
+
        if(!this->preRunHook())
                return;
 
@@ -157,12 +140,14 @@
 void
 ControlledThread::setRunCmd(bool newVal) {
        QMutexLocker(&this->runCmdLock);
+/*     std::cout << threadName.toStdString() << " Old RunCmd:" << this->runCmd 
<< "\t New RunCmd:" << newVal << "\n"; */
        this->runCmd = newVal;
 }
 
 void
 ControlledThread::setRunStatus(bool newVal) {
        QMutexLocker(&this->runStatusLock);
+/*     std::cout << threadName.toStdString() << " Old runStatus:" << 
this->runStatus << "\t New runStatus:" << newVal << "\n"; */
        this->runStatus = newVal;
 }
 


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and, 
should the need arise, upgrade to a full multi-node Oracle RAC database 
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to