forked from edubart/minicoro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminicoro.c
More file actions
101 lines (88 loc) · 2.85 KB
/
Copy pathminicoro.c
File metadata and controls
101 lines (88 loc) · 2.85 KB
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
#define MINICORO_IMPL
#include "minicoro.h"
#include <assert.h>
#include <string.h>
#include <setjmp.h>
mco_coro* mco_coro_init(void (*effectful_fn)(mco_coro* co), void* user_data) {
mco_coro* co;
mco_desc desc = mco_desc_init(effectful_fn, 0);
desc.user_data = user_data;
mco_result res = mco_create(&co, &desc);
assert(res == MCO_SUCCESS);
return co;
}
void* mco_coro_get_user_data(mco_coro* k) {
return mco_get_user_data(k);
}
char mco_coro_free(mco_coro* k) {
mco_result res = mco_destroy(k);
assert(res == MCO_SUCCESS);
// Ante's Cranelift codegen currently doesn't filter out Unit values.
// So since this function (and other void-returning functions) are thought
// to return unit values we still need to return one here.
return 0;
}
char mco_coro_is_suspended(mco_coro* k) {
return mco_status(k) == MCO_SUSPENDED;
}
char mco_coro_push(mco_coro* k, const void* arg, size_t size) {
mco_result res = mco_push(k, arg, size);
assert(res == MCO_SUCCESS);
return 0;
}
char mco_coro_pop(mco_coro* k, void* arg, size_t size) {
mco_result res = mco_pop(k, arg, size);
assert(res == MCO_SUCCESS);
return 0;
}
char mco_coro_suspend(mco_coro* k) {
mco_result res = mco_yield(k);
assert(res == MCO_SUCCESS);
return 0;
}
char mco_coro_resume(mco_coro* k) {
mco_result res = mco_resume(k);
assert(res == MCO_SUCCESS);
return 0;
}
mco_coro* mco_coro_running(void) {
return mco_running();
}
size_t mco_coro_bytes_stored(mco_coro* k) {
return mco_get_bytes_stored(k);
}
// Bulk-move `len` bytes from `src`'s storage to `dst`'s storage, preserving
// byte order. Used by the effect-lowering pass to forward an effect through a
// nested handler's drive without parsing the args by type.
char mco_coro_transfer(mco_coro* src, mco_coro* dst, size_t len) {
if (len == 0) return 0;
assert(src != NULL && dst != NULL);
assert(len <= src->bytes_stored);
assert(dst->bytes_stored + len <= dst->storage_size);
memcpy(&dst->storage[dst->bytes_stored],
&src->storage[src->bytes_stored - len],
len);
dst->bytes_stored += len;
src->bytes_stored -= len;
return 0;
}
// Not a coroutine function but used by Fail/Throw-like effects.
//
// Calls `body(env)` in a context where any wrapper that calls
// `mco_abort_longjmp(buf, val)` will unwind back to this call and
// `mco_abort_call` will return `val`. Returns 0 if body completes normally.
//
// `buf` must point to at least `sizeof(jmp_buf)` bytes.
int mco_abort_call(void* buf, void (*body)(void* env), void* env) {
int v = setjmp(*(jmp_buf*)buf);
if (v == 0) {
body(env);
return 0;
}
return v;
}
// Unwind back to the matching `mco_abort_call`, setting its return value to
// `val`. Never returns.
void mco_abort_longjmp(void* buf, int val) {
longjmp(*(jmp_buf*)buf, val);
}