1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2010-2017 Intel Corporation
4 * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org
6 * Derived from FreeBSD's bufring.h
7 * Used as BSD-3 Licensed with permission from Kip Macy.
10 #ifndef _RTE_RING_GENERIC_H_
11 #define _RTE_RING_GENERIC_H_
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
)
22 * If there are other enqueues/dequeues in progress that preceded us,
23 * we need to wait for them to complete
26 while (unlikely(ht
->tail
!= old_val
))
33 * @internal This function updates the producer head for enqueue
36 * A pointer to the ring structure
38 * Indicates whether multi-producer path is needed or not
40 * The number of elements we will want to enqueue, i.e. how far should the
43 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
44 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items as possible from ring
46 * Returns head value as it was before the move, i.e. where enqueue starts
48 * Returns the current/new head value i.e. where enqueue finishes
50 * Returns the amount of free space in the ring BEFORE head was moved
52 * Actual number of objects enqueued.
53 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
55 static __rte_always_inline
unsigned int
56 __rte_ring_move_prod_head(struct rte_ring
*r
, unsigned 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
)
61 const uint32_t capacity
= r
->capacity
;
66 /* Reset n to the initial burst count */
69 *old_head
= r
->prod
.head
;
71 /* add rmb barrier to avoid load/load reorder in weak
72 * memory model. It is noop on x86
77 * The subtraction is done between two unsigned 32bits value
78 * (the result is always modulo 32 bits even if we have
79 * *old_head > cons_tail). So 'free_entries' is always between 0
80 * and capacity (which is < size).
82 *free_entries
= (capacity
+ r
->cons
.tail
- *old_head
);
84 /* check that we have enough room in ring */
85 if (unlikely(n
> *free_entries
))
86 n
= (behavior
== RTE_RING_QUEUE_FIXED
) ?
92 *new_head
= *old_head
+ n
;
94 r
->prod
.head
= *new_head
, success
= 1;
96 success
= rte_atomic32_cmpset(&r
->prod
.head
,
97 *old_head
, *new_head
);
98 } while (unlikely(success
== 0));
103 * @internal This function updates the consumer head for dequeue
106 * A pointer to the ring structure
108 * Indicates whether multi-consumer path is needed or not
110 * The number of elements we will want to enqueue, i.e. how far should the
113 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
114 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items as possible from ring
116 * Returns head value as it was before the move, i.e. where dequeue starts
118 * Returns the current/new head value i.e. where dequeue finishes
120 * Returns the number of entries in the ring BEFORE head was moved
122 * - Actual number of objects dequeued.
123 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
125 static __rte_always_inline
unsigned int
126 __rte_ring_move_cons_head(struct rte_ring
*r
, unsigned int is_sc
,
127 unsigned int n
, enum rte_ring_queue_behavior behavior
,
128 uint32_t *old_head
, uint32_t *new_head
,
131 unsigned int max
= n
;
134 /* move cons.head atomically */
136 /* Restore n as it may change every loop */
139 *old_head
= r
->cons
.head
;
141 /* add rmb barrier to avoid load/load reorder in weak
142 * memory model. It is noop on x86
146 /* The subtraction is done between two unsigned 32bits value
147 * (the result is always modulo 32 bits even if we have
148 * cons_head > prod_tail). So 'entries' is always between 0
151 *entries
= (r
->prod
.tail
- *old_head
);
153 /* Set the actual entries for dequeue */
155 n
= (behavior
== RTE_RING_QUEUE_FIXED
) ? 0 : *entries
;
157 if (unlikely(n
== 0))
160 *new_head
= *old_head
+ n
;
162 r
->cons
.head
= *new_head
, success
= 1;
164 success
= rte_atomic32_cmpset(&r
->cons
.head
, *old_head
,
166 } while (unlikely(success
== 0));
170 #endif /* _RTE_RING_GENERIC_H_ */