<commit_before>#include "event.h"
#include <unistd.h>
#include <stdlib.h>

#define internal static

internal ax_event_loop EventLoop = {};

/* TODO(koekeishiya): Must be thread-safe. Called through AXLibConstructEvent macro */
void AXLibAddEvent(ax_event Event)
{
    if(Event.Handle)
        EventLoop.Queue.push(Event);
}

/* TODO(koekeishiya): Should event context be freed (?) */
internal void
AXLibDestroyEvent(ax_event *Event)
{
    if(Event->Context)
        free(Event->Context);

    Event->Handle = NULL;
    Event->Context = NULL;
}

/* NOTE(koekeishiya): Uses dynamic dispatch to process events of any type. */
internal void *
AXLibProcessEventQueue(void *)
{
    while(EventLoop.Running)
    {
        while(!EventLoop.Queue.empty())
        {
            ax_event Event = EventLoop.Queue.front();
            EventLoop.Queue.pop();

            (*Event.Handle)(&Event);
            AXLibDestroyEvent(&Event);
        }

        usleep(100000);
    }

    return NULL;
}

void AXLibStartEventLoop()
{
    if(!EventLoop.Running)
    {
        while(!EventLoop.Queue.empty())
            EventLoop.Queue.pop();

        EventLoop.Running = true;
        pthread_create(&EventLoop.Worker, NULL, &AXLibProcessEventQueue, NULL);
    }
}

void AXLibStopEventLoop()
{
    if(EventLoop.Running)
    {
        EventLoop.Running = false;

        void *Unused;
        pthread_join(EventLoop.Worker, &Unused);
    }
}<commit_msg>removed unnecessary popping of queue<commit_after>#include "event.h"
#include <unistd.h>
#include <stdlib.h>

#define internal static

internal ax_event_loop EventLoop = {};

/* TODO(koekeishiya): Must be thread-safe. Called through AXLibConstructEvent macro */
void AXLibAddEvent(ax_event Event)
{
    if(Event.Handle)
        EventLoop.Queue.push(Event);
}

/* TODO(koekeishiya): Should event context be freed (?) */
internal void
AXLibDestroyEvent(ax_event *Event)
{
    if(Event->Context)
        free(Event->Context);

    Event->Handle = NULL;
    Event->Context = NULL;
}

/* NOTE(koekeishiya): Uses dynamic dispatch to process events of any type. */
internal void *
AXLibProcessEventQueue(void *)
{
    while(EventLoop.Running)
    {
        while(!EventLoop.Queue.empty())
        {
            ax_event Event = EventLoop.Queue.front();
            EventLoop.Queue.pop();

            (*Event.Handle)(&Event);
            AXLibDestroyEvent(&Event);
        }

        usleep(100000);
    }

    return NULL;
}

void AXLibStartEventLoop()
{
    if(!EventLoop.Running)
    {
        EventLoop.Running = true;
        pthread_create(&EventLoop.Worker, NULL, &AXLibProcessEventQueue, NULL);
    }
}

void AXLibStopEventLoop()
{
    if(EventLoop.Running)
    {
        EventLoop.Running = false;
        pthread_join(EventLoop.Worker, NULL);
    }
}
{'fraction_non_alphanumeric': 0.1253141831238779, 'fraction_numerical': 0.004308797127468581, 'mean_word_length': 2.8321870701513068, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '4359128', 'n_tokens_mistral': 905, 'n_tokens_neox': 869, 'n_words': 170}