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.215 by root, Sun Oct 14 19:34:09 2007 UTC

10#include "patchlevel.h" 10#include "patchlevel.h"
11 11
12#include <stdio.h> 12#include <stdio.h>
13#include <errno.h> 13#include <errno.h>
14#include <assert.h> 14#include <assert.h>
15#include <inttypes.h> /* portable stdint.h */
15 16
16#ifdef HAVE_MMAP 17#ifdef HAVE_MMAP
17# include <unistd.h> 18# include <unistd.h>
18# include <sys/mman.h> 19# include <sys/mman.h>
19# ifndef MAP_ANONYMOUS 20# ifndef MAP_ANONYMOUS
148static HV *coro_state_stash, *coro_stash; 149static HV *coro_state_stash, *coro_stash;
149static SV *coro_mortal; /* will be freed after next transfer */ 150static SV *coro_mortal; /* will be freed after next transfer */
150 151
151static GV *irsgv; /* $/ */ 152static GV *irsgv; /* $/ */
152static GV *stdoutgv; /* *STDOUT */ 153static GV *stdoutgv; /* *STDOUT */
153static SV *sv_diehook; 154static SV *rv_diehook;
154static SV *sv_warnhook; 155static SV *rv_warnhook;
156static HV *hv_sig; /* %SIG */
155 157
156/* async_pool helper stuff */ 158/* async_pool helper stuff */
157static SV *sv_pool_rss; 159static SV *sv_pool_rss;
158static SV *sv_pool_size; 160static SV *sv_pool_size;
159static AV *av_async_pool; 161static AV *av_async_pool;
217 219
218 /* process data */ 220 /* process data */
219 AV *mainstack; 221 AV *mainstack;
220 perl_slots *slot; /* basically the saved sp */ 222 perl_slots *slot; /* basically the saved sp */
221 223
222 /* data associated with this coroutine (initial args) */ 224 AV *args; /* data associated with this coroutine (initial args) */
223 AV *args; 225 int refcnt; /* coroutines are refcounted, yes */
224 int refcnt;
225 int flags; /* CF_ flags */ 226 int flags; /* CF_ flags */
227 HV *hv; /* the perl hash associated with this coro, if any */
226 228
227 /* statistics */ 229 /* statistics */
228 int usecount; /* number of transfers to this coro */ 230 int usecount; /* number of transfers to this coro */
229 231
230 /* coro process data */ 232 /* coro process data */
231 int prio; 233 int prio;
232 SV *throw; 234 SV *throw; /* exception to be thrown */
233 235
234 /* async_pool */ 236 /* async_pool */
235 SV *saved_deffh; 237 SV *saved_deffh;
236 238
237 /* linked list */ 239 /* linked list */
238 struct coro *next, *prev; 240 struct coro *next, *prev;
239 HV *hv; /* the perl hash associated with this coro, if any */
240}; 241};
241 242
242typedef struct coro *Coro__State; 243typedef struct coro *Coro__State;
243typedef struct coro *Coro__State_or_hashref; 244typedef struct coro *Coro__State_or_hashref;
244 245
258static struct coro *coro_first; 259static struct coro *coro_first;
259 260
260/** lowlevel stuff **********************************************************/ 261/** lowlevel stuff **********************************************************/
261 262
262static SV * 263static SV *
263coro_get_sv (const char *name, int create) 264coro_get_sv (pTHX_ const char *name, int create)
264{ 265{
265#if PERL_VERSION_ATLEAST (5,9,0) 266#if PERL_VERSION_ATLEAST (5,9,0)
266 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 267 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
267 get_sv (name, create); 268 get_sv (name, create);
268#endif 269#endif
269 return get_sv (name, create); 270 return get_sv (name, create);
270} 271}
271 272
272static SV * 273static AV *
273coro_get_av (const char *name, int create) 274coro_get_av (pTHX_ const char *name, int create)
274{ 275{
275#if PERL_VERSION_ATLEAST (5,9,0) 276#if PERL_VERSION_ATLEAST (5,9,0)
276 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 277 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
277 get_av (name, create); 278 get_av (name, create);
278#endif 279#endif
279 return get_av (name, create); 280 return get_av (name, create);
281}
282
283static HV *
284coro_get_hv (pTHX_ const char *name, int create)
285{
286#if PERL_VERSION_ATLEAST (5,9,0)
287 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
288 get_hv (name, create);
289#endif
290 return get_hv (name, create);
280} 291}
281 292
282static AV * 293static AV *
283coro_clone_padlist (pTHX_ CV *cv) 294coro_clone_padlist (pTHX_ CV *cv)
284{ 295{
333 344
334 /* casting is fun. */ 345 /* casting is fun. */
335 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 346 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
336 free_padlist (aTHX_ padlist); 347 free_padlist (aTHX_ padlist);
337 348
338 SvREFCNT_dec (av);
339
340 return 0; 349 return 0;
341} 350}
342 351
343#define PERL_MAGIC_coro PERL_MAGIC_ext 352#define CORO_MAGIC_type_cv PERL_MAGIC_ext
353#define CORO_MAGIC_type_state PERL_MAGIC_ext
344 354
345static MGVTBL vtbl_coro = {0, 0, 0, 0, coro_cv_free}; 355static MGVTBL coro_cv_vtbl = {
356 0, 0, 0, 0,
357 coro_cv_free
358};
346 359
347#define CORO_MAGIC(cv) \ 360#define CORO_MAGIC(sv,type) \
348 SvMAGIC (cv) \ 361 SvMAGIC (sv) \
349 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 362 ? SvMAGIC (sv)->mg_type == type \
350 ? SvMAGIC (cv) \ 363 ? SvMAGIC (sv) \
351 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 364 : mg_find (sv, type) \
352 : 0 365 : 0
366
367#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
368#define CORO_MAGIC_state(sv) CORO_MAGIC (((SV *)(sv)), CORO_MAGIC_type_state)
353 369
354static struct coro * 370static struct coro *
355SvSTATE_ (pTHX_ SV *coro) 371SvSTATE_ (pTHX_ SV *coro)
356{ 372{
357 HV *stash; 373 HV *stash;
369 /* very slow, but rare, check */ 385 /* very slow, but rare, check */
370 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 386 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
371 croak ("Coro::State object required"); 387 croak ("Coro::State object required");
372 } 388 }
373 389
374 mg = CORO_MAGIC (coro); 390 mg = CORO_MAGIC_state (coro);
375 return (struct coro *)mg->mg_ptr; 391 return (struct coro *)mg->mg_ptr;
376} 392}
377 393
378#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) 394#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
379 395
380/* the next two functions merely cache the padlists */ 396/* the next two functions merely cache the padlists */
381static void 397static void
382get_padlist (pTHX_ CV *cv) 398get_padlist (pTHX_ CV *cv)
383{ 399{
384 MAGIC *mg = CORO_MAGIC (cv); 400 MAGIC *mg = CORO_MAGIC_cv (cv);
385 AV *av; 401 AV *av;
386 402
387 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 403 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
388 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 404 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
389 else 405 else
401} 417}
402 418
403static void 419static void
404put_padlist (pTHX_ CV *cv) 420put_padlist (pTHX_ CV *cv)
405{ 421{
406 MAGIC *mg = CORO_MAGIC (cv); 422 MAGIC *mg = CORO_MAGIC_cv (cv);
407 AV *av; 423 AV *av;
408 424
409 if (expect_false (!mg)) 425 if (expect_false (!mg))
410 { 426 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
411 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
412 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
413 mg->mg_virtual = &vtbl_coro;
414 mg->mg_obj = (SV *)newAV ();
415 }
416 427
417 av = (AV *)mg->mg_obj; 428 av = (AV *)mg->mg_obj;
418 429
419 if (expect_false (AvFILLp (av) >= AvMAX (av))) 430 if (expect_false (AvFILLp (av) >= AvMAX (av)))
420 av_extend (av, AvMAX (av) + 1); 431 av_extend (av, AvMAX (av) + 1);
507 518
508 PUTBACK; 519 PUTBACK;
509 } 520 }
510 521
511 /* allocate some space on the context stack for our purposes */ 522 /* allocate some space on the context stack for our purposes */
523 /* we manually unroll here, as usually 2 slots is enough */
524 if (SLOT_COUNT >= 1) CXINC;
525 if (SLOT_COUNT >= 2) CXINC;
526 if (SLOT_COUNT >= 3) CXINC;
512 { 527 {
513 /* we manually unroll here, as usually 2 slots is enough */
514 int i; 528 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) 529 for (i = 3; i < SLOT_COUNT; ++i)
519 CXINC; 530 CXINC;
520
521 cxstack_ix -= SLOT_COUNT; /* undo allocation */
522 } 531 }
532 cxstack_ix -= SLOT_COUNT; /* undo allocation */
523 533
524 c->mainstack = PL_mainstack; 534 c->mainstack = PL_mainstack;
525 535
526 { 536 {
527 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 537 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
654 return rss; 664 return rss;
655} 665}
656 666
657/** coroutine stack handling ************************************************/ 667/** coroutine stack handling ************************************************/
658 668
669#if 0
670static int (*orig_sigelem_get) (pTHX_ SV *sv, MAGIC *mg);
671
672/*
673 * This overrides the default magic get method of %SIG elements.
674 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
675 * and instead of tryign to save and restore the hash elements, we just provide
676 * readback here.
677 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
678 * not expecting this to actually change the hook. This is a potential problem
679 * when a schedule happens then, but we ignore this.
680 */
681static int
682coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
683{
684 return orig_sigelem_get (aTHX_ sv, mg);
685 const char *s = MgPV_nolen_const (mg);
686
687 if (*s == '_')
688 {
689 if (strEQ (s, "__DIE__" ) && PL_diehook ) return sv_setsv (sv, PL_diehook ), 0;
690 if (strEQ (s, "__WARN__") && PL_warnhook) return sv_setsv (sv, PL_warnhook), 0;
691 }
692
693 return orig_sigelem_get (aTHX_ sv, mg);
694}
695#endif
696
659static void 697static void
660coro_setup (pTHX_ struct coro *coro) 698coro_setup (pTHX_ struct coro *coro)
661{ 699{
662 /* 700 /*
663 * emulate part of the perl startup here. 701 * emulate part of the perl startup here.
671 PL_curpm = 0; 709 PL_curpm = 0;
672 PL_curpad = 0; 710 PL_curpad = 0;
673 PL_localizing = 0; 711 PL_localizing = 0;
674 PL_dirty = 0; 712 PL_dirty = 0;
675 PL_restartop = 0; 713 PL_restartop = 0;
714
715 /* recreate the die/warn hooks */
716 PL_diehook = 0;
717 hv_store (hv_sig, "__DIE__", sizeof ("__DIE__") - 1, newSV (0), 0);
676 PL_diehook = SvREFCNT_inc (sv_diehook); 718 PL_diehook = SvREFCNT_inc (rv_diehook);
719
720 PL_warnhook = 0;
721 hv_store (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, newSV (0), 0);
677 PL_warnhook = SvREFCNT_inc (sv_warnhook); 722 PL_warnhook = SvREFCNT_inc (rv_warnhook);
678 723
679 GvSV (PL_defgv) = newSV (0); 724 GvSV (PL_defgv) = newSV (0);
680 GvAV (PL_defgv) = coro->args; coro->args = 0; 725 GvAV (PL_defgv) = coro->args; coro->args = 0;
681 GvSV (PL_errgv) = newSV (0); 726 GvSV (PL_errgv) = newSV (0);
682 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 727 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
697 PL_op = (OP *)&myop; 742 PL_op = (OP *)&myop;
698 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 743 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
699 SPAGAIN; 744 SPAGAIN;
700 } 745 }
701 746
702 ENTER; /* necessary e.g. for dounwind and to balance the xsub-entersub */ 747 /* this newly created coroutine might be run on an existing cctx which most
748 * likely was suspended in set_stacklevel, called from entersub.
749 * set_stacklevl doesn't do anything on return, but entersub does LEAVE,
750 * so we ENTER here for symmetry
751 */
752 ENTER;
703} 753}
704 754
705static void 755static void
706coro_destroy (pTHX_ struct coro *coro) 756coro_destroy (pTHX_ struct coro *coro)
707{ 757{
874 924
875/* inject a fake call to Coro::State::_cctx_init into the execution */ 925/* 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 */ 926/* _cctx_init should be careful, as it could be called at almost any time */
877/* during execution of a perl program */ 927/* during execution of a perl program */
878static void NOINLINE 928static void NOINLINE
879prepare_cctx (pTHX_ coro_cctx *cctx) 929cctx_prepare (pTHX_ coro_cctx *cctx)
880{ 930{
881 dSP; 931 dSP;
882 LOGOP myop; 932 LOGOP myop;
883 933
884 PL_top_env = &PL_start_env; 934 PL_top_env = &PL_start_env;
902 952
903/* 953/*
904 * this is a _very_ stripped down perl interpreter ;) 954 * this is a _very_ stripped down perl interpreter ;)
905 */ 955 */
906static void 956static void
907coro_run (void *arg) 957cctx_run (void *arg)
908{ 958{
909 dTHX; 959 dTHX;
910 960
911 /* coro_run is the alternative tail of transfer(), so unlock here. */ 961 /* cctx_run is the alternative tail of transfer(), so unlock here. */
912 UNLOCK; 962 UNLOCK;
913 963
914 /* we now skip the entersub that lead to transfer() */ 964 /* we now skip the entersub that lead to transfer() */
915 PL_op = PL_op->op_next; 965 PL_op = PL_op->op_next;
916 966
917 /* inject a fake subroutine call to cctx_init */ 967 /* inject a fake subroutine call to cctx_init */
918 prepare_cctx (aTHX_ (coro_cctx *)arg); 968 cctx_prepare (aTHX_ (coro_cctx *)arg);
919 969
920 /* somebody or something will hit me for both perl_run and PL_restartop */ 970 /* somebody or something will hit me for both perl_run and PL_restartop */
921 PL_restartop = PL_op; 971 PL_restartop = PL_op;
922 perl_run (PL_curinterp); 972 perl_run (PL_curinterp);
923 973
942 ++cctx_count; 992 ++cctx_count;
943 993
944 Newz (0, cctx, 1, coro_cctx); 994 Newz (0, cctx, 1, coro_cctx);
945 995
946#if HAVE_MMAP 996#if HAVE_MMAP
947
948 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE; 997 cctx->ssize = ((coro_stacksize * sizeof (long) + PAGESIZE - 1) / PAGESIZE + CORO_STACKGUARD) * PAGESIZE;
949 /* mmap supposedly does allocate-on-write for us */ 998 /* 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); 999 cctx->sptr = mmap (0, cctx->ssize, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
951 1000
952 if (cctx->sptr != (void *)-1) 1001 if (cctx->sptr != (void *)-1)
973 stack_start = cctx->sptr; 1022 stack_start = cctx->sptr;
974 stack_size = cctx->ssize; 1023 stack_size = cctx->ssize;
975 } 1024 }
976 1025
977 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size); 1026 REGISTER_STACK (cctx, (char *)stack_start, (char *)stack_start + stack_size);
978 coro_create (&cctx->cctx, coro_run, (void *)cctx, stack_start, stack_size); 1027 coro_create (&cctx->cctx, cctx_run, (void *)cctx, stack_start, stack_size);
979 1028
980 return cctx; 1029 return cctx;
981} 1030}
982 1031
983static void 1032static void
1095 prev->flags &= ~CF_RUNNING; 1144 prev->flags &= ~CF_RUNNING;
1096 next->flags |= CF_RUNNING; 1145 next->flags |= CF_RUNNING;
1097 1146
1098 LOCK; 1147 LOCK;
1099 1148
1149 /* first get rid of the old state */
1150 save_perl (aTHX_ prev);
1151
1100 if (expect_false (next->flags & CF_NEW)) 1152 if (expect_false (next->flags & CF_NEW))
1101 { 1153 {
1102 /* need to start coroutine */ 1154 /* need to start coroutine */
1103 next->flags &= ~CF_NEW; 1155 next->flags &= ~CF_NEW;
1104 /* first get rid of the old state */
1105 save_perl (aTHX_ prev);
1106 /* setup coroutine call */ 1156 /* setup coroutine call */
1107 coro_setup (aTHX_ next); 1157 coro_setup (aTHX_ next);
1108 } 1158 }
1109 else 1159 else
1110 {
1111 /* coroutine already started */
1112 save_perl (aTHX_ prev);
1113 load_perl (aTHX_ next); 1160 load_perl (aTHX_ next);
1114 }
1115 1161
1116 prev__cctx = prev->cctx; 1162 prev__cctx = prev->cctx;
1117 1163
1118 /* possibly "free" the cctx */ 1164 /* possibly "free" the cctx */
1119 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE))) 1165 if (expect_true (prev__cctx->idle_sp == STACKLEVEL && !(prev__cctx->flags & CC_TRACE)))
1124 prev->cctx = 0; 1170 prev->cctx = 0;
1125 1171
1126 /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get */ 1172 /* 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 */ 1173 /* without this the next cctx_get might destroy the prev__cctx while still in use */
1128 if (expect_false (CCTX_EXPIRED (prev__cctx))) 1174 if (expect_false (CCTX_EXPIRED (prev__cctx)))
1175 if (!next->cctx)
1129 next->cctx = cctx_get (aTHX); 1176 next->cctx = cctx_get (aTHX);
1130 1177
1131 cctx_put (prev__cctx); 1178 cctx_put (prev__cctx);
1132 } 1179 }
1133 1180
1134 ++next->usecount; 1181 ++next->usecount;
1486#endif 1533#endif
1487 BOOT_PAGESIZE; 1534 BOOT_PAGESIZE;
1488 1535
1489 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1536 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1490 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1537 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1491 1538#if 0
1492 sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); 1539 orig_sigelem_get = PL_vtbl_sigelem.svt_get;
1493 sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); 1540 PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1494 1541#endif
1495 if (!PL_diehook) PL_diehook = SvREFCNT_inc (sv_diehook); 1542 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1496 if (!PL_warnhook) PL_warnhook = SvREFCNT_inc (sv_warnhook); 1543 rv_diehook = SvREFCNT_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1544 rv_warnhook = SvREFCNT_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1497 1545
1498 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1546 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1499 1547
1500 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1548 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1501 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1549 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1507 1555
1508 while (main_top_env->je_prev) 1556 while (main_top_env->je_prev)
1509 main_top_env = main_top_env->je_prev; 1557 main_top_env = main_top_env->je_prev;
1510 1558
1511 coroapi.ver = CORO_API_VERSION; 1559 coroapi.ver = CORO_API_VERSION;
1560 coroapi.rev = CORO_API_REVISION;
1512 coroapi.transfer = api_transfer; 1561 coroapi.transfer = api_transfer;
1513 1562
1514 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1563 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1515} 1564}
1516 1565
1517SV * 1566SV *
1518new (char *klass, ...) 1567new (char *klass, ...)
1519 CODE: 1568 CODE:
1520{ 1569{
1521 struct coro *coro; 1570 struct coro *coro;
1571 MAGIC *mg;
1522 HV *hv; 1572 HV *hv;
1523 int i; 1573 int i;
1524 1574
1525 Newz (0, coro, 1, struct coro); 1575 Newz (0, coro, 1, struct coro);
1526 coro->args = newAV (); 1576 coro->args = newAV ();
1529 if (coro_first) coro_first->prev = coro; 1579 if (coro_first) coro_first->prev = coro;
1530 coro->next = coro_first; 1580 coro->next = coro_first;
1531 coro_first = coro; 1581 coro_first = coro;
1532 1582
1533 coro->hv = hv = newHV (); 1583 coro->hv = hv = newHV ();
1534 sv_magicext ((SV *)hv, 0, PERL_MAGIC_ext, &coro_state_vtbl, (char *)coro, 0)->mg_flags |= MGf_DUP; 1584 mg = sv_magicext ((SV *)hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)coro, 0);
1585 mg->mg_flags |= MGf_DUP;
1535 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1586 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1536 1587
1537 av_extend (coro->args, items - 1); 1588 av_extend (coro->args, items - 1);
1538 for (i = 1; i < items; i++) 1589 for (i = 1; i < items; i++)
1539 av_push (coro->args, newSVsv (ST (i))); 1590 av_push (coro->args, newSVsv (ST (i)));
1583 1634
1584 break; 1635 break;
1585 } 1636 }
1586 1637
1587 BARRIER; 1638 BARRIER;
1639 PUTBACK;
1588 TRANSFER (ta); 1640 TRANSFER (ta);
1589 1641 SPAGAIN; /* might be the sp of a different coroutine now */
1590 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) 1642 /* be extra careful not to ever do anything after TRANSFER */
1591 XSRETURN_YES;
1592} 1643}
1593 1644
1594bool 1645bool
1595_destroy (SV *coro_sv) 1646_destroy (SV *coro_sv)
1596 CODE: 1647 CODE:
1656 1707
1657 { 1708 {
1658 dSP; 1709 dSP;
1659 ENTER; 1710 ENTER;
1660 SAVETMPS; 1711 SAVETMPS;
1712 PUTBACK;
1713 PUSHSTACK;
1661 PUSHMARK (SP); 1714 PUSHMARK (SP);
1662 PUTBACK;
1663 1715
1664 if (ix) 1716 if (ix)
1665 eval_sv (coderef, 0); 1717 eval_sv (coderef, 0);
1666 else 1718 else
1667 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1719 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1668 1720
1721 POPSTACK;
1669 SPAGAIN; 1722 SPAGAIN;
1670 FREETMPS; 1723 FREETMPS;
1671 LEAVE; 1724 LEAVE;
1672 PUTBACK; 1725 PUTBACK;
1673 } 1726 }
1731 1784
1732BOOT: 1785BOOT:
1733{ 1786{
1734 int i; 1787 int i;
1735 1788
1736 sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE); 1789 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
1737 sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE); 1790 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1738 av_async_pool = coro_get_av ("Coro::async_pool", TRUE); 1791 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1739 1792
1740 coro_current = coro_get_sv ("Coro::current", FALSE); 1793 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1741 SvREADONLY_on (coro_current); 1794 SvREADONLY_on (coro_current);
1742 1795
1743 coro_stash = gv_stashpv ("Coro", TRUE); 1796 coro_stash = gv_stashpv ("Coro", TRUE);
1744 1797
1745 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1798 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines