<commit_before>#include "io_manager.h"

namespace silica {
namespace io {

void io_manager::add_io(const io_base& io) {
    m_ios.push_back(std::cref(io));
}

std::list<io_event> io_manager::read(int timeout) {
    return read(std::chrono::milliseconds{timeout});
}

std::list<io_event> io_manager::read(timeval const& timeout) {

    // copy timeval because select may mutate it
    timeval tv (timeout);

    // create fdset
    fd_set fdset;
    const int nfds = make_fdset(fdset);

    // create return list
    std::list<io_event> result;

    // call select()
    if(select(nfds, &fdset, NULL, NULL, &tv) > 0) {
        for(io_base const& sock : m_ios) {
            if(FD_ISSET(sock.m_fd, &fdset)) {
                result.emplace_back(sock, EVENT_READABLE);
            }
        }
    }

    return result;

}

int io_manager::make_fdset(fd_set& fdset) const
{
    int max = -1;
    FD_ZERO(&fdset);
    for(io_base const& sock : m_ios) {
        FD_SET(sock.m_fd, &fdset);
        max = std::max(max, sock.m_fd);
    }
    return max;
}


}
}
<commit_msg>Bugfixes and error handling in select() logic<commit_after>#include "io_manager.h"

#include "common/error.h"
#include <errno.h>

namespace silica {
namespace io {

void io_manager::add_io(const io_base& io) {
    m_ios.push_back(std::cref(io));
}

std::list<io_event> io_manager::read(int timeout) {
    return read(std::chrono::milliseconds{timeout});
}

std::list<io_event> io_manager::read(timeval const& timeout) {

    // copy timeval because select may mutate it
    timeval tv (timeout);

    // create fdset
    fd_set fdset;
    const int nfds = make_fdset(fdset);

    // create return list
    std::list<io_event> result;

    // call select()
    int count = select(nfds, &fdset, NULL, NULL, &tv);
    if(count > 0) {
        for(io_base const& sock : m_ios) {
            if(FD_ISSET(sock.m_fd, &fdset)) {
                result.emplace_back(sock, EVENT_READABLE);
            }
        }
    }
    else if(count < 0){
        make_error("select(): errno = " << errno);
    }

    return result;

}

int io_manager::make_fdset(fd_set& fdset) const
{
    int max = -1;
    FD_ZERO(&fdset);
    for(io_base const& sock : m_ios) {
        FD_SET(sock.m_fd, &fdset);
        max = std::max(max, sock.m_fd);
    }
    return max + 1;
}


}
}

{'fraction_non_alphanumeric': 0.14075993091537134, 'fraction_numerical': 0.0025906735751295338, 'mean_word_length': 2.7613636363636362, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 12, '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': '6680131', 'n_tokens_mistral': 876, 'n_tokens_neox': 852, 'n_words': 180}