8d105a3b76b05fe4d32f2f899d889d4578e28bf2
[dpdk.git] / lib / librte_eal / common / include / rte_tailq.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_TAILQ_H_
35 #define _RTE_TAILQ_H_
36
37 /**
38  * @file
39  *  Here defines rte_tailq APIs for only internal use
40  *
41  */
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 #include <sys/queue.h>
48
49 /** dummy structure type used by the rte_tailq APIs */
50 struct rte_dummy {
51         TAILQ_ENTRY(rte_dummy) next; /**< Pointer entries for a tailq list */
52 };
53 /** dummy */
54 TAILQ_HEAD(rte_dummy_head, rte_dummy);
55
56 #define RTE_TAILQ_NAMESIZE 32
57
58 /**
59  * The structure defining a tailq header entry for storing
60  * in the rte_config structure in shared memory. Each tailq
61  * is identified by name.
62  * Any library storing a set of objects e.g. rings, mempools, hash-tables,
63  * is recommended to use an entry here, so as to make it easy for
64  * a multi-process app to find already-created elements in shared memory.
65  */
66 struct rte_tailq_head {
67         struct rte_dummy_head tailq_head; /**< NOTE: must be first element */
68 };
69
70 /**
71  * Utility macro to make reserving a tailqueue for a particular struct easier.
72  *
73  * @param name
74  *   The name to be given to the tailq - used by lookup to find it later
75  *
76  * @param struct_name
77  *   The name of the list type we are using. (Generally this is the same as the
78  *   first parameter passed to TAILQ_HEAD macro)
79  *
80  * @return
81  *   The return value from rte_eal_tailq_reserve, typecast to the appropriate
82  *   structure pointer type.
83  *   NULL on error, since the tailq_head is the first
84  *   element in the rte_tailq_head structure.
85  */
86 #define RTE_TAILQ_RESERVE(name, struct_name) \
87         (struct struct_name *)(&rte_eal_tailq_reserve(name)->tailq_head)
88
89 /**
90  * Utility macro to make reserving a tailqueue for a particular struct easier.
91  *
92  * @param idx
93  *   The tailq idx defined in rte_tail_t to be given to the tail queue.
94  *       - used by lookup to find it later
95  *
96  * @param struct_name
97  *   The name of the list type we are using. (Generally this is the same as the
98  *   first parameter passed to TAILQ_HEAD macro)
99  *
100  * @return
101  *   The return value from rte_eal_tailq_reserve, typecast to the appropriate
102  *   structure pointer type.
103  *   NULL on error, since the tailq_head is the first
104  *   element in the rte_tailq_head structure.
105  */
106 #define RTE_TAILQ_RESERVE_BY_IDX(idx, struct_name) \
107         (struct struct_name *)(&rte_eal_tailq_reserve_by_idx(idx)->tailq_head)
108
109 /**
110  * Utility macro to make looking up a tailqueue for a particular struct easier.
111  *
112  * @param name
113  *   The name of tailq
114  *
115  * @param struct_name
116  *   The name of the list type we are using. (Generally this is the same as the
117  *   first parameter passed to TAILQ_HEAD macro)
118  *
119  * @return
120  *   The return value from rte_eal_tailq_lookup, typecast to the appropriate
121  *   structure pointer type.
122  *   NULL on error, since the tailq_head is the first
123  *   element in the rte_tailq_head structure.
124  */
125 #define RTE_TAILQ_LOOKUP(name, struct_name) \
126         (struct struct_name *)(&rte_eal_tailq_lookup(name)->tailq_head)
127
128 /**
129  * Utility macro to make looking up a tailqueue for a particular struct easier.
130  *
131  * @param idx
132  *   The tailq idx defined in rte_tail_t to be given to the tail queue.
133  *
134  * @param struct_name
135  *   The name of the list type we are using. (Generally this is the same as the
136  *   first parameter passed to TAILQ_HEAD macro)
137  *
138  * @return
139  *   The return value from rte_eal_tailq_lookup, typecast to the appropriate
140  *   structure pointer type.
141  *   NULL on error, since the tailq_head is the first
142  *   element in the rte_tailq_head structure.
143  */
144 #define RTE_TAILQ_LOOKUP_BY_IDX(idx, struct_name) \
145         (struct struct_name *)(&rte_eal_tailq_lookup_by_idx(idx)->tailq_head)
146
147 /**
148  * Reserve a slot in the tailq list for a particular tailq header
149  * Note: this function, along with rte_tailq_lookup, is not multi-thread safe,
150  * and both these functions should only be called from a single thread at a time
151  *
152  * @param name
153  *   The name to be given to the tail queue.
154  * @return
155  *   A pointer to the newly reserved tailq entry
156  */
157 struct rte_tailq_head *rte_eal_tailq_reserve(const char *name);
158
159 /**
160  * Reserve a slot in the tailq list for a particular tailq header
161  * Note: this function, along with rte_tailq_lookup, is not multi-thread safe,
162  * and both these functions should only be called from a single thread at a time
163  *
164  * @param idx
165  *   The tailq idx defined in rte_tail_t to be given to the tail queue.
166  * @return
167  *   A pointer to the newly reserved tailq entry
168  */
169 struct rte_tailq_head *rte_eal_tailq_reserve_by_idx(const unsigned idx);
170
171 /**
172  * Dump tail queues to the console.
173  */
174 void rte_dump_tailq(void);
175
176 /**
177  * Lookup for a tail queue.
178  *
179  * Get a pointer to a tail queue header of an already reserved tail
180  * queue identified by the name given as an argument.
181  * Note: this function, along with rte_tailq_reserve, is not multi-thread safe,
182  * and both these functions should only be called from a single thread at a time
183  *
184  * @param name
185  *   The name of the queue.
186  * @return
187  *   A pointer to the tail queue head structure.
188  */
189 struct rte_tailq_head *rte_eal_tailq_lookup(const char *name);
190
191 /**
192  * Lookup for a tail queue.
193  *
194  * Get a pointer to a tail queue header of an already reserved tail
195  * queue identified by the name given as an argument.
196  * Note: this function, along with rte_tailq_reserve, is not multi-thread safe,
197  * and both these functions should only be called from a single thread at a time
198  *
199  * @param idx
200  *   The tailq idx defined in rte_tail_t to be given to the tail queue.
201  * @return
202  *   A pointer to the tail queue head structure.
203  */
204 struct rte_tailq_head *rte_eal_tailq_lookup_by_idx(const unsigned idx);
205
206 #ifdef __cplusplus
207 }
208 #endif
209
210 #endif /* _RTE_TAILQ_H_ */