-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathppool_queue.c
138 lines (106 loc) · 2.08 KB
/
ppool_queue.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <rtthread.h>
#include "ppool_queue.h"
int ppool_queue_get_insert_pos(pool_node *head,int priority);
//查询插入的位置
pool_w *ppool_queue_init(void)
{
pool_w *head;
head=rt_malloc(sizeof(pool_w));
if(!head)
{
ppool_errno=PE_QUEUE_NO_MEM;
return NULL;
}
head->len=0;
head->head=NULL;
return head;
}
pool_node *ppool_queue_new(ppool_work task,void *arg,int priority)
{
pool_node *node;
if(priority < 0)
{
ppool_errno=PE_PRIORITY_ERROR;
return NULL;
}
node=rt_malloc(sizeof(pool_node));
if(node == NULL)
{
ppool_errno=PE_QUEUE_NODE_NO_MEM;
return NULL;
}
node->task=task;
node->arg=arg;
node->priority=priority;
node->next=NULL;
return node;
}
void ppool_queue_add(pool_w *head,pool_node *node)
{
int pos;
int i;
pool_node *h=head->head;
pos=ppool_queue_get_insert_pos(h,node->priority);
if(pos == -1)
{
head->head=node;
++head->len;
return;
}
else if(pos == 0)
{
node->next=h;
head->head=node;
++head->len;
return;
}
for(i=0;i < pos-1;++i)
h=h->next;
node->next=h->next;
h->next=node;
++head->len;
}
pool_node *ppool_queue_get_task(pool_w *head)
{
pool_node *task;
if(head->head == NULL)
return NULL;
task=head->head;
head->head=head->head->next;
--head->len;
return task;
}
void ppool_queue_cleanup(pool_w *head)
{
pool_node *h=head->head;
pool_node *temp;
while(h)
{
temp=h;
h=h->next;
rt_free(temp);
}
head->len=0;
head->head=NULL;
}
void ppool_queue_destroy(pool_w *head)
{
pool_node *h=head->head;
pool_node *temp;
while(h)
{
temp=h;
h=h->next;
rt_free(temp);
}
rt_free(head);
}
int ppool_queue_get_insert_pos(pool_node *head,int priority)
{
int pos;
if(head == NULL)
return -1;
for(pos=0;head && priority >= head->priority;++pos)
head=head->next;
return pos;
}