-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysqos_waitincrease_list.c
executable file
·257 lines (210 loc) · 5.72 KB
/
sysqos_waitincrease_list.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//
// Created by root on 18-8-11.
//
#include <assert.h>
#include <memory.h>
#include <stdlib.h>
#include "sysqos_waitincrease_list.h"
#include "sysqos_common.h"
#include "sysqos_interface.h"
static void erase(struct wait_increase_list *q, app_node_t *node)
{
assert(q && node);
list_del(&node->list_wait_increase);
LISTHEAD_INIT(&node->list_wait_increase);
}
static void enqueue(struct wait_increase_list *q, app_node_t *node)
{
assert(q && node);
list_del(&node->list_wait_increase);
LISTHEAD_INIT(&node->list_wait_increase);
list_add_tail(&node->list_wait_increase, &q->head);
}
static bool is_first_exist(struct wait_increase_list *q, app_node_t **node)
{
struct list_head *frist = q->head.next;
assert(q && node);
if ( list_empty(&q->head) )
{
return false;
}
*node = container_of(frist,app_node_t,list_wait_increase);
return true;
}
static void dequeue(struct wait_increase_list *q)
{
struct list_head *frist = q->head.next;
assert(q);
if ( list_empty(&q->head) )
{
return;
}
list_del(frist);
LISTHEAD_INIT(frist);
list_add_tail(frist, &q->left);
}
int wait_increase_list_init(wait_increase_list_t *q,
unsigned long max_queue_depth)
{
int err = 0, i = 0;
q->insert = enqueue;
q->dequeue = dequeue;
q->erase = erase;
q->is_first_exist = is_first_exist;
LISTHEAD_INIT(&q->head);
return QOS_ERROR_OK;
}
void wait_increase_list_exit(wait_increase_list_t *q)
{
}
/** for test****************************************************************************/
#ifdef LIST_HEAD_QUEUE_TEST
#include "stdio.h"
#define MAX_QUEUE_NUM 3
typedef struct test_context
{
void *queue[MAX_QUEUE_NUM + 1];
int begin;
int end;
} test_context_t;
static void test_init(test_context_t *test)
{
int i = 0;
test->begin = test->end = 0;
memset(test->queue, 0, (MAX_QUEUE_NUM + 1) * sizeof(void *));
}
static void test_enqueue(test_context_t *test, void *val)
{
test->queue[test->end] = val;
if ( ++test->end > MAX_QUEUE_NUM )
{
test->end = 0;
}
assert(test->end != test->begin);
}
static void *test_dequeue(test_context_t *test)
{
void *res = NULL;
if ( test->end == test->begin )
{
return res;
}
res = test->queue[test->begin];
if ( ++test->begin > MAX_QUEUE_NUM )
{
test->begin = 0;
}
return res;
}
static void test_case_init_exit(wait_increase_list_t *q)
{
int err = wait_increase_list_init(q, MAX_QUEUE_NUM);
assert(err == 0);
wait_increase_list_exit(q);
err = wait_increase_list_init(q, MAX_QUEUE_NUM);
assert(err == 0);
printf("[test_case_init_exit] %s[OK]%s\n", GREEN, RESET);
}
static bool test_check_dequeue(wait_increase_list_t *q, test_context_t *test)
{
void *tmp_test = test_dequeue(test);
assert(q && test);
if ( tmp_test )
{
void *tmp_q = NULL;
//FIXME
// assert(q->is_first_exist(q, &tmp_q));
assert(tmp_q == tmp_test);
q->dequeue(q);
return true;
}
else
{
return false;
}
}
static void test_case_endequeue(wait_increase_list_t *q)
{
int i = 0;
test_context_t test;
void *tmp;
// q_item_t *item = NULL;
test_init(&test);
for ( i = 0; i < MAX_QUEUE_NUM; i++ )
{
long id = i + 1;
// item = q->insert(q, (void *) id);
// assert(item->data == (void *) id);
test_enqueue(&test, (void *) id);
}
while ( test_check_dequeue(q, &test) );
for ( i = 0; i < MAX_QUEUE_NUM; i++ )
{
long id = i + 1;
// item = q->insert(q, (void *) id);
// assert(item->data == (void *) id);
test_enqueue(&test, (void *) id);
if ( rand() % 2 == 0 )
{
test_check_dequeue(q, &test);
}
}
while ( test_check_dequeue(q, &test) );
// assert(!q->is_first_exist(q, &tmp));
printf("[test_case_endequeue] %s[OK]%s\n", GREEN, RESET);
}
static void test_case_enerasequeue(wait_increase_list_t *q)
{
int i = 0;
test_context_t test;
test_context_t test_item;
void *tmp;
// q_item_t *item = NULL;
// q_item_t *item1 = NULL;
// q_item_t *item2 = NULL;
q->insert(q, (void *) 1);
q->insert(q, (void *) 2);
q->insert(q, (void *) 2);
//
// q->erase(q,item2);
// q->erase(q,item1);
// q->erase(q,item);
// assert(!q->is_first_exist(q, &tmp));
test_init(&test);
test_init(&test_item);
for ( i = 0; i < MAX_QUEUE_NUM; i++ )
{
long id = i + 1;
q->insert(q, (void *) id);
// assert(item->data == (void *) id);
test_enqueue(&test, (void *) id);
// test_enqueue(&test_item, item);
}
tmp = test_dequeue(&test);
while ( tmp )
{
void *tmp1 = NULL;
void *tmp2 = test_dequeue(&test_item);
// assert(is_first_exist(q, &tmp1));
assert(tmp1 == tmp);
q->erase(q, tmp2);
tmp = test_dequeue(&test);
}
// assert(!q->is_first_exist(q, &tmp));
printf("[test_case_enerasequeue] %s[OK]%s\n", GREEN, RESET);
}
#endif
void test_wait_increase_list()
{
//FIXME:重写此部分测试
#ifdef LIST_HEAD_QUEUE_TEST
int err = 0;
printf(YELLOW"--------------test_wait_increase_list----------------------:\n"RESET);
wait_increase_list_t q;
test_case_init_exit(&q);
test_case_endequeue(&q);
test_case_enerasequeue(&q);
wait_increase_list_exit(&q);
printf(BLUE"----------------test_wait_increase_list end-----------------\n"RESET);
#endif
}