forked from cloudwu/coroutine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coroutine.c
303 lines (264 loc) · 6.73 KB
/
coroutine.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "coroutine.h"
#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#ifdef __cplusplus
extern "C"
{
#endif
#define STACK_SIZE (8*1024*1024)
#define DEFAULT_COROUTINE 128
#define MAX_COROUTINE 8192
struct coroutine;
struct schedule {
char stack[STACK_SIZE]; // 保存当前运行coroutine的栈
ucontext_t main;
int nco;
int cap;
int running;
struct coroutine **co;
int last_co_id;
int last_check_idx;
};
struct coroutine {
struct coroutine_callbacks_t callbacks;
ucontext_t ctx;
struct schedule * sch;
ptrdiff_t cap;
ptrdiff_t size;
int status;
char *stack;
time_t create_time;
int fatal;
};
int coroutine_delete( struct schedule * sched, int id );
struct coroutine *
_co_new(struct schedule *sched, struct coroutine_callbacks_t callbacks) {
struct coroutine * co = malloc(sizeof(*co));
co->callbacks = callbacks;
co->sch = sched;
co->cap = 0;
co->size = 0;
co->status = COROUTINE_READY;
co->stack = NULL;
co->create_time = time(NULL);
co->fatal = 0;
return co;
}
void
_co_delete(struct coroutine *co) {
free(co->stack);
free(co);
}
struct schedule *
coroutine_open(void) {
struct schedule *sched = malloc(sizeof(*sched));
sched->nco = 0;
sched->cap = DEFAULT_COROUTINE;
sched->running = -1;
sched->co = malloc(sizeof(struct coroutine *) * sched->cap);
sched->last_co_id = -1;
sched->last_check_idx = 0;
memset(sched->co, 0, sizeof(struct coroutine *) * sched->cap);
return sched;
}
// coroutine_close的时候是强制关闭的,可能部分coroutine还未执行完
void
coroutine_close(struct schedule *sched) {
int i;
for (i=0;i<sched->cap;i++) {
struct coroutine * co = sched->co[i];
if (co) {
_co_delete(co);
}
}
free(sched->co);
sched->co = NULL;
free(sched);
}
int
coroutine_new(struct schedule *sched, struct coroutine_callbacks_t callbacks) {
if (sched->nco >= sched->cap && sched->cap >= MAX_COROUTINE) {
// full
return -1;
}
struct coroutine *co = _co_new(sched, callbacks);
if (sched->nco >= sched->cap) {
int id = sched->cap;
sched->co = realloc(sched->co, sched->cap * 2 * sizeof(struct coroutine *));
memset(sched->co + sched->cap , 0 , sizeof(struct coroutine *) * sched->cap);
sched->co[sched->cap] = co;
sched->cap *= 2;
++sched->nco;
sched->last_co_id = id;
return id;
} else {
int i;
for (i=0;i<sched->cap;i++) {
int id = (i + 1 + sched->last_co_id) % sched->cap;
if (sched->co[id] == NULL) {
sched->co[id] = co;
++sched->nco;
sched->last_co_id = id;
return id;
}
}
}
assert(0);
return -1;
}
static void
mainfunc(uint32_t low32, uint32_t hi32) {
uintptr_t ptr = (uintptr_t)low32 | ((uintptr_t)hi32 << 32);
struct schedule *sched = (struct schedule *)ptr;
int id = sched->running;
struct coroutine *C = sched->co[id];
C->callbacks.main_(sched,C->callbacks.ud_);
coroutine_delete(sched, id);
sched->running = -1;
}
void
coroutine_resume(struct schedule * sched, int id) {
assert(sched->running == -1);
assert(id >=0 && id < sched->cap);
struct coroutine *C = sched->co[id];
if (C == NULL)
return;
int status = C->status;
switch(status) {
case COROUTINE_READY:
getcontext(&C->ctx);
C->ctx.uc_stack.ss_sp = sched->stack;
C->ctx.uc_stack.ss_size = STACK_SIZE;
C->ctx.uc_link = &sched->main;
sched->running = id;
C->status = COROUTINE_RUNNING;
uintptr_t ptr = (uintptr_t)sched;
makecontext(&C->ctx, (void (*)(void)) mainfunc, 2, (uint32_t)ptr, (uint32_t)(ptr>>32));
swapcontext(&sched->main, &C->ctx);
break;
case COROUTINE_SUSPEND:
memcpy(sched->stack + STACK_SIZE - C->size, C->stack, C->size);
sched->running = id;
C->status = COROUTINE_RUNNING;
swapcontext(&sched->main, &C->ctx);
break;
default:
assert(0);
}
}
static void
_save_stack(struct coroutine *C, char *top) {
char dummy = 0;
assert(top - &dummy <= STACK_SIZE);
if (C->cap < top - &dummy) {
free(C->stack);
C->cap = top-&dummy;
C->stack = malloc(C->cap); // 创建栈空间
}
C->size = top - &dummy;
memcpy(C->stack, &dummy, C->size); // 保存
}
void
coroutine_yield(struct schedule * sched) {
int id = sched->running;
assert(id >= 0);
struct coroutine * C = sched->co[id];
assert((char *)&C > sched->stack);
_save_stack(C,sched->stack + STACK_SIZE);
C->status = COROUTINE_SUSPEND;
sched->running = -1;
swapcontext(&C->ctx , &sched->main);
// 这里resume/delete的时候一定要把原来的函数context执行完,不然函数里声明类的析构函数不会被执行!
}
int
coroutine_status(struct schedule * sched, int id) {
assert(id>=0 && id < sched->cap);
if (sched->co[id] == NULL) {
return COROUTINE_DEAD;
}
return sched->co[id]->status;
}
int
coroutine_running(struct schedule * sched) {
return sched->running;
}
int coroutine_delete( struct schedule * sched, int id ) {
assert(id>=0 && id < sched->cap);
struct coroutine *C = sched->co[id];
if (NULL == C) {
return -1;
}
C->callbacks.ondelete_(sched, C->callbacks.ud_);
_co_delete(C);
sched->co[id] = NULL;
--sched->nco;
return 0;
}
int coroutine_check_timeout( struct schedule * sched, int max_check_num, int life ) {
int del_num = 0;
time_t now = time(NULL);
int check_num = 0;
int i = -1;
while (check_num < max_check_num && check_num <= sched->cap) {
++check_num;
i = sched->last_check_idx;
i = (i >= sched->cap || i < 0)? 0: i;
sched->last_check_idx = i + 1;
struct coroutine * C = sched->co[i];
if (NULL == C) {
continue;
}
if (now - C->create_time >= life) {
C->callbacks.ontimeout_(sched, i, C->callbacks.ud_);
C->fatal = 1;
// 让他执行完, coroutine要检查fatal字段
coroutine_resume(sched, i);
++del_num;
}
}
return del_num;
}
int coroutine_id( struct schedule * sched ) {
return sched->running;
}
void* coroutine_get_ud( struct schedule * sched, int id ) {
assert(id>=0 && id < sched->cap);
struct coroutine *C = sched->co[id];
if (NULL == C) {
return NULL;
}
return C->callbacks.ud_;
}
int coroutine_check( struct schedule * sched, int id ) {
if (id < 0 || id >= sched->cap) {
return -1;
}
struct coroutine *C = sched->co[id];
if (NULL == C) {
return -1;
}
return 0;
}
int coroutine_fatal( struct schedule * sched, int id ) {
assert(id>=0 && id < sched->cap);
struct coroutine *C = sched->co[id];
if (NULL == C) {
return 0;
}
return C->fatal;
}
int coroutine_get_sched_cap( struct schedule * sched ) {
return sched->cap;
}
int coroutine_get_sched_cur( struct schedule * sched ) {
return sched->nco;
}
#ifdef __cplusplus
}
#endif