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.423 by root, Mon Oct 8 23:13:36 2012 UTC vs.
Revision 1.425 by root, Wed Dec 5 00:14:22 2012 UTC

23#include <errno.h> 23#include <errno.h>
24#include <assert.h> 24#include <assert.h>
25 25
26#ifndef SVs_PADSTALE 26#ifndef SVs_PADSTALE
27# 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))
28#endif 45#endif
29 46
30#if defined(_WIN32) 47#if defined(_WIN32)
31# undef HAS_GETTIMEOFDAY 48# undef HAS_GETTIMEOFDAY
32# undef setjmp 49# undef setjmp
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
646ecb_inline void 676ecb_inline void
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
994 { 1024 {
995 SV **svp = 0; 1025 SV **svp = 0;
996 1026
997 if (strEQ (s, "__DIE__" )) svp = &PL_diehook; 1027 if (strEQ (s, "__DIE__" )) svp = &PL_diehook;
998 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 1028 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
999 1029
1000 if (svp) 1030 if (svp)
1001 { 1031 {
1002 SV *ssv; 1032 SV *ssv;
1003 1033
1004 if (!*svp) 1034 if (!*svp)
1110 PL_hints = 0; 1140 PL_hints = 0;
1111 1141
1112 /* recreate the die/warn hooks */ 1142 /* recreate the die/warn hooks */
1113 PL_diehook = SvREFCNT_inc (rv_diehook); 1143 PL_diehook = SvREFCNT_inc (rv_diehook);
1114 PL_warnhook = SvREFCNT_inc (rv_warnhook); 1144 PL_warnhook = SvREFCNT_inc (rv_warnhook);
1115 1145
1116 GvSV (PL_defgv) = newSV (0); 1146 GvSV (PL_defgv) = newSV (0);
1117 GvAV (PL_defgv) = coro->args; coro->args = 0; 1147 GvAV (PL_defgv) = coro->args; coro->args = 0;
1118 GvSV (PL_errgv) = newSV (0); 1148 GvSV (PL_errgv) = newSV (0);
1119 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);
1120 GvHV (PL_hintgv) = 0; 1150 GvHV (PL_hintgv) = 0;
1731 return; 1761 return;
1732 1762
1733 slf_destroy (aTHX_ coro); 1763 slf_destroy (aTHX_ coro);
1734 1764
1735 coro->flags |= CF_ZOMBIE; 1765 coro->flags |= CF_ZOMBIE;
1736 1766
1737 if (coro->flags & CF_READY) 1767 if (coro->flags & CF_READY)
1738 { 1768 {
1739 /* reduce nready, as destroying a ready coro effectively unreadies it */ 1769 /* reduce nready, as destroying a ready coro effectively unreadies it */
1740 /* alternative: look through all ready queues and remove the coro */ 1770 /* alternative: look through all ready queues and remove the coro */
1741 --coro_nready; 1771 --coro_nready;
2174 2204
2175static void 2205static void
2176coro_set_status (pTHX_ struct coro *coro, SV **arg, int items) 2206coro_set_status (pTHX_ struct coro *coro, SV **arg, int items)
2177{ 2207{
2178 AV *av; 2208 AV *av;
2179 2209
2180 if (coro->status) 2210 if (coro->status)
2181 { 2211 {
2182 av = coro->status; 2212 av = coro->status;
2183 av_clear (av); 2213 av_clear (av);
2184 } 2214 }
2231 2261
2232 coro = SvSTATE (arg [0]); 2262 coro = SvSTATE (arg [0]);
2233 coro_hv = coro->hv; 2263 coro_hv = coro->hv;
2234 2264
2235 coro_set_status (aTHX_ coro, arg + 1, items - 1); 2265 coro_set_status (aTHX_ coro, arg + 1, items - 1);
2236 2266
2237 if (ecb_expect_false (coro->flags & CF_NOCANCEL)) 2267 if (ecb_expect_false (coro->flags & CF_NOCANCEL))
2238 { 2268 {
2239 /* coro currently busy cancelling something, so just notify it */ 2269 /* coro currently busy cancelling something, so just notify it */
2240 coro->slf_frame.data = (void *)coro; 2270 coro->slf_frame.data = (void *)coro;
2241 2271
2420 2450
2421static int 2451static int
2422slf_check_rouse_wait (pTHX_ struct CoroSLF *frame) 2452slf_check_rouse_wait (pTHX_ struct CoroSLF *frame)
2423{ 2453{
2424 SV *data = (SV *)frame->data; 2454 SV *data = (SV *)frame->data;
2425 2455
2426 if (CORO_THROW) 2456 if (CORO_THROW)
2427 return 0; 2457 return 0;
2428 2458
2429 if (SvTYPE (SvRV (data)) != SVt_PVAV) 2459 if (SvTYPE (SvRV (data)) != SVt_PVAV)
2430 return 1; 2460 return 1;
2466 cb = sv_2mortal (coro->rouse_cb); 2496 cb = sv_2mortal (coro->rouse_cb);
2467 coro->rouse_cb = 0; 2497 coro->rouse_cb = 0;
2468 } 2498 }
2469 2499
2470 if (!SvROK (cb) 2500 if (!SvROK (cb)
2471 || SvTYPE (SvRV (cb)) != SVt_PVCV 2501 || SvTYPE (SvRV (cb)) != SVt_PVCV
2472 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback) 2502 || CvXSUB ((CV *)SvRV (cb)) != coro_rouse_callback)
2473 croak ("Coro::rouse_wait called with illegal callback argument,"); 2503 croak ("Coro::rouse_wait called with illegal callback argument,");
2474 2504
2475 { 2505 {
2476 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */ 2506 CV *cv = (CV *)SvRV (cb); /* for S_GENSUB_ARG */
3816 3846
3817 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3847 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3818 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3848 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3819 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3849 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3820 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */ 3850 CvNODEBUG_on (get_cv ("Coro::_pool_handler", 0)); /* work around a debugger bug */
3821 3851
3822 coro_stash = gv_stashpv ("Coro", TRUE); 3852 coro_stash = gv_stashpv ("Coro", TRUE);
3823 3853
3824 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX)); 3854 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (CORO_PRIO_MAX));
3825 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH)); 3855 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (CORO_PRIO_HIGH));
3826 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL)); 3856 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (CORO_PRIO_NORMAL));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines