<commit_before>#include <assert.h>
#include <stdbool.h>

#include <unistd.h>
#include <signal.h>
#include <sys/time.h>

#include "gameloop.h"

static const suseconds_t ONE_SIXTIETH_OF_A_SECOND = 1000000 / 60;

static const struct itimerval frame_timer = {
    .it_interval = {
        .tv_usec = ONE_SIXTIETH_OF_A_SECOND
    },
    .it_value = {
        .tv_usec = ONE_SIXTIETH_OF_A_SECOND
    }
};

static frame_function_t frame_function = NULL;
static volatile bool should_continue = false;
static volatile frame_t elapsed_frames = 0;

/* Store the old sigaction. */
static struct sigaction old_action;

static void gameloop(int signal) {
    assert(signal == SIGALRM);
    frame_function(elapsed_frames);
    elapsed_frames++;
}

static void init_gameloop(frame_function_t func) {
    struct sigaction action = {
        .sa_handler = gameloop,
        .sa_mask = 0,
        .sa_flags = 0
    };

    frame_function = func;<commit_msg>Add signal blocking.<commit_after>#include <assert.h>
#include <stdbool.h>

#include <unistd.h>
#include <signal.h>
#include <sys/time.h>

#include "gameloop.h"

static const suseconds_t ONE_SIXTIETH_OF_A_SECOND = 1000000 / 60;

static const struct itimerval frame_timer = {
    .it_interval = {
        .tv_usec = ONE_SIXTIETH_OF_A_SECOND
    },
    .it_value = {
        .tv_usec = ONE_SIXTIETH_OF_A_SECOND
    }
};

static frame_function_t frame_function = NULL;
static volatile bool should_continue = false;
static volatile frame_t elapsed_frames = 0;

/* Store the old sigaction. */
static struct sigaction old_action;
static sigset_t old_mask;

static void block_signals() {
    sigset_t all_signals;
    sigfillset(&all_signals);

    sigprocmask(SIG_BLOCK, &all_signals, &old_mask);
}

static void unblock_signals() {
    sigprocmask(SIG_SETMASK, &old_mask, NULL);
}

/** Function that runs on SIGALRM. */
static void gameloop(int signal) {
    assert(signal == SIGALRM);

    block_signals();
    frame_function(elapsed_frames);
    elapsed_frames++;
    unblock_signals();
}

static void init_gameloop(frame_function_t func) {
    struct sigaction action = {
        .sa_handler = gameloop,
        .sa_mask = 0,
        .sa_flags = 0
    };

    frame_function = func;
{'fraction_non_alphanumeric': 0.10103743797925124, 'fraction_numerical': 0.010825439783491205, 'mean_word_length': 3.5826446280991737, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '7341074', 'n_tokens_mistral': 827, 'n_tokens_neox': 801, 'n_words': 174}