Changeset 3264 for code/branches
- Timestamp:
- Jul 1, 2009, 11:22:03 AM (15 years ago)
- Location:
- code/branches/netp6/src/core
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/netp6/src/core/CorePrecompiledHeaders.h
r3214 r3264 60 60 #include <boost/shared_ptr.hpp> // 12 61 61 #include <boost/preprocessor/cat.hpp> // 12 62 // Included by both filesystem and thread but still relatively small63 #include <boost/iterator/iterator_facade.hpp> // 1064 62 65 63 // Just in case some header included windows.h -
code/branches/netp6/src/core/CorePrereqs.h
r3240 r3264 150 150 class SubclassIdentifier; 151 151 class TclBind; 152 struct TclInterpreterBundle;153 152 class TclThreadManager; 154 153 class Template; … … 207 206 template <class String, class Traits> class basic_path; 208 207 typedef basic_path<std::string, path_traits> path; 209 } 208 } 210 209 class thread; 211 210 class mutex; -
code/branches/netp6/src/core/IRC.cc
r3196 r3264 47 47 { 48 48 RegisterRootObject(IRC); 49 this-> bundle_ = 0;49 this->interpreter_ = 0; 50 50 } 51 51 … … 54 54 unsigned int threadID = (unsigned int)IRC_TCL_THREADID; 55 55 TclThreadManager::createID(threadID); 56 this-> bundle_ = TclThreadManager::getInstance().getInterpreterBundle(threadID);56 this->interpreter_ = TclThreadManager::getInstance().getTclInterpreter(threadID); 57 57 58 58 try 59 59 { 60 this-> bundle_->interpreter_->def("orxonox::irc::say", IRC::tcl_say, Tcl::variadic());61 this-> bundle_->interpreter_->def("orxonox::irc::privmsg", IRC::tcl_privmsg, Tcl::variadic());62 this-> bundle_->interpreter_->def("orxonox::irc::action", IRC::tcl_action, Tcl::variadic());63 this-> bundle_->interpreter_->def("orxonox::irc::info", IRC::tcl_info, Tcl::variadic());60 this->interpreter_->def("orxonox::irc::say", IRC::tcl_say, Tcl::variadic()); 61 this->interpreter_->def("orxonox::irc::privmsg", IRC::tcl_privmsg, Tcl::variadic()); 62 this->interpreter_->def("orxonox::irc::action", IRC::tcl_action, Tcl::variadic()); 63 this->interpreter_->def("orxonox::irc::info", IRC::tcl_info, Tcl::variadic()); 64 64 } 65 65 catch (Tcl::tcl_error const &e) … … 68 68 { COUT(1) << "Error while initializing Tcl (IRC): " << e.what(); } 69 69 70 this->nickname_ = "orx" + getConvertedValue<int, std::string>( (unsigned int)rand());70 this->nickname_ = "orx" + getConvertedValue<int, std::string>(static_cast<unsigned int>(rand())); 71 71 TclThreadManager::execute(threadID, "set nickname " + this->nickname_); 72 72 TclThreadManager::execute(threadID, "source irc.tcl"); … … 81 81 bool IRC::eval(const std::string& command) 82 82 { 83 if (!IRC::getInstance(). bundle_)83 if (!IRC::getInstance().interpreter_) 84 84 { 85 85 IRC::getInstance().initialize(); … … 90 90 try 91 91 { 92 IRC::getInstance(). bundle_->interpreter_->eval(command);92 IRC::getInstance().interpreter_->eval(command); 93 93 return true; 94 94 } -
code/branches/netp6/src/core/IRC.h
r3196 r3264 59 59 ~IRC() {} 60 60 61 Tcl InterpreterBundle* bundle_;61 Tcl::interpreter* interpreter_; 62 62 std::string nickname_; 63 63 }; -
code/branches/netp6/src/core/TclThreadManager.cc
r3196 r3264 30 30 31 31 #include <boost/bind.hpp> 32 #include <boost/thread/condition.hpp> 33 #include <boost/thread/mutex.hpp> 34 #include <boost/thread/thread.hpp> 32 35 #include <OgreTimer.h> 33 36 #include <cpptcl/cpptcl.h> … … 56 59 SetConsoleCommand(TclThreadManager, flush, false).argumentCompleter(0, autocompletion::tclthreads()); 57 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 58 89 TclThreadManager* TclThreadManager::singletonRef_s = 0; 59 90 … … 66 97 67 98 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 99 this->orxonoxInterpreterBundle_->id_ = 0; 100 this->orxonoxInterpreterBundle_->interpreter_ = interpreter; 101 threadID_g = boost::this_thread::get_id(); 75 102 } 76 103 … … 79 106 unsigned int threadID; 80 107 { 81 boost::mutex::scoped_lock bundles_lock( this->bundlesMutex_);108 boost::mutex::scoped_lock bundles_lock(bundlesMutex_g); 82 109 if (this->interpreterBundles_.begin() == this->interpreterBundles_.end()) 83 110 return; … … 92 119 unsigned int TclThreadManager::create() 93 120 { 94 boost::mutex::scoped_lock bundles_lock( TclThreadManager::getInstance().bundlesMutex_);121 boost::mutex::scoped_lock bundles_lock(bundlesMutex_g); 95 122 TclThreadManager::getInstance().threadCounter_++; 96 123 std::string name = getConvertedValue<unsigned int, std::string>(TclThreadManager::getInstance().threadCounter_); … … 132 159 if (bundle->finished_) 133 160 { 134 boost::mutex::scoped_lock bundles_lock(TclThreadManager::getInstance().bundlesMutex_); 135 #if (BOOST_VERSION >= 103500) 161 boost::mutex::scoped_lock bundles_lock(bundlesMutex_g); 136 162 boost::mutex::scoped_try_lock interpreter_lock(bundle->interpreterMutex_); 137 #else138 boost::try_mutex::scoped_try_lock interpreter_lock(bundle->interpreterMutex_);139 #endif140 163 try 141 164 { 142 165 while (!interpreter_lock.try_lock()) 143 166 { 144 TclThreadManager::getInstance().orxonoxEvalCondition_.notify_one(); 145 #if (BOOST_VERSION >= 103500) 167 orxonoxEvalCondition_g.notify_one(); 146 168 boost::this_thread::yield(); 147 #else148 boost::thread::yield();149 #endif150 169 } 151 170 } catch (...) {} … … 157 176 } 158 177 159 TclThreadManager::getInstance().orxonoxEvalCondition_.notify_one(); 160 #if (BOOST_VERSION >= 103500) 178 orxonoxEvalCondition_g.notify_one(); 161 179 boost::this_thread::yield(); 162 #else163 boost::thread::yield();164 #endif165 180 } 166 181 … … 181 196 std::string TclThreadManager::query(unsigned int threadID, const std::string& command) 182 197 { 183 return TclThreadManager::getInstance().evalQuery(TclThreadManager::getInstance().orxonoxInterpreterBundle_ .id_, threadID, command);198 return TclThreadManager::getInstance().evalQuery(TclThreadManager::getInstance().orxonoxInterpreterBundle_->id_, threadID, command); 184 199 } 185 200 … … 191 206 output += "\t\t"; 192 207 { 193 boost::mutex::scoped_lock queue_lock(TclThreadManager::getInstance().orxonoxInterpreterBundle_ .queueMutex_);194 output += getConvertedValue<unsigned int, std::string>(TclThreadManager::getInstance().orxonoxInterpreterBundle_ .queue_.size());208 boost::mutex::scoped_lock queue_lock(TclThreadManager::getInstance().orxonoxInterpreterBundle_->queueMutex_); 209 output += getConvertedValue<unsigned int, std::string>(TclThreadManager::getInstance().orxonoxInterpreterBundle_->queue_.size()); 195 210 } 196 211 output += "\t\t"; … … 198 213 COUT(0) << output << std::endl; 199 214 200 boost::mutex::scoped_lock bundles_lock( TclThreadManager::getInstance().bundlesMutex_);215 boost::mutex::scoped_lock bundles_lock(bundlesMutex_g); 201 216 for (std::map<unsigned int, TclInterpreterBundle*>::const_iterator it = TclThreadManager::getInstance().interpreterBundles_.begin(); it != TclThreadManager::getInstance().interpreterBundles_.end(); ++it) 202 217 { … … 209 224 output += "\t\t"; 210 225 { 211 #if (BOOST_VERSION >= 103500)212 226 boost::mutex::scoped_try_lock interpreter_lock((*it).second->interpreterMutex_); 213 #else214 boost::try_mutex::scoped_try_lock interpreter_lock((*it).second->interpreterMutex_);215 #endif216 227 if (interpreter_lock.try_lock()) 217 228 output += "ready"; … … 228 239 if (threadID == 0) 229 240 { 230 bundle = &TclThreadManager::getInstance().orxonoxInterpreterBundle_;241 bundle = TclThreadManager::getInstance().orxonoxInterpreterBundle_; 231 242 COUT(0) << "Queue dump of Orxonox:" << std::endl; 232 243 } … … 254 265 TclInterpreterBundle* bundle = 0; 255 266 if (threadID == 0) 256 bundle = &TclThreadManager::getInstance().orxonoxInterpreterBundle_;267 bundle = TclThreadManager::getInstance().orxonoxInterpreterBundle_; 257 268 else 258 269 if (!(bundle = TclThreadManager::getInstance().getInterpreterBundle(threadID))) … … 334 345 TclInterpreterBundle* TclThreadManager::getInterpreterBundle(unsigned int threadID) 335 346 { 336 boost::mutex::scoped_lock bundles_lock( this->bundlesMutex_);347 boost::mutex::scoped_lock bundles_lock(bundlesMutex_g); 337 348 std::map<unsigned int, TclInterpreterBundle*>::iterator it = this->interpreterBundles_.find(threadID); 338 349 if (it != this->interpreterBundles_.end()) … … 347 358 } 348 359 360 Tcl::interpreter* TclThreadManager::getTclInterpreter(unsigned int threadID) 361 { 362 return this->getInterpreterBundle(threadID)->interpreter_; 363 } 364 349 365 std::string TclThreadManager::dumpList(const std::list<unsigned int>& list) 350 366 { … … 362 378 void TclThreadManager::error(const std::string& error) 363 379 { 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) 380 if (boost::this_thread::get_id() != threadID_g) 381 { 382 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_); 383 if (this->orxonoxInterpreterBundle_->queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH) 384 { 374 385 boost::this_thread::yield(); 375 #else376 boost::thread::yield();377 #endif378 386 return; 379 387 } … … 385 393 void TclThreadManager::debug(const std::string& error) 386 394 { 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) 395 if (boost::this_thread::get_id() != threadID_g) 396 { 397 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_); 398 if (this->orxonoxInterpreterBundle_->queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH) 399 { 397 400 boost::this_thread::yield(); 398 #else399 boost::thread::yield();400 #endif401 401 return; 402 402 } … … 408 408 void TclThreadManager::pushCommandToQueue(const std::string& command) 409 409 { 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);410 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_); 411 while (this->orxonoxInterpreterBundle_->queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH) 412 fullQueueCondition_g.wait(queue_lock); 413 414 this->orxonoxInterpreterBundle_->queue_.push_back(command); 415 415 } 416 416 417 417 void TclThreadManager::forceCommandToFrontOfQueue(const std::string& command) 418 418 { 419 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_ .queueMutex_);420 this->orxonoxInterpreterBundle_ .queue_.push_front(command);419 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_); 420 this->orxonoxInterpreterBundle_->queue_.push_front(command); 421 421 } 422 422 423 423 std::string TclThreadManager::popCommandFromQueue() 424 424 { 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();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 fullQueueCondition_g.notify_one(); 429 429 return temp; 430 430 } … … 432 432 bool TclThreadManager::queueIsEmpty() 433 433 { 434 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_ .queueMutex_);435 return this->orxonoxInterpreterBundle_ .queue_.empty();434 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_); 435 return this->orxonoxInterpreterBundle_->queue_.empty(); 436 436 } 437 437 … … 505 505 if (querier) 506 506 { 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); 507 if (this->updateQueriersList(querier, this->orxonoxInterpreterBundle_)) 508 { 509 boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_->interpreterMutex_); 510 orxonoxEvalCondition_g.wait(interpreter_lock); 515 511 516 512 if (!CommandExecutor::execute(command, false)) … … 521 517 } 522 518 523 boost::mutex::scoped_lock queriers_lock(this->orxonoxInterpreterBundle_ .queriersMutex_);524 this->orxonoxInterpreterBundle_ .queriers_.clear();519 boost::mutex::scoped_lock queriers_lock(this->orxonoxInterpreterBundle_->queriersMutex_); 520 this->orxonoxInterpreterBundle_->queriers_.clear(); 525 521 } 526 522 return output; … … 533 529 target = this->getInterpreterBundle(threadID); 534 530 else 535 target = &this->orxonoxInterpreterBundle_;531 target = this->orxonoxInterpreterBundle_; 536 532 537 533 std::string output = ""; … … 542 538 querier = this->getInterpreterBundle(querierID); 543 539 else 544 querier = &this->orxonoxInterpreterBundle_;540 querier = this->orxonoxInterpreterBundle_; 545 541 546 542 if (querier) … … 548 544 if (this->updateQueriersList(querier, target)) 549 545 { 550 #if (BOOST_VERSION >= 103500)551 546 boost::mutex::scoped_try_lock interpreter_lock(target->interpreterMutex_); 552 #else553 boost::try_mutex::scoped_try_lock interpreter_lock(target->interpreterMutex_);554 #endif555 547 bool successfullyLocked = false; 556 548 try … … 562 554 while (!interpreter_lock.try_lock()) 563 555 { 564 #if (BOOST_VERSION >= 103500)565 556 boost::this_thread::yield(); 566 #else567 boost::thread::yield();568 #endif569 557 } 570 558 … … 599 587 { 600 588 { 601 this->orxonoxEvalCondition_.notify_one(); 602 #if (BOOST_VERSION >= 103500) 589 orxonoxEvalCondition_g.notify_one(); 603 590 boost::this_thread::yield(); 604 #else 605 boost::thread::yield(); 606 #endif 607 } 608 609 { 610 boost::mutex::scoped_lock bundles_lock(this->bundlesMutex_); 591 } 592 593 { 594 boost::mutex::scoped_lock bundles_lock(bundlesMutex_g); 611 595 for (std::map<unsigned int, TclInterpreterBundle*>::iterator it = this->interpreterBundles_.begin(); it != this->interpreterBundles_.end(); ++it) 612 596 { … … 626 610 627 611 { 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 612 boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_->interpreterMutex_); 633 613 unsigned long maxtime = (unsigned long)(time.getDeltaTime() * 1000000 * TCLTHREADMANAGER_MAX_CPU_USAGE); 634 614 Ogre::Timer timer; … … 644 624 std::list<unsigned int> TclThreadManager::getThreadList() const 645 625 { 646 boost::mutex::scoped_lock bundles_lock( TclThreadManager::getInstance().bundlesMutex_);626 boost::mutex::scoped_lock bundles_lock(bundlesMutex_g); 647 627 std::list<unsigned int> threads; 648 628 for (std::map<unsigned int, TclInterpreterBundle*>::const_iterator it = this->interpreterBundles_.begin(); it != this->interpreterBundles_.end(); ++it) … … 654 634 { 655 635 TclThreadManager::getInstance().debug("TclThread_execute: " + command); 656 #if (BOOST_VERSION >= 103500)657 636 boost::mutex::scoped_lock interpreter_lock(interpreterBundle->interpreterMutex_); 658 #else659 boost::try_mutex::scoped_lock interpreter_lock(interpreterBundle->interpreterMutex_);660 #endif661 637 try 662 638 { -
code/branches/netp6/src/core/TclThreadManager.h
r3196 r3264 35 35 #include <map> 36 36 #include <string> 37 #include <boost/thread/condition.hpp>38 #include <boost/thread/mutex.hpp>39 #include <boost/thread/thread.hpp>40 41 37 #include "core/OrxonoxClass.h" 42 38 43 39 namespace orxonox 44 40 { 45 struct _CoreExport TclInterpreterBundle 46 { 47 unsigned int id_; 48 49 std::list<std::string> queue_; 50 boost::mutex queueMutex_; 51 52 Tcl::interpreter* interpreter_; 53 std::string interpreterName_; 54 boost::try_mutex interpreterMutex_; 55 56 std::list<unsigned int> queriers_; 57 boost::mutex queriersMutex_; 58 59 bool running_; 60 boost::mutex runningMutex_; 61 62 bool finished_; 63 boost::mutex finishedMutex_; 64 boost::condition finishedCondition_; 65 }; 41 // Internal struct 42 struct TclInterpreterBundle; 66 43 67 44 class _CoreExport TclThreadManager : public OrxonoxClass … … 101 78 102 79 Tcl::interpreter* createNewTclInterpreter(const std::string& threadID); 80 Tcl::interpreter* getTclInterpreter(unsigned int threadID); 103 81 TclInterpreterBundle* getInterpreterBundle(unsigned int threadID); 104 82 std::string dumpList(const std::list<unsigned int>& list); … … 119 97 120 98 unsigned int threadCounter_; 121 TclInterpreterBundle orxonoxInterpreterBundle_;99 TclInterpreterBundle* orxonoxInterpreterBundle_; 122 100 std::map<unsigned int, TclInterpreterBundle*> interpreterBundles_; 123 boost::mutex bundlesMutex_;124 boost::condition fullQueueCondition_;125 boost::condition orxonoxEvalCondition_;126 #if (BOOST_VERSION >= 103500)127 boost::thread::id threadID_;128 #else129 boost::thread threadID_;130 #endif131 101 132 102 static TclThreadManager* singletonRef_s;
Note: See TracChangeset
for help on using the changeset viewer.