summaryrefslogtreecommitdiffstats
path: root/src/test-asyncq.c
blob: 26dbc559079d72b2f356ea5b88c40901169cdc27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdlib.h>

#include "asyncq.h"
#include "thread.h"
#include "macro.h"

#define ITERATIONS_MAX 1000

static void thread(void *userdata) {
    sa_asyncq_t *q = userdata;
    int i;
    
    for (i = 0; i < ITERATIONS_MAX; i++) {
        sa_asyncq_item_t *i;

        i = sa_asyncq_get(q);
        sa_assert(i);

        sa_asyncq_push(q, i);

        if (rand() & 1)
            sa_thread_yield();
    }
}

int main(int argc, char *argv[]) {
    int j;
    sa_thread_t *t;
    sa_asyncq_t q;
    sa_asyncq_item_t *i;

    sa_assert_success(sa_asyncq_init(&q));
    
    t = sa_thread_new(thread, &q);
    sa_assert(t);

    for (j = 0; j < ITERATIONS_MAX; j++) {
        
        do {
            i = sa_asyncq_pop(&q, 0);
            printf("%s ", i ? "gotcha" : "miss");

            if (i)
                sa_asyncq_recycle(&q, i);
        } while (i);

        if (rand() & 1)
            sa_thread_yield();
    }

    printf("\n");
    
    sa_thread_free(t);

    while (sa_asyncq_pop(&q, 1))
        ;

    sa_asyncq_done(&q);
    

    return 0;
}