summaryrefslogtreecommitdiffstats
path: root/sys/dev/pci/drm/include/linux/llist.h
blob: 8c0b37da123c94e88ac76a5c964c34a96ecc05d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Public domain. */

#ifndef _LINUX_LLIST_H
#define _LINUX_LLIST_H

#include <sys/atomic.h>

struct llist_node {
	struct llist_node *next;
};

struct llist_head {
	struct llist_node *first;
};

#define llist_entry(ptr, type, member) \
	((ptr) ? container_of(ptr, type, member) : NULL)

static inline struct llist_node *
llist_del_all(struct llist_head *head)
{
	return atomic_swap_ptr(&head->first, NULL);
}

static inline struct llist_node *
llist_del_first(struct llist_head *head)
{
	struct llist_node *first, *next;

	do {
		first = head->first;
		if (first == NULL)
			return NULL;
		next = first->next;
	} while (atomic_cas_ptr(&head->first, first, next) != first);

	return first;
}

static inline bool
llist_add(struct llist_node *new, struct llist_head *head)
{
	struct llist_node *first;

	do {
		new->next = first = head->first;
	} while (atomic_cas_ptr(&head->first, first, new) != first);

	return (first == NULL);
}

static inline bool
llist_add_batch(struct llist_node *new_first, struct llist_node *new_last,
    struct llist_head *head)
{
	struct llist_node *first;

	do {
		new_last->next = first = head->first;
	} while (atomic_cas_ptr(&head->first, first, new_first) != first);

	return (first == NULL);
}

static inline void
init_llist_head(struct llist_head *head)
{
	head->first = NULL;
}

static inline bool
llist_empty(struct llist_head *head)
{
	return (head->first == NULL);
}

#define llist_for_each_safe(pos, n, node)				\
	for ((pos) = (node);						\
	    (pos) != NULL &&						\
	    ((n) = (pos)->next, pos);					\
	    (pos) = (n))

#define llist_for_each_entry_safe(pos, n, node, member) 		\
	for (pos = llist_entry((node), __typeof(*pos), member); 	\
	    pos != NULL &&						\
	    (n = llist_entry(pos->member.next, __typeof(*pos), member), pos); \
	    pos = n)

#define llist_for_each_entry(pos, node, member)				\
	for ((pos) = llist_entry((node), __typeof(*(pos)), member);	\
	    (pos) != NULL;						\
	    (pos) = llist_entry((pos)->member.next, __typeof(*(pos)), member))

#endif