xref: /dflybsd-src/sys/dev/drm/include/linux/llist.h (revision 1ffa416f1f9e418612e4ae3745dbf75fda1a2fa8)
11382b9d0SFrançois Tigeot /*
2*1ffa416fSFrançois Tigeot  * Copyright (c) 2019-2020 Jonathan Gray <jsg@openbsd.org>
31382b9d0SFrançois Tigeot  * Copyright (c) 2020 François Tigeot <ftigeot@wolfpond.org>
41382b9d0SFrançois Tigeot  *
51382b9d0SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
61382b9d0SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
71382b9d0SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
81382b9d0SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
91382b9d0SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
101382b9d0SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
111382b9d0SFrançois Tigeot  *
121382b9d0SFrançois Tigeot  * The above copyright notice and this permission notice (including the next
131382b9d0SFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
141382b9d0SFrançois Tigeot  * Software.
151382b9d0SFrançois Tigeot  *
161382b9d0SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171382b9d0SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181382b9d0SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
191382b9d0SFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
201382b9d0SFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
211382b9d0SFrançois Tigeot  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
221382b9d0SFrançois Tigeot  * SOFTWARE.
231382b9d0SFrançois Tigeot  */
241382b9d0SFrançois Tigeot 
251382b9d0SFrançois Tigeot #ifndef _LINUX_LLIST_H_
261382b9d0SFrançois Tigeot #define _LINUX_LLIST_H_
271382b9d0SFrançois Tigeot 
281382b9d0SFrançois Tigeot #include <linux/atomic.h>
291382b9d0SFrançois Tigeot #include <linux/kernel.h>
301382b9d0SFrançois Tigeot 
311382b9d0SFrançois Tigeot struct llist_node {
321382b9d0SFrançois Tigeot 	struct llist_node *next;
331382b9d0SFrançois Tigeot };
341382b9d0SFrançois Tigeot 
351382b9d0SFrançois Tigeot struct llist_head {
361382b9d0SFrançois Tigeot 	struct llist_node *first;
371382b9d0SFrançois Tigeot };
381382b9d0SFrançois Tigeot 
391382b9d0SFrançois Tigeot #define llist_entry(ptr, type, member) \
401382b9d0SFrançois Tigeot 	((ptr) ? container_of(ptr, type, member) : NULL)
411382b9d0SFrançois Tigeot 
421382b9d0SFrançois Tigeot static inline struct llist_node *
llist_del_all(struct llist_head * head)431382b9d0SFrançois Tigeot llist_del_all(struct llist_head *head)
441382b9d0SFrançois Tigeot {
451382b9d0SFrançois Tigeot 	return atomic_swap_ptr((void *)&head->first, NULL);
461382b9d0SFrançois Tigeot }
471382b9d0SFrançois Tigeot 
48*1ffa416fSFrançois Tigeot static inline struct llist_node *
llist_del_first(struct llist_head * head)49*1ffa416fSFrançois Tigeot llist_del_first(struct llist_head *head)
50*1ffa416fSFrançois Tigeot {
51*1ffa416fSFrançois Tigeot 	struct llist_node *first, *next;
52*1ffa416fSFrançois Tigeot 
53*1ffa416fSFrançois Tigeot 	do {
54*1ffa416fSFrançois Tigeot 		first = head->first;
55*1ffa416fSFrançois Tigeot 		if (first == NULL)
56*1ffa416fSFrançois Tigeot 			return NULL;
57*1ffa416fSFrançois Tigeot 		next = first->next;
58*1ffa416fSFrançois Tigeot 	} while (atomic_cas_ptr(&head->first, first, next) != first);
59*1ffa416fSFrançois Tigeot 
60*1ffa416fSFrançois Tigeot 	return first;
61*1ffa416fSFrançois Tigeot }
62*1ffa416fSFrançois Tigeot 
631382b9d0SFrançois Tigeot static inline bool
llist_add(struct llist_node * new,struct llist_head * head)641382b9d0SFrançois Tigeot llist_add(struct llist_node *new, struct llist_head *head)
651382b9d0SFrançois Tigeot {
661382b9d0SFrançois Tigeot 	struct llist_node *first = READ_ONCE(head->first);
671382b9d0SFrançois Tigeot 
681382b9d0SFrançois Tigeot 	do {
691382b9d0SFrançois Tigeot 		new->next = first;
701382b9d0SFrançois Tigeot 	} while (cmpxchg(&head->first, first, new) != first);
711382b9d0SFrançois Tigeot 
721382b9d0SFrançois Tigeot 	return (first == NULL);
731382b9d0SFrançois Tigeot }
741382b9d0SFrançois Tigeot 
751382b9d0SFrançois Tigeot static inline void
init_llist_head(struct llist_head * head)761382b9d0SFrançois Tigeot init_llist_head(struct llist_head *head)
771382b9d0SFrançois Tigeot {
781382b9d0SFrançois Tigeot 	head->first = NULL;
791382b9d0SFrançois Tigeot }
801382b9d0SFrançois Tigeot 
811382b9d0SFrançois Tigeot static inline bool
llist_empty(struct llist_head * head)821382b9d0SFrançois Tigeot llist_empty(struct llist_head *head)
831382b9d0SFrançois Tigeot {
841382b9d0SFrançois Tigeot 	return (head->first == NULL);
851382b9d0SFrançois Tigeot }
861382b9d0SFrançois Tigeot 
871382b9d0SFrançois Tigeot #define llist_for_each_entry_safe(pos, n, node, member) 		\
881382b9d0SFrançois Tigeot 	for (pos = llist_entry((node), __typeof(*pos), member); 	\
891382b9d0SFrançois Tigeot 	    pos != NULL &&						\
901382b9d0SFrançois Tigeot 	    (n = llist_entry(pos->member.next, __typeof(*pos), member), pos); \
911382b9d0SFrançois Tigeot 	    pos = n)
921382b9d0SFrançois Tigeot 
93*1ffa416fSFrançois Tigeot #define llist_for_each_entry(pos, node, member)				\
94*1ffa416fSFrançois Tigeot 	for ((pos) = llist_entry((node), __typeof(*(pos)), member);	\
95*1ffa416fSFrançois Tigeot 	    (pos) != NULL;						\
96*1ffa416fSFrançois Tigeot 	    (pos) = llist_entry((pos)->member.next, __typeof(*(pos)), member))
97*1ffa416fSFrançois Tigeot 
981382b9d0SFrançois Tigeot #endif	/* _LINUX_LLIST_H_ */
99