- finally fixed the unit sorting hang bug, try it tomryen

This commit is contained in:
Mark Vejvoda 2011-07-06 06:38:56 +00:00
parent 15488ddb31
commit b8bd927b7b
4 changed files with 54 additions and 26 deletions

View File

@ -48,6 +48,7 @@ bool CommandGroupSorter::operator< (const CommandGroupSorter &j) const {
} }
Command *command= this->unit->getCurrrentCommandThreadSafe(); Command *command= this->unit->getCurrrentCommandThreadSafe();
//Command *command= this->unit->getCurrCommand();
if( command != NULL && if( command != NULL &&
(command->getCommandType()->getClass() == ccMove || (command->getCommandType()->getClass() == ccMove ||
command->getCommandType()->getClass() == ccAttack) && command->getCommandType()->getClass() == ccAttack) &&
@ -55,6 +56,7 @@ bool CommandGroupSorter::operator< (const CommandGroupSorter &j) const {
int curCommandGroupId = command->getUnitCommandGroupId(); int curCommandGroupId = command->getUnitCommandGroupId();
Command *commandPeer = j.unit->getCurrrentCommandThreadSafe(); Command *commandPeer = j.unit->getCurrrentCommandThreadSafe();
//Command *commandPeer = j.unit->getCurrCommand();
if(commandPeer == NULL) { if(commandPeer == NULL) {
return true; return true;
} }
@ -100,7 +102,7 @@ void FactionThread::setQuitStatus(bool value) {
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 FactionThread::signalPathfinder(int frameIndex, std::vector<CommandGroupSorter> *unitsInFactionsSorted) { void FactionThread::signalPathfinder(int frameIndex, std::vector<CommandGroupSorter *> *unitsInFactionsSorted) {
if(frameIndex >= 0) { if(frameIndex >= 0) {
static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
MutexSafeWrapper safeMutex(&triggerIdMutex,mutexOwnerId); MutexSafeWrapper safeMutex(&triggerIdMutex,mutexOwnerId);
@ -188,7 +190,7 @@ void FactionThread::execute() {
//std::vector<CommandGroupSorter> *unitsInFactionsSorted //std::vector<CommandGroupSorter> *unitsInFactionsSorted
int unitCount = unitsInFactionsSorted->size(); int unitCount = unitsInFactionsSorted->size();
for(int j = 0; j < unitCount; ++j) { for(int j = 0; j < unitCount; ++j) {
Unit *unit = (*unitsInFactionsSorted)[j].unit; Unit *unit = (*unitsInFactionsSorted)[j]->unit;
if(unit == NULL) { if(unit == NULL) {
throw runtime_error("unit == NULL"); throw runtime_error("unit == NULL");
} }
@ -277,7 +279,7 @@ 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::signalWorkerThread(int frameIndex, std::vector<CommandGroupSorter> *unitsInFactionsSorted) { void Faction::signalWorkerThread(int frameIndex, std::vector<CommandGroupSorter *> *unitsInFactionsSorted) {
if(workerThread != NULL) { if(workerThread != NULL) {
workerThread->signalPathfinder(frameIndex,unitsInFactionsSorted); workerThread->signalPathfinder(frameIndex,unitsInFactionsSorted);
} }

View File

@ -65,7 +65,7 @@ protected:
Semaphore semTaskSignalled; Semaphore semTaskSignalled;
Mutex triggerIdMutex; Mutex triggerIdMutex;
std::pair<int,bool> frameIndex; std::pair<int,bool> frameIndex;
std::vector<CommandGroupSorter> *unitsInFactionsSorted; std::vector<CommandGroupSorter *> *unitsInFactionsSorted;
virtual void setQuitStatus(bool value); virtual void setQuitStatus(bool value);
virtual void setTaskCompleted(int frameIndex); virtual void setTaskCompleted(int frameIndex);
@ -74,7 +74,7 @@ protected:
public: public:
FactionThread(Faction *faction); FactionThread(Faction *faction);
virtual void execute(); virtual void execute();
void signalPathfinder(int frameIndex,std::vector<CommandGroupSorter> *unitsInFactionsSorted); void signalPathfinder(int frameIndex,std::vector<CommandGroupSorter *> *unitsInFactionsSorted);
bool isSignalPathfinderCompleted(int frameIndex); bool isSignalPathfinderCompleted(int frameIndex);
}; };
@ -203,7 +203,7 @@ public:
World * getWorld() { return world; } World * getWorld() { return world; }
int getFrameCount(); int getFrameCount();
void signalWorkerThread(int frameIndex,std::vector<CommandGroupSorter> *unitsInFactionsSorted); void signalWorkerThread(int frameIndex,std::vector<CommandGroupSorter *> *unitsInFactionsSorted);
bool isWorkerThreadSignalCompleted(int frameIndex); bool isWorkerThreadSignalCompleted(int frameIndex);
void limitResourcesToStore(); void limitResourcesToStore();

View File

@ -908,6 +908,9 @@ Command *Unit::getCurrCommand() const {
} }
void Unit::replaceCurrCommand(Command *cmd) { void Unit::replaceCurrCommand(Command *cmd) {
static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
MutexSafeWrapper safeMutex(&mutexCommands,mutexOwnerId);
assert(commands.empty() == false); assert(commands.empty() == false);
commands.front() = cmd; commands.front() = cmd;
this->setCurrentUnitTitle(""); this->setCurrentUnitTitle("");
@ -967,22 +970,23 @@ CommandResult Unit::giveCommand(Command *command, bool tryQueue) {
} }
else{ else{
//Delete all lower-prioirty commands //Delete all lower-prioirty commands
static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
MutexSafeWrapper safeMutex(&mutexCommands,mutexOwnerId);
for(list<Command*>::iterator i= commands.begin(); i != commands.end();){ for(list<Command*>::iterator i= commands.begin(); i != commands.end();){
if((*i)->getPriority() < command_priority){ if((*i)->getPriority() < command_priority){
if(SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled) if(SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands).enabled)
SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d] Deleting lower priority command [%s]\n",__FILE__,__FUNCTION__,__LINE__,(*i)->toString().c_str()); SystemFlags::OutputDebug(SystemFlags::debugUnitCommands,"In [%s::%s Line: %d] Deleting lower priority command [%s]\n",__FILE__,__FUNCTION__,__LINE__,(*i)->toString().c_str());
static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
MutexSafeWrapper safeMutex(&mutexCommands,mutexOwnerId);
deleteQueuedCommand(*i); deleteQueuedCommand(*i);
i= commands.erase(i); i= commands.erase(i);
safeMutex.ReleaseLock();
} }
else{ else{
++i; ++i;
} }
} }
safeMutex.ReleaseLock();
} }
if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis());
@ -1055,19 +1059,23 @@ CommandResult Unit::finishCommand() {
delete commands.front(); delete commands.front();
commands.erase(commands.begin()); commands.erase(commands.begin());
safeMutex.ReleaseLock(true);
this->unitPath->clear(); this->unitPath->clear();
while (commands.empty() == false) { while (commands.empty() == false) {
if (commands.front()->getUnit() != NULL && livingUnitsp.find(commands.front()->getUnit()) == livingUnitsp.end()) { if (commands.front()->getUnit() != NULL && livingUnitsp.find(commands.front()->getUnit()) == livingUnitsp.end()) {
safeMutex.Lock();
delete commands.front(); delete commands.front();
commands.erase(commands.begin()); commands.erase(commands.begin());
} else { safeMutex.ReleaseLock(true);
}
else {
break; break;
} }
} }
safeMutex.ReleaseLock();
return crSuccess; return crSuccess;
} }
@ -1093,10 +1101,12 @@ CommandResult Unit::cancelCommand() {
delete commands.back(); delete commands.back();
commands.pop_back(); commands.pop_back();
safeMutex.ReleaseLock();
//clear routes //clear routes
this->unitPath->clear(); this->unitPath->clear();
safeMutex.ReleaseLock();
return crSuccess; return crSuccess;
} }
@ -2113,17 +2123,20 @@ void Unit::updateTarget(){
} }
void Unit::clearCommands() { void Unit::clearCommands() {
static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
MutexSafeWrapper safeMutex(&mutexCommands,mutexOwnerId);
this->setCurrentUnitTitle(""); this->setCurrentUnitTitle("");
this->unitPath->clear(); this->unitPath->clear();
while(commands.empty() == false) { while(commands.empty() == false) {
undoCommand(commands.back()); undoCommand(commands.back());
static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
MutexSafeWrapper safeMutex(&mutexCommands,mutexOwnerId);
delete commands.back(); delete commands.back();
commands.pop_back(); commands.pop_back();
safeMutex.ReleaseLock();
} }
safeMutex.ReleaseLock();
} }
void Unit::deleteQueuedCommand(Command *command) { void Unit::deleteQueuedCommand(Command *command) {

View File

@ -275,10 +275,10 @@ void World::updateAllFactionUnits() {
// Prioritize grouped command units so closest units to target go first // Prioritize grouped command units so closest units to target go first
// units // units
const bool sortedUnitsAllowed = true; bool sortedUnitsAllowed = true;
int factionCount = getFactionCount(); std::map<int, std::vector<CommandGroupSorter *> > unitsInFactionsSorted;
std::map<int, std::vector<CommandGroupSorter> > unitsInFactionsSorted; int factionCount = getFactionCount();
if(sortedUnitsAllowed == true) { if(sortedUnitsAllowed == true) {
for(int i = 0; i < factionCount; ++i) { for(int i = 0; i < factionCount; ++i) {
Faction *faction = getFaction(i); Faction *faction = getFaction(i);
@ -286,6 +286,9 @@ void World::updateAllFactionUnits() {
throw runtime_error("faction == NULL"); throw runtime_error("faction == NULL");
} }
std::vector<CommandGroupSorter *> &unitListToSort = unitsInFactionsSorted[faction->getIndex()];
unitListToSort.clear();
int unitCount = faction->getUnitCount(); int unitCount = faction->getUnitCount();
for(int j = 0; j < unitCount; ++j) { for(int j = 0; j < unitCount; ++j) {
Unit *unit = faction->getUnit(j); Unit *unit = faction->getUnit(j);
@ -293,13 +296,16 @@ void World::updateAllFactionUnits() {
throw runtime_error("unit == NULL"); throw runtime_error("unit == NULL");
} }
unitsInFactionsSorted[faction->getIndex()].push_back(CommandGroupSorter(unit)); unitListToSort.push_back(new CommandGroupSorter(unit));
}
if(unitListToSort.size() > 0) {
std::sort(unitListToSort.begin(),unitListToSort.end());
} }
std::vector<CommandGroupSorter> &unitListToSort = unitsInFactionsSorted[faction->getIndex()];
std::sort(unitListToSort.begin(),unitListToSort.end());
} }
} }
//sortedUnitsAllowed = false;
// Signal the faction threads to do any pre-processing // Signal the faction threads to do any pre-processing
for(int i = 0; i < factionCount; ++i) { for(int i = 0; i < factionCount; ++i) {
Faction *faction = getFaction(i); Faction *faction = getFaction(i);
@ -308,7 +314,7 @@ void World::updateAllFactionUnits() {
} }
if(sortedUnitsAllowed == true) { if(sortedUnitsAllowed == true) {
std::vector<CommandGroupSorter> &unitListSorted = unitsInFactionsSorted[faction->getIndex()]; std::vector<CommandGroupSorter *> &unitListSorted = unitsInFactionsSorted[faction->getIndex()];
faction->signalWorkerThread(frameCount,&unitListSorted); faction->signalWorkerThread(frameCount,&unitListSorted);
} }
else { else {
@ -350,7 +356,7 @@ void World::updateAllFactionUnits() {
throw runtime_error("faction == NULL"); throw runtime_error("faction == NULL");
} }
std::vector<CommandGroupSorter> *unitListSorted = NULL; std::vector<CommandGroupSorter *> *unitListSorted = NULL;
int unitCount = faction->getUnitCount(); int unitCount = faction->getUnitCount();
if(sortedUnitsAllowed == true) { if(sortedUnitsAllowed == true) {
unitListSorted = &unitsInFactionsSorted[faction->getIndex()]; unitListSorted = &unitsInFactionsSorted[faction->getIndex()];
@ -360,7 +366,7 @@ void World::updateAllFactionUnits() {
for(int j = 0; j < unitCount; ++j) { for(int j = 0; j < unitCount; ++j) {
Unit *unit = NULL; Unit *unit = NULL;
if(sortedUnitsAllowed == true) { if(sortedUnitsAllowed == true) {
unit = (*unitListSorted)[j].unit; unit = (*unitListSorted)[j]->unit;
} }
else { else {
unit = faction->getUnit(j); unit = faction->getUnit(j);
@ -370,6 +376,11 @@ void World::updateAllFactionUnits() {
} }
unitUpdater.updateUnit(unit); unitUpdater.updateUnit(unit);
if(sortedUnitsAllowed == true) {
delete (*unitListSorted)[j];
(*unitListSorted)[j] = NULL;
}
} }
// int unitCount = faction->getUnitCount(); // int unitCount = faction->getUnitCount();
@ -383,6 +394,8 @@ void World::updateAllFactionUnits() {
// } // }
} }
unitsInFactionsSorted.clear();
if(SystemFlags::VERBOSE_MODE_ENABLED && chrono.getMillis() >= 20) printf("In [%s::%s Line: %d] *** Faction MAIN thread processing 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() >= 20) printf("In [%s::%s Line: %d] *** Faction MAIN thread processing took [%lld] msecs for %d factions for frameCount = %d.\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis(),factionCount,frameCount);
} }