refactor
This commit is contained in:
		| @@ -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 { | ||||||
|   | |||||||
| @@ -29,24 +29,29 @@ 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>>{}, | ||||||
|         futures.emplace_back( |         [this, &freq](auto &&futures, auto &&item) { | ||||||
|             std::async(std::launch::async, [this, &freq, item]() -> void { |           if (item.second.freq != freq) { | ||||||
|               if (config_->get_event_level() == event_level::trace || |             return futures; | ||||||
|                   freq != frequency::second) { |           } | ||||||
|                 event_system::instance().raise<polling_item_begin>(item.first); |  | ||||||
|               } |           futures.emplace_back( | ||||||
|               item.second.action(stop_requested_); |               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_end>(item.first); |                   event_system::instance().raise<polling_item_begin>( | ||||||
|               } |                       item.first); | ||||||
|             })); |                 } | ||||||
|       } |                 item.second.action(stop_requested_); | ||||||
|     } |                 if (config_->get_event_level() == event_level::trace || | ||||||
|  |                     freq != frequency::second) { | ||||||
|  |                   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(); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user