conservative saving

This commit is contained in:
EntireTwix 2021-06-23 15:23:54 -07:00
parent ac859f2c17
commit cf036901bb
2 changed files with 28 additions and 1 deletions

View file

@ -1,7 +1,8 @@
#pragma once
#include <iostream> //temporary
#include <fstream>
#include <iostream>
#include <shared_mutex>
#include <atomic>
#include "error_responses.hpp"
#include "parallel-hashmap/parallel_hashmap/phmap.h"
#include "user.h"
@ -18,6 +19,16 @@ private:
std::mutex>
users;
#if CONSERVATIVE_DISK_SAVE
std::atomic<bool> change_flag = false; //if true changes have been made
void ChangesMade() noexcept; //called after making changes
void ChangesSaved() noexcept; //called after saving
#elif
#define ChangesMade() ;
#define ChangesSaved() ;
#endif
/**
* @brief size_l should be grabbed if the operation MODIFIES the size (shared), this is so that when save claims unique
*

View file

@ -1,11 +1,27 @@
#include "bank.h"
#if CONSERVATIVE_DISK_SAVE
void Bank::ChangesMade() noexcept
{
while (change_flag.exchange(true, std::memory_order_relaxed))
;
std::atomic_thread_fence(std::memory_order_acquire);
}
void Bank::ChangesSaved() noexcept
{
std::atomic_thread_fence(std::memory_order_release);
change_flag.store(false, std::memory_order_relaxed);
}
#endif
int_fast8_t Bank::AddUser(const std::string &name, const std::string &init_pass) noexcept
{
if (name.size() > max_name_size)
{
return ErrorResponse::NameTooLong;
}
//replace with string find
for (char c : name)
{
if (c == ' ')