Fix data race

This commit is contained in:
badaix 2021-09-11 22:25:05 +02:00
parent 35967292f3
commit fc4e863b84
2 changed files with 12 additions and 0 deletions

View file

@ -113,6 +113,7 @@ void Config::init(const std::string& root_directory, const std::string& user, co
void Config::save() void Config::save()
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (filename_.empty()) if (filename_.empty())
init(); init();
std::ofstream ofs(filename_.c_str(), std::ofstream::out | std::ofstream::trunc); std::ofstream ofs(filename_.c_str(), std::ofstream::out | std::ofstream::trunc);
@ -128,6 +129,7 @@ ClientInfoPtr Config::getClientInfo(const std::string& clientId) const
if (clientId.empty()) if (clientId.empty())
return nullptr; return nullptr;
std::lock_guard<std::recursive_mutex> lock(mutex_);
for (const auto& group : groups) for (const auto& group : groups)
{ {
for (auto client : group->clients) for (auto client : group->clients)
@ -143,6 +145,7 @@ ClientInfoPtr Config::getClientInfo(const std::string& clientId) const
GroupPtr Config::addClientInfo(ClientInfoPtr client) GroupPtr Config::addClientInfo(ClientInfoPtr client)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_);
GroupPtr group = getGroupFromClient(client); GroupPtr group = getGroupFromClient(client);
if (!group) if (!group)
{ {
@ -156,6 +159,7 @@ GroupPtr Config::addClientInfo(ClientInfoPtr client)
GroupPtr Config::addClientInfo(const std::string& clientId) GroupPtr Config::addClientInfo(const std::string& clientId)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_);
ClientInfoPtr client = getClientInfo(clientId); ClientInfoPtr client = getClientInfo(clientId);
if (!client) if (!client)
client = make_shared<ClientInfo>(clientId); client = make_shared<ClientInfo>(clientId);
@ -165,6 +169,7 @@ GroupPtr Config::addClientInfo(const std::string& clientId)
GroupPtr Config::getGroup(const std::string& groupId) const GroupPtr Config::getGroup(const std::string& groupId) const
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_);
for (auto group : groups) for (auto group : groups)
{ {
if (group->id == groupId) if (group->id == groupId)
@ -177,6 +182,7 @@ GroupPtr Config::getGroup(const std::string& groupId) const
GroupPtr Config::getGroupFromClient(const std::string& clientId) GroupPtr Config::getGroupFromClient(const std::string& clientId)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_);
for (auto group : groups) for (auto group : groups)
{ {
for (const auto& c : group->clients) for (const auto& c : group->clients)
@ -197,6 +203,7 @@ GroupPtr Config::getGroupFromClient(ClientInfoPtr client)
json Config::getServerStatus(const json& streams) const json Config::getServerStatus(const json& streams) const
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_);
Host host; Host host;
host.update(); host.update();
// TODO: Set MAC and IP // TODO: Set MAC and IP
@ -214,6 +221,7 @@ json Config::getServerStatus(const json& streams) const
json Config::getGroups() const json Config::getGroups() const
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_);
json result = json::array(); json result = json::array();
for (const auto& group : groups) for (const auto& group : groups)
result.push_back(group->toJson()); result.push_back(group->toJson());
@ -223,6 +231,7 @@ json Config::getGroups() const
void Config::remove(ClientInfoPtr client) void Config::remove(ClientInfoPtr client)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_);
auto group = getGroupFromClient(client); auto group = getGroupFromClient(client);
if (!group) if (!group)
return; return;
@ -234,6 +243,7 @@ void Config::remove(ClientInfoPtr client)
void Config::remove(GroupPtr group, bool force) void Config::remove(GroupPtr group, bool force)
{ {
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (!group) if (!group)
return; return;

View file

@ -20,6 +20,7 @@
#define CONFIG_HPP #define CONFIG_HPP
#include <memory> #include <memory>
#include <mutex>
#include <string> #include <string>
#include <sys/time.h> #include <sys/time.h>
#include <vector> #include <vector>
@ -397,6 +398,7 @@ public:
private: private:
Config() = default; Config() = default;
~Config(); ~Config();
mutable std::recursive_mutex mutex_;
std::string filename_; std::string filename_;
}; };