1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2010-2020 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_HTS_C11_MEM_H_
11 #define _RTE_RING_HTS_C11_MEM_H_
14 * @file rte_ring_hts_c11_mem.h
15 * It is not recommended to include this file directly,
16 * include <rte_ring.h> instead.
17 * Contains internal helper functions for head/tail sync (HTS) ring mode.
18 * For more information please refer to <rte_ring_hts.h>.
22 * @internal update tail with new value.
24 static __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)
30 RTE_SET_USED(enqueue);
32 tail = old_tail + num;
33 __atomic_store_n(&ht->ht.pos.tail, tail, __ATOMIC_RELEASE);
37 * @internal waits till tail will become equal to head.
38 * Means no writer/reader is active for that ring.
39 * Suppose to work as serialization point.
41 static __rte_always_inline void
42 __rte_ring_hts_head_wait(const struct rte_ring_hts_headtail *ht,
43 union __rte_ring_hts_pos *p)
45 while (p->pos.head != p->pos.tail) {
47 p->raw = __atomic_load_n(&ht->ht.raw, __ATOMIC_ACQUIRE);
52 * @internal This function updates the producer head for enqueue
54 static __rte_always_inline unsigned int
55 __rte_ring_hts_move_prod_head(struct rte_ring *r, unsigned int num,
56 enum rte_ring_queue_behavior behavior, uint32_t *old_head,
57 uint32_t *free_entries)
60 union __rte_ring_hts_pos np, op;
62 const uint32_t capacity = r->capacity;
64 op.raw = __atomic_load_n(&r->hts_prod.ht.raw, __ATOMIC_ACQUIRE);
67 /* Reset n to the initial burst count */
71 * wait for tail to be equal to head,
72 * make sure that we read prod head/tail *before*
75 __rte_ring_hts_head_wait(&r->hts_prod, &op);
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 + r->cons.tail - op.pos.head;
85 /* check that we have enough room in ring */
86 if (unlikely(n > *free_entries))
87 n = (behavior == RTE_RING_QUEUE_FIXED) ?
93 np.pos.tail = op.pos.tail;
94 np.pos.head = op.pos.head + n;
97 * this CAS(ACQUIRE, ACQUIRE) serves as a hoist barrier to prevent:
98 * - OOO reads of cons tail value
99 * - OOO copy of elems from the ring
101 } while (__atomic_compare_exchange_n(&r->hts_prod.ht.raw,
103 0, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE) == 0);
105 *old_head = op.pos.head;
110 * @internal This function updates the consumer head for dequeue
112 static __rte_always_inline unsigned int
113 __rte_ring_hts_move_cons_head(struct rte_ring *r, unsigned int num,
114 enum rte_ring_queue_behavior behavior, uint32_t *old_head,
118 union __rte_ring_hts_pos np, op;
120 op.raw = __atomic_load_n(&r->hts_cons.ht.raw, __ATOMIC_ACQUIRE);
122 /* move cons.head atomically */
124 /* Restore n as it may change every loop */
128 * wait for tail to be equal to head,
129 * make sure that we read cons head/tail *before*
132 __rte_ring_hts_head_wait(&r->hts_cons, &op);
134 /* The subtraction is done between two unsigned 32bits value
135 * (the result is always modulo 32 bits even if we have
136 * cons_head > prod_tail). So 'entries' is always between 0
139 *entries = r->prod.tail - op.pos.head;
141 /* Set the actual entries for dequeue */
143 n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : *entries;
145 if (unlikely(n == 0))
148 np.pos.tail = op.pos.tail;
149 np.pos.head = op.pos.head + n;
152 * this CAS(ACQUIRE, ACQUIRE) serves as a hoist barrier to prevent:
153 * - OOO reads of prod tail value
154 * - OOO copy of elems from the ring
156 } while (__atomic_compare_exchange_n(&r->hts_cons.ht.raw,
158 0, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE) == 0);
160 *old_head = op.pos.head;
164 #endif /* _RTE_RING_HTS_C11_MEM_H_ */