summaryrefslogtreecommitdiff
path: root/src/util/queue.h
blob: 08b0a308920ae6979266eb282a220769f9571413 (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
#pragma once

#include <al/array.h>
#include <nnwt/thread.h>

#define queue(type)             \
    struct {                    \
        array(type) a;          \
        struct nn_mutex mutex;  \
    }

#define camu_queue_lock(q)          \
    do {                            \
        nn_mutex_lock(&(q).mutex);  \
    } while (0)

#define camu_queue_unlock(q)          \
    do {                              \
        nn_mutex_unlock(&(q).mutex);  \
    } while (0)

#define camu_queue_init(q)          \
    do {                            \
        al_array_init((q).a);       \
        nn_mutex_init(&(q).mutex);  \
    } while (0)

#define camu_queue_push(q, item)      \
    do {                              \
        al_array_push((q).a, item);   \
    } while (0)

#define camu_queue_try_pop(q, s, r)        \
    do {                                   \
        nn_mutex_lock(&(q).mutex);         \
        if ((s = (q).a.count) > 0) {       \
            al_array_pop_at((q).a, 0, r);  \
        }                                  \
        nn_mutex_unlock(&(q).mutex);       \
    } while (0)

#define camu_queue_free(q)             \
    do {                               \
        nn_mutex_destroy(&(q).mutex);  \
        al_array_free((q).a);          \
    } while (0)