2012-08-28 22:32:15 +02:00
|
|
|
#include "event_manager.hh"
|
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
#include <poll.h>
|
2012-08-28 22:32:15 +02:00
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
FDWatcher::FDWatcher(int fd, Callback callback)
|
|
|
|
: m_fd{fd}, m_callback{std::move(callback)}
|
2012-12-03 18:49:09 +01:00
|
|
|
{
|
2013-01-31 18:58:25 +01:00
|
|
|
EventManager::instance().m_fd_watchers.insert(this);
|
2012-12-03 18:49:09 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
FDWatcher::~FDWatcher()
|
2012-08-28 22:32:15 +02:00
|
|
|
{
|
2013-01-31 18:58:25 +01:00
|
|
|
EventManager::instance().m_fd_watchers.erase(this);
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|
|
|
|
|
2014-11-25 02:00:18 +01:00
|
|
|
void FDWatcher::run(EventMode mode)
|
|
|
|
{
|
|
|
|
m_callback(*this, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Timer(TimePoint date, Callback callback, EventMode mode)
|
|
|
|
: m_date{date}, m_callback{std::move(callback)}, m_mode(mode)
|
2013-01-14 19:07:38 +01:00
|
|
|
{
|
2014-08-15 00:51:24 +02:00
|
|
|
if (EventManager::has_instance())
|
|
|
|
EventManager::instance().m_timers.insert(this);
|
2013-01-14 19:07:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Timer::~Timer()
|
|
|
|
{
|
2014-08-15 00:51:24 +02:00
|
|
|
if (EventManager::has_instance())
|
|
|
|
EventManager::instance().m_timers.erase(this);
|
2013-01-14 19:07:38 +01:00
|
|
|
}
|
|
|
|
|
2014-11-25 02:00:18 +01:00
|
|
|
void Timer::run(EventMode mode)
|
2013-01-14 19:07:38 +01:00
|
|
|
{
|
2014-11-25 02:00:18 +01:00
|
|
|
if (mode & m_mode)
|
|
|
|
{
|
|
|
|
m_date = TimePoint::max();
|
|
|
|
m_callback(*this);
|
|
|
|
}
|
|
|
|
else // try again a little later
|
|
|
|
m_date = Clock::now() + std::chrono::milliseconds{10};
|
2013-01-14 19:07:38 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
EventManager::EventManager()
|
2012-08-28 22:32:15 +02:00
|
|
|
{
|
2013-01-10 18:54:40 +01:00
|
|
|
m_forced_fd.reserve(4);
|
|
|
|
}
|
2012-11-27 13:57:03 +01:00
|
|
|
|
2013-01-10 18:54:40 +01:00
|
|
|
EventManager::~EventManager()
|
|
|
|
{
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(m_fd_watchers.empty());
|
|
|
|
kak_assert(m_timers.empty());
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|
|
|
|
|
2014-11-25 02:00:18 +01:00
|
|
|
void EventManager::handle_next_events(EventMode mode)
|
2012-08-28 22:32:15 +02:00
|
|
|
{
|
2013-01-10 18:54:40 +01:00
|
|
|
std::vector<pollfd> events;
|
|
|
|
events.reserve(m_fd_watchers.size());
|
|
|
|
for (auto& watcher : m_fd_watchers)
|
|
|
|
events.emplace_back(pollfd{ watcher->fd(), POLLIN | POLLPRI, 0 });
|
2014-01-30 13:41:34 +01:00
|
|
|
|
|
|
|
TimePoint next_timer = TimePoint::max();
|
|
|
|
for (auto& timer : m_timers)
|
|
|
|
{
|
|
|
|
if (timer->next_date() <= next_timer)
|
|
|
|
next_timer = timer->next_date();
|
|
|
|
}
|
2014-03-18 19:53:35 +01:00
|
|
|
using namespace std::chrono;
|
|
|
|
auto timeout = duration_cast<milliseconds>(next_timer - Clock::now()).count();
|
|
|
|
poll(events.data(), events.size(), timeout < INT_MAX ? (int)timeout : INT_MAX);
|
2014-08-17 16:58:42 +02:00
|
|
|
|
|
|
|
// gather forced fds *after* poll, so that signal handlers can write to
|
|
|
|
// m_forced_fd, interupt poll, and directly be serviced.
|
|
|
|
std::vector<int> forced;
|
|
|
|
std::swap(forced, m_forced_fd);
|
|
|
|
|
2014-11-24 14:49:41 +01:00
|
|
|
for (auto& event : events)
|
2012-11-26 14:08:27 +01:00
|
|
|
{
|
|
|
|
const int fd = event.fd;
|
2012-12-03 18:49:09 +01:00
|
|
|
if (event.revents or contains(forced, fd))
|
2013-01-10 18:54:40 +01:00
|
|
|
{
|
2013-01-11 14:28:13 +01:00
|
|
|
auto it = find_if(m_fd_watchers,
|
|
|
|
[fd](FDWatcher* w) { return w->fd() == fd; });
|
2013-01-10 18:54:40 +01:00
|
|
|
if (it != m_fd_watchers.end())
|
2014-11-25 02:00:18 +01:00
|
|
|
(*it)->run(mode);
|
2013-01-10 18:54:40 +01:00
|
|
|
}
|
2012-11-26 14:08:27 +01:00
|
|
|
}
|
2013-01-14 19:07:38 +01:00
|
|
|
|
|
|
|
TimePoint now = Clock::now();
|
|
|
|
for (auto& timer : m_timers)
|
|
|
|
{
|
|
|
|
if (timer->next_date() <= now)
|
2014-11-25 02:00:18 +01:00
|
|
|
timer->run(mode);
|
2013-01-14 19:07:38 +01:00
|
|
|
}
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|
|
|
|
|
2012-10-27 15:01:13 +02:00
|
|
|
void EventManager::force_signal(int fd)
|
|
|
|
{
|
2013-01-10 18:54:40 +01:00
|
|
|
m_forced_fd.push_back(fd);
|
|
|
|
}
|
2012-10-27 15:01:13 +02:00
|
|
|
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|