sfm(th): add mtx,locks,l_guards;IS_RUNNING->atomic
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
b7c7558e1f
commit
b5bf269494
|
@ -14,11 +14,11 @@
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
namespace fortuna {
|
namespace fortuna {
|
||||||
constinit bool SeedFileManager::RUNNING{false};
|
|
||||||
|
|
||||||
SeedFileManager::SeedFileManager(
|
SeedFileManager::SeedFileManager(
|
||||||
const fortuna::accumulator::Accumulator& da_accumulator) noexcept {
|
const fortuna::accumulator::Accumulator& da_accumulator) noexcept {
|
||||||
|
@ -29,19 +29,27 @@ SeedFileManager::~SeedFileManager() noexcept {
|
||||||
}
|
}
|
||||||
|
|
||||||
auto SeedFileManager::is_job_running() -> bool {
|
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 {
|
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 {
|
auto SeedFileManager::do_stuff() -> void {
|
||||||
|
std::unique_lock<std::recursive_mutex> ul(mtx);
|
||||||
|
|
||||||
update_seed_file();
|
update_seed_file();
|
||||||
do_task.thread_pls(config.write_interval, [this] { write_seed_file(); });
|
do_task.thread_pls(config.write_interval, [this] { write_seed_file(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
auto SeedFileManager::seed_file_exists() const -> bool {
|
auto SeedFileManager::seed_file_exists() const -> bool {
|
||||||
|
std::lock_guard<std::recursive_mutex> lg(mtx);
|
||||||
|
|
||||||
// based on https://stackoverflow.com/a/6296808
|
// based on https://stackoverflow.com/a/6296808
|
||||||
struct stat buf;
|
struct stat buf;
|
||||||
if (stat(config.seed_f_path.c_str(), &buf) != -1) {
|
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 {
|
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};
|
CryptoPP::SecByteBlock buff{config.seed_f_length};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -72,7 +82,7 @@ auto SeedFileManager::update_seed_file() -> void {
|
||||||
const std::string msg{"\t[!] sfm: error opening seed file!\n"};
|
const std::string msg{"\t[!] sfm: error opening seed file!\n"};
|
||||||
fmt::print("{} {}\n", msg, config.seed_f_path);
|
fmt::print("{} {}\n", msg, config.seed_f_path);
|
||||||
|
|
||||||
fortuna::SeedFileManager::RUNNING = false;
|
fortuna::SeedFileManager::IS_RUNNING = false;
|
||||||
throw std::runtime_error(msg);
|
throw std::runtime_error(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,7 +99,7 @@ auto SeedFileManager::update_seed_file() -> void {
|
||||||
f_stream.gcount(),
|
f_stream.gcount(),
|
||||||
config.seed_f_length);
|
config.seed_f_length);
|
||||||
|
|
||||||
fortuna::SeedFileManager::RUNNING = false;
|
fortuna::SeedFileManager::IS_RUNNING = false;
|
||||||
throw std::runtime_error(msg);
|
throw std::runtime_error(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,13 +113,15 @@ auto SeedFileManager::update_seed_file() -> void {
|
||||||
}
|
}
|
||||||
catch (std::exception& e) {
|
catch (std::exception& e) {
|
||||||
fmt::print("{}\n", e.what());
|
fmt::print("{}\n", e.what());
|
||||||
fortuna::SeedFileManager::RUNNING = false;
|
fortuna::SeedFileManager::IS_RUNNING = false;
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto SeedFileManager::write_seed_file() -> void {
|
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};
|
CryptoPP::SecByteBlock buff{config.seed_f_length};
|
||||||
const std::string da_buff{accumulator.get_random_data(
|
const std::string da_buff{accumulator.get_random_data(
|
||||||
static_cast<unsigned int>(config.seed_f_length))};
|
static_cast<unsigned int>(config.seed_f_length))};
|
||||||
|
@ -138,7 +150,7 @@ auto SeedFileManager::write_seed_file() -> void {
|
||||||
catch (std::exception& e) {
|
catch (std::exception& e) {
|
||||||
fmt::print("[!] sfm: error writing to seed file!\n");
|
fmt::print("[!] sfm: error writing to seed file!\n");
|
||||||
fmt::print("{}\n", e.what());
|
fmt::print("{}\n", e.what());
|
||||||
fortuna::SeedFileManager::RUNNING = false;
|
fortuna::SeedFileManager::IS_RUNNING = false;
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,9 @@
|
||||||
#include "accumulator.h"
|
#include "accumulator.h"
|
||||||
#include "do_task.h"
|
#include "do_task.h"
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace fortuna {
|
namespace fortuna {
|
||||||
|
@ -21,7 +23,7 @@ public:
|
||||||
conf(){};
|
conf(){};
|
||||||
};
|
};
|
||||||
|
|
||||||
static auto is_job_running() -> bool;
|
auto is_job_running() -> bool;
|
||||||
auto do_stuff() -> void;
|
auto do_stuff() -> void;
|
||||||
|
|
||||||
explicit SeedFileManager(
|
explicit SeedFileManager(
|
||||||
|
@ -29,11 +31,12 @@ public:
|
||||||
~SeedFileManager() noexcept;
|
~SeedFileManager() noexcept;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static auto set_job_running(bool) -> void;
|
auto set_job_running(bool) -> void;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const conf config;
|
const conf config;
|
||||||
static constinit bool RUNNING;
|
mutable std::recursive_mutex mtx;
|
||||||
|
std::atomic<bool> IS_RUNNING{false};
|
||||||
DoTask do_task;
|
DoTask do_task;
|
||||||
fortuna::accumulator::Accumulator accumulator;
|
fortuna::accumulator::Accumulator accumulator;
|
||||||
|
|
||||||
|
|
Reference in New Issue