mirror of
https://github.com/Expand-sys/CCash
synced 2026-03-22 20:47:10 +11:00
🎨🐎 multi threaded flag for conditional compiling
This commit is contained in:
parent
10537fb942
commit
0e5836b777
5 changed files with 57 additions and 9 deletions
|
|
@ -31,13 +31,13 @@ target_sources(${PROJECT_NAME} PRIVATE
|
||||||
if(USER_SAVE_LOC)
|
if(USER_SAVE_LOC)
|
||||||
set(USER_SAVE ${USER_SAVE_LOC})
|
set(USER_SAVE ${USER_SAVE_LOC})
|
||||||
else()
|
else()
|
||||||
set(USER_SAVE "\"users.json\"")
|
set(USER_SAVE "\"../users.json\"")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(DROGON_CONFIG_LOC)
|
if(DROGON_CONFIG_LOC)
|
||||||
set(DROGON_CONFIG ${DROGON_CONFIG_LOC})
|
set(DROGON_CONFIG ${DROGON_CONFIG_LOC})
|
||||||
else()
|
else()
|
||||||
set(DROGON_CONFIG "\"config.json\"")
|
set(DROGON_CONFIG "\"../config.json\"")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
configure_file(ccash_config.hpp.in ${CMAKE_CURRENT_SOURCE_DIR}/include/ccash_config.hpp)
|
configure_file(ccash_config.hpp.in ${CMAKE_CURRENT_SOURCE_DIR}/include/ccash_config.hpp)
|
||||||
|
|
|
||||||
|
|
@ -37,3 +37,5 @@ version 3 will work
|
||||||
etc
|
etc
|
||||||
*/
|
*/
|
||||||
#define API_VERSION 1
|
#define API_VERSION 1
|
||||||
|
|
||||||
|
#define MULTI_THREADED true
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
#include <parallel-hashmap/parallel_hashmap/phmap.h>
|
#include <parallel-hashmap/parallel_hashmap/phmap.h>
|
||||||
#include "user.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"
|
#include "change_flag.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -14,6 +14,7 @@ using BankResponse = std::pair<drogon::HttpStatusCode, Json::Value>;
|
||||||
|
|
||||||
class Bank
|
class Bank
|
||||||
{
|
{
|
||||||
|
#if MULTI_THREADED
|
||||||
phmap::parallel_flat_hash_map<
|
phmap::parallel_flat_hash_map<
|
||||||
std::string, User,
|
std::string, User,
|
||||||
xxHashStringGen,
|
xxHashStringGen,
|
||||||
|
|
@ -22,10 +23,17 @@ class Bank
|
||||||
4UL,
|
4UL,
|
||||||
std::mutex>
|
std::mutex>
|
||||||
users;
|
users;
|
||||||
|
#else
|
||||||
|
phmap::parallel_flat_hash_map<std::string, User, xxHashStringGen> users;
|
||||||
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
|
#if MULTI_THREADED
|
||||||
ChangeFlag save_flag;
|
ChangeFlag save_flag;
|
||||||
|
#else
|
||||||
|
bool save_flag = false;
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
6
main.cpp
6
main.cpp
|
|
@ -35,7 +35,13 @@ int main(int argc, char **argv)
|
||||||
<< "\nSSE 3 : " << (__builtin_cpu_supports("sse3") ? "enabled" : "disabled")
|
<< "\nSSE 3 : " << (__builtin_cpu_supports("sse3") ? "enabled" : "disabled")
|
||||||
<< "\nSSE 4.1 : " << (__builtin_cpu_supports("sse4.1") ? "enabled" : "disabled")
|
<< "\nSSE 4.1 : " << (__builtin_cpu_supports("sse4.1") ? "enabled" : "disabled")
|
||||||
<< "\nSSE 4.2 : " << (__builtin_cpu_supports("sse4.2") ? "enabled" : "disabled")
|
<< "\nSSE 4.2 : " << (__builtin_cpu_supports("sse4.2") ? "enabled" : "disabled")
|
||||||
|
#if MULTI_THREADED
|
||||||
<< "\n\nThreads : " << get_nprocs() + 1
|
<< "\n\nThreads : " << get_nprocs() + 1
|
||||||
|
<< "\nMulti thread : disabled"
|
||||||
|
#else
|
||||||
|
<< "\n\nThreads : " << 2
|
||||||
|
<< "\nMulti thread : disabled"
|
||||||
|
#endif
|
||||||
<< std::endl; //flushing before EventLoop
|
<< 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");
|
static_assert(bool(MAX_LOG_SIZE) == bool(PRE_LOG_SIZE), "You must either utilize both or neither logging variables.\n");
|
||||||
|
|
|
||||||
42
src/bank.cpp
42
src/bank.cpp
|
|
@ -18,9 +18,17 @@ __attribute__((always_inline)) inline bool ValidUsrname(const std::string &name)
|
||||||
|
|
||||||
using namespace drogon;
|
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;
|
uint64_t res = 0;
|
||||||
users.if_contains(name, [&res](const User &u) { res = u.balance + 1; });
|
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;
|
BankResponse state;
|
||||||
|
#if MULTI_THREADED
|
||||||
std::shared_lock<std::shared_mutex> lock{send_funds_l}; //about 10% of this function's cost
|
std::shared_lock<std::shared_mutex> lock{send_funds_l}; //about 10% of this function's cost
|
||||||
|
#endif
|
||||||
#if MAX_LOG_SIZE > 0
|
#if MAX_LOG_SIZE > 0
|
||||||
Transaction temp(a_name, b_name, amount);
|
Transaction temp(a_name, b_name, amount);
|
||||||
if (!users.modify_if(a_name, [&temp, &state, amount](User &a) {
|
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
|
#endif
|
||||||
|
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
|
#if MULTI_THREADED
|
||||||
save_flag.SetChangesOn(); //about 5% of this function's cost
|
save_flag.SetChangesOn(); //about 5% of this function's cost
|
||||||
|
#else
|
||||||
|
save_flag = true;
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
return state;
|
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); });
|
users.modify_if(name, [&new_pass](User &u) { u.password = xxHashStringGen{}(new_pass); });
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
|
#if MULTI_THREADED
|
||||||
save_flag.SetChangesOn();
|
save_flag.SetChangesOn();
|
||||||
|
#else
|
||||||
|
save_flag = true;
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
|
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 (users.modify_if(name, [amount](User &u) { u.balance = amount; }))
|
||||||
{
|
{
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
|
#if MULTI_THREADED
|
||||||
save_flag.SetChangesOn();
|
save_flag.SetChangesOn();
|
||||||
|
#else
|
||||||
|
save_flag = true;
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
return {k200OK, "Balance set!"};
|
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"};
|
return {k400BadRequest, "Invalid Name, breaks size and/or character restrictions"};
|
||||||
}
|
}
|
||||||
|
#if MULTI_THREADED
|
||||||
std::shared_lock<std::shared_mutex> lock{size_l};
|
std::shared_lock<std::shared_mutex> lock{size_l};
|
||||||
|
#endif
|
||||||
return (users.try_emplace_l(
|
return (users.try_emplace_l(
|
||||||
std::move(name), [](User &) {}, std::move(init_pass)))
|
std::move(name), [](User &) {}, std::move(init_pass)))
|
||||||
? BankResponse(k200OK, "User added!")
|
? 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"};
|
return {k400BadRequest, "Invalid Name, breaks size and/or character restrictions"};
|
||||||
}
|
}
|
||||||
|
#if MULTI_THREADED
|
||||||
std::shared_lock<std::shared_mutex> lock{size_l};
|
std::shared_lock<std::shared_mutex> lock{size_l};
|
||||||
|
#endif
|
||||||
return (users.try_emplace_l(
|
return (users.try_emplace_l(
|
||||||
std::move(name), [](User &) {}, init_bal, std::move(init_pass)))
|
std::move(name), [](User &) {}, init_bal, std::move(init_pass)))
|
||||||
? BankResponse(k200OK, "User added!")
|
? 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
|
BankResponse Bank::DelUser(const std::string &name) noexcept
|
||||||
{
|
{
|
||||||
|
#if MULTI_THREADED
|
||||||
std::shared_lock<std::shared_mutex> lock{size_l};
|
std::shared_lock<std::shared_mutex> lock{size_l};
|
||||||
|
#endif
|
||||||
#if RETURN_ON_DEL
|
#if RETURN_ON_DEL
|
||||||
uint32_t bal;
|
uint32_t bal;
|
||||||
if (users.erase_if(name, [this, &bal, &name](User &u) {
|
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()
|
void Bank::Save()
|
||||||
{
|
{
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
if (save_flag.GetChangeState())
|
if (GetChangeState())
|
||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
Json::Value temp;
|
Json::Value temp;
|
||||||
|
|
||||||
//loading info into json temp
|
//loading info into json temp
|
||||||
{
|
{
|
||||||
|
#if MULTI_THREADED
|
||||||
std::scoped_lock<std::shared_mutex, std::shared_mutex> lock{size_l, send_funds_l};
|
std::scoped_lock<std::shared_mutex, std::shared_mutex> lock{size_l, send_funds_l};
|
||||||
|
#endif
|
||||||
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
|
||||||
|
|
@ -212,7 +240,11 @@ void Bank::Save()
|
||||||
user_save.close();
|
user_save.close();
|
||||||
}
|
}
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
|
#if MULTI_THREADED
|
||||||
save_flag.SetChangesOff();
|
save_flag.SetChangesOff();
|
||||||
|
#else
|
||||||
|
save_flag = true;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue