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.420 by root, Fri Apr 13 10:53:25 2012 UTC vs.
Revision 1.448 by root, Thu Jun 4 22:58:28 2015 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
12#include "perl.h" 12#include "perl.h"
13#include "XSUB.h" 13#include "XSUB.h"
14#include "perliol.h" 14#include "perliol.h"
15 15
16#include "schmorp.h" 16#include "schmorp.h"
17
18#define ECB_NO_THREADS 1
19#define ECB_NO_LIBM 1
17#include "ecb.h" 20#include "ecb.h"
18 21
19#include <stddef.h> 22#include <stddef.h>
20#include <stdio.h> 23#include <stdio.h>
21#include <errno.h> 24#include <errno.h>
22#include <assert.h> 25#include <assert.h>
23 26
24#ifndef SVs_PADSTALE 27#ifndef SVs_PADSTALE
25# define SVs_PADSTALE 0 28# define SVs_PADSTALE 0
29#endif
30
31#ifdef PadARRAY
32# define NEWPADAPI 1
33# define newPADLIST(var) (Newz (0, var, 1, PADLIST), Newx (PadlistARRAY (var), 2, PAD *))
34#else
35typedef AV PADNAMELIST;
36# if !PERL_VERSION_ATLEAST(5,8,0)
37typedef AV PADLIST;
38typedef AV PAD;
39# endif
40# define PadlistARRAY(pl) ((PAD **)AvARRAY (pl))
41# define PadlistMAX(pl) AvFILLp (pl)
42# define PadlistNAMES(pl) (*PadlistARRAY (pl))
43# define PadARRAY AvARRAY
44# define PadMAX AvFILLp
45# define newPADLIST(var) ((var) = newAV (), av_extend (var, 1))
46#endif
47#ifndef PadnamelistREFCNT
48# define PadnamelistREFCNT(pnl) SvREFCNT (pnl)
49#endif
50#ifndef PadnamelistREFCNT_dec
51# define PadnamelistREFCNT_dec(pnl) SvREFCNT_dec (pnl)
52#endif
53
54/* 5.19.something has replaced SVt_BIND by SVt_INVLIST */
55/* we just alias it to SVt_IV, as that is sufficient for swap_sv for now */
56#if PERL_VERSION_ATLEAST(5,19,0)
57# define SVt_BIND SVt_IV
26#endif 58#endif
27 59
28#if defined(_WIN32) 60#if defined(_WIN32)
29# undef HAS_GETTIMEOFDAY 61# undef HAS_GETTIMEOFDAY
30# undef setjmp 62# undef setjmp
33# define setjmp _setjmp /* deep magic */ 65# define setjmp _setjmp /* deep magic */
34#else 66#else
35# include <inttypes.h> /* most portable stdint.h */ 67# include <inttypes.h> /* most portable stdint.h */
36#endif 68#endif
37 69
38#if HAVE_MMAP
39# include <unistd.h>
40# include <sys/mman.h>
41# ifndef MAP_ANONYMOUS
42# ifdef MAP_ANON
43# define MAP_ANONYMOUS MAP_ANON
44# else
45# undef HAVE_MMAP
46# endif
47# endif
48# include <limits.h>
49# ifndef PAGESIZE
50# define PAGESIZE pagesize
51# define BOOT_PAGESIZE pagesize = sysconf (_SC_PAGESIZE)
52static long pagesize;
53# else
54# define BOOT_PAGESIZE (void)0
55# endif
56#else
57# define PAGESIZE 0
58# define BOOT_PAGESIZE (void)0
59#endif
60
61#if CORO_USE_VALGRIND
62# include <valgrind/valgrind.h>
63#endif
64
65/* the maximum number of idle cctx that will be pooled */ 70/* the maximum number of idle cctx that will be pooled */
66static int cctx_max_idle = 4; 71static int cctx_max_idle = 4;
67 72
68#if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0) 73#if defined(DEBUGGING) && PERL_VERSION_ATLEAST(5,12,0)
69# define HAS_SCOPESTACK_NAME 1 74# define HAS_SCOPESTACK_NAME 1
70#endif
71
72#if !__i386 && !__x86_64 && !__powerpc && !__m68k && !__alpha && !__mips && !__sparc64
73# undef CORO_STACKGUARD
74#endif
75
76#ifndef CORO_STACKGUARD
77# define CORO_STACKGUARD 0
78#endif 75#endif
79 76
80/* prefer perl internal functions over our own? */ 77/* prefer perl internal functions over our own? */
81#ifndef CORO_PREFER_PERL_FUNCTIONS 78#ifndef CORO_PREFER_PERL_FUNCTIONS
82# define CORO_PREFER_PERL_FUNCTIONS 0 79# define CORO_PREFER_PERL_FUNCTIONS 0
173typedef struct coro_cctx 170typedef struct coro_cctx
174{ 171{
175 struct coro_cctx *next; 172 struct coro_cctx *next;
176 173
177 /* the stack */ 174 /* the stack */
178 void *sptr; 175 struct coro_stack stack;
179 size_t ssize;
180 176
181 /* cpu state */ 177 /* cpu state */
182 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 178 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
183#ifndef NDEBUG 179#ifndef NDEBUG
184 JMPENV *idle_te; /* same as idle_sp, but for top_env */ 180 JMPENV *idle_te; /* same as idle_sp, but for top_env */
293#define coro_nready coroapi.nready 289#define coro_nready coroapi.nready
294 290
295/** JIT *********************************************************************/ 291/** JIT *********************************************************************/
296 292
297#if CORO_JIT 293#if CORO_JIT
298 /* APPLE doesn't have HAVE_MMAP though */ 294 /* APPLE doesn't have mmap though */
299 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__) 295 #define CORO_JIT_UNIXY (__linux || __FreeBSD__ || __OpenBSD__ || __NetBSD__ || __solaris || __APPLE__)
300 #ifndef CORO_JIT_TYPE 296 #ifndef CORO_JIT_TYPE
301 #if __x86_64 && CORO_JIT_UNIXY 297 #if ECB_AMD64 && CORO_JIT_UNIXY
302 #define CORO_JIT_TYPE "amd64-unix" 298 #define CORO_JIT_TYPE "amd64-unix"
303 #elif __i386 && CORO_JIT_UNIXY 299 #elif __i386 && CORO_JIT_UNIXY
304 #define CORO_JIT_TYPE "x86-unix" 300 #define CORO_JIT_TYPE "x86-unix"
305 #endif 301 #endif
306 #endif 302 #endif
307#endif 303#endif
308 304
309#if !defined(CORO_JIT_TYPE) || !HAVE_MMAP 305#if !defined(CORO_JIT_TYPE) || _POSIX_MEMORY_PROTECTION <= 0
310 #undef CORO_JIT 306 #undef CORO_JIT
311#endif 307#endif
312 308
313#if CORO_JIT 309#if CORO_JIT
314 typedef void (*load_save_perl_slots_type)(perl_slots *); 310 typedef void (*load_save_perl_slots_type)(perl_slots *);
371time_init (pTHX) 367time_init (pTHX)
372{ 368{
373 SV **svp; 369 SV **svp;
374 370
375 require_pv ("Time/HiRes.pm"); 371 require_pv ("Time/HiRes.pm");
376 372
377 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); 373 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
378 374
379 if (!svp) croak ("Time::HiRes is required, but missing. Caught"); 375 if (!svp) croak ("Time::HiRes is required, but missing. Caught");
380 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); 376 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught");
381 377
522#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) 518#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
523 519
524/*****************************************************************************/ 520/*****************************************************************************/
525/* padlist management and caching */ 521/* padlist management and caching */
526 522
527ecb_inline AV * 523ecb_inline PADLIST *
528coro_derive_padlist (pTHX_ CV *cv) 524coro_derive_padlist (pTHX_ CV *cv)
529{ 525{
530 AV *padlist = CvPADLIST (cv); 526 PADLIST *padlist = CvPADLIST (cv);
531 AV *newpadlist, *newpad; 527 PADLIST *newpadlist;
528 PADNAMELIST *padnames;
529 PAD *newpad;
530 PADOFFSET off = PadlistMAX (padlist) + 1;
532 531
533 newpadlist = newAV (); 532#if NEWPADAPI
533
534 /* if we had the original CvDEPTH, we might be able to steal the CvDEPTH+1 entry instead */
535 /* 20131102194744.GA6705@schmorp.de, 20131102195825.2013.qmail@lists-nntp.develooper.com */
536 while (!PadlistARRAY (padlist)[off - 1])
537 --off;
538
539 Perl_pad_push (aTHX_ padlist, off);
540
541 newpad = PadlistARRAY (padlist)[off];
542 PadlistARRAY (padlist)[off] = 0;
543
544#else
545
546#if PERL_VERSION_ATLEAST (5,10,0)
547 Perl_pad_push (aTHX_ padlist, off);
548#else
549 Perl_pad_push (aTHX_ padlist, off, 1);
550#endif
551
552 newpad = PadlistARRAY (padlist)[off];
553 PadlistMAX (padlist) = off - 1;
554
555#endif
556
557 newPADLIST (newpadlist);
558#if !PERL_VERSION_ATLEAST(5,15,3)
559 /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */
534 AvREAL_off (newpadlist); 560 AvREAL_off (newpadlist);
535#if PERL_VERSION_ATLEAST (5,10,0)
536 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1);
537#else
538 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
539#endif 561#endif
540 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
541 --AvFILLp (padlist);
542 562
543 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); 563 /* Already extended to 2 elements by newPADLIST. */
544 av_store (newpadlist, 1, (SV *)newpad); 564 PadlistMAX (newpadlist) = 1;
565
566 padnames = PadlistNAMES (padlist);
567 ++PadnamelistREFCNT (padnames);
568 PadlistNAMES (newpadlist) = padnames;
569
570 PadlistARRAY (newpadlist)[1] = newpad;
545 571
546 return newpadlist; 572 return newpadlist;
547} 573}
548 574
549ecb_inline void 575ecb_inline void
550free_padlist (pTHX_ AV *padlist) 576free_padlist (pTHX_ PADLIST *padlist)
551{ 577{
552 /* may be during global destruction */ 578 /* may be during global destruction */
553 if (!IN_DESTRUCT) 579 if (!IN_DESTRUCT)
554 { 580 {
555 I32 i = AvFILLp (padlist); 581 I32 i = PadlistMAX (padlist);
556 582
557 while (i > 0) /* special-case index 0 */ 583 while (i > 0) /* special-case index 0 */
558 { 584 {
559 /* we try to be extra-careful here */ 585 /* we try to be extra-careful here */
560 AV *av = (AV *)AvARRAY (padlist)[i--]; 586 PAD *pad = PadlistARRAY (padlist)[i--];
561 I32 j = AvFILLp (av);
562 587
588 if (pad)
589 {
590 I32 j = PadMAX (pad);
591
563 while (j >= 0) 592 while (j >= 0)
564 SvREFCNT_dec (AvARRAY (av)[j--]); 593 SvREFCNT_dec (PadARRAY (pad)[j--]);
565 594
566 AvFILLp (av) = -1; 595 PadMAX (pad) = -1;
567 SvREFCNT_dec (av); 596 SvREFCNT_dec (pad);
597 }
568 } 598 }
569 599
570 SvREFCNT_dec (AvARRAY (padlist)[0]); 600 PadnamelistREFCNT_dec (PadlistNAMES (padlist));
571 601
602#if NEWPADAPI
603 Safefree (PadlistARRAY (padlist));
604 Safefree (padlist);
605#else
572 AvFILLp (padlist) = -1; 606 AvFILLp (padlist) = -1;
607 AvREAL_off (padlist);
573 SvREFCNT_dec ((SV*)padlist); 608 SvREFCNT_dec ((SV*)padlist);
609#endif
574 } 610 }
575} 611}
576 612
577static int 613static int
578coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 614coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
579{ 615{
580 AV *padlist; 616 PADLIST *padlist;
581 AV *av = (AV *)mg->mg_obj; 617 PADLIST **padlists = (PADLIST **)(mg->mg_ptr + sizeof(size_t));
618 size_t len = *(size_t *)mg->mg_ptr;
582 619
583 /* perl manages to free our internal AV and _then_ call us */ 620 /* perl manages to free our internal AV and _then_ call us */
584 if (IN_DESTRUCT) 621 if (IN_DESTRUCT)
585 return 0; 622 return 0;
586 623
587 /* casting is fun. */ 624 while (len--)
588 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
589 free_padlist (aTHX_ padlist); 625 free_padlist (aTHX_ padlists[len]);
590
591 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
592 626
593 return 0; 627 return 0;
594} 628}
595 629
596static MGVTBL coro_cv_vtbl = { 630static MGVTBL coro_cv_vtbl = {
601/* the next two functions merely cache the padlists */ 635/* the next two functions merely cache the padlists */
602ecb_inline void 636ecb_inline void
603get_padlist (pTHX_ CV *cv) 637get_padlist (pTHX_ CV *cv)
604{ 638{
605 MAGIC *mg = CORO_MAGIC_cv (cv); 639 MAGIC *mg = CORO_MAGIC_cv (cv);
606 AV *av; 640 size_t *lenp;
607 641
608 if (ecb_expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 642 if (ecb_expect_true (mg && *(lenp = (size_t *)mg->mg_ptr)))
609 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 643 CvPADLIST (cv) = ((PADLIST **)(mg->mg_ptr + sizeof(size_t)))[--*lenp];
610 else 644 else
611 { 645 {
612#if CORO_PREFER_PERL_FUNCTIONS 646#if CORO_PREFER_PERL_FUNCTIONS
613 /* this is probably cleaner? but also slower! */ 647 /* this is probably cleaner? but also slower! */
614 /* in practise, it seems to be less stable */ 648 /* in practise, it seems to be less stable */
624 658
625ecb_inline void 659ecb_inline void
626put_padlist (pTHX_ CV *cv) 660put_padlist (pTHX_ CV *cv)
627{ 661{
628 MAGIC *mg = CORO_MAGIC_cv (cv); 662 MAGIC *mg = CORO_MAGIC_cv (cv);
629 AV *av;
630 663
631 if (ecb_expect_false (!mg)) 664 if (ecb_expect_false (!mg))
665 {
632 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 666 mg = sv_magicext ((SV *)cv, 0, CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
667 Newz (0, mg->mg_ptr ,sizeof (size_t) + sizeof (PADLIST *), char);
668 mg->mg_len = 1; /* so mg_free frees mg_ptr */
669 }
670 else
671 Renew (mg->mg_ptr,
672 sizeof(size_t) + (*(size_t *)mg->mg_ptr + 1) * sizeof(PADLIST *),
673 char);
633 674
634 av = (AV *)mg->mg_obj; 675 ((PADLIST **)(mg->mg_ptr + sizeof (size_t))) [(*(size_t *)mg->mg_ptr)++] = CvPADLIST (cv);
635
636 if (ecb_expect_false (AvFILLp (av) >= AvMAX (av)))
637 av_extend (av, AvFILLp (av) + 1);
638
639 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
640} 676}
641 677
642/** load & save, init *******************************************************/ 678/** load & save, init *******************************************************/
643 679
644ecb_inline void 680ecb_inline void
719 /* now do the ugly restore mess */ 755 /* now do the ugly restore mess */
720 while (ecb_expect_true (cv = (CV *)POPs)) 756 while (ecb_expect_true (cv = (CV *)POPs))
721 { 757 {
722 put_padlist (aTHX_ cv); /* mark this padlist as available */ 758 put_padlist (aTHX_ cv); /* mark this padlist as available */
723 CvDEPTH (cv) = PTR2IV (POPs); 759 CvDEPTH (cv) = PTR2IV (POPs);
724 CvPADLIST (cv) = (AV *)POPs; 760 CvPADLIST (cv) = (PADLIST *)POPs;
725 } 761 }
726 762
727 PUTBACK; 763 PUTBACK;
728 } 764 }
729 765
992 { 1028 {
993 SV **svp = 0; 1029 SV **svp = 0;
994 1030
995 if (strEQ (s, "__DIE__" )) svp = &PL_diehook; 1031 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
996 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 1032 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
997 1033
998 if (svp) 1034 if (svp)
999 { 1035 {
1000 SV *ssv; 1036 SV *ssv;
1001 1037
1002 if (!*svp) 1038 if (!*svp)
1108 PL_hints = 0; 1144 PL_hints = 0;
1109 1145
1110 /* recreate the die/warn hooks */ 1146 /* recreate the die/warn hooks */
1111 PL_diehook = SvREFCNT_inc (rv_diehook); 1147 PL_diehook = SvREFCNT_inc (rv_diehook);
1112 PL_warnhook = SvREFCNT_inc (rv_warnhook); 1148 PL_warnhook = SvREFCNT_inc (rv_warnhook);
1113 1149
1114 GvSV (PL_defgv) = newSV (0); 1150 GvSV (PL_defgv) = newSV (0);
1115 GvAV (PL_defgv) = coro->args; coro->args = 0; 1151 GvAV (PL_defgv) = coro->args; coro->args = 0;
1116 GvSV (PL_errgv) = newSV (0); 1152 GvSV (PL_errgv) = newSV (0);
1117 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 1153 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
1118 GvHV (PL_hintgv) = 0; 1154 GvHV (PL_hintgv) = newHV ();
1155#if PERL_VERSION_ATLEAST (5,10,0)
1156 hv_magic (GvHV (PL_hintgv), 0, PERL_MAGIC_hints);
1157#endif
1119 PL_rs = newSVsv (GvSV (irsgv)); 1158 PL_rs = newSVsv (GvSV (irsgv));
1120 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 1159 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
1121 1160
1122 { 1161 {
1123 dSP; 1162 dSP;
1487static coro_cctx * 1526static coro_cctx *
1488cctx_new_empty (void) 1527cctx_new_empty (void)
1489{ 1528{
1490 coro_cctx *cctx = cctx_new (); 1529 coro_cctx *cctx = cctx_new ();
1491 1530
1492 cctx->sptr = 0; 1531 cctx->stack.sptr = 0;
1493 coro_create (&cctx->cctx, 0, 0, 0, 0); 1532 coro_create (&cctx->cctx, 0, 0, 0, 0);
1494 1533
1495 return cctx; 1534 return cctx;
1496} 1535}
1497 1536
1498/* create a new cctx suitable as destination/running a perl interpreter */ 1537/* create a new cctx suitable as destination/running a perl interpreter */
1499static coro_cctx * 1538static coro_cctx *
1500cctx_new_run (void) 1539cctx_new_run (void)
1501{ 1540{
1502 coro_cctx *cctx = cctx_new (); 1541 coro_cctx *cctx = cctx_new ();
1503 void *stack_start;
1504 size_t stack_size;
1505 1542
1506#if HAVE_MMAP 1543 if (!coro_stack_alloc (&cctx->stack, cctx_stacksize))
1507 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1508 /* mmap supposedly does allocate-on-write for us */
1509 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, 0, 0);
1510
1511 if (cctx->sptr != (void *)-1)
1512 {
1513 #if CORO_STACKGUARD
1514 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1515 #endif
1516 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1517 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1518 cctx->flags |= CC_MAPPED;
1519 } 1544 {
1520 else
1521#endif
1522 {
1523 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1524 New (0, cctx->sptr, cctx_stacksize, long);
1525
1526 if (!cctx->sptr)
1527 {
1528 perror ("FATAL: unable to allocate stack for coroutine, exiting."); 1545 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1529 _exit (EXIT_FAILURE); 1546 _exit (EXIT_FAILURE);
1530 }
1531
1532 stack_start = cctx->sptr;
1533 stack_size = cctx->ssize;
1534 } 1547 }
1535 1548
1536 #if CORO_USE_VALGRIND
1537 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1538 #endif
1539
1540 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size); 1549 coro_create (&cctx->cctx, cctx_run, (void *)cctx, cctx->stack.sptr, cctx->stack.ssze);
1541 1550
1542 return cctx; 1551 return cctx;
1543} 1552}
1544 1553
1545static void 1554static void
1551 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current)); 1560 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));
1552 1561
1553 --cctx_count; 1562 --cctx_count;
1554 coro_destroy (&cctx->cctx); 1563 coro_destroy (&cctx->cctx);
1555 1564
1556 /* coro_transfer creates new, empty cctx's */ 1565 coro_stack_free (&cctx->stack);
1557 if (cctx->sptr)
1558 {
1559 #if CORO_USE_VALGRIND
1560 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
1561 #endif
1562
1563#if HAVE_MMAP
1564 if (cctx->flags & CC_MAPPED)
1565 munmap (cctx->sptr, cctx->ssize);
1566 else
1567#endif
1568 Safefree (cctx->sptr);
1569 }
1570 1566
1571 Safefree (cctx); 1567 Safefree (cctx);
1572} 1568}
1573 1569
1574/* wether this cctx should be destructed */ 1570/* wether this cctx should be destructed */
1593} 1589}
1594 1590
1595static void 1591static void
1596cctx_put (coro_cctx *cctx) 1592cctx_put (coro_cctx *cctx)
1597{ 1593{
1598 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); 1594 assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->stack.sptr));
1599 1595
1600 /* free another cctx if overlimit */ 1596 /* free another cctx if overlimit */
1601 if (ecb_expect_false (cctx_idle >= cctx_max_idle)) 1597 if (ecb_expect_false (cctx_idle >= cctx_max_idle))
1602 { 1598 {
1603 coro_cctx *first = cctx_first; 1599 coro_cctx *first = cctx_first;
1729 return; 1725 return;
1730 1726
1731 slf_destroy (aTHX_ coro); 1727 slf_destroy (aTHX_ coro);
1732 1728
1733 coro->flags |= CF_ZOMBIE; 1729 coro->flags |= CF_ZOMBIE;
1734 1730
1735 if (coro->flags & CF_READY) 1731 if (coro->flags & CF_READY)
1736 { 1732 {
1737 /* reduce nready, as destroying a ready coro effectively unreadies it */ 1733 /* reduce nready, as destroying a ready coro effectively unreadies it */
1738 /* alternative: look through all ready queues and remove the coro */ 1734 /* alternative: look through all ready queues and remove the coro */
1739 --coro_nready; 1735 --coro_nready;
1935 /* nothing to schedule: call the idle handler */ 1931 /* nothing to schedule: call the idle handler */
1936 if (SvROK (sv_idle) 1932 if (SvROK (sv_idle)
1937 && SvOBJECT (SvRV (sv_idle))) 1933 && SvOBJECT (SvRV (sv_idle)))
1938 { 1934 {
1939 if (SvRV (sv_idle) == SvRV (coro_current)) 1935 if (SvRV (sv_idle) == SvRV (coro_current))
1936 {
1937 require_pv ("Carp");
1938
1939 {
1940 dSP;
1941
1942 ENTER;
1943 SAVETMPS;
1944
1945 PUSHMARK (SP);
1940 croak ("FATAL: $Coro::IDLE blocked itself - did you try to block inside an event loop callback? Caught"); 1946 XPUSHs (sv_2mortal (newSVpv ("FATAL: $Coro::idle blocked itself - did you try to block inside an event loop callback? Caught", 0)));
1947 PUTBACK;
1948 call_pv ("Carp::confess", G_VOID | G_DISCARD);
1949
1950 FREETMPS;
1951 LEAVE;
1952 }
1953 }
1941 1954
1942 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ 1955 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1943 api_ready (aTHX_ SvRV (sv_idle)); 1956 api_ready (aTHX_ SvRV (sv_idle));
1944 --coro_nready; 1957 --coro_nready;
1945 } 1958 }
2143 AV *od = coro->on_destroy; 2156 AV *od = coro->on_destroy;
2144 2157
2145 if (!od) 2158 if (!od)
2146 return; 2159 return;
2147 2160
2161 coro->on_destroy = 0;
2162 sv_2mortal ((SV *)od);
2163
2148 while (AvFILLp (od) >= 0) 2164 while (AvFILLp (od) >= 0)
2149 { 2165 {
2150 SV *cb = sv_2mortal (av_pop (od)); 2166 SV *cb = sv_2mortal (av_pop (od));
2151 2167
2152 /* coro hv's (and only hv's at the moment) are supported as well */ 2168 /* coro hv's (and only hv's at the moment) are supported as well */
2172 2188
2173static void 2189static void
2174coro_set_status (pTHX_ struct coro *coro, SV **arg, int items) 2190coro_set_status (pTHX_ struct coro *coro, SV **arg, int items)
2175{ 2191{
2176 AV *av; 2192 AV *av;
2177 2193
2178 if (coro->status) 2194 if (coro->status)
2179 { 2195 {
2180 av = coro->status; 2196 av = coro->status;
2181 av_clear (av); 2197 av_clear (av);
2182 } 2198 }
2229 2245
2230 coro = SvSTATE (arg [0]); 2246 coro = SvSTATE (arg [0]);
2231 coro_hv = coro->hv; 2247 coro_hv = coro->hv;
2232 2248
2233 coro_set_status (aTHX_ coro, arg + 1, items - 1); 2249 coro_set_status (aTHX_ coro, arg + 1, items - 1);
2234 2250
2235 if (ecb_expect_false (coro->flags & CF_NOCANCEL)) 2251 if (ecb_expect_false (coro->flags & CF_NOCANCEL))
2236 { 2252 {
2237 /* coro currently busy cancelling something, so just notify it */ 2253 /* coro currently busy cancelling something, so just notify it */
2238 coro->slf_frame.data = (void *)coro; 2254 coro->slf_frame.data = (void *)coro;
2239 2255
2246 slf_init_terminate_cancel_common (aTHX_ frame, coro_hv); 2262 slf_init_terminate_cancel_common (aTHX_ frame, coro_hv);
2247 } 2263 }
2248 else 2264 else
2249 { 2265 {
2250 struct coro *self = SvSTATE_current; 2266 struct coro *self = SvSTATE_current;
2267
2268 if (!self)
2269 croak ("Coro::cancel called outside of thread content,");
2251 2270
2252 /* otherwise we cancel directly, purely for speed reasons 2271 /* otherwise we cancel directly, purely for speed reasons
2253 * unfortunately, this requires some magic trickery, as 2272 * unfortunately, this requires some magic trickery, as
2254 * somebody else could cancel us, so we have to fight the cancellation. 2273 * somebody else could cancel us, so we have to fight the cancellation.
2255 * this is ugly, and hopefully fully worth the extra speed. 2274 * this is ugly, and hopefully fully worth the extra speed.
2418 2437
2419static int 2438static int
2420slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) 2439slf_check_rouse_wait (pTHX_ struct CoroSLF *frame)
2421{ 2440{
2422 SV *data = (SV *)frame->data; 2441 SV *data = (SV *)frame->data;
2423 2442
2424 if (CORO_THROW) 2443 if (CORO_THROW)
2425 return 0; 2444 return 0;
2426 2445
2427 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2446 if (SvTYPE (SvRV (data)) != SVt_PVAV)
2428 return 1; 2447 return 1;
2464 cb = sv_2mortal (coro->rouse_cb); 2483 cb = sv_2mortal (coro->rouse_cb);
2465 coro->rouse_cb = 0; 2484 coro->rouse_cb = 0;
2466 } 2485 }
2467 2486
2468 if (!SvROK (cb) 2487 if (!SvROK (cb)
2469 || SvTYPE (SvRV (cb)) != SVt_PVCV 2488 || SvTYPE (SvRV (cb)) != SVt_PVCV
2470 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) 2489 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
2471 croak ("Coro::rouse_wait called with illegal callback argument,"); 2490 croak ("Coro::rouse_wait called with illegal callback argument,");
2472 2491
2473 { 2492 {
2474 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ 2493 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */
2597 2616
2598/* "undo"/cancel a running slf call - used when cancelling a coro, mainly */ 2617/* "undo"/cancel a running slf call - used when cancelling a coro, mainly */
2599static void 2618static void
2600slf_destroy (pTHX_ struct coro *coro) 2619slf_destroy (pTHX_ struct coro *coro)
2601{ 2620{
2602 /* this callback is reserved for slf functions needing to do cleanup */ 2621 struct CoroSLF frame = coro->slf_frame;
2603 if (coro->slf_frame.destroy && coro->slf_frame.prepare && !PL_dirty)
2604 coro->slf_frame.destroy (aTHX_ &coro->slf_frame);
2605 2622
2606 /* 2623 /*
2607 * The on_destroy above most likely is from an SLF call. 2624 * The on_destroy below most likely is from an SLF call.
2608 * Since by definition the SLF call will not finish when we destroy 2625 * Since by definition the SLF call will not finish when we destroy
2609 * the coro, we will have to force-finish it here, otherwise 2626 * the coro, we will have to force-finish it here, otherwise
2610 * cleanup functions cannot call SLF functions. 2627 * cleanup functions cannot call SLF functions.
2611 */ 2628 */
2612 coro->slf_frame.prepare = 0; 2629 coro->slf_frame.prepare = 0;
2630
2631 /* this callback is reserved for slf functions needing to do cleanup */
2632 if (frame.destroy && frame.prepare && !PL_dirty)
2633 frame.destroy (aTHX_ &frame);
2613} 2634}
2614 2635
2615/* 2636/*
2616 * these not obviously related functions are all rolled into one 2637 * these not obviously related functions are all rolled into one
2617 * function to increase chances that they all will call transfer with the same 2638 * function to increase chances that they all will call transfer with the same
2957{ 2978{
2958 AV *av = (AV *)frame->data; 2979 AV *av = (AV *)frame->data;
2959 SV *count_sv = AvARRAY (av)[0]; 2980 SV *count_sv = AvARRAY (av)[0];
2960 SV *coro_hv = SvRV (coro_current); 2981 SV *coro_hv = SvRV (coro_current);
2961 2982
2983 frame->destroy = 0;
2984
2962 /* if we are about to throw, don't actually acquire the lock, just throw */ 2985 /* if we are about to throw, don't actually acquire the lock, just throw */
2963 if (CORO_THROW) 2986 if (ecb_expect_false (CORO_THROW))
2987 {
2988 /* we still might be responsible for the semaphore, so wake up others */
2989 coro_semaphore_adjust (aTHX_ av, 0);
2990
2964 return 0; 2991 return 0;
2992 }
2965 else if (SvIVX (count_sv) > 0) 2993 else if (SvIVX (count_sv) > 0)
2966 { 2994 {
2967 frame->destroy = 0;
2968
2969 if (acquire) 2995 if (acquire)
2970 SvIVX (count_sv) = SvIVX (count_sv) - 1; 2996 SvIVX (count_sv) = SvIVX (count_sv) - 1;
2971 else 2997 else
2972 coro_semaphore_adjust (aTHX_ av, 0); 2998 coro_semaphore_adjust (aTHX_ av, 0);
2973 2999
3195 /* it quickly returns */ 3221 /* it quickly returns */
3196 if (CORO_THROW) 3222 if (CORO_THROW)
3197 return 0; 3223 return 0;
3198 3224
3199 /* one element that is an RV? repeat! */ 3225 /* one element that is an RV? repeat! */
3200 if (AvFILLp (state) == 0 && SvROK (AvARRAY (state)[0])) 3226 if (AvFILLp (state) == 0 && SvTYPE (AvARRAY (state)[0]) != SVt_PV)
3201 return 1; 3227 return 1;
3202 3228
3203 /* restore status */ 3229 /* restore status */
3204 { 3230 {
3205 SV *data_sv = av_pop (state); 3231 SV *data_sv = av_pop (state);
3433 3459
3434PROTOTYPES: DISABLE 3460PROTOTYPES: DISABLE
3435 3461
3436BOOT: 3462BOOT:
3437{ 3463{
3464#define VARx(name,expr,type) if (sizeof (type) < sizeof (expr)) croak ("FATAL: Coro thread context slot '" # name "' too small for this version of perl.");
3465#include "state.h"
3438#ifdef USE_ITHREADS 3466#ifdef USE_ITHREADS
3439# if CORO_PTHREAD 3467# if CORO_PTHREAD
3440 coro_thx = PERL_GET_CONTEXT; 3468 coro_thx = PERL_GET_CONTEXT;
3441# endif 3469# endif
3442#endif 3470#endif
3443 BOOT_PAGESIZE;
3444
3445 /* perl defines these to check for existance first, but why it doesn't */ 3471 /* perl defines these to check for existance first, but why it doesn't */
3446 /* just create them one at init time is not clear to me, except for */ 3472 /* just create them one at init time is not clear to me, except for */
3447 /* programs trying to delete them, but... */ 3473 /* programs trying to delete them, but... */
3448 /* anyway, we declare this as invalid and make sure they are initialised here */ 3474 /* anyway, we declare this as invalid and make sure they are initialised here */
3449 DEFSV; 3475 DEFSV;
3519void 3545void
3520transfer (...) 3546transfer (...)
3521 PROTOTYPE: $$ 3547 PROTOTYPE: $$
3522 CODE: 3548 CODE:
3523 CORO_EXECUTE_SLF_XS (slf_init_transfer); 3549 CORO_EXECUTE_SLF_XS (slf_init_transfer);
3524
3525void
3526_exit (int code)
3527 PROTOTYPE: $
3528 CODE:
3529 _exit (code);
3530 3550
3531SV * 3551SV *
3532clone (Coro::State coro) 3552clone (Coro::State coro)
3533 CODE: 3553 CODE:
3534{ 3554{
3589void 3609void
3590list () 3610list ()
3591 PROTOTYPE: 3611 PROTOTYPE:
3592 PPCODE: 3612 PPCODE:
3593{ 3613{
3594 struct coro *coro; 3614 struct coro *coro;
3595 for (coro = coro_first; coro; coro = coro->next) 3615 for (coro = coro_first; coro; coro = coro->next)
3596 if (coro->hv) 3616 if (coro->hv)
3597 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 3617 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
3598} 3618}
3599 3619
3663 RETVAL = boolSV (coro->flags & ix); 3683 RETVAL = boolSV (coro->flags & ix);
3664 OUTPUT: 3684 OUTPUT:
3665 RETVAL 3685 RETVAL
3666 3686
3667void 3687void
3668throw (Coro::State self, SV *exception = &PL_sv_undef) 3688throw (SV *self, SV *exception = &PL_sv_undef)
3669 PROTOTYPE: $;$ 3689 PROTOTYPE: $;$
3670 CODE: 3690 CODE:
3671{ 3691{
3692 struct coro *coro = SvSTATE (self);
3672 struct coro *current = SvSTATE_current; 3693 struct coro *current = SvSTATE_current;
3673 SV **exceptionp = self == current ? &CORO_THROW : &self->except; 3694 SV **exceptionp = coro == current ? &CORO_THROW : &coro->except;
3674 SvREFCNT_dec (*exceptionp); 3695 SvREFCNT_dec (*exceptionp);
3675 SvGETMAGIC (exception); 3696 SvGETMAGIC (exception);
3676 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; 3697 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3698
3699 api_ready (aTHX_ self);
3677} 3700}
3678 3701
3679void 3702void
3680api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3703api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3681 PROTOTYPE: $;$ 3704 PROTOTYPE: $;$
3758 3781
3759void 3782void
3760times (Coro::State self) 3783times (Coro::State self)
3761 PPCODE: 3784 PPCODE:
3762{ 3785{
3763 struct coro *current = SvSTATE (coro_current); 3786 struct coro *current = SvSTATE (coro_current);
3764 3787
3765 if (ecb_expect_false (current == self)) 3788 if (ecb_expect_false (current == self))
3766 { 3789 {
3767 coro_times_update (); 3790 coro_times_update ();
3768 coro_times_add (SvSTATE (coro_current)); 3791 coro_times_add (SvSTATE (coro_current));
3811 3834
3812 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3835 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3813 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3836 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3814 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3837 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3815 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ 3838 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */
3816 3839
3817 coro_stash = gv_stashpv ("Coro", TRUE); 3840 coro_stash = gv_stashpv ("Coro", TRUE);
3818 3841
3819 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); 3842 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3820 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); 3843 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3821 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); 3844 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
4043 on_leave = 1 4066 on_leave = 1
4044 PROTOTYPE: & 4067 PROTOTYPE: &
4045 CODE: 4068 CODE:
4046{ 4069{
4047 struct coro *coro = SvSTATE_current; 4070 struct coro *coro = SvSTATE_current;
4048 AV **avp = ix ? &coro->on_leave : &coro->on_enter; 4071 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
4049 4072
4050 block = s_get_cv_croak (block); 4073 block = s_get_cv_croak (block);
4051 4074
4052 if (!*avp) 4075 if (!*avp)
4053 *avp = newAV (); 4076 *avp = newAV ();
4073 4096
4074SV * 4097SV *
4075new (SV *klass, SV *count = 0) 4098new (SV *klass, SV *count = 0)
4076 CODE: 4099 CODE:
4077{ 4100{
4078 int semcnt = 1; 4101 int semcnt = 1;
4079 4102
4080 if (count) 4103 if (count)
4081 { 4104 {
4082 SvGETMAGIC (count); 4105 SvGETMAGIC (count);
4083 4106
4143 XSRETURN_NO; 4166 XSRETURN_NO;
4144} 4167}
4145 4168
4146void 4169void
4147waiters (SV *self) 4170waiters (SV *self)
4148 PPCODE: 4171 PPCODE:
4149{ 4172{
4150 AV *av = (AV *)SvRV (self); 4173 AV *av = (AV *)SvRV (self);
4151 int wcount = AvFILLp (av) + 1 - 1; 4174 int wcount = AvFILLp (av) + 1 - 1;
4152 4175
4153 if (GIMME_V == G_SCALAR) 4176 if (GIMME_V == G_SCALAR)
4165 4188
4166void 4189void
4167_may_delete (SV *sem, int count, unsigned int extra_refs) 4190_may_delete (SV *sem, int count, unsigned int extra_refs)
4168 PPCODE: 4191 PPCODE:
4169{ 4192{
4170 AV *av = (AV *)SvRV (sem); 4193 AV *av = (AV *)SvRV (sem);
4171 4194
4172 if (SvREFCNT ((SV *)av) == 1 + extra_refs 4195 if (SvREFCNT ((SV *)av) == 1 + extra_refs
4173 && AvFILLp (av) == 0 /* no waiters, just count */ 4196 && AvFILLp (av) == 0 /* no waiters, just count */
4174 && SvIV (AvARRAY (av)[0]) == count) 4197 && SvIV (AvARRAY (av)[0]) == count)
4175 XSRETURN_YES; 4198 XSRETURN_YES;
4196 4219
4197void 4220void
4198broadcast (SV *self) 4221broadcast (SV *self)
4199 CODE: 4222 CODE:
4200{ 4223{
4201 AV *av = (AV *)SvRV (self); 4224 AV *av = (AV *)SvRV (self);
4202 coro_signal_wake (aTHX_ av, AvFILLp (av)); 4225 coro_signal_wake (aTHX_ av, AvFILLp (av));
4203} 4226}
4204 4227
4205void 4228void
4206send (SV *self) 4229send (SV *self)
4214 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ 4237 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */
4215} 4238}
4216 4239
4217IV 4240IV
4218awaited (SV *self) 4241awaited (SV *self)
4219 CODE: 4242 CODE:
4220 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; 4243 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1;
4221 OUTPUT: 4244 OUTPUT:
4222 RETVAL 4245 RETVAL
4223 4246
4224 4247
4229 4252
4230void 4253void
4231_schedule (...) 4254_schedule (...)
4232 CODE: 4255 CODE:
4233{ 4256{
4234 static int incede; 4257 static int incede;
4235 4258
4236 api_cede_notself (aTHX); 4259 api_cede_notself (aTHX);
4237 4260
4238 ++incede; 4261 ++incede;
4239 while (coro_nready >= incede && api_cede (aTHX)) 4262 while (coro_nready >= incede && api_cede (aTHX))
4283 { 4306 {
4284 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect; 4307 PL_ppaddr [OP_SSELECT] = coro_old_pp_sselect;
4285 coro_old_pp_sselect = 0; 4308 coro_old_pp_sselect = 0;
4286 } 4309 }
4287 4310
4311MODULE = Coro::State PACKAGE = Coro::Util
4312
4313void
4314_exit (int code)
4315 CODE:
4316 _exit (code);
4317
4318NV
4319time ()
4320 CODE:
4321 RETVAL = nvtime (aTHX);
4322 OUTPUT:
4323 RETVAL
4324
4325NV
4326gettimeofday ()
4327 PPCODE:
4328{
4329 UV tv [2];
4330 u2time (aTHX_ tv);
4331 EXTEND (SP, 2);
4332 PUSHs (sv_2mortal (newSVuv (tv [0])));
4333 PUSHs (sv_2mortal (newSVuv (tv [1])));
4334}
4335

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines