mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-17 00:22:14 +11:00
🐎 made add/del user based on shared lock system for Save()
This commit is contained in:
parent
ea511c71b2
commit
d2fcf4f863
1 changed files with 11 additions and 12 deletions
|
|
@ -15,14 +15,14 @@ private:
|
|||
std::mutex>
|
||||
users;
|
||||
|
||||
std::mutex size_users;
|
||||
std::shared_mutex size_lock;
|
||||
|
||||
public:
|
||||
std::string admin_pass;
|
||||
|
||||
bool AddUser(const std::string &name, std::string &&init_pass)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock{size_users};
|
||||
std::shared_lock<std::shared_mutex> lock{size_lock};
|
||||
return users.try_emplace_l(
|
||||
name, [](User &) {}, std::forward<std::string &&>(init_pass));
|
||||
}
|
||||
|
|
@ -31,7 +31,7 @@ public:
|
|||
bool state = (admin_pass == attempt);
|
||||
if (state)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock{size_users};
|
||||
std::shared_lock<std::shared_mutex> lock{size_lock};
|
||||
state = users.try_emplace_l(
|
||||
name, [](User &) {}, init_bal, std::forward<std::string &&>(init_pass));
|
||||
}
|
||||
|
|
@ -40,12 +40,10 @@ public:
|
|||
|
||||
bool DelUser(const std::string &name, const std::string &attempt)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock{size_users};
|
||||
return users.erase_if(name, [&attempt](const User &u) { return (attempt == u.password); });
|
||||
}
|
||||
bool AdminDelUser(const std::string &name, const std::string &attempt)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock{size_users};
|
||||
return users.erase_if(name, [this, &attempt](const User &) { return (admin_pass == attempt); });
|
||||
}
|
||||
|
||||
|
|
@ -95,15 +93,16 @@ public:
|
|||
|
||||
std::ofstream user_save("users.json");
|
||||
Json::Value temp;
|
||||
//this gets access to the parallel map with the size being constant
|
||||
std::unique_lock<std::mutex> lock{size_users};
|
||||
for (const auto &u : users)
|
||||
|
||||
{
|
||||
//this gets read access to prevent writes
|
||||
users.if_contains(u.first, [&temp, &u](const User &u_val) {
|
||||
temp[u.first] = u_val.Serialize();
|
||||
})
|
||||
std::unique_lock<std::shared_mutex> lock{size_lock}; //grabbing it from any busy add/del opperations
|
||||
for (const auto &u : users)
|
||||
{
|
||||
temp[u.first] = u.second.Serialize();
|
||||
}
|
||||
std::cout << "dropped unique lock\n";
|
||||
}
|
||||
|
||||
writer->write(temp, &user_save);
|
||||
user_save.close();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue