- fixes for some threading shutdown bugs

- We now display when a screenshot is taken in game showing an ingame console message
- Updated AI to attack closest attackers
- Updated found enemy to be closest attacker
This commit is contained in:
Mark Vejvoda
2011-03-28 21:04:47 +00:00
parent 8be4bafdcf
commit 3ed636aae2
20 changed files with 514 additions and 80 deletions

View File

@@ -125,13 +125,13 @@ protected:
public:
LogFileThread();
virtual ~LogFileThread();
virtual void execute();
void addLogEntry(SystemFlags::DebugType type, string logEntry);
std::size_t getLogEntryBufferCount();
virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false);
};
}}//end namespace
#endif

View File

@@ -119,12 +119,16 @@ void FileCRCPreCacheThread::execute() {
workerThread->start();
consumedWorkers += currentWorkerMax;
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Spawning CRC thread, preCacheWorkerThreadList.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,(int)preCacheWorkerThreadList.size());
if(consumedWorkers >= techPaths.size()) {
break;
}
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Waiting for Spawned CRC threads to complete = preCacheWorkerThreadList.size()\n",__FILE__,__FUNCTION__,__LINE__,(int)preCacheWorkerThreadList.size());
sleep(0);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Waiting for Spawned CRC threads to complete, preCacheWorkerThreadList.size() = %d\n",__FILE__,__FUNCTION__,__LINE__,(int)preCacheWorkerThreadList.size());
bool hasRunningWorkerThread = true;
for(;hasRunningWorkerThread == true;) {
hasRunningWorkerThread = false;
@@ -137,11 +141,13 @@ void FileCRCPreCacheThread::execute() {
if(workerThread->getRunningStatus() == true) {
hasRunningWorkerThread = true;
if(getQuitStatus() == true) {
if(getQuitStatus() == true &&
workerThread->getQuitStatus() == false) {
workerThread->signalQuit();
}
}
else if(workerThread->getRunningStatus() == false) {
sleep(10);
delete workerThread;
preCacheWorkerThreadList[idx] = NULL;
}
@@ -211,7 +217,8 @@ void FileCRCPreCacheThread::execute() {
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unknown error\n",__FILE__,__FUNCTION__,__LINE__);
}
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] FILE CRC PreCache thread is exiting\n",__FILE__,__FUNCTION__,__LINE__);
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] FILE CRC PreCache thread is exiting, getDeleteSelfOnExecutionDone() = %d\n",__FILE__,__FUNCTION__,__LINE__,getDeleteSelfOnExecutionDone());
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] FILE CRC PreCache thread is exiting, getDeleteSelfOnExecutionDone() = %d\n",__FILE__,__FUNCTION__,__LINE__,getDeleteSelfOnExecutionDone());
}
deleteSelfIfRequired();
}
@@ -378,11 +385,18 @@ bool SimpleTaskThread::getTaskSignalled() {
LogFileThread::LogFileThread() : BaseThread() {
logList.clear();
lastSaveToDisk = time(NULL);
static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
mutexLogList.setOwnerId(mutexOwnerId);
}
LogFileThread::~LogFileThread() {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#1 In [%s::%s Line: %d] LogFile thread is deleting\n",__FILE__,__FUNCTION__,__LINE__);
}
void LogFileThread::addLogEntry(SystemFlags::DebugType type, string logEntry) {
static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
MutexSafeWrapper safeMutex(&mutexLogList,mutexOwnerId);
mutexLogList.setOwnerId(mutexOwnerId);
LogFileEntry entry;
entry.type = type;
entry.entry = logEntry;
@@ -398,7 +412,6 @@ bool LogFileThread::checkSaveCurrentLogBufferToDisk() {
bool ret = false;
if(difftime(time(NULL),lastSaveToDisk) >= 5 ||
LogFileThread::getLogEntryBufferCount() >= 1000000) {
//LogFileThread::getLogEntryBufferCount() >= 10000) {
lastSaveToDisk = time(NULL);
ret = true;
}
@@ -409,6 +422,7 @@ void LogFileThread::execute() {
bool mustDeleteSelf = false;
{
RunningStatusSafeWrapper runningStatus(this);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("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__);
if(getQuitStatus() == true) {
@@ -416,9 +430,11 @@ void LogFileThread::execute() {
return;
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__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) {
@@ -430,7 +446,9 @@ void LogFileThread::execute() {
}
// Ensure remaining entryies are logged to disk on shutdown
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
saveToDisk(true,false);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
catch(const exception &ex) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
@@ -439,28 +457,43 @@ void LogFileThread::execute() {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] UNKNOWN Error\n",__FILE__,__FUNCTION__,__LINE__);
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] LogFile thread is exiting\n",__FILE__,__FUNCTION__,__LINE__);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] LogFile thread is starting to exit\n",__FILE__,__FUNCTION__,__LINE__);
mustDeleteSelf = getDeleteSelfOnExecutionDone();
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] LogFile thread is exiting, mustDeleteSelf = %d\n",__FILE__,__FUNCTION__,__LINE__,mustDeleteSelf);
}
if(mustDeleteSelf == true) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] LogFile thread is deleting self\n",__FILE__,__FUNCTION__,__LINE__);
delete this;
return;
}
}
std::size_t LogFileThread::getLogEntryBufferCount() {
static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__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);
signalQuit();
}
return ret;
}
void LogFileThread::saveToDisk(bool forceSaveAll,bool logListAlreadyLocked) {
static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__);
MutexSafeWrapper safeMutex(NULL,mutexOwnerId);
if(logListAlreadyLocked == false) {
safeMutex.setMutex(&mutexLogList);
mutexLogList.setOwnerId(mutexOwnerId);
}
std::size_t logCount = logList.size();
@@ -480,7 +513,14 @@ void LogFileThread::saveToDisk(bool forceSaveAll,bool logListAlreadyLocked) {
}
safeMutex.Lock();
logList.erase(logList.begin(),logList.begin() + logCount);
if(logList.size() > 0) {
if(logList.size() <= logCount) {
char szBuf[1024]="";
sprintf(szBuf,"logList.size() <= logCount [%lld][%lld]",(long long int)logList.size(),(long long int)logCount);
throw runtime_error(szBuf);
}
logList.erase(logList.begin(),logList.begin() + logCount);
}
safeMutex.ReleaseLock();
}
}

View File

@@ -67,12 +67,16 @@ FTPServerThread::FTPServerThread(std::pair<string,string> mapsPath,
}
FTPServerThread::~FTPServerThread() {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
ftpShutdown();
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
// Remove any UPNP port forwarded ports
UPNP_Tools::upnp_rem_redirect(ServerSocket::getFTPServerPort());
for(int clientIndex = 1; clientIndex <= maxPlayers; ++clientIndex) {
UPNP_Tools::upnp_rem_redirect(ServerSocket::getFTPServerPort() + clientIndex);
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
void FTPServerThread::signalQuit() {
@@ -227,6 +231,7 @@ void FTPServerThread::execute() {
}
if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] FTP Server thread is exiting\n",__FILE__,__FUNCTION__,__LINE__);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] FTP Server thread is exiting\n",__FILE__,__FUNCTION__,__LINE__);
}
}

View File

@@ -69,7 +69,7 @@ bool SystemFlags::getThreadedLoggerRunning() {
std::size_t SystemFlags::getLogEntryBufferCount() {
std::size_t ret = 0;
if(threadLogger != NULL) {
if(threadLogger != NULL && threadLogger->getRunningStatus() == true) {
ret = threadLogger->getLogEntryBufferCount();
}
return ret;
@@ -187,7 +187,18 @@ CURL *SystemFlags::initHTTP() {
SystemFlags::SystemFlagsType & SystemFlags::getSystemSettingType(DebugType type) {
if(SystemFlags::debugLogFileList == NULL) {
SystemFlags::init(false);
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(threadLogger == NULL && SystemFlags::SHUTDOWN_PROGRAM_MODE == true) {
//throw runtime_error("threadLogger == NULL && SystemFlags::SHUTDOWN_PROGRAM_MODE == true");
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] ERROR threadLogger == NULL && SystemFlags::SHUTDOWN_PROGRAM_MODE == true\n",__FILE__,__FUNCTION__,__LINE__);
static SystemFlagsType *result = new SystemFlagsType();
result->enabled = SystemFlags::VERBOSE_MODE_ENABLED;
return *result;
}
else {
SystemFlags::init(false);
}
}
return (*debugLogFileList)[type];
@@ -195,8 +206,15 @@ SystemFlags::SystemFlagsType & SystemFlags::getSystemSettingType(DebugType type)
void SystemFlags::init(bool haveSpecialOutputCommandLineOption) {
SystemFlags::haveSpecialOutputCommandLineOption = haveSpecialOutputCommandLineOption;
//if(SystemFlags::debugLogFileList.size() == 0) {
if(SystemFlags::debugLogFileList == NULL) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(threadLogger == NULL && SystemFlags::SHUTDOWN_PROGRAM_MODE == true) {
//throw runtime_error("threadLogger == NULL && SystemFlags::SHUTDOWN_PROGRAM_MODE == true");
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] ERROR threadLogger == NULL && SystemFlags::SHUTDOWN_PROGRAM_MODE == true\n",__FILE__,__FUNCTION__,__LINE__);
return;
}
SystemFlags::debugLogFileList = new std::map<SystemFlags::DebugType,SystemFlags::SystemFlagsType>();
(*SystemFlags::debugLogFileList)[SystemFlags::debugSystem] = SystemFlags::SystemFlagsType(SystemFlags::debugSystem);
@@ -211,6 +229,7 @@ void SystemFlags::init(bool haveSpecialOutputCommandLineOption) {
}
if(threadLogger == NULL && SystemFlags::SHUTDOWN_PROGRAM_MODE == false) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
threadLogger = new LogFileThread();
threadLogger->start();
sleep(1);
@@ -219,7 +238,7 @@ void SystemFlags::init(bool haveSpecialOutputCommandLineOption) {
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(curl_handle == NULL) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("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__);
curl_handle = SystemFlags::initHTTP();
@@ -265,20 +284,40 @@ void SystemFlags::cleanupHTTP(CURL **handle, bool globalCleanup) {
SystemFlags::~SystemFlags() {
SystemFlags::Close();
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(curl_handle != NULL) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
SystemFlags::cleanupHTTP(&curl_handle, true);
curl_handle = NULL;
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
void SystemFlags::Close() {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(threadLogger != NULL) {
SystemFlags::ENABLE_THREADED_LOGGING = false;
//SystemFlags::SHUTDOWN_PROGRAM_MODE=true;
time_t elapsed = time(NULL);
threadLogger->signalQuit();
threadLogger->shutdownAndWait();
delete threadLogger;
threadLogger = NULL;
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//threadLogger->shutdownAndWait();
for(;threadLogger->canShutdown(false) == false &&
difftime(time(NULL),elapsed) <= 15;) {
//sleep(150);
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(threadLogger->canShutdown(true)) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
delete threadLogger;
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
threadLogger = NULL;
//delete threadLogger;
//threadLogger = NULL;
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
if(SystemFlags::debugLogFileList != NULL) {
@@ -295,6 +334,8 @@ void SystemFlags::Close() {
delete SystemFlags::debugLogFileList;
SystemFlags::debugLogFileList = NULL;
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(SystemFlags::lockFile != -1) {
#ifndef WIN32
close(SystemFlags::lockFile);
@@ -310,18 +351,31 @@ void SystemFlags::Close() {
}
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(SystemFlags::debugLogFileList != NULL) {
if(SystemFlags::haveSpecialOutputCommandLineOption == false) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("END Closing logfiles\n");
}
}
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
void SystemFlags::handleDebug(DebugType type, const char *fmt, ...) {
if(SystemFlags::debugLogFileList == NULL) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(threadLogger == NULL && SystemFlags::SHUTDOWN_PROGRAM_MODE == true) {
//throw runtime_error("threadLogger == NULL && SystemFlags::SHUTDOWN_PROGRAM_MODE == true");
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] ERROR threadLogger == NULL && SystemFlags::SHUTDOWN_PROGRAM_MODE == true\n",__FILE__,__FUNCTION__,__LINE__);
//return;
}
SystemFlags::init(false);
}
SystemFlags::SystemFlagsType &currentDebugLog = (*SystemFlags::debugLogFileList)[type];
//SystemFlags::SystemFlagsType &currentDebugLog = (*SystemFlags::debugLogFileList)[type];
SystemFlags::SystemFlagsType &currentDebugLog =getSystemSettingType(type);
if(currentDebugLog.enabled == false) {
return;
}
@@ -355,9 +409,11 @@ void SystemFlags::handleDebug(DebugType type, const char *fmt, ...) {
void SystemFlags::logDebugEntry(DebugType type, string debugEntry, time_t debugTime) {
if(SystemFlags::debugLogFileList == NULL) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
SystemFlags::init(false);
}
SystemFlags::SystemFlagsType &currentDebugLog = (*SystemFlags::debugLogFileList)[type];
//SystemFlags::SystemFlagsType &currentDebugLog = (*SystemFlags::debugLogFileList)[type];
SystemFlags::SystemFlagsType &currentDebugLog =getSystemSettingType(type);
if(currentDebugLog.enabled == false) {
return;
}