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
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Ali Mohammad Pur <ali.mpfard@gmail.com>
Date: Fri, 11 Feb 2022 16:37:54 +0330
Subject: [PATCH] Make coroutines a noop
Serenity doesn't have ucontext.
---
src/coroutine.c | 41 +++++++++++++++++++++++++++++++++++++++--
1 file changed, 39 insertions(+), 2 deletions(-)
diff --git a/src/coroutine.c b/src/coroutine.c
index 8497869..8215cfa 100644
--- a/src/coroutine.c
+++ b/src/coroutine.c
@@ -8,7 +8,7 @@
#if __APPLE__ && __MACH__
# include <sys/ucontext.h>
-#else
+#elif !defined(__serenity__)
# include <ucontext.h>
#endif
@@ -17,6 +17,7 @@
struct coroutine;
+#ifndef __serenity__
struct schedule {
char stack[STACK_SIZE];
ucontext_t main;
@@ -36,9 +37,13 @@ struct coroutine {
int status;
char* stack;
};
+#endif
struct coroutine* _co_new(struct schedule* S, coroutine_func func, void* ud)
{
+#ifdef __serenity__
+ return NULL;
+#else
struct coroutine* co = malloc(sizeof(*co));
co->func = func;
co->ud = ud;
@@ -48,16 +53,20 @@ struct coroutine* _co_new(struct schedule* S, coroutine_func func, void* ud)
co->status = COROUTINE_READY;
co->stack = NULL;
return co;
+#endif
}
void _co_delete(struct coroutine* co)
{
+#ifndef __serenity__
free(co->stack);
free(co);
+#endif
}
struct schedule* coroutine_open(void)
{
+#ifndef __serenity__
struct schedule* S = malloc(sizeof(*S));
S->nco = 0;
S->cap = DEFAULT_COROUTINE;
@@ -65,10 +74,14 @@ struct schedule* coroutine_open(void)
S->co = malloc(sizeof(struct coroutine*) * S->cap);
memset(S->co, 0, sizeof(struct coroutine*) * S->cap);
return S;
+#else
+ return NULL;
+#endif
}
void coroutine_close(struct schedule* S)
{
+#ifndef __serenity__
int i;
for (i = 0; i < S->cap; i++) {
struct coroutine* co = S->co[i];
@@ -79,10 +92,12 @@ void coroutine_close(struct schedule* S)
free(S->co);
S->co = NULL;
free(S);
+#endif
}
int coroutine_new(struct schedule* S, coroutine_func func, void* ud)
{
+#ifndef __serenity__
struct coroutine* co = _co_new(S, func, ud);
if (S->nco >= S->cap) {
int id = S->cap;
@@ -105,10 +120,14 @@ int coroutine_new(struct schedule* S, coroutine_func func, void* ud)
}
assert(0);
return -1;
+#else
+ return 0;
+#endif
}
static void mainfunc(uint32_t low32, uint32_t hi32)
{
+#ifndef __serenity__
uintptr_t ptr = (uintptr_t)low32 | ((uintptr_t)hi32 << 32);
struct schedule* S = (struct schedule*)ptr;
int id = S->running;
@@ -118,10 +137,12 @@ static void mainfunc(uint32_t low32, uint32_t hi32)
S->co[id] = NULL;
--S->nco;
S->running = -1;
+#endif
}
void coroutine_resume(struct schedule* S, int id)
{
+#ifndef __serenity__
assert(S->running == -1);
assert(id >= 0 && id < S->cap);
struct coroutine* C = S->co[id];
@@ -150,8 +171,10 @@ void coroutine_resume(struct schedule* S, int id)
default:
assert(0);
}
+#endif
}
+#ifndef __serenity__
static void _save_stack(struct coroutine* C, char* top)
{
char dummy = 0;
@@ -164,9 +187,11 @@ static void _save_stack(struct coroutine* C, char* top)
C->size = top - &dummy;
memcpy(C->stack, &dummy, C->size);
}
+#endif
void coroutine_yield(struct schedule* S)
{
+#ifndef __serenity__
int id = S->running;
assert(id >= 0);
struct coroutine* C = S->co[id];
@@ -175,15 +200,27 @@ void coroutine_yield(struct schedule* S)
C->status = COROUTINE_SUSPEND;
S->running = -1;
swapcontext(&C->ctx, &S->main);
+#endif
}
int coroutine_status(struct schedule* S, int id)
{
+#ifndef __serenity__
assert(id >= 0 && id < S->cap);
if (S->co[id] == NULL) {
return COROUTINE_DEAD;
}
return S->co[id]->status;
+#else
+ return COROUTINE_DEAD;
+#endif
}
-int coroutine_running(struct schedule* S) { return S->running; }
+int coroutine_running(struct schedule* S)
+{
+#ifndef __serenity__
+ return S->running;
+#else
+ return 0;
+#endif
+}
|