Changeset 3304 for code/trunk/src/core/TclThreadManager.cc
- Timestamp:
- Jul 18, 2009, 6:23:31 PM (16 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/netp6 (added) merged: 3215,3226-3227,3229-3231,3240,3242,3251-3252,3256,3258-3259,3263-3264,3267-3268,3277,3283-3284,3289,3298-3299,3302
- Property svn:mergeinfo changed
-
code/trunk/src/core/TclThreadManager.cc
r3301 r3304 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 60 91 TclThreadManager::TclThreadManager(Tcl::interpreter* interpreter) 92 : orxonoxInterpreterBundle_(new TclInterpreterBundle()) 61 93 { 62 94 RegisterRootObject(TclThreadManager); … … 66 98 67 99 this->threadCounter_ = 0; 68 this->orxonoxInterpreterBundle_.id_ = 0; 69 this->orxonoxInterpreterBundle_.interpreter_ = interpreter; 70 #if (BOOST_VERSION >= 103500) 71 this->threadID_ = boost::this_thread::get_id(); 72 #else 73 // 74 #endif 100 this->orxonoxInterpreterBundle_->id_ = 0; 101 this->orxonoxInterpreterBundle_->interpreter_ = interpreter; 102 threadID_g = boost::this_thread::get_id(); 75 103 } 76 104 … … 79 107 unsigned int threadID; 80 108 { 81 boost::mutex::scoped_lock bundles_lock( this->bundlesMutex_);109 boost::mutex::scoped_lock bundles_lock(bundlesMutex_g); 82 110 if (this->interpreterBundles_.begin() == this->interpreterBundles_.end()) 83 111 return; … … 87 115 this->destroy(threadID); 88 116 117 delete this->orxonoxInterpreterBundle_; 118 89 119 singletonRef_s = 0; 90 120 } … … 92 122 unsigned int TclThreadManager::create() 93 123 { 94 boost::mutex::scoped_lock bundles_lock( TclThreadManager::getInstance().bundlesMutex_);124 boost::mutex::scoped_lock bundles_lock(bundlesMutex_g); 95 125 TclThreadManager::getInstance().threadCounter_++; 96 126 std::string name = multi_cast<std::string>(TclThreadManager::getInstance().threadCounter_); … … 132 162 if (bundle->finished_) 133 163 { 134 boost::mutex::scoped_lock bundles_lock(TclThreadManager::getInstance().bundlesMutex_); 135 #if (BOOST_VERSION >= 103500) 164 boost::mutex::scoped_lock bundles_lock(bundlesMutex_g); 136 165 boost::mutex::scoped_try_lock interpreter_lock(bundle->interpreterMutex_); 137 #else138 boost::try_mutex::scoped_try_lock interpreter_lock(bundle->interpreterMutex_);139 #endif140 166 try 141 167 { 142 168 while (!interpreter_lock.try_lock()) 143 169 { 144 TclThreadManager::getInstance().orxonoxEvalCondition_.notify_one(); 145 #if (BOOST_VERSION >= 103500) 170 orxonoxEvalCondition_g.notify_one(); 146 171 boost::this_thread::yield(); 147 #else148 boost::thread::yield();149 #endif150 172 } 151 173 } catch (...) {} … … 157 179 } 158 180 159 TclThreadManager::getInstance().orxonoxEvalCondition_.notify_one(); 160 #if (BOOST_VERSION >= 103500) 181 orxonoxEvalCondition_g.notify_one(); 161 182 boost::this_thread::yield(); 162 #else163 boost::thread::yield();164 #endif165 183 } 166 184 … … 181 199 std::string TclThreadManager::query(unsigned int threadID, const std::string& command) 182 200 { 183 return TclThreadManager::getInstance().evalQuery(TclThreadManager::getInstance().orxonoxInterpreterBundle_ .id_, threadID, command);201 return TclThreadManager::getInstance().evalQuery(TclThreadManager::getInstance().orxonoxInterpreterBundle_->id_, threadID, command); 184 202 } 185 203 … … 191 209 output += "\t\t"; 192 210 { 193 boost::mutex::scoped_lock queue_lock(TclThreadManager::getInstance().orxonoxInterpreterBundle_ .queueMutex_);194 output += multi_cast<std::string>(TclThreadManager::getInstance().orxonoxInterpreterBundle_ .queue_.size());211 boost::mutex::scoped_lock queue_lock(TclThreadManager::getInstance().orxonoxInterpreterBundle_->queueMutex_); 212 output += multi_cast<std::string>(TclThreadManager::getInstance().orxonoxInterpreterBundle_->queue_.size()); 195 213 } 196 214 output += "\t\t"; … … 198 216 COUT(0) << output << std::endl; 199 217 200 boost::mutex::scoped_lock bundles_lock( TclThreadManager::getInstance().bundlesMutex_);218 boost::mutex::scoped_lock bundles_lock(bundlesMutex_g); 201 219 for (std::map<unsigned int, TclInterpreterBundle*>::const_iterator it = TclThreadManager::getInstance().interpreterBundles_.begin(); it != TclThreadManager::getInstance().interpreterBundles_.end(); ++it) 202 220 { … … 209 227 output += "\t\t"; 210 228 { 211 #if (BOOST_VERSION >= 103500)212 229 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 230 if (interpreter_lock.try_lock()) 217 231 output += "ready"; … … 228 242 if (threadID == 0) 229 243 { 230 bundle = &TclThreadManager::getInstance().orxonoxInterpreterBundle_;244 bundle = TclThreadManager::getInstance().orxonoxInterpreterBundle_; 231 245 COUT(0) << "Queue dump of Orxonox:" << std::endl; 232 246 } … … 254 268 TclInterpreterBundle* bundle = 0; 255 269 if (threadID == 0) 256 bundle = &TclThreadManager::getInstance().orxonoxInterpreterBundle_;270 bundle = TclThreadManager::getInstance().orxonoxInterpreterBundle_; 257 271 else 258 272 if (!(bundle = TclThreadManager::getInstance().getInterpreterBundle(threadID))) … … 334 348 TclInterpreterBundle* TclThreadManager::getInterpreterBundle(unsigned int threadID) 335 349 { 336 boost::mutex::scoped_lock bundles_lock( this->bundlesMutex_);350 boost::mutex::scoped_lock bundles_lock(bundlesMutex_g); 337 351 std::map<unsigned int, TclInterpreterBundle*>::iterator it = this->interpreterBundles_.find(threadID); 338 352 if (it != this->interpreterBundles_.end()) … … 347 361 } 348 362 363 Tcl::interpreter* TclThreadManager::getTclInterpreter(unsigned int threadID) 364 { 365 return this->getInterpreterBundle(threadID)->interpreter_; 366 } 367 349 368 std::string TclThreadManager::dumpList(const std::list<unsigned int>& list) 350 369 { … … 362 381 void TclThreadManager::error(const std::string& error) 363 382 { 364 #if (BOOST_VERSION >= 103500) 365 if (boost::this_thread::get_id() != this->threadID_) 366 #else 367 if (boost::thread() != this->threadID_) 368 #endif 369 { 370 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_); 371 if (this->orxonoxInterpreterBundle_.queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH) 372 { 373 #if (BOOST_VERSION >= 103500) 383 if (boost::this_thread::get_id() != threadID_g) 384 { 385 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_); 386 if (this->orxonoxInterpreterBundle_->queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH) 387 { 374 388 boost::this_thread::yield(); 375 #else376 boost::thread::yield();377 #endif378 389 return; 379 390 } … … 385 396 void TclThreadManager::debug(const std::string& error) 386 397 { 387 #if (BOOST_VERSION >= 103500) 388 if (boost::this_thread::get_id() != this->threadID_) 389 #else 390 if (boost::thread() != this->threadID_) 391 #endif 392 { 393 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_.queueMutex_); 394 if (this->orxonoxInterpreterBundle_.queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH) 395 { 396 #if (BOOST_VERSION >= 103500) 398 if (boost::this_thread::get_id() != threadID_g) 399 { 400 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_); 401 if (this->orxonoxInterpreterBundle_->queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH) 402 { 397 403 boost::this_thread::yield(); 398 #else399 boost::thread::yield();400 #endif401 404 return; 402 405 } … … 408 411 void TclThreadManager::pushCommandToQueue(const std::string& command) 409 412 { 410 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_ .queueMutex_);411 while (this->orxonoxInterpreterBundle_ .queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH)412 this->fullQueueCondition_.wait(queue_lock);413 414 this->orxonoxInterpreterBundle_ .queue_.push_back(command);413 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_); 414 while (this->orxonoxInterpreterBundle_->queue_.size() >= TCLTHREADMANAGER_MAX_QUEUE_LENGTH) 415 fullQueueCondition_g.wait(queue_lock); 416 417 this->orxonoxInterpreterBundle_->queue_.push_back(command); 415 418 } 416 419 417 420 void TclThreadManager::forceCommandToFrontOfQueue(const std::string& command) 418 421 { 419 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_ .queueMutex_);420 this->orxonoxInterpreterBundle_ .queue_.push_front(command);422 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_); 423 this->orxonoxInterpreterBundle_->queue_.push_front(command); 421 424 } 422 425 423 426 std::string TclThreadManager::popCommandFromQueue() 424 427 { 425 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_ .queueMutex_);426 std::string temp = this->orxonoxInterpreterBundle_ .queue_.front();427 this->orxonoxInterpreterBundle_ .queue_.pop_front();428 this->fullQueueCondition_.notify_one();428 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_); 429 std::string temp = this->orxonoxInterpreterBundle_->queue_.front(); 430 this->orxonoxInterpreterBundle_->queue_.pop_front(); 431 fullQueueCondition_g.notify_one(); 429 432 return temp; 430 433 } … … 432 435 bool TclThreadManager::queueIsEmpty() 433 436 { 434 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_ .queueMutex_);435 return this->orxonoxInterpreterBundle_ .queue_.empty();437 boost::mutex::scoped_lock queue_lock(this->orxonoxInterpreterBundle_->queueMutex_); 438 return this->orxonoxInterpreterBundle_->queue_.empty(); 436 439 } 437 440 … … 505 508 if (querier) 506 509 { 507 if (this->updateQueriersList(querier, &this->orxonoxInterpreterBundle_)) 508 { 509 #if (BOOST_VERSION >= 103500) 510 boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_.interpreterMutex_); 511 #else 512 boost::try_mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_.interpreterMutex_); 513 #endif 514 this->orxonoxEvalCondition_.wait(interpreter_lock); 510 if (this->updateQueriersList(querier, this->orxonoxInterpreterBundle_)) 511 { 512 boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_->interpreterMutex_); 513 orxonoxEvalCondition_g.wait(interpreter_lock); 515 514 516 515 if (!CommandExecutor::execute(command, false)) … … 521 520 } 522 521 523 boost::mutex::scoped_lock queriers_lock(this->orxonoxInterpreterBundle_ .queriersMutex_);524 this->orxonoxInterpreterBundle_ .queriers_.clear();522 boost::mutex::scoped_lock queriers_lock(this->orxonoxInterpreterBundle_->queriersMutex_); 523 this->orxonoxInterpreterBundle_->queriers_.clear(); 525 524 } 526 525 return output; … … 533 532 target = this->getInterpreterBundle(threadID); 534 533 else 535 target = &this->orxonoxInterpreterBundle_;534 target = this->orxonoxInterpreterBundle_; 536 535 537 536 std::string output = ""; … … 542 541 querier = this->getInterpreterBundle(querierID); 543 542 else 544 querier = &this->orxonoxInterpreterBundle_;543 querier = this->orxonoxInterpreterBundle_; 545 544 546 545 if (querier) … … 548 547 if (this->updateQueriersList(querier, target)) 549 548 { 550 #if (BOOST_VERSION >= 103500)551 549 boost::mutex::scoped_try_lock interpreter_lock(target->interpreterMutex_); 552 #else553 boost::try_mutex::scoped_try_lock interpreter_lock(target->interpreterMutex_);554 #endif555 550 bool successfullyLocked = false; 556 551 try … … 562 557 while (!interpreter_lock.try_lock()) 563 558 { 564 #if (BOOST_VERSION >= 103500)565 559 boost::this_thread::yield(); 566 #else567 boost::thread::yield();568 #endif569 560 } 570 561 … … 599 590 { 600 591 { 601 this->orxonoxEvalCondition_.notify_one(); 602 #if (BOOST_VERSION >= 103500) 592 orxonoxEvalCondition_g.notify_one(); 603 593 boost::this_thread::yield(); 604 #else 605 boost::thread::yield(); 606 #endif 607 } 608 609 { 610 boost::mutex::scoped_lock bundles_lock(this->bundlesMutex_); 594 } 595 596 { 597 boost::mutex::scoped_lock bundles_lock(bundlesMutex_g); 611 598 for (std::map<unsigned int, TclInterpreterBundle*>::iterator it = this->interpreterBundles_.begin(); it != this->interpreterBundles_.end(); ++it) 612 599 { … … 626 613 627 614 { 628 #if (BOOST_VERSION >= 103500) 629 boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_.interpreterMutex_); 630 #else 631 boost::try_mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_.interpreterMutex_); 632 #endif 615 boost::mutex::scoped_lock interpreter_lock(this->orxonoxInterpreterBundle_->interpreterMutex_); 633 616 unsigned long maxtime = static_cast<unsigned long>(time.getDeltaTime() * 1000000 * TCLTHREADMANAGER_MAX_CPU_USAGE); 634 617 Ogre::Timer timer; … … 644 627 std::list<unsigned int> TclThreadManager::getThreadList() const 645 628 { 646 boost::mutex::scoped_lock bundles_lock( TclThreadManager::getInstance().bundlesMutex_);629 boost::mutex::scoped_lock bundles_lock(bundlesMutex_g); 647 630 std::list<unsigned int> threads; 648 631 for (std::map<unsigned int, TclInterpreterBundle*>::const_iterator it = this->interpreterBundles_.begin(); it != this->interpreterBundles_.end(); ++it) … … 654 637 { 655 638 TclThreadManager::getInstance().debug("TclThread_execute: " + command); 656 #if (BOOST_VERSION >= 103500)657 639 boost::mutex::scoped_lock interpreter_lock(interpreterBundle->interpreterMutex_); 658 #else659 boost::try_mutex::scoped_lock interpreter_lock(interpreterBundle->interpreterMutex_);660 #endif661 640 try 662 641 {
Note: See TracChangeset
for help on using the changeset viewer.