xref: /dpdk/lib/stack/rte_stack_lf.h (revision b7fe612ac1de393f869c9818d5503633c8e96b36)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4 
5 #ifndef _RTE_STACK_LF_H_
6 #define _RTE_STACK_LF_H_
7 
8 #if !(defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_ARM64))
9 #include "rte_stack_lf_stubs.h"
10 #else
11 #ifdef RTE_USE_C11_MEM_MODEL
12 #include "rte_stack_lf_c11.h"
13 #else
14 #include "rte_stack_lf_generic.h"
15 #endif
16 #endif
17 
18 /**
19  * @internal Push several objects on the lock-free stack (MT-safe).
20  *
21  * @param s
22  *   A pointer to the stack structure.
23  * @param obj_table
24  *   A pointer to a table of void * pointers (objects).
25  * @param n
26  *   The number of objects to push on the stack from the obj_table.
27  * @return
28  *   Actual number of objects enqueued.
29  */
30 static __rte_always_inline unsigned int
31 __rte_stack_lf_push(struct rte_stack *s,
32 		    void * const *obj_table,
33 		    unsigned int n)
34 {
35 	struct rte_stack_lf_elem *tmp, *first, *last = NULL;
36 	unsigned int i;
37 
38 	if (unlikely(n == 0))
39 		return 0;
40 
41 	/* Pop n free elements */
42 	first = __rte_stack_lf_pop_elems(&s->stack_lf.free, n, NULL, &last);
43 	if (unlikely(first == NULL))
44 		return 0;
45 
46 	/* Construct the list elements */
47 	for (tmp = first, i = 0; i < n; i++, tmp = tmp->next)
48 		tmp->data = obj_table[n - i - 1];
49 
50 	/* Push them to the used list */
51 	__rte_stack_lf_push_elems(&s->stack_lf.used, first, last, n);
52 
53 	return n;
54 }
55 
56 /**
57  * @internal Pop several objects from the lock-free stack (MT-safe).
58  *
59  * @param s
60  *   A pointer to the stack structure.
61  * @param obj_table
62  *   A pointer to a table of void * pointers (objects).
63  * @param n
64  *   The number of objects to pull from the stack.
65  * @return
66  *   - Actual number of objects popped.
67  */
68 static __rte_always_inline unsigned int
69 __rte_stack_lf_pop(struct rte_stack *s, void **obj_table, unsigned int n)
70 {
71 	struct rte_stack_lf_elem *first, *last = NULL;
72 
73 	if (unlikely(n == 0))
74 		return 0;
75 
76 	/* Pop n used elements */
77 	first = __rte_stack_lf_pop_elems(&s->stack_lf.used,
78 					 n, obj_table, &last);
79 	if (unlikely(first == NULL))
80 		return 0;
81 
82 	/* Push the list elements to the free list */
83 	__rte_stack_lf_push_elems(&s->stack_lf.free, first, last, n);
84 
85 	return n;
86 }
87 
88 /**
89  * @internal Initialize a lock-free stack.
90  *
91  * @param s
92  *   A pointer to the stack structure.
93  * @param count
94  *   The size of the stack.
95  */
96 void
97 rte_stack_lf_init(struct rte_stack *s, unsigned int count);
98 
99 /**
100  * @internal Return the memory required for a lock-free stack.
101  *
102  * @param count
103  *   The size of the stack.
104  * @return
105  *   The bytes to allocate for a lock-free stack.
106  */
107 ssize_t
108 rte_stack_lf_get_memsize(unsigned int count);
109 
110 #endif /* _RTE_STACK_LF_H_ */
111