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_PEEK_ELEM_PVT_H_
11 #define _RTE_RING_PEEK_ELEM_PVT_H_
14 * @file rte_ring_peek_elem_pvt.h
15 * It is not recommended to include this file directly,
16 * include <rte_ring.h> instead.
17 * Contains internal helper functions for rte_ring peek API.
18 * For more information please refer to <rte_ring_peek.h>.
22 * @internal get current tail value.
23 * This function should be used only for single thread producer/consumer.
24 * Check that user didn't request to move tail above the head.
26 * - return zero, that will cause abort any pending changes and
27 * return head to its previous position.
28 * - throw an assert in debug mode.
30 static __rte_always_inline uint32_t
31 __rte_ring_st_get_tail(struct rte_ring_headtail *ht, uint32_t *tail,
41 num = (n >= num) ? num : 0;
48 * @internal set new values for head and tail.
49 * This function should be used only for single thread producer/consumer.
50 * Should be used only in conjunction with __rte_ring_st_get_tail.
52 static __rte_always_inline void
53 __rte_ring_st_set_head_tail(struct rte_ring_headtail *ht, uint32_t tail,
54 uint32_t num, uint32_t enqueue)
58 RTE_SET_USED(enqueue);
62 __atomic_store_n(&ht->tail, pos, __ATOMIC_RELEASE);
66 * @internal get current tail value.
67 * This function should be used only for producer/consumer in MT_HTS mode.
68 * Check that user didn't request to move tail above the head.
70 * - return zero, that will cause abort any pending changes and
71 * return head to its previous position.
72 * - throw an assert in debug mode.
74 static __rte_always_inline uint32_t
75 __rte_ring_hts_get_tail(struct rte_ring_hts_headtail *ht, uint32_t *tail,
79 union __rte_ring_hts_pos p;
81 p.raw = __atomic_load_n(&ht->ht.raw, __ATOMIC_RELAXED);
82 n = p.pos.head - p.pos.tail;
85 num = (n >= num) ? num : 0;
92 * @internal set new values for head and tail as one atomic 64 bit operation.
93 * This function should be used only for producer/consumer in MT_HTS mode.
94 * Should be used only in conjunction with __rte_ring_hts_get_tail.
96 static __rte_always_inline void
97 __rte_ring_hts_set_head_tail(struct rte_ring_hts_headtail *ht, uint32_t tail,
98 uint32_t num, uint32_t enqueue)
100 union __rte_ring_hts_pos p;
102 RTE_SET_USED(enqueue);
104 p.pos.head = tail + num;
105 p.pos.tail = p.pos.head;
107 __atomic_store_n(&ht->ht.raw, p.raw, __ATOMIC_RELEASE);
111 * @internal This function moves prod head value.
113 static __rte_always_inline unsigned int
114 __rte_ring_do_enqueue_start(struct rte_ring *r, uint32_t n,
115 enum rte_ring_queue_behavior behavior, uint32_t *free_space)
117 uint32_t free, head, next;
119 switch (r->prod.sync_type) {
120 case RTE_RING_SYNC_ST:
121 n = __rte_ring_move_prod_head(r, RTE_RING_SYNC_ST, n,
122 behavior, &head, &next, &free);
124 case RTE_RING_SYNC_MT_HTS:
125 n = __rte_ring_hts_move_prod_head(r, n, behavior,
128 case RTE_RING_SYNC_MT:
129 case RTE_RING_SYNC_MT_RTS:
131 /* unsupported mode, shouldn't be here */
137 if (free_space != NULL)
138 *free_space = free - n;
143 * @internal This function moves cons head value and copies up to *n*
144 * objects from the ring to the user provided obj_table.
146 static __rte_always_inline unsigned int
147 __rte_ring_do_dequeue_start(struct rte_ring *r, void *obj_table,
148 uint32_t esize, uint32_t n, enum rte_ring_queue_behavior behavior,
151 uint32_t avail, head, next;
153 switch (r->cons.sync_type) {
154 case RTE_RING_SYNC_ST:
155 n = __rte_ring_move_cons_head(r, RTE_RING_SYNC_ST, n,
156 behavior, &head, &next, &avail);
158 case RTE_RING_SYNC_MT_HTS:
159 n = __rte_ring_hts_move_cons_head(r, n, behavior,
162 case RTE_RING_SYNC_MT:
163 case RTE_RING_SYNC_MT_RTS:
165 /* unsupported mode, shouldn't be here */
172 __rte_ring_dequeue_elems(r, head, obj_table, esize, n);
174 if (available != NULL)
175 *available = avail - n;
179 #endif /* _RTE_RING_PEEK_ELEM_PVT_H_ */