<commit_before>#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <strings.h>

#include "test_util.h"
#include "basic_event_loop.h"

int debug = 0;

#define DPRINTF(x, format, ...) { \
    if (x <= debug) { \
        printf("%s: " format "\n" , __FUNCTION__, ## __VA_ARGS__); \
    } \
}

/* From ring.h */
typedef struct Ring RingItem;
typedef struct Ring {
    RingItem *prev;
    RingItem *next;
} Ring;

static inline void ring_init(Ring *ring)
{
    ring->next = ring->prev = ring;
}

static inline void ring_item_init(RingItem *item)
{
    item->next = item->prev = NULL;
}

static inline int ring_item_is_linked(RingItem *item)
{
    return !!item->next;
}

static inline int ring_is_empty(Ring *ring)
{
    ASSERT(ring->next != NULL && ring->prev != NULL);
    return ring == ring->next;
}

static inline void ring_add(Ring *ring, RingItem *item)
{
    ASSERT(ring->next != NULL && ring->prev != NULL);
    ASSERT(item->next == NULL && item->prev == NULL);

    item->next = ring->next;
    item->prev = ring;
    ring->next = item->next->prev = item;
}

static inline void __ring_remove(RingItem *item)
{
    item->next->prev = item->prev;
    item->prev->next = item->next;
    item->prev = item->next = 0;
}

static inline void ring_remove(RingItem *item)
{
    ASSERT(item->next != NULL && item->prev != NULL);
    ASSERT(item->next != item);

    __ring_remove(item);
}

static inline RingItem *ring_get_head(Ring *ring)
{
    RingItem *ret;

    ASSERT(ring->next != NULL && ring->prev != NULL);

    if (ring_is_empty(ring)) {
        return NULL;
    }
    ret = ring->next;
    return ret;
}

static inline RingItem *ring_get_tail(Ring *ring)
{
    RingItem *ret;

    ASSERT(ring->next != NULL && ring->prev != NULL);

    if (ring_is_empty(ring)) {
        return NULL;
    }
    ret = ring->prev;
    return ret;
}

static inline RingItem *ring_next(Ring *ring, RingItem *pos)
{
    RingItem *ret;

    ASSERT(ring->next != NULL && ring->prev != NULL);
    ASSERT(pos);
    ASSERT(pos->next != NULL && pos->prev != NULL);
    ret = pos->next;
    return (ret == ring) ? NULL : ret;
}

static inline RingItem *ring_prev(Ring *ring, RingItem *pos)
{
    RingItem *ret;

    ASSERT(ring->next != NULL && ring->prev != NULL);
    ASSERT(pos);
    ASSERT(pos->next != NULL && pos->prev != NULL);
    ret = pos->prev;
    return (ret == ring) ? NULL : ret;
}

#define RING_FOREACH_SAFE(var, next, ring)                    \
    for ((var) = ring_get_head(ring),                         \
         (next) = (var) ? ring_next(ring, (var)) : NULL;      \
            (var);                                            \
            (var) = (next),                                   \
            (next) = (var) ? ring_next(ring, (var)) : NULL)

/**/

#define NOT_IMPLEMENTED printf("%s not implemented\n", __func__);

static SpiceCoreInterface core;

typedef struct SpiceTimer {
    RingItem link;
    SpiceTimerFunc func;
    struct timeval tv_start;
    int ms;
    void *opaque;
} Timer;

Ring timers;

static SpiceTimer* timer_add(SpiceTimerFunc func, void *opaque)
{
    SpiceTimer *timer = calloc(sizeof(SpiceTimer), 1);

    timer->func = func;
    timer->opaque = opaque;
    ring_add(&timers, &timer->link);
    return timer;
}

static void add_ms_to_timeval(struct timeval *tv, int ms)
{
    tv->tv_usec += 1000 * ms;
    while (tv->tv_usec >= 1000000) {
        tv->tv_sec++;
        tv->tv_usec -= 1000000;
    }
}

static void timer_start(SpiceTimer *timer, uint32_t ms)
{
    gettimeofday(&timer->tv_start, NULL);
    timer->ms = ms;
    // already add ms to timer value
    add_ms_to_timeval(&timer->tv_start, ms);
    ASSERT(timer->ms);
}

static void timer_cancel(SpiceTimer *timer)
{
    timer->ms = 0;
}

static void timer_remove(SpiceTimer *timer)
{
    ring_remove(&timer->link);<commit_msg>server/tests/basic_event_loop: strange assert failure; fix<commit_after>#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <strings.h>

#include "test_util.h"
#include "basic_event_loop.h"

int debug = 0;

#define DPRINTF(x, format, ...) { \
    if (x <= debug) { \
        printf("%s: " format "\n" , __FUNCTION__, ## __VA_ARGS__); \
    } \
}

/* From ring.h */
typedef struct Ring RingItem;
typedef struct Ring {
    RingItem *prev;
    RingItem *next;
} Ring;

static inline void ring_init(Ring *ring)
{
    ring->next = ring->prev = ring;
}

static inline void ring_item_init(RingItem *item)
{
    item->next = item->prev = NULL;
}

static inline int ring_item_is_linked(RingItem *item)
{
    return !!item->next;
}

static inline int ring_is_empty(Ring *ring)
{
    ASSERT(ring->next != NULL && ring->prev != NULL);
    return ring == ring->next;
}

static inline void ring_add(Ring *ring, RingItem *item)
{
    ASSERT(ring->next != NULL && ring->prev != NULL);
    ASSERT(item->next == NULL && item->prev == NULL);

    item->next = ring->next;
    item->prev = ring;
    ring->next = item->next->prev = item;
}

static inline void __ring_remove(RingItem *item)
{
    item->next->prev = item->prev;
    item->prev->next = item->next;
    item->prev = item->next = 0;
}

static inline void ring_remove(RingItem *item)
{
    ASSERT(item->next != NULL && item->prev != NULL);
    ASSERT(item->next != item);

    __ring_remove(item);
}

static inline RingItem *ring_get_head(Ring *ring)
{
    RingItem *ret;

    ASSERT(ring->next != NULL && ring->prev != NULL);

    if (ring_is_empty(ring)) {
        return NULL;
    }
    ret = ring->next;
    return ret;
}

static inline RingItem *ring_get_tail(Ring *ring)
{
    RingItem *ret;

    ASSERT(ring->next != NULL && ring->prev != NULL);

    if (ring_is_empty(ring)) {
        return NULL;
    }
    ret = ring->prev;
    return ret;
}

static inline RingItem *ring_next(Ring *ring, RingItem *pos)
{
    RingItem *ret;

    ASSERT(ring->next != NULL && ring->prev != NULL);
    ASSERT(pos);
    ASSERT(pos->next != NULL && pos->prev != NULL);
    ret = pos->next;
    return (ret == ring) ? NULL : ret;
}

static inline RingItem *ring_prev(Ring *ring, RingItem *pos)
{
    RingItem *ret;

    ASSERT(ring->next != NULL && ring->prev != NULL);
    ASSERT(pos);
    ASSERT(pos->next != NULL && pos->prev != NULL);
    ret = pos->prev;
    return (ret == ring) ? NULL : ret;
}

#define RING_FOREACH_SAFE(var, next, ring)                    \
    for ((var) = ring_get_head(ring),                         \
         (next) = (var) ? ring_next(ring, (var)) : NULL;      \
            (var);                                            \
            (var) = (next),                                   \
            (next) = (var) ? ring_next(ring, (var)) : NULL)

/**/

#define NOT_IMPLEMENTED printf("%s not implemented\n", __func__);

static SpiceCoreInterface core;

typedef struct SpiceTimer {
    RingItem link;
    SpiceTimerFunc func;
    struct timeval tv_start;
    int ms;
    void *opaque;
} Timer;

Ring timers;

static SpiceTimer* timer_add(SpiceTimerFunc func, void *opaque)
{
    SpiceTimer *timer = calloc(sizeof(SpiceTimer), 1);

    timer->func = func;
    timer->opaque = opaque;
    ring_add(&timers, &timer->link);
    return timer;
}

static void add_ms_to_timeval(struct timeval *tv, int ms)
{
    tv->tv_usec += 1000 * ms;
    while (tv->tv_usec >= 1000000) {
        tv->tv_sec++;
        tv->tv_usec -= 1000000;
    }
}

static void timer_start(SpiceTimer *timer, uint32_t ms)
{
    ASSERT(ms);
    gettimeofday(&timer->tv_start, NULL);
    timer->ms = ms;
    // already add ms to timer value
    add_ms_to_timeval(&timer->tv_start, ms);
}

static void timer_cancel(SpiceTimer *timer)
{
    timer->ms = 0;
}

static void timer_remove(SpiceTimer *timer)
{
    ring_remove(&timer->link);
{'fraction_non_alphanumeric': 0.15466321243523315, 'fraction_numerical': 0.0062176165803108805, 'mean_word_length': 2.68368320610687, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 128, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4205027', 'n_tokens_mistral': 2741, 'n_tokens_neox': 2630, 'n_words': 641}