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