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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/*
* Copyright (C) NGINX, Inc.
*/
#ifndef _NXT_NVBCQ_H_INCLUDED_
#define _NXT_NVBCQ_H_INCLUDED_
/* Numeric VBart Circular Queue */
#define NXT_NVBCQ_SIZE 16384
typedef uint32_t nxt_nvbcq_atomic_t;
struct nxt_nvbcq_s {
nxt_nvbcq_atomic_t head;
nxt_nvbcq_atomic_t entries[NXT_NVBCQ_SIZE];
nxt_nvbcq_atomic_t tail;
};
typedef struct nxt_nvbcq_s nxt_nvbcq_t;
static inline nxt_nvbcq_atomic_t
nxt_nvbcq_head(nxt_nvbcq_t const volatile *q)
{
return q->head;
}
static inline nxt_nvbcq_atomic_t
nxt_nvbcq_tail(nxt_nvbcq_t const volatile *q)
{
return q->tail;
}
static inline void
nxt_nvbcq_tail_cmp_inc(nxt_nvbcq_t volatile *q, nxt_nvbcq_atomic_t t)
{
nxt_atomic_cmp_set(&q->tail, t, t + 1);
}
static inline nxt_nvbcq_atomic_t
nxt_nvbcq_index(nxt_nvbcq_t const volatile *q, nxt_nvbcq_atomic_t i)
{
return i % NXT_NVBCQ_SIZE;
}
static inline nxt_nvbcq_atomic_t
nxt_nvbcq_map(nxt_nvbcq_t const volatile *q, nxt_nvbcq_atomic_t i)
{
return i % NXT_NVBCQ_SIZE;
}
static inline nxt_nvbcq_atomic_t
nxt_nvbcq_empty(nxt_nvbcq_t const volatile *q)
{
return NXT_NVBCQ_SIZE;
}
static void
nxt_nvbcq_init(nxt_nvbcq_t volatile *q)
{
nxt_nvbcq_atomic_t i;
q->head = 0;
for (i = 0; i < NXT_NVBCQ_SIZE; i++) {
q->entries[i] = NXT_NVBCQ_SIZE;
}
q->tail = NXT_NVBCQ_SIZE;
}
static void
nxt_nvbcq_enqueue(nxt_nvbcq_t volatile *q, nxt_nvbcq_atomic_t val)
{
nxt_nvbcq_atomic_t t, h, i;
t = nxt_nvbcq_tail(q);
h = t - NXT_NVBCQ_SIZE;
for ( ;; ) {
i = nxt_nvbcq_map(q, t);
if (q->entries[i] == NXT_NVBCQ_SIZE
&& nxt_atomic_cmp_set(&q->entries[i], NXT_NVBCQ_SIZE, val))
{
nxt_nvbcq_tail_cmp_inc(q, t);
return;
}
if ((t - h) == NXT_NVBCQ_SIZE) {
h = nxt_nvbcq_head(q);
if ((t - h) == NXT_NVBCQ_SIZE) {
return;
}
}
t++;
}
}
static nxt_nvbcq_atomic_t
nxt_nvbcq_dequeue(nxt_nvbcq_t volatile *q)
{
nxt_nvbcq_atomic_t h, t, i, e;
h = nxt_nvbcq_head(q);
t = h + NXT_NVBCQ_SIZE;
for ( ;; ) {
i = nxt_nvbcq_map(q, h);
e = q->entries[i];
if (e < NXT_NVBCQ_SIZE
&& nxt_atomic_cmp_set(&q->entries[i], e, NXT_NVBCQ_SIZE))
{
nxt_atomic_cmp_set(&q->head, h, h + 1);
return e;
}
if ((t - h) == NXT_NVBCQ_SIZE) {
t = nxt_nvbcq_tail(q);
if ((t - h) == NXT_NVBCQ_SIZE) {
return NXT_NVBCQ_SIZE;
}
}
h++;
}
}
#endif /* _NXT_NVBCQ_H_INCLUDED_ */
|