first public release
[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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #ifndef _RTE_TAILQ_H_
37 #define _RTE_TAILQ_H_
38
39 /**
40  * @file
41  *
42  */
43
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 #include <sys/queue.h>
50
51 #ifndef __KERNEL__
52 /** dummy structure type used by the rte_tailq APIs */
53 struct rte_dummy {
54         TAILQ_ENTRY(rte_dummy) next; /**< Pointer entries for a tailq list */
55 };
56 /** dummy */
57 TAILQ_HEAD(rte_dummy_head, rte_dummy);
58
59 #define RTE_TAILQ_NAMESIZE 32
60
61 /**
62  * The structure defining a tailq header entry for storing
63  * in the rte_config structure in shared memory. Each tailq
64  * is identified by name.
65  * Any library storing a set of objects e.g. rings, mempools, hash-tables,
66  * is recommended to use an entry here, so as to make it easy for
67  * a multi-process app to find already-created elements in shared memory.
68  */
69 struct rte_tailq_head {
70         struct rte_dummy_head tailq_head; /**< NOTE: must be first element */
71         char qname[RTE_TAILQ_NAMESIZE]; /**< Queue name */
72 };
73 #else
74 struct rte_tailq_head {};
75 #endif
76
77 /**
78  * Utility macro to make reserving a tailqueue for a particular struct easier.
79  *
80  * @param name
81  *   The name to be given to the tailq - used by lookup to find it later
82  *
83  * @param struct_name
84  *   The name of the list type we are using. (Generally this is the same as the
85  *   first parameter passed to TAILQ_HEAD macro)
86  *
87  * @return
88  *   The return value from rte_eal_tailq_reserve, typecast to the appropriate
89  *   structure pointer type.
90  *   NULL on error, since the tailq_head is the first
91  *   element in the rte_tailq_head structure.
92  */
93 #define RTE_TAILQ_RESERVE(name, struct_name) \
94         (struct struct_name *)(&rte_eal_tailq_reserve(name)->tailq_head)
95
96 /**
97  * Utility macro to make looking up a tailqueue for a particular struct easier.
98  *
99  * @param name
100  *   The name of the tailq
101  *
102  * @param struct_name
103  *   The name of the list type we are using. (Generally this is the same as the
104  *   first parameter passed to TAILQ_HEAD macro)
105  *
106  * @return
107  *   The return value from rte_eal_tailq_lookup, typecast to the appropriate
108  *   structure pointer type.
109  *   NULL on error, since the tailq_head is the first
110  *   element in the rte_tailq_head structure.
111  */
112 #define RTE_TAILQ_LOOKUP(name, struct_name) \
113         (struct struct_name *)(&rte_eal_tailq_lookup(name)->tailq_head)
114
115 /**
116  * Reserve a slot in the tailq list for a particular tailq header
117  * Note: this function, along with rte_tailq_lookup, is not multi-thread safe,
118  * and both these functions should only be called from a single thread at a time
119  *
120  * @param name
121  *   The name to be given to the tail queue.
122  * @return
123  *   A pointer to the newly reserved tailq entry
124  */
125 struct rte_tailq_head *rte_eal_tailq_reserve(const char *name);
126
127 /**
128  * Lookup for a tail queue.
129  *
130  * Get a pointer to a tail queue header of an already reserved tail
131  * queue identified by the name given as an argument.
132  * Note: this function, along with rte_tailq_reserve, is not multi-thread safe,
133  * and both these functions should only be called from a single thread at a time
134  *
135  * @param name
136  *   The name of the queue.
137  * @return
138  *   A pointer to the tail queue head structure.
139  */
140 struct rte_tailq_head *rte_eal_tailq_lookup(const char *name);
141
142 #ifdef __cplusplus
143 }
144 #endif
145
146 #endif /* _RTE_TAILQ_H_ */