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.202 by root, Mon Oct 8 02:50:23 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
130#else 131#else
131# define LOCK (void)0 132# define LOCK (void)0
132# define UNLOCK (void)0 133# define UNLOCK (void)0
133#endif 134#endif
134 135
135#define strpair(const) const, sizeof (const) - 1
136
137/* helper storage struct for Coro::AIO */ 136/* helper storage struct for Coro::AIO */
138struct io_state 137struct io_state
139{ 138{
140 int errorno; 139 int errorno;
141 I32 laststype; 140 I32 laststype;
150static HV *coro_state_stash, *coro_stash; 149static HV *coro_state_stash, *coro_stash;
151static SV *coro_mortal; /* will be freed after next transfer */ 150static SV *coro_mortal; /* will be freed after next transfer */
152 151
153static GV *irsgv; /* $/ */ 152static GV *irsgv; /* $/ */
154static GV *stdoutgv; /* *STDOUT */ 153static GV *stdoutgv; /* *STDOUT */
155 154static SV *rv_diehook;
155static SV *rv_warnhook;
156static HV *hv_sig; /* %SIG */ 156static HV *hv_sig; /* %SIG */
157static SV *sv_diehook;
158static SV *sv_warnhook;
159 157
160/* async_pool helper stuff */ 158/* async_pool helper stuff */
161static SV *sv_pool_rss; 159static SV *sv_pool_rss;
162static SV *sv_pool_size; 160static SV *sv_pool_size;
163static AV *av_async_pool; 161static AV *av_async_pool;
221 219
222 /* process data */ 220 /* process data */
223 AV *mainstack; 221 AV *mainstack;
224 perl_slots *slot; /* basically the saved sp */ 222 perl_slots *slot; /* basically the saved sp */
225 223
226 /* data associated with this coroutine (initial args) */ 224 AV *args; /* data associated with this coroutine (initial args) */
227 AV *args; 225 int refcnt; /* coroutines are refcounted, yes */
228 int refcnt;
229 int flags; /* CF_ flags */ 226 int flags; /* CF_ flags */
227 HV *hv; /* the perl hash associated with this coro, if any */
230 228
231 /* statistics */ 229 /* statistics */
232 int usecount; /* number of transfers to this coro */ 230 int usecount; /* number of transfers to this coro */
233 231
234 /* coro process data */ 232 /* coro process data */
235 int prio; 233 int prio;
236 SV *throw; 234 SV *throw; /* exception to be thrown */
237 235
238 /* async_pool */ 236 /* async_pool */
239 SV *saved_deffh; 237 SV *saved_deffh;
240 238
241 /* linked list */ 239 /* linked list */
242 struct coro *next, *prev; 240 struct coro *next, *prev;
243 HV *hv; /* the perl hash associated with this coro, if any */
244}; 241};
245 242
246typedef struct coro *Coro__State; 243typedef struct coro *Coro__State;
247typedef struct coro *Coro__State_or_hashref; 244typedef struct coro *Coro__State_or_hashref;
248 245
262static struct coro *coro_first; 259static struct coro *coro_first;
263 260
264/** lowlevel stuff **********************************************************/ 261/** lowlevel stuff **********************************************************/
265 262
266static SV * 263static SV *
267coro_get_sv (const char *name, int create) 264coro_get_sv (pTHX_ const char *name, int create)
268{ 265{
269#if PERL_VERSION_ATLEAST (5,9,0) 266#if PERL_VERSION_ATLEAST (5,9,0)
270 /* 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 */
271 get_sv (name, create); 268 get_sv (name, create);
272#endif 269#endif
273 return get_sv (name, create); 270 return get_sv (name, create);
274} 271}
275 272
276static AV * 273static AV *
277coro_get_av (const char *name, int create) 274coro_get_av (pTHX_ const char *name, int create)
278{ 275{
279#if PERL_VERSION_ATLEAST (5,9,0) 276#if PERL_VERSION_ATLEAST (5,9,0)
280 /* 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 */
281 get_av (name, create); 278 get_av (name, create);
282#endif 279#endif
283 return get_av (name, create); 280 return get_av (name, create);
284} 281}
285 282
286static HV * 283static HV *
287coro_get_hv (const char *name, int create) 284coro_get_hv (pTHX_ const char *name, int create)
288{ 285{
289#if PERL_VERSION_ATLEAST (5,9,0) 286#if PERL_VERSION_ATLEAST (5,9,0)
290 /* silence stupid and wrong 5.10 warning that I am unable to switch off */ 287 /* silence stupid and wrong 5.10 warning that I am unable to switch off */
291 get_hv (name, create); 288 get_hv (name, create);
292#endif 289#endif
347 344
348 /* casting is fun. */ 345 /* casting is fun. */
349 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av))) 346 while (&PL_sv_undef != (SV *)(padlist = (AV *)av_pop (av)))
350 free_padlist (aTHX_ padlist); 347 free_padlist (aTHX_ padlist);
351 348
352 SvREFCNT_dec (av);
353
354 return 0; 349 return 0;
355} 350}
356 351
357#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
358 354
359static 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};
360 359
361#define CORO_MAGIC(cv) \ 360#define CORO_MAGIC(sv,type) \
362 SvMAGIC (cv) \ 361 SvMAGIC (sv) \
363 ? SvMAGIC (cv)->mg_type == PERL_MAGIC_coro \ 362 ? SvMAGIC (sv)->mg_type == type \
364 ? SvMAGIC (cv) \ 363 ? SvMAGIC (sv) \
365 : mg_find ((SV *)cv, PERL_MAGIC_coro) \ 364 : mg_find (sv, type) \
366 : 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)
367 369
368static struct coro * 370static struct coro *
369SvSTATE_ (pTHX_ SV *coro) 371SvSTATE_ (pTHX_ SV *coro)
370{ 372{
371 HV *stash; 373 HV *stash;
383 /* very slow, but rare, check */ 385 /* very slow, but rare, check */
384 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State")) 386 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
385 croak ("Coro::State object required"); 387 croak ("Coro::State object required");
386 } 388 }
387 389
388 mg = CORO_MAGIC (coro); 390 mg = CORO_MAGIC_state (coro);
389 return (struct coro *)mg->mg_ptr; 391 return (struct coro *)mg->mg_ptr;
390} 392}
391 393
392#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv)) 394#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
393 395
394/* the next two functions merely cache the padlists */ 396/* the next two functions merely cache the padlists */
395static void 397static void
396get_padlist (pTHX_ CV *cv) 398get_padlist (pTHX_ CV *cv)
397{ 399{
398 MAGIC *mg = CORO_MAGIC (cv); 400 MAGIC *mg = CORO_MAGIC_cv (cv);
399 AV *av; 401 AV *av;
400 402
401 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) 403 if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0))
402 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; 404 CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--];
403 else 405 else
415} 417}
416 418
417static void 419static void
418put_padlist (pTHX_ CV *cv) 420put_padlist (pTHX_ CV *cv)
419{ 421{
420 MAGIC *mg = CORO_MAGIC (cv); 422 MAGIC *mg = CORO_MAGIC_cv (cv);
421 AV *av; 423 AV *av;
422 424
423 if (expect_false (!mg)) 425 if (expect_false (!mg))
424 { 426 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
425 sv_magic ((SV *)cv, 0, PERL_MAGIC_coro, 0, 0);
426 mg = mg_find ((SV *)cv, PERL_MAGIC_coro);
427 mg->mg_virtual = &vtbl_coro;
428 mg->mg_obj = (SV *)newAV ();
429 }
430 427
431 av = (AV *)mg->mg_obj; 428 av = (AV *)mg->mg_obj;
432 429
433 if (expect_false (AvFILLp (av) >= AvMAX (av))) 430 if (expect_false (AvFILLp (av) >= AvMAX (av)))
434 av_extend (av, AvMAX (av) + 1); 431 av_extend (av, AvMAX (av) + 1);
452 GvSV (irsgv) = slot->irsgv; 449 GvSV (irsgv) = slot->irsgv;
453 450
454 #define VAR(name,type) PL_ ## name = slot->name; 451 #define VAR(name,type) PL_ ## name = slot->name;
455 # include "state.h" 452 # include "state.h"
456 #undef VAR 453 #undef VAR
457
458 /*hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);*/
459 /*hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);*/
460 454
461 { 455 {
462 dSP; 456 dSP;
463 457
464 CV *cv; 458 CV *cv;
557 * allocate various perl stacks. This is an exact copy 551 * allocate various perl stacks. This is an exact copy
558 * of perl.c:init_stacks, except that it uses less memory 552 * of perl.c:init_stacks, except that it uses less memory
559 * on the (sometimes correct) assumption that coroutines do 553 * on the (sometimes correct) assumption that coroutines do
560 * not usually need a lot of stackspace. 554 * not usually need a lot of stackspace.
561 */ 555 */
562#if 1 556#if CORO_PREFER_PERL_FUNCTIONS
563# define coro_init_stacks init_stacks 557# define coro_init_stacks init_stacks
564#else 558#else
565static void 559static void
566coro_init_stacks (pTHX) 560coro_init_stacks (pTHX)
567{ 561{
669 663
670 return rss; 664 return rss;
671} 665}
672 666
673/** coroutine stack handling ************************************************/ 667/** coroutine stack handling ************************************************/
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
674 696
675static void 697static void
676coro_setup (pTHX_ struct coro *coro) 698coro_setup (pTHX_ struct coro *coro)
677{ 699{
678 /* 700 /*
687 PL_curpm = 0; 709 PL_curpm = 0;
688 PL_curpad = 0; 710 PL_curpad = 0;
689 PL_localizing = 0; 711 PL_localizing = 0;
690 PL_dirty = 0; 712 PL_dirty = 0;
691 PL_restartop = 0; 713 PL_restartop = 0;
692 PL_diehook = 0; hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0); 714
693 PL_warnhook = 0; hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0); 715 /* recreate the die/warn hooks */
716 PL_diehook = 0;
717 hv_store (hv_sig, "__DIE__", sizeof ("__DIE__") - 1, newSV (0), 0);
718 PL_diehook = SvREFCNT_inc (rv_diehook);
719
720 PL_warnhook = 0;
721 hv_store (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, newSV (0), 0);
722 PL_warnhook = SvREFCNT_inc (rv_warnhook);
694 723
695 GvSV (PL_defgv) = newSV (0); 724 GvSV (PL_defgv) = newSV (0);
696 GvAV (PL_defgv) = coro->args; coro->args = 0; 725 GvAV (PL_defgv) = coro->args; coro->args = 0;
697 GvSV (PL_errgv) = newSV (0); 726 GvSV (PL_errgv) = newSV (0);
698 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);
713 PL_op = (OP *)&myop; 742 PL_op = (OP *)&myop;
714 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX); 743 PL_op = PL_ppaddr[OP_ENTERSUB](aTHX);
715 SPAGAIN; 744 SPAGAIN;
716 } 745 }
717 746
718 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;
719} 753}
720 754
721static void 755static void
722coro_destroy (pTHX_ struct coro *coro) 756coro_destroy (pTHX_ struct coro *coro)
723{ 757{
807 PUSHMARK (SP); 841 PUSHMARK (SP);
808 PUSHs (&PL_sv_no); 842 PUSHs (&PL_sv_no);
809 PUSHs (fullname); 843 PUSHs (fullname);
810 PUSHs (sv_2mortal (newRV_noinc ((SV *)av))); 844 PUSHs (sv_2mortal (newRV_noinc ((SV *)av)));
811 PUTBACK; 845 PUTBACK;
812 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); 846 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
813 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 847 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
814 SPAGAIN; 848 SPAGAIN;
815 FREETMPS; 849 FREETMPS;
816 LEAVE; 850 LEAVE;
817 PL_runops = runops_trace; 851 PL_runops = runops_trace;
846 PUSHMARK (SP); 880 PUSHMARK (SP);
847 PUSHs (&PL_sv_yes); 881 PUSHs (&PL_sv_yes);
848 PUSHs (fullname); 882 PUSHs (fullname);
849 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef); 883 PUSHs (cx->blk_sub.hasargs ? sv_2mortal (newRV_inc ((SV *)cx->blk_sub.argarray)) : &PL_sv_undef);
850 PUTBACK; 884 PUTBACK;
851 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_sub_cb"), 0); 885 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_sub_cb", sizeof ("_trace_sub_cb") - 1, 0);
852 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 886 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
853 SPAGAIN; 887 SPAGAIN;
854 FREETMPS; 888 FREETMPS;
855 LEAVE; 889 LEAVE;
856 PL_runops = runops_trace; 890 PL_runops = runops_trace;
870 PL_runops = RUNOPS_DEFAULT; 904 PL_runops = RUNOPS_DEFAULT;
871 PUSHMARK (SP); 905 PUSHMARK (SP);
872 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0))); 906 PUSHs (sv_2mortal (newSVpv (OutCopFILE (oldcop), 0)));
873 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop)))); 907 PUSHs (sv_2mortal (newSViv (CopLINE (oldcop))));
874 PUTBACK; 908 PUTBACK;
875 cb = hv_fetch ((HV *)SvRV (coro_current), strpair ("_trace_line_cb"), 0); 909 cb = hv_fetch ((HV *)SvRV (coro_current), "_trace_line_cb", sizeof ("_trace_line_cb") - 1, 0);
876 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 910 if (cb) call_sv (*cb, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
877 SPAGAIN; 911 SPAGAIN;
878 FREETMPS; 912 FREETMPS;
879 LEAVE; 913 LEAVE;
880 PL_runops = runops_trace; 914 PL_runops = runops_trace;
1110 prev->flags &= ~CF_RUNNING; 1144 prev->flags &= ~CF_RUNNING;
1111 next->flags |= CF_RUNNING; 1145 next->flags |= CF_RUNNING;
1112 1146
1113 LOCK; 1147 LOCK;
1114 1148
1149 /* first get rid of the old state */
1150 save_perl (aTHX_ prev);
1151
1115 if (expect_false (next->flags & CF_NEW)) 1152 if (expect_false (next->flags & CF_NEW))
1116 { 1153 {
1117 /* need to start coroutine */ 1154 /* need to start coroutine */
1118 next->flags &= ~CF_NEW; 1155 next->flags &= ~CF_NEW;
1119 /* first get rid of the old state */
1120 save_perl (aTHX_ prev);
1121 /* setup coroutine call */ 1156 /* setup coroutine call */
1122 coro_setup (aTHX_ next); 1157 coro_setup (aTHX_ next);
1123 } 1158 }
1124 else 1159 else
1125 {
1126 /* coroutine already started */
1127 save_perl (aTHX_ prev);
1128 load_perl (aTHX_ next); 1160 load_perl (aTHX_ next);
1129 }
1130 1161
1131 prev__cctx = prev->cctx; 1162 prev__cctx = prev->cctx;
1132 1163
1133 /* possibly "free" the cctx */ 1164 /* possibly "free" the cctx */
1134 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)))
1502#endif 1533#endif
1503 BOOT_PAGESIZE; 1534 BOOT_PAGESIZE;
1504 1535
1505 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV); 1536 irsgv = gv_fetchpv ("/" , GV_ADD|GV_NOTQUAL, SVt_PV);
1506 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO); 1537 stdoutgv = gv_fetchpv ("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
1507 1538#if 0
1539 orig_sigelem_get = PL_vtbl_sigelem.svt_get;
1540 PL_vtbl_sigelem.svt_get = coro_sigelem_get;
1541#endif
1508 hv_sig = coro_get_hv ("SIG", TRUE); 1542 hv_sig = coro_get_hv (aTHX_ "SIG", TRUE);
1509 sv_diehook = coro_get_sv ("Coro::State::DIEHOOK" , TRUE); 1543 rv_diehook = SvREFCNT_inc ((SV *)gv_fetchpv ("Coro::State::diehook" , 0, SVt_PVCV));
1510 sv_warnhook = coro_get_sv ("Coro::State::WARNHOOK", TRUE); 1544 rv_warnhook = SvREFCNT_inc ((SV *)gv_fetchpv ("Coro::State::warnhook", 0, SVt_PVCV));
1511
1512 if (!PL_diehook ) hv_store (hv_sig, strpair ("__DIE__" ), SvREFCNT_inc (sv_diehook ), 0);
1513 if (!PL_warnhook) hv_store (hv_sig, strpair ("__WARN__"), SvREFCNT_inc (sv_warnhook), 0);
1514 1545
1515 coro_state_stash = gv_stashpv ("Coro::State", TRUE); 1546 coro_state_stash = gv_stashpv ("Coro::State", TRUE);
1516 1547
1517 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE)); 1548 newCONSTSUB (coro_state_stash, "CC_TRACE" , newSViv (CC_TRACE));
1518 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB)); 1549 newCONSTSUB (coro_state_stash, "CC_TRACE_SUB" , newSViv (CC_TRACE_SUB));
1524 1555
1525 while (main_top_env->je_prev) 1556 while (main_top_env->je_prev)
1526 main_top_env = main_top_env->je_prev; 1557 main_top_env = main_top_env->je_prev;
1527 1558
1528 coroapi.ver = CORO_API_VERSION; 1559 coroapi.ver = CORO_API_VERSION;
1560 coroapi.rev = CORO_API_REVISION;
1529 coroapi.transfer = api_transfer; 1561 coroapi.transfer = api_transfer;
1530 1562
1531 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL)); 1563 assert (("PRIO_NORMAL must be 0", !PRIO_NORMAL));
1532} 1564}
1533 1565
1534SV * 1566SV *
1535new (char *klass, ...) 1567new (char *klass, ...)
1536 CODE: 1568 CODE:
1537{ 1569{
1538 struct coro *coro; 1570 struct coro *coro;
1571 MAGIC *mg;
1539 HV *hv; 1572 HV *hv;
1540 int i; 1573 int i;
1541 1574
1542 Newz (0, coro, 1, struct coro); 1575 Newz (0, coro, 1, struct coro);
1543 coro->args = newAV (); 1576 coro->args = newAV ();
1546 if (coro_first) coro_first->prev = coro; 1579 if (coro_first) coro_first->prev = coro;
1547 coro->next = coro_first; 1580 coro->next = coro_first;
1548 coro_first = coro; 1581 coro_first = coro;
1549 1582
1550 coro->hv = hv = newHV (); 1583 coro->hv = hv = newHV ();
1551 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;
1552 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1)); 1586 RETVAL = sv_bless (newRV_noinc ((SV *)hv), gv_stashpv (klass, 1));
1553 1587
1554 av_extend (coro->args, items - 1); 1588 av_extend (coro->args, items - 1);
1555 for (i = 1; i < items; i++) 1589 for (i = 1; i < items; i++)
1556 av_push (coro->args, newSVsv (ST (i))); 1590 av_push (coro->args, newSVsv (ST (i)));
1600 1634
1601 break; 1635 break;
1602 } 1636 }
1603 1637
1604 BARRIER; 1638 BARRIER;
1639 PUTBACK;
1605 TRANSFER (ta); 1640 TRANSFER (ta);
1606 1641 SPAGAIN; /* might be the sp of a different coroutine now */
1607 if (expect_false (GIMME_V != G_VOID && ta.next != ta.prev)) 1642 /* be extra careful not to ever do anything after TRANSFER */
1608 XSRETURN_YES;
1609} 1643}
1610 1644
1611bool 1645bool
1612_destroy (SV *coro_sv) 1646_destroy (SV *coro_sv)
1613 CODE: 1647 CODE:
1673 1707
1674 { 1708 {
1675 dSP; 1709 dSP;
1676 ENTER; 1710 ENTER;
1677 SAVETMPS; 1711 SAVETMPS;
1712 PUTBACK;
1713 PUSHSTACK;
1678 PUSHMARK (SP); 1714 PUSHMARK (SP);
1679 PUTBACK;
1680 1715
1681 if (ix) 1716 if (ix)
1682 eval_sv (coderef, 0); 1717 eval_sv (coderef, 0);
1683 else 1718 else
1684 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 1719 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
1685 1720
1721 POPSTACK;
1686 SPAGAIN; 1722 SPAGAIN;
1687 FREETMPS; 1723 FREETMPS;
1688 LEAVE; 1724 LEAVE;
1689 PUTBACK; 1725 PUTBACK;
1690 } 1726 }
1748 1784
1749BOOT: 1785BOOT:
1750{ 1786{
1751 int i; 1787 int i;
1752 1788
1753 sv_pool_rss = coro_get_sv ("Coro::POOL_RSS" , TRUE); 1789 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
1754 sv_pool_size = coro_get_sv ("Coro::POOL_SIZE" , TRUE); 1790 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
1755 av_async_pool = coro_get_av ("Coro::async_pool", TRUE); 1791 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
1756 1792
1757 coro_current = coro_get_sv ("Coro::current", FALSE); 1793 coro_current = coro_get_sv (aTHX_ "Coro::current", FALSE);
1758 SvREADONLY_on (coro_current); 1794 SvREADONLY_on (coro_current);
1759 1795
1760 coro_stash = gv_stashpv ("Coro", TRUE); 1796 coro_stash = gv_stashpv ("Coro", TRUE);
1761 1797
1762 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX)); 1798 newCONSTSUB (coro_stash, "PRIO_MAX", newSViv (PRIO_MAX));
1845 CODE: 1881 CODE:
1846{ 1882{
1847 struct coro *coro = SvSTATE (coro_current); 1883 struct coro *coro = SvSTATE (coro_current);
1848 HV *hv = (HV *)SvRV (coro_current); 1884 HV *hv = (HV *)SvRV (coro_current);
1849 AV *defav = GvAV (PL_defgv); 1885 AV *defav = GvAV (PL_defgv);
1850 SV *invoke = hv_delete (hv, strpair ("_invoke"), 0); 1886 SV *invoke = hv_delete (hv, "_invoke", sizeof ("_invoke") - 1, 0);
1851 AV *invoke_av; 1887 AV *invoke_av;
1852 int i, len; 1888 int i, len;
1853 1889
1854 if (!invoke) 1890 if (!invoke)
1855 croak ("\3async_pool terminate\2\n"); 1891 croak ("\3async_pool terminate\2\n");
1856 1892
1857 SvREFCNT_dec (coro->saved_deffh); 1893 SvREFCNT_dec (coro->saved_deffh);
1858 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv); 1894 coro->saved_deffh = SvREFCNT_inc ((SV *)PL_defoutgv);
1859 1895
1860 hv_store (hv, "desc", sizeof ("desc") - 1, 1896 hv_store (hv, "desc", sizeof ("desc") - 1,
1861 newSVpvn (strpair ("[async_pool]")), 0); 1897 newSVpvn ("[async_pool]", sizeof ("[async_pool]") - 1), 0);
1862 1898
1863 invoke_av = (AV *)SvRV (invoke); 1899 invoke_av = (AV *)SvRV (invoke);
1864 len = av_len (invoke_av); 1900 len = av_len (invoke_av);
1865 1901
1866 sv_setsv (cb, AvARRAY (invoke_av)[0]); 1902 sv_setsv (cb, AvARRAY (invoke_av)[0]);
1889 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss) 1925 if (coro_rss (aTHX_ coro) > SvIV (sv_pool_rss)
1890 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size)) 1926 || av_len (av_async_pool) + 1 >= SvIV (sv_pool_size))
1891 croak ("\3async_pool terminate\2\n"); 1927 croak ("\3async_pool terminate\2\n");
1892 1928
1893 av_clear (GvAV (PL_defgv)); 1929 av_clear (GvAV (PL_defgv));
1894 hv_store ((HV *)SvRV (coro_current), strpair ("desc"), 1930 hv_store ((HV *)SvRV (coro_current), "desc", sizeof ("desc") - 1,
1895 newSVpvn (strpair ("[async_pool idle]")), 0); 1931 newSVpvn ("[async_pool idle]", sizeof ("[async_pool idle]") - 1), 0);
1896 1932
1897 coro->prio = 0; 1933 coro->prio = 0;
1898 1934
1899 if (coro->cctx && (coro->cctx->flags & CC_TRACE)) 1935 if (coro->cctx && (coro->cctx->flags & CC_TRACE))
1900 api_trace (coro_current, 0); 1936 api_trace (coro_current, 0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines