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.427 by root, Wed Dec 5 14:35:34 2012 UTC vs.
Revision 1.438 by root, Mon Nov 18 10:27:53 2013 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
26#ifndef SVs_PADSTALE 26#ifndef SVs_PADSTALE
27# define SVs_PADSTALE 0 27# define SVs_PADSTALE 0
28#endif 28#endif
29 29
30#ifdef PadARRAY 30#ifdef PadARRAY
31# define NEWPADAPI 31# define NEWPADAPI 1
32# define newPADLIST(var) (Newz (0, var, 1, PADLIST), Newx (PadlistARRAY (var), 2, PAD *)) 32# define newPADLIST(var) (Newz (0, var, 1, PADLIST), Newx (PadlistARRAY (var), 2, PAD *))
33#else 33#else
34typedef AV PADNAMELIST; 34typedef AV PADNAMELIST;
35# if !PERL_VERSION_ATLEAST(5,8,0) 35# if !PERL_VERSION_ATLEAST(5,8,0)
36typedef AV PADLIST; 36typedef AV PADLIST;
42# define PadARRAY AvARRAY 42# define PadARRAY AvARRAY
43# define PadMAX AvFILLp 43# define PadMAX AvFILLp
44# define newPADLIST(var) ((var) = newAV (), av_extend (var, 1)) 44# define newPADLIST(var) ((var) = newAV (), av_extend (var, 1))
45#endif 45#endif
46 46
47/* 5.19.something has replaced SVt_BIND by SVt_INVLIST */
48/* we just alias it to SVt_IV, as that is sufficient for swap_sv for now */
49#if PERL_VERSION_ATLEAST(5,19,0)
50# define SVt_BIND SVt_IV
51#endif
52
47#if defined(_WIN32) 53#if defined(_WIN32)
48# undef HAS_GETTIMEOFDAY 54# undef HAS_GETTIMEOFDAY
49# undef setjmp 55# undef setjmp
50# undef longjmp 56# undef longjmp
51# undef _exit 57# undef _exit
52# define setjmp _setjmp /* deep magic */ 58# define setjmp _setjmp /* deep magic */
53#else 59#else
54# include <inttypes.h> /* most portable stdint.h */ 60# include <inttypes.h> /* most portable stdint.h */
55#endif 61#endif
56 62
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 */ 63/* the maximum number of idle cctx that will be pooled */
85static int cctx_max_idle = 4; 64static int cctx_max_idle = 4;
86 65
87#if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) 66#if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0)
88# define HAS_SCOPESTACK_NAME 1 67# 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 68#endif
98 69
99/* prefer perl internal functions over our own? */ 70/* prefer perl internal functions over our own? */
100#ifndef CORO_PREFER_PERL_FUNCTIONS 71#ifndef CORO_PREFER_PERL_FUNCTIONS
101# define CORO_PREFER_PERL_FUNCTIONS 0 72# define CORO_PREFER_PERL_FUNCTIONS 0
192typedef struct coro_cctx 163typedef struct coro_cctx
193{ 164{
194 struct coro_cctx *next; 165 struct coro_cctx *next;
195 166
196 /* the stack */ 167 /* the stack */
197 void *sptr; 168 struct coro_stack stack;
198 size_t ssize;
199 169
200 /* cpu state */ 170 /* cpu state */
201 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 171 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
202#ifndef NDEBUG 172#ifndef NDEBUG
203 JMPENV *idle_te; /* same as idle_sp, but for top_env */ 173 JMPENV *idle_te; /* same as idle_sp, but for top_env */
312#define coro_nready coroapi.nready 282#define coro_nready coroapi.nready
313 283
314/** JIT *********************************************************************/ 284/** JIT *********************************************************************/
315 285
316#if CORO_JIT 286#if CORO_JIT
317 /* APPLE doesn't have HAVE_MMAP though */ 287 /* APPLE doesn't have mmap though */
318 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) 288 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__)
319 #ifndef CORO_JIT_TYPE 289 #ifndef CORO_JIT_TYPE
320 #if __x86_64 && CORO_JIT_UNIXY 290 #if ECB_AMD64 && CORO_JIT_UNIXY
321 #define CORO_JIT_TYPE "amd64-unix" 291 #define CORO_JIT_TYPE "amd64-unix"
322 #elif __i386 && CORO_JIT_UNIXY 292 #elif __i386 && CORO_JIT_UNIXY
323 #define CORO_JIT_TYPE "x86-unix" 293 #define CORO_JIT_TYPE "x86-unix"
324 #endif 294 #endif
325 #endif 295 #endif
326#endif 296#endif
327 297
328#if !defined(CORO_JIT_TYPE) || !HAVE_MMAP 298#if !defined(CORO_JIT_TYPE) || _POSIX_MEMORY_PROTECTION <= 0
329 #undef CORO_JIT 299 #undef CORO_JIT
330#endif 300#endif
331 301
332#if CORO_JIT 302#if CORO_JIT
333 typedef void (*load_save_perl_slots_type)(perl_slots *); 303 typedef void (*load_save_perl_slots_type)(perl_slots *);
547coro_derive_padlist (pTHX_ CV *cv) 517coro_derive_padlist (pTHX_ CV *cv)
548{ 518{
549 PADLIST *padlist = CvPADLIST (cv); 519 PADLIST *padlist = CvPADLIST (cv);
550 PADLIST *newpadlist; 520 PADLIST *newpadlist;
551 PAD *newpad; 521 PAD *newpad;
552 PADOFFSET const off = PadlistMAX (padlist) + 1; 522 PADOFFSET off = PadlistMAX (padlist) + 1;
553 523
554 newPADLIST(newpadlist); 524#if NEWPADAPI
555#if !PERL_VERSION_ATLEAST(5,15,3) 525
556 /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */ 526 /* if we had the original CvDEPTH, we might be able to steal the CvDEPTH+1 entry instead */
557 AvREAL_off (newpadlist); 527 /* 20131102194744.GA6705@schmorp.de, 20131102195825.2013.qmail@lists-nntp.develooper.com */
558#endif 528 while (!PadlistARRAY (padlist)[off - 1])
529 --off;
530
531 Perl_pad_push (aTHX_ padlist, off);
532
533 newpad = PadlistARRAY (padlist)[off];
534 PadlistARRAY (padlist)[off] = 0;
535
536#else
537
559#if PERL_VERSION_ATLEAST (5,10,0) 538#if PERL_VERSION_ATLEAST (5,10,0)
560 Perl_pad_push (aTHX_ padlist, off); 539 Perl_pad_push (aTHX_ padlist, off);
561#else 540#else
562 Perl_pad_push (aTHX_ padlist, off, 1); 541 Perl_pad_push (aTHX_ padlist, off, 1);
563#endif 542#endif
543
564 newpad = PadlistARRAY (padlist)[off]; 544 newpad = PadlistARRAY (padlist)[off];
565 PadlistMAX (padlist) = off - 1; 545 PadlistMAX (padlist) = off - 1;
546
547#endif
548
549 newPADLIST (newpadlist);
550#if !PERL_VERSION_ATLEAST(5,15,3)
551 /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */
552 AvREAL_off (newpadlist);
553#endif
566 554
567 /* Already extended to 2 elements by newPADLIST. */ 555 /* Already extended to 2 elements by newPADLIST. */
568 PadlistMAX (newpadlist) = 1; 556 PadlistMAX (newpadlist) = 1;
569 PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist)); 557 PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist));
570 PadlistARRAY (newpadlist)[1] = newpad; 558 PadlistARRAY (newpadlist)[1] = newpad;
582 570
583 while (i > 0) /* special-case index 0 */ 571 while (i > 0) /* special-case index 0 */
584 { 572 {
585 /* we try to be extra-careful here */ 573 /* we try to be extra-careful here */
586 PAD *pad = PadlistARRAY (padlist)[i--]; 574 PAD *pad = PadlistARRAY (padlist)[i--];
575
576 if (pad)
577 {
587 I32 j = PadMAX (pad); 578 I32 j = PadMAX (pad);
588 579
589 while (j >= 0) 580 while (j >= 0)
590 SvREFCNT_dec (PadARRAY (pad)[j--]); 581 SvREFCNT_dec (PadARRAY (pad)[j--]);
591 582
592 PadMAX (pad) = -1; 583 PadMAX (pad) = -1;
593 SvREFCNT_dec (pad); 584 SvREFCNT_dec (pad);
585 }
594 } 586 }
595 587
596 SvREFCNT_dec (PadlistNAMES (padlist)); 588 SvREFCNT_dec (PadlistNAMES (padlist));
597 589
598#ifdef NEWPADAPI 590#if NEWPADAPI
599 Safefree (PadlistARRAY (padlist)); 591 Safefree (PadlistARRAY (padlist));
600 Safefree (padlist); 592 Safefree (padlist);
601#else 593#else
602 AvFILLp (padlist) = -1; 594 AvFILLp (padlist) = -1;
603 AvREAL_off (padlist); 595 AvREAL_off (padlist);
1519static coro_cctx * 1511static coro_cctx *
1520cctx_new_empty (void) 1512cctx_new_empty (void)
1521{ 1513{
1522 coro_cctx *cctx = cctx_new (); 1514 coro_cctx *cctx = cctx_new ();
1523 1515
1524 cctx->sptr = 0; 1516 cctx->stack.sptr = 0;
1525 coro_create (&cctx->cctx, 0, 0, 0, 0); 1517 coro_create (&cctx->cctx, 0, 0, 0, 0);
1526 1518
1527 return cctx; 1519 return cctx;
1528} 1520}
1529 1521
1530/* create a new cctx suitable as destination/running a perl interpreter */ 1522/* create a new cctx suitable as destination/running a perl interpreter */
1531static coro_cctx * 1523static coro_cctx *
1532cctx_new_run (void) 1524cctx_new_run (void)
1533{ 1525{
1534 coro_cctx *cctx = cctx_new (); 1526 coro_cctx *cctx = cctx_new ();
1535 void *stack_start;
1536 size_t stack_size;
1537 1527
1538#if CORO_FIBER 1528 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 { 1529 {
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."); 1530 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1568 _exit (EXIT_FAILURE); 1531 _exit (EXIT_FAILURE);
1569 }
1570
1571 stack_start = cctx->sptr;
1572 stack_size = cctx->ssize;
1573 } 1532 }
1574 #endif
1575 1533
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); 1534 coro_create (&cctx->cctx, cctx_run, (void *)cctx, cctx->stack.sptr, cctx->stack.ssze);
1581 1535
1582 return cctx; 1536 return cctx;
1583} 1537}
1584 1538
1585static void 1539static void
1591 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); 1545 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));
1592 1546
1593 --cctx_count; 1547 --cctx_count;
1594 coro_destroy (&cctx->cctx); 1548 coro_destroy (&cctx->cctx);
1595 1549
1596 /* coro_transfer creates new, empty cctx's */ 1550 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 1551
1611 Safefree (cctx); 1552 Safefree (cctx);
1612} 1553}
1613 1554
1614/* wether this cctx should be destructed */ 1555/* wether this cctx should be destructed */
1633} 1574}
1634 1575
1635static void 1576static void
1636cctx_put (coro_cctx *cctx) 1577cctx_put (coro_cctx *cctx)
1637{ 1578{
1638 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); 1579 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->stack.sptr));
1639 1580
1640 /* free another cctx if overlimit */ 1581 /* free another cctx if overlimit */
1641 if (ecb_expect_false (cctx_idle >= cctx_max_idle)) 1582 if (ecb_expect_false (cctx_idle >= cctx_max_idle))
1642 { 1583 {
1643 coro_cctx *first = cctx_first; 1584 coro_cctx *first = cctx_first;
2182{ 2123{
2183 AV *od = coro->on_destroy; 2124 AV *od = coro->on_destroy;
2184 2125
2185 if (!od) 2126 if (!od)
2186 return; 2127 return;
2128
2129 coro->on_destroy = 0;
2130 sv_2mortal ((SV *)od);
2187 2131
2188 while (AvFILLp (od) >= 0) 2132 while (AvFILLp (od) >= 0)
2189 { 2133 {
2190 SV *cb = sv_2mortal (av_pop (od)); 2134 SV *cb = sv_2mortal (av_pop (od));
2191 2135
3235 /* it quickly returns */ 3179 /* it quickly returns */
3236 if (CORO_THROW) 3180 if (CORO_THROW)
3237 return 0; 3181 return 0;
3238 3182
3239 /* one element that is an RV? repeat! */ 3183 /* one element that is an RV? repeat! */
3240 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 3184 if (AvFILLp (state) == 0 && SvTYPE (AvARRAY (state)[0]) != SVt_PV)
3241 return 1; 3185 return 1;
3242 3186
3243 /* restore status */ 3187 /* restore status */
3244 { 3188 {
3245 SV *data_sv = av_pop (state); 3189 SV *data_sv = av_pop (state);
3478#ifdef USE_ITHREADS 3422#ifdef USE_ITHREADS
3479# if CORO_PTHREAD 3423# if CORO_PTHREAD
3480 coro_thx = PERL_GET_CONTEXT; 3424 coro_thx = PERL_GET_CONTEXT;
3481# endif 3425# endif
3482#endif 3426#endif
3483 BOOT_PAGESIZE;
3484
3485 /* perl defines these to check for existance first, but why it doesn't */ 3427 /* 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 */ 3428 /* just create them one at init time is not clear to me, except for */
3487 /* programs trying to delete them, but... */ 3429 /* programs trying to delete them, but... */
3488 /* anyway, we declare this as invalid and make sure they are initialised here */ 3430 /* anyway, we declare this as invalid and make sure they are initialised here */
3489 DEFSV; 3431 DEFSV;
3559void 3501void
3560transfer (...) 3502transfer (...)
3561 PROTOTYPE: $$ 3503 PROTOTYPE: $$
3562 CODE: 3504 CODE:
3563 CORO_EXECUTE_SLF_XS (slf_init_transfer); 3505 CORO_EXECUTE_SLF_XS (slf_init_transfer);
3564
3565void
3566_exit (int code)
3567 PROTOTYPE: $
3568 CODE:
3569 _exit (code);
3570 3506
3571SV * 3507SV *
3572clone (Coro::State coro) 3508clone (Coro::State coro)
3573 CODE: 3509 CODE:
3574{ 3510{
4326 { 4262 {
4327 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; 4263 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
4328 coro_old_pp_sselect = 0; 4264 coro_old_pp_sselect = 0;
4329 } 4265 }
4330 4266
4267MODULE = Coro::State PACKAGE = Coro::Util
4268
4269void
4270_exit (int code)
4271 CODE:
4272 _exit (code);
4273
4274NV
4275time ()
4276 CODE:
4277 RETVAL = nvtime (aTHX);
4278 OUTPUT:
4279 RETVAL
4280
4281NV
4282gettimeofday ()
4283 PPCODE:
4284{
4285 UV tv [2];
4286 u2time (aTHX_ tv);
4287 EXTEND (SP, 2);
4288 PUSHs (sv_2mortal (newSVuv (tv [0])));
4289 PUSHs (sv_2mortal (newSVuv (tv [1])));
4290}
4291

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines