1 #include "EventService.h"
2 #include "WaitFreeQueue.h"
3 #include "ActiveMarker.h"
11 TimedEvent::TimedEvent(EventService& eventService,
12 const std::chrono::steady_clock::duration& duration,
13 const std::function<
void()>& callback) :
14 m_timer(eventService.createTimer())
17 m_timer->expires_from_now(duration);
18 m_timer->async_wait([&, callback](
const boost::system::error_code& ec) {
25 TimedEvent::~TimedEvent() =
default;
28 m_io_service(io_service),
29 m_work(new boost::asio::io_service::work(io_service)),
30 m_background_handlers_queue(new
WaitFreeQueue<std::function<void()>>())
42 std::unique_ptr<boost::asio::steady_timer> EventService::createTimer()
44 return std::make_unique<boost::asio::steady_timer>(m_io_service);
48 std::shared_ptr<bool> activeMarker)
50 m_background_handlers_queue->push([handler, activeMarker]() {
58 const std::chrono::steady_clock::duration& duration,
59 std::shared_ptr<bool> activeMarker) {
60 auto timer = std::make_shared<boost::asio::steady_timer>(m_io_service);
61 timer->expires_from_now(duration);
62 timer->async_wait([&, handler, activeMarker, timer](
const boost::system::error_code& ec) {
72 collectHandlersQueue();
75 while (!m_handlers.empty()) {
76 std::function<void()> handler = std::move(this->m_handlers.front());
77 m_handlers.pop_front();
80 collectHandlersQueue();
85 size_t EventService::collectHandlersQueue()
88 WaitFreeQueue<std::function<void()>>::node * x = m_background_handlers_queue->pop_all();
92 m_handlers.push_back(std::move(tmp->data));
100 collectHandlersQueue();
102 if (!m_handlers.empty()) {
103 std::function<void()> handler = std::move(this->m_handlers.front());
104 m_handlers.pop_front();