@@ -121,7 +121,7 @@ static void* setup_stack(void* stack, void (*f)(void*), void* arg)
121
121
// Linux x86_64 call convention
122
122
// %rdi, %rsi, %rdx, %rcx, %r8, and %r9
123
123
124
- void __attribute__((naked )) coroutine_yield ( void )
124
+ static void __attribute__((naked )) yield ( Sleep_Mode sm , int fd )
125
125
{
126
126
// @arch
127
127
#if __x86_64__ && __linux__
@@ -133,8 +133,9 @@ void __attribute__((naked)) coroutine_yield(void)
133
133
" pushq %r13\n"
134
134
" pushq %r14\n"
135
135
" pushq %r15\n"
136
- " movq %rsp, %rdi\n" // rsp
137
- " movq $0, %rsi\n" // sm = SM_NONE
136
+ " movq %rsp, %rdx\n" // rsp
137
+ // " movq %rsi, %rsi\n" // fd
138
+ // " movq %rdi, %rdi\n" // sm
138
139
" jmp coroutine_switch_context\n" );
139
140
#elif __x86_64__ && __APPLE__
140
141
asm(
@@ -145,80 +146,28 @@ void __attribute__((naked)) coroutine_yield(void)
145
146
" pushq %r13\n"
146
147
" pushq %r14\n"
147
148
" pushq %r15\n"
148
- " movq %rsp, %rdi\n" // rsp
149
- " movq $0, %rsi\n" // sm = SM_NONE
149
+ " movq %rsp, %rdx\n" // rsp
150
+ // " movq %rsi, %rsi\n" // fd
151
+ // " movq %rdi, %rdi\n" // sm
150
152
" jmp _coroutine_switch_context\n" );
151
153
#else
152
- #error weird cpu/os combo
154
+ #error unsupported cpu/os combo
153
155
#endif
154
156
}
155
157
156
- void __attribute__(( naked )) coroutine_sleep_read ( int fd )
158
+ void coroutine_yield ( void )
157
159
{
158
- // @arch
159
- #if __x86_64__ && __linux__
160
- asm(
161
- " pushq %rdi\n"
162
- " pushq %rbp\n"
163
- " pushq %rbx\n"
164
- " pushq %r12\n"
165
- " pushq %r13\n"
166
- " pushq %r14\n"
167
- " pushq %r15\n"
168
- " movq %rdi, %rdx\n" // fd
169
- " movq %rsp, %rdi\n" // rsp
170
- " movq $1, %rsi\n" // sm = SM_READ
171
- " jmp coroutine_switch_context\n" );
172
- #elif __x86_64__ && __APPLE__
173
- asm(
174
- " pushq %rdi\n"
175
- " pushq %rbp\n"
176
- " pushq %rbx\n"
177
- " pushq %r12\n"
178
- " pushq %r13\n"
179
- " pushq %r14\n"
180
- " pushq %r15\n"
181
- " movq %rdi, %rdx\n" // fd
182
- " movq %rsp, %rdi\n" // rsp
183
- " movq $1, %rsi\n" // sm = SM_READ
184
- " jmp _coroutine_switch_context\n" );
185
- #else
186
- #error weird cpu/os combo
187
- #endif
160
+ yield (SM_NONE , -1 );
188
161
}
189
162
190
- void __attribute__(( naked )) coroutine_sleep_write (int fd )
163
+ void coroutine_sleep_read (int fd )
191
164
{
192
- // @arch
193
- #if __x86_64__ && __linux__
194
- asm(
195
- " pushq %rdi\n"
196
- " pushq %rbp\n"
197
- " pushq %rbx\n"
198
- " pushq %r12\n"
199
- " pushq %r13\n"
200
- " pushq %r14\n"
201
- " pushq %r15\n"
202
- " movq %rdi, %rdx\n" // fd
203
- " movq %rsp, %rdi\n" // rsp
204
- " movq $2, %rsi\n" // sm = SM_WRITE
205
- " jmp coroutine_switch_context\n" );
206
- #elif __x86_64__ && __APPLE__
207
- asm(
208
- " pushq %rdi\n"
209
- " pushq %rbp\n"
210
- " pushq %rbx\n"
211
- " pushq %r12\n"
212
- " pushq %r13\n"
213
- " pushq %r14\n"
214
- " pushq %r15\n"
215
- " movq %rdi, %rdx\n" // fd
216
- " movq %rsp, %rdi\n" // rsp
217
- " movq $2, %rsi\n" // sm = SM_WRITE
218
- " jmp _coroutine_switch_context\n" );
219
- #else
220
- #error weird cpu/os combo
221
- #endif
165
+ yield (SM_READ , fd );
166
+ }
167
+
168
+ void coroutine_sleep_write (int fd )
169
+ {
170
+ yield (SM_WRITE , fd );
222
171
}
223
172
224
173
void __attribute__((naked )) coroutine_restore_context (void * rsp )
@@ -251,7 +200,7 @@ void __attribute__((naked)) coroutine_restore_context(void *rsp)
251
200
#endif
252
201
}
253
202
254
- void coroutine_switch_context (void * rsp , Sleep_Mode sm , int fd )
203
+ void coroutine_switch_context (Sleep_Mode sm , int fd , void * rsp )
255
204
{
256
205
contexts .items [active .items [current ]].rsp = rsp ;
257
206
0 commit comments