-
Notifications
You must be signed in to change notification settings - Fork 1
/
infqueue.h
90 lines (75 loc) · 2.05 KB
/
infqueue.h
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
#include "ext_list.h"
#include "hunter.h"
struct hk_inf_queue {
struct list_head queue;
spinlock_t lock;
int num;
};
static inline void hk_inf_queue_init(struct hk_inf_queue *queue)
{
INIT_LIST_HEAD(&queue->queue);
queue->num = 0;
spin_lock_init(&queue->lock);
}
static inline void hk_inf_queue_destory(struct hk_inf_queue *queue, void (*destor)(void *node))
{
struct list_head *pos = NULL, *n = NULL;
spin_lock(&queue->lock);
list_for_each_safe(pos, n, &queue->queue)
{
list_del(pos);
queue->num--;
if (destor)
destor(pos);
}
spin_unlock(&queue->lock);
}
static inline void hk_inf_queue_modify(struct hk_inf_queue *queue, void (*callback)(void *node))
{
struct list_head *pos = NULL;
spin_lock(&queue->lock);
list_for_each(pos, &queue->queue)
{
if (callback)
callback(pos);
}
spin_unlock(&queue->lock);
}
static inline int hk_inf_queue_length(struct hk_inf_queue *queue)
{
spin_lock(&queue->lock);
int num = queue->num;
spin_unlock(&queue->lock);
return num;
}
static inline void hk_inf_queue_add_tail_locked(struct hk_inf_queue *queue, struct list_head *node)
{
spin_lock(&queue->lock);
list_add_tail(node, &queue->queue);
queue->num++;
spin_unlock(&queue->lock);
}
static inline int hk_inf_queue_try_pop_front_batch_locked(struct hk_inf_queue *queue, struct list_head *popped_head, int batch_num)
{
struct list_head *pos = NULL;
int pop_num = 0, i = 0;
spin_lock(&queue->lock);
pop_num = min(batch_num, queue->num);
if (pop_num == 0) {
spin_unlock(&queue->lock);
return 0;
}
list_for_each(pos, &queue->queue)
{
if (i >= pop_num)
break;
i++;
}
// NOTE: popped_head is a circular doubly linked list,
// while queue->queue remains a non-circular
// doubly linked list
list_cut_position(popped_head, &queue->queue, pos->prev);
queue->num -= pop_num;
spin_unlock(&queue->lock);
return pop_num;
}