ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libcoro/coro.c
(Generate patch)

Comparing libcoro/coro.c (file contents):
Revision 1.35 by root, Mon Nov 3 16:05:38 2008 UTC vs.
Revision 1.73 by root, Tue Aug 14 15:46:03 2018 UTC

1/* 1/*
2 * Copyright (c) 2001-2008 Marc Alexander Lehmann <schmorp@schmorp.de> 2 * Copyright (c) 2001-2011 Marc Alexander Lehmann <schmorp@schmorp.de>
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without modifica- 4 * Redistribution and use in source and binary forms, with or without modifica-
5 * tion, are permitted provided that the following conditions are met: 5 * tion, are permitted provided that the following conditions are met:
6 * 6 *
7 * 1. Redistributions of source code must retain the above copyright notice, 7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer. 8 * this list of conditions and the following disclaimer.
9 * 9 *
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the 11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 12 * documentation and/or other materials provided with the distribution.
13 * 13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- 15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
16 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 16 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- 17 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
18 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 18 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
38 * go to Ralf S. Engelschall <rse@engelschall.com>. 38 * go to Ralf S. Engelschall <rse@engelschall.com>.
39 */ 39 */
40 40
41#include "coro.h" 41#include "coro.h"
42 42
43#include <stddef.h>
44#include <string.h>
45
46/*****************************************************************************/
47/* ucontext/setjmp/asm backends */
48/*****************************************************************************/
49#if CORO_UCONTEXT || CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX || CORO_ASM
50
51# if CORO_UCONTEXT
52# include <stddef.h>
53# endif
54
43#if !defined(STACK_ADJUST_PTR) 55# if !defined(STACK_ADJUST_PTR)
56# if __sgi
44/* IRIX is decidedly NON-unix */ 57/* IRIX is decidedly NON-unix */
45# if __sgi
46# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8) 58# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8)
47# define STACK_ADJUST_SIZE(sp,ss) ((ss) - 8) 59# define STACK_ADJUST_SIZE(sp,ss) ((ss) - 8)
48# elif (__i386__ && CORO_LINUX) || (_M_IX86 && CORO_LOSER) 60# elif (__i386__ && CORO_LINUX) || (_M_IX86 && CORO_LOSER)
49# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss)) 61# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss))
50# define STACK_ADJUST_SIZE(sp,ss) (ss) 62# define STACK_ADJUST_SIZE(sp,ss) (ss)
51# elif (__amd64__ && CORO_LINUX) || ((_M_AMD64 || _M_IA64) && CORO_LOSER) 63# elif (__amd64__ && CORO_LINUX) || ((_M_AMD64 || _M_IA64) && CORO_LOSER)
52# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8) 64# define STACK_ADJUST_PTR(sp,ss) ((char *)(sp) + (ss) - 8)
53# define STACK_ADJUST_SIZE(sp,ss) (ss) 65# define STACK_ADJUST_SIZE(sp,ss) (ss)
54# else 66# else
55# define STACK_ADJUST_PTR(sp,ss) (sp) 67# define STACK_ADJUST_PTR(sp,ss) (sp)
56# define STACK_ADJUST_SIZE(sp,ss) (ss) 68# define STACK_ADJUST_SIZE(sp,ss) (ss)
69# endif
57# endif 70# endif
58#endif
59 71
60#if CORO_UCONTEXT
61# include <stddef.h>
62#endif
63
64#if CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX || CORO_ASM
65
66#include <stdlib.h> 72# include <stdlib.h>
67 73
68#if CORO_SJLJ 74# if CORO_SJLJ
69# include <stdio.h> 75# include <stdio.h>
70# include <signal.h> 76# include <signal.h>
71# include <unistd.h> 77# include <unistd.h>
72#endif 78# endif
73 79
74static volatile coro_func coro_init_func; 80static coro_func coro_init_func;
75static volatile void *coro_init_arg; 81static void *coro_init_arg;
76static volatile coro_context *new_coro, *create_coro; 82static coro_context *new_coro, *create_coro;
77
78/* what we really want to detect here is wether we use a new-enough version of GAS */
79/* instead, check for gcc 3, ELF and GNU/Linux and hope for the best */
80#if __GNUC__ >= 3 && __ELF__ && __linux__
81# define HAVE_CFI 1
82#endif
83 83
84static void 84static void
85coro_init (void) 85coro_init (void)
86{ 86{
87 volatile coro_func func = coro_init_func; 87 volatile coro_func func = coro_init_func;
88 volatile void *arg = coro_init_arg; 88 volatile void *arg = coro_init_arg;
89 89
90 coro_transfer ((coro_context *)new_coro, (coro_context *)create_coro); 90 coro_transfer (new_coro, create_coro);
91
92#if __GCC_HAVE_DWARF2_CFI_ASM && __amd64
93 /*asm (".cfi_startproc");*/
94 /*asm (".cfi_undefined rip");*/
95#endif
91 96
92 func ((void *)arg); 97 func ((void *)arg);
98
99#if __GCC_HAVE_DWARF2_CFI_ASM && __amd64
100 /*asm (".cfi_endproc");*/
101#endif
93 102
94 /* the new coro returned. bad. just abort() for now */ 103 /* the new coro returned. bad. just abort() for now */
95 abort (); 104 abort ();
96} 105}
97 106
98# if CORO_SJLJ 107# if CORO_SJLJ
99 108
100static volatile int trampoline_count; 109static volatile int trampoline_done;
101 110
102/* trampoline signal handler */ 111/* trampoline signal handler */
103static void 112static void
104trampoline (int sig) 113trampoline (int sig)
105{ 114{
106 if (setjmp (((coro_context *)new_coro)->env)) 115 if (coro_setjmp (new_coro->env))
107 {
108#if HAVE_CFI
109 asm (".cfi_startproc");
110#endif
111 coro_init (); /* start it */ 116 coro_init (); /* start it */
112#if HAVE_CFI
113 asm (".cfi_endproc");
114#endif
115 }
116 else 117 else
117 trampoline_count++; 118 trampoline_done = 1;
118} 119}
119 120
120# endif 121# endif
121 122
122#endif
123
124#if CORO_ASM 123# if CORO_ASM
124
125 #if __arm__ && \
126 (defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
127 || defined __ARM_ARCH_7R__ || defined __ARM_ARCH_7M__ \
128 || __ARM_ARCH == 7)
129 #define CORO_ARM 1
130 #endif
131
132 #if _WIN32 || __CYGWIN__
133 #define CORO_WIN_TIB 1
134 #endif
135
125asm ( 136 asm (
126 ".text\n" 137 "\t.text\n"
138 #if _WIN32 || __CYGWIN__
139 "\t.globl _coro_transfer\n"
140 "_coro_transfer:\n"
141 #else
127 ".globl coro_transfer\n" 142 "\t.globl coro_transfer\n"
128 ".type coro_transfer, @function\n"
129 "coro_transfer:\n" 143 "coro_transfer:\n"
144 #endif
145 /* windows, of course, gives a shit on the amd64 ABI and uses different registers */
146 /* http://blogs.msdn.com/freik/archive/2005/03/17/398200.aspx */
130#if __amd64 147 #if __amd64
131# define NUM_SAVED 5 148
149 #if _WIN32 || __CYGWIN__
150 #define NUM_SAVED 29
151 "\tsubq $168, %rsp\t" /* one dummy qword to improve alignment */
152 "\tmovaps %xmm6, (%rsp)\n"
153 "\tmovaps %xmm7, 16(%rsp)\n"
154 "\tmovaps %xmm8, 32(%rsp)\n"
155 "\tmovaps %xmm9, 48(%rsp)\n"
156 "\tmovaps %xmm10, 64(%rsp)\n"
157 "\tmovaps %xmm11, 80(%rsp)\n"
158 "\tmovaps %xmm12, 96(%rsp)\n"
159 "\tmovaps %xmm13, 112(%rsp)\n"
160 "\tmovaps %xmm14, 128(%rsp)\n"
161 "\tmovaps %xmm15, 144(%rsp)\n"
162 "\tpushq %rsi\n"
163 "\tpushq %rdi\n"
164 "\tpushq %rbp\n"
132 "\tpush %rbx\n" 165 "\tpushq %rbx\n"
133 "\tpush %r12\n" 166 "\tpushq %r12\n"
134 "\tpush %r13\n" 167 "\tpushq %r13\n"
135 "\tpush %r14\n" 168 "\tpushq %r14\n"
136 "\tpush %r15\n" 169 "\tpushq %r15\n"
170 #if CORO_WIN_TIB
171 "\tpushq %fs:0x0\n"
172 "\tpushq %fs:0x8\n"
173 "\tpushq %fs:0xc\n"
174 #endif
175 "\tmovq %rsp, (%rcx)\n"
176 "\tmovq (%rdx), %rsp\n"
177 #if CORO_WIN_TIB
178 "\tpopq %fs:0xc\n"
179 "\tpopq %fs:0x8\n"
180 "\tpopq %fs:0x0\n"
181 #endif
182 "\tpopq %r15\n"
183 "\tpopq %r14\n"
184 "\tpopq %r13\n"
185 "\tpopq %r12\n"
186 "\tpopq %rbx\n"
187 "\tpopq %rbp\n"
188 "\tpopq %rdi\n"
189 "\tpopq %rsi\n"
190 "\tmovaps (%rsp), %xmm6\n"
191 "\tmovaps 16(%rsp), %xmm7\n"
192 "\tmovaps 32(%rsp), %xmm8\n"
193 "\tmovaps 48(%rsp), %xmm9\n"
194 "\tmovaps 64(%rsp), %xmm10\n"
195 "\tmovaps 80(%rsp), %xmm11\n"
196 "\tmovaps 96(%rsp), %xmm12\n"
197 "\tmovaps 112(%rsp), %xmm13\n"
198 "\tmovaps 128(%rsp), %xmm14\n"
199 "\tmovaps 144(%rsp), %xmm15\n"
200 "\taddq $168, %rsp\n"
201 #else
202 #define NUM_SAVED 6
203 "\tpushq %rbp\n"
204 "\tpushq %rbx\n"
205 "\tpushq %r12\n"
206 "\tpushq %r13\n"
207 "\tpushq %r14\n"
208 "\tpushq %r15\n"
137 "\tmov %rsp, (%rdi)\n" 209 "\tmovq %rsp, (%rdi)\n"
138 "\tmov (%rsi), %rsp\n" 210 "\tmovq (%rsi), %rsp\n"
139 "\tpop %r15\n" 211 "\tpopq %r15\n"
140 "\tpop %r14\n" 212 "\tpopq %r14\n"
141 "\tpop %r13\n" 213 "\tpopq %r13\n"
142 "\tpop %r12\n" 214 "\tpopq %r12\n"
143 "\tpop %rbx\n" 215 "\tpopq %rbx\n"
144#elif __i386 216 "\tpopq %rbp\n"
217 #endif
218 "\tpopq %rcx\n"
219 "\tjmpq *%rcx\n"
220
221 #elif __i386__
222
145# define NUM_SAVED 4 223 #define NUM_SAVED 4
146 "\tpush %ebx\n"
147 "\tpush %esi\n"
148 "\tpush %edi\n"
149 "\tpush %ebp\n" 224 "\tpushl %ebp\n"
225 "\tpushl %ebx\n"
226 "\tpushl %esi\n"
227 "\tpushl %edi\n"
228 #if CORO_WIN_TIB
229 #undef NUM_SAVED
230 #define NUM_SAVED 7
231 "\tpushl %fs:0\n"
232 "\tpushl %fs:4\n"
233 "\tpushl %fs:8\n"
234 #endif
150 "\tmov %esp, (%eax)\n" 235 "\tmovl %esp, (%eax)\n"
151 "\tmov (%edx), %esp\n" 236 "\tmovl (%edx), %esp\n"
152 "\tpop %ebp\n" 237 #if CORO_WIN_TIB
238 "\tpopl %fs:8\n"
239 "\tpopl %fs:4\n"
240 "\tpopl %fs:0\n"
241 #endif
153 "\tpop %edi\n" 242 "\tpopl %edi\n"
154 "\tpop %esi\n" 243 "\tpopl %esi\n"
155 "\tpop %ebx\n" 244 "\tpopl %ebx\n"
156#else 245 "\tpopl %ebp\n"
246 "\tpopl %ecx\n"
247 "\tjmpl *%ecx\n"
248
249 #elif CORO_ARM /* untested, what about thumb, neon, iwmmxt? */
250
251 #if __ARM_PCS_VFP
252 "\tvpush {d8-d15}\n"
253 #define NUM_SAVED (9 + 8 * 2)
254 #else
255 #define NUM_SAVED 9
256 #endif
257 "\tpush {r4-r11,lr}\n"
258 "\tstr sp, [r0]\n"
259 "\tldr sp, [r1]\n"
260 "\tpop {r4-r11,lr}\n"
261 #if __ARM_PCS_VFP
262 "\tvpop {d8-d15}\n"
263 #endif
264 "\tmov r15, lr\n"
265
266 #elif __mips__ && 0 /* untested, 32 bit only */
267
268 #define NUM_SAVED (12 + 8 * 2)
269 /* TODO: n64/o64, lw=>ld */
270
271 "\t.set nomips16\n"
272 "\t.frame $sp,112,$31\n"
273 #if __mips_soft_float
274 "\taddiu $sp,$sp,-44\n"
275 #else
276 "\taddiu $sp,$sp,-112\n"
277 "\ts.d $f30,88($sp)\n"
278 "\ts.d $f28,80($sp)\n"
279 "\ts.d $f26,72($sp)\n"
280 "\ts.d $f24,64($sp)\n"
281 "\ts.d $f22,56($sp)\n"
282 "\ts.d $f20,48($sp)\n"
283 #endif
284 "\tsw $28,40($sp)\n"
285 "\tsw $31,36($sp)\n"
286 "\tsw $fp,32($sp)\n"
287 "\tsw $23,28($sp)\n"
288 "\tsw $22,24($sp)\n"
289 "\tsw $21,20($sp)\n"
290 "\tsw $20,16($sp)\n"
291 "\tsw $19,12($sp)\n"
292 "\tsw $18,8($sp)\n"
293 "\tsw $17,4($sp)\n"
294 "\tsw $16,0($sp)\n"
295 "\tsw $sp,0($4)\n"
296 "\tlw $sp,0($5)\n"
297 #if !__mips_soft_float
298 "\tl.d $f30,88($sp)\n"
299 "\tl.d $f28,80($sp)\n"
300 "\tl.d $f26,72($sp)\n"
301 "\tl.d $f24,64($sp)\n"
302 "\tl.d $f22,56($sp)\n"
303 "\tl.d $f20,48($sp)\n"
304 #endif
305 "\tlw $28,40($sp)\n"
306 "\tlw $31,36($sp)\n"
307 "\tlw $fp,32($sp)\n"
308 "\tlw $23,28($sp)\n"
309 "\tlw $22,24($sp)\n"
310 "\tlw $21,20($sp)\n"
311 "\tlw $20,16($sp)\n"
312 "\tlw $19,12($sp)\n"
313 "\tlw $18,8($sp)\n"
314 "\tlw $17,4($sp)\n"
315 "\tlw $16,0($sp)\n"
316 "\tj $31\n"
317 #if __mips_soft_float
318 "\taddiu $sp,$sp,44\n"
319 #else
320 "\taddiu $sp,$sp,112\n"
321 #endif
322
323 #else
157# error unsupported architecture 324 #error unsupported architecture
325 #endif
326 );
327
158#endif 328# endif
159 "\tret\n"
160);
161#endif
162 329
163#if CORO_PTHREAD 330void
164 331coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, size_t ssize)
165struct coro_init_args {
166 coro_func func;
167 void *arg;
168 coro_context *self, *main;
169};
170
171pthread_mutex_t coro_mutex = PTHREAD_MUTEX_INITIALIZER;
172
173static void *
174trampoline (void *args_)
175{ 332{
176 struct coro_init_args *args = (struct coro_init_args *)args_; 333 coro_context nctx;
177 coro_func func = args->func;
178 void *arg = args->arg;
179
180 pthread_mutex_lock (&coro_mutex);
181 pthread_cond_destroy (&args->self->c);
182 coro_transfer (args->self, args->main);
183 func (arg);
184 pthread_mutex_unlock (&coro_mutex);
185
186 return 0;
187}
188
189asm("");
190
191void coro_transfer(coro_context *prev, coro_context *next)
192{
193 pthread_cond_init (&prev->c, 0);
194 pthread_cond_signal (&next->c);
195 pthread_cond_wait (&prev->c, &coro_mutex);
196 pthread_cond_destroy (&prev->c);
197}
198
199#endif
200
201/* initialize a machine state */
202void coro_create (coro_context *ctx,
203 coro_func coro, void *arg,
204 void *sptr, long ssize)
205{
206#if CORO_UCONTEXT
207
208 getcontext (&(ctx->uc));
209
210 ctx->uc.uc_link = 0;
211 ctx->uc.uc_stack.ss_sp = STACK_ADJUST_PTR (sptr,ssize);
212 ctx->uc.uc_stack.ss_size = (size_t) STACK_ADJUST_SIZE (sptr,ssize);
213 ctx->uc.uc_stack.ss_flags = 0;
214
215 makecontext (&(ctx->uc), (void (*)()) coro, 1, arg);
216
217#elif CORO_SJLJ || CORO_LOSER || CORO_LINUX || CORO_IRIX || CORO_ASM
218
219# if CORO_SJLJ 334# if CORO_SJLJ
220 stack_t ostk, nstk; 335 stack_t ostk, nstk;
221 struct sigaction osa, nsa; 336 struct sigaction osa, nsa;
222 sigset_t nsig, osig; 337 sigset_t nsig, osig;
223# endif 338# endif
224 coro_context nctx; 339
340 if (!coro)
341 return;
225 342
226 coro_init_func = coro; 343 coro_init_func = coro;
227 coro_init_arg = arg; 344 coro_init_arg = arg;
228 345
229 new_coro = ctx; 346 new_coro = ctx;
245 perror ("sigaction"); 362 perror ("sigaction");
246 abort (); 363 abort ();
247 } 364 }
248 365
249 /* set the new stack */ 366 /* set the new stack */
250 nstk.ss_sp = STACK_ADJUST_PTR (sptr,ssize); /* yes, some platforms (IRIX) get this wrong. */ 367 nstk.ss_sp = STACK_ADJUST_PTR (sptr, ssize); /* yes, some platforms (IRIX) get this wrong. */
251 nstk.ss_size = STACK_ADJUST_SIZE (sptr,ssize); 368 nstk.ss_size = STACK_ADJUST_SIZE (sptr, ssize);
252 nstk.ss_flags = 0; 369 nstk.ss_flags = 0;
253 370
254 if (sigaltstack (&nstk, &ostk) < 0) 371 if (sigaltstack (&nstk, &ostk) < 0)
255 { 372 {
256 perror ("sigaltstack"); 373 perror ("sigaltstack");
257 abort (); 374 abort ();
258 } 375 }
259 376
260 trampoline_count = 0; 377 trampoline_done = 0;
261 kill (getpid (), SIGUSR2); 378 kill (getpid (), SIGUSR2);
262 sigfillset (&nsig); sigdelset (&nsig, SIGUSR2); 379 sigfillset (&nsig); sigdelset (&nsig, SIGUSR2);
263 380
264 while (!trampoline_count) 381 while (!trampoline_done)
265 sigsuspend (&nsig); 382 sigsuspend (&nsig);
266 383
267 sigaltstack (0, &nstk); 384 sigaltstack (0, &nstk);
268 nstk.ss_flags = SS_DISABLE; 385 nstk.ss_flags = SS_DISABLE;
269 if (sigaltstack (&nstk, 0) < 0) 386 if (sigaltstack (&nstk, 0) < 0)
275 392
276 if (~ostk.ss_flags & SS_DISABLE) 393 if (~ostk.ss_flags & SS_DISABLE)
277 sigaltstack (&ostk, 0); 394 sigaltstack (&ostk, 0);
278 395
279 sigaction (SIGUSR2, &osa, 0); 396 sigaction (SIGUSR2, &osa, 0);
280
281 sigprocmask (SIG_SETMASK, &osig, 0); 397 sigprocmask (SIG_SETMASK, &osig, 0);
282 398
283# elif CORO_LOSER 399# elif CORO_LOSER
284 400
285 setjmp (ctx->env); 401 coro_setjmp (ctx->env);
286#if __CYGWIN__ 402 #if __CYGWIN__ && __i386__
287 ctx->env[7] = (long)((char *)sptr + ssize); 403 ctx->env[8] = (long) coro_init;
288 ctx->env[8] = (long)coro_init; 404 ctx->env[7] = (long) ((char *)sptr + ssize) - sizeof (long);
405 #elif __CYGWIN__ && __x86_64__
406 ctx->env[7] = (long) coro_init;
407 ctx->env[6] = (long) ((char *)sptr + ssize) - sizeof (long);
408 #elif defined __MINGW32__
409 ctx->env[5] = (long) coro_init;
410 ctx->env[4] = (long) ((char *)sptr + ssize) - sizeof (long);
289#elif defined(_M_IX86) 411 #elif defined _M_IX86
290 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long)coro_init; 412 ((_JUMP_BUFFER *)&ctx->env)->Eip = (long) coro_init;
291 ((_JUMP_BUFFER *)&ctx->env)->Esp = (long)STACK_ADJUST_PTR (sptr, ssize); 413 ((_JUMP_BUFFER *)&ctx->env)->Esp = (long) STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
292#elif defined(_M_AMD64) 414 #elif defined _M_AMD64
293 ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64)coro_init; 415 ((_JUMP_BUFFER *)&ctx->env)->Rip = (__int64) coro_init;
294 ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64)STACK_ADJUST_PTR (sptr, ssize); 416 ((_JUMP_BUFFER *)&ctx->env)->Rsp = (__int64) STACK_ADJUST_PTR (sptr, ssize) - sizeof (__int64);
295#elif defined(_M_IA64) 417 #elif defined _M_IA64
296 ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64)coro_init; 418 ((_JUMP_BUFFER *)&ctx->env)->StIIP = (__int64) coro_init;
297 ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64)STACK_ADJUST_PTR (sptr, ssize); 419 ((_JUMP_BUFFER *)&ctx->env)->IntSp = (__int64) STACK_ADJUST_PTR (sptr, ssize) - sizeof (__int64);
298#else 420 #else
299# error "microsoft libc or architecture not supported" 421 #error "microsoft libc or architecture not supported"
300#endif 422 #endif
301 423
302# elif CORO_LINUX 424# elif CORO_LINUX
303 425
304 _setjmp (ctx->env); 426 coro_setjmp (ctx->env);
305#if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP) 427 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (JB_PC) && defined (JB_SP)
306 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; 428 ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init;
307 ctx->env[0].__jmpbuf[JB_SP] = (long)STACK_ADJUST_PTR (sptr, ssize); 429 ctx->env[0].__jmpbuf[JB_SP] = (long) STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
308#elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__) 430 #elif __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 0 && defined (__mc68000__)
309 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; 431 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init;
310 ctx->env[0].__jmpbuf[0].__sp = (int *)((char *)sptr + ssize); 432 ctx->env[0].__jmpbuf[0].__sp = (int *) ((char *)sptr + ssize) - sizeof (long);
311#elif defined (__GNU_LIBRARY__) && defined (__i386__) 433 #elif defined (__GNU_LIBRARY__) && defined (__i386__)
312 ctx->env[0].__jmpbuf[0].__pc = (char *)coro_init; 434 ctx->env[0].__jmpbuf[0].__pc = (char *) coro_init;
313 ctx->env[0].__jmpbuf[0].__sp = (void *)((char *)sptr + ssize); 435 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long);
314#elif defined (__GNU_LIBRARY__) && defined (__amd64__) 436 #elif defined (__GNU_LIBRARY__) && defined (__x86_64__)
315 ctx->env[0].__jmpbuf[JB_PC] = (long)coro_init; 437 ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init;
316 ctx->env[0].__jmpbuf[JB_RSP] = (long)STACK_ADJUST_PTR (sptr, ssize); 438 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long);
317#else 439 #else
318# error "linux libc or architecture not supported" 440 #error "linux libc or architecture not supported"
319#endif 441 #endif
320 442
321# elif CORO_IRIX 443# elif CORO_IRIX
322 444
323 setjmp (ctx->env); 445 coro_setjmp (ctx->env, 0);
324 ctx->env[JB_PC] = (__uint64_t)coro_init; 446 ctx->env[JB_PC] = (__uint64_t)coro_init;
325 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize); 447 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
326 448
327# elif CORO_ASM 449# elif CORO_ASM
328 450
451 #if __i386__ || __x86_64__
329 ctx->sp = (volatile void **)(ssize + (char *)sptr); 452 ctx->sp = (void **)(ssize + (char *)sptr);
330 /* we try to allow for both functions with and without frame pointers */ 453 *--ctx->sp = (void *)abort; /* needed for alignment only */
331 *--ctx->sp = (void *)coro_init; 454 *--ctx->sp = (void *)coro_init;
332 { 455 #if CORO_WIN_TIB
333 int i; 456 *--ctx->sp = 0; /* ExceptionList */
334 for (i = NUM_SAVED; i--; ) 457 *--ctx->sp = (char *)sptr + ssize; /* StackBase */
335 *--ctx->sp = 0; 458 *--ctx->sp = sptr; /* StackLimit */
336 } 459 #endif
460 #elif CORO_ARM
461 /* return address stored in lr register, don't push anything */
462 #else
463 #error unsupported architecture
464 #endif
465
466 ctx->sp -= NUM_SAVED;
467 memset (ctx->sp, 0, sizeof (*ctx->sp) * NUM_SAVED);
468
469 #if __i386__ || __x86_64__
470 /* done already */
471 #elif CORO_ARM
472 ctx->sp[0] = coro; /* r4 */
473 ctx->sp[1] = arg; /* r5 */
474 ctx->sp[8] = (char *)coro_init; /* lr */
475 #else
476 #error unsupported architecture
477 #endif
478
479# elif CORO_UCONTEXT
480
481 getcontext (&(ctx->uc));
482
483 ctx->uc.uc_link = 0;
484 ctx->uc.uc_stack.ss_sp = sptr;
485 ctx->uc.uc_stack.ss_size = (size_t)ssize;
486 ctx->uc.uc_stack.ss_flags = 0;
487
488 makecontext (&(ctx->uc), (void (*)())coro_init, 0);
337 489
338# endif 490# endif
339 491
340 coro_transfer ((coro_context *)create_coro, (coro_context *)new_coro); 492 coro_transfer (create_coro, new_coro);
493}
341 494
495/*****************************************************************************/
496/* pthread backend */
497/*****************************************************************************/
342# elif CORO_PTHREAD 498#elif CORO_PTHREAD
343 499
344 pthread_t id; 500/* this mutex will be locked by the running coroutine */
345 pthread_attr_t attr; 501pthread_mutex_t coro_mutex = PTHREAD_MUTEX_INITIALIZER;
502
503struct coro_init_args
504{
505 coro_func func;
506 void *arg;
507 coro_context *self, *main;
508};
509
510static void *
511coro_init (void *args_)
512{
513 struct coro_init_args *args = (struct coro_init_args *)args_;
514 coro_func func = args->func;
515 void *arg = args->arg;
516
517 coro_transfer (args->self, args->main);
518 func (arg);
519
520 return 0;
521}
522
523void
524coro_transfer (coro_context *prev, coro_context *next)
525{
526 pthread_mutex_lock (&coro_mutex);
527
528 next->flags = 1;
529 pthread_cond_signal (&next->cv);
530
531 prev->flags = 0;
532
533 while (!prev->flags)
534 pthread_cond_wait (&prev->cv, &coro_mutex);
535
536 if (prev->flags == 2)
537 {
538 pthread_mutex_unlock (&coro_mutex);
539 pthread_cond_destroy (&prev->cv);
540 pthread_detach (pthread_self ());
541 pthread_exit (0);
542 }
543
544 pthread_mutex_unlock (&coro_mutex);
545}
546
547void
548coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, size_t ssize)
549{
346 coro_context nctx; 550 static coro_context nctx;
347 struct coro_init_args args;
348 static int once; 551 static int once;
349 552
350 if (!once) 553 if (!once)
351 { 554 {
352 pthread_mutex_lock (&coro_mutex);
353 once = 1; 555 once = 1;
556
557 pthread_cond_init (&nctx.cv, 0);
354 } 558 }
355 559
560 pthread_cond_init (&ctx->cv, 0);
561
562 if (coro)
563 {
564 pthread_attr_t attr;
565 struct coro_init_args args;
566 pthread_t id;
567
356 args.func = coro; 568 args.func = coro;
357 args.arg = arg; 569 args.arg = arg;
358 args.self = ctx; 570 args.self = ctx;
359 args.main = &nctx; 571 args.main = &nctx;
360 572
361 pthread_attr_init (&attr); 573 pthread_attr_init (&attr);
362 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); 574#if __UCLIBC__
575 /* exists, but is borked */
576 /*pthread_attr_setstacksize (&attr, (size_t)ssize);*/
577#elif __CYGWIN__
578 /* POSIX, not here */
363 pthread_attr_setstack (&attr, sptr, (size_t)ssize); 579 pthread_attr_setstacksize (&attr, (size_t)ssize);
364 pthread_create (&id, &attr, trampoline, &args);
365
366 pthread_cond_init (&args.self->c, 0);
367 coro_transfer (args.main, args.self);
368
369#else 580#else
581 pthread_attr_setstack (&attr, sptr, (size_t)ssize);
582#endif
583 pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS);
584 pthread_create (&id, &attr, coro_init, &args);
585
586 coro_transfer (args.main, args.self);
587 }
588}
589
590void
591coro_destroy (coro_context *ctx)
592{
593 pthread_mutex_lock (&coro_mutex);
594 ctx->flags = 2;
595 pthread_cond_signal (&ctx->cv);
596 pthread_mutex_unlock (&coro_mutex);
597}
598
599/*****************************************************************************/
600/* fiber backend */
601/*****************************************************************************/
602#elif CORO_FIBER
603
604#define WIN32_LEAN_AND_MEAN
605#if _WIN32_WINNT < 0x0400
606 #undef _WIN32_WINNT
607 #define _WIN32_WINNT 0x0400
608#endif
609#include <windows.h>
610
611VOID CALLBACK
612coro_init (PVOID arg)
613{
614 coro_context *ctx = (coro_context *)arg;
615
616 ctx->coro (ctx->arg);
617}
618
619void
620coro_transfer (coro_context *prev, coro_context *next)
621{
622 if (!prev->fiber)
623 {
624 prev->fiber = GetCurrentFiber ();
625
626 if (prev->fiber == 0 || prev->fiber == (void *)0x1e00)
627 prev->fiber = ConvertThreadToFiber (0);
628 }
629
630 SwitchToFiber (next->fiber);
631}
632
633void
634coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, size_t ssize)
635{
636 ctx->fiber = 0;
637 ctx->coro = coro;
638 ctx->arg = arg;
639
640 if (!coro)
641 return;
642
643 ctx->fiber = CreateFiber (ssize, coro_init, ctx);
644}
645
646void
647coro_destroy (coro_context *ctx)
648{
649 DeleteFiber (ctx->fiber);
650}
651
652#else
370# error unsupported backend 653 #error unsupported backend
654#endif
655
656/*****************************************************************************/
657/* stack management */
658/*****************************************************************************/
659#if CORO_STACKALLOC
660
661#include <stdlib.h>
662
663#ifndef _WIN32
664# include <unistd.h>
665#endif
666
667#if CORO_USE_VALGRIND
668# include <valgrind/valgrind.h>
669#endif
670
671#if _POSIX_MAPPED_FILES
672# include <sys/mman.h>
673# define CORO_MMAP 1
674# ifndef MAP_ANONYMOUS
675# ifdef MAP_ANON
676# define MAP_ANONYMOUS MAP_ANON
677# else
678# undef CORO_MMAP
679# endif
371#endif 680# endif
372} 681# include <limits.h>
682#else
683# undef CORO_MMAP
684#endif
373 685
686#if _POSIX_MEMORY_PROTECTION
687# ifndef CORO_GUARDPAGES
688# define CORO_GUARDPAGES 4
689# endif
690#else
691# undef CORO_GUARDPAGES
692#endif
693
694#if !CORO_MMAP
695# undef CORO_GUARDPAGES
696#endif
697
698#if !__i386__ && !__x86_64__ && !__powerpc__ && !__arm__ && !__aarch64__ && !__m68k__ && !__alpha__ && !__mips__ && !__sparc64__
699# undef CORO_GUARDPAGES
700#endif
701
702#ifndef CORO_GUARDPAGES
703# define CORO_GUARDPAGES 0
704#endif
705
706#if !PAGESIZE
707 #if !CORO_MMAP
708 #define PAGESIZE 4096
709 #else
710 static size_t
711 coro_pagesize (void)
712 {
713 static size_t pagesize;
714
715 if (!pagesize)
716 pagesize = sysconf (_SC_PAGESIZE);
717
718 return pagesize;
719 }
720
721 #define PAGESIZE coro_pagesize ()
722 #endif
723#endif
724
725int
726coro_stack_alloc (struct coro_stack *stack, unsigned int size)
727{
728 if (!size)
729 size = 256 * 1024;
730
731 stack->sptr = 0;
732 stack->ssze = ((size_t)size * sizeof (void *) + PAGESIZE - 1) / PAGESIZE * PAGESIZE;
733
734#if CORO_FIBER
735
736 stack->sptr = (void *)stack;
737 return 1;
738
739#else
740
741 size_t ssze = stack->ssze + CORO_GUARDPAGES * PAGESIZE;
742 void *base;
743
744 #if CORO_MMAP
745 /* mmap supposedly does allocate-on-write for us */
746 base = mmap (0, ssze, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
747
748 if (base == (void *)-1)
749 {
750 /* some systems don't let us have executable heap */
751 /* we assume they won't need executable stack in that case */
752 base = mmap (0, ssze, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
753
754 if (base == (void *)-1)
755 return 0;
756 }
757
758 #if CORO_GUARDPAGES
759 mprotect (base, CORO_GUARDPAGES * PAGESIZE, PROT_NONE);
760 #endif
761
762 base = (void*)((char *)base + CORO_GUARDPAGES * PAGESIZE);
763 #else
764 base = malloc (ssze);
765 if (!base)
766 return 0;
767 #endif
768
769 #if CORO_USE_VALGRIND
770 stack->valgrind_id = VALGRIND_STACK_REGISTER ((char *)base, ((char *)base) + ssze - CORO_GUARDPAGES * PAGESIZE);
771 #endif
772
773 stack->sptr = base;
774 return 1;
775
776#endif
777}
778
779void
780coro_stack_free (struct coro_stack *stack)
781{
782#if CORO_FIBER
783 /* nop */
784#else
785 #if CORO_USE_VALGRIND
786 VALGRIND_STACK_DEREGISTER (stack->valgrind_id);
787 #endif
788
789 #if CORO_MMAP
790 if (stack->sptr)
791 munmap ((void*)((char *)stack->sptr - CORO_GUARDPAGES * PAGESIZE),
792 stack->ssze + CORO_GUARDPAGES * PAGESIZE);
793 #else
794 free (stack->sptr);
795 #endif
796#endif
797}
798
799#endif
800

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines