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

Comparing libcoro/coro.c (file contents):
Revision 1.65 by root, Wed Dec 5 13:10:21 2012 UTC vs.
Revision 1.68 by root, Sun Mar 6 06:26:21 2016 UTC

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>
43#include <string.h> 44#include <string.h>
44 45
45/*****************************************************************************/ 46/*****************************************************************************/
46/* ucontext/setjmp/asm backends */ 47/* ucontext/setjmp/asm backends */
47/*****************************************************************************/ 48/*****************************************************************************/
113} 114}
114 115
115# endif 116# endif
116 117
117# if CORO_ASM 118# if CORO_ASM
119
120 #if __arm__ && \
121 (defined __ARM_ARCH_7__ || defined __ARM_ARCH_7A__ \
122 || defined __ARM_ARCH_7R__ || defined __ARM_ARCH_7M__ \
123 || __ARCH_ARCH == 7)
124 #define CORO_ARM 1
125 #endif
118 126
119 #if _WIN32 || __CYGWIN__ 127 #if _WIN32 || __CYGWIN__
120 #define CORO_WIN_TIB 1 128 #define CORO_WIN_TIB 1
121 #endif 129 #endif
122 130
203 "\tpopq %rbp\n" 211 "\tpopq %rbp\n"
204 #endif 212 #endif
205 "\tpopq %rcx\n" 213 "\tpopq %rcx\n"
206 "\tjmpq *%rcx\n" 214 "\tjmpq *%rcx\n"
207 215
208 #elif __i386 216 #elif __i386__
209 217
210 #define NUM_SAVED 4 218 #define NUM_SAVED 4
211 "\tpushl %ebp\n" 219 "\tpushl %ebp\n"
212 "\tpushl %ebx\n" 220 "\tpushl %ebx\n"
213 "\tpushl %esi\n" 221 "\tpushl %esi\n"
231 "\tpopl %ebx\n" 239 "\tpopl %ebx\n"
232 "\tpopl %ebp\n" 240 "\tpopl %ebp\n"
233 "\tpopl %ecx\n" 241 "\tpopl %ecx\n"
234 "\tjmpl *%ecx\n" 242 "\tjmpl *%ecx\n"
235 243
244 #elif CORO_ARM /* untested, what about thumb, neon, iwmmxt? */
245
246 #if __ARM_PCS_VFP
247 "\tvpush {d8-d15}\n"
248 #define NUM_SAVED (9 + 8 * 2)
249 #else
250 #define NUM_SAVED 9
251 #endif
252 "\tpush {r4-r11,lr}\n"
253 "\tstr sp, [r0]\n"
254 "\tldr sp, [r1]\n"
255 "\tpop {r4-r11,lr}\n"
256 #if __ARM_PCS_VFP
257 "\tvpop {d8-d15}\n"
258 #endif
259 "\tmov r15, lr\n"
260
261 #elif __mips__ && 0 /* untested, 32 bit only */
262
263 #define NUM_SAVED (12 + 8 * 2)
264 /* TODO: n64/o64, lw=>ld */
265
266 "\t.set nomips16\n"
267 "\t.frame $sp,112,$31\n"
268 #if __mips_soft_float
269 "\taddiu $sp,$sp,-44\n"
270 #else
271 "\taddiu $sp,$sp,-112\n"
272 "\ts.d $f30,88($sp)\n"
273 "\ts.d $f28,80($sp)\n"
274 "\ts.d $f26,72($sp)\n"
275 "\ts.d $f24,64($sp)\n"
276 "\ts.d $f22,56($sp)\n"
277 "\ts.d $f20,48($sp)\n"
278 #endif
279 "\tsw $28,40($sp)\n"
280 "\tsw $31,36($sp)\n"
281 "\tsw $fp,32($sp)\n"
282 "\tsw $23,28($sp)\n"
283 "\tsw $22,24($sp)\n"
284 "\tsw $21,20($sp)\n"
285 "\tsw $20,16($sp)\n"
286 "\tsw $19,12($sp)\n"
287 "\tsw $18,8($sp)\n"
288 "\tsw $17,4($sp)\n"
289 "\tsw $16,0($sp)\n"
290 "\tsw $sp,0($4)\n"
291 "\tlw $sp,0($5)\n"
292 #if !__mips_soft_float
293 "\tl.d $f30,88($sp)\n"
294 "\tl.d $f28,80($sp)\n"
295 "\tl.d $f26,72($sp)\n"
296 "\tl.d $f24,64($sp)\n"
297 "\tl.d $f22,56($sp)\n"
298 "\tl.d $f20,48($sp)\n"
299 #endif
300 "\tlw $28,40($sp)\n"
301 "\tlw $31,36($sp)\n"
302 "\tlw $fp,32($sp)\n"
303 "\tlw $23,28($sp)\n"
304 "\tlw $22,24($sp)\n"
305 "\tlw $21,20($sp)\n"
306 "\tlw $20,16($sp)\n"
307 "\tlw $19,12($sp)\n"
308 "\tlw $18,8($sp)\n"
309 "\tlw $17,4($sp)\n"
310 "\tlw $16,0($sp)\n"
311 "\tj $31\n"
312 #if __mips_soft_float
313 "\taddiu $sp,$sp,44\n"
314 #else
315 "\taddiu $sp,$sp,112\n"
316 #endif
317
236 #else 318 #else
237 #error unsupported architecture 319 #error unsupported architecture
238 #endif 320 #endif
239 ); 321 );
240 322
241# endif 323# endif
242 324
243void 325void
244coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) 326coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, size_t ssize)
245{ 327{
246 coro_context nctx; 328 coro_context nctx;
247# if CORO_SJLJ 329# if CORO_SJLJ
248 stack_t ostk, nstk; 330 stack_t ostk, nstk;
249 struct sigaction osa, nsa; 331 struct sigaction osa, nsa;
310 sigprocmask (SIG_SETMASK, &osig, 0); 392 sigprocmask (SIG_SETMASK, &osig, 0);
311 393
312# elif CORO_LOSER 394# elif CORO_LOSER
313 395
314 coro_setjmp (ctx->env); 396 coro_setjmp (ctx->env);
315 #if __CYGWIN__ && __i386 397 #if __CYGWIN__ && __i386__
316 ctx->env[8] = (long) coro_init; 398 ctx->env[8] = (long) coro_init;
317 ctx->env[7] = (long) ((char *)sptr + ssize) - sizeof (long); 399 ctx->env[7] = (long) ((char *)sptr + ssize) - sizeof (long);
318 #elif __CYGWIN__ && __x86_64 400 #elif __CYGWIN__ && __x86_64__
319 ctx->env[7] = (long) coro_init; 401 ctx->env[7] = (long) coro_init;
320 ctx->env[6] = (long) ((char *)sptr + ssize) - sizeof (long); 402 ctx->env[6] = (long) ((char *)sptr + ssize) - sizeof (long);
321 #elif defined __MINGW32__ 403 #elif defined __MINGW32__
322 ctx->env[5] = (long) coro_init; 404 ctx->env[5] = (long) coro_init;
323 ctx->env[4] = (long) ((char *)sptr + ssize) - sizeof (long); 405 ctx->env[4] = (long) ((char *)sptr + ssize) - sizeof (long);
344 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init; 426 ctx->env[0].__jmpbuf[0].__aregs[0] = (long int)coro_init;
345 ctx->env[0].__jmpbuf[0].__sp = (int *) ((char *)sptr + ssize) - sizeof (long); 427 ctx->env[0].__jmpbuf[0].__sp = (int *) ((char *)sptr + ssize) - sizeof (long);
346 #elif defined (__GNU_LIBRARY__) && defined (__i386__) 428 #elif defined (__GNU_LIBRARY__) && defined (__i386__)
347 ctx->env[0].__jmpbuf[0].__pc = (char *) coro_init; 429 ctx->env[0].__jmpbuf[0].__pc = (char *) coro_init;
348 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long); 430 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long);
349 #elif defined (__GNU_LIBRARY__) && defined (__amd64__) 431 #elif defined (__GNU_LIBRARY__) && defined (__x86_64__)
350 ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init; 432 ctx->env[0].__jmpbuf[JB_PC] = (long) coro_init;
351 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long); 433 ctx->env[0].__jmpbuf[0].__sp = (void *) ((char *)sptr + ssize) - sizeof (long);
352 #else 434 #else
353 #error "linux libc or architecture not supported" 435 #error "linux libc or architecture not supported"
354 #endif 436 #endif
359 ctx->env[JB_PC] = (__uint64_t)coro_init; 441 ctx->env[JB_PC] = (__uint64_t)coro_init;
360 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long); 442 ctx->env[JB_SP] = (__uint64_t)STACK_ADJUST_PTR (sptr, ssize) - sizeof (long);
361 443
362# elif CORO_ASM 444# elif CORO_ASM
363 445
446 #if __i386__ || __x86_64__
364 ctx->sp = (void **)(ssize + (char *)sptr); 447 ctx->sp = (void **)(ssize + (char *)sptr);
365 *--ctx->sp = (void *)abort; /* needed for alignment only */ 448 *--ctx->sp = (void *)abort; /* needed for alignment only */
366 *--ctx->sp = (void *)coro_init; 449 *--ctx->sp = (void *)coro_init;
367
368 #if CORO_WIN_TIB 450 #if CORO_WIN_TIB
369 *--ctx->sp = 0; /* ExceptionList */ 451 *--ctx->sp = 0; /* ExceptionList */
370 *--ctx->sp = (char *)sptr + ssize; /* StackBase */ 452 *--ctx->sp = (char *)sptr + ssize; /* StackBase */
371 *--ctx->sp = sptr; /* StackLimit */ 453 *--ctx->sp = sptr; /* StackLimit */
454 #endif
455 #elif CORO_ARM
456 /* return address stored in lr register, don't push anything */
457 #else
458 #error unsupported architecture
372 #endif 459 #endif
373 460
374 ctx->sp -= NUM_SAVED; 461 ctx->sp -= NUM_SAVED;
375 memset (ctx->sp, 0, sizeof (*ctx->sp) * NUM_SAVED); 462 memset (ctx->sp, 0, sizeof (*ctx->sp) * NUM_SAVED);
463
464 #if __i386__ || __x86_64__
465 /* done already */
466 #elif CORO_ARM
467 ctx->sp[0] = coro; /* r4 */
468 ctx->sp[1] = arg; /* r5 */
469 ctx->sp[8] = (char *)coro_init; /* lr */
470 #else
471 #error unsupported architecture
472 #endif
376 473
377# elif CORO_UCONTEXT 474# elif CORO_UCONTEXT
378 475
379 getcontext (&(ctx->uc)); 476 getcontext (&(ctx->uc));
380 477
441 pthread_testcancel (); 538 pthread_testcancel ();
442#endif 539#endif
443} 540}
444 541
445void 542void
446coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) 543coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, size_t ssize)
447{ 544{
448 static coro_context nctx; 545 static coro_context nctx;
449 static int once; 546 static int once;
450 547
451 if (!once) 548 if (!once)
506/* fiber backend */ 603/* fiber backend */
507/*****************************************************************************/ 604/*****************************************************************************/
508#elif CORO_FIBER 605#elif CORO_FIBER
509 606
510#define WIN32_LEAN_AND_MEAN 607#define WIN32_LEAN_AND_MEAN
608#if _WIN32_WINNT < 0x0400
609 #undef _WIN32_WINNT
511#define _WIN32_WINNT 0x0400 610 #define _WIN32_WINNT 0x0400
611#endif
512#include <windows.h> 612#include <windows.h>
513 613
514VOID CALLBACK 614VOID CALLBACK
515coro_init (PVOID arg) 615coro_init (PVOID arg)
516{ 616{
532 632
533 SwitchToFiber (next->fiber); 633 SwitchToFiber (next->fiber);
534} 634}
535 635
536void 636void
537coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) 637coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, size_t ssize)
538{ 638{
539 ctx->fiber = 0; 639 ctx->fiber = 0;
540 ctx->coro = coro; 640 ctx->coro = coro;
541 ctx->arg = arg; 641 ctx->arg = arg;
542 642
551{ 651{
552 DeleteFiber (ctx->fiber); 652 DeleteFiber (ctx->fiber);
553} 653}
554 654
555#else 655#else
556# error unsupported backend 656 #error unsupported backend
657#endif
658
659/*****************************************************************************/
660/* stack management */
661/*****************************************************************************/
662#if CORO_STACKALLOC
663
664#include <stdlib.h>
665
666#ifndef _WIN32
667# include <unistd.h>
668#endif
669
670#if CORO_USE_VALGRIND
671# include <valgrind/valgrind.h>
672#endif
673
674#if _POSIX_MAPPED_FILES
675# include <sys/mman.h>
676# define CORO_MMAP 1
677# ifndef MAP_ANONYMOUS
678# ifdef MAP_ANON
679# define MAP_ANONYMOUS MAP_ANON
680# else
681# undef CORO_MMAP
682# endif
557#endif 683# endif
684# include <limits.h>
685#else
686# undef CORO_MMAP
687#endif
558 688
689#if _POSIX_MEMORY_PROTECTION
690# ifndef CORO_GUARDPAGES
691# define CORO_GUARDPAGES 4
692# endif
693#else
694# undef CORO_GUARDPAGES
695#endif
696
697#if !CORO_MMAP
698# undef CORO_GUARDPAGES
699#endif
700
701#if !__i386__ && !__x86_64__ && !__powerpc__ && !__arm__ && !__aarch64__ && !__m68k__ && !__alpha__ && !__mips__ && !__sparc64__
702# undef CORO_GUARDPAGES
703#endif
704
705#ifndef CORO_GUARDPAGES
706# define CORO_GUARDPAGES 0
707#endif
708
709#if !PAGESIZE
710 #if !CORO_MMAP
711 #define PAGESIZE 4096
712 #else
713 static size_t
714 coro_pagesize (void)
715 {
716 static size_t pagesize;
717
718 if (!pagesize)
719 pagesize = sysconf (_SC_PAGESIZE);
720
721 return pagesize;
722 }
723
724 #define PAGESIZE coro_pagesize ()
725 #endif
726#endif
727
728int
729coro_stack_alloc (struct coro_stack *stack, unsigned int size)
730{
731 if (!size)
732 size = 256 * 1024;
733
734 stack->sptr = 0;
735 stack->ssze = ((size_t)size * sizeof (void *) + PAGESIZE - 1) / PAGESIZE * PAGESIZE;
736
737#if CORO_FIBER
738
739 stack->sptr = (void *)stack;
740 return 1;
741
742#else
743
744 size_t ssze = stack->ssze + CORO_GUARDPAGES * PAGESIZE;
745 void *base;
746
747 #if CORO_MMAP
748 /* mmap supposedly does allocate-on-write for us */
749 base = mmap (0, ssze, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
750
751 if (base == (void *)-1)
752 {
753 /* some systems don't let us have executable heap */
754 /* we assume they won't need executable stack in that case */
755 base = mmap (0, ssze, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
756
757 if (base == (void *)-1)
758 return 0;
759 }
760
761 #if CORO_GUARDPAGES
762 mprotect (base, CORO_GUARDPAGES * PAGESIZE, PROT_NONE);
763 #endif
764
765 base = (void*)((char *)base + CORO_GUARDPAGES * PAGESIZE);
766 #else
767 base = malloc (ssze);
768 if (!base)
769 return 0;
770 #endif
771
772 #if CORO_USE_VALGRIND
773 stack->valgrind_id = VALGRIND_STACK_REGISTER ((char *)base, ((char *)base) + ssze - CORO_GUARDPAGES * PAGESIZE);
774 #endif
775
776 stack->sptr = base;
777 return 1;
778
779#endif
780}
781
782void
783coro_stack_free (struct coro_stack *stack)
784{
785#if CORO_FIBER
786 /* nop */
787#else
788 #if CORO_USE_VALGRIND
789 VALGRIND_STACK_DEREGISTER (stack->valgrind_id);
790 #endif
791
792 #if CORO_MMAP
793 if (stack->sptr)
794 munmap ((void*)((char *)stack->sptr - CORO_GUARDPAGES * PAGESIZE),
795 stack->ssze + CORO_GUARDPAGES * PAGESIZE);
796 #else
797 free (stack->sptr);
798 #endif
799#endif
800}
801
802#endif
803

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines