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.66 by root, Fri Dec 7 14:21:09 2012 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/*****************************************************************************/
239 ); 240 );
240 241
241# endif 242# endif
242 243
243void 244void
244coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) 245coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, size_t ssize)
245{ 246{
246 coro_context nctx; 247 coro_context nctx;
247# if CORO_SJLJ 248# if CORO_SJLJ
248 stack_t ostk, nstk; 249 stack_t ostk, nstk;
249 struct sigaction osa, nsa; 250 struct sigaction osa, nsa;
441 pthread_testcancel (); 442 pthread_testcancel ();
442#endif 443#endif
443} 444}
444 445
445void 446void
446coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) 447coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, size_t ssize)
447{ 448{
448 static coro_context nctx; 449 static coro_context nctx;
449 static int once; 450 static int once;
450 451
451 if (!once) 452 if (!once)
506/* fiber backend */ 507/* fiber backend */
507/*****************************************************************************/ 508/*****************************************************************************/
508#elif CORO_FIBER 509#elif CORO_FIBER
509 510
510#define WIN32_LEAN_AND_MEAN 511#define WIN32_LEAN_AND_MEAN
512#if _WIN32_WINNT < 0x0400
513 #undef _WIN32_WINNT
511#define _WIN32_WINNT 0x0400 514 #define _WIN32_WINNT 0x0400
515#endif
512#include <windows.h> 516#include <windows.h>
513 517
514VOID CALLBACK 518VOID CALLBACK
515coro_init (PVOID arg) 519coro_init (PVOID arg)
516{ 520{
532 536
533 SwitchToFiber (next->fiber); 537 SwitchToFiber (next->fiber);
534} 538}
535 539
536void 540void
537coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, long ssize) 541coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, size_t ssize)
538{ 542{
539 ctx->fiber = 0; 543 ctx->fiber = 0;
540 ctx->coro = coro; 544 ctx->coro = coro;
541 ctx->arg = arg; 545 ctx->arg = arg;
542 546
551{ 555{
552 DeleteFiber (ctx->fiber); 556 DeleteFiber (ctx->fiber);
553} 557}
554 558
555#else 559#else
556# error unsupported backend 560 #error unsupported backend
561#endif
562
563/*****************************************************************************/
564/* stack management */
565/*****************************************************************************/
566#if CORO_STACKALLOC
567
568#include <stdlib.h>
569
570#ifndef _WIN32
571# include <unistd.h>
572#endif
573
574#if CORO_USE_VALGRIND
575# include <valgrind/valgrind.h>
576#endif
577
578#if _POSIX_MAPPED_FILES
579# include <sys/mman.h>
580# define CORO_MMAP 1
581# ifndef MAP_ANONYMOUS
582# ifdef MAP_ANON
583# define MAP_ANONYMOUS MAP_ANON
584# else
585# undef CORO_MMAP
586# endif
557#endif 587# endif
588# include <limits.h>
589#else
590# undef CORO_MMAP
591#endif
558 592
593#if _POSIX_MEMORY_PROTECTION
594# ifndef CORO_GUARDPAGES
595# define CORO_GUARDPAGES 4
596# endif
597#else
598# undef CORO_GUARDPAGES
599#endif
600
601#if !CORO_MMAP
602# undef CORO_GUARDPAGES
603#endif
604
605#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
606# undef CORO_GUARDPAGES
607#endif
608
609#ifndef CORO_GUARDPAGES
610# define CORO_GUARDPAGES 0
611#endif
612
613#if !PAGESIZE
614 #if !CORO_MMAP
615 #define PAGESIZE 4096
616 #else
617 static size_t
618 coro_pagesize (void)
619 {
620 static size_t pagesize;
621
622 if (!pagesize)
623 pagesize = sysconf (_SC_PAGESIZE);
624
625 return pagesize;
626 }
627
628 #define PAGESIZE coro_pagesize ()
629 #endif
630#endif
631
632int
633coro_stack_alloc (struct coro_stack *stack, unsigned int size)
634{
635 if (!size)
636 size = 256 * 1024;
637
638 stack->sptr = 0;
639 stack->ssze = ((size_t)size * sizeof (void *) + PAGESIZE - 1) / PAGESIZE * PAGESIZE;
640
641#if CORO_FIBER
642
643 stack->sptr = (void *)stack;
644 return 1;
645
646#else
647
648 size_t ssze = stack->ssze + CORO_GUARDPAGES * PAGESIZE;
649 void *base;
650
651 #if CORO_MMAP
652 /* mmap supposedly does allocate-on-write for us */
653 base = mmap (0, ssze, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
654
655 if (base == (void *)-1)
656 {
657 /* some systems don't let us have executable heap */
658 /* we assume they won't need executable stack in that case */
659 base = mmap (0, ssze, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
660
661 if (base == (void *)-1)
662 return 0;
663 }
664
665 #if CORO_GUARDPAGES
666 mprotect (base, CORO_GUARDPAGES * PAGESIZE, PROT_NONE);
667 #endif
668
669 base = (void*)((char *)base + CORO_GUARDPAGES * PAGESIZE);
670 #else
671 base = malloc (ssze);
672 if (!base)
673 return 0;
674 #endif
675
676 #if CORO_USE_VALGRIND
677 stack->valgrind_id = VALGRIND_STACK_REGISTER ((char *)base, (char *)ssze - CORO_GUARDPAGES * PAGESIZE);
678 #endif
679
680 stack->sptr = base;
681 return 1;
682
683#endif
684}
685
686void
687coro_stack_free (struct coro_stack *stack)
688{
689#if CORO_FIBER
690 /* nop */
691#else
692 #if CORO_USE_VALGRIND
693 VALGRIND_STACK_DEREGISTER (stack->valgrind_id);
694 #endif
695
696 #if CORO_MMAP
697 if (stack->sptr)
698 munmap ((void*)((char *)stack->sptr - CORO_GUARDPAGES * PAGESIZE),
699 stack->ssze + CORO_GUARDPAGES * PAGESIZE);
700 #else
701 free (stack->sptr);
702 #endif
703#endif
704}
705
706#endif
707

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines