mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-17 08:32:13 +11:00
🐎 thread_local
This commit is contained in:
parent
805a7d05b3
commit
fd6d085ae5
1 changed files with 20 additions and 24 deletions
44
src/bank.cpp
44
src/bank.cpp
|
|
@ -67,20 +67,20 @@ bool Bank::GetChangeState() const noexcept
|
||||||
|
|
||||||
BankResponse Bank::GetBal(const std::string &name) const noexcept
|
BankResponse Bank::GetBal(const std::string &name) const noexcept
|
||||||
{
|
{
|
||||||
uint32_t res = 0;
|
static thread_local uint32_t res = 0;
|
||||||
users.if_contains(name, [&res](const User &u) { res = u.balance + 1; });
|
users.if_contains(name, [](const User &u) { res = u.balance + 1; });
|
||||||
return res ? BankResponse(k200OK, std::to_string(res - 1)) : BankResponse(k404NotFound, "\"User not found\"");
|
return res ? BankResponse(k200OK, std::to_string(res - 1)) : BankResponse(k404NotFound, "\"User not found\"");
|
||||||
}
|
}
|
||||||
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
||||||
{
|
{
|
||||||
BankResponse res;
|
|
||||||
#if MAX_LOG_SIZE > 0
|
#if MAX_LOG_SIZE > 0
|
||||||
|
BankResponse res;
|
||||||
if (!users.modify_if(name, [&res](User &u) { res = BankResponse(k200OK, u.log.GetLog()); }))
|
if (!users.modify_if(name, [&res](User &u) { res = BankResponse(k200OK, u.log.GetLog()); }))
|
||||||
{
|
{
|
||||||
return BankResponse(k404NotFound, "\"User not found\"");
|
return BankResponse(k404NotFound, "\"User not found\"");
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
return res;
|
return res;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept
|
BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept
|
||||||
{
|
{
|
||||||
|
|
@ -102,11 +102,11 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
||||||
return {k404NotFound, "\"Reciever does not exist\""};
|
return {k404NotFound, "\"Reciever does not exist\""};
|
||||||
}
|
}
|
||||||
|
|
||||||
BankResponse state;
|
static thread_local BankResponse state;
|
||||||
std::shared_lock<std::shared_mutex> lock{save_lock}; //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
|
#if MAX_LOG_SIZE > 0
|
||||||
Transaction temp(a_name, b_name, amount);
|
static thread_local Transaction temp(a_name, b_name, amount);
|
||||||
if (!users.modify_if(a_name, [&temp, &state, amount](User &a) {
|
if (!users.modify_if(a_name, [amount](User &a) {
|
||||||
#else
|
#else
|
||||||
if (!users.modify_if(a_name, [&state, amount](User &a) {
|
if (!users.modify_if(a_name, [&state, amount](User &a) {
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -130,14 +130,13 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
||||||
if (state.first == k200OK)
|
if (state.first == k200OK)
|
||||||
{
|
{
|
||||||
#if MAX_LOG_SIZE > 0
|
#if MAX_LOG_SIZE > 0
|
||||||
users.modify_if(b_name, [&temp, amount](User &b) {
|
users.modify_if(b_name, [amount](User &b) {
|
||||||
b.balance += amount;
|
b.balance += amount;
|
||||||
b.log.AddTrans(std::move(temp));
|
b.log.AddTrans(std::move(temp));
|
||||||
}); //about 40% of this function's cost
|
}); //about 40% of this function's cost
|
||||||
#else
|
#else
|
||||||
users.modify_if(b_name, [amount](User &b) { b.balance += amount; });
|
users.modify_if(b_name, [amount](User &b) { b.balance += amount; });
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
#if MULTI_THREADED
|
#if MULTI_THREADED
|
||||||
save_flag.SetChangesOn(); //about 5% of this function's cost
|
save_flag.SetChangesOn(); //about 5% of this function's cost
|
||||||
|
|
@ -150,8 +149,8 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
||||||
}
|
}
|
||||||
bool Bank::VerifyPassword(const std::string &name, std::string_view &&attempt) const noexcept
|
bool Bank::VerifyPassword(const std::string &name, std::string_view &&attempt) const noexcept
|
||||||
{
|
{
|
||||||
bool res = false;
|
static thread_local bool res = false;
|
||||||
users.if_contains(name, [&res, &attempt](const User &u) { res = (u.password == xxHashStringGen{}(std::move(attempt))); });
|
users.if_contains(name, [&attempt](const User &u) { res = (u.password == xxHashStringGen{}(std::move(attempt))); });
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -244,10 +243,7 @@ BankResponse Bank::DelUser(const std::string &name) noexcept
|
||||||
std::shared_lock<std::shared_mutex> lock{save_lock};
|
std::shared_lock<std::shared_mutex> lock{save_lock};
|
||||||
#if RETURN_ON_DEL
|
#if RETURN_ON_DEL
|
||||||
uint32_t bal;
|
uint32_t bal;
|
||||||
if (users.if_contains(name, [this, &bal](const User &u) {
|
if (users.if_contains(name, [this, &bal](const User &u) { bal = u.balance; }) && bal)
|
||||||
bal = u.balance;
|
|
||||||
}) &&
|
|
||||||
bal)
|
|
||||||
{
|
{
|
||||||
users.modify_if(return_account, [ this, bal ](User & u))
|
users.modify_if(return_account, [ this, bal ](User & u))
|
||||||
{
|
{
|
||||||
|
|
@ -273,7 +269,7 @@ BankResponse Bank::DelUser(const std::string &name) noexcept
|
||||||
}
|
}
|
||||||
void Bank::Save()
|
void Bank::Save()
|
||||||
{
|
{
|
||||||
Json::Value temp;
|
static thread_local Json::Value temp;
|
||||||
|
|
||||||
//loading info into json temp
|
//loading info into json temp
|
||||||
{
|
{
|
||||||
|
|
@ -281,7 +277,7 @@ void Bank::Save()
|
||||||
for (const auto &u : users)
|
for (const auto &u : users)
|
||||||
{
|
{
|
||||||
//we know it contains this key but we call this func to grab mutex
|
//we know it contains this key but we call this func to grab mutex
|
||||||
users.if_contains(u.first, [&temp, &u](const User &u_val) { temp[u.first] = u_val.Serialize(); });
|
users.if_contains(u.first, [&u](const User &u_val) { temp[u.first] = u_val.Serialize(); });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (temp.isNull())
|
if (temp.isNull())
|
||||||
|
|
@ -290,9 +286,9 @@ void Bank::Save()
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::ofstream user_save(users_location);
|
static thread_local std::ofstream user_save(users_location);
|
||||||
Json::StreamWriterBuilder builder;
|
static thread_local Json::StreamWriterBuilder builder;
|
||||||
const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
|
static thread_local const std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
|
||||||
writer->write(temp, &user_save);
|
writer->write(temp, &user_save);
|
||||||
user_save.close();
|
user_save.close();
|
||||||
}
|
}
|
||||||
|
|
@ -308,12 +304,12 @@ void Bank::Save()
|
||||||
//NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS
|
//NOT THREAD SAFE, BY NO MEANS SHOULD THIS BE CALLED WHILE RECEIEVING REQUESTS
|
||||||
void Bank::Load()
|
void Bank::Load()
|
||||||
{
|
{
|
||||||
Json::CharReaderBuilder builder;
|
static thread_local Json::CharReaderBuilder builder;
|
||||||
|
|
||||||
Json::Value temp;
|
static thread_local Json::Value temp;
|
||||||
std::ifstream user_save(users_location);
|
static thread_local std::ifstream user_save(users_location);
|
||||||
builder["collectComments"] = true;
|
builder["collectComments"] = true;
|
||||||
JSONCPP_STRING errs;
|
static thread_local JSONCPP_STRING errs;
|
||||||
if (!parseFromStream(builder, user_save, &temp, &errs))
|
if (!parseFromStream(builder, user_save, &temp, &errs))
|
||||||
{
|
{
|
||||||
std::cerr << errs << '\n';
|
std::cerr << errs << '\n';
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue