Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3313 was 3313, checked in by rgrieder, 15 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
Line 
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
29#include "TclThreadManager.h"
30
31#include <boost/bind.hpp>
32#include <boost/thread/condition.hpp>
33#include <boost/thread/mutex.hpp>
34#include <boost/thread/thread.hpp>
35#include <OgreTimer.h>
36#include <cpptcl/cpptcl.h>
37
38#include "util/Convert.h"
39#include "util/Debug.h"
40#include "Clock.h"
41#include "CommandExecutor.h"
42#include "ConsoleCommand.h"
43#include "CoreIncludes.h"
44#include "TclBind.h"
45
46namespace orxonox
47{
48    const unsigned int TCLTHREADMANAGER_MAX_QUEUE_LENGTH = 100;
49    const float TCLTHREADMANAGER_MAX_CPU_USAGE = 0.50f;
50
51    SetConsoleCommandShortcutAlias(TclThreadManager, execute, "tclexecute").argumentCompleter(0, autocompletion::tclthreads());
52    SetConsoleCommandShortcutAlias(TclThreadManager, query,   "tclquery"  ).argumentCompleter(0, autocompletion::tclthreads());
53    SetConsoleCommand(TclThreadManager, create,  false);
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());
57    SetConsoleCommand(TclThreadManager, status,  false);
58    SetConsoleCommand(TclThreadManager, dump,    false).argumentCompleter(0, autocompletion::tclthreads());
59    SetConsoleCommand(TclThreadManager, flush,   false).argumentCompleter(0, autocompletion::tclthreads());
60
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
89    TclThreadManager* TclThreadManager::singletonRef_s = 0;
90
91    TclThreadManager::TclThreadManager(Tcl::interpreter* interpreter)
92        : orxonoxInterpreterBundle_(new TclInterpreterBundle())
93    {
94        RegisterRootObject(TclThreadManager);
95
96        assert(singletonRef_s == 0);
97        singletonRef_s = this;
98
99        this->threadCounter_ = 0;
100        this->orxonoxInterpreterBundle_->id_ = 0;
101        this->orxonoxInterpreterBundle_->interpreter_ = interpreter;
102        threadID_g = boost::this_thread::get_id();
103    }
104
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);
116
117        delete this->orxonoxInterpreterBundle_;
118
119        singletonRef_s = 0;
120    }
121
122    unsigned int TclThreadManager::create()
123    {
124        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
125        TclThreadManager::getInstance().threadCounter_++;
126        std::string name = multi_cast<std::string>(TclThreadManager::getInstance().threadCounter_);
127
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_;
138    }
139
140    unsigned int TclThreadManager::createID(unsigned int threadID)
141    {
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);
152        if (bundle)
153        {
154            {
155                boost::mutex::scoped_lock running_lock(bundle->runningMutex_);
156                bundle->running_ = false;
157            }
158            while (true)
159            {
160                {
161                    boost::mutex::scoped_lock finished_lock(bundle->finishedMutex_);
162                    if (bundle->finished_)
163                    {
164                        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
165                        boost::mutex::scoped_try_lock interpreter_lock(bundle->interpreterMutex_);
166                        try
167                        {
168                            while (!interpreter_lock.try_lock())
169                            {
170                                orxonoxEvalCondition_g.notify_one();
171                                boost::this_thread::yield();
172                            }
173                        } catch (...) {}
174                        delete bundle->interpreter_;
175                        delete bundle;
176                        TclThreadManager::getInstance().interpreterBundles_.erase(threadID);
177                        break;
178                    }
179                }
180
181                orxonoxEvalCondition_g.notify_one();
182                boost::this_thread::yield();
183            }
184
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";
223            {
224                boost::mutex::scoped_lock queue_lock((*it).second->queueMutex_);
225                output += multi_cast<std::string>((*it).second->queue_.size());
226            }
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    }
238
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)))
250            {
251                COUT(0) << "Queue dump of Tcl-thread " << threadID << ":" << std::endl;
252            }
253            else
254                return;
255        }
256
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    }
265
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;
274
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;
280        }
281        else
282        {
283            COUT(0) << "Flushed queue of Tcl-interpreter " << threadID << "." << std::endl;
284        }
285    }
286
287    void TclThreadManager::tcl_execute(Tcl::object const &args)
288    {
289        TclThreadManager::getInstance().pushCommandToQueue(stripEnclosingBraces(args.get()));
290    }
291
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    }
296
297    std::string TclThreadManager::tcl_crossquery(int querierID, int threadID, Tcl::object const &args)
298    {
299        return TclThreadManager::getInstance().evalQuery(static_cast<unsigned int>(querierID), static_cast<unsigned int>(threadID), stripEnclosingBraces(args.get()));
300    }
301
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
318        try
319        {
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);
324
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);
329
330            i->eval("rename exit tcl::exit");
331            i->eval("proc exit {} { orxonox TclThreadManager destroy " + threadID + " }");
332
333            i->eval("redef_puts");
334
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;   }
344
345        return i;
346    }
347
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;
355        }
356        else
357        {
358            this->error("Error: No Tcl-interpreter with ID " + multi_cast<std::string>(threadID) + " existing.");
359            return 0;
360        }
361    }
362
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)
372        {
373            if (it != list.begin())
374                output += " ";
375
376            output += multi_cast<std::string>(*it);
377        }
378        return output;
379    }
380
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);
394    }
395
396    void TclThreadManager::debug(const std::string& error)
397    {
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);
409    }
410
411    void TclThreadManager::pushCommandToQueue(const std::string& command)
412    {
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);
418    }
419
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    }
425
426    std::string TclThreadManager::popCommandFromQueue()
427    {
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;
433    }
434
435    bool TclThreadManager::queueIsEmpty()
436    {
437        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_);
438        return this->orxonoxInterpreterBundle_->queue_.empty();
439    }
440
441    void TclThreadManager::pushCommandToQueue(unsigned int threadID, const std::string& command)
442    {
443        TclInterpreterBundle* bundle = this->getInterpreterBundle(threadID);
444        if (bundle)
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
453            bundle->queue_.push_back(command);
454        }
455    }
456
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    }
469
470    bool TclThreadManager::queueIsEmpty(unsigned int threadID)
471    {
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;
479    }
480
481    bool TclThreadManager::updateQueriersList(TclInterpreterBundle* querier, TclInterpreterBundle* target)
482    {
483        if (querier == target)
484            return false;
485
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;
502    }
503
504    std::string TclThreadManager::evalQuery(unsigned int querierID, const std::string& command)
505    {
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;
526    }
527
528    std::string TclThreadManager::evalQuery(unsigned int querierID, unsigned int threadID, const std::string& command)
529    {
530        TclInterpreterBundle* target = 0;
531        if (threadID)
532            target = this->getInterpreterBundle(threadID);
533        else
534            target = this->orxonoxInterpreterBundle_;
535
536        std::string output = "";
537        if (target)
538        {
539            TclInterpreterBundle* querier = 0;
540            if (querierID)
541                querier = this->getInterpreterBundle(querierID);
542            else
543                querier = this->orxonoxInterpreterBundle_;
544
545            if (querier)
546            {
547                if (this->updateQueriersList(querier, target))
548                {
549                    boost::mutex::scoped_try_lock interpreter_lock(target->interpreterMutex_);
550                    bool successfullyLocked = false;
551                    try
552                    {
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                            }
561
562                            successfullyLocked = true;
563                        }
564                    } catch (...) {}
565
566                    if (successfullyLocked)
567                    {
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()));   }
575                    }
576                    else
577                    {
578                        this->error("Error: Couldn't query Tcl-interpreter with ID " + multi_cast<std::string>(threadID) + ", interpreter is busy right now.");
579                    }
580                }
581
582                boost::mutex::scoped_lock queriers_lock(target->queriersMutex_);
583                target->queriers_.clear();
584            }
585        }
586        return output;
587    }
588
589    void TclThreadManager::update(const Clock& time)
590    {
591        {
592            orxonoxEvalCondition_g.notify_one();
593            boost::this_thread::yield();
594        }
595
596        {
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            }
612        }
613
614        {
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            }
624        }
625    }
626
627    std::list<unsigned int> TclThreadManager::getThreadList() const
628    {
629        boost::mutex::scoped_lock bundles_lock(bundlesMutex_g);
630        std::list<unsigned int> threads;
631        for (std::map<unsigned int, TclInterpreterBundle*>::const_iterator it = this->interpreterBundles_.begin(); it != this->interpreterBundles_.end(); ++it)
632            threads.push_back((*it).first);
633        return threads;
634    }
635
636    void tclThread(TclInterpreterBundle* interpreterBundle, std::string command)
637    {
638        TclThreadManager::getInstance().debug("TclThread_execute: " + command);
639        boost::mutex::scoped_lock interpreter_lock(interpreterBundle->interpreterMutex_);
640        try
641        {
642            interpreterBundle->interpreter_->eval(command);
643        }
644        catch (Tcl::tcl_error const &e)
645        {
646            TclThreadManager::getInstance().error("Tcl (ID " + multi_cast<std::string>(interpreterBundle->id_) + ") error: " + e.what());
647        }
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        }
652
653        boost::mutex::scoped_lock finished_lock(interpreterBundle->finishedMutex_);
654        interpreterBundle->finished_ = true;
655        interpreterBundle->finishedCondition_.notify_all();
656    }
657}
Note: See TracBrowser for help on using the repository browser.