From: Maksym Mamontov Date: Fri, 8 Nov 2024 10:53:38 +0000 (+0200) Subject: Merge remote-tracking branch 'github/master' X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/2196a3a0cdc5384c082febb7f4aa5994cc7d80db?hp=73bee151e048054bde9a39c5ad2efec25af9c77b Merge remote-tracking branch 'github/master' --- diff --git a/include/stg/locker.h b/include/stg/locker.h deleted file mode 100644 index 16b0323f..00000000 --- a/include/stg/locker.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * This program 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 2 of the License, or - * (at your option) any later version. - * - * This program 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 this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Author : Boris Mikhailenko - */ - -/* - $Revision: 1.5 $ - $Date: 2010/03/04 11:57:11 $ - $Author: faust $ -*/ - - -#ifndef STG_LOCKER_H -#define STG_LOCKER_H - -#include - -//----------------------------------------------------------------------------- -class STG_LOCKER -{ -public: - explicit STG_LOCKER(pthread_mutex_t& m) - : mutex(&m) - { - pthread_mutex_lock(mutex); - } - explicit STG_LOCKER(pthread_mutex_t * m) - : mutex(m) - { - pthread_mutex_lock(mutex); - } - - ~STG_LOCKER() - { - pthread_mutex_unlock(mutex); - } -private: - STG_LOCKER(const STG_LOCKER & rvalue); - STG_LOCKER & operator=(const STG_LOCKER & rvalue); - - pthread_mutex_t * mutex; -}; -//----------------------------------------------------------------------------- - -#endif //STG_LOCKER_H diff --git a/include/stg/user_property.h b/include/stg/user_property.h index d73a636b..3400abd0 100644 --- a/include/stg/user_property.h +++ b/include/stg/user_property.h @@ -7,7 +7,6 @@ #include "stg/subscriptions.h" #include "stg/admin_conf.h" #include "stg/logger.h" -#include "stg/locker.h" #include "stg/settings.h" #include "stg/scriptexecuter.h" #include "stg/common.h" diff --git a/projects/rscriptd/listener.cpp b/projects/rscriptd/listener.cpp index a1193527..e4f1e2cd 100644 --- a/projects/rscriptd/listener.cpp +++ b/projects/rscriptd/listener.cpp @@ -22,7 +22,6 @@ #include "listener.h" #include "stg/scriptexecuter.h" -#include "stg/locker.h" #include "stg/common.h" #include "stg/const.h" diff --git a/projects/stargazer/corps_impl.h b/projects/stargazer/corps_impl.h index 193c5dbd..e4601301 100644 --- a/projects/stargazer/corps_impl.h +++ b/projects/stargazer/corps_impl.h @@ -22,7 +22,6 @@ #include "stg/corporations.h" #include "stg/corp_conf.h" -#include "stg/locker.h" #include "stg/logger.h" #include diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp index c607b2ac..60d3846c 100644 --- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp +++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp @@ -25,7 +25,6 @@ #include "inetaccess.h" #include "stg/common.h" -#include "stg/locker.h" #include "stg/tariff.h" #include "stg/settings.h" diff --git a/projects/stargazer/plugins/other/ping/ping.cpp b/projects/stargazer/plugins/other/ping/ping.cpp index 2cbd1f8b..892331f6 100644 --- a/projects/stargazer/plugins/other/ping/ping.cpp +++ b/projects/stargazer/plugins/other/ping/ping.cpp @@ -1,7 +1,6 @@ #include "ping.h" #include "stg/user.h" -#include "stg/locker.h" #include "stg/user_property.h" #include diff --git a/projects/stargazer/plugins/other/rscript/rscript.cpp b/projects/stargazer/plugins/other/rscript/rscript.cpp index 36e84be8..24a8bd71 100644 --- a/projects/stargazer/plugins/other/rscript/rscript.cpp +++ b/projects/stargazer/plugins/other/rscript/rscript.cpp @@ -24,7 +24,6 @@ #include "ur_functor.h" #include "stg/common.h" -#include "stg/locker.h" #include "stg/users.h" #include "stg/user_property.h" #include "stg/logger.h" diff --git a/projects/stargazer/plugins/store/files/file_store.cpp b/projects/stargazer/plugins/store/files/file_store.cpp index eedb60e8..d654a28c 100644 --- a/projects/stargazer/plugins/store/files/file_store.cpp +++ b/projects/stargazer/plugins/store/files/file_store.cpp @@ -37,7 +37,6 @@ #include "stg/const.h" #include "stg/blowfish.h" #include "stg/logger.h" -#include "stg/locker.h" #include "stg/admin_conf.h" #include "stg/tariff.h" #include "stg/tariff_conf.h" @@ -369,10 +368,6 @@ FILES_STORE::FILES_STORE() : m_version("file_store v.1.04"), m_logger(STG::PluginLogger::get("store_files")) { -pthread_mutexattr_t attr; -pthread_mutexattr_init(&attr); -pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); -pthread_mutex_init(&m_mutex, &attr); } //----------------------------------------------------------------------------- int FILES_STORE::ParseSettings() @@ -380,7 +375,7 @@ int FILES_STORE::ParseSettings() int ret = m_storeSettings.ParseSettings(m_settings); if (ret) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = m_storeSettings.GetStrError(); } return ret; @@ -392,12 +387,12 @@ std::vector files; if (GetFileList(&files, m_storeSettings.GetUsersDir(), S_IFDIR, "")) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Failed to open '" + m_storeSettings.GetUsersDir() + "': " + std::string(strerror(errno)); return -1; } -STG_LOCKER lock(&m_mutex); +std::lock_guard lock(m_mutex); userList->swap(files); @@ -410,12 +405,12 @@ std::vector files; if (GetFileList(&files, m_storeSettings.GetAdminsDir(), S_IFREG, ".adm")) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Failed to open '" + m_storeSettings.GetAdminsDir() + "': " + std::string(strerror(errno)); return -1; } -STG_LOCKER lock(&m_mutex); +std::lock_guard lock(m_mutex); adminList->swap(files); @@ -428,12 +423,12 @@ std::vector files; if (GetFileList(&files, m_storeSettings.GetTariffsDir(), S_IFREG, ".tf")) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Failed to open '" + m_storeSettings.GetTariffsDir() + "': " + std::string(strerror(errno)); return -1; } -STG_LOCKER lock(&m_mutex); +std::lock_guard lock(m_mutex); tariffList->swap(files); @@ -446,12 +441,12 @@ std::vector files; if (GetFileList(&files, m_storeSettings.GetServicesDir(), S_IFREG, ".serv")) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Failed to open '" + m_storeSettings.GetServicesDir() + "': " + std::string(strerror(errno)); return -1; } -STG_LOCKER lock(&m_mutex); +std::lock_guard lock(m_mutex); list->swap(files); @@ -488,7 +483,7 @@ while ((entry = readdir(d))) { if (unlink(str.c_str())) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "unlink failed. Message: '"; m_errorStr += strerror(errno); m_errorStr += "'"; @@ -513,7 +508,7 @@ closedir(d); if (rmdir(path)) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "rmdir failed. Message: '"; m_errorStr += strerror(errno); m_errorStr += "'"; @@ -532,7 +527,7 @@ strprintf(&fileName, "%s%s", m_storeSettings.GetUsersDir().c_str(), login.c_str( if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = std::string("mkdir failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; @@ -541,7 +536,7 @@ if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == strprintf(&fileName, "%s%s/conf", m_storeSettings.GetUsersDir().c_str(), login.c_str()); if (Touch(fileName)) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot create file \"" + fileName + "\'"; printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno)); return -1; @@ -550,7 +545,7 @@ if (Touch(fileName)) strprintf(&fileName, "%s%s/stat", m_storeSettings.GetUsersDir().c_str(), login.c_str()); if (Touch(fileName)) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot create file \"" + fileName + "\'"; printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno)); return -1; @@ -568,7 +563,7 @@ if (access(dirName.c_str(), F_OK) != 0) { if (mkdir(dirName.c_str(), 0700) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Directory '" + dirName + "' cannot be created."; printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; @@ -581,7 +576,7 @@ if (access(dirName.c_str(), F_OK) == 0) strprintf(&dirName1, "%s/%s", m_storeSettings.GetUsersDir().c_str(), login.c_str()); if (rename(dirName1.c_str(), dirName.c_str())) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error moving dir from " + dirName1 + " to " + dirName; printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno)); return -1; @@ -620,7 +615,7 @@ int e = cf.Error(); if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' data not read."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str()); return -1; @@ -628,14 +623,14 @@ if (e) if (cf.ReadString("Password", &conf->password, "") < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' data not read. Parameter Password."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str()); return -1; } if (conf->password.empty()) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' password is blank."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str()); return -1; @@ -643,14 +638,14 @@ if (conf->password.empty()) if (cf.ReadString("tariff", &conf->tariffName, "") < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' data not read. Parameter Tariff."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str()); return -1; } if (conf->tariffName.empty()) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' tariff is blank."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str()); return -1; @@ -664,7 +659,7 @@ try } catch (const std::string & s) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s; printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str()); return -1; @@ -672,7 +667,7 @@ catch (const std::string & s) if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str()); return -1; @@ -680,7 +675,7 @@ if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0) if (cf.ReadInt("down", &conf->disabled, 0) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' data not read. Parameter Down."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str()); return -1; @@ -688,7 +683,7 @@ if (cf.ReadInt("down", &conf->disabled, 0) != 0) if (cf.ReadInt("passive", &conf->passive, 0) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' data not read. Parameter Passive."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str()); return -1; @@ -713,7 +708,7 @@ for (int i = 0; i < USERDATA_NUM; i++) if (cf.ReadDouble("Credit", &conf->credit, 0) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' data not read. Parameter Credit."; printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str()); return -1; @@ -746,7 +741,7 @@ int e = cf.Error(); if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + "."; printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str()); return -1; @@ -760,7 +755,7 @@ for (int i = 0; i < DIR_NUM; i++) snprintf(s, 22, "D%d", i); if (cf.ReadULongLongInt(s, &traff, 0) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s); printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str()); return -1; @@ -770,7 +765,7 @@ for (int i = 0; i < DIR_NUM; i++) snprintf(s, 22, "U%d", i); if (cf.ReadULongLongInt(s, &traff, 0) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s); printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str()); return -1; @@ -780,7 +775,7 @@ for (int i = 0; i < DIR_NUM; i++) if (cf.ReadDouble("Cash", &stat->cash, 0) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' stat not read. Parameter Cash"; printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str()); return -1; @@ -788,7 +783,7 @@ if (cf.ReadDouble("Cash", &stat->cash, 0) != 0) if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb"; printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str()); return -1; @@ -796,7 +791,7 @@ if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0) if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime"; printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str()); return -1; @@ -804,7 +799,7 @@ if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0) if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime"; printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str()); return -1; @@ -812,7 +807,7 @@ if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0) if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd"; printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str()); return -1; @@ -820,7 +815,7 @@ if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0) if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime"; printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str()); return -1; @@ -840,7 +835,7 @@ int e = cfstat.Error(); if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = std::string("User \'") + login + "\' conf not written\n"; printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str()); return -1; @@ -851,7 +846,7 @@ e += chown(fileName.c_str(), m_storeSettings.GetConfUID(), m_storeSettings.GetCo if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -896,7 +891,7 @@ fileName = m_storeSettings.GetUsersDir() + "/" + login + "/stat"; if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = std::string("User \'") + login + "\' stat not written\n"; printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str()); return -1; @@ -924,7 +919,7 @@ e += chown(fileName.c_str(), m_storeSettings.GetStatUID(), m_storeSettings.GetSt if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -949,7 +944,7 @@ if (f) } else { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot open \'" + fileName + "\'"; printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str()); return -1; @@ -960,7 +955,7 @@ e += chown(fileName.c_str(), m_storeSettings.GetLogUID(), m_storeSettings.GetLog if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -985,7 +980,7 @@ if (f) } else { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot open \'" + fileName + "\'"; printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str()); return -1; @@ -996,7 +991,7 @@ e += chown(fileName.c_str(), m_storeSettings.GetLogUID(), m_storeSettings.GetLog if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -1073,7 +1068,7 @@ CONFIGFILE s(stat1, true); if (s.Error()) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot create file '" + stat1 + "'"; printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str()); return -1; @@ -1088,7 +1083,7 @@ CONFIGFILE s2(stat2, true); if (s2.Error()) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot create file '" + stat2 + "'"; printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str()); return -1; @@ -1126,7 +1121,7 @@ strprintf(&fileName, "%s/%s.adm", m_storeSettings.GetAdminsDir().c_str(), login. if (Touch(fileName)) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot create file " + fileName; printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str()); return -1; @@ -1141,7 +1136,7 @@ std::string fileName; strprintf(&fileName, "%s/%s.adm", m_storeSettings.GetAdminsDir().c_str(), login.c_str()); if (unlink(fileName.c_str())) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "unlink failed. Message: '"; m_errorStr += strerror(errno); m_errorStr += "'"; @@ -1163,7 +1158,7 @@ strprintf(&fileName, "%s/%s.adm", m_storeSettings.GetAdminsDir().c_str(), ac.log if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot write admin " + ac.login + ". " + fileName; printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str()); return -1; @@ -1219,7 +1214,7 @@ std::string p; if (cf.Error()) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot open " + fileName; printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str()); return -1; @@ -1227,7 +1222,7 @@ if (cf.Error()) if (cf.ReadString("password", &p, "*")) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error in parameter password"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str()); return -1; @@ -1261,7 +1256,7 @@ if (cf.ReadUShortInt("ChgConf", &a, 0) == 0) ac->priv.userConf = a; else { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error in parameter ChgConf"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str()); return -1; @@ -1271,7 +1266,7 @@ if (cf.ReadUShortInt("ChgPassword", &a, 0) == 0) ac->priv.userPasswd = a; else { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error in parameter ChgPassword"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str()); return -1; @@ -1281,7 +1276,7 @@ if (cf.ReadUShortInt("ChgStat", &a, 0) == 0) ac->priv.userStat = a; else { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error in parameter ChgStat"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str()); return -1; @@ -1291,7 +1286,7 @@ if (cf.ReadUShortInt("ChgCash", &a, 0) == 0) ac->priv.userCash = a; else { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error in parameter ChgCash"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str()); return -1; @@ -1301,7 +1296,7 @@ if (cf.ReadUShortInt("UsrAddDel", &a, 0) == 0) ac->priv.userAddDel = a; else { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error in parameter UsrAddDel"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str()); return -1; @@ -1311,7 +1306,7 @@ if (cf.ReadUShortInt("ChgAdmin", &a, 0) == 0) ac->priv.adminChg = a; else { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error in parameter ChgAdmin"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str()); return -1; @@ -1321,7 +1316,7 @@ if (cf.ReadUShortInt("ChgTariff", &a, 0) == 0) ac->priv.tariffChg = a; else { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error in parameter ChgTariff"; printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str()); return -1; @@ -1346,7 +1341,7 @@ std::string fileName; strprintf(&fileName, "%s/%s.tf", m_storeSettings.GetTariffsDir().c_str(), name.c_str()); if (Touch(fileName)) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot create file " + fileName; printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str()); return -1; @@ -1360,7 +1355,7 @@ std::string fileName; strprintf(&fileName, "%s/%s.tf", m_storeSettings.GetTariffsDir().c_str(), name.c_str()); if (unlink(fileName.c_str())) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "unlink failed. Message: '"; m_errorStr += strerror(errno); m_errorStr += "'"; @@ -1378,7 +1373,7 @@ td->tariffConf.name = tariffName; if (conf.Error() != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read file " + fileName; printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str()); return -1; @@ -1390,7 +1385,7 @@ for (int i = 0; idirPrice[i].priceDayA, 0.0) < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str()); return -1; @@ -1415,7 +1410,7 @@ for (int i = 0; idirPrice[i].priceDayB, 0.0) < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str()); return -1; @@ -1425,7 +1420,7 @@ for (int i = 0; idirPrice[i].priceNightA, 0.0) < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str()); return -1; @@ -1435,7 +1430,7 @@ for (int i = 0; idirPrice[i].priceNightB, 0.0) < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str()); return -1; @@ -1445,7 +1440,7 @@ for (int i = 0; idirPrice[i].threshold, 0) < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str()); return -1; @@ -1454,7 +1449,7 @@ for (int i = 0; idirPrice[i].singlePrice, 0) < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str()); return -1; @@ -1463,7 +1458,7 @@ for (int i = 0; idirPrice[i].noDiscount, 0) < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param; printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str()); return -1; @@ -1472,7 +1467,7 @@ for (int i = 0; itariffConf.fee, 0) < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee"; printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str()); return -1; @@ -1480,7 +1475,7 @@ if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0) if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read tariff " + tariffName + ". Parameter Free"; printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str()); return -1; @@ -1488,7 +1483,7 @@ if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0) if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost"; printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str()); return -1; @@ -1496,7 +1491,7 @@ if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0) if (conf.ReadString("TraffType", &str, "") < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType"; printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str()); return -1; @@ -1529,7 +1524,7 @@ std::string fileName = m_storeSettings.GetTariffsDir() + "/" + tariffName + ".tf if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error writing tariff " + tariffName; printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str()); return e; @@ -1590,7 +1585,7 @@ strprintf(&fileName, "%s/%s.serv", m_storeSettings.GetServicesDir().c_str(), nam if (Touch(fileName)) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot create file " + fileName; printfd(__FILE__, "FILES_STORE::AddService - failed to add service '%s'\n", name.c_str()); return -1; @@ -1605,7 +1600,7 @@ std::string fileName; strprintf(&fileName, "%s/%s.serv", m_storeSettings.GetServicesDir().c_str(), name.c_str()); if (unlink(fileName.c_str())) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "unlink failed. Message: '"; m_errorStr += strerror(errno); m_errorStr += "'"; @@ -1627,7 +1622,7 @@ strprintf(&fileName, "%s/%s.serv", m_storeSettings.GetServicesDir().c_str(), con if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot write service " + conf.name + ". " + fileName; printfd(__FILE__, "FILES_STORE::SaveService - failed to save service '%s'\n", conf.name.c_str()); return -1; @@ -1650,7 +1645,7 @@ CONFIGFILE cf(fileName); if (cf.Error()) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot open " + fileName; printfd(__FILE__, "FILES_STORE::RestoreService - failed to restore service '%s'\n", name.c_str()); return -1; @@ -1658,7 +1653,7 @@ if (cf.Error()) if (cf.ReadString("name", &conf->name, name)) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error in parameter 'name'"; printfd(__FILE__, "FILES_STORE::RestoreService - name read failed for service '%s'\n", name.c_str()); return -1; @@ -1666,7 +1661,7 @@ if (cf.ReadString("name", &conf->name, name)) if (cf.ReadString("comment", &conf->comment, "")) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error in parameter 'comment'"; printfd(__FILE__, "FILES_STORE::RestoreService - comment read failed for service '%s'\n", name.c_str()); return -1; @@ -1674,7 +1669,7 @@ if (cf.ReadString("comment", &conf->comment, "")) if (cf.ReadDouble("cost", &conf->cost, 0.0)) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error in parameter 'cost'"; printfd(__FILE__, "FILES_STORE::RestoreService - cost read failed for service '%s'\n", name.c_str()); return -1; @@ -1683,7 +1678,7 @@ if (cf.ReadDouble("cost", &conf->cost, 0.0)) unsigned short value = 0; if (cf.ReadUShortInt("pay_day", &value, 0)) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error in parameter 'pay_day'"; printfd(__FILE__, "FILES_STORE::RestoreService - pay day read failed for service '%s'\n", name.c_str()); return -1; @@ -1710,7 +1705,7 @@ if (access(dn, F_OK) != 0) { if (mkdir(dn, 0700) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Directory \'" + std::string(dn) + "\' cannot be created."; printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; @@ -1722,7 +1717,7 @@ e += chmod(dn, m_storeSettings.GetStatModeDir()); if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -1743,7 +1738,7 @@ if (access(dn, F_OK) != 0) { if (mkdir(dn, 0700) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Directory \'" + std::string(dn) + "\' cannot be created."; printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; @@ -1755,7 +1750,7 @@ e += chmod(dn, m_storeSettings.GetStatModeDir()); if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -1769,7 +1764,7 @@ if (access(dn, F_OK) != 0) { if (mkdir(dn, 0700) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Directory \'" + std::string(dn) + "\' cannot be created."; printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno)); return -1; @@ -1781,7 +1776,7 @@ e += chmod(dn, m_storeSettings.GetStatModeDir()); if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -1791,7 +1786,7 @@ statFile = fopen (fn, "at"); if (!statFile) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "File \'" + std::string(fn) + "\' cannot be written."; printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno)); return -1; @@ -1818,7 +1813,7 @@ s2 = lt2->tm_sec; if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n", h1, m1, s1, h2, m2, s2) < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = std::string("fprint failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno)); fclose(statFile); @@ -1840,7 +1835,7 @@ while (stIter != statTree.end()) u.c_str(), stIter->second.cash) < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "fprint failed. Message: '"; m_errorStr += strerror(errno); m_errorStr += "'"; @@ -1856,7 +1851,7 @@ while (stIter != statTree.end()) u.c_str(), stIter->second.cash) < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = std::string("fprint failed. Message: '"); m_errorStr += strerror(errno); m_errorStr += "'"; @@ -1876,7 +1871,7 @@ e += chmod(fn, m_storeSettings.GetStatMode()); if (e) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno)); } @@ -1894,7 +1889,7 @@ if (access(dn.c_str(), F_OK) != 0) { if (mkdir(dn.c_str(), 0700) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Directory \'"; m_errorStr += dn; m_errorStr += "\' cannot be created."; @@ -1912,7 +1907,7 @@ strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id); if (Touch(fn)) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "File \'"; m_errorStr += fn; m_errorStr += "\' cannot be writen."; @@ -1932,7 +1927,7 @@ strprintf(&fileName, "%s/%s/messages/%lld", m_storeSettings.GetUsersDir().c_str( if (access(fileName.c_str(), F_OK) != 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Message for user \'"; m_errorStr += login + "\' with ID \'"; m_errorStr += std::to_string(msg.header.id) + "\' does not exist."; @@ -1945,7 +1940,7 @@ Touch(fileName + ".new"); msgFile = fopen((fileName + ".new").c_str(), "wt"); if (!msgFile) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "File \'" + fileName + "\' cannot be writen."; printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno)); return -1; @@ -1962,7 +1957,7 @@ res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0); if (!res) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = std::string("fprintf failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno)); fclose(msgFile); @@ -1975,7 +1970,7 @@ chmod((fileName + ".new").c_str(), m_storeSettings.GetConfMode()); if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Error moving dir from " + fileName + ".new to " + fileName; printfd(__FILE__, "FILES_STORE::EditMessage - rename failed. Message: '%s'\n", strerror(errno)); return -1; @@ -2020,7 +2015,7 @@ for (unsigned i = 0; i < messages.size(); i++) { if (unlink((dn + messages[i]).c_str())) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno)); return -1; @@ -2038,7 +2033,7 @@ for (unsigned i = 0; i < messages.size(); i++) { if (unlink((dn + messages[i]).c_str())) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'"; printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno)); return -1; @@ -2060,7 +2055,7 @@ FILE * msgFile; msgFile = fopen(fileName.c_str(), "rt"); if (!msgFile) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "File \'"; m_errorStr += fileName; m_errorStr += "\' cannot be openned."; @@ -2081,7 +2076,7 @@ memset(p, 0, sizeof(p)); for (int pos = 0; pos < 6; pos++) { if (fgets(p, sizeof(p) - 1, msgFile) == NULL) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read file \'"; m_errorStr += fileName; m_errorStr += "\'. Missing data."; @@ -2099,7 +2094,7 @@ for (int pos = 0; pos < 6; pos++) if (feof(msgFile)) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read file \'"; m_errorStr += fileName; m_errorStr += "\'. Missing data."; @@ -2111,7 +2106,7 @@ for (int pos = 0; pos < 6; pos++) if (str2x(p, *(d[pos]))) { - STG_LOCKER lock(&m_mutex); + std::lock_guard lock(m_mutex); m_errorStr = "Cannot read file \'"; m_errorStr += fileName; m_errorStr += "\'. Incorrect value. \'"; diff --git a/projects/stargazer/plugins/store/files/file_store.h b/projects/stargazer/plugins/store/files/file_store.h index dbb8ab61..3153665b 100644 --- a/projects/stargazer/plugins/store/files/file_store.h +++ b/projects/stargazer/plugins/store/files/file_store.h @@ -27,9 +27,9 @@ #include "stg/logger.h" #include +#include #include -#include //----------------------------------------------------------------------------- class FILES_STORE_SETTINGS @@ -201,7 +201,7 @@ class FILES_STORE: public STG::Store std::string m_version; FILES_STORE_SETTINGS m_storeSettings; STG::ModuleSettings m_settings; - mutable pthread_mutex_t m_mutex; + mutable std::mutex m_mutex; STG::PluginLogger m_logger; }; diff --git a/projects/stargazer/plugins/store/firebird/firebird_store.cpp b/projects/stargazer/plugins/store/firebird/firebird_store.cpp index c11b442b..fc588fcc 100644 --- a/projects/stargazer/plugins/store/firebird/firebird_store.cpp +++ b/projects/stargazer/plugins/store/firebird/firebird_store.cpp @@ -52,7 +52,6 @@ FIREBIRD_STORE::FIREBIRD_STORE() schemaVersion(0), logger(STG::PluginLogger::get("store_firebird")) { -pthread_mutex_init(&mutex, NULL); } //----------------------------------------------------------------------------- FIREBIRD_STORE::~FIREBIRD_STORE() diff --git a/projects/stargazer/plugins/store/firebird/firebird_store.h b/projects/stargazer/plugins/store/firebird/firebird_store.h index 4e3dfb68..a34b758f 100644 --- a/projects/stargazer/plugins/store/firebird/firebird_store.h +++ b/projects/stargazer/plugins/store/firebird/firebird_store.h @@ -21,7 +21,6 @@ #pragma once #include "stg/store.h" -#include "stg/locker.h" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wold-style-cast" #include "stg/ibpp.h" @@ -32,6 +31,7 @@ #include #include #include +#include class FIREBIRD_STORE : public STG::Store { @@ -114,7 +114,7 @@ class FIREBIRD_STORE : public STG::Store std::string db_server, db_database, db_user, db_password; STG::ModuleSettings settings; mutable IBPP::Database db; - mutable pthread_mutex_t mutex; + mutable std::mutex m_mutex; IBPP::TIL til; IBPP::TLR tlr; int schemaVersion; diff --git a/projects/stargazer/plugins/store/firebird/firebird_store_admins.cpp b/projects/stargazer/plugins/store/firebird/firebird_store_admins.cpp index 87d63344..fd00a7e9 100644 --- a/projects/stargazer/plugins/store/firebird/firebird_store_admins.cpp +++ b/projects/stargazer/plugins/store/firebird/firebird_store_admins.cpp @@ -40,7 +40,7 @@ //----------------------------------------------------------------------------- int FIREBIRD_STORE::GetAdminsList(std::vector * adminsList) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -71,7 +71,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::SaveAdmin(const STG::AdminConf & ac) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -129,7 +129,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::RestoreAdmin(STG::AdminConf * ac, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -192,7 +192,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::AddAdmin(const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -230,7 +230,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::DelAdmin(const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); diff --git a/projects/stargazer/plugins/store/firebird/firebird_store_corporations.cpp b/projects/stargazer/plugins/store/firebird/firebird_store_corporations.cpp index 0d088759..4e9cac4d 100644 --- a/projects/stargazer/plugins/store/firebird/firebird_store_corporations.cpp +++ b/projects/stargazer/plugins/store/firebird/firebird_store_corporations.cpp @@ -35,7 +35,7 @@ //----------------------------------------------------------------------------- int FIREBIRD_STORE::GetCorpsList(std::vector * corpsList) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -66,7 +66,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::SaveCorp(const STG::CorpConf & cc) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -94,7 +94,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::RestoreCorp(STG::CorpConf * cc, const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -132,7 +132,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::AddCorp(const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -159,7 +159,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::DelCorp(const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); diff --git a/projects/stargazer/plugins/store/firebird/firebird_store_messages.cpp b/projects/stargazer/plugins/store/firebird/firebird_store_messages.cpp index 90c3e573..d2c4a3ee 100644 --- a/projects/stargazer/plugins/store/firebird/firebird_store_messages.cpp +++ b/projects/stargazer/plugins/store/firebird/firebird_store_messages.cpp @@ -36,7 +36,7 @@ //----------------------------------------------------------------------------- int FIREBIRD_STORE::AddMessage(STG::Message * msg, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -73,7 +73,7 @@ return 0; int FIREBIRD_STORE::EditMessage(const STG::Message & msg, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -111,7 +111,7 @@ int FIREBIRD_STORE::GetMessage(uint64_t id, STG::Message * msg, const std::string &) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -157,7 +157,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::DelMessage(uint64_t id, const std::string &) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -185,7 +185,7 @@ return 0; int FIREBIRD_STORE::GetMessageHdrs(std::vector * hdrsList, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); diff --git a/projects/stargazer/plugins/store/firebird/firebird_store_services.cpp b/projects/stargazer/plugins/store/firebird/firebird_store_services.cpp index 539fcf73..2afe1181 100644 --- a/projects/stargazer/plugins/store/firebird/firebird_store_services.cpp +++ b/projects/stargazer/plugins/store/firebird/firebird_store_services.cpp @@ -36,7 +36,7 @@ //----------------------------------------------------------------------------- int FIREBIRD_STORE::GetServicesList(std::vector * servicesList) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -67,7 +67,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::SaveService(const STG::ServiceConf & sc) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -102,7 +102,7 @@ return 0; int FIREBIRD_STORE::RestoreService(STG::ServiceConf * sc, const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -144,7 +144,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::AddService(const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -172,7 +172,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::DelService(const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); diff --git a/projects/stargazer/plugins/store/firebird/firebird_store_tariffs.cpp b/projects/stargazer/plugins/store/firebird/firebird_store_tariffs.cpp index 58b57139..93360f21 100644 --- a/projects/stargazer/plugins/store/firebird/firebird_store_tariffs.cpp +++ b/projects/stargazer/plugins/store/firebird/firebird_store_tariffs.cpp @@ -45,7 +45,7 @@ const int pt_mega = 1024 * 1024; //----------------------------------------------------------------------------- int FIREBIRD_STORE::GetTariffsList(std::vector * tariffsList) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -76,7 +76,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::AddTariff(const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -104,7 +104,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::DelTariff(const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -132,7 +132,7 @@ return 0; int FIREBIRD_STORE::SaveTariff(const STG::TariffData & td, const std::string & tariffName) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -267,7 +267,7 @@ return 0; int FIREBIRD_STORE::RestoreTariff(STG::TariffData * td, const std::string & tariffName) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); diff --git a/projects/stargazer/plugins/store/firebird/firebird_store_users.cpp b/projects/stargazer/plugins/store/firebird/firebird_store_users.cpp index ba21aa61..075f95b8 100644 --- a/projects/stargazer/plugins/store/firebird/firebird_store_users.cpp +++ b/projects/stargazer/plugins/store/firebird/firebird_store_users.cpp @@ -39,7 +39,7 @@ //----------------------------------------------------------------------------- int FIREBIRD_STORE::GetUsersList(std::vector * usersList) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -70,7 +70,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::AddUser(const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -98,7 +98,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::DelUser(const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -126,7 +126,7 @@ return 0; int FIREBIRD_STORE::SaveUserStat(const STG::UserStat & stat, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); return SaveStat(stat, login); } @@ -231,7 +231,7 @@ return 0; int FIREBIRD_STORE::SaveUserConf(const STG::UserConf & conf, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -359,7 +359,7 @@ return 0; int FIREBIRD_STORE::RestoreUserStat(STG::UserStat * stat, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -446,7 +446,7 @@ return 0; int FIREBIRD_STORE::RestoreUserConf(STG::UserConf * conf, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amRead, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -573,7 +573,7 @@ int FIREBIRD_STORE::WriteUserChgLog(const std::string & login, const std::string & newValue, const std::string & message = "") const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -633,7 +633,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -670,7 +670,7 @@ int FIREBIRD_STORE::WriteUserDisconnect(const std::string & login, double /*freeMb*/, const std::string & /*reason*/) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -718,7 +718,7 @@ int FIREBIRD_STORE::WriteDetailedStat(const STG::TraffStat & statTree, time_t lastStat, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); @@ -768,7 +768,7 @@ return 0; //----------------------------------------------------------------------------- int FIREBIRD_STORE::SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); IBPP::Transaction tr = IBPP::TransactionFactory(db, IBPP::amWrite, til, tlr); IBPP::Statement st = IBPP::StatementFactory(db, tr); diff --git a/projects/stargazer/plugins/store/postgresql/postgresql_store.cpp b/projects/stargazer/plugins/store/postgresql/postgresql_store.cpp index ba478966..e5738286 100644 --- a/projects/stargazer/plugins/store/postgresql/postgresql_store.cpp +++ b/projects/stargazer/plugins/store/postgresql/postgresql_store.cpp @@ -66,7 +66,6 @@ POSTGRESQL_STORE::POSTGRESQL_STORE() connection(NULL), logger(STG::PluginLogger::get("store_postgresql")) { -pthread_mutex_init(&mutex, NULL); } //----------------------------------------------------------------------------- POSTGRESQL_STORE::~POSTGRESQL_STORE() @@ -75,7 +74,6 @@ if (connection) { PQfinish(connection); } -pthread_mutex_destroy(&mutex); } //----------------------------------------------------------------------------- int POSTGRESQL_STORE::ParseSettings() diff --git a/projects/stargazer/plugins/store/postgresql/postgresql_store.h b/projects/stargazer/plugins/store/postgresql/postgresql_store.h index b4a1c5e8..7b2e9604 100644 --- a/projects/stargazer/plugins/store/postgresql/postgresql_store.h +++ b/projects/stargazer/plugins/store/postgresql/postgresql_store.h @@ -26,6 +26,7 @@ #include #include +#include #include @@ -148,7 +149,7 @@ class POSTGRESQL_STORE : public STG::Store std::string password; std::string clientEncoding; STG::ModuleSettings settings; - mutable pthread_mutex_t mutex; + mutable std::mutex m_mutex; mutable int version; int retries; diff --git a/projects/stargazer/plugins/store/postgresql/postgresql_store_admins.cpp b/projects/stargazer/plugins/store/postgresql/postgresql_store_admins.cpp index b1c1203e..e313b4a0 100644 --- a/projects/stargazer/plugins/store/postgresql/postgresql_store_admins.cpp +++ b/projects/stargazer/plugins/store/postgresql/postgresql_store_admins.cpp @@ -28,7 +28,6 @@ #include "postgresql_store.h" -#include "stg/locker.h" #include "stg/common.h" #include "stg/admin_conf.h" #include "stg/blowfish.h" @@ -44,7 +43,7 @@ //----------------------------------------------------------------------------- int POSTGRESQL_STORE::GetAdminsList(std::vector * adminsList) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -100,7 +99,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::SaveAdmin(const STG::AdminConf & ac) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -199,7 +198,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::RestoreAdmin(STG::AdminConf * ac, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -320,7 +319,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::AddAdmin(const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -390,7 +389,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::DelAdmin(const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { diff --git a/projects/stargazer/plugins/store/postgresql/postgresql_store_corporations.cpp b/projects/stargazer/plugins/store/postgresql/postgresql_store_corporations.cpp index 1387d418..6c6b9985 100644 --- a/projects/stargazer/plugins/store/postgresql/postgresql_store_corporations.cpp +++ b/projects/stargazer/plugins/store/postgresql/postgresql_store_corporations.cpp @@ -29,7 +29,6 @@ #include "postgresql_store.h" #include "stg/corp_conf.h" -#include "stg/locker.h" #include "stg/common.h" #include @@ -41,7 +40,7 @@ //----------------------------------------------------------------------------- int POSTGRESQL_STORE::GetCorpsList(std::vector * corpsList) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -97,7 +96,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::SaveCorp(const STG::CorpConf & cc) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -163,7 +162,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::RestoreCorp(STG::CorpConf * cc, const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -246,7 +245,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::AddCorp(const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -313,7 +312,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::DelCorp(const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { diff --git a/projects/stargazer/plugins/store/postgresql/postgresql_store_messages.cpp b/projects/stargazer/plugins/store/postgresql/postgresql_store_messages.cpp index 905b467c..3bc9c749 100644 --- a/projects/stargazer/plugins/store/postgresql/postgresql_store_messages.cpp +++ b/projects/stargazer/plugins/store/postgresql/postgresql_store_messages.cpp @@ -30,7 +30,6 @@ #include "postgresql_store.h" #include "stg/common.h" -#include "stg/locker.h" #include "stg/message.h" #include @@ -42,7 +41,7 @@ //----------------------------------------------------------------------------- int POSTGRESQL_STORE::AddMessage(STG::Message * msg, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -145,7 +144,7 @@ return 0; int POSTGRESQL_STORE::EditMessage(const STG::Message & msg, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -231,7 +230,7 @@ int POSTGRESQL_STORE::GetMessage(uint64_t id, STG::Message * msg, const std::string &) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -309,7 +308,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::DelMessage(uint64_t id, const std::string &) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -361,7 +360,7 @@ return 0; int POSTGRESQL_STORE::GetMessageHdrs(std::vector * hdrsList, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { diff --git a/projects/stargazer/plugins/store/postgresql/postgresql_store_services.cpp b/projects/stargazer/plugins/store/postgresql/postgresql_store_services.cpp index 3d098161..2b99fcee 100644 --- a/projects/stargazer/plugins/store/postgresql/postgresql_store_services.cpp +++ b/projects/stargazer/plugins/store/postgresql/postgresql_store_services.cpp @@ -31,7 +31,6 @@ #include "stg/service_conf.h" #include "stg/common.h" -#include "stg/locker.h" #include #include @@ -42,7 +41,7 @@ //----------------------------------------------------------------------------- int POSTGRESQL_STORE::GetServicesList(std::vector * servicesList) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -98,7 +97,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::SaveService(const STG::ServiceConf & sc) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -178,7 +177,7 @@ return 0; int POSTGRESQL_STORE::RestoreService(STG::ServiceConf * sc, const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -265,7 +264,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::AddService(const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -332,7 +331,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::DelService(const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { diff --git a/projects/stargazer/plugins/store/postgresql/postgresql_store_tariffs.cpp b/projects/stargazer/plugins/store/postgresql/postgresql_store_tariffs.cpp index 1b68da80..085675c3 100644 --- a/projects/stargazer/plugins/store/postgresql/postgresql_store_tariffs.cpp +++ b/projects/stargazer/plugins/store/postgresql/postgresql_store_tariffs.cpp @@ -30,7 +30,6 @@ #include "stg/tariff_conf.h" #include "stg/common.h" -#include "stg/locker.h" #include #include @@ -49,7 +48,7 @@ const int pt_mega = 1024 * 1024; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::GetTariffsList(std::vector * tariffsList) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -105,7 +104,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::AddTariff(const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -168,7 +167,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::DelTariff(const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -232,7 +231,7 @@ return 0; int POSTGRESQL_STORE::SaveTariff(const STG::TariffData & td, const std::string & tariffName) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -417,7 +416,7 @@ return 0; int POSTGRESQL_STORE::RestoreTariff(STG::TariffData * td, const std::string & tariffName) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { diff --git a/projects/stargazer/plugins/store/postgresql/postgresql_store_users.cpp b/projects/stargazer/plugins/store/postgresql/postgresql_store_users.cpp index 6919ed00..3e564337 100644 --- a/projects/stargazer/plugins/store/postgresql/postgresql_store_users.cpp +++ b/projects/stargazer/plugins/store/postgresql/postgresql_store_users.cpp @@ -34,7 +34,6 @@ #include "stg/user_traff.h" #include "stg/common.h" #include "stg/const.h" -#include "stg/locker.h" #include "../../../stg_timer.h" #include @@ -47,7 +46,7 @@ //----------------------------------------------------------------------------- int POSTGRESQL_STORE::GetUsersList(std::vector * usersList) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -103,7 +102,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::AddUser(const std::string & name) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -167,7 +166,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::DelUser(const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -231,7 +230,7 @@ return 0; int POSTGRESQL_STORE::SaveUserStat(const STG::UserStat & stat, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); return SaveStat(stat, login); } @@ -342,7 +341,7 @@ return 0; int POSTGRESQL_STORE::SaveUserConf(const STG::UserConf & conf, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -613,7 +612,7 @@ return 0; int POSTGRESQL_STORE::RestoreUserStat(STG::UserStat * stat, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -753,7 +752,7 @@ return 0; int POSTGRESQL_STORE::RestoreUserConf(STG::UserConf * conf, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -996,7 +995,7 @@ int POSTGRESQL_STORE::WriteUserChgLog(const std::string & login, const std::string & newValue, const std::string & message = "") const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -1113,7 +1112,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -1199,7 +1198,7 @@ int POSTGRESQL_STORE::WriteUserDisconnect(const std::string & login, double freeMb, const std::string & reason) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -1358,7 +1357,7 @@ int POSTGRESQL_STORE::WriteDetailedStat(const STG::TraffStat & statTree, time_t lastStat, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (PQstatus(connection) != CONNECTION_OK) { @@ -1437,7 +1436,7 @@ return 0; //----------------------------------------------------------------------------- int POSTGRESQL_STORE::SaveMonthStat(const STG::UserStat & stat, int month, int year, const std::string & login) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); return SaveStat(stat, login, year, month); } diff --git a/projects/stargazer/services_impl.h b/projects/stargazer/services_impl.h index d6b4d6b5..eea5e7f1 100644 --- a/projects/stargazer/services_impl.h +++ b/projects/stargazer/services_impl.h @@ -22,7 +22,6 @@ #include "stg/services.h" #include "stg/service_conf.h" -#include "stg/locker.h" #include "stg/noncopyable.h" #include "stg/logger.h" diff --git a/projects/stargazer/tariffs_impl.cpp b/projects/stargazer/tariffs_impl.cpp index 792e53da..9d9ed208 100644 --- a/projects/stargazer/tariffs_impl.cpp +++ b/projects/stargazer/tariffs_impl.cpp @@ -30,7 +30,6 @@ #include "tariffs_impl.h" -#include "stg/locker.h" #include "stg/logger.h" #include "stg/store.h" #include "stg/admin.h" diff --git a/projects/stargazer/traffcounter_impl.cpp b/projects/stargazer/traffcounter_impl.cpp index 6a573428..70ade1ae 100644 --- a/projects/stargazer/traffcounter_impl.cpp +++ b/projects/stargazer/traffcounter_impl.cpp @@ -40,7 +40,6 @@ #include // strtol #include "stg/common.h" -#include "stg/locker.h" #include "stg/const.h" // MONITOR_TIME_DELAY_SEC #include "traffcounter_impl.h" #include "stg_timer.h" diff --git a/projects/stargazer/user_impl.cpp b/projects/stargazer/user_impl.cpp index 7116a11e..af94d477 100644 --- a/projects/stargazer/user_impl.cpp +++ b/projects/stargazer/user_impl.cpp @@ -154,11 +154,6 @@ m_afterDisabledConn = properties.disabled.afterChange([this](auto oldVal, auto n m_beforeTariffConn = properties.tariffName.beforeChange([this](const auto& oldVal, const auto& newVal){ onTariffChange(oldVal, newVal); }); m_beforeCashConn = properties.cash.beforeChange([this](auto oldVal, auto newVal){ onCashChange(oldVal, newVal); }); m_afterIPConn = ips.afterChange([this](const auto& oldVal, const auto& newVal){ onIPChange(oldVal, newVal); }); - -pthread_mutexattr_t attr; -pthread_mutexattr_init(&attr); -pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); -pthread_mutex_init(&mutex, &attr); } //----------------------------------------------------------------------------- UserImpl::UserImpl(const UserImpl & u) @@ -232,21 +227,11 @@ UserImpl::UserImpl(const UserImpl & u) m_afterIPConn = ips.afterChange([this](const auto& oldVal, const auto& newVal){ onIPChange(oldVal, newVal); }); properties.SetProperties(u.properties); - - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(&mutex, &attr); -} -//----------------------------------------------------------------------------- -UserImpl::~UserImpl() -{ -pthread_mutex_destroy(&mutex); } //----------------------------------------------------------------------------- void UserImpl::SetLogin(const std::string & l) { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); static int idGen = 0; assert(login.empty() && "Login is already set"); login = l; @@ -255,7 +240,7 @@ id = idGen++; //----------------------------------------------------------------------------- int UserImpl::ReadConf() { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); UserConf conf; if (store->RestoreUserConf(&conf, login)) @@ -303,7 +288,7 @@ return 0; //----------------------------------------------------------------------------- int UserImpl::ReadStat() { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); UserStat stat; if (store->RestoreUserStat(&stat, login)) @@ -322,7 +307,7 @@ return 0; //----------------------------------------------------------------------------- int UserImpl::WriteConf() { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); UserConf conf(properties.GetConf()); printfd(__FILE__, "UserImpl::WriteConf()\n"); @@ -341,7 +326,7 @@ return 0; //----------------------------------------------------------------------------- int UserImpl::WriteStat() { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); UserStat stat(properties.GetStat()); if (store->SaveUserStat(stat, login)) @@ -360,7 +345,7 @@ return 0; //----------------------------------------------------------------------------- int UserImpl::WriteMonthStat() { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); time_t tt = stgTime - 3600; struct tm t1; localtime_r(&tt, &t1); @@ -380,7 +365,7 @@ return 0; //----------------------------------------------------------------------------- int UserImpl::Authorize(uint32_t ip, uint32_t dirs, const Auth * auth) { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); /* * Authorize user. It only means that user will be authorized. Nothing more. * User can be connected or disconnected while authorized. @@ -458,7 +443,7 @@ return 0; //----------------------------------------------------------------------------- void UserImpl::Unauthorize(const Auth * auth, const std::string & reason) { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); /* * Authorizer tries to unauthorize user, that was not authorized by it */ @@ -480,14 +465,14 @@ if (authorizedBy.empty()) //----------------------------------------------------------------------------- bool UserImpl::IsAuthorizedBy(const Auth * auth) const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); // Is this user authorized by specified authorizer? return authorizedBy.find(auth) != authorizedBy.end(); } //----------------------------------------------------------------------------- std::vector UserImpl::GetAuthorizers() const { - STG_LOCKER lock(&mutex); + std::lock_guard lock(m_mutex); std::vector list; std::transform(authorizedBy.begin(), authorizedBy.end(), std::back_inserter(list), [](const auto auth){ return auth->GetVersion(); }); return list; @@ -499,8 +484,6 @@ void UserImpl::Connect(bool fakeConnect) * Connect user to Internet. This function is differ from Authorize() !!! */ -STG_LOCKER lock(&mutex); - if (!fakeConnect) { std::string scriptOnConnect = settings->GetScriptsDir() + "/OnConnect"; @@ -552,8 +535,6 @@ void UserImpl::Disconnect(bool fakeDisconnect, const std::string & reason) * Disconnect user from Internet. This function is differ from UnAuthorize() !!! */ -STG_LOCKER lock(&mutex); - if (!lastIPForDisconnect) { printfd(__FILE__, "lastIPForDisconnect\n"); @@ -618,7 +599,7 @@ sessionDownloadModTime = stgTime; //----------------------------------------------------------------------------- void UserImpl::Run() { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (stgTime > lastWriteStat + settings->GetStatWritePeriod()) { @@ -675,7 +656,7 @@ else //----------------------------------------------------------------------------- void UserImpl::UpdatePingTime(time_t t) { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (t) pingTime = t; else @@ -710,7 +691,7 @@ void UserImpl::AddTraffStatU(int dir, uint32_t ip, uint16_t port, uint32_t len) void UserImpl::AddTraffStatU(int dir, uint32_t ip, uint32_t len) #endif { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (!m_connected || tariff == NULL) return; @@ -803,7 +784,7 @@ void UserImpl::AddTraffStatD(int dir, uint32_t ip, uint16_t port, uint32_t len) void UserImpl::AddTraffStatD(int dir, uint32_t ip, uint32_t len) #endif { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (!m_connected || tariff == NULL) return; @@ -891,7 +872,7 @@ else //----------------------------------------------------------------------------- void UserImpl::OnAdd() { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); std::string scriptOnAdd = settings->GetScriptsDir() + "/OnUserAdd"; @@ -909,7 +890,7 @@ else //----------------------------------------------------------------------------- void UserImpl::OnDelete() { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); std::string scriptOnDel = settings->GetScriptsDir() + "/OnUserDel"; @@ -946,7 +927,7 @@ if (!traffStatSaved.second.empty()) TraffStat ts; { - STG_LOCKER lock(&mutex); + std::lock_guard lock(m_mutex); ts.swap(traffStat); } @@ -962,7 +943,7 @@ if (ts.size() && !disabledDetailStat) if (!hard) { printfd(__FILE__, "UserImpl::WriteDetailStat() - pushing detail stat to queue\n"); - STG_LOCKER lock(&mutex); + std::lock_guard lock(m_mutex); traffStatSaved.second.swap(ts); traffStatSaved.first = lastWriteDetailedStat; } @@ -975,8 +956,12 @@ return 0; //----------------------------------------------------------------------------- double UserImpl::GetPassiveTimePart() const { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); +return getPassiveTimePart(); +} +double UserImpl::getPassiveTimePart() const +{ static const std::array daysInMonth{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; struct tm tms; @@ -1001,7 +986,7 @@ return static_cast(dt) / secMonth; //----------------------------------------------------------------------------- void UserImpl::SetPassiveTimeAsNewUser() { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); time_t t = stgTime; struct tm tm; @@ -1015,7 +1000,7 @@ passiveTime = static_cast(pt * 24 * 3600 * daysCurrMon); //----------------------------------------------------------------------------- void UserImpl::MidnightResetSessionStat() { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (m_connected) { @@ -1026,7 +1011,7 @@ if (m_connected) //----------------------------------------------------------------------------- void UserImpl::ProcessNewMonth() { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); // Reset traff if (m_connected) Disconnect(true, "fake"); @@ -1069,7 +1054,7 @@ if (nextTariff.ConstData() != "") //----------------------------------------------------------------------------- void UserImpl::ProcessDayFeeSpread() { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (passive.ConstData() || tariff == NULL) return; @@ -1106,7 +1091,7 @@ ResetPassiveTime(); //----------------------------------------------------------------------------- void UserImpl::ProcessDayFee() { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (tariff == NULL) return; @@ -1117,7 +1102,7 @@ if (tariff->GetPeriod() != Tariff::MONTH) double passiveTimePart = 1.0; if (!settings->GetFullFee()) { - passiveTimePart = GetPassiveTimePart(); + passiveTimePart = getPassiveTimePart(); } else { @@ -1177,7 +1162,7 @@ switch (settings->GetFeeChargeType()) //----------------------------------------------------------------------------- void UserImpl::ProcessDailyFee() { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (passive.ConstData() || tariff == NULL) return; @@ -1214,10 +1199,12 @@ struct tm tms; time_t t = stgTime; localtime_r(&t, &tms); +std::lock_guard lock(m_mutex); + double passiveTimePart = 1.0; if (!settings->GetFullFee()) { - passiveTimePart = GetPassiveTimePart(); + passiveTimePart = getPassiveTimePart(); } else { @@ -1285,7 +1272,7 @@ if (tariff != NULL) //----------------------------------------------------------------------------- int UserImpl::AddMessage(Message * msg) { -STG_LOCKER lock(&mutex); +std::lock_guard lock(m_mutex); if (SendMessage(*msg)) { @@ -1425,6 +1412,7 @@ std::string UserImpl::GetParamValue(const std::string & name) const //----------------------------------------------------------------------------- void UserImpl::onPassiveChange(int oldVal, int newVal) { + std::lock_guard lock(m_mutex); if (newVal && !oldVal && tariff != NULL) properties.cash.Set(cash - tariff->GetPassiveCost(), *sysAdmin, @@ -1435,6 +1423,7 @@ void UserImpl::onPassiveChange(int oldVal, int newVal) //----------------------------------------------------------------------------- void UserImpl::onDisabledChange(int oldVal, int newVal) { + std::lock_guard lock(m_mutex); if (oldVal && !newVal && GetConnected()) Disconnect(false, "disabled"); else if (!oldVal && newVal && IsInetable()) @@ -1443,7 +1432,7 @@ void UserImpl::onDisabledChange(int oldVal, int newVal) //----------------------------------------------------------------------------- void UserImpl::onTariffChange(const std::string& /*oldVal*/, const std::string& newVal) { - STG_LOCKER lock(&mutex); + std::lock_guard lock(m_mutex); if (settings->GetReconnectOnTariffChange() && m_connected) Disconnect(false, "Change tariff"); tariff = tariffs->FindByName(newVal); @@ -1466,6 +1455,7 @@ void UserImpl::onCashChange(double oldVal, double newVal) //----------------------------------------------------------------------------- void UserImpl::onIPChange(const UserIPs& oldVal, const UserIPs& newVal) { + std::lock_guard lock(m_mutex); printfd(__FILE__, "Change IP from '%s' to '%s'\n", oldVal.toString().c_str(), newVal.toString().c_str()); if (m_connected) Disconnect(false, "Change IP"); diff --git a/projects/stargazer/user_impl.h b/projects/stargazer/user_impl.h index 7e14311c..c5b30a1d 100644 --- a/projects/stargazer/user_impl.h +++ b/projects/stargazer/user_impl.h @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -66,7 +67,6 @@ class UserImpl : public User const Users * u, const Services & svcs); UserImpl(const UserImpl & u); - virtual ~UserImpl(); int ReadConf(); int ReadStat(); @@ -253,9 +253,11 @@ class UserImpl : public User ScopedConnection m_afterIPConn; void onIPChange(const UserIPs& oldVal, const UserIPs& newVal); - mutable pthread_mutex_t mutex; + mutable std::mutex m_mutex; std::string errorStr; + + double getPassiveTimePart() const; }; //-----------------------------------------------------------------------------