diff --git a/source/glest_game/menu/menu_state_mods.cpp b/source/glest_game/menu/menu_state_mods.cpp index 96afae266..5df776666 100644 --- a/source/glest_game/menu/menu_state_mods.cpp +++ b/source/glest_game/menu/menu_state_mods.cpp @@ -474,6 +474,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) { getTilesetsLocalList(); for(unsigned int i=0; i < tilesetListRemote.size(); i++) { + + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + return; + } + string result=refreshTilesetModInfo(tilesetListRemote[i]); if(result != "") { ModInfo modinfo; @@ -493,6 +499,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) { } for(unsigned int i=0; i < tilesetFilesUserData.size(); i++) { + + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + return; + } + string tilesetName = tilesetFilesUserData[i]; bool alreadyHasTileset = (tilesetCacheList.find(tilesetName) != tilesetCacheList.end()); if(alreadyHasTileset == false) { @@ -524,6 +536,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) { getTechsLocalList(); for(unsigned int i=0; i < techListRemote.size(); i++) { + + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + return; + } + string result=refreshTechModInfo(techListRemote[i]); if(result != "") { ModInfo modinfo; @@ -549,6 +567,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) { } for(unsigned int i=0; i < techTreeFilesUserData.size(); i++) { + + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + return; + } + string techName = techTreeFilesUserData[i]; bool alreadyHasTech = (techCacheList.find(techName) != techCacheList.end()); if(alreadyHasTech == false) { @@ -587,6 +611,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) { getMapsLocalList(); for(unsigned int i=0; i < mapListRemote.size(); i++) { + + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + return; + } + string result=refreshMapModInfo(mapListRemote[i]); if(result != "") { ModInfo modinfo; @@ -612,6 +642,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) { } for(unsigned int i=0; i < mapFilesUserData.size(); i++) { + + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + return; + } + string mapName = mapFilesUserData[i]; bool alreadyHasMap = (mapCacheList.find(mapName) != mapCacheList.end()); if(alreadyHasMap == false) { @@ -650,6 +686,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) { getScenariosLocalList(); for(unsigned int i=0; i < scenarioListRemote.size(); i++) { + + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + return; + } + string result=refreshScenarioModInfo(scenarioListRemote[i]); if(result != "") { ModInfo modinfo; @@ -669,6 +711,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) { } for(unsigned int i=0; i < scenarioFilesUserData.size(); i++) { + + if(callingThread->getQuitStatus() == true || safeMutexThreadOwner.isValidMutex() == false) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + return; + } + string scenarioName = scenarioFilesUserData[i]; bool alreadyHasScenario = (scenarioCacheList.find(scenarioName) != scenarioCacheList.end()); if(alreadyHasScenario == false) { diff --git a/source/glest_game/network/connection_slot.h b/source/glest_game/network/connection_slot.h index 0d641a5aa..52b68f44e 100644 --- a/source/glest_game/network/connection_slot.h +++ b/source/glest_game/network/connection_slot.h @@ -110,7 +110,9 @@ private: Semaphore semSlotWorkerTaskCompleted; //Mutex mutexSocket; + ReadWriteMutex socketRWLMutex; Socket* socket; + int playerIndex; string name; bool ready; @@ -134,8 +136,6 @@ private: int playerStatus; string playerLanguage; - ReadWriteMutex socketRWLMutex; - public: ConnectionSlot(ServerInterface* serverInterface, int playerIndex); ~ConnectionSlot(); diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 41948dd81..456cda89a 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -333,6 +333,10 @@ void FactionThread::execute() { safeMutex.ReleaseLock(); setTaskCompleted(frameIndex.first); + this->faction->signalWorkerTaskCompleted(); + } + else { + this->faction->signalWorkerTaskCompleted(); } if(getQuitStatus() == true) { @@ -345,6 +349,7 @@ void FactionThread::execute() { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] ****************** ENDING worker thread this = %p\n",__FILE__,__FUNCTION__,__LINE__,this); } catch(const exception &ex) { + this->faction->signalWorkerTaskCompleted(); //setRunningStatus(false); SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); @@ -352,6 +357,7 @@ void FactionThread::execute() { throw runtime_error(ex.what()); } + this->faction->signalWorkerTaskCompleted(); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); } @@ -412,6 +418,15 @@ Faction::~Faction() { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } +void Faction::signalWorkerTaskCompleted() { + semWorkerTaskCompleted.signal(); +} + +bool Faction::waitWorkerTaskCompleted(int waitMilliseconds) { + int result = semWorkerTaskCompleted.waitTillSignalled(waitMilliseconds); + return (result == 0); +} + void Faction::end() { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); diff --git a/source/glest_game/type_instances/faction.h b/source/glest_game/type_instances/faction.h index 4af5c9660..727f9b8f3 100644 --- a/source/glest_game/type_instances/faction.h +++ b/source/glest_game/type_instances/faction.h @@ -137,10 +137,15 @@ private: set livingUnits; set livingUnitsp; + Semaphore semWorkerTaskCompleted; + public: Faction(); ~Faction(); + void signalWorkerTaskCompleted(); + bool waitWorkerTaskCompleted(int waitMilliseconds=-1); + void addLivingUnits(int id) { livingUnits.insert(id); } void addLivingUnitsp(Unit *unit) { livingUnitsp.insert(unit); } diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index 25943011a..907378c6c 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -372,30 +372,40 @@ void World::updateAllFactionUnits() { faction->signalWorkerThread(frameCount); } - bool workThreadsFinished = false; Chrono chrono; chrono.start(); const int MAX_FACTION_THREAD_WAIT_MILLISECONDS = 20000; - for(;chrono.getMillis() < MAX_FACTION_THREAD_WAIT_MILLISECONDS;) { - workThreadsFinished = true; - for(int i = 0; i < factionCount; ++i) { - Faction *faction = getFaction(i); - if(faction == NULL) { - throw runtime_error("faction == NULL"); - } - if(faction->isWorkerThreadSignalCompleted(frameCount) == false) { - workThreadsFinished = false; - break; - } - } - if(workThreadsFinished == false) { - //sleep(0); - } - else { - break; +// bool workThreadsFinished = false; +// for(;chrono.getMillis() < MAX_FACTION_THREAD_WAIT_MILLISECONDS;) { +// workThreadsFinished = true; +// for(int i = 0; i < factionCount; ++i) { +// Faction *faction = getFaction(i); +// if(faction == NULL) { +// throw runtime_error("faction == NULL"); +// } +// if(faction->isWorkerThreadSignalCompleted(frameCount) == false) { +// workThreadsFinished = false; +// break; +// } +// } +// if(workThreadsFinished == false) { +// //sleep(0); +// } +// else { +// break; +// } +// } + + // Wait for faction threads to finish + for(int i = 0; i < factionCount; ++i) { + Faction *faction = getFaction(i); + if(faction == NULL) { + throw runtime_error("faction == NULL"); } + faction->waitWorkerTaskCompleted(MAX_FACTION_THREAD_WAIT_MILLISECONDS); } + // if(SystemFlags::VERBOSE_MODE_ENABLED && chrono.getMillis() >= 10) printf("In [%s::%s Line: %d] *** Faction thread preprocessing took [%lld] msecs for %d factions for frameCount = %d.\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis(),factionCount,frameCount);