<commit_before>#include "arch/io/timer_provider.hpp"  // For RDB_USE_TIMER_ITIMER_PROVIDER

#if RDB_USE_TIMER_ITIMER_PROVIDER


#include <sys/time.h>
#include <string.h>

// TODO(OSX) We never free this list (just its elements).  Make it part of the thread pool.
__thread intrusive_list_t<timer_itimer_provider_t> *itimer_callback_list = 0;

void add_to_itimer_callback_list(timer_itimer_provider_t *provider) {
    if (!itimer_callback_list) {
        itimer_callback_list = new intrusive_list_t<timer_itimer_provider_t>;
    }
    itimer_callback_list->push_back(provider);
}

void remove_from_itimer_callback_list(timer_itimer_provider_t *provider) {
    guarantee(itimer_callback_list);
    itimer_callback_list->remove(provider);
}

void timer_itimer_forward_alrm() {
    if (itimer_callback_list) {
        timer_itimer_provider_t *next = NULL;
        timer_itimer_provider_t *curr = itimer_callback_list->head();
        while (curr != NULL) {
            next = itimer_callback_list->next(curr);
            curr->on_alrm();
            curr = next;
        }
    }
}

timer_itimer_provider_t::timer_itimer_provider_t(linux_event_queue_t *queue,
                                                 timer_provider_callback_t *callback,
                                                 UNUSED time_t secs, UNUSED int32_t nsecs)
    : queue_(queue), callback_(callback) {
    add_to_itimer_callback_list(this);
}

timer_itimer_provider_t::~timer_itimer_provider_t() {
    remove_from_itimer_callback_list(this);<commit_msg>Made timer_itimer_provider_t not memory-leak its thread-local variable.<commit_after>#include "arch/io/timer_provider.hpp"  // For RDB_USE_TIMER_ITIMER_PROVIDER

#if RDB_USE_TIMER_ITIMER_PROVIDER


#include <sys/time.h>
#include <string.h>

struct timer_itimer_data_t {
    // A list of providers whose on_alrm functions we should call.
    intrusive_list_t<timer_itimer_provider_t> callbacks;

    // Used to prevent force access to timer_itimer mechanisms to happen outside.
    bool in_alrm_handler;

    timer_itimer_data_t() : in_alrm_handler(false) { }
};

__thread timer_itimer_data_t *itimer_data = 0;

void add_to_itimer_callback_list(timer_itimer_provider_t *provider) {
    if (!itimer_data) {
        itimer_data = itimer_data = new timer_itimer_data_t;
    }

    guarantee(!itimer_data->in_alrm_handler);
    itimer_data->callbacks.push_back(provider);
}

void remove_from_itimer_callback_list(timer_itimer_provider_t *provider) {
    guarantee(itimer_data != NULL);
    guarantee(!itimer_data->in_alrm_handler);

    itimer_data->callbacks.remove(provider);
    if (itimer_data->callbacks.empty()) {
        delete itimer_data;
        itimer_data = NULL;
    }
}

void timer_itimer_forward_alrm() {
    guarantee(!itimer_data->in_alrm_handler);
    itimer_data->in_alrm_handler = true;
    if (itimer_data) {
        timer_itimer_provider_t *next = NULL;
        timer_itimer_provider_t *curr = itimer_data->callbacks.head();
        while (curr != NULL) {
            next = itimer_data->callbacks.next(curr);
            curr->on_alrm();
            curr = next;
        }
    }
    itimer_data->in_alrm_handler = false;
}

timer_itimer_provider_t::timer_itimer_provider_t(linux_event_queue_t *queue,
                                                 timer_provider_callback_t *callback,
                                                 UNUSED time_t secs, UNUSED int32_t nsecs)
    : queue_(queue), callback_(callback) {
    add_to_itimer_callback_list(this);
}

timer_itimer_provider_t::~timer_itimer_provider_t() {
    remove_from_itimer_callback_list(this);
{'fraction_non_alphanumeric': 0.08879023307436182, 'fraction_numerical': 0.001664816870144284, 'mean_word_length': 3.4070904645476774, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3214176', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1217, 'n_words': 187}