<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdatomic.h>

#include "knd_queue.h"
#include "knd_config.h"
#include "knd_utils.h"

int knd_queue_push(struct kndQueue *self,
                   void *elem)
{
    size_t head_pos, tail_pos, next_tail_pos;

    do {
        tail_pos = atomic_load_explicit(&self->tail_pos,
                                        memory_order_relaxed);
        head_pos = atomic_load_explicit(&self->head_pos,
                                        memory_order_relaxed);
        next_tail_pos = tail_pos + 1;

        if (next_tail_pos == head_pos) {
            knd_log("-- queue is full right now:  tail_pos:%zu head_pos:%zu ",
                    tail_pos, head_pos);
            return knd_LIMIT;
        }

        //knd_log(".. try pushing: tail pos:%zu  head pos:%zu",
        //        tail_pos, head_pos);

        if (next_tail_pos == self->capacity) {
            next_tail_pos = 0;
            if (head_pos == 0) {
                return knd_LIMIT;
            }
        }
    }
    while (!atomic_compare_exchange_weak(&self->tail_pos, &tail_pos, next_tail_pos));

    if (self->elems[next_tail_pos]) {
        knd_log("-- anomaly: next tail pos %zu is not empty?", next_tail_pos);
        return knd_FAIL;
    }
    self->elems[next_tail_pos] = elem;

    return knd_OK;
}

int knd_queue_pop(struct kndQueue *self,
                  void **result)
{
    void *elem;
    size_t head_pos, tail_pos, next_head_pos;

    do {
        tail_pos = atomic_load_explicit(&self->tail_pos,
                                        memory_order_relaxed);
        head_pos = atomic_load_explicit(&self->head_pos,
                                        memory_order_relaxed);
        if (tail_pos == head_pos) {
            //knd_log("-- queue is empty at head pos %zu", head_pos);
            return knd_NO_MATCH;
        }
        next_head_pos = head_pos + 1;

        if (next_head_pos == self->capacity) {
            next_head_pos = 0;
        }
        //knd_log("== try to pop: tail pos:%zu  next head pos:%zu",
        //        tail_pos, next_head_pos);
    }
    while (!atomic_compare_exchange_weak(&self->head_pos, &head_pos, next_head_pos));

    if (!self->elems[next_head_pos]) {
        knd_log("-- pos %zu is empty?", head_pos);
        return knd_FAIL;
    }

    elem = self->elems[next_head_pos];
    self->elems[next_head_pos] = NULL;
    *result = elem;

    return knd_OK;
}

int knd_queue_new(struct kndQueue **queue,
                  size_t capacity)
{
    struct kndQueue *self;
    self = malloc(sizeof(struct kndQueue));
    if (!self) return knd_NOMEM;

    memset(self, 0, sizeof(struct kndQueue));

    if (capacity) {
        self->elems = calloc(capacity, sizeof(void*));
        if (!self->elems) return knd_NOMEM;
        self->capacity = capacity;
    }

    *queue = self;
    return knd_OK;
}
<commit_msg>change memory ordering in kndQueue<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdatomic.h>

#include "knd_queue.h"
#include "knd_config.h"
#include "knd_utils.h"

int knd_queue_push(struct kndQueue *self,
                   void *elem)
{
    size_t head_pos, tail_pos, next_tail_pos;

    do {
        tail_pos = atomic_load_explicit(&self->tail_pos,
                                        memory_order_acquire);
        head_pos = atomic_load_explicit(&self->head_pos,
                                        memory_order_acquire);
        next_tail_pos = tail_pos + 1;

        if (next_tail_pos == head_pos) {
            knd_log("-- queue is full right now:  tail_pos:%zu head_pos:%zu ",
                    tail_pos, head_pos);
            return knd_LIMIT;
        }

        //knd_log(".. try pushing: tail pos:%zu  head pos:%zu",
        //        tail_pos, head_pos);

        if (next_tail_pos == self->capacity) {
            next_tail_pos = 0;
            if (head_pos == 0) {
                return knd_LIMIT;
            }
        }
    }
    while (!atomic_compare_exchange_weak(&self->tail_pos, &tail_pos, next_tail_pos));

    if (self->elems[next_tail_pos]) {
        knd_log("-- anomaly: next tail pos %zu is not empty?", next_tail_pos);
        return knd_FAIL;
    }
    self->elems[next_tail_pos] = elem;

    return knd_OK;
}

int knd_queue_pop(struct kndQueue *self,
                  void **result)
{
    void *elem;
    size_t head_pos, tail_pos, next_head_pos;

    do {
        tail_pos = atomic_load_explicit(&self->tail_pos,
                                        memory_order_release);
        head_pos = atomic_load_explicit(&self->head_pos,
                                        memory_order_release);
        if (tail_pos == head_pos) {
            //knd_log("-- queue is empty at head pos %zu", head_pos);
            return knd_NO_MATCH;
        }
        next_head_pos = head_pos + 1;

        if (next_head_pos == self->capacity) {
            next_head_pos = 0;
        }
        //knd_log("== try to pop: tail pos:%zu  next head pos:%zu",
        //        tail_pos, next_head_pos);
    } while (!atomic_compare_exchange_weak(&self->head_pos, &head_pos, next_head_pos));

    if (!self->elems[next_head_pos]) {
        knd_log("-- pos %zu is empty?", head_pos);
        return knd_FAIL;
    }

    elem = self->elems[next_head_pos];
    self->elems[next_head_pos] = NULL;
    *result = elem;

    return knd_OK;
}

int knd_queue_new(struct kndQueue **queue,
                  size_t capacity)
{
    struct kndQueue *self;
    self = malloc(sizeof(struct kndQueue));
    if (!self) return knd_NOMEM;

    memset(self, 0, sizeof(struct kndQueue));

    if (capacity) {
        self->elems = calloc(capacity, sizeof(void*));
        if (!self->elems) return knd_NOMEM;
        self->capacity = capacity;
    }

    *queue = self;
    return knd_OK;
}

{'fraction_non_alphanumeric': 0.11599657827202738, 'fraction_numerical': 0.002053036783575706, 'mean_word_length': 1.9690198070086338, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 43, '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': '4576857', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1912, 'n_words': 402}