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.323 by root, Sat Nov 22 06:03:10 2008 UTC vs.
Revision 1.331 by root, Thu Nov 27 12:00:59 2008 UTC

133#else 133#else
134# define dSTACKLEVEL volatile void *stacklevel 134# define dSTACKLEVEL volatile void *stacklevel
135# define STACKLEVEL ((void *)&stacklevel) 135# define STACKLEVEL ((void *)&stacklevel)
136#endif 136#endif
137 137
138#define IN_DESTRUCT (PL_main_cv == Nullcv) 138#define IN_DESTRUCT PL_dirty
139 139
140#if __GNUC__ >= 3 140#if __GNUC__ >= 3
141# define attribute(x) __attribute__(x) 141# define attribute(x) __attribute__(x)
142# define expect(expr,value) __builtin_expect ((expr),(value)) 142# define expect(expr,value) __builtin_expect ((expr),(value))
143# define INLINE static inline 143# define INLINE static inline
175static HV *coro_state_stash, *coro_stash; 175static HV *coro_state_stash, *coro_stash;
176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */ 176static volatile SV *coro_mortal; /* will be freed/thrown after next transfer */
177 177
178static AV *av_destroy; /* destruction queue */ 178static AV *av_destroy; /* destruction queue */
179static SV *sv_manager; /* the manager coro */ 179static SV *sv_manager; /* the manager coro */
180static SV *sv_idle; /* $Coro::idle */
180 181
181static GV *irsgv; /* $/ */ 182static GV *irsgv; /* $/ */
182static GV *stdoutgv; /* *STDOUT */ 183static GV *stdoutgv; /* *STDOUT */
183static SV *rv_diehook; 184static SV *rv_diehook;
184static SV *rv_warnhook; 185static SV *rv_warnhook;
246{ 247{
247 SV *defsv; 248 SV *defsv;
248 AV *defav; 249 AV *defav;
249 SV *errsv; 250 SV *errsv;
250 SV *irsgv; 251 SV *irsgv;
252 HV *hinthv;
251#define VAR(name,type) type name; 253#define VAR(name,type) type name;
252# include "state.h" 254# include "state.h"
253#undef VAR 255#undef VAR
254} perl_slots; 256} perl_slots;
255 257
379 381
380static void 382static void
381free_padlist (pTHX_ AV *padlist) 383free_padlist (pTHX_ AV *padlist)
382{ 384{
383 /* may be during global destruction */ 385 /* may be during global destruction */
384 if (SvREFCNT (padlist)) 386 if (!IN_DESTRUCT)
385 { 387 {
386 I32 i = AvFILLp (padlist); 388 I32 i = AvFILLp (padlist);
389
387 while (i >= 0) 390 while (i >= 0)
388 { 391 {
389 SV **svp = av_fetch (padlist, i--, FALSE); 392 /* we try to be extra-careful here */
390 if (svp) 393 AV *av = (AV *)AvARRAY (padlist)[i--];
391 { 394
392 SV *sv; 395 I32 i = AvFILLp (av);
393 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 396
397 while (i >= 0)
398 SvREFCNT_dec (AvARRAY (av)[i--]);
399
400 AvFILLp (av) = -1;
394 SvREFCNT_dec (sv); 401 SvREFCNT_dec (av);
395
396 SvREFCNT_dec (*svp);
397 }
398 } 402 }
399 403
404 AvFILLp (padlist) = -1;
400 SvREFCNT_dec ((SV*)padlist); 405 SvREFCNT_dec ((SV*)padlist);
401 } 406 }
402} 407}
403 408
404static int 409static int
501 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 506 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
502 507
503 av = (AV *)mg->mg_obj; 508 av = (AV *)mg->mg_obj;
504 509
505 if (expect_false (AvFILLp (av) >= AvMAX (av))) 510 if (expect_false (AvFILLp (av) >= AvMAX (av)))
506 av_extend (av, AvMAX (av) + 1); 511 av_extend (av, AvFILLp (av) + 1);
507 512
508 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 513 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
509} 514}
510 515
511/** load & save, init *******************************************************/ 516/** load & save, init *******************************************************/
516 perl_slots *slot = c->slot; 521 perl_slots *slot = c->slot;
517 c->slot = 0; 522 c->slot = 0;
518 523
519 PL_mainstack = c->mainstack; 524 PL_mainstack = c->mainstack;
520 525
521 GvSV (PL_defgv) = slot->defsv; 526 GvSV (PL_defgv) = slot->defsv;
522 GvAV (PL_defgv) = slot->defav; 527 GvAV (PL_defgv) = slot->defav;
523 GvSV (PL_errgv) = slot->errsv; 528 GvSV (PL_errgv) = slot->errsv;
524 GvSV (irsgv) = slot->irsgv; 529 GvSV (irsgv) = slot->irsgv;
530 GvHV (PL_hintgv) = slot->hinthv;
525 531
526 #define VAR(name,type) PL_ ## name = slot->name; 532 #define VAR(name,type) PL_ ## name = slot->name;
527 # include "state.h" 533 # include "state.h"
528 #undef VAR 534 #undef VAR
529 535
615 c->mainstack = PL_mainstack; 621 c->mainstack = PL_mainstack;
616 622
617 { 623 {
618 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 624 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
619 625
620 slot->defav = GvAV (PL_defgv); 626 slot->defav = GvAV (PL_defgv);
621 slot->defsv = DEFSV; 627 slot->defsv = DEFSV;
622 slot->errsv = ERRSV; 628 slot->errsv = ERRSV;
623 slot->irsgv = GvSV (irsgv); 629 slot->irsgv = GvSV (irsgv);
630 slot->hinthv = GvHV (PL_hintgv);
624 631
625 #define VAR(name,type) slot->name = PL_ ## name; 632 #define VAR(name,type) slot->name = PL_ ## name;
626 # include "state.h" 633 # include "state.h"
627 #undef VAR 634 #undef VAR
628 } 635 }
864 871
865 PL_runops = RUNOPS_DEFAULT; 872 PL_runops = RUNOPS_DEFAULT;
866 PL_curcop = &PL_compiling; 873 PL_curcop = &PL_compiling;
867 PL_in_eval = EVAL_NULL; 874 PL_in_eval = EVAL_NULL;
868 PL_comppad = 0; 875 PL_comppad = 0;
876 PL_comppad_name = 0;
877 PL_comppad_name_fill = 0;
878 PL_comppad_name_floor = 0;
869 PL_curpm = 0; 879 PL_curpm = 0;
870 PL_curpad = 0; 880 PL_curpad = 0;
871 PL_localizing = 0; 881 PL_localizing = 0;
872 PL_dirty = 0; 882 PL_dirty = 0;
873 PL_restartop = 0; 883 PL_restartop = 0;
874#if PERL_VERSION_ATLEAST (5,10,0) 884#if PERL_VERSION_ATLEAST (5,10,0)
875 PL_parser = 0; 885 PL_parser = 0;
876#endif 886#endif
887 PL_hints = 0;
877 888
878 /* recreate the die/warn hooks */ 889 /* recreate the die/warn hooks */
879 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 890 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
880 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); 891 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
881 892
882 GvSV (PL_defgv) = newSV (0); 893 GvSV (PL_defgv) = newSV (0);
883 GvAV (PL_defgv) = coro->args; coro->args = 0; 894 GvAV (PL_defgv) = coro->args; coro->args = 0;
884 GvSV (PL_errgv) = newSV (0); 895 GvSV (PL_errgv) = newSV (0);
885 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 896 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
897 GvHV (PL_hintgv) = 0;
886 PL_rs = newSVsv (GvSV (irsgv)); 898 PL_rs = newSVsv (GvSV (irsgv));
887 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 899 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
888 900
889 { 901 {
890 dSP; 902 dSP;
944 SvREFCNT_dec (GvAV (PL_defgv)); 956 SvREFCNT_dec (GvAV (PL_defgv));
945 SvREFCNT_dec (GvSV (PL_errgv)); 957 SvREFCNT_dec (GvSV (PL_errgv));
946 SvREFCNT_dec (PL_defoutgv); 958 SvREFCNT_dec (PL_defoutgv);
947 SvREFCNT_dec (PL_rs); 959 SvREFCNT_dec (PL_rs);
948 SvREFCNT_dec (GvSV (irsgv)); 960 SvREFCNT_dec (GvSV (irsgv));
961 SvREFCNT_dec (GvHV (PL_hintgv));
949 962
950 SvREFCNT_dec (PL_diehook); 963 SvREFCNT_dec (PL_diehook);
951 SvREFCNT_dec (PL_warnhook); 964 SvREFCNT_dec (PL_warnhook);
952 965
953 SvREFCNT_dec (coro->saved_deffh); 966 SvREFCNT_dec (coro->saved_deffh);
1168 /* 1181 /*
1169 * If perl-run returns we assume exit() was being called or the coro 1182 * If perl-run returns we assume exit() was being called or the coro
1170 * fell off the end, which seems to be the only valid (non-bug) 1183 * fell off the end, which seems to be the only valid (non-bug)
1171 * reason for perl_run to return. We try to exit by jumping to the 1184 * reason for perl_run to return. We try to exit by jumping to the
1172 * bootstrap-time "top" top_env, as we cannot restore the "main" 1185 * bootstrap-time "top" top_env, as we cannot restore the "main"
1173 * coroutine as Coro has no such concept 1186 * coroutine as Coro has no such concept.
1187 * This actually isn't valid with the pthread backend, but OSes requiring
1188 * that backend are too broken to do it in a standards-compliant way.
1174 */ 1189 */
1175 PL_top_env = main_top_env; 1190 PL_top_env = main_top_env;
1176 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1191 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1177 } 1192 }
1178} 1193}
1329 /* TODO: throwing up here is considered harmful */ 1344 /* TODO: throwing up here is considered harmful */
1330 1345
1331 if (expect_true (prev != next)) 1346 if (expect_true (prev != next))
1332 { 1347 {
1333 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1348 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1334 croak ("Coro::State::transfer called with non-running/new prev Coro::State, but can only transfer from running or new states,"); 1349 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,");
1335 1350
1336 if (expect_false (next->flags & CF_RUNNING)) 1351 if (expect_false (next->flags & CF_RUNNING))
1337 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,"); 1352 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1338 1353
1339 if (expect_false (next->flags & CF_DESTROYED)) 1354 if (expect_false (next->flags & CF_DESTROYED))
1383 coro_setup (aTHX_ next); 1398 coro_setup (aTHX_ next);
1384 } 1399 }
1385 else 1400 else
1386 load_perl (aTHX_ next); 1401 load_perl (aTHX_ next);
1387 1402
1403 assert (!prev->cctx);//D temporary
1404
1388 /* possibly untie and reuse the cctx */ 1405 /* possibly untie and reuse the cctx */
1389 if (expect_true ( 1406 if (expect_true (
1390 cctx_current->idle_sp == STACKLEVEL 1407 cctx_current->idle_sp == STACKLEVEL
1391 && !(cctx_current->flags & CC_TRACE) 1408 && !(cctx_current->flags & CC_TRACE)
1392 && !force_cctx 1409 && !force_cctx
1393 )) 1410 ))
1394 { 1411 {
1395 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ 1412 /* I assume that stacklevel is a stronger indicator than PL_top_env changes */
1396 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); 1413 assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te));
1397 1414
1398 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1415 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */
1399 /* without this the next cctx_get might destroy the prev__cctx while still in use */ 1416 /* without this the next cctx_get might destroy the running cctx while still in use */
1400 if (expect_false (CCTX_EXPIRED (cctx_current))) 1417 if (expect_false (CCTX_EXPIRED (cctx_current)))
1401 if (!next->cctx) 1418 if (expect_true (!next->cctx))
1402 next->cctx = cctx_get (aTHX); 1419 next->cctx = cctx_get (aTHX);
1403 1420
1404 cctx_put (cctx_current); 1421 cctx_put (cctx_current);
1405 assert (!prev->cctx);//D temporary
1406 } 1422 }
1407 else 1423 else
1408 prev->cctx = cctx_current; 1424 prev->cctx = cctx_current;
1409 1425
1410 ++next->usecount; 1426 ++next->usecount;
1581{ 1597{
1582 struct coro *coro; 1598 struct coro *coro;
1583 SV *sv_hook; 1599 SV *sv_hook;
1584 void (*xs_hook)(void); 1600 void (*xs_hook)(void);
1585 1601
1586 if (SvROK (coro_sv))
1587 coro_sv = SvRV (coro_sv);
1588
1589 coro = SvSTATE (coro_sv); 1602 coro = SvSTATE (coro_sv);
1590 1603
1591 if (coro->flags & CF_READY) 1604 if (coro->flags & CF_READY)
1592 return 0; 1605 return 0;
1593 1606
1667 } 1680 }
1668 } 1681 }
1669 else 1682 else
1670 { 1683 {
1671 /* nothing to schedule: call the idle handler */ 1684 /* nothing to schedule: call the idle handler */
1685 if (SvROK (sv_idle)
1686 && SvOBJECT (SvRV (sv_idle)))
1687 {
1688 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1689 api_ready (aTHX_ SvRV (sv_idle));
1690 --coro_nready;
1691 }
1692 else
1693 {
1672 dSP; 1694 dSP;
1673 1695
1674 ENTER; 1696 ENTER;
1675 SAVETMPS; 1697 SAVETMPS;
1676 1698
1677 PUSHMARK (SP); 1699 PUSHMARK (SP);
1678 PUTBACK; 1700 PUTBACK;
1679 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1701 call_sv (sv_idle, G_VOID | G_DISCARD);
1680 1702
1681 FREETMPS; 1703 FREETMPS;
1682 LEAVE; 1704 LEAVE;
1705 }
1683 } 1706 }
1684 } 1707 }
1685} 1708}
1686 1709
1687INLINE void 1710INLINE void
1758static void 1781static void
1759api_trace (pTHX_ SV *coro_sv, int flags) 1782api_trace (pTHX_ SV *coro_sv, int flags)
1760{ 1783{
1761 struct coro *coro = SvSTATE (coro_sv); 1784 struct coro *coro = SvSTATE (coro_sv);
1762 1785
1786 if (coro->flags & CF_RUNNING)
1787 croak ("cannot enable tracing on a running coroutine, caught");
1788
1763 if (flags & CC_TRACE) 1789 if (flags & CC_TRACE)
1764 { 1790 {
1765 if (!coro->cctx) 1791 if (!coro->cctx)
1766 coro->cctx = cctx_new_run (); 1792 coro->cctx = cctx_new_run ();
1767 else if (!(coro->cctx->flags & CC_TRACE)) 1793 else if (!(coro->cctx->flags & CC_TRACE))
1768 croak ("cannot enable tracing on coroutine with custom stack,"); 1794 croak ("cannot enable tracing on coroutine with custom stack, caught");
1769 1795
1770 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL)); 1796 coro->cctx->flags |= CC_NOREUSE | (flags & (CC_TRACE | CC_TRACE_ALL));
1771 } 1797 }
1772 else if (coro->cctx && coro->cctx->flags & CC_TRACE) 1798 else if (coro->cctx && coro->cctx->flags & CC_TRACE)
1773 { 1799 {
2347 SV **ary; 2373 SV **ary;
2348 2374
2349 /* unfortunately, building manually saves memory */ 2375 /* unfortunately, building manually saves memory */
2350 Newx (ary, 2, SV *); 2376 Newx (ary, 2, SV *);
2351 AvALLOC (av) = ary; 2377 AvALLOC (av) = ary;
2378#if PERL_VERSION_ATLEAST (5,10,0)
2352 /*AvARRAY (av) = ary;*/ 2379 AvARRAY (av) = ary;
2380#else
2353 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2381 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2382 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2383 SvPVX ((SV *)av) = (char *)ary;
2384#endif
2354 AvMAX (av) = 1; 2385 AvMAX (av) = 1;
2355 AvFILLp (av) = 0; 2386 AvFILLp (av) = 0;
2356 ary [0] = newSViv (count); 2387 ary [0] = newSViv (count);
2357 2388
2358 return newRV_noinc ((SV *)av); 2389 return newRV_noinc ((SV *)av);
3094 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3125 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3095 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3126 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3096 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3127 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3097 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3128 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3098 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3129 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3130 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3099 3131
3100 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3132 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3101 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3133 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3102 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3134 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3103 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3135 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines