- thread safe localtime and hopefully back to good performance

This commit is contained in:
SoftCoder 2015-11-21 11:30:23 -08:00
parent 9e0d227172
commit 49ae35e7cf
8 changed files with 77 additions and 37 deletions

View File

@ -6460,20 +6460,22 @@ string Game::saveGame(string name, string path) {
Config &config= Config::getInstance();
// auto name file if using saved file pattern string
if(name == GameConstants::saveGameFilePattern) {
time_t curTime = time(NULL);
struct tm *loctime = localtime (&curTime);
//time_t curTime = time(NULL);
//struct tm *loctime = localtime (&curTime);
struct tm loctime = threadsafe_localtime(systemtime_now());
char szBuf2[100]="";
strftime(szBuf2,100,"%Y%m%d_%H%M%S",loctime);
strftime(szBuf2,100,"%Y%m%d_%H%M%S",&loctime);
char szBuf[8096]="";
snprintf(szBuf,8096,name.c_str(),szBuf2);
name = szBuf;
}
else if(name == GameConstants::saveGameFileAutoTestDefault) {
time_t curTime = time(NULL);
struct tm *loctime = localtime (&curTime);
//time_t curTime = time(NULL);
//struct tm *loctime = localtime (&curTime);
struct tm loctime = threadsafe_localtime(systemtime_now());
char szBuf2[100]="";
strftime(szBuf2,100,"%Y%m%d_%H%M%S",loctime);
strftime(szBuf2,100,"%Y%m%d_%H%M%S",&loctime);
char szBuf[8096]="";
snprintf(szBuf,8096,name.c_str(),szBuf2);
@ -6504,10 +6506,11 @@ string Game::saveGame(string name, string path) {
XmlNode *rootNodeReplay = xmlTreeSaveGame.getRootNode();
//std::map<string,string> mapTagReplacements;
time_t now = time(NULL);
struct tm *loctime = localtime (&now);
//time_t now = time(NULL);
//struct tm *loctime = localtime (&now);
struct tm loctime = threadsafe_localtime(systemtime_now());
char szBuf[4096]="";
strftime(szBuf,4095,"%Y-%m-%d %H:%M:%S",loctime);
strftime(szBuf,4095,"%Y-%m-%d %H:%M:%S",&loctime);
rootNodeReplay->addAttribute("version",glestVersionString, mapTagReplacements);
rootNodeReplay->addAttribute("timestamp",szBuf, mapTagReplacements);
@ -6533,10 +6536,11 @@ string Game::saveGame(string name, string path) {
XmlNode *rootNode = xmlTree.getRootNode();
std::map<string,string> mapTagReplacements;
time_t now = time(NULL);
struct tm *loctime = localtime (&now);
//time_t now = time(NULL);
//struct tm *loctime = localtime (&now);
struct tm loctime = threadsafe_localtime(systemtime_now());
char szBuf[4096]="";
strftime(szBuf,4095,"%Y-%m-%d %H:%M:%S",loctime);
strftime(szBuf,4095,"%Y-%m-%d %H:%M:%S",&loctime);
rootNode->addAttribute("version",glestVersionString, mapTagReplacements);
rootNode->addAttribute("timestamp",szBuf, mapTagReplacements);

View File

@ -2334,10 +2334,11 @@ void Renderer::renderClock() {
}
if(config.getBool("InGameLocalClock","true") == true) {
time_t nowTime = time(NULL);
struct tm *loctime = localtime(&nowTime);
//time_t nowTime = time(NULL);
//struct tm *loctime = localtime(&nowTime);
struct tm loctime = threadsafe_localtime(systemtime_now());
char szBuf2[100]="";
strftime(szBuf2,100,"%H:%M",loctime);
strftime(szBuf2,100,"%H:%M",&loctime);
Lang &lang= Lang::getInstance();
char szBuf[200]="";

View File

@ -588,10 +588,11 @@ void stackdumper(unsigned int type, EXCEPTION_POINTERS *ep, bool fatalExit) {
#endif
if(logFile.is_open() == true) {
time_t curtime = time (NULL);
struct tm *loctime = localtime (&curtime);
//time_t curtime = time (NULL);
//struct tm *loctime = localtime (&curtime);
struct tm loctime = threadsafe_localtime(systemtime_now());
char szBuf2[100]="";
strftime(szBuf2,100,"%Y-%m-%d %H:%M:%S",loctime);
strftime(szBuf2,100,"%Y-%m-%d %H:%M:%S",&loctime);
logFile << "[" << szBuf2 << "] Runtime Error information:" << std::endl;
logFile << "======================================================" << std::endl;

View File

@ -3092,9 +3092,10 @@ std::string ServerInterface::DumpStatsToLog(bool dumpToStringOnly) const {
if(slot->isConnected() == true) {
time_t connectTime = slot->getConnectedTime();
struct tm *loctime = localtime (&connectTime);
//struct tm *loctime = localtime (&connectTime);
struct tm loctime = threadsafe_localtime(connectTime);
char szBuf[8096] = "";
strftime(szBuf,100,"%Y-%m-%d %H:%M:%S",loctime);
strftime(szBuf,100,"%Y-%m-%d %H:%M:%S",&loctime);
const int HOURS_IN_DAY = 24;
const int MINUTES_IN_HOUR = 60;

View File

@ -25,6 +25,7 @@
#include <utility>
#include <SDL.h>
#include <map>
#include <chrono>
#include "leak_dumper.h"
#if (defined WIN32) && !(defined snprintf)
@ -90,6 +91,12 @@ public:
virtual void ShellCommandOutput_CallbackEvent(string cmd,char *output,void *userdata) = 0;
};
typedef std::chrono::time_point<std::chrono::system_clock> system_time_point;
tm threadsafe_localtime(const time_t &time);
// extracting std::time_t from std:chrono for "now"
time_t systemtime_now();
// =====================================================
// class PerformanceTimer
// =====================================================

View File

@ -102,6 +102,26 @@ int ScreenHeight = 600;
}
/*
A thread safe localtime proxy
*/
tm threadsafe_localtime(const time_t &time) {
tm tm_snapshot;
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
localtime_s(&tm_snapshot, &time);
#else
localtime_r(&time, &tm_snapshot); // POSIX
#endif
return tm_snapshot;
}
// extracting std::time_t from std:chrono for "now"
time_t systemtime_now() {
system_time_point system_now = std::chrono::system_clock::now();
return std::chrono::system_clock::to_time_t(system_now);
}
// =====================================
// PerformanceTimer
// =====================================
@ -786,9 +806,10 @@ pair<bool,time_t> hasCachedFileCRCValue(string crcCacheFile, uint32 &value) {
value = crcValue;
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) {
struct tm *loctime = localtime (&refreshDate);
//struct tm *loctime = localtime (&refreshDate);
struct tm loctime = threadsafe_localtime(refreshDate);
char szBuf1[100]="";
strftime(szBuf1,100,"%Y-%m-%d %H:%M:%S",loctime);
strftime(szBuf1,100,"%Y-%m-%d %H:%M:%S",&loctime);
SystemFlags::OutputDebug(SystemFlags::debugSystem,
"=-=-=-=- READ CACHE for Cache file [%s] refreshDate = %ld [%s], crcValue = %u\n",
@ -796,19 +817,20 @@ pair<bool,time_t> hasCachedFileCRCValue(string crcCacheFile, uint32 &value) {
}
}
else {
time_t now = time(NULL);
struct tm *loctime = localtime (&now);
//time_t now = time(NULL);
//struct tm *loctime = localtime (&now);
struct tm loctime = threadsafe_localtime(systemtime_now());
char szBuf1[100]="";
strftime(szBuf1,100,"%Y-%m-%d %H:%M:%S",loctime);
strftime(szBuf1,100,"%Y-%m-%d %H:%M:%S",&loctime);
loctime = localtime (&refreshDate);
loctime = threadsafe_localtime(refreshDate);
char szBuf2[100]="";
strftime(szBuf2,100,"%Y-%m-%d %H:%M:%S",loctime);
strftime(szBuf2,100,"%Y-%m-%d %H:%M:%S",&loctime);
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,
"=-=-=-=- NEED TO CALCULATE CRC for Cache file [%s] now = %ld [%s], refreshDate = %ld [%s], crcValue = %u\n",
crcCacheFile.c_str(),now, szBuf1, refreshDate, szBuf2, crcValue);
crcCacheFile.c_str(), systemtime_now(), szBuf1, refreshDate, szBuf2, crcValue);
}
}
}
@ -844,9 +866,10 @@ void writeCachedFileCRCValue(string crcCacheFile, uint32 &crcValue, string actua
time_t now = time(NULL);
time_t refreshDate = now + (REFRESH_CRC_DAY_SECONDS * offset);
struct tm *loctime = localtime (&refreshDate);
//struct tm *loctime = localtime (&refreshDate);
struct tm loctime = threadsafe_localtime(refreshDate);
char szBuf1[100]="";
strftime(szBuf1,100,"%Y-%m-%d %H:%M:%S",loctime);
strftime(szBuf1,100,"%Y-%m-%d %H:%M:%S",&loctime);
string writeGameVer = Shared::PlatformByteOrder::toCommonEndian(gameVersion);
string writeGameGITVersion = Shared::PlatformByteOrder::toCommonEndian(gameGITVersion);
@ -904,9 +927,10 @@ time_t getFolderTreeContentsCheckSumRecursivelyLastGenerated(vector<string> path
uint32 crcValue = 0;
pair<bool,time_t> crcResult = hasCachedFileCRCValue(crcCacheFile, crcValue);
if(crcResult.first == true) {
struct tm *loctime = localtime (&crcResult.second);
//struct tm *loctime = localtime (&crcResult.second);
struct tm loctime = threadsafe_localtime(crcResult.second);
char szBuf1[100]="";
strftime(szBuf1,100,"%Y-%m-%d %H:%M:%S",loctime);
strftime(szBuf1,100,"%Y-%m-%d %H:%M:%S",&loctime);
if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] scanning folders found CACHED FILE for cacheKey [%s] last updated [%s]\n",__FILE__,__FUNCTION__,__LINE__,cacheKey.c_str(),szBuf1);
//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\n-------------- In [%s::%s Line: %d] scanning folders found CACHED FILE for cacheKey [%s] last updated [%s]\n",__FILE__,__FUNCTION__,__LINE__,cacheKey.c_str(),szBuf1);

View File

@ -35,10 +35,11 @@ void AllocRegistry::dump(const char *path) {
int leakCount=0;
size_t leakBytes=0;
time_t debugTime = time(NULL);
struct tm *loctime = localtime (&debugTime);
//time_t debugTime = time(NULL);
//struct tm *loctime = localtime (&debugTime);
struct tm loctime = threadsafe_localtime(systemtime_now());
char szBuf2[100]="";
strftime(szBuf2,100,"%Y-%m-%d %H:%M:%S",loctime);
strftime(szBuf2,100,"%Y-%m-%d %H:%M:%S",&loctime);
#ifdef WIN32
FILE* f= _wfopen(utf8_decode(path).c_str(), L"wt");

View File

@ -454,8 +454,9 @@ void SystemFlags::logDebugEntry(DebugType type, string debugEntry, time_t debugT
// Get the current time.
// time_t curtime = time (NULL);
// Convert it to local time representation.
struct tm *loctime = localtime (&debugTime);
strftime(szBuf2,100,"%Y-%m-%d %H:%M:%S",loctime);
//struct tm *loctime = localtime (&debugTime);
std::tm loctime = threadsafe_localtime(debugTime);
strftime(szBuf2,100,"%Y-%m-%d %H:%M:%S",&loctime);
}
/*
va_list argList;