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.324 by root, Sat Nov 22 07:02:28 2008 UTC vs.
Revision 1.332 by root, Thu Nov 27 12:20:31 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
353 HV *st; 355 HV *st;
354 GV *gvp; 356 GV *gvp;
355 return sv_2cv (sv, &st, &gvp, 0); 357 return sv_2cv (sv, &st, &gvp, 0);
356} 358}
357 359
360/*****************************************************************************/
361/* magic glue */
362
363#define CORO_MAGIC_type_cv 26
364#define CORO_MAGIC_type_state PERL_MAGIC_ext
365
366#define CORO_MAGIC_NN(sv, type) \
367 (expect_true (SvMAGIC (sv)->mg_type == type) \
368 ? SvMAGIC (sv) \
369 : mg_find (sv, type))
370
371#define CORO_MAGIC(sv, type) \
372 (expect_true (SvMAGIC (sv)) \
373 ? CORO_MAGIC_NN (sv, type) \
374 : 0)
375
376#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
377#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
378
379INLINE struct coro *
380SvSTATE_ (pTHX_ SV *coro)
381{
382 HV *stash;
383 MAGIC *mg;
384
385 if (SvROK (coro))
386 coro = SvRV (coro);
387
388 if (expect_false (SvTYPE (coro) != SVt_PVHV))
389 croak ("Coro::State object required");
390
391 stash = SvSTASH (coro);
392 if (expect_false (stash != coro_stash && stash != coro_state_stash))
393 {
394 /* very slow, but rare, check */
395 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
396 croak ("Coro::State object required");
397 }
398
399 mg = CORO_MAGIC_state (coro);
400 return (struct coro *)mg->mg_ptr;
401}
402
403#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
404
405/* faster than SvSTATE, but expects a coroutine hv */
406#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
407#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
408
409/*****************************************************************************/
410/* padlist management and caching */
411
358static AV * 412static AV *
359coro_derive_padlist (pTHX_ CV *cv) 413coro_derive_padlist (pTHX_ CV *cv)
360{ 414{
361 AV *padlist = CvPADLIST (cv); 415 AV *padlist = CvPADLIST (cv);
362 AV *newpadlist, *newpad; 416 AV *newpadlist, *newpad;
369 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 423 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
370#endif 424#endif
371 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 425 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
372 --AvFILLp (padlist); 426 --AvFILLp (padlist);
373 427
374 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 428 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
375 av_store (newpadlist, 1, (SV *)newpad); 429 av_store (newpadlist, 1, (SV *)newpad);
376 430
377 return newpadlist; 431 return newpadlist;
378} 432}
379 433
380static void 434static void
381free_padlist (pTHX_ AV *padlist) 435free_padlist (pTHX_ AV *padlist)
382{ 436{
383 /* may be during global destruction */ 437 /* may be during global destruction */
384 if (SvREFCNT (padlist)) 438 if (!IN_DESTRUCT)
385 { 439 {
386 I32 i = AvFILLp (padlist); 440 I32 i = AvFILLp (padlist);
387 while (i >= 0) 441
442 while (i > 0) /* special-case index 0 */
388 { 443 {
389 SV **svp = av_fetch (padlist, i--, FALSE); 444 /* we try to be extra-careful here */
390 if (svp) 445 AV *av = (AV *)AvARRAY (padlist)[i--];
391 { 446 I32 j = AvFILLp (av);
392 SV *sv; 447
393 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 448 while (j >= 0)
449 SvREFCNT_dec (AvARRAY (av)[j--]);
450
451 AvFILLp (av) = -1;
394 SvREFCNT_dec (sv); 452 SvREFCNT_dec (av);
395
396 SvREFCNT_dec (*svp);
397 }
398 } 453 }
399 454
455 SvREFCNT_dec (AvARRAY (padlist)[0]);
456
457 AvFILLp (padlist) = -1;
400 SvREFCNT_dec ((SV*)padlist); 458 SvREFCNT_dec ((SV*)padlist);
401 } 459 }
402} 460}
403 461
404static int 462static int
413 471
414 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 472 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
415 473
416 return 0; 474 return 0;
417} 475}
418
419#define CORO_MAGIC_type_cv 26
420#define CORO_MAGIC_type_state PERL_MAGIC_ext
421 476
422static MGVTBL coro_cv_vtbl = { 477static MGVTBL coro_cv_vtbl = {
423 0, 0, 0, 0, 478 0, 0, 0, 0,
424 coro_cv_free 479 coro_cv_free
425}; 480};
426
427#define CORO_MAGIC_NN(sv, type) \
428 (expect_true (SvMAGIC (sv)->mg_type == type) \
429 ? SvMAGIC (sv) \
430 : mg_find (sv, type))
431
432#define CORO_MAGIC(sv, type) \
433 (expect_true (SvMAGIC (sv)) \
434 ? CORO_MAGIC_NN (sv, type) \
435 : 0)
436
437#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
438#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
439
440INLINE struct coro *
441SvSTATE_ (pTHX_ SV *coro)
442{
443 HV *stash;
444 MAGIC *mg;
445
446 if (SvROK (coro))
447 coro = SvRV (coro);
448
449 if (expect_false (SvTYPE (coro) != SVt_PVHV))
450 croak ("Coro::State object required");
451
452 stash = SvSTASH (coro);
453 if (expect_false (stash != coro_stash && stash != coro_state_stash))
454 {
455 /* very slow, but rare, check */
456 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
457 croak ("Coro::State object required");
458 }
459
460 mg = CORO_MAGIC_state (coro);
461 return (struct coro *)mg->mg_ptr;
462}
463
464#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
465
466/* faster than SvSTATE, but expects a coroutine hv */
467#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
468#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
469 481
470/* the next two functions merely cache the padlists */ 482/* the next two functions merely cache the padlists */
471static void 483static void
472get_padlist (pTHX_ CV *cv) 484get_padlist (pTHX_ CV *cv)
473{ 485{
501 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 513 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
502 514
503 av = (AV *)mg->mg_obj; 515 av = (AV *)mg->mg_obj;
504 516
505 if (expect_false (AvFILLp (av) >= AvMAX (av))) 517 if (expect_false (AvFILLp (av) >= AvMAX (av)))
506 av_extend (av, AvMAX (av) + 1); 518 av_extend (av, AvFILLp (av) + 1);
507 519
508 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 520 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
509} 521}
510 522
511/** load & save, init *******************************************************/ 523/** load & save, init *******************************************************/
516 perl_slots *slot = c->slot; 528 perl_slots *slot = c->slot;
517 c->slot = 0; 529 c->slot = 0;
518 530
519 PL_mainstack = c->mainstack; 531 PL_mainstack = c->mainstack;
520 532
521 GvSV (PL_defgv) = slot->defsv; 533 GvSV (PL_defgv) = slot->defsv;
522 GvAV (PL_defgv) = slot->defav; 534 GvAV (PL_defgv) = slot->defav;
523 GvSV (PL_errgv) = slot->errsv; 535 GvSV (PL_errgv) = slot->errsv;
524 GvSV (irsgv) = slot->irsgv; 536 GvSV (irsgv) = slot->irsgv;
537 GvHV (PL_hintgv) = slot->hinthv;
525 538
526 #define VAR(name,type) PL_ ## name = slot->name; 539 #define VAR(name,type) PL_ ## name = slot->name;
527 # include "state.h" 540 # include "state.h"
528 #undef VAR 541 #undef VAR
529 542
615 c->mainstack = PL_mainstack; 628 c->mainstack = PL_mainstack;
616 629
617 { 630 {
618 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 631 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
619 632
620 slot->defav = GvAV (PL_defgv); 633 slot->defav = GvAV (PL_defgv);
621 slot->defsv = DEFSV; 634 slot->defsv = DEFSV;
622 slot->errsv = ERRSV; 635 slot->errsv = ERRSV;
623 slot->irsgv = GvSV (irsgv); 636 slot->irsgv = GvSV (irsgv);
637 slot->hinthv = GvHV (PL_hintgv);
624 638
625 #define VAR(name,type) slot->name = PL_ ## name; 639 #define VAR(name,type) slot->name = PL_ ## name;
626 # include "state.h" 640 # include "state.h"
627 #undef VAR 641 #undef VAR
628 } 642 }
864 878
865 PL_runops = RUNOPS_DEFAULT; 879 PL_runops = RUNOPS_DEFAULT;
866 PL_curcop = &PL_compiling; 880 PL_curcop = &PL_compiling;
867 PL_in_eval = EVAL_NULL; 881 PL_in_eval = EVAL_NULL;
868 PL_comppad = 0; 882 PL_comppad = 0;
883 PL_comppad_name = 0;
884 PL_comppad_name_fill = 0;
885 PL_comppad_name_floor = 0;
869 PL_curpm = 0; 886 PL_curpm = 0;
870 PL_curpad = 0; 887 PL_curpad = 0;
871 PL_localizing = 0; 888 PL_localizing = 0;
872 PL_dirty = 0; 889 PL_dirty = 0;
873 PL_restartop = 0; 890 PL_restartop = 0;
874#if PERL_VERSION_ATLEAST (5,10,0) 891#if PERL_VERSION_ATLEAST (5,10,0)
875 PL_parser = 0; 892 PL_parser = 0;
876#endif 893#endif
894 PL_hints = 0;
877 895
878 /* recreate the die/warn hooks */ 896 /* recreate the die/warn hooks */
879 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 897 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); 898 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
881 899
882 GvSV (PL_defgv) = newSV (0); 900 GvSV (PL_defgv) = newSV (0);
883 GvAV (PL_defgv) = coro->args; coro->args = 0; 901 GvAV (PL_defgv) = coro->args; coro->args = 0;
884 GvSV (PL_errgv) = newSV (0); 902 GvSV (PL_errgv) = newSV (0);
885 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 903 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
904 GvHV (PL_hintgv) = 0;
886 PL_rs = newSVsv (GvSV (irsgv)); 905 PL_rs = newSVsv (GvSV (irsgv));
887 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 906 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
888 907
889 { 908 {
890 dSP; 909 dSP;
944 SvREFCNT_dec (GvAV (PL_defgv)); 963 SvREFCNT_dec (GvAV (PL_defgv));
945 SvREFCNT_dec (GvSV (PL_errgv)); 964 SvREFCNT_dec (GvSV (PL_errgv));
946 SvREFCNT_dec (PL_defoutgv); 965 SvREFCNT_dec (PL_defoutgv);
947 SvREFCNT_dec (PL_rs); 966 SvREFCNT_dec (PL_rs);
948 SvREFCNT_dec (GvSV (irsgv)); 967 SvREFCNT_dec (GvSV (irsgv));
968 SvREFCNT_dec (GvHV (PL_hintgv));
949 969
950 SvREFCNT_dec (PL_diehook); 970 SvREFCNT_dec (PL_diehook);
951 SvREFCNT_dec (PL_warnhook); 971 SvREFCNT_dec (PL_warnhook);
952 972
953 SvREFCNT_dec (coro->saved_deffh); 973 SvREFCNT_dec (coro->saved_deffh);
1168 /* 1188 /*
1169 * If perl-run returns we assume exit() was being called or the coro 1189 * 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) 1190 * 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 1191 * 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" 1192 * bootstrap-time "top" top_env, as we cannot restore the "main"
1173 * coroutine as Coro has no such concept 1193 * coroutine as Coro has no such concept.
1194 * This actually isn't valid with the pthread backend, but OSes requiring
1195 * that backend are too broken to do it in a standards-compliant way.
1174 */ 1196 */
1175 PL_top_env = main_top_env; 1197 PL_top_env = main_top_env;
1176 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1198 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1177 } 1199 }
1178} 1200}
1329 /* TODO: throwing up here is considered harmful */ 1351 /* TODO: throwing up here is considered harmful */
1330 1352
1331 if (expect_true (prev != next)) 1353 if (expect_true (prev != next))
1332 { 1354 {
1333 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1355 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,"); 1356 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,");
1335 1357
1336 if (expect_false (next->flags & CF_RUNNING)) 1358 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,"); 1359 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1338 1360
1339 if (expect_false (next->flags & CF_DESTROYED)) 1361 if (expect_false (next->flags & CF_DESTROYED))
1582{ 1604{
1583 struct coro *coro; 1605 struct coro *coro;
1584 SV *sv_hook; 1606 SV *sv_hook;
1585 void (*xs_hook)(void); 1607 void (*xs_hook)(void);
1586 1608
1587 if (SvROK (coro_sv))
1588 coro_sv = SvRV (coro_sv);
1589
1590 coro = SvSTATE (coro_sv); 1609 coro = SvSTATE (coro_sv);
1591 1610
1592 if (coro->flags & CF_READY) 1611 if (coro->flags & CF_READY)
1593 return 0; 1612 return 0;
1594 1613
1668 } 1687 }
1669 } 1688 }
1670 else 1689 else
1671 { 1690 {
1672 /* nothing to schedule: call the idle handler */ 1691 /* nothing to schedule: call the idle handler */
1692 if (SvROK (sv_idle)
1693 && SvOBJECT (SvRV (sv_idle)))
1694 {
1695 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1696 api_ready (aTHX_ SvRV (sv_idle));
1697 --coro_nready;
1698 }
1699 else
1700 {
1673 dSP; 1701 dSP;
1674 1702
1675 ENTER; 1703 ENTER;
1676 SAVETMPS; 1704 SAVETMPS;
1677 1705
1678 PUSHMARK (SP); 1706 PUSHMARK (SP);
1679 PUTBACK; 1707 PUTBACK;
1680 call_sv (get_sv ("Coro::idle", FALSE), G_VOID | G_DISCARD); 1708 call_sv (sv_idle, G_VOID | G_DISCARD);
1681 1709
1682 FREETMPS; 1710 FREETMPS;
1683 LEAVE; 1711 LEAVE;
1712 }
1684 } 1713 }
1685 } 1714 }
1686} 1715}
1687 1716
1688INLINE void 1717INLINE void
2351 SV **ary; 2380 SV **ary;
2352 2381
2353 /* unfortunately, building manually saves memory */ 2382 /* unfortunately, building manually saves memory */
2354 Newx (ary, 2, SV *); 2383 Newx (ary, 2, SV *);
2355 AvALLOC (av) = ary; 2384 AvALLOC (av) = ary;
2385#if PERL_VERSION_ATLEAST (5,10,0)
2356 /*AvARRAY (av) = ary;*/ 2386 AvARRAY (av) = ary;
2387#else
2357 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2388 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2389 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2390 SvPVX ((SV *)av) = (char *)ary;
2391#endif
2358 AvMAX (av) = 1; 2392 AvMAX (av) = 1;
2359 AvFILLp (av) = 0; 2393 AvFILLp (av) = 0;
2360 ary [0] = newSViv (count); 2394 ary [0] = newSViv (count);
2361 2395
2362 return newRV_noinc ((SV *)av); 2396 return newRV_noinc ((SV *)av);
3098 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3132 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3099 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3133 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3100 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3134 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3101 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3135 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3102 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3136 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3137 sv_idle = coro_get_sv (aTHX_ "Coro::idle" , TRUE);
3103 3138
3104 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle); 3139 sv_async_pool_idle = newSVpv ("[async pool idle]", 0); SvREADONLY_on (sv_async_pool_idle);
3105 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro); 3140 sv_Coro = newSVpv ("Coro", 0); SvREADONLY_on (sv_Coro);
3106 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler); 3141 cv_pool_handler = get_cv ("Coro::pool_handler", GV_ADD); SvREADONLY_on (cv_pool_handler);
3107 cv_coro_state_new = get_cv ("Coro::State::new", 0); SvREADONLY_on (cv_coro_state_new); 3142 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