Index: code/branches/console/src/core/TclThreadManager.cc
===================================================================
--- code/branches/console/src/core/TclThreadManager.cc	(revision 1255)
+++ code/branches/console/src/core/TclThreadManager.cc	(revision 1257)
@@ -43,5 +43,5 @@
 #include "util/Convert.h"
 
-#define TCLTHREADMANAGER_MAX_QUEUE_LENGTH 1024
+#define TCLTHREADMANAGER_MAX_QUEUE_LENGTH 100
 #define TCLTHREADMANAGER_MAX_CPU_USAGE 0.50
 
@@ -65,4 +65,5 @@
         this->orxonoxInterpreterBundle_.id_ = 0;
         this->orxonoxInterpreterBundle_.interpreter_ = TclBind::getInstance().getTclInterpreter();
+        this->threadID_ = boost::this_thread::get_id();
     }
 
@@ -75,5 +76,5 @@
     unsigned int TclThreadManager::create()
     {
-        boost::mutex::scoped_lock lock(TclThreadManager::getInstance().bundlesMutex_);
+        boost::mutex::scoped_lock bundles_lock(TclThreadManager::getInstance().bundlesMutex_);
         TclThreadManager::getInstance().threadCounter_++;
         std::string name = getConvertedValue<unsigned int, std::string>(TclThreadManager::getInstance().threadCounter_);
@@ -84,4 +85,5 @@
         bundle->interpreterName_ = name;
         bundle->running_ = true;
+        bundle->finished_ = true;
 
         TclThreadManager::getInstance().interpreterBundles_[TclThreadManager::getInstance().threadCounter_] = bundle;
@@ -96,21 +98,35 @@
         {
             {
-                boost::mutex::scoped_lock lock(bundle->runningMutex_);
+                boost::mutex::scoped_lock running_lock(bundle->runningMutex_);
                 bundle->running_ = false;
             }
-            {
-                boost::mutex::scoped_lock lock(bundle->finishedMutex_);
-                while (!bundle->finished_)
-                    bundle->finishedCondition_.wait(lock);
-            }
-            {
-                boost::mutex::scoped_lock lock(bundle->interpreterMutex_);
-                delete bundle->interpreter_;
-            }
-            delete bundle;
-            {
-                boost::mutex::scoped_lock lock(TclThreadManager::getInstance().bundlesMutex_);
-                TclThreadManager::getInstance().interpreterBundles_.erase(threadID);
-            }
+            while (true)
+            {
+                {
+                    boost::mutex::scoped_lock finished_lock(bundle->finishedMutex_);
+                    if (bundle->finished_)
+                    {
+                        boost::mutex::scoped_lock bundles_lock(TclThreadManager::getInstance().bundlesMutex_);
+                        boost::mutex::scoped_lock interpreter_lock(bundle->interpreterMutex_, boost::defer_lock_t());
+                        try
+                        {
+                            while (!interpreter_lock.try_lock())
+                            {
+                                TclThreadManager::getInstance().orxonoxEvalCondition_.notify_one();
+                                boost::this_thread::yield();
+                            }
+                        } catch (...) {}
+                        delete bundle->interpreter_;
+                        delete bundle;
+                        TclThreadManager::getInstance().interpreterBundles_.erase(threadID);
+                        break;
+                    }
+                }
+
+                TclThreadManager::getInstance().orxonoxEvalCondition_.notify_one();
+                boost::this_thread::yield();
+            }
+
+            COUT(0) << "Destroyed Tcl-interpreter with ID " << threadID << std::endl;
         }
     }
@@ -158,5 +174,5 @@
         if (bundle)
         {
-            boost::mutex::scoped_lock lock(bundle->runningMutex_);
+            boost::mutex::scoped_lock running_lock(bundle->runningMutex_);
             return bundle->running_;
         }
@@ -201,8 +217,5 @@
     TclInterpreterBundle* TclThreadManager::getInterpreterBundle(unsigned int threadID)
     {
-        if (threadID == 0)
-            return &this->orxonoxInterpreterBundle_;
-
-        boost::mutex::scoped_lock lock(this->bundlesMutex_);
+        boost::mutex::scoped_lock bundles_lock(this->bundlesMutex_);
         std::map<unsigned int, TclInterpreterBundle*>::iterator it = this->interpreterBundles_.find(threadID);
         if (it != this->interpreterBundles_.end())
@@ -212,5 +225,5 @@
         else
         {
-            this->forceCommandToFrontOfQueue("error Error: No Tcl-interpreter with ID " + getConvertedValue<unsigned int, std::string>(threadID) + " existing.");
+            this->error("Error: No Tcl-interpreter with ID " + getConvertedValue<unsigned int, std::string>(threadID) + " existing.");
             return 0;
         }
@@ -230,9 +243,21 @@
     }
 
+    void TclThreadManager::error(const std::string& error)
+    {
+        if (boost::this_thread::get_id() != this->threadID_)
+        {
+            boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
+            if (this->orxonoxInterpreterBundle_.queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
+                return;
+        }
+
+        this->forceCommandToFrontOfQueue("error " + error);
+    }
+
     void TclThreadManager::pushCommandToQueue(const std::string& command)
     {
-        boost::mutex::scoped_lock lock(this->orxonoxInterpreterBundle_.queueMutex_);
+        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
         while (this->orxonoxInterpreterBundle_.queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
-            this->fullQueueCondition_.wait(lock);
+            this->fullQueueCondition_.wait(queue_lock);
 
         this->orxonoxInterpreterBundle_.queue_.push_back(command);
@@ -241,5 +266,5 @@
     void TclThreadManager::forceCommandToFrontOfQueue(const std::string& command)
     {
-        boost::mutex::scoped_lock lock(this->orxonoxInterpreterBundle_.queueMutex_);
+        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
         this->orxonoxInterpreterBundle_.queue_.push_front(command);
     }
@@ -247,5 +272,5 @@
     std::string TclThreadManager::popCommandFromQueue()
     {
-        boost::mutex::scoped_lock lock(this->orxonoxInterpreterBundle_.queueMutex_);
+        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
         std::string temp = this->orxonoxInterpreterBundle_.queue_.front();
         this->orxonoxInterpreterBundle_.queue_.pop_front();
@@ -256,5 +281,5 @@
     bool TclThreadManager::queueIsEmpty()
     {
-        boost::mutex::scoped_lock lock(this->orxonoxInterpreterBundle_.queueMutex_);
+        boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_);
         return this->orxonoxInterpreterBundle_.queue_.empty();
     }
@@ -265,8 +290,8 @@
         if (bundle)
         {
-            boost::mutex::scoped_lock lock(bundle->queueMutex_);
+            boost::mutex::scoped_lock queue_lock(bundle->queueMutex_);
             if (bundle->queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)
             {
-                this->forceCommandToFrontOfQueue("error Error: Queue of Tcl-interpreter " + getConvertedValue<unsigned int, std::string>(threadID) + " is full, couldn't add command.");
+                this->error("Error: Queue of Tcl-interpreter " + getConvertedValue<unsigned int, std::string>(threadID) + " is full, couldn't add command.");
                 return;
             }
@@ -281,5 +306,5 @@
         if (bundle)
         {
-            boost::mutex::scoped_lock lock(bundle->queueMutex_);
+            boost::mutex::scoped_lock queue_lock(bundle->queueMutex_);
             std::string temp = bundle->queue_.front();
             bundle->queue_.pop_front();
@@ -294,5 +319,5 @@
         if (bundle)
         {
-            boost::mutex::scoped_lock lock(bundle->queueMutex_);
+            boost::mutex::scoped_lock queue_lock(bundle->queueMutex_);
             return bundle->queue_.empty();
         }
@@ -305,8 +330,8 @@
             return false;
 
-        boost::mutex::scoped_lock lock(target->queriersMutex_);
-
-        {
-            boost::mutex::scoped_lock lock(querier->queriersMutex_);
+        boost::mutex::scoped_lock queriers_lock(target->queriersMutex_);
+
+        {
+            boost::mutex::scoped_lock queriers_lock(querier->queriersMutex_);
             target->queriers_.insert(target->queriers_.end(), querier->queriers_.begin(), querier->queriers_.end());
         }
@@ -316,5 +341,5 @@
         if (std::find(target->queriers_.begin(), target->queriers_.end(), target->id_) != target->queriers_.end())
         {
-            this->forceCommandToFrontOfQueue("error Error: Circular query (" + this->dumpList(target->queriers_) + " -> " + getConvertedValue<unsigned int, std::string>(target->id_) + "), couldn't query Tcl-interpreter with ID " + getConvertedValue<unsigned int, std::string>(target->id_) + " from other interpreter with ID " + getConvertedValue<unsigned int, std::string>(querier->id_) + ".");
+            this->error("Error: Circular query (" + this->dumpList(target->queriers_) + " -> " + getConvertedValue<unsigned int, std::string>(target->id_) + "), couldn't query Tcl-interpreter with ID " + getConvertedValue<unsigned int, std::string>(target->id_) + " from other interpreter with ID " + getConvertedValue<unsigned int, std::string>(querier->id_) + ".");
             return false;
         }
@@ -331,9 +356,9 @@
             if (this->updateQueriersList(querier, &this->orxonoxInterpreterBundle_))
             {
-                boost::mutex::scoped_lock lock(this->orxonoxInterpreterBundle_.interpreterMutex_);
-                this->orxonoxEvalCondition_.wait(lock);
+                boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_.interpreterMutex_);
+                this->orxonoxEvalCondition_.wait(interpreter_lock);
 
                 if (!CommandExecutor::execute(command, false))
-                    this->forceCommandToFrontOfQueue("error Error: Can't execute command \"" + command + "\"!");
+                    this->error("Error: Can't execute command \"" + command + "\"!");
 
                 if (CommandExecutor::getLastEvaluation().hasReturnvalue())
@@ -341,5 +366,5 @@
             }
 
-            boost::mutex::scoped_lock lock(this->orxonoxInterpreterBundle_.queriersMutex_);
+            boost::mutex::scoped_lock queriers_lock(this->orxonoxInterpreterBundle_.queriersMutex_);
             this->orxonoxInterpreterBundle_.queriers_.clear();
         }
@@ -349,5 +374,10 @@
     std::string TclThreadManager::evalQuery(unsigned int querierID, unsigned int threadID, const std::string& command)
     {
-        TclInterpreterBundle* target = this->getInterpreterBundle(threadID);
+        TclInterpreterBundle* target = 0;
+        if (threadID)
+            target = this->getInterpreterBundle(threadID);
+        else
+            target = &this->orxonoxInterpreterBundle_;
+
         std::string output = "";
         if (target)
@@ -363,13 +393,13 @@
                 if (this->updateQueriersList(querier, target))
                 {
-                    boost::mutex::scoped_lock lock(target->interpreterMutex_, boost::defer_lock_t());
+                    boost::mutex::scoped_lock interpreter_lock(target->interpreterMutex_, boost::defer_lock_t());
                     bool successfullyLocked = false;
                     try
                     {
                         if (querierID == 0 || std::find(querier->queriers_.begin(), querier->queriers_.end(), (unsigned int)0) != querier->queriers_.end())
-                            successfullyLocked = lock.try_lock();
+                            successfullyLocked = interpreter_lock.try_lock();
                         else
                         {
-                            while (!lock.try_lock())
+                            while (!interpreter_lock.try_lock())
                                 boost::this_thread::yield();
 
@@ -383,15 +413,15 @@
                         {   output = (std::string)target->interpreter_->eval(command);   }
                         catch (Tcl::tcl_error const &e)
-                        {   this->forceCommandToFrontOfQueue("error Tcl error: " + (std::string)e.what());   }
+                        {   this->error("Tcl error: " + (std::string)e.what());   }
                         catch (std::exception const &e)
-                        {   this->forceCommandToFrontOfQueue("error Error while executing Tcl: " + (std::string)e.what());   }
+                        {   this->error("Error while executing Tcl: " + (std::string)e.what());   }
                     }
                     else
                     {
-                        this->forceCommandToFrontOfQueue("error Error: Couldn't query Tcl-interpreter with ID " + getConvertedValue<unsigned int, std::string>(threadID) + ", interpreter is busy right now.");
+                        this->error("Error: Couldn't query Tcl-interpreter with ID " + getConvertedValue<unsigned int, std::string>(threadID) + ", interpreter is busy right now.");
                     }
                 }
 
-                boost::mutex::scoped_lock lock(target->queriersMutex_);
+                boost::mutex::scoped_lock queriers_lock(target->queriersMutex_);
                 target->queriers_.clear();
             }
@@ -408,18 +438,16 @@
 
         {
-            boost::mutex::scoped_lock lock(this->bundlesMutex_);
+            boost::mutex::scoped_lock bundles_lock(this->bundlesMutex_);
             for (std::map<unsigned int, TclInterpreterBundle*>::iterator it = this->interpreterBundles_.begin(); it != this->interpreterBundles_.end(); ++it)
             {
-                boost::mutex::scoped_lock lock((*it).second->queueMutex_);
+                boost::mutex::scoped_lock queue_lock((*it).second->queueMutex_);
                 if (!(*it).second->queue_.empty())
                 {
                     std::string command = (*it).second->queue_.front();
                     (*it).second->queue_.pop_front();
-
                     {
-                        boost::mutex::scoped_lock lock((*it).second->finishedMutex_);
+                        boost::mutex::scoped_lock finished_lock((*it).second->finishedMutex_);
                         (*it).second->finished_ = false;
                     }
-
                     boost::thread(boost::bind(&tclThread, (*it).second, command));
                 }
@@ -428,5 +456,5 @@
 
         {
-            boost::mutex::scoped_lock lock(this->orxonoxInterpreterBundle_.interpreterMutex_);
+            boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_.interpreterMutex_);
             unsigned long maxtime = (unsigned long)(dt * 1000000 * TCLTHREADMANAGER_MAX_CPU_USAGE);
             Ogre::Timer timer;
@@ -442,19 +470,19 @@
     void tclThread(TclInterpreterBundle* interpreterBundle, std::string command)
     {
+        boost::mutex::scoped_lock interpreter_lock(interpreterBundle->interpreterMutex_);
         try
         {
-            boost::mutex::scoped_lock lock(interpreterBundle->interpreterMutex_);
             interpreterBundle->interpreter_->eval(command);
         }
         catch (Tcl::tcl_error const &e)
         {
-            TclThreadManager::getInstance().forceCommandToFrontOfQueue("error Tcl (ID " + getConvertedValue<unsigned int, std::string>(interpreterBundle->id_) + ") error: " + e.what());
+            TclThreadManager::getInstance().error("Tcl (ID " + getConvertedValue<unsigned int, std::string>(interpreterBundle->id_) + ") error: " + e.what());
         }
         catch (std::exception const &e)
         {
-            TclThreadManager::getInstance().forceCommandToFrontOfQueue("error Error while executing Tcl (ID " + getConvertedValue<unsigned int, std::string>(interpreterBundle->id_) + "): " + e.what());
-        }
-
-        boost::mutex::scoped_lock lock(interpreterBundle->finishedMutex_);
+            TclThreadManager::getInstance().error("Error while executing Tcl (ID " + getConvertedValue<unsigned int, std::string>(interpreterBundle->id_) + "): " + e.what());
+        }
+
+        boost::mutex::scoped_lock finished_lock(interpreterBundle->finishedMutex_);
         interpreterBundle->finished_ = true;
         interpreterBundle->finishedCondition_.notify_all();
Index: code/branches/console/src/core/TclThreadManager.h
===================================================================
--- code/branches/console/src/core/TclThreadManager.h	(revision 1255)
+++ code/branches/console/src/core/TclThreadManager.h	(revision 1257)
@@ -85,4 +85,5 @@
             TclInterpreterBundle* getInterpreterBundle(unsigned int threadID);
             std::string dumpList(const std::list<unsigned int>& list);
+            void error(const std::string& error);
 
             void pushCommandToQueue(const std::string& command);
@@ -113,4 +114,5 @@
             boost::condition fullQueueCondition_;
             boost::condition orxonoxEvalCondition_;
+            boost::thread::id threadID_;
     };
 
