From fda239b10a70dae8de6ef15455136dd2c296f3f2 Mon Sep 17 00:00:00 2001 From: Vojtech Bocek Date: Wed, 7 Jan 2015 22:55:13 +0100 Subject: Add locking to DataManager, remove unused C parts Change-Id: I33e8f96611bd9a6795bdd3fb588c20716bdb5929 Signed-off-by: Vojtech Bocek --- data.cpp | 151 ++++++++++++++------------------------------------------------- data.h | 41 ----------------- data.hpp | 8 +--- 3 files changed, 33 insertions(+), 167 deletions(-) delete mode 100644 data.h diff --git a/data.cpp b/data.cpp index 88bc86ada..3259a2132 100644 --- a/data.cpp +++ b/data.cpp @@ -59,7 +59,6 @@ extern "C" { #include "twcommon.h" - #include "data.h" #include "gui/pages.h" #include "minuitwrp/minui.h" void gui_notifyVarChange(const char *name, const char* value); @@ -79,6 +78,8 @@ extern blanktimer blankTimer; #endif extern bool datamedia; +pthread_mutex_t DataManager::m_valuesLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER; + // Device ID functions void DataManager::sanitize_device_id(char* device_id) { const char* whitelist ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-._"; @@ -221,7 +222,10 @@ void DataManager::get_device_id(void) { int DataManager::ResetDefaults() { + pthread_mutex_lock(&m_valuesLock); mValues.clear(); + pthread_mutex_unlock(&m_valuesLock); + mConstValues.clear(); SetDefaultValues(); return 0; @@ -270,6 +274,8 @@ int DataManager::LoadValues(const string filename) map::iterator pos; + pthread_mutex_lock(&m_valuesLock); + pos = mValues.find(Name); if (pos != mValues.end()) { @@ -278,6 +284,9 @@ int DataManager::LoadValues(const string filename) } else mValues.insert(TNameValuePair(Name, TStrIntPair(Value, 1))); + + pthread_mutex_unlock(&m_valuesLock); + #ifndef TW_NO_SCREEN_TIMEOUT if (Name == "tw_screen_timeout_secs") blankTimer.setTime(atoi(Value.c_str())); @@ -319,6 +328,8 @@ int DataManager::SaveValues() int file_version = FILE_VERSION; fwrite(&file_version, 1, sizeof(int), out); + pthread_mutex_lock(&m_valuesLock); + map::iterator iter; for (iter = mValues.begin(); iter != mValues.end(); ++iter) { @@ -333,6 +344,9 @@ int DataManager::SaveValues() fwrite(iter->second.first.c_str(), 1, length, out); } } + + pthread_mutex_unlock(&m_valuesLock); + fclose(out); tw_set_default_metadata(mBackingFile.c_str()); #endif // ifdef TW_OEM_BUILD @@ -364,12 +378,16 @@ int DataManager::GetValue(const string varName, string& value) return 0; } + pthread_mutex_lock(&m_valuesLock); map::iterator pos; pos = mValues.find(localStr); - if (pos == mValues.end()) + if (pos == mValues.end()){ + pthread_mutex_unlock(&m_valuesLock); return -1; + } value = pos->second.first; + pthread_mutex_unlock(&m_valuesLock); return 0; } @@ -406,25 +424,6 @@ unsigned long long DataManager::GetValue(const string varName, unsigned long lon return 0; } -// This is a dangerous function. It will create the value if it doesn't exist so it has a valid c_str -string& DataManager::GetValueRef(const string varName) -{ - if (!mInitialized) - SetDefaultValues(); - - map::iterator constPos; - constPos = mConstValues.find(varName); - if (constPos != mConstValues.end()) - return constPos->second; - - map::iterator pos; - pos = mValues.find(varName); - if (pos == mValues.end()) - pos = (mValues.insert(TNameValuePair(varName, TStrIntPair("", 0)))).first; - - return pos->second.first; -} - // This function will return an empty string if the value doesn't exist string DataManager::GetStrValue(const string varName) { @@ -457,6 +456,8 @@ int DataManager::SetValue(const string varName, string value, int persist /* = 0 if (constChk != mConstValues.end()) return -1; + pthread_mutex_lock(&m_valuesLock); + map::iterator pos; pos = mValues.find(varName); if (pos == mValues.end()) @@ -467,6 +468,8 @@ int DataManager::SetValue(const string varName, string value, int persist /* = 0 if (pos->second.second != 0) SaveValues(); + pthread_mutex_unlock(&m_valuesLock); + #ifndef TW_NO_SCREEN_TIMEOUT if (varName == "tw_screen_timeout_secs") { blankTimer.setTime(atoi(value.c_str())); @@ -535,13 +538,15 @@ void DataManager::DumpValues() { map::iterator iter; gui_print("Data Manager dump - Values with leading X are persisted.\n"); + pthread_mutex_lock(&m_valuesLock); for (iter = mValues.begin(); iter != mValues.end(); ++iter) gui_print("%c %s=%s\n", iter->second.second ? 'X' : ' ', iter->first.c_str(), iter->second.first.c_str()); + pthread_mutex_unlock(&m_valuesLock); } void DataManager::update_tz_environment_variables(void) { - setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1); + setenv("TZ", GetStrValue(TW_TIME_ZONE_VAR).c_str(), 1); tzset(); } @@ -589,6 +594,8 @@ void DataManager::SetDefaultValues() get_device_id(); + pthread_mutex_lock(&m_valuesLock); + mInitialized = 1; mConstValues.insert(make_pair("true", "1")); @@ -848,6 +855,8 @@ void DataManager::SetDefaultValues() mConstValues.insert(make_pair("tw_has_mtp", "0")); mConstValues.insert(make_pair("tw_mtp_enabled", "0")); #endif + + pthread_mutex_unlock(&m_valuesLock); } // Magic Values @@ -1012,7 +1021,7 @@ void DataManager::ReadSettingsFile(void) memset(mkdir_path, 0, sizeof(mkdir_path)); memset(settings_file, 0, sizeof(settings_file)); - sprintf(mkdir_path, "%s/TWRP", DataManager_GetSettingsStoragePath()); + sprintf(mkdir_path, "%s/TWRP", GetSettingsStoragePath().c_str()); sprintf(settings_file, "%s/.twrps", mkdir_path); if (!PartitionManager.Mount_Settings_Storage(false)) @@ -1042,107 +1051,11 @@ string DataManager::GetCurrentStoragePath(void) return GetStrValue("tw_storage_path"); } -string& DataManager::CGetCurrentStoragePath() -{ - return GetValueRef("tw_storage_path"); -} - string DataManager::GetSettingsStoragePath(void) { return GetStrValue("tw_settings_path"); } -string& DataManager::CGetSettingsStoragePath() -{ - return GetValueRef("tw_settings_path"); -} - -extern "C" int DataManager_ResetDefaults(void) -{ - return DataManager::ResetDefaults(); -} - -extern "C" void DataManager_LoadDefaults(void) -{ - return DataManager::SetDefaultValues(); -} - -extern "C" int DataManager_LoadValues(const char* filename) -{ - return DataManager::LoadValues(filename); -} - -extern "C" int DataManager_Flush(void) -{ - return DataManager::Flush(); -} - -extern "C" int DataManager_GetValue(const char* varName, char* value) -{ - int ret; - string str; - - ret = DataManager::GetValue(varName, str); - if (ret == 0) - strcpy(value, str.c_str()); - return ret; -} - -extern "C" const char* DataManager_GetStrValue(const char* varName) -{ - string& str = DataManager::GetValueRef(varName); - return str.c_str(); -} - -extern "C" const char* DataManager_GetCurrentStoragePath(void) -{ - string& str = DataManager::CGetCurrentStoragePath(); - return str.c_str(); -} - -extern "C" const char* DataManager_GetSettingsStoragePath(void) -{ - string& str = DataManager::CGetSettingsStoragePath(); - return str.c_str(); -} - -extern "C" int DataManager_GetIntValue(const char* varName) -{ - return DataManager::GetIntValue(varName); -} - -extern "C" int DataManager_SetStrValue(const char* varName, char* value) -{ - return DataManager::SetValue(varName, value, 0); -} - -extern "C" int DataManager_SetIntValue(const char* varName, int value) -{ - return DataManager::SetValue(varName, value, 0); -} - -extern "C" int DataManager_SetFloatValue(const char* varName, float value) -{ - return DataManager::SetValue(varName, value, 0); -} - -extern "C" int DataManager_ToggleIntValue(const char* varName) -{ - if (DataManager::GetIntValue(varName)) - return DataManager::SetValue(varName, 0); - else - return DataManager::SetValue(varName, 1); -} - -extern "C" void DataManager_DumpValues(void) -{ - return DataManager::DumpValues(); -} - -extern "C" void DataManager_ReadSettingsFile(void) -{ - return DataManager::ReadSettingsFile(); -} void DataManager::Vibrate(const string varName) { int vib_value = 0; diff --git a/data.h b/data.h deleted file mode 100644 index 0ca9f4eb6..000000000 --- a/data.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - Copyright 2012 bigbiff/Dees_Troy TeamWin - This file is part of TWRP/TeamWin Recovery Project. - - TWRP is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - TWRP is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with TWRP. If not, see . -*/ - -#ifndef _DATA_HEADER -#define _DATA_HEADER - -int DataManager_ResetDefaults(void); -void DataManager_LoadDefaults(void); -int DataManager_LoadValues(const char* filename); -int DataManager_Flush(void); -const char* DataManager_GetStrValue(const char* varName); -const char* DataManager_GetCurrentStoragePath(void); -const char* DataManager_GetSettingsStoragePath(void); -int DataManager_GetIntValue(const char* varName); - -int DataManager_SetStrValue(const char* varName, char* value); -int DataManager_SetIntValue(const char* varName, int value); -int DataManager_SetFloatValue(const char* varName, float value); - -int DataManager_ToggleIntValue(const char* varName); - -void DataManager_DumpValues(void); -void DataManager_ReadSettingsFile(void); - -#endif // _DATA_HEADER - diff --git a/data.hpp b/data.hpp index df57f234a..e1ea738cc 100644 --- a/data.hpp +++ b/data.hpp @@ -38,9 +38,6 @@ public: static int GetValue(const string varName, float& value); static unsigned long long GetValue(const string varName, unsigned long long& value); - // This is a dangerous function. It will create the value if it doesn't exist so it has a valid c_str - static string& GetValueRef(const string varName); - // Helper functions static string GetStrValue(const string varName); static int GetIntValue(const string varName); @@ -62,16 +59,12 @@ public: static void ReadSettingsFile(void); static string GetCurrentStoragePath(void); - static string& CGetCurrentStoragePath(); static string GetSettingsStoragePath(void); - static string& CGetSettingsStoragePath(); protected: typedef pair TStrIntPair; - typedef pair TStrULLPair; typedef pair TNameValuePair; static map mValues; - static map mULLValues; static string mBackingFile; static int mInitialized; @@ -86,6 +79,7 @@ private: static void sanitize_device_id(char* device_id); static void get_device_id(void); + static pthread_mutex_t m_valuesLock; }; #endif // _DATAMANAGER_HPP_HEADER -- cgit v1.2.3