fortuna: add task runner "do_task"; link pthread
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
2a67a37cbe
commit
cf41d323a4
|
@ -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)
|
||||
|
|
|
@ -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
|
|
@ -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
|
Reference in New Issue