Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/TclThreadManager.cc @ 3313

Last change on this file since 3313 was 3313, checked in by rgrieder, 16 years ago

Reverted TclThreadManager commits to make a tag (there's possibly still an unresolved issue with the TclThreadManager under linux when terminating the game (mutex assert)).

  • Property svn:eol-style set to native
File size: 25.1 KB
RevLine 
[1505]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
[1792]29#include "TclThreadManager.h"
30
[1505]31#include <boost/bind.hpp>
[3313]32#include <boost/thread/condition.hpp>
33#include <boost/thread/mutex.hpp>
[3304]34#include <boost/thread/thread.hpp>
[1505]35#include <OgreTimer.h>
[3196]36#include <cpptcl/cpptcl.h>
[1505]37
[3196]38#include "util/Convert.h"
[3313]39#include "util/Debug.h"
[2896]40#include "Clock.h"
[3196]41#include "CommandExecutor.h"
42#include "ConsoleCommand.h"
[1505]43#include "CoreIncludes.h"
44#include "TclBind.h"
45
46namespace orxonox
47{
[3313]48    const unsigned int TCLTHREADMANAGER_MAX_QUEUE_LENGTH = 100;
[1786]49    const float TCLTHREADMANAGER_MAX_CPU_USAGE = 0.50f;
[1784]50
[1747]51    SetConsoleCommandShortcutAlias(TclThreadManager, execute, "tclexecute").argumentCompleter(0, autocompletion::tclthreads());
52    SetConsoleCommandShortcutAlias(TclThreadManager, query,   "tclquery"  ).argumentCompleter(0, autocompletion::tclthreads());
[1505]53    SetConsoleCommand(TclThreadManager, create,  false);
[1747]54    SetConsoleCommand(TclThreadManager, destroy, false).argumentCompleter(0, autocompletion::tclthreads());
55    SetConsoleCommand(TclThreadManager, execute, false).argumentCompleter(0, autocompletion::tclthreads());
56    SetConsoleCommand(TclThreadManager, query,   false).argumentCompleter(0, autocompletion::tclthreads());
[3313]57    SetConsoleCommand(TclThreadManager, status,  false);
58    SetConsoleCommand(TclThreadManager, dump,    false).argumentCompleter(0, autocompletion::tclthreads());
59    SetConsoleCommand(TclThreadManager, flush,   false).argumentCompleter(0, autocompletion::tclthreads());
[1505]60
[3304]61    struct TclInterpreterBundle
62    {
[3313]63        unsigned int id_;
[3304]64
[3313]65        std::list<std::string> queue_;
66        boost::mutex queueMutex_;
[3304]67
[3313]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_;
[3304]81    };
82
83
[3313]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
89    TclThreadManager* TclThreadManager::singletonRef_s = 0;
90
[1792]91    TclThreadManager::TclThreadManager(Tcl::interpreter* interpreter)
[3313]92        : orxonoxInterpreterBundle_(new TclInterpreterBundle())
[1505]93    {
94        RegisterRootObject(TclThreadManager);
95
[3313]96        assert(singletonRef_s == 0);
97        singletonRef_s = this;
[1792]98
[3313]99        this->threadCounter_ = 0;
100        this->orxonoxInterpreterBundle_->id_ = 0;
101        this->orxonoxInterpreterBundle_->interpreter_ = interpreter;
102        threadID_g = boost::this_thread::get_id();
103    }
[1505]104
[3313]105    TclThreadManager::~TclThreadManager()
106    {
107        unsigned int threadID;
108        {
109            boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
110            if (this->interpreterBundles_.begin() == this->interpreterBundles_.end())
111                return;
112            else
113                threadID = this->interpreterBundles_.begin()->first;
114        }
115        this->destroy(threadID);
[3307]116
[3313]117        delete this->orxonoxInterpreterBundle_;
[3307]118
[3313]119        singletonRef_s = 0;
[1505]120    }
121
[3313]122    unsigned int TclThreadManager::create()
[1505]123    {
[3313]124        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
125        TclThreadManager::getInstance().threadCounter_++;
126        std::string name = multi_cast<std::string>(TclThreadManager::getInstance().threadCounter_);
[1505]127
[3313]128        TclInterpreterBundle* bundle = new TclInterpreterBundle;
129        bundle->id_ = TclThreadManager::getInstance().threadCounter_;
130        bundle->interpreter_ = TclThreadManager::getInstance().createNewTclInterpreter(name);
131        bundle->interpreterName_ = name;
132        bundle->running_ = true;
133        bundle->finished_ = true;
134
135        TclThreadManager::getInstance().interpreterBundles_[TclThreadManager::getInstance().threadCounter_] = bundle;
136        COUT(0) << "Created new Tcl-interpreter with ID " << TclThreadManager::getInstance().threadCounter_ << std::endl;
137        return TclThreadManager::getInstance().threadCounter_;
[1505]138    }
139
[3313]140    unsigned int TclThreadManager::createID(unsigned int threadID)
[1505]141    {
[3313]142        unsigned int temp = TclThreadManager::getInstance().threadCounter_;
143        TclThreadManager::getInstance().threadCounter_ = threadID - 1;
144        TclThreadManager::create();
145        TclThreadManager::getInstance().threadCounter_ = temp;
146        return threadID;
147    }
148
149    void TclThreadManager::destroy(unsigned int threadID)
150    {
151        TclInterpreterBundle* bundle = TclThreadManager::getInstance().getInterpreterBundle(threadID);
[1505]152        if (bundle)
153        {
154            {
[3313]155                boost::mutex::scoped_lock running_lock(bundle->runningMutex_);
156                bundle->running_ = false;
[1505]157            }
[3313]158            while (true)
[1505]159            {
160                {
[3313]161                    boost::mutex::scoped_lock finished_lock(bundle->finishedMutex_);
162                    if (bundle->finished_)
[1505]163                    {
[3313]164                        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
165                        boost::mutex::scoped_try_lock interpreter_lock(bundle->interpreterMutex_);
[1505]166                        try
167                        {
[3313]168                            while (!interpreter_lock.try_lock())
[1505]169                            {
[3313]170                                orxonoxEvalCondition_g.notify_one();
171                                boost::this_thread::yield();
[1505]172                            }
[3313]173                        } catch (...) {}
174                        delete bundle->interpreter_;
175                        delete bundle;
176                        TclThreadManager::getInstance().interpreterBundles_.erase(threadID);
177                        break;
[1505]178                    }
179                }
[3313]180
181                orxonoxEvalCondition_g.notify_one();
182                boost::this_thread::yield();
[1505]183            }
184
[3313]185            COUT(0) << "Destroyed Tcl-interpreter with ID " << threadID << std::endl;
186        }
187    }
188
189    void TclThreadManager::execute(unsigned int threadID, const std::string& _command)
190    {
191        std::string command = stripEnclosingBraces(_command);
192
193        if (threadID == 0)
194            TclThreadManager::getInstance().pushCommandToQueue(command);
195        else
196            TclThreadManager::getInstance().pushCommandToQueue(threadID, command);
197    }
198
199    std::string TclThreadManager::query(unsigned int threadID, const std::string& command)
200    {
201        return TclThreadManager::getInstance().evalQuery(TclThreadManager::getInstance().orxonoxInterpreterBundle_->id_, threadID, command);
202    }
203
204    void TclThreadManager::status()
205    {
206        COUT(0) << "Thread ID" << '\t' << "Queue size" << '\t' << "State" << std::endl;
207
208        std::string output = "Orxonox";
209        output += "\t\t";
210        {
211            boost::mutex::scoped_lock queue_lock(TclThreadManager::getInstance().orxonoxInterpreterBundle_->queueMutex_);
212            output += multi_cast<std::string>(TclThreadManager::getInstance().orxonoxInterpreterBundle_->queue_.size());
213        }
214        output += "\t\t";
215        output += "busy";
216        COUT(0) << output << std::endl;
217
218        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
219        for (std::map<unsigned int, TclInterpreterBundle*>::const_iterator it = TclThreadManager::getInstance().interpreterBundles_.begin(); it != TclThreadManager::getInstance().interpreterBundles_.end(); ++it)
220        {
221            std::string output = multi_cast<std::string>((*it).first);
222            output += "\t\t";
[3307]223            {
[3313]224                boost::mutex::scoped_lock queue_lock((*it).second->queueMutex_);
225                output += multi_cast<std::string>((*it).second->queue_.size());
[1505]226            }
[3313]227            output += "\t\t";
228            {
229                boost::mutex::scoped_try_lock interpreter_lock((*it).second->interpreterMutex_);
230                if (interpreter_lock.try_lock())
231                    output += "ready";
232                else
233                    output += "busy";
234            }
235            COUT(0) << output << std::endl;
236        }
237    }
[1505]238
[3313]239    void TclThreadManager::dump(unsigned int threadID)
240    {
241        TclInterpreterBundle* bundle = 0;
242        if (threadID == 0)
243        {
244            bundle = TclThreadManager::getInstance().orxonoxInterpreterBundle_;
245            COUT(0) << "Queue dump of Orxonox:" << std::endl;
246        }
247        else
248        {
249            if ((bundle = TclThreadManager::getInstance().getInterpreterBundle(threadID)))
[1505]250            {
[3313]251                COUT(0) << "Queue dump of Tcl-thread " << threadID << ":" << std::endl;
252            }
253            else
254                return;
255        }
[1505]256
[3313]257        boost::mutex::scoped_lock queue_lock(bundle->queueMutex_);
258        unsigned int index = 0;
259        for (std::list<std::string>::const_iterator it = bundle->queue_.begin(); it != bundle->queue_.end(); ++it)
260        {
261            index++;
262            COUT(0) << index << ": " << (*it) << std::endl;
263        }
264    }
[1505]265
[3313]266    void TclThreadManager::flush(unsigned int threadID)
267    {
268        TclInterpreterBundle* bundle = 0;
269        if (threadID == 0)
270            bundle = TclThreadManager::getInstance().orxonoxInterpreterBundle_;
271        else
272            if (!(bundle = TclThreadManager::getInstance().getInterpreterBundle(threadID)))
273                return;
[1505]274
[3313]275        boost::mutex::scoped_lock queue_lock(bundle->queueMutex_);
276        bundle->queue_.clear();
277        if (threadID == 0)
278        {
279            COUT(0) << "Flushed queue of Orxonox Tcl-interpreter." << std::endl;
[1505]280        }
[3313]281        else
282        {
283            COUT(0) << "Flushed queue of Tcl-interpreter " << threadID << "." << std::endl;
284        }
[1505]285    }
286
[3313]287    void TclThreadManager::tcl_execute(Tcl::object const &args)
[1505]288    {
[3313]289        TclThreadManager::getInstance().pushCommandToQueue(stripEnclosingBraces(args.get()));
[1505]290    }
291
[3313]292    std::string TclThreadManager::tcl_query(int querierID, Tcl::object const &args)
293    {
294        return TclThreadManager::getInstance().evalQuery(static_cast<unsigned int>(querierID), stripEnclosingBraces(args.get()));
295    }
[1505]296
[3313]297    std::string TclThreadManager::tcl_crossquery(int querierID, int threadID, Tcl::object const &args)
[1505]298    {
[3313]299        return TclThreadManager::getInstance().evalQuery(static_cast<unsigned int>(querierID), static_cast<unsigned int>(threadID), stripEnclosingBraces(args.get()));
300    }
[1505]301
[3313]302    bool TclThreadManager::tcl_running(int threadID)
303    {
304        TclInterpreterBundle* bundle = TclThreadManager::getInstance().getInterpreterBundle(static_cast<unsigned int>(threadID));
305        if (bundle)
306        {
307            boost::mutex::scoped_lock running_lock(bundle->runningMutex_);
308            return bundle->running_;
309        }
310        return false;
311    }
312
313    Tcl::interpreter* TclThreadManager::createNewTclInterpreter(const std::string& threadID)
314    {
315        Tcl::interpreter* i = 0;
316        i = new Tcl::interpreter(TclBind::getInstance().getTclLibPath());
317
[3307]318        try
[1505]319        {
[3313]320            i->def("orxonox::query", TclThreadManager::tcl_query, Tcl::variadic());
321            i->def("orxonox::crossquery", TclThreadManager::tcl_crossquery, Tcl::variadic());
322            i->def("orxonox::execute", TclThreadManager::tcl_execute, Tcl::variadic());
323            i->def("orxonox::running", TclThreadManager::tcl_running);
[1505]324
[3313]325            i->def("execute", TclThreadManager::tcl_execute, Tcl::variadic());
326            i->eval("proc query args { orxonox::query " + threadID + " $args }");
327            i->eval("proc crossquery {id args} { orxonox::crossquery " + threadID + " $id $args }");
328            i->eval("set id " + threadID);
[1505]329
[3313]330            i->eval("rename exit tcl::exit");
331            i->eval("proc exit {} { orxonox TclThreadManager destroy " + threadID + " }");
[1505]332
[3313]333            i->eval("redef_puts");
[1505]334
[3313]335//            i->eval("rename while tcl::while");
336//            i->eval("proc while {test command} { tcl::while {[uplevel 1 expr $test]} {uplevel 1 $command} }"); // (\"$test\" && [orxonox::running " + threadID + "]])
337//            i->eval("rename for tcl::for");
338//            i->eval("proc for {start test next command} { uplevel tcl::for \"$start\" \"$test\" \"$next\" \"$command\" }");
339        }
340        catch (Tcl::tcl_error const &e)
341        {   COUT(1) << "Tcl error while creating Tcl-interpreter (" << threadID << "): " << e.what() << std::endl;   }
342        catch (std::exception const &e)
343        {   COUT(1) << "Error while creating Tcl-interpreter (" << threadID << "): " << e.what() << std::endl;   }
[1505]344
[3313]345        return i;
346    }
[1505]347
[3313]348    TclInterpreterBundle* TclThreadManager::getInterpreterBundle(unsigned int threadID)
349    {
350        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
351        std::map<unsigned int, TclInterpreterBundle*>::iterator it = this->interpreterBundles_.find(threadID);
352        if (it != this->interpreterBundles_.end())
353        {
354            return (*it).second;
[1505]355        }
[3313]356        else
357        {
358            this->error("Error: No Tcl-interpreter with ID " + multi_cast<std::string>(threadID) + " existing.");
359            return 0;
360        }
361    }
[1505]362
[3313]363    Tcl::interpreter* TclThreadManager::getTclInterpreter(unsigned int threadID)
364    {
365        return this->getInterpreterBundle(threadID)->interpreter_;
366    }
367
368    std::string TclThreadManager::dumpList(const std::list<unsigned int>& list)
369    {
370        std::string output = "";
371        for (std::list<unsigned int>::const_iterator it = list.begin(); it != list.end(); ++it)
[1505]372        {
[3313]373            if (it != list.begin())
374                output += " ";
375
376            output += multi_cast<std::string>(*it);
[1505]377        }
[3313]378        return output;
379    }
[1505]380
[3313]381    void TclThreadManager::error(const std::string& error)
382    {
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            {
388                boost::this_thread::yield();
389                return;
390            }
391        }
392
393        this->forceCommandToFrontOfQueue("error " + error);
[3304]394    }
395
[3313]396    void TclThreadManager::debug(const std::string& error)
[1505]397    {
[3313]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            {
403                boost::this_thread::yield();
404                return;
405            }
406        }
407
408        this->forceCommandToFrontOfQueue("debug " + error);
[1505]409    }
410
[3313]411    void TclThreadManager::pushCommandToQueue(const std::string& command)
[1505]412    {
[3313]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);
[1505]418    }
419
[3313]420    void TclThreadManager::forceCommandToFrontOfQueue(const std::string& command)
421    {
422        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
423        this->orxonoxInterpreterBundle_->queue_.push_front(command);
424    }
[1505]425
[3313]426    std::string TclThreadManager::popCommandFromQueue()
[1505]427    {
[3313]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();
432        return temp;
[1505]433    }
434
[3313]435    bool TclThreadManager::queueIsEmpty()
[1505]436    {
[3313]437        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
438        return this->orxonoxInterpreterBundle_->queue_.empty();
[1505]439    }
440
[3313]441    void TclThreadManager::pushCommandToQueue(unsigned int threadID, const std::string& command)
[1505]442    {
[3313]443        TclInterpreterBundle* bundle = this->getInterpreterBundle(threadID);
[3307]444        if (bundle)
[3313]445        {
446            boost::mutex::scoped_lock queue_lock(bundle->queueMutex_);
447            if (bundle->queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
448            {
449                this->error("Error: Queue of Tcl-interpreter " + multi_cast<std::string>(threadID) + " is full, couldn't add command.");
450                return;
451            }
452
[3307]453            bundle->queue_.push_back(command);
[3313]454        }
[1505]455    }
456
[3313]457    std::string TclThreadManager::popCommandFromQueue(unsigned int threadID)
458    {
459        TclInterpreterBundle* bundle = this->getInterpreterBundle(threadID);
460        if (bundle)
461        {
462            boost::mutex::scoped_lock queue_lock(bundle->queueMutex_);
463            std::string temp = bundle->queue_.front();
464            bundle->queue_.pop_front();
465            return temp;
466        }
467        return "";
468    }
[3307]469
[3313]470    bool TclThreadManager::queueIsEmpty(unsigned int threadID)
[1505]471    {
[3313]472        TclInterpreterBundle* bundle = this->getInterpreterBundle(threadID);
473        if (bundle)
474        {
475            boost::mutex::scoped_lock queue_lock(bundle->queueMutex_);
476            return bundle->queue_.empty();
477        }
478        return true;
[1505]479    }
480
[3313]481    bool TclThreadManager::updateQueriersList(TclInterpreterBundle* querier, TclInterpreterBundle* target)
482    {
483        if (querier == target)
484            return false;
[1505]485
[3313]486        boost::mutex::scoped_lock queriers_lock(target->queriersMutex_);
487
488        {
489            boost::mutex::scoped_lock queriers_lock(querier->queriersMutex_);
490            target->queriers_.insert(target->queriers_.end(), querier->queriers_.begin(), querier->queriers_.end());
491        }
492
493        target->queriers_.insert(target->queriers_.end(), querier->id_);
494
495        if (std::find(target->queriers_.begin(), target->queriers_.end(), target->id_) != target->queriers_.end())
496        {
497            this->error("Error: Circular query (" + this->dumpList(target->queriers_) + " -> " + multi_cast<std::string>(target->id_) + "), couldn't query Tcl-interpreter with ID " + multi_cast<std::string>(target->id_) + " from other interpreter with ID " + multi_cast<std::string>(querier->id_) + ".");
498            return false;
499        }
500
501        return true;
[1505]502    }
503
[3313]504    std::string TclThreadManager::evalQuery(unsigned int querierID, const std::string& command)
[1505]505    {
[3313]506        TclInterpreterBundle* querier = this->getInterpreterBundle(querierID);
507        std::string output = "";
508        if (querier)
509        {
510            if (this->updateQueriersList(querier, this->orxonoxInterpreterBundle_))
511            {
512                boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_->interpreterMutex_);
513                orxonoxEvalCondition_g.wait(interpreter_lock);
514
515                if (!CommandExecutor::execute(command, false))
516                    this->error("Error: Can't execute command \"" + command + "\"!");
517
518                if (CommandExecutor::getLastEvaluation().hasReturnvalue())
519                    output = CommandExecutor::getLastEvaluation().getReturnvalue().getString();
520            }
521
522            boost::mutex::scoped_lock queriers_lock(this->orxonoxInterpreterBundle_->queriersMutex_);
523            this->orxonoxInterpreterBundle_->queriers_.clear();
524        }
525        return output;
[1505]526    }
527
[3313]528    std::string TclThreadManager::evalQuery(unsigned int querierID, unsigned int threadID, const std::string& command)
[1505]529    {
[3313]530        TclInterpreterBundle* target = 0;
531        if (threadID)
532            target = this->getInterpreterBundle(threadID);
533        else
534            target = this->orxonoxInterpreterBundle_;
[1505]535
[3313]536        std::string output = "";
537        if (target)
[1505]538        {
[3313]539            TclInterpreterBundle* querier = 0;
540            if (querierID)
541                querier = this->getInterpreterBundle(querierID);
542            else
543                querier = this->orxonoxInterpreterBundle_;
[1505]544
[3313]545            if (querier)
[1505]546            {
[3313]547                if (this->updateQueriersList(querier, target))
[1505]548                {
[3313]549                    boost::mutex::scoped_try_lock interpreter_lock(target->interpreterMutex_);
550                    bool successfullyLocked = false;
551                    try
[1505]552                    {
[3313]553                        if (querierID == 0 || std::find(querier->queriers_.begin(), querier->queriers_.end(), 0U) != querier->queriers_.end())
554                            successfullyLocked = interpreter_lock.try_lock();
555                        else
556                        {
557                            while (!interpreter_lock.try_lock())
558                            {
559                                boost::this_thread::yield();
560                            }
[1505]561
[3313]562                            successfullyLocked = true;
563                        }
564                    } catch (...) {}
[1505]565
[3313]566                    if (successfullyLocked)
[1505]567                    {
[3313]568                        this->debug("TclThread_query: " + command);
569                        try
570                        {   output = static_cast<std::string>(target->interpreter_->eval(command));   }
571                        catch (Tcl::tcl_error const &e)
572                        {   this->error("Tcl error: " + static_cast<std::string>(e.what()));   }
573                        catch (std::exception const &e)
574                        {   this->error("Error while executing Tcl: " + static_cast<std::string>(e.what()));   }
[1505]575                    }
576                    else
577                    {
[3313]578                        this->error("Error: Couldn't query Tcl-interpreter with ID " + multi_cast<std::string>(threadID) + ", interpreter is busy right now.");
[1505]579                    }
[3313]580                }
[3307]581
[3313]582                boost::mutex::scoped_lock queriers_lock(target->queriersMutex_);
583                target->queriers_.clear();
[3307]584            }
[1505]585        }
586        return output;
587    }
588
[3313]589    void TclThreadManager::update(const Clock& time)
[1505]590    {
591        {
[3313]592            orxonoxEvalCondition_g.notify_one();
593            boost::this_thread::yield();
[1505]594        }
[3313]595
[1505]596        {
[3313]597            boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
598            for (std::map<unsigned int, TclInterpreterBundle*>::iterator it = this->interpreterBundles_.begin(); it != this->interpreterBundles_.end(); ++it)
599            {
600                boost::mutex::scoped_lock queue_lock((*it).second->queueMutex_);
601                if (!(*it).second->queue_.empty())
602                {
603                    std::string command = (*it).second->queue_.front();
604                    (*it).second->queue_.pop_front();
605                    {
606                        boost::mutex::scoped_lock finished_lock((*it).second->finishedMutex_);
607                        (*it).second->finished_ = false;
608                    }
609                    boost::thread(boost::bind(&tclThread, (*it).second, command));
610                }
611            }
[1505]612        }
613
614        {
[3313]615            boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_->interpreterMutex_);
616            unsigned long maxtime = static_cast<unsigned long>(time.getDeltaTime() * 1000000 * TCLTHREADMANAGER_MAX_CPU_USAGE);
617            Ogre::Timer timer;
618            while (!this->queueIsEmpty())
619            {
620                CommandExecutor::execute(this->popCommandFromQueue(), false);
621                if (timer.getMicroseconds() > maxtime)
622                    break;
623            }
[1505]624        }
625    }
626
627    std::list<unsigned int> TclThreadManager::getThreadList() const
628    {
[3313]629        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
[1505]630        std::list<unsigned int> threads;
631        for (std::map<unsigned int, TclInterpreterBundle*>::const_iterator it = this->interpreterBundles_.begin(); it != this->interpreterBundles_.end(); ++it)
[3313]632            threads.push_back((*it).first);
[1505]633        return threads;
634    }
635
[3313]636    void tclThread(TclInterpreterBundle* interpreterBundle, std::string command)
[1505]637    {
[3313]638        TclThreadManager::getInstance().debug("TclThread_execute: " + command);
639        boost::mutex::scoped_lock interpreter_lock(interpreterBundle->interpreterMutex_);
640        try
[1505]641        {
[3313]642            interpreterBundle->interpreter_->eval(command);
[1505]643        }
[3313]644        catch (Tcl::tcl_error const &e)
[1505]645        {
[3313]646            TclThreadManager::getInstance().error("Tcl (ID " + multi_cast<std::string>(interpreterBundle->id_) + ") error: " + e.what());
[1505]647        }
[3313]648        catch (std::exception const &e)
649        {
650            TclThreadManager::getInstance().error("Error while executing Tcl (ID " + multi_cast<std::string>(interpreterBundle->id_) + "): " + e.what());
651        }
[1505]652
[3313]653        boost::mutex::scoped_lock finished_lock(interpreterBundle->finishedMutex_);
654        interpreterBundle->finished_ = true;
655        interpreterBundle->finishedCondition_.notify_all();
[1505]656    }
657}
Note: See TracBrowser for help on using the repository browser.