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.158 by root, Fri Sep 21 02:23:48 2007 UTC vs.
Revision 1.169 by root, Thu Sep 27 16:25:10 2007 UTC

132static AV *main_mainstack; /* used to differentiate between $main and others */ 132static AV *main_mainstack; /* used to differentiate between $main and others */
133static JMPENV *main_top_env; 133static JMPENV *main_top_env;
134static HV *coro_state_stash, *coro_stash; 134static HV *coro_state_stash, *coro_stash;
135static SV *coro_mortal; /* will be freed after next transfer */ 135static SV *coro_mortal; /* will be freed after next transfer */
136 136
137/* async_pool helper stuff */
138static SV *sv_pool_rss;
139static SV *sv_pool_size;
140static AV *av_async_pool;
141
137static struct coro_cctx *cctx_first; 142static struct coro_cctx *cctx_first;
138static int cctx_count, cctx_idle; 143static int cctx_count, cctx_idle;
144
145enum {
146 CC_MAPPED = 0x01,
147 CC_NOREUSE = 0x02, /* throw this away after tracing */
148 CC_TRACE = 0x04,
149 CC_TRACE_SUB = 0x08, /* trace sub calls */
150 CC_TRACE_LINE = 0x10, /* trace each statement */
151 CC_TRACE_ALL = CC_TRACE_SUB | CC_TRACE_LINE,
152};
139 153
140/* this is a structure representing a c-level coroutine */ 154/* this is a structure representing a c-level coroutine */
141typedef struct coro_cctx { 155typedef struct coro_cctx {
142 struct coro_cctx *next; 156 struct coro_cctx *next;
143 157
152 coro_context cctx; 166 coro_context cctx;
153 167
154#if CORO_USE_VALGRIND 168#if CORO_USE_VALGRIND
155 int valgrind_id; 169 int valgrind_id;
156#endif 170#endif
157 char inuse, mapped; 171 unsigned char flags;
158} coro_cctx; 172} coro_cctx;
159 173
160enum { 174enum {
161 CF_RUNNING = 0x0001, /* coroutine is running */ 175 CF_RUNNING = 0x0001, /* coroutine is running */
162 CF_READY = 0x0002, /* coroutine is ready */ 176 CF_READY = 0x0002, /* coroutine is ready */
284 SvMAGIC (cv) \ 298 SvMAGIC (cv) \
285 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 299 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \
286 ? SvMAGIC (cv) \ 300 ? SvMAGIC (cv) \
287 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 301 : mg_find ((SV *)cv, PERL_MAGIC_coro) \
288 : 0 302 : 0
303
304static struct coro *
305SvSTATE_ (pTHX_ SV *coro)
306{
307 HV *stash;
308 MAGIC *mg;
309
310 if (SvROK (coro))
311 coro = SvRV (coro);
312
313 stash = SvSTASH (coro);
314 if (stash != coro_stash && stash != coro_state_stash)
315 {
316 /* very slow, but rare, check */
317 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
318 croak ("Coro::State object required");
319 }
320
321 mg = CORO_MAGIC (coro);
322 return (struct coro *)mg->mg_ptr;
323}
324
325#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
289 326
290/* the next two functions merely cache the padlists */ 327/* the next two functions merely cache the padlists */
291static void 328static void
292get_padlist (pTHX_ CV *cv) 329get_padlist (pTHX_ CV *cv)
293{ 330{
579 /* 616 /*
580 * emulate part of the perl startup here. 617 * emulate part of the perl startup here.
581 */ 618 */
582 coro_init_stacks (aTHX); 619 coro_init_stacks (aTHX);
583 620
621 PL_runops = RUNOPS_DEFAULT;
584 PL_curcop = &PL_compiling; 622 PL_curcop = &PL_compiling;
585 PL_in_eval = EVAL_NULL; 623 PL_in_eval = EVAL_NULL;
586 PL_comppad = 0; 624 PL_comppad = 0;
587 PL_curpm = 0; 625 PL_curpm = 0;
588 PL_localizing = 0; 626 PL_localizing = 0;
619 SvREFCNT_dec (coro_mortal); 657 SvREFCNT_dec (coro_mortal);
620 coro_mortal = 0; 658 coro_mortal = 0;
621 } 659 }
622} 660}
623 661
662static int
663runops_trace (pTHX)
664{
665 COP *oldcop = 0;
666 int oldcxix = -2;
667 struct coro *coro = SvSTATE (coro_current); /* trace cctx is tied to specific coro */
668 coro_cctx *cctx = coro->cctx;
669
670 while ((PL_op = CALL_FPTR (PL_op->op_ppaddr) (aTHX)))
671 {
672 PERL_ASYNC_CHECK ();
673
674 if (PL_op->op_type == OP_LEAVESUB && cctx->flags & CC_TRACE_SUB)
675 {
676 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
677 SV **bot, **top;
678 AV *av = newAV (); /* return values */
679 SV **cb;
680 runops_proc_t old_runops = PL_runops;
681 dSP;
682 ENTER;
683 SAVETMPS;
684 EXTEND (SP, 3);
685 PL_runops = RUNOPS_DEFAULT;
686
687 GV *gv = CvGV (cx->blk_sub.cv);
688 SV *fullname = sv_2mortal (newSV (0));
689 if (isGV (gv))
690 gv_efullname3 (fullname, gv, 0);
691
692 bot = PL_stack_base + cx->blk_oldsp + 1;
693 top = cx->blk_gimme == G_ARRAY ? SP + 1
694 : cx->blk_gimme == G_SCALAR ? bot + 1
695 : bot;
696
697 while (bot < top)
698 av_push (av, SvREFCNT_inc (*bot++));
699
700 PUSHMARK (SP);
701 PUSHs (&PL_sv_no);
702 PUSHs (fullname);
703 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
704 PUTBACK;
705 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
706 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
707 SPAGAIN;
708
709 FREETMPS;
710 LEAVE;
711 PL_runops = old_runops;
712 }
713
714 if (oldcop != PL_curcop)
715 {
716 oldcop = PL_curcop;
717
718 if (PL_curcop != &PL_compiling)
719 {
720 SV **cb;
721 runops_proc_t old_runops = PL_runops;
722 dSP;
723 ENTER;
724 SAVETMPS;
725 EXTEND (SP, 3);
726 PL_runops = RUNOPS_DEFAULT;
727
728 if (oldcxix != cxstack_ix && cctx->flags & CC_TRACE_SUB)
729 {
730 PERL_CONTEXT *cx = &cxstack[cxstack_ix];
731
732 if (CxTYPE (cx) == CXt_SUB && oldcxix < cxstack_ix)
733 {
734 GV *gv = CvGV (cx->blk_sub.cv);
735 SV *fullname = sv_2mortal (newSV (0));
736 if (isGV (gv))
737 gv_efullname3 (fullname, gv, 0);
738
739 PUSHMARK (SP);
740 PUSHs (&PL_sv_yes);
741 PUSHs (fullname);
742 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
743 PUTBACK;
744 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
745 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
746 SPAGAIN;
747 }
748
749 oldcxix = cxstack_ix;
750 }
751
752 if (cctx->flags & CC_TRACE_LINE)
753 {
754 PUSHMARK (SP);
755 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
756 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
757 PUTBACK;
758 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
759 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
760 SPAGAIN;
761 }
762
763 FREETMPS;
764 LEAVE;
765 PL_runops = old_runops;
766 }
767 }
768 }
769
770 TAINT_NOT;
771 return 0;
772}
773
624/* inject a fake call to Coro::State::_cctx_init into the execution */ 774/* inject a fake call to Coro::State::_cctx_init into the execution */
625/* _cctx_init should be careful, as it could be called at almost any time */ 775/* _cctx_init should be careful, as it could be called at almost any time */
626/* during execution of a perl program */ 776/* during execution of a perl program */
627static void NOINLINE 777static void NOINLINE
628prepare_cctx (pTHX_ coro_cctx *cctx) 778prepare_cctx (pTHX_ coro_cctx *cctx)
629{ 779{
630 dSP; 780 dSP;
631 LOGOP myop; 781 LOGOP myop;
782
783 PL_top_env = &PL_start_env;
784
785 if (cctx->flags & CC_TRACE)
786 PL_runops = runops_trace;
632 787
633 Zero (&myop, 1, LOGOP); 788 Zero (&myop, 1, LOGOP);
634 myop.op_next = PL_op; 789 myop.op_next = PL_op;
635 myop.op_flags = OPf_WANT_VOID | OPf_STACKED; 790 myop.op_flags = OPf_WANT_VOID | OPf_STACKED;
636 791
653 dTHX; 808 dTHX;
654 809
655 /* coro_run is the alternative tail of transfer(), so unlock here. */ 810 /* coro_run is the alternative tail of transfer(), so unlock here. */
656 UNLOCK; 811 UNLOCK;
657 812
658 PL_top_env = &PL_start_env; 813 /* we now skip the entersub that lead to transfer() */
814 PL_op = PL_op->op_next;
659 815
660 /* inject a fake subroutine call to cctx_init */ 816 /* inject a fake subroutine call to cctx_init */
661 prepare_cctx (aTHX_ (coro_cctx *)arg); 817 prepare_cctx (aTHX_ (coro_cctx *)arg);
662 818
663 /* somebody or something will hit me for both perl_run and PL_restartop */ 819 /* somebody or something will hit me for both perl_run and PL_restartop */
697# if CORO_STACKGUARD 853# if CORO_STACKGUARD
698 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE); 854 mprotect (cctx->sptr, CORO_STACKGUARD * PAGESIZE, PROT_NONE);
699# endif 855# endif
700 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr; 856 stack_start = CORO_STACKGUARD * PAGESIZE + (char *)cctx->sptr;
701 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE; 857 stack_size = cctx->ssize - CORO_STACKGUARD * PAGESIZE;
702 cctx->mapped = 1; 858 cctx->flags |= CC_MAPPED;
703 } 859 }
704 else 860 else
705#endif 861#endif
706 { 862 {
707 cctx->ssize = coro_stacksize * (long)sizeof (long); 863 cctx->ssize = coro_stacksize * (long)sizeof (long);
734#if CORO_USE_VALGRIND 890#if CORO_USE_VALGRIND
735 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id); 891 VALGRIND_STACK_DEREGISTER (cctx->valgrind_id);
736#endif 892#endif
737 893
738#if HAVE_MMAP 894#if HAVE_MMAP
739 if (cctx->mapped) 895 if (cctx->flags & CC_MAPPED)
740 munmap (cctx->sptr, cctx->ssize); 896 munmap (cctx->sptr, cctx->ssize);
741 else 897 else
742#endif 898#endif
743 Safefree (cctx->sptr); 899 Safefree (cctx->sptr);
744 900
752 { 908 {
753 coro_cctx *cctx = cctx_first; 909 coro_cctx *cctx = cctx_first;
754 cctx_first = cctx->next; 910 cctx_first = cctx->next;
755 --cctx_idle; 911 --cctx_idle;
756 912
757 if (cctx->ssize >= coro_stacksize) 913 if (cctx->ssize >= coro_stacksize && !(cctx->flags & CC_NOREUSE))
758 return cctx; 914 return cctx;
759 915
760 cctx_destroy (cctx); 916 cctx_destroy (cctx);
761 } 917 }
762 918
763 PL_op = PL_op->op_next;
764 return cctx_new (); 919 return cctx_new ();
765} 920}
766 921
767static void 922static void
768cctx_put (coro_cctx *cctx) 923cctx_put (coro_cctx *cctx)
772 { 927 {
773 coro_cctx *first = cctx_first; 928 coro_cctx *first = cctx_first;
774 cctx_first = first->next; 929 cctx_first = first->next;
775 --cctx_idle; 930 --cctx_idle;
776 931
777 assert (!first->inuse);
778 cctx_destroy (first); 932 cctx_destroy (first);
779 } 933 }
780 934
781 ++cctx_idle; 935 ++cctx_idle;
782 cctx->next = cctx_first; 936 cctx->next = cctx_first;
803 957
804 if (prev->flags & CF_NEW) 958 if (prev->flags & CF_NEW)
805 { 959 {
806 /* create a new empty context */ 960 /* create a new empty context */
807 Newz (0, prev->cctx, 1, coro_cctx); 961 Newz (0, prev->cctx, 1, coro_cctx);
808 prev->cctx->inuse = 1;
809 prev->flags &= ~CF_NEW; 962 prev->flags &= ~CF_NEW;
810 prev->flags |= CF_RUNNING; 963 prev->flags |= CF_RUNNING;
811 } 964 }
812 965
813 /*TODO: must not croak here */ 966 /*TODO: must not croak here */
831 next->flags &= ~CF_NEW; 984 next->flags &= ~CF_NEW;
832 /* first get rid of the old state */ 985 /* first get rid of the old state */
833 save_perl (aTHX_ prev); 986 save_perl (aTHX_ prev);
834 /* setup coroutine call */ 987 /* setup coroutine call */
835 setup_coro (aTHX_ next); 988 setup_coro (aTHX_ next);
836 /* need a new stack */
837 assert (!next->cctx);
838 } 989 }
839 else 990 else
840 { 991 {
841 /* coroutine already started */ 992 /* coroutine already started */
842 save_perl (aTHX_ prev); 993 save_perl (aTHX_ prev);
844 } 995 }
845 996
846 prev__cctx = prev->cctx; 997 prev__cctx = prev->cctx;
847 998
848 /* possibly "free" the cctx */ 999 /* possibly "free" the cctx */
849 if (prev__cctx->idle_sp == STACKLEVEL) 1000 if (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))
850 { 1001 {
851 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */ 1002 /* I assume that STACKLEVEL is a stronger indicator than PL_top_env changes */
852 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te)); 1003 assert (("ERROR: current top_env must equal previous top_env", PL_top_env == prev__cctx->idle_te));
853 1004
854 prev->cctx = 0; 1005 prev->cctx = 0;
855 1006
856 cctx_put (prev__cctx); 1007 cctx_put (prev__cctx);
857 prev__cctx->inuse = 0;
858 } 1008 }
859 1009
860 if (!next->cctx) 1010 if (!next->cctx)
861 {
862 next->cctx = cctx_get (aTHX); 1011 next->cctx = cctx_get (aTHX);
863 assert (!next->cctx->inuse);
864 next->cctx->inuse = 1;
865 }
866 1012
867 if (prev__cctx != next->cctx) 1013 if (prev__cctx != next->cctx)
868 { 1014 {
869 prev__cctx->top_env = PL_top_env; 1015 prev__cctx->top_env = PL_top_env;
870 PL_top_env = next->cctx->top_env; 1016 PL_top_env = next->cctx->top_env;
972#else 1118#else
973# define MGf_DUP 0 1119# define MGf_DUP 0
974#endif 1120#endif
975}; 1121};
976 1122
977static struct coro *
978SvSTATE_ (pTHX_ SV *coro)
979{
980 HV *stash;
981 MAGIC *mg;
982
983 if (SvROK (coro))
984 coro = SvRV (coro);
985
986 stash = SvSTASH (coro);
987 if (stash != coro_stash && stash != coro_state_stash)
988 {
989 /* very slow, but rare, check */
990 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
991 croak ("Coro::State object required");
992 }
993
994 mg = CORO_MAGIC (coro);
995 return (struct coro *)mg->mg_ptr;
996}
997
998#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
999
1000static void 1123static void
1001prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv) 1124prepare_transfer (pTHX_ struct transfer_args *ta, SV *prev_sv, SV *next_sv)
1002{ 1125{
1003 ta->prev = SvSTATE (prev_sv); 1126 ta->prev = SvSTATE (prev_sv);
1004 ta->next = SvSTATE (next_sv); 1127 ta->next = SvSTATE (next_sv);
1214#endif 1337#endif
1215 BOOT_PAGESIZE; 1338 BOOT_PAGESIZE;
1216 1339
1217 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1340 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1218 1341
1342 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1343 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1344 newCONSTSUB (coro_state_stash, "CC_TRACE_LINE", newSViv (CC_TRACE_LINE));
1345 newCONSTSUB (coro_state_stash, "CC_TRACE_ALL" , newSViv (CC_TRACE_ALL));
1346
1219 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV)); 1347 newCONSTSUB (coro_state_stash, "SAVE_DEFAV", newSViv (CORO_SAVE_DEFAV));
1220 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV)); 1348 newCONSTSUB (coro_state_stash, "SAVE_DEFSV", newSViv (CORO_SAVE_DEFSV));
1221 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV)); 1349 newCONSTSUB (coro_state_stash, "SAVE_ERRSV", newSViv (CORO_SAVE_ERRSV));
1222 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV)); 1350 newCONSTSUB (coro_state_stash, "SAVE_IRSSV", newSViv (CORO_SAVE_IRSSV));
1223 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH)); 1351 newCONSTSUB (coro_state_stash, "SAVE_DEFFH", newSViv (CORO_SAVE_DEFFH));
1374 if (coro->hv) 1502 if (coro->hv)
1375 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv))); 1503 XPUSHs (sv_2mortal (newRV_inc ((SV *)coro->hv)));
1376} 1504}
1377 1505
1378void 1506void
1379_eval (Coro::State coro, SV *coderef) 1507call (Coro::State coro, SV *coderef)
1508 ALIAS:
1509 eval = 1
1380 CODE: 1510 CODE:
1381{ 1511{
1382 if (coro->mainstack) 1512 if (coro->mainstack)
1383 { 1513 {
1384 struct coro temp; 1514 struct coro temp;
1395 dSP; 1525 dSP;
1396 ENTER; 1526 ENTER;
1397 SAVETMPS; 1527 SAVETMPS;
1398 PUSHMARK (SP); 1528 PUSHMARK (SP);
1399 PUTBACK; 1529 PUTBACK;
1530 if (ix)
1531 eval_sv (coderef, 0);
1532 else
1400 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1533 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1401 SPAGAIN; 1534 SPAGAIN;
1402 FREETMPS; 1535 FREETMPS;
1403 LEAVE; 1536 LEAVE;
1404 PUTBACK; 1537 PUTBACK;
1405 } 1538 }
1423 CODE: 1556 CODE:
1424 RETVAL = boolSV (coro->flags & ix); 1557 RETVAL = boolSV (coro->flags & ix);
1425 OUTPUT: 1558 OUTPUT:
1426 RETVAL 1559 RETVAL
1427 1560
1561void
1562trace (Coro::State coro, int flags = CC_TRACE | CC_TRACE_SUB)
1563 CODE:
1564 if (flags & CC_TRACE)
1565 {
1566 if (!coro->cctx)
1567 coro->cctx = cctx_new ();
1568 else if (!(coro->cctx->flags & CC_TRACE))
1569 croak ("cannot enable tracing on coroutine with custom stack");
1570
1571 coro->cctx->flags |= flags & (CC_TRACE | CC_TRACE_ALL);
1572 }
1573 else
1574 if (coro->cctx && coro->cctx->flags & CC_TRACE)
1575 {
1576 coro->cctx->flags &= ~(CC_TRACE | CC_TRACE_ALL);
1577 coro->cctx->flags |= CC_NOREUSE;
1578
1579 if (coro->flags & CF_RUNNING)
1580 PL_runops = RUNOPS_DEFAULT;
1581 else
1582 coro->runops = RUNOPS_DEFAULT;
1583 }
1584
1585SV *
1586has_stack (Coro::State coro)
1587 PROTOTYPE: $
1588 CODE:
1589 RETVAL = boolSV (!!coro->cctx);
1590 OUTPUT:
1591 RETVAL
1592
1593int
1594is_traced (Coro::State coro)
1595 PROTOTYPE: $
1596 CODE:
1597 RETVAL = (coro->cctx ? coro->cctx->flags : 0) & CC_TRACE_ALL;
1598 OUTPUT:
1599 RETVAL
1600
1428IV 1601IV
1429rss (Coro::State coro) 1602rss (Coro::State coro)
1430 PROTOTYPE: $ 1603 PROTOTYPE: $
1431 CODE: 1604 CODE:
1432 RETVAL = coro_rss (coro); 1605 RETVAL = coro_rss (coro);
1438 1611
1439BOOT: 1612BOOT:
1440{ 1613{
1441 int i; 1614 int i;
1442 1615
1616 sv_pool_rss = get_sv ("Coro::POOL_RSS" , TRUE);
1617 sv_pool_size = get_sv ("Coro::POOL_SIZE" , TRUE);
1618 av_async_pool = get_av ("Coro::async_pool", TRUE);
1619
1620 coro_current = get_sv ("Coro::current", FALSE);
1621 SvREADONLY_on (coro_current);
1622
1443 coro_stash = gv_stashpv ("Coro", TRUE); 1623 coro_stash = gv_stashpv ("Coro", TRUE);
1444 1624
1445 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1625 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1446 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH)); 1626 newCONSTSUB (coro_stash, "PRIO_HIGH", newSViv (PRIO_HIGH));
1447 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL)); 1627 newCONSTSUB (coro_stash, "PRIO_NORMAL", newSViv (PRIO_NORMAL));
1448 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW)); 1628 newCONSTSUB (coro_stash, "PRIO_LOW", newSViv (PRIO_LOW));
1449 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE)); 1629 newCONSTSUB (coro_stash, "PRIO_IDLE", newSViv (PRIO_IDLE));
1450 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN)); 1630 newCONSTSUB (coro_stash, "PRIO_MIN", newSViv (PRIO_MIN));
1451
1452 coro_current = get_sv ("Coro::current", FALSE);
1453 SvREADONLY_on (coro_current);
1454 1631
1455 for (i = PRIO_MAX - PRIO_MIN + 1; i--; ) 1632 for (i = PRIO_MAX - PRIO_MIN + 1; i--; )
1456 coro_ready[i] = newAV (); 1633 coro_ready[i] = newAV ();
1457 1634
1458 { 1635 {
1516 CODE: 1693 CODE:
1517 RETVAL = coro_nready; 1694 RETVAL = coro_nready;
1518 OUTPUT: 1695 OUTPUT:
1519 RETVAL 1696 RETVAL
1520 1697
1698# for async_pool speedup
1699void
1700_pool_1 (SV *cb)
1701 CODE:
1702{
1703 int i, len;
1704 HV *hv = (HV *)SvRV (coro_current);
1705 AV *defav = GvAV (PL_defgv);
1706 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1707 AV *invoke_av;
1708
1709 if (!invoke)
1710 croak ("\3terminate\2\n");
1711
1712 hv_store (hv, "desc", sizeof ("desc") - 1,
1713 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1714
1715 invoke_av = (AV *)SvRV (invoke);
1716 len = av_len (invoke_av);
1717
1718 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1719
1720 if (len > 0)
1721 {
1722 av_fill (defav, len - 1);
1723 for (i = 0; i < len; ++i)
1724 av_store (defav, i, SvREFCNT_inc (AvARRAY (invoke_av)[i + 1]));
1725 }
1726
1727 SvREFCNT_dec (invoke);
1728}
1729
1730void
1731_pool_2 (SV *cb)
1732 CODE:
1733{
1734 struct coro *coro = SvSTATE (coro_current);
1735
1736 sv_setsv (cb, &PL_sv_undef);
1737
1738 if (coro_rss (coro) > SvIV (sv_pool_rss)
1739 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1740 croak ("\3terminate\2\n");
1741
1742 av_clear (GvAV (PL_defgv));
1743 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1744 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1745 coro->save = CORO_SAVE_DEF;
1746 coro->prio = 0;
1747 av_push (av_async_pool, newSVsv (coro_current));
1748}
1749
1750
1521MODULE = Coro::State PACKAGE = Coro::AIO 1751MODULE = Coro::State PACKAGE = Coro::AIO
1522 1752
1523SV * 1753SV *
1524_get_state () 1754_get_state ()
1525 CODE: 1755 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines