🎨🐎 multi threaded flag for conditional compiling

This commit is contained in:
EntireTwix 2021-07-03 00:39:42 -07:00
parent 10537fb942
commit 0e5836b777
5 changed files with 57 additions and 9 deletions

View file

@ -31,13 +31,13 @@ target_sources(${PROJECT_NAME} PRIVATE
if(USER_SAVE_LOC)
set(USER_SAVE ${USER_SAVE_LOC})
else()
set(USER_SAVE "\"users.json\"")
set(USER_SAVE "\"../users.json\"")
endif()
if(DROGON_CONFIG_LOC)
set(DROGON_CONFIG ${DROGON_CONFIG_LOC})
else()
set(DROGON_CONFIG "\"config.json\"")
set(DROGON_CONFIG "\"../config.json\"")
endif()
configure_file(ccash_config.hpp.in ${CMAKE_CURRENT_SOURCE_DIR}/include/ccash_config.hpp)

View file

@ -37,3 +37,5 @@ version 3 will work
etc
*/
#define API_VERSION 1
#define MULTI_THREADED true

View file

@ -6,7 +6,7 @@
#include <parallel-hashmap/parallel_hashmap/phmap.h>
#include "user.h"
#if CONSERVATIVE_DISK_SAVE && MAX_LOG_SIZE < 0
#if (CONSERVATIVE_DISK_SAVE && MAX_LOG_SIZE < 0) && !MULTI_THREADED
#include "change_flag.h"
#endif
@ -14,6 +14,7 @@ using BankResponse = std::pair<drogon::HttpStatusCode, Json::Value>;
class Bank
{
#if MULTI_THREADED
phmap::parallel_flat_hash_map<
std::string, User,
xxHashStringGen,
@ -22,10 +23,17 @@ class Bank
4UL,
std::mutex>
users;
#else
phmap::parallel_flat_hash_map<std::string, User, xxHashStringGen> users;
#endif
private:
#if CONSERVATIVE_DISK_SAVE
#if MULTI_THREADED
ChangeFlag save_flag;
#else
bool save_flag = false;
#endif
#endif
/**

View file

@ -35,7 +35,13 @@ int main(int argc, char **argv)
<< "\nSSE 3 : " << (__builtin_cpu_supports("sse3") ? "enabled" : "disabled")
<< "\nSSE 4.1 : " << (__builtin_cpu_supports("sse4.1") ? "enabled" : "disabled")
<< "\nSSE 4.2 : " << (__builtin_cpu_supports("sse4.2") ? "enabled" : "disabled")
#if MULTI_THREADED
<< "\n\nThreads : " << get_nprocs() + 1
<< "\nMulti thread : disabled"
#else
<< "\n\nThreads : " << 2
<< "\nMulti thread : disabled"
#endif
<< std::endl; //flushing before EventLoop
static_assert(bool(MAX_LOG_SIZE) == bool(PRE_LOG_SIZE), "You must either utilize both or neither logging variables.\n");

View file

@ -18,9 +18,17 @@ __attribute__((always_inline)) inline bool ValidUsrname(const std::string &name)
using namespace drogon;
bool Bank::GetChangeState() const noexcept { return save_flag.GetChangeState(); }
bool Bank::GetChangeState() const noexcept
{
#if MULTI_THREADED
return save_flag.GetChangeState();
#else
return save_flag;
#endif
}
BankResponse Bank::GetBal(const std::string &name) const noexcept
BankResponse
Bank::GetBal(const std::string &name) const noexcept
{
uint64_t res = 0;
users.if_contains(name, [&res](const User &u) { res = u.balance + 1; });
@ -58,7 +66,9 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
}
BankResponse state;
#if MULTI_THREADED
std::shared_lock<std::shared_mutex> lock{send_funds_l}; //about 10% of this function's cost
#endif
#if MAX_LOG_SIZE > 0
Transaction temp(a_name, b_name, amount);
if (!users.modify_if(a_name, [&temp, &state, amount](User &a) {
@ -94,7 +104,11 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
#endif
#if CONSERVATIVE_DISK_SAVE
#if MULTI_THREADED
save_flag.SetChangesOn(); //about 5% of this function's cost
#else
save_flag = true;
#endif
#endif
}
return state;
@ -110,7 +124,11 @@ void Bank::ChangePassword(const std::string &name, std::string &&new_pass) noexc
{
users.modify_if(name, [&new_pass](User &u) { u.password = xxHashStringGen{}(new_pass); });
#if CONSERVATIVE_DISK_SAVE
#if MULTI_THREADED
save_flag.SetChangesOn();
#else
save_flag = true;
#endif
#endif
}
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
@ -118,7 +136,11 @@ BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
if (users.modify_if(name, [amount](User &u) { u.balance = amount; }))
{
#if CONSERVATIVE_DISK_SAVE
#if MULTI_THREADED
save_flag.SetChangesOn();
#else
save_flag = true;
#endif
#endif
return {k200OK, "Balance set!"};
}
@ -142,8 +164,9 @@ BankResponse Bank::AddUser(std::string &&name, std::string &&init_pass) noexcept
{
return {k400BadRequest, "Invalid Name, breaks size and/or character restrictions"};
}
#if MULTI_THREADED
std::shared_lock<std::shared_mutex> lock{size_l};
#endif
return (users.try_emplace_l(
std::move(name), [](User &) {}, std::move(init_pass)))
? BankResponse(k200OK, "User added!")
@ -155,8 +178,9 @@ BankResponse Bank::AdminAddUser(std::string &&name, uint32_t init_bal, std::stri
{
return {k400BadRequest, "Invalid Name, breaks size and/or character restrictions"};
}
#if MULTI_THREADED
std::shared_lock<std::shared_mutex> lock{size_l};
#endif
return (users.try_emplace_l(
std::move(name), [](User &) {}, init_bal, std::move(init_pass)))
? BankResponse(k200OK, "User added!")
@ -164,7 +188,9 @@ BankResponse Bank::AdminAddUser(std::string &&name, uint32_t init_bal, std::stri
}
BankResponse Bank::DelUser(const std::string &name) noexcept
{
#if MULTI_THREADED
std::shared_lock<std::shared_mutex> lock{size_l};
#endif
#if RETURN_ON_DEL
uint32_t bal;
if (users.erase_if(name, [this, &bal, &name](User &u) {
@ -185,14 +211,16 @@ BankResponse Bank::DelUser(const std::string &name) noexcept
void Bank::Save()
{
#if CONSERVATIVE_DISK_SAVE
if (save_flag.GetChangeState())
if (GetChangeState())
{
#endif
Json::Value temp;
//loading info into json temp
{
#if MULTI_THREADED
std::scoped_lock<std::shared_mutex, std::shared_mutex> lock{size_l, send_funds_l};
#endif
for (const auto &u : users)
{
//we know it contains this key but we call this func to grab mutex
@ -212,7 +240,11 @@ void Bank::Save()
user_save.close();
}
#if CONSERVATIVE_DISK_SAVE
#if MULTI_THREADED
save_flag.SetChangesOff();
#else
save_flag = true;
#endif
}
#endif
}