1 /* Licensed under BSD-MIT - see LICENSE file for details */
9 #include <ccan/container_of/container_of.h>
12 * struct lqueue_link - a queue link
13 * @next: next entry, or front of queue, if this is the back
15 * This is used as a link within a queue entry.
20 * struct lqueue_link ql;
24 struct lqueue_link *next;
28 * struct lqueue - the head of a queue
29 * @b: the back of the queue (NULL if empty)
32 struct lqueue_link *back;
36 * LQUEUE - define and initialize an empty queue
37 * @name: the name of the lqueue.
39 * The LQUEUE macro defines an lqueue and initializes it to an empty
40 * queue. It can be prepended by "static" to define a static lqueue.
48 * assert(lqueue_empty(&my_queue));
50 #define LQUEUE(name) \
51 struct lqueue name = { NULL, }
54 * lqueue_init - initialize a queue
55 * @h: the lqueue to set to an empty queue
58 * struct lqueue *qp = malloc(sizeof(*qp));
61 static inline void lqueue_init(struct lqueue *q)
67 * lqueue_empty - is a queue empty?
70 * If the queue is empty, returns true.
73 * assert(lqueue_empty(qp));
75 static inline bool lqueue_empty(const struct lqueue *q)
77 return (q->back == NULL);
81 * lqueue_entry - convert an lqueue_link back into the structure containing it.
83 * @type: the type of the entry
84 * @member: the lqueue_link member of the type
89 * struct lqueue_link ql;
91 * assert(lqueue_entry(&w.ql, struct waiter, ql) == &w);
93 #define lqueue_entry(n, type, member) container_of_or_null(n, type, member)
96 * lqueue_front - get front entry in a queue
98 * @type: the type of queue entries
99 * @member: the lqueue_link entry
101 * If the queue is empty, returns NULL.
106 * f = lqueue_front(qp, struct waiter, ql);
107 * assert(lqueue_dequeue(qp, struct waiter, ql) == f);
109 #define lqueue_front(q, type, member) \
110 lqueue_entry(lqueue_front_((q)), type, member)
111 static inline struct lqueue_link *lqueue_front_(const struct lqueue *q)
116 return q->back->next;
120 * lqueue_back - get back entry in a queue
122 * @type: the type of queue entries
123 * @member: the lqueue_link entry
125 * If the queue is empty, returns NULL.
130 * lqueue_enqueue(qp, &b, ql);
131 * assert(lqueue_back(qp, struct waiter, ql) == &b);
133 #define lqueue_back(q, type, member) \
134 lqueue_entry(lqueue_back_((q)), type, member)
135 static inline struct lqueue_link *lqueue_back_(const struct lqueue *q)
141 * lqueue_enqueue - add an entry to the back of a queue
142 * @q: the queue to add the node to
143 * @e: the item to enqueue
144 * @member: the lqueue_link field of *e
146 * The lqueue_link does not need to be initialized; it will be overwritten.
148 #define lqueue_enqueue(q, e, member) \
149 lqueue_enqueue_((q), &((e)->member))
150 static inline void lqueue_enqueue_(struct lqueue *q, struct lqueue_link *e)
152 if (lqueue_empty(q)) {
153 /* New entry will be both front and back of queue */
157 e->next = lqueue_front_(q);
164 * lqueue_dequeue - remove and return the entry from the front of the queue
166 * @type: the type of queue entries
167 * @member: the lqueue_link field of @type
169 * Note that this leaves the returned entry's link in an undefined
170 * state; it can be added to another queue, but not deleted again.
172 #define lqueue_dequeue(q, type, member) \
173 lqueue_entry(lqueue_dequeue_((q)), type, member)
174 static inline struct lqueue_link *lqueue_dequeue_(struct lqueue *q)
176 struct lqueue_link *front;
181 front = lqueue_front_(q);
182 if (front == lqueue_back_(q)) {
183 assert(front->next == front);
186 q->back->next = front->next;
191 #endif /* CCAN_LQUEUE_H */