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.199 by root, Sun Oct 7 13:53:37 2007 UTC vs.
Revision 1.204 by root, Mon Oct 8 03:03:39 2007 UTC

130#else 130#else
131# define LOCK (void)0 131# define LOCK (void)0
132# define UNLOCK (void)0 132# define UNLOCK (void)0
133#endif 133#endif
134 134
135#define strpair(const) const, sizeof (const) - 1
136
135/* helper storage struct for Coro::AIO */ 137/* helper storage struct for Coro::AIO */
136struct io_state 138struct io_state
137{ 139{
138 int errorno; 140 int errorno;
139 I32 laststype; 141 I32 laststype;
148static HV *coro_state_stash, *coro_stash; 150static HV *coro_state_stash, *coro_stash;
149static SV *coro_mortal; /* will be freed after next transfer */ 151static SV *coro_mortal; /* will be freed after next transfer */
150 152
151static GV *irsgv; /* $/ */ 153static GV *irsgv; /* $/ */
152static GV *stdoutgv; /* *STDOUT */ 154static GV *stdoutgv; /* *STDOUT */
155
156static HV *hv_sig; /* %SIG */
153static SV *sv_diehook; 157static SV *sv_diehook;
154static SV *sv_warnhook; 158static SV *sv_warnhook;
155 159
156/* async_pool helper stuff */ 160/* async_pool helper stuff */
157static SV *sv_pool_rss; 161static SV *sv_pool_rss;
217 221
218 /* process data */ 222 /* process data */
219 AV *mainstack; 223 AV *mainstack;
220 perl_slots *slot; /* basically the saved sp */ 224 perl_slots *slot; /* basically the saved sp */
221 225
222 /* data associated with this coroutine (initial args) */ 226 AV *args; /* data associated with this coroutine (initial args) */
223 AV *args; 227 int refcnt; /* coroutines are refcounted, yes */
224 int refcnt;
225 int flags; /* CF_ flags */ 228 int flags; /* CF_ flags */
229 HV *hv; /* the perl hash associated with this coro, if any */
226 230
227 /* statistics */ 231 /* statistics */
228 int usecount; /* number of transfers to this coro */ 232 int usecount; /* number of transfers to this coro */
229 233
230 /* coro process data */ 234 /* coro process data */
231 int prio; 235 int prio;
232 SV *throw; 236 SV *throw; /* exception to be thrown */
233 237
234 /* async_pool */ 238 /* async_pool */
235 SV *saved_deffh; 239 SV *saved_deffh;
236 240
237 /* linked list */ 241 /* linked list */
238 struct coro *next, *prev; 242 struct coro *next, *prev;
239 HV *hv; /* the perl hash associated with this coro, if any */
240}; 243};
241 244
242typedef struct coro *Coro__State; 245typedef struct coro *Coro__State;
243typedef struct coro *Coro__State_or_hashref; 246typedef struct coro *Coro__State_or_hashref;
244 247
267 get_sv (name, create); 270 get_sv (name, create);
268#endif 271#endif
269 return get_sv (name, create); 272 return get_sv (name, create);
270} 273}
271 274
272static SV * 275static AV *
273coro_get_av (const char *name, int create) 276coro_get_av (const char *name, int create)
274{ 277{
275#if PERL_VERSION_ATLEAST (5,9,0) 278#if PERL_VERSION_ATLEAST (5,9,0)
276 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 279 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
277 get_av (name, create); 280 get_av (name, create);
278#endif 281#endif
279 return get_av (name, create); 282 return get_av (name, create);
283}
284
285static HV *
286coro_get_hv (const char *name, int create)
287{
288#if PERL_VERSION_ATLEAST (5,9,0)
289 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
290 get_hv (name, create);
291#endif
292 return get_hv (name, create);
280} 293}
281 294
282static AV * 295static AV *
283coro_clone_padlist (pTHX_ CV *cv) 296coro_clone_padlist (pTHX_ CV *cv)
284{ 297{
439 452
440 #define VAR(name,type) PL_ ## name = slot->name; 453 #define VAR(name,type) PL_ ## name = slot->name;
441 # include "state.h" 454 # include "state.h"
442 #undef VAR 455 #undef VAR
443 456
457 /*hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);*/
458 /*hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);*/
459
444 { 460 {
445 dSP; 461 dSP;
446 462
447 CV *cv; 463 CV *cv;
448 464
507 523
508 PUTBACK; 524 PUTBACK;
509 } 525 }
510 526
511 /* allocate some space on the context stack for our purposes */ 527 /* allocate some space on the context stack for our purposes */
528 /* we manually unroll here, as usually 2 slots is enough */
529 if (SLOT_COUNT >= 1) CXINC;
530 if (SLOT_COUNT >= 2) CXINC;
531 if (SLOT_COUNT >= 3) CXINC;
512 { 532 {
513 /* we manually unroll here, as usually 2 slots is enough */
514 int i; 533 int i;
515 if (SLOT_COUNT >= 1) CXINC;
516 if (SLOT_COUNT >= 2) CXINC;
517 if (SLOT_COUNT >= 3) CXINC;
518 for (i = 3; i < SLOT_COUNT; ++i) 534 for (i = 3; i < SLOT_COUNT; ++i)
519 CXINC; 535 CXINC;
520
521 cxstack_ix -= SLOT_COUNT; /* undo allocation */
522 } 536 }
537 cxstack_ix -= SLOT_COUNT; /* undo allocation */
523 538
524 c->mainstack = PL_mainstack; 539 c->mainstack = PL_mainstack;
525 540
526 { 541 {
527 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 542 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
671 PL_curpm = 0; 686 PL_curpm = 0;
672 PL_curpad = 0; 687 PL_curpad = 0;
673 PL_localizing = 0; 688 PL_localizing = 0;
674 PL_dirty = 0; 689 PL_dirty = 0;
675 PL_restartop = 0; 690 PL_restartop = 0;
676 PL_diehook = SvREFCNT_inc (sv_diehook); 691 PL_diehook = 0; hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);
677 PL_warnhook = SvREFCNT_inc (sv_warnhook); 692 PL_warnhook = 0; hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);
678 693
679 GvSV (PL_defgv) = newSV (0); 694 GvSV (PL_defgv) = newSV (0);
680 GvAV (PL_defgv) = coro->args; coro->args = 0; 695 GvAV (PL_defgv) = coro->args; coro->args = 0;
681 GvSV (PL_errgv) = newSV (0); 696 GvSV (PL_errgv) = newSV (0);
682 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 697 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
791 PUSHMARK (SP); 806 PUSHMARK (SP);
792 PUSHs (&PL_sv_no); 807 PUSHs (&PL_sv_no);
793 PUSHs (fullname); 808 PUSHs (fullname);
794 PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 809 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
795 PUTBACK; 810 PUTBACK;
796 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 811 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0);
797 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 812 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
798 SPAGAIN; 813 SPAGAIN;
799 FREETMPS; 814 FREETMPS;
800 LEAVE; 815 LEAVE;
801 PL_runops = runops_trace; 816 PL_runops = runops_trace;
830 PUSHMARK (SP); 845 PUSHMARK (SP);
831 PUSHs (&PL_sv_yes); 846 PUSHs (&PL_sv_yes);
832 PUSHs (fullname); 847 PUSHs (fullname);
833 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 848 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
834 PUTBACK; 849 PUTBACK;
835 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0); 850 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0);
836 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 851 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
837 SPAGAIN; 852 SPAGAIN;
838 FREETMPS; 853 FREETMPS;
839 LEAVE; 854 LEAVE;
840 PL_runops = runops_trace; 855 PL_runops = runops_trace;
854 PL_runops = RUNOPS_DEFAULT; 869 PL_runops = RUNOPS_DEFAULT;
855 PUSHMARK (SP); 870 PUSHMARK (SP);
856 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); 871 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
857 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); 872 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
858 PUTBACK; 873 PUTBACK;
859 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0); 874 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_line_cb"), 0);
860 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 875 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
861 SPAGAIN; 876 SPAGAIN;
862 FREETMPS; 877 FREETMPS;
863 LEAVE; 878 LEAVE;
864 PL_runops = runops_trace; 879 PL_runops = runops_trace;
874 889
875/* inject a fake call to Coro::State::_cctx_init into the execution */ 890/* inject a fake call to Coro::State::_cctx_init into the execution */
876/* _cctx_init should be careful, as it could be called at almost any time */ 891/* _cctx_init should be careful, as it could be called at almost any time */
877/* during execution of a perl program */ 892/* during execution of a perl program */
878static void NOINLINE 893static void NOINLINE
879prepare_cctx (pTHX_ coro_cctx *cctx) 894cctx_prepare (pTHX_ coro_cctx *cctx)
880{ 895{
881 dSP; 896 dSP;
882 LOGOP myop; 897 LOGOP myop;
883 898
884 PL_top_env = &PL_start_env; 899 PL_top_env = &PL_start_env;
902 917
903/* 918/*
904 * this is a _very_ stripped down perl interpreter ;) 919 * this is a _very_ stripped down perl interpreter ;)
905 */ 920 */
906static void 921static void
907coro_run (void *arg) 922cctx_run (void *arg)
908{ 923{
909 dTHX; 924 dTHX;
910 925
911 /* coro_run is the alternative tail of transfer(), so unlock here. */ 926 /* cctx_run is the alternative tail of transfer(), so unlock here. */
912 UNLOCK; 927 UNLOCK;
913 928
914 /* we now skip the entersub that lead to transfer() */ 929 /* we now skip the entersub that lead to transfer() */
915 PL_op = PL_op->op_next; 930 PL_op = PL_op->op_next;
916 931
917 /* inject a fake subroutine call to cctx_init */ 932 /* inject a fake subroutine call to cctx_init */
918 prepare_cctx (aTHX_ (coro_cctx *)arg); 933 cctx_prepare (aTHX_ (coro_cctx *)arg);
919 934
920 /* somebody or something will hit me for both perl_run and PL_restartop */ 935 /* somebody or something will hit me for both perl_run and PL_restartop */
921 PL_restartop = PL_op; 936 PL_restartop = PL_op;
922 perl_run (PL_curinterp); 937 perl_run (PL_curinterp);
923 938
942 ++cctx_count; 957 ++cctx_count;
943 958
944 Newz (0, cctx, 1, coro_cctx); 959 Newz (0, cctx, 1, coro_cctx);
945 960
946#if HAVE_MMAP 961#if HAVE_MMAP
947
948 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 962 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
949 /* mmap supposedly does allocate-on-write for us */ 963 /* mmap supposedly does allocate-on-write for us */
950 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); 964 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
951 965
952 if (cctx->sptr != (void *)-1) 966 if (cctx->sptr != (void *)-1)
973 stack_start = cctx->sptr; 987 stack_start = cctx->sptr;
974 stack_size = cctx->ssize; 988 stack_size = cctx->ssize;
975 } 989 }
976 990
977 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 991 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size);
978 coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); 992 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
979 993
980 return cctx; 994 return cctx;
981} 995}
982 996
983static void 997static void
1124 prev->cctx = 0; 1138 prev->cctx = 0;
1125 1139
1126 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1140 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */
1127 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1141 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1128 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1142 if (expect_false (CCTX_EXPIRED (prev__cctx)))
1143 if (!next->cctx)
1129 next->cctx = cctx_get (aTHX); 1144 next->cctx = cctx_get (aTHX);
1130 1145
1131 cctx_put (prev__cctx); 1146 cctx_put (prev__cctx);
1132 } 1147 }
1133 1148
1134 ++next->usecount; 1149 ++next->usecount;
1487 BOOT_PAGESIZE; 1502 BOOT_PAGESIZE;
1488 1503
1489 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1504 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1490 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1505 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1491 1506
1507 hv_sig = coro_get_hv ("SIG", TRUE);
1492 sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); 1508 sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE);
1493 sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); 1509 sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE);
1494 1510
1495 if (!PL_diehook) PL_diehook = SvREFCNT_inc (sv_diehook); 1511 if (!PL_diehook ) hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);
1496 if (!PL_warnhook) PL_warnhook = SvREFCNT_inc (sv_warnhook); 1512 if (!PL_warnhook) hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);
1497 1513
1498 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1514 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1499 1515
1500 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1516 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1501 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1517 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1828 CODE: 1844 CODE:
1829{ 1845{
1830 struct coro *coro = SvSTATE (coro_current); 1846 struct coro *coro = SvSTATE (coro_current);
1831 HV *hv = (HV *)SvRV (coro_current); 1847 HV *hv = (HV *)SvRV (coro_current);
1832 AV *defav = GvAV (PL_defgv); 1848 AV *defav = GvAV (PL_defgv);
1833 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0); 1849 SV *invoke = hv_delete (hv, strpair ("_invoke"), 0);
1834 AV *invoke_av; 1850 AV *invoke_av;
1835 int i, len; 1851 int i, len;
1836 1852
1837 if (!invoke) 1853 if (!invoke)
1838 croak ("\3async_pool terminate\2\n"); 1854 croak ("\3async_pool terminate\2\n");
1839 1855
1840 SvREFCNT_dec (coro->saved_deffh); 1856 SvREFCNT_dec (coro->saved_deffh);
1841 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 1857 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv);
1842 1858
1843 hv_store (hv, "desc", sizeof ("desc") - 1, 1859 hv_store (hv, "desc", sizeof ("desc") - 1,
1844 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0); 1860 newSVpvn (strpair ("[async_pool]")), 0);
1845 1861
1846 invoke_av = (AV *)SvRV (invoke); 1862 invoke_av = (AV *)SvRV (invoke);
1847 len = av_len (invoke_av); 1863 len = av_len (invoke_av);
1848 1864
1849 sv_setsv (cb, AvARRAY (invoke_av)[0]); 1865 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1872 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 1888 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1873 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1889 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1874 croak ("\3async_pool terminate\2\n"); 1890 croak ("\3async_pool terminate\2\n");
1875 1891
1876 av_clear (GvAV (PL_defgv)); 1892 av_clear (GvAV (PL_defgv));
1877 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1, 1893 hv_store ((HV *)SvRV (coro_current), strpair ("desc"),
1878 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0); 1894 newSVpvn (strpair ("[async_pool idle]")), 0);
1879 1895
1880 coro->prio = 0; 1896 coro->prio = 0;
1881 1897
1882 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 1898 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1883 api_trace (coro_current, 0); 1899 api_trace (coro_current, 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines