added drogon

This commit is contained in:
EntireTwix 2021-04-02 01:28:19 -07:00
parent db25d7deb9
commit 80ffdff068
4 changed files with 26 additions and 4 deletions

3
.gitmodules vendored
View file

@ -1,3 +1,6 @@
[submodule "parallel-hashmap"]
path = third_party/parallel-hashmap
url = https://github.com/greg7mdp/parallel-hashmap
[submodule "drogon"]
path = third_party/drogon
url = https://github.com/an-tao/drogon

View file

@ -11,4 +11,8 @@ set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC include)
target_include_directories(${PROJECT_NAME} PUBLIC include)
target_include_directories(${PROJECT_NAME} PUBLIC third_party)
add_subdirectory(third_party/drogon)
target_link_libraries(${PROJECT_NAME} PRIVATE drogon)

View file

@ -8,6 +8,10 @@ private:
uint_fast64_t balance;
std::string password;
//for read/write of object's state concurrently
std::mutex bal_lock;
std::mutex pass_lock;
public:
/**
* @brief User constructor
@ -26,6 +30,7 @@ public:
bool ChangePassword(const std::string &attempt, std::string &&new_pass)
{
std::lock_guard<std::mutex> lock{pass_lock};
const bool state = (password == attempt);
if (state)
{
@ -45,7 +50,14 @@ public:
*/
static bool SendFunds(User &a, User &b, uint_fast64_t amount, const std::string &attempt)
{
const bool state = (a.password == attempt) && (a.balance >= amount);
bool state;
{
std::lock_guard<std::mutex> lock{a.pass_lock};
state = (a.password == attempt);
}
std::scoped_lock<std::mutex, std::mutex> lock{a.bal_lock, b.bal_lock};
state = state && (a.balance >= amount);
if (state)
{
a.balance -= amount;
@ -59,8 +71,9 @@ public:
*
* @return the balance
*/
uint_fast64_t GetBal() const
uint_fast64_t GetBal()
{
std::lock_guard<std::mutex> lock{bal_lock};
return balance;
}
@ -70,8 +83,9 @@ public:
* @param attempt the password
* @return wether the passwords match
*/
bool VerifyPassword(const std::string &attempt) const
bool VerifyPassword(const std::string &attempt) //for connected services
{
std::lock_guard<std::mutex> lock{pass_lock};
return (password == attempt);
}
};

1
third_party/drogon vendored Submodule

@ -0,0 +1 @@
Subproject commit a19d0427ed1fe80f40e5d4d2c8450a48c807be20