- faction threads now use semaphores to signal completion lowering cpu use on main thread

This commit is contained in:
Mark Vejvoda
2011-11-26 23:11:33 +00:00
parent 7fb7b5e611
commit fbb3f0b8e5
5 changed files with 98 additions and 20 deletions

View File

@@ -474,6 +474,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) {
getTilesetsLocalList(); getTilesetsLocalList();
for(unsigned int i=0; i < tilesetListRemote.size(); i++) { 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]); string result=refreshTilesetModInfo(tilesetListRemote[i]);
if(result != "") { if(result != "") {
ModInfo modinfo; ModInfo modinfo;
@@ -493,6 +499,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) {
} }
for(unsigned int i=0; i < tilesetFilesUserData.size(); i++) { 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]; string tilesetName = tilesetFilesUserData[i];
bool alreadyHasTileset = (tilesetCacheList.find(tilesetName) != tilesetCacheList.end()); bool alreadyHasTileset = (tilesetCacheList.find(tilesetName) != tilesetCacheList.end());
if(alreadyHasTileset == false) { if(alreadyHasTileset == false) {
@@ -524,6 +536,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) {
getTechsLocalList(); getTechsLocalList();
for(unsigned int i=0; i < techListRemote.size(); i++) { 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]); string result=refreshTechModInfo(techListRemote[i]);
if(result != "") { if(result != "") {
ModInfo modinfo; ModInfo modinfo;
@@ -549,6 +567,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) {
} }
for(unsigned int i=0; i < techTreeFilesUserData.size(); i++) { 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]; string techName = techTreeFilesUserData[i];
bool alreadyHasTech = (techCacheList.find(techName) != techCacheList.end()); bool alreadyHasTech = (techCacheList.find(techName) != techCacheList.end());
if(alreadyHasTech == false) { if(alreadyHasTech == false) {
@@ -587,6 +611,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) {
getMapsLocalList(); getMapsLocalList();
for(unsigned int i=0; i < mapListRemote.size(); i++) { 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]); string result=refreshMapModInfo(mapListRemote[i]);
if(result != "") { if(result != "") {
ModInfo modinfo; ModInfo modinfo;
@@ -612,6 +642,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) {
} }
for(unsigned int i=0; i < mapFilesUserData.size(); i++) { 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]; string mapName = mapFilesUserData[i];
bool alreadyHasMap = (mapCacheList.find(mapName) != mapCacheList.end()); bool alreadyHasMap = (mapCacheList.find(mapName) != mapCacheList.end());
if(alreadyHasMap == false) { if(alreadyHasMap == false) {
@@ -650,6 +686,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) {
getScenariosLocalList(); getScenariosLocalList();
for(unsigned int i=0; i < scenarioListRemote.size(); i++) { 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]); string result=refreshScenarioModInfo(scenarioListRemote[i]);
if(result != "") { if(result != "") {
ModInfo modinfo; ModInfo modinfo;
@@ -669,6 +711,12 @@ void MenuStateMods::simpleTask(BaseThread *callingThread) {
} }
for(unsigned int i=0; i < scenarioFilesUserData.size(); i++) { 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]; string scenarioName = scenarioFilesUserData[i];
bool alreadyHasScenario = (scenarioCacheList.find(scenarioName) != scenarioCacheList.end()); bool alreadyHasScenario = (scenarioCacheList.find(scenarioName) != scenarioCacheList.end());
if(alreadyHasScenario == false) { if(alreadyHasScenario == false) {

View File

@@ -110,7 +110,9 @@ private:
Semaphore semSlotWorkerTaskCompleted; Semaphore semSlotWorkerTaskCompleted;
//Mutex mutexSocket; //Mutex mutexSocket;
ReadWriteMutex socketRWLMutex;
Socket* socket; Socket* socket;
int playerIndex; int playerIndex;
string name; string name;
bool ready; bool ready;
@@ -134,8 +136,6 @@ private:
int playerStatus; int playerStatus;
string playerLanguage; string playerLanguage;
ReadWriteMutex socketRWLMutex;
public: public:
ConnectionSlot(ServerInterface* serverInterface, int playerIndex); ConnectionSlot(ServerInterface* serverInterface, int playerIndex);
~ConnectionSlot(); ~ConnectionSlot();

View File

@@ -333,6 +333,10 @@ void FactionThread::execute() {
safeMutex.ReleaseLock(); safeMutex.ReleaseLock();
setTaskCompleted(frameIndex.first); setTaskCompleted(frameIndex.first);
this->faction->signalWorkerTaskCompleted();
}
else {
this->faction->signalWorkerTaskCompleted();
} }
if(getQuitStatus() == true) { 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); 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) { catch(const exception &ex) {
this->faction->signalWorkerTaskCompleted();
//setRunningStatus(false); //setRunningStatus(false);
SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); 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()); 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__); 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__); 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() { void Faction::end() {
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

View File

@@ -137,10 +137,15 @@ private:
set<int> livingUnits; set<int> livingUnits;
set<Unit*> livingUnitsp; set<Unit*> livingUnitsp;
Semaphore semWorkerTaskCompleted;
public: public:
Faction(); Faction();
~Faction(); ~Faction();
void signalWorkerTaskCompleted();
bool waitWorkerTaskCompleted(int waitMilliseconds=-1);
void addLivingUnits(int id) { livingUnits.insert(id); } void addLivingUnits(int id) { livingUnits.insert(id); }
void addLivingUnitsp(Unit *unit) { livingUnitsp.insert(unit); } void addLivingUnitsp(Unit *unit) { livingUnitsp.insert(unit); }

View File

@@ -372,30 +372,40 @@ void World::updateAllFactionUnits() {
faction->signalWorkerThread(frameCount); faction->signalWorkerThread(frameCount);
} }
bool workThreadsFinished = false;
Chrono chrono; Chrono chrono;
chrono.start(); chrono.start();
const int MAX_FACTION_THREAD_WAIT_MILLISECONDS = 20000; const int MAX_FACTION_THREAD_WAIT_MILLISECONDS = 20000;
for(;chrono.getMillis() < MAX_FACTION_THREAD_WAIT_MILLISECONDS;) { // bool workThreadsFinished = false;
workThreadsFinished = true; // for(;chrono.getMillis() < MAX_FACTION_THREAD_WAIT_MILLISECONDS;) {
for(int i = 0; i < factionCount; ++i) { // workThreadsFinished = true;
Faction *faction = getFaction(i); // for(int i = 0; i < factionCount; ++i) {
if(faction == NULL) { // Faction *faction = getFaction(i);
throw runtime_error("faction == NULL"); // if(faction == NULL) {
} // throw runtime_error("faction == NULL");
if(faction->isWorkerThreadSignalCompleted(frameCount) == false) { // }
workThreadsFinished = false; // if(faction->isWorkerThreadSignalCompleted(frameCount) == false) {
break; // workThreadsFinished = false;
} // break;
} // }
if(workThreadsFinished == false) { // }
//sleep(0); // if(workThreadsFinished == false) {
} // //sleep(0);
else { // }
break; // 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); 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);