diff --git a/seed_file_management.cpp b/seed_file_management.cpp index 035e6d7..ea7d03e 100644 --- a/seed_file_management.cpp +++ b/seed_file_management.cpp @@ -14,11 +14,11 @@ #include #include #include +#include #include #include 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 lg(mtx); + + return this->IS_RUNNING; } auto SeedFileManager::set_job_running(bool running) -> void { - SeedFileManager::RUNNING = running; + std::lock_guard lg(mtx); + + this->IS_RUNNING = running; } auto SeedFileManager::do_stuff() -> void { + std::unique_lock 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 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 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 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(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; } } diff --git a/seed_file_management.h b/seed_file_management.h index 9272443..690997b 100644 --- a/seed_file_management.h +++ b/seed_file_management.h @@ -4,7 +4,9 @@ #include "accumulator.h" #include "do_task.h" +#include #include +#include #include 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 IS_RUNNING{false}; DoTask do_task; fortuna::accumulator::Accumulator accumulator;