Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 18, 2009, 6:23:31 PM (15 years ago)
Author:
rgrieder
Message:

Merged netp6 branch back to the trunk.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/core/TclThreadManager.cc

    r3301 r3304  
    3030
    3131#include <boost/bind.hpp>
     32#include <boost/thread/condition.hpp>
     33#include <boost/thread/mutex.hpp>
     34#include <boost/thread/thread.hpp>
    3235#include <OgreTimer.h>
    3336#include <cpptcl/cpptcl.h>
     
    5659    SetConsoleCommand(TclThreadManager, flush,   false).argumentCompleter(0, autocompletion::tclthreads());
    5760
     61    struct TclInterpreterBundle
     62    {
     63        unsigned int id_;
     64
     65        std::list<std::string> queue_;
     66        boost::mutex queueMutex_;
     67
     68        Tcl::interpreter* interpreter_;
     69        std::string interpreterName_;
     70        boost::try_mutex interpreterMutex_;
     71
     72        std::list<unsigned int> queriers_;
     73        boost::mutex queriersMutex_;
     74
     75        bool running_;
     76        boost::mutex runningMutex_;
     77
     78        bool finished_;
     79        boost::mutex finishedMutex_;
     80        boost::condition finishedCondition_;
     81    };
     82
     83
     84    static boost::thread::id threadID_g;
     85    static boost::mutex bundlesMutex_g;
     86    static boost::condition fullQueueCondition_g;
     87    static boost::condition orxonoxEvalCondition_g;
     88
    5889    TclThreadManager* TclThreadManager::singletonRef_s = 0;
    5990
    6091    TclThreadManager::TclThreadManager(Tcl::interpreter* interpreter)
     92        : orxonoxInterpreterBundle_(new TclInterpreterBundle())
    6193    {
    6294        RegisterRootObject(TclThreadManager);
     
    6698
    6799        this->threadCounter_ = 0;
    68         this->orxonoxInterpreterBundle_.id_ = 0;
    69         this->orxonoxInterpreterBundle_.interpreter_ = interpreter;
    70 #if (BOOST_VERSION >= 103500)
    71         this->threadID_ = boost::this_thread::get_id();
    72 #else
    73         //
    74 #endif
     100        this->orxonoxInterpreterBundle_->id_ = 0;
     101        this->orxonoxInterpreterBundle_->interpreter_ = interpreter;
     102        threadID_g = boost::this_thread::get_id();
    75103    }
    76104
     
    79107        unsigned int threadID;
    80108        {
    81             boost::mutex::scoped_lock bundles_lock(this->bundlesMutex_);
     109            boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
    82110            if (this->interpreterBundles_.begin() == this->interpreterBundles_.end())
    83111                return;
     
    87115        this->destroy(threadID);
    88116
     117        delete this->orxonoxInterpreterBundle_;
     118
    89119        singletonRef_s = 0;
    90120    }
     
    92122    unsigned int TclThreadManager::create()
    93123    {
    94         boost::mutex::scoped_lock bundles_lock(TclThreadManager::getInstance().bundlesMutex_);
     124        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
    95125        TclThreadManager::getInstance().threadCounter_++;
    96126        std::string name = multi_cast<std::string>(TclThreadManager::getInstance().threadCounter_);
     
    132162                    if (bundle->finished_)
    133163                    {
    134                         boost::mutex::scoped_lock bundles_lock(TclThreadManager::getInstance().bundlesMutex_);
    135 #if (BOOST_VERSION >= 103500)
     164                        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
    136165                        boost::mutex::scoped_try_lock interpreter_lock(bundle->interpreterMutex_);
    137 #else
    138                         boost::try_mutex::scoped_try_lock interpreter_lock(bundle->interpreterMutex_);
    139 #endif
    140166                        try
    141167                        {
    142168                            while (!interpreter_lock.try_lock())
    143169                            {
    144                                 TclThreadManager::getInstance().orxonoxEvalCondition_.notify_one();
    145 #if (BOOST_VERSION >= 103500)
     170                                orxonoxEvalCondition_g.notify_one();
    146171                                boost::this_thread::yield();
    147 #else
    148                                 boost::thread::yield();
    149 #endif
    150172                            }
    151173                        } catch (...) {}
     
    157179                }
    158180
    159                 TclThreadManager::getInstance().orxonoxEvalCondition_.notify_one();
    160 #if (BOOST_VERSION >= 103500)
     181                orxonoxEvalCondition_g.notify_one();
    161182                boost::this_thread::yield();
    162 #else
    163                 boost::thread::yield();
    164 #endif
    165183            }
    166184
     
    181199    std::string TclThreadManager::query(unsigned int threadID, const std::string& command)
    182200    {
    183         return TclThreadManager::getInstance().evalQuery(TclThreadManager::getInstance().orxonoxInterpreterBundle_.id_, threadID, command);
     201        return TclThreadManager::getInstance().evalQuery(TclThreadManager::getInstance().orxonoxInterpreterBundle_->id_, threadID, command);
    184202    }
    185203
     
    191209        output += "\t\t";
    192210        {
    193             boost::mutex::scoped_lock queue_lock(TclThreadManager::getInstance().orxonoxInterpreterBundle_.queueMutex_);
    194             output += multi_cast<std::string>(TclThreadManager::getInstance().orxonoxInterpreterBundle_.queue_.size());
     211            boost::mutex::scoped_lock queue_lock(TclThreadManager::getInstance().orxonoxInterpreterBundle_->queueMutex_);
     212            output += multi_cast<std::string>(TclThreadManager::getInstance().orxonoxInterpreterBundle_->queue_.size());
    195213        }
    196214        output += "\t\t";
     
    198216        COUT(0) << output << std::endl;
    199217
    200         boost::mutex::scoped_lock bundles_lock(TclThreadManager::getInstance().bundlesMutex_);
     218        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
    201219        for (std::map<unsigned int, TclInterpreterBundle*>::const_iterator it = TclThreadManager::getInstance().interpreterBundles_.begin(); it != TclThreadManager::getInstance().interpreterBundles_.end(); ++it)
    202220        {
     
    209227            output += "\t\t";
    210228            {
    211 #if (BOOST_VERSION >= 103500)
    212229                boost::mutex::scoped_try_lock interpreter_lock((*it).second->interpreterMutex_);
    213 #else
    214                 boost::try_mutex::scoped_try_lock interpreter_lock((*it).second->interpreterMutex_);
    215 #endif
    216230                if (interpreter_lock.try_lock())
    217231                    output += "ready";
     
    228242        if (threadID == 0)
    229243        {
    230             bundle = &TclThreadManager::getInstance().orxonoxInterpreterBundle_;
     244            bundle = TclThreadManager::getInstance().orxonoxInterpreterBundle_;
    231245            COUT(0) << "Queue dump of Orxonox:" << std::endl;
    232246        }
     
    254268        TclInterpreterBundle* bundle = 0;
    255269        if (threadID == 0)
    256             bundle = &TclThreadManager::getInstance().orxonoxInterpreterBundle_;
     270            bundle = TclThreadManager::getInstance().orxonoxInterpreterBundle_;
    257271        else
    258272            if (!(bundle = TclThreadManager::getInstance().getInterpreterBundle(threadID)))
     
    334348    TclInterpreterBundle* TclThreadManager::getInterpreterBundle(unsigned int threadID)
    335349    {
    336         boost::mutex::scoped_lock bundles_lock(this->bundlesMutex_);
     350        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
    337351        std::map<unsigned int, TclInterpreterBundle*>::iterator it = this->interpreterBundles_.find(threadID);
    338352        if (it != this->interpreterBundles_.end())
     
    347361    }
    348362
     363    Tcl::interpreter* TclThreadManager::getTclInterpreter(unsigned int threadID)
     364    {
     365        return this->getInterpreterBundle(threadID)->interpreter_;
     366    }
     367
    349368    std::string TclThreadManager::dumpList(const std::list<unsigned int>& list)
    350369    {
     
    362381    void TclThreadManager::error(const std::string& error)
    363382    {
    364 #if (BOOST_VERSION >= 103500)
    365         if (boost::this_thread::get_id() != this->threadID_)
    366 #else
    367         if (boost::thread() != this->threadID_)
    368 #endif
    369         {
    370             boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
    371             if (this->orxonoxInterpreterBundle_.queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
    372             {
    373 #if (BOOST_VERSION >= 103500)
     383        if (boost::this_thread::get_id() != threadID_g)
     384        {
     385            boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
     386            if (this->orxonoxInterpreterBundle_->queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
     387            {
    374388                boost::this_thread::yield();
    375 #else
    376                 boost::thread::yield();
    377 #endif
    378389                return;
    379390            }
     
    385396    void TclThreadManager::debug(const std::string& error)
    386397    {
    387 #if (BOOST_VERSION >= 103500)
    388         if (boost::this_thread::get_id() != this->threadID_)
    389 #else
    390         if (boost::thread() != this->threadID_)
    391 #endif
    392         {
    393             boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
    394             if (this->orxonoxInterpreterBundle_.queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
    395             {
    396 #if (BOOST_VERSION >= 103500)
     398        if (boost::this_thread::get_id() != threadID_g)
     399        {
     400            boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
     401            if (this->orxonoxInterpreterBundle_->queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
     402            {
    397403                boost::this_thread::yield();
    398 #else
    399                 boost::thread::yield();
    400 #endif
    401404                return;
    402405            }
     
    408411    void TclThreadManager::pushCommandToQueue(const std::string& command)
    409412    {
    410         boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
    411         while (this->orxonoxInterpreterBundle_.queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
    412             this->fullQueueCondition_.wait(queue_lock);
    413 
    414         this->orxonoxInterpreterBundle_.queue_.push_back(command);
     413        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
     414        while (this->orxonoxInterpreterBundle_->queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
     415            fullQueueCondition_g.wait(queue_lock);
     416
     417        this->orxonoxInterpreterBundle_->queue_.push_back(command);
    415418    }
    416419
    417420    void TclThreadManager::forceCommandToFrontOfQueue(const std::string& command)
    418421    {
    419         boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
    420         this->orxonoxInterpreterBundle_.queue_.push_front(command);
     422        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
     423        this->orxonoxInterpreterBundle_->queue_.push_front(command);
    421424    }
    422425
    423426    std::string TclThreadManager::popCommandFromQueue()
    424427    {
    425         boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
    426         std::string temp = this->orxonoxInterpreterBundle_.queue_.front();
    427         this->orxonoxInterpreterBundle_.queue_.pop_front();
    428         this->fullQueueCondition_.notify_one();
     428        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
     429        std::string temp = this->orxonoxInterpreterBundle_->queue_.front();
     430        this->orxonoxInterpreterBundle_->queue_.pop_front();
     431        fullQueueCondition_g.notify_one();
    429432        return temp;
    430433    }
     
    432435    bool TclThreadManager::queueIsEmpty()
    433436    {
    434         boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
    435         return this->orxonoxInterpreterBundle_.queue_.empty();
     437        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
     438        return this->orxonoxInterpreterBundle_->queue_.empty();
    436439    }
    437440
     
    505508        if (querier)
    506509        {
    507             if (this->updateQueriersList(querier, &this->orxonoxInterpreterBundle_))
    508             {
    509 #if (BOOST_VERSION >= 103500)
    510                 boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_.interpreterMutex_);
    511 #else
    512                 boost::try_mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_.interpreterMutex_);
    513 #endif
    514                 this->orxonoxEvalCondition_.wait(interpreter_lock);
     510            if (this->updateQueriersList(querier, this->orxonoxInterpreterBundle_))
     511            {
     512                boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_->interpreterMutex_);
     513                orxonoxEvalCondition_g.wait(interpreter_lock);
    515514
    516515                if (!CommandExecutor::execute(command, false))
     
    521520            }
    522521
    523             boost::mutex::scoped_lock queriers_lock(this->orxonoxInterpreterBundle_.queriersMutex_);
    524             this->orxonoxInterpreterBundle_.queriers_.clear();
     522            boost::mutex::scoped_lock queriers_lock(this->orxonoxInterpreterBundle_->queriersMutex_);
     523            this->orxonoxInterpreterBundle_->queriers_.clear();
    525524        }
    526525        return output;
     
    533532            target = this->getInterpreterBundle(threadID);
    534533        else
    535             target = &this->orxonoxInterpreterBundle_;
     534            target = this->orxonoxInterpreterBundle_;
    536535
    537536        std::string output = "";
     
    542541                querier = this->getInterpreterBundle(querierID);
    543542            else
    544                 querier = &this->orxonoxInterpreterBundle_;
     543                querier = this->orxonoxInterpreterBundle_;
    545544
    546545            if (querier)
     
    548547                if (this->updateQueriersList(querier, target))
    549548                {
    550 #if (BOOST_VERSION >= 103500)
    551549                    boost::mutex::scoped_try_lock interpreter_lock(target->interpreterMutex_);
    552 #else
    553                     boost::try_mutex::scoped_try_lock interpreter_lock(target->interpreterMutex_);
    554 #endif
    555550                    bool successfullyLocked = false;
    556551                    try
     
    562557                            while (!interpreter_lock.try_lock())
    563558                            {
    564 #if (BOOST_VERSION >= 103500)
    565559                                boost::this_thread::yield();
    566 #else
    567                                 boost::thread::yield();
    568 #endif
    569560                            }
    570561
     
    599590    {
    600591        {
    601             this->orxonoxEvalCondition_.notify_one();
    602 #if (BOOST_VERSION >= 103500)
     592            orxonoxEvalCondition_g.notify_one();
    603593            boost::this_thread::yield();
    604 #else
    605             boost::thread::yield();
    606 #endif
    607         }
    608 
    609         {
    610             boost::mutex::scoped_lock bundles_lock(this->bundlesMutex_);
     594        }
     595
     596        {
     597            boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
    611598            for (std::map<unsigned int, TclInterpreterBundle*>::iterator it = this->interpreterBundles_.begin(); it != this->interpreterBundles_.end(); ++it)
    612599            {
     
    626613
    627614        {
    628 #if (BOOST_VERSION >= 103500)
    629             boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_.interpreterMutex_);
    630 #else
    631             boost::try_mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_.interpreterMutex_);
    632 #endif
     615            boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_->interpreterMutex_);
    633616            unsigned long maxtime = static_cast<unsigned long>(time.getDeltaTime() * 1000000 * TCLTHREADMANAGER_MAX_CPU_USAGE);
    634617            Ogre::Timer timer;
     
    644627    std::list<unsigned int> TclThreadManager::getThreadList() const
    645628    {
    646         boost::mutex::scoped_lock bundles_lock(TclThreadManager::getInstance().bundlesMutex_);
     629        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
    647630        std::list<unsigned int> threads;
    648631        for (std::map<unsigned int, TclInterpreterBundle*>::const_iterator it = this->interpreterBundles_.begin(); it != this->interpreterBundles_.end(); ++it)
     
    654637    {
    655638        TclThreadManager::getInstance().debug("TclThread_execute: " + command);
    656 #if (BOOST_VERSION >= 103500)
    657639        boost::mutex::scoped_lock interpreter_lock(interpreterBundle->interpreterMutex_);
    658 #else
    659         boost::try_mutex::scoped_lock interpreter_lock(interpreterBundle->interpreterMutex_);
    660 #endif
    661640        try
    662641        {
Note: See TracChangeset for help on using the changeset viewer.