This commit is contained in:
Scott E. Graves 2024-12-03 10:03:42 -06:00
parent 28dc153822
commit d0a8f9df58
2 changed files with 26 additions and 19 deletions

View File

@ -26,6 +26,7 @@
namespace repertory { namespace repertory {
class app_config; class app_config;
class polling final { class polling final {
public: public:
enum struct frequency { enum struct frequency {

View File

@ -29,15 +29,20 @@ polling polling::instance_;
void polling::frequency_thread( void polling::frequency_thread(
std::function<std::uint32_t()> get_frequency_seconds, frequency freq) { std::function<std::uint32_t()> get_frequency_seconds, frequency freq) {
while (not stop_requested_) { while (not stop_requested_) {
std::deque<std::future<void>> futures;
unique_mutex_lock lock(mutex_); unique_mutex_lock lock(mutex_);
for (const auto &item : items_) { auto futures = std::accumulate(
if (item.second.freq == freq) { items_.begin(), items_.end(), std::deque<std::future<void>>{},
[this, &freq](auto &&futures, auto &&item) {
if (item.second.freq != freq) {
return futures;
}
futures.emplace_back( futures.emplace_back(
std::async(std::launch::async, [this, &freq, item]() -> void { std::async(std::launch::async, [this, &freq, item]() -> void {
if (config_->get_event_level() == event_level::trace || if (config_->get_event_level() == event_level::trace ||
freq != frequency::second) { freq != frequency::second) {
event_system::instance().raise<polling_item_begin>(item.first); event_system::instance().raise<polling_item_begin>(
item.first);
} }
item.second.action(stop_requested_); item.second.action(stop_requested_);
if (config_->get_event_level() == event_level::trace || if (config_->get_event_level() == event_level::trace ||
@ -45,8 +50,8 @@ void polling::frequency_thread(
event_system::instance().raise<polling_item_end>(item.first); event_system::instance().raise<polling_item_end>(item.first);
} }
})); }));
} return futures;
} });
lock.unlock(); lock.unlock();
while (not futures.empty()) { while (not futures.empty()) {
@ -111,13 +116,14 @@ void polling::stop() {
event_system::instance().raise<service_shutdown_begin>("polling"); event_system::instance().raise<service_shutdown_begin>("polling");
stop_requested_ = true; stop_requested_ = true;
unique_mutex_lock lock2(mutex_); unique_mutex_lock thread_lock(mutex_);
notify_.notify_all(); notify_.notify_all();
lock2.unlock(); thread_lock.unlock();
high_frequency_thread_->join(); high_frequency_thread_->join();
low_frequency_thread_->join(); low_frequency_thread_->join();
second_frequency_thread_->join(); second_frequency_thread_->join();
high_frequency_thread_.reset(); high_frequency_thread_.reset();
low_frequency_thread_.reset(); low_frequency_thread_.reset();
second_frequency_thread_.reset(); second_frequency_thread_.reset();