2012-08-28 22:32:15 +02:00
|
|
|
#include "event_manager.hh"
|
|
|
|
|
2014-12-23 14:34:21 +01:00
|
|
|
#include "containers.hh"
|
|
|
|
|
2014-12-03 14:56:02 +01:00
|
|
|
#include <unistd.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
|
|
|
{
|
2014-12-09 22:59:47 +01:00
|
|
|
EventManager::instance().m_fd_watchers.push_back(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
|
|
|
{
|
2014-12-09 22:59:47 +01:00
|
|
|
unordered_erase(EventManager::instance().m_fd_watchers, 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);
|
|
|
|
}
|
|
|
|
|
2014-12-03 14:56:02 +01:00
|
|
|
void FDWatcher::close_fd()
|
|
|
|
{
|
2015-06-08 23:42:51 +02:00
|
|
|
if (m_fd != -1)
|
|
|
|
{
|
|
|
|
close(m_fd);
|
|
|
|
m_fd = -1;
|
|
|
|
}
|
2014-12-03 14:56:02 +01:00
|
|
|
}
|
|
|
|
|
2014-11-25 02:00:18 +01:00
|
|
|
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-12-19 00:12:58 +01:00
|
|
|
if (m_callback and EventManager::has_instance())
|
2014-12-09 22:59:47 +01:00
|
|
|
EventManager::instance().m_timers.push_back(this);
|
2013-01-14 19:07:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Timer::~Timer()
|
|
|
|
{
|
2014-12-19 00:12:58 +01:00
|
|
|
if (m_callback and EventManager::has_instance())
|
2014-12-09 22:59:47 +01:00
|
|
|
unordered_erase(EventManager::instance().m_timers, 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-12-19 00:12:58 +01:00
|
|
|
kak_assert(m_callback);
|
2014-11-29 21:14:52 +01:00
|
|
|
if (mode == m_mode)
|
2014-11-25 02:00:18 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2014-12-03 14:56:02 +01:00
|
|
|
FD_ZERO(&m_forced_fd);
|
2013-01-10 18:54:40 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-06-09 21:28:24 +02:00
|
|
|
void EventManager::handle_next_events(EventMode mode, sigset_t* sigmask)
|
2012-08-28 22:32:15 +02:00
|
|
|
{
|
2014-12-03 14:56:02 +01:00
|
|
|
int max_fd = 0;
|
|
|
|
fd_set rfds;
|
|
|
|
FD_ZERO(&rfds);
|
2013-01-10 18:54:40 +01:00
|
|
|
for (auto& watcher : m_fd_watchers)
|
2014-12-03 14:56:02 +01:00
|
|
|
{
|
|
|
|
const int fd = watcher->fd();
|
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
max_fd = std::max(fd, max_fd);
|
|
|
|
FD_SET(fd, &rfds);
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 13:41:34 +01:00
|
|
|
|
2014-12-12 14:55:32 +01:00
|
|
|
bool with_timeout = false;
|
2015-06-09 21:28:24 +02:00
|
|
|
timespec ts{};
|
2014-12-04 21:01:34 +01:00
|
|
|
if (not m_timers.empty())
|
2014-01-30 13:41:34 +01:00
|
|
|
{
|
2014-12-12 14:55:32 +01:00
|
|
|
auto next_date = (*std::min_element(
|
2014-12-04 21:01:34 +01:00
|
|
|
m_timers.begin(), m_timers.end(), [](Timer* lhs, Timer* rhs) {
|
|
|
|
return lhs->next_date() < rhs->next_date();
|
2014-12-12 14:55:32 +01:00
|
|
|
}))->next_date();
|
|
|
|
|
|
|
|
if (next_date != TimePoint::max())
|
|
|
|
{
|
|
|
|
with_timeout = true;
|
2015-06-09 21:28:24 +02:00
|
|
|
using namespace std::chrono; using ns = std::chrono::nanoseconds;
|
|
|
|
auto nsecs = std::max(ns(0), duration_cast<ns>(next_date - Clock::now()));
|
|
|
|
auto secs = duration_cast<seconds>(nsecs);
|
|
|
|
ts = timespec{ (time_t)secs.count(), (long)(nsecs - secs).count() };
|
2014-12-12 14:55:32 +01:00
|
|
|
}
|
2014-01-30 13:41:34 +01:00
|
|
|
}
|
2015-06-09 21:28:24 +02:00
|
|
|
int res = pselect(max_fd + 1, &rfds, nullptr, nullptr,
|
|
|
|
with_timeout ? &ts : nullptr, sigmask);
|
2014-08-17 16:58:42 +02:00
|
|
|
|
2015-06-09 14:27:51 +02:00
|
|
|
// copy forced fds *after* select, so that signal handlers can write to
|
|
|
|
// m_forced_fd, interupt select, and directly be serviced.
|
2014-12-03 14:56:02 +01:00
|
|
|
fd_set forced = m_forced_fd;
|
|
|
|
FD_ZERO(&m_forced_fd);
|
2014-08-17 16:58:42 +02:00
|
|
|
|
2014-12-03 14:56:02 +01:00
|
|
|
for (int fd = 0; fd < max_fd + 1; ++fd)
|
2012-11-26 14:08:27 +01:00
|
|
|
{
|
2014-12-03 14:56:02 +01:00
|
|
|
if ((res > 0 and FD_ISSET(fd, &rfds)) or FD_ISSET(fd, &forced))
|
2013-01-10 18:54:40 +01:00
|
|
|
{
|
2013-01-11 14:28:13 +01:00
|
|
|
auto it = find_if(m_fd_watchers,
|
2014-12-03 14:56:02 +01:00
|
|
|
[fd](const 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)
|
|
|
|
{
|
2014-12-03 14:56:02 +01:00
|
|
|
FD_SET(fd, &m_forced_fd);
|
2013-01-10 18:54:40 +01:00
|
|
|
}
|
2012-10-27 15:01:13 +02:00
|
|
|
|
2012-08-28 22:32:15 +02:00
|
|
|
}
|