🔥🎨 simplified by using only one save_lock

This commit is contained in:
EntireTwix 2021-07-03 19:45:52 -07:00
parent a561742347
commit 239e8007e9
2 changed files with 5 additions and 15 deletions

View file

@ -36,17 +36,7 @@ private:
#endif
#endif
/**
* @brief size_l should be grabbed if the operation MODIFIES the size (shared), this is so that when save claims unique
*
*/
std::shared_mutex size_l;
/**
* @brief send_funds_l should be grabbed if balances are being MODIFIED (shared) or if an operation needs to READ without the intermediary states that sendfunds has (unique)
*
*/
std::shared_mutex send_funds_l;
std::shared_mutex save_lock;
public:
std::string admin_account;

View file

@ -68,7 +68,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
}
BankResponse state;
std::shared_lock<std::shared_mutex> lock{send_funds_l}; //about 10% of this function's cost
std::shared_lock<std::shared_mutex> lock{save_lock}; //about 10% of this function's cost
#if MAX_LOG_SIZE > 0
Transaction temp(a_name, b_name, amount);
if (!users.modify_if(a_name, [&temp, &state, amount](User &a) {
@ -164,7 +164,7 @@ BankResponse Bank::AddUser(std::string &&name, uint32_t init_bal, std::string &&
{
return {k400BadRequest, "Invalid Name, breaks size and/or character restrictions"};
}
std::shared_lock<std::shared_mutex> lock{size_l};
std::shared_lock<std::shared_mutex> lock{save_lock};
if (users.try_emplace_l(
std::move(name), [](User &) {}, init_bal, std::move(init_pass)))
{
@ -184,7 +184,7 @@ BankResponse Bank::AddUser(std::string &&name, uint32_t init_bal, std::string &&
}
BankResponse Bank::DelUser(const std::string &name) noexcept
{
std::shared_lock<std::shared_mutex> lock{size_l};
std::shared_lock<std::shared_mutex> lock{save_lock};
#if RETURN_ON_DEL
uint32_t bal;
if (users.if_contains(name, [this, &bal](const User &u) {
@ -223,7 +223,7 @@ void Bank::Save()
//loading info into json temp
{
std::scoped_lock<std::shared_mutex, std::shared_mutex> lock{size_l, send_funds_l};
std::unique_lock<std::shared_mutex> lock{save_lock};
for (const auto &u : users)
{
//we know it contains this key but we call this func to grab mutex