fortuna: add print mutex, impl with unique_lock

* used for orderly printing to screen, currently only used by the two of
  the services: generator_service and seed_file_manager_service
* lock the mutex in a unique_lock only when printing to screen, unlock
  it immediately after printing is done
* use proper chrono type for sleep_time (instead of uint)
* also, generator_service is no longer a static method
This commit is contained in:
surtur 2022-01-10 07:43:55 +01:00
parent ebb1e46e1c
commit 1817b4a82e
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 18 additions and 6 deletions

View File

@ -24,7 +24,7 @@ Fortuna::Fortuna() {
catch (CryptoPP::Exception& e) {
fmt::print(stderr, "{}\n", e.what());
}
th_gen = std::thread(generator_service, &R.Gen);
th_gen = std::thread(&Fortuna::generator_service, this, &R.Gen);
th_sfm = std::thread(&Fortuna::seed_file_manager_service, this);
}
Fortuna::~Fortuna() noexcept {
@ -83,31 +83,41 @@ auto Fortuna::random_data(unsigned int n_bytes) -> void {
} // random_data
auto Fortuna::generator_service(fortuna::generator::Generator* Gen) -> void {
fmt::print("[i] fortuna: starting generator service\n");
int i{0};
uint sleep_time{1000}; // in ms
std::chrono::milliseconds sleep_time{1000};
std::chrono::system_clock::time_point time_point;
std::unique_lock<std::mutex> p_ul(print_mtx);
fmt::print("[i] fortuna: starting generator service\n");
p_ul.unlock();
while (true) {
p_ul.lock();
fmt::print("sleeping [{}]\n", i);
p_ul.unlock();
++i;
time_point = fortuna::Util::current_time();
p_ul.lock();
fmt::print("[*] g: @{}\n", time_point);
p_ul.unlock();
std::this_thread::sleep_until(time_point +
std::chrono::milliseconds(sleep_time));
}
}
auto Fortuna::seed_file_manager_service() -> void {
std::lock_guard<std::mutex> lg(mtx);
fmt::print("[i] fortuna: starting seed file manager service\n");
static constexpr const std::chrono::seconds checkup_interval{10};
std::unique_lock<std::mutex> p_ul(print_mtx);
fmt::print("[i] fortuna: starting seed file manager service\n");
fmt::print("[*] sfm: checkup interval {}\n", checkup_interval);
p_ul.unlock();
auto right_now{fortuna::Util::current_time()};
std::unique_lock<std::mutex> mtx_l(mtx);
SeedFileManager sfm(this->accumulator);
mtx_l.unlock();
while (true) {
right_now = fortuna::Util::current_time();
p_ul.lock();
fmt::print("[*] sfm: checkup time @{}\n", right_now);
if (!sfm.is_job_running()) {
fmt::print("[*] sfm: job not running, starting\n");
@ -121,6 +131,7 @@ auto Fortuna::seed_file_manager_service() -> void {
else {
fmt::print("[*] sfm: job running\n");
}
p_ul.unlock();
std::this_thread::sleep_until(right_now +
std::chrono::seconds(checkup_interval));
}

View File

@ -18,6 +18,7 @@ public:
static constexpr const unsigned int reseed_interval{10000};
static constexpr const char num_of_pools{32};
std::mutex mtx;
std::mutex print_mtx;
std::thread th_gen;
std::thread th_sfm;
@ -59,7 +60,7 @@ public:
fmt::print("PRNG initialized\n");
}
static auto generator_service(fortuna::generator::Generator*) -> void;
auto generator_service(fortuna::generator::Generator*) -> void;
auto seed_file_manager_service() -> void;