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.326 by root, Mon Nov 24 04:56:38 2008 UTC vs.
Revision 1.346 by root, Tue Jun 9 18:56:45 2009 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
238enum { 238enum {
239 CF_RUNNING = 0x0001, /* coroutine is running */ 239 CF_RUNNING = 0x0001, /* coroutine is running */
240 CF_READY = 0x0002, /* coroutine is ready */ 240 CF_READY = 0x0002, /* coroutine is ready */
241 CF_NEW = 0x0004, /* has never been switched to */ 241 CF_NEW = 0x0004, /* has never been switched to */
242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */ 242 CF_DESTROYED = 0x0008, /* coroutine data has been freed */
243 CF_SUSPENDED = 0x0010, /* coroutine can't be scheduled */
243}; 244};
244 245
245/* the structure where most of the perl state is stored, overlaid on the cxstack */ 246/* the structure where most of the perl state is stored, overlaid on the cxstack */
246typedef struct 247typedef struct
247{ 248{
248 SV *defsv; 249 SV *defsv;
249 AV *defav; 250 AV *defav;
250 SV *errsv; 251 SV *errsv;
251 SV *irsgv; 252 SV *irsgv;
253 HV *hinthv;
252#define VAR(name,type) type name; 254#define VAR(name,type) type name;
253# include "state.h" 255# include "state.h"
254#undef VAR 256#undef VAR
255} perl_slots; 257} perl_slots;
256 258
283 285
284 /* async_pool */ 286 /* async_pool */
285 SV *saved_deffh; 287 SV *saved_deffh;
286 SV *invoke_cb; 288 SV *invoke_cb;
287 AV *invoke_av; 289 AV *invoke_av;
290
291 /* on_enter/on_leave */
292 AV *on_enter;
293 AV *on_leave;
288 294
289 /* linked list */ 295 /* linked list */
290 struct coro *next, *prev; 296 struct coro *next, *prev;
291}; 297};
292 298
351INLINE CV * 357INLINE CV *
352coro_sv_2cv (pTHX_ SV *sv) 358coro_sv_2cv (pTHX_ SV *sv)
353{ 359{
354 HV *st; 360 HV *st;
355 GV *gvp; 361 GV *gvp;
356 return sv_2cv (sv, &st, &gvp, 0); 362 CV *cv = sv_2cv (sv, &st, &gvp, 0);
363
364 if (!cv)
365 croak ("code reference expected");
366
367 return cv;
357} 368}
369
370/*****************************************************************************/
371/* magic glue */
372
373#define CORO_MAGIC_type_cv 26
374#define CORO_MAGIC_type_state PERL_MAGIC_ext
375
376#define CORO_MAGIC_NN(sv, type) \
377 (expect_true (SvMAGIC (sv)->mg_type == type) \
378 ? SvMAGIC (sv) \
379 : mg_find (sv, type))
380
381#define CORO_MAGIC(sv, type) \
382 (expect_true (SvMAGIC (sv)) \
383 ? CORO_MAGIC_NN (sv, type) \
384 : 0)
385
386#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
387#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
388
389INLINE struct coro *
390SvSTATE_ (pTHX_ SV *coro)
391{
392 HV *stash;
393 MAGIC *mg;
394
395 if (SvROK (coro))
396 coro = SvRV (coro);
397
398 if (expect_false (SvTYPE (coro) != SVt_PVHV))
399 croak ("Coro::State object required");
400
401 stash = SvSTASH (coro);
402 if (expect_false (stash != coro_stash && stash != coro_state_stash))
403 {
404 /* very slow, but rare, check */
405 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
406 croak ("Coro::State object required");
407 }
408
409 mg = CORO_MAGIC_state (coro);
410 return (struct coro *)mg->mg_ptr;
411}
412
413#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
414
415/* faster than SvSTATE, but expects a coroutine hv */
416#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
417#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
418
419/*****************************************************************************/
420/* padlist management and caching */
358 421
359static AV * 422static AV *
360coro_derive_padlist (pTHX_ CV *cv) 423coro_derive_padlist (pTHX_ CV *cv)
361{ 424{
362 AV *padlist = CvPADLIST (cv); 425 AV *padlist = CvPADLIST (cv);
370 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1); 433 Perl_pad_push (aTHX_ padlist, AvFILLp (padlist) + 1, 1);
371#endif 434#endif
372 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)]; 435 newpad = (AV *)AvARRAY (padlist)[AvFILLp (padlist)];
373 --AvFILLp (padlist); 436 --AvFILLp (padlist);
374 437
375 av_store (newpadlist, 0, SvREFCNT_inc_NN (*av_fetch (padlist, 0, FALSE))); 438 av_store (newpadlist, 0, SvREFCNT_inc_NN (AvARRAY (padlist)[0]));
376 av_store (newpadlist, 1, (SV *)newpad); 439 av_store (newpadlist, 1, (SV *)newpad);
377 440
378 return newpadlist; 441 return newpadlist;
379} 442}
380 443
381static void 444static void
382free_padlist (pTHX_ AV *padlist) 445free_padlist (pTHX_ AV *padlist)
383{ 446{
384 /* may be during global destruction */ 447 /* may be during global destruction */
385 if (SvREFCNT (padlist)) 448 if (!IN_DESTRUCT)
386 { 449 {
387 I32 i = AvFILLp (padlist); 450 I32 i = AvFILLp (padlist);
388 while (i >= 0) 451
452 while (i > 0) /* special-case index 0 */
389 { 453 {
390 SV **svp = av_fetch (padlist, i--, FALSE); 454 /* we try to be extra-careful here */
391 if (svp) 455 AV *av = (AV *)AvARRAY (padlist)[i--];
392 { 456 I32 j = AvFILLp (av);
393 SV *sv; 457
394 while (&PL_sv_undef != (sv = av_pop ((AV *)*svp))) 458 while (j >= 0)
459 SvREFCNT_dec (AvARRAY (av)[j--]);
460
461 AvFILLp (av) = -1;
395 SvREFCNT_dec (sv); 462 SvREFCNT_dec (av);
396
397 SvREFCNT_dec (*svp);
398 }
399 } 463 }
400 464
465 SvREFCNT_dec (AvARRAY (padlist)[0]);
466
467 AvFILLp (padlist) = -1;
401 SvREFCNT_dec ((SV*)padlist); 468 SvREFCNT_dec ((SV*)padlist);
402 } 469 }
403} 470}
404 471
405static int 472static int
414 481
415 SvREFCNT_dec (av); /* sv_magicext increased the refcount */ 482 SvREFCNT_dec (av); /* sv_magicext increased the refcount */
416 483
417 return 0; 484 return 0;
418} 485}
419
420#define CORO_MAGIC_type_cv 26
421#define CORO_MAGIC_type_state PERL_MAGIC_ext
422 486
423static MGVTBL coro_cv_vtbl = { 487static MGVTBL coro_cv_vtbl = {
424 0, 0, 0, 0, 488 0, 0, 0, 0,
425 coro_cv_free 489 coro_cv_free
426}; 490};
427
428#define CORO_MAGIC_NN(sv, type) \
429 (expect_true (SvMAGIC (sv)->mg_type == type) \
430 ? SvMAGIC (sv) \
431 : mg_find (sv, type))
432
433#define CORO_MAGIC(sv, type) \
434 (expect_true (SvMAGIC (sv)) \
435 ? CORO_MAGIC_NN (sv, type) \
436 : 0)
437
438#define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv)
439#define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state)
440
441INLINE struct coro *
442SvSTATE_ (pTHX_ SV *coro)
443{
444 HV *stash;
445 MAGIC *mg;
446
447 if (SvROK (coro))
448 coro = SvRV (coro);
449
450 if (expect_false (SvTYPE (coro) != SVt_PVHV))
451 croak ("Coro::State object required");
452
453 stash = SvSTASH (coro);
454 if (expect_false (stash != coro_stash && stash != coro_state_stash))
455 {
456 /* very slow, but rare, check */
457 if (!sv_derived_from (sv_2mortal (newRV_inc (coro)), "Coro::State"))
458 croak ("Coro::State object required");
459 }
460
461 mg = CORO_MAGIC_state (coro);
462 return (struct coro *)mg->mg_ptr;
463}
464
465#define SvSTATE(sv) SvSTATE_ (aTHX_ (sv))
466
467/* faster than SvSTATE, but expects a coroutine hv */
468#define SvSTATE_hv(hv) ((struct coro *)CORO_MAGIC_NN ((SV *)hv, CORO_MAGIC_type_state)->mg_ptr)
469#define SvSTATE_current SvSTATE_hv (SvRV (coro_current))
470 491
471/* the next two functions merely cache the padlists */ 492/* the next two functions merely cache the padlists */
472static void 493static void
473get_padlist (pTHX_ CV *cv) 494get_padlist (pTHX_ CV *cv)
474{ 495{
502 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); 523 mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0);
503 524
504 av = (AV *)mg->mg_obj; 525 av = (AV *)mg->mg_obj;
505 526
506 if (expect_false (AvFILLp (av) >= AvMAX (av))) 527 if (expect_false (AvFILLp (av) >= AvMAX (av)))
507 av_extend (av, AvMAX (av) + 1); 528 av_extend (av, AvFILLp (av) + 1);
508 529
509 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); 530 AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv);
510} 531}
511 532
512/** load & save, init *******************************************************/ 533/** load & save, init *******************************************************/
534
535static void
536on_enterleave_call (pTHX_ SV *cb);
513 537
514static void 538static void
515load_perl (pTHX_ Coro__State c) 539load_perl (pTHX_ Coro__State c)
516{ 540{
517 perl_slots *slot = c->slot; 541 perl_slots *slot = c->slot;
518 c->slot = 0; 542 c->slot = 0;
519 543
520 PL_mainstack = c->mainstack; 544 PL_mainstack = c->mainstack;
521 545
522 GvSV (PL_defgv) = slot->defsv; 546 GvSV (PL_defgv) = slot->defsv;
523 GvAV (PL_defgv) = slot->defav; 547 GvAV (PL_defgv) = slot->defav;
524 GvSV (PL_errgv) = slot->errsv; 548 GvSV (PL_errgv) = slot->errsv;
525 GvSV (irsgv) = slot->irsgv; 549 GvSV (irsgv) = slot->irsgv;
550 GvHV (PL_hintgv) = slot->hinthv;
526 551
527 #define VAR(name,type) PL_ ## name = slot->name; 552 #define VAR(name,type) PL_ ## name = slot->name;
528 # include "state.h" 553 # include "state.h"
529 #undef VAR 554 #undef VAR
530 555
544 PUTBACK; 569 PUTBACK;
545 } 570 }
546 571
547 slf_frame = c->slf_frame; 572 slf_frame = c->slf_frame;
548 CORO_THROW = c->except; 573 CORO_THROW = c->except;
574
575 if (expect_false (c->on_enter))
576 {
577 int i;
578
579 for (i = 0; i <= AvFILLp (c->on_enter); ++i)
580 on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]);
581 }
549} 582}
550 583
551static void 584static void
552save_perl (pTHX_ Coro__State c) 585save_perl (pTHX_ Coro__State c)
553{ 586{
587 if (expect_false (c->on_leave))
588 {
589 int i;
590
591 for (i = AvFILLp (c->on_leave); i >= 0; --i)
592 on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]);
593 }
594
554 c->except = CORO_THROW; 595 c->except = CORO_THROW;
555 c->slf_frame = slf_frame; 596 c->slf_frame = slf_frame;
556 597
557 { 598 {
558 dSP; 599 dSP;
616 c->mainstack = PL_mainstack; 657 c->mainstack = PL_mainstack;
617 658
618 { 659 {
619 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1); 660 perl_slots *slot = c->slot = (perl_slots *)(cxstack + cxstack_ix + 1);
620 661
621 slot->defav = GvAV (PL_defgv); 662 slot->defav = GvAV (PL_defgv);
622 slot->defsv = DEFSV; 663 slot->defsv = DEFSV;
623 slot->errsv = ERRSV; 664 slot->errsv = ERRSV;
624 slot->irsgv = GvSV (irsgv); 665 slot->irsgv = GvSV (irsgv);
666 slot->hinthv = GvHV (PL_hintgv);
625 667
626 #define VAR(name,type) slot->name = PL_ ## name; 668 #define VAR(name,type) slot->name = PL_ ## name;
627 # include "state.h" 669 # include "state.h"
628 #undef VAR 670 #undef VAR
629 } 671 }
756#endif 798#endif
757 799
758/* 800/*
759 * This overrides the default magic get method of %SIG elements. 801 * This overrides the default magic get method of %SIG elements.
760 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook 802 * The original one doesn't provide for reading back of PL_diehook/PL_warnhook
761 * and instead of tryign to save and restore the hash elements, we just provide 803 * and instead of trying to save and restore the hash elements, we just provide
762 * readback here. 804 * readback here.
763 * We only do this when the hook is != 0, as they are often set to 0 temporarily,
764 * not expecting this to actually change the hook. This is a potential problem
765 * when a schedule happens then, but we ignore this.
766 */ 805 */
767static int 806static int
768coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) 807coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg)
769{ 808{
770 const char *s = MgPV_nolen_const (mg); 809 const char *s = MgPV_nolen_const (mg);
823 if (strEQ (s, "__WARN__")) svp = &PL_warnhook; 862 if (strEQ (s, "__WARN__")) svp = &PL_warnhook;
824 863
825 if (svp) 864 if (svp)
826 { 865 {
827 SV *old = *svp; 866 SV *old = *svp;
828 *svp = newSVsv (sv); 867 *svp = SvOK (sv) ? newSVsv (sv) : 0;
829 SvREFCNT_dec (old); 868 SvREFCNT_dec (old);
830 return 0; 869 return 0;
831 } 870 }
832 } 871 }
833 872
865 904
866 PL_runops = RUNOPS_DEFAULT; 905 PL_runops = RUNOPS_DEFAULT;
867 PL_curcop = &PL_compiling; 906 PL_curcop = &PL_compiling;
868 PL_in_eval = EVAL_NULL; 907 PL_in_eval = EVAL_NULL;
869 PL_comppad = 0; 908 PL_comppad = 0;
909 PL_comppad_name = 0;
910 PL_comppad_name_fill = 0;
911 PL_comppad_name_floor = 0;
870 PL_curpm = 0; 912 PL_curpm = 0;
871 PL_curpad = 0; 913 PL_curpad = 0;
872 PL_localizing = 0; 914 PL_localizing = 0;
873 PL_dirty = 0; 915 PL_dirty = 0;
874 PL_restartop = 0; 916 PL_restartop = 0;
875#if PERL_VERSION_ATLEAST (5,10,0) 917#if PERL_VERSION_ATLEAST (5,10,0)
876 PL_parser = 0; 918 PL_parser = 0;
877#endif 919#endif
920 PL_hints = 0;
878 921
879 /* recreate the die/warn hooks */ 922 /* recreate the die/warn hooks */
880 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook ); 923 PL_diehook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__DIE__" , sizeof ("__DIE__" ) - 1, 1), rv_diehook );
881 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook); 924 PL_warnhook = 0; SvSetMagicSV (*hv_fetch (hv_sig, "__WARN__", sizeof ("__WARN__") - 1, 1), rv_warnhook);
882 925
883 GvSV (PL_defgv) = newSV (0); 926 GvSV (PL_defgv) = newSV (0);
884 GvAV (PL_defgv) = coro->args; coro->args = 0; 927 GvAV (PL_defgv) = coro->args; coro->args = 0;
885 GvSV (PL_errgv) = newSV (0); 928 GvSV (PL_errgv) = newSV (0);
886 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0); 929 GvSV (irsgv) = newSVpvn ("\n", 1); sv_magic (GvSV (irsgv), (SV *)irsgv, PERL_MAGIC_sv, "/", 0);
930 GvHV (PL_hintgv) = 0;
887 PL_rs = newSVsv (GvSV (irsgv)); 931 PL_rs = newSVsv (GvSV (irsgv));
888 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv); 932 PL_defoutgv = (GV *)SvREFCNT_inc_NN (stdoutgv);
889 933
890 { 934 {
891 dSP; 935 dSP;
920 /* copy throw, in case it was set before coro_setup */ 964 /* copy throw, in case it was set before coro_setup */
921 CORO_THROW = coro->except; 965 CORO_THROW = coro->except;
922} 966}
923 967
924static void 968static void
925coro_destruct (pTHX_ struct coro *coro) 969coro_unwind_stacks (pTHX)
926{ 970{
927 if (!IN_DESTRUCT) 971 if (!IN_DESTRUCT)
928 { 972 {
929 /* restore all saved variables and stuff */ 973 /* restore all saved variables and stuff */
930 LEAVE_SCOPE (0); 974 LEAVE_SCOPE (0);
938 POPSTACK_TO (PL_mainstack); 982 POPSTACK_TO (PL_mainstack);
939 983
940 /* unwind main stack */ 984 /* unwind main stack */
941 dounwind (-1); 985 dounwind (-1);
942 } 986 }
987}
988
989static void
990coro_destruct_perl (pTHX_ struct coro *coro)
991{
992 coro_unwind_stacks (aTHX);
943 993
944 SvREFCNT_dec (GvSV (PL_defgv)); 994 SvREFCNT_dec (GvSV (PL_defgv));
945 SvREFCNT_dec (GvAV (PL_defgv)); 995 SvREFCNT_dec (GvAV (PL_defgv));
946 SvREFCNT_dec (GvSV (PL_errgv)); 996 SvREFCNT_dec (GvSV (PL_errgv));
947 SvREFCNT_dec (PL_defoutgv); 997 SvREFCNT_dec (PL_defoutgv);
948 SvREFCNT_dec (PL_rs); 998 SvREFCNT_dec (PL_rs);
949 SvREFCNT_dec (GvSV (irsgv)); 999 SvREFCNT_dec (GvSV (irsgv));
1000 SvREFCNT_dec (GvHV (PL_hintgv));
950 1001
951 SvREFCNT_dec (PL_diehook); 1002 SvREFCNT_dec (PL_diehook);
952 SvREFCNT_dec (PL_warnhook); 1003 SvREFCNT_dec (PL_warnhook);
953 1004
954 SvREFCNT_dec (coro->saved_deffh); 1005 SvREFCNT_dec (coro->saved_deffh);
1169 /* 1220 /*
1170 * If perl-run returns we assume exit() was being called or the coro 1221 * If perl-run returns we assume exit() was being called or the coro
1171 * fell off the end, which seems to be the only valid (non-bug) 1222 * fell off the end, which seems to be the only valid (non-bug)
1172 * reason for perl_run to return. We try to exit by jumping to the 1223 * reason for perl_run to return. We try to exit by jumping to the
1173 * bootstrap-time "top" top_env, as we cannot restore the "main" 1224 * bootstrap-time "top" top_env, as we cannot restore the "main"
1174 * coroutine as Coro has no such concept 1225 * coroutine as Coro has no such concept.
1226 * This actually isn't valid with the pthread backend, but OSes requiring
1227 * that backend are too broken to do it in a standards-compliant way.
1175 */ 1228 */
1176 PL_top_env = main_top_env; 1229 PL_top_env = main_top_env;
1177 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */ 1230 JMPENV_JUMP (2); /* I do not feel well about the hardcoded 2 at all */
1178 } 1231 }
1179} 1232}
1256cctx_destroy (coro_cctx *cctx) 1309cctx_destroy (coro_cctx *cctx)
1257{ 1310{
1258 if (!cctx) 1311 if (!cctx)
1259 return; 1312 return;
1260 1313
1261 assert (cctx != cctx_current);//D temporary 1314 assert (("FATAL: tried to destroy current cctx", cctx != cctx_current));//D temporary?
1262 1315
1263 --cctx_count; 1316 --cctx_count;
1264 coro_destroy (&cctx->cctx); 1317 coro_destroy (&cctx->cctx);
1265 1318
1266 /* coro_transfer creates new, empty cctx's */ 1319 /* coro_transfer creates new, empty cctx's */
1330 /* TODO: throwing up here is considered harmful */ 1383 /* TODO: throwing up here is considered harmful */
1331 1384
1332 if (expect_true (prev != next)) 1385 if (expect_true (prev != next))
1333 { 1386 {
1334 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) 1387 if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW))))
1335 croak ("Coro::State::transfer called with a suspended prev Coro::State, but can only transfer from running or new states,"); 1388 croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,");
1336 1389
1337 if (expect_false (next->flags & CF_RUNNING))
1338 croak ("Coro::State::transfer called with running next Coro::State, but can only transfer to inactive states,");
1339
1340 if (expect_false (next->flags & CF_DESTROYED)) 1390 if (expect_false (next->flags & (CF_RUNNING | CF_DESTROYED | CF_SUSPENDED)))
1341 croak ("Coro::State::transfer called with destroyed next Coro::State, but can only transfer to inactive states,"); 1391 croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,");
1342 1392
1343#if !PERL_VERSION_ATLEAST (5,10,0) 1393#if !PERL_VERSION_ATLEAST (5,10,0)
1344 if (expect_false (PL_lex_state != LEX_NOTPARSING)) 1394 if (expect_false (PL_lex_state != LEX_NOTPARSING))
1345 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); 1395 croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,");
1346#endif 1396#endif
1384 coro_setup (aTHX_ next); 1434 coro_setup (aTHX_ next);
1385 } 1435 }
1386 else 1436 else
1387 load_perl (aTHX_ next); 1437 load_perl (aTHX_ next);
1388 1438
1389 assert (!prev->cctx);//D temporary
1390
1391 /* possibly untie and reuse the cctx */ 1439 /* possibly untie and reuse the cctx */
1392 if (expect_true ( 1440 if (expect_true (
1393 cctx_current->idle_sp == STACKLEVEL 1441 cctx_current->idle_sp == STACKLEVEL
1394 && !(cctx_current->flags & CC_TRACE) 1442 && !(cctx_current->flags & CC_TRACE)
1395 && !force_cctx 1443 && !force_cctx
1436coro_state_destroy (pTHX_ struct coro *coro) 1484coro_state_destroy (pTHX_ struct coro *coro)
1437{ 1485{
1438 if (coro->flags & CF_DESTROYED) 1486 if (coro->flags & CF_DESTROYED)
1439 return 0; 1487 return 0;
1440 1488
1441 if (coro->on_destroy) 1489 if (coro->on_destroy && !PL_dirty)
1442 coro->on_destroy (aTHX_ coro); 1490 coro->on_destroy (aTHX_ coro);
1443 1491
1444 coro->flags |= CF_DESTROYED; 1492 coro->flags |= CF_DESTROYED;
1445 1493
1446 if (coro->flags & CF_READY) 1494 if (coro->flags & CF_READY)
1450 --coro_nready; 1498 --coro_nready;
1451 } 1499 }
1452 else 1500 else
1453 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */ 1501 coro->flags |= CF_READY; /* make sure it is NOT put into the readyqueue */
1454 1502
1455 if (coro->mainstack && coro->mainstack != main_mainstack) 1503 if (coro->mainstack
1504 && coro->mainstack != main_mainstack
1505 && coro->slot
1506 && !PL_dirty)
1456 { 1507 {
1457 struct coro temp; 1508 struct coro *current = SvSTATE_current;
1458 1509
1459 assert (("FATAL: tried to destroy currently running coroutine (please report)", !(coro->flags & CF_RUNNING))); 1510 assert (("FATAL: tried to destroy currently running coroutine", coro->mainstack != PL_mainstack));
1460 1511
1461 save_perl (aTHX_ &temp); 1512 save_perl (aTHX_ current);
1462 load_perl (aTHX_ coro); 1513 load_perl (aTHX_ coro);
1463 1514
1464 coro_destruct (aTHX_ coro); 1515 coro_destruct_perl (aTHX_ coro);
1465 1516
1466 load_perl (aTHX_ &temp); 1517 load_perl (aTHX_ current);
1467 1518
1468 coro->slot = 0; 1519 coro->slot = 0;
1469 } 1520 }
1470 1521
1471 cctx_destroy (coro->cctx); 1522 cctx_destroy (coro->cctx);
1652 if (expect_true (next_sv)) 1703 if (expect_true (next_sv))
1653 { 1704 {
1654 struct coro *next = SvSTATE_hv (next_sv); 1705 struct coro *next = SvSTATE_hv (next_sv);
1655 1706
1656 /* cannot transfer to destroyed coros, skip and look for next */ 1707 /* cannot transfer to destroyed coros, skip and look for next */
1657 if (expect_false (next->flags & CF_DESTROYED)) 1708 if (expect_false (next->flags & (CF_DESTROYED | CF_SUSPENDED)))
1658 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */ 1709 SvREFCNT_dec (next_sv); /* coro_nready has already been taken care of by destroy */
1659 else 1710 else
1660 { 1711 {
1661 next->flags &= ~CF_READY; 1712 next->flags &= ~CF_READY;
1662 --coro_nready; 1713 --coro_nready;
1670 /* nothing to schedule: call the idle handler */ 1721 /* nothing to schedule: call the idle handler */
1671 if (SvROK (sv_idle) 1722 if (SvROK (sv_idle)
1672 && SvOBJECT (SvRV (sv_idle))) 1723 && SvOBJECT (SvRV (sv_idle)))
1673 { 1724 {
1674 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */ 1725 ++coro_nready; /* hack so that api_ready doesn't invoke ready hook */
1675 api_ready (SvRV (sv_idle)); 1726 api_ready (aTHX_ SvRV (sv_idle));
1676 --coro_nready; 1727 --coro_nready;
1677 } 1728 }
1678 else 1729 else
1679 { 1730 {
1680 dSP; 1731 dSP;
1841 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */ 1892 av_push (av_destroy, (SV *)newRV_inc ((SV *)hv)); /* RVinc for perl */
1842 api_ready (aTHX_ sv_manager); 1893 api_ready (aTHX_ sv_manager);
1843 1894
1844 frame->prepare = prepare_schedule; 1895 frame->prepare = prepare_schedule;
1845 frame->check = slf_check_repeat; 1896 frame->check = slf_check_repeat;
1897
1898 /* as a minor optimisation, we could unwind all stacks here */
1899 /* but that puts extra pressure on pp_slf, and is not worth much */
1900 /*coro_unwind_stacks (aTHX);*/
1846} 1901}
1847 1902
1848/*****************************************************************************/ 1903/*****************************************************************************/
1849/* async pool handler */ 1904/* async pool handler */
1850 1905
1933 SV *data = (SV *)GENSUB_ARG; 1988 SV *data = (SV *)GENSUB_ARG;
1934 1989
1935 if (SvTYPE (SvRV (data)) != SVt_PVAV) 1990 if (SvTYPE (SvRV (data)) != SVt_PVAV)
1936 { 1991 {
1937 /* first call, set args */ 1992 /* first call, set args */
1993 SV *coro = SvRV (data);
1938 AV *av = newAV (); 1994 AV *av = newAV ();
1939 SV *coro = SvRV (data);
1940 1995
1941 SvRV_set (data, (SV *)av); 1996 SvRV_set (data, (SV *)av);
1942 api_ready (aTHX_ coro);
1943 SvREFCNT_dec (coro);
1944 1997
1945 /* better take a full copy of the arguments */ 1998 /* better take a full copy of the arguments */
1946 while (items--) 1999 while (items--)
1947 av_store (av, items, newSVsv (ST (items))); 2000 av_store (av, items, newSVsv (ST (items)));
2001
2002 api_ready (aTHX_ coro);
2003 SvREFCNT_dec (coro);
1948 } 2004 }
1949 2005
1950 XSRETURN_EMPTY; 2006 XSRETURN_EMPTY;
1951} 2007}
1952 2008
1969 2025
1970 EXTEND (SP, AvFILLp (av) + 1); 2026 EXTEND (SP, AvFILLp (av) + 1);
1971 for (i = 0; i <= AvFILLp (av); ++i) 2027 for (i = 0; i <= AvFILLp (av); ++i)
1972 PUSHs (sv_2mortal (AvARRAY (av)[i])); 2028 PUSHs (sv_2mortal (AvARRAY (av)[i]));
1973 2029
1974 /* we have stolen the elements, so ste length to zero and free */ 2030 /* we have stolen the elements, so set length to zero and free */
1975 AvFILLp (av) = -1; 2031 AvFILLp (av) = -1;
1976 av_undef (av); 2032 av_undef (av);
1977 2033
1978 PUTBACK; 2034 PUTBACK;
1979 } 2035 }
2269 2325
2270 PL_op->op_ppaddr = pp_slf; 2326 PL_op->op_ppaddr = pp_slf;
2271 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */ 2327 /*PL_op->op_type = OP_CUSTOM; /* we do behave like entersub still */
2272 2328
2273 PL_op = (OP *)&slf_restore; 2329 PL_op = (OP *)&slf_restore;
2330}
2331
2332/*****************************************************************************/
2333/* dynamic wind */
2334
2335static void
2336on_enterleave_call (pTHX_ SV *cb)
2337{
2338 dSP;
2339
2340 PUSHSTACK;
2341
2342 PUSHMARK (SP);
2343 PUTBACK;
2344 call_sv (cb, G_VOID | G_DISCARD);
2345 SPAGAIN;
2346
2347 POPSTACK;
2348}
2349
2350static SV *
2351coro_avp_pop_and_free (pTHX_ AV **avp)
2352{
2353 AV *av = *avp;
2354 SV *res = av_pop (av);
2355
2356 if (AvFILLp (av) < 0)
2357 {
2358 *avp = 0;
2359 SvREFCNT_dec (av);
2360 }
2361
2362 return res;
2363}
2364
2365static void
2366coro_pop_on_enter (pTHX_ void *coro)
2367{
2368 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_enter);
2369 SvREFCNT_dec (cb);
2370}
2371
2372static void
2373coro_pop_on_leave (pTHX_ void *coro)
2374{
2375 SV *cb = coro_avp_pop_and_free (aTHX_ &((struct coro *)coro)->on_leave);
2376 on_enterleave_call (aTHX_ sv_2mortal (cb));
2274} 2377}
2275 2378
2276/*****************************************************************************/ 2379/*****************************************************************************/
2277/* PerlIO::cede */ 2380/* PerlIO::cede */
2278 2381
2359 SV **ary; 2462 SV **ary;
2360 2463
2361 /* unfortunately, building manually saves memory */ 2464 /* unfortunately, building manually saves memory */
2362 Newx (ary, 2, SV *); 2465 Newx (ary, 2, SV *);
2363 AvALLOC (av) = ary; 2466 AvALLOC (av) = ary;
2467#if PERL_VERSION_ATLEAST (5,10,0)
2364 /*AvARRAY (av) = ary;*/ 2468 AvARRAY (av) = ary;
2469#else
2365 SvPVX ((SV *)av) = (char *)ary; /* 5.8.8 needs this syntax instead of AvARRAY = ary */ 2470 /* 5.8.8 needs this syntax instead of AvARRAY = ary, yet */
2471 /* -DDEBUGGING flags this as a bug, despite it perfectly working */
2472 SvPVX ((SV *)av) = (char *)ary;
2473#endif
2366 AvMAX (av) = 1; 2474 AvMAX (av) = 1;
2367 AvFILLp (av) = 0; 2475 AvFILLp (av) = 0;
2368 ary [0] = newSViv (count); 2476 ary [0] = newSViv (count);
2369 2477
2370 return newRV_noinc ((SV *)av); 2478 return newRV_noinc ((SV *)av);
2899SV * 3007SV *
2900clone (Coro::State coro) 3008clone (Coro::State coro)
2901 CODE: 3009 CODE:
2902{ 3010{
2903#if CORO_CLONE 3011#if CORO_CLONE
2904 struct coro *ncoro = coro_clone (coro); 3012 struct coro *ncoro = coro_clone (aTHX_ coro);
2905 MAGIC *mg; 3013 MAGIC *mg;
2906 /* TODO: too much duplication */ 3014 /* TODO: too much duplication */
2907 ncoro->hv = newHV (); 3015 ncoro->hv = newHV ();
2908 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0); 3016 mg = sv_magicext ((SV *)ncoro->hv, 0, CORO_MAGIC_type_state, &coro_state_vtbl, (char *)ncoro, 0);
2909 mg->mg_flags |= MGf_DUP; 3017 mg->mg_flags |= MGf_DUP;
2971 eval = 1 3079 eval = 1
2972 CODE: 3080 CODE:
2973{ 3081{
2974 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot)) 3082 if (coro->mainstack && ((coro->flags & CF_RUNNING) || coro->slot))
2975 { 3083 {
2976 struct coro temp; 3084 struct coro *current = SvSTATE_current;
2977 3085
2978 if (!(coro->flags & CF_RUNNING)) 3086 if (current != coro)
2979 { 3087 {
2980 PUTBACK; 3088 PUTBACK;
2981 save_perl (aTHX_ &temp); 3089 save_perl (aTHX_ current);
2982 load_perl (aTHX_ coro); 3090 load_perl (aTHX_ coro);
3091 SPAGAIN;
2983 } 3092 }
2984 3093
2985 {
2986 dSP;
2987 ENTER;
2988 SAVETMPS;
2989 PUTBACK;
2990 PUSHSTACK; 3094 PUSHSTACK;
3095
2991 PUSHMARK (SP); 3096 PUSHMARK (SP);
3097 PUTBACK;
2992 3098
2993 if (ix) 3099 if (ix)
2994 eval_sv (coderef, 0); 3100 eval_sv (coderef, 0);
2995 else 3101 else
2996 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD); 3102 call_sv (coderef, G_KEEPERR | G_EVAL | G_VOID | G_DISCARD);
2997 3103
2998 POPSTACK; 3104 POPSTACK;
2999 SPAGAIN; 3105 SPAGAIN;
3000 FREETMPS;
3001 LEAVE;
3002 PUTBACK;
3003 }
3004 3106
3005 if (!(coro->flags & CF_RUNNING)) 3107 if (current != coro)
3006 { 3108 {
3109 PUTBACK;
3007 save_perl (aTHX_ coro); 3110 save_perl (aTHX_ coro);
3008 load_perl (aTHX_ &temp); 3111 load_perl (aTHX_ current);
3009 SPAGAIN; 3112 SPAGAIN;
3010 } 3113 }
3011 } 3114 }
3012} 3115}
3013 3116
3017 ALIAS: 3120 ALIAS:
3018 is_ready = CF_READY 3121 is_ready = CF_READY
3019 is_running = CF_RUNNING 3122 is_running = CF_RUNNING
3020 is_new = CF_NEW 3123 is_new = CF_NEW
3021 is_destroyed = CF_DESTROYED 3124 is_destroyed = CF_DESTROYED
3125 is_suspended = CF_SUSPENDED
3022 CODE: 3126 CODE:
3023 RETVAL = boolSV (coro->flags & ix); 3127 RETVAL = boolSV (coro->flags & ix);
3024 OUTPUT: 3128 OUTPUT:
3025 RETVAL 3129 RETVAL
3026 3130
3091 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv; 3195 SV **dst = ix ? (SV **)&self->slot->defav : (SV **)&self->slot->defsv;
3092 3196
3093 SV *tmp = *src; *src = *dst; *dst = tmp; 3197 SV *tmp = *src; *src = *dst; *dst = tmp;
3094 } 3198 }
3095 3199
3200void
3201cancel (Coro::State self)
3202 CODE:
3203 coro_state_destroy (aTHX_ self);
3204 coro_call_on_destroy (aTHX_ self); /* actually only for Coro objects */
3205
3096 3206
3097MODULE = Coro::State PACKAGE = Coro 3207MODULE = Coro::State PACKAGE = Coro
3098 3208
3099BOOT: 3209BOOT:
3100{ 3210{
3101 int i; 3211 int i;
3102 3212
3103 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE); 3213 sv_pool_rss = coro_get_sv (aTHX_ "Coro::POOL_RSS" , TRUE);
3104 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE); 3214 sv_pool_size = coro_get_sv (aTHX_ "Coro::POOL_SIZE" , TRUE);
3105 cv_coro_run = get_cv ( "Coro::_terminate", GV_ADD); 3215 cv_coro_run = get_cv ( "Coro::_coro_run" , GV_ADD);
3106 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD); 3216 cv_coro_terminate = get_cv ( "Coro::terminate" , GV_ADD);
3107 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current); 3217 coro_current = coro_get_sv (aTHX_ "Coro::current" , FALSE); SvREADONLY_on (coro_current);
3108 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE); 3218 av_async_pool = coro_get_av (aTHX_ "Coro::async_pool", TRUE);
3109 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE); 3219 av_destroy = coro_get_av (aTHX_ "Coro::destroy" , TRUE);
3110 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE); 3220 sv_manager = coro_get_sv (aTHX_ "Coro::manager" , TRUE);
3174cede_notself (...) 3284cede_notself (...)
3175 CODE: 3285 CODE:
3176 CORO_EXECUTE_SLF_XS (slf_init_cede_notself); 3286 CORO_EXECUTE_SLF_XS (slf_init_cede_notself);
3177 3287
3178void 3288void
3179_cancel (Coro::State self)
3180 CODE:
3181 coro_state_destroy (aTHX_ self);
3182 coro_call_on_destroy (aTHX_ self);
3183
3184void
3185_set_current (SV *current) 3289_set_current (SV *current)
3186 PROTOTYPE: $ 3290 PROTOTYPE: $
3187 CODE: 3291 CODE:
3188 SvREFCNT_dec (SvRV (coro_current)); 3292 SvREFCNT_dec (SvRV (coro_current));
3189 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current))); 3293 SvRV_set (coro_current, SvREFCNT_inc_NN (SvRV (current)));
3231 PROTOTYPE: 3335 PROTOTYPE:
3232 CODE: 3336 CODE:
3233 RETVAL = coro_nready; 3337 RETVAL = coro_nready;
3234 OUTPUT: 3338 OUTPUT:
3235 RETVAL 3339 RETVAL
3340
3341void
3342suspend (Coro::State self)
3343 PROTOTYPE: $
3344 CODE:
3345 self->flags |= CF_SUSPENDED;
3346
3347void
3348resume (Coro::State self)
3349 PROTOTYPE: $
3350 CODE:
3351 self->flags &= ~CF_SUSPENDED;
3236 3352
3237void 3353void
3238_pool_handler (...) 3354_pool_handler (...)
3239 CODE: 3355 CODE:
3240 CORO_EXECUTE_SLF_XS (slf_init_pool_handler); 3356 CORO_EXECUTE_SLF_XS (slf_init_pool_handler);
3295rouse_wait (...) 3411rouse_wait (...)
3296 PROTOTYPE: ;$ 3412 PROTOTYPE: ;$
3297 PPCODE: 3413 PPCODE:
3298 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait); 3414 CORO_EXECUTE_SLF_XS (slf_init_rouse_wait);
3299 3415
3416void
3417on_enter (SV *block)
3418 ALIAS:
3419 on_leave = 1
3420 PROTOTYPE: &
3421 CODE:
3422{
3423 struct coro *coro = SvSTATE_current;
3424 AV **avp = ix ? &coro->on_leave : &coro->on_enter;
3425
3426 block = (SV *)coro_sv_2cv (aTHX_ block);
3427
3428 if (!*avp)
3429 *avp = newAV ();
3430
3431 av_push (*avp, SvREFCNT_inc (block));
3432
3433 if (!ix)
3434 on_enterleave_call (aTHX_ block);
3435
3436 LEAVE; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3437 SAVEDESTRUCTOR_X (ix ? coro_pop_on_leave : coro_pop_on_enter, (void *)coro);
3438 ENTER; /* pp_entersub unfortunately forces an ENTER/LEAVE around XS calls */
3439}
3440
3300 3441
3301MODULE = Coro::State PACKAGE = PerlIO::cede 3442MODULE = Coro::State PACKAGE = PerlIO::cede
3302 3443
3303BOOT: 3444BOOT:
3304 PerlIO_define_layer (aTHX_ &PerlIO_cede); 3445 PerlIO_define_layer (aTHX_ &PerlIO_cede);
3314 GvSTASH (CvGV (cv)) 3455 GvSTASH (CvGV (cv))
3315 ); 3456 );
3316 OUTPUT: 3457 OUTPUT:
3317 RETVAL 3458 RETVAL
3318 3459
3319# helper for Coro::Channel 3460# helper for Coro::Channel and others
3320SV * 3461SV *
3321_alloc (int count) 3462_alloc (int count)
3322 CODE: 3463 CODE:
3323 RETVAL = coro_waitarray_new (aTHX_ count); 3464 RETVAL = coro_waitarray_new (aTHX_ count);
3324 OUTPUT: 3465 OUTPUT:
3382 for (i = 1; i <= wcount; ++i) 3523 for (i = 1; i <= wcount; ++i)
3383 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i]))); 3524 PUSHs (sv_2mortal (newRV_inc (AvARRAY (av)[i])));
3384 } 3525 }
3385} 3526}
3386 3527
3528MODULE = Coro::State PACKAGE = Coro::SemaphoreSet
3529
3530void
3531_may_delete (SV *sem, int count, int extra_refs)
3532 PPCODE:
3533{
3534 AV *av = (AV *)SvRV (sem);
3535
3536 if (SvREFCNT ((SV *)av) == 1 + extra_refs
3537 && AvFILLp (av) == 0 /* no waiters, just count */
3538 && SvIV (AvARRAY (av)[0]) == count)
3539 XSRETURN_YES;
3540
3541 XSRETURN_NO;
3542}
3543
3387MODULE = Coro::State PACKAGE = Coro::Signal 3544MODULE = Coro::State PACKAGE = Coro::Signal
3388 3545
3389SV * 3546SV *
3390new (SV *klass) 3547new (SV *klass)
3391 CODE: 3548 CODE:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines