17960a7a847d0997e83e5a001d6460c9e637ed80
[dpdk.git] / lib / librte_eal / common / include / rte_tailq.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #ifndef _RTE_TAILQ_H_
36 #define _RTE_TAILQ_H_
37
38 /**
39  * @file
40  *  Here defines rte_tailq APIs for only internal use
41  *
42  */
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 #include <sys/queue.h>
49
50 /** dummy structure type used by the rte_tailq APIs */
51 struct rte_dummy {
52         TAILQ_ENTRY(rte_dummy) next; /**< Pointer entries for a tailq list */
53 };
54 /** dummy */
55 TAILQ_HEAD(rte_dummy_head, rte_dummy);
56
57 #define RTE_TAILQ_NAMESIZE 32
58
59 /**
60  * The structure defining a tailq header entry for storing
61  * in the rte_config structure in shared memory. Each tailq
62  * is identified by name.
63  * Any library storing a set of objects e.g. rings, mempools, hash-tables,
64  * is recommended to use an entry here, so as to make it easy for
65  * a multi-process app to find already-created elements in shared memory.
66  */
67 struct rte_tailq_head {
68         struct rte_dummy_head tailq_head; /**< NOTE: must be first element */
69 };
70
71 /**
72  * Utility macro to make reserving a tailqueue for a particular struct easier.
73  *
74  * @param name
75  *   The name to be given to the tailq - used by lookup to find it later
76  *
77  * @param struct_name
78  *   The name of the list type we are using. (Generally this is the same as the
79  *   first parameter passed to TAILQ_HEAD macro)
80  *
81  * @return
82  *   The return value from rte_eal_tailq_reserve, typecast to the appropriate
83  *   structure pointer type.
84  *   NULL on error, since the tailq_head is the first
85  *   element in the rte_tailq_head structure.
86  */
87 #define RTE_TAILQ_RESERVE(name, struct_name) \
88         (struct struct_name *)(&rte_eal_tailq_reserve(name)->tailq_head)
89
90 /**
91  * Utility macro to make reserving a tailqueue for a particular struct easier.
92  *
93  * @param idx
94  *   The tailq idx defined in rte_tail_t to be given to the tail queue.
95  *       - used by lookup to find it later
96  *
97  * @param struct_name
98  *   The name of the list type we are using. (Generally this is the same as the
99  *   first parameter passed to TAILQ_HEAD macro)
100  *
101  * @return
102  *   The return value from rte_eal_tailq_reserve, typecast to the appropriate
103  *   structure pointer type.
104  *   NULL on error, since the tailq_head is the first
105  *   element in the rte_tailq_head structure.
106  */
107 #define RTE_TAILQ_RESERVE_BY_IDX(idx, struct_name) \
108         (struct struct_name *)(&rte_eal_tailq_reserve_by_idx(idx)->tailq_head)
109
110 /**
111  * Utility macro to make looking up a tailqueue for a particular struct easier.
112  *
113  * @param name
114  *   The name of tailq
115  *
116  * @param struct_name
117  *   The name of the list type we are using. (Generally this is the same as the
118  *   first parameter passed to TAILQ_HEAD macro)
119  *
120  * @return
121  *   The return value from rte_eal_tailq_lookup, typecast to the appropriate
122  *   structure pointer type.
123  *   NULL on error, since the tailq_head is the first
124  *   element in the rte_tailq_head structure.
125  */
126 #define RTE_TAILQ_LOOKUP(name, struct_name) \
127         (struct struct_name *)(&rte_eal_tailq_lookup(name)->tailq_head)
128
129 /**
130  * Utility macro to make looking up a tailqueue for a particular struct easier.
131  *
132  * @param idx
133  *   The tailq idx defined in rte_tail_t to be given to the tail queue.
134  *
135  * @param struct_name
136  *   The name of the list type we are using. (Generally this is the same as the
137  *   first parameter passed to TAILQ_HEAD macro)
138  *
139  * @return
140  *   The return value from rte_eal_tailq_lookup, typecast to the appropriate
141  *   structure pointer type.
142  *   NULL on error, since the tailq_head is the first
143  *   element in the rte_tailq_head structure.
144  */
145 #define RTE_TAILQ_LOOKUP_BY_IDX(idx, struct_name) \
146         (struct struct_name *)(&rte_eal_tailq_lookup_by_idx(idx)->tailq_head)
147
148 /**
149  * Reserve a slot in the tailq list for a particular tailq header
150  * Note: this function, along with rte_tailq_lookup, is not multi-thread safe,
151  * and both these functions should only be called from a single thread at a time
152  *
153  * @param name
154  *   The name to be given to the tail queue.
155  * @return
156  *   A pointer to the newly reserved tailq entry
157  */
158 struct rte_tailq_head *rte_eal_tailq_reserve(const char *name);
159
160 /**
161  * Reserve a slot in the tailq list for a particular tailq header
162  * Note: this function, along with rte_tailq_lookup, is not multi-thread safe,
163  * and both these functions should only be called from a single thread at a time
164  *
165  * @param idx
166  *   The tailq idx defined in rte_tail_t to be given to the tail queue.
167  * @return
168  *   A pointer to the newly reserved tailq entry
169  */
170 struct rte_tailq_head *rte_eal_tailq_reserve_by_idx(const unsigned idx);
171
172 /**
173  * Dump tail queues to the console.
174  */
175 void rte_dump_tailq(void);
176
177 /**
178  * Lookup for a tail queue.
179  *
180  * Get a pointer to a tail queue header of an already reserved tail
181  * queue identified by the name given as an argument.
182  * Note: this function, along with rte_tailq_reserve, is not multi-thread safe,
183  * and both these functions should only be called from a single thread at a time
184  *
185  * @param name
186  *   The name of the queue.
187  * @return
188  *   A pointer to the tail queue head structure.
189  */
190 struct rte_tailq_head *rte_eal_tailq_lookup(const char *name);
191
192 /**
193  * Lookup for a tail queue.
194  *
195  * Get a pointer to a tail queue header of an already reserved tail
196  * queue identified by the name given as an argument.
197  * Note: this function, along with rte_tailq_reserve, is not multi-thread safe,
198  * and both these functions should only be called from a single thread at a time
199  *
200  * @param idx
201  *   The tailq idx defined in rte_tail_t to be given to the tail queue.
202  * @return
203  *   A pointer to the tail queue head structure.
204  */
205 struct rte_tailq_head *rte_eal_tailq_lookup_by_idx(const unsigned idx);
206
207 #ifdef __cplusplus
208 }
209 #endif
210
211 #endif /* _RTE_TAILQ_H_ */