This source file includes following definitions.
- spsc_queue_init
- spsc_queue_peek
- spsc_queue_count
- spsc_queue_push
- spsc_queue_pop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 #ifndef DRM_SCHEDULER_SPSC_QUEUE_H_
25 #define DRM_SCHEDULER_SPSC_QUEUE_H_
26
27 #include <linux/atomic.h>
28 #include <linux/preempt.h>
29
30
31
32 struct spsc_node {
33
34
35 struct spsc_node *next;
36 };
37
38 struct spsc_queue {
39
40 struct spsc_node *head;
41
42
43 atomic_long_t tail;
44
45 atomic_t job_count;
46 };
47
48 static inline void spsc_queue_init(struct spsc_queue *queue)
49 {
50 queue->head = NULL;
51 atomic_long_set(&queue->tail, (long)&queue->head);
52 atomic_set(&queue->job_count, 0);
53 }
54
55 static inline struct spsc_node *spsc_queue_peek(struct spsc_queue *queue)
56 {
57 return queue->head;
58 }
59
60 static inline int spsc_queue_count(struct spsc_queue *queue)
61 {
62 return atomic_read(&queue->job_count);
63 }
64
65 static inline bool spsc_queue_push(struct spsc_queue *queue, struct spsc_node *node)
66 {
67 struct spsc_node **tail;
68
69 node->next = NULL;
70
71 preempt_disable();
72
73 tail = (struct spsc_node **)atomic_long_xchg(&queue->tail, (long)&node->next);
74 WRITE_ONCE(*tail, node);
75 atomic_inc(&queue->job_count);
76
77
78
79
80
81 smp_wmb();
82
83 preempt_enable();
84
85 return tail == &queue->head;
86 }
87
88
89 static inline struct spsc_node *spsc_queue_pop(struct spsc_queue *queue)
90 {
91 struct spsc_node *next, *node;
92
93
94 smp_rmb();
95
96 node = READ_ONCE(queue->head);
97
98 if (!node)
99 return NULL;
100
101 next = READ_ONCE(node->next);
102 WRITE_ONCE(queue->head, next);
103
104 if (unlikely(!next)) {
105
106
107 if (atomic_long_cmpxchg(&queue->tail,
108 (long)&node->next, (long) &queue->head) != (long)&node->next) {
109
110 do {
111 smp_rmb();
112 } while (unlikely(!(queue->head = READ_ONCE(node->next))));
113 }
114 }
115
116 atomic_dec(&queue->job_count);
117 return node;
118 }
119
120
121
122 #endif