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)),
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()
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();
size_t processAllHandlers()
Processes all registered handlers.
size_t processOneHandler()
Processes one handler, if possible.
void runOnMainThreadDelayed(const std::function< void()> &handler, const std::chrono::steady_clock::duration &duration, std::shared_ptr< bool > activeMarker=std::make_shared< bool >(true))
EventService(boost::asio::io_service &io_service)
Ctor.
void runOnMainThread(const std::function< void()> &handler, std::shared_ptr< bool > activeMarker=std::make_shared< bool >(true))
Adds a handler which will be run on the main thread.
A queue optimized for insertion from background threads and consumption from one main thread...