sfm(th): add mtx,locks,l_guards;IS_RUNNING->atomic
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-01-21 05:32:23 +01:00
parent b7c7558e1f
commit b5bf269494
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 27 additions and 12 deletions

View File

@ -14,11 +14,11 @@
#include <cstdint>
#include <fstream>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <utility>
namespace fortuna {
constinit bool SeedFileManager::RUNNING{false};
SeedFileManager::SeedFileManager(
const fortuna::accumulator::Accumulator& da_accumulator) noexcept {
@ -29,19 +29,27 @@ SeedFileManager::~SeedFileManager() noexcept {
}
auto SeedFileManager::is_job_running() -> bool {
return RUNNING;
std::lock_guard<std::recursive_mutex> lg(mtx);
return this->IS_RUNNING;
}
auto SeedFileManager::set_job_running(bool running) -> void {
SeedFileManager::RUNNING = running;
std::lock_guard<std::recursive_mutex> lg(mtx);
this->IS_RUNNING = running;
}
auto SeedFileManager::do_stuff() -> void {
std::unique_lock<std::recursive_mutex> ul(mtx);
update_seed_file();
do_task.thread_pls(config.write_interval, [this] { write_seed_file(); });
}
auto SeedFileManager::seed_file_exists() const -> bool {
std::lock_guard<std::recursive_mutex> lg(mtx);
// based on https://stackoverflow.com/a/6296808
struct stat buf;
if (stat(config.seed_f_path.c_str(), &buf) != -1) {
@ -51,7 +59,9 @@ auto SeedFileManager::seed_file_exists() const -> bool {
}
auto SeedFileManager::update_seed_file() -> void {
fortuna::SeedFileManager::RUNNING = true;
std::lock_guard<std::recursive_mutex> lg(mtx);
fortuna::SeedFileManager::IS_RUNNING = true;
CryptoPP::SecByteBlock buff{config.seed_f_length};
try {
@ -72,7 +82,7 @@ auto SeedFileManager::update_seed_file() -> void {
const std::string msg{"\t[!] sfm: error opening seed file!\n"};
fmt::print("{} {}\n", msg, config.seed_f_path);
fortuna::SeedFileManager::RUNNING = false;
fortuna::SeedFileManager::IS_RUNNING = false;
throw std::runtime_error(msg);
}
@ -89,7 +99,7 @@ auto SeedFileManager::update_seed_file() -> void {
f_stream.gcount(),
config.seed_f_length);
fortuna::SeedFileManager::RUNNING = false;
fortuna::SeedFileManager::IS_RUNNING = false;
throw std::runtime_error(msg);
}
}
@ -103,13 +113,15 @@ auto SeedFileManager::update_seed_file() -> void {
}
catch (std::exception& e) {
fmt::print("{}\n", e.what());
fortuna::SeedFileManager::RUNNING = false;
fortuna::SeedFileManager::IS_RUNNING = false;
throw;
}
}
auto SeedFileManager::write_seed_file() -> void {
fortuna::SeedFileManager::RUNNING = true;
std::lock_guard<std::recursive_mutex> lg(mtx);
fortuna::SeedFileManager::IS_RUNNING = true;
CryptoPP::SecByteBlock buff{config.seed_f_length};
const std::string da_buff{accumulator.get_random_data(
static_cast<unsigned int>(config.seed_f_length))};
@ -138,7 +150,7 @@ auto SeedFileManager::write_seed_file() -> void {
catch (std::exception& e) {
fmt::print("[!] sfm: error writing to seed file!\n");
fmt::print("{}\n", e.what());
fortuna::SeedFileManager::RUNNING = false;
fortuna::SeedFileManager::IS_RUNNING = false;
throw;
}
}

View File

@ -4,7 +4,9 @@
#include "accumulator.h"
#include "do_task.h"
#include <atomic>
#include <chrono>
#include <mutex>
#include <string>
namespace fortuna {
@ -21,7 +23,7 @@ public:
conf(){};
};
static auto is_job_running() -> bool;
auto is_job_running() -> bool;
auto do_stuff() -> void;
explicit SeedFileManager(
@ -29,11 +31,12 @@ public:
~SeedFileManager() noexcept;
protected:
static auto set_job_running(bool) -> void;
auto set_job_running(bool) -> void;
private:
const conf config;
static constinit bool RUNNING;
mutable std::recursive_mutex mtx;
std::atomic<bool> IS_RUNNING{false};
DoTask do_task;
fortuna::accumulator::Accumulator accumulator;