fortuna: add thread-synchronising latch

This commit is contained in:
surtur 2022-01-20 08:08:11 +01:00
parent 5b1c9ba71c
commit 3b537e1e26
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 24 additions and 0 deletions

View File

@ -17,6 +17,7 @@
#include <exception>
#include <memory>
#include <mutex>
#include <thread>
namespace fortuna {
static constexpr const unsigned int min_pool_size{64};
@ -31,12 +32,20 @@ Fortuna::Fortuna() {
fmt::print(stderr, "{}\n", e.what());
// perhaps die on error
}
static constexpr const std::chrono::milliseconds duration{150};
std::this_thread::sleep_for(duration);
th_gen = std::thread(&Fortuna::generator_service,
this,
std::make_shared<fortuna::generator::Generator>());
th_accu = std::thread(&Fortuna::accumulator_service, this);
th_sfm = std::thread(&Fortuna::seed_file_manager_service, this);
th_urandom = std::thread(&Fortuna::urandom_entropy_src_service, this);
this->sync_point.count_down();
}
Fortuna::~Fortuna() noexcept {
if (th_gen.joinable()) {
@ -124,6 +133,10 @@ auto Fortuna::generator_service(
std::shared_ptr<fortuna::generator::Generator> Gen) -> void {
int i{0};
static constexpr const std::chrono::milliseconds sleep_time{1000};
fmt::print("[i] gen: waiting on a latch\n");
this->sync_point.count_down();
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");
@ -145,6 +158,10 @@ auto Fortuna::generator_service(
auto Fortuna::accumulator_service() -> void {
static constexpr const std::chrono::seconds sleep_time{10};
fmt::print("[i] accu: waiting on a latch\n");
this->sync_point.count_down();
std::chrono::system_clock::time_point time_point;
std::unique_lock<std::mutex> p_ul(print_mtx);
fmt::print("[i] fortuna: starting accumulator service\n");
@ -162,6 +179,10 @@ auto Fortuna::accumulator_service() -> void {
auto Fortuna::seed_file_manager_service() -> void {
static constexpr const std::chrono::seconds checkup_interval{10};
fmt::print("[i] sfm: waiting on a latch\n");
this->sync_point.count_down();
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);

View File

@ -9,6 +9,7 @@
#include <array>
#include <chrono>
#include <latch>
#include <memory>
#include <mutex>
#include <thread>
@ -26,6 +27,7 @@ public:
std::thread th_accu;
std::thread th_sfm;
std::thread th_urandom;
std::latch sync_point{5}; // 4 services + main thread
Fortuna();
~Fortuna() noexcept;
@ -79,6 +81,7 @@ public:
e.what());
}
p_ul.try_lock();
this->sync_point.count_down();
fmt::print("[*] fortuna: PRNG initialized\n");
}