1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
|
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2009-2010 Titus Tscharntke (info@titusgames.de) and
// Mark Vejvoda (mark_vejvoda@hotmail.com)
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#include "simple_threads.h"
#include "util.h"
#include "platform_common.h"
#include <algorithm>
#include "conversion.h"
#include "platform_util.h"
#include "cache_manager.h"
#include "leak_dumper.h"
using namespace std;
using namespace Shared::Util;
using namespace Shared::PlatformCommon;
namespace Shared { namespace PlatformCommon {
const static int MAX_FileCRCPreCacheThread_WORKER_THREADS = 3;
const static double PAUSE_SECONDS_BETWEEN_WORKERS = 15;
string FileCRCPreCacheThread::preCacheThreadCacheLookupKey = "";
FileCRCPreCacheThread::FileCRCPreCacheThread() : BaseThread(),
mutexPauseForGame(new Mutex(CODE_AT_LINE)) {
techDataPaths.clear();
workerThreadTechPaths.clear();
preCacheWorkerThreadList.clear();
processTechCB = NULL;
pauseForGame = false;
uniqueID = "FileCRCPreCacheThread";
}
FileCRCPreCacheThread::FileCRCPreCacheThread(vector<string> techDataPaths,
vector<string> workerThreadTechPaths,
FileCRCPreCacheThreadCallbackInterface *processTechCB) :
mutexPauseForGame(new Mutex(CODE_AT_LINE)) {
this->techDataPaths = techDataPaths;
this->workerThreadTechPaths = workerThreadTechPaths;
preCacheWorkerThreadList.clear();
this->processTechCB = processTechCB;
pauseForGame = false;
uniqueID = "FileCRCPreCacheThread";
}
FileCRCPreCacheThread::~FileCRCPreCacheThread() {
bool threadControllerMode = (workerThreadTechPaths.size() == 0);
FileCRCPreCacheThread * &preCacheCRCThreadPtr = CacheManager::getCachedItem< FileCRCPreCacheThread * >(preCacheThreadCacheLookupKey);
if(preCacheCRCThreadPtr != NULL && threadControllerMode == true) {
preCacheCRCThreadPtr = NULL;
}
delete mutexPauseForGame;
mutexPauseForGame = NULL;
}
void FileCRCPreCacheThread::setPauseForGame(bool pauseForGame) {
static string mutexOwnerId = CODE_AT_LINE;
MutexSafeWrapper safeMutex(mutexPauseForGame,mutexOwnerId);
this->pauseForGame = pauseForGame;
for(unsigned int index = 0; index < preCacheWorkerThreadList.size(); ++index) {
FileCRCPreCacheThread *worker = preCacheWorkerThreadList[index];
if(worker != NULL) {
worker->setPauseForGame(this->pauseForGame);
}
}
}
bool FileCRCPreCacheThread::getPauseForGame() {
static string mutexOwnerId = CODE_AT_LINE;
MutexSafeWrapper safeMutex(mutexPauseForGame,mutexOwnerId);
return this->pauseForGame;
}
bool FileCRCPreCacheThread::canShutdown(bool deleteSelfIfShutdownDelayed) {
bool ret = (getExecutingTask() == false);
if(ret == false && deleteSelfIfShutdownDelayed == true) {
setDeleteSelfOnExecutionDone(deleteSelfIfShutdownDelayed);
deleteSelfIfRequired();
signalQuit();
}
return ret;
}
void FileCRCPreCacheThread::execute() {
{
RunningStatusSafeWrapper runningStatus(this);
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(getQuitStatus() == true) {
deleteSelfIfRequired();
return;
}
bool threadControllerMode = (workerThreadTechPaths.size() == 0);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("FILE CRC PreCache thread is running threadControllerMode = %d\n",threadControllerMode);
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"FILE CRC PreCache thread is running threadControllerMode = %d\n",threadControllerMode);
try {
if(threadControllerMode == true) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("********************** CRC Controller thread START **********************\n");
time_t elapsedTime = time(NULL);
Checksum::clearFileCache();
vector<string> techPaths;
findDirs(techDataPaths, techPaths);
if(techPaths.empty() == false) {
// Always calc megapack first so its up to date sooner
const string megapackTechtreeName = "megapack";
vector<string>::iterator iterFindMegaPack = std::find(techPaths.begin(),techPaths.end(),megapackTechtreeName);
if(iterFindMegaPack != techPaths.end()) {
techPaths.erase(iterFindMegaPack);
techPaths.insert(techPaths.begin(),megapackTechtreeName);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Found megapack techtree and placing it at the TOP of the list\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
}
unsigned int techsPerWorker = ((unsigned int)techPaths.size() / (unsigned int)MAX_FileCRCPreCacheThread_WORKER_THREADS);
if(techPaths.size() % MAX_FileCRCPreCacheThread_WORKER_THREADS != 0) {
techsPerWorker++;
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] techsPerWorker = %u, MAX_FileCRCPreCacheThread_WORKER_THREADS = %d, techPaths.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,techsPerWorker,MAX_FileCRCPreCacheThread_WORKER_THREADS,(int)techPaths.size());
try {
unsigned int consumedWorkers = 0;
for(unsigned int workerIdx = 0; workerIdx < (unsigned int)MAX_FileCRCPreCacheThread_WORKER_THREADS; ++workerIdx) {
if(getQuitStatus() == true) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
break;
}
unsigned int currentWorkerMax = ((unsigned int)techPaths.size() - consumedWorkers);
if(currentWorkerMax > techsPerWorker) {
currentWorkerMax = techsPerWorker;
}
vector<string> workerTechList;
unsigned int endConsumerIndex = consumedWorkers + currentWorkerMax;
for(unsigned int idx = consumedWorkers; idx < endConsumerIndex; idx++) {
string techName = techPaths[idx];
workerTechList.push_back(techName);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Spawning CRC thread for Tech [%s] [%d of %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,techName.c_str(),idx+1,(int)techPaths.size());
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] workerIdx = %u, currentWorkerMax = %u, endConsumerIndex = %u\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,workerIdx,currentWorkerMax,endConsumerIndex);
// Pause before launching this worker thread
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("About to process CRC for factions waiting...\n");
time_t pauseTime = time(NULL);
while(getQuitStatus() == false &&
difftime(time(NULL),pauseTime) <= PAUSE_SECONDS_BETWEEN_WORKERS) {
sleep(25);
}
if(getQuitStatus() == true) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
break;
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Starting CRC for faction workers...\n");
FileCRCPreCacheThread *workerThread =
new FileCRCPreCacheThread(techDataPaths,
workerTechList,
this->processTechCB);
static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__);
workerThread->setUniqueID(mutexOwnerId);
workerThread->setPauseForGame(this->getPauseForGame());
static string mutexOwnerId2 = CODE_AT_LINE;
MutexSafeWrapper safeMutexPause(mutexPauseForGame,mutexOwnerId2);
preCacheWorkerThreadList.push_back(workerThread);
safeMutexPause.ReleaseLock();
workerThread->start();
consumedWorkers += currentWorkerMax;
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Spawning CRC thread, preCacheWorkerThreadList.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,(int)preCacheWorkerThreadList.size());
if(consumedWorkers >= techPaths.size()) {
break;
}
}
}
catch(const exception &ex) {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what());
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what());
}
catch(...) {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] UNKNOWN Error\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unknown error\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
}
sleep(0);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Waiting for Spawned CRC threads to complete, preCacheWorkerThreadList.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,(int)preCacheWorkerThreadList.size());
try {
bool hasRunningWorkerThread = true;
for(;hasRunningWorkerThread == true;) {
hasRunningWorkerThread = false;
for(unsigned int idx = 0; idx < preCacheWorkerThreadList.size(); idx++) {
FileCRCPreCacheThread *workerThread = preCacheWorkerThreadList[idx];
if(workerThread != NULL) {
if(workerThread->getRunningStatus() == true) {
hasRunningWorkerThread = true;
if(getQuitStatus() == true &&
workerThread->getQuitStatus() == false) {
workerThread->signalQuit();
}
}
else if(workerThread->getRunningStatus() == false) {
sleep(25);
static string mutexOwnerId2 = CODE_AT_LINE;
MutexSafeWrapper safeMutexPause(mutexPauseForGame,mutexOwnerId2);
delete workerThread;
preCacheWorkerThreadList[idx] = NULL;
safeMutexPause.ReleaseLock();
}
}
}
if( getQuitStatus() == false &&
hasRunningWorkerThread == true) {
sleep(25);
}
else if(getQuitStatus() == true) {
for(unsigned int idx = 0; idx < preCacheWorkerThreadList.size(); idx++) {
FileCRCPreCacheThread *workerThread = preCacheWorkerThreadList[idx];
if(workerThread != NULL && workerThread->getQuitStatus() == false) {
workerThread->signalQuit();
}
}
}
}
}
catch(const exception &ex) {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what());
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what());
}
catch(...) {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] UNKNOWN Error\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unknown error\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("********************** CRC Controller thread took %.2f seconds END **********************\n",difftime(time(NULL),elapsedTime));
}
}
else {
try {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\tStarting CRC for faction worker thread for techs: %d...\n",(int)workerThreadTechPaths.size());
for(unsigned int idx = 0; idx < workerThreadTechPaths.size(); idx++) {
string techName = this->workerThreadTechPaths[idx];
if(getQuitStatus() == true) {
break;
}
//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("--------------------- CRC worker thread START for tech [%s] ---------------------------\n",techName.c_str());
//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] caching CRC value for Tech [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,techName.c_str());
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] caching CRC value for Tech [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,techName.c_str());
time_t elapsedTime = time(NULL);
// Clear existing CRC to force a CRC refresh
//string pathSearchString = string("/") + techName + string("/*");
//const string filterFileExt = ".xml";
//clearFolderTreeContentsCheckSum(techDataPaths, pathSearchString, filterFileExt);
//clearFolderTreeContentsCheckSumList(techDataPaths, pathSearchString, filterFileExt);
if(getQuitStatus() == true) {
break;
}
//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("--------------------- CRC worker thread running for tech [%s] ---------------------------\n",techName.c_str());
if(getQuitStatus() == false) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\t\tAbout to process CRC for techName [%s]\n",techName.c_str());
// Do not process CRC's while game in progress
if(getPauseForGame() == true) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\t\tGame in progress so waiting to process CRC for techName [%s]\n",techName.c_str());
for(;getPauseForGame() == true &&
getQuitStatus() == false;) {
sleep(25);
}
if(getQuitStatus() == true) {
break;
}
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\t\tStart Processing CRC for techName [%s]\n",techName.c_str());
uint32 techCRC = getFolderTreeContentsCheckSumRecursively(techDataPaths, string("/") + techName + string("/*"), ".xml", NULL, true);
//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] cached CRC value for Tech [%s] is [%d] took %.3f seconds.\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,techName.c_str(),techCRC,difftime(time(NULL),elapsedTime));
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cached CRC value for Tech [%s] is [%u] took %.3f seconds.\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,techName.c_str(),techCRC,difftime(time(NULL),elapsedTime));
vector<string> results;
for(unsigned int idx = 0; idx < techDataPaths.size(); idx++) {
string &techPath = techDataPaths[idx];
endPathWithSlash(techPath);
findAll(techPath + techName + "/factions/*.", results, false, false);
if(results.empty() == false) {
break;
}
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\t\tStarting CRC for faction worker thread for results: %d...\n",(int)results.size());
if(results.empty() == true) {
for(unsigned int factionIdx = 0;
factionIdx < results.size();
++factionIdx) {
string factionName = results[factionIdx];
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\t\t\tAbout to process CRC for factionName [%s]\n",factionName.c_str());
// Do not process CRC's while game in progress
if(getPauseForGame() == true) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\t\t\tGame in progress so waiting to process CRC for factionName [%s]\n",factionName.c_str());
for(;getPauseForGame() == true &&
getQuitStatus() == false;) {
sleep(25);
}
if(getQuitStatus() == true) {
break;
}
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\t\t\tStart Processing CRC for factionName [%s]\n",factionName.c_str());
uint32 factionCRC = getFolderTreeContentsCheckSumRecursively(techDataPaths, "/" + techName + "/factions/" + factionName + "/*", ".xml", NULL, true);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\t\t\tDone Processing CRC for factionName [%s] CRC [%d]\n",factionName.c_str(),factionCRC);
}
}
//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] cached CRC value for Tech [%s] is [%d] took %.3f seconds.\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,techName.c_str(),techCRC,difftime(time(NULL),elapsedTime));
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cached CRC value for Tech [%s] is [%d] took %.3f seconds.\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,techName.c_str(),techCRC,difftime(time(NULL),elapsedTime));
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("--------------------- CRC worker thread END for tech [%s] ---------------------------\n",techName.c_str());
if(getQuitStatus() == true) {
break;
}
}
}
catch(const exception &ex) {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what());
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what());
}
catch(...) {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] UNKNOWN Error\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unknown error\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
}
}
}
catch(const exception &ex) {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what());
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what());
}
catch(...) {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] UNKNOWN Error\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unknown error\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
}
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] FILE CRC PreCache thread is exiting...\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] FILE CRC PreCache thread is exiting...\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
}
deleteSelfIfRequired();
}
SimpleTaskThread::SimpleTaskThread( SimpleTaskCallbackInterface *simpleTaskInterface,
unsigned int executionCount,
unsigned int millisecsBetweenExecutions,
bool needTaskSignal,
void *userdata, bool wantSetupAndShutdown) : BaseThread(),
simpleTaskInterface(NULL),
overrideShutdownTask(NULL),
mutexSimpleTaskInterfaceValid(new Mutex(CODE_AT_LINE)),
mutexTaskSignaller(new Mutex(CODE_AT_LINE)),
mutexLastExecuteTimestamp(new Mutex(CODE_AT_LINE)) {
uniqueID = "SimpleTaskThread";
this->simpleTaskInterface = simpleTaskInterface;
this->simpleTaskInterfaceValid = (this->simpleTaskInterface != NULL);
this->executionCount = executionCount;
this->millisecsBetweenExecutions = millisecsBetweenExecutions;
this->needTaskSignal = needTaskSignal;
this->overrideShutdownTask = NULL;
this->userdata = userdata;
this->wantSetupAndShutdown = wantSetupAndShutdown;
//if(this->userdata != NULL) {
// printf("IN SImpleThread this->userdata [%p]\n",this->userdata);
//}
setTaskSignalled(false);
string mutexOwnerId = CODE_AT_LINE;
MutexSafeWrapper safeMutex(mutexLastExecuteTimestamp,mutexOwnerId);
mutexLastExecuteTimestamp->setOwnerId(mutexOwnerId);
lastExecuteTimestamp = time(NULL);
if(this->wantSetupAndShutdown == true) {
string mutexOwnerId1 = CODE_AT_LINE;
MutexSafeWrapper safeMutex1(mutexSimpleTaskInterfaceValid,mutexOwnerId1);
if(this->simpleTaskInterfaceValid == true) {
safeMutex1.ReleaseLock();
this->simpleTaskInterface->setupTask(this,userdata);
}
}
}
SimpleTaskThread::~SimpleTaskThread() {
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
try {
cleanup();
delete mutexSimpleTaskInterfaceValid;
mutexSimpleTaskInterfaceValid = NULL;
delete mutexTaskSignaller;
mutexTaskSignaller = NULL;
delete mutexLastExecuteTimestamp;
mutexLastExecuteTimestamp = NULL;
}
catch(const exception &ex) {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what());
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->getUniqueID().c_str());
throw megaglest_runtime_error(ex.what());
//abort();
}
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
}
void SimpleTaskThread::cleanup() {
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
if(this->wantSetupAndShutdown == true) {
if(this->overrideShutdownTask != NULL) {
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
this->overrideShutdownTask(this);
this->overrideShutdownTask = NULL;
}
else if(this->simpleTaskInterface != NULL) {
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
string mutexOwnerId1 = CODE_AT_LINE;
MutexSafeWrapper safeMutex1(mutexSimpleTaskInterfaceValid,mutexOwnerId1);
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
if(this->simpleTaskInterfaceValid == true) {
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
safeMutex1.ReleaseLock();
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
this->simpleTaskInterface->shutdownTask(this,userdata);
this->simpleTaskInterface = NULL;
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
}
}
}
//printf("~SimpleTaskThread LINE: %d this = %p\n",__LINE__,this);
}
void SimpleTaskThread::setOverrideShutdownTask(taskFunctionCallback *ptr) {
this->overrideShutdownTask = ptr;
}
bool SimpleTaskThread::isThreadExecutionLagging() {
string mutexOwnerId = CODE_AT_LINE;
MutexSafeWrapper safeMutex(mutexLastExecuteTimestamp,mutexOwnerId);
mutexLastExecuteTimestamp->setOwnerId(mutexOwnerId);
bool result = (difftime(time(NULL),lastExecuteTimestamp) >= 5.0);
safeMutex.ReleaseLock();
return result;
}
bool SimpleTaskThread::canShutdown(bool deleteSelfIfShutdownDelayed) {
bool ret = (getExecutingTask() == false);
if(ret == false && deleteSelfIfShutdownDelayed == true) {
setDeleteSelfOnExecutionDone(deleteSelfIfShutdownDelayed);
deleteSelfIfRequired();
signalQuit();
}
return ret;
}
bool SimpleTaskThread::getSimpleTaskInterfaceValid() {
string mutexOwnerId1 = CODE_AT_LINE;
MutexSafeWrapper safeMutex1(mutexSimpleTaskInterfaceValid,mutexOwnerId1);
return this->simpleTaskInterfaceValid;
}
void SimpleTaskThread::setSimpleTaskInterfaceValid(bool value) {
string mutexOwnerId1 = CODE_AT_LINE;
MutexSafeWrapper safeMutex1(mutexSimpleTaskInterfaceValid,mutexOwnerId1);
this->simpleTaskInterfaceValid = value;
}
void SimpleTaskThread::execute() {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
void *ptr_cpy = this->ptr;
bool mustDeleteSelf = false;
{
{
RunningStatusSafeWrapper runningStatus(this);
try {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(getQuitStatus() == true) {
return;
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->getUniqueID().c_str());
unsigned int idx = 0;
for(;this->simpleTaskInterface != NULL;) {
string mutexOwnerId1 = CODE_AT_LINE;
MutexSafeWrapper safeMutex1(mutexSimpleTaskInterfaceValid,mutexOwnerId1);
if(this->simpleTaskInterfaceValid == false) {
break;
}
safeMutex1.ReleaseLock();
bool runTask = true;
if(needTaskSignal == true) {
runTask = getTaskSignalled();
if(runTask == true) {
setTaskSignalled(false);
}
}
if(getQuitStatus() == true) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->getUniqueID().c_str());
break;
}
else if(runTask == true) {
if(getQuitStatus() == false) {
ExecutingTaskSafeWrapper safeExecutingTaskMutex(this);
//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
this->simpleTaskInterface->simpleTask(this,this->userdata);
//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(getQuitStatus() == true) {
break;
}
string mutexOwnerId = CODE_AT_LINE;
MutexSafeWrapper safeMutex(mutexLastExecuteTimestamp,mutexOwnerId);
mutexLastExecuteTimestamp->setOwnerId(mutexOwnerId);
lastExecuteTimestamp = time(NULL);
safeMutex.ReleaseLock();
}
}
if(this->executionCount > 0) {
idx++;
if(idx >= this->executionCount) {
break;
}
}
if(getQuitStatus() == true) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->getUniqueID().c_str());
break;
}
sleep(this->millisecsBetweenExecutions);
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s] END\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->getUniqueID().c_str());
mustDeleteSelf = getDeleteSelfOnExecutionDone();
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
}
catch(const exception &ex) {
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what());
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->getUniqueID().c_str());
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s] END\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->getUniqueID().c_str());
mustDeleteSelf = getDeleteSelfOnExecutionDone();
throw megaglest_runtime_error(ex.what());
}
}
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(mustDeleteSelf == true) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(isThreadDeleted(ptr_cpy) == false) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
this->setDeleteAfterExecute(true);
}
return;
}
}
void SimpleTaskThread::setTaskSignalled(bool value) {
string mutexOwnerId = CODE_AT_LINE;
MutexSafeWrapper safeMutex(mutexTaskSignaller,mutexOwnerId);
mutexTaskSignaller->setOwnerId(mutexOwnerId);
taskSignalled = value;
safeMutex.ReleaseLock();
}
bool SimpleTaskThread::getTaskSignalled() {
string mutexOwnerId = CODE_AT_LINE;
MutexSafeWrapper safeMutex(mutexTaskSignaller,mutexOwnerId);
mutexTaskSignaller->setOwnerId(mutexOwnerId);
bool retval = taskSignalled;
safeMutex.ReleaseLock();
return retval;
}
// -------------------------------------------------
LogFileThread::LogFileThread() : BaseThread(), mutexLogList(new Mutex(CODE_AT_LINE)) {
uniqueID = "LogFileThread";
logList.clear();
lastSaveToDisk = time(NULL);
static string mutexOwnerId = CODE_AT_LINE;
mutexLogList->setOwnerId(mutexOwnerId);
}
LogFileThread::~LogFileThread() {
delete mutexLogList;
mutexLogList = NULL;
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#1 In [%s::%s Line: %d] LogFile thread is deleting\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
}
void LogFileThread::addLogEntry(SystemFlags::DebugType type, string logEntry) {
static string mutexOwnerId = CODE_AT_LINE;
MutexSafeWrapper safeMutex(mutexLogList,mutexOwnerId);
mutexLogList->setOwnerId(mutexOwnerId);
LogFileEntry entry;
entry.type = type;
entry.entry = logEntry;
entry.entryDateTime = time(NULL);
logList.push_back(entry);
//if(logList.size() >= 750000) {
// saveToDisk(false,true);
//}
}
bool LogFileThread::checkSaveCurrentLogBufferToDisk() {
bool ret = false;
if(difftime(time(NULL),lastSaveToDisk) >= 5.0 ||
(difftime(time(NULL), lastSaveToDisk) >= 2.0 &&
LogFileThread::getLogEntryBufferCount() >= 1000000)) {
lastSaveToDisk = time(NULL);
ret = true;
}
return ret;
}
void LogFileThread::execute() {
void *ptr_cpy = this->ptr;
bool mustDeleteSelf = false;
{
RunningStatusSafeWrapper runningStatus(this);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(getQuitStatus() == true) {
deleteSelfIfRequired();
return;
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"LogFile thread is running\n");
try {
ExecutingTaskSafeWrapper safeExecutingTaskMutex(this);
for(;this->getQuitStatus() == false;) {
while(this->getQuitStatus() == false &&
checkSaveCurrentLogBufferToDisk() == true) {
saveToDisk(false,false);
}
if(this->getQuitStatus() == false) {
sleep(25);
}
}
// Ensure remaining entryies are logged to disk on shutdown
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
saveToDisk(true,false);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
}
catch(const exception &ex) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what());
}
catch(...) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] UNKNOWN Error\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] LogFile thread is starting to exit\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
mustDeleteSelf = getDeleteSelfOnExecutionDone();
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] LogFile thread is exiting, mustDeleteSelf = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,mustDeleteSelf);
}
if(mustDeleteSelf == true) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] LogFile thread is deleting self\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__);
if(isThreadDeleted(ptr_cpy) == false) {
this->setDeleteAfterExecute(true);
}
return;
}
}
std::size_t LogFileThread::getLogEntryBufferCount() {
static string mutexOwnerId = CODE_AT_LINE;
MutexSafeWrapper safeMutex(mutexLogList,mutexOwnerId);
mutexLogList->setOwnerId(mutexOwnerId);
std::size_t logCount = logList.size();
safeMutex.ReleaseLock();
return logCount;
}
bool LogFileThread::canShutdown(bool deleteSelfIfShutdownDelayed) {
bool ret = (getExecutingTask() == false);
if(ret == false && deleteSelfIfShutdownDelayed == true) {
setDeleteSelfOnExecutionDone(deleteSelfIfShutdownDelayed);
deleteSelfIfRequired();
signalQuit();
}
return ret;
}
void LogFileThread::saveToDisk(bool forceSaveAll,bool logListAlreadyLocked) {
static string mutexOwnerId = CODE_AT_LINE;
MutexSafeWrapper safeMutex(NULL,mutexOwnerId);
if(logListAlreadyLocked == false) {
safeMutex.setMutex(mutexLogList);
mutexLogList->setOwnerId(mutexOwnerId);
}
std::size_t logCount = logList.size();
if(logCount > 0) {
vector<LogFileEntry> tempLogList = logList;
safeMutex.ReleaseLock(true);
logCount = tempLogList.size();
//if(forceSaveAll == false) {
// logCount = min(logCount,(std::size_t)2000000);
//}
if(logCount > 0) {
for(unsigned int i = 0; i < logCount; ++i) {
LogFileEntry &entry = tempLogList[i];
SystemFlags::logDebugEntry(entry.type, entry.entry, entry.entryDateTime);
}
safeMutex.Lock();
if(logList.size() > 0) {
if(logList.size() < logCount) {
char szBuf[8096]="";
snprintf(szBuf,8096,"logList.size() <= logCount [%lld][%lld]",(long long int)logList.size(),(long long int)logCount);
throw megaglest_runtime_error(szBuf);
}
logList.erase(logList.begin(),logList.begin() + logCount);
}
safeMutex.ReleaseLock();
}
}
else {
safeMutex.ReleaseLock();
}
}
}}//end namespace
|