DPDK  26.07.0
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 
13 #include <rte_stdatomic.h>
14 
26 static __rte_always_inline void
27 __rte_ring_hts_update_tail(struct rte_ring_hts_headtail *ht, uint32_t old_tail,
28  uint32_t num)
29 {
30  uint32_t tail;
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. This release ensures that all updates to *ht
37  * and the ring array made by this thread become visible to the opposing
38  * thread once the tail value written here is observed.
39  */
40  rte_atomic_store_explicit(&ht->ht.pos.tail, tail, rte_memory_order_release);
41 }
42 
54 static __rte_always_inline union __rte_ring_hts_pos
55 __rte_ring_hts_head_wait(const struct rte_ring_hts_headtail *ht,
56  rte_memory_order memorder)
57 {
58  union __rte_ring_hts_pos p;
59  p.raw = rte_atomic_load_explicit(&ht->ht.raw, memorder);
60 
61  while (p.pos.head != p.pos.tail) {
62  rte_pause();
63  p.raw = rte_atomic_load_explicit(&ht->ht.raw, memorder);
64  }
65 
66  return p;
67 }
68 
93 static __rte_always_inline uint32_t
94 __rte_ring_hts_move_head(struct rte_ring_hts_headtail *d,
95  const struct rte_ring_headtail *s, uint32_t capacity, unsigned int num,
96  enum rte_ring_queue_behavior behavior, uint32_t *old_head,
97  uint32_t *entries)
98 {
99  uint32_t n, stail;
100  union __rte_ring_hts_pos np, op;
101 
102  do {
103  /* Reset n to the initial burst count */
104  n = num;
105 
106  /*
107  * wait for tail to be equal to head,
108  * make sure that we read prod head/tail *before*
109  * reading cons tail.
110  */
111  /*
112  * A0: Synchronizes with the CAS at R1.
113  * Establishes a happens-before relationship with a thread of the same
114  * type that released the ht.raw, ensuring this thread observes all of
115  * its memory effects needed to maintain a safe partial order.
116  */
117  op = __rte_ring_hts_head_wait(d, rte_memory_order_acquire);
118 
119  /*
120  * A1: Establish a synchronizes-with edge using a store-release at R0.
121  * This ensures that all memory effects from the preceding opposing
122  * thread are observed.
123  */
124  stail = rte_atomic_load_explicit(&s->tail, rte_memory_order_acquire);
125 
126  /*
127  * The subtraction is done between two unsigned 32bits value
128  * (the result is always modulo 32 bits even if we have
129  * *old_head > cons_tail). So 'entries' is always between 0
130  * and capacity (which is < size).
131  */
132  *entries = capacity + stail - op.pos.head;
133 
134  /* check that we have enough room in ring */
135  if (unlikely(n > *entries))
136  n = (behavior == RTE_RING_QUEUE_FIXED) ?
137  0 : *entries;
138 
139  if (n == 0)
140  break;
141 
142  np.pos.tail = op.pos.tail;
143  np.pos.head = op.pos.head + n;
144 
145  /*
146  * R1: Establishes a synchronizes-with edge with the load-acquire
147  * of ht.raw at A0. This makes sure that the store-release to the
148  * tail by this thread, if it was of the opposite type, becomes
149  * visible to another thread of the current type. That thread will
150  * then observe the updates in the same order, keeping a safe
151  * partial order.
152  */
153  } while (rte_atomic_compare_exchange_strong_explicit(&d->ht.raw,
154  (uint64_t *)(uintptr_t)&op.raw, np.raw,
155  rte_memory_order_release,
156  rte_memory_order_relaxed) == 0);
157 
158  *old_head = op.pos.head;
159  return n;
160 }
164 static __rte_always_inline unsigned int
165 __rte_ring_hts_move_prod_head(struct rte_ring *r, unsigned int num,
166  enum rte_ring_queue_behavior behavior, uint32_t *old_head,
167  uint32_t *free_entries)
168 {
169  return __rte_ring_hts_move_head(&r->hts_prod, &r->cons,
170  r->capacity, num, behavior, old_head, free_entries);
171 }
172 
176 static __rte_always_inline unsigned int
177 __rte_ring_hts_move_cons_head(struct rte_ring *r, unsigned int num,
178  enum rte_ring_queue_behavior behavior, uint32_t *old_head,
179  uint32_t *entries)
180 {
181  return __rte_ring_hts_move_head(&r->hts_cons, &r->prod,
182  0, num, behavior, old_head, entries);
183 }
184 
207 static __rte_always_inline unsigned int
208 __rte_ring_do_hts_enqueue_elem(struct rte_ring *r, const void *obj_table,
209  uint32_t esize, uint32_t n, enum rte_ring_queue_behavior behavior,
210  uint32_t *free_space)
211 {
212  uint32_t free, head;
213 
214  n = __rte_ring_hts_move_prod_head(r, n, behavior, &head, &free);
215 
216  if (n != 0) {
217  __rte_ring_enqueue_elems(r, head, obj_table, esize, n);
218  __rte_ring_hts_update_tail(&r->hts_prod, head, n);
219  }
220 
221  if (free_space != NULL)
222  *free_space = free - n;
223  return n;
224 }
225 
248 static __rte_always_inline unsigned int
249 __rte_ring_do_hts_dequeue_elem(struct rte_ring *r, void *obj_table,
250  uint32_t esize, uint32_t n, enum rte_ring_queue_behavior behavior,
251  uint32_t *available)
252 {
253  uint32_t entries, head;
254 
255  n = __rte_ring_hts_move_cons_head(r, n, behavior, &head, &entries);
256 
257  if (n != 0) {
258  __rte_ring_dequeue_elems(r, head, obj_table, esize, n);
259  __rte_ring_hts_update_tail(&r->hts_cons, head, n);
260  }
261 
262  if (available != NULL)
263  *available = entries - n;
264  return n;
265 }
266 
267 #endif /* _RTE_RING_HTS_ELEM_PVT_H_ */
#define __rte_always_inline
Definition: rte_common.h:506
rte_ring_queue_behavior
Definition: rte_ring_core.h:40
#define unlikely(x)
static void rte_pause(void)
uint32_t capacity