summaryrefslogtreecommitdiff
path: root/sys/compat/linuxkpi/common/include/linux/llist.h
blob: fd842f05e9eb205f59ef0bc0c8624e7d36d0121d (plain)
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
95
96
97
98
99
100
101
/* Public domain. */

#ifndef _LINUXKPI_LINUX_LLIST_H
#define _LINUXKPI_LINUX_LLIST_H

#include <sys/types.h>
#include <machine/atomic.h>

struct llist_node {
	struct llist_node *next;
};

struct llist_head {
	struct llist_node *first;
};

#define	LLIST_HEAD_INIT(name)	{ NULL }
#define	LLIST_HEAD(name)	struct llist_head name = LLIST_HEAD_INIT(name)

#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 ((void *)atomic_readandclear_ptr((uintptr_t *)&head->first));
}

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_cmpset_ptr((uintptr_t *)&head->first,
	    (uintptr_t)first, (uintptr_t)next) == 0);

	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_cmpset_ptr((uintptr_t *)&head->first,
	    (uintptr_t)first, (uintptr_t)new) == 0);

	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_cmpset_ptr((uintptr_t *)&head->first,
	    (uintptr_t)first, (uintptr_t)new_first) == 0);

	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