forked from cloudwu/coroutine
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.c
160 lines (140 loc) · 3.74 KB
/
main.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
#include "coroutine.h"
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
struct args {
int n;
};
double timediff(struct timeval *begin, struct timeval *end) {
return (end->tv_sec + end->tv_usec * 1.0 / 1000000) -
(begin->tv_sec + begin->tv_usec * 1.0 / 1000000);
}
///////////////////////////////////////////////////////////////////////////////////
// 简单测试
static void foo(schedule_t * S, void *ud) {
struct args * arg = ud;
int start = arg->n;
int i;
for (i=0;i<5;i++) {
printf("coroutine %d : %d\n", co_running(S) , start + i);
co_yield(S);
}
}
static void test(schedule_t *S) {
struct args arg1 = { 0 };
struct args arg2 = { 100 };
int co1 = co_new(S, foo, &arg1);
int co2 = co_new(S, foo, &arg2);
printf("test start===============\n");
while (co_status(S,co1) && co_status(S,co2)) {
co_resume(S,co1);
co_resume(S,co2);
}
printf("test end==============\n");
}
///////////////////////////////////////////////////////////////////////////////////
// 递归创建1000个协程
static void foo2(schedule_t * S, void *ud) {
struct args * arg = ud;
if (arg->n < 1000) {
arg->n++;
int co = co_new(S, foo2, arg);
printf("coroutine: %d, n=%d\n", co, arg->n);
co_resume(S, co);
}
}
static void test2(schedule_t *S) {
printf("test2 start===============\n");
struct args arg = { 0 };
int co = co_new(S, foo2, &arg);
printf("coroutine: %d, n=%d\n", co, arg.n);
co_resume(S, co);
printf("test2 end==============\n");
}
///////////////////////////////////////////////////////////////////////////////////
// 主协程测试
static void test3(schedule_t *S) {
printf("test3 start===============\n");
int mainco = co_running(S);
printf("main co: %d\n", mainco);
printf("yield mainco: %d\n", co_yield(S));
printf("test3 end==============\n");
}
///////////////////////////////////////////////////////////////////////////////////
// 协程相互resume
static int co1, co2, co3;
static void foo4_3(schedule_t *S, void *ud) {
printf("[%d] run\n", co_running(S));
co_resume(S, co2);
printf("[%d] run done\n", co_running(S));
}
static void foo4_2(schedule_t *S, void *ud) {
printf("[%d] run\n", co_running(S));
co_yield(S);
printf("[%d] run done\n", co_running(S));
}
static void foo4_1(schedule_t *S, void *ud) {
printf("[%d] run\n", co_running(S));
co_resume(S, co2);
co3 = co_new(S, foo4_3, NULL);
co_resume(S, co3);
printf("[%d] run done\n", co_running(S));
}
static void test4(schedule_t *S) {
printf("test4 start===============\n");
co1 = co_new(S, foo4_1, NULL);
co2 = co_new(S, foo4_2, NULL);
co_resume(S, co1);
printf("test4 end===============\n");
}
///////////////////////////////////////////////////////////////////////////////////
// 协程的创建和切换消耗
int stop = 0;
static void foo_5(schedule_t *S, void *ud) {
while (!stop) {
co_yield(S);
}
}
static void test5(schedule_t *S) {
printf("test5 start===============\n");
struct timeval begin;
struct timeval end;
int i;
int count = 1000;
gettimeofday(&begin, NULL);
for (i = 0; i < count; ++i) {
co_new(S, foo_5, NULL);
}
gettimeofday(&end, NULL);
printf("create time=%f\n", timediff(&begin, &end));
gettimeofday(&begin, NULL);
for (i =0; i < 5000000; ++i) {
int co = (i % count) + 1;
co_resume(S, co);
}
gettimeofday(&end, NULL);
printf("swap time=%f\n", timediff(&begin, &end));
// 先释放掉原来的
stop = 1;
for (i = 0; i < count; ++i) {
int co = (i % count) + 1;
co_resume(S, co);
}
gettimeofday(&begin, NULL);
for (i = 0; i < count; ++i) {
co_new(S, foo_5, NULL);
}
gettimeofday(&end, NULL);
printf("create time2=%f\n", timediff(&begin, &end));
printf("test5 end===============\n");
}
int main() {
schedule_t * S = co_open(0);
test(S);
test2(S);
test3(S);
test4(S);
test5(S);
co_close(S);
return 0;
}