orotool/src/eventia/timer.cpp
King_DuckZ 7fb055cd40 Refactor event code.
This splits the thread pool code from the event code, and
class names are now wrong (I will update them in the next
commit). Also, this disregards the roar11 vs tf library
thing, taskflow was not working anyways and I will probably
remove all the conditional compilation stuff about it for
now.
2020-08-09 19:26:18 +01:00

47 lines
1 KiB
C++

#include "timer.hpp"
#include "private/context.hpp"
#include <ev++.h>
#include <mutex>
#include <cassert>
#include <iostream>
namespace eve {
struct EvTimerTask::LocalData {
explicit LocalData (const Context& ctx);
ev::timer timer;
Context context;
};
EvTimerTask::LocalData::LocalData (const Context& ctx) :
context(ctx)
{
}
EvTimerTask::EvTimerTask (double delay, const Context& ctx) :
m_local(std::make_unique<LocalData>(ctx))
{
assert(m_local->context.ev_mutex);
m_local->timer.set(*m_local->context.loop);
m_local->timer.set<EvTimerTask, &EvTimerTask::on_timer_ev>(this);
this->set_timer(delay);
}
EvTimerTask::~EvTimerTask() noexcept = default;
void EvTimerTask::on_timer_ev() {
std::cout << "EvTimerTask::on_timer_ev()\n";
m_local->timer.stop();
this->on_timer();
}
void EvTimerTask::set_timer (double delay) {
std::unique_lock<std::mutex> lock(*m_local->context.ev_mutex);
ev_now_update(*m_local->context.loop);
m_local->timer.start(delay, 0.0);
m_local->context.async->send();
}
} //namespace eve