Revert "Add a horrible mutex to make ConfigFile thread-safe"

This reverts commit 3eb8e2d41fc99dba8765e301adf3758df5e0f314.

Fixes a number of isses with ConfigFile, and prevents the hotkey thread
from potentially corrupting the config file.
This commit is contained in:
jp9000 2015-01-23 16:56:58 -08:00
parent ba7a078694
commit 521a90585a
2 changed files with 1 additions and 119 deletions

View File

@ -65,8 +65,6 @@ BOOL ConfigFile::LoadFile(DWORD dwOpenMode)
return 0;
}
OSEnterMutex(hHorribleThreadSafetyMutex);
if(bOpen)
Close();
@ -95,14 +93,11 @@ BOOL ConfigFile::LoadFile(DWORD dwOpenMode)
bOpen = 1;
OSLeaveMutex(hHorribleThreadSafetyMutex);
return 1;
}
void ConfigFile::LoadData()
{
OSEnterMutex(hHorribleThreadSafetyMutex);
TSTR lpCurLine = lpFileData, lpNextLine;
ConfigSection *lpCurSection=NULL;
DWORD i;
@ -161,13 +156,10 @@ void ConfigFile::LoadData()
*lpNextLine = '\r';
}
OSLeaveMutex(hHorribleThreadSafetyMutex);
}
void ConfigFile::Close()
{
OSEnterMutex(hHorribleThreadSafetyMutex);
DWORD i,j,k;
for(i=0; i<Sections.Num(); i++)
@ -196,13 +188,10 @@ void ConfigFile::Close()
}
bOpen = 0;
OSLeaveMutex(hHorribleThreadSafetyMutex);
}
BOOL ConfigFile::SaveAs(CTSTR lpPath)
{
OSEnterMutex(hHorribleThreadSafetyMutex);
XFile newFile;
String tmpPath = lpPath;
@ -211,28 +200,18 @@ BOOL ConfigFile::SaveAs(CTSTR lpPath)
if (newFile.Open(tmpPath, XFILE_WRITE, XFILE_CREATEALWAYS))
{
if (newFile.Write("\xEF\xBB\xBF", 3) != 3)
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return FALSE;
}
if (!newFile.WriteAsUTF8(lpFileData))
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return FALSE;
}
newFile.Close();
if (!OSRenameFile(tmpPath, lpPath))
Log(TEXT("ConfigFile::SaveAs: Unable to move new config file %s to %s"), tmpPath.Array(), lpPath);
strFileName = lpPath;
OSLeaveMutex(hHorribleThreadSafetyMutex);
return TRUE;
}
OSLeaveMutex(hHorribleThreadSafetyMutex);
return FALSE;
}
@ -243,7 +222,6 @@ void ConfigFile::SetFilePath(CTSTR lpPath)
String ConfigFile::GetString(CTSTR lpSection, CTSTR lpKey, CTSTR def)
{
OSEnterMutex(hHorribleThreadSafetyMutex);
assert(lpSection);
assert(lpKey);
@ -258,16 +236,11 @@ String ConfigFile::GetString(CTSTR lpSection, CTSTR lpKey, CTSTR def)
{
ConfigKey &key = section.Keys[j];
if(scmpi(lpKey, key.name) == 0)
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return String(key.ValueList[0]);
}
}
}
}
OSLeaveMutex(hHorribleThreadSafetyMutex);
if(def)
return String(def);
else
@ -276,7 +249,6 @@ String ConfigFile::GetString(CTSTR lpSection, CTSTR lpKey, CTSTR def)
CTSTR ConfigFile::GetStringPtr(CTSTR lpSection, CTSTR lpKey, CTSTR def)
{
OSEnterMutex(hHorribleThreadSafetyMutex);
assert(lpSection);
assert(lpKey);
@ -291,16 +263,11 @@ CTSTR ConfigFile::GetStringPtr(CTSTR lpSection, CTSTR lpKey, CTSTR def)
{
ConfigKey &key = section.Keys[j];
if(scmpi(lpKey, key.name) == 0)
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return key.ValueList[0];
}
}
}
}
OSLeaveMutex(hHorribleThreadSafetyMutex);
if(def)
return def;
else
@ -309,7 +276,6 @@ CTSTR ConfigFile::GetStringPtr(CTSTR lpSection, CTSTR lpKey, CTSTR def)
int ConfigFile::GetInt(CTSTR lpSection, CTSTR lpKey, int def)
{
OSEnterMutex(hHorribleThreadSafetyMutex);
assert(lpSection);
assert(lpKey);
@ -326,35 +292,24 @@ int ConfigFile::GetInt(CTSTR lpSection, CTSTR lpKey, int def)
if(scmpi(lpKey, key.name) == 0)
{
if(scmpi(key.ValueList[0], TEXT("true")) == 0)
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return 1;
}
else if(scmpi(key.ValueList[0], TEXT("false")) == 0)
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return 0;
}
else
{
if(ValidIntString(key.ValueList[0]))
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return tstring_base_to_int(key.ValueList[0], NULL, 0);
}
}
}
}
}
}
OSLeaveMutex(hHorribleThreadSafetyMutex);
return def;
}
DWORD ConfigFile::GetHex(CTSTR lpSection, CTSTR lpKey, DWORD def)
{
OSEnterMutex(hHorribleThreadSafetyMutex);
assert(lpSection);
assert(lpKey);
@ -369,21 +324,16 @@ DWORD ConfigFile::GetHex(CTSTR lpSection, CTSTR lpKey, DWORD def)
{
ConfigKey &key = section.Keys[j];
if(scmpi(lpKey, key.name) == 0)
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return tstring_base_to_int(key.ValueList[0], NULL, 0);
}
}
}
}
OSLeaveMutex(hHorribleThreadSafetyMutex);
return def;
}
float ConfigFile::GetFloat(CTSTR lpSection, CTSTR lpKey, float def)
{
OSEnterMutex(hHorribleThreadSafetyMutex);
assert(lpSection);
assert(lpKey);
@ -398,21 +348,16 @@ float ConfigFile::GetFloat(CTSTR lpSection, CTSTR lpKey, float def)
{
ConfigKey &key = section.Keys[j];
if(scmpi(lpKey, key.name) == 0)
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return (float)tstof(key.ValueList[0]);
}
}
}
}
OSLeaveMutex(hHorribleThreadSafetyMutex);
return def;
}
Color4 ConfigFile::GetColor(CTSTR lpSection, CTSTR lpKey)
{
OSEnterMutex(hHorribleThreadSafetyMutex);
assert(lpSection);
assert(lpKey);
@ -446,12 +391,10 @@ Color4 ConfigFile::GetColor(CTSTR lpSection, CTSTR lpKey)
if(!(strValue = schr(strValue, ',')))
{
ret.w = 1.0f;
OSLeaveMutex(hHorribleThreadSafetyMutex);
return ret;
}
ret.w = float(tstof(++strValue));
OSLeaveMutex(hHorribleThreadSafetyMutex);
return ret;
}
else if(*strValue == '[')
@ -471,18 +414,15 @@ Color4 ConfigFile::GetColor(CTSTR lpSection, CTSTR lpKey)
if(!(strValue = schr(strValue, ',')))
{
ret.w = 1.0f;
OSLeaveMutex(hHorribleThreadSafetyMutex);
return ret;
}
ret.w = (float(tstoi(++strValue))/255.0f)+0.001f;
OSLeaveMutex(hHorribleThreadSafetyMutex);
return ret;
}
else if( (*LPWORD(strValue) == 'x0') ||
(*LPWORD(strValue) == 'X0') )
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return RGBA_to_Vect4(tstring_base_to_int(strValue+2, NULL, 16));
}
}
@ -490,13 +430,11 @@ Color4 ConfigFile::GetColor(CTSTR lpSection, CTSTR lpKey)
}
}
OSLeaveMutex(hHorribleThreadSafetyMutex);
return Color4(0.0f, 0.0f, 0.0f, 0.0f);
}
BOOL ConfigFile::GetStringList(CTSTR lpSection, CTSTR lpKey, StringList &StrList)
{
OSEnterMutex(hHorribleThreadSafetyMutex);
assert(lpSection);
assert(lpKey);
@ -523,13 +461,11 @@ BOOL ConfigFile::GetStringList(CTSTR lpSection, CTSTR lpKey, StringList &StrList
}
}
OSLeaveMutex(hHorribleThreadSafetyMutex);
return bFoundKey;
}
BOOL ConfigFile::GetIntList(CTSTR lpSection, CTSTR lpKey, List<int> &IntList)
{
OSEnterMutex(hHorribleThreadSafetyMutex);
assert(lpSection);
assert(lpKey);
@ -565,13 +501,11 @@ BOOL ConfigFile::GetIntList(CTSTR lpSection, CTSTR lpKey, List<int> &IntList)
}
}
OSLeaveMutex(hHorribleThreadSafetyMutex);
return bFoundKey;
}
BOOL ConfigFile::GetFloatList(CTSTR lpSection, CTSTR lpKey, List<float> &FloatList)
{
OSEnterMutex(hHorribleThreadSafetyMutex);
assert(lpSection);
assert(lpKey);
@ -597,13 +531,11 @@ BOOL ConfigFile::GetFloatList(CTSTR lpSection, CTSTR lpKey, List<float> &FloatLi
}
}
OSLeaveMutex(hHorribleThreadSafetyMutex);
return bFoundKey;
}
BOOL ConfigFile::GetColorList(CTSTR lpSection, CTSTR lpKey, List<Color4> &ColorList)
{
OSEnterMutex(hHorribleThreadSafetyMutex);
assert(lpSection);
assert(lpKey);
@ -677,7 +609,6 @@ BOOL ConfigFile::GetColorList(CTSTR lpSection, CTSTR lpKey, List<Color4> &ColorL
}
}
OSLeaveMutex(hHorribleThreadSafetyMutex);
return bFoundKey;
}
@ -820,7 +751,6 @@ void ConfigFile::SetColorList(CTSTR lpSection, CTSTR lpKey, List<Color4> &ColorL
BOOL ConfigFile::HasKey(CTSTR lpSection, CTSTR lpKey)
{
OSEnterMutex(hHorribleThreadSafetyMutex);
for(unsigned int i=0; i<Sections.Num(); i++)
{
ConfigSection &section = Sections[i];
@ -829,15 +759,11 @@ BOOL ConfigFile::HasKey(CTSTR lpSection, CTSTR lpKey)
for(unsigned int j=0; j<section.Keys.Num(); j++)
{
if(scmpi(section.Keys[j].name, lpKey) == 0)
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return TRUE;
}
}
}
}
OSLeaveMutex(hHorribleThreadSafetyMutex);
return FALSE;
}
@ -845,7 +771,6 @@ BOOL ConfigFile::HasKey(CTSTR lpSection, CTSTR lpKey)
void ConfigFile::SetKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
{
OSEnterMutex(hHorribleThreadSafetyMutex);
assert(lpSection);
assert(lpKey);
TSTR lpTemp = lpFileData, lpEnd = &lpFileData[dwLength], lpSectionStart;
@ -885,8 +810,6 @@ void ConfigFile::SetKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
if(LoadFile(XFILE_OPENEXISTING))
LoadData();
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
}
@ -906,8 +829,6 @@ void ConfigFile::SetKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
if(LoadFile(XFILE_OPENEXISTING))
LoadData();
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
}
else if(*(LPWORD)lpTemp == '//')
@ -925,10 +846,7 @@ void ConfigFile::SetKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
int newlen = slen(newvalue);
if ((*lpTemp == '\r' && *newvalue == '\0') || (lpNextLine - lpTemp == newlen && !scmp_n(lpTemp, newvalue, newlen)))
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
}
String tmpFileName = strFileName;
tmpFileName += TEXT(".tmp");
@ -937,25 +855,13 @@ void ConfigFile::SetKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
if (file.Open(tmpFileName, XFILE_WRITE, XFILE_CREATEALWAYS))
{
if (file.Write("\xEF\xBB\xBF", 3) != 3)
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
}
if (!file.WriteAsUTF8(&lpFileData[2], DWORD(lpTemp - lpFileData - 2)))
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
}
if (!file.WriteAsUTF8(newvalue, slen(newvalue)))
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
}
if (!file.WriteAsUTF8(lpNextLine, slen(lpNextLine) - 2))
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
}
file.Close();
if (!OSRenameFile(tmpFileName, strFileName))
@ -964,8 +870,6 @@ void ConfigFile::SetKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
if(LoadFile(XFILE_OPENEXISTING))
LoadData();
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
}
}
@ -985,13 +889,10 @@ void ConfigFile::SetKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
if(LoadFile(XFILE_OPENEXISTING))
LoadData();
OSLeaveMutex(hHorribleThreadSafetyMutex);
}
void ConfigFile::Remove(CTSTR lpSection, CTSTR lpKey)
{
OSEnterMutex(hHorribleThreadSafetyMutex);
assert(lpSection);
assert(lpKey);
TSTR lpTemp = lpFileData, lpEnd = &lpFileData[dwLength], lpSectionStart;
@ -1014,18 +915,12 @@ void ConfigFile::Remove(CTSTR lpSection, CTSTR lpKey)
}while(lpTemp < lpEnd);
if(!bInSection)
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return; //not possible, usually.
}
do
{
if(*lpTemp == '[')
{
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
}
else if(bInSection && (*(LPWORD)lpTemp != '//'))
{
if((scmpi_n(lpTemp, lpKey, dwKeyNameSize) == 0) && (lpTemp[dwKeyNameSize] == '='))
@ -1039,16 +934,12 @@ void ConfigFile::Remove(CTSTR lpSection, CTSTR lpKey)
if(LoadFile(XFILE_OPENEXISTING))
LoadData();
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
}
}
lpTemp = schr(lpTemp, '\n')+1;
}while(lpTemp < lpEnd);
OSLeaveMutex(hHorribleThreadSafetyMutex);
}
void ConfigFile::AddKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
@ -1090,8 +981,6 @@ void ConfigFile::AddKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
if(LoadFile(XFILE_OPENEXISTING))
LoadData();
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
}
@ -1113,8 +1002,6 @@ void ConfigFile::AddKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
if(LoadFile(XFILE_OPENEXISTING))
LoadData();
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
}
else if(*(LPWORD)lpTemp == '//')
@ -1143,8 +1030,6 @@ void ConfigFile::AddKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
if(LoadFile(XFILE_OPENEXISTING))
LoadData();
OSLeaveMutex(hHorribleThreadSafetyMutex);
return;
}
}
@ -1164,6 +1049,4 @@ void ConfigFile::AddKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue)
if(LoadFile(XFILE_OPENEXISTING))
LoadData();
OSLeaveMutex(hHorribleThreadSafetyMutex);
}

View File

@ -39,7 +39,7 @@ struct ConfigSection
class BASE_EXPORT ConfigFile
{
public:
ConfigFile() : bOpen(0), strFileName(), lpFileData(NULL), dwLength(0) { hHorribleThreadSafetyMutex = OSCreateMutex(); }
ConfigFile() : bOpen(0), strFileName(), lpFileData(NULL), dwLength(0) {}
~ConfigFile() {Close();}
BOOL Create(CTSTR lpConfigFile);
@ -105,5 +105,4 @@ private:
String strFileName;
TSTR lpFileData;
DWORD dwLength;
HANDLE hHorribleThreadSafetyMutex;
};