… | |
… | |
12 | #include "perl.h" |
12 | #include "perl.h" |
13 | #include "XSUB.h" |
13 | #include "XSUB.h" |
14 | #include "perliol.h" |
14 | #include "perliol.h" |
15 | |
15 | |
16 | #include "schmorp.h" |
16 | #include "schmorp.h" |
|
|
17 | #include "ecb.h" |
17 | |
18 | |
18 | #include <stddef.h> |
19 | #include <stddef.h> |
19 | #include <stdio.h> |
20 | #include <stdio.h> |
20 | #include <errno.h> |
21 | #include <errno.h> |
21 | #include <assert.h> |
22 | #include <assert.h> |
… | |
… | |
87 | # define STACKLEVEL ((void *)&stacklevel) |
88 | # define STACKLEVEL ((void *)&stacklevel) |
88 | #endif |
89 | #endif |
89 | |
90 | |
90 | #define IN_DESTRUCT PL_dirty |
91 | #define IN_DESTRUCT PL_dirty |
91 | |
92 | |
92 | #if __GNUC__ >= 3 |
|
|
93 | # define attribute(x) __attribute__(x) |
|
|
94 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
|
|
95 | # define INLINE static inline |
|
|
96 | #else |
|
|
97 | # define attribute(x) |
|
|
98 | # define expect(expr,value) (expr) |
|
|
99 | # define INLINE static |
|
|
100 | #endif |
|
|
101 | |
|
|
102 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
103 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
104 | |
|
|
105 | #define NOINLINE attribute ((noinline)) |
|
|
106 | |
|
|
107 | #include "CoroAPI.h" |
93 | #include "CoroAPI.h" |
108 | #define GCoroAPI (&coroapi) /* very sneaky */ |
94 | #define GCoroAPI (&coroapi) /* very sneaky */ |
109 | |
95 | |
110 | #ifdef USE_ITHREADS |
96 | #ifdef USE_ITHREADS |
111 | # if CORO_PTHREAD |
97 | # if CORO_PTHREAD |
… | |
… | |
328 | |
314 | |
329 | /** time stuff **************************************************************/ |
315 | /** time stuff **************************************************************/ |
330 | |
316 | |
331 | #ifdef HAS_GETTIMEOFDAY |
317 | #ifdef HAS_GETTIMEOFDAY |
332 | |
318 | |
333 | static void |
319 | ECB_INLINE void |
334 | coro_u2time (pTHX_ UV ret[2]) |
320 | coro_u2time (pTHX_ UV ret[2]) |
335 | { |
321 | { |
336 | struct timeval tv; |
322 | struct timeval tv; |
337 | gettimeofday (&tv, 0); |
323 | gettimeofday (&tv, 0); |
338 | |
324 | |
339 | ret [0] = tv.tv_sec; |
325 | ret [0] = tv.tv_sec; |
340 | ret [1] = tv.tv_usec; |
326 | ret [1] = tv.tv_usec; |
341 | } |
327 | } |
342 | |
328 | |
343 | static double |
329 | ECB_INLINE double |
344 | coro_nvtime () |
330 | coro_nvtime () |
345 | { |
331 | { |
346 | struct timeval tv; |
332 | struct timeval tv; |
347 | gettimeofday (&tv, 0); |
333 | gettimeofday (&tv, 0); |
348 | |
334 | |
349 | return tv.tv_sec + tv.tv_usec * 1e-6; |
335 | return tv.tv_sec + tv.tv_usec * 1e-6; |
350 | } |
336 | } |
351 | |
337 | |
352 | static void |
338 | ECB_INLINE void |
353 | time_init (pTHX) |
339 | time_init (pTHX) |
354 | { |
340 | { |
355 | nvtime = coro_nvtime; |
341 | nvtime = coro_nvtime; |
356 | u2time = coro_u2time; |
342 | u2time = coro_u2time; |
357 | } |
343 | } |
358 | |
344 | |
359 | #else |
345 | #else |
360 | |
346 | |
361 | static void |
347 | ECB_INLINE void |
362 | time_init (pTHX) |
348 | time_init (pTHX) |
363 | { |
349 | { |
364 | SV **svp; |
350 | SV **svp; |
365 | |
351 | |
366 | require_pv ("Time/HiRes.pm"); |
352 | require_pv ("Time/HiRes.pm"); |
… | |
… | |
378 | |
364 | |
379 | #endif |
365 | #endif |
380 | |
366 | |
381 | /** lowlevel stuff **********************************************************/ |
367 | /** lowlevel stuff **********************************************************/ |
382 | |
368 | |
383 | static SV * |
369 | static SV * ecb_noinline |
384 | coro_get_sv (pTHX_ const char *name, int create) |
370 | coro_get_sv (pTHX_ const char *name, int create) |
385 | { |
371 | { |
386 | #if PERL_VERSION_ATLEAST (5,10,0) |
372 | #if PERL_VERSION_ATLEAST (5,10,0) |
387 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
373 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
388 | get_sv (name, create); |
374 | get_sv (name, create); |
389 | #endif |
375 | #endif |
390 | return get_sv (name, create); |
376 | return get_sv (name, create); |
391 | } |
377 | } |
392 | |
378 | |
393 | static AV * |
379 | static AV * ecb_noinline |
394 | coro_get_av (pTHX_ const char *name, int create) |
380 | coro_get_av (pTHX_ const char *name, int create) |
395 | { |
381 | { |
396 | #if PERL_VERSION_ATLEAST (5,10,0) |
382 | #if PERL_VERSION_ATLEAST (5,10,0) |
397 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
383 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
398 | get_av (name, create); |
384 | get_av (name, create); |
399 | #endif |
385 | #endif |
400 | return get_av (name, create); |
386 | return get_av (name, create); |
401 | } |
387 | } |
402 | |
388 | |
403 | static HV * |
389 | static HV * ecb_noinline |
404 | coro_get_hv (pTHX_ const char *name, int create) |
390 | coro_get_hv (pTHX_ const char *name, int create) |
405 | { |
391 | { |
406 | #if PERL_VERSION_ATLEAST (5,10,0) |
392 | #if PERL_VERSION_ATLEAST (5,10,0) |
407 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
393 | /* silence stupid and wrong 5.10 warning that I am unable to switch off */ |
408 | get_hv (name, create); |
394 | get_hv (name, create); |
409 | #endif |
395 | #endif |
410 | return get_hv (name, create); |
396 | return get_hv (name, create); |
411 | } |
397 | } |
412 | |
398 | |
413 | INLINE void |
399 | ECB_INLINE void |
414 | coro_times_update () |
400 | coro_times_update () |
415 | { |
401 | { |
416 | #ifdef coro_clock_gettime |
402 | #ifdef coro_clock_gettime |
417 | struct timespec ts; |
403 | struct timespec ts; |
418 | |
404 | |
… | |
… | |
431 | time_real [0] = tv [0]; |
417 | time_real [0] = tv [0]; |
432 | time_real [1] = tv [1] * 1000; |
418 | time_real [1] = tv [1] * 1000; |
433 | #endif |
419 | #endif |
434 | } |
420 | } |
435 | |
421 | |
436 | INLINE void |
422 | ECB_INLINE void |
437 | coro_times_add (struct coro *c) |
423 | coro_times_add (struct coro *c) |
438 | { |
424 | { |
439 | c->t_real [1] += time_real [1]; |
425 | c->t_real [1] += time_real [1]; |
440 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
426 | if (c->t_real [1] > 1000000000) { c->t_real [1] -= 1000000000; ++c->t_real [0]; } |
441 | c->t_real [0] += time_real [0]; |
427 | c->t_real [0] += time_real [0]; |
… | |
… | |
443 | c->t_cpu [1] += time_cpu [1]; |
429 | c->t_cpu [1] += time_cpu [1]; |
444 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
430 | if (c->t_cpu [1] > 1000000000) { c->t_cpu [1] -= 1000000000; ++c->t_cpu [0]; } |
445 | c->t_cpu [0] += time_cpu [0]; |
431 | c->t_cpu [0] += time_cpu [0]; |
446 | } |
432 | } |
447 | |
433 | |
448 | INLINE void |
434 | ECB_INLINE void |
449 | coro_times_sub (struct coro *c) |
435 | coro_times_sub (struct coro *c) |
450 | { |
436 | { |
451 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
437 | if (c->t_real [1] < time_real [1]) { c->t_real [1] += 1000000000; --c->t_real [0]; } |
452 | c->t_real [1] -= time_real [1]; |
438 | c->t_real [1] -= time_real [1]; |
453 | c->t_real [0] -= time_real [0]; |
439 | c->t_real [0] -= time_real [0]; |
… | |
… | |
461 | /* magic glue */ |
447 | /* magic glue */ |
462 | |
448 | |
463 | #define CORO_MAGIC_type_cv 26 |
449 | #define CORO_MAGIC_type_cv 26 |
464 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
450 | #define CORO_MAGIC_type_state PERL_MAGIC_ext |
465 | |
451 | |
466 | #define CORO_MAGIC_NN(sv, type) \ |
452 | #define CORO_MAGIC_NN(sv, type) \ |
467 | (expect_true (SvMAGIC (sv)->mg_type == type) \ |
453 | (ecb_expect_true (SvMAGIC (sv)->mg_type == type) \ |
468 | ? SvMAGIC (sv) \ |
454 | ? SvMAGIC (sv) \ |
469 | : mg_find (sv, type)) |
455 | : mg_find (sv, type)) |
470 | |
456 | |
471 | #define CORO_MAGIC(sv, type) \ |
457 | #define CORO_MAGIC(sv, type) \ |
472 | (expect_true (SvMAGIC (sv)) \ |
458 | (ecb_expect_true (SvMAGIC (sv)) \ |
473 | ? CORO_MAGIC_NN (sv, type) \ |
459 | ? CORO_MAGIC_NN (sv, type) \ |
474 | : 0) |
460 | : 0) |
475 | |
461 | |
476 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
462 | #define CORO_MAGIC_cv(cv) CORO_MAGIC (((SV *)(cv)), CORO_MAGIC_type_cv) |
477 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
463 | #define CORO_MAGIC_state(sv) CORO_MAGIC_NN (((SV *)(sv)), CORO_MAGIC_type_state) |
478 | |
464 | |
479 | INLINE MAGIC * |
465 | ECB_INLINE MAGIC * |
480 | SvSTATEhv_p (pTHX_ SV *coro) |
466 | SvSTATEhv_p (pTHX_ SV *coro) |
481 | { |
467 | { |
482 | MAGIC *mg; |
468 | MAGIC *mg; |
483 | |
469 | |
484 | if (expect_true ( |
470 | if (ecb_expect_true ( |
485 | SvTYPE (coro) == SVt_PVHV |
471 | SvTYPE (coro) == SVt_PVHV |
486 | && (mg = CORO_MAGIC_state (coro)) |
472 | && (mg = CORO_MAGIC_state (coro)) |
487 | && mg->mg_virtual == &coro_state_vtbl |
473 | && mg->mg_virtual == &coro_state_vtbl |
488 | )) |
474 | )) |
489 | return mg; |
475 | return mg; |
490 | |
476 | |
491 | return 0; |
477 | return 0; |
492 | } |
478 | } |
493 | |
479 | |
494 | INLINE struct coro * |
480 | ECB_INLINE struct coro * |
495 | SvSTATE_ (pTHX_ SV *coro) |
481 | SvSTATE_ (pTHX_ SV *coro) |
496 | { |
482 | { |
497 | MAGIC *mg; |
483 | MAGIC *mg; |
498 | |
484 | |
499 | if (SvROK (coro)) |
485 | if (SvROK (coro)) |
… | |
… | |
513 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
499 | #define SvSTATE_current SvSTATE_hv (SvRV (coro_current)) |
514 | |
500 | |
515 | /*****************************************************************************/ |
501 | /*****************************************************************************/ |
516 | /* padlist management and caching */ |
502 | /* padlist management and caching */ |
517 | |
503 | |
518 | static AV * |
504 | ECB_INLINE AV * |
519 | coro_derive_padlist (pTHX_ CV *cv) |
505 | coro_derive_padlist (pTHX_ CV *cv) |
520 | { |
506 | { |
521 | AV *padlist = CvPADLIST (cv); |
507 | AV *padlist = CvPADLIST (cv); |
522 | AV *newpadlist, *newpad; |
508 | AV *newpadlist, *newpad; |
523 | |
509 | |
… | |
… | |
535 | av_store (newpadlist, 1, (SV *)newpad); |
521 | av_store (newpadlist, 1, (SV *)newpad); |
536 | |
522 | |
537 | return newpadlist; |
523 | return newpadlist; |
538 | } |
524 | } |
539 | |
525 | |
540 | static void |
526 | ECB_INLINE void |
541 | free_padlist (pTHX_ AV *padlist) |
527 | free_padlist (pTHX_ AV *padlist) |
542 | { |
528 | { |
543 | /* may be during global destruction */ |
529 | /* may be during global destruction */ |
544 | if (!IN_DESTRUCT) |
530 | if (!IN_DESTRUCT) |
545 | { |
531 | { |
… | |
… | |
588 | 0, 0, 0, 0, |
574 | 0, 0, 0, 0, |
589 | coro_cv_free |
575 | coro_cv_free |
590 | }; |
576 | }; |
591 | |
577 | |
592 | /* the next two functions merely cache the padlists */ |
578 | /* the next two functions merely cache the padlists */ |
593 | static void |
579 | ECB_INLINE void |
594 | get_padlist (pTHX_ CV *cv) |
580 | get_padlist (pTHX_ CV *cv) |
595 | { |
581 | { |
596 | MAGIC *mg = CORO_MAGIC_cv (cv); |
582 | MAGIC *mg = CORO_MAGIC_cv (cv); |
597 | AV *av; |
583 | AV *av; |
598 | |
584 | |
599 | if (expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
585 | if (ecb_expect_true (mg && AvFILLp ((av = (AV *)mg->mg_obj)) >= 0)) |
600 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
586 | CvPADLIST (cv) = (AV *)AvARRAY (av)[AvFILLp (av)--]; |
601 | else |
587 | else |
602 | { |
588 | { |
603 | #if CORO_PREFER_PERL_FUNCTIONS |
589 | #if CORO_PREFER_PERL_FUNCTIONS |
604 | /* this is probably cleaner? but also slower! */ |
590 | /* this is probably cleaner? but also slower! */ |
… | |
… | |
611 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
597 | CvPADLIST (cv) = coro_derive_padlist (aTHX_ cv); |
612 | #endif |
598 | #endif |
613 | } |
599 | } |
614 | } |
600 | } |
615 | |
601 | |
616 | static void |
602 | ECB_INLINE void |
617 | put_padlist (pTHX_ CV *cv) |
603 | put_padlist (pTHX_ CV *cv) |
618 | { |
604 | { |
619 | MAGIC *mg = CORO_MAGIC_cv (cv); |
605 | MAGIC *mg = CORO_MAGIC_cv (cv); |
620 | AV *av; |
606 | AV *av; |
621 | |
607 | |
622 | if (expect_false (!mg)) |
608 | if (ecb_expect_false (!mg)) |
623 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
609 | mg = sv_magicext ((SV *)cv, (SV *)newAV (), CORO_MAGIC_type_cv, &coro_cv_vtbl, 0, 0); |
624 | |
610 | |
625 | av = (AV *)mg->mg_obj; |
611 | av = (AV *)mg->mg_obj; |
626 | |
612 | |
627 | if (expect_false (AvFILLp (av) >= AvMAX (av))) |
613 | if (ecb_expect_false (AvFILLp (av) >= AvMAX (av))) |
628 | av_extend (av, AvFILLp (av) + 1); |
614 | av_extend (av, AvFILLp (av) + 1); |
629 | |
615 | |
630 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
616 | AvARRAY (av)[++AvFILLp (av)] = (SV *)CvPADLIST (cv); |
631 | } |
617 | } |
632 | |
618 | |
… | |
… | |
677 | } |
663 | } |
678 | #endif |
664 | #endif |
679 | } |
665 | } |
680 | } |
666 | } |
681 | |
667 | |
682 | #define SWAP_SVS(coro) \ |
668 | #define SWAP_SVS(coro) \ |
683 | if (expect_false ((coro)->swap_sv)) \ |
669 | if (ecb_expect_false ((coro)->swap_sv)) \ |
684 | swap_svs (aTHX_ (coro)) |
670 | swap_svs (aTHX_ (coro)) |
685 | |
671 | |
686 | static void |
672 | static void |
687 | on_enterleave_call (pTHX_ SV *cb); |
673 | on_enterleave_call (pTHX_ SV *cb); |
688 | |
674 | |
… | |
… | |
708 | dSP; |
694 | dSP; |
709 | |
695 | |
710 | CV *cv; |
696 | CV *cv; |
711 | |
697 | |
712 | /* now do the ugly restore mess */ |
698 | /* now do the ugly restore mess */ |
713 | while (expect_true (cv = (CV *)POPs)) |
699 | while (ecb_expect_true (cv = (CV *)POPs)) |
714 | { |
700 | { |
715 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
701 | put_padlist (aTHX_ cv); /* mark this padlist as available */ |
716 | CvDEPTH (cv) = PTR2IV (POPs); |
702 | CvDEPTH (cv) = PTR2IV (POPs); |
717 | CvPADLIST (cv) = (AV *)POPs; |
703 | CvPADLIST (cv) = (AV *)POPs; |
718 | } |
704 | } |
… | |
… | |
721 | } |
707 | } |
722 | |
708 | |
723 | slf_frame = c->slf_frame; |
709 | slf_frame = c->slf_frame; |
724 | CORO_THROW = c->except; |
710 | CORO_THROW = c->except; |
725 | |
711 | |
726 | if (expect_false (enable_times)) |
712 | if (ecb_expect_false (enable_times)) |
727 | { |
713 | { |
728 | if (expect_false (!times_valid)) |
714 | if (ecb_expect_false (!times_valid)) |
729 | coro_times_update (); |
715 | coro_times_update (); |
730 | |
716 | |
731 | coro_times_sub (c); |
717 | coro_times_sub (c); |
732 | } |
718 | } |
733 | |
719 | |
734 | if (expect_false (c->on_enter)) |
720 | if (ecb_expect_false (c->on_enter)) |
735 | { |
721 | { |
736 | int i; |
722 | int i; |
737 | |
723 | |
738 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
724 | for (i = 0; i <= AvFILLp (c->on_enter); ++i) |
739 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
725 | on_enterleave_call (aTHX_ AvARRAY (c->on_enter)[i]); |
… | |
… | |
745 | static void |
731 | static void |
746 | save_perl (pTHX_ Coro__State c) |
732 | save_perl (pTHX_ Coro__State c) |
747 | { |
733 | { |
748 | SWAP_SVS (c); |
734 | SWAP_SVS (c); |
749 | |
735 | |
750 | if (expect_false (c->on_leave)) |
736 | if (ecb_expect_false (c->on_leave)) |
751 | { |
737 | { |
752 | int i; |
738 | int i; |
753 | |
739 | |
754 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
740 | for (i = AvFILLp (c->on_leave); i >= 0; --i) |
755 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
741 | on_enterleave_call (aTHX_ AvARRAY (c->on_leave)[i]); |
756 | } |
742 | } |
757 | |
743 | |
758 | times_valid = 0; |
744 | times_valid = 0; |
759 | |
745 | |
760 | if (expect_false (enable_times)) |
746 | if (ecb_expect_false (enable_times)) |
761 | { |
747 | { |
762 | coro_times_update (); times_valid = 1; |
748 | coro_times_update (); times_valid = 1; |
763 | coro_times_add (c); |
749 | coro_times_add (c); |
764 | } |
750 | } |
765 | |
751 | |
… | |
… | |
779 | |
765 | |
780 | XPUSHs (Nullsv); |
766 | XPUSHs (Nullsv); |
781 | /* this loop was inspired by pp_caller */ |
767 | /* this loop was inspired by pp_caller */ |
782 | for (;;) |
768 | for (;;) |
783 | { |
769 | { |
784 | while (expect_true (cxix >= 0)) |
770 | while (ecb_expect_true (cxix >= 0)) |
785 | { |
771 | { |
786 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
772 | PERL_CONTEXT *cx = &ccstk[cxix--]; |
787 | |
773 | |
788 | if (expect_true (CxTYPE (cx) == CXt_SUB) || expect_false (CxTYPE (cx) == CXt_FORMAT)) |
774 | if (ecb_expect_true (CxTYPE (cx) == CXt_SUB) || ecb_expect_false (CxTYPE (cx) == CXt_FORMAT)) |
789 | { |
775 | { |
790 | CV *cv = cx->blk_sub.cv; |
776 | CV *cv = cx->blk_sub.cv; |
791 | |
777 | |
792 | if (expect_true (CvDEPTH (cv))) |
778 | if (ecb_expect_true (CvDEPTH (cv))) |
793 | { |
779 | { |
794 | EXTEND (SP, 3); |
780 | EXTEND (SP, 3); |
795 | PUSHs ((SV *)CvPADLIST (cv)); |
781 | PUSHs ((SV *)CvPADLIST (cv)); |
796 | PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); |
782 | PUSHs (INT2PTR (SV *, (IV)CvDEPTH (cv))); |
797 | PUSHs ((SV *)cv); |
783 | PUSHs ((SV *)cv); |
… | |
… | |
800 | get_padlist (aTHX_ cv); |
786 | get_padlist (aTHX_ cv); |
801 | } |
787 | } |
802 | } |
788 | } |
803 | } |
789 | } |
804 | |
790 | |
805 | if (expect_true (top_si->si_type == PERLSI_MAIN)) |
791 | if (ecb_expect_true (top_si->si_type == PERLSI_MAIN)) |
806 | break; |
792 | break; |
807 | |
793 | |
808 | top_si = top_si->si_prev; |
794 | top_si = top_si->si_prev; |
809 | ccstk = top_si->si_cxstack; |
795 | ccstk = top_si->si_cxstack; |
810 | cxix = top_si->si_cxix; |
796 | cxix = top_si->si_cxix; |
… | |
… | |
812 | |
798 | |
813 | PUTBACK; |
799 | PUTBACK; |
814 | } |
800 | } |
815 | |
801 | |
816 | /* allocate some space on the context stack for our purposes */ |
802 | /* allocate some space on the context stack for our purposes */ |
817 | if (expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) |
803 | if (ecb_expect_false (cxstack_ix + SLOT_COUNT >= cxstack_max)) |
818 | { |
804 | { |
819 | unsigned int i; |
805 | unsigned int i; |
820 | |
806 | |
821 | for (i = 0; i < SLOT_COUNT; ++i) |
807 | for (i = 0; i < SLOT_COUNT; ++i) |
822 | CXINC; |
808 | CXINC; |
… | |
… | |
971 | * This overrides the default magic get method of %SIG elements. |
957 | * This overrides the default magic get method of %SIG elements. |
972 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
958 | * The original one doesn't provide for reading back of PL_diehook/PL_warnhook |
973 | * and instead of trying to save and restore the hash elements (extremely slow), |
959 | * and instead of trying to save and restore the hash elements (extremely slow), |
974 | * we just provide our own readback here. |
960 | * we just provide our own readback here. |
975 | */ |
961 | */ |
976 | static int |
962 | static int ecb_cold |
977 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
963 | coro_sigelem_get (pTHX_ SV *sv, MAGIC *mg) |
978 | { |
964 | { |
979 | const char *s = MgPV_nolen_const (mg); |
965 | const char *s = MgPV_nolen_const (mg); |
980 | |
966 | |
981 | if (*s == '_') |
967 | if (*s == '_') |
… | |
… | |
993 | } |
979 | } |
994 | |
980 | |
995 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
981 | return orig_sigelem_get ? orig_sigelem_get (aTHX_ sv, mg) : 0; |
996 | } |
982 | } |
997 | |
983 | |
998 | static int |
984 | static int ecb_cold |
999 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
985 | coro_sigelem_clr (pTHX_ SV *sv, MAGIC *mg) |
1000 | { |
986 | { |
1001 | const char *s = MgPV_nolen_const (mg); |
987 | const char *s = MgPV_nolen_const (mg); |
1002 | |
988 | |
1003 | if (*s == '_') |
989 | if (*s == '_') |
… | |
… | |
1017 | } |
1003 | } |
1018 | |
1004 | |
1019 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
1005 | return orig_sigelem_clr ? orig_sigelem_clr (aTHX_ sv, mg) : 0; |
1020 | } |
1006 | } |
1021 | |
1007 | |
1022 | static int |
1008 | static int ecb_cold |
1023 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1009 | coro_sigelem_set (pTHX_ SV *sv, MAGIC *mg) |
1024 | { |
1010 | { |
1025 | const char *s = MgPV_nolen_const (mg); |
1011 | const char *s = MgPV_nolen_const (mg); |
1026 | |
1012 | |
1027 | if (*s == '_') |
1013 | if (*s == '_') |
… | |
… | |
1062 | return 1; |
1048 | return 1; |
1063 | } |
1049 | } |
1064 | |
1050 | |
1065 | static UNOP init_perl_op; |
1051 | static UNOP init_perl_op; |
1066 | |
1052 | |
1067 | static void NOINLINE /* noinline to keep it out of the transfer fast path */ |
1053 | ecb_noinline static void /* noinline to keep it out of the transfer fast path */ |
1068 | init_perl (pTHX_ struct coro *coro) |
1054 | init_perl (pTHX_ struct coro *coro) |
1069 | { |
1055 | { |
1070 | /* |
1056 | /* |
1071 | * emulate part of the perl startup here. |
1057 | * emulate part of the perl startup here. |
1072 | */ |
1058 | */ |
… | |
… | |
1134 | /* copy throw, in case it was set before init_perl */ |
1120 | /* copy throw, in case it was set before init_perl */ |
1135 | CORO_THROW = coro->except; |
1121 | CORO_THROW = coro->except; |
1136 | |
1122 | |
1137 | SWAP_SVS (coro); |
1123 | SWAP_SVS (coro); |
1138 | |
1124 | |
1139 | if (expect_false (enable_times)) |
1125 | if (ecb_expect_false (enable_times)) |
1140 | { |
1126 | { |
1141 | coro_times_update (); |
1127 | coro_times_update (); |
1142 | coro_times_sub (coro); |
1128 | coro_times_sub (coro); |
1143 | } |
1129 | } |
1144 | } |
1130 | } |
… | |
… | |
1217 | SvREFCNT_dec (coro->invoke_cb); |
1203 | SvREFCNT_dec (coro->invoke_cb); |
1218 | SvREFCNT_dec (coro->invoke_av); |
1204 | SvREFCNT_dec (coro->invoke_av); |
1219 | } |
1205 | } |
1220 | } |
1206 | } |
1221 | |
1207 | |
1222 | INLINE void |
1208 | ECB_INLINE void |
1223 | free_coro_mortal (pTHX) |
1209 | free_coro_mortal (pTHX) |
1224 | { |
1210 | { |
1225 | if (expect_true (coro_mortal)) |
1211 | if (ecb_expect_true (coro_mortal)) |
1226 | { |
1212 | { |
1227 | SvREFCNT_dec ((SV *)coro_mortal); |
1213 | SvREFCNT_dec ((SV *)coro_mortal); |
1228 | coro_mortal = 0; |
1214 | coro_mortal = 0; |
1229 | } |
1215 | } |
1230 | } |
1216 | } |
… | |
… | |
1365 | |
1351 | |
1366 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1352 | return frame->check (aTHX_ frame); /* execute the restored frame - there must be one */ |
1367 | } |
1353 | } |
1368 | |
1354 | |
1369 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1355 | /* initialises PL_top_env and injects a pseudo-slf-call to set the stacklevel */ |
1370 | static void NOINLINE |
1356 | static void ecb_noinline |
1371 | cctx_prepare (pTHX) |
1357 | cctx_prepare (pTHX) |
1372 | { |
1358 | { |
1373 | PL_top_env = &PL_start_env; |
1359 | PL_top_env = &PL_start_env; |
1374 | |
1360 | |
1375 | if (cctx_current->flags & CC_TRACE) |
1361 | if (cctx_current->flags & CC_TRACE) |
… | |
… | |
1386 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1372 | slf_frame.prepare = slf_prepare_set_stacklevel; |
1387 | slf_frame.check = slf_check_set_stacklevel; |
1373 | slf_frame.check = slf_check_set_stacklevel; |
1388 | } |
1374 | } |
1389 | |
1375 | |
1390 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1376 | /* the tail of transfer: execute stuff we can only do after a transfer */ |
1391 | INLINE void |
1377 | ECB_INLINE void |
1392 | transfer_tail (pTHX) |
1378 | transfer_tail (pTHX) |
1393 | { |
1379 | { |
1394 | free_coro_mortal (aTHX); |
1380 | free_coro_mortal (aTHX); |
1395 | } |
1381 | } |
1396 | |
1382 | |
… | |
… | |
1548 | #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) |
1534 | #define CCTX_EXPIRED(cctx) ((cctx)->gen != cctx_gen || ((cctx)->flags & CC_NOREUSE)) |
1549 | |
1535 | |
1550 | static coro_cctx * |
1536 | static coro_cctx * |
1551 | cctx_get (pTHX) |
1537 | cctx_get (pTHX) |
1552 | { |
1538 | { |
1553 | while (expect_true (cctx_first)) |
1539 | while (ecb_expect_true (cctx_first)) |
1554 | { |
1540 | { |
1555 | coro_cctx *cctx = cctx_first; |
1541 | coro_cctx *cctx = cctx_first; |
1556 | cctx_first = cctx->next; |
1542 | cctx_first = cctx->next; |
1557 | --cctx_idle; |
1543 | --cctx_idle; |
1558 | |
1544 | |
1559 | if (expect_true (!CCTX_EXPIRED (cctx))) |
1545 | if (ecb_expect_true (!CCTX_EXPIRED (cctx))) |
1560 | return cctx; |
1546 | return cctx; |
1561 | |
1547 | |
1562 | cctx_destroy (cctx); |
1548 | cctx_destroy (cctx); |
1563 | } |
1549 | } |
1564 | |
1550 | |
… | |
… | |
1569 | cctx_put (coro_cctx *cctx) |
1555 | cctx_put (coro_cctx *cctx) |
1570 | { |
1556 | { |
1571 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
1557 | assert (("FATAL: cctx_put called on non-initialised cctx in Coro (please report)", cctx->sptr)); |
1572 | |
1558 | |
1573 | /* free another cctx if overlimit */ |
1559 | /* free another cctx if overlimit */ |
1574 | if (expect_false (cctx_idle >= cctx_max_idle)) |
1560 | if (ecb_expect_false (cctx_idle >= cctx_max_idle)) |
1575 | { |
1561 | { |
1576 | coro_cctx *first = cctx_first; |
1562 | coro_cctx *first = cctx_first; |
1577 | cctx_first = first->next; |
1563 | cctx_first = first->next; |
1578 | --cctx_idle; |
1564 | --cctx_idle; |
1579 | |
1565 | |
… | |
… | |
1590 | static void |
1576 | static void |
1591 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1577 | transfer_check (pTHX_ struct coro *prev, struct coro *next) |
1592 | { |
1578 | { |
1593 | /* TODO: throwing up here is considered harmful */ |
1579 | /* TODO: throwing up here is considered harmful */ |
1594 | |
1580 | |
1595 | if (expect_true (prev != next)) |
1581 | if (ecb_expect_true (prev != next)) |
1596 | { |
1582 | { |
1597 | if (expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1583 | if (ecb_expect_false (!(prev->flags & (CF_RUNNING | CF_NEW)))) |
1598 | croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); |
1584 | croak ("Coro::State::transfer called with a blocked prev Coro::State, but can only transfer from running or new states,"); |
1599 | |
1585 | |
1600 | if (expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED))) |
1586 | if (ecb_expect_false (next->flags & (CF_RUNNING | CF_ZOMBIE | CF_SUSPENDED))) |
1601 | croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); |
1587 | croak ("Coro::State::transfer called with running, destroyed or suspended next Coro::State, but can only transfer to inactive states,"); |
1602 | |
1588 | |
1603 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1589 | #if !PERL_VERSION_ATLEAST (5,10,0) |
1604 | if (expect_false (PL_lex_state != LEX_NOTPARSING)) |
1590 | if (ecb_expect_false (PL_lex_state != LEX_NOTPARSING)) |
1605 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1591 | croak ("Coro::State::transfer called while parsing, but this is not supported in your perl version,"); |
1606 | #endif |
1592 | #endif |
1607 | } |
1593 | } |
1608 | } |
1594 | } |
1609 | |
1595 | |
1610 | /* always use the TRANSFER macro */ |
1596 | /* always use the TRANSFER macro */ |
1611 | static void NOINLINE /* noinline so we have a fixed stackframe */ |
1597 | static void ecb_noinline /* noinline so we have a fixed stackframe */ |
1612 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1598 | transfer (pTHX_ struct coro *prev, struct coro *next, int force_cctx) |
1613 | { |
1599 | { |
1614 | dSTACKLEVEL; |
1600 | dSTACKLEVEL; |
1615 | |
1601 | |
1616 | /* sometimes transfer is only called to set idle_sp */ |
1602 | /* sometimes transfer is only called to set idle_sp */ |
1617 | if (expect_false (!prev)) |
1603 | if (ecb_expect_false (!prev)) |
1618 | { |
1604 | { |
1619 | cctx_current->idle_sp = STACKLEVEL; |
1605 | cctx_current->idle_sp = STACKLEVEL; |
1620 | assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1606 | assert (cctx_current->idle_te = PL_top_env); /* just for the side-effect when asserts are enabled */ |
1621 | } |
1607 | } |
1622 | else if (expect_true (prev != next)) |
1608 | else if (ecb_expect_true (prev != next)) |
1623 | { |
1609 | { |
1624 | coro_cctx *cctx_prev; |
1610 | coro_cctx *cctx_prev; |
1625 | |
1611 | |
1626 | if (expect_false (prev->flags & CF_NEW)) |
1612 | if (ecb_expect_false (prev->flags & CF_NEW)) |
1627 | { |
1613 | { |
1628 | /* create a new empty/source context */ |
1614 | /* create a new empty/source context */ |
1629 | prev->flags &= ~CF_NEW; |
1615 | prev->flags &= ~CF_NEW; |
1630 | prev->flags |= CF_RUNNING; |
1616 | prev->flags |= CF_RUNNING; |
1631 | } |
1617 | } |
… | |
… | |
1634 | next->flags |= CF_RUNNING; |
1620 | next->flags |= CF_RUNNING; |
1635 | |
1621 | |
1636 | /* first get rid of the old state */ |
1622 | /* first get rid of the old state */ |
1637 | save_perl (aTHX_ prev); |
1623 | save_perl (aTHX_ prev); |
1638 | |
1624 | |
1639 | if (expect_false (next->flags & CF_NEW)) |
1625 | if (ecb_expect_false (next->flags & CF_NEW)) |
1640 | { |
1626 | { |
1641 | /* need to start coroutine */ |
1627 | /* need to start coroutine */ |
1642 | next->flags &= ~CF_NEW; |
1628 | next->flags &= ~CF_NEW; |
1643 | /* setup coroutine call */ |
1629 | /* setup coroutine call */ |
1644 | init_perl (aTHX_ next); |
1630 | init_perl (aTHX_ next); |
1645 | } |
1631 | } |
1646 | else |
1632 | else |
1647 | load_perl (aTHX_ next); |
1633 | load_perl (aTHX_ next); |
1648 | |
1634 | |
1649 | /* possibly untie and reuse the cctx */ |
1635 | /* possibly untie and reuse the cctx */ |
1650 | if (expect_true ( |
1636 | if (ecb_expect_true ( |
1651 | cctx_current->idle_sp == STACKLEVEL |
1637 | cctx_current->idle_sp == STACKLEVEL |
1652 | && !(cctx_current->flags & CC_TRACE) |
1638 | && !(cctx_current->flags & CC_TRACE) |
1653 | && !force_cctx |
1639 | && !force_cctx |
1654 | )) |
1640 | )) |
1655 | { |
1641 | { |
1656 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1642 | /* I assume that stacklevel is a stronger indicator than PL_top_env changes */ |
1657 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1643 | assert (("FATAL: current top_env must equal previous top_env in Coro (please report)", PL_top_env == cctx_current->idle_te)); |
1658 | |
1644 | |
1659 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1645 | /* if the cctx is about to be destroyed we need to make sure we won't see it in cctx_get. */ |
1660 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1646 | /* without this the next cctx_get might destroy the running cctx while still in use */ |
1661 | if (expect_false (CCTX_EXPIRED (cctx_current))) |
1647 | if (ecb_expect_false (CCTX_EXPIRED (cctx_current))) |
1662 | if (expect_true (!next->cctx)) |
1648 | if (ecb_expect_true (!next->cctx)) |
1663 | next->cctx = cctx_get (aTHX); |
1649 | next->cctx = cctx_get (aTHX); |
1664 | |
1650 | |
1665 | cctx_put (cctx_current); |
1651 | cctx_put (cctx_current); |
1666 | } |
1652 | } |
1667 | else |
1653 | else |
1668 | prev->cctx = cctx_current; |
1654 | prev->cctx = cctx_current; |
1669 | |
1655 | |
1670 | ++next->usecount; |
1656 | ++next->usecount; |
1671 | |
1657 | |
1672 | cctx_prev = cctx_current; |
1658 | cctx_prev = cctx_current; |
1673 | cctx_current = expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1659 | cctx_current = ecb_expect_false (next->cctx) ? next->cctx : cctx_get (aTHX); |
1674 | |
1660 | |
1675 | next->cctx = 0; |
1661 | next->cctx = 0; |
1676 | |
1662 | |
1677 | if (expect_false (cctx_prev != cctx_current)) |
1663 | if (ecb_expect_false (cctx_prev != cctx_current)) |
1678 | { |
1664 | { |
1679 | cctx_prev->top_env = PL_top_env; |
1665 | cctx_prev->top_env = PL_top_env; |
1680 | PL_top_env = cctx_current->top_env; |
1666 | PL_top_env = cctx_current->top_env; |
1681 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1667 | coro_transfer (&cctx_prev->cctx, &cctx_current->cctx); |
1682 | } |
1668 | } |
… | |
… | |
1748 | Safefree (coro); |
1734 | Safefree (coro); |
1749 | |
1735 | |
1750 | return 0; |
1736 | return 0; |
1751 | } |
1737 | } |
1752 | |
1738 | |
1753 | static int |
1739 | static int ecb_cold |
1754 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
1740 | coro_state_dup (pTHX_ MAGIC *mg, CLONE_PARAMS *params) |
1755 | { |
1741 | { |
1756 | /* called when perl clones the current process the slow way (windows process emulation) */ |
1742 | /* called when perl clones the current process the slow way (windows process emulation) */ |
1757 | /* WE SIMply nuke the pointers in the copy, causing perl to croak */ |
1743 | /* WE SIMply nuke the pointers in the copy, causing perl to croak */ |
1758 | mg->mg_ptr = 0; |
1744 | mg->mg_ptr = 0; |
… | |
… | |
1789 | TRANSFER (ta, 1); |
1775 | TRANSFER (ta, 1); |
1790 | } |
1776 | } |
1791 | |
1777 | |
1792 | /** Coro ********************************************************************/ |
1778 | /** Coro ********************************************************************/ |
1793 | |
1779 | |
1794 | INLINE void |
1780 | ECB_INLINE void |
1795 | coro_enq (pTHX_ struct coro *coro) |
1781 | coro_enq (pTHX_ struct coro *coro) |
1796 | { |
1782 | { |
1797 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1783 | struct coro **ready = coro_ready [coro->prio - CORO_PRIO_MIN]; |
1798 | |
1784 | |
1799 | SvREFCNT_inc_NN (coro->hv); |
1785 | SvREFCNT_inc_NN (coro->hv); |
… | |
… | |
1801 | coro->next_ready = 0; |
1787 | coro->next_ready = 0; |
1802 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1788 | *(ready [0] ? &ready [1]->next_ready : &ready [0]) = coro; |
1803 | ready [1] = coro; |
1789 | ready [1] = coro; |
1804 | } |
1790 | } |
1805 | |
1791 | |
1806 | INLINE struct coro * |
1792 | ECB_INLINE struct coro * |
1807 | coro_deq (pTHX) |
1793 | coro_deq (pTHX) |
1808 | { |
1794 | { |
1809 | int prio; |
1795 | int prio; |
1810 | |
1796 | |
1811 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
1797 | for (prio = CORO_PRIO_MAX - CORO_PRIO_MIN + 1; --prio >= 0; ) |
… | |
… | |
1864 | { |
1850 | { |
1865 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1851 | return !!(SvSTATE (coro_sv)->flags & CF_READY); |
1866 | } |
1852 | } |
1867 | |
1853 | |
1868 | /* expects to own a reference to next->hv */ |
1854 | /* expects to own a reference to next->hv */ |
1869 | INLINE void |
1855 | ECB_INLINE void |
1870 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1856 | prepare_schedule_to (pTHX_ struct coro_transfer_args *ta, struct coro *next) |
1871 | { |
1857 | { |
1872 | SV *prev_sv = SvRV (coro_current); |
1858 | SV *prev_sv = SvRV (coro_current); |
1873 | |
1859 | |
1874 | ta->prev = SvSTATE_hv (prev_sv); |
1860 | ta->prev = SvSTATE_hv (prev_sv); |
… | |
… | |
1887 | { |
1873 | { |
1888 | for (;;) |
1874 | for (;;) |
1889 | { |
1875 | { |
1890 | struct coro *next = coro_deq (aTHX); |
1876 | struct coro *next = coro_deq (aTHX); |
1891 | |
1877 | |
1892 | if (expect_true (next)) |
1878 | if (ecb_expect_true (next)) |
1893 | { |
1879 | { |
1894 | /* cannot transfer to destroyed coros, skip and look for next */ |
1880 | /* cannot transfer to destroyed coros, skip and look for next */ |
1895 | if (expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED))) |
1881 | if (ecb_expect_false (next->flags & (CF_ZOMBIE | CF_SUSPENDED))) |
1896 | SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ |
1882 | SvREFCNT_dec (next->hv); /* coro_nready has already been taken care of by destroy */ |
1897 | else |
1883 | else |
1898 | { |
1884 | { |
1899 | next->flags &= ~CF_READY; |
1885 | next->flags &= ~CF_READY; |
1900 | --coro_nready; |
1886 | --coro_nready; |
… | |
… | |
1933 | } |
1919 | } |
1934 | } |
1920 | } |
1935 | } |
1921 | } |
1936 | } |
1922 | } |
1937 | |
1923 | |
1938 | INLINE void |
1924 | ECB_INLINE void |
1939 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1925 | prepare_cede (pTHX_ struct coro_transfer_args *ta) |
1940 | { |
1926 | { |
1941 | api_ready (aTHX_ coro_current); |
1927 | api_ready (aTHX_ coro_current); |
1942 | prepare_schedule (aTHX_ ta); |
1928 | prepare_schedule (aTHX_ ta); |
1943 | } |
1929 | } |
1944 | |
1930 | |
1945 | INLINE void |
1931 | ECB_INLINE void |
1946 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1932 | prepare_cede_notself (pTHX_ struct coro_transfer_args *ta) |
1947 | { |
1933 | { |
1948 | SV *prev = SvRV (coro_current); |
1934 | SV *prev = SvRV (coro_current); |
1949 | |
1935 | |
1950 | if (coro_nready) |
1936 | if (coro_nready) |
… | |
… | |
1980 | { |
1966 | { |
1981 | struct coro_transfer_args ta; |
1967 | struct coro_transfer_args ta; |
1982 | |
1968 | |
1983 | prepare_cede (aTHX_ &ta); |
1969 | prepare_cede (aTHX_ &ta); |
1984 | |
1970 | |
1985 | if (expect_true (ta.prev != ta.next)) |
1971 | if (ecb_expect_true (ta.prev != ta.next)) |
1986 | { |
1972 | { |
1987 | TRANSFER (ta, 1); |
1973 | TRANSFER (ta, 1); |
1988 | return 1; |
1974 | return 1; |
1989 | } |
1975 | } |
1990 | else |
1976 | else |
… | |
… | |
2203 | coro = SvSTATE (arg [0]); |
2189 | coro = SvSTATE (arg [0]); |
2204 | coro_hv = coro->hv; |
2190 | coro_hv = coro->hv; |
2205 | |
2191 | |
2206 | coro_set_status (aTHX_ coro, arg + 1, items - 1); |
2192 | coro_set_status (aTHX_ coro, arg + 1, items - 1); |
2207 | |
2193 | |
2208 | if (expect_false (coro->flags & CF_NOCANCEL)) |
2194 | if (ecb_expect_false (coro->flags & CF_NOCANCEL)) |
2209 | { |
2195 | { |
2210 | /* coro currently busy cancelling something, so just notify it */ |
2196 | /* coro currently busy cancelling something, so just notify it */ |
2211 | coro->slf_frame.data = (void *)coro; |
2197 | coro->slf_frame.data = (void *)coro; |
2212 | |
2198 | |
2213 | frame->prepare = prepare_nop; |
2199 | frame->prepare = prepare_nop; |
… | |
… | |
2321 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2307 | slf_init_pool_handler (pTHX_ struct CoroSLF *frame, CV *cv, SV **arg, int items) |
2322 | { |
2308 | { |
2323 | HV *hv = (HV *)SvRV (coro_current); |
2309 | HV *hv = (HV *)SvRV (coro_current); |
2324 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2310 | struct coro *coro = SvSTATE_hv ((SV *)hv); |
2325 | |
2311 | |
2326 | if (expect_true (coro->saved_deffh)) |
2312 | if (ecb_expect_true (coro->saved_deffh)) |
2327 | { |
2313 | { |
2328 | /* subsequent iteration */ |
2314 | /* subsequent iteration */ |
2329 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2315 | SvREFCNT_dec ((SV *)PL_defoutgv); PL_defoutgv = (GV *)coro->saved_deffh; |
2330 | coro->saved_deffh = 0; |
2316 | coro->saved_deffh = 0; |
2331 | |
2317 | |
… | |
… | |
2597 | I32 checkmark; /* mark SP to see how many elements check has pushed */ |
2583 | I32 checkmark; /* mark SP to see how many elements check has pushed */ |
2598 | |
2584 | |
2599 | /* set up the slf frame, unless it has already been set-up */ |
2585 | /* set up the slf frame, unless it has already been set-up */ |
2600 | /* the latter happens when a new coro has been started */ |
2586 | /* the latter happens when a new coro has been started */ |
2601 | /* or when a new cctx was attached to an existing coroutine */ |
2587 | /* or when a new cctx was attached to an existing coroutine */ |
2602 | if (expect_true (!slf_frame.prepare)) |
2588 | if (ecb_expect_true (!slf_frame.prepare)) |
2603 | { |
2589 | { |
2604 | /* first iteration */ |
2590 | /* first iteration */ |
2605 | dSP; |
2591 | dSP; |
2606 | SV **arg = PL_stack_base + TOPMARK + 1; |
2592 | SV **arg = PL_stack_base + TOPMARK + 1; |
2607 | int items = SP - arg; /* args without function object */ |
2593 | int items = SP - arg; /* args without function object */ |
… | |
… | |
2648 | while (slf_frame.check (aTHX_ &slf_frame)); |
2634 | while (slf_frame.check (aTHX_ &slf_frame)); |
2649 | |
2635 | |
2650 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
2636 | slf_frame.prepare = 0; /* invalidate the frame, we are done processing it */ |
2651 | |
2637 | |
2652 | /* exception handling */ |
2638 | /* exception handling */ |
2653 | if (expect_false (CORO_THROW)) |
2639 | if (ecb_expect_false (CORO_THROW)) |
2654 | { |
2640 | { |
2655 | SV *exception = sv_2mortal (CORO_THROW); |
2641 | SV *exception = sv_2mortal (CORO_THROW); |
2656 | |
2642 | |
2657 | CORO_THROW = 0; |
2643 | CORO_THROW = 0; |
2658 | sv_setsv (ERRSV, exception); |
2644 | sv_setsv (ERRSV, exception); |
… | |
… | |
2660 | } |
2646 | } |
2661 | |
2647 | |
2662 | /* return value handling - mostly like entersub */ |
2648 | /* return value handling - mostly like entersub */ |
2663 | /* make sure we put something on the stack in scalar context */ |
2649 | /* make sure we put something on the stack in scalar context */ |
2664 | if (GIMME_V == G_SCALAR |
2650 | if (GIMME_V == G_SCALAR |
2665 | && expect_false (PL_stack_sp != PL_stack_base + checkmark + 1)) |
2651 | && ecb_expect_false (PL_stack_sp != PL_stack_base + checkmark + 1)) |
2666 | { |
2652 | { |
2667 | dSP; |
2653 | dSP; |
2668 | SV **bot = PL_stack_base + checkmark; |
2654 | SV **bot = PL_stack_base + checkmark; |
2669 | |
2655 | |
2670 | if (sp == bot) /* too few, push undef */ |
2656 | if (sp == bot) /* too few, push undef */ |
… | |
… | |
2784 | { |
2770 | { |
2785 | PerlIOBuf base; |
2771 | PerlIOBuf base; |
2786 | NV next, every; |
2772 | NV next, every; |
2787 | } PerlIOCede; |
2773 | } PerlIOCede; |
2788 | |
2774 | |
2789 | static IV |
2775 | static IV ecb_cold |
2790 | PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
2776 | PerlIOCede_pushed (pTHX_ PerlIO *f, const char *mode, SV *arg, PerlIO_funcs *tab) |
2791 | { |
2777 | { |
2792 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2778 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2793 | |
2779 | |
2794 | self->every = SvCUR (arg) ? SvNV (arg) : 0.01; |
2780 | self->every = SvCUR (arg) ? SvNV (arg) : 0.01; |
2795 | self->next = nvtime () + self->every; |
2781 | self->next = nvtime () + self->every; |
2796 | |
2782 | |
2797 | return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); |
2783 | return PerlIOBuf_pushed (aTHX_ f, mode, Nullsv, tab); |
2798 | } |
2784 | } |
2799 | |
2785 | |
2800 | static SV * |
2786 | static SV * ecb_cold |
2801 | PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
2787 | PerlIOCede_getarg (pTHX_ PerlIO *f, CLONE_PARAMS *param, int flags) |
2802 | { |
2788 | { |
2803 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2789 | PerlIOCede *self = PerlIOSelf (f, PerlIOCede); |
2804 | |
2790 | |
2805 | return newSVnv (self->every); |
2791 | return newSVnv (self->every); |
… | |
… | |
3217 | dSP; |
3203 | dSP; |
3218 | |
3204 | |
3219 | static SV *prio_cv; |
3205 | static SV *prio_cv; |
3220 | static SV *prio_sv; |
3206 | static SV *prio_sv; |
3221 | |
3207 | |
3222 | if (expect_false (!prio_cv)) |
3208 | if (ecb_expect_false (!prio_cv)) |
3223 | { |
3209 | { |
3224 | prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0); |
3210 | prio_cv = (SV *)get_cv ("IO::AIO::aioreq_pri", 0); |
3225 | prio_sv = newSViv (0); |
3211 | prio_sv = newSViv (0); |
3226 | } |
3212 | } |
3227 | |
3213 | |
… | |
… | |
3332 | av_push (coro->args, newSVsv (argv [i])); |
3318 | av_push (coro->args, newSVsv (argv [i])); |
3333 | } |
3319 | } |
3334 | |
3320 | |
3335 | return coro_sv; |
3321 | return coro_sv; |
3336 | } |
3322 | } |
|
|
3323 | |
|
|
3324 | #ifndef __cplusplus |
|
|
3325 | ecb_cold XS(boot_Coro__State); |
|
|
3326 | #endif |
3337 | |
3327 | |
3338 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3328 | MODULE = Coro::State PACKAGE = Coro::State PREFIX = api_ |
3339 | |
3329 | |
3340 | PROTOTYPES: DISABLE |
3330 | PROTOTYPES: DISABLE |
3341 | |
3331 | |
… | |
… | |
3654 | times (Coro::State self) |
3644 | times (Coro::State self) |
3655 | PPCODE: |
3645 | PPCODE: |
3656 | { |
3646 | { |
3657 | struct coro *current = SvSTATE (coro_current); |
3647 | struct coro *current = SvSTATE (coro_current); |
3658 | |
3648 | |
3659 | if (expect_false (current == self)) |
3649 | if (ecb_expect_false (current == self)) |
3660 | { |
3650 | { |
3661 | coro_times_update (); |
3651 | coro_times_update (); |
3662 | coro_times_add (SvSTATE (coro_current)); |
3652 | coro_times_add (SvSTATE (coro_current)); |
3663 | } |
3653 | } |
3664 | |
3654 | |
3665 | EXTEND (SP, 2); |
3655 | EXTEND (SP, 2); |
3666 | PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); |
3656 | PUSHs (sv_2mortal (newSVnv (self->t_real [0] + self->t_real [1] * 1e-9))); |
3667 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
3657 | PUSHs (sv_2mortal (newSVnv (self->t_cpu [0] + self->t_cpu [1] * 1e-9))); |
3668 | |
3658 | |
3669 | if (expect_false (current == self)) |
3659 | if (ecb_expect_false (current == self)) |
3670 | coro_times_sub (SvSTATE (coro_current)); |
3660 | coro_times_sub (SvSTATE (coro_current)); |
3671 | } |
3661 | } |
3672 | |
3662 | |
3673 | void |
3663 | void |
3674 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |
3664 | swap_sv (Coro::State coro, SV *sv, SV *swapsv) |