-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcoru.c
86 lines (71 loc) · 2.06 KB
/
coru.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
/*
* coru, a small coroutine library
*
* Copyright (c) 2019 Christopher Haster
* Distributed under the MIT license
*/
#include "coru.h"
#include "coru_platform.h"
// Global object for the currently running coroutine
//
// Note that the active coroutine's stack pointer is swapped with
// its parent's while running
static coru_t *coru_active = NULL;
// Coroutine operations
int coru_create(coru_t *coru, void (*cb)(void*), void *data, size_t size) {
void *buffer = coru_malloc(size);
if (!buffer) {
return CORU_ERR_NOMEM;
}
int err = coru_create_inplace(coru, cb, data, buffer, size);
coru->allocated = buffer;
return err;
}
int coru_create_inplace(coru_t *coru,
void (*cb)(void*), void *data,
void *buffer, size_t size) {
coru->canary = NULL;
coru->allocated = NULL;
int err = coru_plat_init(&coru->sp, &coru->canary, cb, data, buffer, size);
if (err) {
return err;
}
if (coru->canary) {
*coru->canary = (uintptr_t)0x636f7275;
}
return 0;
}
void coru_destroy(coru_t *coru) {
coru_free(coru->allocated);
}
int coru_resume(coru_t *coru) {
// we should not be resuming ourselves
CORU_ASSERT(coru != coru_active);
// push previous coroutine's info on the current stack
coru_t *prev = coru_active;
coru_active = coru;
// yield into coroutine
int state = coru_plat_yield(&coru->sp, 0);
// restore previous coroutine's info
coru_active = prev;
return state;
}
void coru_yield(void) {
// do nothing if we are not a coroutine, this lets yield be used in
// shared libraries
if (!coru_active) {
return;
}
// check canary, if this fails a stack overflow occured
CORU_ASSERT(!coru_active->canary ||
*coru_active->canary == (uintptr_t)0x636f7275);
// yield out of coroutine
coru_plat_yield(&coru_active->sp, CORU_ERR_AGAIN);
}
// terminate a coroutine, not public but must be called below
// when a coroutine ends
void coru_halt(void) {
while (true) {
coru_plat_yield(&coru_active->sp, 0);
}
}