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.415 by root, Tue Aug 30 07:48:53 2011 UTC vs.
Revision 1.427 by root, Wed Dec 5 14:35:34 2012 UTC

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
17#include "ecb.h" 19#include "ecb.h"
18 20
19#include <stddef.h> 21#include <stddef.h>
20#include <stdio.h> 22#include <stdio.h>
21#include <errno.h> 23#include <errno.h>
22#include <assert.h> 24#include <assert.h>
23 25
24#ifndef SVs_PADSTALE 26#ifndef SVs_PADSTALE
25# define SVs_PADSTALE 0 27# define SVs_PADSTALE 0
28#endif
29
30#ifdef PadARRAY
31# define NEWPADAPI
32# define newPADLIST(var) (Newz (0, var, 1, PADLIST), Newx (PadlistARRAY (var), 2, PAD *))
33#else
34typedef AV PADNAMELIST;
35# if !PERL_VERSION_ATLEAST(5,8,0)
36typedef AV PADLIST;
37typedef AV PAD;
38# endif
39# define PadlistARRAY(pl) ((PAD **)AvARRAY (pl))
40# define PadlistMAX(pl) AvFILLp (pl)
41# define PadlistNAMES(pl) (*PadlistARRAY (pl))
42# define PadARRAY AvARRAY
43# define PadMAX AvFILLp
44# define newPADLIST(var) ((var) = newAV (), av_extend (var, 1))
26#endif 45#endif
27 46
28#if defined(_WIN32) 47#if defined(_WIN32)
29# undef HAS_GETTIMEOFDAY 48# undef HAS_GETTIMEOFDAY
30# undef setjmp 49# undef setjmp
101# if CORO_PTHREAD 120# if CORO_PTHREAD
102static void *coro_thx; 121static void *coro_thx;
103# endif 122# endif
104#endif 123#endif
105 124
106/* used in state.h */
107#define VAR(name,type) VARx(name, PL_ ## name, type)
108
109#ifdef __linux 125#ifdef __linux
110# include <time.h> /* for timespec */ 126# include <time.h> /* for timespec */
111# include <syscall.h> /* for SYS_* */ 127# include <syscall.h> /* for SYS_* */
112# ifdef SYS_clock_gettime 128# ifdef SYS_clock_gettime
113# define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts)) 129# define coro_clock_gettime(id, ts) syscall (SYS_clock_gettime, (id), (ts))
181 void *sptr; 197 void *sptr;
182 size_t ssize; 198 size_t ssize;
183 199
184 /* cpu state */ 200 /* cpu state */
185 void *idle_sp; /* sp of top-level transfer/schedule/cede call */ 201 void *idle_sp; /* sp of top-level transfer/schedule/cede call */
202#ifndef NDEBUG
186 JMPENV *idle_te; /* same as idle_sp, but for top_env, TODO: remove once stable */ 203 JMPENV *idle_te; /* same as idle_sp, but for top_env */
204#endif
187 JMPENV *top_env; 205 JMPENV *top_env;
188 coro_context cctx; 206 coro_context cctx;
189 207
190 U32 gen; 208 U32 gen;
191#if CORO_USE_VALGRIND 209#if CORO_USE_VALGRIND
211}; 229};
212 230
213/* the structure where most of the perl state is stored, overlaid on the cxstack */ 231/* the structure where most of the perl state is stored, overlaid on the cxstack */
214typedef struct 232typedef struct
215{ 233{
216#define VARx(name,expr,type) type name; 234 #define VARx(name,expr,type) type name;
217# include "state.h" 235 #include "state.h"
218#undef VARx
219} perl_slots; 236} perl_slots;
220 237
221/* how many context stack entries do we need for perl_slots */ 238/* how many context stack entries do we need for perl_slots */
222#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT)) 239#define SLOT_COUNT ((sizeof (perl_slots) + sizeof (PERL_CONTEXT) - 1) / sizeof (PERL_CONTEXT))
223 240
373time_init (pTHX) 390time_init (pTHX)
374{ 391{
375 SV **svp; 392 SV **svp;
376 393
377 require_pv ("Time/HiRes.pm"); 394 require_pv ("Time/HiRes.pm");
378 395
379 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0); 396 svp = hv_fetch (PL_modglobal, "Time::NVtime", 12, 0);
380 397
381 if (!svp) croak ("Time::HiRes is required, but missing. Caught"); 398 if (!svp) croak ("Time::HiRes is required, but missing. Caught");
382 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught"); 399 if (!SvIOK (*svp)) croak ("Time::NVtime isn't a function pointer. Caught");
383 400
524#define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) 541#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
525 542
526/*****************************************************************************/ 543/*****************************************************************************/
527/* padlist management and caching */ 544/* padlist management and caching */
528 545
529ecb_inline AV * 546ecb_inline PADLIST *
530coro_derive_padlist (pTHX_ CV *cv) 547coro_derive_padlist (pTHX_ CV *cv)
531{ 548{
532 AV *padlist = CvPADLIST (cv); 549 PADLIST *padlist = CvPADLIST (cv);
533 AV *newpadlist, *newpad; 550 PADLIST *newpadlist;
551 PAD *newpad;
552 PADOFFSET const off = PadlistMAX (padlist) + 1;
534 553
535 newpadlist = newAV (); 554 newPADLIST(newpadlist);
555#if !PERL_VERSION_ATLEAST(5,15,3)
556 /* Padlists are AvREAL as of 5.15.3. See perl bug #98092 and perl commit 7d953ba. */
536 AvREAL_off (newpadlist); 557 AvREAL_off (newpadlist);
558#endif
537#if PERL_VERSION_ATLEAST (5,10,0) 559#if PERL_VERSION_ATLEAST (5,10,0)
538 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1); 560 Perl_pad_push (aTHX_ padlist, off);
539#else 561#else
540 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 562 Perl_pad_push (aTHX_ padlist, off, 1);
541#endif 563#endif
542 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 564 newpad = PadlistARRAY (padlist)[off];
543 --AvFILLp (padlist); 565 PadlistMAX (padlist) = off - 1;
544 566
545 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0])); 567 /* Already extended to 2 elements by newPADLIST. */
546 av_store (newpadlist, 1, (SV *)newpad); 568 PadlistMAX (newpadlist) = 1;
569 PadlistNAMES (newpadlist) = (PADNAMELIST *)SvREFCNT_inc_NN (PadlistNAMES (padlist));
570 PadlistARRAY (newpadlist)[1] = newpad;
547 571
548 return newpadlist; 572 return newpadlist;
549} 573}
550 574
551ecb_inline void 575ecb_inline void
552free_padlist (pTHX_ AV *padlist) 576free_padlist (pTHX_ PADLIST *padlist)
553{ 577{
554 /* may be during global destruction */ 578 /* may be during global destruction */
555 if (!IN_DESTRUCT) 579 if (!IN_DESTRUCT)
556 { 580 {
557 I32 i = AvFILLp (padlist); 581 I32 i = PadlistMAX (padlist);
558 582
559 while (i > 0) /* special-case index 0 */ 583 while (i > 0) /* special-case index 0 */
560 { 584 {
561 /* we try to be extra-careful here */ 585 /* we try to be extra-careful here */
562 AV *av = (AV *)AvARRAY (padlist)[i--]; 586 PAD *pad = PadlistARRAY (padlist)[i--];
563 I32 j = AvFILLp (av); 587 I32 j = PadMAX (pad);
564 588
565 while (j >= 0) 589 while (j >= 0)
566 SvREFCNT_dec (AvARRAY (av)[j--]); 590 SvREFCNT_dec (PadARRAY (pad)[j--]);
567 591
568 AvFILLp (av) = -1; 592 PadMAX (pad) = -1;
569 SvREFCNT_dec (av); 593 SvREFCNT_dec (pad);
570 } 594 }
571 595
572 SvREFCNT_dec (AvARRAY (padlist)[0]); 596 SvREFCNT_dec (PadlistNAMES (padlist));
573 597
598#ifdef NEWPADAPI
599 Safefree (PadlistARRAY (padlist));
600 Safefree (padlist);
601#else
574 AvFILLp (padlist) = -1; 602 AvFILLp (padlist) = -1;
603 AvREAL_off (padlist);
575 SvREFCNT_dec ((SV*)padlist); 604 SvREFCNT_dec ((SV*)padlist);
605#endif
576 } 606 }
577} 607}
578 608
579static int 609static int
580coro_cv_free (pTHX_ SV *sv, MAGIC *mg) 610coro_cv_free (pTHX_ SV *sv, MAGIC *mg)
581{ 611{
582 AV *padlist; 612 PADLIST *padlist;
583 AV *av = (AV *)mg->mg_obj; 613 PADLIST **padlists = (PADLIST **)(mg->mg_ptr + sizeof(size_t));
614 size_t len = *(size_t *)mg->mg_ptr;
584 615
585 /* perl manages to free our internal AV and _then_ call us */ 616 /* perl manages to free our internal AV and _then_ call us */
586 if (IN_DESTRUCT) 617 if (IN_DESTRUCT)
587 return 0; 618 return 0;
588 619
589 /* casting is fun. */ 620 while (len--)
590 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
591 free_padlist (aTHX_ padlist); 621 free_padlist (aTHX_ padlists[len]);
592
593 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
594 622
595 return 0; 623 return 0;
596} 624}
597 625
598static MGVTBL coro_cv_vtbl = { 626static MGVTBL coro_cv_vtbl = {
603/* the next two functions merely cache the padlists */ 631/* the next two functions merely cache the padlists */
604ecb_inline void 632ecb_inline void
605get_padlist (pTHX_ CV *cv) 633get_padlist (pTHX_ CV *cv)
606{ 634{
607 MAGIC *mg = CORO_MAGIC_cv (cv); 635 MAGIC *mg = CORO_MAGIC_cv (cv);
608 AV *av; 636 size_t *lenp;
609 637
610 if (ecb_expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 638 if (ecb_expect_true (mg && *(lenp = (size_t *)mg->mg_ptr)))
611 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 639 CvPADLIST (cv) = ((PADLIST **)(mg->mg_ptr + sizeof(size_t)))[--*lenp];
612 else 640 else
613 { 641 {
614#if CORO_PREFER_PERL_FUNCTIONS 642#if CORO_PREFER_PERL_FUNCTIONS
615 /* this is probably cleaner? but also slower! */ 643 /* this is probably cleaner? but also slower! */
616 /* in practise, it seems to be less stable */ 644 /* in practise, it seems to be less stable */
626 654
627ecb_inline void 655ecb_inline void
628put_padlist (pTHX_ CV *cv) 656put_padlist (pTHX_ CV *cv)
629{ 657{
630 MAGIC *mg = CORO_MAGIC_cv (cv); 658 MAGIC *mg = CORO_MAGIC_cv (cv);
631 AV *av;
632 659
633 if (ecb_expect_false (!mg)) 660 if (ecb_expect_false (!mg))
661 {
634 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 662 mg = sv_magicext ((SV *)cv, 0, CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
663 Newz (0, mg->mg_ptr ,sizeof (size_t) + sizeof (PADLIST *), char);
664 mg->mg_len = 1; /* so mg_free frees mg_ptr */
665 }
666 else
667 Renew (mg->mg_ptr,
668 sizeof(size_t) + (*(size_t *)mg->mg_ptr + 1) * sizeof(PADLIST *),
669 char);
635 670
636 av = (AV *)mg->mg_obj; 671 ((PADLIST **)(mg->mg_ptr + sizeof (size_t))) [(*(size_t *)mg->mg_ptr)++] = CvPADLIST (cv);
637
638 if (ecb_expect_false (AvFILLp (av) >= AvMAX (av)))
639 av_extend (av, AvFILLp (av) + 1);
640
641 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
642} 672}
643 673
644/** load & save, init *******************************************************/ 674/** load & save, init *******************************************************/
645 675
676ecb_inline void
677swap_sv (SV *a, SV *b)
678{
679 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
680 SV tmp;
681
682 /* swap sv_any */
683 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
684
685 /* swap sv_flags */
686 SvFLAGS (&tmp) = SvFLAGS (a);
687 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
688 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
689
690#if PERL_VERSION_ATLEAST (5,10,0)
691 /* perl 5.10 and later complicates this _quite_ a bit, but it also
692 * is much faster, so no quarrels here. alternatively, we could
693 * sv_upgrade to avoid this.
694 */
695 {
696 /* swap sv_u */
697 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
698
699 /* if SvANY points to the head, we need to adjust the pointers,
700 * as the pointer for a still points to b, and maybe vice versa.
701 */
702 #define svany_in_head(type) \
703 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
704
705 if (svany_in_head (SvTYPE (a)))
706 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
707
708 if (svany_in_head (SvTYPE (b)))
709 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
710 }
711#endif
712}
713
646/* swap sv heads, at least logically */ 714/* swap sv heads, at least logically */
647static void 715static void
648swap_svs (pTHX_ Coro__State c) 716swap_svs (pTHX_ Coro__State c)
649{ 717{
650 int i; 718 int i;
651 719
652 for (i = 0; i <= AvFILLp (c->swap_sv); ) 720 for (i = 0; i <= AvFILLp (c->swap_sv); i += 2)
653 { 721 swap_sv (AvARRAY (c->swap_sv)[i], AvARRAY (c->swap_sv)[i + 1]);
654 SV *a = AvARRAY (c->swap_sv)[i++];
655 SV *b = AvARRAY (c->swap_sv)[i++];
656
657 const U32 keep = SVs_PADSTALE | SVs_PADTMP | SVs_PADMY; /* keep these flags */
658 SV tmp;
659
660 /* swap sv_any */
661 SvANY (&tmp) = SvANY (a); SvANY (a) = SvANY (b); SvANY (b) = SvANY (&tmp);
662
663 /* swap sv_flags */
664 SvFLAGS (&tmp) = SvFLAGS (a);
665 SvFLAGS (a) = (SvFLAGS (a) & keep) | (SvFLAGS (b ) & ~keep);
666 SvFLAGS (b) = (SvFLAGS (b) & keep) | (SvFLAGS (&tmp) & ~keep);
667
668#if PERL_VERSION_ATLEAST (5,10,0)
669 /* perl 5.10 complicates this _quite_ a bit, but it also is
670 * much faster, so no quarrels here. alternatively, we could
671 * sv_upgrade to avoid this.
672 */
673 {
674 /* swap sv_u */
675 tmp.sv_u = a->sv_u; a->sv_u = b->sv_u; b->sv_u = tmp.sv_u;
676
677 /* if SvANY points to the head, we need to adjust the pointers,
678 * as the pointer for a still points to b, and maybe vice versa.
679 */
680 #define svany_in_head(type) \
681 (((1 << SVt_NULL) | (1 << SVt_BIND) | (1 << SVt_IV) | (1 << SVt_RV)) & (1 << (type)))
682
683 if (svany_in_head (SvTYPE (a)))
684 SvANY (a) = (void *)((PTRV)SvANY (a) - (PTRV)b + (PTRV)a);
685
686 if (svany_in_head (SvTYPE (b)))
687 SvANY (b) = (void *)((PTRV)SvANY (b) - (PTRV)a + (PTRV)b);
688 }
689#endif
690 }
691} 722}
692 723
693#define SWAP_SVS(coro) \ 724#define SWAP_SVS(coro) \
694 if (ecb_expect_false ((coro)->swap_sv)) \ 725 if (ecb_expect_false ((coro)->swap_sv)) \
695 swap_svs (aTHX_ (coro)) 726 swap_svs (aTHX_ (coro))
707 738
708#if CORO_JIT 739#if CORO_JIT
709 load_perl_slots (slot); 740 load_perl_slots (slot);
710#else 741#else
711 #define VARx(name,expr,type) expr = slot->name; 742 #define VARx(name,expr,type) expr = slot->name;
712 # include "state.h" 743 #include "state.h"
713 #undef VARx
714#endif 744#endif
715 745
716 { 746 {
717 dSP; 747 dSP;
718 748
721 /* now do the ugly restore mess */ 751 /* now do the ugly restore mess */
722 while (ecb_expect_true (cv = (CV *)POPs)) 752 while (ecb_expect_true (cv = (CV *)POPs))
723 { 753 {
724 put_padlist (aTHX_ cv); /* mark this padlist as available */ 754 put_padlist (aTHX_ cv); /* mark this padlist as available */
725 CvDEPTH (cv) = PTR2IV (POPs); 755 CvDEPTH (cv) = PTR2IV (POPs);
726 CvPADLIST (cv) = (AV *)POPs; 756 CvPADLIST (cv) = (PADLIST *)POPs;
727 } 757 }
728 758
729 PUTBACK; 759 PUTBACK;
730 } 760 }
731 761
840 870
841#if CORO_JIT 871#if CORO_JIT
842 save_perl_slots (slot); 872 save_perl_slots (slot);
843#else 873#else
844 #define VARx(name,expr,type) slot->name = expr; 874 #define VARx(name,expr,type) slot->name = expr;
845 # include "state.h" 875 #include "state.h"
846 #undef VARx
847#endif 876#endif
848 } 877 }
849} 878}
850 879
851/* 880/*
995 { 1024 {
996 SV **svp = 0; 1025 SV **svp = 0;
997 1026
998 if (strEQ (s, "__DIE__" )) svp = &PL_diehook; 1027 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
999 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 1028 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
1000 1029
1001 if (svp) 1030 if (svp)
1002 { 1031 {
1003 SV *ssv; 1032 SV *ssv;
1004 1033
1005 if (!*svp) 1034 if (!*svp)
1111 PL_hints = 0; 1140 PL_hints = 0;
1112 1141
1113 /* recreate the die/warn hooks */ 1142 /* recreate the die/warn hooks */
1114 PL_diehook = SvREFCNT_inc (rv_diehook); 1143 PL_diehook = SvREFCNT_inc (rv_diehook);
1115 PL_warnhook = SvREFCNT_inc (rv_warnhook); 1144 PL_warnhook = SvREFCNT_inc (rv_warnhook);
1116 1145
1117 GvSV (PL_defgv) = newSV (0); 1146 GvSV (PL_defgv) = newSV (0);
1118 GvAV (PL_defgv) = coro->args; coro->args = 0; 1147 GvAV (PL_defgv) = coro->args; coro->args = 0;
1119 GvSV (PL_errgv) = newSV (0); 1148 GvSV (PL_errgv) = newSV (0);
1120 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 1149 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
1121 GvHV (PL_hintgv) = 0; 1150 GvHV (PL_hintgv) = 0;
1414transfer_tail (pTHX) 1443transfer_tail (pTHX)
1415{ 1444{
1416 free_coro_mortal (aTHX); 1445 free_coro_mortal (aTHX);
1417} 1446}
1418 1447
1448/* try to exit the same way perl's main function would do */
1449/* we do not bother resetting the environment or other things *7
1450/* that are not, uhm, essential */
1451/* this obviously also doesn't work when perl is embedded */
1452static void ecb_noinline ecb_cold
1453perlish_exit (pTHX)
1454{
1455 int exitstatus = perl_destruct (PL_curinterp);
1456 perl_free (PL_curinterp);
1457 exit (exitstatus);
1458}
1459
1419/* 1460/*
1420 * this is a _very_ stripped down perl interpreter ;) 1461 * this is a _very_ stripped down perl interpreter ;)
1421 */ 1462 */
1422static void 1463static void
1423cctx_run (void *arg) 1464cctx_run (void *arg)
1450 */ 1491 */
1451 1492
1452 /* 1493 /*
1453 * If perl-run returns we assume exit() was being called or the coro 1494 * If perl-run returns we assume exit() was being called or the coro
1454 * fell off the end, which seems to be the only valid (non-bug) 1495 * fell off the end, which seems to be the only valid (non-bug)
1455 * reason for perl_run to return. We try to exit by jumping to the 1496 * reason for perl_run to return. We try to mimic whatever perl is normally
1456 * bootstrap-time "top" top_env, as we cannot restore the "main" 1497 * doing in that case. YMMV.
1457 * coroutine as Coro has no such concept.
1458 * This actually isn't valid with the pthread backend, but OSes requiring
1459 * that backend are too broken to do it in a standards-compliant way.
1460 */ 1498 */
1461 PL_top_env = main_top_env; 1499 perlish_exit (aTHX);
1462 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1463 } 1500 }
1464} 1501}
1465 1502
1466static coro_cctx * 1503static coro_cctx *
1467cctx_new (void) 1504cctx_new (void)
1496{ 1533{
1497 coro_cctx *cctx = cctx_new (); 1534 coro_cctx *cctx = cctx_new ();
1498 void *stack_start; 1535 void *stack_start;
1499 size_t stack_size; 1536 size_t stack_size;
1500 1537
1538#if CORO_FIBER
1539
1540 cctx->ssize = cctx_stacksize * sizeof (long);
1541 cctx->sptr = 0;
1542
1543#else
1544
1501#if HAVE_MMAP 1545 #if HAVE_MMAP
1502 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 1546 cctx->ssize = ((cctx_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
1503 /* mmap supposedly does allocate-on-write for us */ 1547 /* mmap supposedly does allocate-on-write for us */
1504 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, 0, 0); 1548 cctx->sptr = mmap (0, cctx->ssize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS, -1, 0);
1505 1549
1506 if (cctx->sptr != (void *)-1) 1550 if (cctx->sptr != (void *)-1)
1507 { 1551 {
1508 #if CORO_STACKGUARD 1552 #if CORO_STACKGUARD
1509 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 1553 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
1510 #endif 1554 #endif
1511 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE; 1555 stack_start = (char *)cctx->sptr + CORO_STACKGUARD * PAGESIZE;
1512 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 1556 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
1513 cctx->flags |= CC_MAPPED; 1557 cctx->flags |= CC_MAPPED;
1514 } 1558 }
1515 else 1559 else
1516#endif 1560 #endif
1517 { 1561 {
1518 cctx->ssize = cctx_stacksize * (long)sizeof (long); 1562 cctx->ssize = cctx_stacksize * (long)sizeof (long);
1519 New (0, cctx->sptr, cctx_stacksize, long); 1563 New (0, cctx->sptr, cctx_stacksize, long);
1520 1564
1521 if (!cctx->sptr) 1565 if (!cctx->sptr)
1522 { 1566 {
1523 perror ("FATAL: unable to allocate stack for coroutine, exiting."); 1567 perror ("FATAL: unable to allocate stack for coroutine, exiting.");
1524 _exit (EXIT_FAILURE); 1568 _exit (EXIT_FAILURE);
1525 } 1569 }
1526 1570
1527 stack_start = cctx->sptr; 1571 stack_start = cctx->sptr;
1528 stack_size = cctx->ssize; 1572 stack_size = cctx->ssize;
1529 } 1573 }
1574 #endif
1530 1575
1531 #if CORO_USE_VALGRIND 1576 #if CORO_USE_VALGRIND
1532 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size); 1577 cctx->valgrind_id = VALGRIND_STACK_REGISTER ((char *)stack_start, (char *)stack_start + stack_size);
1533 #endif 1578 #endif
1534 1579
1724 return; 1769 return;
1725 1770
1726 slf_destroy (aTHX_ coro); 1771 slf_destroy (aTHX_ coro);
1727 1772
1728 coro->flags |= CF_ZOMBIE; 1773 coro->flags |= CF_ZOMBIE;
1729 1774
1730 if (coro->flags & CF_READY) 1775 if (coro->flags & CF_READY)
1731 { 1776 {
1732 /* reduce nready, as destroying a ready coro effectively unreadies it */ 1777 /* reduce nready, as destroying a ready coro effectively unreadies it */
1733 /* alternative: look through all ready queues and remove the coro */ 1778 /* alternative: look through all ready queues and remove the coro */
1734 --coro_nready; 1779 --coro_nready;
2167 2212
2168static void 2213static void
2169coro_set_status (pTHX_ struct coro *coro, SV **arg, int items) 2214coro_set_status (pTHX_ struct coro *coro, SV **arg, int items)
2170{ 2215{
2171 AV *av; 2216 AV *av;
2172 2217
2173 if (coro->status) 2218 if (coro->status)
2174 { 2219 {
2175 av = coro->status; 2220 av = coro->status;
2176 av_clear (av); 2221 av_clear (av);
2177 } 2222 }
2224 2269
2225 coro = SvSTATE (arg [0]); 2270 coro = SvSTATE (arg [0]);
2226 coro_hv = coro->hv; 2271 coro_hv = coro->hv;
2227 2272
2228 coro_set_status (aTHX_ coro, arg + 1, items - 1); 2273 coro_set_status (aTHX_ coro, arg + 1, items - 1);
2229 2274
2230 if (ecb_expect_false (coro->flags & CF_NOCANCEL)) 2275 if (ecb_expect_false (coro->flags & CF_NOCANCEL))
2231 { 2276 {
2232 /* coro currently busy cancelling something, so just notify it */ 2277 /* coro currently busy cancelling something, so just notify it */
2233 coro->slf_frame.data = (void *)coro; 2278 coro->slf_frame.data = (void *)coro;
2234 2279
2413 2458
2414static int 2459static int
2415slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) 2460slf_check_rouse_wait (pTHX_ struct CoroSLF *frame)
2416{ 2461{
2417 SV *data = (SV *)frame->data; 2462 SV *data = (SV *)frame->data;
2418 2463
2419 if (CORO_THROW) 2464 if (CORO_THROW)
2420 return 0; 2465 return 0;
2421 2466
2422 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2467 if (SvTYPE (SvRV (data)) != SVt_PVAV)
2423 return 1; 2468 return 1;
2459 cb = sv_2mortal (coro->rouse_cb); 2504 cb = sv_2mortal (coro->rouse_cb);
2460 coro->rouse_cb = 0; 2505 coro->rouse_cb = 0;
2461 } 2506 }
2462 2507
2463 if (!SvROK (cb) 2508 if (!SvROK (cb)
2464 || SvTYPE (SvRV (cb)) != SVt_PVCV 2509 || SvTYPE (SvRV (cb)) != SVt_PVCV
2465 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) 2510 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
2466 croak ("Coro::rouse_wait called with illegal callback argument,"); 2511 croak ("Coro::rouse_wait called with illegal callback argument,");
2467 2512
2468 { 2513 {
2469 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ 2514 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */
3390 int count; 3435 int count;
3391 3436
3392 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1); 3437 eval_pv ("require 'Coro/jit-" CORO_JIT_TYPE ".pl'", 1);
3393 3438
3394 PUSHMARK (SP); 3439 PUSHMARK (SP);
3395#define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type)); 3440 #define VARx(name,expr,type) pushav_4uv (aTHX_ (UV)&(expr), sizeof (expr), offsetof (perl_slots, name), sizeof (type));
3396# include "state.h" 3441 #include "state.h"
3397#undef VARx
3398 count = call_pv ("Coro::State::_jit", G_ARRAY); 3442 count = call_pv ("Coro::State::_jit", G_ARRAY);
3399 SPAGAIN; 3443 SPAGAIN;
3400 3444
3401 save = POPs; save_ptr = SvPVbyte (save, save_len); 3445 save = POPs; save_ptr = SvPVbyte (save, save_len);
3402 load = POPs; load_ptr = SvPVbyte (load, load_len); 3446 load = POPs; load_ptr = SvPVbyte (load, load_len);
3585void 3629void
3586list () 3630list ()
3587 PROTOTYPE: 3631 PROTOTYPE:
3588 PPCODE: 3632 PPCODE:
3589{ 3633{
3590 struct coro *coro; 3634 struct coro *coro;
3591 for (coro = coro_first; coro; coro = coro->next) 3635 for (coro = coro_first; coro; coro = coro->next)
3592 if (coro->hv) 3636 if (coro->hv)
3593 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 3637 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
3594} 3638}
3595 3639
3659 RETVAL = boolSV (coro->flags & ix); 3703 RETVAL = boolSV (coro->flags & ix);
3660 OUTPUT: 3704 OUTPUT:
3661 RETVAL 3705 RETVAL
3662 3706
3663void 3707void
3664throw (Coro::State self, SV *exception = &PL_sv_undef) 3708throw (SV *self, SV *exception = &PL_sv_undef)
3665 PROTOTYPE: $;$ 3709 PROTOTYPE: $;$
3666 CODE: 3710 CODE:
3667{ 3711{
3712 struct coro *coro = SvSTATE (self);
3668 struct coro *current = SvSTATE_current; 3713 struct coro *current = SvSTATE_current;
3669 SV **exceptionp = self == current ? &CORO_THROW : &self->except; 3714 SV **exceptionp = coro == current ? &CORO_THROW : &coro->except;
3670 SvREFCNT_dec (*exceptionp); 3715 SvREFCNT_dec (*exceptionp);
3671 SvGETMAGIC (exception); 3716 SvGETMAGIC (exception);
3672 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0; 3717 *exceptionp = SvOK (exception) ? newSVsv (exception) : 0;
3718
3719 api_ready (aTHX_ self);
3673} 3720}
3674 3721
3675void 3722void
3676api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB) 3723api_trace (SV *coro, int flags = CC_TRACE | CC_TRACE_SUB)
3677 PROTOTYPE: $;$ 3724 PROTOTYPE: $;$
3754 3801
3755void 3802void
3756times (Coro::State self) 3803times (Coro::State self)
3757 PPCODE: 3804 PPCODE:
3758{ 3805{
3759 struct coro *current = SvSTATE (coro_current); 3806 struct coro *current = SvSTATE (coro_current);
3760 3807
3761 if (ecb_expect_false (current == self)) 3808 if (ecb_expect_false (current == self))
3762 { 3809 {
3763 coro_times_update (); 3810 coro_times_update ();
3764 coro_times_add (SvSTATE (coro_current)); 3811 coro_times_add (SvSTATE (coro_current));
3807 3854
3808 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3855 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3809 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3856 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3810 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3857 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3811 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ 3858 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */
3812 3859
3813 coro_stash = gv_stashpv ("Coro", TRUE); 3860 coro_stash = gv_stashpv ("Coro", TRUE);
3814 3861
3815 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); 3862 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3816 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); 3863 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3817 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); 3864 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));
4039 on_leave = 1 4086 on_leave = 1
4040 PROTOTYPE: & 4087 PROTOTYPE: &
4041 CODE: 4088 CODE:
4042{ 4089{
4043 struct coro *coro = SvSTATE_current; 4090 struct coro *coro = SvSTATE_current;
4044 AV **avp = ix ? &coro->on_leave : &coro->on_enter; 4091 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
4045 4092
4046 block = s_get_cv_croak (block); 4093 block = s_get_cv_croak (block);
4047 4094
4048 if (!*avp) 4095 if (!*avp)
4049 *avp = newAV (); 4096 *avp = newAV ();
4069 4116
4070SV * 4117SV *
4071new (SV *klass, SV *count = 0) 4118new (SV *klass, SV *count = 0)
4072 CODE: 4119 CODE:
4073{ 4120{
4074 int semcnt = 1; 4121 int semcnt = 1;
4075 4122
4076 if (count) 4123 if (count)
4077 { 4124 {
4078 SvGETMAGIC (count); 4125 SvGETMAGIC (count);
4079 4126
4139 XSRETURN_NO; 4186 XSRETURN_NO;
4140} 4187}
4141 4188
4142void 4189void
4143waiters (SV *self) 4190waiters (SV *self)
4144 PPCODE: 4191 PPCODE:
4145{ 4192{
4146 AV *av = (AV *)SvRV (self); 4193 AV *av = (AV *)SvRV (self);
4147 int wcount = AvFILLp (av) + 1 - 1; 4194 int wcount = AvFILLp (av) + 1 - 1;
4148 4195
4149 if (GIMME_V == G_SCALAR) 4196 if (GIMME_V == G_SCALAR)
4161 4208
4162void 4209void
4163_may_delete (SV *sem, int count, unsigned int extra_refs) 4210_may_delete (SV *sem, int count, unsigned int extra_refs)
4164 PPCODE: 4211 PPCODE:
4165{ 4212{
4166 AV *av = (AV *)SvRV (sem); 4213 AV *av = (AV *)SvRV (sem);
4167 4214
4168 if (SvREFCNT ((SV *)av) == 1 + extra_refs 4215 if (SvREFCNT ((SV *)av) == 1 + extra_refs
4169 && AvFILLp (av) == 0 /* no waiters, just count */ 4216 && AvFILLp (av) == 0 /* no waiters, just count */
4170 && SvIV (AvARRAY (av)[0]) == count) 4217 && SvIV (AvARRAY (av)[0]) == count)
4171 XSRETURN_YES; 4218 XSRETURN_YES;
4192 4239
4193void 4240void
4194broadcast (SV *self) 4241broadcast (SV *self)
4195 CODE: 4242 CODE:
4196{ 4243{
4197 AV *av = (AV *)SvRV (self); 4244 AV *av = (AV *)SvRV (self);
4198 coro_signal_wake (aTHX_ av, AvFILLp (av)); 4245 coro_signal_wake (aTHX_ av, AvFILLp (av));
4199} 4246}
4200 4247
4201void 4248void
4202send (SV *self) 4249send (SV *self)
4210 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */ 4257 SvIVX (AvARRAY (av)[0]) = 1; /* remember the signal */
4211} 4258}
4212 4259
4213IV 4260IV
4214awaited (SV *self) 4261awaited (SV *self)
4215 CODE: 4262 CODE:
4216 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1; 4263 RETVAL = AvFILLp ((AV *)SvRV (self)) + 1 - 1;
4217 OUTPUT: 4264 OUTPUT:
4218 RETVAL 4265 RETVAL
4219 4266
4220 4267
4225 4272
4226void 4273void
4227_schedule (...) 4274_schedule (...)
4228 CODE: 4275 CODE:
4229{ 4276{
4230 static int incede; 4277 static int incede;
4231 4278
4232 api_cede_notself (aTHX); 4279 api_cede_notself (aTHX);
4233 4280
4234 ++incede; 4281 ++incede;
4235 while (coro_nready >= incede && api_cede (aTHX)) 4282 while (coro_nready >= incede && api_cede (aTHX))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines