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