1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2017,2018 HXT-semitech Corporation.
4 * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org
5 * Copyright (c) 2021 Arm Limited
7 * Derived from FreeBSD's bufring.h
8 * Used as BSD-3 Licensed with permission from Kip Macy.
11 #ifndef _RTE_RING_C11_PVT_H_
12 #define _RTE_RING_C11_PVT_H_
14 static __rte_always_inline void
15 __rte_ring_update_tail(struct rte_ring_headtail *ht, uint32_t old_val,
16 uint32_t new_val, uint32_t single, uint32_t enqueue)
18 RTE_SET_USED(enqueue);
21 * If there are other enqueues/dequeues in progress that preceded us,
22 * we need to wait for them to complete
25 rte_wait_until_equal_32(&ht->tail, old_val, __ATOMIC_RELAXED);
27 __atomic_store_n(&ht->tail, new_val, __ATOMIC_RELEASE);
31 * @internal This function updates the producer head for enqueue
34 * A pointer to the ring structure
36 * Indicates whether multi-producer path is needed or not
38 * The number of elements we will want to enqueue, i.e. how far should the
41 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
42 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items as possible from ring
44 * Returns head value as it was before the move, i.e. where enqueue starts
46 * Returns the current/new head value i.e. where enqueue finishes
48 * Returns the amount of free space in the ring BEFORE head was moved
50 * Actual number of objects enqueued.
51 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
53 static __rte_always_inline unsigned int
54 __rte_ring_move_prod_head(struct rte_ring *r, unsigned int is_sp,
55 unsigned int n, enum rte_ring_queue_behavior behavior,
56 uint32_t *old_head, uint32_t *new_head,
57 uint32_t *free_entries)
59 const uint32_t capacity = r->capacity;
64 *old_head = __atomic_load_n(&r->prod.head, __ATOMIC_RELAXED);
66 /* Reset n to the initial burst count */
69 /* Ensure the head is read before tail */
70 __atomic_thread_fence(__ATOMIC_ACQUIRE);
72 /* load-acquire synchronize with store-release of ht->tail
75 cons_tail = __atomic_load_n(&r->cons.tail,
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).
83 *free_entries = (capacity + cons_tail - *old_head);
85 /* check that we have enough room in ring */
86 if (unlikely(n > *free_entries))
87 n = (behavior == RTE_RING_QUEUE_FIXED) ?
93 *new_head = *old_head + n;
95 r->prod.head = *new_head, success = 1;
97 /* on failure, *old_head is updated */
98 success = __atomic_compare_exchange_n(&r->prod.head,
102 } while (unlikely(success == 0));
107 * @internal This function updates the consumer head for dequeue
110 * A pointer to the ring structure
112 * Indicates whether multi-consumer path is needed or not
114 * The number of elements we will want to dequeue, i.e. how far should the
117 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
118 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items as possible from ring
120 * Returns head value as it was before the move, i.e. where dequeue starts
122 * Returns the current/new head value i.e. where dequeue finishes
124 * Returns the number of entries in the ring BEFORE head was moved
126 * - Actual number of objects dequeued.
127 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
129 static __rte_always_inline unsigned int
130 __rte_ring_move_cons_head(struct rte_ring *r, int is_sc,
131 unsigned int n, enum rte_ring_queue_behavior behavior,
132 uint32_t *old_head, uint32_t *new_head,
135 unsigned int max = n;
139 /* move cons.head atomically */
140 *old_head = __atomic_load_n(&r->cons.head, __ATOMIC_RELAXED);
142 /* Restore n as it may change every loop */
145 /* Ensure the head is read before tail */
146 __atomic_thread_fence(__ATOMIC_ACQUIRE);
148 /* this load-acquire synchronize with store-release of ht->tail
151 prod_tail = __atomic_load_n(&r->prod.tail,
154 /* The subtraction is done between two unsigned 32bits value
155 * (the result is always modulo 32 bits even if we have
156 * cons_head > prod_tail). So 'entries' is always between 0
159 *entries = (prod_tail - *old_head);
161 /* Set the actual entries for dequeue */
163 n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : *entries;
165 if (unlikely(n == 0))
168 *new_head = *old_head + n;
170 r->cons.head = *new_head, success = 1;
172 /* on failure, *old_head will be updated */
173 success = __atomic_compare_exchange_n(&r->cons.head,
177 } while (unlikely(success == 0));
181 #endif /* _RTE_RING_C11_PVT_H_ */