- 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();
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) {

View File

@@ -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();

View File

@@ -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__);

View File

@@ -137,10 +137,15 @@ private:
set<int> livingUnits;
set<Unit*> 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); }

View File

@@ -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);