<commit_before>#include "event_manager.hh"

#include <algorithm>

namespace Kakoune
{

EventManager::EventManager() { m_forced.reserve(4); }

void EventManager::watch(int fd, EventHandler handler)
{
    auto event = std::find_if(m_events.begin(), m_events.end(),
                              [&](const pollfd& pfd) { return pfd.fd == fd; });
    if (event != m_events.end())
        throw runtime_error("fd already watched");

    m_events.push_back(pollfd{ fd, POLLIN | POLLPRI, 0 });
    m_handlers.push_back(std::move(handler));
}

void EventManager::unwatch(int fd)
{
    for (size_t i = 0; i < m_events.size(); ++i)
    {
        if (m_events[i].fd == fd)
        {
            m_events.erase(m_events.begin() + i);
            m_handlers.erase(m_handlers.begin() + i);
            return;
        }
    }
}

void EventManager::handle_next_events()
{
    const int timeout_ms = 100;
    int res = poll(m_events.data(), m_events.size(), timeout_ms);
    if (res >= 0)
    {
        for (size_t i = 0; i < m_events.size(); ++i)
        {
            if (m_events[i].revents or contains(m_forced, m_events[i].fd))
                m_handlers[i](m_events[i].fd);
        }
        m_forced.clear();
    }
}

void EventManager::force_signal(int fd)
{
     m_forced.push_back(fd);
}

}
<commit_msg>EventManager: handle forced events even when poll return -1<commit_after>#include "event_manager.hh"

#include <algorithm>

namespace Kakoune
{

EventManager::EventManager() { m_forced.reserve(4); }

void EventManager::watch(int fd, EventHandler handler)
{
    auto event = std::find_if(m_events.begin(), m_events.end(),
                              [&](const pollfd& pfd) { return pfd.fd == fd; });
    if (event != m_events.end())
        throw runtime_error("fd already watched");

    m_events.push_back(pollfd{ fd, POLLIN | POLLPRI, 0 });
    m_handlers.push_back(std::move(handler));
}

void EventManager::unwatch(int fd)
{
    for (size_t i = 0; i < m_events.size(); ++i)
    {
        if (m_events[i].fd == fd)
        {
            m_events.erase(m_events.begin() + i);
            m_handlers.erase(m_handlers.begin() + i);
            return;
        }
    }
}

void EventManager::handle_next_events()
{
    const int timeout_ms = 100;
    int res = poll(m_events.data(), m_events.size(), timeout_ms);
    for (size_t i = 0; i < m_events.size(); ++i)
    {
        if ((res > 0 and m_events[i].revents) or
            contains(m_forced, m_events[i].fd))
            m_handlers[i](m_events[i].fd);
    }
    m_forced.clear();
}

void EventManager::force_signal(int fd)
{
     m_forced.push_back(fd);
}

}

{'fraction_non_alphanumeric': 0.15352220520673812, 'fraction_numerical': 0.006508422664624809, 'mean_word_length': 2.6596638655462184, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4186478', 'n_tokens_mistral': 948, 'n_tokens_neox': 934, 'n_words': 180}