DPDK  18.02.0
rte_ring_generic.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (c) 2010-2017 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_GENERIC_H_
11 #define _RTE_RING_GENERIC_H_
12 
13 static __rte_always_inline void
14 update_tail(struct rte_ring_headtail *ht, uint32_t old_val, uint32_t new_val,
15  uint32_t single, uint32_t enqueue)
16 {
17  if (enqueue)
18  rte_smp_wmb();
19  else
20  rte_smp_rmb();
21  /*
22  * If there are other enqueues/dequeues in progress that preceded us,
23  * we need to wait for them to complete
24  */
25  if (!single)
26  while (unlikely(ht->tail != old_val))
27  rte_pause();
28 
29  ht->tail = new_val;
30 }
31 
55 static __rte_always_inline unsigned int
56 __rte_ring_move_prod_head(struct rte_ring *r, int is_sp,
57  unsigned int n, enum rte_ring_queue_behavior behavior,
58  uint32_t *old_head, uint32_t *new_head,
59  uint32_t *free_entries)
60 {
61  const uint32_t capacity = r->capacity;
62  unsigned int max = n;
63  int success;
64 
65  do {
66  /* Reset n to the initial burst count */
67  n = max;
68 
69  *old_head = r->prod.head;
70 
71  /* add rmb barrier to avoid load/load reorder in weak
72  * memory model. It is noop on x86
73  */
74  rte_smp_rmb();
75 
76  const uint32_t cons_tail = r->cons.tail;
77  /*
78  * The subtraction is done between two unsigned 32bits value
79  * (the result is always modulo 32 bits even if we have
80  * *old_head > cons_tail). So 'free_entries' is always between 0
81  * and capacity (which is < size).
82  */
83  *free_entries = (capacity + cons_tail - *old_head);
84 
85  /* check that we have enough room in ring */
86  if (unlikely(n > *free_entries))
87  n = (behavior == RTE_RING_QUEUE_FIXED) ?
88  0 : *free_entries;
89 
90  if (n == 0)
91  return 0;
92 
93  *new_head = *old_head + n;
94  if (is_sp)
95  r->prod.head = *new_head, success = 1;
96  else
97  success = rte_atomic32_cmpset(&r->prod.head,
98  *old_head, *new_head);
99  } while (unlikely(success == 0));
100  return n;
101 }
102 
126 static __rte_always_inline unsigned int
127 __rte_ring_move_cons_head(struct rte_ring *r, int is_sc,
128  unsigned int n, enum rte_ring_queue_behavior behavior,
129  uint32_t *old_head, uint32_t *new_head,
130  uint32_t *entries)
131 {
132  unsigned int max = n;
133  int success;
134 
135  /* move cons.head atomically */
136  do {
137  /* Restore n as it may change every loop */
138  n = max;
139 
140  *old_head = r->cons.head;
141 
142  /* add rmb barrier to avoid load/load reorder in weak
143  * memory model. It is noop on x86
144  */
145  rte_smp_rmb();
146 
147  const uint32_t prod_tail = r->prod.tail;
148  /* The subtraction is done between two unsigned 32bits value
149  * (the result is always modulo 32 bits even if we have
150  * cons_head > prod_tail). So 'entries' is always between 0
151  * and size(ring)-1.
152  */
153  *entries = (prod_tail - *old_head);
154 
155  /* Set the actual entries for dequeue */
156  if (n > *entries)
157  n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : *entries;
158 
159  if (unlikely(n == 0))
160  return 0;
161 
162  *new_head = *old_head + n;
163  if (is_sc)
164  r->cons.head = *new_head, success = 1;
165  else
166  success = rte_atomic32_cmpset(&r->cons.head, *old_head,
167  *new_head);
168  } while (unlikely(success == 0));
169  return n;
170 }
171 
172 #endif /* _RTE_RING_GENERIC_H_ */
static void rte_smp_rmb(void)
static int rte_atomic32_cmpset(volatile uint32_t *dst, uint32_t exp, uint32_t src)
#define __rte_always_inline
Definition: rte_common.h:110
#define unlikely(x)
static void rte_pause(void)
static void rte_smp_wmb(void)
uint32_t capacity
Definition: rte_ring.h:102