Skip to content

Commit 2c9930a

Browse files
committed
Combine yield functions
This patch takes advantage of the arguments to the yield functions being in the same order.
1 parent bb90359 commit 2c9930a

File tree

1 file changed

+18
-69
lines changed

1 file changed

+18
-69
lines changed

coroutine.c

Lines changed: 18 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static void* setup_stack(void* stack, void (*f)(void*), void* arg)
121121
// Linux x86_64 call convention
122122
// %rdi, %rsi, %rdx, %rcx, %r8, and %r9
123123

124-
void __attribute__((naked)) coroutine_yield(void)
124+
static void __attribute__((naked)) yield(Sleep_Mode sm, int fd)
125125
{
126126
// @arch
127127
#if __x86_64__ && __linux__
@@ -133,8 +133,9 @@ void __attribute__((naked)) coroutine_yield(void)
133133
" pushq %r13\n"
134134
" pushq %r14\n"
135135
" 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
138139
" jmp coroutine_switch_context\n");
139140
#elif __x86_64__ && __APPLE__
140141
asm(
@@ -145,80 +146,28 @@ void __attribute__((naked)) coroutine_yield(void)
145146
" pushq %r13\n"
146147
" pushq %r14\n"
147148
" 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
150152
" jmp _coroutine_switch_context\n");
151153
#else
152-
#error weird cpu/os combo
154+
#error unsupported cpu/os combo
153155
#endif
154156
}
155157

156-
void __attribute__((naked)) coroutine_sleep_read(int fd)
158+
void coroutine_yield(void)
157159
{
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);
188161
}
189162

190-
void __attribute__((naked)) coroutine_sleep_write(int fd)
163+
void coroutine_sleep_read(int fd)
191164
{
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);
222171
}
223172

224173
void __attribute__((naked)) coroutine_restore_context(void *rsp)
@@ -251,7 +200,7 @@ void __attribute__((naked)) coroutine_restore_context(void *rsp)
251200
#endif
252201
}
253202

254-
void coroutine_switch_context(void *rsp, Sleep_Mode sm, int fd)
203+
void coroutine_switch_context(Sleep_Mode sm, int fd, void* rsp)
255204
{
256205
contexts.items[active.items[current]].rsp = rsp;
257206

0 commit comments

Comments
 (0)