ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/State.xs
(Generate patch)

Comparing Coro/Coro/State.xs (file contents):
Revision 1.428 by root, Wed Dec 5 16:34:12 2012 UTC vs.
Revision 1.429 by root, Fri Dec 7 14:21:09 2012 UTC

1/* this works around a bug in mingw32 providing a non-working setjmp */ 1/* this works around a bug in mingw32 providing a non-working setjmp */
2#define USE_NO_MINGW_SETJMP_TWO_ARGS 2#define USE_NO_MINGW_SETJMP_TWO_ARGS
3 3
4#define NDEBUG 1 4#define NDEBUG 1 /* perl usually disables NDEBUG later */
5 5
6#include "libcoro/coro.c" 6#include "libcoro/coro.c"
7 7
8#define PERL_NO_GET_CONTEXT 8#define PERL_NO_GET_CONTEXT
9#define PERL_EXT 9#define PERL_EXT
52# define setjmp _setjmp /* deep magic */ 52# define setjmp _setjmp /* deep magic */
53#else 53#else
54# include <inttypes.h> /* most portable stdint.h */ 54# include <inttypes.h> /* most portable stdint.h */
55#endif 55#endif
56 56
57#if HAVE_MMAP
58# include <unistd.h>
59# include <sys/mman.h>
60# ifndef MAP_ANONYMOUS
61# ifdef MAP_ANON
62# define MAP_ANONYMOUS MAP_ANON
63# else
64# undef HAVE_MMAP
65# endif
66# endif
67# include <limits.h>
68# ifndef PAGESIZE
69# define PAGESIZE pagesize
70# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
71static long pagesize;
72# else
73# define BOOT_PAGESIZE (void)0
74# endif
75#else
76# define PAGESIZE 0
77# define BOOT_PAGESIZE (void)0
78#endif
79
80#if CORO_USE_VALGRIND
81# include <valgrind/valgrind.h>
82#endif
83
84/* the maximum number of idle cctx that will be pooled */ 57/* the maximum number of idle cctx that will be pooled */
85static int cctx_max_idle = 4; 58static int cctx_max_idle = 4;
86 59
87#if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) 60#if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0)
88# define HAS_SCOPESTACK_NAME 1 61# define HAS_SCOPESTACK_NAME 1
89#endif
90
91#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
92# undef CORO_STACKGUARD
93#endif
94
95#ifndef CORO_STACKGUARD
96# define CORO_STACKGUARD 0
97#endif 62#endif
98 63
99/* prefer perl internal functions over our own? */ 64/* prefer perl internal functions over our own? */
100#ifndef CORO_PREFER_PERL_FUNCTIONS 65#ifndef CORO_PREFER_PERL_FUNCTIONS
101# define CORO_PREFER_PERL_FUNCTIONS 0 66# define CORO_PREFER_PERL_FUNCTIONS 0
192typedef struct coro_cctx 157typedef struct coro_cctx
193{ 158{
194 struct coro_cctx *next; 159 struct coro_cctx *next;
195 160
196 /* the stack */ 161 /* the stack */
197 void *sptr; 162 struct coro_stack stack;
198 size_t ssize;
199 163
200 /* cpu state */ 164 /* cpu state */
201 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 165 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
202#ifndef NDEBUG 166#ifndef NDEBUG
203 JMPENV *idle_te; /* same as idle_sp, but for top_env */ 167 JMPENV *idle_te; /* same as idle_sp, but for top_env */
312#define coro_nready coroapi.nready 276#define coro_nready coroapi.nready
313 277
314/** JIT *********************************************************************/ 278/** JIT *********************************************************************/
315 279
316#if CORO_JIT 280#if CORO_JIT
317 /* APPLE doesn't have HAVE_MMAP though */ 281 /* APPLE doesn't have mmap though */
318 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) 282 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__)
319 #ifndef CORO_JIT_TYPE 283 #ifndef CORO_JIT_TYPE
320 #if __x86_64 && CORO_JIT_UNIXY 284 #if __x86_64 && CORO_JIT_UNIXY
321 #define CORO_JIT_TYPE "amd64-unix" 285 #define CORO_JIT_TYPE "amd64-unix"
322 #elif __i386 && CORO_JIT_UNIXY 286 #elif __i386 && CORO_JIT_UNIXY
323 #define CORO_JIT_TYPE "x86-unix" 287 #define CORO_JIT_TYPE "x86-unix"
324 #endif 288 #endif
325 #endif 289 #endif
326#endif 290#endif
327 291
328#if !defined(CORO_JIT_TYPE) || !HAVE_MMAP 292#if !defined(CORO_JIT_TYPE) || _POSIX_MEMORY_PROTECTION <= 0
329 #undef CORO_JIT 293 #undef CORO_JIT
330#endif 294#endif
331 295
332#if CORO_JIT 296#if CORO_JIT
333 typedef void (*load_save_perl_slots_type)(perl_slots *); 297 typedef void (*load_save_perl_slots_type)(perl_slots *);
1519static coro_cctx * 1483static coro_cctx *
1520cctx_new_empty (void) 1484cctx_new_empty (void)
1521{ 1485{
1522 coro_cctx *cctx = cctx_new (); 1486 coro_cctx *cctx = cctx_new ();
1523 1487
1524 cctx->sptr = 0; 1488 cctx->stack.sptr = 0;
1525 coro_create (&cctx->cctx, 0, 0, 0, 0); 1489 coro_create (&cctx->cctx, 0, 0, 0, 0);
1526 1490
1527 return cctx; 1491 return cctx;
1528} 1492}
1529 1493
1530/* create a new cctx suitable as destination/running a perl interpreter */ 1494/* create a new cctx suitable as destination/running a perl interpreter */
1531static coro_cctx * 1495static coro_cctx *
1532cctx_new_run (void) 1496cctx_new_run (void)
1533{ 1497{
1534 coro_cctx *cctx = cctx_new (); 1498 coro_cctx *cctx = cctx_new ();
1535 void *stack_start;
1536 size_t stack_size;
1537 1499
1538#if CORO_FIBER 1500 if (!coro_stack_alloc (&cctx->stack, cctx_stacksize))
1539
1540 cctx->ssize = cctx_stacksize * sizeof (long);
1541 cctx->sptr = 0;
1542
1543#else
1544
1545 #if HAVE_MMAP
1546 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1547 /* mmap supposedly does allocate-on-write for us */
1548 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0);
1549
1550 if (cctx->sptr != (void *)-1)
1551 { 1501 {
1552 #if CORO_STACKGUARD
1553 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1554 #endif
1555 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1556 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1557 cctx->flags |= CC_MAPPED;
1558 }
1559 else
1560 #endif
1561 {
1562 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1563 New (0, cctx->sptr, cctx_stacksize, long);
1564
1565 if (!cctx->sptr)
1566 {
1567 perror ("FATAL: unable to allocate stack for coroutine, exiting."); 1502 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1568 _exit (EXIT_FAILURE); 1503 _exit (EXIT_FAILURE);
1569 }
1570
1571 stack_start = cctx->sptr;
1572 stack_size = cctx->ssize;
1573 } 1504 }
1574 #endif
1575 1505
1576 #if CORO_USE_VALGRIND
1577 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1578 #endif
1579
1580 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); 1506 coro_create (&cctx->cctx, cctx_run, (void *)cctx, cctx->stack.sptr, cctx->stack.ssze);
1581 1507
1582 return cctx; 1508 return cctx;
1583} 1509}
1584 1510
1585static void 1511static void
1591 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); 1517 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));
1592 1518
1593 --cctx_count; 1519 --cctx_count;
1594 coro_destroy (&cctx->cctx); 1520 coro_destroy (&cctx->cctx);
1595 1521
1596 /* coro_transfer creates new, empty cctx's */ 1522 coro_stack_free (&cctx->stack);
1597 if (cctx->sptr)
1598 {
1599 #if CORO_USE_VALGRIND
1600 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1601 #endif
1602
1603 #if HAVE_MMAP
1604 if (cctx->flags & CC_MAPPED)
1605 munmap (cctx->sptr, cctx->ssize);
1606 else
1607 #endif
1608 Safefree (cctx->sptr);
1609 }
1610 1523
1611 Safefree (cctx); 1524 Safefree (cctx);
1612} 1525}
1613 1526
1614/* wether this cctx should be destructed */ 1527/* wether this cctx should be destructed */
1633} 1546}
1634 1547
1635static void 1548static void
1636cctx_put (coro_cctx *cctx) 1549cctx_put (coro_cctx *cctx)
1637{ 1550{
1638 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); 1551 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->stack.sptr));
1639 1552
1640 /* free another cctx if overlimit */ 1553 /* free another cctx if overlimit */
1641 if (ecb_expect_false (cctx_idle >= cctx_max_idle)) 1554 if (ecb_expect_false (cctx_idle >= cctx_max_idle))
1642 { 1555 {
1643 coro_cctx *first = cctx_first; 1556 coro_cctx *first = cctx_first;
3478#ifdef USE_ITHREADS 3391#ifdef USE_ITHREADS
3479# if CORO_PTHREAD 3392# if CORO_PTHREAD
3480 coro_thx = PERL_GET_CONTEXT; 3393 coro_thx = PERL_GET_CONTEXT;
3481# endif 3394# endif
3482#endif 3395#endif
3483 BOOT_PAGESIZE;
3484
3485 /* perl defines these to check for existance first, but why it doesn't */ 3396 /* perl defines these to check for existance first, but why it doesn't */
3486 /* just create them one at init time is not clear to me, except for */ 3397 /* just create them one at init time is not clear to me, except for */
3487 /* programs trying to delete them, but... */ 3398 /* programs trying to delete them, but... */
3488 /* anyway, we declare this as invalid and make sure they are initialised here */ 3399 /* anyway, we declare this as invalid and make sure they are initialised here */
3489 DEFSV; 3400 DEFSV;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines