Skip to content

Commit 21497d6

Browse files
committed
Update Full feature Test Code
1 parent df6fba6 commit 21497d6

File tree

1 file changed

+135
-16
lines changed

1 file changed

+135
-16
lines changed

example.c

Lines changed: 135 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "lfqueue.h"
1010

1111

12+
typedef void (*test_function)(pthread_t*);
13+
1214
void one_enq_and_multi_deq(pthread_t *threads);
1315
void one_deq_and_multi_enq(pthread_t *threads);
1416
void multi_enq_deq(pthread_t *threads);
@@ -17,21 +19,87 @@ void* worker_s(void *);
1719
void* worker_c(void *);
1820
void* worker_single_c(void *);
1921

22+
/**Testing must**/
23+
void one_enq_and_multi_deq_must(pthread_t *threads);
24+
void one_deq_must_and_multi_enq(pthread_t *threads);
25+
void multi_enq_deq_must(pthread_t *threads);
26+
void* worker_sc_must(void *);
27+
void* worker_s_must(void *);
28+
void* worker_c_must(void *);
29+
void* worker_single_c_must(void *);
30+
31+
void running_test(test_function testfn);
32+
2033
struct timeval tv1, tv2;
2134
#define total_put 50000
22-
int nthreads = 4; //sysconf(_SC_NPROCESSORS_ONLN); // Linux
35+
#define total_running_loop 50
36+
int nthreads = 4;
2337
int one_thread = 1;
2438
int nthreads_exited = 0;
2539
lfqueue_t *myq;
2640

41+
42+
43+
void* worker_c_must(void *arg) {
44+
int i = 0;
45+
int *int_data;
46+
int total_loop = total_put * (*(int*)arg);
47+
while (i++ < total_loop) {
48+
/*Dequeue*/
49+
int_data = lfqueue_deq_must(myq);
50+
// printf("%d\n", *int_data);
51+
52+
free(int_data);
53+
}
54+
__sync_add_and_fetch(&nthreads_exited, 1);
55+
return 0;
56+
}
57+
58+
void* worker_single_c_must(void *arg) {
59+
int i = 0;
60+
int *int_data;
61+
int total_loop = total_put * (*(int*)arg);
62+
while (i++ < total_loop) {
63+
/*Dequeue*/
64+
int_data = lfqueue_single_deq_must(myq);
65+
// printf("%d\n", *int_data);
66+
67+
free(int_data);
68+
}
69+
__sync_add_and_fetch(&nthreads_exited, 1);
70+
return 0;
71+
}
72+
73+
void* worker_sc_must(void *arg)
74+
{
75+
int i = 0;
76+
int *int_data;
77+
while (i < total_put) {
78+
int_data = (int*)malloc(sizeof(int));
79+
assert(int_data != NULL);
80+
*int_data = i++;
81+
/*Enqueue*/
82+
while (lfqueue_enq(myq, int_data)) {
83+
printf("ENQ FULL?\n");
84+
}
85+
86+
/*Dequeue*/
87+
int_data = lfqueue_deq_must(myq);
88+
// printf("%d\n", *int_data);
89+
free(int_data);
90+
}
91+
__sync_add_and_fetch(&nthreads_exited, 1);
92+
return 0;
93+
}
94+
2795
void* worker_c(void *arg) {
2896
int i = 0;
2997
int *int_data;
3098
int total_loop = total_put * (*(int*)arg);
3199
while (i++ < total_loop) {
32100
/*Dequeue*/
33101
while ((int_data = lfqueue_deq(myq)) == NULL) {
34-
102+
lfqueue_sleep(1);
35103
}
36104
// printf("%d\n", *int_data);
37105

@@ -48,7 +116,7 @@ void* worker_single_c(void *arg) {
48116
while (i++ < total_loop) {
49117
/*Dequeue*/
50118
while ((int_data = lfqueue_single_deq(myq)) == NULL) {
51-
119+
lfqueue_sleep(1);
52120
}
53121
// printf("%d\n", *int_data);
54122

@@ -93,7 +161,7 @@ void* worker_sc(void *arg)
93161

94162
/*Dequeue*/
95163
while ((int_data = lfqueue_deq(myq)) == NULL) {
96-
// printf("DEQ EMPTY? %zu\n", lfqueue_size(myq));
164+
lfqueue_sleep(1);
97165
}
98166
// printf("%d\n", *int_data);
99167
free(int_data);
@@ -126,6 +194,7 @@ void multi_enq_deq(pthread_t *threads) {
126194
join_threads;
127195
// detach_thread_and_loop;
128196
}
197+
129198
void one_deq_and_multi_enq(pthread_t *threads) {
130199
printf("-----------%s---------------\n", "one_deq_and_multi_enq");
131200
int i;
@@ -152,29 +221,62 @@ void one_enq_and_multi_deq(pthread_t *threads) {
152221
#pragma GCC diagnostic pop
153222

154223
}
155-
int ri = 10;
156-
int main(void)
157-
{
158-
int n;
159224

160-
myq = malloc(sizeof (lfqueue_t));
161-
if (lfqueue_init(myq) == -1)
162-
return -1;
163225

164-
for (n = 0; n < 300; n++) {
226+
void one_deq_must_and_multi_enq(pthread_t *threads) {
227+
printf("-----------%s---------------\n", "one_deq_must_and_multi_enq");
228+
int i;
229+
for (i = 0; i < nthreads; i++)
230+
pthread_create(threads + i, NULL, worker_s, &one_thread);
231+
232+
worker_single_c_must(&nthreads);
233+
234+
join_threads;
235+
// detach_thread_and_loop;
236+
}
237+
238+
void one_enq_and_multi_deq_must(pthread_t *threads) {
239+
printf("-----------%s---------------\n", "one_enq_and_multi_deq_must");
240+
int i;
241+
for (i = 0; i < nthreads; i++)
242+
pthread_create(threads + i, NULL, worker_c_must, &one_thread);
243+
244+
worker_s(&nthreads);
245+
246+
#pragma GCC diagnostic push
247+
#pragma GCC diagnostic ignored "-Wimplicit-function-declaration"
248+
detach_thread_and_loop;
249+
#pragma GCC diagnostic pop
250+
251+
}
252+
253+
void multi_enq_deq_must(pthread_t *threads) {
254+
printf("-----------%s---------------\n", "multi_enq_deq_must");
255+
int i;
256+
for (i = 0; i < nthreads; i++) {
257+
pthread_create(threads + i, NULL, worker_sc_must, NULL);
258+
}
259+
260+
join_threads;
261+
// detach_thread_and_loop;
262+
}
263+
264+
void running_test(test_function testfn) {
265+
int n;
266+
for (n = 0; n < total_running_loop; n++) {
165267
printf("Current running at =%d, ", n);
166268
nthreads_exited = 0;
167-
168269
/* Spawn threads. */
169270
pthread_t threads[nthreads];
170271
printf("Using %d thread%s.\n", nthreads, nthreads == 1 ? "" : "s");
171272
printf("Total requests %d \n", total_put);
172273
gettimeofday(&tv1, NULL);
173274

174-
//one_enq_and_multi_deq(threads);
275+
testfn(threads);
276+
// one_enq_and_multi_deq(threads);
175277

176278
//one_deq_and_multi_enq(threads);
177-
multi_enq_deq(threads);
279+
// multi_enq_deq(threads);
178280
// worker_s(&ri);
179281
// worker_c(&ri);
180282

@@ -183,14 +285,31 @@ int main(void)
183285
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
184286
(double) (tv2.tv_sec - tv1.tv_sec));
185287

186-
//getchar();
187288
lfqueue_sleep(10);
188289
assert ( 0 == lfqueue_size(myq) && "Error, all queue should be consumed but not");
189290
}
291+
}
292+
293+
int main(void) {
294+
myq = malloc(sizeof (lfqueue_t));
295+
if (lfqueue_init(myq) == -1)
296+
return -1;
297+
298+
running_test(one_enq_and_multi_deq);
299+
running_test(one_enq_and_multi_deq_must);
300+
301+
running_test(one_deq_and_multi_enq);
302+
running_test(one_deq_must_and_multi_enq);
303+
304+
running_test(multi_enq_deq);
305+
running_test(multi_enq_deq_must);
306+
307+
190308
lfqueue_destroy(myq);
191309
// sleep(3);
192310
free(myq);
193311

312+
printf("Test Pass!\n");
194313

195314
return 0;
196315
}

0 commit comments

Comments
 (0)