-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
116 lines (95 loc) · 2.28 KB
/
main.cpp
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
/**
* @file main.cpp
* @author gzy
* @version 0.1
* @brief :Test for asymmetric coroutine library for C++
*/
#include "myCoroutine.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
void func1(void * arg)
{
puts("1");
puts("11");
puts("111");
puts("1111");
}
void func2(void * arg)
{
puts("22");
puts("22");
coroutine_yield(*(schedule_t *)arg);
puts("22");
puts("22");
}
void func3(void *arg)
{
puts("3333");
puts("3333");
coroutine_yield(*(schedule_t *)arg);
puts("3333");
puts("3333");
}
void context_test()
{
char stack[1024*128];
ucontext_t uc1,ucmain;
// 获取当前上下文到uc1中
getcontext(&uc1);
// 给uci分配新的栈空间,并指定后继是ucmain
uc1.uc_stack.ss_sp = stack;
uc1.uc_stack.ss_size = 1024*128;
uc1.uc_stack.ss_flags = 0;
uc1.uc_link = &ucmain;
// 设置uc1的入口函数为func1,执行完成后 执行uc1.uc_link
makecontext(&uc1,(void (*)())func1,0);
// 保存当前上下文到ucmain, 切换到uc1执行
swapcontext(&ucmain,&uc1);
puts("main");
}
void schedule_test()
{
schedule_t s;
// 创建两个协程 返回在协程数组里面的下标id
int id1 = coroutine_create(s,func3,&s);
int id2 = coroutine_create(s,func2,&s);
puts("create over \n");
while(!schedule_finished(s)){
coroutine_resume(s,id1);
coroutine_resume(s,id2);
}
puts("main over");
}
int test_perfor()
{
struct timeval start;
struct timeval end;
gettimeofday(&start, NULL);
schedule_t s;
int c_id[10000];
for(int i = 0; i < 10000; i++)
{
c_id[i] = coroutine_create(s, func2, &s);
}
gettimeofday(&end, NULL);
double timeuse = 1000 * ( end.tv_sec - start.tv_sec ) + 0.001*(end.tv_usec-start.tv_usec);
printf("time: %f ms\n", timeuse);
gettimeofday(&start, NULL);
while(!schedule_finished(s))
for(auto id : c_id)
{
coroutine_resume(s, id);
}
gettimeofday(&end, NULL);
timeuse = 1000 * ( end.tv_sec - start.tv_sec ) + 0.001*(end.tv_usec-start.tv_usec);
printf("time: %f ms\n", timeuse);
}
int main()
{
// context_test();
// puts("----------------");
// schedule_test();
test_perfor();
return 0;
}