<commit_before>#include <sys/timerfd.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/eventfd.h>
#include "event_queue.hpp"
#include "timer.hpp"

linux_timer_handler_t::linux_timer_handler_t(linux_event_queue_t *queue)
    : queue(queue)
{
    int res;
    
    timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
    check("Could not create timer", timer_fd < 0);

    res = fcntl(timer_fd, F_SETFL, O_NONBLOCK);
    check("Could not make timer non-blocking", res != 0);

    itimerspec timer_spec;
    bzero(&timer_spec, sizeof(timer_spec));
    timer_spec.it_value.tv_sec = timer_spec.it_interval.tv_sec = TIMER_TICKS_IN_MS / 1000;
    timer_spec.it_value.tv_nsec = timer_spec.it_interval.tv_nsec = (TIMER_TICKS_IN_MS % 1000) * 1000000L;
    res = timerfd_settime(timer_fd, 0, &timer_spec, NULL);
    check("Could not arm the timer.", res != 0);
    
    timer_ticks_since_server_startup = 0;
    
    queue->watch_resource(timer_fd, poll_event_in, this);
}

linux_timer_handler_t::~linux_timer_handler_t() {
    
    int res;
    
    // Delete the registered timers
    // TODO: We should instead assert there are no timers.
    while (linux_timer_token_t *t = timers.head()) {
        timers.remove(t);
        delete t;
    }
    
    res = close(timer_fd);
    check("Could not close the timer.", res != 0);
}<commit_msg>Asserting that timers are all deleted when the server shuts down<commit_after>#include <sys/timerfd.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/eventfd.h>
#include "event_queue.hpp"
#include "timer.hpp"

linux_timer_handler_t::linux_timer_handler_t(linux_event_queue_t *queue)
    : queue(queue)
{
    int res;
    
    timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
    check("Could not create timer", timer_fd < 0);

    res = fcntl(timer_fd, F_SETFL, O_NONBLOCK);
    check("Could not make timer non-blocking", res != 0);

    itimerspec timer_spec;
    bzero(&timer_spec, sizeof(timer_spec));
    timer_spec.it_value.tv_sec = timer_spec.it_interval.tv_sec = TIMER_TICKS_IN_MS / 1000;
    timer_spec.it_value.tv_nsec = timer_spec.it_interval.tv_nsec = (TIMER_TICKS_IN_MS % 1000) * 1000000L;
    res = timerfd_settime(timer_fd, 0, &timer_spec, NULL);
    check("Could not arm the timer.", res != 0);
    
    timer_ticks_since_server_startup = 0;
    
    queue->watch_resource(timer_fd, poll_event_in, this);
}

linux_timer_handler_t::~linux_timer_handler_t() {
    
    int res;
    
    // Delete the registered timers
    while (linux_timer_token_t *t = timers.head()) {
        assert(t->deleted);
        timers.remove(t);
        delete t;
    }
    
    res = close(timer_fd);
    check("Could not close the timer.", res != 0);
}
{'fraction_non_alphanumeric': 0.1216017634092579, 'fraction_numerical': 0.016164584864070537, 'mean_word_length': 3.678694158075601, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, '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': '5074912', 'n_tokens_mistral': 1055, 'n_tokens_neox': 1002, 'n_words': 204}