-
Notifications
You must be signed in to change notification settings - Fork 1
/
mroutine.c
252 lines (202 loc) · 5.27 KB
/
mroutine.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
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/mman.h>
#include <ucontext.h>
#include "mroutine.h"
#define DEF_STACK_SIZE (1024 * 1024)
#define MIN_STACK_SIZE (4 * 1024)
#define DEF_COROUTINE_NUM (32)
// 兼容性
#ifndef MAP_STACK
#define MAP_STACK 0x20000
#endif
struct coroutine_t
{
mr_fun_t fun;
void *args;
void *ret;
int status;
ucontext_t prev;
mid_t prev_mid;
ucontext_t cur;
char *stack;
int stack_size;
};
struct mroutine_t
{
int stack_size;
struct coroutine_t **crs;
int cr_num;
int cr_max;
mid_t cr_mid;
};
static struct coroutine_t *cr_ini(mr_fun_t fun, int stack_size)
{
struct coroutine_t *cr = (struct coroutine_t *)malloc(sizeof (struct coroutine_t));
if (cr == NULL)
return NULL;
cr->fun = fun;
cr->args = NULL;
cr->ret = NULL;
cr->status = MR_READY;
cr->stack_size = stack_size;
cr->stack = mmap(NULL, cr->stack_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_STACK | MAP_GROWSDOWN | MAP_ANONYMOUS,
-1, 0);
if (cr->stack == MAP_FAILED)
{
free(cr);
return NULL;
}
return cr;
}
static void cr_fini(struct coroutine_t *cr)
{
if (cr == NULL)
return;
munmap(cr->stack, cr->stack_size);
free(cr);
}
struct mroutine_t *mr_ini(int coroutine_num, int stack_size)
{
struct mroutine_t *mr = (struct mroutine_t *)malloc(sizeof (struct mroutine_t));
if (mr == NULL)
return NULL;
if (stack_size <= 0)
stack_size = DEF_STACK_SIZE;
else if (stack_size < MIN_STACK_SIZE)
stack_size = MIN_STACK_SIZE;
mr->stack_size = stack_size;
if (coroutine_num <= DEF_COROUTINE_NUM)
coroutine_num = DEF_COROUTINE_NUM;
mr->cr_num = 0;
mr->cr_max = 2 * coroutine_num;
mr->crs = (struct coroutine_t **)malloc(mr->cr_max * sizeof (struct coroutine_t *));
if (mr->crs == NULL)
{
free(mr);
return NULL;
}
memset(mr->crs, 0, mr->cr_max * sizeof (struct coroutine_t *));
mr->cr_mid = -1;
return mr;
}
void mr_fini(struct mroutine_t *mr)
{
if (mr == NULL)
return;
int check_num;
int i;
for (i = 0, check_num = 0; i < mr->cr_max && check_num < mr->cr_num; i++)
{
if (mr->crs[i] != NULL)
{
check_num++;
cr_fini(mr->crs[i]);
mr->crs[i] = NULL;
}
}
free(mr->crs);
mr->crs = NULL;
}
mid_t mr_create(struct mroutine_t *mr, mr_fun_t fun)
{
struct coroutine_t *cr = cr_ini(fun, mr->stack_size);
if (cr == NULL)
return -1;
if (mr->cr_num >= mr->cr_max)
{
mid_t mid = mr->cr_max;
struct coroutine_t **temp = (struct coroutine_t **)realloc(mr->crs, 2 * mr->cr_max * sizeof (struct coroutine_t *));
if (temp == NULL)
return -1;
mr->crs = temp;
mr->cr_max = 2 * mr->cr_max;
mr->cr_num++;
mr->crs[mid] = cr;
return mid;
}
else
{
int check_num;
int i;
for (i = 0, check_num = 0; i < mr->cr_max && check_num <= mr->cr_num; i++)
{
if (mr->crs[i] == NULL)
{
mr->cr_num++;
mr->crs[i] = cr;
return i;
}
}
}
return -1;
}
static void fun_wrap(uint32_t low, uint32_t high)
{
uintptr_t ptr = ((uintptr_t)high) << 32 | (uintptr_t)low;
struct mroutine_t *mr = (struct mroutine_t *)ptr;
struct coroutine_t *cr = mr->crs[mr->cr_mid];
cr->ret = cr->fun(mr, cr->args);
cr->status = MR_DEAD;
}
void *mr_resume(struct mroutine_t *mr, mid_t mid, void *args)
{
if (mr == NULL || mid < 0 || mid >= mr->cr_max || mr->crs[mid] == NULL)
return NULL;
struct coroutine_t *cr = mr->crs[mid];
void *ret = NULL;
switch (cr->status)
{
case MR_READY:
getcontext(&cr->cur);
cr->cur.uc_stack.ss_sp = cr->stack;
cr->cur.uc_stack.ss_size = cr->stack_size;
cr->cur.uc_link = &cr->prev;
cr->args = args;
cr->status = MR_RUNNING;
cr->prev_mid = mr->cr_mid;
mr->cr_mid = mid;
uintptr_t ptr= (uintptr_t)mr;
makecontext(&cr->cur, (void (*)(void))fun_wrap, 2, (uint32_t)ptr, (uint32_t)(ptr>>32));
swapcontext(&cr->prev, &cr->cur);
ret = cr->ret;
mr->cr_mid = cr->prev_mid;
if (cr->status == MR_DEAD)
{
cr_fini(cr);
mr->crs[mid] = NULL;
mr->cr_num--;
}
break;
case MR_SUSPEND:
cr->args = args;
cr->status = MR_RUNNING;
cr->prev_mid = mr->cr_mid;
mr->cr_mid = mid;
swapcontext(&cr->prev, &cr->cur);
ret = cr->ret;
mr->cr_mid = cr->prev_mid;
break;
}
return ret;
}
void *mr_yield(struct mroutine_t *mr, void *ret)
{
if (mr == NULL || mr->cr_mid == -1)
return NULL;
struct coroutine_t *cr = mr->crs[mr->cr_mid];
cr->status = MR_SUSPEND;
cr->ret = ret;
mr->cr_mid = cr->prev_mid;
swapcontext(&cr->cur, &cr->prev);
return cr->args;
}
int mr_status(struct mroutine_t *mr, mid_t mid)
{
if (mid < 0 || mid >= mr->cr_num || mr->crs[mid] == NULL)
return MR_DEAD;
else
return mr->crs[mid]->status;
}