-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtest.c
129 lines (104 loc) · 3 KB
/
test.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
/***************************************************************************
*
* Copyright 2017 by Sean Conner.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Comments, questions and criticisms can be sent to: [email protected]
*
*************************************************************************/
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include "coroutine.h"
/**************************************************************************/
static uintptr_t foo(coroutine__s *self,uintptr_t d)
{
d = coroutine_yield(self,d);
return d;
}
static uintptr_t routine1(coroutine__s *self,uintptr_t d)
{
while(true)
{
printf("routine1=%" PRIuPTR "\n",d);
d = foo(self,d + 100);
}
return 0;
}
/**************************************************************************/
static uintptr_t routine2(coroutine__s *self,uintptr_t d)
{
while(true)
{
printf("routine2=%" PRIuPTR "\n",d);
d = coroutine_yield(self,d + 1);
}
return 0;
}
/**************************************************************************/
static uintptr_t routine3(coroutine__s *self,uintptr_t d)
{
while(true)
{
printf("routine3=%" PRIuPTR "\n",d);
d = coroutine_yield(self,d + 2);
}
return 0;
}
/**************************************************************************/
static uintptr_t test(coroutine__s *self,uintptr_t d)
{
printf("Hello %" PRIuPTR "\n",d);
d = coroutine_yield(self,d + 10);
printf("Goodbye %" PRIuPTR "\n",d);
return d + 10;
}
/**************************************************************************/
int main(void)
{
if (true)
{
coroutine__s *co;
uintptr_t r = 0;
coroutine_create(&co,0,test);
for (int i = 0 ; i < 10 ; i++)
{
r = coroutine_yield(co,r);
printf("test-%d=%" PRIuPTR "\n",i,r);
}
coroutine_free(co);
}
printf("---------\n");
if (true)
{
coroutine__s *co1;
coroutine__s *co2;
coroutine__s *co3;
coroutine_create(&co1,0,routine1);
coroutine_create(&co2,0,routine2);
coroutine_create(&co3,0,routine3);
for (int i = 0 , r = 0 ; i < 10 ; i++)
{
r = coroutine_yield(co1,r);
r = coroutine_yield(co2,r + 200);
r = coroutine_yield(co3,r + 300);
}
coroutine_free(co3);
coroutine_free(co2);
coroutine_free(co1);
}
return 0;
}
/**************************************************************************/