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.426 by root, Wed Dec 5 00:19:57 2012 UTC vs.
Revision 1.436 by root, Tue Nov 5 14:38:46 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;
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 ECB_AMD64 && 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 *);
547coro_derive_padlist (pTHX_ CV *cv) 511coro_derive_padlist (pTHX_ CV *cv)
548{ 512{
549 PADLIST *padlist = CvPADLIST (cv); 513 PADLIST *padlist = CvPADLIST (cv);
550 PADLIST *newpadlist; 514 PADLIST *newpadlist;
551 PAD *newpad; 515 PAD *newpad;
552 PADOFFSET const off = PadlistMAX (padlist) + 1; 516 PADOFFSET off = PadlistMAX (padlist) + 1;
553 517
554 newPADLIST(newpadlist); 518#if NEWPADAPI
555#if !PERL_VERSION_ATLEAST(5,15,3) 519
556 /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */ 520 /* if we had the original CvDEPTH, we might be able to steal the CvDEPTH+1 entry instead */
557 AvREAL_off (newpadlist); 521 /* 20131102194744.GA6705@schmorp.de, 20131102195825.2013.qmail@lists-nntp.develooper.com */
558#endif 522 while (!PadlistARRAY (padlist)[off - 1])
523 --off;
524
525 Perl_pad_push (aTHX_ padlist, off);
526
527 newpad = PadlistARRAY (padlist)[off];
528 PadlistARRAY (padlist)[off] = 0;
529
530#else
531
559#if PERL_VERSION_ATLEAST (5,10,0) 532#if PERL_VERSION_ATLEAST (5,10,0)
560 Perl_pad_push (aTHX_ padlist, off); 533 Perl_pad_push (aTHX_ padlist, off);
561#else 534#else
562 Perl_pad_push (aTHX_ padlist, off, 1); 535 Perl_pad_push (aTHX_ padlist, off, 1);
563#endif 536#endif
537
564 newpad = PadlistARRAY (padlist)[off]; 538 newpad = PadlistARRAY (padlist)[off];
565 PadlistMAX (padlist) = off - 1; 539 PadlistMAX (padlist) = off - 1;
540
541#endif
542
543 newPADLIST (newpadlist);
544#if !PERL_VERSION_ATLEAST(5,15,3)
545 /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */
546 AvREAL_off (newpadlist);
547#endif
566 548
567 /* Already extended to 2 elements by newPADLIST. */ 549 /* Already extended to 2 elements by newPADLIST. */
568 PadlistMAX (newpadlist) = 1; 550 PadlistMAX (newpadlist) = 1;
569 PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist)); 551 PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist));
570 PadlistARRAY (newpadlist)[1] = newpad; 552 PadlistARRAY (newpadlist)[1] = newpad;
593 SvREFCNT_dec (pad); 575 SvREFCNT_dec (pad);
594 } 576 }
595 577
596 SvREFCNT_dec (PadlistNAMES (padlist)); 578 SvREFCNT_dec (PadlistNAMES (padlist));
597 579
598#ifdef NEWPADAPI 580#if NEWPADAPI
599 Safefree (PadlistARRAY (padlist)); 581 Safefree (PadlistARRAY (padlist));
600 Safefree (padlist); 582 Safefree (padlist);
601#else 583#else
602 AvFILLp (padlist) = -1; 584 AvFILLp (padlist) = -1;
603 AvREAL_off (padlist); 585 AvREAL_off (padlist);
1519static coro_cctx * 1501static coro_cctx *
1520cctx_new_empty (void) 1502cctx_new_empty (void)
1521{ 1503{
1522 coro_cctx *cctx = cctx_new (); 1504 coro_cctx *cctx = cctx_new ();
1523 1505
1524 cctx->sptr = 0; 1506 cctx->stack.sptr = 0;
1525 coro_create (&cctx->cctx, 0, 0, 0, 0); 1507 coro_create (&cctx->cctx, 0, 0, 0, 0);
1526 1508
1527 return cctx; 1509 return cctx;
1528} 1510}
1529 1511
1530/* create a new cctx suitable as destination/running a perl interpreter */ 1512/* create a new cctx suitable as destination/running a perl interpreter */
1531static coro_cctx * 1513static coro_cctx *
1532cctx_new_run (void) 1514cctx_new_run (void)
1533{ 1515{
1534 coro_cctx *cctx = cctx_new (); 1516 coro_cctx *cctx = cctx_new ();
1535 void *stack_start;
1536 size_t stack_size;
1537 1517
1538#if HAVE_MMAP 1518 if (!coro_stack_alloc (&cctx->stack, cctx_stacksize))
1539 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1540 /* mmap supposedly does allocate-on-write for us */
1541 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0);
1542
1543 if (cctx->sptr != (void *)-1)
1544 {
1545 #if CORO_STACKGUARD
1546 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1547 #endif
1548 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1549 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1550 cctx->flags |= CC_MAPPED;
1551 } 1519 {
1552 else
1553#endif
1554 {
1555 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1556 New (0, cctx->sptr, cctx_stacksize, long);
1557
1558 if (!cctx->sptr)
1559 {
1560 perror ("FATAL: unable to allocate stack for coroutine, exiting."); 1520 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1561 _exit (EXIT_FAILURE); 1521 _exit (EXIT_FAILURE);
1562 }
1563
1564 stack_start = cctx->sptr;
1565 stack_size = cctx->ssize;
1566 } 1522 }
1567 1523
1568 #if CORO_USE_VALGRIND
1569 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1570 #endif
1571
1572 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); 1524 coro_create (&cctx->cctx, cctx_run, (void *)cctx, cctx->stack.sptr, cctx->stack.ssze);
1573 1525
1574 return cctx; 1526 return cctx;
1575} 1527}
1576 1528
1577static void 1529static void
1583 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); 1535 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));
1584 1536
1585 --cctx_count; 1537 --cctx_count;
1586 coro_destroy (&cctx->cctx); 1538 coro_destroy (&cctx->cctx);
1587 1539
1588 /* coro_transfer creates new, empty cctx's */ 1540 coro_stack_free (&cctx->stack);
1589 if (cctx->sptr)
1590 {
1591 #if CORO_USE_VALGRIND
1592 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1593 #endif
1594
1595#if HAVE_MMAP
1596 if (cctx->flags & CC_MAPPED)
1597 munmap (cctx->sptr, cctx->ssize);
1598 else
1599#endif
1600 Safefree (cctx->sptr);
1601 }
1602 1541
1603 Safefree (cctx); 1542 Safefree (cctx);
1604} 1543}
1605 1544
1606/* wether this cctx should be destructed */ 1545/* wether this cctx should be destructed */
1625} 1564}
1626 1565
1627static void 1566static void
1628cctx_put (coro_cctx *cctx) 1567cctx_put (coro_cctx *cctx)
1629{ 1568{
1630 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); 1569 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->stack.sptr));
1631 1570
1632 /* free another cctx if overlimit */ 1571 /* free another cctx if overlimit */
1633 if (ecb_expect_false (cctx_idle >= cctx_max_idle)) 1572 if (ecb_expect_false (cctx_idle >= cctx_max_idle))
1634 { 1573 {
1635 coro_cctx *first = cctx_first; 1574 coro_cctx *first = cctx_first;
2174{ 2113{
2175 AV *od = coro->on_destroy; 2114 AV *od = coro->on_destroy;
2176 2115
2177 if (!od) 2116 if (!od)
2178 return; 2117 return;
2118
2119 coro->on_destroy = 0;
2120 sv_2mortal ((SV *)od);
2179 2121
2180 while (AvFILLp (od) >= 0) 2122 while (AvFILLp (od) >= 0)
2181 { 2123 {
2182 SV *cb = sv_2mortal (av_pop (od)); 2124 SV *cb = sv_2mortal (av_pop (od));
2183 2125
3227 /* it quickly returns */ 3169 /* it quickly returns */
3228 if (CORO_THROW) 3170 if (CORO_THROW)
3229 return 0; 3171 return 0;
3230 3172
3231 /* one element that is an RV? repeat! */ 3173 /* one element that is an RV? repeat! */
3232 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 3174 if (AvFILLp (state) == 0 && SvTYPE (AvARRAY (state)[0]) != SVt_PV)
3233 return 1; 3175 return 1;
3234 3176
3235 /* restore status */ 3177 /* restore status */
3236 { 3178 {
3237 SV *data_sv = av_pop (state); 3179 SV *data_sv = av_pop (state);
3470#ifdef USE_ITHREADS 3412#ifdef USE_ITHREADS
3471# if CORO_PTHREAD 3413# if CORO_PTHREAD
3472 coro_thx = PERL_GET_CONTEXT; 3414 coro_thx = PERL_GET_CONTEXT;
3473# endif 3415# endif
3474#endif 3416#endif
3475 BOOT_PAGESIZE;
3476
3477 /* perl defines these to check for existance first, but why it doesn't */ 3417 /* perl defines these to check for existance first, but why it doesn't */
3478 /* just create them one at init time is not clear to me, except for */ 3418 /* just create them one at init time is not clear to me, except for */
3479 /* programs trying to delete them, but... */ 3419 /* programs trying to delete them, but... */
3480 /* anyway, we declare this as invalid and make sure they are initialised here */ 3420 /* anyway, we declare this as invalid and make sure they are initialised here */
3481 DEFSV; 3421 DEFSV;
3551void 3491void
3552transfer (...) 3492transfer (...)
3553 PROTOTYPE: $$ 3493 PROTOTYPE: $$
3554 CODE: 3494 CODE:
3555 CORO_EXECUTE_SLF_XS (slf_init_transfer); 3495 CORO_EXECUTE_SLF_XS (slf_init_transfer);
3556
3557void
3558_exit (int code)
3559 PROTOTYPE: $
3560 CODE:
3561 _exit (code);
3562 3496
3563SV * 3497SV *
3564clone (Coro::State coro) 3498clone (Coro::State coro)
3565 CODE: 3499 CODE:
3566{ 3500{
4318 { 4252 {
4319 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; 4253 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
4320 coro_old_pp_sselect = 0; 4254 coro_old_pp_sselect = 0;
4321 } 4255 }
4322 4256
4257MODULE = Coro::State PACKAGE = Coro::Util
4258
4259void
4260_exit (int code)
4261 CODE:
4262 _exit (code);
4263
4264NV
4265time ()
4266 CODE:
4267 RETVAL = nvtime (aTHX);
4268 OUTPUT:
4269 RETVAL
4270
4271NV
4272gettimeofday ()
4273 PPCODE:
4274{
4275 UV tv [2];
4276 u2time (aTHX_ tv);
4277 EXTEND (SP, 2);
4278 PUSHs (sv_2mortal (newSVuv (tv [0])));
4279 PUSHs (sv_2mortal (newSVuv (tv [1])));
4280}
4281

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines