DPDK 22.11.11-rc1
rte_ring_hts_elem_pvt.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 *
3 * Copyright (c) 2010-2020 Intel Corporation
4 * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org
5 * All rights reserved.
6 * Derived from FreeBSD's bufring.h
7 * Used as BSD-3 Licensed with permission from Kip Macy.
8 */
9
10#ifndef _RTE_RING_HTS_ELEM_PVT_H_
11#define _RTE_RING_HTS_ELEM_PVT_H_
12
24static __rte_always_inline void
25__rte_ring_hts_update_tail(struct rte_ring_hts_headtail *ht, uint32_t old_tail,
26 uint32_t num, uint32_t enqueue)
27{
28 uint32_t tail;
29
30 RTE_SET_USED(enqueue);
31
32 tail = old_tail + num;
33
34 /*
35 * R0: Release the tail update. Establishes a synchronization edge with
36 * the load-acquire at A1/A3. This release ensures that all updates to
37 * *ht and the ring array made by this thread become visible to the
38 * opposing thread once the tail value written here is observed.
39 */
40 __atomic_store_n(&ht->ht.pos.tail, tail, __ATOMIC_RELEASE);
41}
42
54static __rte_always_inline union __rte_ring_hts_pos
55__rte_ring_hts_head_wait(const struct rte_ring_hts_headtail *ht,
56 int memorder)
57{
58 union __rte_ring_hts_pos p;
59 p.raw = __atomic_load_n(&ht->ht.raw, memorder);
60
61 while (p.pos.head != p.pos.tail) {
62 rte_pause();
63 p.raw = __atomic_load_n(&ht->ht.raw, memorder);
64 }
65
66 return p;
67}
68
72static __rte_always_inline unsigned int
73__rte_ring_hts_move_prod_head(struct rte_ring *r, unsigned int num,
74 enum rte_ring_queue_behavior behavior, uint32_t *old_head,
75 uint32_t *free_entries)
76{
77 uint32_t n, cons_tail;
78 union __rte_ring_hts_pos np, op;
79
80 const uint32_t capacity = r->capacity;
81
82 do {
83 /* Reset n to the initial burst count */
84 n = num;
85
86 /*
87 * wait for tail to be equal to head,
88 * make sure that we read prod head/tail *before*
89 * reading cons tail.
90 */
91 /*
92 * A0: Synchronizes with the CAS at R1.
93 * Establishes a happens-before relationship with a thread of the same
94 * type that released the ht.raw, ensuring this thread observes all of
95 * its memory effects needed to maintain a safe partial order.
96 */
97 op = __rte_ring_hts_head_wait(&r->hts_prod, __ATOMIC_ACQUIRE);
98
99 /*
100 * A1: Establish a synchronizes-with edge using a store-release at R0.
101 * This ensures that all memory effects from the preceding opposing
102 * thread are observed.
103 */
104 cons_tail = __atomic_load_n(&r->cons.tail, __ATOMIC_ACQUIRE);
105
106 /*
107 * The subtraction is done between two unsigned 32bits value
108 * (the result is always modulo 32 bits even if we have
109 * *old_head > cons_tail). So 'free_entries' is always between 0
110 * and capacity (which is < size).
111 */
112 *free_entries = capacity + cons_tail - op.pos.head;
113
114 /* check that we have enough room in ring */
115 if (unlikely(n > *free_entries))
116 n = (behavior == RTE_RING_QUEUE_FIXED) ?
117 0 : *free_entries;
118
119 if (n == 0)
120 break;
121
122 np.pos.tail = op.pos.tail;
123 np.pos.head = op.pos.head + n;
124
125 /*
126 * R1: Establishes a synchronizes-with edge with the load-acquire
127 * of ht.raw at A0. This makes sure that the store-release to the
128 * tail by this thread, if it was of the opposite type, becomes
129 * visible to another thread of the current type. That thread will
130 * then observe the updates in the same order, keeping a safe
131 * partial order.
132 */
133 } while (__atomic_compare_exchange_n(&r->hts_prod.ht.raw,
134 &op.raw, np.raw,
135 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED) == 0);
136
137 *old_head = op.pos.head;
138 return n;
139}
140
144static __rte_always_inline unsigned int
145__rte_ring_hts_move_cons_head(struct rte_ring *r, unsigned int num,
146 enum rte_ring_queue_behavior behavior, uint32_t *old_head,
147 uint32_t *entries)
148{
149 uint32_t n, prod_tail;
150 union __rte_ring_hts_pos np, op;
151
152 /* move cons.head atomically */
153 do {
154 /* Restore n as it may change every loop */
155 n = num;
156
157 /*
158 * wait for tail to be equal to head,
159 * make sure that we read cons head/tail *before*
160 * reading prod tail.
161 */
162 /*
163 * A2: Synchronizes with the CAS at R2.
164 * Establishes a happens-before relationship with a thread of the same
165 * type that released the ht.raw, ensuring this thread observes all of
166 * its memory effects needed to maintain a safe partial order.
167 */
168 op = __rte_ring_hts_head_wait(&r->hts_cons, __ATOMIC_ACQUIRE);
169
170 /*
171 * A3: Establish a synchronizes-with edge using a store-release at R0.
172 * This ensures that all memory effects from the preceding opposing
173 * thread are observed.
174 */
175 prod_tail = __atomic_load_n(&r->prod.tail, __ATOMIC_ACQUIRE);
176
177 /* The subtraction is done between two unsigned 32bits value
178 * (the result is always modulo 32 bits even if we have
179 * cons_head > prod_tail). So 'entries' is always between 0
180 * and size(ring)-1.
181 */
182 *entries = prod_tail - op.pos.head;
183
184 /* Set the actual entries for dequeue */
185 if (n > *entries)
186 n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : *entries;
187
188 if (unlikely(n == 0))
189 break;
190
191 np.pos.tail = op.pos.tail;
192 np.pos.head = op.pos.head + n;
193
194 /*
195 * R2: Establishes a synchronizes-with edge with the load-acquire
196 * of ht.raw at A2. This makes sure that the store-release to the
197 * tail by this thread, if it was of the opposite type, becomes
198 * visible to another thread of the current type. That thread will
199 * then observe the updates in the same order, keeping a safe
200 * partial order.
201 */
202 } while (__atomic_compare_exchange_n(&r->hts_cons.ht.raw,
203 &op.raw, np.raw,
204 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED) == 0);
205
206 *old_head = op.pos.head;
207 return n;
208}
209
232static __rte_always_inline unsigned int
233__rte_ring_do_hts_enqueue_elem(struct rte_ring *r, const void *obj_table,
234 uint32_t esize, uint32_t n, enum rte_ring_queue_behavior behavior,
235 uint32_t *free_space)
236{
237 uint32_t free, head;
238
239 n = __rte_ring_hts_move_prod_head(r, n, behavior, &head, &free);
240
241 if (n != 0) {
242 __rte_ring_enqueue_elems(r, head, obj_table, esize, n);
243 __rte_ring_hts_update_tail(&r->hts_prod, head, n, 1);
244 }
245
246 if (free_space != NULL)
247 *free_space = free - n;
248 return n;
249}
250
273static __rte_always_inline unsigned int
274__rte_ring_do_hts_dequeue_elem(struct rte_ring *r, void *obj_table,
275 uint32_t esize, uint32_t n, enum rte_ring_queue_behavior behavior,
276 uint32_t *available)
277{
278 uint32_t entries, head;
279
280 n = __rte_ring_hts_move_cons_head(r, n, behavior, &head, &entries);
281
282 if (n != 0) {
283 __rte_ring_dequeue_elems(r, head, obj_table, esize, n);
284 __rte_ring_hts_update_tail(&r->hts_cons, head, n, 0);
285 }
286
287 if (available != NULL)
288 *available = entries - n;
289 return n;
290}
291
292#endif /* _RTE_RING_HTS_ELEM_PVT_H_ */
#define unlikely(x)
#define RTE_SET_USED(x)
Definition: rte_common.h:135
#define __rte_always_inline
Definition: rte_common.h:255
static void rte_pause(void)
rte_ring_queue_behavior
Definition: rte_ring_core.h:43
@ RTE_RING_QUEUE_FIXED
Definition: rte_ring_core.h:45
volatile uint32_t tail
Definition: rte_ring_core.h:70
uint32_t capacity