fortuna: add task runner "do_task"; link pthread
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2021-12-12 18:42:47 +01:00
parent 2a67a37cbe
commit cf41d323a4
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
3 changed files with 66 additions and 2 deletions

View File

@ -183,8 +183,9 @@ add_subdirectory(lib/fmt EXCLUDE_FROM_ALL)
endif(NOT CMAKE_EXE_LINKER_FLAGS MATCHES "-fuse-ld=lld")
endif()
add_executable(fortuna main.cpp generator.cpp generator.h fortuna.cpp fortuna.h accumulator.cpp accumulator.h pool.cpp pool.h event_adder.h event_adder_impl.h event_scheduler.h entropy_src.h urandom_entropy_src.h)
add_executable(fortuna main.cpp generator.cpp generator.h fortuna.cpp fortuna.h accumulator.cpp accumulator.h pool.cpp pool.h event_adder.h event_adder_impl.h event_scheduler.h entropy_src.h urandom_entropy_src.h do_task.cpp do_task.h)
# ref: https://cmake.org/pipermail/cmake/2016-May/063400.html
target_link_libraries(fortuna
PRIVATE cryptopp
PRIVATE fmt::fmt-header-only)
PRIVATE fmt::fmt-header-only
PRIVATE pthread)

37
do_task.cpp Normal file
View File

@ -0,0 +1,37 @@
#ifndef FORTUNA_DO_TASK_CPP
#define FORTUNA_DO_TASK_CPP
#include "do_task.h"
#include <utility>
namespace fortuna {
auto DoTask::die_pls() -> void {
do_sleep.unlock();
threadie.join();
}
auto DoTask::thread_pls(const std::chrono::seconds& interval,
std::function<void()> callback) -> void {
if (threadie.joinable()) {
die_pls();
}
do_sleep.lock();
threadie = std::thread([this, interval, callback = std::move(callback)] {
while (!do_sleep.try_lock_for(interval)) {
callback();
}
});
}
DoTask::~DoTask() noexcept {
if (threadie.joinable()) {
die_pls();
}
}
} //namespace fortuna
#endif//FORTUNA_DO_TASK_CPP

26
do_task.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef FORTUNA_DO_TASK_H
#define FORTUNA_DO_TASK_H
#include <chrono>
#include <functional>
#include <mutex>
#include <thread>
namespace fortuna {
class DoTask {
private:
std::timed_mutex do_sleep;
std::thread threadie;
public:
~DoTask() noexcept;
auto thread_pls(const std::chrono::seconds& interval,
std::function<void()> callback) -> void;
auto die_pls() -> void;
}; // class DoTask
} // namespace fortuna
#endif//FORTUNA_DO_TASK_H